



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




This project document based on an expanded version of Nachos Project 2 that was written by Geoff Voelker.
The second phase of Nachos is to support multiprogramming. As in the first assignment, we give you some of the code you need and your job is to complete the system and enhance it.
Up to now, all the code you have written for Nachos has been part of the operating system kernel. In a real operating system the kernel not only uses its procedures internally, but also allows user-level programs to access some of its routines via system calls. An executing user program is a process. In this project you will modify Nachos to support multiple processes, using system calls to have processes request services from the kernel.
Since your kernel does not trust user programs to execute safely, the kernel and the (simulated) hardware will work together to protect the system from damage by malicious or buggy user programs. To this end, you will implement simple versions of key mechanisms found in real operating system kernels: virtual address- ing, protected system calls and kernel exception handling, and preemptive timeslicing. Virtual addressing prevents user processes from accessing kernel data structures or the memory of other programs; your ker- nel will use process page tables to safely allow multiple processes to reside in memory at the same time. With protected system calls and exceptions, all entries into the kernel funnel through a single kernel routine, âExceptionHandlerâ; for extra credit you may âbullet-proofâ this routine so that buggy or malicious user programs cannot cause the kernel to crash or behave inappropriately. Finally, your kernel will use preemp- tive scheduling to share the simulated CPU fairly among the active user processes, so that no process can take over the system. All of these protection mechanisms require cooperation between the hardware and the operating system kernel software. Your implementation will be based on âhardwareâ support in the Nachos MIPS simulator, which resembles a real MIPS processor.
If all processes are created by other processes, then who creates the first user process? The operating system kernel creates this process itself as part of its initialization sequence. This is bootstrapping. You can âbootâ the Nachos kernel by running nachos with the -x option (x for âexecuteâ), giving the name of an initial program to run as the initial process. The Nachos release implements the -x option by calling StartProcess in progtest.cc to handcraft the initial process and execute the initial program within it. The initial process may then create other processes, which may create other processes...and so on.
The first step is to read and understand the part of the system we have written for you. The code that you will have to look at and modify is spread over several directories. There are some kernel files in âuserprogâ, a few additional machine simulation files in âmachineâ, and a stub file system in âfilesysâ. The user programs are in âtestâ, and utilities to generate a Nachos loadable executable are in âbinâ. Since Nachos executes MIPS instructions (and there arenât very many MIPS machines left!), we also provide you a cross-compiler. The cross-compiler runs on Linux and Solaris and compiles user programs into MIPS format.
The code we provide can run a single user-level âCâ program at a time. As a test case, weâve provided you with a trivial user program, âhaltâ. All âhaltâ does is to turn around and ask the operating system to shut the
machine down. To run the âhaltâ program, make and then run Nachos in the âuserprogâ directory:
% cd userprog % ./nachos -x ../test/halt
Trace what happens as the user program gets loaded, runs, and invokes a system call. When you are writing and testing this project, you will need to write other test programs (in c, in the test directory) to ensure that your OS system calls work correctly.
As with the previous assignment, you may find Nartenâs Road Map to Nachos (linked from course web site) helpful. In addition, Geoff Voelker has posted some tips for getting started on the UCSD version of this project which I have linked from the course web site. Also, it is OK to change the constants in âmachine.hâ if it helps you design better test cases. For example, you can change the amount of âphysical memoryâ allocated by Nachos. Note that the code for this part of the project is enabled by the USER PROGRAM compiler macro.
The files for this assignment are:
progtest.cc â test routines for running user programs. addrspace.h, addrspace.cc â create an address space in which to run a user program, and load the program from disk. syscall.h â the system call interface: kernel procedures that user programs can invoke.
exception.cc â the handler for system calls and other user-level exceptions, such as page faults. In the code we supply, only the âhaltâ system call is supported. bitmap.h, bitmap.cc â routines for manipulating bitmaps (this might be useful for keeping track of physical page frames) filesys.h, openfile.h (found in the filesys directory) â a stub defining the Nachos file system routines. For this assignment, we have implemented the Nachos file system by directly making the corresponding calls to the UNIX file system; this is so that you need to debug only one thing at a time. In assignment four, weâll implement the Nachos file system for real on a simulated disk. translate.h, translate.cc â translation table routines. In the code we supply, we assume that every virtual address is the same as its physical address â this restricts us to running one user program at a time. You will generalize this to allow multiple user programs to be run concurrently. We will not ask you to implement virtual memory support until in assignment 3; for now, every page must be in physical memory.
machine.h, machine.cc â emulates the part of the machine that executes user programs: main memory, processor registers, etc.
mipssim.cc â emulates the integer instruction set of a MIPS R2/3000 processor. console.h, console.cc â emulates a terminal device using UNIX files. A terminal is (i) byte oriented, (ii) incoming bytes can be read and written at the same time, and (iii) bytes arrive asynchronously (as a result of user keystrokes), without being explicitly requested.
Note: to cleanly handle these failures, you will need to move the AddrSpace loading code out of the constructor and into a new AddrSpace method that can return an error. It is poor programming practice to put code that can fail into a class constructor, as the Nachos designers have done in this release. If necessary, update StartProcess to use your new AddrSpace interface so that you do not break the Nachos -x option. Modify AddrSpace to call the memory manager to release the pages allocated to a process when the process is destroyed. Make sure your AddrSpace code also releases any frames allocated to the process in the case where it discovers that it does not have enough memory to load the entire process.
3.[10 pts] The current version of the âexecâ system call does not provide any way for the user program to
pass parameters or arguments to the newly created address space. UNIX does allow this, for instance, to pass in command line arguments to the new address space. Implement this feature.
There are two steps to get this done. First, you need a new signature for Exec so that you can give it the arguments to be passed to the new process. Second, you need to figure out how to pass those arguments to that process.
SpaceID Exec(char *name, int argc, char **argv, int willJoin);
ânameâ is the filename as before, âargcâ is the number of arguments, âargvâ is the array of argument strings, and âwillJoinâ determines whether this process will be joined by another. The semantics of argc and argv are exactly like standard C. The semantics of the âwillJoinâ argument are the same as for Nachos threads â non-zero means that a process will eventually Join on the one being Execâed. If you do not implement the Join system call, you should pass in â0â for this argument.
You will need to change the declaration of Exec in syscall.h, and you will need to change your test programs to use this new version of Exec. If a program does not pass in arguments to the new process, just pass in zero for argc, argv, and willJoin.
NOTE: When you construct the arguments to the child process, include as the first argument (argv[0]) the program name (the ânameâ argument passed as the first argument to Exec). This follows C con- ventions.
Before you can copy in the arguments, you need to create some space for them. To do this, grow the processâ virtual address space by some amount (e.g., just like the address space is grown to accomo- date the user stack). You decide how much (and document this in your writeup), and make sure that you check that the arguments do not overflow this region. Donât make it too large, though, or you will consume too much physical memory. Then copy the arguments into that region of the virtual address space (this is the opposite of reading system call arguments out of the virtual address space).
To setup the machine so that the process is started with the arguments, you can assume that the arguments to main use the same set of registers as the arguments passed in from system calls (i.e., same calling convention). The comments at the top of ExceptionHandler describe which registers to use.
NOTE: To test this, you will need to create a test program that has arguments in its main routine:
int main(int argc, char *argv[]) { // e.g., check to make sure that the arguments are valid... }
You do not need to support command line arguments to the first process that is started when nachos starts, just processes that are Exec()ed.