-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.c
121 lines (90 loc) · 2.65 KB
/
tables.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
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
/**
* This file contains the C code for setting up the GDT and the IDT. Although
* they are not logically very similar, they were merged because they are
* similar enough in structure.
*
* References
* http://www.osdever.net/bkerndev/Docs/gdt.htm
* http://wiki.osdev.org/GDT_Tutorial
* http://wiki.osdev.org/GDT
* http://www.osdever.net/bkerndev/Docs/idt.htm
* http://wiki.osdev.org/Interrupt_Descriptor_Table
*/
#include "irq.h"
#include "tables.h"
#include "vm.h"
#include "x86.h"
struct gdt_entry {
uint16_t limit0;
uint16_t base0;
uint8_t base16;
uint8_t access;
uint8_t limit16 : 4;
uint8_t flags : 4;
uint8_t base24;
} GDT[6];
struct idt_entry {
uint16_t base0;
uint16_t selector; /* offset into the GDT */
uint8_t reserved; /* "reserved" fields must be 0 */
uint8_t attr; /* constant values and variable DPL */
uint16_t base16;
} IDT[0x100];
struct tbl_ptr {
uint16_t limit;
size_t address;
} __attribute__((packed))
gdtp, idtp;
void gdt_set_entry(size_t idx, size_t base, size_t limit, uint8_t access)
{
GDT[idx].limit0 = limit & 0xFFFF;
GDT[idx].limit16 = (limit >> 16) & 0x0F;
GDT[idx].base0 = base & 0xFFFF;
GDT[idx].base16 = (base >> 16) & 0xFF;
GDT[idx].base24 = (base >> 24) & 0xFF;
if (access == GDT_ACCESS_TSS) {
GDT[idx].access = 0x80 | access;
GDT[idx].flags = 0;
} else {
GDT[idx].access = 0x90 | GDT_ACCESS_WRITE | access;
GDT[idx].flags = 0xC;
}
}
void gdt_add_entry(size_t base, size_t limit, uint8_t access)
{
static uint8_t idx = 0;
gdt_set_entry(idx, base, limit, access);
idx++;
}
void gdt_init(void)
{
gdtp.limit = sizeof(GDT) - 1;
gdtp.address = (size_t) GDT;
/* NULL segment */
gdt_add_entry(0, 0, 0);
/* kernel code segment */
gdt_add_entry(MEM_START, MEM_END, GDT_ACCESS_KMODE | GDT_ACCESS_EXEC);
/* kernel data segment */
gdt_add_entry(MEM_START, MEM_END, GDT_ACCESS_KMODE);
/* user code segment */
gdt_add_entry(MEM_START, MEM_END, GDT_ACCESS_UMODE | GDT_ACCESS_EXEC);
/* user data segment */
gdt_add_entry(MEM_START, MEM_END, GDT_ACCESS_UMODE);
/* Defined in `bootstrap.S` */
lgdt();
}
void idt_encode(size_t idx, size_t addr, uint16_t sel, uint8_t dpl)
{
IDT[idx].base0 = addr & 0xFFFF;
IDT[idx].base16 = (addr >> 16) & 0xFFFF;
IDT[idx].selector = sel;
IDT[idx].attr = IDT_ATTR_PRESENT | IDT_ATTR_INTERRUPT | (dpl << 5);
IDT[idx].reserved = 0x00;
}
void idt_init(void)
{
idtp.limit = sizeof(IDT) - 1;
idtp.address = (size_t) IDT;
irq_setup();
lidt(); /* Defined in `bootstrap.S` */
}