Table of Contents

02. Your first kernel module

Way back when, kernels used to be monolithic, meaning that adding new functionality required recompiling and installing it, followed by a reboot. Today, things are much easier. By using the kmod daemon (man 8 kmod), users are allowed to load and unload modules (i.e.: kernel object files) on demand, without all the fuss. These modules are C programs that must implement initialization and removal functions that are called automatically. Usually, these functions register / unregister other functions contained in your object with core kernel systems.

We can use lsmod to get a list of all present modules, and modinfo to obtain detailed information about a specific module.

[student@host ~]$ lsmod
ecdh_generic           16384  1 bluetooth
 
[student@host ~]$ modinfo ecdh_generic | grep description
description:    ECDH generic algorithm
 
[student@host ~]$ modinfo bluetooth | grep description 
description:    Bluetooth Core ver 2.22

What we can understand from this is that the Elliptic Curve Diffie-Hellman module is 16384 bytes in size and is used by one other module, via the bluetooth ECDH helper. As you probably noticed, elixir.bootlin.com is a critical resource in navigating the kernel code.

If it's not a module that you're unsure about but a device, you can use udevadvm get more information about it. For example, if you have a NVMe drive (an SSD, let's say) and you want to figure out what drivers are involved in its operation, you can tell udevadm to scan sysfs bottom-up, starting with that device:

[student@host ~]$ udevadm info -a /dev/nvme0n1 | grep DRIVER
    DRIVERS=="nvme"
    DRIVERS=="pcieport"

From this, we glean that we need both the NVMe driver and the PCIe driver in order to operate our SSD.

Task A - Prepare your build system

Take a look at this piece of documentation before you get started. Then, create a new directory with the following structure:

.
├── Kbuild    --> defines the output module via obj-m
├── Makefile  --> defines the build targets, relying on the Linux headers
└── my_first_module.c

The makefile should look something like this:

KDIR ?= /lib/modules/`uname -r`/build
 
build:
	$(MAKE) -C $(KDIR) M=$(PWD) modules
 
clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean

Notice how KDIR is used to determine the precise kernel that we are compiling for. If invoked without overwriting KDIR, its default value ensures that the module is compiled for our current system (given that the kernel headers are installed). Note, however, that in order to compile the module for our board, it's not sufficient to point to the correct repo path. You still have to pass the CROSS_COMPILE and ARCH variables. Otherwise, the kernel's .config will be reset.

TLDR: modify KDIR + pass the appropriate variables when cross-compiling the Linux kernel!

Task B - Write a minimal module

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
 
MODULE_DESCRIPTION("A test module.");
MODULE_AUTHOR("Student");
MODULE_LICENSE("GPL");
 
/* custom log message header; used by pr_* */
#ifdef pr_fmt
#undef pr_fmt
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#endif
 
/* init - module initialization callback
 *  @return :  0 if everything went well ==> module is loaded
 *            -1 if an error ocurred     ==> module is not loaded
 */
static int init(void)
{
    pr_info("Hello world!\n");
 
    return 0;
}
 
/* fini - module removal callback
 */
static void fini(void)
{
    pr_info("Goodbye cruel, cruel world!\n");
}
 
/* register on_init and on_exit event handlers */
module_init(init);
module_exit(fini);

A few things to mention about this code:

Task C - Insert the module into the kernel

After you compile the kernel for your machine, insert it into the kernel, then remove it. You can do this via insmod and rmmod. Check the kernel debug log using dmesg.

Once you're convinced that the module works, clean the workspace then rebuild it for the board. Pass the kernel object (i.e.: *.ko file) via SSH, then repeat the process remotely.

If you want to investigate the kernel log of a previous session, you can use journalctl:

# show all log info from previous boot
[student@host ~]$ journalctl -b -1 -a

This can come in handy when you want to investigate crashes.

1) section(”.init.text”