2.6 simple implementation of kernel module programming: Hello World

Source: Internet
Author: User

After programming the application, we need to know the kernel module and the programming method of the driver. In the book "Linux device drivers", we have prepared a simple example at the beginning: Hello world.

Step 1:

Write the following code:

# Include <Linux/init. h> // note that if this header file is not included, a warning is generated during compilation. // insmod
No output after hello. Ko

# Include <Linux/module. h>

Module_license ("dual BSD/GPL ");

 

Static int hello_init (void)

{

Printk ("Hello, world \ n ");

Return 0;

}

 

Static void hello_exit (void)

{

Printk ("Goodbye, cruel world \ n ");

}

 

Module_init (hello_init );

Module_exit (hello_exit );

 

Here I refer to the difference in programming 2.4 kernel and 2.6 kernel modules on todaygood's blog:

1. Use a new portal

Must contain

Module_init (your_init_func );

Module_exit (your_exit_func );

Old Version:
Int init_module (void );

Void cleanup_module (voi );

2.4 can be used in both cases. For example, you do not need to display any header files for the entry function.

2. GPL

Module_license ("dual BSD/GPL ");

Old Version: module_license ("GPL ");

 

Step 2:

When writing makefile, you must specify the following variables:

Obi-M = Hello. o

Kerneldir: =/usr/local/ARM/k9linux/linux-2.6.13/images_dir/lib/modules/2.6.13/build/* Specify the build directory here, because the files under this directory will be used during compilation */

Kernelhead: =/usr/local/ARM/k9linux/linux-2.6.13/include/* The header file of Hello word is the kernel header file, so we must specify it here. This is for cross-compilation. If it is for the local machine, the GCC on the local machine has specified the address of the kernel header file in the compilation configuration */

# The current directory is passed to sub-makes as argument

PWD: = $ (shell PWD)

 

Modules:

$ (Make)-C $ (kerneldir)-I $ (kernelhead) M = $ (PWD) Modules

 

Modules_install:

$ (Make)-C $ (kerneldir) M = $ (PWD) modules_install

 

Clean:

Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions

 

. Phony: modules modules_install clean

 

Step 3:

When we cross-compile the Linux kernel, make zimage and then directly create the image uimage. If the kernel module is dynamically loaded, We need to compile and install the kernel module after make zimage.

# Make modules // all modules configured as kernel modules during Kernel configuration Compilation

# Make install_mod_path =/usr/local/ARM/k9linux/linux-2.6.13/images_dir modules_install

/* Install the module. The command will pass the module. Ko file compiled above to install_mod_path/lib/modules/Linux-version/kernel/derives /...... Under the corresponding folder. The install_mod_path/lib/modules/directory generated here is hello
Required in the makefile Of The World module */

 

Once the module is copied, the kernel tries to establish the module dependency according to the requirements of the module tool program during execution. Because depmod is a tool program used to establish module dependencies, it is not used to process cross-compilation modules, so execution will fail.

 

To establish a module dependency for a module, you must use the module dependency provided by busybox to establish a program. Copy depmod. pl under busybox/examples to the kernel folder. Now we can establish the module dependency for the target board:

#./Depmod. pl-B images_dir/modules/lib/modules-K vmlinux-F system. Map

 

Among them, the-K option is used to specify the uncompressed kernel image, and the-F option is used to specify the system ing file (-K and-F should not be used at the same time ), -Option B is used to specify the basic directory of the module for which the kernel needs to establish dependency. Because the execution result of this tool is sent to the standard output, we can redirect it to a file whose file name is always called modules. Dep

 

Step 4:

Run your own kernel module (not tested yet. I will release the test results and steps on my Bolg in a timely manner when the test is complete, I hope Linux beginners like me can master Linux as soon as possible. Thanks again to those netizens who frequently posted their experiences on the Internet. During my Linux study, I have referenced many blogs. I hope you will learn from them in the future .)

When running our own kernel module, we only need to copy the kernel module XXX. Ko to the target board through NFS,

# Insmod XXX. Ko

# Lsmod

In this way, we can see that our kernel module has been loaded into the kernel.

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.