Step-by-step implementation of Linux device-driven HelloWorld modules

Source: Internet
Author: User
Tags bz2 syslog

Having learned so many programming languages, there is always a hello world opening that cannot help but lament the power of Hello World. Oh, nonsense less, why the story of course to start from this Hello world.

Check the kernel version used by your OS first
[[Email protected]:~]$ uname-r
2.6.22-14-generic/* This is the result of my display */

If the system is installed, the source code is installed automatically. In the/USR/SRC directory there is a corresponding version of the directory to use. For example (I am under my own)
[[email protected] :/usr/src]# ls
Linux-headers-2.6.22-14
Linux-headers-2.6.22-14-generic
linux-source-2.6.22/* This is the extracted source directory */
LINUX-SOURCE-2.6.22.TAR.BZ2/* This is my next source package */

If there is no source code. (There is no general Ubuntu.)
Check out the source package that you can download (remember not to use this command with Superuser otherwise ...). Will be prompted without this command)
[email protected] :/usr/src# apt-cache Search Linux-source
Linux-source-linux kernel source with Ubuntu patches
Xen-source-2.6.16-linux kernel source for version 2.6.17 with Ubuntu patches
Linux-source-2.6.22-linux kernel source for version 2.6.22 with Ubuntu patches
[email protected] :/usr/src#

I chose Linux-source-2.6.22-linux kernel source for version 2.6.22 with Ubuntu patches this ~
Then install the

[email protected] # sudo apt-get install linux-source-2.6.22

After the download is complete, under/USR/SRC, the file name is: linux-source-2.6.22.tar.bz2, is a compressed package, decompression can get the entire kernel source code:

Note that you have switched to Super User mode

[email protected] :/usr/src# tar-jxvf linux-source-2.6.20.tar.bz2

Unzip into a new directory/usr/src/linux-source-2.6.22, all the source code is in this directory.

Enter this directory

Start configuring the kernel select the fastest original configuration (default) mode (I am)

[email protected] :/usr/src/linux-source-2.6.22# make Menuconfig

Of course you can also use your preferred configuration such as Menuconfig, Xconfig (must have GTK environment bar). It doesn't have to be trimmed anyway, so it's OK to configure it in that way.

When you're done, start make it's a long time to be here for 11 hours. (Make sure the space is enough for me to compile and use 1.8G) I partition time to give/directory 30G space, I do not encounter this problem. It was my friend who met.

[email protected] :/usr/src/linux-source-2.6.22$ make

[email protected] :/usr/src/linux-source-2.6.22$ make Bzimage

Of course, the first make can also be done without execution, directly makes Bzimage. After execution, you can see that a new file has been generated under the current directory: Vmlinux, whose property is-rwxr-xr-x.

And then:

[email protected] :/usr/src/linux-source-2.6.22# make Modules/* Compile module */

[email protected] :/usr/src/linux-source-2.6.22# make modules_install/* Install module */

After execution finishes, a new directory is generated under/lib/modules/lib/modules/2.6.22-14-generic/
。 The build directory under this path is used for subsequent compilation of the module file. This completes the kernel compilation. You can restart the system.

At this point the kernel tree has been established is not very difficult .....

(1) Linux open source of course, the contribution of sources, please see below (as to what is open source, tragic I do not understand now):

#include <linux/init.h>  //Include two header files in all module code # include <linux/module.h>module_license ("Dual BSD/GPL" ); All module codes should specify the license used by static int hello_init (void)  {        printk (kern_alert "hello,world\n");  return 0;  }   static void Hello_exit (void)  {        printk (kern_alert "Goodbye,cruel world\n");  }  Module_init (hello_init);  Module_exit (Hello_exit);   

See here we understand that the driver is plainly to provide a function interface to the user space program calls. There is a main () entry in the C language, where is the entry for the device driver? You guessed it, it's module_init (), and its argument is a function pointer, telling me the entrance is in Hello_init (). Understand this meaning, module_exit () will not have to say more.

(2) Even the source code is given to you, it is not stingy a makefile is as follows:

Ifneq ($ (kernelrelease),)       obj-m: = hello.o        #设置模块名字else     kerneldir: =/lib/modules/$ (shell uname-r)/build   #将目录改为内核所在目录     PWD: = $ (shell pwd) all  :       $ (make)-C $ (kerneldir) subdirs=$ (PWD) modules  endif clean  : rm-f *.o       *.ko * . mod.c. hello*    

Do not say do not understand makefile, the world, even the map do not understand, do not know how mixed. Here to say is $ (make) here must be capitalized make, I began to lowercase, how all the hard, depressed ah.

$ (make)-C $ (kerneldir) Subdirs = $ (PWD) modules what does that mean? This means first changing the directory to the directory specified by the-C option (that is, the kernel source code directory), which holds the kernel's top-level makefile file. The subdirs= option allows the makefile to be returned to the module source code directory before the modules target is constructed. The modules target then points to the module that the OBJ-M variable sets. (In fact, this kind of writing makefile command is still a little annoying, what can be done, who let us be stupid birds, clever way? There, I would like to see the next introduction-----clever makefile, hehe).

(3) Well, there are all, should not have a bit not greedy. Make some of the following:

[[Email protected]~]# make
Make-c/lib/modules/2.6.29.4-167.fc11.i686.pae/build Subdirs=/root/device Modules
MAKE[1]: Entering directory '/usr/src/kernels/2.6.29.4-167.fc11.i686.pae '
Building modules, Stage 2.
Modpost 1 Modules
cc/root/device/hello.mod.o
LD [M]/root/device/hello.ko
MAKE[1]: Leaving directory '/usr/src/kernels/2.6.29.4-167.fc11.i686.pae '

This is the generation of the Hello.ko module (if not, there is a eight or nine is the makefile problem, also said will write, dew the prototype bar), then you can see the results

First, open a terminal B (the one you just made do not turn off, call it a), like this
[Email protected]~]# tail-f/var/log/messages

Then enter in the A terminal
[Email protected]~]# insmod/hello.ko haha at B terminal is not seen on the localhost Kernel:hello,world

Then enter in the A terminal
[[email protected] ~]# rmmod hello haha at b terminal is not see the localhost kernel:goodbye,cruel world

View load Modules
[Email protected] ~]# Lsmod
Module Size used by
Hello 2560 0
Already loaded on the ~ ~

What about the output of that program? The book says that if it does not appear in the terminal it will be written into the syslog file
[email protected]: ~/linux_ Driver Development $ cat/var/log/syslog | grep World
Mar 12:14:53 Shana Kernel: [5937.529297] Hello, world
Mar 12:16:05 Shana Kernel: [6009.439036] Goodbye, Cruel world

At this point, one of the simplest HelloWorld device driver demo program is finished, is not quite fun, the key is quite excited, this is the key. Ha ha

When the workers have to work, when the soldiers are ready to fight (or some people are always bullying me not), so, when the programmer, it must be at the end of a note about what, irritability ah.

So let's just explain:
(1) This helloworld, I suggest to implement under Fedora, in CentOS or redhat or those I have not tried, there are some problems. This is mainly in some systems do not support the module, it is necessary to recompile the kernel, select "Enable loadable module supports", so that can. You have to forget to go north, I can not use a rope to cover your water is, no matter, anyway, I am concerned about the HelloWorld completed, this is the legendary responsibility division of labor, that? Oh, wait, I must not on another day.
(2) When writing makefile, all the blanks after wrapping are tab keys, not space (SPACEBAR).
(3) The file of this routine is saved as HELLO.C, otherwise the compilation does not pass.

Learning experience:
(1) The drive module runs in kernel space, and the runtime cannot rely on any function libraries and module connections, so the function that is called when writing the driver can only be a function that is part of the kernel.
(2) An important difference between a driver module and an application is that the application exits without releasing resources or other cleanup work, but the module's exit function must carefully undo everything that the initialization function has done, or something will remain in the system before the system reboots.
(3) The processor's various operating modes (levels) are designed for the operating system's user space and kernel space. Only two levels are used in the Unix class's operating system: highest and lowest levels.
(4) Pay great attention to the concurrent processing of the driver.
(5) A function with double underscore (_ _) in the kernel API, usually the underlying component of the interface, should be used with caution.
(6) Kernel code cannot implement floating point book operation.

Step-by-step implementation of Linux device-driven HelloWorld modules

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.