xref: /xv6-public/mmu.h (revision b8912d99)
1 // This file contains definitions for the
2 // x86 memory management unit (MMU).
3 
4 // Eflags register
5 #define FL_CF           0x00000001      // Carry Flag
6 #define FL_PF           0x00000004      // Parity Flag
7 #define FL_AF           0x00000010      // Auxiliary carry Flag
8 #define FL_ZF           0x00000040      // Zero Flag
9 #define FL_SF           0x00000080      // Sign Flag
10 #define FL_TF           0x00000100      // Trap Flag
11 #define FL_IF           0x00000200      // Interrupt Enable
12 #define FL_DF           0x00000400      // Direction Flag
13 #define FL_OF           0x00000800      // Overflow Flag
14 #define FL_IOPL_MASK    0x00003000      // I/O Privilege Level bitmask
15 #define FL_IOPL_0       0x00000000      //   IOPL == 0
16 #define FL_IOPL_1       0x00001000      //   IOPL == 1
17 #define FL_IOPL_2       0x00002000      //   IOPL == 2
18 #define FL_IOPL_3       0x00003000      //   IOPL == 3
19 #define FL_NT           0x00004000      // Nested Task
20 #define FL_RF           0x00010000      // Resume Flag
21 #define FL_VM           0x00020000      // Virtual 8086 mode
22 #define FL_AC           0x00040000      // Alignment Check
23 #define FL_VIF          0x00080000      // Virtual Interrupt Flag
24 #define FL_VIP          0x00100000      // Virtual Interrupt Pending
25 #define FL_ID           0x00200000      // ID flag
26 
27 // Segment Descriptor
28 struct segdesc {
29   uint lim_15_0 : 16;  // Low bits of segment limit
30   uint base_15_0 : 16; // Low bits of segment base address
31   uint base_23_16 : 8; // Middle bits of segment base address
32   uint type : 4;       // Segment type (see STS_ constants)
33   uint s : 1;          // 0 = system, 1 = application
34   uint dpl : 2;        // Descriptor Privilege Level
35   uint p : 1;          // Present
36   uint lim_19_16 : 4;  // High bits of segment limit
37   uint avl : 1;        // Unused (available for software use)
38   uint rsv1 : 1;       // Reserved
39   uint db : 1;         // 0 = 16-bit segment, 1 = 32-bit segment
40   uint g : 1;          // Granularity: limit scaled by 4K when set
41   uint base_31_24 : 8; // High bits of segment base address
42 };
43 
44 // Normal segment
45 #define SEG(type, base, lim, dpl) (struct segdesc)                      \
46 { ((lim) >> 12) & 0xffff, (base) & 0xffff, ((base) >> 16) & 0xff,       \
47     type, 1, dpl, 1, (uint) (lim) >> 28, 0, 0, 1, 1,                    \
48     (uint) (base) >> 24 }
49 
50 #define SEG16(type, base, lim, dpl) (struct segdesc)                    \
51 { (lim) & 0xffff, (base) & 0xffff, ((base) >> 16) & 0xff,               \
52     type, 1, dpl, 1, (uint) (lim) >> 16, 0, 0, 1, 0,                    \
53     (uint) (base) >> 24 }
54 
55 #define DPL_USER    0x3     // User DPL
56 
57 // Application segment type bits
58 #define STA_X       0x8     // Executable segment
59 #define STA_E       0x4     // Expand down (non-executable segments)
60 #define STA_C       0x4     // Conforming code segment (executable only)
61 #define STA_W       0x2     // Writeable (non-executable segments)
62 #define STA_R       0x2     // Readable (executable segments)
63 #define STA_A       0x1     // Accessed
64 
65 // System segment type bits
66 #define STS_T16A    0x1     // Available 16-bit TSS
67 #define STS_LDT     0x2     // Local Descriptor Table
68 #define STS_T16B    0x3     // Busy 16-bit TSS
69 #define STS_CG16    0x4     // 16-bit Call Gate
70 #define STS_TG      0x5     // Task Gate / Coum Transmitions
71 #define STS_IG16    0x6     // 16-bit Interrupt Gate
72 #define STS_TG16    0x7     // 16-bit Trap Gate
73 #define STS_T32A    0x9     // Available 32-bit TSS
74 #define STS_T32B    0xB     // Busy 32-bit TSS
75 #define STS_CG32    0xC     // 32-bit Call Gate
76 #define STS_IG32    0xE     // 32-bit Interrupt Gate
77 #define STS_TG32    0xF     // 32-bit Trap Gate
78 
79 // PAGEBREAK: 40
80 // Task state segment format
81 struct taskstate {
82   uint link;         // Old ts selector
83   uint esp0;         // Stack pointers and segment selectors
84   ushort ss0;        //   after an increase in privilege level
85   ushort padding1;
86   uint *esp1;
87   ushort ss1;
88   ushort padding2;
89   uint *esp2;
90   ushort ss2;
91   ushort padding3;
92   void *cr3;         // Page directory base
93   uint *eip;         // Saved state from last task switch
94   uint eflags;
95   uint eax;          // More saved state (registers)
96   uint ecx;
97   uint edx;
98   uint ebx;
99   uint *esp;
100   uint *ebp;
101   uint esi;
102   uint edi;
103   ushort es;         // Even more saved state (segment selectors)
104   ushort padding4;
105   ushort cs;
106   ushort padding5;
107   ushort ss;
108   ushort padding6;
109   ushort ds;
110   ushort padding7;
111   ushort fs;
112   ushort padding8;
113   ushort gs;
114   ushort padding9;
115   ushort ldt;
116   ushort padding10;
117   ushort t;          // Trap on task switch
118   ushort iomb;       // I/O map base address
119 };
120 
121 // PAGEBREAK: 12
122 // Gate descriptors for interrupts and traps
123 struct gatedesc {
124   uint off_15_0 : 16;   // low 16 bits of offset in segment
125   uint cs : 16;         // code segment selector
126   uint args : 5;        // # args, 0 for interrupt/trap gates
127   uint rsv1 : 3;        // reserved(should be zero I guess)
128   uint type : 4;        // type(STS_{TG,IG32,TG32})
129   uint s : 1;           // must be 0 (system)
130   uint dpl : 2;         // descriptor(meaning new) privilege level
131   uint p : 1;           // Present
132   uint off_31_16 : 16;  // high bits of offset in segment
133 };
134 
135 // Set up a normal interrupt/trap gate descriptor.
136 // - istrap: 1 for a trap (= exception) gate, 0 for an interrupt gate.
137 //   interrupt gate clears FL_IF, trap gate leaves FL_IF alone
138 // - sel: Code segment selector for interrupt/trap handler
139 // - off: Offset in code segment for interrupt/trap handler
140 // - dpl: Descriptor Privilege Level -
141 //        the privilege level required for software to invoke
142 //        this interrupt/trap gate explicitly using an int instruction.
143 #define SETGATE(gate, istrap, sel, off, d)                \
144 {                                                         \
145   (gate).off_15_0 = (uint) (off) & 0xffff;                \
146   (gate).cs = (sel);                                      \
147   (gate).args = 0;                                        \
148   (gate).rsv1 = 0;                                        \
149   (gate).type = (istrap) ? STS_TG32 : STS_IG32;           \
150   (gate).s = 0;                                           \
151   (gate).dpl = (d);                                       \
152   (gate).p = 1;                                           \
153   (gate).off_31_16 = (uint) (off) >> 16;                  \
154 }
155 
156