1. 早期的電腦只能定址到1MB (20bits), 而0 - 640KB 是給早期電腦的RAM使用, 稱為low memory
2. 384KB: 0x000A0000 through 0x000FFFFF: reserved for video and firmware held in non-volatile memory
3. Basic Input/Output System (BIOS), which occupies the 64KB region from 0x000F0000 through 0x000FFFFF. (960KB - 1MB)
4. BIOS會找到boot loader, 放到 0x7C00 - 0x7DFF
+------------------+ <- 0xFFFFFFFF (4GB) | 32-bit | | memory mapped | | devices | | | /\/\/\/\/\/\/\/\/\/\ /\/\/\/\/\/\/\/\/\/\ | | | Unused | | | +------------------+ <- depends on amount of RAM | | | | | Extended Memory | <- OS | | | | +------------------+ <- 0x00100000 (1MB) | BIOS ROM | +------------------+ <- 0x000F0000 (960KB) | 16-bit devices, | | expansion ROMs | +------------------+ <- 0x000C0000 (768KB) | VGA Display | +------------------+ <- 0x000A0000 (640KB) | | | Low Memory | | | +------------------+ <- 0x00000000
1. BIOS
開機後第一行執行的code
[f000:fff0] 0xffff0: ljmp $0xf000,$0xe05b
[segment:OFFSET] linear addr: instruction在real mode 底下, segmentation的轉換方式為
linear address = 16 * segment + offset
16 * 0xf000 + 0xfff0 # in hex multiplication by 16 is = 0xf0000 + 0xfff0 # easy--just append a 0. = 0xffff0
0xffff0落在960KB到1MB之間, 所以是BIOS, BIOS必須再往前跳一點, 在開始執行
BIOS會初始化interrupt table,和一些devices, 然後把OS從硬碟中讀出來
之後就交給boot loaderThe bios read the boot loader from disk and transfer control to it.
2. Boot Loader
boot loader的位置固定在0x7c00 through 0x7dff, 當開機後BIOS會去尋找可以用來開機的boot sector (512byte), 可能在disk上或其他地方, 找到後把boot sector load到0x7c00, 利用jmp instruction to set the CS:IP = 0000: 7c00.
boot loader的主要兩個工作
1. switch the processor mode from real to protected mode.
2. 把kernel image從硬碟中讀出來 放到physical address 0x00100000 (1MB)的地方
用CR0來enable protected mode (boot/boot.S):
lgdt gdtdesc
movl %cr0, %eax
orl $CR0_PE_ON, %eax
movl %eax, %cr0
(lgdt: load GDT)Real mode和Protected mode:
傳統16 bit 8086 CPU有16位元暫存器, 匯流排等等, 利用CS: IP 的方法來定址到1MB的記憶體, 物理位址 = CS * 16 + IP. 當轉換到protected mode時, CS不在代表一個address, 而是代表GDT裡面的index, 而GDT entry裡面有base, limit, 等資料結構 (Descriptor).
這就是為什麼上面那段code要先lgdt gdtdesc之後才可以切換到protected mode
See orange 3-10
3. Loading the kernel
OS通常linked and run在high virtual address such as 0xf0100000, 但有些機器無法提供如此高的memory address, 所以我們使用了processor的memory map把0xf0100000 map到0x00100000也就是RAM 1MB的地方 (就是在BIOS上方).
reference:
1. http://www.cs.sunysb.edu/~porter/courses/cse506/f11/lab1.html
沒有留言:
張貼留言