30 LOG_DEBUG(
"Initializing GDT...\n");
31 LOG_DEBUG(
"Setting Entries...\n");
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));
47 tss_data.rsp0 = (uint64_t)(kernel_stack +
sizeof(kernel_stack));
49 tss_data.iopb_offset =
sizeof(
struct tss);
51 LOG_DEBUG(
"Creating TSS...\n");
53 uint64_t tss_base = (uint64_t) & tss_data;
54 uint64_t tss_limit =
sizeof(tss_data) - 1;
56 uint64_t tss_entry_1 = 0;
57 uint64_t tss_entry_2 = 0;
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;
65 tss_entry_2 |= (tss_base >> 32);
67 LOG_DEBUG(
"Copying TSS to GDT table...\n");
69 memcpy(&gdt[5], &tss_entry_1, 8);
70 memcpy(&gdt[6], &tss_entry_2, 8);
72 LOG_DEBUG(
"Setting Up GDTR...\n");
73 _gdtr.limit =
sizeof(gdt) - 1;
74 _gdtr.base = (uint64_t) & gdt;
76 LOG_DEBUG(
"Loading GDT...\n");
77 asm volatile (
"lgdt %0" : :
"m"(_gdtr) :
"memory");
79 LOG_DEBUG(
"Setting Descriptors...\n");
81 "movw $0x10, %%ax\n\t"
90 LOG_DEBUG(
"Setting CS (long jump)...\n");
93 "leaq 1f(%%rip), %%rax\n\t"
100 LOG_DEBUG(
"Marking TSS as busy...\n");
102 "movw $0x28, %%ax\n\t"
104 : : :
"rax",
"memory"
107 LOG_DEBUG(
"Loaded GDT Successfully\n\n");
uint8_t get_access_byte(bool rw, bool exec, bool type, uint8_t dpl)
Generates the access byte for the GDT.
uint8_t get_flag_byte(bool granularity, bool size, bool long_mode)
Generates the flag byte for the GDT.
void gdt_set_entry(int index, uint8_t access, uint8_t flags)
Sets an entry of the gdt with the correct access and flags.
void * memcpy(void *dest, const void *src, size_t n)
Copies memory from one address to the other without overlapping.