The Various x86 Modes': On Understanding Key Differences Among The Processor's Several Execution-Architectures
The Various x86 Modes': On Understanding Key Differences Among The Processor's Several Execution-Architectures
Protected mode
IA-32e mode
Compatibility mode
Mode transitions
The processor starts up in real mode Mode-transitions normally happen under program control (except for transitions to System Management Mode) Details of programming a mode-change depend on which modes are involved Some mode-transfers arent possible (and some mode-changes arent documented)
Classroom/Lab machines
The workstations in our Kudlick classroom and Fifth-Floor CS Labs use slightly older Pentium processors that lack the circuitry for supporting the IA-32e mode But the new Core-2 Duo machines, which we can access remotely, do implement all of the modes we previously depicted
Enabling protected-mode
Protected-mode was first introduced in the 80286 processor (used in the IBM-PC/AT) Intel added some special system registers to support protected-mode, and to control the transition from the power-on real-mode
Global Descriptor Table register (GDTR) Interrupt Descriptor Table register (IDTR) Local Descriptor Table register (LDTR) Task Register (TR) Machine Status Word (MSW)
Legend: PE (Protected-Mode Enabled): 0=no, 1=yes MP (Math-coprocessor Present): 0=no, 1=yes EM (Emulate Math-coprocessor): 0=no, 1=yes TS (Task has been Switched): 0=no, 1=yes
sticky bit
This registers defined bits would initially be zeros at system startup, but they could be modified under program control by executing the special LMSW instruction (Load Machine Status Word), or could be inspected by executing the SMSW instruction (Store Machine Status Word)
Descriptor tables
Memory-addressing in protected-mode is based upon some special data-structures called Segment Descriptors that define a memory-region and assign its properties
Word 3 Word 2
SegmentBase[23..16]
Segment-Limit[15..0]
Word 0
Legend: P = Present (1=yes, 0=no) DPL = Descriptor Privilege Level (00=supervisor, 11=user) S = System-segment (0=yes, 1=no) X = eXecutable: 1=yes (i.e., code-segment), 0=no (i.e., data-segment) C/D = Conforming code (1=yes, 0=no) when X-bit equals 1 expands-Down (1=yes, 0=no) when X-bit equals 0 R/W = Readable (1=yes, 0=no) when X-bit equals 1 Writable (1=yes, 0=no) when X-bit equals 0 A = segment has been Accessed by the CPU (1=yes, 0=no)
0x60
0x58
segment-limit
base
limit
base-address
0x00
null descriptor
Backward compatibility
Intels 80386 (and later) processors are backwardly compatible with the 80286, but they implement some extensions to support 32-bit registers and addresses (and some early forms of virtualization) Register MSW is enlarged and renamed, (and its stickly PE-bit design-flaw is corrected in the renamed mechanism)
Control Register 0
Register CR0 is the 32-bit version of the old MSW register (Machine Status Word) It contains the PE-bit (Protection Enabled)
when PE=0 the CPU is in real-mode when PE=1 the CPU is in protected-mode
PCN G DW A M W P NE T EMP E T SMP E
# leaving protected-mode (with interrupts turned off) mov %cr0, %eax # get current Machine Status btr $0, %eax # reset image of bit #0 (the PE-bit) mov %eax, %cr0 # leave protected mode
Enhanced Descriptor-Format
63 32
Base[31..24]
Base[23..16]
Base[15..0] 31
Limit[15..0] 0
Legend: G = Granularity: (0=byte-granularity, 1=page-granularity) D = Default operand and address size: (0=16-bits, 1=32-bits) RSV = Reserved bit (but recently used for 64-bit technology) AVL = Available (this bit can be used by programmers for any purpose) Several instances of this basic segment-descriptor data-structure will occur in the Global Descriptor Table (and maybe also in some Local Descriptor Tables)
In-class exercise #1
Make a copy of the eflags.s demo (from our class website), and name it cr0.s Replace the two instructions that it uses to get the value of EFLAGS with just a single instruction that will get the value of CR0 Change the message-string appropriately Then assemble, link, and execute your modified demo-program
In-class exercise #2
Can you write a program that will show the contents of the system-register GDTR? You need to use the SGDT-instruction But it will require a memory-operand big enough to hold this registers 48-bits And you will need to adjust the counter in your program-loop for 48 binary-digits, as well as your message-strings format