{"id":674,"date":"2022-10-04T11:22:39","date_gmt":"2022-10-04T18:22:39","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=674"},"modified":"2022-10-04T11:22:39","modified_gmt":"2022-10-04T18:22:39","slug":"using-qemu-to-develop-and-debug-linux-kernel-modules","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/qemu\/","title":{"rendered":"Using QEMU to Develop and Debug Linux Kernel Modules"},"content":{"rendered":"<p>This tutorial shows how to use VisualKernel and QEMU to efficiently develop and debug kernel modules. QEMU is very lightweight, requires no initial setup and is the only virtual machine engine that supports kernel debugging on Windows hosts running the Hyper-V platform (required for WSL2).<\/p>\n<p>Before you begin, install Visual Studio and VisualKernel 4.0 or later.<\/p>\n<ol>\n<li>Start Visual Studio and locate the Linux Kernel Module Wizard:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/01-newprj-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-628\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/01-newprj-1.png\" alt=\"\" width=\"1014\" height=\"675\" \/><\/a><\/li>\n<li>Enter the name and location of the project you would like to create:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/02-prjname-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-675\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/02-prjname-2.png\" alt=\"\" width=\"1014\" height=\"675\" \/><\/a><\/li>\n<li>Choose the project template you would like to use to create your first kernel module. In this tutorial we will use the basic &#8220;Hello, World&#8221; template, however you can choose any other template you prefer: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/03-hello-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-676\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/03-hello-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>The next page of the wizard allows selecting the kernel target. The easiest way to get working would be to download an <a href=\"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/vms\/\">existing pre-built VM image<\/a>, however in this tutorial we will show how to create a QEMU VM from scratch. Select &#8220;<strong>Setup a new target for running Linux kernels -&gt; Use an existing virtual machine<\/strong> <strong>-&gt; QEMU<\/strong>&#8220;. If you have not installed QEMU before, click the &#8220;Install QEMU&#8221; button to automatically download a Windows build of QEMU:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/04-newvm-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-677\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/04-newvm-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>Note that unlike VMWare or VirtualBox, QEMU does not need to install any services or drivers on the Windows machine and can be installed without using an administrator account. You can read more about VisualKernel integration with QEMU on <a href=\"https:\/\/sysprogs.com\/VisualKernel\/documentation\/qemu\/\">this page<\/a>.<\/li>\n<li>Once QEMU has been installed, click &#8220;Create a new virtual machine&#8221;. When VisualKernel asks for the VM parameters, enter the VM name\/location and point it to the installer disc image: \u00a0 <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/05-vmsettings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-678\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/05-vmsettings.png\" alt=\"\" width=\"881\" height=\"414\" \/><\/a>Press &#8220;OK&#8221; to create the VM. VisualKernel will save the settings you entered and will start the VM automatically.<\/li>\n<li>Depending on the QEMU version you are using, some Linux installers (e.g. <strong>Fedora\/CentoOS<\/strong>, but not <strong>Ubuntu<\/strong>) may freeze on the initial countdown screen. This happens due to the way QEMU emulates timer interrupts in real mode (before Linux is loaded) and does not affect other functionality: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/06-hang.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-679\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/06-hang.png\" alt=\"\" width=\"642\" height=\"537\" \/><\/a><\/li>\n<li>If this happens, locate the <strong>&lt;VM Name&gt;.run<\/strong> file in the VM directory and find out the GDB port used by the VM:\n<pre class=\"\">&lt;QemuVMRun xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"&gt;\r\n  &lt;PID&gt;22820&lt;\/PID&gt;\r\n  &lt;GDBPort&gt;5874&lt;\/GDBPort&gt;\r\n  &lt;MonitorPort&gt;5876&lt;\/MonitorPort&gt;\r\n  &lt;!-- .... --&gt;\r\n&lt;\/QemuVMRun&gt;<\/pre>\n<\/li>\n<li>Then launch the <strong>C:\\Program Files (x86)\\Sysprogs\\VisualKernel\\KernelTools\\gdb-multiarch.exe<\/strong> file and run the following command:\n<pre class=\"\">x\/3i $pc-1<\/pre>\n<p>If GDB shows that the VM is stopped juts after the &#8220;hlt&#8221; instruction, it is affected by the timer bug. To workaround it, replace &#8216;hlt&#8217; with &#8216;nop&#8217; and resume the VM:<\/p>\n<pre class=\"\">set *((char *)$pc-1)=0x90\r\nstepi\r\ncontinue<\/pre>\n<p><a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/07-continue-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-692\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/07-continue-1.png\" alt=\"\" width=\"979\" height=\"512\" \/><\/a><\/li>\n<li>This should unfreeze the VM and allow you to install the Linux OS as usual:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/08-install-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-681\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/08-install-1.png\" alt=\"\" width=\"1282\" height=\"857\" \/><\/a><\/li>\n<li>Proceed with the Linux installation. When the installation is finished, proceed with rebooting the VM: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/09-done.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-682\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/09-done.png\" alt=\"\" width=\"1282\" height=\"857\" \/><\/a><\/li>\n<li>Once the Linux OS has been installed, click &#8220;Next&#8221; in the kernel module wizard. VisualKernel will ask how to connect to the VM using SSH:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/10-ssh.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-683\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/10-ssh.png\" alt=\"\" width=\"586\" height=\"510\" \/><\/a><\/li>\n<li>Enter the settings and press &#8220;Connect&#8221;. VisualKernel will begin waiting for the VM to start the SSH server: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/10-waiting.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-684\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/10-waiting.png\" alt=\"\" width=\"586\" height=\"213\" \/><\/a>If the SSH server doesn&#8217;t start automatically, follow the instructions on <a href=\"https:\/\/visualgdb.com\/documentation\/linux\/ssh\/\">this page<\/a> to install and start it manually.<\/li>\n<li>Once the SSH connection has been established, VisualKernel will ask you for the build settings for your module:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/11-setup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-685\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/11-setup.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Proceed with the default settings, allowing VisualKernel to download and install the Linux kernel headers and symbols: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/12-download.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-686\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/12-download.png\" alt=\"\" width=\"736\" height=\"553\" \/><\/a><\/li>\n<li>Proceed with the suggested source code access settings (storing the sources on Windows and uploading them to Linux):<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/13-source.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-687\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/13-source.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>The last page of the wizard allows configuring the debugging settings. Proceed with the QEMU-specific debug method and press &#8220;Finish&#8221; to generate the project:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/14-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-688\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/14-debug.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Build the kernel module as usual and set a breakpoint in the init() function: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/15-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-689\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/15-build.png\" alt=\"\" width=\"1385\" height=\"923\" \/><\/a><\/li>\n<li>Press F5 to begin debugging. VisualKernel will automatically load the module into the kernel and the breakpoint will trigger: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/17-break.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-691\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/17-break.png\" alt=\"\" width=\"1385\" height=\"923\" \/><\/a><\/li>\n<li>You can use the <strong>Tools-&gt;VisualKernel-&gt;Manage QEMU Virtual Machines<\/strong> to view the QEMU VMs managed by VisualKernel. You can start\/stop each VM, create snapshots or view internal logs. See <a href=\"https:\/\/sysprogs.com\/VisualKernel\/documentation\/qemu\/\">this page<\/a> more details.<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/16-snap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-690\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/16-snap.png\" alt=\"\" width=\"1045\" height=\"699\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use VisualKernel and QEMU to efficiently develop and debug kernel modules. QEMU is very lightweight,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[56,26],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/674"}],"collection":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":2,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/674\/revisions\/694"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}