kernel presentation at ILUG on Sat.

Charles Steinkuehler charles at steinkuehler.net
Mon May 5 17:17:32 CDT 2003


James Colannino wrote:
> I live in another part of the United States and so I wouldn't have been 
> able to be there, but I have a couple questions about the kernel that I 
> have been itching to figure out and was wondering if anyone could help 
> enlighten me :)
> 
> First, how exactly does the kernel uncompress itself?

It's magic!

> Are there a few 
> lines of code at the beginning of the binary that instruct the computer 
> on how to uncompress the rest?

Yes, it's very much like the way a boot-loader works.  A tiny section of 
code is responsible for extrating (or loading) the rest of the kernel 
(or boot-loader).  How many "levels" of this happen can change, 
depending on the system complexity.  For example, in a typical x86 based 
PC, you have the following general boot sequence:

- BIOS performs POST, loads and runs the MBR
- MBR code (all 256 bytes!) loads the initial boot-loader (typically 
K-Bytes of code (several sectors at fixed location on the disk), but not 
the entire boot-loader)
- The initial boot-loader loads the rest of the boot-loader
- The boot loader determines which kernel and initial ramdisk to load, 
and which options (if any) to pass to the kernel
- The kernel and initial ramdisk are loaded into memory
- The boot-loader starts running the kernel
- The kernel decompresses itself, and begins general "housekeeping" 
(initializing buses, configuring the interrupt controler(s), etc)
- If an initial ramdisk was provided, the kernel optionally decompresses 
it (if compressed) and tries to mount it and run /linuxrc
- The final root filesystem is mounted, and the kernel runs init.

>  What program handles the compression 
> during compilation and what sort of algorithm is used?  Gzip maybe?

Gzip does the compression (see the gzip -f -9 line below).

Binutils, including ld (the linker) and objcopy are used along with some 
custom tools (arch/i386/boot/tools/build) to turn the raw kernel image 
(vmlinux) into a bootable bzImage.

> lol I'm hoping SOMEONE can help me out with this.  I haven't found much 
> googling, partly because I'm not sure exactly under what terms I should 
> search for, and also, I've posted similiar questions to a couple other 
> user groups in my area, and no one seems to have an answer.

Take a look at the tail end of your kernel compile output sometime.  A 
representative example is included below:

<make bzImage output...will probably wrap horribly>
make[1]: Entering directory 
`/home/usr/src/linux-2.4.21-rc1-ac3/arch/i386/boot'
gcc -E -D__KERNEL__ -I/home/usr/src/linux-2.4.21-rc1-ac3/include 
-D__BIG_KERNEL__ -D__ASSEMBLY__ -traditional -DSVGA_MODE=NORMAL_VGA 
setup.S -o bsetup.s
as -o bsetup.o bsetup.s
bsetup.s: Assembler messages:
bsetup.s:3237: Warning: indirect lcall without `*'
ld -m elf_i386 -Ttext 0x0 -s --oformat binary -e begtext -o bsetup bsetup.o
make[2]: Entering directory 
`/home/usr/src/linux-2.4.21-rc1-ac3/arch/i386/boot/compressed'
tmppiggy=_tmp_$$piggy; rm -f $tmppiggy $tmppiggy.gz $tmppiggy.lnk; objcopy -O binary -R .note -R 
.comment -S 
/home/usr/src/linux-2.4.21-rc1-ac3/vmlinux $tmppiggy; gzip -f -9 < $tmppiggy > $tmppiggy.gz; echo 
"SECTIONS { .data : { input_len = .; LONG(input_data_end - 
input_data) input_data = .; *(.data) input_data_end = .; }}" > 
$tmppiggy.lnk; ld -m elf_i386 -r -o piggy.o -b binary $tmppiggy.gz -b elf32-i386 -T 
$tmppiggy.lnk; rm -f $tmppiggy $tmppiggy.gz $tmppiggy.lnk
ld -m elf_i386 -Ttext 0x100000 -e startup_32 -o bvmlinux head.o misc.o 
piggy.o
make[2]: Leaving directory 
`/home/usr/src/linux-2.4.21-rc1-ac3/arch/i386/boot/compressed'
objcopy -O binary -R .note -R .comment -S compressed/bvmlinux 
compressed/bvmlinux.out
tools/build -b bbootsect bsetup compressed/bvmlinux.out CURRENT > bzImage
Root device is (9, 1)
Boot sector 512 bytes.
Setup is 4789 bytes.
System is 862 kB
make[1]: Leaving directory 
`/home/usr/src/linux-2.4.21-rc1-ac3/arch/i386/boot'
[root at localhost linux-2.4.21-rc1-ac3]#

</output>

Note the conversion of the uncompressed kernel (vmlinux) into a gzipped 
file, then into an elf file (piggy.o), which gets included along with 
head.o, misc.o, bbootsect, and bsetup into bzImage.

If you really want to know what's going on when you start your kernel, 
look at the source files in your arch/i386/boot directory, along with 
the Makefile.

-- 
Charles Steinkuehler
charles at steinkuehler.net




More information about the Kclug mailing list