<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://kernelexplorer.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Exploring NtosKrnl.exe</title><link>http://kernelexplorer.net/blogs/kore/default.aspx</link><description>Technical autopsy of the Windows kernel</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 (Build: 30912.2823)</generator><item><title>Building the Windows kernel</title><link>http://kernelexplorer.net/blogs/kore/archive/2009/04/19/Building-the-Windows-kernel.aspx</link><pubDate>Mon, 20 Apr 2009 06:11:00 GMT</pubDate><guid isPermaLink="false">36297f19-9499-41a7-8426-8a5311bd7045:15</guid><dc:creator>jhualpa</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://kernelexplorer.net/blogs/kore/rsscomments.aspx?PostID=15</wfw:commentRss><comments>http://kernelexplorer.net/blogs/kore/archive/2009/04/19/Building-the-Windows-kernel.aspx#comments</comments><description>&lt;p&gt;In this first post I will introduce the WRK v1.2 (Windows Research Kernel). The idea is to explain Windows&amp;#39; OS concepts, especially with source code, as I dig in the sources and acquire more knowledge of the kernel. My 2 fundamentals learning resources are: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Microsoft Windows Internals, 4th Ed 2005, Mark Russinovich and David Solomon. &lt;/li&gt;    &lt;li&gt;The Windows Curriculum Resource Kit (CRK) &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;The Windows Research Kernel&lt;/h2&gt;  &lt;p&gt;The Windows Research Kernel v1.2 contains the sources for the core of the Windows (NTOS) kernel and a build environment for a kernel that will run on x86 (Windows Server 2003 Service Pack 1) and AMD64 (Windows XP x64 Professional). The Windows kernel implements the basic OS functions for processes, threads, virtual memory and cache managers, I/O management, the registry, executive functions such as the kernel heap and synchronization, the object manager, the local procedure call mechanism, the security reference monitor, low-level CPU management (thread scheduling, Asynchronous and Deferred Procedure calls, interrupt/trap handling, exceptions), etc. &lt;/p&gt;  &lt;p&gt;The NT Hardware Abstraction Layer, file systems, network stacks, and device drivers are implemented separately from NTOS and loaded into kernel mode as dynamic libraries. Sources for these dynamic components are not included in the WRK, but some are available in various development kits published by Microsoft.&lt;/p&gt;  &lt;p&gt;The following picture represents the organization of the WRK sources: &lt;/p&gt;  &lt;p&gt;&lt;img style="display:block;float:none;margin-left:auto;margin-right:auto;" src="http://farm4.static.flickr.com/3648/3391898782_13288215e3_b.jpg" alt="" /&gt; &lt;/p&gt;  &lt;p&gt;The “.” in the tree or the %wrk% environment variable represents the directory where you copied the WRK sources. In my case is “C:\WRK”. The %wrk%\public\ directory contains a number of include files shared among system components. The %wrk%\base\ntos\ contains the Windows kernel sources. The primary kernel source components included in the WRK are organized as follows:&lt;/p&gt;  &lt;div align="center"&gt;   &lt;table style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;" align="center"&gt;           &lt;h3 align="center"&gt;&lt;strong&gt;Directory&lt;/strong&gt;&lt;/h3&gt;         &lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;" align="center"&gt;           &lt;h3 align="center"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/h3&gt;         &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;cache\&lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;cache manager&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;config\&lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;registry implementation&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;dbgk\&lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;user-mode debugger support&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;ex\&lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;executive functions (kernel heap, synchronization, time)&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;fsrtl\ &lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;file system run-time support &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;io\ &lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;I/O manager &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;ke\&lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;scheduler, CPU management, low-level synchronization &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;lpc\ &lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;local procedure call implementation &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;mm\ &lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;virtual memory manager &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;ob\&lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;kernel object manager&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;ps\ &lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;process/thread support &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;se\ &lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;security functions &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;wmi\&lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;Windows Management Instrumentation&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;inc\&lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;NTOS-only include files&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;rtl\&lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;kernel run-time support&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;init\ &lt;/td&gt;          &lt;td style="border-bottom:black 1px solid;border-left:black 1px solid;border-top:black 1px solid;border-right:black 1px solid;"&gt;kernel startup&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/div&gt;  &lt;p&gt;To build and deploy the WRK:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;From the WRK root we set the current directory to &lt;strong&gt;%wrk%\base\ntos\&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;Type &lt;strong&gt;nmake -nologo x86=&lt;/strong&gt; or &lt;strong&gt;nmake -nologo amd64=&lt;/strong&gt; to compile the kernel for the x86 or x64 architecture respectively. If something went wrong and you want the build to start over again then type &lt;strong&gt;nmake -nologo x86= clean&lt;/strong&gt; or &lt;strong&gt;nmake -nologo amd64= clean&lt;/strong&gt;. &lt;img src="http://farm4.static.flickr.com/3578/3391898858_4af5bebbca_o.jpg" alt="" /&gt; &lt;/li&gt;    &lt;li&gt;After a successful build the produced kernel files will be in &lt;strong&gt;%wrk%\base\ntos\BUILD\EXE\wrkx86.exe&lt;/strong&gt; (for x86) or &lt;strong&gt;%wrk%\base\ntos\BUILD\EXE\wrkx64.exe&lt;/strong&gt; (for x64).       &lt;p&gt;&lt;a href="http://farm4.static.flickr.com/3564/3395081847_03ab6bc9fc_o.jpg"&gt;&lt;img border="0" src="http://farm4.static.flickr.com/3564/3395081847_38f35b11c1.jpg" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;At this moment we have a brand new kernel built using the WRK build environment on Windows Vista. As I stated before, this kernel is a modified Windows Server 2003 SP1 kernel. So If I want to test it and debug it I need a Windows Server 2003 SP1 target OS.&lt;/p&gt;  &lt;p&gt;To debug a kernel or a device driver, you need two computers: the machine being debugged (called the target machine) and another machine to run the debugger (called the host machine). In this case I will use a virtual machine infrastructure to avoid the need of having to configure an additional physical machine. From now on, the physical machine that runs Virtual PC and the debugger (WinDbg) will be referred to as the host machine, and the virtual machine running on the host will be known as the target machine. &lt;/p&gt;  &lt;p&gt;A debugging session requires that the host and target machines are connected. When both machines are physical machines, you need to use a physical connection such as a null-modem cable, an Institute of Electrical and Electronics Engineers (IEEE) 1394 cable, or a USB 2.0 cable. But if the target machine is a virtual machine on the host machine, no physical connection is necessary because no second physical machine exists. Virtual PC does not support IEEE 1394 ports, but it does support serial ports and it is possible to emulate the behavior of a physical null modem cable by debugging a virtual machine over named pipes. Next I will configure the target, the debugger and the host to work in a virtual environment. &lt;/p&gt;  &lt;h2&gt;Configuring the target&lt;/h2&gt;  &lt;p&gt;To configure the target machine for debugging:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;From the &lt;b&gt;Settings&lt;/b&gt; dialog box of the virtual machine, specify that we are going to connect to the COM1 serial port on the target virtual machine via the named pipe “\\.\pipe\debugPipe”.       &lt;p&gt;&lt;img src="http://farm4.static.flickr.com/3641/3391898676_2c933a8909_o.jpg" alt="" /&gt;&lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;Start the virtual machine and open its &lt;strong&gt;boot.ini&lt;/strong&gt; file. This file is in the root of C: but is hidden by default, so you might need to change your Windows Explorer settings to see the file. &lt;/li&gt;    &lt;li&gt;After that we copy the new kernel to the target virtual machine at &lt;strong&gt;%SystemRoot%\system32\&lt;/strong&gt; and the corresponding precompiled hal.dll from &lt;strong&gt;%wrk%\WS03SP1HALS\x86&lt;/strong&gt;, which for an x86 virtual PC target is halacpim.dll. &lt;/li&gt;    &lt;li&gt;Then modify the &lt;strong&gt;boot.ini&lt;/strong&gt; file as follows:       &lt;p&gt;&lt;/p&gt;      &lt;p&gt;&lt;a href="http://farm4.static.flickr.com/3575/3391899062_0d827dd83b_o.jpg"&gt;&lt;img border="0" src="http://farm4.static.flickr.com/3575/3391899062_af56dc26b5.jpg" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;      &lt;p&gt;&lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;The &lt;strong&gt;boot.ini&lt;/strong&gt; file will have 4 entries, this means that during boot time we will have to choose between 4 options.&lt;/p&gt;      &lt;ul&gt;       &lt;ul&gt;         &lt;li&gt;           &lt;p&gt;Windows Server 2003, with the Microsoft provided kernel (ntoskrnl.exe) and debugging disabled.&lt;/p&gt;         &lt;/li&gt;          &lt;li&gt;           &lt;p&gt;Windows Server 2003, with the Microsoft provided kernel (ntoskrnl.exe) and debugging enabled.&lt;/p&gt;         &lt;/li&gt;          &lt;li&gt;           &lt;p&gt;Windows Server 2003, with my custom provided kernel (wrkx86.exe) and debugging enabled.&lt;/p&gt;         &lt;/li&gt;          &lt;li&gt;           &lt;p&gt;Windows Server 2003, with my custom provided kernel (wrkx86.exe) and debugging disabled.&lt;/p&gt;         &lt;/li&gt;       &lt;/ul&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Please note the use of the /kernel and /hal switches for providing an alternative kernel to boot. Also notice the /debug and /debugport switches, which enable to attach a debugger (WinDbg) throught the named piped configured for the port COM1. For more information visit the &lt;a href="http://technet.microsoft.com/en-us/sysinternals/bb963892.aspx"&gt;BOOT INI Options Reference&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;Shut down the target virtual machine. (Do not turn off and close the machine; only shut it down. The virtual machine will be restarted to use the changes you just made.) Then select the boot option for the custom provided kernel with debugging enabled. &lt;/li&gt;    &lt;li&gt;And we will boot up the new kernel we have recently built/linked. &lt;/li&gt;    &lt;li&gt;Let’s see the output of winver for the new kernel (wrkx86.exe).      &lt;p&gt;&lt;img src="http://farm4.static.flickr.com/3554/3391898922_52225381e8_o.jpg" alt="" /&gt;&lt;/p&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;And now let’s contrast it with the output of winver for the official kernel (ntoskrnl.exe).&lt;img src="http://farm4.static.flickr.com/3557/3391088505_23babc8a57_o.jpg" alt="" /&gt;&amp;#160; &lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Configuring the debugger&lt;/h2&gt;  &lt;p&gt;The first step is to enable kernel debugging between the host and the target is to download and install the debug symbols on the host machine. The debug symbols are found in Program Debug Database (.pdb) files, which you can obtain from a symbol server or from a file system cache. The symbol server can be a corporate symbol server or the Microsoft symbol server at &lt;a href="http://msdl.microsoft.com/download/symbols"&gt;http://msdl.microsoft.com/download/symbols&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Using the Microsoft symbol server is a good idea since Microsoft keeps the server current with the most recent symbols for service packs and Windows updates. When using a symbol server, you need to specify a local or network directory in which to cache the symbols that you download from the symbol server. If the directory you specify does not exist, it will be created for you. The general syntax for using the Microsoft symbol server is the following:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;srv*symbolStoreDirectoryPath* http://msdl.microsoft.com/download/symbols&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;To use it from WinDbg, press Ctrl+S to set the symbol search path.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://farm4.static.flickr.com/3556/3395050111_a3820b0032_o.jpg"&gt;&lt;img border="0" src="http://farm4.static.flickr.com/3556/3395050111_05f92ee8ca.jpg" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h2&gt;Configuring the host&lt;/h2&gt;  &lt;p&gt;In WinDbg, press Ctrl+K to start kernel debugging and configure it to connect to the target virtual machine via a COM port. In order to achieve this check the Pipe checkbox to enable the use of pipe addresses on serial connections and also set the Port Name to the named pipe defined previously on the target “\\.\pipe\debugPipe” . This allows us to debug over a named pipe.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://farm4.static.flickr.com/3636/3391898720_3f2edd32ba_o.jpg"&gt;&lt;img border="0" src="http://farm4.static.flickr.com/3636/3391898720_eca0317ebf.jpg" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h2&gt;Debugging the target virtual machine from the physical host&lt;/h2&gt;  &lt;p&gt;To debug the target virtual machine from a physical host machine:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Run WinDbg from host machine and start kernel debugging. Then start the target virtual machine. The kernel debugger will wait until the boot.ini debug-enabled virtual target boots up. Once the target machine starts the boot process, the debugger continues. &lt;/li&gt;    &lt;li&gt;Press &lt;b&gt;CTRL+BREAK&lt;/b&gt; to break into the debugger and get a command prompt.       &lt;p&gt;&lt;a href="http://farm4.static.flickr.com/3554/3391088687_82422fc0a5_o.jpg"&gt;&lt;img border="0" src="http://farm4.static.flickr.com/3554/3391088687_f66e47f00f.jpg" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;      &lt;p&gt;&lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;At the resulting command prompt, enter &lt;b&gt;lmDvmnt &lt;/b&gt;to confirm that the kernel debugging connection was successful. The previous command displays the information about a module named “nt”, which corresponds to the Windows kernel. In this case the kernel is the new one(wrkx86.exe):       &lt;p&gt;&lt;a href="http://farm4.static.flickr.com/3550/3391899180_900decc712_o.jpg"&gt;&lt;img border="0" src="http://farm4.static.flickr.com/3550/3391899180_99e85d0d4f.jpg" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;      &lt;p&gt;And now let’s contrast it with the output of WinDbg when debugging the official kernel (ntoskrnl.exe).&lt;/p&gt;      &lt;p&gt;&lt;a href="http://farm4.static.flickr.com/3436/3391899114_31d45a132b_o.jpg"&gt;&lt;img border="0" src="http://farm4.static.flickr.com/3436/3391899114_95dfdc2f1d.jpg" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;   &lt;/li&gt; &lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://kernelexplorer.net/aggbug.aspx?PostID=15" width="1" height="1"&gt;</description><category domain="http://kernelexplorer.net/blogs/kore/archive/tags/debug/default.aspx">debug</category><category domain="http://kernelexplorer.net/blogs/kore/archive/tags/WinDbg/default.aspx">WinDbg</category><category domain="http://kernelexplorer.net/blogs/kore/archive/tags/NTOS/default.aspx">NTOS</category><category domain="http://kernelexplorer.net/blogs/kore/archive/tags/WRK/default.aspx">WRK</category><category domain="http://kernelexplorer.net/blogs/kore/archive/tags/kernel/default.aspx">kernel</category></item></channel></rss>
