/*
* 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.
*/
//預備知識
//在看本程序以前 咱們須要搞懂邏輯地址 虛擬地址 物理地址 以及他們之間的關係
//參考:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=2083672
//NOTE:這個程序比較好懂 只需熟悉x86的保護模式相關硬件 熟悉相關的AT&T語法
//建議:我有一本比較好的書 <<80386保護模式下編程>> 須要的在評論裏留下郵箱 我發給你
/*
該程序功能總結:
1. 設置內核堆棧;
2. 設置中斷描述符表idt;
3. 設置全局描述符表gdt;
4. 設置頁目錄表和頁表;
5. 將/init/main.c程序的入口地址預先壓入堆棧中,並在隨後利用返回指令彈出該地址,去執行main()程序。
*/
.text
.globl _idt,_gdt,_pg_dir,_tmp_floppy_area
_pg_dir:
startup_32:
//注意:應在保護模式下(經過GDT)思考內存的問題
movl $0x10,%eax
mov %ax,%ds ;把代碼中的段選擇子設置爲特權級爲零,指向GDT,GDT的第二項
mov %ax,%es ;注意參考setup.S中設置的相關GDTR和GDT的值
mov %ax,%fs
mov %ax,%gs
lss _stack_start,%esp ;lss指令表示把stack_start->ss:esp ss爲0x10 esp爲0x1e25c
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 //由於從新加載了GDT IDT因此須要從新加載段選擇子
mov %ax,%gs
lss _stack_start,%esp
xorl %eax,%eax
//下面開始驗證A20是否開啓
//具體方法是讓
//若是沒有開啓A20那麼[0x0]=[0x100000](1M)=eax 那麼就陷入死循環
1: incl %eax # check that A20 really IS enabled
movl %eax,0x000000 # loop forever if it isn't
cmpl %eax,0x100000 ;這裏的0x0 和 0x100000就是內存中那個地址
je 1b //b表示backward 向後跳轉1 標號能夠有0-10 f表示forward
/*
* 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
/////////////////////////////////////head.S完////////////////////////////////
/*
* We depend on ET to be correct. This checks for 287/387.
*/
check_x87:
fninit //init協處理器
fstsw %ax //將協處理器的狀態字發送到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
.align 2
//這裏是機器碼
1: .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
ret
/*
* setup_idt
*
* sets up a idt with 256 entries pointing to
* ignore_int, interrupt gates. It then loads
* idt. Everything that wants to install itself
* in the idt-table may do so themselves. Interrupts
* are enabled elsewhere, when we can be relatively
* sure everything is ok. This routine will be over-
* written by the page tables.
*/
setup_idt:
lea ignore_int,%edx //lea/lds/lfs/lss/lgs等指令
movl $0x00080000,%eax
movw %dx,%ax /* selector = 0x0008 = cs */
movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
lea _idt,%edi
mov $256,%ecx
rp_sidt:
movl %eax,(%edi)
movl %edx,4(%edi)
addl $8,%edi
dec %ecx
jne rp_sidt
lidt idt_descr
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_descr
ret
/*
* 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:
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.
/* This is the default interrupt "handler" :-) */
int_msg:
.asciz "Unknown interrupt\n\r"
.align 2
//調用內核函數printk顯示int_msg
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
pushl $int_msg
call _printk ;該函數在/kernel/printk.c中
popl %eax
pop %fs
pop %es
pop %ds
popl %edx
popl %ecx
popl %eax
iret
/*
* Setup_paging
*
* This routine sets up paging by setting the page bit
* in cr0. The page tables are set up, identity-mapping
* the first 16MB. The pager assumes that no illegal
* addresses are produced (ie >4Mb on a 4Mb machine).
*
* NOTE! Although all physical memory should be identity
* mapped by this routine, only the kernel page functions
* use the >1Mb addresses directly. All "normal" functions
* use just the lower 1Mb, or the local data space, which
* will be mapped to some other place - mm keeps track of
* that.
*
* For those with more memory than 16 Mb - tough luck. I've
* not got it, why should you :-) The source is here. Change
* it. (Seriously - it shouldn't be too difficult. Mostly
* change some constants etc. I left it at 16Mb, as my machine
* even cannot be extended past that (ok, but it was cheap :-)
* I've tried to show which constants to change by having
* some kind of marker at them (search for "16Mb"), but I
* won't guarantee that's all :-( )
*/
.align 2
/*設置頁面後 其內存地址空間以下
...
...
...
main.c
gdt(2K)
idt(2K)
head.s部分代碼
0x5000 軟盤緩衝區 即上面的(tmp_floppy_erea)
0x4000 頁表3 (如下代碼都是覆蓋head.s自己來建立的 須要常駐內存)
...
...
0x1000 頁表0
0x0000 頁目錄(4kB)
*/
setup_paging:
movl $1024*5,%ecx /* 5 pages - pg_dir+4 page tables */ //1K+4K
xorl %eax,%eax
xorl %edi,%edi /* pg_dir is at 0x000 */
cld;rep;stosl //清方向位 循環
//這裏是設置頁目錄 而且設置每一個頁表都是 bit/user r/w的
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 /* --------- " " --------- */
//這裏設置的是4個頁表項
movl $pg3+4092,%edi
movl $0xfff007,%eax /* 16Mb - 4096 + 7 (r/w user,p) */
std
//填充未填滿的空間
1: stosl /* fill pages backwards - more efficient :-) */
subl $0x1000,%eax //stosl指令將eax的值保存到es:edi指向的空間 stosl會自動調整edi
jge 1b
//cr3寄存器中包含頁的基址 設爲0x0
xorl %eax,%eax /* pg_dir is at 0x0000 */
movl %eax,%cr3 /* cr3 - page directory start */
//設置cr0 開啓分頁機制
movl %cr0,%eax
orl $0x80000000,%eax
movl %eax,%cr0 /* set paging (PG) bit */
ret /* this also flushes prefetch-queue */ //跳到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 /* TEMPORARY - don't use */
.fill 252,8,0 /* space for LDT's and TSS's etc */
php