[SOLVED] OSlab-Assignment 2

30.00 $

Category:

Description

5/5 - (2 votes)

Assignment 2: System Calls and Processes

CSE3201: Operating System

Computer Science and Engineering

Version 1.0

 

2             Introduction

In this assignment you will be implementing a set of file-and process-related system calls. Upon completion, your operating system will be able to run multiple copies of a single application at user-level and perform some basic file I/O.

A substantial part of this assignment is understanding how OS/161 works and determining what code is required to implement the required functionality. Expect to spend at least as long browsing and digesting OS/161 code as actually writing and debugging your own code.

Your current OS/161 system has minimal support for running executable, nothing that could be considered a true process. Assignment 2 starts the transformation of OS/161 into something closer to a true multi-tasking operating system. After this assignment, it will be capable of running multiple processes from actual compiled programs stored in your account. The program will be loaded into OS/161 and executed in user mode by System/161; this will occur under the control of your kernel. First, however, you must implement part of the interface between user-mode programs (”userland”) and the kernel. As usual, we provide part of the code you will need. Your job is to design and build the missing pieces.

Our code can run one user-level C program at a time as long as it doesn’t want to do anything but shut the system down. We have provided sample user programs that do this (reboot, halt, poweroff), as well as others that make use of features you might be adding in this and future assignments. So far, all the code you have written for OS/161 has only been run within, and only been used by, the operating system kernel. In a real operating system, the kernel’s main function is to provide support for user-level programs. Most such support is accessed via ”system calls.” We give you one system call, reboot(), which is implemented in the function sys reboot() in main.c. In GDB, if you put a breakpoint on sys reboot and run the ”reboot” program, you can use ”backtrace” to see how it got there.

For those attempting the advanced version of the assignment, you will also be implementing the subsystem that keeps track of multiple tasks. You must decide what data structures you will need to hold the data pertinent to a ”process” (hint: look at kernel include files of your favorite operating system for suggestions, specifically the proc structure.) The first step is to read and understand the parts of the system that we have written for you.

2.1           User-level programs

Our System/161 simulator can run normal C programs if they are compiled with a cross-compiler, cs161-gcc. This runs on a host (e.g., a Linux x86 machine) and produces MIPS executables; it is the same compiler used to compile the OS/161 kernel. To create new user programs, you will need to edit the Makefile in bin, sbin, or testbin (depending on where you put your programs) and then create a directory similar to those that already exist. Use an existing program and its Makefile as a template.

2.2           Design

In the beginning, you should tackle this assignment by producing a DESIGN DOCUMENT. The design document should clearly reflect the development of your solution. They should not merely explain what you programmed. If you try to code first and design later, or even if you design hastily and rush into coding, you will most certainly end up in a software ”tar pit”. Don’t do it! Plan everything you will do. Don’t even think about coding until you can precisely explain to your partner what problems you need to solve and how the pieces relate to each other. Note that it can often be hard to write (or talk) about new software design, you are facing problems that you have not seen before, and therefore even finding terminology to describe your ideas can be difficult. There is no magic solution to this problem, but it gets more comfortable with practice. The important thing is to go ahead and try. Always try to describe your ideas and designs to someone else. In order to reach an understanding, you may have to invent terminology and notation, this is fine. If you do this, by the time you have completed your design, you will find that you have the ability to discuss problems that you have never seen before efficiently. Why do you think that CSE is filled with so much jargon? To help you get started, we have provided the following questions as a guide for reading through the code. We recommend that you answer questions for the different modules and be prepared to discuss with your group member. Once you have prepared the answers, you should be ready to develop a strategy for designing your code for this assignment.

3             Code Walking

Discuss your answers to the code walk-through questions to your assignment partner.

3.1           Running user-level program

kern/{syscall,lib,vm}

These directories contain the files that are responsible for the loading and running of userlevel programs. Currently, the files in these directories are loadelf.c, runprogram.c, and uio.c, and etc., although you may want to add more of your own during this assignment. Understanding these files is the key to getting started with the assignment, especially the advanced part, the implementation of multi-programming. Note that to answer some of the questions, you will have to look in files outside these directories.

syscall/loadelf.c

This file contains the functions responsible for loading an ELF executable from the filesystem and into virtual memory space. Of course, at this point this virtual memory space does not provide what is normally meant by virtual memory, although there is translation between the addresses that executables “believe” they are using and physical addresses, there is no mechanism for providing more memory than exists physically.

syscall/runprogram.c

This file contains only one function, runprogram() , which is the function that is responsible for running a program from the kernel menu. Once you have designed your file system calls, a program started by runprogram() should have the standard file descriptors (stdout, stderr) available while it’s running.

In the advanced assignment, runprogram() is a good base for writing the execv() system call, but only a base. When writing your design doc, you should determine what more is required for execv() that runprogram() does not need to worry about. Once you have design your process framework, runprogram() should be altered to start processes properly within this framework.

lib/uio.c

This file contains functions for moving data between kernel and user space. Knowing when and how to cross this boundary is critical to properly implementing userlevel programs, so this is a good file to read very carefully. You should also examine the code in vm/copyinout.c.

Questions

3.1.1. What are the ELF magic numbers?

3.1.2. What is the difference between UIO USERISPACE and UIO USERSPACE? When should one use UIO SYSSPACE instead?

3.1.3. Why can the struct uio that is used to read in a segment be allocated on the stack in load segment() (i.e., where does the memory read actually go)?

3.1.4. In runprogram(), why is it important to call vfs close before going to usermode?

3.1.5. What function forces the processor to switch into usermode?      Is this function machine dependent?

3.1.6. In what file are copyin and copyout defined? memmove? Why can’t copyin and copyout be implemented as simply as memmove?

3.1.7. What (briefly) is the purpose of userptr t?

3.2           Traps and SystemCalls

kern/arch/mips

Exceptions are the key to operating systems; they are the mechanism that enables the OS to regain control of execution and therefore do its job. You can think of exceptions as the interface between the processor and the operating system. When the OS boots, it installs an “exception handler” (carefully crafted assembly code) at a specific address in memory. When the processor raises an exception, it invokes this, which sets up a “trap frame” and calls into the operating system. Since “exception” is such an overloaded term in computer science, operating system lingo for an exception is a “trap”, when the OS traps execution. Interrupts are exceptions, and more significantly for this assignment, so are system calls. Specifically, syscall.c handles traps that happen to be syscalls. Understanding at least the C code in this directory is key to being a real operating systems junkie, so we highly recommend reading through it carefully.

locore/trap.c mips trap() is the key function for returning control to the operating system. This is the C function that gets called by the assembly exception handler. enter new process() is the key function for returning control to user programs. kill curthread() is the function for handling broken user programs; when the processor is in usermode and hits something it can’t handle (say, a bad instruction), it raises an exception. There’s no way to recover from this, so the OS needs to kill off the process. The advance part of this assignment will include writing a useful version of this function.

syscall/syscall.c syscall() is the function that delegates the actual work of a system call off to the kernel function that implements it. Notice that reboot is the only case currently handled. You will also find a function, enter forked process(), which is a stub where you will place your code to implement the fork system call.

Questions

3.2.1. What is the numerical value of the exception code for a MIPS system call?

3.2.2. Why does mips trap() set curspl to IPL HIGH “manually”, instead of using splhigh()?

3.2.3. How many bytes is an instruction in MIPS? (Answer this by reading syscall() carefully, not by looking somewhere else.)

3.2.4. What is the contents of the struct mips syscall stored?

3.2.5. What would be required to implement a system call that took more than 4 arguments?

3.2.6. What is the purpose of userptr t?

3.3           userland/lib/crt0/mips/crt0.S : the C startup code)

There’s only one file in here, crt0.S, which contains the MIPS assembly code that receives control first when a user-level program is started. It calls main() . This is the code that your execv implementation will be interfacing to, so be sure to check what values it expects to appear in what registers and so forth.

userland/lib/libc/unix/errno.c

This is where the global variable errno is defined.

userland/lib/libc/arch/mips/syscalls-mips.S

This file contains the machine-dependent code necessary for implementing the userlevel side of MIPS system calls.

syscalls.S

This file is created from syscalls-mips.S at compile time and is the actual file assembled to put into the C library. The actual names of the system calls are placed in this file using a script called gensyscalls.sh (in libc/syscalls) that reads them from the kernel’s header files. This avoids having to make a second list of the system calls. In a real system, typically each system call stub is placed in its own source file, to allow selectively linking them in. OS/161 puts them all together to simplify the makefiles.

Questions

3.3.1. What is the purpose of the SYSCALL macro?

3.3.2. What is the MIPS instruction that actually triggers a system call? (Answer this by reading the source in this directory, not looking somewhere else.)

3.4           kern/include

The files vfs.h and vnode.h in this directory contain function declarations and comments that are directly relevant to this assignment.

Questions

3.4.1. How are vfs open, vfs close used? What other vfs () calls are relevant?

3.4.2. What are VOP READ, VOP WRITE? How are they used?

3.4.3. What does VOP TRYSEEK do?

3.4.4. Where is the struct thread defined? What does this structure contain?

3.5           fork()

fork()

Answer these questions by reading the fork() man page and the sections dealing with fork() in the textbook.

Questions

3.5.1. What is the purpose of the fork() system call?

3.5.2. What process state is shared between the parent and child?

3.5.3. What process state is copied between the parent and child?

 

4.2           Obtaining and setting up the distribution in CVS

In this section, you will be setting up the cvs repository that will contain your code. Only one of you needs to do the following. We suggest your partner sit in on this part of the assignment.

  • Change to your group’s project (assignment) directory, and create a directory for your cvs repository.

% cd $HOME/osprjXXXX

% mkdir cvsroot

%

  • Check the permissions match as below.

% ls -l total 4 drwxrws— 4 <user_name> <group_name>      4096 Mar 10 15:22 cvsroot %

If not, you have done something wrong with your umask setup and need to fix it.

As a special case, if your permissions look like this:

drwxrwx— 4 <user_name> <group_name> 4096 Mar 10 15:22 cvsroot Your directory does not have the setgid bit set. Run this command:

chmod g+s cvsroot

  • Check your CVSROOT environment variable as below.

% echo $CVSROOT

$HOME/osprjXXXX/cvsroot

%

If your CVSROOT is not an appropriately modified version of the above, then you will need to fix it.

  • Initialize the cvs repository. This should be the only time you need to do it for the rest of your assignments.

% cvs init