https://git.reactos.org/?p=reactos.git;a=commitdiff;h=6c5b3f203d3a1e6687d92653ba630a1c73556f3c
commit 6c5b3f203d3a1e6687d92653ba630a1c73556f3c Author: Timo Kreuzer <timo.kreu...@reactos.org> AuthorDate: Wed Dec 6 10:49:06 2023 +0200 Commit: Timo Kreuzer <timo.kreu...@reactos.org> CommitDate: Fri Dec 8 19:38:59 2023 +0200 [NTOS:KE/x64] Add back a stubs.c file --- ntoskrnl/ke/amd64/stubs.c | 195 ++++++++++++++++++++++++++++++++++++++++ ntoskrnl/ke/amd64/traphandler.c | 185 +------------------------------------- ntoskrnl/ntos.cmake | 1 + 3 files changed, 197 insertions(+), 184 deletions(-) diff --git a/ntoskrnl/ke/amd64/stubs.c b/ntoskrnl/ke/amd64/stubs.c new file mode 100644 index 00000000000..cb2d4a37248 --- /dev/null +++ b/ntoskrnl/ke/amd64/stubs.c @@ -0,0 +1,195 @@ +/* + * PROJECT: ReactOS Kernel + * LICENSE: GPL - See COPYING in the top level directory + * PURPOSE: stubs + * PROGRAMMERS: Timo Kreuzer (timo.kreu...@reactos.org) + */ + +/* INCLUDES ******************************************************************/ + +#include <ntoskrnl.h> + +#define NDEBUG +#include <debug.h> + +/* GLOBALS *******************************************************************/ + +ULONG ProcessCount; +SIZE_T KeXStateLength = sizeof(XSAVE_FORMAT); + +PVOID +KiSwitchKernelStackHelper( + LONG_PTR StackOffset, + PVOID OldStackBase); + +/* + * Kernel stack layout (example pointers): + * 0xFFFFFC0F'2D008000 KTHREAD::StackBase + * [XSAVE_AREA size == KeXStateLength = 0x440] + * 0xFFFFFC0F'2D007BC0 KTHREAD::StateSaveArea _XSAVE_FORMAT + * 0xFFFFFC0F'2D007B90 KTHREAD::InitialStack + * [0x190 bytes KTRAP_FRAME] + * 0xFFFFFC0F'2D007A00 KTHREAD::TrapFrame + * [KSTART_FRAME] or ... + * [KSWITCH_FRAME] + * 0xFFFFFC0F'2D007230 KTHREAD::KernelStack + */ + +PVOID +NTAPI +KiSwitchKernelStack(PVOID StackBase, PVOID StackLimit) +{ + PKTHREAD CurrentThread; + PVOID OldStackBase; + LONG_PTR StackOffset; + SIZE_T StackSize; + PKIPCR Pcr; + ULONG Eflags; + + /* Get the current thread */ + CurrentThread = KeGetCurrentThread(); + + /* Save the old stack base */ + OldStackBase = CurrentThread->StackBase; + + /* Get the size of the current stack */ + StackSize = (ULONG_PTR)CurrentThread->StackBase - CurrentThread->StackLimit; + ASSERT(StackSize <= (ULONG_PTR)StackBase - (ULONG_PTR)StackLimit); + + /* Copy the current stack contents to the new stack */ + RtlCopyMemory((PUCHAR)StackBase - StackSize, + (PVOID)CurrentThread->StackLimit, + StackSize); + + /* Calculate the offset between the old and the new stack */ + StackOffset = (PUCHAR)StackBase - (PUCHAR)CurrentThread->StackBase; + + /* Disable interrupts while messing with the stack */ + Eflags = __readeflags(); + _disable(); + + /* Set the new trap frame */ + CurrentThread->TrapFrame = (PKTRAP_FRAME)Add2Ptr(CurrentThread->TrapFrame, + StackOffset); + + /* Set the new initial stack */ + CurrentThread->InitialStack = Add2Ptr(CurrentThread->InitialStack, + StackOffset); + + /* Set the new stack limits */ + CurrentThread->StackBase = StackBase; + CurrentThread->StackLimit = (ULONG_PTR)StackLimit; + CurrentThread->LargeStack = TRUE; + + /* Adjust RspBase in the PCR */ + Pcr = (PKIPCR)KeGetPcr(); + Pcr->Prcb.RspBase += StackOffset; + + /* Adjust Rsp0 in the TSS */ + Pcr->TssBase->Rsp0 += StackOffset; + + /* Restore interrupts */ + __writeeflags(Eflags); + + return OldStackBase; +} + +DECLSPEC_NORETURN +VOID +KiIdleLoop(VOID) +{ + PKPRCB Prcb = KeGetCurrentPrcb(); + PKTHREAD OldThread, NewThread; + + /* Now loop forever */ + while (TRUE) + { + /* Start of the idle loop: disable interrupts */ + _enable(); + YieldProcessor(); + YieldProcessor(); + _disable(); + + /* Check for pending timers, pending DPCs, or pending ready threads */ + if ((Prcb->DpcData[0].DpcQueueDepth) || + (Prcb->TimerRequest) || + (Prcb->DeferredReadyListHead.Next)) + { + /* Quiesce the DPC software interrupt */ + HalClearSoftwareInterrupt(DISPATCH_LEVEL); + + /* Handle it */ + KiRetireDpcList(Prcb); + } + + /* Check if a new thread is scheduled for execution */ + if (Prcb->NextThread) + { + /* Enable interrupts */ + _enable(); + + /* Capture current thread data */ + OldThread = Prcb->CurrentThread; + NewThread = Prcb->NextThread; + + /* Set new thread data */ + Prcb->NextThread = NULL; + Prcb->CurrentThread = NewThread; + + /* The thread is now running */ + NewThread->State = Running; + + /* Do the swap at SYNCH_LEVEL */ + KfRaiseIrql(SYNCH_LEVEL); + + /* Switch away from the idle thread */ + KiSwapContext(APC_LEVEL, OldThread); + + /* Go back to DISPATCH_LEVEL */ + KeLowerIrql(DISPATCH_LEVEL); + } + else + { + /* Continue staying idle. Note the HAL returns with interrupts on */ + Prcb->PowerState.IdleFunction(&Prcb->PowerState); + } + } +} + +VOID +NTAPI +KiSwapProcess(IN PKPROCESS NewProcess, + IN PKPROCESS OldProcess) +{ + PKIPCR Pcr = (PKIPCR)KeGetPcr(); + +#ifdef CONFIG_SMP + /* Update active processor mask */ + InterlockedXor64((PLONG64)&NewProcess->ActiveProcessors, Pcr->Prcb.SetMember); + InterlockedXor64((PLONG64)&OldProcess->ActiveProcessors, Pcr->Prcb.SetMember); +#endif + + /* Update CR3 */ + __writecr3(NewProcess->DirectoryTableBase[0]); + + /* Update IOPM offset */ + Pcr->TssBase->IoMapBase = NewProcess->IopmOffset; +} + +NTSTATUS +NTAPI +NtSetLdtEntries(ULONG Selector1, LDT_ENTRY LdtEntry1, ULONG Selector2, LDT_ENTRY LdtEntry2) +{ + UNIMPLEMENTED; + __debugbreak(); + return STATUS_UNSUCCESSFUL; +} + +NTSTATUS +NTAPI +NtVdmControl(IN ULONG ControlCode, + IN PVOID ControlData) +{ + /* Not supported */ + return STATUS_NOT_IMPLEMENTED; +} diff --git a/ntoskrnl/ke/amd64/traphandler.c b/ntoskrnl/ke/amd64/traphandler.c index 7933d2c1cf9..2b67b07d7b8 100644 --- a/ntoskrnl/ke/amd64/traphandler.c +++ b/ntoskrnl/ke/amd64/traphandler.c @@ -1,21 +1,17 @@ /* * PROJECT: ReactOS Kernel * LICENSE: GPL - See COPYING in the top level directory - * PURPOSE: stubs + * PURPOSE: x64 trap handlers * PROGRAMMERS: Timo Kreuzer (timo.kreu...@reactos.org) */ /* INCLUDES ******************************************************************/ #include <ntoskrnl.h> -#include <fltkernel.h> #define NDEBUG #include <debug.h> -ULONG ProcessCount; -SIZE_T KeXStateLength = sizeof(XSAVE_FORMAT); - VOID KiRetireDpcListInDpcStack( PKPRCB Prcb, @@ -88,165 +84,6 @@ KiDpcInterruptHandler(VOID) KeLowerIrql(OldIrql); } -PVOID -KiSwitchKernelStackHelper( - LONG_PTR StackOffset, - PVOID OldStackBase); - -/* - * Kernel stack layout (example pointers): - * 0xFFFFFC0F'2D008000 KTHREAD::StackBase - * [XSAVE_AREA size == KeXStateLength = 0x440] - * 0xFFFFFC0F'2D007BC0 KTHREAD::StateSaveArea _XSAVE_FORMAT - * 0xFFFFFC0F'2D007B90 KTHREAD::InitialStack - * [0x190 bytes KTRAP_FRAME] - * 0xFFFFFC0F'2D007A00 KTHREAD::TrapFrame - * [KSTART_FRAME] or ... - * [KSWITCH_FRAME] - * 0xFFFFFC0F'2D007230 KTHREAD::KernelStack - */ - -PVOID -NTAPI -KiSwitchKernelStack(PVOID StackBase, PVOID StackLimit) -{ - PKTHREAD CurrentThread; - PVOID OldStackBase; - LONG_PTR StackOffset; - SIZE_T StackSize; - PKIPCR Pcr; - ULONG Eflags; - - /* Get the current thread */ - CurrentThread = KeGetCurrentThread(); - - /* Save the old stack base */ - OldStackBase = CurrentThread->StackBase; - - /* Get the size of the current stack */ - StackSize = (ULONG_PTR)CurrentThread->StackBase - CurrentThread->StackLimit; - ASSERT(StackSize <= (ULONG_PTR)StackBase - (ULONG_PTR)StackLimit); - - /* Copy the current stack contents to the new stack */ - RtlCopyMemory((PUCHAR)StackBase - StackSize, - (PVOID)CurrentThread->StackLimit, - StackSize); - - /* Calculate the offset between the old and the new stack */ - StackOffset = (PUCHAR)StackBase - (PUCHAR)CurrentThread->StackBase; - - /* Disable interrupts while messing with the stack */ - Eflags = __readeflags(); - _disable(); - - /* Set the new trap frame */ - CurrentThread->TrapFrame = (PKTRAP_FRAME)Add2Ptr(CurrentThread->TrapFrame, - StackOffset); - - /* Set the new initial stack */ - CurrentThread->InitialStack = Add2Ptr(CurrentThread->InitialStack, - StackOffset); - - /* Set the new stack limits */ - CurrentThread->StackBase = StackBase; - CurrentThread->StackLimit = (ULONG_PTR)StackLimit; - CurrentThread->LargeStack = TRUE; - - /* Adjust RspBase in the PCR */ - Pcr = (PKIPCR)KeGetPcr(); - Pcr->Prcb.RspBase += StackOffset; - - /* Adjust Rsp0 in the TSS */ - Pcr->TssBase->Rsp0 += StackOffset; - - /* Restore interrupts */ - __writeeflags(Eflags); - - return OldStackBase; -} - -DECLSPEC_NORETURN -VOID -KiIdleLoop(VOID) -{ - PKPRCB Prcb = KeGetCurrentPrcb(); - PKTHREAD OldThread, NewThread; - - /* Now loop forever */ - while (TRUE) - { - /* Start of the idle loop: disable interrupts */ - _enable(); - YieldProcessor(); - YieldProcessor(); - _disable(); - - /* Check for pending timers, pending DPCs, or pending ready threads */ - if ((Prcb->DpcData[0].DpcQueueDepth) || - (Prcb->TimerRequest) || - (Prcb->DeferredReadyListHead.Next)) - { - /* Quiesce the DPC software interrupt */ - HalClearSoftwareInterrupt(DISPATCH_LEVEL); - - /* Handle it */ - KiRetireDpcList(Prcb); - } - - /* Check if a new thread is scheduled for execution */ - if (Prcb->NextThread) - { - /* Enable interrupts */ - _enable(); - - /* Capture current thread data */ - OldThread = Prcb->CurrentThread; - NewThread = Prcb->NextThread; - - /* Set new thread data */ - Prcb->NextThread = NULL; - Prcb->CurrentThread = NewThread; - - /* The thread is now running */ - NewThread->State = Running; - - /* Do the swap at SYNCH_LEVEL */ - KfRaiseIrql(SYNCH_LEVEL); - - /* Switch away from the idle thread */ - KiSwapContext(APC_LEVEL, OldThread); - - /* Go back to DISPATCH_LEVEL */ - KeLowerIrql(DISPATCH_LEVEL); - } - else - { - /* Continue staying idle. Note the HAL returns with interrupts on */ - Prcb->PowerState.IdleFunction(&Prcb->PowerState); - } - } -} - -VOID -NTAPI -KiSwapProcess(IN PKPROCESS NewProcess, - IN PKPROCESS OldProcess) -{ - PKIPCR Pcr = (PKIPCR)KeGetPcr(); - -#ifdef CONFIG_SMP - /* Update active processor mask */ - InterlockedXor64((PLONG64)&NewProcess->ActiveProcessors, Pcr->Prcb.SetMember); - InterlockedXor64((PLONG64)&OldProcess->ActiveProcessors, Pcr->Prcb.SetMember); -#endif - - /* Update CR3 */ - __writecr3(NewProcess->DirectoryTableBase[0]); - - /* Update IOPM offset */ - Pcr->TssBase->IoMapBase = NewProcess->IopmOffset; -} - #define MAX_SYSCALL_PARAMS 16 NTSTATUS @@ -402,23 +239,3 @@ KiSystemService(IN PKTHREAD Thread, __debugbreak(); } -NTSTATUS -NTAPI -NtSetLdtEntries -(ULONG Selector1, LDT_ENTRY LdtEntry1, ULONG Selector2, LDT_ENTRY LdtEntry2) -{ - UNIMPLEMENTED; - __debugbreak(); - return STATUS_UNSUCCESSFUL; -} - -NTSTATUS -NTAPI -NtVdmControl(IN ULONG ControlCode, - IN PVOID ControlData) -{ - /* Not supported */ - return STATUS_NOT_IMPLEMENTED; -} - - diff --git a/ntoskrnl/ntos.cmake b/ntoskrnl/ntos.cmake index 1fc4307573f..26a1e07b17e 100644 --- a/ntoskrnl/ntos.cmake +++ b/ntoskrnl/ntos.cmake @@ -359,6 +359,7 @@ elseif(ARCH STREQUAL "amd64") ${REACTOS_SOURCE_DIR}/ntoskrnl/mm/amd64/init.c ${REACTOS_SOURCE_DIR}/ntoskrnl/mm/amd64/procsup.c ${REACTOS_SOURCE_DIR}/ntoskrnl/ps/amd64/psctx.c + ${REACTOS_SOURCE_DIR}/ntoskrnl/ke/amd64/stubs.c ${REACTOS_SOURCE_DIR}/ntoskrnl/ke/amd64/traphandler.c ${REACTOS_SOURCE_DIR}/ntoskrnl/ke/amd64/usercall.c) if(BUILD_MP)