Docsity

Operating System Project: File I/O and Disk Management - Prof. Jonathan Walpole, Study Guides, Projects, Research of Operating Systems

Toggle sidebar
bg1
Project 5 Operating Systems
Programming Project 5:
User-Level Processes
Due Date: 11-29-06
Overview and Goal
In this project, you will explore user-level processes. You will create a single process, running in its
own address space. When this user-level process executes, the CPU will be in “user mode.”
The user-level process will make system calls to the kernel, which will cause the CPU to switch into
“system mode.” Upon completion, the CPU will switch back to user mode before resuming execution
of the user-level process.
The user-level process will execute in its own “logical address space.” Its address space will be broken
into a number of “pages” and each page will be stored in a frame in memory. The pages will be resident
(i.e., stored in frames in physical memory) at all times and will not be swapped out to disk in this
project. (Contrast this with “virtual” memory, in which some pages may not be resident in memory.)
The kernel will be entirely protected from the user-level program; nothing the user-level program does
can crash the kernel.
Download New Files
The files for this project are available in:
http://www.cs.pdx.edu/~harry/Blitz/OSProject/p5/
Please retain your old files from previous projects and don’t modify them once you submit them.
You should get the following files:
Switch.s
Runtime.s
System.h
System.c
List.h
List.c
BitMap.h
BitMap.c
Page 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Operating System Project: File I/O and Disk Management - Prof. Jonathan Walpole and more Study Guides, Projects, Research Operating Systems in PDF only on Docsity!

Programming Project 5:

User-Level Processes

Due Date: 11-29-

Overview and Goal

In this project, you will explore user-level processes. You will create a single process, running in its own address space. When this user-level process executes, the CPU will be in “user mode.” The user-level process will make system calls to the kernel, which will cause the CPU to switch into “system mode.” Upon completion, the CPU will switch back to user mode before resuming execution of the user-level process. The user-level process will execute in its own “logical address space.” Its address space will be broken into a number of “pages” and each page will be stored in a frame in memory. The pages will be resident (i.e., stored in frames in physical memory) at all times and will not be swapped out to disk in this project. (Contrast this with “virtual” memory, in which some pages may not be resident in memory.) The kernel will be entirely protected from the user-level program; nothing the user-level program does can crash the kernel.

Download New Files

The files for this project are available in: http://www.cs.pdx.edu/~harry/Blitz/OSProject/p5/ Please retain your old files from previous projects and don’t modify them once you submit them. You should get the following files: Switch.s Runtime.s System.h System.c List.h List.c BitMap.h BitMap.c

This code adds the following classes: DiskDriver FileManager FileControlBlock OpenFile You will use these classes, but you should not modify them. There will be a single DiskDriver object (called diskDriver ) which is created and initialized at start-up time. There will be a single FileManager object (called fileManager ) which is created and initialized at start-up time. The new main function contains statements to create and initialize the diskDriver and the fileManager objects. FileControlBlock and OpenFile objects will be handled much like Threads and ProcessControlBlocks. They are a limited resource. A limited supply is created at start-up time and then they are managed by the fileManager. There is a free list of FileControlBlock objects and a free list of OpenFile objects. The fileManager oversees both of these free lists. Threads may make requests and may return resources, by invoking methods in the fileManager. The diskDriver object encapsulates all the hardware specific details of the disk. It provides a method that allows a thread to read a sector from disk into a memory frame and it provides a method that writes a frame from memory to a sector on disk.

Other Changes To Your Kernel Code

Please make the following changes to your copy of Kernel.h : Change NUMBER_OF_PHYSICAL_PAGE_FRAMES = 27 -- for testing only to: NUMBER_OF_PHYSICAL_PAGE_FRAMES = 100 -- for testing only Change --diskDriver: DiskDriver --fileManager: FileManager to: diskDriver: DiskDriver fileManager: FileManager Add a function prototype for the function InitFirstProcess. You can add it after the other function prototypes: Change ProcessFinish (exitStatus: int) to: ProcessFinish (exitStatus: int)

InitFirstProcess ()

Seek 8000 For this task, you should modify only the handler methods (e.g., Handle_Sys_Fork , Handle_Sys_Join , etc.) You should not modify SyscallTrapHandler or the wrapper functions in UserSystem.

Task 3:

Implement the Exec syscall. The Exec syscall will read a new executable program from disk and copy it into the address space of the process which invoked the Exec. It will then begin execution of the new program. Unless there are errors, there will not be a return from the Exec syscall.

The User-Level View

First, let’s look at our operating system from the users’ point of view. User-level programs will be able to invoke the following kernel routines: Exit Shutdown Yield Fork Join Exec Create Open Read Write Seek Close (This is the grand plan for our OS; these system calls will not be implemented in this project.) These syscalls are quite similar to kernel syscalls of the same names in Unix. We describe their precise functionality later. A user-level program will be written in KPL and linked with the following files: UserSystem.h UserSystem.c UserRuntime.s We are providing a sample user-level program in MyProgram.h / .c.

The UserSystem package includes a wrapper (or “jacket”) function for each of the system calls. Here are the names of the wrapper functions. There is a one-to-one correspondence between the system calls and the wrapper functions.

As an example, here is the code for the wrapper function for “Read.” It simply invokes DoSyscall and returns whatever DoSyscall returns.

function Sys_Read (fileDesc: int, buffer: ptr to char, sizeInBytes: int) returns int return DoSyscall (SYSCALL_READ, fileDesc, buffer asInteger, sizeInBytes,

endFunction Here is the function prototype for DoSyscall : external DoSyscall (funCode, arg1, arg2, arg3, arg4: int) returns int The DoSyscall routine is set up to deal with up to 4 arguments. Since the Read syscall only needs 3 arguments, the wrapper function must supply an extra zero for the fourth argument. DoSyscall treats all of its arguments as untyped words (i.e., as int ), so the wrapper functions must coerce the types of the arguments if they are not int. Whatever DoSyscall returns, the wrapper function will return. DoSyscall is in UserRuntime.s , which will be linked with all user programs. The code is given next. It moves each of the 4 arguments into registers r1, r2, r3, and r4. It then moves the function code into register r5 and executes the syscall instruction. It assumes the kernel will place the result (if any) in r1, so after the syscall instruction, it moves the return value from r1 to the stack, so that the wrapper function can retrieve it. DoSyscall: load [r15+8],r1! Move arg1 into r load [r15+12],r2! Move arg2 into r load [r15+16],r3! Move arg3 into r load [r15+20],r4! Move arg4 into r load [r15+4],r5! Move funcCode into r syscall r5! Do the syscall store r1,[r15+4]! Move result from r1 onto stack ret! Return Some of the kernel routines require no arguments and/or return no result. As an example, consider the wrapper function for Yield. The compiler knows that DoSyscall returns a result, so it insists that we do something with this value. The wrapper function simply moves it into a variable and ignores it. function Sys_Yield () var ignore: int ignore = DoSyscall (SYSCALL_YIELD, 0, 0, 0, 0) endFunction

asm UserRuntime.s comp UserSystem -unsafe asm UserSystem.s comp MyProgram -unsafe asm MyProgram.s lddd UserRuntime.o UserSystem.o MyProgram.o -o MyProgram Note that there is no connection with the kernel. The user-level programs are compiled and linked independently. All communication with the kernel will be through the syscall interface, via the wrapper functions. This is exactly the way Unix works. For user-level programs, library functions and wrapper functions are brought into the “a.out” file at link-time, as needed. This explains why a seemingly small “C” program can produce a rather large “a.out” executable. One small use of printf in a program will pull in, at link-time, all the output formatting and buffering routines you can possibly imagine. When an OS wants to execute a user-level program, it will go to a disk file to find the executable. Then it will read that executable into memory and start up the new process. In order to execute MyProgram, we will need to introduce the BLITZ “disk.” The disk is simulated with a Unix file called “DISK.” After the user-level program is compiled, it must be placed on the BLITZ disk with the following Unix commands: diskUtil -i diskUtil -a MyProgram MyProgram The first command creates an empty file system on the disk. The second command copies a file from the Unix file system to the BLITZ disk. It creates a directory entry and moves the data to the proper place on the simulated BLITZ disk. Commands to initialize the BLITZ disk have also been added to the makefile. Once the kernel is running, it will read the file from the simulated BLITZ disk and copy it into memory.

The Syscall Interface

In our OS, each process will have exactly one thread. A process may also have several open files and can do I/O via the Read and Write syscalls. The I/O will go to the BLITZ disk. For now, there is no serial (i.e., terminal) device. Next we describe each syscall in more detail. function Sys_Exit (returnStatus: int) This function causes the current process and its thread to terminate. The returnStatus will be saved so that it can be passed to a Sys_Join executed by the parent process. This function never returns.

function Sys_Shutdown () This function will cause an immediate shutdown of the kernel. It will not return.

If the input is coming from the serial device (the terminal), this function will wait for at least one character to be typed before returning, and then will return as many characters as have been typed and buffered since the previous call to this function. This function will return the number of characters moved. If there are errors, it will return -1. function Sys_Write (fileDesc: int, buffer: ptr to char, sizeInBytes: int) returns int This function is passed the fileDescriptor of a file (which is assumed to have been successfully opened), a pointer to an area of memory, and a count of the number of bytes to transfer. This function writes that many bytes from the memory to the current position in the file. If the end of the file is reached, the file’s size will be increased. The current file position will be advanced by the number of bytes transferred, so that future writes will follow the data transferred in this invocation. The output may also be directed to the serial output, i.e., to the terminal. This function will return the number of characters moved. If there are errors, it will return -1. function Sys_Seek (fileDesc: int, newCurrentPosition: int) returns int This function is passed the fileDescriptor of a file (which is assumed to have been successfully opened), and a new current position. This function sets the current position in the file to the given value and returns the new current position. Setting the current position to zero causes the next read or write to refer to the very first byte in the file. If the file size is N bytes, setting the position to N will cause the next write to append data to the end of the file. The current position is always between 0 and N, where N is the file's size in bytes. If -1 is supplied as the new current position, the current position will be set to N (the file size in bytes) and N will be returned. It is an error to supply a newCurrentPosition that is less than -1 or greater than N. If so, -1 will be returned. function Sys_Close (fileDesc: int) This function is passed the fileDescriptor of a file, which is assumed to be open. It closes the file, which includes writing out any data buffered by the kernel.

Asynchronous Interrupts

From time-to-time an asynchronous interrupt will occur. Consider a DiskInterrupt as an example. When this happens, an assembly routine called DiskInterruptHandler in Runtime.s will be jumped to. It begins by saving the system registers (after all, a Disk Interrupt might occur while a kernel routine is executing and we’ll need to return to it). Then DiskInterruptHandler performs an “upcall” to the function named DiskInterruptHandler in Kernel.c. Perhaps it is a little confusing to have an assembly routine and a KPL routine with the same name, but, oh well... The high-level DiskInterruptHandler routine simply signals a semaphore and returns to the assembly DiskInterruptHandler routine, which restores the system registers and returns to whatever code was interrupted. All the time while these routines are running, interrupts are disabled and no other interrupts can occur. Also note that the interrupt handler uses space on the system stack of whichever thread was interrupted. It might be that some unsuspecting user-level code was running. Although the interrupt handler will use the system stack of that thread, the thread will be none-the-wiser. While the interrupt handler is running, it is running as part of some more-or-less randomly selected thread. The interrupt handler is not a thread on its own.

Error Exception Handling

When a runtime error is detected by the CPU, the CPU performs exception processing, which is similar to the way it processes an interrupt. Here are the sorts of runtime errors that can occur in the BLITZ architecture: Illegal Instruction Arithmetic Exception Address Exception Page Invalid Exception Page Read-only Exception Privileged Instruction Alignment Exception As an example, consider what happens when an Alignment Exception occurs. (The others are handled the same way.) The CPU will consult the interrupt vector in low memory (see Runtime.s ) and will jump to an assembly language routine called AlignmentExceptionHandler. The assembly routine first checks to see if the interrupted code was executing in system mode or not. If it was in system mode, then the assumption is that there is a bug in the kernel, so the assembly routine prints a message and halts execution. However, if the CPU was in user mode, the assumption is that the user-level program has a bug. The OS will need to handle that bug without itself stopping. So the assembly AlignmentExceptionHandler routine makes an upcall to a KPL routine with the same name.

Syscalls

When a user-level thread executes a syscall instruction, the assembly routine SyscallTrapHandler in Runtime.s will be invoked. The assembly routine will then call a KPL routine with the same name. The assembly routine does not need to save registers because the interrupted code was executing in user mode and the handler will be executed in system mode. Recall that just before the syscall, the DoSyscall routine placed the arguments in the (user) registers r1 , r2 , r3 , and r4 , with an integer indicating which kernel function is wanted in register r5. The SyscallTrapHandler assembly routine takes the values from the user registers. Since it is running in system mode, it must use a special instruction called “readu” to get values from the user registers. It pushes them on to the system stack so that the high-level routine can access them. Then it calls the high-level SyscallTrapHandler routine. When the high-level routine returns, it takes the returned value from the stack and moves it into user register r1 , using an instruction called “writeu,” and then executes a “reti” instruction to return to the interrupted user-level process. Execution will resume back in DoSyscall directly after the “syscall” instruction. The high-level routine called SyscallTrapHandler simply takes a look at the function code and calls the appropriate routine to finish the work. For every kind of syscall, there is a corresponding “handler routine” in the OS. System call Handler function in the kernel Exit Handle_Sys_Exit Shutdown Handle_Sys_Shutdown Yield Handle_Sys_Yield Fork Handle_Sys_Fork Join Handle_Sys_Join Exec Handle_Sys_Exec Create Handle_Sys_Create Open Handle_Sys_Open Read Handle_Sys_Read Write Handle_Sys_Write Seek Handle_Sys_Seek Close Handle_Sys_Close It is these routines that you will need to implement, in this and other projects. Note that interrupts will be disabled when the SyscallTrapHandler routine begins. The first thing the high-level routine does is set the global variable currentInterruptStatus to DISABLED so that it is accurate. In fact, all the interrupt and exception handlers begin by setting currentInterruptStatus to DISABLED for this reason. Also note that after the handler routines return to the interrupted routine, interrupts will be re-enabled. Why? Because the Status Register in the CPU will be restored as part of the operation of the reti instruction, restoring the interrupt (and paging and system mode) status bits to what they were when the interrupt occurred. (Note that we do not bother to change currentInterruptStatus to ENABLED before returning to user-level code, because any re-entry to the kernel code must be through

SyscallTrapHandler , or an interrupt or exception handler, and each of these begins by setting currentInterruptStatus .) Implementing the Shutdown syscall is straightforward. The handler should call FatalError with the following message: Syscall 'Shutdown' was invoked by a user thread

The BLITZ Disk

The BLITZ computer includes a disk which is emulated using a file called DISK on the host computer. In other words, a write to the BLITZ disk will cause data to be written to a Unix file and a read from the BLITZ disk will cause a read from the Unix file. The emulator will simulate the delays involved in reading, by taking account of the current (simulated) disk head position. When the I/O is complete— that is the simulated time when the emulator has calculated the disk I/O will have completed—the emulator causes a DiskInterrupt to occur. To interface with the BLITZ disk, we have supplied a class called DiskDriver , which makes it unnecessary for you to write the code that actually reads and writes disk sectors. You can just use the code in the class DiskDriver. There is only one DiskDriver object; it is created and initialized at startup time. class DiskDriver superclass Object fields ... semToSignalOnCompletion: ptr to Semaphore semUsedInSynchMethods: Semaphore diskBusy: Mutex methods Init () SynchReadSector (sectorAddr, numberOfSectors, memoryAddr: int) StartReadSector (sectorAddr, numberOfSectors, memoryAddr: int, whoCares: ptr to Semaphore) SynchWriteSector (sectorAddr, numberOfSectors, memoryAddr: int) StartWriteSector (sectorAddr, numberOfSectors, memoryAddr: int, whoCares: ptr to Semaphore) endClass This class provides a way to read and write sectors synchronously as well as a way to read and write sectors asynchronously. To perform a disk operation without blocking the calling thread, you can call StartReadSector or StartWriteSector. These methods are passed the number of the sector on the disk at which to begin the transfer, the number of sectors to transfer and the location in memory to transfer the data to or from. These methods are also passed a pointer to a Semaphore; upon completion of the operation (possibly in error!) this semaphore will be signaled. This is exactly the semaphore that is signaled whenever a