Linux Kernel module learning notes ########## Makefile ########## obj-m: = modname. o modname will be generated after compilation. ko must have modname in the current directory. [c/s/S] The object obj-m value *. o *. ko ++ multiple files: obj-m: = modname. omodname-objs: = 1.o 2.o... compiled modname. o by modname-objs *. o generate *** will not compile the modname in the current directory. [c/s/S] File ########## module command ######### insmod modname. ko or modprobe modname to use The modprobe command to install the module to the file system lsmod [modname] or cat/proc/modulesrmmod m Odule_name or modprobe-r modnamemodinfo modname. ko ########## Basic module structure ########## include <linux/init. h> include <linux/module. h> static int _ init test_init (void) {printk ("hello test init !!! \ N "); return 0;} static void _ exit test_exit (void) {printk (" hello test exit !!! \ N ");} module_init (test_init); module_exit (test_exit); MODULE_LICENSE (" GPL "); MODULE_AUTHOR ("... "); MODULE_DESCRIPTION ("... "); www.2cto.com ########## module parameters ########## type var_name; module_param (var_name, type, perm ); insmod modname. ko var_name = value ++ ++ int num = SIZE; type var_name [SIZE]; module_param_array (var_name, type, & num, perm); insmod modname. ko var_name = val0, val1, val2 ,..., valSIZE ++ ++ var_name: variable (must be global) type: byte short ushort int uint long ulong charp bool invboolperm: Specify/sys/module/$ (modname)/parameters/$ (param_name) file permissions (the content of this file is the value of var_name) ---> kernel/include/linux/stat. h: 50 0 ---> (no corresponding file exists) S_IRWXUGO S_IALLUGO S_IRUGO S_IWUGO S_IXUGO & num: parameter length pointer (must be global) www.2cto.com ########## symbol export ######### EXPORT_SYMBOL (symbol_name ); kernel ----> kernel or module A ----> module A does not need to export the symbol module ----> kernel or module A ----> Module B can only use the exported symbol kernel ----> module no matter the module symbol cannot be used for export or not ######### printk ########## level: include/linux/printk. h: 9cat/proc/sys/kernel/printkecho a [B]>/proc/sys/kernel/printk a: messages with a lower level than a will be printed to console B: Default level