1 / 40

By: Monis Javed Nalin Sharma Obedullah

By: Monis Javed Nalin Sharma Obedullah. Kernel Module Programming. a program having control over everything that occurs in system. central part of operating system. it acts as an interface between the user applications and the hardware.

cora
Download Presentation

By: Monis Javed Nalin Sharma Obedullah

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. By:MonisJavedNalin SharmaObedullah Kernel Module Programming

  2. a program having control over everything that occurs in system. central part of operating system. it acts as an interface between the user applications and the hardware. What is Kernel?

  3. Kernel is responsible for: • Process Management - allocating resources to a process. - synchronization among processes. • Memory Management - allocating and deallocating memory to programs. • Device Management - controlling several devices attached to the system. • Storage Management - disk management. - disk scheduling. Role of the Kernel

  4. Monolithic Kernel • Micro Kernel • Hybrid Kernel • Nano Kernel • Exo Kernel Types of Kernel

  5. 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. Monolithic Kernel

  6. 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 functionality. Example include: QNX ,Minix. Micro Kernel

  7.  have the ability to pick and choose what they want to run in user mode and what they want to run in supervisor mode. Example : Windows NT,2000, XP, Vista,7,8, Mac OS X. Hybrid Kernel

  8. Introducing Kernel Modules

  9. 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 demand. They extend the functionality of the kernel without the need to reboot the system. Kernel Modules

  10. 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…

  11. 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. Kernel Modules Contd…

  12. Module Programming

  13. • 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 kernel modules • modprobe:High level handling of loadable modules Module Commands

  14. modules already loaded into the kernel can be listed by running lsmod, which gets its information byreading the file /proc/modules. When the kernel needs a feature that is not resident in the kernel, the kernel module daemon kmodexecutes modprobe to load the module in. modprobe is passed a string in one of two forms: · A module name like soft or ppp. · A more generic identifier like char−major−10−30. How Do Modules Get Into The Kernel?

  15. If modprobe is handed a generic identifier, it first looks for that string in the file /etc/modules.conf. If it finds an alias line like: alias char−major−10−30 soft it knows that the generic identifier refers to the module soft.o 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 loaded. This file is created by depmod −a and contains module dependencies. Contd…...

  16. Lastly, modprobe usesinsmodto first load any prerequisite modules into the kernel, and then the requested module. modprobe directs insmod to /lib/modules/version/, the standard directory for modules. insmod is intended to be fairly dumb about the location of modules, whereas modprobe is aware of the default location of modules. Contd….

  17. Example: 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 insmod /lib/modules/2.5.1/kernel/fs/fat/fat.o insmod /lib/modules/2.5.1/kernel/fs/msdos/msdos.o 2. using modprobe modprobe –a msdos Contd…

  18. Before We Begin to get into the code

  19. 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 the kernel. Modversioning

  20. Steps • #cd /usr/src/linux-headers-3.8.0-34-generic/ • #make menuconfig

  21. /* hello−1.c − The simplest kernel module.*/ #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_ALERT */ intinit_module(void) { printk("Hello world \n"); return 0; //A non 0 return means init_module failed; module can't be loaded. } void cleanup_module(void) { printk(KERN_ALERT "Goodbye world 1.\n"); } Hello World Program

  22. Kernel modules must have at least two functions: 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 rmmoded.

  23. 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 function). The cleanup_module() function is supposed to undo whatever init_module() did, so the module can be unloaded safely.

  24. 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 for them. The header file linux/kernel.h describes what each priority means. Introducing printk()

  25. 1. KERN_EMERG "<0>" /* emergency message*/ 2. KERN_ALERT "<1>" /* action must be taken immediately*/ 3 KERN_CRIT "<2>" /* critical conditions*/ 4. KERN_ERR "<3>" /* error conditions*/ 5. KERN_WARNING "<4>" /* warning conditions*/ 6. KERN_NOTICE "<5>" /* normal but significant condition*/ 7. KERN_INFO "<6>" /* informational message*/ 8. KERN_DEBUG "<7>" /* debug-level messages*/ Default : DEFAULT_MESSAGE_LOGLEVEL printk() Contd…

  26. If the priority is less than intconsole_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. printk() Contd…

  27. 1. write the kernel module code with filename like filename.c 2. make a file containing code for compiling the module and save it using name “Makefile”. 3. now compile the module code by issuing command make. To see what kind of information it is , type:- modinfofilename.ko Steps for Compilation of Kernel Module

  28. 4. now insert module by command modprobefilename.ko OR insmodfilename.ko - you can see your inserted module in cat /proc/modules OR by command lsmod Contd…

  29. A makefile is a script for appropriate compilation of different type of sources to the appropriate object code. 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. Make File

  30. 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 the makefile. Contd….

  31. obj-m += filename.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean How to Make a “Makefile” for Kernel Module:

  32. you can rename the init and cleanup functions of your modules. This is done with the --- module_init() and module_exit() These are macros defined in linux/init.h Rename your Function Name:-

  33. #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int _init myname(void) { printk(KERN_INFO “Hello world?”); return 0; } static void _exit yourname(void) { printk(KERN_INFO “Goodbye”); } module_init(myname); module_exit(yourname); Example:-

  34. Modules Spanning Multiple Files:- First we invent an object name for our combined module, Second we tell make what object files are part of that module. Modules Spanning Multiple Files:-

  35. begin.c #include <linux/kernel.h> #include <linux/module.h> intinit_module(void) { printk(KERN_INFO "Hello, world\n"); return 0; } end.c #include <linux/kernel.h> #include <linux/module.h> void cleanup_module() { printk(KERN_INFO "Goodbye\n"); } Makefile :- obj-m += beginend.o beginend-objs := begin.oend.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

  36. Modules can be unloaded using rmmod command. rmmod ensures the restriction that the modules are not in use. Unlinked from the kernel and unlisted from the list of kernel modules Dependency is released Module Unloading

  37. 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. Kernel Modules vs Application Program

  38. The Linux Kernel Module Programming Guide by Peter Jay Salzman Michael Burian OriPomerantz References

  39. Thank You

More Related