-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdt.c
31 lines (23 loc) · 1.04 KB
/
gdt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "gdt.h"
extern void load_gdt(struct gdt_ptr*);
struct gdt_entry gdt_entries[5];
struct gdt_ptr gdt_ptr;
static void gdt_set_gate(int32_t entry, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) {
gdt_entries[entry].base_low = (base & 0xFFFF);
gdt_entries[entry].base_middle = (base >> 16) & 0xFF;
gdt_entries[entry].base_high = (base >> 24) & 0xFF;
gdt_entries[entry].limit_low = (limit & 0xFFFF);
gdt_entries[entry].granularity = (limit >> 16) & 0x0F;
gdt_entries[entry].granularity |= gran & 0xF0;
gdt_entries[entry].access = access;
}
void gdt_init() {
gdt_ptr.limit = (sizeof(struct gdt_entry) * 5) - 1;
gdt_ptr.base = (uint32_t)&gdt_entries;
gdt_set_gate(0, 0, 0, 0, 0); // Null segment
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); // Code segment
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); // Data segment
gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); // User mode code segment
gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); // User mode data segment
load_gdt(&gdt_ptr);
}