Topic 5-Kernel module development

Source: Internet
Author: User

I. Basic
The 1.Linux kernel is huge and contains a large number of components. Linux files are often referred to as zimage and Bzimage, which are extracted to memory during kernel boot.
2. How do I use these components of Linux? In general, we would like to say, let all the components are included in the Linux kernel file, so solve everything, easy.
But the thought that followed was that if a component was actually running for 1 minutes during the whole system, but the whole system was running for an hour, the remaining 59 minutes would be wasted, and it would occupy the memory space. How bad you say, waste time and waste space.
3. New ideas for using components: dynamic loading and unloading. That is, when the component needs to be loaded, when it is not needed, it is unloaded. This is what we call the kernel module mechanism.
4. About kernel modules. Has the following characteristics:
(1) not be compiled into kernel file
(2) can be dynamically loaded and unloaded
5. Operations on kernel modules
(1) Load Insmod
(2) Uninstalling Rmmod
(3) View Lsmod
Two. Design and write
1. Analysis and structural generalization

2. Code writing

(1) The General init function is of type static int, exit function static void type
(2) The difference between PRINTK and printf is that the first thing PRINTK need is the parameter that represents the priority
Three. Written by makefile
1. A variable is required to indicate the name of the kernel module that we eventually generate
(1) If the kernel module has only one source file, the general module name and the original file, just change. C to. o
Obj-m: = HELLOWORLD.O
(2) If there are multiple source files, then it should be the module name can be arbitrarily, but also more than one statement
Module name-OBJS: = FILE1.O file2.o file3.o
2. Define a variable that indicates the path where the kernel code resides. Because the module is kernel-based, you use what version of the kernel as the basis, the compiled module can only be used in the same version of the kernel.
KIR: =/Kernel Source code path
3. Write the rules, only the target all, but no dependencies, but there are commands.
Make-c $ (kdir) M = $ (PWD) Modules Cross_compile = arm-linux ARCH = Arm
-C indicates the path (kernel path) to be followed by-C to compile the module, and M indicates the path to the module file
4. Finally write clean pseudo-directives to clear the resulting intermediate and. ko files. Copy the Ko file to the file system.

5.rmmod should have the same directory name as the kernel version of the kernel module under the/lib/modules directory
Frequently Used (UNAME-R) to obtain kernel versions
Mkdir-p/lib/modules/$ (UNAME-R)
6. Download the kernel to SDRAM using the TFTP address file name command, then mount the file System with NFS, start the LINXU system, and then operate the kernel module.

Four. Module options available
1. Module declaration
(1) Module_license ("Compliance Agreement")
Without this statement, a warning is reported when the module is loaded.
(2) Module_author ("author")
(3) module_version ("version")
(4) Module_description ("function description")
2. Module parameters
(1) As with the normal application, you can pass some parameters to the module when loading the module.
(2) The difference is that the data type and variable name of these parameters must be stated beforehand, and
Module_param (name,type,perm) this macro definition. He is used to indicate variables that save module parameters.
Name: variable name
Type: variable types, typically Int,bool and Charp (string type, remember is charp, not char and char*)
Perm: Permissions. Generally divided into S_irugo and s_iwusr. Usually they are bitwise OR.
3. Module symbol Export
(1) When a module to use the function of another module (variable), if there is no symbolic export, not only to implement this function, but also to declare that the function is exported to other modules to use, otherwise the use of this function in other modules will be error, or even can not load the module to use it.
(2) Use Export_symbol (symbol name) or EXPORT_SYMBOL_GPL (symbol name) to declare. The latter can only be used for modules that follow the GPL protocol.



If multiple standalone modules are generated, continue adding the. o file after obj-m.

Topic 5-Kernel module development

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.