82 lines
2.4 KiB
C
82 lines
2.4 KiB
C
#include "gdt.h"
|
|
#include <stdint.h>
|
|
#include "../io/serial.h"
|
|
|
|
// Descriptors are 8-byte wide (64bits)
|
|
// So the selectors will be (in bytes): 0x0, 0x8, 0x10, 0x18, etc..
|
|
uint64_t gdt_entries[NUM_GDT_ENTRIES];
|
|
struct GDTR gdtr;
|
|
|
|
static void gdt_load()
|
|
{
|
|
asm("lgdt %0" : : "m"(gdtr));
|
|
}
|
|
|
|
static void gdt_flush()
|
|
{
|
|
// Here, 0x8 is the kernel code selector
|
|
// and 0x10 is the kernel data selector
|
|
asm volatile (
|
|
"mov $0x10, %%ax \n" // Reload segments with kernel data selector
|
|
"mov %%ax, %%ds \n"
|
|
"mov %%ax, %%es \n"
|
|
"mov %%ax, %%fs \n"
|
|
"mov %%ax, %%gs \n"
|
|
"mov %%ax, %%ss \n"
|
|
|
|
"pushq $0x8 \n" // CS reload
|
|
"lea 1f(%%rip), %%rax \n"
|
|
"push %%rax \n"
|
|
"lretq \n"
|
|
"1: \n" // Execution continues here after CS reload
|
|
:
|
|
:
|
|
: "rax", "memory"
|
|
);
|
|
}
|
|
|
|
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;
|
|
|
|
// The -1 subtraction is some wizardry explained in the OSDev wiki -> GDT
|
|
gdtr.limit = NUM_GDT_ENTRIES * sizeof(uint64_t) - 1;
|
|
gdtr.address = (uint64_t)gdt_entries;
|
|
|
|
// Load the GDT we created, flush the old one
|
|
gdt_load();
|
|
gdt_flush();
|
|
|
|
serial_kputs("kernel: gdt: Initialized GDT!\n");
|
|
} |