LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
gdt.c
Go to the documentation of this file.
1
9#include <arch/x86_64/gdt.h>
10#include <lib/kstring.h>
11#include <stdbool.h>
12#include <kernel/tools.h>
13#include <kernel/log.h>
14#include <stdint.h>
15
16struct gdt_entry gdt[7];
17struct gdtr _gdtr;
18struct tss tss_data;
19
20static uint8_t ist1_stack[16384] __attribute__((aligned(16)));
21static uint8_t ist2_stack[16384] __attribute__((aligned(16)));
22static uint8_t ist3_stack[16384] __attribute__((aligned(16)));
23static uint8_t ist4_stack[16384] __attribute__((aligned(16)));
24static uint8_t ist5_stack[16384] __attribute__((aligned(16)));
25static uint8_t ist6_stack[16384] __attribute__((aligned(16)));
26static uint8_t ist7_stack[16384] __attribute__((aligned(16)));
27static uint8_t kernel_stack[16384] __attribute__((aligned(16)));
28
29void init_gdt(void) {
30 LOG_DEBUG("Initializing GDT...\n");
31 LOG_DEBUG("Setting Entries...\n");
32
33 gdt_set_entry(1, get_access_byte(true, true, CODE_DATA_SEGMENT, 0), get_flag_byte(LIMIT_4KIB_PAGES, SEGMENT_16_BIT, true)); // kernel code
34 gdt_set_entry(2, get_access_byte(true, false, CODE_DATA_SEGMENT, 0), get_flag_byte(LIMIT_4KIB_PAGES, SEGMENT_16_BIT, false)); // kernel data
35 gdt_set_entry(3, get_access_byte(true, false, CODE_DATA_SEGMENT, 3), get_flag_byte(LIMIT_4KIB_PAGES, SEGMENT_16_BIT, false)); // user data
36 gdt_set_entry(4, get_access_byte(true, true, CODE_DATA_SEGMENT, 3), get_flag_byte(LIMIT_4KIB_PAGES, SEGMENT_16_BIT, true)); // user code
37
38 LOG_DEBUG("Assigning IST Stacks...\n");
39 tss_data.ist[0] = (uint64_t)(ist1_stack + sizeof(ist1_stack));
40 tss_data.ist[1] = (uint64_t)(ist2_stack + sizeof(ist2_stack));
41 tss_data.ist[2] = (uint64_t)(ist3_stack + sizeof(ist3_stack));
42 tss_data.ist[3] = (uint64_t)(ist4_stack + sizeof(ist4_stack));
43 tss_data.ist[4] = (uint64_t)(ist5_stack + sizeof(ist5_stack));
44 tss_data.ist[5] = (uint64_t)(ist6_stack + sizeof(ist6_stack));
45 tss_data.ist[6] = (uint64_t)(ist7_stack + sizeof(ist7_stack));
46
47 tss_data.rsp0 = (uint64_t)(kernel_stack + sizeof(kernel_stack));
48
49 tss_data.iopb_offset = sizeof(struct tss);
50
51 LOG_DEBUG("Creating TSS...\n");
52
53 uint64_t tss_base = (uint64_t) & tss_data;
54 uint64_t tss_limit = sizeof(tss_data) - 1;
55
56 uint64_t tss_entry_1 = 0;
57 uint64_t tss_entry_2 = 0;
58
59 tss_entry_1 |= (tss_limit & 0xFFFF);
60 tss_entry_1 |= (tss_base & 0xFFFFFF) << 16;
61 tss_entry_1 |= 0x89ULL << 40;
62 tss_entry_1 |= ((tss_limit >> 16) & 0xF) << 48;
63 tss_entry_1 |= ((tss_base >> 24) & 0xFF) << 56;
64
65 tss_entry_2 |= (tss_base >> 32);
66
67 LOG_DEBUG("Copying TSS to GDT table...\n");
68
69 memcpy(&gdt[5], &tss_entry_1, 8);
70 memcpy(&gdt[6], &tss_entry_2, 8);
71
72 LOG_DEBUG("Setting Up GDTR...\n");
73 _gdtr.limit = sizeof(gdt) - 1;
74 _gdtr.base = (uint64_t) & gdt;
75
76 LOG_DEBUG("Loading GDT...\n");
77 asm volatile ("lgdt %0" : : "m"(_gdtr) : "memory");
78
79 LOG_DEBUG("Setting Descriptors...\n");
80 asm volatile (
81 "movw $0x10, %%ax\n\t"
82 "movw %%ax, %%ds\n\t"
83 "movw %%ax, %%es\n\t"
84 "movw %%ax, %%ss\n\t"
85 "movw %%ax, %%fs\n\t"
86 "movw %%ax, %%gs\n\t"
87 : : : "rax", "memory"
88 );
89
90 LOG_DEBUG("Setting CS (long jump)...\n");
91 asm volatile (
92 "pushq $0x08\n\t"
93 "leaq 1f(%%rip), %%rax\n\t"
94 "pushq %%rax\n\t"
95 "lretq\n\t"
96 "1:\n\t"
97 : : : "rax", "memory"
98 );
99
100 LOG_DEBUG("Marking TSS as busy...\n");
101 asm volatile (
102 "movw $0x28, %%ax\n\t"
103 "ltr %%ax\n\t"
104 : : : "rax", "memory"
105 );
106
107 LOG_DEBUG("Loaded GDT Successfully\n\n");
108}
109
110void gdt_set_entry(int index, uint8_t access, uint8_t flags) {
111 gdt[index].limit = 0xFFFF;
112 gdt[index].base1 = 0;
113 gdt[index].base2 = 0;
114 gdt[index].base3 = 0;
115
116 gdt[index].flags = flags | 0b00001111;
117 gdt[index].access = access;
118}
119
120uint8_t get_access_byte(bool rw, bool exec, bool type, uint8_t dpl) {
121 return 0x80 // Present
122 | ((dpl & 3) << 5) // Privilege level
123 | (type ? 0x10 : 0x00) // Descriptor type (S)
124 | (exec ? 0x08 : 0x00) // Executable
125 | (rw ? 0x02 : 0x00); // Readable / writable
126}
127
128uint8_t get_flag_byte(bool granularity, bool size, bool long_mode) {
129 return (granularity ? 0x80 : 0)
130 | (size ? 0x40 : 0)
131 | (long_mode ? 0x20 : 0);
132}
uint8_t get_access_byte(bool rw, bool exec, bool type, uint8_t dpl)
Generates the access byte for the GDT.
Definition gdt.c:120
uint8_t get_flag_byte(bool granularity, bool size, bool long_mode)
Generates the flag byte for the GDT.
Definition gdt.c:128
void gdt_set_entry(int index, uint8_t access, uint8_t flags)
Sets an entry of the gdt with the correct access and flags.
Definition gdt.c:110
void init_gdt(void)
Initializes the gdt and loads it.
Definition gdt.c:29
Initializes and holds the GDT for the kernel.
Utilities for memory and string operations.
void * memcpy(void *dest, const void *src, size_t n)
Copies memory from one address to the other without overlapping.
Definition kstring.c:14
Logging utilities for the kernel.
Extra kernel utilities.