# Mit6.828 Lab1: Part2 Jos Bootstrap -- bootloader

## 16位保护模式

In the 80286’s 16-bit protected mode, selector values are interpreted completely differently than in real mode. In real mode, a selector value is a paragraph number of physical memory. In protected mode, a selector value is an index into a descriptor table. In both modes, programs are divided into segments. In real mode, these segments are at fixed positions in physical memory and the selector value denotes the paragraph number of the beginning of the segment. In protected mode, the segments are not at fixed positions in physical memory. In fact, they do not have to be in memory at all!

## 32位保护模式

80386引入了32位保护模式，与16位保护模式的两点主要的区别：

• 偏移扩展到32位，允许段的大小扩展到4 gigabytes。
• 段可以划分成4KB大小的页的基本访问单元，现在虚拟内存系统中使用置换页而不是置换段，也就是任意时刻只有段的一部分在内存中，在286的16位模式中，要么整个段在内存中，要么置换到硬盘中，但是286这种置换段的方式显然不适合32位保护模式。

## Exercise 3

1. 进入32位保护模式的时机

BIOS要从实模式跳到保护模式，先设置GDTR以及GITR寄存器，由于跳转到保护模式后，内存的寻址方式采用分段机制的寻址方式。因此需要在进入保护模式之前准备好全局描述符表。然后设置CR0寄存器，设置CR0寄存器中的PE位然后执行一条ljmp指令进入保护模式。

1. 内核的第一条指令在地址0x10000c处执行。

### What is ELF and three types of ELF

ELF (Executable and Linking Format), the object file format, There are three main types of object files.

• A relocatable file holds code and data suitable for linking with other object files to create an
executable or a shared object file
.
• An executable file holds a program suitable for execution
• A shared object file holds code and data suitable for linking in two contexts. First, the link
editor may process it with other relocatable and shared object files to create another object file
.
Second, the dynamic linker combines it with an executable file and other shared objects to
create a process image
.

Created by the assembler and link editor, object files are binary representations of programs
intended to execute directly on a processor
.

### ELF File Format

The significance of ELF file format.
Object files participate in program linking (building a program) and program execution
(running a program). For convenience and efficiency, the object file format provides parallel
views of a file’s contents, reflecting the differing needs of these activities

• ELF header resides at the beginning and holds a “road map’’ describing the file’s organization
• Sections hold the bulk of object file information for the linking view: instructions, data, symbol table, relocation information, and so on.
• A program header table, if present, tells the system how to create a process image. Files used to build a process image (execute a program) must have a program header table; relocatable files do not need one.
• A section header table contains information describing the file’s sections. Every section has an entry in the table; each entry gives information such as the section name, the section size, and so on. Files used during linking must have a section header table; other object files may or may not have one.

• e_phoff
This member holds the program header table’s file offset in bytes. If the file has no program header table, this member holds zero.
• e_phnum
This member holds the number of entries in the program header table. Thus the product of e_phentsize and e_phnum gives the table’s size in bytes. If a file has no program header table, e_phnum holds the value zero.

boot loader就是在这几条语句发现该读取多少扇区进入内存，第一条语句是查找内核的program header所在的位置，结构体Proghdr表示program header entry。 因此ph是program header的位置亦即第一条program header entry的位置，eph是program header结束的位置。for循环调用readseg函数，以p_addr,p_offset,p_memsz为参数，即从内核指定的偏移ph->p_offset读取ph->p_memsz大小的字节到内存的ph->p_pa地址中。

On systems for which physical addressing is relevant, this member is reserved for the segment’s physical address.
• p_offset
This member gives the offset from the beginning of the file at which the first byte of the segment resides
• p_memsz
This member gives the number of bytes in the memory image of the segment; it may be zero.

C定义的ELF headers在inc/elf.h中，通常使用的program sections是：

• .text: 程序的可执行指令
• .data: 保存程序初始化数据的数据分区(data section)，如全局变量的初始化int x=6;

We set the link address by passing -Ttext 0x7C00 to the linker in boot/Makefrag, so the linker will produce the correct memory addresses in the generated code.

### Exercise 5.

Trace through the first few instructions of the boot loader again and identify the first instruction that would “break” or otherwise do the wrong thing if you were to get the boot loader’s link address wrong. Then change the link address in boot/Makefrag to something wrong, run make clean, recompile the lab with make, and trace into the boot loader again to see what happens. Don’t forget to change the link address back and make clean again afterward!

ELF header中的e_entry很重要，这个域保存了程序开始执行的link address(entry_point，虚拟地址)。可以通过如下指令查看内核的第一条执行的语句：

### Exercise 6.

Reset the machine (exit QEMU/GDB and start them again). Examine the 8 words of memory at 0x00100000 at the point the BIOS enters the boot loader, and then again at the point the boot loader enters the kernel. Why are they different? What is there at the second breakpoint? (You do not really need to use QEMU to answer this question. Just think.)