Lesson 2. Booting the Computer (1)

Booting is the first task the OS has to deal with after you power on a computer. The boot loader is probably the best starting point to begin the OS study. An interesting question is: what happens when you power on the computer? Of course, the OS starts functioning. But what actions are performed exactly? To answer this question, we first have to know what microprocessor is installed. Since Intel's processors are dominant in the PC market, we assume the OS is running on an x86 machine. The "Intel Architecture Software Developer's Manual" is the best source for your reference. It describes every detail about the processor. Volume 3 is especially helpful as it describes operating system support environment of the Intel processors. "FreeBSD Architecture Handbook" also has a very detail description about the hardware behavior and the FreeBSD implementation. Related references will be provided at the end for your reference.

Let's start the journey by powering on the computer. The processor first performs hardware initialization of the processor and an optional built-in self-test (BIST). One of the tasks is to set the processor's registers to a predefined state. The first instruction executed is located at physical address 0xFFFFFFFF0. This starting address is formed as follows. During a hardware reset, the address generation is handled somehow differently from real-mode and protected-mode. The CS (Code Segment) register has two parts: the visible segment selector part and the hidden base address part. During the reset, these two parts are loaded with 0xF000 and 0xFFFF0000. The EIP (Instruction Pointer) register is initialized to 0xFFF0. The starting address is formed by adding the value in the EIP to the base address (that is, 0xFFFF0000 + 0xFFF0 = 0xFFFFFFF0). Note after the first time the CS register is loaded with a new value after a hardware reset, the processor will follow the rule of address translation in real-mode (memory address mode will be discussed in later topics). Thus to insure the base address in CS register remains intact during initialization, the initialization code must not contain a far jump or far call or allow an interrupt to occur.

The state of control register CR0 is initialized to 0x60000010 as shown in Figure. 1. It places the processor in real-mode with paging disabled. The linear address and the physical address are identical. According to the real-mode address translation rule, the maximum addressable memory space is 1Mb. However, the first instruction address is pointed at 0xFFFFFFF0, slightly less than 4Gb. It is out of the addressable memory space in the real-mode. The hardware translates this address so that it points to a piece of code in the basic input output system (BIOS).

The BIOS is a piece of read-only memory chip (ROM) on the motherboard. It contains many low-level service routines designed specific to the motherboard. After hardware initialization completes, the processor starts to fetch and execute the instruction from the address 0xFFFFFFF0, which indeed resides in the BIOS ROM. Usually, a jump instruction is put at that address, directing to the BIOS's power on self test routine (POST). The POST routine performs a series of hardware tests, including memory, system bus and other peripheral devices. It also prepares necessary execution environment before handing over the control. One important step is determining the boot device from a list of candidates (e.g. floppy disk, hard disk, CD-ROM and USB driver). Finally, the POST routine loads the first sector (512 bytes) of the boot device into the memory at address 0x07C00 and then jumps to that memory location for execution. The first sector of the boot device is called boot sector or master boot record (MBR).

In summary, after the computer is powered on, the BIOS routine loads the boot sector into memory at address 0x07C00 and starts executing instructions from there.

[1] Intel Architecture Software Developer's Manual Volume 3 - System Programming
[2] FreeBSD Architecture Handbook
[3] 操作系统引导探究 / Operating System Booting Investigation (Chinese)