75 lines
1.3 KiB
C
75 lines
1.3 KiB
C
|
#include "gdt.h"
|
||
|
|
||
|
static struct gdt gdt = { 0 };
|
||
|
|
||
|
static void create_kernel_ds()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
static void create_kernel_cs()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
static void create_gdt()
|
||
|
{
|
||
|
create_kernel_ds();
|
||
|
create_kernel_cs();
|
||
|
}
|
||
|
|
||
|
static void load_gdt()
|
||
|
{
|
||
|
struct gdt_r gdtr;
|
||
|
gdtr.addr = (uint32_t)&gdt;
|
||
|
gdtr.limit = GDT_SIZE - 1;
|
||
|
|
||
|
asm volatile("lgdt %0\n"
|
||
|
:
|
||
|
: "m" (gdtr)
|
||
|
: "memory");
|
||
|
}
|
||
|
|
||
|
static void load_ds()
|
||
|
{
|
||
|
struct segment_selector selector;
|
||
|
selector.index = KERNEL_DS_INDEX;
|
||
|
selector.table_indicator = 0;
|
||
|
selector.rpl = 0;
|
||
|
|
||
|
asm volatile("movw %0, %%ax\n"
|
||
|
"movw %%ax, %%ds\n"
|
||
|
"movw %%ax, %%fs\n"
|
||
|
"movw %%ax, %%gs\n"
|
||
|
"movw %%ax, %%ss\n"
|
||
|
:
|
||
|
: "m" (selector)
|
||
|
: "ax");
|
||
|
}
|
||
|
|
||
|
static void load_cs()
|
||
|
{
|
||
|
struct segment_selector selector;
|
||
|
selector.index = KERNEL_CS_INDEX;
|
||
|
selector.table_indicator = 0;
|
||
|
selector.rpl = 0;
|
||
|
|
||
|
asm volatile("pushl %0\n"
|
||
|
"pushl $1f\n"
|
||
|
"lret\n"
|
||
|
"1:\n"
|
||
|
:
|
||
|
: "m" (selector));
|
||
|
}
|
||
|
|
||
|
static void reload_segment_selectors()
|
||
|
{
|
||
|
load_ds();
|
||
|
load_cs();
|
||
|
}
|
||
|
|
||
|
void init_gdt()
|
||
|
{
|
||
|
create_gdt();
|
||
|
load_gdt();
|
||
|
reload_segment_selectors();
|
||
|
}
|