1 / 24

Intro to Kernel Modules and /proc

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.

afessenden
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$> sudo insmod hello.ko • rmmod – remove a module /usr/src/hello$> sudo 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: $> sudo 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 $> sudo apt-get install linux-manual-3.16 $> 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.15.4/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 $> sudo insmod hello_proc.ko $> sudo tail /var/log/syslog $> cat /proc/helloworld $> echo 2 > /proc/helloworld $> sudo rmmod hello_proc

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

  24. Remember Example #> echo “Hello there” > /proc/remember #> cat /proc/remember Hello there #>

More Related