ntoskrnl.exe (short for "Windows NT operating system kernel executable"), also known as the kernel image, is an executable file that contains the kernel and executive layers of the Microsoft Windows NT kernel, and is responsible for hardware abstraction, process handling, and memory management. In addition to the kernel and executive layers, it contains the cache manager, security reference monitor, memory manager, scheduler (Dispatcher), and blue screen of death (the prose and portions of the code).
Overview Newer x86 32-bit variants of ntoskrnl.exe heavily depend on hal.dll due to architecture-dependent code being compiled directly into it. However, AMD64 compiled variants of ntoskrnl.exe link HAL statically into the kernel but also keep a stub HAL.dll kernel module in the address space for backwards compatibility: allowing legacy kernel drivers, importing from HAL.dll, to be redirected to equivalent code in the kernel instead. With all this, the kernel image is not a native application, therefore it is not linked against ntdll.dll. Instead, ntoskrnl.exe has its own entry point routine named KiSystemStartup (as per symbol information published by Microsoft) that calls the architecture-independent kernel initialization subroutines. The kernel is compiled using a static copy of the C Runtime, which for a normal program would noticeably increase its file size. In Windows XP and earlier, the Windows installation source ships four kernel image files to support uniprocessor systems, symmetric multiprocessor (SMP) systems, CPUs with PAE, and CPUs without PAE. Windows setup decides whether the system is uniprocessor or multiprocessor, then, installs both the PAE and non-PAE variants of the kernel image for the decided kind. On a multiprocessor system, setup installs ntkrnlmp.exe and ntkrpamp.exe but renames them to ntoskrnl.exe and ntkrnlpa.exe respectively. Starting with Windows Vista, Microsoft began unifying the kernel images as multi-core CPUs took to the market and PAE became mandatory.
The kernel's code uses prefixes to indicate their associated subsystem. An example is IoCreateDevice and ObReferenceObjectByHandle. Both functions have different prefix names to differentiate critical managers within the kernel code: Io being used for I/O Manager functions and Ob for Object Manager functions. Variations of these prefixes exist for internal functions that are not being exported by the kernel, such as adding an i after the first letter (e.g., Ki for “Kernel Internal”) or appending p to the full prefix (e.g., Psp for “Process Support Internal”). The following table lists some known prefixes.
Initialization Before handing the execution to the kernel, the bootloader initializes the system to protected mode on x86 32-bit platforms and long mode with 4-level paging enabled (or 5-level paging if available) on x86 64-bit platforms (both platforms will have their CPU GDT, IDT and TSS ready). When control is transferred to the kernel, it receives an information record, known as the Loader Parameter Block, from the bootloader. The block contains information about the hardware, the path to the Windows Registry file, kernel parameters, path of the files loaded by the bootloader (SYSTEM Registry hive, nls for character encoding conversion, and vga font). The definition of this structure can be retrieved by using the kernel debugger. The entry point of ntoskrnl.exe is firstly run on the boot processor (the first processor to start on the system). The boot processor will initialize the kernel executive and create a system thread in order to launch smss.exe. After enabling interrupts, the boot processor will enter an idle loop.
Construction
Interrupt handling
Modern operating systems use interrupts instead of I/O port polling to wait for information from devices. In the x86 architecture, interrupts are handled through the Interrupt Dispatch Table (IDT). When a device triggers an interrupt and the interrupt flag (IF) in the FLAGS register is set, the processor's hardware looks for an interrupt handler in the table entry corresponding to the interrupt number to which in turn has been translated from IRQ by PIC chips, or in more modern hardware, APIC. Interrupt handlers usually save some subset of the state of registers before handling it and restore them back to their original values when done. The interrupt table contains handlers for hardware interrupts, software interrupts, and exceptions. For some IA-32 versions of the kernel, one example of such a software interrupt handler (of which there are many) is in its IDT table entry 2E16 (hexadecimal; 46 in decimal), used in assembly language as INT 2EH for system calls, which are Microsoft internal usage calls, leading to kernel system calls. For newer versions, different mechanisms making use of SYSENTER instruction and in AMD64 SYSCALL instruction are used instead. One notable feature of NT's interrupt handling is that interrupts are usually conditionally masked based on their priority (called "IRQL"), instead of disabling all IRQs via the interrupt flag. This permits various kernel components to carry on critical operations without necessarily blocking services of peripherals and other devices.
Memory manager
The entire physical memory (RAM) address range is broken into many small blocks also called pages, 4KB in size each, and mapped to virtual addresses. A few of the properties of each block are stored in structures called page table entries, which are managed by the OS and accessed by the processor's hardware. Page tables are organized into a tree structure, and the physical page number of the top-level table is stored in control register 3 (CR3). Microsoft Windows divides virtual address space into two regions. The lower part, starting at zero, is instantiated separately for each process and is accessible from both user and kernel mode. Application programs run in processes and supply code that runs in user mode. The upper part is accessible only from kernel mode, and with some exceptions, is instantiated just once, system-wide. ntoskrnl.exe is mapped into this region, as are several other kernel mode components. This region also contains data used by kernel mode code, such as the kernel mode heaps and the file system cache.
Registry
… excerpt ends here. Continue reading the full article.