Page 1 of 1
if paging exception occurs in kernel. prompt and halt?
Posted: Sat Jan 30, 2010 4:01 am
by blackoil
How to design the paging exception handler?
Re: if paging exception occurs in kernel. prompt and halt?
Posted: Sat Jan 30, 2010 5:35 am
by Creature
AFAIK, the page-fault handler can be called for a number of reasons. If it is called because a "page is not present", you should check if the page that is missing might be swapped to disk and retrieve it if necessary. If the page isn't on disk and isn't present, or some other error occurred, you could just turn off interrupts (if they aren't off), provide a register dump (the address causing the page fault is in CR2, I believe) and halt the entire kernel (like any ISR).
I have no way to swap pages to disk at the moment, so I just panic if a page-fault occurs (dump contents of registers and information regarding the page-fault).
Re: if paging exception occurs in kernel. prompt and halt?
Posted: Sat Jan 30, 2010 5:59 am
by AJ
Hi,
You also have to deal with the situations where you have allocated the virtual space to a process (via sbrk), but have not allocated the physical page yet, in which case you need to call your page frame allocator, or the case where a stack extends beyond its current limit.
If a PFE occurs because a process has tried to access memory outside its memory space, terminate that process and create a log / display an error message. There is no reason why this should affect any other process (unless it's your kernel or a server that page faults).
Cheers,
Adam
Re: if paging exception occurs in kernel. prompt and halt?
Posted: Sat Jan 30, 2010 6:34 am
by skyking
What should you prompt for? Abort, retry, ignore? When page fault occurs you must either fix the fault and continue or stop (reboot or bluescreen), it's no use returning from page fault if the problem is not handled (for example by reading the page from swapfile).
Unless you're kernel itself has swappable pages you've already lost if it happens in the kernel. If the fault is from syscall argument supplied from userspace process you may be better of checking the validity of the arguments and swap the virtual memory in before accessing it (the kernel can know this anyway). The alternative is to have a page fault handler that checks if the page is in the calling process memory space (and if not make sure the syscall returns with proper error code), this is not what I recommend since it's very hard for the kernel page fault handler to know from where the error originates (if it's because of syscall arguments or because of kernel internals) and take the proper action.
Simply put: keep the kernel in memory and don't access memory that isn't paged in.
Re: if paging exception occurs in kernel. prompt and halt?
Posted: Sat Jan 30, 2010 8:48 am
by blackoil
OK, so if it's a bad kernel, I should halt the system.
Re: if paging exception occurs in kernel. prompt and halt?
Posted: Sat Jan 30, 2010 11:44 am
by neon
If a #PF occurs, the exception handler should determine the cause of the page fault and fix it in some way. ie; if the page is swapped out, swap it back in. If the process accesses a page outside of the allocated heap terminate the process with an error.
When the system is in its early stages (not necessarily a "bad kernel") it is easiest to just make it halt the system As the system matures you would generally want to write it to handle the page faults as needed.