From 8eb878c24eb0afb6ddd452973653b3f9684155f4 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Thu, 15 Dec 2016 22:53:13 +0000 Subject: [PATCH] update --- README.md | 2 ++ TODO.md | 7 +++++++ build.md | 18 +++++++++++++++++- host/Makefile | 31 +++++++------------------------ host/README.md | 24 +++++++++++++++--------- 5 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 TODO.md diff --git a/README.md b/README.md index 4cb330e..9155383 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,5 @@ 1. [kmod](kmod.md) 1. Examples 1. [Host](host/) + +TODO: use Buildroot as in this answer http://stackoverflow.com/a/38813400/895245 to install and test modules on QEMU. diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..e465721 --- /dev/null +++ b/TODO.md @@ -0,0 +1,7 @@ +# TODO + +- build kernel module against a given kernel tree +- build out of tree kernel module into the kernel image + - http://stackoverflow.com/questions/7353851/insert-linux-kernel-module-statically + - http://stackoverflow.com/questions/8421970/compiling-a-driver-as-a-part-of-a-kernel-not-as-a-module +- .ko file format vs .o http://stackoverflow.com/questions/10476990/difference-between-o-and-ko-file diff --git a/build.md b/build.md index 8943a3d..0d17591 100644 --- a/build.md +++ b/build.md @@ -2,7 +2,23 @@ The module building system. -Kernel modules are built using a makefile located at: +Explained at: + +Full method: get the kernel, build it to a directory `$KDIR`, and then run: + + make -C "$KDIR" M="$(PWD)" modules + +Quick method: no need for a full build, just: + + make modules_prepare + +but you lose some functionality. TODO: module insert on host then fails with: + + insmod: ERROR: could not insert module hello.ko: Invalid module format + +even though kernel tree was checked out to match the host. + +Using your distro's kernel version: /lib/modules/$(uname -r)/build diff --git a/host/Makefile b/host/Makefile index 7697721..bf34299 100644 --- a/host/Makefile +++ b/host/Makefile @@ -1,33 +1,16 @@ -OUT_EXT := .ko -OBJ_EXT := .o -RUN := hello -RUN_EXT := $(RUN)$(OUT_EXT) - obj-m += hello.o ccflags-y := -Wno-declaration-after-statement -std=gnu99 -.PHONY: clean ins log rm run my-ins +.PHONY: all clean -all: $(RUN_EXT) ins_rm_mod.out +all: hello.ko ins_rm_mod.out hello.ko: hello.c - make -C /lib/modules/$(shell uname -r)/build M="$(PWD)" modules + make -C '/lib/modules/$(shell uname -r)/build' M='$(PWD)' modules clean: - make -C /lib/modules/$(shell uname -r)/build M="$(PWD)" clean - rm -f *.out + make -C '/lib/modules/$(shell uname -r)/build' M='$(PWD)' clean + rm -f ins_rm_mod.out -ins: all - sudo insmod '$(RUN_EXT)' - -log: - dmesg - -rm: - if lsmod | grep -Eq '^$(RUN) '; then sudo rmmod '$(RUN_EXT)'; fi - -ins_rm_mod.out: ins_rm_mod.c - gcc -Wall -std=gnu99 -o '$@' '$<' - -ins_rm_run: ins_rm_mod.out $(RUN_EXT) - sudo ./ins_rm_mod.out +ins_rm_mod.out: ins_rm_mod.c hello.c + gcc -Wall -std=gnu99 -o ins_rm_mod.out ins_rm_mod.c diff --git a/host/README.md b/host/README.md index 53b2c3f..5e084e1 100644 --- a/host/README.md +++ b/host/README.md @@ -9,7 +9,7 @@ Simple things that can be demonstrated by inserting a module into the currently This method easier to setup, but it is not recommended for development, as: -- it may break your system. +- it may break your system - you can't control which kernel version to use Use VMs instead. @@ -20,15 +20,21 @@ We only use it for super simple examples. Build, insert and remove a hello world module: - make ins - make rm - make log + make -The last lines of the log should contain: + sudo insmod hello.ko - init_module - cleanup_module + # Our module should be there. + sudo lsmod | grep hello -Insert and remove a module from a C program: + # Last message should be: init_module + dmest -T - make ins_rm_run + sudo rmmod hello + + # Last message should be: cleanup_module + dmest -T + +Insert and remove the `hello.ko` module from a C program with system calls: + + sudo ./ins_rm_mod.out