导航

操作系统内核学习【一】

Posted on 2010-11-17 17:21  fxcser  阅读(512)  评论(0)    收藏  举报

BootingProcess

引导过程

After power-up or RESET, an initialization will be performed on processors, it sets registers to a known state (note here, it is not a known value) and places the processor in real-mode. Then the processor will execute the instruction at physical address FFFFFF0 which usually is a far JMP instruction which set by EPROM. You might wonder how segment:offset pairs can present a physical address FFFFFF0, actually there is an invisible part in CS register, it stores a base address as FFFF0000,and the CS will be loaded with F000 during this initialization, but this just happens during the reset, after that they work in ordinary way. 

系统通电或重启时,处理器会进行初始化,设置寄存器为已知状态(not a known value  ??)并且设置处理器为实模式。处理器将执行物理地址FFFFFF0上的指令,这条指令通常是由EPROM设置的远跳转指令【far JMP instruction】。也许你想知道段:偏移地址如何表示物理地址FFFFFF0,实际上在CS寄存器里有一块不可见部分,它保存了FFFF0000基地址。CS寄存器在初始化时会被装入F000,但这些只是发生在重启时,以后将会以正常方式工作

Once the BIOS takes full control, it then try to load the operating system. Because the BIOS has no idea of the OS you are using: Windows, Linux or other wiredstuff like Skelix :), so creating an environment required by the OS will be a tough work for BIOS. So the BIOS left this work to OS itself, after POST, BIOS just load the first sector of the boot driver into a fixed location, that is, physical address 00007C00, then the code start at 00007C00 takes control and starts to creating the environment needed by the OS.

BIOS获得控制权后就加载操作系统。BIOS不知道你在用什么操作系统,windows、linux还是其他的,对BIOS来说为操作系统初始化环境是非常困难的,因此BIOS把这个工作留给了操作系统本身。BIOS只是把引导驱动的第一个扇区加载到了一个固定的位置--00007C00。加载到这个位置的那些代码接管控制权,创建OS启动需要的环境。

So the BIOS must find a 512 byte sector on the drive which it boots from and the sector must be ended with 0xAA55, which is a flag means this sector is a valid boot sector. Skelix boots from floppy disk.

BIOS在它引导的驱动上必须找到一块512字节的扇区,并且扇区要以0XAA55作为结束符。以上这些表明这个扇区是合法的。Skelix从软盘引导启动。

You should keep it in mind, at startup, the processor is in Real mode and uses segment:offset pairs to access a 1MB memory space without memory privilege protections. And we can use BIOS interrupts at this stage,even though they are not used in Skelix any more after the following tutorial.

在启动时处理器处于实模式下,没有内存特权保护,以段:偏移地址方式读取1M的内存空间。我们在这个阶段可以使用BIOS中断方式,但在以后的Skelix教程中并没有使用。


 

第一个程序

功能:什么都不做,死循环保持黑屏

1 .text
2 .globl start
3 .code16
4 start:
5
jmp start
6 .org 0x1fe, 0x90
7 .word 0xaa55
1、.text标志这段代码开始

2、被.globl修饰,start类似于C语言的extern

3、GCC默认32位地址和操作数,此处说明是16位

4~5、死循环,显示黑屏用

6、.org规定目标地址的偏移量,此处是1fe。我们的目的是把AA55作为标记写入1fe。0x1fe就是510。汇编指令和机器码对应起来0x90就是nop(什么都不做)。

 

执行程序的可以选择终端敲大量命令或是使用Makefile文件,Makefile文件代码如下

 

代码
AS=as
LD=ld

.s.
o:
${AS} -a $< -o $*.o >$*.map

all: final.img

final.
img: bootsect
mv bootsect final.img

bootsect: bootsect.o
${LD} --oformat binary -N -e start -Ttext 0x7c00 -o bootsect $<

1、as、ld GCC汇编和连接

 

2、--oformat指示目标文件的类型;binary表示没有程序头和其他信息,只是原始的二进制文件,如果没有这个标志,ld默认生成ELF文件;-N把text和data节设置为可读写;-e start表明代码从start开始执行;-Ttext 0x7c00将text节起始地址设置为0x7c00,是引导块开始的地方,所有的引用地址都是在7c00这个地址上加出来的

 

此程序编译执行,显示黑屏,达到预期效果

 

编辑我们经典的helloworld程序

代码
1 .text
2 .globl start
3 .code16
4  start:
5 jmp code
6  msg:
7 .string "Hello World!\x0"
8  code:
9 movw $0xb800,%ax
10 movw %ax, %es
11 xorw %ax, %ax
12 movw %ax, %ds
13
14 movw $msg, %si
15 xorw %di, %di
16 cld
17 movb $0x07, %al
18
19 1:
20 cmp $0, (%si)
21 je 1f
22 movsb
23 stosb
24 jmp 1b
25 1: jmp 1b
26
27 .org 0x1fe, 0x90
28 .word 0xaa55
29
30