jankos/gdt.c
shockrah c94252c3f2 Huge GDT update:
GDT control is how I would like it to be but there are some issues, namely:
1. far jumping to flush the code segment crashes the whole os(not good)
2. Segment selectors seem fine apart from the code segment thing
3. some administrative issues regarding the structure of the project which is slowly making things painful
2019-10-14 00:23:37 -07:00

40 lines
1.2 KiB
C

#include "gdt.h"
// Congirures a given gdt entry base on the entry index given
void gdt_configure_entry(u32 entry, u32 base, u32 limit, u8 access, u8 gran) {
// base address
gdt_entries[entry].low_base = (base & 0xffff);
gdt_entries[entry].middle_base = (base >> 16) & 0xff;
gdt_entries[entry].high_base = (base >> 24) & 0xff;
// descriptor limits
gdt_entries[entry].low_limit = (limit & 0xffff);
gdt_entries[entry].granular = ((limit >> 16) & 0x0f);
gdt_entries[entry].granular |= (gran & 0xf0);
gdt_entries[entry].access = access;
}
// Configure the gdt pointer desribed in gdt.h as type GDT_PTR
void gdt_configure() {
// gdt_ptr configuration
gdt_ptr.size = (sizeof(struct GDT_Entry) * NO_GDT_ENTRIES) - 1;
// First entry is just the null descriptor and is left along overall
gdt_configure_entry(0, 0, 0, 0, 0);
// Second entry: code segment base adders
// Base address: 0
// limit: 4GB
// Granularity: 4KB
// 32-bit opcodes
// Code segment selector
gdt_configure_entry(1, 0, 0xffffffff, 0x9a, 0xcf);
// Finally the data segment
// All the same except the desriptor type specifies that its data
gdt_configure_entry(2, 0, 0xffffffff, 0x92, 0xcf);
// Load in the new changes to the gdt
load_gdt();
}