From 0e3e4d4769062bd685bfd50e1a3f85e921756658 Mon Sep 17 00:00:00 2001 From: pjht Date: Sat, 9 Feb 2019 13:54:40 -0600 Subject: [PATCH] Add GDT initialization --- cpu/i386/cpu_init.c | 3 +++ cpu/i386/gdt.c | 39 +++++++++++++++++++++++++++++++++++++++ cpu/i386/gdt.h | 6 ++++++ 3 files changed, 48 insertions(+) create mode 100644 cpu/i386/gdt.c create mode 100644 cpu/i386/gdt.h diff --git a/cpu/i386/cpu_init.c b/cpu/i386/cpu_init.c index e662164..6fff686 100644 --- a/cpu/i386/cpu_init.c +++ b/cpu/i386/cpu_init.c @@ -1,2 +1,5 @@ +#include "gdt.h" + void cpu_init() { + gdt_init(); } diff --git a/cpu/i386/gdt.c b/cpu/i386/gdt.c new file mode 100644 index 0000000..c107c24 --- /dev/null +++ b/cpu/i386/gdt.c @@ -0,0 +1,39 @@ +#include +#define NUM_ENTRIES 4 + +typedef struct { + uint16_t limit_low16; + uint16_t base_low16; + uint8_t base_mid8; + uint8_t access; + uint8_t limit_flags; + uint8_t base_high8; +} gdt_entry; + +typedef struct { + uint16_t size; + uint32_t address; +} gdt_description; + +gdt_entry gdt[NUM_ENTRIES-1]; +gdt_description gdt_desc; + + +void gdt_init() { + set_entry(0,0,0,0); + set_entry(1,0,0xFFFFF,0x9E); + set_entry(2,0,0xFFFFF,0x92); + gdt_desc.size=(sizeof(gdt_entry)*NUM_ENTRIES)-1; + gdt_desc.address=&gdt[0]; + asm volatile("lgdt gdt_desc"); +} + +void set_entry(int i,uint32_t base,uint32_t limit,uint8_t access) { + gdt[i].limit_low16=limit&0xFFFF; + gdt[i].base_low16=base&0xFFFFF; + gdt[i].base_mid8=(base&0xFF0000)>>16; + gdt[i].access=access; + uint8_t limit_high4=(limit&0xF0000)>>16; + gdt[i].limit_flags=0xC0&limit_high4; + gdt[i].base_high8=(base&0xFF000000)>>24; +} diff --git a/cpu/i386/gdt.h b/cpu/i386/gdt.h new file mode 100644 index 0000000..2c244f0 --- /dev/null +++ b/cpu/i386/gdt.h @@ -0,0 +1,6 @@ +#ifndef GDT_H +#define GDT_H + +void gdt_init(); + +#endif