-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegmentation.c
61 lines (52 loc) · 1.87 KB
/
segmentation.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
#include "segmentation.h"
#include "gdt.h"
#include "log.h"
#include "string.h"
SegmentDescriptor gdt[5];
void init_segmentation() {
// leave 0 (null) segment as is
// segment 1 is for code
gdt[1].base_31_24 = 0x00;
// 0b1100 (4 byte granularity, 32-bit operations, no 64-bit operations)
// 0b1111 (high nibble of segment limit)
// 0b1001 (present in memory, privilege = 0, data/code segment
// 0b1010 (execute/read, non-conforming, non-accessed)
gdt[1].flags = 0xCF9A;
gdt[1].base_23_16 = 0x00;
gdt[1].base_15_0 = 0x0000;
gdt[1].limit = 0xFFFF;
// segment 2 is for data
gdt[2].base_31_24 = 0x00;
// 0b1100 (4 byte granularity, 32-bit operations, no 64-bit operations)
// 0b1111 (high nibble of segment limit)
// 0b1001 (present in memory, privilege = 0, data/code segment
// 0b0010 (read/write, no-expand down [?], non-accessed)
gdt[2].flags = 0xCF92;
gdt[2].base_23_16 = 0x00;
gdt[2].base_15_0 = 0x0000;
gdt[2].limit = 0xFFFF;
// segment 3 is for user code
gdt[3].base_31_24 = 0x00;
// 0b1100 (4 byte granularity, 32-bit operations, no 64-bit operations)
// 0b1111 (high nibble of segment limit)
// 0b1001 (present in memory, privilege = 3, data/code segment
// 0b1010 (execute/read, non-conforming, non-accessed)
gdt[3].flags = 0xCFFA;
gdt[3].base_23_16 = 0x00;
gdt[3].base_15_0 = 0x0000;
gdt[3].limit = 0xFFFF;
// segment 4 is for user data
gdt[4].base_31_24 = 0x00;
// 0b1100 (4 byte granularity, 32-bit operations, no 64-bit operations)
// 0b1111 (high nibble of segment limit)
// 0b1001 (present in memory, privilege = 3, data/code segment
// 0b0010 (read/write, no-expand down [?], non-accessed)
gdt[4].flags = 0xCFF2;
gdt[4].base_23_16 = 0x00;
gdt[4].base_15_0 = 0x0000;
gdt[4].limit = 0xFFFF;
GDTSpec gdt_spec;
gdt_spec.address = (unsigned int)gdt;
gdt_spec.size = sizeof(gdt);
lgdt(&gdt_spec);
}