From 47dd86037dcd0338292fbb921a681655deecf96b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Tue, 10 Dec 2024 22:20:42 +0100 Subject: [PATCH] ** WIP ** Ensure proper port parameter overriding retrieval in case the debugger gets enabled not at boot, but later at runtime --- drivers/base/kdcom/kdcom.c | 212 ++++++++++++++++++++++-------------- drivers/base/kdcom/kddll.h | 2 +- drivers/base/kdgdb/kdcom.c | 199 +++++++++++++++++++-------------- ntoskrnl/kd/kd.h | 8 +- ntoskrnl/kd/kdio.c | 46 +++++--- ntoskrnl/kd/kdmain.c | 71 +++++++----- ntoskrnl/kdbg/kdb.h | 4 +- ntoskrnl/kdbg/kdb_cli.c | 28 +++-- ntoskrnl/kdbg/kdb_symbols.c | 14 +-- 9 files changed, 356 insertions(+), 228 deletions(-) diff --git a/drivers/base/kdcom/kdcom.c b/drivers/base/kdcom/kdcom.c index 49c722cbf14..8eb68733dac 100644 --- a/drivers/base/kdcom/kdcom.c +++ b/drivers/base/kdcom/kdcom.c @@ -148,6 +148,112 @@ KdpPortInitialize(IN ULONG ComPortNumber, return STATUS_SUCCESS; } + +/** + * @brief + * Loads port parameters from the Loader Parameter Block, if available. + **/ +static NTSTATUS +KdpRetrieveParameters( + _In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock) +{ + static BOOLEAN AreParamsRetrieved = FALSE; + PSTR CommandLine, PortString, BaudString, IrqString; + ULONG Value; + +KDDBGPRINT("%s(%p)\n", __FUNCTION__, LoaderBlock); + + /* Load parameters only once if they haven't been already */ + if (AreParamsRetrieved) + return STATUS_SUCCESS; + AreParamsRetrieved = TRUE; + + /* Check if we have a loader block, and if not, attempt to use the + * system one. If it's unavailable (post phase-1 init), just return. */ + if (!LoaderBlock) + LoaderBlock = KeLoaderBlock; + if (!LoaderBlock) + return STATUS_SUCCESS; + + /* Check if we have a command line */ + CommandLine = LoaderBlock->LoadOptions; + if (!CommandLine) + return STATUS_SUCCESS; + + /* Upcase it */ + _strupr(CommandLine); + + /* Check if we got the /DEBUGPORT parameter */ + PortString = strstr(CommandLine, "DEBUGPORT"); + if (PortString) + { + /* Move past the actual string, to reach the port*/ + PortString += strlen("DEBUGPORT"); + + /* Now get past any spaces and skip the equal sign */ + while (*PortString == ' ') PortString++; + PortString++; + + /* Do we have a serial port? */ + if (strncmp(PortString, "COM", 3) != 0) + { + return STATUS_INVALID_PARAMETER; + } + + /* Check for a valid Serial Port */ + PortString += 3; + Value = atol(PortString); + if (Value >= sizeof(BaseArray) / sizeof(BaseArray[0])) + { + return STATUS_INVALID_PARAMETER; + } + + /* Set the port to use */ + ComPortNumber = Value; + } + + /* Check if we got a baud rate */ + BaudString = strstr(CommandLine, "BAUDRATE"); + if (BaudString) + { + /* Move past the actual string, to reach the rate */ + BaudString += strlen("BAUDRATE"); + + /* Now get past any spaces */ + while (*BaudString == ' ') BaudString++; + + /* And make sure we have a rate */ + if (*BaudString) + { + /* Read and set it */ + Value = atol(BaudString + 1); + if (Value) ComPortBaudRate = Value; + } + } + + /* Check Serial Port Settings [IRQ] */ + IrqString = strstr(CommandLine, "IRQ"); + if (IrqString) + { + /* Move past the actual string, to reach the rate */ + IrqString += strlen("IRQ"); + + /* Now get past any spaces */ + while (*IrqString == ' ') IrqString++; + + /* And make sure we have an IRQ */ + if (*IrqString) + { + /* Read and set it */ + Value = atol(IrqString + 1); + if (Value) ComPortIrq = Value; + } + } + + return STATUS_SUCCESS; +} + + /** * @brief * Phase 0 initialization. Invoked by KdInitSystem() when the debugger @@ -165,88 +271,18 @@ KdDebuggerInitialize0( _In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock) { NTSTATUS Status; - PCHAR CommandLine, PortString, BaudString, IrqString; - ULONG Value; - /* Check if we have a LoaderBlock */ - if (LoaderBlock) - { - /* Get the Command Line */ - CommandLine = LoaderBlock->LoadOptions; +#ifdef KDDEBUG +CpInitialize(&KdDebugComPort, UlongToPtr(BaseArray[/*ComPort*/4]), /*DEFAULT_BAUD_RATE*/115200); +#endif +KDDBGPRINT("KdDebuggerInitialize0\n"); - /* Upcase it */ - _strupr(CommandLine); - - /* Get the port and baud rate */ - PortString = strstr(CommandLine, "DEBUGPORT"); - BaudString = strstr(CommandLine, "BAUDRATE"); - IrqString = strstr(CommandLine, "IRQ"); - - /* Check if we got the /DEBUGPORT parameter */ - if (PortString) - { - /* Move past the actual string, to reach the port*/ - PortString += strlen("DEBUGPORT"); - - /* Now get past any spaces and skip the equal sign */ - while (*PortString == ' ') PortString++; - PortString++; - - /* Do we have a serial port? */ - if (strncmp(PortString, "COM", 3) != 0) - { - return STATUS_INVALID_PARAMETER; - } - - /* Check for a valid Serial Port */ - PortString += 3; - Value = atol(PortString); - if (Value >= sizeof(BaseArray) / sizeof(BaseArray[0])) - { - return STATUS_INVALID_PARAMETER; - } - - /* Set the port to use */ - ComPortNumber = Value; - } - - /* Check if we got a baud rate */ - if (BaudString) - { - /* Move past the actual string, to reach the rate */ - BaudString += strlen("BAUDRATE"); - - /* Now get past any spaces */ - while (*BaudString == ' ') BaudString++; - - /* And make sure we have a rate */ - if (*BaudString) - { - /* Read and set it */ - Value = atol(BaudString + 1); - if (Value) ComPortBaudRate = Value; - } - } - - /* Check Serial Port Settings [IRQ] */ - if (IrqString) - { - /* Move past the actual string, to reach the rate */ - IrqString += strlen("IRQ"); - - /* Now get past any spaces */ - while (*IrqString == ' ') IrqString++; - - /* And make sure we have an IRQ */ - if (*IrqString) - { - /* Read and set it */ - Value = atol(IrqString + 1); - if (Value) ComPortIrq = Value; - } - } - } + /* Capture the parameters if this is the first invocation */ + Status = KdpRetrieveParameters(LoaderBlock); + if (!NT_SUCCESS(Status)) + return Status; // Or, keep using the default parameters? +#if 0 #ifdef KDDEBUG /* * Try to find a free COM port and use it as the KD debugging port. @@ -269,9 +305,10 @@ KdDebuggerInitialize0( if (ComPort != 0) CpInitialize(&KdDebugComPort, UlongToPtr(BaseArray[ComPort]), DEFAULT_BAUD_RATE); } +#endif #endif - KDDBGPRINT("KdDebuggerInitialize0\n"); + //KDDBGPRINT("KdDebuggerInitialize0\n"); /* Initialize the port */ Status = KdpPortInitialize(ComPortNumber, ComPortBaudRate); @@ -299,6 +336,21 @@ NTAPI KdDebuggerInitialize1( _In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock) { + NTSTATUS Status; + +#ifdef KDDEBUG +CpInitialize(&KdDebugComPort, UlongToPtr(BaseArray[/*ComPort*/4]), /*DEFAULT_BAUD_RATE*/115200); +#endif +KDDBGPRINT("KdDebuggerInitialize1\n"); + + /* Capture the parameters if KdDebuggerInitialize0() wasn't invoked already */ + Status = KdpRetrieveParameters(LoaderBlock); + if (!NT_SUCCESS(Status)) + return Status; // Or, keep using the default parameters? + + // TODO: If we already have a MMIO COM port, + // map it in memory and update KdComPortInUse. + return STATUS_SUCCESS; } diff --git a/drivers/base/kdcom/kddll.h b/drivers/base/kdcom/kddll.h index 03bdad9fcf0..a090cdd48fb 100644 --- a/drivers/base/kdcom/kddll.h +++ b/drivers/base/kdcom/kddll.h @@ -13,7 +13,7 @@ #include #include -// #define KDDEBUG /* uncomment to enable debugging this dll */ +#define KDDEBUG /* uncomment to enable debugging this dll */ #ifndef KDDEBUG #define KDDBGPRINT(...) diff --git a/drivers/base/kdgdb/kdcom.c b/drivers/base/kdgdb/kdcom.c index 3bc1105512f..462a9238fc3 100644 --- a/drivers/base/kdgdb/kdcom.c +++ b/drivers/base/kdgdb/kdcom.c @@ -141,6 +141,110 @@ KdpPortInitialize(IN ULONG ComPortNumber, return STATUS_SUCCESS; } + +/** + * @brief + * Loads port parameters from the Loader Parameter Block, if available. + **/ +static NTSTATUS +KdpRetrieveParameters( + _In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock) +{ + static BOOLEAN AreParamsRetrieved = FALSE; + PSTR CommandLine, PortString, BaudString, IrqString; + ULONG Value; + + /* Load parameters only once if they haven't been already */ + if (AreParamsRetrieved) + return STATUS_SUCCESS; + AreParamsRetrieved = TRUE; + + /* Check if we have a loader block, and if not, attempt to use the + * system one. If it's unavailable (post phase-1 init), just return. */ + if (!LoaderBlock) + LoaderBlock = KeLoaderBlock; + if (!LoaderBlock) + return STATUS_SUCCESS; + + /* Check if we have a command line */ + CommandLine = LoaderBlock->LoadOptions; + if (!CommandLine) + return STATUS_SUCCESS; + + /* Upcase it */ + _strupr(CommandLine); + + /* Check if we got the /DEBUGPORT parameter */ + PortString = strstr(CommandLine, "DEBUGPORT"); + if (PortString) + { + /* Move past the actual string, to reach the port*/ + PortString += strlen("DEBUGPORT"); + + /* Now get past any spaces and skip the equal sign */ + while (*PortString == ' ') PortString++; + PortString++; + + /* Do we have a serial port? */ + if (strncmp(PortString, "COM", 3) != 0) + { + return STATUS_INVALID_PARAMETER; + } + + /* Check for a valid Serial Port */ + PortString += 3; + Value = atol(PortString); + if (Value >= sizeof(BaseArray) / sizeof(BaseArray[0])) + { + return STATUS_INVALID_PARAMETER; + } + + /* Set the port to use */ + ComPortNumber = Value; + } + + /* Check if we got a baud rate */ + BaudString = strstr(CommandLine, "BAUDRATE"); + if (BaudString) + { + /* Move past the actual string, to reach the rate */ + BaudString += strlen("BAUDRATE"); + + /* Now get past any spaces */ + while (*BaudString == ' ') BaudString++; + + /* And make sure we have a rate */ + if (*BaudString) + { + /* Read and set it */ + Value = atol(BaudString + 1); + if (Value) ComPortBaudRate = Value; + } + } + + /* Check Serial Port Settings [IRQ] */ + IrqString = strstr(CommandLine, "IRQ"); + if (IrqString) + { + /* Move past the actual string, to reach the rate */ + IrqString += strlen("IRQ"); + + /* Now get past any spaces */ + while (*IrqString == ' ') IrqString++; + + /* And make sure we have an IRQ */ + if (*IrqString) + { + /* Read and set it */ + Value = atol(IrqString + 1); + if (Value) ComPortIrq = Value; + } + } + + return STATUS_SUCCESS; +} + + /** * @brief * Phase 0 initialization. Invoked by KdInitSystem() when the debugger @@ -157,87 +261,12 @@ NTAPI KdDebuggerInitialize0( _In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock) { - PCHAR CommandLine, PortString, BaudString, IrqString; - ULONG Value; + NTSTATUS Status; - /* Check if we have a LoaderBlock */ - if (LoaderBlock) - { - /* Get the Command Line */ - CommandLine = LoaderBlock->LoadOptions; - - /* Upcase it */ - _strupr(CommandLine); - - /* Get the port and baud rate */ - PortString = strstr(CommandLine, "DEBUGPORT"); - BaudString = strstr(CommandLine, "BAUDRATE"); - IrqString = strstr(CommandLine, "IRQ"); - - /* Check if we got the /DEBUGPORT parameter */ - if (PortString) - { - /* Move past the actual string, to reach the port*/ - PortString += strlen("DEBUGPORT"); - - /* Now get past any spaces and skip the equal sign */ - while (*PortString == ' ') PortString++; - PortString++; - - /* Do we have a serial port? */ - if (strncmp(PortString, "COM", 3) != 0) - { - return STATUS_INVALID_PARAMETER; - } - - /* Check for a valid Serial Port */ - PortString += 3; - Value = atol(PortString); - if (Value >= sizeof(BaseArray) / sizeof(BaseArray[0])) - { - return STATUS_INVALID_PARAMETER; - } - - /* Set the port to use */ - ComPortNumber = Value; - } - - /* Check if we got a baud rate */ - if (BaudString) - { - /* Move past the actual string, to reach the rate */ - BaudString += strlen("BAUDRATE"); - - /* Now get past any spaces */ - while (*BaudString == ' ') BaudString++; - - /* And make sure we have a rate */ - if (*BaudString) - { - /* Read and set it */ - Value = atol(BaudString + 1); - if (Value) ComPortBaudRate = Value; - } - } - - /* Check Serial Port Settings [IRQ] */ - if (IrqString) - { - /* Move past the actual string, to reach the rate */ - IrqString += strlen("IRQ"); - - /* Now get past any spaces */ - while (*IrqString == ' ') IrqString++; - - /* And make sure we have an IRQ */ - if (*IrqString) - { - /* Read and set it */ - Value = atol(IrqString + 1); - if (Value) ComPortIrq = Value; - } - } - } + /* Capture the parameters if this is the first invocation */ + Status = KdpRetrieveParameters(LoaderBlock); + if (!NT_SUCCESS(Status)) + return Status; // Or, keep using the default parameters? #ifdef KDDEBUG /* @@ -282,6 +311,16 @@ NTAPI KdDebuggerInitialize1( _In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock) { + NTSTATUS Status; + + /* Capture the parameters if KdDebuggerInitialize0() wasn't invoked already */ + Status = KdpRetrieveParameters(LoaderBlock); + if (!NT_SUCCESS(Status)) + return Status; // Or, keep using the default parameters? + + // TODO: If we already have a MMIO COM port, + // map it in memory and update KdComPortInUse. + return STATUS_SUCCESS; } diff --git a/ntoskrnl/kd/kd.h b/ntoskrnl/kd/kd.h index 0e10f0c90b0..bb8b4d1907c 100644 --- a/ntoskrnl/kd/kd.h +++ b/ntoskrnl/kd/kd.h @@ -8,7 +8,7 @@ typedef NTSTATUS (NTAPI *PKDP_INIT_ROUTINE)( _In_ struct _KD_DISPATCH_TABLE *DispatchTable, - _In_ ULONG BootPhase); + _In_ ULONG InitPhase); typedef VOID @@ -55,19 +55,19 @@ NTSTATUS NTAPI KdpScreenInit( _In_ struct _KD_DISPATCH_TABLE *DispatchTable, - _In_ ULONG BootPhase); + _In_ ULONG InitPhase); NTSTATUS NTAPI KdpSerialInit( _In_ struct _KD_DISPATCH_TABLE *DispatchTable, - _In_ ULONG BootPhase); + _In_ ULONG InitPhase); NTSTATUS NTAPI KdpDebugLogInit( _In_ struct _KD_DISPATCH_TABLE *DispatchTable, - _In_ ULONG BootPhase); + _In_ ULONG InitPhase); #ifdef KDBG #define KdpKdbgInit KdbInitialize diff --git a/ntoskrnl/kd/kdio.c b/ntoskrnl/kd/kdio.c index cfaa0d46875..55aaad720f1 100644 --- a/ntoskrnl/kd/kdio.c +++ b/ntoskrnl/kd/kdio.c @@ -57,6 +57,8 @@ PKDP_INIT_ROUTINE InitRoutines[KdMax] = #endif }; +extern void KdDbgPortPrintf(PCSTR Format, ...); + /* LOCKING FUNCTIONS *********************************************************/ KIRQL @@ -196,23 +198,27 @@ NTSTATUS NTAPI KdpDebugLogInit( _In_ PKD_DISPATCH_TABLE DispatchTable, - _In_ ULONG BootPhase) + _In_ ULONG InitPhase) { NTSTATUS Status = STATUS_SUCCESS; if (!KdpDebugMode.File) return STATUS_PORT_DISCONNECTED; - if (BootPhase == 0) +KdDbgPortPrintf("%s(%d)\n", __FUNCTION__, InitPhase); + + if (InitPhase == 0) { +/////// ENABLING PORT /////// /* Write out the functions that we support for now */ DispatchTable->KdpPrintRoutine = KdpPrintToLogFile; +///////////////////////////// - /* Register for BootPhase 1 initialization and as a Provider */ + /* Register for InitPhase 1 initialization and as a Provider */ DispatchTable->KdpInitRoutine = KdpDebugLogInit; InsertTailList(&KdProviders, &DispatchTable->KdProvidersList); } - else if (BootPhase == 1) + else if (InitPhase == 1) { /* Allocate a buffer for debug log */ KdpDebugBuffer = ExAllocatePoolZero(NonPagedPool, @@ -229,13 +235,13 @@ KdpDebugLogInit( /* Initialize spinlock */ KeInitializeSpinLock(&KdpDebugLogSpinLock); - /* Register for later BootPhase 2 reinitialization */ + /* Register for later InitPhase 2 reinitialization */ DispatchTable->KdpInitRoutine = KdpDebugLogInit; /* Announce ourselves */ HalDisplayString(" File log debugging enabled\r\n"); } - else if (BootPhase >= 2) + else if (InitPhase >= 2) { UNICODE_STRING FileName; OBJECT_ATTRIBUTES ObjectAttributes; @@ -323,6 +329,7 @@ KdpDebugLogInit( KeInitializeEvent(&KdpLoggerThreadEvent, SynchronizationEvent, TRUE); +/////// ENABLING PORT /////// /* Create the logger thread */ Status = PsCreateSystemThread(&ThreadHandle, THREAD_ALL_ACCESS, @@ -345,6 +352,7 @@ KdpDebugLogInit( sizeof(Priority)); ZwClose(ThreadHandle); +///////////////////////////// return Status; Failure: @@ -391,13 +399,16 @@ NTSTATUS NTAPI KdpSerialInit( _In_ PKD_DISPATCH_TABLE DispatchTable, - _In_ ULONG BootPhase) + _In_ ULONG InitPhase) { if (!KdpDebugMode.Serial) return STATUS_PORT_DISCONNECTED; - if (BootPhase == 0) +KdDbgPortPrintf("%s(%d)\n", __FUNCTION__, InitPhase); + + if (InitPhase == 0) { +/////// ENABLING PORT /////// /* Write out the functions that we support for now */ DispatchTable->KdpPrintRoutine = KdpSerialPrint; @@ -408,15 +419,16 @@ KdpSerialInit( return STATUS_DEVICE_DOES_NOT_EXIST; } KdComPortInUse = SerialPortInfo.Address; +///////////////////////////// /* Initialize spinlock */ KeInitializeSpinLock(&KdpSerialSpinLock); - /* Register for BootPhase 1 initialization and as a Provider */ + /* Register for InitPhase 1 initialization and as a Provider */ DispatchTable->KdpInitRoutine = KdpSerialInit; InsertTailList(&KdProviders, &DispatchTable->KdProvidersList); } - else if (BootPhase == 1) + else if (InitPhase == 1) { /* Announce ourselves */ HalDisplayString(" Serial debugging enabled\r\n"); @@ -514,24 +526,30 @@ NTSTATUS NTAPI KdpScreenInit( _In_ PKD_DISPATCH_TABLE DispatchTable, - _In_ ULONG BootPhase) + _In_ ULONG InitPhase) { if (!KdpDebugMode.Screen) return STATUS_PORT_DISCONNECTED; - if (BootPhase == 0) +KdDbgPortPrintf("%s(%d)\n", __FUNCTION__, InitPhase); + + if (InitPhase == 0) { +/////// ENABLING PORT /////// /* Write out the functions that we support for now */ DispatchTable->KdpPrintRoutine = KdpScreenPrint; +///////////////////////////// - /* Register for BootPhase 1 initialization and as a Provider */ + /* Register for InitPhase 1 initialization and as a Provider */ DispatchTable->KdpInitRoutine = KdpScreenInit; InsertTailList(&KdProviders, &DispatchTable->KdProvidersList); } - else if (BootPhase == 1) + else if (InitPhase == 1) { +/////// ENABLING PORT /////// /* Take control of the display */ KdpScreenAcquire(); +///////////////////////////// /* Announce ourselves */ HalDisplayString(" Screen debugging enabled\r\n"); diff --git a/ntoskrnl/kd/kdmain.c b/ntoskrnl/kd/kdmain.c index f34ab85252e..223a96a931d 100644 --- a/ntoskrnl/kd/kdmain.c +++ b/ntoskrnl/kd/kdmain.c @@ -117,44 +117,51 @@ NTAPI KdDebuggerInitialize0( _In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock) { - PCHAR CommandLine, Port = NULL; + // static BOOLEAN AreParamsRetrieved = FALSE; ULONG i; BOOLEAN Success = FALSE; KdDbgPortPrintf("%s(0x%p)\n", __FUNCTION__, LoaderBlock); - if (LoaderBlock) + /* Check if we have a loader block, and if not, attempt to use the + * system one. If it's unavailable (post phase-1 init), just return. */ + if (!LoaderBlock) + LoaderBlock = KeLoaderBlock; + // if (!LoaderBlock) + // return STATUS_SUCCESS; + + /* Check if we have a command line */ + if (LoaderBlock && LoaderBlock->LoadOptions) { - /* Check if we have a command line */ + PSTR CommandLine, Port = NULL; + CommandLine = LoaderBlock->LoadOptions; - if (CommandLine) + // if (!CommandLine) + // return STATUS_SUCCESS; + + /* Upcase it */ + _strupr(CommandLine); + + /* Get terminal settings */ + KdpGetTerminalSettings(CommandLine); + + /* Check if we got the /DEBUGPORT parameter(s) */ + Port = strstr(CommandLine, "DEBUGPORT"); + while (Port) { - /* Upcase it */ - _strupr(CommandLine); + /* Move past the actual string, to reach the port*/ + Port += sizeof("DEBUGPORT") - 1; - /* Get terminal settings */ - KdpGetTerminalSettings(CommandLine); + /* Now get past any spaces and skip the equal sign */ + while (*Port == ' ') Port++; + Port++; - /* Get the port */ - Port = strstr(CommandLine, "DEBUGPORT"); + /* Get the debug mode and wrapper */ + Port = KdpGetDebugMode(Port); + Port = strstr(Port, "DEBUGPORT"); } } - /* Check if we got the /DEBUGPORT parameter(s) */ - while (Port) - { - /* Move past the actual string, to reach the port*/ - Port += sizeof("DEBUGPORT") - 1; - - /* Now get past any spaces and skip the equal sign */ - while (*Port == ' ') Port++; - Port++; - - /* Get the debug mode and wrapper */ - Port = KdpGetDebugMode(Port); - Port = strstr(Port, "DEBUGPORT"); - } - /* Use serial port then */ if (KdpDebugMode.Value == 0) KdpDebugMode.Serial = TRUE; @@ -188,13 +195,13 @@ KdpDriverReinit( PLIST_ENTRY CurrentEntry; PKD_DISPATCH_TABLE CurrentTable; PKDP_INIT_ROUTINE KdpInitRoutine; - ULONG BootPhase = (Count + 1); // Do BootPhase >= 2 + ULONG InitPhase = (Count + 1); // Do InitPhase >= 2 BOOLEAN ScheduleReinit = FALSE; ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL); DPRINT("*** KD %sREINITIALIZATION - Phase %d ***\n", - Context ? "" : "BOOT ", BootPhase); + Context ? "" : "BOOT ", InitPhase); /* Call the registered providers */ for (CurrentEntry = KdProviders.Flink; @@ -213,7 +220,7 @@ KdpDriverReinit( /* Get the initialization routine and reset it */ KdpInitRoutine = CurrentTable->KdpInitRoutine; CurrentTable->KdpInitRoutine = NULL; - CurrentTable->InitStatus = KdpInitRoutine(CurrentTable, BootPhase); + CurrentTable->InitStatus = KdpInitRoutine(CurrentTable, InitPhase); DPRINT("KdpInitRoutine(%p) returned 0x%08lx\n", CurrentTable, CurrentTable->InitStatus); @@ -340,6 +347,12 @@ KdDebuggerInitialize1( KdDbgPortPrintf("%s(0x%p)\n", __FUNCTION__, LoaderBlock); +// +// TODO: If Init phase 0 wasn't invoked (because the debugger started +// in a disabled state), we need to invoke it there right now, but +// without enabling the corresponding debug ports. +// + /* Make space for the displayed providers' signons */ HalDisplayString("\r\n"); @@ -449,7 +462,7 @@ KdDbgPortPrintf("%s(0x%p)\n", __FUNCTION__, LoaderBlock); * Once the KdpDriverEntry() driver entrypoint is called, we register * KdpDriverReinit() for re-initialization with the I/O Manager, in order * to provide more initialization points. KdpDriverReinit() calls the KD - * providers at BootPhase >= 2, and schedules further reinitializations + * providers at InitPhase >= 2, and schedules further reinitializations * (at most 3 more) if any of the providers request so. **/ orgHalInitPnpDriver = diff --git a/ntoskrnl/kdbg/kdb.h b/ntoskrnl/kdbg/kdb.h index b76c4706bc5..e74f94737b3 100644 --- a/ntoskrnl/kdbg/kdb.h +++ b/ntoskrnl/kdbg/kdb.h @@ -97,7 +97,7 @@ NTSTATUS NTAPI KdbInitialize( _In_ PKD_DISPATCH_TABLE DispatchTable, - _In_ ULONG BootPhase); + _In_ ULONG InitPhase); BOOLEAN NTAPI @@ -188,7 +188,7 @@ KdbSymProcessSymbols( BOOLEAN KdbSymInit( - _In_ ULONG BootPhase); + _In_ ULONG InitPhase); /* from kdb.c */ diff --git a/ntoskrnl/kdbg/kdb_cli.c b/ntoskrnl/kdbg/kdb_cli.c index b4794dca739..0d5d866620b 100644 --- a/ntoskrnl/kdbg/kdb_cli.c +++ b/ntoskrnl/kdbg/kdb_cli.c @@ -3537,13 +3537,15 @@ KdbDebugPrint( * debug strings before they will be wiped over by next writes. */ } +extern void KdDbgPortPrintf(PCSTR Format, ...); + /** * @brief Initializes the KDBG debugger. * * @param[in] DispatchTable * Pointer to the KD dispatch table. * - * @param[in] BootPhase + * @param[in] InitPhase * Phase of initialization. * * @return A status value. @@ -3553,15 +3555,19 @@ NTSTATUS NTAPI KdbInitialize( _In_ PKD_DISPATCH_TABLE DispatchTable, - _In_ ULONG BootPhase) + _In_ ULONG InitPhase) { /* Saves the different symbol-loading status across boot phases */ static ULONG LoadSymbols = 0; - if (BootPhase == 0) +KdDbgPortPrintf("%s(%d)\n", __FUNCTION__, InitPhase); + + if (InitPhase == 0) { +/////// ENABLING PORT /////// /* Write out the functions that we support for now */ DispatchTable->KdpPrintRoutine = KdbDebugPrint; +///////////////////////////// /* Check if we have a command line */ if (KeLoaderBlock && KeLoaderBlock->LoadOptions) @@ -3570,13 +3576,13 @@ KdbInitialize( KdbpGetCommandLineSettings(KeLoaderBlock->LoadOptions); } - /* Register for BootPhase 1 initialization and as a Provider */ + /* Register for InitPhase 1 initialization and as a Provider */ DispatchTable->KdpInitRoutine = KdbInitialize; InsertTailList(&KdProviders, &DispatchTable->KdProvidersList); } - else if (BootPhase == 1) + else if (InitPhase == 1) { - /* Register for later BootPhase 2 reinitialization */ + /* Register for later InitPhase 2 reinitialization */ DispatchTable->KdpInitRoutine = KdbInitialize; /* Initialize Dmesg support */ @@ -3594,14 +3600,14 @@ KdbInitialize( KeInitializeSpinLock(&KdpDmesgLogSpinLock); } - /* Initialize symbols support in BootPhase 0 and 1 */ - if (BootPhase <= 1) + /* Initialize symbols support in InitPhase 0 and 1 */ + if (InitPhase <= 1) { LoadSymbols <<= 1; - LoadSymbols |= KdbSymInit(BootPhase); + LoadSymbols |= KdbSymInit(InitPhase); } - if (BootPhase == 1) + if (InitPhase == 1) { /* Announce ourselves */ CHAR buffer[60]; @@ -3613,7 +3619,7 @@ KdbInitialize( HalDisplayString(buffer); } - if (BootPhase >= 2) + if (InitPhase >= 2) { /* I/O is now set up for disk access: load the KDBinit file */ NTSTATUS Status = KdbpCliInit(); diff --git a/ntoskrnl/kdbg/kdb_symbols.c b/ntoskrnl/kdbg/kdb_symbols.c index 11ccd572219..740764801d6 100644 --- a/ntoskrnl/kdbg/kdb_symbols.c +++ b/ntoskrnl/kdbg/kdb_symbols.c @@ -333,23 +333,23 @@ KdbSymProcessSymbols( /** * @brief Initializes the KDB symbols implementation. * - * @param[in] BootPhase + * @param[in] InitPhase * Phase of initialization. * * @return - * TRUE if symbols are to be loaded at this given BootPhase; FALSE if not. + * TRUE if symbols are to be loaded at this given InitPhase; FALSE if not. **/ BOOLEAN KdbSymInit( - _In_ ULONG BootPhase) + _In_ ULONG InitPhase) { #if 1 // FIXME: This is a workaround HACK!! static BOOLEAN OrigLoadSymbols = FALSE; #endif - DPRINT("KdbSymInit() BootPhase=%d\n", BootPhase); + DPRINT("KdbSymInit() InitPhase=%d\n", InitPhase); - if (BootPhase == 0) + if (InitPhase == 0) { PSTR CommandLine; SHORT Found = FALSE; @@ -405,13 +405,13 @@ KdbSymInit( } #if 1 // FIXME: This is a workaround HACK!! -// Save the actual value of LoadSymbols but disable it for BootPhase 0. +// Save the actual value of LoadSymbols but disable it for InitPhase 0. OrigLoadSymbols = LoadSymbols; LoadSymbols = FALSE; return OrigLoadSymbols; #endif } - else if (BootPhase == 1) + else if (InitPhase == 1) { HANDLE Thread; NTSTATUS Status;