Page 1 of 1

Handling Page Fault Gracefully in User Process

Posted: Tue Jul 01, 2008 9:18 am
by prajwal
Hi All,

I have a problem handling Page Fault that is raised by a user process (ring 3).

PS: Paltform: Intel 0x386, Using hardware supported Task switch, Segmentation and Paging

Currently, this is what the infrastructure I have for Page Fault:-

when a user process accesses a mem area which is not present, then it page faults. I have loaded the IDT for Page Fault Exception 0xE with an Interrupt Task Gate. So, the page fault trigerrs a TSS switch which executes the page fault handler in ring 0 (kernel mode). allocates the page and returns back (TSS switch back) to user process (ring 3). So far so good, it worked fine.
But now if Page allocation fails due to some reason, then I need to stop/kill that process. How to do this? The Kernel (which includes scheduler) is a Seperate TSS, Page Fault Handler is a seperate TSS and user process is a seperate TSS. How to swtich back to kernel TSS w/o executing any further instructions of user process and kill it??

I tried using a normal exception handler and also interrupt gate for page fault handler -> then switched to actual page allocation routine through a TSS switch manually and communicated the success/failure status via shared memory back to page fault exception handler. But no luck as I get General Protection Fault when I finally do a IRET from exception handler!! :((

Can anyone please advise.

Thanks,
- MosMan

Re: Handling Page Fault Gracefully in User Process

Posted: Tue Jul 01, 2008 9:35 am
by AJ
Hi,

I'm guessing this isn't the answer you want, but its the way I do it so it may give you some ideas!

I would suggest handing the page fault without switching TSS (just let the CPU switch to a safe ring 0 stack). That way, the PFE happens in the context of the current task and all the kernel needs to do is execute your exit() function, just like it would for a normal task termination.

Another way I could see you doing this is that the PFE handler task adjusts the EIP of the faulting task to point to an exit function, or lists the task for termination in some global list handled by your scheduler and calls your sleep() function. Using this method, the task will again get terminated in the usual way.

HTH,
Adam

Re: Handling Page Fault Gracefully in User Process

Posted: Tue Jul 01, 2008 11:04 am
by piranha
Yes, thats what I do.

The fault handler displays the information, and then "Killing Task ###", and then send the kill signal to the task, ending it before it can execute anymore.

Simple.

-JL

Re: Handling Page Fault Gracefully in User Process

Posted: Tue Jul 01, 2008 1:24 pm
by Combuster
It's basically about having the exception handler kill the task without having to wait for something else (the rest of the kernel) to do it. Or at least lock it up enough so that it cant continue, and then post a message to another thread who is in charge of the capital punishment. I won't go about the details as some suggestions have already been made :wink:

Re: Handling Page Fault Gracefully in User Process

Posted: Tue Jul 01, 2008 7:14 pm
by Brendan
Hi,
mosman wrote:when a user process accesses a mem area which is not present, then it page faults. I have loaded the IDT for Page Fault Exception 0xE with an Interrupt Task Gate. So, the page fault trigerrs a TSS switch which executes the page fault handler in ring 0 (kernel mode). allocates the page and returns back (TSS switch back) to user process (ring 3). So far so good, it worked fine.
But now if Page allocation fails due to some reason, then I need to stop/kill that process. How to do this? The Kernel (which includes scheduler) is a Seperate TSS, Page Fault Handler is a seperate TSS and user process is a seperate TSS. How to swtich back to kernel TSS w/o executing any further instructions of user process and kill it??
Your page fault handler could modify CS:EIP and SS:ESP in the interrupted task's TSS, so when your page fault handler does IRET it returns to the task that caused the exception but also returns to kernel code (e.g. your "killThisProcess()" code) instead of the user-level code that caused the exception.

However, I tend to agree with AJ - except for possibly the double fault exception handler, I'd use one TSS per thread/process, and wouldn't use TSSs for the kernel or other exception handlers.

Part of the reason for this is that when an exception is handled by an Interrupt Task Gate it's not re-entrant. For example, if your page fault handler causes a page fault it'll trash itself (which prevents some tricks, and if any IRQ handler might cause a page fault it means that you need to keep IRQs disabled in the page fault handler). Worse, it means you can't do task switches from inside the page fault handler, which is extremely important IMHO, because later on you'll probably want to run other tasks while you wait for pages to be sent to/from disk (for memory mapped files, swap space, etc) and the other tasks will need to be able to use the "already in use" page fault handler too. It also complicates things for multi-CPU support (and it's much slower than using an interrupt gate or trap gate).


Cheers,

Brendan

Re: Handling Page Fault Gracefully in User Process

Posted: Wed Jul 02, 2008 5:32 am
by prajwal
First of all Thanks a lot for valid suggestions...

Its a good idea to adjust CS:EIP and SS:ESP to exit from process. To be more precise, when I use interrupt gate (say) instead of a interrupt task for page fault handling, then I donot have problem terminating the process gracefully upon page allocation failure but the problem comes while returning back to the faulting process after successful page allocation. As I already mentioned, I get General Protection Fault when I do IRET finally from Page Fault Handler which is entered via interrupt gate. Should I need to do any additional thing like adjusting stack, EIP etc... before returning from page fault handler?

Thanks,
- MosMan

Re: Handling Page Fault Gracefully in User Process

Posted: Wed Jul 02, 2008 5:50 am
by Brendan
Hi,
mosman wrote:As I already mentioned, I get General Protection Fault when I do IRET finally from Page Fault Handler which is entered via interrupt gate. Should I need to do any additional thing like adjusting stack, EIP etc... before returning from page fault handler?
You shouldn't need to do anything extra (as long as you're flushing the TLB, removing the error code from the stack before doing IRETD, and not trashing the stack accidentally).


Cheers,

Brendan

Re: Handling Page Fault Gracefully in User Process

Posted: Wed Jul 02, 2008 8:43 am
by prajwal
Ok. that make sense. Let me go back home and try this more carefully...

once again, Thanks a lot,
- MosMan