In this post i shall exclusively talk about the insmod untility.
As mentioned before the insmod utility is one of the ways to add modules to the kernel, the other two being using modprobe and kernel using kerneld daemon.
The insmod (insert module) is present as a user space utility.It calls the init_module user space system call, which calls the kernel system call sys_init_module.The insmod utility only takes care of loading the module image to the temporary memory and making a kernel system call which further will ake care of resoving sysmbols,alocating memory, making a kernel entry ,notifying etc.
The kernel function sys_init_module when called along with parameters viz module name and user arguments performs the following operations
sys_init( )
1.check for the permissions (only the su is priviledged to add a kernel module)
2.call the load module ;
mod = load_module(mod_name,args)
3.Add the module or register the module in module_list linked list.
4.Notify threads waiting for the state change of this module
5.call the module’s module_init() function and here is where the functionality of the LKM lies.
6.change the module state to LIVE , which is visible when a cat /proc/modules is done
7.return to the caller.
Now let is have a peek into the load_module() function which is defined in /kernel/linux/module.c the load module performs the following functions.
load_module( )
1.The load module first allocates a temporary kernel memory to the .ko file present in the user spce
2.It then performs a sanity check such as bad object file, incorrect header content etc
3.Next,parsing of the ELF header to map it to certain LKM variables.The ELF is the format of any Linux Kernel module aka Extending Linkable Format.
4.Read the user arguments sent through the function call.
5.Allocate Permanent Memory to the LKM
6.Move the contents from the temporary memory to allocated permanent memory.
7.Resove the symbolic references by referiing to the kernel symbol table and perform relocations
8.Clean up the temporary memory allocated
9.Return reference address for the newly loaded module.
Thus we just went through the steps followed by a insmod to add a module dynamically.
In my next post i shall talk about modprobe utility.