Complete Linux kernel programming 1

Source: Internet
Author: User
Linux Kernel Programming complete set 1-Linux general technology-Linux programming and kernel information. The following is a detailed description. Linux Kernel Programming)
Author: Ori Pomerantz
Translation: Xu hui

Translator's preface
This is my first attempt. I have never touched Linux before, so the translation is very rough. Sometimes I don't understand it myself, so I have to follow it. In addition, there must be many mistakes or mistakes. I have always been so rough, and I always lose face to my organization .? If you find any errors or unclear explanations, please send your statement to my mailbox.
This is intended to meet Linux heroes in the world. I am Xu hui (No.: shuiguang yueying, zhenwei tianzi). Now I am studying at Peking University founder Research Institute. My main research interests are information security, data encryption, and Linux security. Because our work is still under development, we hope to be able to get to know the most about Linux and network security experts. If you have any projects to cooperate with, have any good suggestions, have security requirements, or have good information, please contact us. I am very grateful .? // Bow
The English version of this book can be found at http://metalab.unc.edu/ldp. For the printed version, see the post description.
Finally, it must be declared that the translation of this book is completely personal, and I only represent myself. This document is for internal communication and shall not be used by any organization or individual for commercial purposes without the permission of the author or the translator. If found, I have the right to pursue legal liability.
Email: xu_hui@icst.pku.edu.cn

1. Hello, world
When the first plug-in primitive programmer drilled out the first "Cave computer" program on the wall, it was a program that printed "Hello world" represented by a pattern on the horns. The Roman programming textbooks start with the "Salut, Mundi" program. I don't know what will happen if people break this tradition, but I don't think it is safer to discover the consequences.
A kernel module contains at least two functions: init_module, called when this module is inserted into the kernel; cleanup_module, called when the module is removed. In typical cases, init_module registers a handle for something in the kernel, or extracts the program in the kernel into its own code (usually it calls the original working code after some work ). The Clean_module module requires that all processing work performed by the init_module be revoked so that the module can be safely uninstalled.

Exhello. c
/* Hello. c
* Copyright (C) 1998 by Ori Pomerantz
*
* "" Hello, world ""-the kernel module version.
*/

/* The necessary header files */

/* Standard in kernel modules */
# Include/* Were doing kernel work */
# Include/* Specifically, a module */



/* Deal with CONFIG_MODVERSIONS */
# If CONFIG_MODVERSIONS = 1
# Define MODVERSIONS
# Include
# Endif



/* Initialize the module */
Int init_module ()
{
Printk ("" Hello, world-this is the kernel speaking "");

/* If we return a non zero value, it means that
* Init_module failed and the kernel module
* Cant be loaded */
Return 0;
}


/* Cleanup-undid whatever init_module did */
Void cleanup_module ()
{
Printk ("" Short is the life of a kernel module "");
}

1.1 kernel module compilation File
A kernel module is not a file that can be executed independently, but a target file that needs to be connected to the kernel at runtime. Therefore, they need to be compiled using the-c option. In addition, all kernel modules must contain specific labels:
? _ KERNEL _ -- this flag tells the header file that the Code will run in the KERNEL module, rather than as a user process.
? MODULE -- this flag tells the header file to define the appropriate kernel MODULE.
? LINUX-Technically speaking, this sign is not necessary. However, if you want to write a more formal kernel module and compile it on multiple operating systems, this sign will make you feel convenient. It allows you to perform regular compilation independently of the operating system.
There are other include flag options that can be selected, depending on the option of the compilation module. If you cannot determine how the kernel is compiled, you can find it in/usr/include/linux/config. h.
? _ SMP _ -- symmetric multithreading. It must be defined when the kernel is compiled to support symmetric multithreading (although running on a processing machine. If so, you need to do something else (See Chapter 1 ).
? CONFIG_MODVERSIONS -- If CONFIG_MODVERSIONS is activated, You need to define it in compilation and include the file/usr/include/linux/modversions. h. This can be automatically completed by code.

Ex Makefile

# Makefile for a basic kernel module

CC = gcc
MODCFLAGS: =-Wall-DMODULE-d1_kernel _-DLINUX

Hello. o: hello. c/usr/include/linux/version. h
$ (CC) $ (MODCFLAGS)-c hello. c
Echo insmod hello. o to turn it on
Echo rmmod hello to turn if off
Echo
Echo X and kernel programming do not mix.
Echo Do the insmod and rmmod from outside

Therefore, not the rest is root (you didn't compile it as root, but on the edge (Note 1.1 ). Right ?), Then insert or remove hello from your core content. When you do this, you should note that your new module is in/proc/modules.
In addition, it is not recommended to insert a compilation file from X because the kernel has a message that needs to be printed using printk, which is sent to the console. If you do not use X, it is sent to the virtual terminal you use (which one you choose with Alt-F) and you can see it. On the contrary, if you use X, there are two possibilities. If xterm? C opens a console, where the output will be sent. If not, the output will be sent to virtual terminal 7 -- the one that is "overwritten" by X.
If your kernel becomes unstable, you can receive debugging messages without X. In addition to X, printk can be output directly from the kernel to the console. In X, if printk is output to a user-state process (xterm? C ). When the process receives the CPU time, it sends it to the X server process. Then, when the X server process receives the CPU time, it will display, but an unstable kernel means that the system will crash or restart, so you do not want to display the error message, the error may be explained to you, But it exceeds the correct time.
More than 1.2 file Kernel Modules
Sometimes it makes sense to separate a kernel module among several source files. In this case, you need to do the following:
1. Add a line # define _ NO_VERSION _ to all the source files except one __. This is very important because module. h generally includes the definition of kernel_version, which is a global variable and contains the kernel version compiled by the module. If you need version. h, you need to include it yourself, because module. h does not automatically include _ NO_VERSION.
2. Compile the source file as usual.
3. Combine all target files into one. In X86, use ld? M elf_i386? R? O. o <1st source file>
Here is an example of such a kernel module.
Ex start. c

/* Start. c
* Copyright (C) 1999 by Ori Pomerantz
*
* "" Hello, world ""-the kernel module version.
* This file already des just the start routine
*/

/* The necessary header files */

/* Standard in kernel modules */
# Include/* Were doing kernel work */
# Include/* Specifically, a module */



/* Deal with CONFIG_MODVERSIONS */
# If CONFIG_MODVERSIONS = 1
# Define MODVERSIONS
# Include
# Endif



/* Initialize the module */
Int init_module ()
{
Printk ("" Hello, world-this is the kernel speaking "");

/* If we return a non zero value, it means that
* Init_module failed and the kernel module
* Cant be loaded */
Return 0;
}
Ex stop. c

/* Stop. c
* Copyright (C) 1999 by Ori Pomerantz
*
* "" Hello, world ""-the kernel module version. This
* File except des just the stop routine.
*/

/* The necessary header files */

/* Standard in kernel modules */
# Include/* Were doing kernel work */

# Define _ NO_VERSION _/* This isnt "" the "file
* Of the kernel module */
# Include/* Specifically, a module */

# Include/* Not supported ded
* Module. h because
* Of the _ NO_VERSION __*/



/* Deal with CONFIG_MODVERSIONS */
# If CONFIG_MODVERSIONS = 1
# Define MODVERSIONS
# Include
# Endif




/* Cleanup-undid whatever init_module did */
Void cleanup_module ()
{
Printk ("" Short is the life of a kernel module "");
}
Ex Makefile

# Makefile for a multifile kernel module

CC = gcc
MODCFLAGS: =-Wall-DMODULE-d1_kernel _-DLINUX

Hello. o: start. o stop. o
Ld-m elf_i386-r-o hello. o start. o stop. o

Start. o: start. c/usr/include/linux/version. h
$ (CC) $ (MODCFLAGS)-c start. c

Stop. o: stop. c/usr/include/linux/version. h
$ (CC) $ (MODCFLAGS)-c stop. c
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.