《Linux Kernel Development》Note 2
A Beast of a Different Nature
No libc
There are multiple reasons for this, including some chicken and the egg situations, but the primary reason is speed and size. The full C library — or even a decent subset of if — is too large and too inefficient for the kernel.
GNU C
Like any self-respecting Unix kernel, the Linux kernel is programmed in C. Perhaps surprising, the kernel is not programmed in strict ANSI C. Instead, where applicable, the kernel developers make use of various language extensions available in gcc.
The kernel developers use both ISO C99 and GNU C extensions to the C language. These changes wed the Linux kernel to gcc , although recently compilers, such as the Intel C compiler, have sufficiently supported enough gcc features that they too an compile the Linux kernel. The ISO C99 extensions that the kernel uses are nothing special and, because C99 is an official revision of the C language, are slowly cropping up in a lot of other code. The more interesting, and perhaps unfamiliar, deviations from standrd ANSI C are those provided by GNU C. Let’s look at some of the more interesting extensions that may show up in kernel code.
Inline Function
Inline Assembly
Branch Annotation
The kernel wraps the directive in very easy to use macros, likely() unlikely() .
No Memory Protection
When a user-space application attempts an illegal memory access, the kernel can trap the error and kill the process. If the kernel attempts an illegal memory access, the results are less controlled. Memory violations in the kernel result in an oops, which is a major kernel error. It should go without saying that you must not access illegal memory, dereference a NULL pointer, and so on — but within the kernel, the stakes are much higher!
Additionally, kernel memory is not pageable. Therefore, every byte of memory you consume is one less byte of available physical memory. Keep that in mind next time you have to add one more feature to the kernel!
No (Easy) Use of Floating Point
Unlike user-space, the kernel does not have the luxury of seamless support for floating point. Using floating point inside the kernel requires manually saving and restoring the floating point registers, among possible other chores. The short answer is: Don’t do it; no floating point in the kernel.
Small, fixed Size Stack
The kernel stack is neither large nor dynamic; it is small and fixed in size. The kernel stack is fixed at 8KB on 32-bit architectures and 16KB on most 64-bit architectures.
For more discussion on the kernel stack, see the section Statically Allocating on the Stack in Chaper 10, “Memory Management”.
Synchronization and Concurrency
Portability Is Important