Kernel module Writing

Source: Internet
Author: User
Tags dmesg
This document copyleft Rosetta all, the use of the GPL release, you can freely copy, reprint, reprint, please maintain the integrity of the document.
Reference: Linux device driver, third edition
Source: http://blog.csdn.net/rosetta/article/details/7563601

Previously also wanted to pass the Linux kernel programming direction long exhibition, but there is no opportunity to get started, read the "Linux device Driver" (bought more than two years), but like to see the heavenly Book.
Now to the development of network programming, involved or very wide, this is also a chance, the top let me the IPSec algorithm module, although this work is very boring, but still let me find and device-driven related things, let my spirit spirits, thus again picked up the " Linux device driver to answer questions for me.
Recently still looking at the implementation of Openswan Klips, involving to the driver of the network card writing, and slowly to organize into notes, today first of all the basic knowledge of the first post.

Knowledge Points:
1. Compile multiple. C source files ko (single file writing online there are too many tutorials, multiple C source file compilation is this I met this time)
2. Write a kernel module with parameters (This is also noted in passing, although seemingly not used)
3. INSMOD,RMMOD,MKNOD,MODPROBE,DMESG and other commands use (dmesg more useful, some people do not know with this command, said to use the serial port display information, in fact, DMESG command more convenient,-c option to clear information)
4. Why the kernel cannot be inserted. (This is also the key, many times easy to operate, such as compiling the kernel source code, but may encounter a series of errors, solve a bunch of errors can most enhance a person's ability)

First, compile the KO module of multiple. c files and insert the kernel
Test_module.c
#include <linux/init.h>
#include <linux/module.h>

#include "Extern_app.h"

static char *who = "Linux ss";
static int many = 1;
Module_param (many, int, s_irugo);
Module_param (who, Charp, S_irugo);

static int __init hello_init (void)
{
Test ();
static int i = 0;

for (i = 0; i < many; i++)
PRINTK (Kern_alert "Hello,%s!\n", who);

return 0;
}

static void __exit hello_exit (void)
{
PRINTK (Kern_alert "goodbye\n");
}

Module_init (Hello_init);
Module_exit (Hello_exit);
Module_license ("GPL");

Extern_app.c
#include <linux/init.h>
#include <linux/module.h>
int test (void)
{
PRINTK ("Test here.\n");

return 0;
}

Extern_app.h
#ifndef __extern_app_h
#define __extern_app_h

extern int test (void);

#endif

Makefile
#ifneq ($ (kernelrelease),)
Obj-m: = MY_MODULE.O
MY_MODULE-OBJS: = TEST_MODULE.O EXTERN_APP.O
#else
Kerneldir? =/lib/modules/$ (Shell uname-r)/build
PWD: = $ (Shell PWD)

Default
$ (make)-C $ (Kerneldir) m=$ (PWD) modules

Clean
RM-RF *.o *~ core. Depend *.cmd *.ko. *.mod.c
RM-RF module.* modules.*

. Phony:default Clean
#endif

Make, generate My_module.ko if execution succeeds

After executing the insmod and calling DMESG, the normal situation is as follows:
[Root@localhost module_kernel]# insmod My_module.ko who=coding many=5
[Root@localhost module_kernel]# DMESG
Test here.
Hello, coding!.
Hello, coding!.
Hello, coding!.
Hello, coding!.
Hello, coding!.
[Root@localhost module_kernel]#

Rmmod Remove module from kernel
[Root@localhost module_kernel]# Insmod My_module.ko
Insmod:error inserting ' My_module.ko ':-1 File exists
[Root@localhost module_kernel]# Rmmod My_module

Second, the reason for the failure to solve
Errors that are frequently encountered in inserting modules:
[Root@panlimin module_study]# Insmod My_module.ko
Insmod:error inserting ' My_module.ko ':-1 Invalid module format
DMESG View Kernel error message:
[Root@panlimin module_study]# DMESG
My_module:no symbol version for Module_layout
[Root@panlimin module_study]#

This error generally occurs when make has a warning like this:
Warning:symbol version Dump/root/study/kernel/linux-2.6.34/module.symvers
is missing; Modules would have no dependencies and modversions.

This is the problem with the Linux kernel version dependency, which typically occurs when the kernel is not linked to the correct kernel source directory tree, or
The Module.symvers file at the top of the kernel tree is missing. Because the module code must be recompiled for the kernel version to be linked.
If you do not have this warning, but if you report this error when insmod, you need to see if the Module.symvers file is correct, such as the file may be 0 bytes,
Module.symvers is generated when the kernel is compiled, and if the release is available in the corresponding kernel tree in the/lib/module/directory, just make sure that the file is correct
It would be no problem to make,insmod again.

In addition to INSMOD can insert modules into the kernel, modprobe can also, the difference is that the latter will consider whether the module to be inserted refers to some of the current kernel does not exist symbols,
If you have such a reference, Modprobe will find the module and try to insert it into the kernel. So sometimes you can use Modprobe when Insmod inserts are not valid.

If there is a problem with the symbol table due to the module dependencies, for example, the ~/.viminfo is corrupted, and even the network interface cannot be loaded, it can be in the/lib/module/corresponding kernel directory
Executing the depmod-a to regenerate the correct symbol table will take some time to execute.


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.