views:

26

answers:

2

Hello Everyone,

Is there a way in Windows/Linux or any other operating system to know at instruction-level, if a memory access generated a Page Fault? The code I'm imagining about would look something like this:

    Buffer *buffer = new Buffer();

    ...Do something with the buffer...            
    if(thisProcess.generatedPageFault) {
       ...Do something...
    }

    ...Do something else with the buffer...            
    if(thisProcess.generatedPageFault) {
       ...Do something...
    }
A: 

Accordingly to the Intel documentation for the x86 processor - a page fault is Interrupt 14. The kernel at a low level would have an interrupt handler set up to trap that page fault. When that occurs, the kernel's interrupt handler handles the situation accordingly.

Now, since this is at the nuts and bolts level, and residing in ring 0 code, I would not think you can actually monitor that...you may get around that by creating a driver to simply watch for a page fault (again, dependent on the OS privileges and internal data structures occupied by the kernel), and pass the information back out to user-land space... I doubt if that would be easily exposable...

Hope this helps, Best regards, Tom.

tommieb75
Hey. Thanks for the information. Yes, I completely agree that you need a Interrupt Service Routine and have to hook it to the appropriate interrupt number. But, will it be possible to do that on the OS? You suggestion of writing a driver is very much reasonable, but can you point me on some resources that will help me do that?
Bhaktavatsalam Nallanthighal
+1  A: 

For linux the closest thing you'll get is reading /proc/self/stat and parse out the no. of pagefaults before and after your calls - the format is described here: http://linux.die.net/man/5/proc

Keep in mind, reading/parsing that file could itself cause pagefaults - atleast the 1. time you do it, of if you allocate memory to read it (like calling fopen)

nos
Hey. Great perspective on the second issue. Do you know of an intelligent way to prevent reading/parsing the file's pagefaults from being excluded?
Bhaktavatsalam Nallanthighal
If you just use a static buffer to read into, and use open/read (and not e.g. fopen/fread), you should be fairly certain there should not be any pagefaults other than possibly the 1.
nos