Page 1 of 1

Page Fault Handling

Posted: Tue Oct 20, 2009 6:57 pm
by oib111
So I setup a page-fault handler, but after the page-fault occurs and my handler runs a GPF occurs. Is this happening because the page isn't properly mapped?

Re: Page Fault Handling

Posted: Tue Oct 20, 2009 7:15 pm
by NickJohnson
Does the GPF occur during or after the handler? If it's during, you probably have some problem with reloading the process image, where an incorrect segment descriptor (caused by trying to reload from the wrong area of memory) can cause a GPF. It could be other things, but it most likely has nothing to do with paging, otherwise you would get another page fault.

Re: Page Fault Handling

Posted: Tue Oct 20, 2009 8:07 pm
by oib111
Well I added some lines of the code that look like this:

Code: Select all

p = page;
*p = 0xdeadbeef;
put("All mapped.");
If the page hadn't been mapped correctly, another page fault would occur when trying to write 0xdeadbeef. When I tested the page-fault handler with those extra lines, "All mapped" was printed out, so I don't think it's a problem with whether or not the page is mapped or any code within the page mapping function.

Re: Page Fault Handling

Posted: Wed Oct 21, 2009 2:47 am
by Combuster
Did you take care of the error code after a PF? If not, your stack is corrupt and you'd indeed get a GPF the moment you try to return

Re: Page Fault Handling

Posted: Wed Oct 21, 2009 8:21 am
by oib111
What do you mean take care of the error code?

Re: Page Fault Handling

Posted: Wed Oct 21, 2009 9:42 am
by Firestryke31
IIRC when the CPU generates a page fault it pushes what happened on the stack, as well as putting where it happened in CR2. If you don't handle cleaning up this error code (most tutorial IRQ handlers do without explaining too well) then the error code throws off the stack and you get either the wrong return address or a corrupt stack after return (I can't remember which at the moment).

Re: Page Fault Handling

Posted: Wed Oct 21, 2009 5:24 pm
by oib111
Do you mean to add 4 to esp so that it points to EIP and not the error code? If so, that didn't work.

Re: Page Fault Handling

Posted: Thu Oct 22, 2009 8:21 pm
by oib111
Fixed...request lock.

Re: Page Fault Handling

Posted: Fri Oct 23, 2009 1:30 am
by tantrikwizard
oib111 wrote:Do you mean to add 4 to esp so that it points to EIP and not the error code? If so, that didn't work.
yes, pop the error code from the stack before returning.

Re: Page Fault Handling

Posted: Fri Oct 23, 2009 6:45 am
by pcmattman
oib111 wrote:Fixed...request lock.
Would you mind telling us how you fixed it, so that others who stumble across the same problem can find a solution rather than a dead-end?

Re: Page Fault Handling

Posted: Fri Oct 23, 2009 8:14 am
by oib111
Popped the error code off the stack. It wasn't working before for some other reasons, that aren't really worth mentioning. Just silly coding mistakes.