Introduction to Linux Kernel Modules

Linux operates in two modes. One is Kernel mode (kernel space) and other is User mode (user space). The kernel works in the highest level (also called as supervisor mode) where it has all the authority and the applications work in the lowest level where the direct access to the hardware and the memory are prohibited. Keeping in line with 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 has invoked 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. 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 it’s internal data structures and routines.

The alternative to this is the micro kernel structure where the functional pieces of the kernel is 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 the piece of codes which can be dynamically linked to the kernel (according to the need) even after the system bootup. Password protect a folder.

They can be unlinked from the kernel and removed when they are no longer needed. Mostly the linux kernel modules are used for the device drivers or the pseudo-device drivers such as network drivers or file system. When a linux kernel module is loaded, it becomes the 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:Let us now discuss about the life cycle of the 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 the later invocation.

The module is registered to the kernel and attaches it’s data-structures and functionality in 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 will go into the detailed life cycle of the module later. Simple Module Program: Let us now program a simple module to review its life-cycle. In the following program, the init_module and the cleanup_module functions are demonstrated. The init_module is called when the module is inserted into the kernel and the cleanup_module is called just before removing it from the kernel.

Leave a Reply

You must be logged in to post a comment.