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
any way to trap a pagefault on stack in kernel space?
- Colonel Kernel
- 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?
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.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
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re:any way to trap a pagefault on stack in kernel space?
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
proxy
Re:any way to trap a pagefault on stack in kernel space?
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.
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?
ahh, i'll have to look at that, i like it as a solution, thanks.
proxy
proxy