diff --git a/src/arch/x86/gdt.c b/src/arch/x86/gdt.c index 54dd6fc..37edfd3 100644 --- a/src/arch/x86/gdt.c +++ b/src/arch/x86/gdt.c @@ -51,6 +51,43 @@ static void gdt_flush() ); } +/* + * get_set_entry - Sets a GDT entry + * @num: Number of the entry (index in GDT) + * @flags: Flags (Granularity, Size, Long mode) + * @access: Access byte (contains Descriptor Privilege Level) + * + * This function fills a GDT entry with the specified @flags + * and @access byte. The base and limit fields are left to zero + * because we don't use segmentation for memory management. + */ + +static void gdt_set_entry(int num, uint8_t flags, uint8_t access) +{ + uint64_t gdt_entry = 0; + + gdt_entry |= (access << 8); + gdt_entry |= (flags << 20); + + // Rest (base, limit) is always zero + gdt_entries[num] = gdt_entry << 32; +} + +/* + * gdt_set_tss - Setup the TSS entry in the GDT + * @num: Number of the entry (index in GDT) + * + * This function sets up a Task State Segment entry + * in the Global Descriptor Table. + * + * The entry is 128-bit long, so it actually takes + * two 64-bit GDT entries. + */ +static void gdt_set_tss(int num) +{ + +} + /* * gdt_init - Global Descriptor Table initialization * @@ -62,38 +99,11 @@ static void gdt_flush() */ void gdt_init() { - // Null descriptor (required) - gdt_entries[0] = 0; - - // Kernel code segment - uint64_t kernel_code = 0; - kernel_code |= 0b1101 << 8; // Selector type: accessed, read-enable, no conforming - kernel_code |= 1 << 12; // not a system descriptor - kernel_code |= 0 << 13; // DPL field = 0 - kernel_code |= 1 << 15; // Present - kernel_code |= 1 << 21; // Long mode - - // Left shift 32 bits so we place our stuff in the upper 32 bits of the descriptor. - // The lower 32 bits contain limit and part of base and therefore are ignored in Long Mode - // (because we'll use paging; segmentation is used only for legacy) - gdt_entries[1] = kernel_code << 32; - - uint64_t kernel_data = 0; - kernel_data |= 0b0011 << 8; - kernel_data |= 1 << 12; - kernel_data |= 0 << 13; - kernel_data |= 1 << 15; - kernel_data |= 1 << 21; - - gdt_entries[2] = kernel_data << 32; - - // We re-use the kernel descriptors here, and just update their DPL fields - // (Descriptor privilege level) from ring 0 -> to ring 3 (userspace) - uint64_t user_code = kernel_code | (3 << 13); - gdt_entries[3] = user_code; - - uint64_t user_data = kernel_data | (3 << 13); - gdt_entries[4] = user_data; + gdt_set_entry(0, 0, 0); // Null descriptor (0x0) + gdt_set_entry(1, 0xA, 0x9B); // Kernel code (0x8) + gdt_set_entry(2, 0xC, 0x93); // Kernel data (0x10) + gdt_set_entry(3, 0xA, 0xFB); // User code (0x18) + gdt_set_entry(4, 0xC, 0xF3); // User data (0x20) // The -1 subtraction is some wizardry explained in the OSDev wiki -> GDT gdtr.limit = NUM_GDT_ENTRIES * sizeof(uint64_t) - 1; @@ -103,5 +113,7 @@ void gdt_init() gdt_load(); gdt_flush(); + // Here we should load the task register with TSS selector + DEBUG("GDT initialized"); } \ No newline at end of file