linux0.12 head.s代码解析
- 重新设置IDT和GDT,为256个中断门设置默认的中断处理函数
- 检查A20地址线是否启用
- 设置数学协处理器
- 将main函数相关的参数压栈
- 设置分页机制,将页表映射到0~16MB的物理内存上
- 返回main函数执行
源码详细注释如下:
/** linux/boot/head.s** (C) 1991 Linus Torvalds*//** head.s contains the 32-bit startup code.** NOTE!!! Startup happens at absolute address 0x00000000, which is also where* the page directory will exist. The startup code will be overwritten by* the page directory.*/
.text
.globl _idt,_gdt,_pg_dir,_tmp_floppy_area
_pg_dir:
startup_32:
# 段寄存器先指向setup.S中的数据段movl $0x10,%eaxmov %ax,%dsmov %ax,%esmov %ax,%fsmov %ax,%gslss _stack_start,%esp # 设置栈指针和栈段寄存器
# 设置IDT和GDTcall setup_idtcall setup_gdt
# 重新加载段寄存器和栈movl $0x10,%eaxmov %ax,%dsmov %ax,%esmov %ax,%fsmov %ax,%gslss _stack_start,%esp
# 检查A20地址线是否启用xorl %eax,%eax
1: incl %eax # check that A20 really IS enabledmovl %eax,0x000000 # loop forever if it isn'tcmpl %eax,0x100000je 1b
/** 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 chipandl $0x80000011,%eax # 清楚其他位,保留PG PE ET位orl $2,%eax # set MP位,表示启用数学协处理器movl %eax,%cr0 # 写回CR0寄存器call check_x87jmp after_page_tables# 设置协处理器
check_x87:fninit # 初始化协处理器fstsw %ax # 取协处理器状态字到ax寄存器cmpb $0,%alje 1f # 如果协处理器状态字为0,则有协处理器,跳转1movl %cr0,%eaxxorl $6,%eax # 清除MP、设置EM位movl %eax,%cr0ret
.align 2
1: .byte 0xDB,0xE4 # 等价于fsetpm,用于设置协处理器模式retsetup_idt:
/** %eax:* 位31-16: 0x0008 (段选择子)* 位15-0: ignore_int地址的低16位* %edx:* 位31-16: 0x8E00 (中断门属性)* 位15-0: ignore_int地址的高16位
*/lea ignore_int,%edxmovl $0x00080000,%eaxmovw %dx,%axmovw $0x8E00,%dxlea _idt,%edi # edi指向IDT的基地址mov $256,%ecx # 256个中断门
rp_sidt:movl %eax,(%edi)movl %edx,4(%edi)addl $8,%edi # 移动到下一个中断门描述符dec %ecx # 循环计数器减1jne rp_sidtlidt idt_descr # 加载IDT描述符ret/** 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_descrret/** 物理地址 内容 大小 用途* 0x0000 页目录(_pg_dir) 4KB 页目录表* 0x1000 页表0(pg0) 4KB 映射0-4MB物理内存* 0x2000 页表1(pg1) 4KB 映射4-8MB物理内存 * 0x3000 页表2(pg2) 4KB 映射8-12MB物理内存* 0x4000 页表3(pg3) 4KB 映射12-16MB物理内存* 0x5000 软盘缓冲区 4KB 软盘DMA缓冲区*/
.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,0after_page_tables:pushl $0pushl $0pushl $0 # 这些是main函数的参数pushl $L6 # 如果main函数决定返回,则返回地址为L6pushl $_main # main函数地址压栈jmp setup_paging # 跳转设置分页机制
L6:jmp L6 # main应该永远不会返回,以防万一,我们不知道会发生什么# 默认中断处理程序
int_msg:.asciz "Unknown interrupt\n\r"
.align 2
ignore_int:pushl %eaxpushl %ecxpushl %edxpush %dspush %espush %fsmovl $0x10,%eaxmov %ax,%dsmov %ax,%esmov %ax,%fspushl $int_msgcall _printkpopl %eaxpop %fspop %espop %dspopl %edxpopl %ecxpopl %eaxiret.align 2
setup_paging:
# 清零页目录和页表区域movl $1024*5,%ecxxorl %eax,%eaxxorl %edi,%edicld;rep;stosl
# 设置页目录项movl $pg0+7,_pg_dirmovl $pg1+7,_pg_dir+4movl $pg2+7,_pg_dir+8movl $pg3+7,_pg_dir+12
# 通过循环将页表映射到物理内存,从高地址向低地址填充movl $pg3+4092,%edimovl $0xfff007,%eaxstd
1: stoslsubl $0x1000,%eaxjge 1bxorl %eax,%eaxmovl %eax,%cr3 # 设置CR3寄存器,指向页目录的起始地址movl %cr0,%eaxorl $0x80000000,%eaxmovl %eax,%cr0 # 启用分页机制ret # 由于之前压栈了main,返回main函数执行.align 2
.word 0
idt_descr:.word 256*8-1 # idt contains 256 entries.long _idt
.align 2
.word 0
gdt_descr:.word 256*8-1 # so does gdt (not that that's any.long _gdt # magic number, but it works for me :^).align 3
_idt: .fill 256,8,0 # idt is uninitialized_gdt: .quad 0x0000000000000000 /* NULL descriptor */.quad 0x00c09a0000000fff /* 内核代码段 16Mb */.quad 0x00c0920000000fff /* 内核数据段 16Mb */.quad 0x0000000000000000 /* 暂时未使用 */.fill 252,8,0 /* space for LDT's and TSS's etc */