[Linux] [kernel module] Build and load a simple loadable kernel module

A memo about building and loading the kernel module You can get a stacktrace with dump_stack ().

Source

testmod.c


#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>

MODULE_LICENSE("MIT");

static int testmod_init(void)
{
	printk(KERN_INFO "driver loaded\n");
	dump_stack();
	return 0;
}

static void testmod_exit(void)
{
	printk(KERN_INFO "driver unloaded\n");
}

module_init(testmod_init);
module_exit(testmod_exit);

Makefile


KERNELSRCDIR = /lib/modules/$(shell uname -r)/build
ARCH=x86
#ARCH=arm
#CROSS_COMPILE=arm-none-linux-gnueabi-
VERBOSE = 0

obj-m := testmod.o

all:
	make -C $(KERNELSRCDIR) \
		M=$(PWD) \
		KBUILD_VERBOSE=$(VERBOSE) \
		ARCH=$(ARCH) \
		CROSS_COMPILE=$(CROSS_COMPILE) \
		modules

clean:
	make -C $(KERNELSRCDIR) M=$(PWD) KBUILD_VERBOSE=$(VERBOSE) ARCH=$(ARCH) clean

important point:

Execution example

Check the operation by insmod


$ make
$ sudo insmod testmod.ko
$ sudo rmmod testmod
$ dmesg
[20487.308696] testmod: module license 'MIT' taints kernel.
[20487.308709] Disabling lock debugging due to kernel taint
[20487.310110] driver loaded
[20487.310116] Pid: 32263, comm: insmod Tainted: P           O XXXXX.x86_64 #1
[20487.310121] Call Trace:
[20487.310130]  [<ffffffffa00f8000>] ? 0xffffffffa00f7fff
[20487.310138]  [<ffffffffa00f801c>] testmod_init+0x1c/0x20 [testmod]
[20487.310146]  [<ffffffff8100212a>] do_one_initcall+0x12a/0x180
[20487.310213]  [<ffffffff810b60a6>] sys_init_module+0x10f6/0x20b0
[20487.310220]  [<ffffffff815f38e9>] system_call_fastpath+0x16/0x1b
[20487.383930] driver unloaded

Relation

Summary of how to check kernel module information --Qiita [Linux] [kernel module] How to pass parameters as arguments when loading a loadable kernel module --Qiita [Linux] [kernel module] Create kthread in kernel module --Qiita

reference

Compiling Kernel Modules Creating a Linux kernel module for ARM-seriously makefile - Cross compiling a kernel module - Stack Overflow

Recommended Posts

[Linux] [kernel module] Build and load a simple loadable kernel module
Build a simple WebDAV server on Linux
Linux kernel build time
[Linux] [kernel module] How to pass parameters as arguments when loading a loadable kernel module
Linux Kernel Build for DE10nano
[Note] WSL2 kernel build and use
[Linux] [kernel module] Create kthread in kernel module
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
Build a drone simulator environment and try a simple flight with Mission Planner
Build Linux on a Windows environment. Steps to install Laradock and migrate
[UE4] Build DedicatedServer on Windows and Linux
A quick overview of the Linux kernel
Build a Samba server on Arch Linux
[Linux] Build a jenkins environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server ―― 1. Apache introduction
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server --2 PHP introduction
Effective and simple Web server security measures "Linux"
Build a simple Python virtual environment without pyenv
Build a virtual environment with pyenv and venv
Load a package created locally with Go Module
Build a mruby development environment for ESP32 (Linux)
Build a server on Linux and local network with Raspberry Pi NextCloud and desktop sharing