This repository has been archived on 2024-06-25. You can view files and clone it, but cannot push or open issues or pull requests.
rockOS/kernel/arch/i386/boot/gdt.c

59 lines
1.6 KiB
C
Raw Normal View History

2022-07-18 18:14:20 +03:00
/**
* Referenced from https://github.com/tendstofortytwo/clay
* kernel/kernel.c
*/
#include <kernel/gdt.h>
#include <stdio.h>
#include <stdint.h>
uint32_t TSS[32];
gdt_entry GDT[GDT_ENTRIES];
gdt_ptr gp;
void gdt_fill(gdt_entry* target, gdt* source) {
if(source->limit > 0xFFFFF) {
printf("Invalid GDT limit %d\n", source->limit);
for(;;);
}
target->limit_lo = source->limit & 0xFFFF;
target->limit_hi_flags = (source->limit >> 16) & 0x0F;
target->base_lo = source->base & 0xFFFF;
target->base_mid = (source->base >> 16) & 0xFF;
target->base_hi = (source->base >> 24) & 0xFF;
target->access = source->access;
target->limit_hi_flags |= (source->flags << 4);
}
void gdt_init(void) {
uint32_t tssRef = (uint32_t) TSS;
gdt rows[GDT_ENTRIES] = {
{0, 0x00000, 0x00, 0x0}, // 0x0000 Null Descriptor
{0, 0xFFFFF, 0x9A, 0xC}, // 0x0008 Kernel Mode Code Segment
{0, 0xFFFFF, 0x92, 0xC}, // 0x0010 Kernel Mode Data Segment
{0, 0xFFFFF, 0xFA, 0xC}, // 0x0018 User Mode Code Segment
{0, 0xFFFFF, 0xF2, 0xC}, // 0x0020 User Mode Data Segment
{tssRef, sizeof(TSS), 0x89, 0x0}, // 0x0028 Task State Segment
};
for(uint32_t i = 0; i < GDT_ENTRIES; ++i) {
gdt_fill(GDT+i, rows+i);
}
gp.base = (uintptr_t)&GDT[0];
gp.limit = sizeof(gdt_entry) * GDT_ENTRIES - 1;
__asm__ volatile("lgdt %0" : : "m"(gp)); // Load new GDT
__asm__ volatile(
"jmp $0x0008, $fix_cs\n"
"fix_cs:\n"
"mov $0x0010, %ax\n"
"mov %ax, %ds\n"
"mov %ax, %es\n"
"mov %ax, %fs\n"
"mov %ax, %gs\n"
"mov %ax, %ss"
);
2022-07-18 18:14:20 +03:00
}