General Protection Fault with TSS
Posted: Tue May 28, 2019 1:54 pm
Hello,
I'm currently trying to implement a TSS for multitasking on my kernel. Here is my code:
Basically, I'm getting a General Protection Fault when the kernel reaches the ltr instruction in tss_flush.
Is my GDT entry wrong or could the problem come from somewhere else?
Note: I already loaded my GDT when calling this code
Thanks in advance
I'm currently trying to implement a TSS for multitasking on my kernel. Here is my code:
Code: Select all
static tss_entry_t tss_entry;
__attribute__((cold))
static void tss_init(void)
{
const uint32_t base = (uint32_t) &tss_entry;
const uint64_t limit = sizeof(tss_entry_t);
const uint8_t flags = 0x40;
const uint8_t access = 0x86;
void *tss_gdt = tss_gdt_entry();
bzero(tss_gdt, sizeof(uint64_t));
*((uint16_t *) (tss_gdt)) = limit & 0xffff;
*((uint16_t *) (tss_gdt + 2)) = base & 0xffff;
*((uint8_t *) (tss_gdt + 4)) = (base >> 16) & 0xff;
*((uint8_t *) (tss_gdt + 5)) = access;
*((uint8_t *) (tss_gdt + 6)) = ((limit >> 16) & 0xf) | flags;
*((uint8_t *) (tss_gdt + 7)) = (base >> 24) & 0xff;
bzero(&tss_entry, sizeof(tss_entry_t));
tss_entry.ss0 = 0x10;
asm volatile("mov %%esp, %0" : "=a"(tss_entry.esp0));
tss_flush();
}
Code: Select all
__attribute__((packed))
struct tss_entry
{
uint32_t prev_tss;
uint32_t esp0;
uint32_t ss0;
uint32_t esp1;
uint32_t ss1;
uint32_t esp2;
uint32_t ss2;
uint32_t cr3;
uint32_t eip;
uint32_t eflags;
uint32_t eax;
uint32_t ecx;
uint32_t edx;
uint32_t ebx;
uint32_t esp;
uint32_t ebp;
uint32_t esi;
uint32_t edi;
uint32_t es;
uint32_t cs;
uint32_t ss;
uint32_t ds;
uint32_t fs;
uint32_t gs;
uint32_t ldt;
uint16_t trap;
uint16_t iomap_base;
};
typedef struct tss_entry tss_entry_t;
Code: Select all
.global tss_gdt_entry
.global tss_flush
tss_gdt_entry:
mov gdt_tss, %eax
ret
tss_flush:
mov TSS_OFFSET, %ax
ltr %ax
ret
Is my GDT entry wrong or could the problem come from somewhere else?
Note: I already loaded my GDT when calling this code
Thanks in advance