summaryrefslogtreecommitdiff
path: root/src/gdt.c
blob: 22489ac4ab515b9737f14640880417a811eb020d (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <gdt.h>

struct gdt_entry {
	uint16_t limit_low;
	uint16_t base_low;
	uint8_t base_middle_low;
	uint8_t access;
	uint8_t gran;
	uint8_t base_middle_high;
	uint32_t base_high;
	uint32_t reseved;
} __attribute__((packed));

struct gdt {
	uint16_t limit;
	uint64_t base;
} __attribute__((packed));

struct gdt_entry gdt[3];
struct gdt gdt_ptr;

extern void gdt_flush();

void gdt_set_gate(size_t num, uint64_t base, uint32_t limit, uint8_t access, uint8_t gran) {
	gdt[num] = (struct gdt_entry) {
		.base_low = base & 0xFFFF,
		.base_middle_low = (base >> 16) & 0xFF,
		.base_middle_high = (base >> 24) & 0xFF,
		.base_high = (base >> 32) & 0xFFFFFFFF,
		.limit_low = limit & 0xFFFF,
		.gran = ((limit >> 16) & 0x0F) | (gran & 0xF0),
		.access = access
	};
}

void gdt_install() {
	gdt_ptr.limit = (sizeof(struct gdt_entry) * 3) - 1;
	gdt_ptr.base = (uint64_t) &gdt;

	gdt_set_gate(0, 0, 0, 0, 0);
	gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
	gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
	gdt_flush();
}