This README provides a concise introduction to the basics of kernel programming, covering the fundamentals of device drivers, kernel modules, and more.
A device driver, often simply referred to as a 'driver,' is a software component that facilitates interaction between the computer's OS and a hardware device.
+----------+ +----------+ +----------+
| User | | Kernel | | Hardware |
+----------+ +----------+ +----------+
^ ^ ^
| | |
User Interface Kernel Interface Hardware Interface
A device driver essentially serves as the mediator with:
- One side communicating with the kernel
- One side interfacing with the hardware
- One side interacting with the user
Traditionally, to add functionalities to the kernel, it had to be recompiled and the system rebooted. Kernel Modules change this by allowing code to be dynamically loaded and unloaded from the kernel upon demand.
- Loadable Kernel Modules (LKM): Another name for Kernel Modules.
- Extension:
.ko
which stands for Kernel Object.
By default, modules are stored in /lib/modules/<kernel version>
on the root file system.
While every device driver is a kernel module, not all kernel modules are device drivers. Kernel modules serve various purposes, including:
- Device Drivers
- File Systems
- System Calls
- Network drivers (e.g., those implementing TCP/IP)
- TTY line disciplines (for terminal devices)
- Memory efficient as they can be loaded/unloaded on demand.
- No need to reboot after every modification.
- Bugs in modules don't necessarily halt the system.
- Easier debugging and maintenance.
- Simplified management for multiple machines.
- Consumes more memory due to module management.
- Module management consumes unpageable kernel memory.
- A basic kernel with a number of modules loaded will consume more memory than a equivalent kernel module with the drivers compiled into the kernel image itself.
- Modules load late in the boot process , so essential features must be in the base kernel.
- so all core functionality should be in the base kernel.
- Static kernels prevent run-time modifications, enhancing security.
To support modules, the kernel should have the CONFIG_MODULES=y
option enabled.
- In-Source Tree: Present within the Linux Kernel Source Code.
- Out-of-Tree: Absent from the Linux Kernel Source Code, though they can eventually become in-tree modules.
- List Modules:
lsmod
(Info derived from/sys/modules
).
-
gives infor on size of module.
-
used by field explains the dependency
-
Module Information:
modinfo
provides detailed information about a module.
Note -
- parm : acceptable parameter for the module
- intree : It's an intree module
With this foundation, you're ready to delve deeper into the fascinating world of kernel programming.