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

49 lines
1.4 KiB
C

/**
* 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 = (void*) GDT;
gp.limit = (GDT_ENTRIES * sizeof(gdt_entry)) - 1;
gdt_load();
}