In Project #2, part 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.
For the purposes of this worksheet, assume that Processor.PageSize is 128 bytes (0x80), 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).
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 0x7E 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 array spans multiple pages, a simply 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 array 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.