給i386寫驅(qū)動 -2

https://arjunsreedharan.org/post/82710718100/kernels-101-lets-write-a-kernel
神文檔,可操作
寫一下這個文檔的理解過程

**How does an x86 machine boot**
Most registers of the x86 CPU have well defined values after power-on. The Instruction Pointer (EIP) register holds the memory address for the instruction being executed by the processor. EIP is hardcoded to the value **0xFFFFFFF0**. Thus, the x86 CPU is hardwired to begin execution at the physical address 0xFFFFFFF0\. It is in fact, the last 16 bytes of the 32-bit address space. This memory address is called reset vector.

商店以后,特別特殊的(E)IP寄存器,它上電以后固定是一堆(7個)F跟一個0,
上電以后IP從這里開始,那也就是只給留了16Byte的32位空間(每個Byte32位)呀,
這也就夠一個跳轉(zhuǎn)的,也就是reset跳轉(zhuǎn),這個七個f一個0就叫reset vector
Now, the chipset’s memory map makes sure that 0xFFFFFFF0 is mapped to a certain part of the BIOS, not to the RAM. Meanwhile, the BIOS copies itself to the RAM for faster access. This is called shadowing. The address 0xFFFFFFF0 will contain just a jump instruction to the address in memory where BIOS has copied itself.
既然這樣,主板廠商在做BIOS的時候第一件事兒就是把它的代碼復制到RAM上,這個過程叫Shadowing。
那七F一0地址就特別重要,基本一定BIOS復制過來的代碼里,這個位置肯定是一個跳轉(zhuǎn)唄。
Thus, the BIOS code starts its execution. BIOS first searches for a bootable device in the configured boot device order. It checks for a certain magic number to determine if the device is bootable or not. (whether bytes 511 and 512 of first sector are 0xAA55)
從七F一0跳轉(zhuǎn)一下,跳到BIOS代碼開始的地方,BIOS就這么跑起來了,
這時候BIOS就去找引導設備了,挨個存儲設備看引導扇區(qū),也就是它們的0扇區(qū)
Once the BIOS has found a bootable device, it copies the contents of the device’s first sector into RAM starting from physical address 0x7c00; and then jumps into the address and executes the code just loaded. This code is called the bootloader.
在哪個設備上看到0xAA55了,那就牛逼了,可以把這個扇區(qū)復制到RAM內(nèi)存的0x7C00地址傻瓜,寫成32位的話,其實是0x00007c00。
之后跳到這個7c00開始執(zhí)行代碼,這種占用一個扇區(qū)大小的代碼叫bootloader
The bootloader then loads the kernel at the physical address 0x100000. The address 0x100000 is used as the start-address for all big kernels on x86 machines.
這個一扇區(qū)的代碼,會把內(nèi)核kernel加載到物理地址0x10 0000,這個地址不就是高位2低位0咩
據(jù)說在所有的大內(nèi)核系統(tǒng)里面,都喜歡用這個地址來作為開始地址,可能也是IBM規(guī)定的
All x86 processors begin in a simplistic 16-bit mode called real mode. The GRUB bootloader makes the switch to 32-bit protected mode by setting the lowest bit of CR0 register to 1. Thus the kernel loads in 32-bit protected mode.
上電以后,都是16比特的實模式,可以通過grub來配置成保護模式,
做法就是把CR0的值配置成1,這樣一整加載內(nèi)核的時候就是按照32位加載了
Do note that in case of linux kernel, GRUB detects linux boot protocol and loads linux kernel in real mode. Linux kernel itself [makes the switch] to protected mode.
Linux的做法是,grub檢測到啟動協(xié)議,之后用16位的實模式來加載內(nèi)核,
之后內(nèi)核運行起來了,由Linux內(nèi)核來切換到保護模式

話說這樣有啥好處呀

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 幾個學習鏈接:https://www.cnblogs.com/LexMoon/p/How_to_Make_a_Co...
    少女白潔閱讀 766評論 0 0
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,906評論 0 0
  • 01 這幾年自媒體的發(fā)展,很多人靠寫字成了網(wǎng)紅。比如,咪蒙,孫晴悅,特立獨行的貓,剽悍一只貓,李尚龍等等。一夜爆...
    靈女俠閱讀 475評論 0 0
  • 暑假來了,什么才是孩子最喜歡的呢?怎么的生活孩子會更開心呢?要怎么樣合理安排才能給孩子一個好假期?這些都是令家長比...
    七月風閱讀 179評論 0 4
  • 部屋(へや)に機(つくえ)と椅子(いす)があります 機(つくえ)の上に貓(ねこ)がいます 売電(ばいてん)は 駅?...
    anniecool閱讀 180評論 0 0