Hints for A3
This file is likely to be updated, so check back periodically and use your
browser's refresh button.
Quick Topic Links
Getting Started
When you reconfigure your kernel for Assignment 3, major changes
will be made that will prevent your kernel from compiling.
These changes include:
- the file kern/arch/mips/vm/dumbvm.c will no longer be compiled and
included in the kernel
- the file kern/vm/addrspace.c will be compiled and included
in the kernel - it was not included in the kernels that you compiled
for A1 and A2.
- the file kern/vm/vm.c will be compiled and included
in the kernel - it was not included in the kernels that you compiled
for A1 and A2.
- the preprocess option OPT_DUMBVM will no longer be defined.
This means that most of the fields in the addrspace structure
defined in include/addrspace.h will no longer be present in
that structure.
If you look at addrspace.c you will find empty skeletons
for the addrspace methods. They used to be implemented
in dumbvm.c, which is no longer being compiled. The idea
is that you should put your new implementations of those functions
into addrspace.c
Additionally vm.c contains some skeletons
for some VM funtions. They have been included just
to enable compilation of A3.
NOTE: that although you may be able to compile the kernel
it will not execute correctly until you have working
implementations for some of the functions in addrspace.c
and vm.c.
You are may choose to use any or none of these skeletons for A3.
Note that the prototypes for the functions in vm.c
are provided in kern/include/vm.h
and that
kern/arch/mips/include/vm.h
may also contain some useful information and definitions.
The best way to get started on A3 is to first get back to where
you can compile and run your kernel,
and then start to implement
the requirements for A3. If you don't do this, you will will not
be able to test your A3 work incrementally as you get it done, and
you'll
be left with a huge mess to test at the end - almost a guarantee
of testing and debugging nightmares.
One way to get started is to simply copy the implementations of
the addrspace functions from dumbvm.c
to addrspace.c.
You will also need initial implementations of the other functions
from dumbvm.c (like the fault handler, vm_fault
and the various low level VM and physical memory functions,
such as vm_bootstrap and getppages).
For these, you can use kern/vm/vm.c and/or
create a new source file
(e.g, kern/arch/mips/vm/vm.c)
and copy their implementations
from dumbvm.c to that file.
(If you use a new file,
don't forget to add your new file to the kernel configuration
and reconfigure your kernel!)
Finally, you will need to patch up the addrspace structure
in addrspace.h so that the fields that went away
when OPT_DUMBVM stopped being defined will be present again.
Once you've made these changes, make sure that you can build
and run your kernel. Everything that worked after A2 should
be working again.
Once you have done this, you can start working on the various
parts of A3. Since you have a working kernel, you should be
able to test each part of A3 as you build it.
Synchronization
- Remember that when a page fault occurs and the page needs
to be loaded, the process that caused the page fault will
be blocked while the page loads. While that process is blocked,
other processes in the system may run.
Handling TLB Faults
- Make sure that your TLB fault handler (vm_fault) does
not do anything that might cause another TLB fault. The result
will be a potentially infinite nesting of TLB faults from which
your kernel will probably not recover.
In particular, your TLB fault handler should avoid anything that
involves touching virtual addresses in the application's part
of the virtual address space, since those attempts might generate
faults, depending on what's in the TLB. Functions like
copyin and copyout are examples of functions
that touch application virtual addresses.
as_copy
- The as_copy function is needed only by the
fork system call. Most A3 testing will not involve
fork. Save as_copy for last - work on it
only if you have time.
Testing
For many tests, we will be relying on the virtual memory statistics
output by your kernel as an indication of whether your kernel is
behaving as expected.
With the exception of the programs used for the general multi-process
tests, none of these programs make use of command line arguments, and
none make use of system calls other than write to the
console and exit.
I didn't get the A2 system calls working. What can I do?
A3 does not depend heavily on your A2 work.
Most of the testing for A3 will involve only writes to the
console and _exit, both of which are implemented in the
OS/161 code that we distribute to you.
Programming the TLB
The code that is used to implement the TLB routines
can be found in kern/arch/mips/vm/tlb-mips1.S
A description of what these functions
do as well as some useful #defines
can be found in
kern/arch/mips/include/tlb.h
You can find out quite a lot of detail about how
the R3000 TLB operates in the
R3000 manual.
See "Memory Management and the TLB, Chapter 6".
PDF viewer starting page number is 80 and document
starting page number is "6-1".
Tracking Virtual Memory Statistics
Please do not preload and/or save and restore TLB contents.
Please only demand load the TLB. Even after a TLB invalidation.
This will allow us to more easily compare statistics.
Below we try to answer some frequently asked questions and
to clarify what the stats should count.
"TLB Reloads": The number of TLB reloads (TLB faults that did not require a page fault)
- This is what some people/systems call a "soft fault".
- It is meant to count how many times a TLB fault occurs when the
page is actually in memory but there is not a valid mapping in the TLB
(so the only action required is to reload/install a valid TLB mapping for the page).
So this is a count of TLB faults that do not result in a page fault.
- Again, you should not be preloading or reloading the TLB, the TLB
should only be demand loaded.
"Page Faults (Zeroed)" : The number of Page Faults that did not require a disk copy.
- These are interesting because they are cheaper than page faults that require
a copy from disk.
- Only count full pages zeroed.
If part of the page is copied from disk and the
remainder is zeroed that does not count as a Page Fault (Zeroed),
instead it counts as a "Page Fault (Disk)"
"Page Faults (Disk)": The number of Page Faults that required a disk copy (e.g., loading a text/code page)
- These are the most expensive faults because they require a copy from disk.
- If part of the page is copied from disk and the
remainder is zeroed, count that as a Page Fault (Disk)".
Do not count it as a Page Fault (Zeroed).
Demand Loading Pages with Program Arguments
If you get to the point where you are calling runnprogram
or using execv with arguments you may need to
touch (and/or preload) one or more pages in order to copy
the arguments onto the new program's stack. This is perfectly
acceptable but only load those pages that are necessary
(if there aren't any appropriate VM statistics to update
when this happens don't worry about it).
Losing Output During Booting
Sometimes it can be helpful to buffer more kprintf output
before the kprintf subsystem is initialized.
To permit more kprintfs to happen before everything is intialized
you can make a modification similar to the one shown below
to the file kern/dev/generic/console.c
#if OPT_A3
/* increase the size substantially */
#define DELAYBUFSIZE 10240
#else
#define DELAYBUFSIZE 1024
#endif
static char delayed_outbuf[DELAYBUFSIZE];
Understanding How Physical Memory is Handled
Look at gettppages in
kern/arch/mips/mips/dumbvm.c.
Notice how getppages gets more page frames when needed.
When is getppages first called and why?
Think about how that will have to differ in an implementation
that needs some sort of data structure (e.g., a coremap) to find free pages.
Have a look at ram_stealmem in
kern/arch/mips/vm/ram.c .
Understand how it works and what
firstpaddr and lastpaddr are doing.
Have a look at kern/vm/kmalloc.c
and kmalloc.
Understand how kmalloc figures out which physical
page frame to give to the caller (when a new frame is needed)
and how the physical address of that frame is turned
into a virtual address that the caller/kernel uses.
Be sure you understand how the MIPS translates a kernel
virtual address to a physical address and what that
means as it relates to allocating free page frames.
Have a look at
kern/arch/mips/vm/ram.c and the function ram_bootstrap().
You should figure out what is in physical memory at this point, where
it is in physical memory and why.
Looking at the comments and information in
kern/arch/sys161/startup/start.S
should help quite a bit.
Remember that the kernel is also a MIPS executable (ELF file).
You may find that you can better understand
what lives in memory after booting the kernel if you examine
the contents of it's executable file (ELF headers).
# Dump the contents of the ELF file into readelf.out
# Now you can look at the contents of the readelf.out
# file to learn more about the kernel.
cs350-readelf -a kernel-ASST3 > readelf.out
Think about how to mark the frames that are already occupied
as used in your coremap.
Note that there will be a bit of a chicken and an egg problem.
In order to mark frames as used you will need to allocate a coremap
data structure (using kmalloc). But kmalloc may be needed to find
one or more free page frames to allocate.