4

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?

aralar
  • 3,022
  • 7
  • 29
  • 44
  • The OS looks in the page table. There may be a single all-in-one page table, but more likely there are two, one which maps virtual addresses to real addresses, for pages that are "resident", and a second which maps all pages to their disk addresses (or indicates that a page is not yet allocated and should be cleared). (There may be other terms for this second page table (and the first one, for that matter), depending on the OS.) – Hot Licks Dec 06 '14 at 00:28
  • Note that the page table for disk locations would be so frequently referenced that, even if it were pageable (some are, some aren't), it would not be paged out for an active process. – Hot Licks Dec 06 '14 at 00:29

2 Answers2

1

The operating system needs to know where the data is. When a page fault occurs, the memory could be in a number of places. Off the top of my head:

  1. It may be initialized data (imagine program startup) that is not on disk at all.

  2. It may be static data (and in some cases dynamic data) that has to be loaded from an executable or shared executable file.

  3. It may have to be loaded from some location within one or more page files.

The MMU does not handle this. All it does is trigger a page fault. The operating system fault handler has to identify the address being accessed, locate the page needed (from one of the sources above), (free physical memory if necessary), allocate a page of physical memory, load the data from the page (if it exits) into physical memory, exit the exception handler to restart the instruction that caused the page fault.

user3344003
  • 20,574
  • 3
  • 26
  • 62
1

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
tangrs
  • 9,709
  • 1
  • 38
  • 53