Linux kernel Series (1)--programming of kernel module __linux

Source: Internet
Author: User
Tags dmesg

Linux kernel has a very powerful function is to dynamically load the module, the module in this case is actually a kernel-supported applet. Before we actually get into the Linux kernel world, we can go through an addiction and load a kernel module on our own.

Since the module is the program, we first write a small program, here or take the simplest "Hello World" program to give examples. Let's start with the code, and then we'll explain.

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

module_ LICENSE ("GPL");

static int __init lkp_init (void)
{
	printk ("<1>hello World!... from the kernel!\n");

static int __exit lkp_exit (void)
{
	printk ("<1>goodbye World!... from the kernel!\n");

Module_init (lkp_init);
Module_exit (Lkp_exit);
The first three headers, unlike the header files that were previously seen, are all in the Linux directory, so where is the directory? In Ubuntu's system, it is placed in the/usr/src/linux-headers-x.x.x.-xx/include/linux/directory, interested partners can go to see, Here is a simple introduction: Module.h in the version of the control of the module information, the following macro definition module_license is defined here; Kernel.h is a commonly used kernel function, such as the printk;init.h here is a macro __ Init (note here are two underscores), __exit and Module_init, Module_exit.

The following macro defines Module_license (the "GPL"); it tells the kernel that the module we wrote is compliant with the GNU General Public License and is simply legal. No such system will give you a warning.

Then there are two functions, static and int belong to the basic knowledge, do not say, the following __init and __exit are two macro definitions, tell the system you write the program is more special, you can special treatment. This is mainly for some performance optimization, in fact, not special treatment does not matter. Both functions define a PRINTK function, which comes from the kernel, similar to printf usage, but it outputs the content to a specific file rather than the standard output, which we'll see in a moment.

Finally, there are two macro definitions, which is the most important part of this code. The first macro definition can be seen as the entrance to our little module, which is to tell the system module to execute the Lkp_init function first, and the other to represent the exit of the module. Their interior is actually done by the callback function, which is not detailed here.

After the program is written, we can give it a name to save, and here we call it hello.c.

The program has, we also need a guide to compile the rules, is the makefile file, the same, we still look at the code first.

OBJ_M:=HELLO.O
Current_path: = $ (shell pwd)
Linux_kernel: = $ (Shell uname-r)
linux_kernel_paht: =/usr/src /linux-headers-(Linux_kernel) all

:
	make-c $ (linux_kernel_paht) m=$ (current_path) modules Clean
:
	Make-c $ (linux_kernel_paht) m=$ (current_path) Clean
Makefile Grammar has a lot of tutorials, here is not much to say, the key to say the role of-C and m=, these two points in the compilation of the kernel is very common: when make is the target of all, C $ (kdir) indicates jump to the kernel source directory read there makefile;m=$ (PWD) Indicates that it is then returned to the current directory to continue reading, executing the current makefile. There is also the last surface of the modules and clean as fixed items that cannot be changed.

After writing the makefile save, then execute the Make command, which produces a number of files, one of which is the. ko file that we need to insert into the kernel, before we insert it, let's take a look at the existing modules in the current kernel: Execute command # lsmod can see the kernel modules in the current system. Then execute the command # Insmod Hello.ko can insert our module into, and then execute the # lsmod can see that our module has been inserted (usually the first one is just inserted). At this point we can use the command #dmesg to view the output of the module we just inserted (usually on the last side). Well, that means our module is ready to run. Of course, since it is ready to plug, you can also uninstall it off. Execute command # rmmod Hello you can uninstall it, and then # DMESG look, you can see the program out of the output of the sentence.

Above, we have completed the kernel module writing and loading.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.