Page 1 of 1

Exception handling code causes triple fault

Posted: Sat Jun 23, 2018 3:46 pm
by DeezRamChips
Hi :)

I finally decided to add a kernel panic screen to my OS instead of just printing out the error when I realized that I wasn't actually catching any exceptions (tested with a divide by zero code) :shock:

Instead, I would get a triple fault and reboot. All other interrupts seem to work (keyboard, syscalls, PIT, etc...) but my exception code doesn't seem to work :/

This is the code, every exception related ISR push their ID on the stack and an optional zero to keep the stack integrity when there is no additional code and calls it:

Code: Select all

_asm_fault_handler:
    pusha
    push %ds
    push %es
    push %fs
    push %gs
    mov $0x10, %ax
    mov %ax, %ds
    mov %ax, %es
    mov %ax, %fs
    mov %ax, %gs
    movl %esp, %eax
    push %eax
    movl _fault_handler, %eax
    call *%eax
    pop %eax
    pop %gs
    pop %fs
    pop %es
    pop %ds
    popa
    add $8, %esp
    iret
I know it's a bit late in the development of my OS that I find out about this but I hope someone can help figure whats wrong :)

Re: Exception handling code causes triple fault

Posted: Sat Jun 23, 2018 4:03 pm
by rwosdev
Are the entries for these specified in the IDT with an interrupt gate? I use an interrupt gate for all types of interrupts and exceptions because it ensures the IF flag is disabled immediately, so interrupts can't interrupt interrupts....

Also, exception handlers should probably do a check to see if the exception is in user code or kernel code (based on bottom 2 bits of IRET stack -> CS). If it's in kernel code you just call your standard text output functions and then HLT for the effect of a panic in text mode (and interrupts should also be disabled at that point)

Re: Exception handling code causes triple fault

Posted: Sat Jun 23, 2018 4:38 pm
by DeezRamChips
rwosdev wrote:Are the entries for these specified in the IDT with an interrupt gate? I use an interrupt gate for all types of interrupts and exceptions because it ensures the IF flag is disabled immediately, so interrupts can't interrupt interrupts....

Also, exception handlers should probably do a check to see if the exception is in user code or kernel code (based on bottom 2 bits of IRET stack -> CS). If it's in kernel code you just call your standard text output functions and then HLT for the effect of a panic in text mode (and interrupts should also be disabled at that point)
Yeah, they are all specified, the divide by zero exception for example is:

Code: Select all

encodeIdtEntry(&_IDT[8 * 0x00], (uint32_t)&_isr0, 0x08, INT_GATE);
And I'm still running everything in ring 0 lol, but thanks for the advice :)

Re: Exception handling code causes triple fault

Posted: Sun Jun 24, 2018 10:11 am
by DeezRamChips
UPDATE !

This was apparently a error on my part when I tried to convert the NASM tutorial I was following to GAS ^^

This is the now working code:

Code: Select all

isr_common_stub:
    pusha
    push %ds
    push %es
    push %fs
    push %gs
    movw $0x10,%ax
    movw %ax,%ds
    movw %ax,%es
    movw %ax,%fs
    movw %ax,%gs
    movl %esp,%eax
    pushl %eax
    movl $_fault_handler, %eax
    call *%eax
    popl %eax
    popl %gs
    popl %fs
    popl %es
    popl %ds
    popa
    addl $8,%esp
    iret