Transcript ch02x
Getting Started with the Kernel
Obtaining the Kernel Source
www.kernel.org
http://lxr.free-electrons.com/
Lines of code
The Kernel Source Tree
Directory
lib
Description
Architecture-specific source
Crypto API
Kernel source documentation
Device drivers
The VFS and the individual file systems
Kernel headers
Kernel boot and initialization
Interprocess communication code
Core subsystems, such as the scheduler
Helper routines
mm
Memory management subsystem and the VM
net
Networking subsystem
Scripts used to build the kernel
Linux Security Module
Sound subsystem
Early user-space code (called initramfs)
arch
crypto
Documentation
drivers
fs
include
init
ipc
kernel
scripts
security
sound
usr
A Beast of a Different Nature
(in kernel mode)
• 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 fixedsize 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>
Inline Functions
An inline function is declared when the
keywords static and inline are used as part of
the function definition.
For example:
static inline void wolf(unsigned long tail_size)
– Because they are marked static , an exported
function is not created.
Inline Assembly
unsigned int low, high;
asm volatile("rdtsc" : "=a" (low), "=d" (high));
/* low and high now contain the lower and
upper 32-bits of the 64-bit tsc */
Branch Annotation
/* we predict 'success' is nearly always nonzero
... */
if (likely(success)) {
/* ... */
}
Note: GCC & ANSI C
“Linux System Programming” Robert Love
Appendix: GCC Extensions to the C Language
No (Easy) Use of Floating Point
• When a user-space process uses floating-point
instructions, the kernel manages the
transition from integer to floating point mode.
• Unlike user-space, the kernel does not have
the luxury of seamless support for floating
point because it cannot easily trap itself.
Note: “DO” have some floating-point computations
• 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
– "-D__NO_MATH_INLINES".
Small, Fixed-Size Stack
• The user-space stack can dynamically grow
• The kernel stack is small and fixed in size
– Historically, the kernel stack is two pages.
– Recently, the size of kernel stack is one page only!!!
Synchronization and Concurrency
• Linux is a preemptive multitasking operating
system.
• The Linux kernel is preemptive.
• Linux supports symmetrical multiprocessing
– For example, Linux can use more than one
core/processor to decode TCP/IP packets (named
softirq)
• Interrupts occur asynchronously
Conclusion
It is imperative that you read and modify the
kernel source
– Only through actually reading and experimenting
with the code can you ever understand it.
– The source is freely available—use it!
– http://140.123.102.115/lxr/linux/
homework
• Use “git” to maintain your kernel source tree
• Compile a Linux kernel