[SOLVED] Problem using task gate as double fault handler

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
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

[SOLVED] Problem using task gate as double fault handler

Post by gzaloprgm »

Hi, I am trying to create a double fault handler in order to catch stack faults and hopefully show useful information.

To do so, I created a TSS and added it to the GDT. (base=Address of TSS, limit=sizeof(TSS), access=0xE9, gran=0xCF)

Then, I set in the new TSS eip, cr3, cs, ds, es, fs, gs, ss, esp and eflags of the handler.

To set the task gate into the IDT, I am using index=8, base=0, selector=0x30, flags=0x85.

Now I am trying some tests to check if it works:

Code: Select all

asm volatile("int $8");
/
asm volatile("jmp $(0x30),$0");
Both work fine in qemu, bochs, virtual box and vmware.

However, when I try to do a double fault (ie. by changing esp to a unmapped page and then trying to push/pop something), the handler does not work :shock:

In qemu it crashes.
In bochs and vmware works.
In vbox doesn't crash but the handler isn't executed.

Why whould this happen? Am I missing some flag?
Thanks,
Gzaloprgm
Last edited by gzaloprgm on Sat Jul 25, 2009 5:40 pm, edited 1 time in total.
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Problem using task gate as double fault handler

Post by pcmattman »

Did you zero your TSS first?
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Re: Problem using task gate as double fault handler

Post by gzaloprgm »

Yes, I am zeroing it.
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Problem using task gate as double fault handler

Post by pcmattman »

Apart from the fields you specified, are you setting anything else in the TSS (SS0:ESP0, for instance)?
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Re: Problem using task gate as double fault handler

Post by gzaloprgm »

Nope, I am memseting every other field to zero. Should I set them? The double fault I want to catch is happening in ring0.

Code: Select all

-GDT:

gdtSetGate(6, (unsigned int) &tssEntryDFault, sizeof( tssEntry_t ), 0xE9, 0xCF );

memset(&tssEntryDFault,0,sizeof(tssEntry_t));
tssEntryDFault.eip=(unsigned int)&test;
tssEntryDFault.cr3=0x1000;
tssEntryDFault.cs = 0x08;
tssEntryDFault.ds = 0x10;
tssEntryDFault.es = 0x10;
tssEntryDFault.fs = 0x10;
tssEntryDFault.gs = 0x10;
tssEntryDFault.ss = 0x10;
tssEntryDFault.esp= (unsigned int)stack + 0x1000;
tssEntryDFault.eflags = 2;

-IDT:

idtSetGate(8, 0, 0x30, 0x85);

Stack is just a an array of chars. Test function only displays a message and halts.

EDIT: If I make the page fault handler with a task gate, it works. So I think that either is a bug of qemu and vbox or a really nasty bug in my kernel. Can anyone confirm that?

EDIT2: My structs are correctly packed, GDT and IDT limits are ok.

EDIT3: After running it in a newer version of qemu, it works. (I was using 0.9.0, now 0.10.5)

Thanks,
gzaloprgm
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: [SOLVED] Problem using task gate as double fault handler

Post by Combuster »

I suggest trying real hardware and check if it works there. Hardware task switching isn't a common application nowadays, so I expect qemu (and its descendant vbox) are a bit less tested on that, while real hardware doesn't quite have that excuse.

That can help you eliminate vbox, as well as being a good thing to do anyway :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply