Assignment 2: Virtual Memory
Due Date: 8am (08:00), Monday 10th May (Week 10)
Worth 30 marks (of the 100 available for the class mark component of the course)
The 10% bonus for one week early applies
The extra 5% bonus for a submitted, working assignment within 48 hours of release, also applies
See course intro for exact details. The notional release time is midnight, Monday, 19th AprilUp to a 20% bonus is available for the advanced part of the assignment
To attempt the advanced part, you must finish the standard assignment at least one week early, and get permission from Kevin.Any bonus awarded for the advanced part can be used to make up for lost marks in any class mark component of the course. The familiarisation questions contained herein are the subject of your week 8 tutorial. Please answer the questions and bring them to your tutorial.
Contents
Introduction
In this assignment you will implement the virtual memory sub-system of OS/161. The existing VM implementation in OS/161, dumbvm, is a minimal implementation with a number of shortcomings. In this assignment you will adapt OS/161 to take full advantage of the simulated hardware by implementing management of the MIPS software-managed Translation Lookaside Buffer (TLB). You will write the code to manage this TLB. You will also write code to manage system memory.If you attempt the advanced portion of this assignment, you will implement paging, the mechanism by which memory pages of an active process can be sent to disk when memory is needed, and restored to memory when required by the program.
The System/161 TLB
In the System/161 machine, each TLB entry includes a 20-bit virtual page number and a 20-bit physical page number as well as the following five fields:- global: 1 bit; if set, ignore the PID bits in the TLB.
- valid: 1 bit; set if the TLB entry contains a valid translation.
- dirty: 1 bit; enables writing to the page referenced by the entry; if this bit is 0, the page is only accessible for reading.
- nocache: 1 bit; unused in System/161. In a real processor, indicates that the hardware cache will be disabled when accessing this page.
- pid: 6 bits; a context or address space ID that can be used to allow entries to remain in the TLB after a context switch.
For this assignment, you may ignore the pid field. Note, however, that you must then flush the TLB on a context switch (why?).
The System/161 Virtual Address Space Map
The MIPS divides its address space into several regions that have hardwired properties. These are:- kseg2, TLB-mapped cacheable kernel space
- kseg1, direct-mapped uncached kernel space
- kseg0, direct-mapped cached kernel space
- kuseg, TLB-mapped cacheable user space
The top of kuseg is 0x80000000. The top of kseg0 is 0xa0000000, and the top of kseg1 is 0xc0000000.
The memory map thus looks like this:
Address Segment Special properties 0xffffffff kseg2 0xc0000000 0xbfffffff kseg1 0xbfc00180 Exception address if BEV set. 0xbfc00100 UTLB exception address if BEV set. 0xbfc00000 Execution begins here after processor reset. 0xa0000000 0x9fffffff kseg0 0x80000080 Exception address if BEV not set. 0x80000000 UTLB exception address if BEV not set. 0x7fffffff kuseg 0x00000000
Setting Up Assignment 2
At the end of the previous assignment, you will have archived a copy of your previous solution. If you have not, please go back and do it now. These instructions assume the src directory from the previous assignment has been removed.
We also assume that the remainder of the ~/cs3231 directory is in place.
Copy the sources to this assignment into this directory.
% cp -r ~cs3231/assigns/asst2/src ~/cs3231You should now have a src directory to work on. You will need to increase the amount of physical memory to run some of the provided tests. Update your sys161.conf so that the ramsize is as follows
31 busctl ramsize=16777216
Configure OS/161 for Assignment 2
Remember to set your PATH environment variable as in previous assignments (e.g. run the 3231 command).
Before proceeding further, configure your new sources, and build and install the user-level libraries and binaries.
% cd ~/cs3231/src % ./configure % make
You have to reconfigure your kernel before you can use the framework provided to do this assignment. The procedure for configuring a kernel is the same as before, except you will use the ASST2 configuration file:
% cd ~/cs3231/src/kern/conf % ./config ASST2You should now see an ASST2 directory in the compile directory.
Building for ASST2
When you built OS/161 for ASST0, you ran make from compile/ASST0 . In ASST2, you run make from (you guessed it) compile/ASST2 .% cd ../compile/ASST2 % make depend % make % make installIf you now run the kernel as you did for previous assignments, you should get to the menu prompt. If you try and run a program, the kernel should panic with a message about vm_fault being unimplemented. For example, run p /bin/true at the OS/161 prompt to run the program /bin/true in ~/cs3231/root.
You are now ready to start the assignment.
Tutorial Exercises
Please answer the following questions and bring them to your tutorial in week 8. You should be familiar enough with navigating the kernel source that you can find the answers to the below questions by yourself (Hint: use the grep utility). You may also find the MIPS r3000 reference useful.1. What is the difference between the different MIPS address space segments? What is the use of each segment?
2. What functions exist to help you manage the TLB? Describe their use. (Hint: look in kern/arch/mips/include/tlb.h)
3. What macros are used to convert from a physical address to a kernel virtual address?
4. What address should the initial user stack pointer be?
5. What are the entryhi and entrylo co-processor registers? Describe their contents.
6. What do the as_* functions do? Why do we need as_prepare_load() and as_complete_load()?
7. What does vm_fault do? When is it called?
8. Assuming a 2-level hierarchical page table (4k pages), show for the following virtual addresses:
- The page number and offset;
- the translated address (after any page allocation); and
- the contents of the page table after the TLB miss.
- 0x100008
- 0x101008
- 0x1000f0
- 0x41000
- 0x41b00
Coding Assignment
This assignment involves designing and implementing a number of data-structures. Before you start, you should work out what data you need to keep track of, and what operations are required.Memory Management
This assignment requires you to keep track of physical memory. The current memory management implementation in dumbvm never frees memory; your implementation should handle both allocation and freeing of frames.You will need a frametable containing information about the memory available in the system. In the basic assignment you will need to keep track of whether a frame is used or not. In the advanced part, you will need to keep track of reference statistics and other information about the frames.
The functions that deal with memory are described in kern/include/vm.h. You may assume that only one page will be allocated at a time --- designing a page allocator that can allocate multiple pages at a time is surprisingly tricky. However, make sure that you never allocate memory (though kmalloc) that is larger than a page!
Note that alloc_kpages() should return the virtual address of the page, i.e., an address in kseg0.
Warning: alloc_kpages() can be called before vm_bootstrap(). The means that your implementation of alloc_kpages() must work before your frametable is initialised. You should just call ram_stealmem() if the frametable hasn't been initialised.
Address Space Management
OS/161 has an address space abstraction, the struct addrspace. To enable OS/161 to interact with your VM implementation, you will need to fill in the functions in kern/vm/addrspace.c, The semantics of these functions is documented in kern/include/addrspace.h.You may use a fixed-size stack region (say 16 pages) for each process.
Address Translation
The main goal for this assignment is to provide virtual memory translation for user programs. To do this, you will need to implement a TLB refill handler. You will also need to implement a page table. For this assignment, you will implement a 2-level hierarchical page table.Note that a hierarchical page table is a lazy data-structure. This means that the contents of the page table, including the second level pages, are only allocated when they are needed. You may find allocating the required pages at load time helps you start your assignment, however, your final solution should allocate pages only when a page-fault occurs.
The following questions may assist you in designing the contents of your page table
- What information do you need to store for each page?
- How does the page table get populated?
Testing and Debugging Your Assignment
To test this assignment, you should run a process that requires more virtual memory than the TLB can map at any one time. You should also ensure that touching memory not in a valid region will raise an exception. The huge and faulter tests in testbin may be useful.Apart from GDB, you may also find the trace161 command useful. trace161 will run the simulator with tracing, for example
% trace161 -t t -f outfile kernelwill record all TLB accesses in outfile.
Hints
Have a close look at the dumbvm implementation, especially vm_fault(). Although it is simple, you should get an idea on how to approach the rest of the assignment.We suggest you implement the assignment in the following order:
- Understand how a page table works, and its relationship with the TLB.
- Understand the specification and the supplied code.
- Work out a basic design for you implementation.
Start simple: assume a small address space. This means you can use a simple, static array as the page table, and can keep you code and data structures simple. - Implement the TLB exception handlers in vm.c using this simplified page table. Note, your final solution should use a 2-level page table!
- Implement the functions in kern/vm/addrspace.c that are required for basic functionality (e.g. as_create(), as_prepare_load(), etc.). Allocating user pages in as_define_region() may also simplify your assignment.
- Test and debug this. Use the debugger!
If you really get stuck, submit at least this much of the solution, and you should get some marks for it. - Understand how the page table works.
- Decide exactly what data structures you need.
- Work out the design for the proper solution, using the 2-level page table.
- Modify your implementation to include the 2-level page table.
- Write routines in kern/vm/frametable.c to manage free frames and copy pages. Modify kern/arch/mips/mips/vm.c to create and delete page table entries, and keep the TLB consistent with the page table.
- Use these routines to finish the functions in kern/vm/addrspace.c.
- Test and debug.
Basic Assignment Submission
Once your assignment works, you now need to generate a diff of the changes you made to OS/161. Firstly, you need to clean the source tree.% cd ~/cs3231/src % make clean % cd kern/compile/ASST2 % make cleanYou will be submitting a diff of your changes to the original tree. So generate a file containing this diff.
% cd ~/cs3231 % diff -N -r -u -X src/.diffex ~cs3231/assigns/asst2/src src > ~/asst2.diffThe arguments to diff are as follows
- -N: Include any new files you add to diff's output.
- -r: Recursively compare directories
- -u: Generate unified diff output.
- -X: Exclude files listed in .diffex from being included in the diff.
Testing Your Submission
Look here for information on testing and resubmitting your assignment.Submitting Your Assignment
Now submit the diff as your assignment.% cd ~ % give cs3231 asst2 asst2.diffYou're now done.
Even though the generated diff output should represent all the changes you have made to the supplied code, occasionally students do something "ingenious" and generate non representative diff output.
We suggest keeping a copy of the source tree as a compressed tar file until you get your results back. After getting your results back, you should be confident the diff was representative and you can safely keep the diff file as a record of your work, and consequently remove the tar file.
To create a compressed tar file and remove the sources to asst1:
% cd ~/cs3231 % tar cvzf asst2.tar.gz srcYou can confirm the tar file contains your assignment by listing its contents.
% tar tvfz asst2.tar.gzIf all looks well, you can remove the sources if your not going to attempt the advanced assignment.
% rm -rf srcYou're now done.
Advanced Assignment
Students who wish to attempt the advanced part of this assignment may gain up to an extra 6 marks (20%) for one (or multiple) of the following:- (easy) Shared pages and copy-on-write.
- (hard) Implement mmap() and related system calls.
- (hard) Implement demand-loading. You should load pages only when they are referenced by the user process, as opposed to at process creation.
- (harder) Implement paging. You should implement some page replacement algorithm and demonstrate your solution running under memory pressure.
Advanced Assignment Submission
Submission for the advanced assignment is similar to the basic assignment, except the advance component is given to a distinguished assignment name: asst2-adv Again, you need to clean the source tree.% cd ~/cs3231/src % make clean % cd kern/compile/ASST2 % make cleanGenerate a file containing this diff.
% cd ~/cs3231 % diff -N -r -u -X src/.diffex ~cs3231/assigns/asst2/src src > ~/asst2-adv.diff
% cd ~ % give cs3231 asst2-adv asst2-adv.diffYou're now done.