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

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
proxy

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

Post 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
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

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

Post 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.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
proxy

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

Post 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
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

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

Post 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.
proxy

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

Post by proxy »

ahh, i'll have to look at that, i like it as a solution, thanks.

proxy
Post Reply