The linux kernel module programming guide

Hopefully someone can update the LDD(linux device driver) and Linux kernel books. In fact Linux Foundation should sponsor such efforts since technical book like this is hard to make any profit.

I've written a little bit about writing a driver & using QEMU to create a custom device for it at [0] & [1]

Are you the David V from Meta, who had bytelab.codes? I recently discovered that blog, and was very excited by the content, only to find he last updated in 2022. Either way, I’m excited to see your site, too! I love finding well-written kernel-level stuff.

Did some digging (embarrassingly) and I don't think they are the same person. Regardless it's quite an interesting blog post!

virtme-ng https://github.com/arighi/virtme-ng makes it really easy to launch development kernels in qemu.

I use qemu extensively especially for early-stage kernel debugging when no console is available; one such was just this week with v6.8 where, on arm64, any kernel command-line parameter >= 146 characters hangs the kernel instantly and silently.

Here's how I used qemu + gdb (on Debian 12 Bookworm amd64 host) to emulate and execute the arm64 kernel build to single-step the problematic code to identify the cause.

1. In a prepared kernel build system (i.e; all build dependencies and cross-compile tools installed) build the kernel image. I do this in an unprivileged systemd-nspawn amd64 container to avoid messy -dev package installs on the host. Nspawn bind-mounts the host's source-code tree which includes a separate build directory:

 cd "$" # copy/install/configure a suitable $/.config; review/edit with: make V=1 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=$ -j 4 menuconfig # build the kernel export KBUILD_BUILD_USER=linux; export KBUILD_BUILD_HOST=iam.tj; time make V=1 LOCALVERSION="" ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=$ -j 12 Image # build gdb helper (Python) scripts export KBUILD_BUILD_USER=linux; export KBUILD_BUILD_HOST=iam.tj; time make V=1 LOCALVERSION="" ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=$ scripts_gdb 

This will create the debug symbols needed by gdb in $/vmlinux and the executable kernel in $/arch/arm64/boot/Image

2. Install "gdb" (and if doing foreign architecture debugging "gdb-multiarch") on the host as well as "qemu-system-arm"

3. Execute the kernel but -S[uspend] it and have QEMU listen for a connection from gdb:

 qemu-system-aarch64 -machine virt,gic-version=3 -cpu max,pauth-impdef=on -smp 2 -m 4096 -nographic -kernel $/arch/arm64/boot/Image -append "debug $( for l in ; do echo -n param$l=$(pwgen $((l-9)) 1)' '; done )" -initrd rootfs/boot/initrd.img-6.8.12-arm64-debug -S -gdb tcp::1234 

The -append and -initrd shown here are optional; in my case no -initrd is actually needed since the (silent) panic occurs in the first few instructions the kernel executes. If debugging loadable modules however they would be in the initrd and loaded in the usual way. If the problem being diagnosed occurs after the root file-system and userspace proper are active then one would need to add the appropriate qemu options for the emulated storage device where the root file-system lives.

4. In another terminal shell (I use "tmux" and create a new tmux window) start the debugger:

 cd $ # this cd is important - gdb needs to be in the base of the BUILD directory gdb-multiarch ./vmlinux 
5. In the gdb shell:
 target remote :1234 break __parse_cmdline continue 

At this point the usual gdb functionality is available to examine memory, variables, single-step, view the stack and so on.

For more details on debugging kernel using gdb and the gdb scripts lx-* see

Edit: Forgot to note that for gdb to be able to use the lx-* Python scripts it usually needs the path authorising:

 echo "add-auto-load-safe-path $/scripts/gdb/vmlinux-gdb.py" > ~/.gdbinit

The wireguard test suite that’s now in the kernel is an excellent way to experiment with using qemu to develop kernel modules and also do automated tests.

I’d link but cumbersome to find on phone.

do you mean this one: https://git.zx2c4.com/wireguard-linux/tree/tools/testing/sel.

there are only 3 files under drivers/net/wireguard/selftest and no qemu there in linux kernel git

 allowedips.c counter.c ratelimiter.c