Page 1 of 1

any way to trap a pagefault on stack in kernel space?

Posted: Wed Sep 28, 2005 2:17 pm
by proxy
is it possible to do this? I realize that you can setup a double fault stack but that doesn't help. the idea is i would like to have a way of detecting kernel stack overruns by using gaurd pages, but it seems impossible to do right.

proxy

Re:any way to trap a pagefault on stack in kernel space?

Posted: Wed Sep 28, 2005 2:25 pm
by Colonel Kernel
proxy wrote: is it possible to do this? I realize that you can setup a double fault stack but that doesn't help. the idea is i would like to have a way of detecting kernel stack overruns by using gaurd pages, but it seems impossible to do right.

proxy
Why doesn't setting up a double fault handler help? If you set up a separate task with its own stack that is always resident, and use a task gate, it should work AFAIK... Ugly, but doable.

Re:any way to trap a pagefault on stack in kernel space?

Posted: Wed Sep 28, 2005 3:01 pm
by proxy
but i can't resume from that, another long term goal i have in mind is to grow the kernel stack as needed (up to a limit).

proxy

Re:any way to trap a pagefault on stack in kernel space?

Posted: Thu Sep 29, 2005 2:41 pm
by nick8325
Well, you can't do this using a trap gate or interrupt gate, because when it gets a page fault on the stack, the processor will try to push the return address and error code onto the stack, and because the stack is not valid...*boom*! ;) You'll get a double fault, and the processor will once again try to push stuff onto the stack, so it'll triple fault.

As far as I know, the only way to do this on x86 is with a task gate. You need to set up a TSS with a different stack from your kernel's main one. Then make your page fault handler a task gate to that. Whenever there's a page fault, you'll end up on the other stack, where you can handle it.

By the way, this problem doesn't seem to exist on most other processors - most of them put the return address in a register rather than on the stack, so there's no need to have a valid stack at all. x86-64 has a flag which can be set to tell the processor to switch stacks on some particular interrupt, so there's no need there for the TSS.

Re:any way to trap a pagefault on stack in kernel space?

Posted: Thu Sep 29, 2005 6:54 pm
by proxy
ahh, i'll have to look at that, i like it as a solution, thanks.

proxy