240 likes | 251 Views
Learn about kernel modules, how to create them, example modules, functions, and kernel headers. Understand the benefits of dynamically loading modules and how to interact with /proc in an operating system.
E N D
Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems
Kernel Modules Or “drivers”, if you prefer…
Kernel Module • A kernel module is a portion of kernel functionality that can be dynamically loaded into the operating system at run-time • Example • USB drivers • File system drivers • Disk drivers • Cryptographic libraries
Why not just compile everything into the kernel? • Each machine only needs a certain number of drivers • For example, should not have to load every single motherboard driver • Load only the modules you need • Smaller system footprint • Dynamically load modules for new devices • Camera, new printer, etc.
Creating a Kernel Module • Hello world example
Sample Kernel Module: hello.c #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static inthello_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);
Sample Kernel Module: hello.c Module headers #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static inthello_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);
Sample Kernel Module: hello.c License declaration #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static inthello_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);
Sample Kernel Module: hello.c #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static inthello_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); Initialization function, runs when module loaded Tells kernel which function to run on load
Sample Kernel Module: hello.c #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static inthello_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); Exit function, runs when module exits Tells kernel which function to run on exit
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*
Compile the Kernel Module /usr/src/hello$> make • Creates hello.ko – This is the finished kernel module!
Inserting and Removing the Module • insmod – insert a module /usr/src/hello$> sudo insmod hello.ko • rmmod – remove a module /usr/src/hello$> sudo rmmod hello.ko
Listing Modules • lsmod – lists all running modules /usr/src/hello$>lsmod
Where is it printing? • 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 • Demo…
Kernel Module vs User Application • All kernel modules are event-driven • Register functions • Wait for requests and service them • Server/client model • No standard C library • Why not? • No floating point support • Segmentation fault could freeze/crash your system • Kernel ‘oops’!
Kernel Functions • printk() instead of printf() • kmalloc() instead of malloc() • kfree() instead of free() • Where can I find definitions of these kernel functions?
Kernel manpages • Section 9 of manpages • Use manpages $> sudo apt-get install linux-manual-3.16 $> man printk
Kernel Headers • #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-4.15.4/include/ for more… • Google is also your friend
procfs Kernel Module • procfs “hello world” example • Creates a read/write procfs entry • Steps • Create entry in module_init function • Registers reading/writing function using file_operations structure • Delete entry in module_cleanup function
Testing Procfs $> sudo insmod hello_proc.ko $> sudo tail /var/log/syslog $> cat /proc/helloworld $> echo 2 > /proc/helloworld $> sudo rmmod hello_proc
Part 2: Remember • You will write your own proc module called remember that • Allows the user to write a string to /proc/remember (max length 80) • Allows the user to read /proc/remember and get back the string that was just added • If no string was added, the string EMPTY should be read instead
Remember Example #> echo “Hello there” > /proc/remember #> cat /proc/remember Hello there #>