User programs invoke system calls by executing the MIPS ``syscall'' instruction, which generates a hardware trap into the Nachos kernel. The Nachos/MIPS simulator implements traps by invoking the Routine RaiseException(), passing it a arguments indicating the exact cause of the trap. RaiseException, in turn, calls ExceptionHandler to take care of the specific problem. ExceptionHandler is passed a single argument indicating the precise cause of the trap.
The ``syscall'' instruction indicates a system call is requested, but doesn't indicate which system call to perform. By convention, user programs place the code indicating the particular system call desired in register r2 before executing the ``syscall'' instruction. Additional arguments to the system call (when appropriate) can be found in registers r4-r7, following the standard C procedure call linkage conventions. Function (and system call) return values are expected to be in register r2 on return.
Warning: When accessing user memory from within the exception handler (or within Nachos in general), user-level addresses cannot be referenced directly. Recall that user-level processes execute in their own private address spaces, which the kernel cannot reference directly. Attempts to dereference pointers passed as arguments to system calls will likely lead to problems (e.g., segmentation faults) if referenced directly. Use ReadMem and WriteMem to dereference pointers passed as arguments to system calls. Consult Section 2.4 for more details.