x86 and xv6
CS 450: Operating Systems Michael Saelee <saelee@iit.edu>
x86 and xv6 CS 450: Operating Systems Michael Saelee - - PowerPoint PPT Presentation
x86 and xv6 CS 450: Operating Systems Michael Saelee <saelee@iit.edu> To work on an OS kernel, we must be intimately familiar with the underlying ISA, hardware, and system conventions - x86 ISA - PC architecture - Unix, GCC, ELF, etc.
CS 450: Operating Systems Michael Saelee <saelee@iit.edu>
7 15 31 16 8 AH AL BH BL CH CL DH DL BP SI DI SP 16-bit AX DX CX BX 32-bit EAX EBX ECX EDX EBP ESI ESP General-Purpose Registers EDI
string operations
As shown in Figure 3-5, the lower 16 bits of the general-purpose registers map directly to the register set found in
movl $0, %eax .L0: addl $1, %eax cmpl $10, %eax # 10-eax jne .L0 # jump if ZF≠0 for (i=0; i<10; i++);
Figure 3-8. EFLAGS Register
31 29 30 28 27 26 25 24 23 22 21 20 19 18 17 16 R F I D A C V M
X Virtual-8086 Mode (VM) X Resume Flag (RF) X Nested Task (NT) X I/O Privilege Level (IOPL) S Overflow Flag (OF) C Direction Flag (DF) X Interrupt Enable Flag (IF) X Alignment Check / Access Control (AC) X ID Flag (ID) X Virtual Interrupt Pending (VIP)
15 13 14 12 11 10 9 8 7 6 5 4 3 2 1 C F A F P F 1 D F I F T F S F Z F N T V I P V I F O F I O P L
X Virtual Interrupt Flag (VIF) X Trap Flag (TF) S Sign Flag (SF) S Zero Flag (ZF) S Auxiliary Carry Flag (AF) S Parity Flag (PF) S Carry Flag (CF) S Indicates a Status Flag C Indicates a Control Flag X Indicates a System Flag Reserved bit positions. DO NOT USE. Always set to values previously read.
16-bit addressing modes: 32-bit addressing modes: (Courtesy WikiMedia Commons)
Figure 3-5. Logical Address to Linear Address Translation
Offset (Effective Address) Base Address Descriptor Table Segment Descriptor 31(63)
15 Logical Address
+
Linear Address 31(63)
15 3 2 1
T I Index
Table Indicator 0 = GDT 1 = LDT Requested Privilege Level (RPL)
RPL
Figure 3-2. Flat Model
Linear Address Space (or Physical Memory) Data and FFFFFFFFH Segment Limit Access Base Address Registers CS SS DS ES FS GS Code Code- and Data-Segment Descriptors Stack Not Present
Figure 3-3. Protected Flat Model
Linear Address Space (or Physical Memory) Data and FFFFFFFFH Segment Limit Access Base Address Registers CS ES SS DS FS GS Code Segment Descriptors Limit Access Base Address Memory I/O Stack Not Present
Figure 3-4. Multi-Segment Model
Linear Address Space (or Physical Memory) Segment Registers CS Segment Descriptors Limit Access Base Address SS Limit Access Base Address DS Limit Access Base Address ES Limit Access Base Address FS Limit Access Base Address GS Limit Access Base Address Limit Access Base Address Limit Access Base Address Limit Access Base Address Limit Access Base Address Stack Code Data Data Data Data
Figure 3-6. Segment Selector
Offset (Effective Address) Base Address Descriptor Table Segment Descriptor 31(63)
15 Logical Address
+
Linear Address 31(63)
15 3 2 1T I Index
Table Indicator 0 = GDT 1 = LDT Requested Privilege Level (RPL)
RPL
Figure 3-10. Global and Local Descriptor Tables
Segment Selector Global Descriptor
T
First Descriptor in GDT is Not Used TI = 0
I
56 40 48 32 24 16 8 TI = 1 56 40 48 32 24 16 8 Table (GDT) Local Descriptor Table (LDT) Base Address Limit GDTR Register LDTR Register Base Address
Limit
Figure 3-8. Segment Descriptor
31 24 23 22 21 20 19 16 15 13 14 12 11 8 7 P
Base 31:24
G D P L
Type
S L
4
31 16 15
Base Address 15:00 Segment Limit 15:00 Base 23:16
D / B A V L
Seg. Limit 19:16
G — Granularity LIMIT — Segment Limit P — Segment present S — Descriptor type (0 = system; 1 = code or data) TYPE — Segment type DPL — Descriptor privilege level AVL — Available for use by system software BASE — Segment base address D/B — Default operation size (0 = 16-bit segment; 1 = 32-bit segment) L — 64-bit code segment (IA-32e mode only)
Figure 4-2. Linear-Address Translation to a 4-KByte Page using 32-Bit Paging
Directory Table Offset Page Directory PDE with PS=0 CR3 Page Table PTE 4-KByte Page Physical Address 31 21 11 12 22 Linear Address 32 10 12 10 20 20 Directory Offset Page Directory PDE with PS=1 CR3 4-MByte Page Physical Address 31 21 22 Linear Address 10 22 32 18
Figure 3-1. Segmentation and Paging
Global Descriptor Table (GDT) Linear Address Space Segment Segment Descriptor Offset Logical Address Segment Base Address Page
Segment Selector Dir Table Offset Linear Address Page Table Page Directory Entry Physical Space Entry (or Far Pointer) Paging Segmentation Address Page
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Address of page directory1 Ignored P C D P W T Ignored CR3 Bits 31:22 of address
Reserved (must be 0) Bits 39:32 of address2 P A T Ignored G 1 D A P C D P W T U / S R / W 1 PDE: 4MB page Address of page table Ignored I g n A P C D P W T U / S R / W 1 PDE: page table Ignored PDE: not present Address of 4KB page frame Ignored G P A T D A P C D P W T U / S R / W 1 PTE: 4KB page Ignored PTE: not present
Figure 4-4. Formats of CR3 and Paging-Structure Entries with 32-Bit Paging
CR1
W P A M
Page-Directory Base
V M E P S E T S D D E P V I P G E M C E P A E P C E N W P G C D P W T P C D
Page-Fault Linear Address
P E E M M P T S N E E T
CR2 CR0 CR4 Reserved CR3 Reserved
31 29 30 28 19 18 17 16 15 6 5 4 3 2 1 31(63) 31(63) 31(63) 12 11 5 4 3 2 31(63) 9 8 7 6 5 4 3 2 1
(PDBR)
13 12 11 10
OSFXSR OSXMMEXCPT
V M X E E X M S 14 18
OSXSAVE PCIDE
17 S M E P 20
FSGSBASE
16 15 S M A P 22 21 P K E U M I P
Real-Address Protected Mode
Virtual-8086 Mode
System Management Mode
PE=1 Reset or VM=1 VM=0 PE=0 Reset
RSM SMI# RSM SMI# RSM SMI# Reset Mode
IA-32e Mode
RSM SMI# LME=1, CR0.PG=1* See** * See Section 9.8.5 ** See Section 9.8.5.4
CR1
W P A MPage-Directory Base
V M E P S E T S D D E P V I P G E M C E P A E P C E N W P G C D P W T P C DPage-Fault Linear Address
P E E M M P T S N E E TCR2 CR0 CR4 Reserved CR3 Reserved
31 29 30 28 19 18 17 16 15 6 5 4 3 2 1 31(63) 31(63) 31(63) 12 11 5 4 3 2 31(63) 9 8 7 6 5 4 3 2 1(PDBR)
13 12 11 10OSFXSR OSXMMEXCPT
V M X E E X M S 14 18OSXSAVE PCIDE
17 S M E P 20FSGSBASE
16 15 S M A P 22 21 P K E U M I P.code16 # Assemble for 16-bit mode .globl start start: cli # BIOS enabled interrupts; disable # Zero data segment registers DS, ES, and SS. xorw %ax,%ax # Set %ax to zero movw %ax,%ds # -> Data Segment
. . .
lgdt gdtdesc movl %cr0, %eax
movl %eax, %cr0 # Complete transition to 32-bit protected mode by using long jmp # to reload %cs and %eip. The segment descriptors are set up with no # translation, so that the mapping is still the identity mapping. ljmp $(SEG_KCODE<<3), $start32 .code32 # Tell assembler to generate 32-bit code now. start32: # Set up the protected-mode data segment registers movw $(SEG_KDATA<<3), %ax # Our data segment selector movw %ax, %ds # -> DS: Data Segment
. . .
# Set up the stack pointer and call into C. movl $start, %esp call bootmain
L0: inb STATUS_PORT, %al andb %al, BUSY_FLAG # check if busy (e.g., still writing) jne L0 movb (%esi,%ebx), %al # load data byte incb %ebx # increment index
jmp L0
Code Segment Stack Segment (Current Priv. Data Segment Stack Seg.
Stack Seg.
Stack Segment (Priv. Level 2) Task-State Segment (TSS) Task Register CR3 Level)
31 100 96 92 88 84 80 76 I/O Map Base Address 15 LDT Segment Selector GS FS DS SS CS 72 68 64 60 56 52 48 44 40 36 32 28 24 20 SS2 16 12 8 4 SS1 SS0 ESP0 Previous Task Link ESP1 ESP2 CR3 (PDBR) T ES EDI ESI EBP ESP EBX EDX ECX EAX EFLAGS EIP Reserved bits. Set to 0. Reserved Reserved Reserved Reserved Reserved Reserved Reserved Reserved Reserved Reserved Reserved Reserved
IDT Limit IDT Base Address
Interrupt Descriptor Table (IDT)
Gate for
IDTR Register
Interrupt #n Gate for Interrupt #3 Gate for Interrupt #2 Gate for Interrupt #1 15 16 47 31 8 16 (n−1)∗8
IDT Interrupt or Code Segment Segment Selector GDT or LDT Segment Interrupt Vector Base Address Destination Procedure Interrupt
+
Descriptor Trap Gate Offset
Vector Mne- monic Description Type Error Code Source #DE Divide Error Fault No DIV and IDIV instructions. 1 #DB Debug Exception Fault/ Trap No Instruction, data, and I/O breakpoints; single-step; and others. 2 — NMI Interrupt Interrupt No Nonmaskable external interrupt. 3 #BP Breakpoint Trap No INT3 instruction. 4 #OF Overflow Trap No INTO instruction. 5 #BR BOUND Range Exceeded Fault No BOUND instruction. 6 #UD Invalid Opcode (Undefined Opcode) Fault No UD instruction or reserved opcode. 7 #NM Device Not Available (No Math Coprocessor) Fault No Floating-point or WAIT/FWAIT instruction. 8 #DF Double Fault Abort Yes (zero) Any instruction that can generate an exception, an NMI, or an INTR. 9 Coprocessor Segment Overrun (reserved) Fault No Floating-point instruction.1 10 #TS Invalid TSS Fault Yes Task switch or TSS access. 11 #NP Segment Not Present Fault Yes Loading segment registers or accessing system segments. 12 #SS Stack-Segment Fault Fault Yes Stack operations and SS register loads. 13 #GP General Protection Fault Yes Any memory reference and other protection checks. 14 #PF Page Fault Fault Yes Any memory reference. 15 — (Intel reserved. Do not use.) No 16 #MF x87 FPU Floating-Point Error (Math Fault) Fault No x87 FPU floating-point or WAIT/FWAIT instruction. 17 #AC Alignment Check Fault Yes (Zero) Any data reference in memory.2 18 #MC Machine Check Abort No Error codes (if any) and source are model dependent.3 19 #XM SIMD Floating-Point Exception Fault No SSE/SSE2/SSE3 floating-point instructions4 20 #VE Virtualization Exception Fault No EPT violations5 21-31 — Intel reserved. Do not use. 32-255 — User Defined (Non-reserved) Interrupts Interrupt External interrupt or INT n instruction. NOTES:
Table 6-1. Protected-Mode Exceptions and Interrupts (Contd.)
Figure 6-2. IDT Gate Descriptors
31 16 15 13 14 12 8 7 P
Offset 31..16
D P L
4
31 16 15
Segment Selector Offset 15..0
1 1 D
Interrupt Gate DPL Offset P Selector Descriptor Privilege Level Offset to procedure entry point Segment Present flag Segment Selector for destination code segment
31 16 15 13 14 12 8 7 P D P L
4
31 16 15
TSS Segment Selector
1 1
Task Gate
4 5
0 0 0
31 16 15 13 14 12 8 7 P
Offset 31..16
D P L
4
31 16 15
Segment Selector Offset 15..0
1 1 1 D
Trap Gate
4 5
0 0 0
Reserved Size of gate: 1 = 32 bits; 0 = 16 bits D
When the processor performs a call to the exception- or interrupt-handler procedure:
When the stack switch occurs: a. The segment selector and stack pointer for the stack to be used by the handler are obtained from the TSS for the currently executing task. On this new stack, the processor pushes the stack segment selector and stack pointer of the interrupted procedure. b. The processor then saves the current state of the EFLAGS, CS, and EIP registers on the new stack (see Figures 6-4). c. If an exception causes an error code to be saved, it is pushed on the new stack after the EIP value.
a. The processor saves the current state of the EFLAGS, CS, and EIP registers on the current stack (see Figures 6-4). b. If an exception causes an error code to be saved, it is pushed on the current stack after the EIP value.
IDT Interrupt or Code Segment Segment Selector GDT or LDT Segment Interrupt Vector Base Address Destination Procedure Interrupt
+
Descriptor Trap Gate Offset
CS Error Code EFLAGS CS EIP ESP After Transfer to Handler Error Code ESP Before Transfer to Handler EFLAGS EIP SS ESP Stack Usage with No Privilege-Level Change Stack Usage with Privilege-Level Change Interrupted Procedure’s Interrupted Procedure’s and Handler’s Stack Handler’s Stack ESP After Transfer to Handler Transfer to Handler ESP Before Stack
Figure 2-1. IA-32 System-Level Registers and Data Structures
Local Descriptor Table (LDT) EFLAGS Register Control Registers CR1 CR2 CR3 CR4 CR0 Global Descriptor Table (GDT) Interrupt Descriptor Table (IDT) IDTR GDTR Interrupt Gate Trap Gate LDT Desc. TSS Desc. Code Stack Code Stack Code Stack Task-State Segment (TSS) Code Data Stack Task Interrupt Handler Exception Handler Protected Procedure TSS Seg. Sel. Call-Gate Segment Selector Dir Table Offset Linear Address Page Directory
Linear Address Space Linear Addr.
Segment Sel. Code, Data or Stack Segment Interrupt Vector TSS Desc.
Task Gate Current TSS Call Gate Task-State Segment (TSS) Code Data Stack Task
Current TSS Current TSS Segment Selector Linear Address Task Register CR3* Page Table
Page Physical Addr. LDTR This page mapping example is for 4-KByte pages and the normal 32-bit physical address size. Register
*Physical Address
Physical Address XCR0 (XFEM)
Protected mode memory- mapped devices Unused Extended memory BIOS ROM Real-mode devices VGA display Low memory
0x00000000 0xFFFFFFFF (4GB) 0x000A0000 (640KB) 0x000C0000 (768KB) 0x000F0000 (960KB) 0x00100000 (1MB) Physical RAM limit
The QEMU PC System emulator simulates the following peripherals:
level, including all non standard modes).
SMP is supported with up to 255 CPUs. QEMU uses the PC BIOS from the Seabios project and the Plex86/Bochs LGPL VGA BIOS.