Linux kernel source code analysis (1) -- compiler. h analysis (1)

Source: Internet
Author: User
The Linux kernel source code contains the file Linux/compile. H. Therefore, first analyze the content in the file as the beginning.

1. Content not defined during compilation

 

The first content of this file is the judgment of macro _ Assembly _. The function of this macro is to avoid defining subsequent content during compilation. This macro is added by using the-D option in the compiler. The aflags parameter also contains the macro definition. During assembly, the compiler defines _ Assembly _ as 1.

 

# Ifndef _ Assembly __

 

 

1.1 attribute definitions used for sparse tool Detection

 

Next, the _ checker _ macro judgment, __checker _ Macro will be defined when checking the kernel code through the sparse (semantic Parser for C) tool. This tool is called when you use make C = 1 or C = 2. This tool can check the kernel functions and variables that declare the relevant attributes that sparse can check in the code.

 

# Ifdef _ checker __

 

The following describes the attributes that sparse can check.

 

# DEFINE _ User _ attribute _ (noderef, address_space (1 )))

# DEFINE _ KERNEL/* default address space */

# DEFINE _ safe _ attribute _ (SAFE ))

# DEFINE _ force _ attribute _ (Force ))

# DEFINE _ nocast _ attribute _ (nocast ))

# DEFINE _ iomem _ attribute _ (noderef, address_space (2 )))

# DEFINE _ acquires (x) _ attribute _ (context (x, 0, 1 )))

# DEFINE _ Releases (x) _ attribute _ (context (x, 1, 0 )))

# DEFINE _ acquire (x) _ context _ (x, 1)

# DEFINE _ release (x) _ context _ (x,-1)

# DEFINE _ cond_lock (x, c) (c )? ({__ Acquire (x); 1 ;}): 0)

Extern void _ chk_user_ptr (const volatile void _ User *);

Extern void _ chk_io_ptr (const volatile void _ iomem *);

 

The _ User attribute is used to modify the address of a variable. The variable must be a non-release reference (no dereference), that is, the address is valid, and the address space of the variable must be 1, here is (address_space (1), the user address space. Sparse divides the address space into three parts. 0 indicates the normal address space, which is the address space for the kernel. 1 indicates the user address space. 2 indicates the device address ing space, that is, the address space of the device register.

The _ KERNEL property modifier variable is the kernel address, which is the default address space in the kernel code.

The _ safe attribute declares this variable as a security variable to avoid kernel functions that are used without verifying input parameters, this will cause the compiler to report an error or generate an alarm. This feature indicates that the variable cannot be empty.

The _ force attribute declares that the variable can be forced type conversion.

_ Nocast declares that the variable parameter type must be consistent with the actual parameter type.

_ Iomem declares that the address space is the device address ing space, and others are the same as _ User.

_ Acquires is a modifier defined by the function attribute. It indicates that the reference count value of this parameter in the function is changed from 1 to 0.

_ Releases is opposite to _ acquires. This modifier is used for sparse to check the number of calls and matching requests during static code detection. It is often used to detect the acquisition and release of locks.

_ Acquire indicates to increase the Count of variable X. The increase is 1.

_ Release: Reduce the number of variable X, and reduce the number to 1. This pair is the same as the one above, except that this pair is used to check the imbalance in the code During function execution.

_ Cond_lock is used to indicate the condition lock. When the value of C is not 0, the Count value is increased by 1 and 1 is returned.

_ Chk_user_ptr and _ chk_io_ptr declare only functions here, and there is no function body. The purpose is to capture compilation errors during compilation and check the parameter type.

 

 

 

Indicate the source and author's contact information during reprinting.

Source: http://www.mythfish.com/

Author contact: Zhang genius mythfish@gmail.com

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.