if paging exception occurs in kernel. prompt and halt?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
blackoil
Member
Member
Posts: 146
Joined: Mon Feb 12, 2007 4:45 am

if paging exception occurs in kernel. prompt and halt?

Post by blackoil »

How to design the paging exception handler?
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: if paging exception occurs in kernel. prompt and halt?

Post 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).
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: if paging exception occurs in kernel. prompt and halt?

Post 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
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: if paging exception occurs in kernel. prompt and halt?

Post 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.
blackoil
Member
Member
Posts: 146
Joined: Mon Feb 12, 2007 4:45 am

Re: if paging exception occurs in kernel. prompt and halt?

Post by blackoil »

OK, so if it's a bad kernel, I should halt the system.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: if paging exception occurs in kernel. prompt and halt?

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply