200 likes | 350 Views
User-Level Processes. Needed to test the system call you implement The “Noff” format file required Look at the Makefile in test MIPS “syscall” instruction invokes the routine RaiseException. How does it work? . How does it work? (cont.). Related Nachos Codes. test/start.s
E N D
User-Level Processes • Needed to test the system call you implement • The “Noff” format file required • Look at the Makefile in test • MIPS “syscall” instruction invokes the routine RaiseException
Related Nachos Codes • test/start.s • Startup assembly code for every user program of Nachos. • syscall.h • Definitions of the system call prototypes • exception.cc • The handler for system calls and other exceptions is here.
Directories of Nachos • Main directories • filesys • machine • network • threads • userprog • Test programs • test
Related Files • Major • test/start.s • The code that starts the program execution • userprog/syscall.h • Defines the system call constants • userprog/execption.cc • The system call handler • Minor • machine/machine.h • Register definitions • test/halt.c • Test user program • filesys/filesys.h • File system definitions • filesys/filesys.cc • Stub implementation • filesys/openfile.h • filesys/openfile.cc
Some Public Variables for the Kernel • register: an array of 40 registers • Declared in machine/machine.h • mainMemory: memory is byte-addressable and organized into 128-byte pages • Declared in machine/machine.h
Starting of a User Program • test/start.s • Define what is needed for a user program. • starting point • system call • startup assembly code of every user program of Nachos • Initialize and run a C program by jumping to location 0.
Exception Types • Execution errors • System calls • …
The Progress of a System Call in a User Program • The user program is responsible for • storing of the system codes in Register 2 • storing of the arguments in Register 4 ,5, 6, and 7. • ExceptionHandler fetches the number for the system call in Register 2. • Execute the corresponding codes for the system call. • Increase the PC. • See the register definitions in machine.h
Declaring of System Call • userprog/syscall.h • system call codes • Ex: #define SC_Exit 1 • Interface for Nachos system calls • Ex: void Exit(int status);
The Entry Point into the Nachos Kernel • ExceptionHandler • userprog/execption.cc • Entry point into the Nachos kernel from user programs. • syscall • exceptions
The Entry Point into the Nachos Kernel (cont.) • The code for the system call is placed in Register 2. • arg • arg1 is in Register 4. • arg2 is in Register 5. • arg3 is in Register 6. • arg4 is inRegister 7. • The return value is in Register 2. • Note • If you are handling a system call, don't forget to increment the pc before return to the user program.
Getting or Setting of the Data in a Register • The function body is in machine/machine.cc. • Kernel machine ReadRegister(x) • x is the register number. • The return type is the integer type. • Kernel machine WriteRegister(x,y) • x is the register number. • y is the value.
Example: system call add(arg1,arg2) • Retrieve the two arguments by • op1=(int)kernelmachineReadRegister(4) • op2=(int)kernelmachineReadRegister(5) • Set the return value by • KernelmachineWriteRegister(2, (int)result)
Example: system call add(arg1,arg2) (cont.) • Modify the return point by • Setting the previous PC (for debugging) • kernel machine WriteRegister( PrevPCReg, kernel machine ReadRegister(PCReg)); • Setting the PC to the next instruction (all instructions are 4 byte wide) • kernel machine WriteRegister( PCReg, kernel machine ReadRegister(PCReg) + 4); • Setting the next PC for a branch execution • kernel->machine->WriteRegister( NextPCReg, kernel->machine->ReadRegister(PCReg)+4);
How If an Argument is an Char Array? • Retrieve the logical address with • kernel->machine->ReadRegister(?) • Retrieve the data with • kernel->machine->ReadMem(int x, int y, int *z) • It is declared in ~/code/machine/machine.h and implemented in ~/code/machine/translate.cc. • x is the virtual address to read from. • y is the number of bytes to read (1, 2, or 4). • z is the place to write the result.
kernel->machine->ReadMem • Translate a virtual address into a physical address by • Translate(int virtAddr, int* physAddr, int size, bool writing) • virtAddr: the virtual address to translate • physAddr: the place to store the physical address • size: the amount of memory being read or written • writing: if TRUE, check the "read-only" bit in the TLB
Run Your Program • Normal mode: • ”./nachos –x ../test/XXX” • Debugger mode: • ”./nachos –d ../test/XXX”
Example Exit() void ExceptionHandler(ExceptionType which) { int type = machine->ReadRegister(2); if ((which == SyscallException) && (type == SC_Halt)) { DEBUG('a', "Shutdown, initiated by user program.\n"); interrupt->Halt(); }if ((which == SyscallException) && (type == SC_Exit)) { /* newly added system call EXIT */ //get the first argument int a4 = machine->ReadRegister(4); printf("\n\n\tNEW SYSCALL: you are in EXIT.\n\n\tThe return value you passed is --> %d\n\n\t The current thread will terminate ...\n\n\n", a4); currentThread->Finish(); } else { printf("Unexpected user mode exception %d %d\n", which, type); ASSERT(FALSE); } }