Salsa - System Calls and Exception Handling Topics Nachos How to compile and run user programs in Nachos


[ Source code in code/userprog | Lab| Quiz ]

System Calls

What are they?
System calls are an interface between the operating system and its application programs. The actual mechanics of issuing a system call are highly machine dependent and are often expressed in assembly language. A procedure library is often provided to make it possible to make system calls from C programs. The system calls vary from operating system to operating system even though the underlying concepts tend to be similar.

As an example, consider the open system call in UNIX:


              open(char *path, int flags, int mode)

This system call opens a specified file and returns a descriptor for that file. path is the address of a string of characters representing a path name which identifies the file to be opened. flags define how the file is to be opened. It can be set to any of several values, e.g. O_RDONLY (for reading only), O_WRONLY (for writing only), O_RDWR (for reading and writing), O_CREAT (create file if it does not exist). mode is only used with the O_CREAT flag and specifies the mode with which the file is to be created.

Some common system calls in UNIX are fork(), read(), write(), execve(), wait() and exit().

Why do we need them?
Increased sharing of system resources in modern operating systems has given rise to various problems. A bug in a program can adversely affect all the other programs running on the system. An erroneous program can modify the program or data of another program or even modify the operating system program itself. For the proper operation of any system, it is necessary to protect the operating system and other programs from any malfunctioning program. An approach used most often is having two (or more) modes of execution - user mode and privileged mode .

At system boot time, the hardware starts in privileged mode and loads the operating system. User processes are started in user mode. Whenever a trap or interrupt occurs, the hardware switches from user mode to privileged mode. The operating system is protected from erroneous programs by designating some of the machine instructions as privileged instructions, those that can be executed only in privileged mode. I/O instructions and instructions to modify the memory-management registers or the timer are privileged instructions. The halt instruction is also privileged.

Since user programs cannot perform I/O but need to, they have to ask the operating system to do it for them. System calls are the way the user programs request the operating system to perform some instructions for them.

Implementation details

A system call usually takes the form of a trap to a specific location in the interrupt vector. This trap can be executed by a generic trap instruction or by a syscall instruction. A system call is treated as a software interrupt by the hardware. Control passes through an interrupt vector to a service routine in the operating system. The kernel examines the interrupting instruction to determine the type of system call, verifies the correctness of the parameters and executes the request, returning control to the instruction following the system call. The parameters to the system call may be passed in registers, on the stack, or in memory with pointers to memory locations passed in registers.

Exception Handlers

Disruptions to normal execution of a program may come in two ways - interrupts and exceptions. Exceptions are caused by the occurrence of unusual conditions during a process' execution and often are the result of programming errors, such as division by zero or integer overflow. The exception causes a trap to the operating system which transfers control through the interrupt vector to the operating system. Thus, servicing of an exception works just like an interrupt.

Upon a program error, the operating system may abnormally terminate the program. When abnormally terminating a program, an error message is displayed and a memory dump of the program is saved in a core file. The user can then examine this memory dump and hopefully fix the problem.

System calls and Exception Handling in Nachos

Nachos provides a skeletal interface for system calls and exception handlers. Below is a list of Nachos system calls and their UNIX equivalent. The actual implementation of system calls and exceptions is part of a lab. To see how exception handling works in Nachos, click here.

List of Nachos system calls and UNIX equivalent
Nachos System Call UNIX System Call Nachos Functionality
Halt() None Stop Nachos.
Exit(int Status) exit() Quit the user program making the call.
Exec(char *name) execv(),execl(),execvp() etc. Run the executable identified by "name".
Join(SpaceId id) wait(), waitpid(), wait3() etc. Wait for "id". Return only when "id" is done.
Create(char *name) creat() Create a nachos file with name "name".
Open(char *name) open() Open the nachos file with name "name".
Write(char *buffer, int size, OpenFileId id) write(), writev() Write "size" bytes from "buffer" to the open file.
Read(char *buffer, int size, OpenFilesId id) read(), readv() Read "size" bytes from open file into "buffer".
Close(OpenFileId id) close() Close the file.
Fork(void (*func)()) fork() Fork a thread to run a procedure "func".
Yield(void (*func)()) None Yield the CPU to another runnable thread.


Note that in UNIX, a halt command is provided which halts the processor. This command, however, can be executed only by superusers - users and user programs cannot execute it.

Source code in code/userprog
Lab
Quiz

Back to Top

Last modified on Thursday, 29-May-97 17:14:41 EDT