Pick a Free OS

Introduction to Linux kernel modules

Linux operates in two modes--the Kernel mode (kernel space) and the User mode (user space). The kernel works in the highest level (also called supervisor mode) where it has all the authority, while the applications work in the lowest level where direct access to hardware and memory are prohibited. Keeping in line with the traditional Unix philosophy, Linux transfers the execution from user space to the kernel space through system calls and the hardware interrupts. The Kernel code executing the system call works in the context of the process, which invokes the system call. As it operates on behalf of the calling process, it can access the data in the processes address space. The kernel code that handles interrupts, works to the processes and related to any particular process.

Linux Kernel Modules

The Linux kernel is a monolithic kernel i.e. it is one single large program where all the functional components of the kernel have access to all of its internal data structures and routines. The alternative to this is the micro kernel structure where the functional pieces of the kernel are broken out into units with strict communication mechanism between them. This makes adding new components into the kernel, via the configuration process, rather time consuming. The best and the robust alternative is the ability to dynamically load and unload the components of the operating system using Linux Kernel Modules.

The Linux kernel modules are piece of codes, which can be dynamically linked to the kernel (according to the need), even after the system bootup. They can be unlinked from the kernel and removed when they are no longer needed. Mostly the Linux kernel modules are used for device drivers or pseudo-device drivers such as network drivers or file system. When a Linux kernel module is loaded, it becomes a part of the Linux kernel as the normal kernel code and functionality and it posses the same rights and responsibilities as the kernel code.

Life cycle of Linux kernel module

The life cycle of a module starts with the init_module(). The task of init_module is to prepare the module for later invocation. The module is registered to the kernel and attaches its data-structures and functionality to the kernel. The kernel-defined external functions are also resolved. The life cycle of the module ends with cleanup_module(). It `unregisters' the module functionality from the kernel. We shall later take a more detailed look at the life cycle of the module.

Simple Module Program



Technically, no, this introductory course won't cover the intricacies of device drivers but let me expand on that.

This course is meant to be an introduction to the basics of kernel programming; that is, it's going to cover the foundations of what every beginning kernel programmer needs to know in terms of kernel structure, configuring and building a new kernel, how to write loadable kernel modules, transferring data between user space and kernel space, simple debugging and so on. Clearly, none of that represents writing an actual device driver, but it's all essential information you'll need when you want to start writing specific types of drivers.

There are already plans for followup courses such as "How to write a PCI driver" and "How to write a USB driver", courses that will (probably) consist of perhaps a dozen lessons each and will cost about the same amount as this introductory course (or maybe slightly less, depending on length).

So if you feel that you already know kernel programming at a basic level, you can just wait for the driver course you're interested in. On the other hand, if you're truly new to kernel programming, you will probably need this intro course as a lead-in to the driver course.