Bootstrap

The xv6's MBR contains the bootloader code (bootasm.S + bootmain.c)

Why xv6 (and other OSes) needs a boot loader, instead of "loading it self" on to the memory?

bootasm.S

Detect  physical memory mapping mapping with int 820h.

On your Linux: $ dmesg | grep e820, both usable and reserved are backed by DRAM.

bootmain.c

The kernel bootstrap

Let's talk about entry and mboot_entry

VMA and LMA

Why we have to use a different LMA? (The following explains the current implementation, but other systems can adopt a better approach.)

memlayout.h: KERNBASE, V2P, P2V

The kernel booting process before entering C

Read Makefile's kernel part, kernel.ld, and memlayout.h to get the dots connected.

What's in a Linux kernel image?

/boot/vmlinuz-linux    # a compressed Linux kernel file (6MB)

$ file vmlinuz-linux

vmlinuz-linux: Linux kernel x86 boot executable bzImage, version 5.2.13-arch1-1-ARCH (builduser@heftig-49962) #1 SMP PREEMPT Fri Sep 6 17:52:33 UTC 2019, RO-rootFS, swap_dev 0x5, Normal VGA

decompress it with a special script:

wget -O extract-vmlinux https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux

bash ./extract-vmlinux vmlinuz-linux > kernel

$ file kernel

vmlinux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=..., stripped

The ELF format

http://www.skyfree.org/linux/references/ELF_Format.pdf

This article explains more about multiboot vs ELF. An ELF executable can be compatible with Multiboot.

The ELF format itself defines some "bootable" metadata. The linux kernel on my laptop is a pure ELF image. It does not contain a multiboot header.

$ readelf -l kernel


Elf file type is EXEC (Executable file)

Entry point 0x1000000

There are 5 program headers, starting at offset 64


Program Headers:

  Type           Offset             VirtAddr           PhysAddr

                 FileSiz            MemSiz              Flags  Align

  LOAD           0x0000000000200000 0xffffffff81000000 0x0000000001000000

                 0x00000000011d0000 0x00000000011d0000  R E    0x200000

  LOAD           0x0000000001400000 0xffffffff82200000 0x0000000002200000

                 0x00000000004dd000 0x00000000004dd000  RW     0x200000

  LOAD           0x0000000001a00000 0x0000000000000000 0x00000000026dd000

                 0x000000000002d000 0x000000000002d000  RW     0x200000

  LOAD           0x0000000001b0a000 0xffffffff8270a000 0x000000000270a000

                 0x000000000016e000 0x0000000000522000  RWE    0x200000

  NOTE           0x0000000000e00e34 0xffffffff81c00e34 0x0000000001c00e34

                 0x00000000000001ec 0x00000000000001ec         0x4