views:

201

answers:

2

After the boot loader hands execution over to the kernel, what happens? I know assembler, so what are the first few instructions that a kernel must make? Or is there a C function that does this? What is the startup sequence before the kernel can execute an arbitrary binary?

+5  A: 

It's asmlinkage void __init start_kernel(void) C function in init/main.c.

Nikolai N Fetissov
+1 Awesome response! But I have one question, who calls start_kernel()?
Rook
I think Matthew already answered that.
Nikolai N Fetissov
+8  A: 

I'll assume that you're talking about x86 here...

It depends where you consider the boundary between "boot loader" and "kernel" to be: the start of the kernel proper is 32-bit protected mode code, but the kernel itself provides some boot code to get there from real mode.

The real mode code is in arch/x86/boot/: start_of_setup does some basic setup of the environment for C, and calls main(), which does some fairly dull stuff, ending with the actual jump to protected mode (see pmjump.S).

Where you end up now depends on whether or not the kernel is compressed. If it is, the entry point is actually a self-decompression routine. This is fairly dull stuff as well, and essentially transparent: the decompression code and compressed kernel are moved higher up in memory out of the way, then the kernel is uncompressed to the original location, and then jumped into as if it had been uncompressed all along. This code is in arch/x86/boot/compressed/ (the entry point is startup_32 in head_32.S).

The kernel really gets going properly at startup_32 in arch/x86/kernel/head_32.S. The code there ends up by calling i386_start_kernel() in arch/x86/kernel/head32.c, which finally calls the generic kernel startup code in start_kernel().

Matthew Slattery
This is a K.O.! How did you find this out? Is there a book I should read?
Rook
Experience, really: enough familiarity with the kernel source tree layout to know just where to look; and enough experience with the type of things that OS bootstrapping involves (having implemented complex multi-stage bootstrap code for embedded systems in the past), combined with some knowledge of x86 booting, to know what to look for. I don't know whether there is a good book which covers this area or not... (The details of kernel internals tend to change rapidly enough that books get outdated quite quickly!)
Matthew Slattery