From 734fd03c8162816e0743bec0150c0135c32dd93a Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Thu, 21 Aug 2008 21:16:57 +0000 Subject: [PATCH] Implement KiSystemStartup in C instead of asm, implement KiSwapStack intrinsic, get rid of KiSetupStackandInitializeKernel, instead do the work in KiSystemStartupReal in C. Move Stack definitions into trap.S and get rid of the whole boot.S file. Comment out the sync loop for the moment and add a comment why it doesn't work (InterlockedBitTestAndSet64 doesn't work correctly). svn path=/branches/ros-amd64-bringup/; revision=35518 --- .../include/internal/amd64/intrin_i.h | 11 +++ reactos/ntoskrnl/ke/amd64/boot.S | 86 ------------------- reactos/ntoskrnl/ke/amd64/kiinit.c | 52 ++++++++--- reactos/ntoskrnl/ke/amd64/trap.S | 15 ++++ reactos/ntoskrnl/ntoskrnl-amd64hack.rbuild | 1 - 5 files changed, 66 insertions(+), 99 deletions(-) delete mode 100644 reactos/ntoskrnl/ke/amd64/boot.S diff --git a/reactos/ntoskrnl/include/internal/amd64/intrin_i.h b/reactos/ntoskrnl/include/internal/amd64/intrin_i.h index a09c4acdc31..9aa185c37ba 100644 --- a/reactos/ntoskrnl/include/internal/amd64/intrin_i.h +++ b/reactos/ntoskrnl/include/internal/amd64/intrin_i.h @@ -11,6 +11,17 @@ #if defined(__GNUC__) +ULONG64 +FORCEINLINE +KiSwapStack(ULONG64 NewStack) +{ + ULONG64 OldStack; + asm volatile ("movq %%rsp, %[oldstack]\n movq %[newstack], %%rsp\n" + : [oldstack] "=rm" (OldStack) + : [newstack] "rm" (NewStack)); + return OldStack; +} + #define Ke386SetInterruptDescriptorTable(X) \ __asm__("lidt %0\n\t" \ : /* no outputs */ \ diff --git a/reactos/ntoskrnl/ke/amd64/boot.S b/reactos/ntoskrnl/ke/amd64/boot.S deleted file mode 100644 index d3064c92b3d..00000000000 --- a/reactos/ntoskrnl/ke/amd64/boot.S +++ /dev/null @@ -1,86 +0,0 @@ -/* - * FILE: ntoskrnl/ke/i386/boot.S - * COPYRIGHT: See COPYING in the top level directory - * PURPOSE: FreeLDR Wrapper Bootstrap Code and Bootstrap Trampoline - * PROGRAMMERs: Alex Ionescu (alex@relsoft.net) - * Thomas Weidenmueller - */ - -/* INCLUDES ******************************************************************/ - -#include -.intel_syntax noprefix -.code64 - -/* GLOBALS *******************************************************************/ - -.bss -.align 16 - -/* Kernel Boot Stack */ -.globl _P0BootStack -.space KERNEL_STACK_SIZE -_P0BootStack: - -/* Kernel Double-Fault and Temporary DPC Stack */ -.globl _KiDoubleFaultStack -.space KERNEL_STACK_SIZE -_KiDoubleFaultStack: - -/* FUNCTIONS *****************************************************************/ -.global _KiSystemStartup -.text -.func KiSystemStartup -_KiSystemStartup: - - /* NTLDR Boot: Call the main kernel initialization */ - test rcx, 0x80000000 - jnz _KiSystemStartupReal - - /* FREELDR Boot: Cal the FreeLDR wrapper */ - jmp _KiRosPrepareForSystemStartup -.endfunc - -/** - * VOID - * KiSetupStackAndInitializeKernel( - * esp+4 = ? -> rcx - * esp+8 = ? -> rdx - * PVOID pNewstack // esp+12 = new stack -> r8 - * esp+16 -> r9 - * esp+20 -> rsp + 8 - * esp+24 -> rsp + 16? - */ -.globl _KiSetupStackAndInitializeKernel -.func KiSetupStackAndInitializeKernel -_KiSetupStackAndInitializeKernel: - - /* Save current stack */ - mov rsi, rsp - - /* Setup the new stack */ - mov rsp, r8 - sub rsp, NPX_FRAME_LENGTH + KTRAP_FRAME_ALIGN + KTRAP_FRAME_LENGTH - push CR0_EM + CR0_TS + CR0_MP - - /* Copy stack parameters to the new stack */ - push [rsi + 16] - push [rsi + 8] - xor rbp, rbp - call _KiInitializeKernel - - /* Set the priority of this thread to 0 */ - mov rbx, PCR[KPCR_CURRENT_THREAD] - mov byte ptr [rbx+KTHREAD_PRIORITY], 0 - - /* Force interrupts enabled and lower IRQL back to DISPATCH_LEVEL */ - sti - mov rcx, DISPATCH_LEVEL - mov cr8, rcx - - /* Set the right wait IRQL */ - mov byte ptr [rbx+KTHREAD_WAIT_IRQL], DISPATCH_LEVEL; - - /* Jump into the idle loop */ - jmp _KiIdleLoop -.endfunc diff --git a/reactos/ntoskrnl/ke/amd64/kiinit.c b/reactos/ntoskrnl/ke/amd64/kiinit.c index f1e7bd6757c..5d16072155d 100644 --- a/reactos/ntoskrnl/ke/amd64/kiinit.c +++ b/reactos/ntoskrnl/ke/amd64/kiinit.c @@ -366,6 +366,7 @@ KiInitializePcr(IN ULONG ProcessorNumber, /* Start us out at PASSIVE_LEVEL */ // Pcr->Irql = PASSIVE_LEVEL; + KeSetCurrentIrql(PASSIVE_LEVEL); /* Set the GDI, IDT, TSS and DPC Stack */ Pcr->GdtBase = (PVOID)Gdt; @@ -386,6 +387,7 @@ KiInitializeKernel(IN PKPROCESS InitProcess, IN CCHAR Number, IN PLOADER_PARAMETER_BLOCK LoaderBlock) { + FrLdrDbgPrint("Enter KiInitializeKernel\n"); #if 0 BOOLEAN NpxPresent; ULONG FeatureBits; @@ -642,6 +644,17 @@ KiGetMachineBootPointers(IN PKGDTENTRY *Gdt, (ULONG64)TssSelector.BaseUpper << 32); } +// Hack +VOID KiRosPrepareForSystemStartup(ULONG, PROS_LOADER_PARAMETER_BLOCK); + +VOID +NTAPI +KiSystemStartup(IN ULONG_PTR Dummy, + IN PROS_LOADER_PARAMETER_BLOCK LoaderBlock) +{ + KiRosPrepareForSystemStartup(Dummy, LoaderBlock); +} + VOID NTAPI KiSystemStartupReal(IN PLOADER_PARAMETER_BLOCK LoaderBlock) @@ -740,13 +753,15 @@ FrLdrDbgPrint("Gdt = %p, Idt = %p, Pcr = %p, Tss = %p\n", Gdt, Idt, Pcr, Tss); // RtlCopyMemory(&Idt[8], &DoubleFaultEntry, sizeof(KIDTENTRY)); } - +#if 0 // FIXME: InterlockedBitTestAndSet64 is broken! It needs to be specified + // that it should reference an absolute address! /* Loop until we can release the freeze lock */ do { /* Loop until execution can continue */ while (*(volatile PKSPIN_LOCK*)&KiFreezeExecutionLock == (PVOID)1); - } while(InterlockedBitTestAndSet((PLONG)&KiFreezeExecutionLock, 0)); + } while(InterlockedBitTestAndSet64((PLONG64)&KiFreezeExecutionLock, 0)); +#endif /* Setup CPU-related fields */ Pcr->Prcb.Number = Cpu; @@ -773,16 +788,29 @@ FrLdrDbgPrint("Gdt = %p, Idt = %p, Pcr = %p, Tss = %p\n", Gdt, Idt, Pcr, Tss); KfRaiseIrql(HIGH_LEVEL); /* Align stack and make space for the trap frame and NPX frame */ - InitialStack &= ~(KTRAP_FRAME_ALIGN - 1); + InitialStack &= ~(16 - 1); -FrLdrDbgPrint("Before KiSetupStackAndInitializeKernel\n"); -for(;;); + /* Switch to new kernel Stack */ + KiSwapStack(InitialStack); - /* Switch to new kernel stack and start kernel bootstrapping */ - KiSetupStackAndInitializeKernel(&KiInitialProcess.Pcb, - InitialThread, - (PVOID)InitialStack, - &Pcr->Prcb, - (CCHAR)Cpu, - KeLoaderBlock); + /* Initialize kernel */ + KiInitializeKernel(&KiInitialProcess.Pcb, + InitialThread, + (PVOID)InitialStack, + &Pcr->Prcb, + (CCHAR)Cpu, + KeLoaderBlock); + + /* Set the priority of this thread to 0 */ + InitialThread->Priority = 0; + + /* Force interrupts enabled and lower IRQL back to DISPATCH_LEVEL */ + _enable(); + KeLowerIrql(DISPATCH_LEVEL); + + /* Set the right wait IRQL */ + InitialThread->WaitIrql = DISPATCH_LEVEL; + + /* Jump into the idle loop */ + KiIdleLoop(); } diff --git a/reactos/ntoskrnl/ke/amd64/trap.S b/reactos/ntoskrnl/ke/amd64/trap.S index 5c43aae70ca..02145c03d73 100644 --- a/reactos/ntoskrnl/ke/amd64/trap.S +++ b/reactos/ntoskrnl/ke/amd64/trap.S @@ -13,8 +13,23 @@ .intel_syntax noprefix .code64 +#define KERNEL_STACK_SIZE 0x6000 + /* GLOBALS *******************************************************************/ +.bss +.align 16 + +/* Kernel Boot Stack */ +.globl _P0BootStack +.space KERNEL_STACK_SIZE +_P0BootStack: + +/* Kernel Double-Fault and Temporary DPC Stack */ +.globl _KiDoubleFaultStack +.space KERNEL_STACK_SIZE +_KiDoubleFaultStack: + .data _MsgGeneralProtFault: diff --git a/reactos/ntoskrnl/ntoskrnl-amd64hack.rbuild b/reactos/ntoskrnl/ntoskrnl-amd64hack.rbuild index 3c010044f10..26b64a0e83a 100644 --- a/reactos/ntoskrnl/ntoskrnl-amd64hack.rbuild +++ b/reactos/ntoskrnl/ntoskrnl-amd64hack.rbuild @@ -90,7 +90,6 @@ - boot.S cpu.c except.c irql.c