head.s程序被编译后,会被链接成system模块的最前面开始的部分,head.s汇编程序与前面的语法格式不同,它采用的是AT&T的汇编语言格式,并且需要使用GNU的gas和gld2进行编译连接,因此注意代码中赋值的方向是从左到右。
startup_32:
movl $0x10,%eax
mov %ax,%ds
mov %ax,%es
mov %ax,%fs
mov %ax,%gs
lss stack_start,%esp
call setup_idt
call setup_gdt
movl $0x10,%eax # reload all the segment registers
mov %ax,%ds # after changing gdt. CS was already
mov %ax,%es # reloaded in 'setup_gdt'
mov %ax,%fs
mov %ax,%gs
lss stack_start,%esp
xorl %eax,%eax
此时机器进入保护模式,段寄存器里面的值不是基地址,而是段选择符,段选择符数据结构在这里说明了,0x10的含义是请求特权级0,选择全局描述符表,选择表中的第2项。它正好指向表中的数据段描述符项。请查看段描述符数据结构
从现在开始,要将ds,es,fs,gs等其他寄存器从实模式转变到保护模式。lss指令会把stack_start指向的内存地址的前四字节装入ESP寄存器,后两字节装入SS段寄存器。
lss stack_start,%esp是将结构体 stact_start 的值传送到ss:esp,即令 ss=0x10(段选择符)和 esp=& user_stack [PAGE_SIZE>>2]。
long user_stack [ PAGE_SIZE>>2 ] ;
struct {
long * a;
short b;
} stack_start = { & user_stack [PAGE_SIZE>>2] , 0x10 };
设置栈地址,栈顶指针指向user_stack数据结构的最末位置,代码在kernel/sched.c中定义。
注意运算符的优先级,&user_stack [PAGE_SIZE>>2]实际上就是&(user_stack [PAGE_SIZE>>2]),一般来说访问数组最后一项是user_stack [PAGE_SIZE>>2 -1 ],下一项就是user_stack [PAGE_SIZE>>2],要是访问这个地方就会发生数组访问越界,所以这个地方也就代表是栈的边界,也就是栈底。
设置中断描述符表
将中断描述符表idt设置成具有256项,并都指向ignore_int,等着main程序启动后,再次重新设置。
setup_idt:
/* 将ignore_int的偏移地址赋值给edx寄存器 */
lea ignore_int,%edx
/* 段选择符放入eax的高16位 */
movl $0x00080000,%eax
/* 把 ignore_int的偏移地址送入eax,根据段描述符的结构,0-15位就是偏移地址 */
movw %dx,%ax /* selector = 0x0008 = cs */
movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
/* 加载中断描述符表地址 */
lea idt,%edi
/* 256个中断描述符,用于循环 */
mov $256,%ecx
rp_sidt:
/* (%edi)表示%es:%edi,因为段寄存器es存的是段选择符0x10 */
/* 表示把%eax中的数据送入(%edi)指向的内存(4字节) */
/* 结合中断门(interrupt gate)数据结构,偏移地址就是ignore_int的地址,0x0008就是段选择符 */
movl %eax,(%edi)
/* 一个段描述符是8字节,上面的也就填充了4字节,%edx的数据送入4(%edi)内存地址 */
movl %edx,4(%edi)
/* 下一个段描述符 */
addl $8,%edi
/* 循环减1 */
dec %ecx
/* jne 表示zf=0跳转 */
jne rp_sidt
/* 加载中断描述符表的值 */
lidt idt_descr
ret
图片来源英特尔® 64 位和 IA-32 架构开发人员手册:卷 3A 197页
中断描述符为64位,包含了其对应中断服务程序的段内偏移地址(Offset)、所在段选择符(Segment Selector)、段特权级(DPL)、段存在标志(P)、段描述符类型(Type),比如中断描述符对应的类型标志位1110(0xE),即中断门。
idt: .fill 256,8,0 # idt is uninitialized
定义中断描述符表,一共256个,每个项占据8字节,里面全是0。
这是重建保护模式下中断服务体系的开始,程序先让所有的中断描述符默认指向ignore_int这个位置(将来main函数里面还要让中断描述符对应具体的中断服务程序),之后还要对中断描述符表寄存器的值进行设置。
ignore_int:
/* 压栈,保存数据 */
pushl %eax
pushl %ecx
pushl %edx
push %ds
push %es
push %fs
/* 段选择符 */
movl $0x10,%eax
mov %ax,%ds
mov %ax,%es
mov %ax,%fs
/* 把调用printk函数的参数地址入栈 */
pushl $int_msg
/* 该函数在kernel/printk.c中 */
call printk
popl %eax
pop %fs
pop %es
pop %ds
popl %edx
popl %ecx
popl %eax
/* 中断返回,把中断调用时压入栈的CPU标志寄存器值也弹出 */
iret
先构造中断描述符表,并使所有中断服务程序指向同一段只显示一行提示信息就返回的服务程序,先使中断机制的整体架构搭建起来。
注意,ds、es、fs、gs、等虽然是16位的寄存器,但入栈后仍然会以32位的形式入栈,也即需要占用4字节的堆栈空间。
idt_descr:
.word 256*8-1 # idt contains 256 entries
.long idt
中断描述符,256个段描述符,一个段描述符占据8字节,一共有256*8个字节
设置全局描述符表
重新设置一个新的GDT,此时仅创建了2个标下表项,GDT的第一项总是设为0,这就确保空段选择符的逻辑地址会被认为是无效的,因此引起一个处理异常。
/*
* setup_gdt
*
* This routines sets up a new gdt and loads it.
* Only two entries are currently built, the same
* ones that were built in init.s. The routine
* is VERY complicated at two whole lines, so this
* rather long comment is certainly needed :-).
* This routine will beoverwritten by the page tables.
*/
setup_gdt:
lgdt gdt_descr
ret
gdt_descr:
.word 256*8-1 # so does gdt (not that that's any
.long gdt # magic number, but it works for me :^)
gdt: .quad 0x0000000000000000 /* NULL descriptor */
.quad 0x00c09a0000000fff /* 16Mb */
.quad 0x00c0920000000fff /* 16Mb */
.quad 0x0000000000000000 /* TEMPORARY - don't use */
.fill 252,8,0 /* space for LDT's and TSS's etc */
head废除已有的GDT,并在内核中的新位置重新创建一个全新的全局描述符表,注意最后的3个FFF,说明段限长不是原来的8MB,而是现在的16MB。
因为GDT被重新修改,主要是限长增加了1倍,变为16MB,需要对一些段选择符进行重新设置
movl $0x10,%eax # reload all the segment registers
mov %ax,%ds # after changing gdt. CS was already
mov %ax,%es # reloaded in 'setup_gdt'
mov %ax,%fs
mov %ax,%gs
检查 A20 是否已打开
A20 是否已打开影响到保护模式是否有效
1: incl %eax # check that A20 really IS enabled
movl %eax,0x000000 # loop forever if it isn't
cmpl %eax,0x100000
/*1b 表示向后 (backward)跳转到标号1处*/
je 1b
采用的方法是向内存地址 0x000000 处写入任意一个数值,然后看内存地址 0x100000 (1M)处是否也是这个数值。如果一直相同的话,就一直比较下去,即死循环、死机,表示 A20 线没有选通,结果内核就不能够使用 1MB 以上内存。
检查 x87 协处理器
/*
* NOTE! 486 should set bit 16, to check for write-protect in supervisor
* mode. Then it would be unnecessary with the "verify_area()"-calls.
* 486 users probably want to set the NE (#5) bit also, so as to use
* int 16 for math errors.
*/
movl %cr0,%eax # check math chip
andl $0x80000011,%eax # Save PG,PE,ET
/* "orl $0x10020,%eax" here for 486 might be good */
orl $2,%eax # set MP
movl %eax,%cr0
call check_x87
jmp after_page_tables
/*
* We depend on ET to be correct. This checks for 287/387.
*/
check_x87:
fninit
fstsw %ax
cmpb $0,%al
je 1f /* no coprocessor: have to set bits */
movl %cr0,%eax
xorl $6,%eax /* reset MP, set EM */
movl %eax,%cr0
ret
/*
* 4字节对齐,这个2是2的指数
*/
.align 2
1: .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
ret
为了弥补 x86 系列在进行浮点计算时的不足,Intel 于 1980 年推出了 x87 系列数学协处理器,那时是一个外置的、可选的芯片。1989 年,Intel 发布了 486 处理器。自从 486 开始,以后的 CPU 一般都内置了协处理器。这样,对于 486 以前的计算机而言,操作系统检验 x87 协处理器是否存在就非常有必要了。
方法是修改控制寄存器CR0,在假设存在协处理器的情况下执行一个协处理器指令,如果出错的话则说明协处理器芯片不存在,需要设置CR0中的协处理器仿真位EM,并复位协处理器存在的标志MP。
设置分页管理系统
/*
* I put the kernel page tables right after the page directory,
* using 4 of them to span 16 Mb of physical memory. People with
* more than 16MB will have to expand this.
*/
.org 0x1000
pg0:
.org 0x2000
pg1:
.org 0x3000
pg2:
.org 0x4000
pg3:
.org 0x5000
/*
* tmp_floppy_area is used by the floppy-driver when DMA cannot
* reach to a buffer-block. It needs to be aligned, so that it isn't
* on a 64kB border.
*/
tmp_floppy_area:
.fill 1024,1,0
after_page_tables:
/* 为调用main方法做准备*/
pushl $0 # These are the parameters to main :-)
pushl $0
pushl $0
pushl $L6 # return address for main, if it decides to.
pushl $main
jmp setup_paging
L6:
jmp L6 # main should never return here, but
# just in case, we know what happens.
.......
setup_paging:
movl $1024*5,%ecx /* 5 pages - pg_dir+4 page tables */
xorl %eax,%eax
xorl %edi,%edi /* pg_dir is at 0x000 */
cld;rep;stosl
/* 因为内核共有4个页表所以只需要设置4项*/
movl $pg0+7,pg_dir /* set present bit/user r/w */
movl $pg1+7,pg_dir+4 /* --------- " " --------- */
movl $pg2+7,pg_dir+8 /* --------- " " --------- */
movl $pg3+7,pg_dir+12 /* --------- " " --------- */
movl $pg3+4092,%edi
movl $0xfff007,%eax /* 16Mb - 4096 + 7 (r/w user,p) */
/*方向位置位,edi值递减*/
std
1: stosl /* fill pages backwards - more efficient :-) */
subl $0x1000,%eax
/*1b 表示向后 (backward)跳转到标号1处*/
jge 1b
xorl %eax,%eax /* pg_dir is at 0x0000 */
movl %eax,%cr3 /* cr3 - page directory start */
movl %cr0,%eax
orl $0x80000000,%eax
movl %eax,%cr0 /* set paging (PG) bit */
ret /* this also flushes prefetch-queue */
cld相对应的指令是std,二者均是用来操作方向标志位DF(Direction Flag)。cld使DF 复位,即是让DF=0,std使DF置位,即DF=1.这两个指令用于串操作指令中。通过执行cld或std指令可以控制方向标志DF,决定内存地址是增大(DF=0,向高地址增加)还是减小(DF=1,向地地址减小)。
stosl指令相当于将eax中的值保存到es:edi指向的地址中,若设置了EFLAGS中的方向位置位(即在stosl指令前使用std指令)则edi自减4,否则(使用cld指令)edi自增4。cld设置edi或同esi为递增方向,rep做(%ecx)次重复操作,stosl表示edi每次增加4,这条语句达到按4字节清空前510244字节地址空间的目的。
$pg0+7表示:0x00001007,是页目录表中的第1项
movl %eax,%cr3设置页目录基址寄存器CR3的值,指向页目录表。页目录表在0x0000处。
movl %eax,%cr0 设置启动使用分页处理,CR0的PG标志置位
图片来源英特尔® 64 位和 IA-32 架构开发人员手册:卷 3A 第76页