2010-12-06 03:21:09 +00:00
|
|
|
/* Copyright (c) 2008-2010, Avian Contributors
|
2008-11-23 23:58:01 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#ifndef BOOTIMAGE_H
|
|
|
|
#define BOOTIMAGE_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2008-11-21 23:20:35 +00:00
|
|
|
namespace vm {
|
|
|
|
|
2008-12-02 02:38:00 +00:00
|
|
|
const unsigned BootMask = (~static_cast<unsigned>(0)) / BytesPerWord;
|
|
|
|
|
|
|
|
const unsigned BootShift = 32 - log(BytesPerWord);
|
|
|
|
|
2009-03-11 01:08:16 +00:00
|
|
|
const unsigned BootFlatConstant = 1 << BootShift;
|
|
|
|
const unsigned BootHeapOffset = 1 << (BootShift + 1);
|
|
|
|
|
2008-11-21 23:20:35 +00:00
|
|
|
class BootImage {
|
|
|
|
public:
|
fix Thread.getStackTrace race conditions
Implementing Thread.getStackTrace is tricky. A thread may interrupt
another thread at any time to grab a stack trace, including while the
latter is executing Java code, JNI code, helper thunks, VM code, or
while transitioning between any of these.
To create a stack trace we use several context fields associated with
the target thread, including snapshots of the instruction pointer,
stack pointer, and frame pointer. These fields must be current,
accurate, and consistent with each other in order to get a reliable
trace. Otherwise, we risk crashing the VM by trying to walk garbage
stack frames or by misinterpreting the size and/or content of
legitimate frames.
This commit addresses sensitive transition points such as entering the
helper thunks which bridge the transitions from Java to native code
(where we must save the stack and frame registers for use from native
code) and stack unwinding (where we must atomically update the thread
context fields to indicate which frame we are unwinding to). When
grabbing a trace for another thread, we determine what kind of code we
caught the thread executing in and use that information to choose the
thread context values with which to begin the trace. See
MyProcessor::getStackTrace::Visitor::visit for details.
In order to atomically update the thread context fields, we do the
following:
1. Create a temporary "transition" object to serve as a staging area
and populate it with the new field values.
2. Update a transition pointer in the thread object to point to the
object created above. As long as this pointer is non-null,
interrupting threads will use the context values in the staging
object instead of those in the thread object.
3. Update the fields in the thread object.
4. Clear the transition pointer in the thread object.
We use a memory barrier between each of these steps to ensure they are
made visible to other threads in program order. See
MyThread::doTransition for details.
2010-06-16 01:10:48 +00:00
|
|
|
class Thunk {
|
|
|
|
public:
|
|
|
|
Thunk():
|
|
|
|
start(0), frameSavedOffset(0), length(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Thunk(unsigned start, unsigned frameSavedOffset, unsigned length):
|
|
|
|
start(start), frameSavedOffset(frameSavedOffset), length(length)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
unsigned start;
|
|
|
|
unsigned frameSavedOffset;
|
|
|
|
unsigned length;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThunkCollection {
|
|
|
|
public:
|
|
|
|
Thunk default_;
|
|
|
|
Thunk defaultVirtual;
|
|
|
|
Thunk native;
|
|
|
|
Thunk aioob;
|
2010-12-19 22:23:19 +00:00
|
|
|
Thunk stackOverflow;
|
fix Thread.getStackTrace race conditions
Implementing Thread.getStackTrace is tricky. A thread may interrupt
another thread at any time to grab a stack trace, including while the
latter is executing Java code, JNI code, helper thunks, VM code, or
while transitioning between any of these.
To create a stack trace we use several context fields associated with
the target thread, including snapshots of the instruction pointer,
stack pointer, and frame pointer. These fields must be current,
accurate, and consistent with each other in order to get a reliable
trace. Otherwise, we risk crashing the VM by trying to walk garbage
stack frames or by misinterpreting the size and/or content of
legitimate frames.
This commit addresses sensitive transition points such as entering the
helper thunks which bridge the transitions from Java to native code
(where we must save the stack and frame registers for use from native
code) and stack unwinding (where we must atomically update the thread
context fields to indicate which frame we are unwinding to). When
grabbing a trace for another thread, we determine what kind of code we
caught the thread executing in and use that information to choose the
thread context values with which to begin the trace. See
MyProcessor::getStackTrace::Visitor::visit for details.
In order to atomically update the thread context fields, we do the
following:
1. Create a temporary "transition" object to serve as a staging area
and populate it with the new field values.
2. Update a transition pointer in the thread object to point to the
object created above. As long as this pointer is non-null,
interrupting threads will use the context values in the staging
object instead of those in the thread object.
3. Update the fields in the thread object.
4. Clear the transition pointer in the thread object.
We use a memory barrier between each of these steps to ensure they are
made visible to other threads in program order. See
MyThread::doTransition for details.
2010-06-16 01:10:48 +00:00
|
|
|
Thunk table;
|
|
|
|
};
|
|
|
|
|
2008-11-21 23:20:35 +00:00
|
|
|
static const unsigned Magic = 0x22377322;
|
|
|
|
|
|
|
|
unsigned magic;
|
|
|
|
|
|
|
|
unsigned heapSize;
|
|
|
|
unsigned codeSize;
|
|
|
|
|
2010-09-14 16:49:41 +00:00
|
|
|
unsigned bootClassCount;
|
|
|
|
unsigned appClassCount;
|
2008-12-02 02:38:00 +00:00
|
|
|
unsigned stringCount;
|
|
|
|
unsigned callCount;
|
|
|
|
|
2010-09-14 16:49:41 +00:00
|
|
|
unsigned bootLoader;
|
|
|
|
unsigned appLoader;
|
2008-11-21 23:20:35 +00:00
|
|
|
unsigned types;
|
2008-11-23 23:58:01 +00:00
|
|
|
unsigned methodTree;
|
|
|
|
unsigned methodTreeSentinal;
|
2009-04-05 21:42:10 +00:00
|
|
|
unsigned virtualThunks;
|
2008-11-28 22:02:45 +00:00
|
|
|
|
|
|
|
uintptr_t codeBase;
|
2008-11-21 23:20:35 +00:00
|
|
|
|
fix Thread.getStackTrace race conditions
Implementing Thread.getStackTrace is tricky. A thread may interrupt
another thread at any time to grab a stack trace, including while the
latter is executing Java code, JNI code, helper thunks, VM code, or
while transitioning between any of these.
To create a stack trace we use several context fields associated with
the target thread, including snapshots of the instruction pointer,
stack pointer, and frame pointer. These fields must be current,
accurate, and consistent with each other in order to get a reliable
trace. Otherwise, we risk crashing the VM by trying to walk garbage
stack frames or by misinterpreting the size and/or content of
legitimate frames.
This commit addresses sensitive transition points such as entering the
helper thunks which bridge the transitions from Java to native code
(where we must save the stack and frame registers for use from native
code) and stack unwinding (where we must atomically update the thread
context fields to indicate which frame we are unwinding to). When
grabbing a trace for another thread, we determine what kind of code we
caught the thread executing in and use that information to choose the
thread context values with which to begin the trace. See
MyProcessor::getStackTrace::Visitor::visit for details.
In order to atomically update the thread context fields, we do the
following:
1. Create a temporary "transition" object to serve as a staging area
and populate it with the new field values.
2. Update a transition pointer in the thread object to point to the
object created above. As long as this pointer is non-null,
interrupting threads will use the context values in the staging
object instead of those in the thread object.
3. Update the fields in the thread object.
4. Clear the transition pointer in the thread object.
We use a memory barrier between each of these steps to ensure they are
made visible to other threads in program order. See
MyThread::doTransition for details.
2010-06-16 01:10:48 +00:00
|
|
|
ThunkCollection thunks;
|
2008-11-28 22:02:45 +00:00
|
|
|
|
|
|
|
unsigned compileMethodCall;
|
2009-04-07 00:34:12 +00:00
|
|
|
unsigned compileVirtualMethodCall;
|
2008-11-28 22:02:45 +00:00
|
|
|
unsigned invokeNativeCall;
|
|
|
|
unsigned throwArrayIndexOutOfBoundsCall;
|
2010-12-19 22:23:19 +00:00
|
|
|
unsigned throwStackOverflowCall;
|
2008-11-28 22:02:45 +00:00
|
|
|
|
|
|
|
#define THUNK(s) unsigned s##Call;
|
2008-11-21 23:20:35 +00:00
|
|
|
#include "thunks.cpp"
|
|
|
|
#undef THUNK
|
|
|
|
};
|
|
|
|
|
2008-11-28 22:02:45 +00:00
|
|
|
inline unsigned
|
|
|
|
codeMapSize(unsigned codeSize)
|
|
|
|
{
|
|
|
|
return ceiling(codeSize, BitsPerWord) * BytesPerWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned
|
|
|
|
heapMapSize(unsigned heapSize)
|
|
|
|
{
|
2008-11-29 23:08:14 +00:00
|
|
|
return ceiling(heapSize, BitsPerWord * BytesPerWord) * BytesPerWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline object
|
|
|
|
bootObject(uintptr_t* heap, unsigned offset)
|
|
|
|
{
|
|
|
|
if (offset) {
|
|
|
|
return reinterpret_cast<object>(heap + offset - 1);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2008-11-28 22:02:45 +00:00
|
|
|
}
|
|
|
|
|
2008-11-21 23:20:35 +00:00
|
|
|
} // namespace vm
|
2008-11-23 23:58:01 +00:00
|
|
|
|
|
|
|
#endif//BOOTIMAGE_H
|