D ed ic ate d O p e ratin g S ys te m March 2011 March Prof. Dr.Antônio AugustoFröhlich http://www.lisha.ufsc.br/Guto Gettingready the for OS [email protected] LISHA/UFSC TheBoot: March 2011

(http://www.lisha.ufsc.br)

1 D ed ic ate d O p e ratin g S ys te m March 2011 March   ● ● ● BIOS got the system ready for ready system the got BIOS on system the brought BIOS ● why? calls, no far, “jmp”so of Lots ● fetched instruction First ● architecture complex a BIOSinitialized Where isWherestack? the 0x7c00 hooksPOST, BIST, The Bootstrap The Where are wenow?

(http://www.lisha.ufsc.br) => Today class =>Today 2 D ed ic ate d O p e ratin g S ys te m ; 0x0000 1000 ­+­­­­­­­­­­­­­­­­­­­­­­­+ BOOT_GDT ; | IDT (4 K) | ; 0x0000 0000 ­+­­­­­­­­­­­­­­­­­­­­­­­+ BOOT_IDT ; PHYSICAL MEMORY MAP ;======; CONSTANTS ; 0x000f f000 ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; | | ; : : ; | UNUSED (384K) | ; 0x000a 0000 ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; | | ; : : ; | DISK IMAGE (608 K) | ; 0x0000 8000 ­+­­­­­­­­­­­­­­­­­­­­­­­+ DISK_IMAGE ; | RESERVED (512 b) | ; 0x0000 7e00 ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; | BOOT CODE (512 b) | BOOTSTRAP_CODE ; 0x0000 7c00 ­+­­­­­­­­­­­­­­­­­­­­­­­+ BOOTSTRAP_STACK ; | BOOT STACK (23 K) | ; : : ; 0x0000 2000 ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; | GDT (4 K) | March 2011 March src/boot/pc_boot.S EPOS Bootstrap:

(http://www.lisha.ufsc.br) 3 D ed ic ate d O p e ratin g S ys te m March 2011 March     ● ● ● ● ● ● ● Bootstrap code Bootstrap (GDT) Segmentation (IDT) Interrupts bits) (16 mode at real ran to be Code Always on at , in any modeinany x86, at on Always (IDTR) anywhere mode, protected At 0x0000 mode,at real always At It uses BIOS to access discs and loadsystem and discs access BIOS uses to It 0x7c00 bellow stays Stack 0x7c00 at Codestarts GDTR EPOS Bootstrap:NotesEPOS

(http://www.lisha.ufsc.br) 4 D ed ic ate d O p e ratin g S ys te m main: entry main

March 2011 March  EPOS Bootstrap:mainEPOS entry point Basic segments initialization segments Basic mov sp,#BOOTSTRAP_STACK ; set stack pointer mov ss,ax mov es,ax mov ds,ax xor ax,ax ; data segment base = 0x00000 cli ; disable interrupts

(http://www.lisha.ufsc.br) 5 D ed ic ate d O p e ratin g S ys te m ; | SYSTEM | ; ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; : : ; | SETUP | ; ­+­­­­­­­­­­­­­­­­­­­­­­­+ DISK_IMAGE_SETUP ; | SYS_INFO (512 bytes) | ; ­+­­­­­­­­­­­­­­­­­­­­­­­+ DISK_IMAGE_SYS_INFO ; DISK IMAGE LAYOUT ; ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; : : ; | APPn | ; ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; : : ; ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; : : ; | APP1 | ; ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; : : ; | LOADER/APP1 | ; ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; : : ; | INIT | ; ­+­­­­­­­­­­­­­­­­­­­­­­­+ ; : : March 2011 March EPOS Bootstrap: disc imagedisc Bootstrap: EPOS layout

(http://www.lisha.ufsc.br) 6 D ed ic ate d O p e ratin g S ys te m ; Load the boot image from the disk into "DISK_IMAGE" ; Stop the drive motor March 2011 March  ● Function calling “print_msg”) (e.g., calling Function call stop_drive call print_msg mov si,#msg_done pop es call load_image mov dx,#0x0000 ; side #0, first drive mov cx,#0x0002 ; starts at track #0, sector #2, mov ax,[n_sec_image] mov bx,#0 ; set es:bx to DISK_IMAGE mov es,ax ; don't try to load es directly mov ax,#IMAGE_SEG push es call print_msg mov si,#msg_loading Parameter passing Parameter loadingimage from disc EPOS Bootstrap:

(http://www.lisha.ufsc.br) 7 D ed ic ate d O p e ratin g S ys te m print_msg: ;======; Parm: si ­> pointer to the string = ; = ; Message must end with 00h ; = ; Desc: Print a \0 terminated string on the screen using the BIOS = ; = ; PRINT_MSG = ;======end_print: print_char: March 2011 March lodsb cld push bp push bx push ax pushf ret popf pop ax pop bx pop bp jmp print_char int 0x10 mov bx,#0x0007 mov ah,#0x0E jz end_print cmp al,#0 print_msgBIOS access – EPOS Bootstrap:

(http://www.lisha.ufsc.br)   ● ● ● CISC magics CISC access BIOS “ “ “ lodsb” cld” 0x10” int 8 D ed ic ate d O p e ratin g S ys te m load_sector: ;======; dx ­> side (dh) and drive number (dl) = ; cx ­> track (ch) and sector number (cl) = ; Parm: es:bx ­> buffer = ; = ; Desc: Load a single sector from disk using the BIOS. = ; = ; LOAD_ONE_SECTOR = ;======ls_disk_halt: ls_disk_error: March 2011 March jc ls_disk_error ; if CY=1, error on reading cli ; int 0x13 sets IF int 0x13 mov ax,#0x0201 ; function #2, load 1 sector push ax pushf jmp ls_disk_halt ; halt call stop_drive call print_msg ; print error msg if disk is bad mov si,#msg_disk_error ret popf pop ax load_sector access – BIOS EPOS Bootstrap:

(http://www.lisha.ufsc.br)  depend on BIOS API BIOS on depend “ method signatures” signatures” method 9 D ed ic ate d O p e ratin g S ys te m ; Load the boot image from the disk into "DISK_IMAGE" ; Stop the drive motor March 2011 March   Why stop drive? stop Why Drive Stop => Print => Image =>Load Print call stop_drive call print_msg mov si,#msg_done pop es call load_image mov dx,#0x0000 ; side #0, first drive mov cx,#0x0002 ; starts at track #0, sector #2, mov ax,[n_sec_image] mov bx,#0 ; set es:bx to DISK_IMAGE mov es,ax ; don't try to load es directly mov ax,#IMAGE_SEG push es call print_msg mov si,#msg_loading loadingimage from disc EPOS Bootstrap:

(http://www.lisha.ufsc.br) 10 D ed ic ate d O p e ratin g S ys te m ; ; ; ; ; ; Get size (in K) call ; Enable A20 line call mov ; Say hello; ; ; ; ; March 2011 March si,#msg_hello pop mov mov pop push push int mov xor enable_a20 print_msg ds [2],dx [0],ax ds 0x15 ah,#0x88 dx,dx #INFO_SEG ds memoryinitialization EPOS Bootstrap: ; what if memory size > 64 Mb?    ● ● A20 line A20 itself by memory will test EPOS unreliable memory is on info BIOS

(http://www.lisha.ufsc.br) we have this legacy this wehave 16MB memory, so access to line this needed 80286 80186 until segment one IOinand data DOSaccess 11 D ed ic ate d O p e ratin g S ys te m ; Zero IDT and GDT ; Enable ; Set GDTR ; Set GDT March 2011 March mov cr0,eax or al,#0x01 mov eax,cr0 lgdt movsw rep ; move WORDS mov cx,#8 add di,#8 mov di,#BOOT_GDT mov si,#GDT_CODE stosw rep ; zero IDT and GDT with AX mov di,#BOOT_IDT mov cx,#0x1000 xor ax,ax cld GDTR entering protected mode ; set PE flag and MP flag EPOS Bootstrap: ; sizeof GDT[1] + GDT[2] = 8 WORDS ; offset GDT[1] = 8 ; GDT[2]=GDT_DATA ; Set GDT[1]=GDT_CODE and ; initial address (relative to ES) ; IDT + GDT = 8K (4K WORDS)

(http://www.lisha.ufsc.br)   yet... entered not But configured mode Protected 12 D ed ic ate d O p e ratin g S ys te m ; Adjust selectors ; jmp 0x0008:#SETUP_ENTRY ; Mode starts here) ; GDT[GDT_CODE]:SETUP. Jump into "SETUP" (actually ix86 Protected ; it by hand. The instruction below is a inter segment jump to ; As Linux as86 can't generate 32 bit instructions, we have to code March 2011 March   ● ● LDT and IDT not assempled yet assempled IDT not and LDT queue pre-fetch the to break Need Next class: pc_setup.cc class: Next registers ForceCPU readconfiguration to .word 0x0008 .long SETUP_ENTRY .byte 0xEA .byte 0x66 mov ss,bx mov gs,bx mov fs,bx mov es,bx mov ds,bx mov bx,#2 * 8 breaking thepre-fetch queue EPOS Bootstrap: ; GDT[2] (DATA) with RPL = 00 ; adjust data selectors to use

(http://www.lisha.ufsc.br) 13