- 重新設置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 */