1 / 22

Intro to Kernel Modules and /proc

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

aemma
Download Presentation

Intro to Kernel Modules and /proc

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems

  2. Kernel Modules Or “drivers”, if you prefer…

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

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

  5. Creating a Kernel Module • Hello world example

  6. 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);

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

  8. 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);

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

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

  11. 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*

  12. Compile the Kernel Module /usr/src/hello#> make • Creates hello.ko – This is the finished kernel module!

  13. Inserting and Removing the Module • insmod – insert a module /usr/src/hello#> insmod hello.ko • rmmod – remove a module /usr/src/hello#> rmmod hello.ko

  14. Listing Modules • lsmod – lists all running modules /usr/src/hello#>lsmod

  15. Where is it printing? • Look inside /var/log/syslog • Hint – to watch syslog in realtime, issue the following command in a second terminal: #> tail –f /var/log/syslog • Demo…

  16. 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’!

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

  18. Kernel manpages • Section 9 of manpages • Use manpages #> apt-get install linux-manual-4.9 #> man printk

  19. 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.20.10/include/ for more… • Google is also your friend

  20. Project 2: Kernel Modules

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

  22. Testing Procfs #> insmod hello_proc.ko #> tail /var/log/syslog #> cat /proc/helloworld #> echo 2 > /proc/helloworld #> rmmod hello_proc

More Related