Search code examples
memorymemory-managementoperating-systempagingdisk

Paging / Page fault handler - how is a virtual address used to find specific content on the disk


Paging allows the machine to provide a layer of indirection between the virtual memory address space and the real one. After being given an address, the MMU goes through the page tables and determines if the corresponding frame is in memory. If it is, then it has found the real address and the relevant instruction can be performed.

What if it's not in RAM, however? What happens when the MMU visits the page table and sees that a corresponding frame needs to be fetched from the disk? How does it know where it is in the disk? How is virtual address we started with used to map something specific in the disk?

I suspect this isn't done through software because it'd be too expensive to have some sort of page table for the disk that maps all of that address space since it'd automatically double the number of I/O operations and use a considerable amount of memory, but is there another way?

Thanks!

Edit: the MMU doesn't deal with the disk other than when telling the OS to fetch a given page. The page fault handler within the OS is what fetches the content from the disk, but how?


Solution

  • The MMU isn't responsible for anything other than translating a virtual address to a physical address. It doesn't do disk accesses or anything.

    The trick used by many operating systems to swap memory to disk or map a file on disk to a virtual memory address space generally works by

    1. Mark the regions that are to be swapped to a disk as invalid in a page table so it causes a page fault on access
    2. When that region is accessed by the program, a page fault is raised by the MMU and control is given back to the operating system
    3. Deduce which page has been accessed and loads the appropriate data from disk into memory
    4. Mark the corresponding entry in the page table as valid and point it to memory containing the data loaded previously
    5. Return control to the program right before the load instruction so the memory access is retried
    6. The program now reads from the now-valid page non-the-wiser