diff --git a/src/kernel/modules/xboxkrnl/objects/xthread.cc b/src/kernel/modules/xboxkrnl/objects/xthread.cc index da955d4db..81d27801d 100644 --- a/src/kernel/modules/xboxkrnl/objects/xthread.cc +++ b/src/kernel/modules/xboxkrnl/objects/xthread.cc @@ -58,6 +58,10 @@ XThread::~XThread() { } } +uint32_t XThread::GetCurrentThreadId(const uint8_t* thread_state_block) { + return XEGETUINT32BE(thread_state_block + 0x14C); +} + uint32_t XThread::thread_id() { return thread_id_; } diff --git a/src/kernel/modules/xboxkrnl/objects/xthread.h b/src/kernel/modules/xboxkrnl/objects/xthread.h index 7a43ec45d..77d7a403d 100644 --- a/src/kernel/modules/xboxkrnl/objects/xthread.h +++ b/src/kernel/modules/xboxkrnl/objects/xthread.h @@ -29,6 +29,8 @@ public: uint32_t creation_flags); virtual ~XThread(); + static uint32_t GetCurrentThreadId(const uint8_t* thread_state_block); + uint32_t thread_id(); uint32_t last_error(); void set_last_error(uint32_t error_code); diff --git a/src/kernel/modules/xboxkrnl/xboxkrnl_rtl.cc b/src/kernel/modules/xboxkrnl/xboxkrnl_rtl.cc index 0bbeec659..a3482cf99 100644 --- a/src/kernel/modules/xboxkrnl/xboxkrnl_rtl.cc +++ b/src/kernel/modules/xboxkrnl/xboxkrnl_rtl.cc @@ -13,6 +13,7 @@ #include #include "kernel/shim_utils.h" +#include "kernel/modules/xboxkrnl/objects/xthread.h" using namespace xe; @@ -395,8 +396,8 @@ void RtlEnterCriticalSection_shim( X_RTL_CRITICAL_SECTION* cs = (X_RTL_CRITICAL_SECTION*)SHIM_MEM_ADDR(cs_ptr); - // TODO(benvanik): get current thread ID. - uint32_t thread_id = 0; + const uint8_t* thread_state_block = ppc_state->membase + ppc_state->r[13]; + uint32_t thread_id = XThread::GetCurrentThreadId(thread_state_block); uint32_t spin_wait_remaining = cs->spin_count_div_256 * 256; spin: @@ -434,8 +435,8 @@ void RtlTryEnterCriticalSection_shim( X_RTL_CRITICAL_SECTION* cs = (X_RTL_CRITICAL_SECTION*)SHIM_MEM_ADDR(cs_ptr); - // TODO(benvanik): get current thread ID. - uint32_t thread_id = 0; + const uint8_t* thread_state_block = ppc_state->membase + ppc_state->r[13]; + uint32_t thread_id = XThread::GetCurrentThreadId(thread_state_block); if (xe_atomic_cas_32(-1, 0, &cs->lock_count)) { // Able to steal the lock right away.