標籤:style 檔案 資料 os art linux
x86系列CPU可以在16位實模式和32位保護模式下運行,實模式的特點是地址匯流排只有20位,也就是只有1MB的定址空間,為了相容老的CPU,Intel x86系列CPU包括最新的CPU在上電時都運行在16位的實模式下,同時在硬體上強行將CS置成0xF000,IP置成0xFFF0,那麼CS:IP就指向0xFFFF0這個地址,也就是上電瞬間代碼從該處開始執行,而BIOS恰恰就儲存在這個地方,可以想象一下,如果連BIOS都沒有將會是一個什麼結果。
BIOS程式被儲存在電腦主板上的一塊ROM晶片裡,首先BIOS程式會將啟動盤中的第一個扇區512位元組內容讀取到記憶體的0x7C00地址處,並跳轉到這個地方,而這個扇區內容正是boot/bootsect.s,這樣CPU的控制權將從BOIS轉移到作業系統這邊來,來看代碼:
entry startstart: mov ax,#BOOTSEGmov ds,axmov ax,#INITSEGmov es,axmov cx,#256sub si,sisub di,direpmovwjmpigo,INITSEG
BOOTSEG值為0x07c0,INITSEG值為0x9000,然後由movw指令將地址0x07C00的512位元組內容複寫到0x90000地址處,然後跳轉到go這個標號處。
go: mov ax,csmov ds,axmov es,ax! put stack at 0x9ff00.mov ss,axmov sp,#0xFF00 ! arbitrary value >>512
從jmpi指令處可以看出段地址即cs的值為0x9000,那麼這裡ds、es和ss寄存器的值都為0x9000,sp的值為0xFF00,即棧指標指向了地址0x9FF00處。
第一階段裝載已經執行完成,接下來將執行第二階段setup的裝載,代碼如下:
! load the setup-sectors directly after the bootblock.! Note that ‘es‘ is already set up.load_setup: mov dx,#0x0000 ! drive 0, head 0mov cx,#0x0002 ! sector 2, track 0mov bx,#0x0200 ! address = 512, in INITSEGmov ax,#0x0200+SETUPLEN ! service 2, nr of sectorsint 0x13 ! read itjnc ok_load_setup ! ok - continuemov dx,#0x0000mov ax,#0x0000 ! reset the disketteint 0x13j load_setup
第二階段的setup代碼從第2個扇區開始共4個扇區,通過int 0x13的中斷處理函數將這4個扇區代碼複製到地址0x90200,也就是緊跟在bootsect代碼後面,如果複製成功,跳轉到符號ok_load_setup處。
ah = 2表示讀扇區al讀扇區數ch磁軌號cl扇區號dh磁頭號(對於磁碟片即面號)dl磁碟機代號、例如0: 軟碟機A 1: 軟碟機Bes:bx指向接收資料的緩衝區
接下來是ok_load_setup處,代碼如下:
ok_load_setup:! Get disk drive parameters, specifically nr of sectors/trackmov dl,#0x00mov ax,#0x0800 ! AH=8 is get drive parametersint 0x13mov ch,#0x00seg csmov sectors,cxmov ax,#INITSEGmov es,ax
調用int 13h中斷常式讀取磁碟機參數,這裡主要是讀取磁軌最大扇區數,讀取結果包存在cl寄存器的低6位(0~5),然後將cx寄存器值儲存在sectors標號處,重設es寄存器值為0x9000。
! Print some inane message mov ah,#0x03 ! read cursor posxor bh,bhint 0x10mov cx,#24mov bx,#0x0007 ! page 0, attribute 7 (normal)mov bp,#msg1mov ax,#0x1301 ! write string, move cursorint 0x10
首先調用int 10h的中斷常式讀取游標的位置,然後還是調用int 10h的中斷常式顯示一條字串"Loading system ..."。這裡之所以要顯示這麼一條資訊,是因為接下來載入system部分時間比較長,為了防止在載入期間使用者誤認為是機器故障而執行不適當的操作而顯示了這麼一條資訊。
已經顯示了一條資訊,接下來就是載入system部分到記憶體0x10000地址處。
! ok, we‘ve written the message, now! we want to load the system (at 0x10000) mov ax,#SYSSEG mov es,ax ! segment of 0x010000 call read_it call kill_motor
調用read_it子函數完成載入。
! This routine loads the system at address 0x10000, making sure! no 64kB boundaries are crossed. We try to load it as fast as! possible, loading whole tracks whenever we can.!! in: es - starting address segment (normally 0x1000)!sread: .word 1+SETUPLEN ! sectors read of current trackhead: .word 0 ! current headtrack: .word 0 ! current trackread_it: mov ax,es test ax,#0x0fffdie: jne die ! es must be at 64kB boundary xor bx,bx ! bx is starting address within segmentrp_read: mov ax,es cmp ax,#ENDSEG ! have we loaded all yet? jb ok1_read retok1_read: seg cs mov ax,sectors sub ax,sread mov cx,ax shl cx,#9 add cx,bx jnc ok2_read je ok2_read xor ax,ax sub ax,bx shr ax,#9ok2_read: call read_track mov cx,ax add ax,sread seg cs cmp ax,sectors jne ok3_read mov ax,#1 sub ax,head jne ok4_read inc trackok4_read: mov head,ax xor ax,axok3_read: mov sread,ax shl cx,#9 add bx,cx jnc rp_read mov ax,es add ax,#0x1000 mov es,ax xor bx,bx jmp rp_readread_track: push ax push bx push cx push dx mov dx,track mov cx,sread inc cx mov ch,dl mov dx,head mov dh,dl mov dl,#0 and dx,#0x0100 mov ah,#2 int 0x13 jc bad_rt pop dx pop cx pop bx pop ax retbad_rt: mov ax,#0 mov dx,#0 int 0x13 pop dx pop cx pop bx pop ax jmp read_track
從上面這一大段可以看到,主要是通過int 13h中斷常式將system部分載入到記憶體位址0x10000處,system大小是192k。system部分載入完成之後,調用kill_motor子函數。
/* * This procedure turns off the floppy drive motor, so * that we enter the kernel in a known state, and * don‘t have to worry about it later. */kill_motor:push dxmov dx,#0x3f2mov al,#0outbpop dxret
kill_motor用於關閉軟碟機馬達。
! After that we check which root-device to use. If the device is! defined (!= 0), nothing is done and the given device is used.! Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending! on the number of sectors that the BIOS reports currently. seg csmov ax,root_devcmp ax,#0jne root_definedseg csmov bx,sectorsmov ax,#0x0208 ! /dev/ps0 - 1.2Mbcmp bx,#15je root_definedmov ax,#0x021c ! /dev/PS0 - 1.44Mbcmp bx,#18je root_definedundef_root:jmp undef_rootroot_defined:seg csmov root_dev,ax
判斷根檔案系統裝置是否被定義,如果已經被定義,則跳轉到root_defined符號處,如果沒有定義,然後根據磁軌扇區數,來定義根檔案系統裝置,最後回寫到root_dev處。
! after that (everyting loaded), we jump to! the setup-routine loaded directly after! the bootblock: jmpi 0,SETUPSEG
程式已經載入完成,根檔案系統裝置也已確定,跳轉到setup代碼處(0x90200)。
總結一下,這部分代碼主要是將作業系統三個階段代碼從啟動盤載入到記憶體中,最後跳轉到第二階段代碼處。
參考書籍:
1. Linux核心設計的藝術
2. Linux核心完全注釋 v3.0