Linux Kernel Module Programming Jason Barto September 4, 2003 Jason Barto
Agenda Introduction Review Standard Functions Write Module v0.1 Compiling Modules Handling Runtime Parameters More Kernel Macros Write Module v0.2 Close and Further Readings Jason Barto
Introduction Similar to Java Servlets with predefined structure and function names Common C functions are not available to modules (such as printf, scanf, strcat, or system) Instead you must use kernel defined functions (/proc/ksyms) Jason Barto
Review Standard Functions printk () 8 Priority Levels: KERN_WARNING, KERN_ALERT Init function sets up variables, tells the kernel what the module provides and registers any necessary functions Cleanup function undoes changes made by init module_init and module_exit Tell kernel which functions are to be used at init and cleanup Jason Barto
Module v0.1 /* module-0.1.c - The simplest kernel module. */ #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_ALERT */ #include <linux/init.h> /* Needed for macros */ int module_v0_1_init(void) { printk(KERN_ALERT "Initing module v0.1\n"); /* A non 0 return means init_module failed; module can't be loaded. */ return 0; } void module_v0_1_stop(void) { printk(KERN_ALERT “Unloading module v0.1\n"); module_init (module_v0_1_init); module_exit (module_v0_1_stop); Jason Barto
Compiling Modules Kernel modules are object files thus the ‘-c’ flag must be used with gcc ‘-O2’ causes the compiler to use optimization and thus translate kernel macros ‘-isystem /lib/modules/`uname -r`/build/include’ will tell gcc to include the proper kernel header files Jason Barto
Compiling Modules cont.. ‘-D__KERNEL__’ will define the KERNEL variable indicating this code will run in kernel mode, not as a user process ‘-DMODULE’ indicates to gcc that this code is a kernel module Jason Barto
Handling Runtime Parameters MODULE_PARM () 2 arguments variable name and type (“b”, “h”, “i”, “l”, “s”) A 2nd parameter of “1-5i” indicates an array of integers between 1 and 5 ints long Declare parameter receiving variables globally Jason Barto
More Kernel Macros MODULE_LICENSE () MODULE_AUTHOR () MODULE_DESCRIPTION () MODULE_SUPPORTED_DEVICE () Jason Barto
Module v0.2 /* module-0.2.c - The simplest kernel module. */ #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_ALERT */ #include <linux/init.h> /* Needed for macros */ MODULE_LICENSE (“GPL”) MODULE_AUTHOR (“Jason Barto”); MODULE_DESCRIPTION (“A module to teach module writing”); MODULE_SUPPORTED_DEVICE (“The mind”); int intparm = 0; char *strparm; MODULE_PARM (strparm, “s”); MODULE_PARM (intparm, “i”); Jason Barto
Module v0.2 cont.. int module_v0__init (void) { printk (KERN_ALERT "Initing module v0.2\n"); printk (KERN_ALERT “Received an int of %d and a string of %s\n”, intparm, strparm); return 0; } void module_v0_2_stop (void) { printk (KERN_ALERT “Unloading module v0.2\n"); module_init (module_v0_2_init); module_exit (module_v0_2_stop); Jason Barto
Further Reading Kernel Module Programming Guide http://www.tldp.org/LDP/lkmpg/ Kernel Device Drivers Introduction http://www.linuxjournal.com/article.php?sid=1219 Kernel Resource Links Page http://jungla.dit.upm.es/~jmseyas/linux/kernel/hackers-docs.html Jason Barto