200 likes | 214 Views
Learn about the advantages and implementation of kernel modules in a precompiled kernel, including module management and driver registration.
E N D
INTRODUCTION • Kernal modules are precompiled, so that a number of useful debugging features can be used. • MODULE_FORCE_UNLOAD - when this option is enabled, one can force the kernel to unload a module even when it believes it is unsafe, via a (rmmod -f module) command. This option can save you a lot of time and a number of reboots during the development of a module. • In case we may want to insert a module into a running kernel which is not allowed to recompile, or on a machine that you prefer not to reboot. In that case it will force to use modules for a precompiled kernel.
KERNEL MODULE • Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. • They extend the functionality of the kernel without the need to reboot the system. • For example, one type of models is the device driver, which allows the kernel to access hardware connected to the system.
KERNEL MODULES • Kernel modules allow a Linux system to be set up with a standard minimal kernel, without any extra device drivers built in Three components to Linux module support. • Module management • Driver registration • Conflict resolution • A kernel module may typically implement a device driver, a file system, or a networking protocol.
MODULE MANAGEMENT • Supports loading modules into memory and letting them talk to the rest of the kernel • Module loading is split into two separate sections: • Managing sections of module code in kernel memory • Handling symbols that modules are allowed to reference • The module requestor manages loading requested, but currently unloaded, modules
DRIVER REGISTRATION • Allows modules to tell the rest of the kernel that a new driver has become available • The kernel maintains dynamic tables of all known drivers, and provides a set of routines. • Registration tables include the following items: • Device drivers • File systems • Network protocols • Binary format
KERNEL MODULE DESCRIPTION • To add a new code to a Linux kernel, it is necessary to add some source files to kernel source tree and recompile the kernel. • We can also add code to the Linux kernel while it is running. • A chunk of code added in such way is called a loadable kernel module
ADVANTAGES OF MODULES • There is no necessity to rebuild the kernel, when a new kernel option is added • Modules help find system problems (if system problem caused a module just don't load it) • Modules save memory • Modules are much faster to maintain and debug • Modules once loaded are in as much fast as kernel
MODULE IMPLEMENTATION • The kernel makes sure that the rest of the kernel can reach the module's global symbols • Module must know the addresses of symbols (variables and functions) in the kernel and in other modules (/proc/syms <2.6 /proc/kallsyms - 2.6) • The kernel keeps track of the use of modules, so that no modules is unloaded while another module or kernel is using it (/proc/modules)
MODULE IMPLEMENTATION • The kernel considers only modules that have been loaded into RAM by the insmod program and for each of them allocates memory area containing: • a module object • null terminated string that represents module's name • the code that implements the functions of the module
COMPILING KERNEL MODULE • A kernel module is not an independent executable, but an object file which will belinked into the kernel in runtime and they should be compiled with • -c flag • _KERNEL_ symbol • MODULE symbol • LINUX symbol • CONFIG_MODVERSIONS symbol
IMPLEMENTATION • #include <linux/module.h> // included for all kernel modules • #include <linux/kernel.h> // included for KERN_INFO • #include <linux/init.h> // included for __init and __exit macros • MODULE_LICENSE("GPL"); • MODULE_AUTHOR("Lakshmanan"); • MODULE_DESCRIPTION("A Simple Hello World module"); • static int __inithello_init(void) • { • printk(KERN_INFO "Hello world!\n"); • return 0; // Non-zero return means that the module couldn't be loaded. • } • static void __exit hello_cleanup(void) • { • printk(KERN_INFO "Cleaning up module.\n"); • } • module_init(hello_init); • module_exit(hello_cleanup);
SAMPLE PROGRAM • echo “Welcome” • while(true) • do • echo “Menu • 1. Date • 2. Calender • 3. Exit” • read input; • case $input in • 1) date;; • 2) cal;; • 3) echo “Program Windsoff…” • exit 0;; • esac • done • save the file as hello.sh.
Adding a new linuxkernal module • To demonstrate, let us goto a simple “Hello World” as a module and inserted in to the kernel
Step 1: • include <linux/module.h>#include <linux/kernel.h>intinit_module(void){ • printk(KERN_INFO “Hello world.n”);return 0; • }void cleanup_module(void){ • printk(KERN_INFO “Goodbye worldn”);}
Step 2 • obj-m += hello.oall:make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean:make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Step 3: • Execute the following command# make • The following output which indicates there is no errormake -C /lib/modules/2.6.27.5-117.fc10.i686/build M=/home/pradeepkumar/lsp modulesmake[1]: Entering directory `/usr/src/kernels/2.6.27.5-117.fc10.i686′
The shell prompt (open the terminal) execute the command one by one# suThe above command waits for the root password, please provide the password# insmodinserting the module# lsmodlisting all the modules running under the kernel,the first module will be listed as hello# dmesgyou can see thelast line says hello world# rmmodremoving the module# dmesgyou can see the last line says goodbye world
CONCLUSION Thus the case has been studied to develop a dynamically loadable Linux kernel module for Linux.