aboutsummaryrefslogtreecommitdiff
path: root/kernel/l0/gdt.c
diff options
context:
space:
mode:
authorAlex Auvolat <alex.auvolat@ens.fr>2014-12-01 17:24:38 +0100
committerAlex Auvolat <alex.auvolat@ens.fr>2014-12-01 17:24:38 +0100
commit0921a33fc9f3724cb36a03a24b58b0a5bfc519b1 (patch)
tree6f3db52ba8c38928749806cb2d94ca548b3b19bd /kernel/l0/gdt.c
parent6f11c9e5c15cc8ef936741e5ee7575b731a47e0f (diff)
downloadkogata-0921a33fc9f3724cb36a03a24b58b0a5bfc519b1.tar.gz
kogata-0921a33fc9f3724cb36a03a24b58b0a5bfc519b1.zip
Advance HH, GDT, IDT (details below)
- Implement higher-half in loader with paging (using 4Mb pages) - Add GDT installation code - Add IDT installation code (spend a loong time debugging the ISRs!) - Add CDROM generation scripts - Add scripts to launch bochs (debug) and qemu (debug)
Diffstat (limited to 'kernel/l0/gdt.c')
-rw-r--r--kernel/l0/gdt.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/kernel/l0/gdt.c b/kernel/l0/gdt.c
new file mode 100644
index 0000000..9ed5210
--- /dev/null
+++ b/kernel/l0/gdt.c
@@ -0,0 +1,35 @@
+#include <gdt.h>
+#include <string.h>
+
+#define GDT_ENTRIES 6 // The contents of each entry is defined in gdt_init.
+
+static gdt_entry_t gdt_entries[GDT_ENTRIES];
+static gdt_ptr_t gdt_ptr;
+
+/* 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;
+ 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;
+}
+
+/* Write data to the GDT and enable it. */
+void gdt_init() {
+ gdt_ptr.limit = (sizeof(gdt_entry_t) * 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 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
+
+ asm volatile ("lgdt %0"::"m"(gdt_ptr):"memory");
+}
+
+/* vim: set ts=4 sw=4 tw=0 noet :*/