140 likes | 308 Views
CS 6560 Operating System Design Kernel Loadable Modules. References Textbook, chapter 16 http://tldp.org/LDP/lkmpg/2.6/html/index.html http://www.linux.org/docs/ldp/howto/Module-HOWTO/index.html https://linuxlink.timesys.com/docs/printk. Kernel Modules: Why?. System call like functionality.
E N D
CS 6560 Operating System DesignKernel Loadable Modules • References • Textbook, chapter 16 • http://tldp.org/LDP/lkmpg/2.6/html/index.html • http://www.linux.org/docs/ldp/howto/Module-HOWTO/index.html • https://linuxlink.timesys.com/docs/printk
Kernel Modules: Why? • System call like functionality. • Don’t have to recompile the kernel. • Separate modules are separate. • Modifying one module doesn’t require another module to recompile. • Modules can be inserted and removed dynamically. • When added they become part of the kernel with access to the rest of the kernel. • When removed, there is no mark left on the kernel (unlike new system calls).
Kernel Module Structure • Kernel modules consist of • An initialization routine that is called when the module is loaded • An exit routine that is called when the module is removed. • Functions and variables that can be exported for use by other parts of the kernel, including other modules. • Meta data that can be accessed by tools and the kernel.
Starting Example Here is an example of kernel module that has the basic structure. //*----------------------------------------------------------------------*/ /* File: CS6560_LKM.c Example of a loadable kernel module that initializes, exports functions, and exits. */ /* Standard headers for LKMs */ #include <linux/module.h> #include <linux/kernel.h> #include <linux/moduleparam.h> #include "CS6560_LKM.h" int init_CS6560_module(void); void exit_CS6560_module(void); /* Initialize the LKM */ int init_CS6560_module() { printk(KERN_ALERT "CS6560_LKM_one: init_CS6560_module\n"); return 0; }
Starting Example (cont.) /* Exit the LKM */ void exit_CS6560_module() { printk("CS6560_LKM_one: exit_CS6560_module\n"); } /* Example exported function */ int CS6560_LKM_function1(int arg1) { printk("CS6560_LKM_one: CS6560_LKM_function1\n"); return 0; } EXPORT_SYMBOL(CS6560_LKM_function1); module_init(init_CS6560_module); module_exit(exit_CS6560_module); MODULE_LICENSE("GPL"); MODULE_AUTHOR("CS6560 at CSUEB"); /*----------------------------------------------------------------------*/
Points • printw and Kernel loglevels - see man pages for syslogd and klogctl. Also see kernel code for printw.c. • Naming and registration of module init and exit functions. • Exporting symbols • Meta data macros
Development Modes • Standalone • Work in separate directory (see pages 282-283) (next slide) • Integrated • Put module code in source code tree (see textbook, pages 281-282) • Pick appropriate directory and place module code there • Add line (obj-m += …) to Makefile in that directory (see info make for how to set variables, and the kbuild documentation on configuration.)
Standalone Makefile # Makefile for kernel module development # # CS6560 Fall 2007 ########################## obj-m := CS6560_LKM_one.o obj-m += CS6560_LKM_two.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Loading and Unloading Modules • Commands • insmod • Insert an LKM into the kernel. • rmmod • Remove an LKM from the kernel. • depmod • Determine interdependencies between LKMs. • kerneld • Kerneld daemon program • ksyms • Display symbols that are exported by the kernel for use by new LKMs. • lsmod • List currently loaded LKMs. • modinfo • Display contents of .modinfo section in an LKM object file. • modprobe • Insert or remove an LKM or set of LKMs intelligently. For example, if you must load A before loading B, modprobe will automatically load A when you tell it to load B.
Examples in Class • Modules_two • Compile • # make • Look at modinfo CS6560_LKM_one.ko modinfo CS6560_LKM_two.ko • Bring up dmesg Use: dmesg | tail or use: cat /proc/kmsg • Load CS6560_LKM_one lsmod CS6560_LKM_one.ko • Look at /proc/modules /sys/module
Try the second one • Load and unload CS6560_LKM_two.ko • Look at what happens each time to • Messages • /proc/modules • /sys/module
Now with parameters • Initialization on command line • Changing variables with /sys/ using cat
Applications of Modules • Proc files • Device drivers • Interrupt Handlers • File systems • System calls • Monitoring and replacing core functionality such as scheduling
Proc Files Example • From • http://tldp.org/LDP/lkmpg/2.6/html/x710.html