Merge pull request #448 from DrChat/ke_stack_switching

Kernel Stack Switching (KeSetCurrentStackPointers)
This commit is contained in:
Ben Vanik 2015-11-03 19:59:12 -08:00
commit da4a7e1bf2
2 changed files with 32 additions and 21 deletions

View File

@ -559,36 +559,32 @@ SHIM_CALL KeUnlockL2_shim(PPCContext* ppc_context, KernelState* kernel_state) {
XELOGD("KeUnlockL2(?)"); XELOGD("KeUnlockL2(?)");
} }
SHIM_CALL MmCreateKernelStack_shim(PPCContext* ppc_context, dword_result_t MmCreateKernelStack(dword_t stack_size, dword_t r4) {
KernelState* kernel_state) { assert_zero(r4); // Unknown argument.
auto stack_size = SHIM_GET_ARG_32(0);
auto unk1 = SHIM_GET_ARG_32(1);
XELOGD("MmCreateKernelStack(%.8X, %.8X)", stack_size, unk1); auto stack_size_aligned = (stack_size + 0xFFF) & 0xFFFFF000;
stack_size = (stack_size + 0xFFF) & 0xFFFFF000;
uint32_t stack_alignment = (stack_size & 0xF000) ? 0x1000 : 0x10000; uint32_t stack_alignment = (stack_size & 0xF000) ? 0x1000 : 0x10000;
uint32_t stack_address; uint32_t stack_address;
kernel_state->memory() kernel_memory()
->LookupHeap(0x70000000) ->LookupHeap(0x70000000)
->AllocRange(0x70000000, 0x7FFFFFFF, stack_size, stack_alignment, ->AllocRange(0x70000000, 0x7FFFFFFF, stack_size_aligned, stack_alignment,
kMemoryAllocationReserve | kMemoryAllocationCommit, kMemoryAllocationReserve | kMemoryAllocationCommit,
kMemoryProtectRead | kMemoryProtectWrite, false, kMemoryProtectRead | kMemoryProtectWrite, false,
&stack_address); &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, dword_result_t MmDeleteKernelStack(lpvoid_t stack_base, lpvoid_t stack_end) {
KernelState* kernel_state) { // Release the stack (where stack_end is the low address)
auto unk0 = SHIM_GET_ARG_32(0); if (kernel_memory()->LookupHeap(0x70000000)->Release(stack_end)) {
auto unk1 = SHIM_GET_ARG_32(1); return X_STATUS_SUCCESS;
}
XELOGD("MmDeleteKernelStack(%.8X, %.8X)", unk0, unk1); return X_STATUS_UNSUCCESSFUL;
assert_always();
SHIM_SET_RETURN_32(0);
} }
DECLARE_XBOXKRNL_EXPORT(MmDeleteKernelStack, ExportTag::kImplemented);
void RegisterMemoryExports(xe::cpu::ExportResolver* export_resolver, void RegisterMemoryExports(xe::cpu::ExportResolver* export_resolver,
KernelState* kernel_state) { 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", KeLockL2, state);
SHIM_SET_MAPPING("xboxkrnl.exe", KeUnlockL2, state); SHIM_SET_MAPPING("xboxkrnl.exe", KeUnlockL2, state);
SHIM_SET_MAPPING("xboxkrnl.exe", MmCreateKernelStack, state);
SHIM_SET_MAPPING("xboxkrnl.exe", MmDeleteKernelStack, state);
} }
} // namespace xboxkrnl } // namespace xboxkrnl

View File

@ -224,6 +224,24 @@ SHIM_CALL NtSuspendThread_shim(PPCContext* ppc_context,
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
} }
void KeSetCurrentStackPointers(lpvoid_t stack_ptr,
pointer_t<X_KTHREAD> 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<X_KPCR*>((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, SHIM_CALL KeSetAffinityThread_shim(PPCContext* ppc_context,
KernelState* kernel_state) { KernelState* kernel_state) {
uint32_t thread_ptr = SHIM_GET_ARG_32(0); uint32_t thread_ptr = SHIM_GET_ARG_32(0);