forked from xamidev/pepperOS
107 lines
3.0 KiB
C
107 lines
3.0 KiB
C
/*
|
|
* @author xamidev <xamidev@riseup.net>
|
|
* @brief Global Descriptor Table (for legacy reasons)
|
|
* @license GPL-3.0-only
|
|
*/
|
|
|
|
#include "gdt.h"
|
|
#include <stdint.h>
|
|
#include "io/serial/serial.h"
|
|
#include <kernel.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;
|
|
|
|
/*
|
|
* gdt_load - Loads Global Descriptor Table
|
|
*/
|
|
static void gdt_load()
|
|
{
|
|
asm("lgdt %0" : : "m"(gdtr));
|
|
}
|
|
|
|
/*
|
|
* gdt_flush - Flushes the Global Descriptor Table
|
|
*
|
|
* This function loads new Segment Selectors to make
|
|
* the GDT changes take effect
|
|
*/
|
|
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"
|
|
);
|
|
}
|
|
|
|
/*
|
|
* gdt_init - Global Descriptor Table initialization
|
|
*
|
|
* This function loads a new GDT in the CPU.
|
|
* It contains a null descriptor, kernel code and data
|
|
* segments, and user code and data segments.
|
|
* However, we do not use segmentation to manage memory on
|
|
* 64-bit x86, as it's deprecated. Instead, we use paging.
|
|
*/
|
|
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();
|
|
|
|
DEBUG("GDT initialized");
|
|
} |