USF CS 630 - Defining protected-mode segment-descriptors

Unformatted text preview:

Defining protected-mode segment-descriptorsWhat will we do once there?Recall PC Memory LayoutThree VRAM zonesArray of picture-elementsColor-Attribute ByteScreen-element locationsx86 “Little-Endian” storageDrawing a character-stringPlanning our memory usageVRAM segment-descriptorSlide 12Slide 13Slide 14Setting up the GDTLoading register LDTREntering protected-modeLeaving protected-modeDemo-programIn-class exercisesDefining protected-mode segment-descriptorsAn example of a protected-mode bootsector application that draws a message to the video displayWhat will we do once there?•Let’s explore writing a bootsector program that will do something perceptible while in protected-mode, namely: show a message•We won’t be able to call BIOS functions (they’re designed to work in real-mode)•We must write directly to video memoryRecall PC Memory LayoutRAM1-MBROM-BIOSVIDEO-BIOSVRAM0xA00000xC00000xF00000x00000Three VRAM zonesGRAPHICSMONOCHROME TEXTCOLOR TEXT64-KB32-KB32-KB0xA00000xB00000xB8000Array of picture-elements•Text-mode VRAM is organized as an array•Each array-element occupies one word •Word’s LSB holds ascii character-code•Word’s MSB holds a color-number pairbgcolor fgcolor ASCII character-code1512 11 8 70bytenybblenybbleColor-Attribute Byte BlinkR G BIntenseG BRforeground color attributebackground color attributeScreen-element locations80 columns 25rowscharacters 0..79characters 80..159Video screencharacters 1920..1999x86 “Little-Endian” storage•Intel’s x86 CPUs use little-endian storage•The “little end” of any multibyte value is stored at the smaller operand-address •Example: EAX = 0x12345678mov [0x9000], EAX Memory-addressesoccupied by operand0x120x340x560x78 0x9000 0x9001 0x9002 0x9003Drawing a character-string•Setup DS:SI with string’s starting address•Setup ES:DI with initial address on screen•Clear DF-bit (Direction Flag) in FLAGS register•Setup desired color attribute-byte in AH registeragain: lodsb ; next character to ALor al, al ; is final null-byte?jz finis ; yes, exit from loopstosw ; write char & colorsjmp again ; go back for anotherfinis:Planning our memory usage•To draw a screen-message in protected- mode, our program will need to address these memory-segments:–its code (executable, at 0x07C00)–its data (readable and writable, at 0x07C00)–its stack (readable, writable, expand-down)–the video ram (32KB, writable, at 0xB8000)•For its return to real-mode, our program will need 64KB code and data segmentsVRAM segment-descriptorBase[31..24] G DRSVAVLLimit[19..16]PDPLS XC/DR/WA Base[23..16]Base[15..0] Limit[15..0]31 16150VRAM Base-Address = 0x000B8000VRAM Segment-Limit = 0x07FFF (32-KB)Segment-attributes: P=1, A=0, S=1, X=0, D=0, W=1DPL=0, G=0, D=0 (RSV=0, AVL=0).WORD 0x7FFF, 0x8000, 0x920B, 0x0000CODE segment-descriptorBase[31..24] G DRSVAVLLimit[19..16]PDPLS XC/DR/WA Base[23..16]Base[15..0] Limit[15..0]31 16150CODE Base-Address = 0x00007C00CODE Segment-Limit = 0x0FFFF (64-KB)Segment-attributes: P=1, A=0, S=1, X=1, C=0, R=1DPL=0, G=0, D=0 (RSV=0, AVL=0).WORD 0xFFFF, 0x7C00, 0x9A00, 0x0000DATA segment-descriptorBase[31..24] G DRSVAVLLimit[19..16]PDPLS XC/DR/WA Base[23..16]Base[15..0] Limit[15..0]31 16150DATA Base-Address = 0x00007C00DATA Segment-Limit = 0x0FFFF (64-KB)Segment-attributes: P=1, A=0, S=1, X=0, D=0, W=1DPL=0, G=0, D=0 (RSV=0, AVL=0).WORD 0xFFFF, 0x7C00, 0x9200, 0x0000STACK segment-descriptorBase[31..24] G DRSVAVLLimit[19..16]PDPLS XC/DR/WA Base[23..16]Base[15..0] Limit[15..0]31 16150STACK Base-Address = 0x00007C00STACK Segment-Limit = 0x001FF (512-Bytes)Segment-attributes: P=1, A=0, S=1, X=0, D=1, W=1DPL=0, G=0, D=0 (RSV=0, AVL=0).WORD 0x01FF, 0x7C00, 0x9600, 0x0000Setting up the GDT•Base-Address must be quadword-aligned.ALIGN 8•NULL-Descriptor occupies first quadwardtheGDT: .WORD 0, 0, 0, 0•GDT base-address and segment-limit:base: #0x00007C00 + #theGDTlimit: 8 * (number of descriptors) - 1Loading register LDTR•We can load LDTR from our stack:mov eax, #0x00007C00 ; boot locationadd eax, #theGDT ; add GDT offsetmov dx, #0x27 ; five descriptorspusheax ; push bits 47..16pushdx ; push bits 15..0lgdt [esp] ; load 48-bit LDTRadd esp, #6 ; discard 3 wordsBASE_ADDRESS LIMITGDTR48-bitsEntering protected-mode•No interrupts from any peripheral devices (since BIOS’s real-mode ISRs won’t work)•Set the PE-bit to 1 (in register CR0) •Do a far-jump (to load the CS attributes)•Load SS:SP with stacktop and attributes•Setup DS and ES for data and vram•Write character-string to video memoryLeaving protected-mode•Be sure segment-registers are loaded with selectors for descriptors that have suitable segment-limits and segment-attributes for correct execution when back in real-mode•Reset PE-bit to 0 (in register CR0)•Do a far-jump (to load CS with paragraph)•Load SS:SP with real-mode stack-address•Wait for user’s keypress before rebootingDemo-program•We have a bootsector program on website (‘pmhello.s’) which illustrates the principles just discussed•Try assembling and installing it:–$ as86 pmhello.s –b pmhello.b–$ dd if=pmhello.b of=/dev/fd0•Restart machine, use the GRUB memu to select this bootsector as execution-optionIn-class exercises•What happens if you changed the ‘code’ descriptor’s access-rights byte from 0x9A to 0x9C (i.e., conforming code-segment)?•Where exactly in does the ‘expand-down’ stack-segment reside?–BASE_ADDRESS = 0x00007C00–SEGMENT_LIMIT =


View Full Document

USF CS 630 - Defining protected-mode segment-descriptors

Documents in this Course
Load more
Download Defining protected-mode segment-descriptors
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Defining protected-mode segment-descriptors and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Defining protected-mode segment-descriptors 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?