2010-12-06 03:21:09 +00:00
|
|
|
/* Copyright (c) 2008-2010, Avian Contributors
|
2008-02-19 18:06:52 +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. */
|
|
|
|
|
2007-10-23 01:00:57 +00:00
|
|
|
#ifndef X86_H
|
|
|
|
#define X86_H
|
|
|
|
|
|
|
|
#include "types.h"
|
2008-01-01 17:08:47 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
2009-08-27 00:26:44 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# include "windows.h"
|
2009-12-13 01:50:59 +00:00
|
|
|
# pragma push_macro("assert")
|
|
|
|
# include "intrin.h"
|
|
|
|
# pragma pop_macro("assert")
|
2009-12-02 15:49:10 +00:00
|
|
|
# undef interface
|
2009-08-27 00:26:44 +00:00
|
|
|
#endif
|
|
|
|
|
2011-02-07 23:28:17 +00:00
|
|
|
#if (defined ARCH_x86_32) || (defined PLATFORM_WINDOWS)
|
2011-02-19 21:20:02 +00:00
|
|
|
# define VA_LIST(x) (&(x))
|
2011-02-02 22:11:34 +00:00
|
|
|
#else
|
|
|
|
# define VA_LIST(x) (x)
|
|
|
|
#endif
|
support stack unwinding without using a frame pointer
Previously, we unwound the stack by following the chain of frame
pointers for normal returns, stack trace creation, and exception
unwinding. On x86, this required reserving EBP/RBP for frame pointer
duties, making it unavailable for general computation and requiring
that it be explicitly saved and restored on entry and exit,
respectively.
On PowerPC, we use an ABI that makes the stack pointer double as a
frame pointer, so it doesn't cost us anything. We've been using the
same convention on ARM, but it doesn't match the native calling
convention, which makes it unusable when we want to call native code
from Java and pass arguments on the stack.
So far, the ARM calling convention mismatch hasn't been an issue
because we've never passed more arguments from Java to native code
than would fit in registers. However, we must now pass an extra
argument (the thread pointer) to e.g. divideLong so it can throw an
exception on divide by zero, which means the last argument must be
passed on the stack. This will clobber the linkage area we've been
using to hold the frame pointer, so we need to stop using it.
One solution would be to use the same convention on ARM as we do on
x86, but this would introduce the same overhead of making a register
unavailable for general use and extra code at method entry and exit.
Instead, this commit removes the need for a frame pointer. Unwinding
involves consulting a map of instruction offsets to frame sizes which
is generated at compile time. This is necessary because stack trace
creation can happen at any time due to Thread.getStackTrace being
called by another thread, and the frame size varies during the
execution of a method.
So far, only x86(_64) is working, and continuations and tail call
optimization are probably broken. More to come.
2011-01-17 02:05:05 +00:00
|
|
|
|
2011-02-19 21:20:02 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
# include "mach/mach_types.h"
|
|
|
|
# include "mach/i386/thread_act.h"
|
|
|
|
# include "mach/i386/thread_status.h"
|
|
|
|
|
|
|
|
# if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
|
|
|
|
# define FIELD(x) __##x
|
|
|
|
# else
|
|
|
|
# define FIELD(x) x
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2009-08-27 00:26:44 +00:00
|
|
|
#ifdef ARCH_x86_32
|
2007-10-23 01:00:57 +00:00
|
|
|
|
2008-06-04 22:21:27 +00:00
|
|
|
# ifdef __APPLE__
|
2011-02-19 21:20:02 +00:00
|
|
|
# define THREAD_STATE x86_THREAD_STATE32
|
|
|
|
# define THREAD_STATE_TYPE x86_thread_state32_t
|
|
|
|
# define THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
|
|
|
|
|
|
|
|
# define THREAD_STATE_IP(state) ((state).FIELD(eip))
|
|
|
|
# define THREAD_STATE_STACK(state) ((state).FIELD(esp))
|
|
|
|
# define THREAD_STATE_THREAD(state) ((state).FIELD(ebx))
|
|
|
|
# define THREAD_STATE_LINK(state) ((state).FIELD(ecx))
|
|
|
|
# define THREAD_STATE_FRAME(state) ((state).FIELD(ebp))
|
|
|
|
|
|
|
|
# define IP_REGISTER(context) \
|
|
|
|
THREAD_STATE_IP(context->uc_mcontext->FIELD(ss))
|
|
|
|
# define STACK_REGISTER(context) \
|
|
|
|
THREAD_STATE_STACK(context->uc_mcontext->FIELD(ss))
|
|
|
|
# define THREAD_REGISTER(context) \
|
|
|
|
THREAD_STATE_THREAD(context->uc_mcontext->FIELD(ss))
|
|
|
|
# define LINK_REGISTER(context) \
|
|
|
|
THREAD_STATE_LINK(context->uc_mcontext->FIELD(ss))
|
|
|
|
# define FRAME_REGISTER(context) \
|
|
|
|
THREAD_STATE_FRAME(context->uc_mcontext->FIELD(ss))
|
|
|
|
|
2008-06-04 22:21:27 +00:00
|
|
|
# else
|
|
|
|
# define IP_REGISTER(context) (context->uc_mcontext.gregs[REG_EIP])
|
|
|
|
# define STACK_REGISTER(context) (context->uc_mcontext.gregs[REG_ESP])
|
|
|
|
# define THREAD_REGISTER(context) (context->uc_mcontext.gregs[REG_EBX])
|
2011-01-26 00:22:43 +00:00
|
|
|
# define LINK_REGISTER(context) (context->uc_mcontext.gregs[REG_ECX])
|
2011-01-28 04:06:01 +00:00
|
|
|
# define FRAME_REGISTER(context) (context->uc_mcontext.gregs[REG_EBP])
|
2008-06-04 22:21:27 +00:00
|
|
|
# endif
|
|
|
|
|
2007-10-23 01:00:57 +00:00
|
|
|
extern "C" uint64_t
|
2007-10-24 17:24:19 +00:00
|
|
|
vmNativeCall(void* function, void* stack, unsigned stackSize,
|
|
|
|
unsigned returnType);
|
2007-10-23 01:00:57 +00:00
|
|
|
|
|
|
|
namespace vm {
|
|
|
|
|
|
|
|
inline uint64_t
|
|
|
|
dynamicCall(void* function, uintptr_t* arguments, uint8_t*,
|
|
|
|
unsigned, unsigned argumentsSize, unsigned returnType)
|
|
|
|
{
|
2007-10-24 17:24:19 +00:00
|
|
|
return vmNativeCall(function, arguments, argumentsSize, returnType);
|
2007-10-23 01:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace vm
|
|
|
|
|
2009-08-27 00:26:44 +00:00
|
|
|
#elif defined ARCH_x86_64
|
2007-10-23 01:00:57 +00:00
|
|
|
|
2009-10-14 16:01:37 +00:00
|
|
|
# ifdef __APPLE__
|
2011-02-19 21:20:02 +00:00
|
|
|
# define THREAD_STATE x86_THREAD_STATE64
|
|
|
|
# define THREAD_STATE_TYPE x86_thread_state64_t
|
|
|
|
# define THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
|
|
|
|
|
|
|
|
# define THREAD_STATE_IP(state) ((state).FIELD(rip))
|
|
|
|
# define THREAD_STATE_STACK(state) ((state).FIELD(rsp))
|
|
|
|
# define THREAD_STATE_THREAD(state) ((state).FIELD(rbx))
|
|
|
|
# define THREAD_STATE_LINK(state) ((state).FIELD(rcx))
|
|
|
|
# define THREAD_STATE_FRAME(state) ((state).FIELD(rbp))
|
|
|
|
|
|
|
|
# define IP_REGISTER(context) \
|
|
|
|
THREAD_STATE_IP(context->uc_mcontext->FIELD(ss))
|
|
|
|
# define STACK_REGISTER(context) \
|
|
|
|
THREAD_STATE_STACK(context->uc_mcontext->FIELD(ss))
|
|
|
|
# define THREAD_REGISTER(context) \
|
|
|
|
THREAD_STATE_THREAD(context->uc_mcontext->FIELD(ss))
|
|
|
|
# define LINK_REGISTER(context) \
|
|
|
|
THREAD_STATE_LINK(context->uc_mcontext->FIELD(ss))
|
|
|
|
# define FRAME_REGISTER(context) \
|
|
|
|
THREAD_STATE_FRAME(context->uc_mcontext->FIELD(ss))
|
|
|
|
|
2009-10-14 16:01:37 +00:00
|
|
|
# else
|
|
|
|
# define IP_REGISTER(context) (context->uc_mcontext.gregs[REG_RIP])
|
|
|
|
# define STACK_REGISTER(context) (context->uc_mcontext.gregs[REG_RSP])
|
|
|
|
# define THREAD_REGISTER(context) (context->uc_mcontext.gregs[REG_RBX])
|
2011-01-26 00:22:43 +00:00
|
|
|
# define LINK_REGISTER(context) (context->uc_mcontext.gregs[REG_RCX])
|
2011-01-28 04:06:01 +00:00
|
|
|
# define FRAME_REGISTER(context) (context->uc_mcontext.gregs[REG_RBP])
|
2009-10-14 16:01:37 +00:00
|
|
|
# endif
|
2008-06-04 22:21:27 +00:00
|
|
|
|
2007-10-23 01:00:57 +00:00
|
|
|
extern "C" uint64_t
|
2009-08-27 00:26:44 +00:00
|
|
|
# ifdef PLATFORM_WINDOWS
|
2009-06-11 15:39:46 +00:00
|
|
|
vmNativeCall(void* function, void* stack, unsigned stackSize,
|
|
|
|
unsigned returnType);
|
2009-07-26 02:48:36 +00:00
|
|
|
# else
|
2007-10-24 17:24:19 +00:00
|
|
|
vmNativeCall(void* function, void* stack, unsigned stackSize,
|
|
|
|
void* gprTable, void* sseTable, unsigned returnType);
|
2009-07-26 02:48:36 +00:00
|
|
|
# endif
|
2007-10-23 01:00:57 +00:00
|
|
|
|
|
|
|
namespace vm {
|
|
|
|
|
2009-08-27 00:26:44 +00:00
|
|
|
# ifdef PLATFORM_WINDOWS
|
2009-06-11 15:39:46 +00:00
|
|
|
inline uint64_t
|
|
|
|
dynamicCall(void* function, uint64_t* arguments, UNUSED uint8_t* argumentTypes,
|
|
|
|
unsigned argumentCount, unsigned, unsigned returnType)
|
|
|
|
{
|
|
|
|
return vmNativeCall(function, arguments, argumentCount, returnType);
|
|
|
|
}
|
2009-07-26 02:48:36 +00:00
|
|
|
# else
|
2007-10-23 01:00:57 +00:00
|
|
|
inline uint64_t
|
2009-10-14 16:01:37 +00:00
|
|
|
dynamicCall(void* function, uintptr_t* arguments, uint8_t* argumentTypes,
|
2007-10-23 01:00:57 +00:00
|
|
|
unsigned argumentCount, unsigned, unsigned returnType)
|
|
|
|
{
|
|
|
|
const unsigned GprCount = 6;
|
|
|
|
uint64_t gprTable[GprCount];
|
|
|
|
unsigned gprIndex = 0;
|
|
|
|
|
|
|
|
const unsigned SseCount = 8;
|
|
|
|
uint64_t sseTable[SseCount];
|
|
|
|
unsigned sseIndex = 0;
|
|
|
|
|
|
|
|
uint64_t stack[argumentCount];
|
|
|
|
unsigned stackIndex = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < argumentCount; ++i) {
|
|
|
|
switch (argumentTypes[i]) {
|
|
|
|
case FLOAT_TYPE:
|
|
|
|
case DOUBLE_TYPE: {
|
|
|
|
if (sseIndex < SseCount) {
|
|
|
|
sseTable[sseIndex++] = arguments[i];
|
|
|
|
} else {
|
|
|
|
stack[stackIndex++] = arguments[i];
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
if (gprIndex < GprCount) {
|
|
|
|
gprTable[gprIndex++] = arguments[i];
|
|
|
|
} else {
|
|
|
|
stack[stackIndex++] = arguments[i];
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-04 22:21:27 +00:00
|
|
|
return vmNativeCall(function, stack, stackIndex * BytesPerWord,
|
2007-10-24 17:24:19 +00:00
|
|
|
(gprIndex ? gprTable : 0),
|
|
|
|
(sseIndex ? sseTable : 0), returnType);
|
2007-10-23 01:00:57 +00:00
|
|
|
}
|
2009-07-26 02:48:36 +00:00
|
|
|
#endif
|
2007-10-23 01:00:57 +00:00
|
|
|
|
|
|
|
} // namespace vm
|
|
|
|
|
|
|
|
#else
|
2008-06-04 22:21:27 +00:00
|
|
|
# error unsupported architecture
|
2007-10-23 01:00:57 +00:00
|
|
|
#endif
|
|
|
|
|
2009-03-09 15:29:37 +00:00
|
|
|
namespace vm {
|
|
|
|
|
|
|
|
inline void
|
|
|
|
trap()
|
|
|
|
{
|
2009-08-27 00:26:44 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
__asm int 3
|
|
|
|
#else
|
2009-03-09 15:29:37 +00:00
|
|
|
asm("int3");
|
2009-08-27 00:26:44 +00:00
|
|
|
#endif
|
2009-03-09 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2009-12-02 15:49:10 +00:00
|
|
|
programOrderMemoryBarrier()
|
2009-03-09 15:29:37 +00:00
|
|
|
{
|
2010-02-05 01:30:13 +00:00
|
|
|
compileTimeMemoryBarrier();
|
2009-03-09 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
storeStoreMemoryBarrier()
|
|
|
|
{
|
2009-12-02 15:49:10 +00:00
|
|
|
programOrderMemoryBarrier();
|
2009-03-09 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
storeLoadMemoryBarrier()
|
|
|
|
{
|
2009-12-02 15:49:10 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
MemoryBarrier();
|
|
|
|
#elif defined ARCH_x86_32
|
|
|
|
__asm__ __volatile__("lock; addl $0,0(%%esp)": : :"memory");
|
|
|
|
#elif defined ARCH_x86_64
|
|
|
|
__asm__ __volatile__("mfence": : :"memory");
|
|
|
|
#endif // ARCH_x86_64
|
2009-03-09 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
loadMemoryBarrier()
|
|
|
|
{
|
2009-12-02 15:49:10 +00:00
|
|
|
programOrderMemoryBarrier();
|
2009-03-09 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
syncInstructionCache(const void*, unsigned)
|
|
|
|
{
|
2009-12-02 15:49:10 +00:00
|
|
|
programOrderMemoryBarrier();
|
2009-03-09 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
2009-11-20 17:40:01 +00:00
|
|
|
#ifdef USE_ATOMIC_OPERATIONS
|
|
|
|
inline bool
|
2009-11-28 22:01:54 +00:00
|
|
|
atomicCompareAndSwap32(uint32_t* p, uint32_t old, uint32_t new_)
|
2009-11-20 17:40:01 +00:00
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
2010-04-15 17:11:10 +00:00
|
|
|
return old == InterlockedCompareExchange
|
|
|
|
(reinterpret_cast<LONG*>(p), new_, old);
|
2009-11-20 17:40:01 +00:00
|
|
|
#elif (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 1)
|
|
|
|
return __sync_bool_compare_and_swap(p, old, new_);
|
2009-11-28 22:01:54 +00:00
|
|
|
#else
|
2009-11-20 17:40:01 +00:00
|
|
|
uint8_t result;
|
|
|
|
|
|
|
|
__asm__ __volatile__("lock; cmpxchgl %2, %0; setz %1"
|
|
|
|
: "=m"(*p), "=q"(result)
|
|
|
|
: "r"(new_), "a"(old), "m"(*p)
|
|
|
|
: "memory");
|
|
|
|
|
|
|
|
return result != 0;
|
2009-11-28 22:01:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
atomicCompareAndSwap64(uint64_t* p, uint64_t old, uint64_t new_)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
2010-04-15 17:11:10 +00:00
|
|
|
return old == InterlockedCompareExchange64
|
|
|
|
(reinterpret_cast<LONGLONG*>(p), new_, old);
|
2009-11-28 22:01:54 +00:00
|
|
|
#elif (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 1)
|
|
|
|
return __sync_bool_compare_and_swap(p, old, new_);
|
2010-11-13 01:29:12 +00:00
|
|
|
#elif defined ARCH_x86_32
|
|
|
|
uint8_t result;
|
|
|
|
|
|
|
|
__asm__ __volatile__("lock; cmpxchg8b %0; setz %1"
|
|
|
|
: "=m"(*p), "=q"(result)
|
|
|
|
: "a"(static_cast<uint32_t>(old)),
|
|
|
|
"d"(static_cast<uint32_t>(old >> 32)),
|
|
|
|
"b"(static_cast<uint32_t>(new_)),
|
|
|
|
"c"(static_cast<uint32_t>(new_ >> 32)),
|
|
|
|
"m"(*p)
|
|
|
|
: "memory");
|
|
|
|
|
|
|
|
return result != 0;
|
2009-11-28 22:01:54 +00:00
|
|
|
#else
|
2009-11-20 17:40:01 +00:00
|
|
|
uint8_t result;
|
|
|
|
|
2009-11-20 22:17:35 +00:00
|
|
|
__asm__ __volatile__("lock; cmpxchgq %2, %0; setz %1"
|
2009-11-20 17:40:01 +00:00
|
|
|
: "=m"(*p), "=q"(result)
|
|
|
|
: "r"(new_), "a"(old), "m"(*p)
|
|
|
|
: "memory");
|
|
|
|
|
|
|
|
return result != 0;
|
2009-11-28 22:01:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
atomicCompareAndSwap(uintptr_t* p, uintptr_t old, uintptr_t new_)
|
|
|
|
{
|
|
|
|
#ifdef ARCH_x86_32
|
2009-12-01 15:23:11 +00:00
|
|
|
return atomicCompareAndSwap32(reinterpret_cast<uint32_t*>(p), old, new_);
|
2009-11-28 22:01:54 +00:00
|
|
|
#elif defined ARCH_x86_64
|
2009-12-01 15:23:11 +00:00
|
|
|
return atomicCompareAndSwap64(reinterpret_cast<uint64_t*>(p), old, new_);
|
2009-11-20 17:40:01 +00:00
|
|
|
#endif // ARCH_x86_64
|
|
|
|
}
|
|
|
|
#endif // USE_ATOMIC_OPERATIONS
|
|
|
|
|
2009-03-09 15:29:37 +00:00
|
|
|
} // namespace vm
|
2007-10-23 01:00:57 +00:00
|
|
|
|
|
|
|
#endif//X86_H
|