2017-11-01 01:26:22 -04:00
|
|
|
#include "gdt.h"
|
|
|
|
|
2017-11-01 01:29:29 -04:00
|
|
|
#include "logger.h"
|
|
|
|
|
2017-11-01 06:47:43 -04:00
|
|
|
struct GdtPointer {
|
|
|
|
unsigned short limit;
|
|
|
|
unsigned int base;
|
|
|
|
}
|
|
|
|
__attribute__((packed));
|
|
|
|
|
|
|
|
struct GdtEntry {
|
|
|
|
unsigned short limit_low;
|
|
|
|
unsigned short base_low;
|
|
|
|
unsigned char base_middle;
|
|
|
|
unsigned char access;
|
|
|
|
unsigned char granularity;
|
|
|
|
unsigned char base_high;
|
|
|
|
}
|
|
|
|
__attribute__((packed));
|
|
|
|
|
2017-11-01 01:26:22 -04:00
|
|
|
static struct GdtPointer gdt_pointer;
|
|
|
|
|
|
|
|
static struct GdtEntry gdt_entries[5];
|
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
static void gdt_set_gate(int num, unsigned int base, unsigned int limit, unsigned char access, unsigned char gran);
|
2017-11-01 01:26:22 -04:00
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
void gdt_flush(unsigned int pointer);
|
2017-11-01 01:26:22 -04:00
|
|
|
|
|
|
|
void gdt_initialize()
|
|
|
|
{
|
2017-11-01 01:29:29 -04:00
|
|
|
logger_info("Setup GDT.");
|
|
|
|
|
2017-11-01 01:26:22 -04:00
|
|
|
gdt_set_gate(0, 0, 0, 0, 0); // Null segment
|
|
|
|
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); // Code segment
|
|
|
|
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); // Data segment
|
|
|
|
gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); // User mode code segment
|
|
|
|
gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); // User mode data segment
|
|
|
|
|
2017-11-01 01:29:29 -04:00
|
|
|
logger_info("Load GDT.");
|
|
|
|
|
2017-11-01 01:26:22 -04:00
|
|
|
gdt_pointer.limit = sizeof(struct GdtEntry) * 5 - 1;
|
2017-11-01 06:08:09 -04:00
|
|
|
gdt_pointer.base = (unsigned int)&gdt_entries;
|
2017-11-01 01:26:22 -04:00
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
gdt_flush((unsigned int)&gdt_pointer);
|
2017-11-01 01:26:22 -04:00
|
|
|
}
|
|
|
|
|
2017-11-01 06:08:09 -04:00
|
|
|
void gdt_set_gate(int num, unsigned int base, unsigned int limit, unsigned char access, unsigned char gran)
|
2017-11-01 01:26:22 -04:00
|
|
|
{
|
|
|
|
gdt_entries[num].base_low = (base & 0xFFFF);
|
|
|
|
gdt_entries[num].base_middle = (base >> 16) & 0xFF;
|
|
|
|
gdt_entries[num].base_high = (base >> 24) & 0xFF;
|
|
|
|
|
|
|
|
gdt_entries[num].limit_low = (limit & 0xFFFF);
|
|
|
|
gdt_entries[num].granularity = (limit >> 16) & 0x0F;
|
|
|
|
|
|
|
|
gdt_entries[num].granularity |= gran & 0xF0;
|
|
|
|
|
|
|
|
gdt_entries[num].access = access;
|
|
|
|
}
|