mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-14 15:06:52 -04:00
Improve GDT entries
This commit is contained in:
parent
8fca1bc2a0
commit
0263dceb1f
@ -64,6 +64,14 @@ void setup_idt(){
|
|||||||
asm volatile("lidt %0" : : "m" (null_idt));
|
asm volatile("lidt %0" : : "m" (null_idt));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr uint64_t gdt_entry(uint32_t base, uint32_t limit, uint16_t flags){
|
||||||
|
return ((((base) & 0xff000000ULL) << (56-24)) |
|
||||||
|
(((flags) & 0x0000f0ffULL) << 40) |
|
||||||
|
(((limit) & 0x000f0000ULL) << (48-16)) |
|
||||||
|
(((base) & 0x00ffffffULL) << 16) |
|
||||||
|
(((limit) & 0x0000ffffULL)));
|
||||||
|
}
|
||||||
|
|
||||||
#define GDT_ENTRY(f1, f2) \
|
#define GDT_ENTRY(f1, f2) \
|
||||||
((static_cast<uint64_t>(0x0FFFF) << 48) | \
|
((static_cast<uint64_t>(0x0FFFF) << 48) | \
|
||||||
(((f1)) << 16) | \
|
(((f1)) << 16) | \
|
||||||
@ -75,13 +83,63 @@ void setup_idt(){
|
|||||||
#define B_10001111 0x8F
|
#define B_10001111 0x8F
|
||||||
#define B_10101111 0xAF
|
#define B_10101111 0xAF
|
||||||
|
|
||||||
|
#define SEG_DESCTYPE(x) ((x) << 0x04) // Descriptor type (0 for system, 1 for code/data)
|
||||||
|
#define SEG_PRES(x) ((x) << 0x07) // Present
|
||||||
|
#define SEG_SAVL(x) ((x) << 0x0C) // Available for system use
|
||||||
|
#define SEG_LONG(x) ((x) << 0x0D) // Long mode
|
||||||
|
#define SEG_SIZE(x) ((x) << 0x0E) // Size (0 for 16-bit, 1 for 32)
|
||||||
|
#define SEG_GRAN(x) ((x) << 0x0F) // Granularity (0 for 1B - 1MB, 1 for 4KB - 4GB)
|
||||||
|
#define SEG_PRIV(x) (((x) & 0x03) << 0x05) // Set privilege level (0 - 3)
|
||||||
|
|
||||||
|
#define SEG_DATA_RD 0x00 // Read-Only
|
||||||
|
#define SEG_DATA_RDA 0x01 // Read-Only, accessed
|
||||||
|
#define SEG_DATA_RDWR 0x02 // Read/Write
|
||||||
|
#define SEG_DATA_RDWRA 0x03 // Read/Write, accessed
|
||||||
|
#define SEG_DATA_RDEXPD 0x04 // Read-Only, expand-down
|
||||||
|
#define SEG_DATA_RDEXPDA 0x05 // Read-Only, expand-down, accessed
|
||||||
|
#define SEG_DATA_RDWREXPD 0x06 // Read/Write, expand-down
|
||||||
|
#define SEG_DATA_RDWREXPDA 0x07 // Read/Write, expand-down, accessed
|
||||||
|
#define SEG_CODE_EX 0x08 // Execute-Only
|
||||||
|
#define SEG_CODE_EXA 0x09 // Execute-Only, accessed
|
||||||
|
#define SEG_CODE_EXRD 0x0A // Execute/Read
|
||||||
|
#define SEG_CODE_EXRDA 0x0B // Execute/Read, accessed
|
||||||
|
#define SEG_CODE_EXC 0x0C // Execute-Only, conforming
|
||||||
|
#define SEG_CODE_EXCA 0x0D // Execute-Only, conforming, accessed
|
||||||
|
#define SEG_CODE_EXRDC 0x0E // Execute/Read, conforming
|
||||||
|
#define SEG_CODE_EXRDCA 0x0F // Execute/Read, conforming, accessed
|
||||||
|
|
||||||
|
constexpr uint16_t code_32_selector(){
|
||||||
|
return
|
||||||
|
SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) |
|
||||||
|
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) |
|
||||||
|
SEG_PRIV(0) | SEG_CODE_EXRD;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr uint16_t code_64_selector(){
|
||||||
|
return
|
||||||
|
SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) |
|
||||||
|
SEG_LONG(1) | SEG_SIZE(1) | SEG_GRAN(1) |
|
||||||
|
SEG_PRIV(0) | SEG_CODE_EXRD;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr uint16_t data_selector(){
|
||||||
|
return
|
||||||
|
SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) |
|
||||||
|
SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) |
|
||||||
|
SEG_PRIV(0) | SEG_DATA_RDWR;
|
||||||
|
}
|
||||||
|
|
||||||
void setup_gdt(){
|
void setup_gdt(){
|
||||||
//TODO On some machines, this should be aligned to 16 bits
|
//TODO On some machines, this should be aligned to 16 bits
|
||||||
static const uint64_t gdt[] = {
|
static const uint64_t gdt[] = {
|
||||||
0, //Null Selector
|
0, //Null Selector
|
||||||
GDT_ENTRY(B_10011010, B_11001111), //32-bit Code Selector (ring 0)
|
gdt_entry(0, 0xFFFFF, code_32_selector()),
|
||||||
GDT_ENTRY(B_10010010, B_10001111), //Data Selector (ring 0)
|
gdt_entry(0, 0xFFFFF, data_selector()),
|
||||||
GDT_ENTRY(B_10011010, B_10101111) //64-bit Code Selector (ring 0)
|
gdt_entry(0, 0xFFFFF, code_64_selector())
|
||||||
|
|
||||||
|
//GDT_ENTRY(B_10011010, B_11001111), //32-bit Code Selector (ring 0)
|
||||||
|
//GDT_ENTRY(, ), //Data Selector (ring 0)
|
||||||
|
//GDT_ENTRY(, ) //64-bit Code Selector (ring 0)
|
||||||
};
|
};
|
||||||
|
|
||||||
static gdt_ptr gdtr;
|
static gdt_ptr gdtr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user