HelenOS sources
This source file includes following definitions.
- gdt_tss_setbase
- gdt_tss_setlimit
- idt_setoffset
- tss_initialize
- idt_init
- pm_init
#include <arch.h>
#include <arch/pm.h>
#include <arch/asm.h>
#include <mm/as.h>
#include <mm/frame.h>
#include <memw.h>
#include <stdlib.h>
descriptor_t gdt[GDT_ITEMS] = {
[NULL_DES] = {
0
},
[KTEXT_DES] = {
.limit_0_15 = 0xffffU,
.limit_16_19 = 0xfU,
.access = AR_PRESENT | AR_CODE | DPL_KERNEL | AR_READABLE,
.longmode = 1,
.granularity = 1
},
[KDATA_DES] = {
.limit_0_15 = 0xffffU,
.limit_16_19 = 0xfU,
.access = AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_KERNEL,
.granularity = 1
},
[UDATA_DES] = {
.limit_0_15 = 0xffffU,
.limit_16_19 = 0xfU,
.access = AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_USER,
.special = 1,
.granularity = 1
},
[UTEXT_DES] = {
.limit_0_15 = 0xffffU,
.limit_16_19 = 0xfU,
.access = AR_PRESENT | AR_CODE | DPL_USER,
.longmode = 1,
.granularity = 1
},
[KTEXT32_DES] = {
.limit_0_15 = 0xffffU,
.limit_16_19 = 0xfU,
.access = AR_PRESENT | AR_CODE | DPL_KERNEL | AR_READABLE,
.special = 1,
.granularity = 1
},
[TSS_DES] = {
0
},
[TSS_DES + 1] = {
0
},
#ifdef CONFIG_FB
[VESA_INIT_CODE_DES] = {
.limit_0_15 = 0xffff,
.limit_16_19 = 0xf,
.base_16_23 = VESA_INIT_SEGMENT >> 12,
.access = AR_PRESENT | AR_CODE | AR_READABLE | DPL_KERNEL
},
[VESA_INIT_DATA_DES] = {
.limit_0_15 = 0xffff,
.limit_16_19 = 0xf,
.base_16_23 = VESA_INIT_SEGMENT >> 12,
.access = AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_KERNEL
}
#endif
};
idescriptor_t idt[IDT_ITEMS];
ptr_16_64_t gdtr = {
.limit = sizeof(gdt),
.base = (uint64_t) gdt
};
ptr_16_64_t idtr = {
.limit = sizeof(idt),
.base = (uint64_t) idt
};
static tss_t tss;
tss_t *tss_p = NULL;
void gdt_tss_setbase(descriptor_t *d, uintptr_t base)
{
tss_descriptor_t *td = (tss_descriptor_t *) d;
td->base_0_15 = base & 0xffffU;
td->base_16_23 = ((base) >> 16) & 0xffU;
td->base_24_31 = ((base) >> 24) & 0xffU;
td->base_32_63 = ((base) >> 32);
}
void gdt_tss_setlimit(descriptor_t *d, uint32_t limit)
{
tss_descriptor_t *td = (tss_descriptor_t *) d;
td->limit_0_15 = limit & 0xffffU;
td->limit_16_19 = (limit >> 16) & 0x0fU;
}
void idt_setoffset(idescriptor_t *d, uintptr_t offset)
{
d->offset_0_15 = offset & 0xffffU;
d->offset_16_31 = (offset >> 16) & 0xffffU;
d->offset_32_63 = offset >> 32;
}
void tss_initialize(tss_t *t)
{
memsetb(t, sizeof(tss_t), 0);
}
void idt_init(void)
{
idescriptor_t *d;
for (unsigned i = 0; i < IDT_ITEMS; i++) {
d = &idt[i];
d->unused = 0;
d->selector = GDT_SELECTOR(KTEXT_DES);
d->present = 1;
d->type = AR_INTERRUPT;
d->dpl = PL_KERNEL;
d->ist = 0;
}
d = &idt[0];
idt_setoffset(d++, (uintptr_t) &int_0);
idt_setoffset(d++, (uintptr_t) &int_1);
idt_setoffset(d++, (uintptr_t) &int_2);
idt_setoffset(d++, (uintptr_t) &int_3);
idt_setoffset(d++, (uintptr_t) &int_4);
idt_setoffset(d++, (uintptr_t) &int_5);
idt_setoffset(d++, (uintptr_t) &int_6);
idt_setoffset(d++, (uintptr_t) &int_7);
idt_setoffset(d++, (uintptr_t) &int_8);
idt_setoffset(d++, (uintptr_t) &int_9);
idt_setoffset(d++, (uintptr_t) &int_10);
idt_setoffset(d++, (uintptr_t) &int_11);
idt_setoffset(d++, (uintptr_t) &int_12);
idt_setoffset(d++, (uintptr_t) &int_13);
idt_setoffset(d++, (uintptr_t) &int_14);
idt_setoffset(d++, (uintptr_t) &int_15);
idt_setoffset(d++, (uintptr_t) &int_16);
idt_setoffset(d++, (uintptr_t) &int_17);
idt_setoffset(d++, (uintptr_t) &int_18);
idt_setoffset(d++, (uintptr_t) &int_19);
idt_setoffset(d++, (uintptr_t) &int_20);
idt_setoffset(d++, (uintptr_t) &int_21);
idt_setoffset(d++, (uintptr_t) &int_22);
idt_setoffset(d++, (uintptr_t) &int_23);
idt_setoffset(d++, (uintptr_t) &int_24);
idt_setoffset(d++, (uintptr_t) &int_25);
idt_setoffset(d++, (uintptr_t) &int_26);
idt_setoffset(d++, (uintptr_t) &int_27);
idt_setoffset(d++, (uintptr_t) &int_28);
idt_setoffset(d++, (uintptr_t) &int_29);
idt_setoffset(d++, (uintptr_t) &int_30);
idt_setoffset(d++, (uintptr_t) &int_31);
idt_setoffset(d++, (uintptr_t) &int_32);
idt_setoffset(d++, (uintptr_t) &int_33);
idt_setoffset(d++, (uintptr_t) &int_34);
idt_setoffset(d++, (uintptr_t) &int_35);
idt_setoffset(d++, (uintptr_t) &int_36);
idt_setoffset(d++, (uintptr_t) &int_37);
idt_setoffset(d++, (uintptr_t) &int_38);
idt_setoffset(d++, (uintptr_t) &int_39);
idt_setoffset(d++, (uintptr_t) &int_40);
idt_setoffset(d++, (uintptr_t) &int_41);
idt_setoffset(d++, (uintptr_t) &int_42);
idt_setoffset(d++, (uintptr_t) &int_43);
idt_setoffset(d++, (uintptr_t) &int_44);
idt_setoffset(d++, (uintptr_t) &int_45);
idt_setoffset(d++, (uintptr_t) &int_46);
idt_setoffset(d++, (uintptr_t) &int_47);
idt_setoffset(d++, (uintptr_t) &int_48);
idt_setoffset(d++, (uintptr_t) &int_49);
idt_setoffset(d++, (uintptr_t) &int_50);
idt_setoffset(d++, (uintptr_t) &int_51);
idt_setoffset(d++, (uintptr_t) &int_52);
idt_setoffset(d++, (uintptr_t) &int_53);
idt_setoffset(d++, (uintptr_t) &int_54);
idt_setoffset(d++, (uintptr_t) &int_55);
idt_setoffset(d++, (uintptr_t) &int_56);
idt_setoffset(d++, (uintptr_t) &int_57);
idt_setoffset(d++, (uintptr_t) &int_58);
idt_setoffset(d++, (uintptr_t) &int_59);
idt_setoffset(d++, (uintptr_t) &int_60);
idt_setoffset(d++, (uintptr_t) &int_61);
idt_setoffset(d++, (uintptr_t) &int_62);
idt_setoffset(d++, (uintptr_t) &int_63);
}
void pm_init(void)
{
descriptor_t *gdt_p = (descriptor_t *) gdtr.base;
tss_descriptor_t *tss_desc;
if (config.cpu_active == 1) {
idt_init();
tss_p = &tss;
} else {
write_cr3((uintptr_t) AS_KERNEL->genarch.page_table);
tss_p = (tss_t *) malloc(sizeof(tss_t));
if (!tss_p)
panic("Cannot allocate TSS.");
}
tss_initialize(tss_p);
tss_desc = (tss_descriptor_t *) (&gdt_p[TSS_DES]);
tss_desc->present = 1;
tss_desc->type = AR_TSS;
tss_desc->dpl = PL_KERNEL;
gdt_tss_setbase(&gdt_p[TSS_DES], (uintptr_t) tss_p);
gdt_tss_setlimit(&gdt_p[TSS_DES], TSS_BASIC_SIZE - 1);
gdtr_load(&gdtr);
idtr_load(&idtr);
tr_load(GDT_SELECTOR(TSS_DES));
}
HelenOS homepage, sources at GitHub