From ffe3b4bf047245389c379b1b256b642ccf2d8c69 Mon Sep 17 00:00:00 2001 From: "Dr. Chat" Date: Mon, 18 May 2015 22:23:59 -0500 Subject: [PATCH 1/3] XHostThread class --- src/xenia/kernel/objects/xthread.cc | 54 ++++++++++++++++++++++++----- src/xenia/kernel/objects/xthread.h | 15 ++++++-- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/xenia/kernel/objects/xthread.cc b/src/xenia/kernel/objects/xthread.cc index 255986f45..bdce06129 100644 --- a/src/xenia/kernel/objects/xthread.cc +++ b/src/xenia/kernel/objects/xthread.cc @@ -156,8 +156,12 @@ X_STATUS XThread::Create() { scratch_address_ = memory()->SystemHeapAlloc(scratch_size_); // Allocate TLS block. - const xe_xex2_header_t* header = module->xex_header(); - uint32_t tls_size = header->tls_info.slot_count * header->tls_info.data_size; + uint32_t tls_size = 32; // Default 32 (is this OK?) + if (module && module->xex_header()) { + const xe_xex2_header_t* header = module->xex_header(); + tls_size = header->tls_info.slot_count * header->tls_info.data_size; + } + tls_address_ = memory()->SystemHeapAlloc(tls_size); if (!tls_address_) { XELOGW("Unable to allocate thread local storage block"); @@ -165,9 +169,20 @@ X_STATUS XThread::Create() { return X_STATUS_NO_MEMORY; } - // Copy in default TLS info. - // TODO(benvanik): is this correct? - memory()->Copy(tls_address_, header->tls_info.raw_data_address, tls_size); + // Copy in default TLS info (or zero it out) + if (module && module->xex_header()) { + const xe_xex2_header_t* header = module->xex_header(); + + // Copy in default TLS info. + // TODO(benvanik): is this correct? + memory()->Copy(tls_address_, header->tls_info.raw_data_address, tls_size); + } else { + memory()->Fill(tls_address_, tls_size, 0); + } + + if (module) { + module->Release(); + } // Allocate processor thread state. // This is thread safe. @@ -179,13 +194,15 @@ X_STATUS XThread::Create() { thread_state_->stack_base()); uint8_t* pcr = memory()->TranslateVirtual(pcr_address_); + std::memset(pcr, 0x0, 0x2D8 + 0xAB0); // Zero the PCR xe::store_and_swap(pcr + 0x000, tls_address_); xe::store_and_swap(pcr + 0x030, pcr_address_); xe::store_and_swap(pcr + 0x070, thread_state_->stack_address() + thread_state_->stack_size()); xe::store_and_swap(pcr + 0x074, thread_state_->stack_address()); xe::store_and_swap(pcr + 0x100, thread_state_address_); - xe::store_and_swap(pcr + 0x150, 0); // DPC active bool? + xe::store_and_swap (pcr + 0x10C, 1); // Current CPU(?) + xe::store_and_swap(pcr + 0x150, 0); // DPC active bool? // Setup the thread state block (last error/etc). uint8_t* p = memory()->TranslateVirtual(thread_state_address_); @@ -236,7 +253,6 @@ X_STATUS XThread::Create() { X_STATUS return_code = PlatformCreate(); if (XFAILED(return_code)) { XELOGW("Unable to create platform thread (%.8X)", return_code); - module->Release(); return return_code; } @@ -249,7 +265,6 @@ X_STATUS XThread::Create() { SetAffinity(proc_mask); } - module->Release(); return X_STATUS_SUCCESS; } @@ -606,5 +621,28 @@ X_STATUS XThread::Delay(uint32_t processor_mode, uint32_t alertable, void* XThread::GetWaitHandle() { return event_->GetWaitHandle(); } +XHostThread::XHostThread(KernelState* kernel_state, uint32_t stack_size, + uint32_t creation_flags, std::function host_fn): + XThread(kernel_state, stack_size, 0, 0, 0, creation_flags), + host_fn_(host_fn) { +} + +void XHostThread::Execute() { + XELOGKERNEL("XThread::Execute thid %d (handle=%.8X, '%s', native=%.8X, )", + thread_id_, handle(), name_.c_str(), + xe::threading::current_thread_id()); + + // Let the kernel know we are starting. + kernel_state()->OnThreadExecute(this); + + int ret = host_fn_(); + + // Let the kernel know we are exiting. + kernel_state()->OnThreadExit(this); + + // Exit. + Exit(ret); +} + } // namespace kernel } // namespace xe diff --git a/src/xenia/kernel/objects/xthread.h b/src/xenia/kernel/objects/xthread.h index e8d6456fb..5cdb11604 100644 --- a/src/xenia/kernel/objects/xthread.h +++ b/src/xenia/kernel/objects/xthread.h @@ -47,7 +47,7 @@ class XThread : public XObject { X_STATUS Create(); X_STATUS Exit(int exit_code); - void Execute(); + virtual void Execute(); static void EnterCriticalRegion(); static void LeaveCriticalRegion(); @@ -69,7 +69,7 @@ class XThread : public XObject { virtual void* GetWaitHandle(); - private: + protected: X_STATUS PlatformCreate(); void PlatformDestroy(); X_STATUS PlatformExit(int exit_code); @@ -103,6 +103,17 @@ class XThread : public XObject { XEvent* event_; }; +class XHostThread : public XThread { + public: + XHostThread(KernelState* kernel_state, uint32_t stack_size, + uint32_t creation_flags, std::function host_fn); + + virtual void Execute(); + + private: + std::function host_fn_; +}; + } // namespace kernel } // namespace xe From b1920f4a8772fa7d5dd5520537894c8ff1b71485 Mon Sep 17 00:00:00 2001 From: "Dr. Chat" Date: Mon, 18 May 2015 22:25:02 -0500 Subject: [PATCH 2/3] Move audio system onto XHostThread --- src/xenia/apu/audio_system.cc | 36 +++++++++++++---------------------- src/xenia/apu/audio_system.h | 7 ++++--- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/xenia/apu/audio_system.cc b/src/xenia/apu/audio_system.cc index 1768592b6..da1d8cab2 100644 --- a/src/xenia/apu/audio_system.cc +++ b/src/xenia/apu/audio_system.cc @@ -14,6 +14,7 @@ #include "xenia/base/math.h" #include "xenia/cpu/processor.h" #include "xenia/cpu/thread_state.h" +#include "xenia/kernel/objects/xthread.h" #include "xenia/emulator.h" #include "xenia/profiling.h" @@ -47,6 +48,7 @@ namespace xe { namespace apu { using namespace xe::cpu; +using namespace xe::kernel; // Size of a hardware XMA context. const uint32_t kXmaContextSize = 64; @@ -89,30 +91,23 @@ X_STATUS AudioSystem::Setup() { } registers_.next_context = 1; - // Setup worker thread state. This lets us make calls into guest code. - thread_state_ = - new ThreadState(emulator_->processor(), 0, ThreadStackType::kKernelStack, - 0, 128 * 1024, 0); - thread_state_->set_name("Audio Worker"); - thread_block_ = memory()->SystemHeapAlloc(2048); - thread_state_->context()->r[13] = thread_block_; - XELOGI("Audio Worker Thread %X Stack: %.8X-%.8X", thread_state_->thread_id(), - thread_state_->stack_address(), - thread_state_->stack_address() + thread_state_->stack_size()); + // Setup our worker thread + std::function thread_fn = [this]() { + this->ThreadStart(); + return 0; + }; - // Create worker thread. - // This will initialize the audio system. - // Init needs to happen there so that any thread-local stuff - // is created on the right thread. running_ = true; - thread_ = std::thread(std::bind(&AudioSystem::ThreadStart, this)); + + thread_ = std::make_unique(emulator()->kernel_state(), + 128 * 1024, 0, thread_fn); + thread_->Create(); return X_STATUS_SUCCESS; } void AudioSystem::ThreadStart() { xe::threading::set_name("Audio Worker"); - xe::Profiler::ThreadEnter("Audio Worker"); // Initialize driver and ringbuffer. Initialize(); @@ -140,7 +135,7 @@ void AudioSystem::ThreadStart() { lock_.unlock(); if (client_callback) { uint64_t args[] = {client_callback_arg}; - processor->Execute(thread_state_, client_callback, args, + processor->Execute(thread_->thread_state(), client_callback, args, xe::countof(args)); } pumped++; @@ -162,8 +157,6 @@ void AudioSystem::ThreadStart() { running_ = false; // TODO(benvanik): call module API to kill? - - xe::Profiler::ThreadExit(); } void AudioSystem::Initialize() {} @@ -171,10 +164,7 @@ void AudioSystem::Initialize() {} void AudioSystem::Shutdown() { running_ = false; ResetEvent(client_wait_handles_[maximum_client_count_]); - thread_.join(); - - delete thread_state_; - memory()->SystemHeapFree(thread_block_); + thread_->Wait(0, 0, 0, NULL); memory()->SystemHeapFree(registers_.xma_context_array_ptr); } diff --git a/src/xenia/apu/audio_system.h b/src/xenia/apu/audio_system.h index 847698ac0..fc2f61381 100644 --- a/src/xenia/apu/audio_system.h +++ b/src/xenia/apu/audio_system.h @@ -19,6 +19,9 @@ #include "xenia/xbox.h" namespace xe { + +namespace kernel { class XHostThread; } + namespace apu { class AudioDriver; @@ -73,9 +76,7 @@ class AudioSystem { Memory* memory_; cpu::Processor* processor_; - std::thread thread_; - cpu::ThreadState* thread_state_; - uint32_t thread_block_; + std::unique_ptr thread_; std::atomic running_; std::mutex lock_; From d1b2b4cde89d927d6963692b131cc8ccc6c9179c Mon Sep 17 00:00:00 2001 From: "Dr. Chat" Date: Mon, 18 May 2015 22:25:35 -0500 Subject: [PATCH 3/3] Start the audio system after the kernel state Expose kernel state in emulator.h --- src/xenia/emulator.cc | 9 +++++---- src/xenia/emulator.h | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/xenia/emulator.cc b/src/xenia/emulator.cc index a909f2ba8..a88760980 100644 --- a/src/xenia/emulator.cc +++ b/src/xenia/emulator.cc @@ -110,10 +110,6 @@ X_STATUS Emulator::Setup() { if (!processor_->Setup()) { return result; } - result = audio_system_->Setup(); - if (result) { - return result; - } result = graphics_system_->Setup(processor_.get(), main_window_->loop(), main_window_.get()); if (result) { @@ -130,6 +126,11 @@ X_STATUS Emulator::Setup() { // Shared kernel state. kernel_state_ = std::make_unique(this); + result = audio_system_->Setup(); + if (result) { + return result; + } + // HLE kernel modules. xboxkrnl_ = std::make_unique(this, kernel_state_.get()); xam_ = std::make_unique(this, kernel_state_.get()); diff --git a/src/xenia/emulator.h b/src/xenia/emulator.h index ab8145c2e..6d389a7f5 100644 --- a/src/xenia/emulator.h +++ b/src/xenia/emulator.h @@ -66,6 +66,8 @@ class Emulator { } kernel::fs::FileSystem* file_system() const { return file_system_.get(); } + kernel::KernelState* kernel_state() const { return kernel_state_.get(); } + kernel::XboxkrnlModule* xboxkrnl() const { return xboxkrnl_.get(); } kernel::XamModule* xam() const { return xam_.get(); }