From bd60d787b3691eaf04ef1d7e7f779466c62818f6 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sun, 15 Sep 2019 00:03:16 +0200 Subject: Began global descriptor table --- src/gdt/gdt.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/gdt/gdt.h | 6 ++++++ 2 files changed, 57 insertions(+) create mode 100644 src/gdt/gdt.c create mode 100644 src/gdt/gdt.h (limited to 'src') diff --git a/src/gdt/gdt.c b/src/gdt/gdt.c new file mode 100644 index 0000000..813b432 --- /dev/null +++ b/src/gdt/gdt.c @@ -0,0 +1,51 @@ +struct gdt_entry { + unsigned short limit_low; + unsigned short base_low; + unsigned char base_middle; + unsigned char access; + unsigned char granularity; + unsigned char base_high; +} __attribute__((packed)); + +struct gdt_ptr { + unsigned short limit; + unsigned int base; +} __attribute__((packed)); + +struct gdt_entry gdt[3]; +struct gdt_ptr gp; + +extern void gdt_flush(); + +void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran) { + // Set descriptor base address + gdt[num].base_low = (base & 0xFFFF); + gdt[num].base_middle = (base >> 16) & 0xFF; + gdt[num].base_high = (base >> 24) & 0xFF; + + // Set descriptor limits + gdt[num].limit_low = (limit & 0xFFFF); + gdt[num].granularity = ((limit >> 16) & 0x0F); + + // Set granularity and access flags + gdt[num].granularity |= (gran & 0xF0); + gdt[num].access = access; +} + +void gdt_install() { + // Set GDT pointer and limit + gp.limit = (sizeof(struct gdt_entry) * 3) - 1; + gp.base = &gdt; + + // NULL descriptor + gdt_set_gate(0, 0, 0, 0, 0); + + // Code segment + gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); + + // Data segment + gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); + + // Remove old GDT and install the new changes! + gdt_flush(); +} \ No newline at end of file diff --git a/src/gdt/gdt.h b/src/gdt/gdt.h new file mode 100644 index 0000000..46d80e2 --- /dev/null +++ b/src/gdt/gdt.h @@ -0,0 +1,6 @@ +#ifndef MELVIX_GDT_H +#define MELVIX_GDT_H + +void gdt_install(); + +#endif -- cgit v1.2.3