Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
gdt.c
Go to the documentation of this file.
1#include "gdt.h"
2#include "init/init.h"
3#include "libk/serial.h"
4#include "procc/thread.h"
5#include <libk/debug/debug.h>
6#include <str.h>
9
10extern void reloadGDT(int cs, int ds);
11
12#define TSS_LOW 0x28ULL
13#define TSS_HIGH 0x30ULL
14
15static gdt_each_core_t __gdt_entries[VOXIA_MAX_CORE];
16uint8_t ap_stack_top[VOXIA_MAX_CORE][65536] __attribute__((aligned(16))); // 64KB
17
18__attribute__((no_stack_protector)) void setup_gdt(int core) {
19 gdt_entry_t* entries = __gdt_entries[core].entries;
20
21 entries[0] = gdt_make_entry(0, 0, 0, 0); // 0x0
22 entries[1] = gdt_make_entry(0, 0xFFFF, 0x9A, 0x00); // 0x08
23 entries[2] = gdt_make_entry(0, 0xFFFF, 0x92, 0x00);// 0x10
24 entries[3] = gdt_make_entry(0, 0xFFFF, 0x9A, 0xCF);
25 entries[4] = gdt_make_entry(0, 0xFFFF, 0x92, 0xCF);
26 entries[5] = gdt_make_entry(0, 0, 0x9A, 0x20); // Kernel CS 0x28
27 entries[6] = gdt_make_entry(0, 0, 0x92, 0x00); // Kernel DS 0x30
28 entries[7] = gdt_make_entry(0, 0, 0xFA, 0xCF); // User CS 32-bit (Base for SYSRET) 0x38
29 entries[8] = gdt_make_entry(0, 0, 0xF2, 0x00); // User SS 64-bit (Base + 8) 0x40
30 entries[9] = gdt_make_entry(0, 0, 0xFA, 0x20); // User CS 64-bit (Base + 16) 0x48
31
32 lm_tss_t* _tss = &__gdt_entries[core].tss;
33 _tss->rsp[0] =
35 _tss->rsp[1] = 0;
36 _tss->rsp[2] = 0;
37 _tss->reserved = 0;
38 _tss->reserved2 = 0;
39 _tss->reserved3 = 0;
40 _tss->reserved4 = 0;
41 _tss->ist[0] = _tss->ist[1] = _tss->ist[2] = _tss->ist[3] = 0;
42 _tss->ist[4] = _tss->ist[5] = _tss->ist[6] = 0;
43 _tss->iomap_base = sizeof(*_tss);
44
45 uint64_t base = (uint64_t) _tss;
46 uint16_t limit = (uint16_t) (sizeof(*_tss) - 1);
47
48 entries[10].limit_low = limit;
49 entries[10].base_low = (uint16_t) (base & 0xFFFF);
50 entries[10].base_middle = (uint8_t) ((base >> 16) & 0xFF);
51 entries[10].access = 0x89;
52 entries[10].flags = 0x00;
53 entries[10].base_high = (uint8_t) ((base >> 24) & 0xFF);
54
55 uint64_t* tss_high = (uint64_t*) ((uintptr_t) &entries[11]);
56 *tss_high = (base >> 32) & 0xFFFFFFFF;
57
58 gdt_ptr_t* _gdt_ptr = &__gdt_entries[core].pointer;
59 _gdt_ptr->limit = (uint16_t) (sizeof(gdt_entry_t) * 14 - 1);
60 _gdt_ptr->base = (uint64_t) entries;
61
62 gdt_flush(*_gdt_ptr);
63 __asm__ volatile("ltr %%ax" : : "a"((uint16_t) 0x50));
64 reloadGDT(0x28, 0x30);
65}
66
68 lm_tss_t* _tss = &__gdt_entries[core].tss;
69 _tss->rsp[0] = stack_top;
70}
71
72INIT(Gdt) {
73 setup_gdt(0);
74}
75
76gdt_entry_t
78 gdt_entry_t entry;
79 entry.base_low = base & 0xFFFF;
80 entry.base_middle = (base >> 16) & 0xFF;
81 entry.base_high = (base >> 24) & 0xFF;
82 entry.limit_low = limit;
83 entry.flags = flags;
84 entry.access = access;
85
86 return entry;
87}
88
89void gdt_flush(gdt_ptr_t p) {
90 __asm__ volatile("lgdt %0" : : "memory"(p));
91}
void setup_gdt(int core)
uint8_t ap_stack_top[VOXIA_MAX_CORE][65536]
elf_section_map uintptr_t base
Definition elf.h:296
gdt_entry_t gdt_make_entry(uint32_t base, uint16_t limit, uint8_t access, uint8_t flags)
Definition gdt.c:77
void reloadGDT(int cs, int ds)
void set_tss_stack(uint16_t core, uintptr_t stack_top)
Definition gdt.c:67
static gdt_each_core_t __gdt_entries[VOXIA_MAX_CORE]
Definition gdt.c:15
void gdt_flush(gdt_ptr_t p)
Definition gdt.c:89
uint16_t flags
Definition thread.h:5
typedef __attribute__
Definition msi.c:47
#define INIT(fn)
Definition init.h:26
unsigned short uint16_t
Definition type.h:13
unsigned int uint32_t
Definition type.h:19
unsigned long uintptr_t
Definition type.h:73
unsigned long uint64_t
Definition type.h:25
unsigned char uint8_t
Definition type.h:7
int core
Definition vm_manager.h:5