Docsity

Nachos Project 2: Multiprogramming with Virtual Memory & User Exceptions, Study Guides, Projects, Research of Operating Systems

Toggle sidebar
bg1
COMP310: Nachos Project #2: Multiprogramming
Due: Monday, Nov 1, 1:15pm
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
1
pf3
pf4
pf5

Partial preview of the text

Download Nachos Project 2: Multiprogramming with Virtual Memory & User Exceptions and more Study Guides, Projects, Research Operating Systems in PDF only on Docsity!

COMP310: Nachos Project #2: Multiprogramming

Due: Monday, Nov 1, 1:15pm

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.

  1. [25 pts] Next, use these new facilities to implement the Exec and Exit system calls as defined in the Nachos System Call Interface and in userprog/syscall.h. If an executing user processes requests a system call (by executing a trap instruction) the machine will transfer control to your kernel by calling ExceptionHandler in exception.cc. Your kernel code must extract the system call identifier and the arguments from the machine registers, decode them, and call internal procedures that implement the system call. You may impose a reasonable limit on the maximum size of a file name. Also, use of Machine:ReadMem and Machine:WriteMem is not forbidden as the comment in machine.h implies. When an Exec call returns, your kernel should have created a new process and started a new thread executing within it to run the specified program. At this point, do not concern yourself with setting up OpenFileIds. For now, you will be able to run user programs, but they will not be able to read any input or write any output. For Exec, you must copy the filename argument from user memory into kernel memory safely, so that a malicious or buggy user process cannot crash your kernel or violate security. The filename string address (char*) passed into the kernel as an argument is a process virtual address; in order for the kernel to access the filename it must locate the characters in the kernel address space (i.e., in the machine’s physical ”main memory” array) by examining the page table for the process. In particular, your kernel must handle the case where the filename string crosses user page boundaries and resides in noncontiguous physical memory. You must also detect an illegal string address or a string that runs off the end of the user’s address space without a terminating null character. Your kernel should handle these cases by returning an error (SpaceId 0) from the Exec system call. Exec must return a unique process identifier (SpaceId), which can be used as an argument to Join. Your kernel will need to keep a table of active processes, and you may find the Table class described above useful here, too. You may find it convenient to implement process exit as an internal kernel procedure called by the Exit system call handler, rather than calling the lower-level procedures directly from ExceptionHandler. This will make it easier to kill a process from inside the kernel (e.g., if the process has some kind of fatal error), by calling the internal exit primitive from another kernel procedure (e.g., ExceptionHandler) in the target process context. In general, this kind of careful internal decomposition will save you from reinventing and redebugging wheels, and it is always good practice. In your Exit system call hander, print out the status value passed as the parameter. This will aid in debugging and testing. [10 pts] Write test programs that test multiprogramming: Exec, Exit, and your AddrSpace imple- mentation. You should make sure that Exec cleanly handles problem cases where the program string argument is invalid for some reason – bad string address, does not end in a null character, and spec- ifies a filename that doesn’t exist. You should also test the boundaries of AddrSpace – trying load a program that doesn’t fit into physical memory, being able to load many programs one after the other (i.e., AddrSpace releases memory when a process goes away so it can be reused again).

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.

  1. Change the signature of Exec to be the following:

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.

  1. To pass in the arguments to the created process, you need to do two things. First, you need to copy the arguments into the process’ address space. Second, you need to setup the machine so that, when the process starts executing, the argc and argv arguments are passed into the main() routine of the program.

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.