# Linux Kernel Module

16th dec,2019


To solve the problem of lack of modularity and extensibility, the modern monolithic kernel has kernel module.
LKM (Loadable Kernel Module) is an object file that contains code, that can extend the kernel functionality at the run time. Most of the Device Driver used as the LKM and it can be unloaded at the time when not needed.

Example of Loadable Kernel Module



Compiling kernel modules

Compiling LKM is different from any user program. The module must compile with the same option as the kernel in which we load the module also we are using different headers so there is a standard method [KBUILD]. In this method, two files are required Makefile, Kbuild file, also for a LKM you need kernel source or atleast kernel headers matching your kernel version as dependency installed in your system.

Example of Makefile

example of kbuild file
It will generate a kernel module file with the extension .ko

Loading/unloading a kernel module

>>> insmode ./example_lkm.ko
// insmode utility is used to load a module to the kernel with root permission

>>> rmmod ./example_lkm.ko
// rmmod is used to remove the module .

both recieves path to the *.ko file as a parameter.

** Information about modules loaded into the kernel can be found using the lsmod command or by inspecting the /proc/modules, /sys/module directories. **

When loading a module , module_init macro will be executed, similarly when the module is unloaded module_exit macro will execute .
A complete example of compiling and loading/unloading a kernel module





SHIVAM CHAUDHARY

cvam0000

#Linux #Embedded divices#RTOS #Embedded Linux #Embedded c#RTOS ANYWHERE #Bash #FreeRTOS


# Popular Posts

1. RTOS VS GPOS