Stack fault 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
seraph9

Stack fault handling

Post by seraph9 »

Hi,
I'm a bit confused about stack faults and their handling; I have setup kernel VM with its own heap and stack. I initially decided that I'd start with a single page for the stack and as the need arises add more to it. This, I thought, should be done by catching faults. But what i don't understand is, what is the kind of fault that occurs ? Is it page fault when esp goes beyond the page or is it stack fault. And be it any of the two, how can I handle them ? I mean the interrupt needs to save the values into a stack right ?.. but the stack just faulted :-\. Is this what is referred to as a double fault ? Are there any ways to handle it, or better any method to grow stack as needed ?

Thanks
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Stack fault handling

Post by Candy »

seraph9 wrote: But what i don't understand is, what is the kind of fault that occurs ?
For a stack access on a non-mapped page, it should be the stack fault. For a stack access beyond the segment limit, I think it's a GPF, although I cannot be sure (could be stack fault as well). Why don't you try it?
And be it any of the two, how can I handle them ? I mean the interrupt needs to save the values into a stack right ?.. but the stack just faulted :-\. Is this what is referred to as a double fault ? Are there any ways to handle it, or better any method to grow stack as needed ?
If you handle it in a decent way, then you don't get a second stack fault. If you handle the stack fault by pushing values, you're doing something wrong. On IA32, use some mechanism to swap stacks (f.i. use a TSS or Task Gate), on AMD64, use a different stack (access with PCD, PAT, and PWT bits).

When a stack fault happens in user space, the stack is first switched to kernel space and then the values are pushed. If you fault in kernel space, you're screwed (unless you thought of that beforehand).
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Stack fault handling

Post by Pype.Clicker »

the best way i've found to handle this situation is to make the stack fault used (i.e. setting a stack segment that has a limit that will *not* let you go to the page fault. As segmentation is applied first and *then* paging, you'll get a stack fault and not a page fault if the two were possible.

The stack fault handler has then to be a *trap gate*, which will make the fault handled in a new task with a fresh stack.

In the case of a *user* mode stack, you can simply have a page fault, as the fault will be handled on the *system* stack defined by SS0:ESP0
Post Reply