Design and Implementation of Linux kernel (1)-features of Linux kernel development

Source: Internet
Author: User

Features of Linux kernel development

Compared with applications in a user spaceProgramThere are many differences in development and kernel development. The most important differences include:

1) The C library cannot be accessed during kernel programming.

2) gnu c must be used for Kernel programming.

3) There is a lack of memory protection mechanisms like user space in kernel programming.

4) floating point numbers are difficult to use during kernel programming.

5) the kernel has only one small fixed-length stack.

6) because the kernel supports asynchronous interrupt, preemption, and SMP, you must always pay attention to synchronization and concurrency.

7) consider the importance of portability.

 

1. No libc library

Unlike applications in a user space, the kernel cannot be linked to use the standard C function library (or other libraries ). The main reason is speed and size. Although not available, most common C-library functions have been implemented in the kernel. For example, the function group of the Operation string is located in the LIB/string. c file, and can be used as long as it contains the <Linux/string. h> header file.

The kernel does not implement printf (), but you can use printk () and printk () to copy formatted strings to the kernel log buffer, the SYSLOG program can obtain kernel information by reading the buffer. The usage of printk () is similar to that of printf ():

Printk ("Hello world! A string: % s and an integer: % d. \ n ", a_string, an_integer );

A major difference between printk () and printf () Is that printk () allows priority setting by specifying a flag. Syslog determines where the system message is displayed based on the priority mark.

Printk (kern_err "This Is A error! \ N ");

 

2. GNU C

The Linux kernel is written in C language, but it does not fully comply with the ansi c standard, but uses many language extensions provided by GCC.

1) Inline Function

Inline enables the function to expand at the position where it is called. This eliminates the overhead (register storage and recovery) caused by function calling and return ). In addition, the compiler will callCodeIt is optimized together with the function itself, so it is possible to further optimize the code. However, there is a price to do this, and the code will become longer, which means occupying more memory space or occupying more instruction cache. Usually, functions with relatively high requirements on time and short length are defined as inline functions.

Static inline void dog (unsinged long tail_size );

In the kernel, inline functions rather than complex macros are preferred for type security reasons.

2) inline assembly

The GCC compiler supports embedding Assembly commands in c functions. Of course, this function can be used only when the corresponding architecture is known during kernel programming.

3) Branch Declaration

For Condition Selection statements, GCC has a built-in command for optimization. When a condition is frequently used or is rarely used, the compiler can optimize the condition Branch selection based on this command, the kernel encapsulates this command into a macro, such as likely () and unlikely ().

For example, the following is a condition selection statement:

If (FOO ){

/*..........*/

}

If you want to mark this option as a rare Branch:

/* We think that the vast majority of Foo time is 0 */

If (unlikely (FOO )){

/*..........*/

}

On the contrary, if we want to mark a branch as usually true

/* We do not think foo is usually 0 */

If (likely (FOO )){

/*..........*/

}

When you want to optimize a condition selection statement, you must check whether such a condition exists. In most cases, it will be true. This is very important: If your judgment is correct, it is determined that this condition is overwhelming, so the performance will be improved. If you make a mistake, the performance will decline. When determining some error conditions, unlikely () and likely () are often used (). As you can guess, unlikely () is widely used in the kernel, because if statements often judge a special situation.

 

3. No memory Protection Mechanism

If a user program tries to perform an illegal memory access, the kernel will find this error, send SIGSEGV, and end the whole process. However, if the kernel illegally accesses the memory itself, it will be difficult to control the consequences. Memory Errors in the kernel will lead to oops, which is the most common type of errors in the kernel. In the kernel, you should not access illegal memory addresses or reference null pointers or other things. Otherwise, it may die, but you may not know what to say-in the kernel, risks are often higher than those outside.

In addition, the memory in the kernel is not paged. That is to say, if one byte is used, the physical memory is reduced by one byte. So remember this when you want to add new features to the kernel.

 

4. Do not use floating point numbers in the kernel easily

When a floating point operation is performed in a user space process, the kernel will complete the mode conversion from Integer Operation to floating point operation. What will be done when executing floating point commands? The kernel selection varies depending on the architecture. However, the kernel usually captures traps and processes them accordingly.

Unlike a user-space process, the kernel cannot perfectly support floating-point operations because it itself cannot fall. When using floating point numbers in the kernel, apart from manually saving and recovering floating point registers, there are other trivial tasks to do. If you want to answer the question bluntly, do not use floating point numbers in the kernel.

 

5. Small and fixed stacks

The user space program can allocate a large amount of space from the stack to store variables, even a huge struct or an array containing many data items. Because the user space stack itself is relatively large, and it can also grow dynamically.

The exact size of the kernel stack varies with the architecture. On x86, the stack size can be 4 kb or 8 KB during compilation. The size of the kernel stack is two pages. Therefore, the size of the 32-bit Kernel stack is 8 KB, while that of the 64-bit kernel is 16 KB, which remains unchanged. Each processor has its own stack.

 

6. Synchronization and concurrency

The kernel is prone to competition conditions. Because many features of the kernel require concurrent access to shared data, a synchronization mechanism is required to ensure that no competition conditions occur, especially:

1) Linux is a multi-task operating system. The kernel process scheduler schedules and reschedules processes on an impromptu basis. The kernel must synchronize these tasks.

2) the Linux kernel supports multi-processor systems. Without proper protection, code running on two or more processors is likely to access the same shared resource.

3) interruptions come asynchronously, regardless of the Code being executed. If appropriate protection is not provided, the interruption may come when the Code accesses shared resources, so that the interrupt handler may access the same resource.

4) the Linux kernel can be preemptible. Without proper protection, a piece of code in the kernel may be preemptible by another piece of code, as a result, several codes may simultaneously access the same resource.

 

7. The importance of portability

Linux is a portable operating system. Most C code should be independent of the architecture and can be compiled and executed on many computers with different architectures. Therefore, the system structure-related code must be properly shared from a specific directory in the kernel code tree.

Such as maintaining the byte order, 64-bit alignment, not assuming the length of the word and page length, and other principles are helpful for portability.

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.