Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 6560 Operating System Design Kernel Loadable Modules

Similar presentations


Presentation on theme: "CS 6560 Operating System Design Kernel Loadable Modules"— Presentation transcript:

1 CS 6560 Operating System Design Kernel Loadable Modules
References Textbook, chapter 16

2 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).

3 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.

4 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; }

5 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"); /* */

6 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

7 Development Modes Standalone Integrated
Work in separate directory (see pages ) (next slide) Integrated Put module code in source code tree (see textbook, pages ) 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.)

8 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

9 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.

10 Examples in Class Modules_two Compile Look at Bring up dmesg
# 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 /proc/modules /sys/module

11 Try the second one Load and unload CS6560_LKM_two.ko
Look at what happens each time to Messages /proc/modules /sys/module

12 Now with parameters Initialization on command line
Changing variables with /sys/ using cat

13 Applications of Modules
Proc files Device drivers Interrupt Handlers File systems System calls Monitoring and replacing core functionality such as scheduling

14 Proc Files Example From


Download ppt "CS 6560 Operating System Design Kernel Loadable Modules"

Similar presentations


Ads by Google