Using Task gate for exceptions and IRQs handling

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
Congdm
Member
Member
Posts: 48
Joined: Wed Aug 01, 2012 10:53 am

Using Task gate for exceptions and IRQs handling

Post by Congdm »

Hi,

In my OS, every programs run in ring 0. So when an interrupt or exception is raised, the CPU will not change stack if I use Interrupt gate in IDT. This leads to some problems like:

Stack is near full -> Page fault -> Push current state to stack (in order to call handler) -> Stack overflow -> Double fault -> Push -> Triple fault -> die

or

Stack overflow -> Push current state -> Stack overflow -> Double fault -> ... -> die

Therefore, I am considering using Task gate to handle exceptions and IRQs, and this will remove stack pressure from interrupt for normal programs. But TSS switching has considerable overhead. Are there any other solutions?
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Using Task gate for exceptions and IRQs handling

Post by iansjack »

I'd say that you have discovered one of the drawbacks of not using the x86 security model. So why does evey program run in ring 0? Are you, perhaps, addressing the wrong problem?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Using Task gate for exceptions and IRQs handling

Post by AJ »

Hi,

You could simply use the task gate for your double fault handler but no other exceptions, or you could run your user-mode code in ring 3. Presumably you are running in protected mode so do not have the IST mechanism available?

Cheers,
Adam

[Edit: ...and +1 to iansjack]
Congdm
Member
Member
Posts: 48
Joined: Wed Aug 01, 2012 10:53 am

Re: Using Task gate for exceptions and IRQs handling

Post by Congdm »

AJ wrote:You could simply use the task gate for your double fault handler but no other exceptions, or you could run your user-mode code in ring 3. Presumably you are running in protected mode so do not have the IST mechanism available?
Thanks, this is much more efficient and simple than my solution. And yes, I am running in protected mode.
iansjack wrote:I'd say that you have discovered one of the drawbacks of not using the x86 security model. So why does evey program run in ring 0? Are you, perhaps, addressing the wrong problem?
Yes, I understand the drawbacks of not using other rings but I am experimenting with running all programs in ring 0.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Using Task gate for exceptions and IRQs handling

Post by AJ »

Hi,

One word of warning if you go down this route - some Virtual Machines (MS VPC, I think, and possibly VirtualBox) did not handle task gates well the last time I tried (admittedly this was 5+ years ago). In the case of VPC, I was told that it was a known bug and there was no intention to fix, because mainstream OSes did not use this feature. Having said that, Bochs and Qemu worked fine IIRC.

Ultimately, though, you are very likely to want to run things in ring 3. I'm just curious - are you running managed code?

Cheers,
Adam
Post Reply