Page 1 of 1

Error code for exception is not pushed ??

Posted: Sat Nov 30, 2013 2:21 pm
by neurocom
Hi All,
I have a very simple code to test if an exception error code is pushed right after 'int 0x08', for example
When I run it, I do NOT get any error code for int 0x8.

Here is the snapshot of the code

Code: Select all

disable_interrupts(); // same as 'cli'

// Initialize 8259 PIC and remapping
outb(0x20, 0x11);
outb(0xA0, 0x11);
outb(0x21, 0x20);
outb(0xA1, 0x28);
outb(0x21, 0x04);
outb(0xA1, 0x02);
outb(0x21, 0x01);
outb(0xA1, 0x01);
outb(0x21, 0x0);
outb(0xA1, 0x0);

// trigger Double Fault 
asm ("int $0x08");
When I run Bochs with it and debug it, I've got the following result in the stack

>> Right before the execution of 'init 0x08'
| STACK 0x00001b10 [0x000000a1]
| STACK 0x00001b14 [0x00000000]
| STACK 0x00001b18 [0x00000000]
| STACK 0x00001b1c [0x00000000]
| STACK 0x00001b20 [0x000e0000]
| STACK 0x00001b24 [0x0000ffac]
| STACK 0x00001b28 [0x00000000]
| STACK 0x00001b2c [0x00000616]
| STACK 0x00001b30 [0x00000000]
| STACK 0x00001b34 [0x00000000]
| STACK 0x00001b38 [0x00000000]
| STACK 0x00001b3c [0x00000000]
| STACK 0x00001b40 [0x00000000]
| STACK 0x00001b44 [0x00000000]
| STACK 0x00001b48 [0x00000000]
| STACK 0x00001b4c [0x00000000]

>>Right after in 0x08 (first line of the exception hanlder)
| STACK 0x00001b04 [0x000008c8]
| STACK 0x00001b08 [0x00000008]
| STACK 0x00001b0c [0x00000002]

| STACK 0x00001b10 [0x000000a1]
| STACK 0x00001b14 [0x00000000]
| STACK 0x00001b18 [0x00000000]
| STACK 0x00001b1c [0x00000000]
| STACK 0x00001b20 [0x000e0000]
| STACK 0x00001b24 [0x0000ffac]
| STACK 0x00001b28 [0x00000000]
| STACK 0x00001b2c [0x00000616]
| STACK 0x00001b30 [0x00000000]
| STACK 0x00001b34 [0x00000000]
| STACK 0x00001b38 [0x00000000]
| STACK 0x00001b3c [0x00000000]
| STACK 0x00001b40 [0x00000000]

I'm sure that only EFLAGS, CS and EIP are pushed, not ERR CODE for the exception
| STACK 0x00001b04 [0x000008c8] ==> EIP
| STACK 0x00001b08 [0x00000008] ==> CS
| STACK 0x00001b0c [0x00000002] ==> EFLAGS

I think there are two possible problems
1. There is not 8259PIC in my Bochs
2. My initialization of 8259PIC is wrong

Can someone kindly explain/advise me to fix the problem.
Appreicated!

Jason

Re: Error code for exception is not pushed ??

Posted: Sat Nov 30, 2013 2:49 pm
by iansjack
You are not triggering an exception; you are calling the interrupt routine that would run if the exception were triggered. Calling an interrupt does not push an error code.

Re: Error code for exception is not pushed ??

Posted: Sat Nov 30, 2013 11:30 pm
by neurocom
iansjack wrote:You are not triggering an exception; you are calling the interrupt routine that would run if the exception were triggered. Calling an interrupt does not push an error code.
=D> Thank you for your answer.
One more question.
If 'INT 0x08' is not for triggering the exception, what is the way to trigger exceptions (e.g Double fault) to identify the error code for the testing purpose?

Thank you,
Jason

Re: Error code for exception is not pushed ??

Posted: Sun Dec 01, 2013 1:18 am
by iansjack
You need to create the conditions that trigger the exception. That's easy enough for most exceptions but a little trickier for the double-fault exception. (Double-faults tend to progress to triple faults.) Do something that will create a page fault in your GPF handler, then in your main program do something to create a GPF. That should do the trick I think.

But you don't really need to test that an error code (of zero) is pushed to the stack; that's guaranteed.

Re: Error code for exception is not pushed ??

Posted: Sun Dec 01, 2013 8:47 am
by neurocom
iansjack wrote:You need to create the conditions that trigger the exception. That's easy enough for most exceptions but a little trickier for the double-fault exception. (Double-faults tend to progress to triple faults.) Do something that will create a page fault in your GPF handler, then in your main program do something to create a GPF. That should do the trick I think.

But you don't really need to test that an error code (of zero) is pushed to the stack; that's guaranteed.
Thanks, iansjack
Great help!

Jason

Re: Error code for exception is not pushed ??

Posted: Sun Dec 01, 2013 8:55 am
by bluemoon
iansjack wrote:You need to create the conditions that trigger the exception. That's easy enough for most exceptions but a little trickier for the double-fault exception. (Double-faults tend to progress to triple faults.) Do something that will create a page fault in your GPF handler, then in your main program do something to create a GPF. That should do the trick I think.
But you don't really need to test that an error code (of zero) is pushed to the stack; that's guaranteed.
It might seem weird, but generate a #PF inside the #GP handler is still a single fault.
To create #DF scenario you need a fault when the CPU attempt to call the fault handler. Easiest way is perhaps a NULL handler(CS=0) for divided by zero exception.

Re: Error code for exception is not pushed ??

Posted: Sun Dec 01, 2013 9:58 am
by iansjack
You are quite right (and it makes sense). I should have said it the other way round - a GPF inside the PF handler. As I understand it that would cause a double-fault?

Re: Error code for exception is not pushed ??

Posted: Mon Dec 02, 2013 2:09 am
by Combuster
iansjack wrote:a GPF inside the PF handler
Double faults are only triggered if the interrupt invocation itself generates additional exceptions. If for instance the CS:IP in the IDT points to unmapped space, then you get back-to-back pagefaults because each individual cause can be logged. That "works" until the stack runs out milliseconds later and pushing the old CS:IP as part of the interrupt fails in which case the handler has to report two specific issues (cs:*ip and ss:*sp) at the same time and ends up in a double fault.