aboutsummaryrefslogtreecommitdiff
path: root/src/utils/gdt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/gdt.c')
-rw-r--r--src/utils/gdt.c56
1 files changed, 48 insertions, 8 deletions
diff --git a/src/utils/gdt.c b/src/utils/gdt.c
index e82ebe4..fd86f5f 100644
--- a/src/utils/gdt.c
+++ b/src/utils/gdt.c
@@ -2,11 +2,14 @@
#include "mem.h"
struct GDT_REGISTER GDTR = { 0, 0 };
+GDT_TSS TSS;
void gdt_memcpy(){
- GDTR.limit=8*4; // Each entry is 8 bytes and 4 entries
+ GDTR.limit=8*8; // Each entry is 8 bytes and 8 entries
GDTR.base=0x800;
-
+ gdt_write_entry((GDT_ENTRY){0,0,0,0},0); // First one must be null
+
+ // ----- Kernel Segments
GDT_ENTRY cs_desc;
cs_desc.base=0;
cs_desc.limit=0xFFFFF;
@@ -26,13 +29,50 @@ void gdt_memcpy(){
ss_desc.access=GDT_PR|GDT_PRVL_0|GDT_S|GDT_RW|GDT_DC;
// Write GDT descriptors into memory
- gdt_write_entry((GDT_ENTRY){0,0,0,0},GDTR.base); // First one must be null
- gdt_write_entry(cs_desc, GDTR.base+8); // Each entry is 64 bits (8 bytes)
- gdt_write_entry(ds_desc, GDTR.base+8*2);
- gdt_write_entry(ss_desc, GDTR.base+8*3);
+ gdt_write_entry(cs_desc, 1);
+ gdt_write_entry(ds_desc, 2);
+ gdt_write_entry(ss_desc, 3);
+
+ // ----- User Segments
+ GDT_ENTRY ucs_desc;
+ ucs_desc.base=0x300000;
+ ucs_desc.limit=0xFFFFF;
+ ucs_desc.flags=GDT_SZ|GDT_GR;
+ ucs_desc.access=GDT_PR|GDT_PRVL_3|GDT_S|GDT_EXEC|GDT_RW|GDT_DC;
+
+ GDT_ENTRY uds_desc;
+ uds_desc.base=0x300000;
+ uds_desc.limit=0xFFFFF;
+ uds_desc.flags=GDT_SZ|GDT_GR;
+ uds_desc.access=GDT_PR|GDT_PRVL_3|GDT_S|GDT_RW;
+
+ GDT_ENTRY uss_desc;
+ uss_desc.base=0; // Not used in stack descriptor
+ uss_desc.limit=0x64; // Define how much entry it can contains
+ uss_desc.flags=GDT_SZ|GDT_GR;
+ uss_desc.access=GDT_PR|GDT_PRVL_3|GDT_S|GDT_RW|GDT_DC;
+
+ // Write GDT descriptors into memory
+ gdt_write_entry(ucs_desc, 4); // Each entry is 64 bits (8 bytes)
+ gdt_write_entry(uds_desc, 5);
+ gdt_write_entry(uss_desc, 6);
+
+ // Init TSS segment
+ TSS.t_reserved=0;
+ TSS.io_map=0;
+ TSS.ss0=0x18;
+ TSS.esp0=0x50000;
+
+ GDT_ENTRY tss_desc;
+ tss_desc.base=(u32)&TSS; // Not used in stack descriptor
+ tss_desc.limit=0x68; // Define how much entry it can contains
+ tss_desc.flags=0;
+ tss_desc.access=0x89 | GDT_PRVL_3; // Note that 0x89 is specific to TSS!
+ gdt_write_entry(tss_desc, 7);
+
}
-void gdt_write_entry(GDT_ENTRY entry, u32 addr){
+void gdt_write_entry(GDT_ENTRY entry, u32 id){
int descriptor[2];
// First row of the descriptor
@@ -47,5 +87,5 @@ void gdt_write_entry(GDT_ENTRY entry, u32 addr){
descriptor[1]=base|access<<8|limit<<16|flags<<20|base2<<24;
// Copy descriptor into memory
- memcpy(descriptor,(void*)addr,8);
+ memcpy(descriptor,(void*)GDTR.base+8*id,8); // Each entry is 64 bits (8 bytes)
}