-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdt.zig
182 lines (161 loc) · 5.03 KB
/
gdt.zig
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
extern fn gdtFlushASM() void; // see https://devdocs.io/zig/index#Assembly
const GDT_ENTRY = packed struct {
limit_low: u16,
base_low: u24,
access: GDT_ACCESS,
limit_high: u4,
flags: GDT_FLAGS,
base_high: u8,
};
const GDT_ACCESS = packed struct {
accessed: u1 = 0, //Indicates whether the segment has been accessed. DEFAUL = 0
read_write: u1, // For data segments: Indicates whether the segment is writable. || For code segments: Indicates whether the segment is readable.
conforming_expand_down: u1, // For data segments: Indicates the direction of growth (0 for upward, 1 for downward). || For code segments: Indicates if the code segment is conforming.
executable: u1, // Indicates whether the segment is executable (code segment).
descriptor_type: u1 = 1, // Descriptor type (0 = system, 1 = code/data)
privilege_level: u2, // Indicates the privilege level of the segment (0-3, with 0 being the highest privilege).
present: u1 = 1, // Indicates whether the segment is present in memory.
};
const GDT_FLAGS = packed struct {
reserved: u1 = 0, //always 0
is_64: u1,
is_32: u1,
granularity: u1,
};
const GDT_DESCRIPTOR = packed struct {
size: u16,
base: u32,
};
const GDT_ENTRIES_LEN = 7;
const NULL_SEG = GDT_ACCESS{
.accessed = 0,
.conforming_expand_down = 0,
.descriptor_type = 0,
.executable = 0,
.present = 0,
.privilege_level = 0,
.read_write = 0,
};
const KERNEL_CODE_SEG = GDT_ACCESS{
.accessed = 0,
.read_write = 1,
.conforming_expand_down = 0,
.executable = 1,
.descriptor_type = 1,
.privilege_level = 0,
.present = 1,
};
const KERNEL_DATA_SEG = GDT_ACCESS{
.accessed = 0,
.read_write = 1,
.conforming_expand_down = 0,
.executable = 0,
.descriptor_type = 1,
.privilege_level = 0,
.present = 1,
};
const KERNEL_STACK_SEG = GDT_ACCESS{
.accessed = 0,
.read_write = 1,
.conforming_expand_down = 1, // the stack grows downwards, set to one
.executable = 0,
.descriptor_type = 1,
.privilege_level = 0,
.present = 1,
};
const USER_CODE_SEG = GDT_ACCESS{
.accessed = 0,
.read_write = 0, // check to do
.conforming_expand_down = 0,
.executable = 1,
.descriptor_type = 1,
.privilege_level = 3,
.present = 1,
};
const USER_DATA_SEG = GDT_ACCESS{
.accessed = 0,
.read_write = 1,
.conforming_expand_down = 0,
.executable = 0,
.descriptor_type = 1,
.privilege_level = 3,
.present = 1,
};
const USER_STACK_SEG = GDT_ACCESS{
.accessed = 0,
.read_write = 1,
.conforming_expand_down = 1, // set to 1, as above stack grows down
.executable = 0,
.descriptor_type = 1,
.privilege_level = 3,
.present = 1,
};
const NULL_FLAGS = GDT_FLAGS{
.granularity = 0,
.is_32 = 0,
.is_64 = 0,
.reserved = 0,
};
const BIT32_FLAGS = GDT_FLAGS{
.granularity = 1,
.is_32 = 1,
.is_64 = 0,
.reserved = 0,
};
const gdt_entries: *[GDT_ENTRIES_LEN]GDT_ENTRY = @ptrFromInt(0x800);
const gdt_descriptor = GDT_DESCRIPTOR{
.base = @intFromPtr(gdt_entries),
.size = GDT_ENTRIES_LEN * @sizeOf(GDT_ENTRY) - 1,
};
//Took https://github.com/SamyPesse/How-to-Make-a-Computer-Operating-System/blob/master/Chapter-6/README.md as reference for stack setup
//should find a more in depth tutorial why/how stack is used in that segment
pub const gdt = struct {
pub fn init() void {
gdt_entries[0] = createGdtEntry(0, 0, NULL_SEG, NULL_FLAGS); // first GDT entry has to be NULL
gdt_entries[1] = createGdtEntry(0, 0xFFFFF, KERNEL_CODE_SEG, BIT32_FLAGS); // Kernel Code Segment
gdt_entries[2] = createGdtEntry(0, 0xFFFFF, KERNEL_DATA_SEG, BIT32_FLAGS); // Kernel Data Segment
gdt_entries[3] = createGdtEntry(0, 0x0, KERNEL_STACK_SEG, BIT32_FLAGS); // Kernel Stack Segment
gdt_entries[4] = createGdtEntry(0, 0xFFFFF, USER_CODE_SEG, BIT32_FLAGS); // User Code Segment
gdt_entries[5] = createGdtEntry(0, 0xFFFFF, USER_DATA_SEG, BIT32_FLAGS); // User Data Segment
gdt_entries[6] = createGdtEntry(0, 0x0, USER_STACK_SEG, BIT32_FLAGS); // User Stack Segment
gdtInitFlush();
}
};
pub fn gdtInitFlush() void {
asm volatile ("lgdtl (%%eax)"
:
: [gdt_descriptor] "{eax}" (&gdt_descriptor),
);
gdtFlushASM();
}
limit_low: u16,
base_low: u24,
access: GDT_ACCESS,
limit_high: u4,
flags: GDT_FLAGS,
base_high: u8,
fn createGdtEntry(base: u32, limit: u32, access: GDT_ACCESS, flags: GDT_FLAGS) GDT_ENTRY {
return GDT_ENTRY{
.limit_low = @truncate(limit),
.base_low = @truncate(base),
.access = access,
.limit_high = @truncate(base >> 16),
.flags = flags,
.base_high = @truncate(base >> 24),
};
}
comptime {
asm (
\\.global gdtFlushASM;
\\.type gdtFlushASM, @function;
\\gdtFlushASM:
\\ movw $0x10, %ax
\\ movw %ax, %ds
\\ movw %ax, %es
\\ movw %ax, %fs
\\ movw %ax, %gs
\\ movw %ax, %ss
\\ ljmp $0x08, $next
\\ next: ret
);
}