Linux Kernel Analysis (2) ---- kernel module introduction | simple kernel module implementation, linux ----

Source: Internet
Author: User

Linux Kernel Analysis (2) ---- kernel module introduction | simple kernel module implementation, linux ----

Linux Kernel Analysis (2)

Yesterday we started Kernel Analysis. Many people on the internet use source code for direct analysis. The problem is that it is very boring and hard to understand, in a sense, the linux system itself is composed of modules. Therefore, I will analyze the kernel based on the design of the kernel module to understand the linux kernel.

We will analyze the following content today:

1. Introduction to Linux Kernel Modules

2. Simple kernel module implementation

 

LLinuxKernel module Introduction

1.What is the kernel module?

In the previous blog, we first configured the kernel. During the configuration process, we selected the kernel components (of course, this option determines the size of our kernel ), then the final kernel is generated. What should we do if we want to add components?

The most stupid way is to reconfigure the kernel and re-compile it. In this case, I have to re-compile the kernel by adding a component. This is obviously not a very scientific method. In fact, our Linux Kernel provides the scalability feature during runtime, this means that when the system is started and running, we can add or remove some features to the kernel.

The code we add to the kernel during runtime becomes a Dynamically Loaded kernel module, which is short for kernel module.

2.Operations on the kernel module

A)Load the kernel module:Insmod

B)Uninstall the kernel module:Rmmod

C)View the kernel module:Lsmod

3.Module Declaration

A)MODULE_LICENSE("GPL"):The licenses recognized by the kernel include GPL (GNU General Public License of any version) and GPL v2.

B)MODULE_AUTHOR("Author "):Declare author information.

C)MODULE_VERSION("Version "):Declare version information or not.

D)MODULE_DESCRIPTION("Function description "):Declare the module function.

4.Module Parameters

When loading the kernel module, we can pass parameters to the same code to achieve different results. Of course, our parameters must be declared using the module_param macro as follows:

1 module_param (name, type, perm)

A)Name:Variable name

B)Type:Data-type kernel support module parameter types: bool, invbool (bool sending and conversion, true to false, false to true), charp (char type pointer value), int, long, short, uint, ulong, ushort,

C)Perm:The common access permission values are S_IRUGO and S_IWUSR. Usually, they are bitwise OR

At the same time, we can also use the following macro Acoustic Array:

  1. 1 Module_param_array (name, type, num, perm)

5.Module symbol Export

When a module needs to use a function (variable) of another module, use EXPORT_SYMBOL (symbol name) or EXPORT_SYMBOL_GPL (symbol name) to declare it.

Note: EXPORT_SYMBOL_GPL () is only applicable to modules that follow the GPL protocol.

LSimple kernel module implementation

I think everyone remembers that when we were learning a language, most of the first program we wrote was output hello world, so I will use the kernel module we just introduced to complete hello world.

1.Write the kernel module

It is not difficult to complete the first kernel module through the introduction of the content above. The following is your own code.

  1. 1 # include <linux/init. h> 2 # include <linux/module. h> 3 MODULE_LICENSE ("GPL"); 4 staticint hello_init (void) 5 {6 printk ("<0> hello world \ n"); 7 return0; 8} 9 staticvoid hello_exit (void) 10 {11 printk ("<0> goodbye \ n"); 12} 13 module_init (hello_init ); // Add a special segment to the module's target code to indicate the position of the kernel initialization function 14 module_exit (hello_exit); // keep up with the macro.

2.MakefileWrite

Makefile compilation is also relatively simple. Note that the Code has already been described.

 

1 obj-m: = hello. o2 DIRS: =/smbshare/linux-2.6.39 // here the path is the kernel source code path, the kernel source code must be compiled, otherwise it will report 3 all: 4 make-C $ (DIRS) M = $ (PWD) modules5 clean: 6 rm-Rf *. o *. ko *. mod. c *. order *. symvers

 

After the above two steps, we compile and load and then uninstall our modules for testing.

Compile:

Load:

View:

Uninstall:

3.PrintkFunction Introduction

The printk function is a kernel print function, which is similar to the printf function. However, there are a total of eight levels of printing permissions than printf. The log level of printk is defined as follows (in include/linux/kernel. h ):

 

1 # define KERN_EMERG 0 // emergency message, which indicates that the system is unavailable before the system crashes. 2 # define KERN_ALERT 1 // report the message, indicates that actions must be taken immediately 3 # define KERN_CRIT 2 // critical conditions, usually involving serious hardware or software operation failures 4 # define KERN_ERR 3 // error conditions, drivers often use KERN_ERR to report hardware errors. 5 # define KERN_WARNING 4 // warning conditions, warn 6 # define KERN_NOTICE 5 // normal but important conditions for reminding 7 # define KERN_INFO 6 // prompt information, such as when the driver is started, print hardware information 8 # define KERN_DEBUG 7 // debug-level message

 

Today's content is relatively simple, but I have a preliminary understanding of the kernel module, so I recommend a better tool for you today, it is called exvim. It integrates a lot of vim tools. I personally feel very convenient, that is, I don't have to worry about losing my configurations when I change my computer or something. Paste a picture of your use, you are interested can go to http://exvim.github.io/understanding.

 

Related Article

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.