140 likes | 239 Views
CS 6560 Operating System Design. Lecture 14 Kernel 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. Kernel Modules. Are you tired of waiting for your kernel to compile?
E N D
CS 6560 Operating System Design Lecture 14 Kernel 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
Kernel Modules • Are you tired of waiting for your kernel to compile? • Kernel modules provide a way to quickly modify a running kernel. • They can be separately compiled and be dynamically added and be removed. • When added they become part of the kernel with access to the rest of the kernel.
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; } /* 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 - inserts module • rmmod - removes module • lsmod - lists modules • depmod - control dependencies • modinfo - display module meta data
Examples in Class • Modules_two • Compile (in directory that contains source code for new module) • # fakeroot 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