Kernel Modules Or drivers if you prefer 1

  • Slides: 19
Download presentation
Kernel Modules Or “drivers”, if you prefer… 1

Kernel Modules Or “drivers”, if you prefer… 1

Kernel Module n n A kernel module is a portion of kernel functionality that

Kernel Module n n A kernel module is a portion of kernel functionality that can be dynamically loaded into the operating system at run-time Example q q USB drivers File system drivers Disk drivers Cryptographic libraries 2

Why not just compile everything into the kernel? n Each machine only needs a

Why not just compile everything into the kernel? n Each machine only needs a certain number of drivers q n Load only the modules you need q n For example, should not have to load every single motherboard driver Smaller system footprint Dynamically load modules for new devices q Camera, new printer, etc. 3

Creating a Kernel Module n Hello world example 4

Creating a Kernel Module n Hello world example 4

Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static

Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); 5

Sample Kernel Module: hello. c Module headers #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual

Sample Kernel Module: hello. c Module headers #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); 6

Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); License

Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); License declaration static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); 7

Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static

Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } Initialization function, runs when module loaded static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); Tells kernel which function to run on load 8

Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static

Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } Exit function, runs when module exits static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); Tells kernel which function to run on exit 9

Sample Kernel Module: Makefile ifneq ($(KERNELRELEASE), ) obj-m : = hello. o else KERNELDIR

Sample Kernel Module: Makefile ifneq ($(KERNELRELEASE), ) obj-m : = hello. o else KERNELDIR ? = /lib/modules/`uname -r`/build/ PWD : = `pwd` default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif clean: rm -f *. ko *. o Module* *mod* 10

Compile the Kernel Module /usr/src/hello$> make n Creates hello. ko – This is the

Compile the Kernel Module /usr/src/hello$> make n Creates hello. ko – This is the finished kernel module! 11

Inserting and Removing the Module n insmod – insert a module /usr/src/hello$> sudo insmod

Inserting and Removing the Module n insmod – insert a module /usr/src/hello$> sudo insmod hello. ko n rmmod – remove a module /usr/src/hello$> sudo rmmod hello. ko 12

Listing Modules n lsmod – lists all running modules /usr/src/hello$>lsmod 13

Listing Modules n lsmod – lists all running modules /usr/src/hello$>lsmod 13

Where is it printing? n n Look inside /var/log/syslog Hint – to watch syslog

Where is it printing? n n Look inside /var/log/syslog Hint – to watch syslog in realtime, issue the following command in a second terminal: $> sudo tail –f /var/log/syslog 14

Kernel Module vs User Application n All kernel modules are event-driven q q q

Kernel Module vs User Application n All kernel modules are event-driven q q q n No standard C library q n n Register functions Wait for requests and service them Server/client model Why not? No floating point support Segmentation fault could freeze/crash your system q Kernel ‘oops’! 15

Kernel Functions n n printk() instead of printf() kmalloc() instead of malloc() kfree() instead

Kernel Functions n n printk() instead of printf() kmalloc() instead of malloc() kfree() instead of free() Where can I find definitions of these kernel functions? 16

Kernel manpages n n Section 9 of manpages Must install manually for our development

Kernel manpages n n Section 9 of manpages Must install manually for our development kernel $> wget http: //ftp. us. debian. org/debian/pool/main/l/linux 2. 6/linux-manual-2. 6. 32_2. 6. 32 -22_all. deb $> sudo dpkg –i linux-manual-2. 6. 32_2. 6. 32 -22_all. deb 17

Kernel Headers n n n n #include <linux/init. h> /* module stuff */ #include

Kernel Headers n n n n #include <linux/init. h> /* module stuff */ #include <linux/module. h> /* module stuff */ #include <asm/semaphore. h> /* locks */ #include <linux/list. h> /* linked lists */ #include <linux/string. h> /* string functions! */ Look inside linux-2. 6. 32/include/ for more… Google is also your friend 18

How can I explore the kernel? n Use lxr (“Linux Cross Referencer”): q q

How can I explore the kernel? n Use lxr (“Linux Cross Referencer”): q q n http: //lxr. linux. no/ Select your kernel version and enter search terms Use grep on your kernel source $> grep –Rn xtime /usr/src/linux 2. 6. 32 q R = recursive, n = display line number 19