<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abhishekojha on Medium]]></title>
        <description><![CDATA[Stories by Abhishekojha on Medium]]></description>
        <link>https://medium.com/@abhishekojha38?source=rss-ee9f0ece97fe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Xjeq6qI_OGY2aowI</url>
            <title>Stories by Abhishekojha on Medium</title>
            <link>https://medium.com/@abhishekojha38?source=rss-ee9f0ece97fe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 20:48:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abhishekojha38/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Debugging the Linux Kernel with KGDB]]></title>
            <link>https://medium.com/@abhishekojha38/debugging-the-linux-kernel-with-kgdb-93b7c422bf39?source=rss-ee9f0ece97fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/93b7c422bf39</guid>
            <category><![CDATA[debugger]]></category>
            <category><![CDATA[kgdb]]></category>
            <category><![CDATA[embedded-systems]]></category>
            <category><![CDATA[gdb]]></category>
            <category><![CDATA[device-drivers]]></category>
            <dc:creator><![CDATA[Abhishekojha]]></dc:creator>
            <pubDate>Sat, 08 Aug 2026 19:21:31 GMT</pubDate>
            <atom:updated>2026-08-08T19:21:31.723Z</atom:updated>
            <content:encoded><![CDATA[<p><em>How to pause a running kernel, set breakpoints in kernel functions, and step through it exactly like you would an application from QEMU to real hardware</em></p><p>Most kernel debugging happens the slow way: add a printk, rebuild, reflash, reboot, repeat. It works, but it&#39;s blunt and it falls apart the moment you&#39;re chasing something timing-sensitive or a crash that only shows up three subsystems deep.</p><p>KGDB is the alternative. It turns the Linux kernel itself into a GDB server, which means you can attach a debugger from a separate host machine, pause the kernel mid-execution, set breakpoints inside kernel functions, inspect memory and call stacks, and step through kernel code line by line the same workflow you’d use to debug a userspace application.</p><p>This post walks through how KGDB actually works, how to set it up in QEMU to learn the mechanics safely, and how to take the same approach to real hardware over a serial line. The full reference notes are in my tech journal: <a href="https://github.com/Abhishekojha38/tech-journal/blob/main/kgdb/Debug-kernel-using-kgdb.adoc">Debug-kernel-using-kgdb.adoc</a></p><h3>How KGDB works</h3><p>The mental model is simpler than it sounds. There are two machines:</p><ul><li>The <strong>host</strong>, running the GDB client</li><li>The <strong>target</strong>, running the Linux kernel you want to debug</li></ul><p>A connection, a serial cable or a network link ties them together, and KGDB, built into the target kernel, acts as the GDB server on the other end, listening for commands from the host.</p><p>Once connected, you can pause the kernel, examine registers, set breakpoints in kernel functions , and step through execution instruction by instruction. It&#39;s the full GDB toolkit, just pointed at the kernel instead of a process.</p><h3>Kernel configuration</h3><p>Before any of this works, the kernel needs to be built with debugging support switched on.</p><p><strong>Enable:</strong></p><ul><li>CONFIG_KGDB — turns on remote debugging</li><li>CONFIG_KGDB_SERIAL_CONSOLE — lets GDB use the serial console</li><li>CONFIG_KALLSYMS_ALL — includes all symbols in the kernel image</li><li>CONFIG_MAGIC_SYSRQ — enables sysrq triggers</li><li>CONFIG_FRAME_POINTER — preserves stack/backtrace information</li><li>CONFIG_DEBUG_INFO — includes debug symbols</li><li>CONFIG_GDB_SCRIPTS — enables the GDB Python helper scripts</li></ul><p><strong>Disable:</strong></p><ul><li>CONFIG_DEBUG_INFO_REDUCED — otherwise debug info gets stripped down</li><li>CONFIG_RANDOMIZE_BASE — disables KASLR, which randomizes the kernel&#39;s memory layout at boot and would otherwise make breakpoint addresses unpredictable</li></ul><p>If you can’t rebuild, KASLR can also be turned off at boot time by editing the boot arguments:</p><pre>setenv bootargs &quot;${bootargs} nokaslr&quot;<br>saveenv</pre><h3>Trying it out in QEMU first</h3><p>Real hardware adds variables you don’t need while you’re still learning the mechanics serial cables, flaky connections, a target that might not boot. QEMU removes all of that, so it’s the right place to build intuition first, using the <a href="https://github.com/Abhishekojha38/yocto-playground">yocto-playground</a> build environment.</p><p>After building the base image and enabling the kernel configs above via bitbake -c menuconfig virtual/kernel, the first thing worth confirming is that the compiled vmlinux actually has debug symbols:</p><pre>file vmlinux</pre><pre>vmlinux: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), statically linked, BuildID[...], with debug_info, not stripped</pre><p>Then launch QEMU with two flags that matter a lot here:</p><pre>runqemu playground-x86 nographic slirp qemuparams=&quot;-s -S&quot; bootparams=&quot;nokaslr&quot;</pre><ul><li>-s starts a GDB server inside QEMU on TCP port 1234</li><li>-S starts QEMU <strong>paused</strong>, waiting for GDB to attach which means you can set breakpoints before a single line of kernel code runs</li></ul><p>nokaslr keeps kernel symbol addresses fixed, so breakpoints and address lookups stay reliable instead of shifting on every boot.</p><p>With the target paused, open a second terminal, cd into the kernel build directory, and start GDB against the same vmlinux:</p><pre>gdb ./vmlinux</pre><p>From inside GDB, load the kernel and module symbols, set a breakpoint, and connect:</p><pre>(gdb) lx-symbols<br>(gdb) b cmos_set_time<br>(gdb) target remote :1234</pre><p>At that point GDB is live-attached to a paused kernel. lx-dmesg dumps the target&#39;s kernel log buffer straight into your GDB session, and apropos lx lists the full set of Linux-aware GDB helper commands things like lx-ps to dump tasks, lx-lsmodfor loaded modules, or lx-device-list-tree to walk the device tree. These exist because plain GDB has no concept of kernel data structures; the vmlinux-gdb.py script (enabled by CONFIG_GDB_SCRIPTS) teaches it how to read them.</p><blockquote><em>If GDB refuses to auto-load </em><em>vmlinux-gdb.py, it&#39;s usually a safe-path restriction. Add the script&#39;s path to </em><em>add-auto-load-safe-path in </em><em>~/.gdbinit and it&#39;ll load without complaint.</em></blockquote><p>add-auto-load-safe-path &lt;PATH&gt;/vmlinux-gdb.py</p><h3>Debugging a loaded kernel module</h3><p>Debugging the kernel body is one problem debugging a loadable module is a slightly different one. The kernel itself lives at well-known virtual addresses, matching the load addresses in the vmlinux ELF file. A .ko module doesn&#39;t have that luxury: it&#39;s just a collection of sections with no fixed load address until the kernel&#39;s module loader (kernel/module.c) decides where to put it at insmod time.</p><p>That means you don’t know where a module will live in memory until <em>after</em> it’s loaded but at that point, you can find out.</p><p>Boot QEMU with a network-capable NIC device attached and kgdboc set at boot:</p><pre>runqemu playground-arm64 nographic qemuparams=&quot;-s -netdev tap,id=net1,ifname=tap1,script=no,downscript=no -device minimal-pcie-nic,netdev=net1&quot; bootparams=&quot;nokaslr kgdboc=ttyAMA0,115200&quot;</pre><p>Once the driver is loaded, its section addresses are exposed under sysfs:</p><pre>for I in /sys/module/minimal_pcie_nic_drv/sections/.*; do echo -n &quot;$I: &quot;; cat &quot;$I&quot;; done</pre><p>That gives you the exact addresses of .text, .init.text, .rodata, and every other section everything GDB needs to locate the module&#39;s code in memory.</p><p>Register the kgdb I/O driver at runtime:</p><pre>echo &quot;ttyAMA0,115200&quot; &gt; /sys/module/kgdboc/parameters/kgdboc</pre><p>Then, back in GDB, load the module’s symbol file at the addresses you just read from sysfs, and set a breakpoint inside it like any other function:</p><pre>(gdb) add-symbol-file &lt;path-to&gt;/minimal_pcie_nic_drv.ko 0xffff80007a5a9000 \<br>    -s .init.data 0xffff80007a5ab000 \<br>    -s .rodata.str1.8 0xffff80007a5a7018<br>(gdb) break minimal_pcie_nic_drv_tx</pre><p>From here it behaves exactly like debugging any other kernel function — except it’s code that didn’t even exist at boot time.</p><h3>Taking it to real hardware</h3><p>The QEMU setup uses a raw TCP socket for the GDB connection, which isn’t available on physical boards those typically expose one serial line for both console output and KGDB. agent-proxy solves that by splitting a single serial port into two logical channels: one for the console, one for GDB.</p><pre>git clone http://git.kernel.org/pub/scm/utils/kernel/kgdb/agent-proxy.git<br>cd agent-proxy<br>make<br>./agent-proxy &#39;127.0.0.1:5550^127.0.0.1:5551&#39; 0 /dev/ttyUSB0,115200</pre><p>Port 5550 gives you the console over telnet; port 5551 is what GDB connects to.</p><p>There are two ways to bring KGDB up on the target:</p><p><strong>At runtime</strong>, once it’s already booted:</p><pre>echo ttymxc0 &gt; /sys/module/kgdboc/parameters/kgdboc</pre><p><strong>At boot</strong>, by adding to the kernel command line:</p><pre>kgdboc=ttymxc0,115200 kgdbwait</pre><p>kgdbwait is the interesting one, it halts the kernel early in boot until a debugger actually attaches:</p><pre>KGDB: Waiting for connection from remote gdb...<br>[2]kdb&gt;</pre><p>If the target is already running, you can trigger a KGDB break-in on demand with a sysrq command instead:</p><pre>echo g &gt; /proc/sysrq-trigger</pre><h3>Connecting GDB to the target</h3><p>With the Yocto SDK environment set up, connect using the cross GDB for your target architecture:</p><pre>aarch64-linux-gnu-gdb ./vmlinux</pre><p>Through the agent-proxy:</p><pre>(gdb) target remote localhost:5551</pre><p>Or directly over serial, without agent-proxy:</p><pre>(gdb) set remotebaud 115200<br>(gdb) target remote /dev/ttyS0</pre><p>From there, the same debugging vocabulary applies whether you’re on an i.MX8MQ board or a QEMU instance set a breakpoint, continue, inspect a struct:</p><pre>(gdb) b dcss-dtg.c:134<br>(gdb) c<br>(gdb) p *dtg<br>(gdb) x/1x 0xffff800082363000<br>(gdb) info frame</pre><p>And the Linux-aware helpers lx-clk-summary, lx-symbols, lx-dmesg, lx-list, lx-device-list work identically on real hardware as they did in QEMU, because they&#39;re reading kernel data structures, not emulator internals.</p><h3>Why this is worth learning</h3><p>printk debugging is fine for the obvious bugs. It falls apart for the ones that depend on precise timing, or that live three function calls deep in a code path you don&#39;t fully understand yet. KGDB turns kernel debugging from &quot;guess, rebuild, reboot&quot; into &quot;pause, inspect, step&quot; the same workflow every application developer already trusts, just pointed at ring 0 instead of userspace.</p><p>Learning it in QEMU first where a bad breakpoint costs you nothing but a VM restart is the safest way to build that muscle memory before taking it to hardware where a mistake means a bench reset.</p><p>Full reference notes, including the kernel config list and every command shown above, are here: <a href="https://github.com/Abhishekojha38/tech-journal/blob/main/kgdb/Debug-kernel-using-kgdb.adoc">Debug-kernel-using-kgdb.adoc</a></p><p><em>Abhishek Ojha — Open Source Software Consultant</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93b7c422bf39" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a QEMU PCIe Device from Scratch]]></title>
            <link>https://medium.com/@abhishekojha38/building-a-qemu-pcie-device-from-scratch-9bb948f6bfd6?source=rss-ee9f0ece97fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bb948f6bfd6</guid>
            <category><![CDATA[embedded-systems]]></category>
            <category><![CDATA[qemu]]></category>
            <category><![CDATA[pcie]]></category>
            <category><![CDATA[device-drivers]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Abhishekojha]]></dc:creator>
            <pubDate>Sat, 08 Aug 2026 19:00:53 GMT</pubDate>
            <atom:updated>2026-08-08T19:00:53.388Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building a PCIe Device from Scratch Inside QEMU</h3><p><em>A hands-on walkthrough of implementing, debugging, and validating a virtual PCIe NIC, from a bare BAR to a full DMA-based TX/RX data path</em></p><p>If you’ve ever wanted to understand PCIe at a level deeper than reading the spec how a device actually announces itself to the bus, how MSI/MSI-X interrupts really work under the hood, or how a driver and a DMA engine talk to each other, there’s no better way to learn than building the device yourself.</p><p>That’s exactly what I did. Over a series of progressive steps, I built a virtual PCIe NIC inside QEMU, paired it with a custom Linux driver, and pushed it all the way from “shows up in lspci&quot; to a working ping test over an emulated DMA-backed TX/RX path.</p><p>The full source is on GitHub: <a href="https://github.com/Abhishekojha38/qemu-pcie">qemu-pcie</a></p><p>This post walks through what the project covers and how PCIe works.</p><h3>Why build a dummy PCIe device?</h3><p>QEMU already emulates real hardware, but that’s the problem when you’re emulating something that already exists, you’re mostly reading someone else’s design decisions. Writing your <em>own</em> PCIe device inside QEMU strips that away. You have to decide:</p><ul><li>What lives in configuration space</li><li>Device internal register space</li><li>How BARs get sized and mapped</li><li>When and how interrupts fire</li><li>How a DMA engine moves data without the CPU babysitting every byte</li></ul><p>And then you have to write the Linux driver on the other side and make it all actually talk.</p><h3>What the project covers</h3><p>The repo is structured as five progressive demos, each one adding a layer of complexity:</p><ul><li>Basic PCIe device creation, visible in lspci</li><li>BARs and MMIO</li><li>PCIe configuration space</li><li>MSI / MSI-X interrupts</li><li>An emulated DMA engine for RX and TX data paths</li><li>Ring-descriptor-based data transfer</li></ul><p>Each stage builds on the last, so you can stop wherever your curiosity runs out or keep going until you have a device that can actually push packets.</p><h3>What happens when QEMU starts the device</h3><p>At a high level, the flow is simple:</p><ol><li>QEMU starts and registers the PCIe device</li><li>The PCI bus discovers it via -device minimal-pcie-nic</li><li>The device’s realize function sets its vendor/device IDs</li><li>Linux enumerates it and sees 1234:11E8</li></ol><p>Everything else in the project builds outward from that moment.</p><h3>Stage 1 — The bare minimum: showing up in lspci</h3><p>The first milestone is deliberately anticlimactic: get the device to exist. No BARs, no interrupts just a PCI device that Linux can see.</p><pre>00:05.0 Ethernet controller: Device 1234:11e8<br>        Subsystem: Red Hat, Inc. Device 1100<br>        Flags: fast devsel</pre><p>And in dmesg:</p><pre>pci 0000:00:05.0: [1234:11e8] type 00 class 0x020000</pre><p>It’s a small win, but it confirms the plumbing Kconfig entry, Meson build entry, and devtool integration with the Yocto build is all wired correctly before adding any real functionality.</p><h3>Stage 2 — BARs and MMIO</h3><p>Next, the device announces a real memory region: BAR0, a 4KB non-prefetchable MMIO window at 0x10043000.</p><pre>Region 0: Memory at 10043000 (32-bit, non-prefetchable) [size=4K]</pre><p>This is where it gets fun you can devmem2 directly into the device&#39;s register space and watch your read/write callbacks fire in real time:</p><pre>root@playground-arm64:~# devmem2 0x10043000 w 0xdeafbeaf<br>minimal_pcie_nic: MMIO write addr=0x0 size=4 data=0xdeafbeaf<br>Write at address 0x10043000: 0xDEAFBEAF, readback 0xDEAFBEAF</pre><p>Reading it back confirms the value stuck the register is live.</p><h3>Stage 3 — MSI and MSI-X interrupts</h3><p>This is where the project earns its keep as a learning tool. MSI interrupts work by writing a specific data value to a specific memory address no shared IRQ lines, no legacy INTx quirks.</p><p>On the QEMU side, enabling MSI is one call:</p><pre>msi_init(pdev,<br>         0,      /* offset in config space */<br>         4,      /* number of MSI vectors */<br>         false,  /* 32-bit address */<br>         true,   /* per-vector masking enabled */<br>         errp)</pre><p>On the driver side, it’s a matching pair of calls — allocate vectors, then request an IRQ handler for each:</p><pre>mdev-&gt;nvec_irq = pci_alloc_irq_vectors(pdev, 1, MAX_MSI_VECTORS, PCI_IRQ_MSI);</pre><pre>for (i = 0; i &lt; mdev-&gt;nvec_irq; i++) {<br>    int irq = pci_irq_vector(pdev, i);<br>    devm_request_irq(&amp;pdev-&gt;dev, irq, minimal_irq_handler, 0, DRV_NAME, mdev);<br>}</pre><p>What made this concrete for me was watching the address/data pair in lspci -vvv:</p><pre>Address: 08020040  Data: 0060</pre><p>All four vectors share the same address only the data value changes per vector. Writing 0x60 to that address fires vector 0, 0x61 fires vector 1, and so on. Once you&#39;ve seen that in a live system, the spec language finally clicks.</p><p>MSI-X takes it further: it needs a second BAR to hold the vector table and pending-bit array, which lets each vector get its own independent address/data pair instead of sharing one.</p><pre>+-----------------------+ 0x10043000 (BAR0)<br>|   Control Registers   |<br>+-----------------------+ 0x10044000 (BAR1)<br>|   MSI-X Table &amp; PBA   |<br>+-----------------------+ 0x10045000</pre><p>Triggering each vector and watching /proc/interrupts increment the right counter is a satisfying way to confirm the whole chain device, bus, GIC, and driver is wired correctly:</p><pre>38:    1     0     0     0  GICv2m-PCI-MSIX-0000:00:05.0   0 Edge   minimal_pcie_nic_drv<br> 39:    0     0     0     0  GICv2m-PCI-MSIX-0000:00:05.0   1 Edge   minimal_pcie_nic_drv</pre><h3>Stage 4 — RX data path with an emulated DMA engine</h3><p>With interrupts working, the next step is moving actual data not just triggering a vector, but delivering bytes from “the network” into guest memory without the CPU copying them by hand.</p><p>The flow looks like this:</p><pre>host → QEMU user-net → minimal-pcie-nic → RX DMA → MSI-X → driver</pre><p>The driver writes the DMA address of a ring descriptor into BAR0. QEMU uses that address to know where to place incoming packets, then fires an interrupt once the transfer is done.</p><p>To test it, I brought up a tap interface on the host and pinged into the guest:</p><pre>ip tuntap add tap1 mode tap<br>ip link set tap1 up<br>ping 192.168.1.100 -I tap1</pre><p>And watched packets land on the guest side:</p><pre>minimal_pcie_nic_drv: RX packet len=90<br>minimal_pcie_nic_drv: RX packet len=90<br>minimal_pcie_nic_drv: RX packet len=203</pre><h3>Stage 5 — Full TX/RX with ring descriptors</h3><p>The final stage closes the loop: both transmit and receive, both DMA-backed, both using ring descriptors. This is the point where the virtual NIC stops being a toy and starts behaving like a real network interface enough to run a genuine two-way ping test with real RTTs:</p><pre>64 bytes from 192.168.10.5: icmp_seq=1 ttl=64 time=4.05 ms<br>64 bytes from 192.168.10.5: icmp_seq=2 ttl=64 time=0.508 ms<br>64 bytes from 192.168.10.5: icmp_seq=3 ttl=64 time=0.339 ms</pre><p>With debug tracing enabled, you can watch a single packet’s entire journey through the ring:</p><pre>minimal_pcie_nic: [TRACE] receive_packet size=98 rx_ring_base=0x47bbb000 rx_head=13<br>minimal_pcie_nic: [TRACE] desc[13] addr=0x45eec000 len=2048 flags=0x0<br>minimal_pcie_nic: [TRACE] DMA write 98 bytes → guest buf 0x45eec000<br>minimal_pcie_nic: [TRACE] desc marked RX_DONE, rx_head now 14, firing IRQ<br>minimal_pcie_nic: [TRACE] MSI-X notify vector=0</pre><p>Five lines that summarize the entire point of the project: a descriptor tells the device where to put data, the device does the DMA, marks the descriptor done, and interrupts the driver. That loop <em>is</em> how real NICs work this project just lets you watch it happen in slow motion, in your own code, with full visibility into every step.</p><h3>Why this is worth doing</h3><p>Reading about PCIe BARs, config space, MSI-X, DMA rings only gets you so far. Building a minimal device and its driver forces you to make every design decision explicit, and debugging your own bugs in this space teaches you far more about <em>real</em> hardware behavior than any datasheet does.</p><p>If you work in embedded Linux, driver development, or hardware bring-up, this kind of exercise is one of the fastest ways to build real intuition for the PCIe stack without needing a lab full of hardware to do it.</p><p>The full source, including all five stages, the Yocto build steps, and every diagram referenced above, is available here:</p><p><a href="https://github.com/Abhishekojha38/qemu-pcie"><strong>github.com/Abhishekojha38/qemu-pcie</strong></a></p><p>If you build on this or hit something interesting while working through it, I’d love to hear about it.</p><p><em>Note: this repo uses flat memory allocations and isn’t optimized, it was built purely to demonstrate PCIe components, not as production-grade code.</em></p><p><em>Abhishek Ojha — Open Source Software Consultant</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bb948f6bfd6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>