Improve GDT

This commit is contained in:
Furkan Mudanyali 2022-07-19 11:47:14 +03:00
parent 4b75737ad0
commit 036a7423fe
3 changed files with 30 additions and 62 deletions

View File

@ -1,51 +1,39 @@
/**
* 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;
static uint32_t tss[32];
static gdt_entry_t gdt[GDT_ENTRIES];
static gdtr_t gdtptr;
void gdt_fill(gdt_entry* target, gdt* source) {
if(source->limit > 0xFFFFF) {
printf("Invalid GDT limit %d\n", source->limit);
for(;;);
}
void gdt_set_descriptor(uint8_t vector, uint32_t base, uint32_t limit, uint8_t access, uint8_t flags) {
gdt[vector].base_low = base & 0xFFFF;
gdt[vector].base_mid = (base >> 16) & 0xFF;
gdt[vector].base_high = (base >> 24) & 0xFF;
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);
gdt[vector].limit_low = limit & 0xFFFF;
gdt[vector].limit_hi_flags = (limit >> 16) & 0x0F;
gdt[vector].limit_hi_flags |= flags << 4;
gdt[vector].access = access;
}
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
};
gdt_set_descriptor(0, 0, 0, 0, 0); // 0x0000 Null Descriptor
gdt_set_descriptor(1, 0, 0xFFFFF, 0x9A, 0xC); // 0x0008 Kernel Mode Code Segment
gdt_set_descriptor(2, 0, 0xFFFFF, 0x92, 0xC); // 0x0010 Kernel Mode Data Segment
gdt_set_descriptor(3, 0, 0xFFFFF, 0xFA, 0xC); // 0x0018 User Mode Code Segment
gdt_set_descriptor(4, 0, 0xFFFFF, 0xF2, 0xC); // 0x0020 User Mode Data Segment
gdt_set_descriptor(5, (uintptr_t)&tss[0], sizeof(tss), 0x89, 0); // 0x0028 Task State Segment
for(uint32_t i = 0; i < GDT_ENTRIES; ++i) {
gdt_fill(GDT+i, rows+i);
}
gdtptr.base = (uintptr_t)&gdt[0];
gdtptr.limit = sizeof(gdt_entry_t) * GDT_ENTRIES - 1;
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("lgdt %0" : : "m"(gdtptr)); // Load new GDT
__asm__ volatile(
"mov %cr0, %eax\n"
"or $1, %al\n" // set PE (Protection Enable) bit in CR0 (Control Register 0)
"mov %eax, %cr0\n"
"jmp $0x0008, $fix_cs\n"
"fix_cs:\n"
"mov $0x0010, %ax\n"

View File

@ -141,7 +141,6 @@ void idt_init(void) {
idtptr.base = (uintptr_t)&idt[0];
idtptr.limit = (uint16_t)sizeof(idt_entry_t) * IDT_MAX_DESCRIPTORS - 1;
idt_set_descriptor(0, int0_wrapper, 0x8E);
idt_set_descriptor(0, int0_wrapper, 0x8E);
idt_set_descriptor(1, int1_wrapper, 0x8E);
idt_set_descriptor(2, int2_wrapper, 0x8E);

View File

@ -1,8 +1,3 @@
/**
* Referenced from https://github.com/tendstofortytwo/clay
* kernel/kernel.c
*/
#ifndef _KERNEL_GDT_H
#define _KERNEL_GDT_H
@ -13,29 +8,15 @@
typedef struct {
uint16_t limit;
uint32_t base;
} __attribute__((packed)) gdt_ptr;
} __attribute__((packed)) gdtr_t;
typedef struct {
uint16_t limit_lo;
uint16_t base_lo;
uint16_t limit_low;
uint16_t base_low;
uint8_t base_mid;
uint8_t access;
uint8_t limit_hi_flags;
uint8_t base_hi;
} __attribute__((packed)) gdt_entry;
uint8_t base_high;
} __attribute__((packed)) gdt_entry_t;
typedef struct {
uint32_t base;
uint32_t limit;
uint8_t access;
uint8_t flags;
} __attribute__((packed)) gdt;
void gdt_fill(gdt_entry*, gdt*);
void gdt_init(void);
extern uint32_t TSS[32];
extern gdt_entry GDT[GDT_ENTRIES];
extern gdt_ptr gp;
#endif
#endif