2006-06-12 17:22:12 +02:00
|
|
|
/*
|
|
|
|
* This file contains definitions for the x86 memory management unit (MMU),
|
|
|
|
* including paging- and segmentation-related data structures and constants,
|
|
|
|
* the %cr0, %cr4, and %eflags registers, and traps.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2006-07-16 18:55:52 +02:00
|
|
|
* Register flags and fundamental constants.
|
2006-06-12 17:22:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define PGSIZE 4096 // bytes mapped by a page
|
|
|
|
|
|
|
|
// Eflags register
|
|
|
|
#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
|
|
|
|
#define FL_IF 0x00000200 // Interrupt Flag
|
|
|
|
#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
|
|
|
|
|
|
|
|
// Page fault error codes
|
|
|
|
#define FEC_PR 0x1 // Page fault caused by protection violation
|
|
|
|
#define FEC_WR 0x2 // Page fault caused by a write
|
|
|
|
#define FEC_U 0x4 // Page fault occured while in user mode
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
2006-07-16 18:55:52 +02:00
|
|
|
* Segmentation data structures and constants.
|
2006-06-12 17:22:12 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __ASSEMBLER__
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Macros to build GDT entries in assembly.
|
|
|
|
*/
|
|
|
|
#define SEG_NULL \
|
|
|
|
.word 0, 0; \
|
|
|
|
.byte 0, 0, 0, 0
|
|
|
|
#define SEG(type,base,lim) \
|
|
|
|
.word (((lim) >> 12) & 0xffff), ((base) & 0xffff); \
|
|
|
|
.byte (((base) >> 16) & 0xff), (0x90 | (type)), \
|
|
|
|
(0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff)
|
|
|
|
|
|
|
|
#else // not __ASSEMBLER__
|
|
|
|
|
|
|
|
// Segment Descriptors
|
2006-07-17 03:58:13 +02:00
|
|
|
struct segdesc {
|
2006-07-17 03:52:13 +02: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 17:22:12 +02:00
|
|
|
};
|
|
|
|
// Null segment
|
2006-07-17 03:58:13 +02:00
|
|
|
#define SEG_NULL (struct segdesc){ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
2006-06-12 17:22:12 +02:00
|
|
|
// Segment that is loadable but faults when used
|
2006-07-17 03:58:13 +02:00
|
|
|
#define SEG_FAULT (struct segdesc){ 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0 }
|
2006-06-12 17:22:12 +02:00
|
|
|
// Normal segment
|
2006-07-17 03:58:13 +02:00
|
|
|
#define SEG(type, base, lim, dpl) (struct segdesc) \
|
2006-06-12 17:22:12 +02:00
|
|
|
{ ((lim) >> 12) & 0xffff, (base) & 0xffff, ((base) >> 16) & 0xff, \
|
2006-07-17 03:52:13 +02:00
|
|
|
type, 1, dpl, 1, (uint) (lim) >> 28, 0, 0, 1, 1, \
|
|
|
|
(uint) (base) >> 24 }
|
2006-07-17 03:58:13 +02:00
|
|
|
#define SEG16(type, base, lim, dpl) (struct segdesc) \
|
2006-06-12 17:22:12 +02:00
|
|
|
{ (lim) & 0xffff, (base) & 0xffff, ((base) >> 16) & 0xff, \
|
2006-07-17 03:52:13 +02:00
|
|
|
type, 1, dpl, 1, (uint) (lim) >> 16, 0, 0, 1, 0, \
|
|
|
|
(uint) (base) >> 24 }
|
2006-06-12 17:22:12 +02:00
|
|
|
|
|
|
|
#endif /* !__ASSEMBLER__ */
|
|
|
|
|
|
|
|
// Application segment type bits
|
|
|
|
#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
|
|
|
|
|
|
|
|
// System segment type bits
|
|
|
|
#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-07-16 18:55:52 +02:00
|
|
|
* Traps.
|
2006-06-12 17:22:12 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ASSEMBLER__
|
|
|
|
|
|
|
|
// Task state segment format (as described by the Pentium architecture book)
|
2006-07-17 03:58:13 +02:00
|
|
|
struct taskstate {
|
2006-07-20 11:07:53 +02:00
|
|
|
uint link; // Old ts selector
|
2006-07-21 15:18:04 +02:00
|
|
|
uint esp0; // Stack pointers and segment selectors
|
2006-07-20 11:07:53 +02:00
|
|
|
ushort ss0; // after an increase in privilege level
|
|
|
|
ushort padding1;
|
|
|
|
uint * esp1;
|
|
|
|
ushort ss1;
|
|
|
|
ushort padding2;
|
|
|
|
uint * esp2;
|
|
|
|
ushort ss2;
|
|
|
|
ushort padding3;
|
|
|
|
void * cr3; // Page directory base
|
|
|
|
uint * eip; // Saved state from last task switch
|
|
|
|
uint eflags;
|
|
|
|
uint eax; // More saved state (registers)
|
|
|
|
uint ecx;
|
|
|
|
uint edx;
|
|
|
|
uint ebx;
|
|
|
|
uint * esp;
|
|
|
|
uint * ebp;
|
|
|
|
uint esi;
|
|
|
|
uint edi;
|
|
|
|
ushort es; // Even more saved state (segment selectors)
|
|
|
|
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;
|
|
|
|
ushort t; // Trap on task switch
|
|
|
|
ushort iomb; // I/O map base address
|
2006-06-12 17:22:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Gate descriptors for interrupts and traps
|
2006-07-17 03:58:13 +02:00
|
|
|
struct gatedesc {
|
2006-07-17 03:52:13 +02:00
|
|
|
uint off_15_0 : 16; // low 16 bits of offset in segment
|
|
|
|
uint ss : 16; // segment selector
|
|
|
|
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 17:22:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Set up a normal interrupt/trap gate descriptor.
|
|
|
|
// - istrap: 1 for a trap (= exception) gate, 0 for an interrupt gate.
|
2006-08-11 00:08:14 +02:00
|
|
|
// interrupt gate clears FL_IF, trap gate leaves FL_IF alone
|
2006-06-12 17:22:12 +02:00
|
|
|
// - sel: Code segment selector for interrupt/trap handler
|
|
|
|
// - off: Offset in code segment for interrupt/trap handler
|
|
|
|
// - dpl: Descriptor Privilege Level -
|
|
|
|
// the privilege level required for software to invoke
|
|
|
|
// this interrupt/trap gate explicitly using an int instruction.
|
2006-07-16 17:41:47 +02:00
|
|
|
#define SETGATE(gate, istrap, sel, off, d) \
|
2006-06-12 17:22:12 +02:00
|
|
|
{ \
|
2006-07-20 11:07:53 +02:00
|
|
|
(gate).off_15_0 = (uint) (off) & 0xffff; \
|
2006-07-16 17:41:47 +02:00
|
|
|
(gate).ss = (sel); \
|
|
|
|
(gate).args = 0; \
|
|
|
|
(gate).rsv1 = 0; \
|
|
|
|
(gate).type = (istrap) ? STS_TG32 : STS_IG32; \
|
|
|
|
(gate).s = 0; \
|
|
|
|
(gate).dpl = (d); \
|
|
|
|
(gate).p = 1; \
|
2006-07-20 11:07:53 +02:00
|
|
|
(gate).off_31_16 = (uint) (off) >> 16; \
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a call gate descriptor.
|
2006-07-16 17:41:47 +02:00
|
|
|
#define SETCALLGATE(gate, ss, off, d) \
|
2006-06-12 17:22:12 +02:00
|
|
|
{ \
|
2006-07-20 11:07:53 +02:00
|
|
|
(gate).off_15_0 = (uint) (off) & 0xffff; \
|
2006-07-16 17:41:47 +02:00
|
|
|
(gate).ss = (ss); \
|
|
|
|
(gate).args = 0; \
|
|
|
|
(gate).rsv1 = 0; \
|
|
|
|
(gate).type = STS_CG32; \
|
|
|
|
(gate).s = 0; \
|
|
|
|
(gate).dpl = (d); \
|
|
|
|
(gate).p = 1; \
|
2006-07-20 11:07:53 +02:00
|
|
|
(gate).off_31_16 = (uint) (off) >> 16; \
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !__ASSEMBLER__ */
|
|
|
|
|