Linux kernel Module Development Basics "Go"

Source: Internet
Author: User

This article was reproduced from: http://blog.csdn.net/coding__madman/article/details/51298180

1. What is a kernel module

The kernel module has the following two features: 1. The module itself is not compiled into a kernel file (zimage or bzimage) and can be dynamically installed or uninstalled during kernel operation, as required.

2. Why kernel modules are required

Cause: The overall structure of the Linux kernel is very large, it contains a lot of components, how to use these components, Method 1: All the components are compiled key kernel, namely: Zimage or bzimage, but this will cause a problem: too much memory. Then the kernel module is born, can not be compiled into the kernel but can be dynamically added to the running kernel!

3. How to use kernel modules

1> Mounting Module Insmod Filename.ko

2> Unload module Rmmod filename

3> View Module Lsmod

Here is a simple example:

Still the same way HelloWorld the program (but there is no main function here)

[HTML]View PlainCopy
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. static int Hello_init ()
  4. {
  5. PRINTK (kern_warning "Hello world!\n");//The preceding macro represents the level of printing
  6. return 0;
  7. }
  8. static void Hello_exit ()
  9. {
  10. PRINTK (kern_warning "Hello exit!\n");
  11. }
  12. Module_init (hello_init);//Use a macro to specify that the load function is called when the import module is loaded
  13. Module_exit (Hello_exit);

Three elements: Load module, unload module, header file


Makefile file

[HTML]View PlainCopy
  1. Obj-m : = helloworld.o
  2. Kdir : =/home/kernel/linux-ok6410//Development Board run the kernel source path, because the module finally is to run on the Development Board, so the compilation of this module is to rely on the board running kernel source code, (Here I Development Board with the kernel source code is this path)
  3. All
  4. Make-c $ (Kdir) m=$ (PWD) modules cross_compile=arm-linux- arch=arm
  5. Clean
  6. Rm-f *.o *.ko *.order *.symvers

Here Kdir is the path of our kernel source code:


Then make to compile the module file, which can see that the Helloworld.ko file was generated

Combined with the previous courses here can be seen through the serial terminal synchronization (see the effect of the relevant command execution)

Optional options for kernel modules:

1. Module declaration

2. Module parameters

3. Symbol output

Linux kernel Module Development Basics "Go"

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.