ReactOS 0.4.16-dev-746-g329a414
|
Go to the source code of this file.
Macros | |
#define | NDEBUG |
#define | CONST_STR_LEN(x) (sizeof(x)/sizeof(x[0]) - 1) |
Functions | |
static VOID | KdpGetTerminalSettings (_In_ PCSTR p1) |
static PCHAR | KdpGetDebugMode (_In_ PCHAR Currentp2) |
NTSTATUS NTAPI | KdDebuggerInitialize0 (_In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock) |
static VOID NTAPI | KdpDriverReinit (_In_ PDRIVER_OBJECT DriverObject, _In_opt_ PVOID Context, _In_ ULONG Count) |
Reinitialization routine. DRIVER_REINITIALIZE. | |
static NTSTATUS NTAPI | KdpDriverEntry (_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath) |
Entry point for the auxiliary driver. DRIVER_INITIALIZE. | |
static NTSTATUS NTAPI | KdpInitDriver (VOID) |
HalInitPnpDriver() callback hook installed by KdDebuggerInitialize1(). | |
NTSTATUS NTAPI | KdDebuggerInitialize1 (_In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock) |
NTSTATUS NTAPI | KdD0Transition (VOID) |
NTSTATUS NTAPI | KdD3Transition (VOID) |
NTSTATUS NTAPI | KdSave (_In_ BOOLEAN SleepTransition) |
NTSTATUS NTAPI | KdRestore (_In_ BOOLEAN SleepTransition) |
Variables | |
static pHalInitPnpDriver | orgHalInitPnpDriver = NULL |
Hooked HalInitPnpDriver() callback. It is initially set by the HAL when HalInitSystem(0, ...) is called earlier on. | |
NTSTATUS NTAPI KdDebuggerInitialize0 | ( | _In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock | ) |
Definition at line 115 of file kdmain.c.
NTSTATUS NTAPI KdDebuggerInitialize1 | ( | _In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock | ) |
We want to be able to perform I/O-related initialization (starting a logger thread for file log debugging, loading KDBinit file for KDBG, etc.). A good place for this would be as early as possible, once the I/O Manager has started the storage and the boot filesystem drivers.
Here is an overview of the initialization steps of the NT Kernel and
KiSystemStartup(KeLoaderBlock) if (Cpu == 0) KdInitSystem(0, KeLoaderBlock); KiSwitchToBootStack() -> KiSystemStartupBootStack() -> KiInitializeKernel() -> ExpInitializeExecutive(Cpu, KeLoaderBlock)
(NOTE: Any unexpected debugger break will call KdInitSystem(0, NULL); ) KdInitSystem(0, LoaderBlock) -> KdDebuggerInitialize0(LoaderBlock);
ExpInitializeExecutive(Cpu == 0): ExpInitializationPhase = 0; HalInitSystem(0, KeLoaderBlock); <– Sets HalInitPnpDriver callback. ... PsInitSystem(LoaderBlock) PsCreateSystemThread(Phase1Initialization)
Phase1Initialization(Discard): ExpInitializationPhase = 1; HalInitSystem(1, KeLoaderBlock); ... Early initialization of Ob, Ex, Ke. KdInitSystem(1, KeLoaderBlock); ... KdDebuggerInitialize1(LoaderBlock); ... IoInitSystem(LoaderBlock);
As we can see, KdDebuggerInitialize1() is the last KD initialization routine the kernel calls, and is called before the I/O Manager starts. Thus, direct Nt/ZwCreateFile ... calls done there would fail. Also, we want to do the I/O initialization as soon as possible. There does not seem to be any exported way to be notified about the I/O manager initialization steps... that is, unless we somehow become a driver and insert ourselves in the flow!
Since we are not a regular driver, we need to invoke IoCreateDriver() to create one. However, remember that we are currently running before IoInitSystem(), the I/O subsystem is not initialized yet. Due to this, calling IoCreateDriver(), much like any other IO functions, would lead to a crash, because it calls ObCreateObject(..., IoDriverObjectType, ...), and IoDriverObjectType is non-initialized yet (it's NULL).
The chosen solution is to hook a "known" exported callback: namely, the HalInitPnpDriver() callback (it initializes the "HAL Root Bus Driver"). It is set very early on by the HAL via the HalInitSystem(0, ...) call, and is called early on by IoInitSystem() before any driver is loaded, but after the I/O Manager has been minimally set up so that new drivers can be created. When the hook: KdpInitDriver() is called, we create our driver with IoCreateDriver(), specifying its entrypoint KdpDriverEntry(), then restore and call the original HalInitPnpDriver() callback.
Another possible unexplored alternative, could be to insert ourselves in the KeLoaderBlock->LoadOrderListHead boot modules list, or in the KeLoaderBlock->BootDriverListHead boot-driver list. (Note that while we may be able to do this, because boot-drivers are resident in memory, much like we are, we cannot insert ourselves in the system-driver list however, since those drivers are expected to come from PE image files.)
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 (at most 3 more) if any of the providers request so.
Definition at line 328 of file kdmain.c.
|
static |
Entry point for the auxiliary driver. DRIVER_INITIALIZE.
Definition at line 267 of file kdmain.c.
Referenced by KdpInitDriver().
|
static |
Reinitialization routine. DRIVER_REINITIALIZE.
Calls each registered provider for reinitialization at Phase >= 2. I/O is now set up for disk access, at different phases.
Definition at line 179 of file kdmain.c.
Referenced by KdpDriverEntry(), and KdpDriverReinit().
Definition at line 53 of file kdmain.c.
Referenced by KdDebuggerInitialize0().
Definition at line 25 of file kdmain.c.
Referenced by KdDebuggerInitialize0().
HalInitPnpDriver() callback hook installed by KdDebuggerInitialize1().
It is called during initialization of the I/O manager and is where the auxiliary driver is created. This driver is needed for receiving reinitialization callbacks in KdpDriverReinit() later. This hook must always call the original HalInitPnpDriver() function and return its returned value, or return STATUS_SUCCESS.
Definition at line 302 of file kdmain.c.
Referenced by KdDebuggerInitialize1().
|
static |
Hooked HalInitPnpDriver() callback. It is initially set by the HAL when HalInitSystem(0, ...) is called earlier on.
Definition at line 288 of file kdmain.c.
Referenced by KdDebuggerInitialize1(), and KdpInitDriver().