2006-09-06 19:08:14 +00:00
|
|
|
// This file contains definitions for the
|
|
|
|
// x86 memory management unit (MMU).
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
// Eflags register
|
2006-09-06 17:04:06 +00:00
|
|
|
#define FL_CF 0x00000001 // Carry Flag
|
|
|
|
#define FL_PF 0x00000004 // Parity Flag
|
|
|
|
#define FL_AF 0x00000010 // Auxiliary carry Flag
|
|
|
|
#define FL_ZF 0x00000040 // Zero Flag
|
|
|
|
#define FL_SF 0x00000080 // Sign Flag
|
|
|
|
#define FL_TF 0x00000100 // Trap Flag
|
2006-09-07 16:54:15 +00:00
|
|
|
#define FL_IF 0x00000200 // Interrupt Enable
|
2006-09-06 17:04:06 +00:00
|
|
|
#define FL_DF 0x00000400 // Direction Flag
|
|
|
|
#define FL_OF 0x00000800 // Overflow Flag
|
|
|
|
#define FL_IOPL_MASK 0x00003000 // I/O Privilege Level bitmask
|
|
|
|
#define FL_IOPL_0 0x00000000 // IOPL == 0
|
|
|
|
#define FL_IOPL_1 0x00001000 // IOPL == 1
|
|
|
|
#define FL_IOPL_2 0x00002000 // IOPL == 2
|
|
|
|
#define FL_IOPL_3 0x00003000 // IOPL == 3
|
|
|
|
#define FL_NT 0x00004000 // Nested Task
|
|
|
|
#define FL_RF 0x00010000 // Resume Flag
|
|
|
|
#define FL_VM 0x00020000 // Virtual 8086 mode
|
|
|
|
#define FL_AC 0x00040000 // Alignment Check
|
|
|
|
#define FL_VIF 0x00080000 // Virtual Interrupt Flag
|
|
|
|
#define FL_VIP 0x00100000 // Virtual Interrupt Pending
|
|
|
|
#define FL_ID 0x00200000 // ID flag
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2010-08-31 20:54:50 +00:00
|
|
|
// Control Register flags
|
2011-08-29 20:12:01 +00:00
|
|
|
#define CR0_PE 0x00000001 // Protection Enable
|
|
|
|
#define CR0_MP 0x00000002 // Monitor coProcessor
|
|
|
|
#define CR0_EM 0x00000004 // Emulation
|
|
|
|
#define CR0_TS 0x00000008 // Task Switched
|
|
|
|
#define CR0_ET 0x00000010 // Extension Type
|
|
|
|
#define CR0_NE 0x00000020 // Numeric Errror
|
|
|
|
#define CR0_WP 0x00010000 // Write Protect
|
|
|
|
#define CR0_AM 0x00040000 // Alignment Mask
|
|
|
|
#define CR0_NW 0x20000000 // Not Writethrough
|
|
|
|
#define CR0_CD 0x40000000 // Cache Disable
|
|
|
|
#define CR0_PG 0x80000000 // Paging
|
|
|
|
|
|
|
|
#define CR4_PSE 0x00000010 // Page size extension
|
2011-08-15 21:41:58 +00:00
|
|
|
|
2011-08-08 17:30:08 +00:00
|
|
|
#define SEG_KCODE 1 // kernel code
|
|
|
|
#define SEG_KDATA 2 // kernel data+stack
|
|
|
|
#define SEG_KCPU 3 // kernel per-cpu data
|
|
|
|
#define SEG_UCODE 4 // user code
|
|
|
|
#define SEG_UDATA 5 // user data+stack
|
|
|
|
#define SEG_TSS 6 // this process's task state
|
|
|
|
|
2010-08-31 20:54:50 +00:00
|
|
|
//PAGEBREAK!
|
2011-08-08 17:30:08 +00:00
|
|
|
#ifndef __ASSEMBLER__
|
2006-08-29 19:06:37 +00:00
|
|
|
// Segment Descriptor
|
2006-07-17 01:58:13 +00:00
|
|
|
struct segdesc {
|
2006-09-06 17:04:06 +00:00
|
|
|
uint lim_15_0 : 16; // Low bits of segment limit
|
|
|
|
uint base_15_0 : 16; // Low bits of segment base address
|
|
|
|
uint base_23_16 : 8; // Middle bits of segment base address
|
|
|
|
uint type : 4; // Segment type (see STS_ constants)
|
|
|
|
uint s : 1; // 0 = system, 1 = application
|
|
|
|
uint dpl : 2; // Descriptor Privilege Level
|
|
|
|
uint p : 1; // Present
|
|
|
|
uint lim_19_16 : 4; // High bits of segment limit
|
|
|
|
uint avl : 1; // Unused (available for software use)
|
|
|
|
uint rsv1 : 1; // Reserved
|
|
|
|
uint db : 1; // 0 = 16-bit segment, 1 = 32-bit segment
|
|
|
|
uint g : 1; // Granularity: limit scaled by 4K when set
|
|
|
|
uint base_31_24 : 8; // High bits of segment base address
|
2006-06-12 15:22:12 +00:00
|
|
|
};
|
2006-08-29 19:06:37 +00:00
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
// Normal segment
|
2009-08-31 06:02:08 +00:00
|
|
|
#define SEG(type, base, lim, dpl) (struct segdesc) \
|
|
|
|
{ ((lim) >> 12) & 0xffff, (uint)(base) & 0xffff, \
|
|
|
|
((uint)(base) >> 16) & 0xff, type, 1, dpl, 1, \
|
|
|
|
(uint)(lim) >> 28, 0, 0, 1, 1, (uint)(base) >> 24 }
|
|
|
|
#define SEG16(type, base, lim, dpl) (struct segdesc) \
|
|
|
|
{ (lim) & 0xffff, (uint)(base) & 0xffff, \
|
|
|
|
((uint)(base) >> 16) & 0xff, type, 1, dpl, 1, \
|
|
|
|
(uint)(lim) >> 16, 0, 0, 1, 0, (uint)(base) >> 24 }
|
2011-08-08 17:30:08 +00:00
|
|
|
#endif
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2007-08-08 09:02:42 +00:00
|
|
|
#define DPL_USER 0x3 // User DPL
|
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
// Application segment type bits
|
2006-09-06 19:08:14 +00:00
|
|
|
#define STA_X 0x8 // Executable segment
|
|
|
|
#define STA_E 0x4 // Expand down (non-executable segments)
|
|
|
|
#define STA_C 0x4 // Conforming code segment (executable only)
|
|
|
|
#define STA_W 0x2 // Writeable (non-executable segments)
|
|
|
|
#define STA_R 0x2 // Readable (executable segments)
|
|
|
|
#define STA_A 0x1 // Accessed
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
// System segment type bits
|
2006-09-06 19:08:14 +00:00
|
|
|
#define STS_T16A 0x1 // Available 16-bit TSS
|
|
|
|
#define STS_LDT 0x2 // Local Descriptor Table
|
|
|
|
#define STS_T16B 0x3 // Busy 16-bit TSS
|
|
|
|
#define STS_CG16 0x4 // 16-bit Call Gate
|
|
|
|
#define STS_TG 0x5 // Task Gate / Coum Transmitions
|
|
|
|
#define STS_IG16 0x6 // 16-bit Interrupt Gate
|
|
|
|
#define STS_TG16 0x7 // 16-bit Trap Gate
|
|
|
|
#define STS_T32A 0x9 // Available 32-bit TSS
|
|
|
|
#define STS_T32B 0xB // Busy 32-bit TSS
|
|
|
|
#define STS_CG32 0xC // 32-bit Call Gate
|
|
|
|
#define STS_IG32 0xE // 32-bit Interrupt Gate
|
|
|
|
#define STS_TG32 0xF // 32-bit Trap Gate
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2011-08-16 19:47:22 +00:00
|
|
|
// A virtual address 'la' has a three-part structure as follows:
|
2010-07-02 18:51:53 +00:00
|
|
|
//
|
|
|
|
// +--------10------+-------10-------+---------12----------+
|
|
|
|
// | Page Directory | Page Table | Offset within Page |
|
|
|
|
// | Index | Index | |
|
|
|
|
// +----------------+----------------+---------------------+
|
2011-08-16 19:47:22 +00:00
|
|
|
// \--- PDX(va) --/ \--- PTX(va) --/
|
2010-07-02 18:51:53 +00:00
|
|
|
|
|
|
|
// page directory index
|
2011-08-29 20:12:01 +00:00
|
|
|
#define PDX(va) (((uint)(va) >> PDXSHIFT) & 0x3FF)
|
2010-07-02 18:51:53 +00:00
|
|
|
|
|
|
|
// page table index
|
2011-08-29 20:12:01 +00:00
|
|
|
#define PTX(va) (((uint)(va) >> PTXSHIFT) & 0x3FF)
|
2010-07-02 18:51:53 +00:00
|
|
|
|
2011-08-16 19:47:22 +00:00
|
|
|
// construct virtual address from indexes and offset
|
2011-08-29 20:12:01 +00:00
|
|
|
#define PGADDR(d, t, o) ((uint)((d) << PDXSHIFT | (t) << PTXSHIFT | (o)))
|
2010-07-02 18:51:53 +00:00
|
|
|
|
|
|
|
// Page directory and page table constants.
|
2011-09-01 14:25:20 +00:00
|
|
|
#define NPDENTRIES 1024 // # directory entries per page directory
|
|
|
|
#define NPTENTRIES 1024 // # PTEs per page table
|
|
|
|
#define PGSIZE 4096 // bytes mapped by a page
|
2010-07-02 18:51:53 +00:00
|
|
|
|
2011-09-01 14:25:20 +00:00
|
|
|
#define PGSHIFT 12 // log2(PGSIZE)
|
|
|
|
#define PTXSHIFT 12 // offset of PTX in a linear address
|
|
|
|
#define PDXSHIFT 22 // offset of PDX in a linear address
|
2010-07-02 18:51:53 +00:00
|
|
|
|
2010-08-05 16:10:54 +00:00
|
|
|
#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
|
2011-08-16 19:47:22 +00:00
|
|
|
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
|
2010-08-05 16:10:54 +00:00
|
|
|
|
2010-07-02 18:51:53 +00:00
|
|
|
// Page table/directory entry flags.
|
2011-08-29 20:12:01 +00:00
|
|
|
#define PTE_P 0x001 // Present
|
|
|
|
#define PTE_W 0x002 // Writeable
|
|
|
|
#define PTE_U 0x004 // User
|
|
|
|
#define PTE_PWT 0x008 // Write-Through
|
|
|
|
#define PTE_PCD 0x010 // Cache-Disable
|
|
|
|
#define PTE_A 0x020 // Accessed
|
|
|
|
#define PTE_D 0x040 // Dirty
|
|
|
|
#define PTE_PS 0x080 // Page Size
|
|
|
|
#define PTE_MBZ 0x180 // Bits must be zero
|
2010-07-02 18:51:53 +00:00
|
|
|
|
|
|
|
// Address in page table or page directory entry
|
2011-08-29 20:12:01 +00:00
|
|
|
#define PTE_ADDR(pte) ((uint)(pte) & ~0xFFF)
|
2013-03-04 21:16:54 +00:00
|
|
|
#define PTE_FLAGS(pte) ((uint)(pte) & 0xFFF)
|
2010-07-02 18:51:53 +00:00
|
|
|
|
2011-08-08 17:30:08 +00:00
|
|
|
#ifndef __ASSEMBLER__
|
2010-07-02 18:51:53 +00:00
|
|
|
typedef uint pte_t;
|
|
|
|
|
2006-08-29 19:06:37 +00:00
|
|
|
// Task state segment format
|
2006-07-17 01:58:13 +00:00
|
|
|
struct taskstate {
|
2007-08-08 09:12:53 +00:00
|
|
|
uint link; // Old ts selector
|
|
|
|
uint esp0; // Stack pointers and segment selectors
|
|
|
|
ushort ss0; // after an increase in privilege level
|
2006-09-06 17:04:06 +00:00
|
|
|
ushort padding1;
|
2006-09-06 17:27:19 +00:00
|
|
|
uint *esp1;
|
2006-09-06 17:04:06 +00:00
|
|
|
ushort ss1;
|
|
|
|
ushort padding2;
|
2006-09-06 17:27:19 +00:00
|
|
|
uint *esp2;
|
2006-09-06 17:04:06 +00:00
|
|
|
ushort ss2;
|
|
|
|
ushort padding3;
|
2007-08-08 09:12:53 +00:00
|
|
|
void *cr3; // Page directory base
|
|
|
|
uint *eip; // Saved state from last task switch
|
2006-09-06 17:04:06 +00:00
|
|
|
uint eflags;
|
2007-08-08 09:12:53 +00:00
|
|
|
uint eax; // More saved state (registers)
|
2006-09-06 17:04:06 +00:00
|
|
|
uint ecx;
|
|
|
|
uint edx;
|
|
|
|
uint ebx;
|
2006-09-06 17:27:19 +00:00
|
|
|
uint *esp;
|
|
|
|
uint *ebp;
|
2006-09-06 17:04:06 +00:00
|
|
|
uint esi;
|
|
|
|
uint edi;
|
2007-08-08 09:12:53 +00:00
|
|
|
ushort es; // Even more saved state (segment selectors)
|
2006-09-06 17:04:06 +00:00
|
|
|
ushort padding4;
|
|
|
|
ushort cs;
|
|
|
|
ushort padding5;
|
|
|
|
ushort ss;
|
|
|
|
ushort padding6;
|
|
|
|
ushort ds;
|
|
|
|
ushort padding7;
|
|
|
|
ushort fs;
|
|
|
|
ushort padding8;
|
|
|
|
ushort gs;
|
|
|
|
ushort padding9;
|
|
|
|
ushort ldt;
|
|
|
|
ushort padding10;
|
2007-08-08 09:12:53 +00:00
|
|
|
ushort t; // Trap on task switch
|
|
|
|
ushort iomb; // I/O map base address
|
2006-06-12 15:22:12 +00:00
|
|
|
};
|
|
|
|
|
2007-08-14 19:41:38 +00:00
|
|
|
// PAGEBREAK: 12
|
2006-06-12 15:22:12 +00:00
|
|
|
// Gate descriptors for interrupts and traps
|
2006-07-17 01:58:13 +00:00
|
|
|
struct gatedesc {
|
2006-09-06 17:04:06 +00:00
|
|
|
uint off_15_0 : 16; // low 16 bits of offset in segment
|
2007-09-27 16:47:50 +00:00
|
|
|
uint cs : 16; // code segment selector
|
2006-09-06 17:04:06 +00:00
|
|
|
uint args : 5; // # args, 0 for interrupt/trap gates
|
|
|
|
uint rsv1 : 3; // reserved(should be zero I guess)
|
|
|
|
uint type : 4; // type(STS_{TG,IG32,TG32})
|
|
|
|
uint s : 1; // must be 0 (system)
|
|
|
|
uint dpl : 2; // descriptor(meaning new) privilege level
|
|
|
|
uint p : 1; // Present
|
|
|
|
uint off_31_16 : 16; // high bits of offset in segment
|
2006-06-12 15:22:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Set up a normal interrupt/trap gate descriptor.
|
|
|
|
// - istrap: 1 for a trap (= exception) gate, 0 for an interrupt gate.
|
2006-08-10 22:08:14 +00:00
|
|
|
// interrupt gate clears FL_IF, trap gate leaves FL_IF alone
|
2006-06-12 15:22:12 +00:00
|
|
|
// - sel: Code segment selector for interrupt/trap handler
|
|
|
|
// - off: Offset in code segment for interrupt/trap handler
|
|
|
|
// - dpl: Descriptor Privilege Level -
|
2006-09-06 17:04:06 +00:00
|
|
|
// the privilege level required for software to invoke
|
|
|
|
// this interrupt/trap gate explicitly using an int instruction.
|
2007-08-08 09:12:53 +00:00
|
|
|
#define SETGATE(gate, istrap, sel, off, d) \
|
|
|
|
{ \
|
2011-01-11 18:01:13 +00:00
|
|
|
(gate).off_15_0 = (uint)(off) & 0xffff; \
|
2007-09-27 16:47:50 +00:00
|
|
|
(gate).cs = (sel); \
|
2006-09-06 17:04:06 +00:00
|
|
|
(gate).args = 0; \
|
|
|
|
(gate).rsv1 = 0; \
|
2007-08-08 09:12:53 +00:00
|
|
|
(gate).type = (istrap) ? STS_TG32 : STS_IG32; \
|
|
|
|
(gate).s = 0; \
|
2006-09-06 17:04:06 +00:00
|
|
|
(gate).dpl = (d); \
|
2007-08-08 09:12:53 +00:00
|
|
|
(gate).p = 1; \
|
2011-01-11 18:01:13 +00:00
|
|
|
(gate).off_31_16 = (uint)(off) >> 16; \
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 17:30:08 +00:00
|
|
|
#endif
|