The Bootstrap the Where Are Wenow?
Total Page:16
File Type:pdf, Size:1020Kb
Dedicated Operating System March2011 Prof. Dr.Antônio AugustoFröhlich http://www.lisha.ufsc.br/Guto Getting ready for the OS for the ready Getting [email protected] LISHA/UFSC The Boot: The March 2011 (http://www.lisha.ufsc.br) 1 Dedicated Operating System March2011 ● ● ● BIOSgot the system ready for BIOSbrought the system on ● Lots of “jmp” so far, no calls, why? ● First instruction fetched ● initializedBIOS a complex architecture Where the stack?Where is 0x7c00 BIST, POST, hooks The Bootstrap Where are we now? we are Where (http://www.lisha.ufsc.br) =>Today class 2 EPOS Bootstrap: m src/boot/pc_boot.S e t ; CONSTANTS s ;============================================================ y ; PHYSICAL MEMORY MAP S ; 0x0000 0000 -+-----------------------+ BOOT_IDT ; | IDT (4 K) | g ; 0x0000 1000 -+-----------------------+ BOOT_GDT ; | GDT (4 K) | n i ; 0x0000 2000 -+-----------------------+ t ; : : a ; | BOOT STACK (23 K) | r ; 0x0000 7c00 -+-----------------------+ BOOTSTRAP_STACK e ; | BOOT CODE (512 b) | BOOTSTRAP_CODE p ; 0x0000 7e00 -+-----------------------+ ; | RESERVED (512 b) | O ; 0x0000 8000 -+-----------------------+ DISK_IMAGE ; | DISK IMAGE (608 K) | d ; : : e ; | | t ; 0x000a 0000 -+-----------------------+ a ; | UNUSED (384K) | c ; : : i ; | | d ; 0x000f f000 -+-----------------------+ e D March 2011 (http://www.lisha.ufsc.br) 3 EPOS Bootstrap: Notes m e t s Code to be ran at real mode (16 bits) y S Interrupts (IDT) ● g At real mode, always at 0x0000 n ● i At protected mode, anywhere (IDTR) t a Segmentation (GDT) r ● e Always on at x86, in any mode p ● GDTR O Bootstrap code d ● e Code starts at 0x7c00 t ● a Stack stays bellow 0x7c00 c ● i It uses BIOS to access discs and load system d e D March 2011 (http://www.lisha.ufsc.br) 4 Dedicated Operating System main: main entry March2011 EPOS Bootstrap: main point entry EPOS Bootstrap: main Basic segmentsinitialization mov sp,#BOOTSTRAP_STACK ; set stack pointer stack set ; sp,#BOOTSTRAP_STACK mov ss,ax mov es,ax mov ds,ax mov 0x00000 = base segment data ; ax,ax xor interrupts disable ; cli (http://www.lisha.ufsc.br) 5 EPOS Bootstrap: disc image layout ; DISK IMAGE LAYOUT ; -+-----------------------+ DISK_IMAGE_SYS_INFO m ; | SYS_INFO (512 bytes) | e t ; -+-----------------------+ DISK_IMAGE_SETUP s ; | SETUP | y ; : : S ; -+-----------------------+ ; | SYSTEM | g ; : : n i ; -+-----------------------+ t ; | INIT | a r ; : : e ; -+-----------------------+ p ; | LOADER/APP1 | ; : : O ; -+-----------------------+ d ; | APP1 | e ; : : t ; -+-----------------------+ a ; : : c i ; -+-----------------------+ d ; | APPn | e ; : : D ; -+-----------------------+ March 2011 (http://www.lisha.ufsc.br) 6 EPOS Bootstrap: loading image from disc m e ; Load the boot image from the disk into "DISK_IMAGE" t mov si,#msg_loading s call print_msg y push es S mov ax,#IMAGE_SEG mov es,ax ; don©t try to load es directly g mov bx,#0 ; set es:bx to DISK_IMAGE n mov ax,[n_sec_image] i t mov cx,#0x0002 ; starts at track #0, sector #2, a mov dx,#0x0000 ; side #0, first drive r call load_image e pop es p mov si,#msg_done call print_msg O d ; Stop the drive motor e call stop_drive t a c Function calling (e.g., “print_msg”) i d ● Parameter passing e D March 2011 (http://www.lisha.ufsc.br) 7 EPOS Bootstrap: print_msg – BIOS access ;==================================================================== ; PRINT_MSG = m ; = e ; Desc: Print a \0 terminated string on the screen using the BIOS = t ; Message must end with 00h ; = s ; = y ; Parm: si -> pointer to the string = S ;==================================================================== print_msg: g pushf push ax n i push bx t push bp BIOS access a cld r ● “int 0x10” e print_char: lodsb p CISC magics cmp al,#0 O jz end_print ● “cld” mov ah,#0x0E d mov bx,#0x0007 ● “lodsb” e int 0x10 t jmp print_char a c end_print: i pop bp d pop bx pop ax e popf D ret March 2011 (http://www.lisha.ufsc.br) 8 EPOS Bootstrap: load_sector – BIOS access ;========================================================================= ; LOAD_ONE_SECTOR = m ; = e ; Desc: Load a single sector from disk using the BIOS. = t ; = s ; Parm: es:bx -> buffer = y ; cx -> track (ch) and sector number (cl) = S ; dx -> side (dh) and drive number (dl) = ;========================================================================= g load_sector: pushf n i push ax t a mov ax,#0x0201 ; function #2, load 1 sector r int 0x13 e cli ; int 0x13 sets IF jc ls_disk_error ; if CY=1, error on reading p O pop ax popf d ret e t ls_disk_error: a mov si,#msg_disk_error c call print_msg ; print error msg if disk is bad i call stop_drive d ls_disk_halt: “method signatures” e jmp ls_disk_halt ; halt D depend on BIOS API March 2011 (http://www.lisha.ufsc.br) 9 EPOS Bootstrap: loading image from disc m e ; Load the boot image from the disk into "DISK_IMAGE" t mov si,#msg_loading s call print_msg y push es S mov ax,#IMAGE_SEG mov es,ax ; don©t try to load es directly g mov bx,#0 ; set es:bx to DISK_IMAGE n mov ax,[n_sec_image] i t mov cx,#0x0002 ; starts at track #0, sector #2, a mov dx,#0x0000 ; side #0, first drive r call load_image e pop es p mov si,#msg_done call print_msg O d ; Stop the drive motor e call stop_drive t a c Print => Load Image => Print => Stop Drive i d Why stop drive? e D March 2011 (http://www.lisha.ufsc.br) 10 EPOS Bootstrap: memory initialization m e ; Get extended memory size (in K) t ; xor dx,dx s ; mov ah,#0x88 y ; int 0x15 ; what if memory size > 64 Mb? S ; push ds ; push #INFO_SEG g ; pop ds BIOS info on memory is n ; mov [0],ax i t ; mov [2],dx unreliable a ; pop ds r EPOS will test memory e ; Say hello; by itself p mov si,#msg_hello call print_msg O A20 line d ; Enable A20 bus line ● DOS access data and IO in e call enable_a20 t one segment until 80186 a ● c 80286 needed this line to i access 16MB memory, so d e we have this legacy D March 2011 (http://www.lisha.ufsc.br) 11 EPOS Bootstrap: entering protected mode m e ; Zero IDT and GDT t cld s xor ax,ax y mov cx,#0x1000 ; IDT + GDT = 8K (4K WORDS) S mov di,#BOOT_IDT ; initial address (relative to ES) rep ; zero IDT and GDT with AX g stosw n i t ; Set GDT a mov si,#GDT_CODE ; Set GDT[1]=GDT_CODE and r mov di,#BOOT_GDT ; GDT[2]=GDT_DATA e add di,#8 ; offset GDT[1] = 8 p mov cx,#8 ; sizeof GDT[1] + GDT[2] = 8 WORDS rep ; move WORDS O movsw d Protected mode e ; Set GDTR t lgdt GDTR configured a c ; Enable Protected Mode But not entered i mov eax,cr0 d or al,#0x01 ; set PE flag and MP flag yet... e mov cr0,eax D March 2011 (http://www.lisha.ufsc.br) 12 EPOS Bootstrap: breaking the pre-fetch queue m ; Adjust selectors e mov bx,#2 * 8 ; adjust data selectors to use t mov ds,bx ; GDT[2] (DATA) with RPL = 00 s mov es,bx y mov fs,bx S mov gs,bx mov ss,bx g n i ; As Linux as86 can©t generate 32 bit instructions, we have to code t ; it by hand. The instruction below is a inter segment jump to a ; GDT[GDT_CODE]:SETUP. Jump into "SETUP" (actually ix86 Protected r ; Mode starts here) e ; jmp 0x0008:#SETUP_ENTRY p .byte 0x66 .byte 0xEA O .long SETUP_ENTRY d .word 0x0008 e t a Need to break the pre-fetch queue c ● i Force CPU to read configuration registers d LDT and IDT not assempled yet e ● D Next class: pc_setup.cc March 2011 (http://www.lisha.ufsc.br) 13.