-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdt.c
64 lines (52 loc) · 1.53 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "gdt.h"
extern long start32;
union gdt_entry gdt[segment_gdt_idx_max]
__attribute__((section(".bits16.data")));
struct gdt_descriptor gdt_descriptor
__attribute__((section(".bits16.data")));
const int kernel_ds = kernel_data * sizeof (union gdt_entry);
__attribute__((section(".bits16.code")))
void init_gdt(void)
{
gdt[null].w1 = 0;
gdt[null].w2 = 0;
for (int i = 0; i < segment_gdt_idx_max; ++i)
{
gdt[i].base_low = 0;
gdt[i].base_mid = 0;
gdt[i].base_hi = 0;
gdt[i].limit_low = ~0;
gdt[i].limit_hi = ~0;
gdt[i].granularity = one_page;
gdt[i].size = bits_32;
gdt[i].long_mode = 0;
gdt[i].reserved = 0;
gdt[i].present = 1;
gdt[i].type = code_or_data;
gdt[i].accessed = 0;
}
gdt[kernel_code].dpl = 0;
gdt[kernel_code].executable = 1;
gdt[kernel_code].rw = 1;
gdt[kernel_code].conforming_or_direction = 0;
gdt[kernel_data].dpl = 0;
gdt[kernel_data].executable = 0;;
gdt[kernel_data].rw = 1;
gdt[kernel_data].conforming_or_direction = expand_up;
gdt[user_code].dpl = 3;
gdt[user_code].executable = 1;
gdt[user_code].rw = 1;
gdt[user_code].conforming_or_direction = 0;
gdt[user_data].dpl = 3;
gdt[user_data].executable = 0;
gdt[user_data].rw = 1;
gdt[user_data].conforming_or_direction = expand_up;
gdt_descriptor.size = sizeof gdt - 1;
gdt_descriptor.address = (long)gdt;
asm volatile("cli");
asm volatile("lgdt (%0)" : : "m"(gdt_descriptor));
asm volatile("mov %%cr0, %%eax" :);
asm volatile("or $1, %%al" :);
asm volatile("mov %%eax, %%cr0" :);
asm volatile("ljmp $0x08, $%0" : : "m"(start32));
}