views:

48

answers:

2

When a page fault exception is raised because the content CPU is trying to access have not been loaded in to memory, how does the OS locate the missing content on the secondary storage (e.g. hard disk)?

Thanks for your explanation in advance.

-ivan

A: 

The OS will locate the memory-mapping structure associated with the page fault, which among other things, will have a handle to the associated file. It will take the handle, do the math to figure out the offset into the file, then issue a Read. When the read returns, the memory manager will copy it into RAM and mark that page as backed by a physical frame (actual RAM).

Paul Betts
+3  A: 

Short Version: The Invalid PTE's address bits will map to the offset within the secondary storage (swap file).

Longer Version: In order to understand what happens there, let's have a very quick recap on the workings of Virtual-to-physical translation. I'll discuss the answer on x86 platform.

The CR3 processor register has 20 bits allocated to point to the start of the page directory. The top 10 bits of the virtual address encode which page directory entry (PDE) the address is used, which is an array of page table entries (PTEs), the next 10 bits in the address encode which PTE refers to the actual physical page in question. The last 12 bits are offset within the page.

When the operating system evicts a page from memory, the PTE is marked as invalid, and the address bits of the PTE become offset into the page file (answering your original question).

It gets slightly more complex with memory mapped file, as they use prototype PTEs.

If you're interested in more info, I strongly recommend Mark russinovich "Windows Internals" book.

Yonatan
On Windows there can be multiple paging files, so that invalid PTE also encodes *which* paging file to read from. You can see what this looks like in a picture in this blog post: http://analyze-v.com/?p=264. I'm being nitpicky though, good answer.
snoone