MSDN Documentation

Microsoft Developer Network

Windows Kernel Setup and Initialization

This document provides an in-depth overview of the Windows kernel's startup process, including critical phases like boot loader execution, kernel initialization, and driver loading. Understanding these steps is fundamental for system programming and debugging.

1. Boot Sequence Overview

The Windows boot process is a multi-stage operation that begins when the system's power is turned on and concludes when the user interface is ready. Key stages include:

  1. Firmware Initialization: The system BIOS/UEFI initializes hardware components.
  2. Boot Loader: The boot loader (bootmgr and winload.exe) loads the kernel (ntoskrnl.exe) and the hardware abstraction layer (hal.dll) into memory.
  3. Kernel Initialization: The loaded kernel takes control, initializes its core structures, and begins loading essential system components.
  4. Driver Loading: Device drivers and kernel services are loaded and started.
  5. System Initialization Complete: The system reaches a stable state, and user login or the desktop environment is presented.

2. The Role of Bootmgr and Winload.exe

bootmgr is the Windows Boot Manager, responsible for presenting boot options. Once an OS is selected, it hands off control to winload.exe. winload.exe's primary responsibilities include:

3. Kernel Initialization (ntoskrnl.exe)

Upon receiving control from winload.exe, the kernel begins its own initialization sequence. This phase is crucial for establishing the foundational components of the operating system:

3.1. Phase 0: Kernel Setup

This is the earliest stage where the kernel initializes basic structures and routines. It involves:

At this point, the kernel is running in its most privileged mode, processing interrupts and exceptions directly.

3.2. Phase 1: Kernel Initialization

This phase involves more complex initialization tasks:

4. Hardware Abstraction Layer (HAL)

The HAL (hal.dll) acts as an abstraction layer between the kernel and the specific hardware architecture. It masks hardware differences, allowing the kernel to remain relatively hardware-independent. During initialization, the kernel identifies and loads the appropriate HAL based on the system's hardware configuration.

5. Driver Loading and Initialization

Once the core kernel and HAL are initialized, the system proceeds to load and start device drivers. This is a critical step for enabling hardware functionality.

The kernel reads driver information from the system registry. Drivers are typically loaded in a specific order, with boot-critical drivers loaded first. Each driver goes through an initialization routine that:

The order of driver loading can be influenced by settings in the registry, particularly the StartType value, which defines when a driver should be loaded (e.g., Boot, System, Automatic, Manual).

Note: Incorrect configuration of boot drivers or their dependencies can lead to system boot failures, often manifesting as a Blue Screen of Death (BSOD).

6. System Initialization Completion

After all critical drivers and system services are loaded and initialized, the kernel transitions to its normal operational state. This includes:

Code Example: Boot Driver Initialization (Conceptual)

While direct kernel code is not exposed, a conceptual representation of a driver's entry point might look like this:


// Example: Simplified DriverEntry function
NTSTATUS DriverEntry(
    PDRIVER_OBJECT DriverObject,
    PUNICODE_STRING RegistryPath
)
{
    NTSTATUS status = STATUS_SUCCESS;
    PDEVICE_OBJECT deviceObject;

    // Initialize driver specific data
    // ...

    // Create a device object for this driver
    status = IoCreateDevice(
        DriverObject,
        0, // Device extension size
        &deviceName, // Device name
        FILE_DEVICE_UNKNOWN, // Device type
        0, // Characteristics
        FALSE, // Exclusive
        &deviceObject
    );

    if (!NT_SUCCESS(status)) {
        // Handle error: Failed to create device object
        return status;
    }

    // Set up IRP dispatch routines (e.g., IRP_MJ_CREATE, IRP_MJ_READ)
    DriverObject->MajorFunction[IRP_MJ_CREATE] = MyCreateClose;
    DriverObject->MajorFunction[IRP_MJ_READ] = MyRead;
    // ... other dispatch routines

    // Initialize hardware for the device
    // ...

    return STATUS_SUCCESS;
}
            
Tip: Use tools like WinDbg with symbols loaded to trace the boot process and analyze driver initialization failures.

Further Reading