Useful information Overview =Lab 3 : Memory Management & System Calls <Multiprogramming - multiple user processes <Page tables <Process handling =Read ”Road Map through Nachos” <Machine: section 2.1 – 2.4 <Examine exception handling in _machine/mipssim.cc (RaiseException) _machine.cc (Machine::RaiseException) _Userprog/exception.cc (ExceptionHandler) <System calls ”prototypes” in syscall.h <FileSystem and OpenFile: 5.2 – 5.3 <User Level Processes: Section 4 29-Sep-04 TBBD63 1 Useful information 29-Sep-04 <In code/userprog: addrspace.h, addrspace.cc `Working dir. is ../code/userprog `Central files for assignment _data structures to keep track of executing user programs (address spaces) _exception.cc – contains ExceptionHandler function _syscall.h – definitions and constants for system calls <In code/machine: _translate.cc `Files necessary for understanding how Nachos works • data structures for managing translation from virtual page # -> physical page # • used for managing memory on behalf of user programs _progtest.cc – test routines, how to load & execute user prog. _Example of running Nachos with a user program: • nachos –x <user_program_name> • E.g.: ../userprog/nachos –x ../test/halt _mipssim.cc (ReadMem, WriteMem, Translate, and OneInstruction) _/machine/machine.* - how MIPS emulator works TBBD63 3 User Program - Example /* test1.c */ #include ”syscall.h” void main(void) { Write(”test1”,5,ConsoleOutput); Exec(”test2”); Exit(1); } 29-Sep-04 /* test2.c */ #include ”syscall.h” void main(void) { Write(”test2”,5,ConsoleOutput); Exit(1); } TBBD63 2 Useful information =Source files: 29-Sep-04 TBBD63 5 29-Sep-04 TBBD63 4 To do: Lab 3 System Calls for Mem. Mgt. =Multiple processes and threads (lab 3) =SpaceId Exec(char *name) <Load a program/file from disk into a new AddrSpace <Create a new Thread for it to execute in (forks and execute a new program) <Return a unique global id for that AddrSpace object (SpaceId of the new program) 29-Sep-04 TBBD63 6 1 User Programs&User Memory System Calls (reminder) =Address Spaces in Lab.2 =”syscall” machine code instruction <arguments in machine registers _register2 (r2)-> type of system call _Additional arguments to system call in reg. r4-r7 _Function and system call return values in reg. r2 Physical memory Kernel <see …/code/test/start.s Process 1 =ExceptionHandler Process 2 <see …/code/userprog/exception.cc _SC_Halt implemented 29-Sep-04 7 TBBD63 User Programs&User Memory =Address Spaces in Lab.3 29-Sep-04 8 TBBD63 Lab 2 - User Programs =Addresses in Nachos Physical memory Address space of Nachos process Kernel machine machine->mainMemory Other machine variables Process 1 Process 2 thread 1 thread 2 thread variables thread variables Adress space of user program. (”Physical” memory of the MIPS machine) 29-Sep-04 9 TBBD63 Lab 3 - Page tables in lab 2 Running process Machine object 29-Sep-04 10 TBBD63 Lab 3 - Page tables in lab 3 Running process Physical Memory 2 1 Machine object 0 Physical Memory n-1 1 2 pageTable pageTable ... 0 Process1 3 ... n Process1 `More than one process running `bitMap can be used for allocation =Only one process running 29-Sep-04 TBBD63 11 29-Sep-04 Process2 TBBD63 12 2 Lab 3 - Memory Management =Multiprogramming `Several processes (user programs) reside in memory at the same time _machine->pageTable contains the pageTable of the current threads address space `One address space object for each process _Different page table for each process _A pageTable inside the AddrSpace object: translation of virtual address to physical address 29-Sep-04 TBBD63 13 Process Creation =Nachos process are formed by: `Creating an address space `Allocating physical memory for the addr. Space `Load content of executable into the physical memory `Initialize registers and address translation tables `Invoke machine::Run() to start executing. `Run turns on the simulated MIPS machine and enter into an infinite loop that executes one instruction at a time. 29-Sep-04 TBBD63 Lab 3 - Address spaces Lab 3 - Address spaces =Address spaces (AddrSpace object) =AddrSpace constructor `AddrSpace constructor allocates memory pages `Use a BitMap object to keep track of free pages _BitMap(int nitems) _Mark _Clear _Test _Find 29-Sep-04 // // // // // `Takes an executable (filename) `Allocates memory, sets up page table total number of items mark (allocate) a position clear (free) a position check if a position is marked find a free position and mark it TBBD63 _Amount of memory needed is stored in noff-header `Loads the executable =Noff binary format, noff header `NoffHeader object stored in the beginning of the file `Three Segment objects in the header 15 29-Sep-04 TBBD63 Noff Executable Format =Noff: Nachos Object File Format =Compare with coff(Unix), com, exe (DOS) _Noff header (information concerning segments below) _Code segment – program residence _Initialized variables segment _Unitialized variables segment TBBD63 16 Noff Format (cont) =Noff: header _information concerning segments below _Describe content of the rest of the file, giving information about programs’s instructions, initialized and uninitialized variables <Noff consists of four parts 29-Sep-04 14 `noffMagic: Noff format check (4 bytes) _Reserved number indicating that the file is in Noff format `For each of the remaining sections Nachos maintains _virtualAddr: segment’s starting address in virtual memory _inFileAddr: start of the segment in Noff file _Size: size of the segment 17 29-Sep-04 TBBD63 18 3 Address spaces =Segments: =The Segment object: `code `virtualAddr _the executable machine code _start address of segment in the address space `initData `inFileAddr _initialized data (constant strings etc) _offset of segment in the file `uninitData `size _Declared but unassigned data (empty arrays etc). _Not stored in the file. 29-Sep-04 _size of the segment 19 TBBD63 User Program - Example /*test1.c*/ #include ”syscall.h” void main(void) { SpaceID pid; Write(”test1”,5,1); pid = Exec(”test2”); Join(pid); Exit(1); } 29-Sep-04 Lab 3 - Address spaces TBBD63 20 System calls for Exit & Join =int Join(SpaceId id) /*test2.c*/ #include ”syscall.h” void main(void) { Write(”test2”,5,1); Exit(1); } TBBD63 29-Sep-04 <Wait for the process with id id to exit <Return the exit status of that process =void Exit(int status) <exits the calling user process <returns ”status” to the parent process 21 29-Sep-04 TBBD63 22 Lab 3 - Memory Management cont. Memory management =TODO: <Allocate/Deallocate physical pages (addrspace) <Setting translation tables <Implement Exec, Exit, Join =Separate OpenFile objects for each process <Different processes can open the same file <Each process has it’s own offset into file (i.e. different OpenFile object) =Tests: <Simultaneously running user programs <Using pre-emptive switches between threads 29-Sep-04 TBBD63 23 29-Sep-04 TBBD63 24 4