#include "arch/x86_64/gdt.h"
#include "arch/x86_64/tss.h"
#include "arch/processor.h"
#include "string.h"
#include "print.h"
static gdt_t s_baseline_gdt =
{
{ 0 }, /* Kernel NULL */
{ /* Kernel Code (64-bit RO EX DPL0) */
.limit_0_15 = 0xFFFF,
.base_0_15 = 0,
.base_16_23 = 0,
.type = CD_SEGMENT_TYPE_CODE | CD_SEGMENT_TYPE_WRITEABLE,
.s = 1,
.dpl = 0,
.p = 1,
.limit_16_19 = 0xF,
.avl = 0,
.l = 1,
.d_b = 0,
.g = 1,
.base_24_31 = 0
},
{ /* Kernel Data (64-bit RW DPL0) */
.limit_0_15 = 0xFFFF,
.base_0_15 = 0,
.base_16_23 = 0,
.type = CD_SEGMENT_TYPE_WRITEABLE,
.s = 1,
.dpl = 0,
.p = 1,
.limit_16_19 = 0xF,
.avl = 0,
.l = 0,
.d_b = 1,
.g = 1,
.base_24_31 = 0
},
{ 0 }, /* User NULL */
{ /* User Data (64-bit RO EX DPL3)*/
.limit_0_15 = 0xFFFF,
.base_0_15 = 0,
.base_16_23 = 0,
.type = CD_SEGMENT_TYPE_WRITEABLE,
.s = 1,
.dpl = 3,
.p = 1,
.limit_16_19 = 0xF,
.avl = 0,
.l = 0,
.d_b = 1,
.g = 1,
.base_24_31 = 0,
},
{ /* User Code (64-bit RO EX DPL3)*/
.limit_0_15 = 0xFFFF,
.base_0_15 = 0,
.base_16_23 = 0,
.type = CD_SEGMENT_TYPE_CODE | CD_SEGMENT_TYPE_WRITEABLE,
.s = 1,
.dpl = 3,
.p = 1,
.limit_16_19 = 0xF,
.avl = 0,
.l = 1,
.d_b = 0,
.g = 1,
.base_24_31 = 0,
},
{ /* TSS Low */
.limit_0_15 = 0,
.base_0_15 = 0,
.base_16_23 = 0,
.type = S_SEGMENT_TYPE_TSS_AVAIL,
.avl = 0,
.s = 0,
.dpl = 0,
.p = 1,
.limit_16_19 = 0,
.l = 0,
.d_b = 0,
.g = 0,
.base_24_31 = 0,
},
{ 0 }
};
void
gdt_setup(processor_t *proc)
{
memcpy(proc->_gdt, s_baseline_gdt, sizeof(gdt_t));
proc->_gdtr = (struct XDTR) {
.length = sizeof(gdt_t) - 1,
.address = (uintptr_t)&proc->_gdt
};
klogf("Processor %i GDT %#016X L%i\n", proc->id, proc->_gdt, proc->_gdtr.length);
}
void
tss_setup(processor_t *proc)
{
segment_descriptor_t *tss_lo = &proc->_gdt[GDT_SEGMENT_TSS_LOW];
segment_descriptor_t *tss_hi = &proc->_gdt[GDT_SEGMENT_TSS_HIGH];
uintptr_t tssb = (uintptr_t)&(proc->_tss);
tss_lo->base_0_15 = tssb & 0xFFFF;
tss_lo->base_16_23 = (tssb >> 16) & 0xFF;
tss_lo->base_24_31 = (tssb >> 24) & 0xFF;
tss_hi->limit_0_15 = (tssb >> 32) & 0xFFFF;
tss_hi->base_0_15 = (tssb >> 48) & 0xFFFF;
size_t tssl = sizeof(struct TSS) - 1;
tss_lo->limit_0_15 = tssl & 0xFFFF;
tss_lo->limit_16_19 = (tssl >> 16) & 0xF;
proc->_tss.iobp = sizeof(struct TSS);
}
void
tss_set_rsp(struct TSS *tss, uint8_t dpl, uintptr_t rsp)
{
tss->rsp[dpl][0] = rsp & 0xFFFFFFFF;
tss->rsp[dpl][1] = (rsp >> 32) & 0xFFFFFFFF;
}