From 3d08be4fac964583512026fb70ac42fd4535cd50 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Sun, 12 May 2024 09:06:24 +0200 Subject: [PATCH] Refactor kernel startup code --- xtoskrnl/ke/amd64/krnlinit.c | 73 ++++++++++++++++++------------------ xtoskrnl/ke/i686/krnlinit.c | 73 ++++++++++++++++++------------------ 2 files changed, 74 insertions(+), 72 deletions(-) diff --git a/xtoskrnl/ke/amd64/krnlinit.c b/xtoskrnl/ke/amd64/krnlinit.c index c2b7cc9..863fcda 100644 --- a/xtoskrnl/ke/amd64/krnlinit.c +++ b/xtoskrnl/ke/amd64/krnlinit.c @@ -20,42 +20,6 @@ XTAPI VOID KepInitializeKernel(VOID) { - PKPROCESSOR_CONTROL_BLOCK Prcb; - ULONG_PTR PageDirectory[2]; - PKPROCESS CurrentProcess; - PKTHREAD CurrentThread; - - /* Get processor control block and current thread */ - Prcb = KeGetCurrentProcessorControlBlock(); - CurrentThread = KeGetCurrentThread(); - - /* Get current process */ - CurrentProcess = CurrentThread->ApcState.Process; - - /* Initialize CPU power state structures */ - PoInitializeProcessorControlBlock(Prcb); - - /* Save processor state */ - KepSaveProcessorState(&Prcb->ProcessorState); - - /* Lower to APC runlevel */ - KeLowerRunLevel(APC_LEVEL); - - /* Initialize Idle process */ - RtlInitializeListHead(&KepProcessListHead); - PageDirectory[0] = 0; - PageDirectory[1] = 0; - KeInitializeProcess(CurrentProcess, 0, 0xFFFFFFFF, PageDirectory, FALSE); - CurrentProcess->Quantum = MAXCHAR; - - /* Initialize Idle thread */ - KeInitializeThread(CurrentProcess, CurrentThread, NULL, NULL, NULL, NULL, NULL, ArKernelBootStack, TRUE); - CurrentThread->NextProcessor = Prcb->CpuNumber; - CurrentThread->Priority = THREAD_HIGH_PRIORITY; - CurrentThread->State = Running; - CurrentThread->Affinity = (ULONG_PTR)1 << Prcb->CpuNumber; - CurrentThread->WaitRunLevel = DISPATCH_LEVEL; - CurrentProcess->ActiveProcessors |= (ULONG_PTR)1 << Prcb->CpuNumber; } /** @@ -91,9 +55,46 @@ XTAPI VOID KepStartKernel(VOID) { + PKPROCESSOR_CONTROL_BLOCK Prcb; + ULONG_PTR PageDirectory[2]; + PKPROCESS CurrentProcess; + PKTHREAD CurrentThread; + + /* Get processor control block and current thread */ + Prcb = KeGetCurrentProcessorControlBlock(); + CurrentThread = KeGetCurrentThread(); + + /* Get current process */ + CurrentProcess = CurrentThread->ApcState.Process; + + /* Initialize CPU power state structures */ + PoInitializeProcessorControlBlock(Prcb); + + /* Save processor state */ + KepSaveProcessorState(&Prcb->ProcessorState); + + /* Lower to APC runlevel */ + KeLowerRunLevel(APC_LEVEL); + /* Initialize XTOS kernel */ KepInitializeKernel(); + /* Initialize Idle process */ + RtlInitializeListHead(&KepProcessListHead); + PageDirectory[0] = 0; + PageDirectory[1] = 0; + KeInitializeProcess(CurrentProcess, 0, 0xFFFFFFFF, PageDirectory, FALSE); + CurrentProcess->Quantum = MAXCHAR; + + /* Initialize Idle thread */ + KeInitializeThread(CurrentProcess, CurrentThread, NULL, NULL, NULL, NULL, NULL, ArKernelBootStack, TRUE); + CurrentThread->NextProcessor = Prcb->CpuNumber; + CurrentThread->Priority = THREAD_HIGH_PRIORITY; + CurrentThread->State = Running; + CurrentThread->Affinity = (ULONG_PTR)1 << Prcb->CpuNumber; + CurrentThread->WaitRunLevel = DISPATCH_LEVEL; + CurrentProcess->ActiveProcessors |= (ULONG_PTR)1 << Prcb->CpuNumber; + /* Enter infinite loop */ DebugPrint(L"KepStartKernel() finished. Entering infinite loop.\n"); for(;;); diff --git a/xtoskrnl/ke/i686/krnlinit.c b/xtoskrnl/ke/i686/krnlinit.c index 1a5ca5f..e762941 100644 --- a/xtoskrnl/ke/i686/krnlinit.c +++ b/xtoskrnl/ke/i686/krnlinit.c @@ -20,42 +20,6 @@ XTAPI VOID KepInitializeKernel(VOID) { - PKPROCESSOR_CONTROL_BLOCK Prcb; - ULONG_PTR PageDirectory[2]; - PKPROCESS CurrentProcess; - PKTHREAD CurrentThread; - - /* Get processor control block and current thread */ - Prcb = KeGetCurrentProcessorControlBlock(); - CurrentThread = KeGetCurrentThread(); - - /* Get current process */ - CurrentProcess = CurrentThread->ApcState.Process; - - /* Initialize CPU power state structures */ - PoInitializeProcessorControlBlock(Prcb); - - /* Save processor state */ - KepSaveProcessorState(&Prcb->ProcessorState); - - /* Lower to APC runlevel */ - KeLowerRunLevel(APC_LEVEL); - - /* Initialize Idle process */ - RtlInitializeListHead(&KepProcessListHead); - PageDirectory[0] = 0; - PageDirectory[1] = 0; - KeInitializeProcess(CurrentProcess, 0, 0xFFFFFFFF, PageDirectory, FALSE); - CurrentProcess->Quantum = MAXCHAR; - - /* Initialize Idle thread */ - KeInitializeThread(CurrentProcess, CurrentThread, NULL, NULL, NULL, NULL, NULL, ArKernelBootStack, TRUE); - CurrentThread->NextProcessor = Prcb->CpuNumber; - CurrentThread->Priority = THREAD_HIGH_PRIORITY; - CurrentThread->State = Running; - CurrentThread->Affinity = (ULONG_PTR)1 << Prcb->CpuNumber; - CurrentThread->WaitRunLevel = DISPATCH_LEVEL; - CurrentProcess->ActiveProcessors |= (ULONG_PTR)1 << Prcb->CpuNumber; } /** @@ -91,9 +55,46 @@ XTAPI VOID KepStartKernel(VOID) { + PKPROCESSOR_CONTROL_BLOCK Prcb; + ULONG_PTR PageDirectory[2]; + PKPROCESS CurrentProcess; + PKTHREAD CurrentThread; + + /* Get processor control block and current thread */ + Prcb = KeGetCurrentProcessorControlBlock(); + CurrentThread = KeGetCurrentThread(); + + /* Get current process */ + CurrentProcess = CurrentThread->ApcState.Process; + + /* Initialize CPU power state structures */ + PoInitializeProcessorControlBlock(Prcb); + + /* Save processor state */ + KepSaveProcessorState(&Prcb->ProcessorState); + + /* Lower to APC runlevel */ + KeLowerRunLevel(APC_LEVEL); + /* Initialize XTOS kernel */ KepInitializeKernel(); + /* Initialize Idle process */ + RtlInitializeListHead(&KepProcessListHead); + PageDirectory[0] = 0; + PageDirectory[1] = 0; + KeInitializeProcess(CurrentProcess, 0, 0xFFFFFFFF, PageDirectory, FALSE); + CurrentProcess->Quantum = MAXCHAR; + + /* Initialize Idle thread */ + KeInitializeThread(CurrentProcess, CurrentThread, NULL, NULL, NULL, NULL, NULL, ArKernelBootStack, TRUE); + CurrentThread->NextProcessor = Prcb->CpuNumber; + CurrentThread->Priority = THREAD_HIGH_PRIORITY; + CurrentThread->State = Running; + CurrentThread->Affinity = (ULONG_PTR)1 << Prcb->CpuNumber; + CurrentThread->WaitRunLevel = DISPATCH_LEVEL; + CurrentProcess->ActiveProcessors |= (ULONG_PTR)1 << Prcb->CpuNumber; + /* Enter infinite loop */ DebugPrint(L"KepStartKernel() finished. Entering infinite loop.\n"); for(;;);