You are on page 1of 10

Getting Started with the Kernel

羅習五 老師
Obtaining the Kernel Source
The Kernel Source Tree
Directory Description
arch Architecture-specific source
crypto Crypto API
Documentation Kernel source documentation
drivers Device drivers
fs The VFS and the individual file systems
include Kernel headers
init Kernel boot and initialization
ipc Interprocess communication code
kernel Core subsystems, such as the scheduler
lib Helper routines

mm Memory management subsystem and the VM

net Networking subsystem

scripts Scripts used to build the kernel
security Linux Security Module
sound Sound subsystem
usr Early user-space code (called initramfs)
Lines of code
Different Natures
• The kernel does not have access to the C library.
• The kernel is coded in GNU C. (not in ANSI C)
• The kernel lacks memory protection like user-space.
• The kernel cannot easily use floating point.
• The kernel has a small fixed-size stack.
• Because the kernel has asynchronous interrupts, is
preemptive, and supports SMP, synchronization and
concurrency are major concerns within the kernel.
• Portability is important.
Different Natures
• NO libc support
– There are multiple reasons for this, including
some chicken-and-the-egg situations, but the
primary reason is speed and size (upcall?).
– Many of the usual libc functions have been
implemented inside the kernel.
• For example <linux/string.h>
Different Natures
• Inline Assembly
– The asm() compiler directive is used to inline
assembly code.
• Branch Annotation
if (unlikely(foo)) { /* ... */ } if (likely(foo)) { /* ... */ }

• Small, Fixed-Size Stack

– Historically, the kernel stack is two pages.
– Recently, the size of kernel stack is one page only!!!
Different Natures
• No (Easy) Use of Floating Point
– Supporting FPU is very expensive for kernel
– Using floating point inside the kernel requires
manually saving and restoring the floating point
– It is very hard to use the technique called “lazy
context switch” to speedup FPU context switching
Note: “DO” have some floating-point

• Recompile GNU‘s libc with option “--without-

fp” to avoid using floating-point hardware
while providing floating-point computations

• Compile the module's .c files with gcc's

– "-msoft-float" option and
Different Natures
• Synchronization and Concurrency
– “Linux” is a preemptive multi-tasking “operating
– The Linux kernel supports multiprocessing.
• For example, Linux can use more than one
core/processor to decode TCP/IP packets (named
– Interrupts occur asynchronously
– The “Linux kernel” is preemptive.