summaryrefslogtreecommitdiff
path: root/src/kernel/mem/gdt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel/mem/gdt.c')
-rw-r--r--src/kernel/mem/gdt.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/kernel/mem/gdt.c b/src/kernel/mem/gdt.c
index e9a1be5..8ee403a 100644
--- a/src/kernel/mem/gdt.c
+++ b/src/kernel/mem/gdt.c
@@ -2,19 +2,23 @@
#include <stdlib.h>
#include <core/monitor.h>
-extern void gdt_flush(uint32_t); //ASM (idt_.asm)
+extern void gdt_flush(uint32_t); //ASM (imported from idt_.asm)
extern void tss_flush();
-#define GDT_ENTRIES 6
+#define GDT_ENTRIES 6 // The contents of each entry is defined in gdt_init.
static struct tss_entry tss_entry;
static struct gdt_entry gdt_entries[GDT_ENTRIES];
static struct gdt_ptr gdt_ptr;
+/* This function is called by the task_switch function on each context switch.
+ It updates the TSS so that an interrupt will be handled on the correct kernel stack
+ (one kernel stack is allocated to each thread). */
void gdt_setKernelStack(uint32_t esp0) {
tss_entry.esp0 = esp0;
}
+/* For internal use only. Writes one entry of the GDT with given parameters. */
static void gdt_setGate(int num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) {
gdt_entries[num].base_low = (base & 0xFFFF);
gdt_entries[num].base_middle = (base >> 16) & 0xFF;
@@ -26,6 +30,7 @@ static void gdt_setGate(int num, uint32_t base, uint32_t limit, uint8_t access,
gdt_entries[num].access = access;
}
+/* For internal use only. Writes one entry of the GDT, that entry being a pointer to the TSS. */
static void gdt_writeTss(int num, uint32_t ss0, uint32_t esp0) {
uint32_t base = (uint32_t)&tss_entry;
uint32_t limit = base + sizeof(struct tss_entry);
@@ -40,15 +45,16 @@ static void gdt_writeTss(int num, uint32_t ss0, uint32_t esp0) {
tss_entry.ss = tss_entry.ds = tss_entry.es = tss_entry.fs = tss_entry.gs = 0x13;
}
+/* Write data to the GDT and enable it. */
void gdt_init() {
gdt_ptr.limit = (sizeof(struct gdt_entry) * GDT_ENTRIES) - 1;
gdt_ptr.base = (uint32_t)&gdt_entries;
gdt_setGate(0, 0, 0, 0, 0); //Null segment
- gdt_setGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); //Kernel code segment
- gdt_setGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); //Kernel data segment
- gdt_setGate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); //User code segment
- gdt_setGate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); //User data segment
+ gdt_setGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); //Kernel code segment 0x08
+ gdt_setGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); //Kernel data segment 0x10
+ gdt_setGate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); //User code segment 0x18
+ gdt_setGate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); //User data segment 0x20
gdt_writeTss(5, 0x10, 0);
gdt_flush((uint32_t)&gdt_ptr);