The first Linux driver development program Hello World

Source: Internet
Author: User

I. Source Code

# Include <Linux/init. h> <br/> # include <Linux/module. h> <br/> module_license ("dual BSD/GPL"); <br/> static _ init int hello_init (void) <br/>{< br/> printk (kern_alert "Hello World/N"); <br/> return 0; <br/>}< br/> static _ exit int hello_exit (void) <br/>{< br/> printk (kern_alert "Goodbye, cruel world/N "); <br/> return 0; <br/>}< br/> module_init (hello_init); <br/> module_exit (hello_exit); <br/> 

Note:

1.1 # include <Linux/init. h>

This header file defines the kernel-specific macro definitions required by the module to load module_init and uninstall module_exit.

1.2 # include <Linux/module. h>

This header file defines the macro definition of the module license: module_license

1.3 module_license ("dual BSD/GPL ");

The purpose of this section of code is to tell the kernel that the module adopts a free license. If such a life cycle is absent, complaints may occur during kernel loading.

1.4 static _ init int hello_init (void)

{

Printk (kern_alert "Hello World/N ");

Return 0;

}

The initialization function should be declared as static because it is meaningless outside of this module.

The _ init flag is a hint for the kernel, indicating that the function is only used during initialization. After the module is loaded, the module loader will discard the initialization function and release the memory occupied by the function.

Printk is the kernel printing function.

1.5 static _ exit int hello_exit (void)

{

Printk (kern_alert "Goodbye, cruel world/N ");

Return 0;

}

The _ exit flag and code change are only used for uninstalling modules. This function can only be called when the module is detached or the system is shut down. If the module is directly embedded in the kernel, the function marked as _ exit will be discarded.

1.6 module_init (hello_init );

This macro will add a special segment in the target code of the module to describe and initialize the location of the function. Without this definition, the initialization function will never be called.

1.7 module_exit (hello_exit );

This macro is used to help the kernel find the clearing function of the module.

 

Ii. makefile

OBJ-M: = hello. O <br/> kdir: =/lib/modules/$ (shell uname-R)/build <br/> pwd: =$ (shell PWD) <br/> All: modules <br/> modules: <br/> make-C $ (kdir) M = $ (PWD) Modules <br/> clean: <br/> RM-RF *. ko *. mod. C *. mod. O module. * <br/> 

2.1 Before compiling the kernel module, ensure that the correct version of the compiler, module tools, and kernel tree are available. Generally, we do not need to worry about this because our file system already has a kernel tree of version 2.6, unless your Linux version is still very old.

2.2 obj-M is used to define which files are compiled into loadable modules. The. o file defined in obj-M is compiled and generated by the. C or. s file in the current directory. A module can consist of one or more. O files. For a module with only one source file, add its. o file directly in obj-M. For modules with multiple source files, add one in obj-M. o file, you also need to define a <module_name>-objs variable to tell makefile this. O files. For example, the module name is test. There are two files: file1.c and file2.c. It should be

OBJ-M: = test. o

Test-objs: = file1.o file2.o

2.3 kdir defines the number of kernel source code directories. You can change the name in uppercase. The common directory address is/lib/modules/$ (shell uname-R)/build, where the uname-R command outputs the kernel version information.

2.4 PWD defines the directory of the source file of the current Module

2.5 modules defines the make command: Make-C $ (kdir) M = $ (PWD) modules. The-C option specifies the kernel source code directory. The M = option allows the makefile to return to the module source code directory before constructing the modules target. The modules target points to the module set in the obj-m variable.

 

Iii. Running

3.1 run the make command in the directory where makefile is located. The <modules>. Ko file is generated, for example, hello. KO in this example. Include other intermediate files.

3.2 run insmod hello. Ko to load the module to the kernel. But we can see that Hello World is not output, because we are using a virtual terminal and the result is output to the system log file. For example, in/var/log/messages, you can use tailf/var/log/messages for viewing.

3.3 When the hello. Ko module is uninstalled, goodbye and cruel world will be output to the/var/log/messages file.

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.