xv6是mit6.828操作系统课程的基于unix v6重新实现的教学操作系统。
本文英文部分是x86版本的xv6(mit6.828 2018及以前的版本)完全注释,github:xv6-annotated
本文中文部分是我学习xv6过程中添加了部分中文注解
DAS BOOT
First things first: in order for a computer to run xv6, we need to load it from
disk into memory and tell the processor to start running it. So how does this
all happen?
The Boot Process
When you press the power button, the hardware gets initialized by a piece of
firmware called the BIOS (Basic Input/Output System) that comes pre-installed on
the motherboard on a ROM chip. Nowadays, your computer probably uses UEFI loaded
from flash memory, but xv6 pretends like it’s 1995 and sticks with BIOS. Since
xv6 runs on x86 hardware, we’re gonna have to satisfy all the janky requirements
that come with that architecture, in addition to the BIOS’s requirements.
Now the BIOS has to load some other code called the boot loader from disk;
then it’s the boot loader’s job to load the OS and get it running. The boot
loader has to act as a middle-man because the BIOS has no idea where on the disk
you decided to put the OS.
The BIOS will look for the boot loader in the very first sector (512 bytes) of
whatever mass storage device you told it to boot from, which we’ll call the boot
disk. The processor will execute the instructions it finds there. This means
you have to make a choice: either your boot loader has to be less than 512 bytes
or you can split it up into smaller parts and have each part load the next one.
xv6 takes the first approach.
The BIOS loads the boot loader into memory at address 0x7C00, then sets the
processor’s %ip register to that address and jumps to it. Remember that %eip
is the instruction pointer on x86? Okay cool. But why did I write %ip instead
of %eip? Well, the BIOS assumes we’re gonna be using 16 bits because of the
hellscape known as backwards-compatibility, so we’ve gotta pretend like it’s
1975 before we can pretend it’s 1995. The irony here is that this initial 16-bit
mode is called “real mode”. So on top of loading the OS, the boot loader will
also have to shepherd the processor from real mode to 32-bit “protected mode”.