2010-12-06 03:21:09 +00:00
|
|
|
/* Copyright (c) 2009-2010, Avian Contributors
|
2009-02-16 15:21:12 +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. */
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
.text
|
|
|
|
|
|
|
|
#define BYTES_PER_WORD 4
|
|
|
|
#define LINKAGE_AREA 6
|
2009-02-17 02:49:28 +00:00
|
|
|
#define ARGUMENT_BASE BYTES_PER_WORD * LINKAGE_AREA
|
2009-02-16 15:21:12 +00:00
|
|
|
|
|
|
|
#define LOCAL(x) L##x
|
2009-05-27 01:02:39 +00:00
|
|
|
|
2009-02-16 15:21:12 +00:00
|
|
|
#ifdef __APPLE__
|
2009-05-27 01:02:39 +00:00
|
|
|
# define GLOBAL(x) _##x
|
2009-02-16 15:21:12 +00:00
|
|
|
#else
|
2009-05-27 01:02:39 +00:00
|
|
|
# define GLOBAL(x) x
|
2009-02-16 15:21:12 +00:00
|
|
|
#endif
|
2009-05-27 01:02:39 +00:00
|
|
|
|
2011-01-30 03:04:29 +00:00
|
|
|
#define THREAD_STACK 2148
|
|
|
|
#define THREAD_CONTINUATION 2156
|
2010-02-05 00:56:21 +00:00
|
|
|
#define THREAD_EXCEPTION 44
|
2011-01-30 03:04:29 +00:00
|
|
|
#define THREAD_EXCEPTION_STACK_ADJUSTMENT 2160
|
|
|
|
#define THREAD_EXCEPTION_OFFSET 2164
|
|
|
|
#define THREAD_EXCEPTION_HANDLER 2168
|
2009-05-27 01:02:39 +00:00
|
|
|
|
|
|
|
#define CONTINUATION_NEXT 4
|
|
|
|
#define CONTINUATION_ADDRESS 16
|
|
|
|
#define CONTINUATION_RETURN_ADDRESS_OFFSET 20
|
|
|
|
#define CONTINUATION_FRAME_POINTER_OFFSET 24
|
|
|
|
#define CONTINUATION_LENGTH 28
|
|
|
|
#define CONTINUATION_BODY 32
|
|
|
|
|
|
|
|
.globl GLOBAL(vmInvoke)
|
|
|
|
GLOBAL(vmInvoke):
|
2009-02-16 15:21:12 +00:00
|
|
|
// save return address
|
|
|
|
mflr r0
|
|
|
|
stw r0,8(r1)
|
|
|
|
|
|
|
|
// r3: thread
|
|
|
|
// r4: function
|
2009-02-17 02:49:28 +00:00
|
|
|
// r5: arguments
|
|
|
|
// r6: argumentFootprint
|
2009-02-16 15:21:12 +00:00
|
|
|
// r7: frameSize
|
2009-02-17 02:49:28 +00:00
|
|
|
// r8: returnType
|
2009-02-16 15:21:12 +00:00
|
|
|
|
|
|
|
// r9: temporary
|
|
|
|
|
2009-02-17 02:49:28 +00:00
|
|
|
// save return type
|
|
|
|
stw r8,44(r1)
|
|
|
|
|
2009-02-16 15:21:12 +00:00
|
|
|
// allocate stack space, adding room for callee-saved registers
|
2009-02-25 01:28:05 +00:00
|
|
|
subfic r9,r7,-80
|
2009-02-16 15:21:12 +00:00
|
|
|
stwux r1,r1,r9
|
|
|
|
|
|
|
|
// save callee-saved registers
|
|
|
|
add r9,r7,r1
|
|
|
|
|
|
|
|
stw r13,0(r9)
|
|
|
|
stw r14,4(r9)
|
|
|
|
stw r15,8(r9)
|
|
|
|
stw r16,12(r9)
|
|
|
|
stw r17,16(r9)
|
|
|
|
stw r18,20(r9)
|
|
|
|
stw r19,24(r9)
|
|
|
|
stw r20,28(r9)
|
|
|
|
stw r21,32(r9)
|
|
|
|
stw r22,36(r9)
|
|
|
|
stw r23,40(r9)
|
|
|
|
stw r24,44(r9)
|
|
|
|
stw r25,48(r9)
|
|
|
|
stw r26,52(r9)
|
|
|
|
stw r27,56(r9)
|
|
|
|
stw r28,60(r9)
|
|
|
|
stw r29,64(r9)
|
|
|
|
stw r30,68(r9)
|
|
|
|
stw r31,72(r9)
|
|
|
|
|
|
|
|
// we use r13 to hold the thread pointer, by convention
|
|
|
|
mr r13,r3
|
|
|
|
|
|
|
|
// copy arguments into place
|
|
|
|
li r16,0
|
2009-05-29 00:56:05 +00:00
|
|
|
addi r18,r1,ARGUMENT_BASE
|
2009-05-27 01:02:39 +00:00
|
|
|
b LOCAL(vmInvoke_argumentTest)
|
2009-02-16 15:21:12 +00:00
|
|
|
|
2009-05-27 01:02:39 +00:00
|
|
|
LOCAL(vmInvoke_argumentLoop):
|
2009-02-16 15:21:12 +00:00
|
|
|
lwzx r17,r16,r5
|
2009-05-29 00:56:05 +00:00
|
|
|
stwx r17,r16,r18
|
2009-02-16 15:21:12 +00:00
|
|
|
addi r16,r16,BYTES_PER_WORD
|
|
|
|
|
2009-05-27 01:02:39 +00:00
|
|
|
LOCAL(vmInvoke_argumentTest):
|
2009-02-16 15:21:12 +00:00
|
|
|
cmplw r16,r6
|
2009-05-27 01:02:39 +00:00
|
|
|
blt LOCAL(vmInvoke_argumentLoop)
|
2009-02-16 15:21:12 +00:00
|
|
|
|
|
|
|
// load and call function address
|
|
|
|
mtctr r4
|
|
|
|
bctrl
|
2009-05-29 00:56:05 +00:00
|
|
|
|
2010-06-25 01:35:07 +00:00
|
|
|
.globl GLOBAL(vmInvoke_returnAddress)
|
|
|
|
GLOBAL(vmInvoke_returnAddress):
|
2009-02-16 15:21:12 +00:00
|
|
|
// restore stack pointer
|
|
|
|
lwz r1,0(r1)
|
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
|
|
|
|
|
|
|
// clear MyThread::stack to avoid confusing another thread calling
|
|
|
|
// java.lang.Thread.getStackTrace on this one. See
|
|
|
|
// MyProcess::getStackTrace in compile.cpp for details on how we get
|
|
|
|
// a reliable stack trace from a thread that might be interrupted at
|
|
|
|
// any point in its execution.
|
|
|
|
li r5,0
|
|
|
|
stw r5,THREAD_STACK(r13)
|
|
|
|
|
|
|
|
.globl GLOBAL(vmInvoke_safeStack)
|
|
|
|
GLOBAL(vmInvoke_safeStack):
|
2009-05-27 01:02:39 +00:00
|
|
|
|
|
|
|
#ifdef AVIAN_CONTINUATIONS
|
|
|
|
// call the next continuation, if any
|
|
|
|
lwz r5,THREAD_CONTINUATION(r13)
|
|
|
|
cmplwi r5,0
|
|
|
|
beq LOCAL(vmInvoke_exit)
|
|
|
|
|
|
|
|
lwz r6,CONTINUATION_LENGTH(r5)
|
|
|
|
slwi r6,r6,2
|
2009-05-29 00:56:05 +00:00
|
|
|
subfic r7,r6,-80
|
|
|
|
stwux r1,r1,r7
|
2009-05-27 01:02:39 +00:00
|
|
|
|
|
|
|
addi r7,r5,CONTINUATION_BODY
|
|
|
|
|
|
|
|
li r8,0
|
2009-05-29 00:56:05 +00:00
|
|
|
addi r10,r1,ARGUMENT_BASE
|
2009-05-27 01:02:39 +00:00
|
|
|
b LOCAL(vmInvoke_continuationTest)
|
|
|
|
|
|
|
|
LOCAL(vmInvoke_continuationLoop):
|
|
|
|
lwzx r9,r7,r8
|
2009-05-29 00:56:05 +00:00
|
|
|
stwx r9,r10,r8
|
2009-05-27 01:02:39 +00:00
|
|
|
addi r8,r8,4
|
|
|
|
|
|
|
|
LOCAL(vmInvoke_continuationTest):
|
|
|
|
cmplw r8,r6
|
|
|
|
ble LOCAL(vmInvoke_continuationLoop)
|
|
|
|
|
|
|
|
lwz r7,CONTINUATION_RETURN_ADDRESS_OFFSET(r5)
|
|
|
|
bl LOCAL(vmInvoke_getPC)
|
|
|
|
|
|
|
|
LOCAL(vmInvoke_getPC):
|
|
|
|
mflr r10
|
2010-06-25 01:35:07 +00:00
|
|
|
la r10,lo16(GLOBAL(vmInvoke_returnAddress)-LOCAL(vmInvoke_getPC))(r10)
|
2009-05-27 01:02:39 +00:00
|
|
|
stwx r10,r1,r7
|
|
|
|
|
|
|
|
lwz r7,CONTINUATION_FRAME_POINTER_OFFSET(r5)
|
|
|
|
lwz r8,0(r1)
|
|
|
|
add r7,r7,r1
|
|
|
|
stw r8,0(r7)
|
|
|
|
stw r7,0(r1)
|
|
|
|
|
|
|
|
lwz r7,CONTINUATION_NEXT(r5)
|
|
|
|
stw r7,THREAD_CONTINUATION(r13)
|
|
|
|
|
|
|
|
// call the continuation unless we're handling an exception
|
|
|
|
lwz r7,THREAD_EXCEPTION(r13)
|
|
|
|
cmpwi r7,0
|
|
|
|
bne LOCAL(vmInvoke_handleException)
|
2009-05-29 00:56:05 +00:00
|
|
|
lwz r7,CONTINUATION_ADDRESS(r5)
|
2009-05-27 01:02:39 +00:00
|
|
|
mtctr r7
|
|
|
|
bctr
|
|
|
|
|
|
|
|
LOCAL(vmInvoke_handleException):
|
|
|
|
// we're handling an exception - call the exception handler instead
|
|
|
|
li r8,0
|
|
|
|
stw r8,THREAD_EXCEPTION(r13)
|
|
|
|
lwz r8,THREAD_EXCEPTION_STACK_ADJUSTMENT(r13)
|
|
|
|
lwz r9,0(r1)
|
|
|
|
subfic r8,r8,0
|
|
|
|
stwux r9,r1,r8
|
|
|
|
lwz r8,THREAD_EXCEPTION_OFFSET(r13)
|
|
|
|
stwx r7,r1,r8
|
|
|
|
|
2009-05-29 00:56:05 +00:00
|
|
|
lwz r7,THREAD_EXCEPTION_HANDLER(r13)
|
2009-05-27 01:02:39 +00:00
|
|
|
mtctr r7
|
|
|
|
bctr
|
|
|
|
|
|
|
|
LOCAL(vmInvoke_exit):
|
|
|
|
#endif // AVIAN_CONTINUATIONS
|
2009-02-16 15:21:12 +00:00
|
|
|
|
|
|
|
// restore callee-saved registers
|
|
|
|
subi r9,r1,80
|
|
|
|
|
|
|
|
lwz r13,0(r9)
|
|
|
|
lwz r14,4(r9)
|
|
|
|
lwz r15,8(r9)
|
|
|
|
lwz r16,12(r9)
|
|
|
|
lwz r17,16(r9)
|
|
|
|
lwz r18,20(r9)
|
|
|
|
lwz r19,24(r9)
|
|
|
|
lwz r20,28(r9)
|
|
|
|
lwz r21,32(r9)
|
|
|
|
lwz r22,36(r9)
|
|
|
|
lwz r23,40(r9)
|
|
|
|
lwz r24,44(r9)
|
|
|
|
lwz r25,48(r9)
|
|
|
|
lwz r26,52(r9)
|
|
|
|
lwz r27,56(r9)
|
|
|
|
lwz r28,60(r9)
|
|
|
|
lwz r29,64(r9)
|
|
|
|
lwz r30,68(r9)
|
|
|
|
lwz r31,72(r9)
|
|
|
|
|
2009-02-17 02:49:28 +00:00
|
|
|
// handle return value based on expected type
|
|
|
|
lwz r8,44(r1)
|
|
|
|
|
2009-05-27 01:02:39 +00:00
|
|
|
LOCAL(vmInvoke_return):
|
2009-02-16 15:21:12 +00:00
|
|
|
// load return address
|
|
|
|
lwz r0,8(r1)
|
|
|
|
mtlr r0
|
|
|
|
|
|
|
|
// return
|
|
|
|
blr
|
2009-05-27 01:02:39 +00:00
|
|
|
|
|
|
|
.globl GLOBAL(vmJumpAndInvoke)
|
|
|
|
GLOBAL(vmJumpAndInvoke):
|
2009-05-29 00:56:05 +00:00
|
|
|
#ifdef AVIAN_CONTINUATIONS
|
2009-05-27 01:02:39 +00:00
|
|
|
// r3: thread
|
|
|
|
// r4: address
|
2011-01-31 02:11:23 +00:00
|
|
|
// r5: stack
|
|
|
|
// r6: argumentFootprint
|
|
|
|
// r7: arguments
|
|
|
|
// r8: frameSize
|
2009-05-27 01:02:39 +00:00
|
|
|
|
2009-05-29 00:56:05 +00:00
|
|
|
// restore (pseudo)-stack pointer (we don't want to touch the real
|
|
|
|
// stack pointer, since we haven't copied the arguments yet)
|
2011-01-31 02:11:23 +00:00
|
|
|
lwz r5,0(r5)
|
2009-05-29 00:56:05 +00:00
|
|
|
|
2011-01-31 02:11:23 +00:00
|
|
|
// make everything between r1 and r5 one big stack frame while we
|
2009-05-29 00:56:05 +00:00
|
|
|
// shuffle things around
|
2011-01-31 02:11:23 +00:00
|
|
|
stw r5,0(r1)
|
2009-05-29 00:56:05 +00:00
|
|
|
|
|
|
|
// allocate new frame, adding room for callee-saved registers
|
2011-01-31 02:11:23 +00:00
|
|
|
subfic r10,r8,-80
|
|
|
|
stwux r5,r5,r10
|
2009-05-27 01:02:39 +00:00
|
|
|
|
|
|
|
mr r13,r3
|
|
|
|
|
|
|
|
// copy arguments into place
|
2011-01-31 02:11:23 +00:00
|
|
|
li r8,0
|
|
|
|
addi r11,r5,ARGUMENT_BASE
|
2009-05-27 01:02:39 +00:00
|
|
|
b LOCAL(vmJumpAndInvoke_argumentTest)
|
|
|
|
|
|
|
|
LOCAL(vmJumpAndInvoke_argumentLoop):
|
2011-01-31 02:11:23 +00:00
|
|
|
lwzx r12,r7,r8
|
|
|
|
stwx r12,r11,r8
|
|
|
|
addi r8,r8,4
|
2009-05-27 01:02:39 +00:00
|
|
|
|
|
|
|
LOCAL(vmJumpAndInvoke_argumentTest):
|
2011-01-31 02:11:23 +00:00
|
|
|
cmplw r8,r6
|
2009-05-27 01:02:39 +00:00
|
|
|
ble LOCAL(vmJumpAndInvoke_argumentLoop)
|
|
|
|
|
2009-05-29 00:56:05 +00:00
|
|
|
// the arguments have been copied, so we can set the real stack
|
|
|
|
// pointer now
|
2011-01-31 02:11:23 +00:00
|
|
|
mr r1,r5
|
2009-05-27 01:02:39 +00:00
|
|
|
|
2009-05-29 00:56:05 +00:00
|
|
|
// set return address to vmInvoke_returnAddress
|
2009-05-27 01:02:39 +00:00
|
|
|
bl LOCAL(vmJumpAndInvoke_getPC)
|
|
|
|
|
|
|
|
LOCAL(vmJumpAndInvoke_getPC):
|
|
|
|
mflr r10
|
2010-06-25 01:35:07 +00:00
|
|
|
la r10,lo16(GLOBAL(vmInvoke_returnAddress)-LOCAL(vmJumpAndInvoke_getPC))(r10)
|
2009-05-27 01:02:39 +00:00
|
|
|
mtlr r10
|
|
|
|
|
|
|
|
mtctr r4
|
|
|
|
bctr
|
2009-05-29 00:56:05 +00:00
|
|
|
#else // not AVIAN_CONTINUATIONS
|
|
|
|
// vmJumpAndInvoke should only be called when continuations are
|
|
|
|
// enabled
|
|
|
|
trap
|
|
|
|
#endif // not AVIAN_CONTINUATIONS
|