Linux Kernel Programming!

Source: Internet
Author: User
Tags dmesg
Linux Kernel Programming! -- Linux general technology-Linux programming and kernel information. The following is a detailed description. This article is from one of my blogs!
Http://blog.chinaunix.net/space... & do = blog & id = 2856305
Welcome to visit!

I have been studying Linux kernel programming these days. here is a little show.
First, write the following linux code. And name it hello. c.
Here you should pay attention to the highlights:
First, Linux kernel programming is different from normal user-mode programming. there is an entry main function. the "main" function here is module_init (); at the same time, there is a aftercare function: module_exit ().
2. Linux kernel programming is different from user-mode programming during compilation. you can directly use the gcc compiler to compile the link to make it executable. Instead, you need to compile a Makefile file, not makefile !! This involves a lot of things in the kernel mode. I have not figured out the specifics! The details will be shared later.

Hello. c file
1 # include
2 # include
3 # include
4 static int _ init lkp_init (void );
5 static int _ init lkp_exit (void );
6
7
8 static int _ init lkp_init (void)
9 {
10 printk ("<1> Hello, Word! \ N ");
11 return 0;
12}
13
14 static int _ exit lkp_exit (void)
15 {
16 printk ("<2> Hello, Word exit \ n ");
17}
18 module_init (lkp_init );
19 module_exit (lkp_exit );

Create a Makefile in the same directory of hello. c.
Makefile file:
1 ifneq ($ (KERNELRELEASE ),)
2 mymodule-objs: = hello. c
3 obj-m + = hello. o
4
5 else
6 PWD: = $ (shell pwd)
7 KVER: = $ (shell uname-r)
8 KDIR: =/lib/modules/$ (KVER)/build
9
10 all:
11 $ (MAKE)-C $ (KDIR) M = $ (PWD)
12 clean:
13 rm-rf *. o *. mod. c *. ko *. symvers * order *. markers *-
14 endif
Note:
When running make, the Makefile file is called. KERNELRELEASE is a variable defined in the kernel source code's top-level/usr/src/linux-headers-2.6.32-33/Makefile file. If the reader is downloading the linux-3.0 kernel, then in the **/linux-3.0/Makefile. The first row. As follows:

379 # Read KERNELRELEASE from include/config/kernel. release (if it exists)
380 KERNELRELEASE = $ (shell cat include/config/kernel. release 2>/dev/null)
381 KERNELVERSION = $ (VERSION) $ (if $ (PATCHLEVEL),. $ (PATCHLEVEL) $ (if $ (SUBLEVEL),. $ (SUBLEVEL) $ (EXTRAVERSION)
When this Makefile is read and executed for the first time, the variable $ (KERNELRELEASE) is not set. Therefore, the condition for the first line of ifneq fails and is executed starting after else, set variables such as PWD, KVER, and KDIR.
When make encounters an all label,-C $ (KDIR) jumps to the kernel source code directory to read the Makefile. M = $ (PWD) indicates that the current directory is returned for further reading, and the current Makefile is executed, that is, the second call of make. The variable $ (KERNELRELEASE) has been defined. Therefore, if ifneq is successful, make will continue to read the content following ifneq. Ifneq contains the kbuild statement, which indicates the dependency between files in the module source code and the target module to be generated.
Statement 2 mymodule-objs: = hello. c table is mymodule. o generated by hello. c. Statement 3 obj-m + = hello. o table is a link that will generate the mymodule. ko module. This file is the module file to be inserted into the kernel.
If the target of make is clean, directly execute the operation after the clean label to delete a lot of things! After executing the rm command after the clean command, make's work is over!

Execution result:
[#35 # caopeng @ laptop: ~] $ Cd kernel/
[#36 # caopeng @ laptop :~ /Kernel] $ ls
Hello. c Makefile
[#37 # caopeng @ laptop :~ /Kernel] $ make
Make-C/lib/modules/2.6.32-33-generic/build M =/home/caopeng/kernel
Make [1]: entering Directory '/usr/src/linux-headers-2.6.32-33-generic'
LD/home/caopeng/kernel/built-in.o
CC [M]/home/caopeng/kernel/hello. o
/Home/caopeng/kernel/hello. c: In function 'lkp _ exit ':
/Home/caopeng/kernel/hello. c: 17: warning: no return statement in function returning non-void
/Home/caopeng/kernel/hello. c: In function '_ exittest ':
/Home/caopeng/kernel/hello. c: 19: warning: return from incompatible pointer type
Building modules, stage 2.
MODPOST 1 modules
CC/home/caopeng/kernel/hello. mod. o
LD [M]/home/caopeng/kernel/hello. ko
Make [1]: leaving Directory '/usr/src/linux-headers-2.6.32-33-generic'
[#38 # caopeng @ laptop :~ /Kernel] $
Compiled successfully !!
Looking at the files in the current directory, you will find a lot more things! As follows:
[#38 # caopeng @ laptop :~ /Kernel] $ ls
Built-in.o hello. ko hello. mod. o Makefile Module. symvers.
Hello. c hello. mod. c hello. o modules. order
[#39 # caopeng @ laptop :~ /Kernel] $
I don't know why all the generated files do not have the x permission! Here, we will use hello. ko to grant it executable permissions. And insert it with insmod! Run the command to view the result!
[#47 # caopeng @ laptop :~ /Kernel] $ sudo rmmod hello. ko
[#48 # caopeng @ laptop :~ /Kernel] $ sudo insmod hello. ko
[#49 # caopeng @ laptop :~ /Kernel] $ dmesg-c
Klogctl: Operation not allowed
[#50 # caopeng @ laptop :~ /Kernel] $ sudo dmesg-c
[2, 14651.331364] Hello, Word exit
[2, 14658.553284] Hello,
[#51 # caopeng @ laptop :~ /Kernel] $
Remember rmmod !!!
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.