Exception handling code causes triple fault

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
DeezRamChips
Member
Member
Posts: 132
Joined: Fri Apr 08, 2016 5:03 am
Location: atapio.cpp - why won't you work :(
Contact:

Exception handling code causes triple fault

Post 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 :)
rwosdev
Member
Member
Posts: 49
Joined: Thu Apr 26, 2018 11:21 pm

Re: Exception handling code causes triple fault

Post 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)
User avatar
DeezRamChips
Member
Member
Posts: 132
Joined: Fri Apr 08, 2016 5:03 am
Location: atapio.cpp - why won't you work :(
Contact:

Re: Exception handling code causes triple fault

Post 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 :)
User avatar
DeezRamChips
Member
Member
Posts: 132
Joined: Fri Apr 08, 2016 5:03 am
Location: atapio.cpp - why won't you work :(
Contact:

Re: Exception handling code causes triple fault

Post 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
Post Reply