Title: A protectedmode exploration
 1A protected-mode exploration
- A look at the steps needed to build 
segment-descriptors for displaying a message 
while in protected-mode 
  2Segment-Descriptor Format
63
32
Base31..24
G
D
R S V
A V L
Limit 19..16
P
D P L
S
X
C / D
R / W
A
Base23..16
Base15..0
Limit15..0
31
0
Legend DPL  Descriptor Privilege Level 
(0..3) G  Granularity (0  byte, 1  
4KB-page) P  Present (0  no, 1  yes) D  
Default size (0  16-bit, 1  32-bit) S  System 
(0  yes, 1  no) X  eXecutable (0  no, 1  
yes) A  Accessed (0  no, 1  
yes) code-segments R  Readable (0  no, 1  
yes) C  Conforming (0no, 1yes) data-segments 
 W  Writable (0  no, 1  yes) D  
expands-Down (0no, 1yes) RSV  Reserved for 
future use by Intel AVL  Available for users 
purposes 
 3Example the vram segment
- The video display-memory for color text occupies 
a 32KB physical address-range from 0x000B8000 
to 0x000BFFFF  - Its segment-limit can be described with byte 
granularity as equal to 0x07FFF (or with 
page granularity as 0x00007 )  - It needs to be a writable data-segment 
 - Its privilege-level ought to be 0 (restricted)
 
  4Descriptor Implementations
00
92
0B
0
0
00
92
0B
0
8
8000
7FFF
8000
0007
Using byte granularity
Using page granularity
  vram-segment descriptor using byte 
granularity .word 0x7FFF, 0x8000, 0x920B, 
0x0000  vram-segment descriptor using page 
granularity .word 0x0007, 0x8000, 0x920B, 0x0080 
 5Code and data segments
- Our programs code and data will reside at the 
base memory-address 0x00010000  - For simplicity when returning to real-mode, we 
can keep segment-limits as 0x0FFFF  - Both segments can retain privilege-level 0 
 - Code-segment readable  executable 
 - Data-segment writable  readable
 
  6Descriptors implemented
data-segment descriptor
code-segment descriptor
00
92
01
0
0
00
9A
01
0
0
0000
FFFF
0000
FFFF
Using byte granularity
Using byte granularity
  data-segment descriptor using byte 
granularity .word 0xFFFF, 0x0000, 0x9201, 
0x0000  code-segment descriptor using byte 
granularity .word 0xFFFF, 0x0000, 0x9A01, 0x0000 
 7Global Descriptor Table
- We can put all of our segment-descriptors into 
the Global Descriptor Table  - Our program executes at privilege-level 0 
 - Every GDT must have a null descriptor 
 - Thus our GDT will need four descriptors
 
 .align 8  the Pentium requires quadword 
alignment theGDT .word 0x0000, 0x0000, 0x0000, 
0x0000  null descriptor .word 0xFFFF, 0x0000, 
0x9A01, 0x0000  code-descriptor .word 0xFFFF, 
0x0000, 0x9201, 0x0000  data-descriptor .word 0x
7FFF, 0x8000, 0x920B, 0x0000  vram-descriptor  
 8GDTR register-format
47
16
15
0
Segment Base-Address
Segment Limit
32 bits
16 bits
The register-image (48-bits) is prepared in a 
memory-location
regGDT .word 0x001F, theGDT, 0x0001  
register-image for GDTR 
 then the register gets loaded from memory via a 
special instruction
 lgdt regGDT  initializes register GDTR 
 9segment-selector format
15
3 2 1 0
INDEX
T I
RPL
16 bits
 Legend RPL  Requested Privilege Level 
(0..3) TI  Table Indicator (0  GDT, 1  
LDT) INDEX  8  number of bytes in table that 
precede the descriptor 
 10segment-selectors defined
- Assembly language source-code is easier for 
humans to read if meaningful symbols are used as 
names for magic numbers 
 These equates provide symbolic names for our 
segment-selectors .equ sel_cs0, 0x0008  
code-segment selector .equ sel_ds0, 0x0010  
data-segment selector ,equ sel_es0, 0x0018  
vram-segment selector 
 11Our pmhello.s demo
- Use these commands to assemble, link, and install 
our demo program (in class)  -   as pmhello.s o pmhello.o 
 -   ld pmhello.o -T ldscript -o pmhello.b 
 -   dd ifpmhello.b of/dev/sda4 seek1 
 - It also needs a boot-sector program that can 
load it at the proper memory-address and then 
transfer control to its entry-point 
  12Our quikload.s loader
- We have provided a boot-sector program that you 
can use in our classroom or labs (its not 
designed to work at other places), or you can use 
your own loader program  - Heres how to assemble, link, and install our 
quikload.s example  -   as quikload.s -o quikload.o 
 -   ld quickload.o -T ldscript -o quikload.b 
 -   dd ifquikloab.b of/dev/sda4
 
  13In-class exercise-set 1
- Find out what will happen if you modify the 
segment-descriptor for video memory so it uses 
page granularity for its limit-field  - Find out what will happen if you do NOT set the 
ES-registers segment-limit to 64K before 
clearing the PE-bit in register CR0  - Find out what will happen if you change the DPL 
and/or RPL to values other than 0 
  14In-class exercise-set 2
- Redesign the pmhello.s program so that it 
expects to be loaded at a higher address  - Say at address 0x00040000 (i.e., at 256KB) 
 - Say at address 0x01000000 (i.e., at 16MB) 
 - You will need to change the disk-address packet 
in our quikload.s program so that it will 
transfer your pmhello.b code from the disk to 
your higher memory address