#include "gdt.h" #include /* GDT Pointer Structure */ struct gdt_ptr gp; /* GDT entries */ struct gdt_entry gdt[5]; extern void gdt_flush(uint32_t); /* Setup a descriptor in the Global Descriptor Table */ void gdt_set_gate(int32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) { /* Setup the descriptor base address */ gdt[num].base_low = (base & 0xFFFF); gdt[num].base_middle = (base >> 16) & 0xFF; gdt[num].base_high = (base >> 24) & 0xFF; /* Setup the descriptor limits */ gdt[num].limit_low = (limit & 0xFFFF); gdt[num].granularity = ((limit >> 16) & 0x0F); /* Finally, set up the granularity and access flags */ gdt[num].granularity |= (gran & 0xF0); gdt[num].access = access; } /* Should be called by main. This will setup the special GDT * pointer, set up the first 3 entries in our GDT, and then * call gdt_flush() in our assembler file in order to tell * the processor where the new GDT is and update the * internal registers */ void init_gdt() { /* Setup the GDT pointer and limit */ gp.limit = (sizeof(struct gdt_entry) * 5) - 1; gp.base = (uint32_t)&gdt; /* Our NULL descriptor */ gdt_set_gate(0, 0, 0, 0, 0); /* The second entry is our Code Segment. The base address * is 0, the limit is 4GBytes, it uses 4KByte granularity, * uses 32-bit opcodes, and is a Code Segment descriptor. * Please check the table above in the tutorial in order * to see exactly what each value means */ /* 0x9A = 1001 1010 P=1, DPL=00, S=1 (Code/Data), Type=1010 (Code, Exec/Read) */ /* 0xCF = 1100 1111 G=1 (4k), DB=1 (32bit), L=0, AVL=0, LimitHigh=0xF */ gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); /* The third entry is our Data Segment. It's EXACTLY the * same as our code segment, but the descriptor type in * this entry's access byte says it's a Data Segment */ /* 0x92 = 1001 0010 P=1, DPL=00, S=1, Type=0010 (Data, Read/Write) */ gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); /* User mode code segment */ gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); /* User mode data segment */ gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); /* Flush out the old GDT and install the new changes! */ gdt_flush((uint32_t)&gp); }