Understanding Linux Kernel Modules: Coding and Implementation, Slides of Computer Applications

An overview of linux kernel modules, their role in os extensibility, and the process of creating and installing them using c language. It covers the basic structure of a c program for a kernel module, the use of 'insmod' and 'rmmod' commands, and the required functions and differences between a normal c application and a kernel module.

Typology: Slides

2012/2013

Uploaded on 04/17/2013

pamelaaaa
pamelaaaa 🇮🇳

4.5

(12)

103 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Typical C layout
Basic structure of a C program:
Comment-banner (showing title and abstract)
Preprocessor directives (e.g., for header-files)
Global data-declarations (if they are needed)
Required ‘main()’ function (as the entry-point)
Can invoke ‘printf()’ (for ‘formatted’ output)
Optionally may define some other functions
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Understanding Linux Kernel Modules: Coding and Implementation and more Slides Computer Applications in PDF only on Docsity!

Typical C layout

  • Basic structure of a C program:
    • Comment-banner (showing title and abstract)
    • Preprocessor directives (e.g., for header-files)
    • Global data-declarations (if they are needed)
    • Required ‘main()’ function (as the entry-point)
    • Can invoke ‘ printf ()’ (for ‘formatted’ output)
    • Optionally may define some other functions

Example program in C

Extensibility with Linux

Two mechanisms for ‘extensibility’:

  • ‘Open Source’ development
  • ‘Loadable’ kernel modules (LKMs)

Loadable Kernel Modules

  • Convenient technique for OS ‘extensibility’
  • Also allows us to study how kernel works
  • Kernel can be modified while it’s running
  • No need to recompile and then reboot
  • But inherently unsafe: any ‘bug’ can cause a system malfunction -- or complete crash!

‘insmod’ and ‘rmmod’

  • We’re allowed to ‘install’ kernel objects:

$ /sbin/insmod myLKM.ko

  • We’re allowed to ‘remove’ kernel objects:

$ /sbin/rmmod myLKM

  • Anyone is allowed to ‘list’ kernel objects:

$ /sbin/lsmod

Creating a new LKM

  • You can use any text-editor (e.g., ‘vi’ or ‘emacs’) to create source-code (in the C language) for a Linux kernel module (i.e., an LKM)
  • But a kernel module differs from a normal C application program (e.g., no ‘ main ()’ function)
  • A kernel module cannot call any of the familiar functions from the standard C runtime libraries
  • For any LKM, two entry-points are mandatory (one for ‘initialization’, and one for ‘cleanup’)

Other LKM differences

  • Module uses ‘ printk ()’ instead of ‘ printf ()’
  • Includes the <linux/module.h> header-file
  • Specifies a legal software license (“GPL”)
  • Compilation requires a special ‘Makefile’
  • Execution is “passive” (it’s a ‘side-effect’)
  • Module has no restriction on ‘privileges’

Required module functions

  • int init_module ( void );

// this gets called during module installation

  • void cleanup_module ( void );

// this gets called during module removal

  • A newer syntax allows memory-efficiency: module_init( my_init ); module_exit( my_exit );

Format of the ‘Makefile’

ifneq ($(KERNELRELEASE),) obj-m := mymod .o else KERNELDIR := /lib/modules/$(shell uname –r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif

Inconveniences

  • That ‘Makefile’ has to be edited every time you create another new module! 
  • Then, when you compile the new module, like this: $ make there are more than a half-dozen files that get created (some of them are ‘hidden’) in your current directory, but just one is the ‘.ko’ (kernel object) that you really wanted

Improvement to ‘mmake’

  • After watching past students use ‘mmake’ we realized that it would be better to allow compiling just one module at a time
  • We kept the former behavior as an option
  • But now we allow the user to specify with a command-line parameter which module (or modules) they wish to re-compile: $ ./mmake mymod

In-class exercise

  • Download ‘mmake.cpp’ from class website and compile it with ‘make’ (or alternatively use: $ g++ mmake.cpp –o mmake )
  • Download the ‘kello.c’ source-file from the website, and compile it using ‘mmake’
  • Add the ‘kello.ko’ kernel-object to Linux using the Linux ‘/sbin/insmod’ command
  • Use ‘dmesg’ to view the kernel’s log-file
  • Remove ‘kello’ (with ‘/sbin/rmmod kello’)

In-class exercise

  • Modify the ‘kello.c’ source-file so that the messages will be visible in a window on the graphical desktop (in addition to being written to the kernel’s log-file)
  • You can switch from graphics-mode to a text-mode console with F
  • You can switch back to graphics mode by typing F

Summary

  • Download mmake.cpp and kello.c
  • Compile mmake.cpp using ‘make’
  • Then compile kello.c using ‘mmake’
  • Install ‘kello.ko’ (and see printk-message)
  • Remove ‘kello’ (to see another message)
  • Modify the ‘printk()’ statements in kello.c
  • Recompile and reinstall to view new info