From b628577855926a4d96efd7213f46786afea918e8 Mon Sep 17 00:00:00 2001 From: "Dr. Chat" Date: Sun, 1 Nov 2015 21:56:40 -0600 Subject: [PATCH 1/2] Implemented KeSetCurrentStackPointers --- .../kernel/xboxkrnl/xboxkrnl_threading.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/xenia/kernel/xboxkrnl/xboxkrnl_threading.cc b/src/xenia/kernel/xboxkrnl/xboxkrnl_threading.cc index 344409672..fd1f14559 100644 --- a/src/xenia/kernel/xboxkrnl/xboxkrnl_threading.cc +++ b/src/xenia/kernel/xboxkrnl/xboxkrnl_threading.cc @@ -224,6 +224,24 @@ SHIM_CALL NtSuspendThread_shim(PPCContext* ppc_context, SHIM_SET_RETURN_32(result); } +void KeSetCurrentStackPointers(lpvoid_t stack_ptr, + pointer_t cur_thread, + lpvoid_t stack_alloc_base, lpvoid_t stack_base, + lpvoid_t stack_limit) { + auto thread = XThread::GetCurrentThread(); + auto context = thread->thread_state()->context(); + context->r[1] = stack_ptr.guest_address(); + + auto pcr = + kernel_memory()->TranslateVirtual((uint32_t)context->r[13]); + pcr->stack_base_ptr = stack_base.guest_address(); + pcr->stack_end_ptr = stack_limit.guest_address(); + + // TODO: Do we need to set the stack info on cur_thread? +} +DECLARE_XBOXKRNL_EXPORT(KeSetCurrentStackPointers, + ExportTag::kThreading | ExportTag::kImplemented); + SHIM_CALL KeSetAffinityThread_shim(PPCContext* ppc_context, KernelState* kernel_state) { uint32_t thread_ptr = SHIM_GET_ARG_32(0); From cd12b3d198f9ba4a4c75f957113297ed6fdd6853 Mon Sep 17 00:00:00 2001 From: "Dr. Chat" Date: Sun, 1 Nov 2015 21:57:35 -0600 Subject: [PATCH 2/2] Converted MmCreateKernelStack / Implemented MmDeleteKernelStack --- src/xenia/kernel/xboxkrnl/xboxkrnl_memory.cc | 35 ++++++++------------ 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/xenia/kernel/xboxkrnl/xboxkrnl_memory.cc b/src/xenia/kernel/xboxkrnl/xboxkrnl_memory.cc index fa3afcee9..c7db380ec 100644 --- a/src/xenia/kernel/xboxkrnl/xboxkrnl_memory.cc +++ b/src/xenia/kernel/xboxkrnl/xboxkrnl_memory.cc @@ -559,36 +559,32 @@ SHIM_CALL KeUnlockL2_shim(PPCContext* ppc_context, KernelState* kernel_state) { XELOGD("KeUnlockL2(?)"); } -SHIM_CALL MmCreateKernelStack_shim(PPCContext* ppc_context, - KernelState* kernel_state) { - auto stack_size = SHIM_GET_ARG_32(0); - auto unk1 = SHIM_GET_ARG_32(1); +dword_result_t MmCreateKernelStack(dword_t stack_size, dword_t r4) { + assert_zero(r4); // Unknown argument. - XELOGD("MmCreateKernelStack(%.8X, %.8X)", stack_size, unk1); - - stack_size = (stack_size + 0xFFF) & 0xFFFFF000; + auto stack_size_aligned = (stack_size + 0xFFF) & 0xFFFFF000; uint32_t stack_alignment = (stack_size & 0xF000) ? 0x1000 : 0x10000; uint32_t stack_address; - kernel_state->memory() + kernel_memory() ->LookupHeap(0x70000000) - ->AllocRange(0x70000000, 0x7FFFFFFF, stack_size, stack_alignment, + ->AllocRange(0x70000000, 0x7FFFFFFF, stack_size_aligned, stack_alignment, kMemoryAllocationReserve | kMemoryAllocationCommit, kMemoryProtectRead | kMemoryProtectWrite, false, &stack_address); - SHIM_SET_RETURN_32(stack_address + stack_size); + return stack_address + stack_size; } +DECLARE_XBOXKRNL_EXPORT(MmCreateKernelStack, ExportTag::kImplemented); -SHIM_CALL MmDeleteKernelStack_shim(PPCContext* ppc_context, - KernelState* kernel_state) { - auto unk0 = SHIM_GET_ARG_32(0); - auto unk1 = SHIM_GET_ARG_32(1); +dword_result_t MmDeleteKernelStack(lpvoid_t stack_base, lpvoid_t stack_end) { + // Release the stack (where stack_end is the low address) + if (kernel_memory()->LookupHeap(0x70000000)->Release(stack_end)) { + return X_STATUS_SUCCESS; + } - XELOGD("MmDeleteKernelStack(%.8X, %.8X)", unk0, unk1); - - assert_always(); - SHIM_SET_RETURN_32(0); + return X_STATUS_UNSUCCESSFUL; } +DECLARE_XBOXKRNL_EXPORT(MmDeleteKernelStack, ExportTag::kImplemented); void RegisterMemoryExports(xe::cpu::ExportResolver* export_resolver, KernelState* kernel_state) { @@ -609,9 +605,6 @@ void RegisterMemoryExports(xe::cpu::ExportResolver* export_resolver, SHIM_SET_MAPPING("xboxkrnl.exe", KeLockL2, state); SHIM_SET_MAPPING("xboxkrnl.exe", KeUnlockL2, state); - - SHIM_SET_MAPPING("xboxkrnl.exe", MmCreateKernelStack, state); - SHIM_SET_MAPPING("xboxkrnl.exe", MmDeleteKernelStack, state); } } // namespace xboxkrnl