diff --git a/reactos/ntoskrnl/amd64stubs.c b/reactos/ntoskrnl/amd64stubs.c index 6da6ea2ca71..fccd94cca8d 100644 --- a/reactos/ntoskrnl/amd64stubs.c +++ b/reactos/ntoskrnl/amd64stubs.c @@ -69,7 +69,6 @@ STUB(_ExSemaphoreObjectType) STUB(KeFeatureBits) STUB(KiSystemService) STUB(KdpGdbStubInit) -STUB(KdbpGetCommandLineSettings) STUB(KdbpSafeReadMemory) STUB(RtlFillMemoryUlong) STUB(RtlCaptureContext) diff --git a/reactos/ntoskrnl/kd/amd64/kd.c b/reactos/ntoskrnl/kd/amd64/kd.c new file mode 100644 index 00000000000..ae6a1b4bd3e --- /dev/null +++ b/reactos/ntoskrnl/kd/amd64/kd.c @@ -0,0 +1,30 @@ + +#include +#define NDEBUG +#include + +VOID +STDCALL +KdbpGetCommandLineSettings(PCHAR p1) +{ + PCHAR p2; + + while (p1 && (p2 = strchr(p1, ' '))) + { + p2++; + + if (!_strnicmp(p2, "KDSERIAL", 8)) + { + p2 += 8; + KdbDebugState |= KD_DEBUG_KDSERIAL; + KdpDebugMode.Serial = TRUE; + } + else if (!_strnicmp(p2, "KDNOECHO", 8)) + { + p2 += 8; + KdbDebugState |= KD_DEBUG_KDNOECHO; + } + + p1 = p2; + } +} diff --git a/reactos/ntoskrnl/kdbg/amd64/kdb.c b/reactos/ntoskrnl/kdbg/amd64/kdb.c new file mode 100644 index 00000000000..0fa9f7d0597 --- /dev/null +++ b/reactos/ntoskrnl/kdbg/amd64/kdb.c @@ -0,0 +1,47 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS kernel + * FILE: ntoskrnl/kdbg/kdb.c + * PURPOSE: Kernel Debugger + * + * PROGRAMMERS: Gregor Anich + */ + +/* INCLUDES ******************************************************************/ + +#include +#define NDEBUG +#include + +/* GLOBALS *******************************************************************/ + +ULONG KdbDebugState = 0; /* KDBG Settings (NOECHO, KDSERIAL) */ + +/* FUNCTIONS *****************************************************************/ + +VOID +STDCALL +KdbpGetCommandLineSettings(PCHAR p1) +{ + PCHAR p2; + + while (p1 && (p2 = strchr(p1, ' '))) + { + p2++; + + if (!_strnicmp(p2, "KDSERIAL", 8)) + { + p2 += 8; + KdbDebugState |= KD_DEBUG_KDSERIAL; + KdpDebugMode.Serial = TRUE; + } + else if (!_strnicmp(p2, "KDNOECHO", 8)) + { + p2 += 8; + KdbDebugState |= KD_DEBUG_KDNOECHO; + } + + p1 = p2; + } +} + diff --git a/reactos/ntoskrnl/ke/amd64/cpu.c b/reactos/ntoskrnl/ke/amd64/cpu.c index 0b0efcbac1b..083e4968d3c 100644 --- a/reactos/ntoskrnl/ke/amd64/cpu.c +++ b/reactos/ntoskrnl/ke/amd64/cpu.c @@ -456,30 +456,45 @@ Ki386InitializeTss(IN PKTSS64 Tss, IN PKGDTENTRY Gdt, IN UINT64 Stack) { - PKGDTENTRY TssEntry; + PKGDTENTRY64 TssEntry; - /* Initialize the boot TSS entry */ - TssEntry = &Gdt[KGDT_TSS / sizeof(KGDTENTRY)]; - TssEntry->Bits.Type = I386_TSS; - TssEntry->Bits.Present = 1; + /* Initialize the TSS descriptor entry */ + TssEntry = (PVOID)((ULONG64)Gdt + KGDT_TSS); + TssEntry->Bits.Type = 9;//AMD64_TSS; TssEntry->Bits.Dpl = 0; + TssEntry->Bits.Present = 1; + TssEntry->Bits.System = 0; + TssEntry->Bits.LongMode = 0; + TssEntry->Bits.DefaultBig = 0; + TssEntry->Bits.Granularity = 0; - /* FIXME: I/O Map */ + /* Descriptor base is the TSS address */ + TssEntry->BaseLow = (ULONG64)Tss & 0xffff; + TssEntry->Bits.BaseMiddle = ((ULONG64)Tss >> 16) & 0xff; + TssEntry->Bits.BaseHigh = ((ULONG64)Tss >> 24) & 0xff; + TssEntry->BaseUpper = (ULONG64)Tss >> 32; - /* Load the task register */ - Ke386SetTr(KGDT_TSS); + /* Set the limit */ + TssEntry->LimitLow = sizeof(KTSS64) -1; + TssEntry->Bits.LimitHigh = 0; - /* Setup stack pointer */ + /* FIXME: I/O Map? */ + Tss->IoMapBase = 0; + + /* Setup ring 0 stack pointer */ Tss->Rsp0 = Stack; /* Setup a stack for Double Fault Traps */ - Tss->Ist[1] = PtrToUlong(KiDoubleFaultStack); + Tss->Ist[1] = (ULONG64)KiDoubleFaultStack; /* Setup a stack for CheckAbort Traps */ - Tss->Ist[2] = PtrToUlong(KiDoubleFaultStack); + Tss->Ist[2] = (ULONG64)KiDoubleFaultStack; /* Setup a stack for NMI Traps */ - Tss->Ist[3] = PtrToUlong(KiDoubleFaultStack); + Tss->Ist[3] = (ULONG64)KiDoubleFaultStack; + + /* Load the task register */ + Ke386SetTr(KGDT_TSS); } diff --git a/reactos/ntoskrnl/ke/amd64/except.c b/reactos/ntoskrnl/ke/amd64/except.c index c21905471be..1eaae2903f1 100644 --- a/reactos/ntoskrnl/ke/amd64/except.c +++ b/reactos/ntoskrnl/ke/amd64/except.c @@ -87,5 +87,6 @@ KeInitExceptions(VOID) KiIdt[i].Reserved1 = 0; } + __lidt(&KiIdtDescriptor.Limit); } diff --git a/reactos/ntoskrnl/ke/amd64/thrdini.c b/reactos/ntoskrnl/ke/amd64/thrdini.c new file mode 100644 index 00000000000..199dc769fc0 --- /dev/null +++ b/reactos/ntoskrnl/ke/amd64/thrdini.c @@ -0,0 +1,231 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS kernel + * FILE: ntoskrnl/ke/i386/thread.c + * PURPOSE: i386 Thread Context Creation + * PROGRAMMER: Alex Ionescu (alex@relsoft.net) + */ + +/* INCLUDES ******************************************************************/ + +#include +#define NDEBUG +#include +#if 0 +typedef struct _KSWITCHFRAME +{ + PVOID ExceptionList; + BOOLEAN ApcBypassDisable; + PVOID RetAddr; +} KSWITCHFRAME, *PKSWITCHFRAME; + +typedef struct _KSTART_FRAME +{ + PKSYSTEM_ROUTINE SystemRoutine; + PKSTART_ROUTINE StartRoutine; + PVOID StartContext; + BOOLEAN UserThread; +} KSTART_FRAME, *PKSTART_FRAME; + +typedef struct _KUINIT_FRAME +{ + KSWITCHFRAME CtxSwitchFrame; + KSTART_FRAME StartFrame; + KTRAP_FRAME TrapFrame; + FX_SAVE_AREA FxSaveArea; +} KUINIT_FRAME, *PKUINIT_FRAME; + +typedef struct _KKINIT_FRAME +{ + KSWITCHFRAME CtxSwitchFrame; + KSTART_FRAME StartFrame; + FX_SAVE_AREA FxSaveArea; +} KKINIT_FRAME, *PKKINIT_FRAME; +#endif +/* FUNCTIONS *****************************************************************/ + +VOID +NTAPI +Ke386InitThreadWithContext(IN PKTHREAD Thread, + IN PKSYSTEM_ROUTINE SystemRoutine, + IN PKSTART_ROUTINE StartRoutine, + IN PVOID StartContext, + IN PCONTEXT ContextPointer) +{ + FrLdrDbgPrint("Ke386InitThreadWithContext stub\n"); +#if 0 + PFX_SAVE_AREA FxSaveArea; + PFXSAVE_FORMAT FxSaveFormat; + PKSTART_FRAME StartFrame; + PKSWITCHFRAME CtxSwitchFrame; + PKTRAP_FRAME TrapFrame; + CONTEXT LocalContext; + PCONTEXT Context = NULL; + ULONG ContextFlags; + + /* Check if this is a With-Context Thread */ + if (ContextPointer) + { + /* Set up the Initial Frame */ + PKUINIT_FRAME InitFrame; + InitFrame = (PKUINIT_FRAME)((ULONG_PTR)Thread->InitialStack - + sizeof(KUINIT_FRAME)); + + /* Copy over the context we got */ + RtlCopyMemory(&LocalContext, ContextPointer, sizeof(CONTEXT)); + Context = &LocalContext; + ContextFlags = CONTEXT_CONTROL; + + /* Zero out the trap frame and save area */ + RtlZeroMemory(&InitFrame->TrapFrame, + KTRAP_FRAME_LENGTH + sizeof(FX_SAVE_AREA)); + + /* Setup the Fx Area */ + FxSaveArea = &InitFrame->FxSaveArea; + + /* Check if we support FXsr */ + if (KeI386FxsrPresent) + { + /* Get the FX Save Format Area */ + FxSaveFormat = (PFXSAVE_FORMAT)Context->ExtendedRegisters; + + /* Set an initial state */ + FxSaveFormat->ControlWord = 0x27F; + FxSaveFormat->StatusWord = 0; + FxSaveFormat->TagWord = 0; + FxSaveFormat->ErrorOffset = 0; + FxSaveFormat->ErrorSelector = 0; + FxSaveFormat->DataOffset = 0; + FxSaveFormat->DataSelector = 0; + FxSaveFormat->MXCsr = 0x1F80; + } + else + { + /* Setup the regular save area */ + Context->FloatSave.ControlWord = 0x27F; + Context->FloatSave.StatusWord = 0; + Context->FloatSave.TagWord = -1; + Context->FloatSave.ErrorOffset = 0; + Context->FloatSave.ErrorSelector = 0; + Context->FloatSave.DataOffset =0; + Context->FloatSave.DataSelector = 0; + } + + /* Check if the CPU has NPX */ + if (KeI386NpxPresent) + { + /* Set an intial NPX State */ + Context->FloatSave.Cr0NpxState = 0; + FxSaveArea->Cr0NpxState = 0; + FxSaveArea->NpxSavedCpu = 0; + + /* Now set the context flags depending on XMM support */ + ContextFlags |= (KeI386FxsrPresent) ? CONTEXT_EXTENDED_REGISTERS : + CONTEXT_FLOATING_POINT; + + /* Set the Thread's NPX State */ + Thread->NpxState = NPX_STATE_NOT_LOADED; + Thread->DispatcherHeader.NpxIrql = PASSIVE_LEVEL; + } + else + { + /* We'll use emulation */ + FxSaveArea->Cr0NpxState = CR0_EM; + Thread->NpxState = NPX_STATE_NOT_LOADED &~ CR0_MP; + } + + /* Disable any debug regiseters */ + Context->ContextFlags &= ~CONTEXT_DEBUG_REGISTERS; + + /* Setup the Trap Frame */ + TrapFrame = &InitFrame->TrapFrame; + + /* Set up a trap frame from the context. */ + KeContextToTrapFrame(Context, + NULL, + TrapFrame, + Context->ContextFlags | ContextFlags, + UserMode); + + /* Set SS, DS, ES's RPL Mask properly */ + TrapFrame->HardwareSegSs |= RPL_MASK; + TrapFrame->SegDs |= RPL_MASK; + TrapFrame->SegEs |= RPL_MASK; + TrapFrame->Dr7 = 0; + + /* Set the debug mark */ + TrapFrame->DbgArgMark = 0xBADB0D00; + + /* Set the previous mode as user */ + TrapFrame->PreviousPreviousMode = UserMode; + + /* Terminate the Exception Handler List */ + TrapFrame->ExceptionList = EXCEPTION_CHAIN_END; + + /* Setup the Stack for KiThreadStartup and Context Switching */ + StartFrame = &InitFrame->StartFrame; + CtxSwitchFrame = &InitFrame->CtxSwitchFrame; + + /* Tell the thread it will run in User Mode */ + Thread->PreviousMode = UserMode; + + /* Tell KiThreadStartup of that too */ + StartFrame->UserThread = TRUE; + } + else + { + /* Set up the Initial Frame for the system thread */ + PKKINIT_FRAME InitFrame; + InitFrame = (PKKINIT_FRAME)((ULONG_PTR)Thread->InitialStack - + sizeof(KKINIT_FRAME)); + + /* Setup the Fx Area */ + FxSaveArea = &InitFrame->FxSaveArea; + RtlZeroMemory(FxSaveArea, sizeof(FX_SAVE_AREA)); + + /* Check if we have Fxsr support */ + if (KeI386FxsrPresent) + { + /* Set the stub FX area */ + FxSaveArea->U.FxArea.ControlWord = 0x27F; + FxSaveArea->U.FxArea.MXCsr = 0x1F80; + } + else + { + /* Set the stub FN area */ + FxSaveArea->U.FnArea.ControlWord = 0x27F; + FxSaveArea->U.FnArea.TagWord = -1; + } + + /* No NPX State */ + Thread->NpxState = NPX_STATE_NOT_LOADED; + + /* Setup the Stack for KiThreadStartup and Context Switching */ + StartFrame = &InitFrame->StartFrame; + CtxSwitchFrame = &InitFrame->CtxSwitchFrame; + + /* Tell the thread it will run in Kernel Mode */ + Thread->PreviousMode = KernelMode; + + /* Tell KiThreadStartup of that too */ + StartFrame->UserThread = FALSE; + } + + /* Now setup the remaining data for KiThreadStartup */ + StartFrame->StartContext = StartContext; + StartFrame->StartRoutine = StartRoutine; + StartFrame->SystemRoutine = SystemRoutine; + + /* And set up the Context Switch Frame */ + CtxSwitchFrame->RetAddr = KiThreadStartup; + CtxSwitchFrame->ApcBypassDisable = TRUE; + CtxSwitchFrame->ExceptionList = EXCEPTION_CHAIN_END;; + + /* Save back the new value of the kernel stack. */ + Thread->KernelStack = (PVOID)CtxSwitchFrame; +#endif +} + +/* EOF */ + + diff --git a/reactos/ntoskrnl/ke/amd64/trap.S b/reactos/ntoskrnl/ke/amd64/trap.S index 5c43aae70ca..a79ea919213 100644 --- a/reactos/ntoskrnl/ke/amd64/trap.S +++ b/reactos/ntoskrnl/ke/amd64/trap.S @@ -17,6 +17,9 @@ .data +_MsgPageFault: +.ascii "Page fault 0x%x at %p!\n\0" + _MsgGeneralProtFault: .ascii "General protection fault at %p!\n\0" @@ -33,55 +36,95 @@ _MsgUnexpectedInterrupt: .global _KiDivideErrorFault _KiDivideErrorFault: + /* Push pseudo error code */ + push 0 + .global _KiDebugTrapOrFault _KiDebugTrapOrFault: + /* Push pseudo error code */ + push 0 .global _KiNmiInterrupt _KiNmiInterrupt: + /* Push pseudo error code */ + push 0 + +jmp $ .global _KiBreakpointTrap _KiBreakpointTrap: -// mov rdx, [rsp] + /* Push pseudo error code */ + push 0 + + push rax + push rcx + push rdx + sub rsp, 0x10 -// movabs rcx, offset _MsgBreakpointTrap -// movabs rax, offset _FrLdrDbgPrint -// call [rax] + movabs rcx, offset _MsgBreakpointTrap + mov rdx, [rsp + 0x10 + 24 + 8] + movabs rax, offset _FrLdrDbgPrint + call [rax] add rsp, 0x10 - iret + + pop rdx + pop rcx + pop rax + add rsp, 8 + iretq .global _KiOverflowTrap _KiOverflowTrap: + /* Push pseudo error code */ + push 0 .global _KiBoundFault _KiBoundFault: + /* Push pseudo error code */ + push 0 .global _KiInvalidOpcodeFault _KiInvalidOpcodeFault: + /* Push pseudo error code */ + push 0 .global _KiNpxNotAvailableFault _KiNpxNotAvailableFault: + /* Push pseudo error code */ + push 0 .global _KiDoubleFaultAbort _KiDoubleFaultAbort: + /* Push pseudo error code */ + push 0 .global _KiNpxSegmentOverrunAbort _KiNpxSegmentOverrunAbort: + /* Push pseudo error code */ + push 0 .global _KiInvalidTssFault _KiInvalidTssFault: + /* We have an error code */ + .global _KiSegmentNotPresentFault _KiSegmentNotPresentFault: + /* We have an error code */ + .global _KiStackFault _KiStackFault: + /* We have an error code */ jmp $ .global _KiGeneralProtectionFault _KiGeneralProtectionFault: + /* We have an error code */ + mov rdx, 0 mov dx, ss movabs rcx, offset _MsgGeneralProtFault @@ -92,18 +135,34 @@ _KiGeneralProtectionFault: .global _KiPageFault _KiPageFault: + /* We have an error code */ + movabs rcx, offset _MsgPageFault + mov rdx, [rsp] + mov r8, [rsp+8] + mov r9, rsp + movabs rax, offset _FrLdrDbgPrint + call [rax] + jmp $ + .global _KiFloatingErrorFault _KiFloatingErrorFault: + /* Push pseudo error code */ + push 0 .global _KiAlignmentFault _KiAlignmentFault: + /* We have an error code */ .global _KiMcheckAbort _KiMcheckAbort: + /* Push pseudo error code */ + push 0 .global _KiXmmException _KiXmmException: + /* Push pseudo error code */ + push 0 .global _KiApcInterrupt _KiApcInterrupt: @@ -111,9 +170,40 @@ _KiApcInterrupt: .global _KiRaiseAssertion _KiRaiseAssertion: +jmp $ .global _KiDebugServiceTrap _KiDebugServiceTrap: + /* Push pseudo error code */ + push 0 + + push rax + push rcx + push rdx + push r8 + push r9 + + /* Create stack space for parameters */ + sub rsp, 0x18 + + /* just forward first 3 parameters */ + call _KdpServiceDispatcher + + /* Skip the int 3, increment return rip */ + inc qword ptr [rsp + 0x18 + 48] + + /* Cleanup */ + add rsp, 0x18 + + pop r9 + pop r8 + pop rdx + pop rcx + pop rax + + add rsp, 8 + iretq + .global _KiDpcInterrupt _KiDpcInterrupt: @@ -121,10 +211,12 @@ _KiDpcInterrupt: .global _KiIpiInterrupt _KiIpiInterrupt: - iret +jmp $ + .global _KiUnexpectedInterrupt _KiUnexpectedInterrupt: +jmp $ movabs rcx, offset _MsgUnexpectedInterrupt movabs rax, offset _FrLdrDbgPrint call [rax] diff --git a/reactos/ntoskrnl/ntoskrnl-amd64hack.rbuild b/reactos/ntoskrnl/ntoskrnl-amd64hack.rbuild index 3c010044f10..e450f445c04 100644 --- a/reactos/ntoskrnl/ntoskrnl-amd64hack.rbuild +++ b/reactos/ntoskrnl/ntoskrnl-amd64hack.rbuild @@ -95,6 +95,7 @@ except.c irql.c kiinit.c + thrdini.c trap.S @@ -303,6 +304,11 @@ kdb_keyboard.c kdb_serial.c + + + kdb.c + + kdb_symbols.c