LoginSignup
5
5

More than 5 years have passed since last update.

[Linux][kernel module] 簡単なローダブルカーネルモジュールのビルドとロード

Last updated at Posted at 2016-07-26

kernel module のビルドとロードをやってみたメモ
dump_stack() でstacktraceが取得できます。

ソース

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

注意点:

  • makefile ではなく Makefile と先頭を大文字にすること
  • Makefile内はタブ文字でインデントとすること(GNU makeの文法として空白文字でインデントするとエラーになる)

実行例

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

関連

カーネルモジュールの情報を確認する方法まとめ - Qiita
[Linux][kernel module] ローダブルカーネルモジュールのロード時に引数でパラメータを渡す方法 - Qiita
[Linux][kernel module] カーネルモジュール内でkthreadを作成する - Qiita

参考

Compiling Kernel Modules
ARM用Linuxのカーネルモジュールを作成する - ひまじめ
makefile - Cross compiling a kernel module - Stack Overflow

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5