layout | title |
---|---|
default |
Home |
This document provides an overview of kernel programming and various concepts related to device drivers, kernel modules, etc.
- What is a device driver
- What is a Kernel Module
- Device Driver vs Kernel Modules
- Advantages of Kernel Modules
- Disadvantages of Kernel Modules
- Types of Modules
- Basic Commands
- insmod vs modprobe
A device driver is a piece of software that controls a particular type of device which is connected to the computer system. Device driver has three sides:
- one talks with Kernel Space
- one talks with hardware
- one talks wirh User Space
Diagram:
==================
= =
= User = ---------
= = |
================== |
| |
| |
| |
================== |
= = |
= Kernel = |
= = | Via Device File
================== |
| |
|Device Driver
| |
================== |
= = |
= Hardware = ---------
= =
==================
Kernel Modules are piece of code that can be loaded/inserted and unloaded/removed from the kernel as per the demand/need. Traditional way of adding code to the kernel was to recompile the kernel and reboot the system.
- Loadable Kernel Modules (LKM)
- Modules
Extension: .ko (Kernel Object)
Modules are installed in the below directory of the rootfs by default:
/lib/modules/<kernel version>
A kernel module may not be a device driver at all. A driver is like a sub-class of module.
Modules are used for the below:
- Device Drivers.
- File System.
- System Calls.
- Network drivers: Drivers implementing a network protocol (TCP/IP).
- TTY line disciplines: For terminal devices.
-
All parts of the base kernel stay loaded all the time. Modules can save you memory, because you have to have them loaded only when you're actually using them
-
Users would need to rebuild and reboot the kernel every time they would require a new functionality.
-
A bug in driver which is compiled as a part of kernel will stop system from loading, whereas module allows systems to load.
-
Faster to maintain and debug
-
Makes it easier to maintain multiple machines on a single kernel base.
-
Size: Module management consumes unpageable kernel memory.
- A basic kernel with a number of modules loaded will consume more memory than an equivalent kernel with the drivers compiled into the kernel image itself.
- This can be a very significant issue on machines with limited physical memory.
-
As the kernel modules are loaded very late in the boot process, hence core functionality has to go in the base kernel (E.g. Memory Management)
-
Security: If you build your kernel statically and disable Linux's dynamic module loading feature, you prevent run-time modification of the kernel code. In order to support modules, the kernel must have been built with the following option enabled:
CONFIG_MODULES=y
-
In-Source Tree: Modules present in the Linux Kernel Source Code
-
Out-of-Tree: Modules not present in the Linux Kernel Source Code.
All modules start out as "out-of-tree" developments, that can be compiled using the context of a source-tree.
Once a module gets accepted to be included, it becomes an in-tree module.
- lsmod – List Modules that Loaded Already
- insmod – Insert Module into Kernel
- rmmod – Remove Module from Kernel
- modinfo – Display Module Info
- modprobe – Add or Remove modules from the kernel
insmod: Loads the module given 'insmod /path/to/module.ko' modprobe: Loads the module only in /lib/modules/$(uname -r) 'modprobe /home/test/hello.ko' will not work
insmod: Dependencies if present are not loaded modprobe: modprobe calculates dependencies, loads the dependencies and then the main module
Modprobe depends on depmod tool to calculate dependencies.
depmod calculates dependencies of all the modules present in /lib/modules/$(uname -r)
folder, and places the dependency information in /lib/modules/$(uname -r)/modules.dep
file
Exapmle:
kernel/drivers/net/wireless/admtek/adm8211.ko: kernel/net/mac80211/mac80211.ko kernel/net/wireless/cfg80211.ko kernel/drivers/misc/eeprom/eeprom_93cx6.ko
When you say modprobe adm8211.ko, eeprom_93cx6.ko, cfg80211.ko is loaded first and then adm8211.ko
Modules are loaded right to left and removed left to right
So while removing adm8211.ko is removed, then cfg80211.ko and finally eeprom_93cx6.ko
We can re-load the modules.dep file by running "depmod -a" command