From 48425da8ff441d7eb52ca039a6419d18cd4fb5b9 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Sat, 12 Jul 2014 22:59:16 -0700 Subject: [PATCH] Sandbox getting close to running. --- src/alloy/backend/ivm/ivm_function.cc | 8 ------ src/alloy/frontend/ppc/ppc_context.h | 1 - src/alloy/runtime/runtime.h | 2 +- src/alloy/runtime/thread_state.h | 6 ++--- src/xenia/cpu/xenon_thread_state.cc | 38 +++------------------------ src/xenia/cpu/xenon_thread_state.h | 7 ----- tools/alloy-sandbox/alloy-sandbox.cc | 10 +++---- 7 files changed, 11 insertions(+), 61 deletions(-) diff --git a/src/alloy/backend/ivm/ivm_function.cc b/src/alloy/backend/ivm/ivm_function.cc index b44f1f07a..f0baf3b36 100644 --- a/src/alloy/backend/ivm/ivm_function.cc +++ b/src/alloy/backend/ivm/ivm_function.cc @@ -128,8 +128,6 @@ int IVMFunction::CallImpl(ThreadState* thread_state, uint64_t return_address) { ics.return_address = return_address; ics.call_return_address = 0; - volatile int* suspend_flag_address = thread_state->suspend_flag_address(); - // TODO(benvanik): DID_CARRY -- need HIR to set a OPCODE_FLAG_SET_CARRY // or something so the fns can set an ics flag. @@ -139,12 +137,6 @@ int IVMFunction::CallImpl(ThreadState* thread_state, uint64_t return_address) { uint32_t ia = 0; while (true) { - // Check suspend. We could do this only on certain instructions, if we - // wanted to speed things up. - if (*suspend_flag_address) { - thread_state->EnterSuspend(); - } - #ifdef TRACE_SOURCE_OFFSET uint64_t source_offset = -1; if (source_index < this->source_map_count_ && diff --git a/src/alloy/frontend/ppc/ppc_context.h b/src/alloy/frontend/ppc/ppc_context.h index 5e40b19f2..d1d28860a 100644 --- a/src/alloy/frontend/ppc/ppc_context.h +++ b/src/alloy/frontend/ppc/ppc_context.h @@ -199,7 +199,6 @@ typedef struct XECACHEALIGN64 PPCContext_s { // Runtime-specific data pointer. Used on callbacks to get access to the // current runtime and its data. runtime::Runtime* runtime; - volatile int suspend_flag; void SetRegFromString(const char* name, const char* value); bool CompareRegWithString(const char* name, const char* value, diff --git a/src/alloy/runtime/runtime.h b/src/alloy/runtime/runtime.h index 5ede95140..fc5b90347 100644 --- a/src/alloy/runtime/runtime.h +++ b/src/alloy/runtime/runtime.h @@ -31,7 +31,7 @@ class Runtime { typedef std::vector ModuleList; public: - Runtime(Memory* memory); + explicit Runtime(Memory* memory); virtual ~Runtime(); Memory* memory() const { return memory_; } diff --git a/src/alloy/runtime/thread_state.h b/src/alloy/runtime/thread_state.h index a10e67438..2d1e36b3f 100644 --- a/src/alloy/runtime/thread_state.h +++ b/src/alloy/runtime/thread_state.h @@ -32,10 +32,8 @@ class ThreadState { void* backend_data() const { return backend_data_; } void* raw_context() const { return raw_context_; } - virtual volatile int* suspend_flag_address() const = 0; - virtual int Suspend(uint32_t timeout_ms = UINT_MAX) = 0; - virtual int Resume(bool force = false) = 0; - virtual void EnterSuspend() = 0; + virtual int Suspend(uint32_t timeout_ms = UINT_MAX) { return 1; } + virtual int Resume(bool force = false) { return 1; } static void Bind(ThreadState* thread_state); static ThreadState* Get(); diff --git a/src/xenia/cpu/xenon_thread_state.cc b/src/xenia/cpu/xenon_thread_state.cc index 57959e15f..b00a75355 100644 --- a/src/xenia/cpu/xenon_thread_state.cc +++ b/src/xenia/cpu/xenon_thread_state.cc @@ -23,13 +23,12 @@ using namespace xe::cpu; XenonThreadState::XenonThreadState( XenonRuntime* runtime, uint32_t thread_id, size_t stack_size, uint64_t thread_state_address) : - stack_size_(stack_size), thread_state_address_(thread_state_address), - ThreadState(runtime, thread_id) { + ThreadState(runtime, thread_id), + stack_size_(stack_size), + thread_state_address_(thread_state_address) { stack_address_ = memory_->HeapAlloc( 0, stack_size, MEMORY_FLAG_ZERO); - debug_break_ = CreateEvent(NULL, FALSE, FALSE, NULL); - // Allocate with 64b alignment. context_ = (PPCContext*)xe_malloc_aligned(sizeof(PPCContext)); assert_true(((uint64_t)context_ & 0xF) == 0); @@ -60,40 +59,9 @@ XenonThreadState::XenonThreadState( XenonThreadState::~XenonThreadState() { runtime_->debugger()->OnThreadDestroyed(this); - CloseHandle(debug_break_); - alloy::tracing::WriteEvent(EventType::ThreadDeinit({ })); xe_free_aligned(context_); memory_->HeapFree(stack_address_, stack_size_); } - -volatile int* XenonThreadState::suspend_flag_address() const { - return &context_->suspend_flag; -} - -int XenonThreadState::Suspend(uint32_t timeout_ms) { - // Set suspend flag. - // One of the checks should call in to OnSuspend() at some point. - poly::atomic_inc(&context_->suspend_flag); - return 0; -} - -int XenonThreadState::Resume(bool force) { - if (context_->suspend_flag) { - if (force) { - context_->suspend_flag = 0; - SetEvent(debug_break_); - } else { - if (!poly::atomic_dec(&context_->suspend_flag)) { - SetEvent(debug_break_); - } - } - } - return 0; -} - -void XenonThreadState::EnterSuspend() { - WaitForSingleObject(debug_break_, INFINITE); -} diff --git a/src/xenia/cpu/xenon_thread_state.h b/src/xenia/cpu/xenon_thread_state.h index ca3035590..0bda965fa 100644 --- a/src/xenia/cpu/xenon_thread_state.h +++ b/src/xenia/cpu/xenon_thread_state.h @@ -34,11 +34,6 @@ public: uint64_t thread_state_address() const { return thread_state_address_; } PPCContext* context() const { return context_; } - virtual volatile int* suspend_flag_address() const; - virtual int Suspend(uint32_t timeout_ms = UINT_MAX); - virtual int Resume(bool force = false); - virtual void EnterSuspend(); - private: uint64_t stack_address_; size_t stack_size_; @@ -46,8 +41,6 @@ private: // NOTE: must be 64b aligned for SSE ops. PPCContext* context_; - - HANDLE debug_break_; }; diff --git a/tools/alloy-sandbox/alloy-sandbox.cc b/tools/alloy-sandbox/alloy-sandbox.cc index 038580c69..324e8ea19 100644 --- a/tools/alloy-sandbox/alloy-sandbox.cc +++ b/tools/alloy-sandbox/alloy-sandbox.cc @@ -7,12 +7,12 @@ ****************************************************************************** */ -#include #include - #include +#include #include #include +#include #include @@ -22,7 +22,6 @@ using namespace alloy::runtime; using namespace xe; using namespace xe::cpu; - int alloy_sandbox(int argc, xechar_t** argv) { Profiler::Initialize(); xe::Profiler::ThreadEnter("main"); @@ -43,8 +42,8 @@ int alloy_sandbox(int argc, xechar_t** argv) { module->LoadFile(0x82000000, "test\\codegen\\instr_add.bin"); runtime->AddModule(module); - XenonThreadState* thread_state = new XenonThreadState( - runtime, 100, 64 * 1024, 0); + XenonThreadState* thread_state = + new XenonThreadState(runtime, 100, 64 * 1024, 0); Function* fn; runtime->ResolveFunction(0x82000000, &fn); @@ -54,6 +53,7 @@ int alloy_sandbox(int argc, xechar_t** argv) { ctx->r[25] = 25; fn->Call(thread_state, ctx->lr); auto result = ctx->r[11]; + printf("%llu", result); delete thread_state;