forked from ReKernel/ReKernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgdt.c
More file actions
executable file
·46 lines (38 loc) · 1.39 KB
/
Copy pathgdt.c
File metadata and controls
executable file
·46 lines (38 loc) · 1.39 KB
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
#include "gdt.h"
/* Three entries: null, kernel code (0x08), kernel data (0x10) */
typedef struct __attribute__((packed)) {
uint16_t limit_lo;
uint16_t base_lo;
uint8_t base_mid;
uint8_t access;
uint8_t granularity; /* high 4 bits = flags, low 4 = limit_hi */
uint8_t base_hi;
} gdt_entry_t;
typedef struct __attribute__((packed)) {
uint16_t limit;
uint32_t base;
} gdt_ptr_t;
static gdt_entry_t gdt[3];
static gdt_ptr_t gdt_ptr;
static void set_entry(int i,
uint32_t base, uint32_t limit,
uint8_t access, uint8_t gran)
{
gdt[i].base_lo = (uint16_t)(base & 0xFFFF);
gdt[i].base_mid = (uint8_t)((base >> 16) & 0xFF);
gdt[i].base_hi = (uint8_t)((base >> 24) & 0xFF);
gdt[i].limit_lo = (uint16_t)(limit & 0xFFFF);
gdt[i].granularity = (uint8_t)(((limit >> 16) & 0x0F) | (gran & 0xF0));
gdt[i].access = access;
}
/* Defined in gdt.asm — flushes segment registers after lgdt */
extern void gdt_flush(uint32_t gdt_ptr_addr);
void gdt_init(void)
{
gdt_ptr.limit = (uint16_t)(sizeof(gdt) - 1);
gdt_ptr.base = (uint32_t)&gdt;
set_entry(0, 0, 0, 0x00, 0x00); /* null descriptor */
set_entry(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); /* kernel code: ring0, exec/read */
set_entry(2, 0, 0xFFFFFFFF, 0x92, 0xCF); /* kernel data: ring0, read/write */
gdt_flush((uint32_t)&gdt_ptr);
}