CSE 120: Nachos VM Worksheet Solutions

In Task 2 in Project 2, you will be creating and initializing the page tables used by user-level processes running on Nachos. This worksheet is intended to give you practice with Nachos page tables so that you are comfortable understanding how virtual memory works in Nachos. The page table for a user-level process is represented by the TranslationEntry[] pageTable array in the UserProcess class. In the UserProcess constructor, you will be initializing each TranslationEntry in the array to have each virtual page point to an allocated physical page.

The size of a memory page in Nachos (Processor.pageSize) is 1024 (0x400) bytes. For the purposes of this worksheet, assume that the program being loaded requires 4 pages and physical memory has 8 pages. In the diagram below, we have assigned a set of physical pages (from Processor.mainMemory) to virtual pages (in UserProcess.pageTable).

  • What is the value of UserProcess.numPages? 4

  • What is the value of Processor.numPhysPages? 8

  • What are the values of the UserProcess.pageTable mappings?

  • What is the virtual address of virtual page 2 (the start of virtual page 2 in the virtual address space)? 2048 (0x800)

  • What is the physical address of virtual page 2? 6144 (0x1800)
    (The physical address will be used as the offset in the Processor.mainMemory array.)

  • What physical page does the virtual address 2100 reside in? 6

  • What is the offset of the virtual address 2100? 52

  • What is the physical address of the virtual address 2100? 6196 (0x1834)

  • You will be modifying the implementations of UserProcess.readVirtualMemory and UserProcess.writeVirtualMemory. The initial implementations use System.arraycopy to copy the data bytes between the kernel and the process virtual address space. In the general case that your implementation has to handle, why does the simple System.arraycopy no longer work? (Hint: Given the page table above, consider the case for readVirtualMemory when vaddr is 1022 (0x3FE) and data.length is 4.)

    The initial implementations with System.arraycopy assume that the virtual and physical page numbers are the same, and hence that the physical pages are contiguous in mainMemory. As a result, even if the data spans multiple pages, a simple arraycopy will work correctly.

    In the more general implementation, however, there can be an arbitrary mapping between virtual and physical pages. So if the data spans two virtual pages, the physical pages holding the actual data bytes will likely not be contiguous. Using arraycopy on the entire data array will therefore break because it assumes that the data bytes are all contiguous in the array.