?? head.s
字號:
.org 0x2000pg1:.org 0x3000pg2:.org 0x4000pg3:.org 0x5000 # 定義下面的內存數據塊從偏移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.*//* 當DMA(直接存儲器訪問)不能訪問緩沖塊時,下面的tmp_floppy_area 內存塊* 就可供軟盤驅動程序使用。其地址需要對齊調整,這樣就不會跨越64kB 邊界。*/_tmp_floppy_area:.fill 1024,1,0 # 共保留1024 項,每項1 字節,填充數值0。# 下面這幾個入棧操作(pushl)用于為調用/init/main.c 程序和返回作準備。# 前面3 個入棧指令不知道作什么用的,也許是Linus 用于在調試時能看清機器碼用的?。# 139 行的入棧操作是模擬調用main.c 程序時首先將返回地址入棧的操作,所以如果# main.c 程序真的退出時,就會返回到這里的標號L6 處繼續執行下去,也即死循環。# 140 行將main.c 的地址壓入堆棧,這樣,在設置分頁處理(setup_paging)結束后# 執行'ret'返回指令時就會將main.c 程序的地址彈出堆棧,并去執行main.c 程序去了。after_page_tables:pushl $0 # These are the parameters to main :-)pushl $0 # 這些是調用main 程序的參數(指init/main.c)。pushl $0pushl $L6 # return address for main, if it decides to.pushl $_main # '_main'是編譯程序對main 的內部表示方法。jmp setup_paging # 跳轉至第198 行。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 # 按4 字節方式對齊內存地址。ignore_int:pushl %eaxpushl %ecxpushl %edxpush %ds # 這里請注意!!ds,es,fs,gs 等雖然是16 位的寄存器,但入棧后# 仍然會以32 位的形式入棧,也即需要占用4 個字節的堆棧空間。push %espush %fsmovl $0x10,%eax # 置段選擇符(使ds,es,fs 指向gdt 表中的數據段)。mov %ax,%dsmov %ax,%esmov %ax,%fspushl $int_msg # 把調用printk 函數的參數指針(地址)入棧。call _printk # 該函數在/kernel/printk.c 中。# '_printk'是printk 編譯后模塊中的內部表示法。popl %eaxpop %fspop %espop %dspopl %edxpopl %ecxpopl %eaxiret # 中斷返回(把中斷調用時壓入棧的CPU 標志寄存器(32 位)值也彈出)。/** 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 :-( )*//** 這個子程序通過設置控制寄存器cr0 的標志(PG 位31)來啟動對內存的分頁處理功能,* 并設置各個頁表項的內容,以恒等映射前16 MB 的物理內存。分頁器假定不會產生非法的* 地址映射(也即在只有4Mb 的機器上設置出大于4Mb 的內存地址)。* 注意!盡管所有的物理地址都應該由這個子程序進行恒等映射,但只有內核頁面管理函數能* 直接使用>1Mb 的地址。所有“一般”函數僅使用低于1Mb 的地址空間,或者是使用局部數據* 空間,地址空間將被映射到其它一些地方去 -- mm(內存管理程序)會管理這些事的。* 對于那些有多于16Mb 內存的家伙 - 太幸運了,我還沒有,為什么你會有?。代碼就在這里,* 對它進行修改吧。(實際上,這并不太困難的。通常只需修改一些常數等。我把它設置為* 16Mb,因為我的機器再怎么擴充甚至不能超過這個界限(當然,我的機器很便宜的?)。* 我已經通過設置某類標志來給出需要改動的地方(搜索“16Mb”),但我不能保證作這些* 改動就行了??)。*/.align 2 # 按4 字節方式對齊內存地址邊界。setup_paging: # 首先對5 頁內存(1 頁目錄 + 4 頁頁表)清零movl $1024*5,%ecx /* 5 pages - pg_dir+4 page tables */xorl %eax,%eaxxorl %edi,%edi /* pg_dir is at 0x000 */# 頁目錄從0x000 地址開始。cld;rep;stosl# 下面4 句設置頁目錄中的項,我們共有4 個頁表所以只需設置4 項。# 頁目錄項的結構與頁表中項的結構一樣,4 個字節為1 項。參見上面113 行下的說明。# "$pg0+7"表示:0x00001007,是頁目錄表中的第1 項。# 則第1 個頁表所在的地址 = 0x00001007 & 0xfffff000 = 0x1000;# 第1 個頁表的屬性標志 = 0x00001007 & 0x00000fff = 0x07,表示該頁存在、用戶可讀寫。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 /* --------- " " --------- */# 下面6 行填寫4 個頁表中所有項的內容,共有:4(頁表)*1024(項/頁表)=4096 項(0 - 0xfff),# 也即能映射物理內存 4096*4Kb = 16Mb。# 每項的內容是:當前項所映射的物理內存地址 + 該頁的標志(這里均為7)。# 使用的方法是從最后一個頁表的最后一項開始按倒退順序填寫。一個頁表的最后一項在頁表中的# 位置是1023*4 = 4092。因此最后一頁的最后一項的位置就是$pg3+4092。movl $pg3+4092,%edi # edi??最后一頁的最后一項。movl $0xfff007,%eax /* 16Mb - 4096 + 7 (r/w user,p) */# 最后1 項對應物理內存頁面的地址是0xfff000,# 加上屬性標志7,即為0xfff007.std # 方向位置位,edi 值遞減(4 字節)。1: stosl /* fill pages backwards - more efficient :-) */subl $0x1000,%eax # 每填寫好一項,物理地址值減0x1000。jge 1b # 如果小于0 則說明全添寫好了。# 設置頁目錄基址寄存器cr3 的值,指向頁目錄表。xorl %eax,%eax /* pg_dir is at 0x0000 */ # 頁目錄表在0x0000 處。movl %eax,%cr3 /* cr3 - page directory start */# 設置啟動使用分頁處理(cr0 的PG 標志,位31)movl %cr0,%eaxorl $0x80000000,%eax # 添上PG 標志。movl %eax,%cr0 /* set paging (PG) bit */ret /* this also flushes prefetch-queue */# 在改變分頁處理標志后要求使用轉移指令刷新預取指令隊列,這里用的是返回指令ret。# 該返回指令的另一個作用是將堆棧中的main 程序的地址彈出,并開始運行/init/main.c 程序。# 本程序到此真正結束了。.align 2 # 按4 字節方式對齊內存地址邊界。.word 0idt_descr: #下面兩行是lidt 指令的6 字節操作數:長度,基址。.word 256*8-1 # idt contains 256 entries.long _idt.align 2.word 0gdt_descr: # 下面兩行是lgdt 指令的6 字節操作數:長度,基址。.word 256*8-1 # so does gdt (not that that's any.long _gdt # magic number, but it works for me :^).align 3 # 按8 字節方式對齊內存地址邊界。_idt: .fill 256,8,0 # idt is uninitialized # 256 項,每項8 字節,填0。# 全局表。前4 項分別是空項(不用)、代碼段描述符、數據段描述符、系統段描述符,其中# 系統段描述符linux 沒有派用處。后面還預留了252 項的空間,用于放置所創建任務的# 局部描述符(LDT)和對應的任務狀態段TSS 的描述符。# (0-nul, 1-cs, 2-ds, 3-sys, 4-TSS0, 5-LDT0, 6-TSS1, 7-LDT1, 8-TSS2 etc...)_gdt: .quad 0x0000000000000000 /* NULL descriptor */.quad 0x00c09a0000000fff /* 16Mb */ # 代碼段最大長度16M。.quad 0x00c0920000000fff /* 16Mb */ # 數據段最大長度16M。.quad 0x0000000000000000 /* TEMPORARY - don't use */.fill 252,8,0 /* space for LDT's and TSS's etc */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -