diff options
Diffstat (limited to 'src/utils/gdt.c')
| -rw-r--r-- | src/utils/gdt.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/utils/gdt.c b/src/utils/gdt.c new file mode 100644 index 0000000..85c4a09 --- /dev/null +++ b/src/utils/gdt.c @@ -0,0 +1,52 @@ +#include "gdt.h" +#include "print.h" +#include "mem.h" + +struct GDT_REGISTER GDTR = { 0, 0 }; + +void gdt_memcpy(){ + GDTR.limit=8*4; // Each entry is 8 bytes and 4 entries + GDTR.base=0x800; + + GDT_ENTRY cs_desc; + cs_desc.base=0; + cs_desc.limit=0xFFFFF; + cs_desc.flags=GDT_SZ|GDT_GR; + cs_desc.access=GDT_PR|GDT_PRVL_0|GDT_S|GDT_EXEC|GDT_RW; + + GDT_ENTRY ds_desc; + ds_desc.base=0; + ds_desc.limit=0xFFFFF; + ds_desc.flags=GDT_SZ|GDT_GR; + ds_desc.access=GDT_PR|GDT_PRVL_0|GDT_S|GDT_RW; + + GDT_ENTRY ss_desc; + ss_desc.base=0; + ss_desc.limit=0; + ss_desc.flags=GDT_SZ|GDT_GR; + 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); +} + +void gdt_write_entry(GDT_ENTRY entry, u32 addr){ + int descriptor[2]; + + // First row of the descriptor + descriptor[0]=(entry.limit & 0xFFFF)|(entry.base << 16); + + // Format second row of the descriptor + u16 base=(entry.base >> 16) & 0xFF; + u16 access=entry.access & 0xFF; + u16 limit=(entry.limit >> 16) & 0xF; // Remember: limits it is on 20 bits so 4 last bits + u8 flags=entry.flags & 0xF; + u8 base2=entry.base >> 24; // Take the last 8 bits + descriptor[1]=base|access<<8|limit<<16|flags<<20|base2<<24; + + // Copy descriptor into memory + memcpy(descriptor,(void*)addr,8); +} |
