Before attempting the kernel development, you need to have an overall understanding of the kernel.
Main content:
- Get the kernel source code
- Structure of Kernel source code
- Methods for compiling the kernel
- Features of kernel development
1. Get the kernel source code
The kernel is open source, all access to the source code is particularly convenient, referring to the following URL, you can download through git or directly compressed source package.
http://www.kernel.org
2. Structure of kernel source code
Directory |
Description |
Arch |
Code for a specific architecture |
Block |
Block device I/O layer |
Crypo |
Encryption API |
Documentation |
Kernel Source Documentation |
Drivers |
Device drivers |
Firmware |
Device firmware required for use with certain drivers |
Fs |
VFS and various file systems |
Include |
Kernel header File |
Init |
Kernel Boot and initialization |
Ipc |
Inter-process Communication code |
Kernel |
A core subsystem such as a scheduler |
Lib |
Same kernel function |
Mm |
Memory management subsystem and VMS |
Net |
Network subsystem |
Samples |
Example, demo code |
Scripts |
Scripts used to compile the kernel |
Security |
Linux Security Module |
Sound |
Voice subsystem |
Usr |
Early user space code (so-called Initramfs) |
Tools |
Tools that are useful in Linux development |
Virt |
Virtualization Infrastructure |
3. How to compile the kernel
Have not actually tried to manually compile the kernel, just update the kernel with yum. This part is later manually compiled and then mended.
After installing the new kernel, you will be prompted to enter which kernel when restarting. When the new kernel is installed multiple times, the startup list can be very long (because there are many versions of the kernel), which is not very convenient.
Here are 3 ways to remove the unused kernels: (How to install the appropriate deletion method)
3.1 RPM Delete method
Rpm-qa | grep kernel* (Find all Linux kernel versions)
Rpm-e kernel-(the version you want to delete)
3.2 Yum Delete method
Yum Remove kernel-(the version to be removed)
3.3 Manual Removal
Delete the kernel library files that are not required under the/lib/modules/directory
Remove the unwanted kernel source from the/usr/src/kernel/directory
Delete core files launched in/boot directory kernel image
To change the configuration of grub, remove the unwanted kernel boot list
4. Features of kernel development4.1 No standard C library
In order to ensure that the kernel is small and efficient, the kernel development can not use the C standard library, so even the most common printf function is not, but fortunately there is a PRINTK function to replace.
4.2 Using GNU C, it is recommended to compile the kernel with GCC 4.4 or later
Because GNU C is used, some of the extensions in GNU C are often used in all cores:
4.2.1 Inline function
The inline function expands at compile time where it is called, reducing the overhead of function calls and performing better. However, frequent use of inline functions also makes the code longer, which consumes more memory at run time.
Therefore, the use of inline functions is best to meet the following points: The function is small, will be repeated calls, the time requirements of the program is more stringent.
Inline Function Example: static inline void sample ();
4.2.2 Inline Compilation
The inline assembly is used in places that are near the bottom or where strict execution times are required. Examples are as follows:
intvolatile("rdtsc""=a " " =d " (high)); /* */
4.2.3 Branch Declaration
If it is often true or often false to judge an if statement in advance, you can use unlikely and likely to optimize the code for this judgment.
/**/if (unlikely (error) ) {/**/} /* * /if (likely (success) ) {/* */}
4.3 No memory protection
Because the kernel is the lowest level of the program, so if the kernel access to the illegal memory, then the entire system will be hung off!! So the risk of kernel development is greater than the risk of user program development.
Furthermore, the memory in the kernel is non-paged, and the physical memory is one byte less per byte of memory. So use memory in the kernel must be cautious.
4.4 Do not use floating-point numbers
The kernel cannot perfectly support floating-point operations, and when using floating-point numbers, it is necessary to manually save and restore floating-point registers and other cumbersome operations.
4.5 Core stack volume is small and fixed
The size of the kernel stack is determined by the time the kernel is compiled, and the size of the kernel stack, although not identical, is fixed for unused architectures.
To see how the kernel stack is sized:
" Stack size "
4.6 Synchronization and concurrency
Linux is a multi-user operating system, so synchronization and concurrency must be handled to prevent deadlocks from competing.
4.7 Portability
The Linux kernel can be used to support a variety of hardware without the use of a structure. So be aware of portability when developing, and use architecture-independent code whenever possible.
"Linux kernel design and implementation" Reading notes (ii)-Preparation of kernel development