给i386写驱动 -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位)呀,
这也就够一个跳转的,也就是reset跳转,这个七个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复制过来的代码里,这个位置肯定是一个跳转呗。
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跳转一下,跳到BIOS代码开始的地方,BIOS就这么跑起来了,
这时候BIOS就去找引导设备了,挨个存储设备看引导扇区,也就是它们的0扇区
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了,那就牛逼了,可以把这个扇区复制到RAM内存的0x7C00地址傻瓜,写成32位的话,其实是0x00007c00。
之后跳到这个7c00开始执行代码,这种占用一个扇区大小的代码叫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.
这个一扇区的代码,会把内核kernel加载到物理地址0x10 0000,这个地址不就是高位2低位0咩
据说在所有的大内核系统里面,都喜欢用这个地址来作为开始地址,可能也是IBM规定的
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,这样一整加载内核的时候就是按照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检测到启动协议,之后用16位的实模式来加载内核,
之后内核运行起来了,由Linux内核来切换到保护模式
话说这样有啥好处呀