extern void _loadgdt(); struct gdt_entry { unsigned short limit_low; unsigned short base_low; unsigned char base_middle; unsigned char access; unsigned char granularity; unsigned char base_high; } __attribute__((packed)); struct gdt_entry gdt[3]; void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran) { /* Ustawianie adres bazowy segmentu */ gdt[num].base_low = (base & 0xFFFF); gdt[num].base_middle = (base >> 16) & 0xFF; gdt[num].base_high = (base >> 24) & 0xFF; /* Ustawianie limtu segmentu */ gdt[num].limit_low = (limit & 0xFFFF); gdt[num].granularity = ((limit >> 16) & 0x0F); /* Ustaw rodzaj deskryptora oraz informacje o segmencie */ gdt[num].granularity |= (gran & 0xF0); gdt[num].access = access; } struct gdt_ptr { unsigned short limit; // Limit GDT unsigned int base; // Adres bazowy GDT } __attribute__((packed)); struct gdt_ptr _gdtptr; void gdt_install() { /* Ustaw wskaźnik */ _gdtptr.limit = (sizeof(struct gdt_entry) * 3) - 1; _gdtptr.base = (unsigned int)&gdt; /* Deskryptor zerowy */ gdt_set_gate(0, 0, 0, 0, 0); /* Deskryptor segmentu kodu: G = 1, D/B = 1, P = 1, DPL = 0, S=1, TYPE = 0x0A Adres bazowy: 0x0, Limit: 0xFFFFFFFF */ gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); /* Deskryptor segmentu danych: G = 1, D/B = 1, P = 1, DPL = 0, S=1, TYPE = 0x02 Adres bazowy: 0x0, Limit: 0xFFFFFFFF */ gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); /* Zaktualizuj rejestr GDTR */ _loadgdt(); }