Macros in Linux kernel module

Source: Internet
Author: User

Author-------Dansen-----xzd2734@163.com

When writing a module, the header file <Linux/module. h> is usually required. In module. H, the symbol _ module_kernel_version is defined.
Static const char _ module_kernel_version [] _ attribute _ (Section (". modinfo") =
"Kernel_version =" uts_release;
# Define uts_release "2.4.18-rmk7-pxa1" // include/Linux/version. h
The symbol _ module_kernel_version is put in the. modinfo section of the. o file after compilation. insmod uses it to check whether the module matches the current kernel version.
If you divide the module code into two parts, you need to include the module in another file. before H, define _ no_version _ so that _ module_kernel_version is not repeatedly defined. However, it is useless in my source code and is used in my module. h does not include such conditions, and compilation will not go wrong. It is true when you use the objdump tool. modinfo has two _ module_kernel_version symbols. Objdump-s hello. o

The following is a MAKEFILE file.

Cc =/opt/host/armv4l/bin/armv4l-unknown-linux-gcc
LD =/opt/host/armv4l/bin/armv4l-unknown-linux-ld
Cflags =-d1_kernel _-dmodule-I/HHARM2410-R3/kernel/include-wall-wstrict-prototypes-wno-trigraphs-OS-MAPCs-fno-strict-aliasing-fno-common -fno-common-pipe-mapcs-32-March = armv4-mtune = arm9tdmi-mshort-load-bytes-msoft-float-I/opt/host/armv4l/src/Linux/include

ALL: Hello. o
Hello. O: Start. o stop. o
$ (LD)-r $ ^-o $ @

. Phony: clean
Clean:
-Rm-f *. o

Of course, it is my arm platform. Pay attention to cflags.-d1_kernel _-dmodule is equivalent to define the _ KERNEL _ and module Macros in each file, you also need to use-I to include the kernel source code.

In addition, let's analyze some macros that are frequently used by the module.
The first one is this_module. It was known from the very beginning that it was the direct module itself, but let's take a deeper look.
Find this_module definition in module. h
# If defined (module )&&! Defined (_ genksyms __)
Extern struct module _ this_module;
# Define this_module (&__ this_module)
# Else
# Ifndef _ genksyms __
# Define this_module null
# Endif
# Endif
Generally, _ genksyms __is not defined, while modules are defined in programming,
Therefore, this_module is a pointer of the struct module type.
We compile the module into. O the target file is not actually linked by calling the file. Obviously, the actual link process occurs when we call the kernel of the module insmod, in insmod, the kernel will create a structure structure of the struct module to accommodate the modules loaded from the user space. During the link, this pointer will be sent to this_module.
The following macros define a string in the. modinfo section and there is nothing to say.
# Define module_author (name )/
Const char _ module_author [] _ attribute _ (Section (". modinfo") =/
"Author =" Name

# Define module_description (DESC )/
Const char _ module_description [] _ attribute _ (Section (". modinfo") =/
"Description =" DESC

The following macros are complex.
# Define mod_inc_use_count _ mod_inc_use_count (this_module)
# Define mod_dec_use_count _ mod_dec_use_count (this_module)
# Define mod_in_use _ mod_in_use (this_module)

# DEFINE _ mod_inc_use_count (MOD )/
(Atomic_inc (& (MOD)-> UC. usecount), (MOD)-> flags | = mod_visited | mod_used_once)
# DEFINE _ mod_dec_use_count (MOD )/
(Atomic_dec (& (MOD)-> UC. usecount), (MOD)-> flags | = mod_visited)
# DEFINE _ mod_in_use (MOD )/
(Mod_member_present (MOD), can_unload) & (MOD)-> can_unload/
? (MOD)-> can_unload (): atomic_read (& (MOD)-> UC. usecount ))

However, it seems that this_module is relatively simple. Obviously, there is a variable used to count the module in the struct module. We can compile a simple module to print the value of the variable in the current module structure.
Union
{
Atomic_t usecount;
Long pad;
} UC;
Mod_inc_use_count is to increase usecount by 1, mod_dec_use_count minus 1
Mod_in_use is quite complicated. If you want to continue learning, continue learning... Compile a simple module to test and compare eazy.

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.