Handling Page Fault Gracefully in User Process

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
User avatar
prajwal
Member
Member
Posts: 154
Joined: Sat Oct 23, 2004 11:00 pm
Contact:

Handling Page Fault Gracefully in User Process

Post 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
complexity is the core of simplicity
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Handling Page Fault Gracefully in User Process

Post 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
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: Handling Page Fault Gracefully in User Process

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Handling Page Fault Gracefully in User Process

Post 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:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Handling Page Fault Gracefully in User Process

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
prajwal
Member
Member
Posts: 154
Joined: Sat Oct 23, 2004 11:00 pm
Contact:

Re: Handling Page Fault Gracefully in User Process

Post 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
complexity is the core of simplicity
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Handling Page Fault Gracefully in User Process

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
prajwal
Member
Member
Posts: 154
Joined: Sat Oct 23, 2004 11:00 pm
Contact:

Re: Handling Page Fault Gracefully in User Process

Post by prajwal »

Ok. that make sense. Let me go back home and try this more carefully...

once again, Thanks a lot,
- MosMan
complexity is the core of simplicity
Post Reply