WHAT IS KERNEL?
a program having control over everything that occurs in
central part of operating system.
it acts as an interface between the user applications
and the hardware.
ROLE OF THE KERNEL
Kernel is responsible for:
- allocating resources to a process.
- synchronization among processes.
- allocating and deallocating memory to programs.
- controlling several devices attached to the system.
- disk management.
- disk scheduling.
Entire operating system works in kernel space.
Larger in size as they retain full privilege access
over various components like file system, IPC,
IO/device management etc.
Slower to load because of large size.
Recompilation is required to add more features
or remove bugs.
Example includes: Linux based OS, MSDOS etc.
Deals with only critical activities such as controlling
memory and CPU.
Everything else is handled under user mode.
Kernel don’t have to worry about lower level
Example include: QNX ,Minix.
have the ability to pick and choose what they want
to run in user mode and what they want to run in
Example : Windows NT,2000, XP, Vista,7,8, Mac
Linux Kernel has ability to extend its set of
features at run time.
Modules are pieces of code that can be
loaded and unloaded into the kernel upon
They extend the functionality of the kernel
without the need to reboot the system.
KERNEL MODULES CONTD…
A module run in kernel mode.
Without loadable kernel modules, an operating
system would have to include all possible
anticipated functionality already compiled directly
into the base kernel.
This require rebooting kernel every time when new
functionality is added.
KERNEL MODULES CONTD…
Kernel modules allow a Linux system to be set up
with a standard minimal kernel without any extra
device drivers built in.
For example, one type of module is the device
driver, which allows the kernel to access hardware
connected to the system.
• modinfo : display information about a kernel module
• lsmod : list loaded modules
• insmod : Install loadable kernel module
• rmmod : Unload loadable modules
• depmod : handle dependency descriptions for loadable
• modprobe : High level handling of loadable modules
HOW DO MODULES GET INTO THE KERNEL?
modules already loaded into the kernel can be
listed by running lsmod, which gets its
information by reading the file /proc/modules.
When the kernel needs a feature that is not
resident in the kernel, the kernel module
daemon kmod executes modprobe to load the
modprobe is passed a string in one of two forms:
· A module name like soft or ppp.
· A more generic identifier like
If modprobe is handed a generic identifier, it first looks for
that string in the file /etc/modules.conf. If it finds an alias
alias char−major−10−30 soft
it knows that the generic identifier refers to the module
Next, modprobe looks through the file
/lib/modules/version/modules.dep, to see if other modules
must be loaded before the requested module may be
This file is created by depmod −a and contains module
Lastly, modprobe uses insmod to first load any prerequisite
modules into the kernel, and then the requested module. modprobe
directs insmod to /lib/modules/version/, the standard directory for
insmod is intended to be fairly dumb about the location of modules,
whereas modprobe is aware of the default location of modules.
We need to insert module msdos.o which requires fat.o
module to be already loaded. This can be done in two ways:
1. using insmod
2. using modprobe
Everyone's system is different and everyone has
their own groove.
A module compiled for one kernel won't load if
you boot a different kernel.
unless you enable CONFIG_MODVERSIONS in
HELLO WORLD PROGRAM
/* hello−1.c − The simplest kernel module.*/
#include <linux/module.h> /* Needed by all modules */
/* Needed for KERN_ALERT */
printk("Hello world n");
//A non 0 return means init_module failed; module can't be
printk(KERN_ALERT "Goodbye world 1.n");
Kernel modules must have at least two
a "start" (initialization) function called
init_module() which is called when the module is
insmoded into the kernel, and
an "end" (cleanup) function called
cleanup_module() which is called just before it is
init_module() either registers a handler for
something with the kernel, or it replaces one of
the kernel functions with its own code (usually
code to do something and then call the original
The cleanup_module() function is supposed to
undo whatever init_module() did, so the module
can be unloaded safely.
printk() is not meant to communicate information
to the user.
It is used to log information or give warnings.
each printk() statement comes with a priority.
There are 8 priorities and the kernel has macros
The header file linux/kernel.h describes what
each priority means.
If the priority is less than int console_loglevel, the
message is printed on your current terminal.
We use a high priority, like KERN_ALERT, to make
sure the printk() messages get printed to your
console rather than just logged to your logfile.
STEPS FOR COMPILATION OF
1. write the kernel module code with filename
2. make a file containing code for compiling the
module and save it using name “Makefile”.
3. now compile the module code by issuing
To see what kind of information it is , type:modinfo filename.ko
4. now insert module by command
- you can see your inserted module in
OR by command
A makefile is a script for appropriate compilation of
different type of sources to the appropriate object
Makefiles are used to define the procedure to
compile and link your program.
as number of files increases it's very difficult to
compile and link them one by one.
Also you will have to remember the dependencies
between these files.
Makefiles are used to automate these tasks so just
define your rules once and instead of compiling
and linking individual files you just need to execute
HOW TO MAKE A “MAKEFILE” FOR KERNEL
obj-m += filename.o
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
RENAME YOUR FUNCTION NAME:
you can rename the init and cleanup functions of
This is done with the --module_init() and
These are macros defined in linux/init.h
Modules can be unloaded using rmmod command.
rmmod ensures the restriction that the modules are not
Unlinked from the kernel and unlisted from the
list of kernel modules
Dependency is released
KERNEL MODULES VS APPLICATION
Applications perform a single task from
beginning to end.
Kernel module just registers itself in order to
serve future requests.
Application can use library function (like printf)
but a module can only use functions exported by
kernel (like printk).
A module runs in kernel space, whereas
applications run in user space.
The Linux Kernel Module Programming Guide
Peter Jay Salzman