Preliminary Exploration of linux kernel programming, parameter transfer and inter-module function calls, Preliminary Exploration of linux

Source: Internet
Author: User
Tags dmesg

Preliminary Exploration of linux kernel programming, parameter transfer and inter-module function calls, Preliminary Exploration of linux

I. Preface

Let's take three small examples to experience Linux kernel programming. As follows:

1. hello world for Kernel Programming

2. module parameter transfer

3. Inter-module function call

2. Preparations

First, install the linux header file on your linux system. The debian series:

1 $:sudo apt-get install linux-headers-`uname -r`

After installation, the directory corresponding to the header file version you just installed is in your/lib/modules/directory. There is also a build folder under the header folder, where the Makefile file will be used to compile the kernel module ., Here is my machine:

Note: The installed header file version must be the same as your system version. Otherwise, you cannot insert your own modules into the kernel of your local computer. If you do not have the corresponding header file when installing the apt-get header file, or if your source file contains an unstable version, there is still no corresponding header file, you can search for the desired deb package here for installation. Or download the kernel source code corresponding to the local machine to build the environment.

3. hello world for Kernel Programming

Let's first take a look at the entry and exit of kernel module files. It consists of two macros to register the entry and exit, respectively:

1 module_init(x);2 module_exit(x);

These two macros are included/linux/module. h In the header file directory. The x in the macro indicates the entry and exit functions registered to the kernel. In general, it is the function called during module initialization and module uninstallation.

Form of initialization function: int my_init (void );

Exit function form: void my_exit (void );

There are also some macros:

MODULE_LICENSE (_ license): the module license.

MODULE_AUTHOR (_ author): the author of the module.

MODULE_VERSION (_ version): module version

MODULE_DESCRIPTION (_ description): The description of the module.

There are some examples.

Now, let's take a look at the simplest hello world example:

File Name: kernel_hello.c

1 # include <linux/init. h> 2 # include <linux/module. h> 3 # include <linux/kernel. h> 4 5/* the following four macros are license, author, module description, module version */6 MODULE_LICENSE ("Dual BSD/GPL "); 7 MODULE_AUTHOR ("yuuyuu"); 8 MODULE_DESCRIPTION ("kernel module hello"); 9 MODULE_VERSION ("1.0 "); 10 11/* entry function */12 static int hello_init (void) 13 {14 printk (KERN_ALERT "hello_init () start \ n"); 15 16 return 0; 17} 18 19/* exit function */20 static void hello_exit (void) 21 {22 printk (KERN_ALERT "hello_exit () start \ n "); 23} 24 25/* Register to the kernel */26 module_init (hello_init); 27 module_exit (hello_exit );

The preceding printk () function is the output function implemented by the kernel itself, and the level of output information when KERN_ALERT is used!

Then write a simple Makefile file:

1 KERNAL_DIR ?= /lib/modules/$(shell uname -r)/build2 PWD := $(shell pwd)3 4 obj-m := kernel_hello.o5 6 default:7     $(MAKE) -C $(KERNAL_DIR) M=$(PWD) modules

KERNAL_DIR: directory of the header file you just installed

PWD: indicates the directory of the module file you are writing.

Obj-m: The module dependency target file. In addition, the kernel Makefile: obj-m indicates that the target file is compiled into a module, obj-y indicates that the file is compiled to the kernel, and ojb-n indicates that the file is not compiled.

-C: When you execute make, switch the working directory to the parameter directory specified after-C, which is the build directory under the header file directory.

M: This is a variable in the kernel Makefile. Return to the current directory to continue reading Makefile. Here, after reading the Makefile under the build directory, return to our directory and read the Makefile we just compiled.

The final modules indicates the compilation module.

Now let's compile it. Execute make in the directory where Makefile is compiled. The module File kernel_hello.ko is generated.

 

View the module information: sudo modinfo kernel_hello.ko

We can see the module information inserted by those macros.

Now we can execute four actions in one breath: insert the module, view the module that has been inserted in the kernel, uninstall the module, and view the dmesg information:

As you can see, the module prints information in the function during initialization and exit.

 

Iv. module parameter transfer

The module parameter transfer is also a macro, in the header file directory include/linux/moduleparam. h:

1 module_param(name, type, perm)

Name: The variable name in the module, which can be specified by the user.

Type: byte, short, ushot, int, uint, long, ulong, charp, bool

Perm: Permission control of the module. Same as linux File Permission control.

File Name: kernel_hello_param.c

1 # include <linux/init. h> 2 # include <linux/module. h> 3 # include <linux/kernel. h> 4 5/* the following four macros are license, author, module description, module version */6 MODULE_LICENSE ("Dual BSD/GPL "); 7 MODULE_AUTHOR ("yuuyuu"); 8 MODULE_DESCRIPTION ("kernel module hello"); 9 MODULE_VERSION ("1.0"); 10 11 static char * msg; 12 module_param (msg, charp, 0644); 13 14/* entry function */15 static int hello_init (void) 16 {17 printk (KERN_ALERT "hello_init () start \ n "); 18 printk (KERN_ALERT "% s \ n", msg); 19 20 return 0; 21} 22 23/* exit function */24 static void hello_exit (void) 25 {26 printk (KERN_ALERT "hello_exit () start \ n"); 27} 28 29/* Register to kernel */30 module_init (hello_init); 31 module_exit (hello_exit );

Lines 11, 12, and 18 are added compared with the previous file. Note that the charp of the second row is the character pointer of the kernel.

After compilation, input parameters are inserted and dmesg displays the following information:

The inserted parameter msg follows the module.

 

5. Inter-module function call

The function of the module is exported to the symbol table for use by other functions. macros are required:

1 EXPORT_SYMBOL(sym)

This macro is included in include/linux/export. h.

Since function calls between modules, We need to write two modules.

File 1: kernel_fun.h

1 #ifndef KERNEL_FUN_H2 #define KERNEL_FUN_H3 4 void fun(void);5 6 #endif

File 2: The module file to be exported: kernel_fun.c

1 # include <linux/init. h> 2 # include <linux/module. h> 3 # include <linux/kernel. h> 4 # include <linux/export. h> 5 6 # include "kernel_fun.h" 7 8/* the following four macros are license, author, module description, module version */9 MODULE_LICENSE ("Dual BSD/GPL"); 10 MODULE_AUTHOR ("yuuyuu"); 11 MODULE_DESCRIPTION ("kernel module hello "); 12 MODULE_VERSION ("1.0"); 13 14/* entry function */15 static int fun_init (void) 16 {17 printk (KERN_ALERT "fun_init () start \ n "); 18 19 return 0; 20} 21 22 void fun () 23 {24 printk (KERN_ALERT "fun () is called \ n "); 25} 26 27/* exit function */28 static void fun_exit (void) 29 {30 printk (KERN_ALERT "fun_exit () start \ n "); 31} 32 33/* Register to kernel */34 module_init (fun_init); 35 module_exit (fun_exit); 36 37/* export symbol table */38 EXPORT_SYMBOL (fun );

The last row is exported to the symbol table.

 

File 3: Call the function of module File 2: kernel_mod.c

1 # include <linux/init. h> 2 # include <linux/module. h> 3 # include <linux/kernel. h> 4 # include <linux/export. h> 5 6 # include "kernel_fun.h" 7 8/* the following four macros are license, author, module description, module version */9 MODULE_LICENSE ("Dual BSD/GPL"); 10 MODULE_AUTHOR ("yuuyuu"); 11 MODULE_DESCRIPTION ("kernel module hello "); 12 MODULE_VERSION ("1.0"); 13 14/* entry function */15 static int mod_init (void) 16 {17 printk (KERN_ALERT "mod_init () start \ n "); 18 19/* call fun */20 fun (); 21 return 0; 22} 23 24/* exit function */25 static void mod_exit (void) 26 {27 printk (KERN_ALERT "mod_exit () start \ n"); 28} 29 30/* Register to kernel */31 module_init (mod_init); 32 module_exit (mod_exit );

Row 3 calls the functions of other modules.

 

Here we need to compile two modules and the corresponding Makefile file:

1 KERNAL_DIR ?= /lib/modules/$(shell uname -r)/build2 PWD := $(shell pwd)3 4 obj-m := kernel_mod.o kernel_fun.o5 6 default:7     $(MAKE) -C $(KERNAL_DIR) M=$(PWD) modules

 

After compiling these two modules, We will verify them now. Note that because kernel_mod depends on kernel_fun, I need to insert the kernel_fun module first.

When uninstalling a module, we need to uninstall kernel_mod for the same reason.

Insert kernel_fun in sequence, view its symbol table, insert kernel_mod, and view dmesg:

We can see that fun () of kernel_fun is called by kernle_mod.

 

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.