Outline TTIT61: Process Programming and Operating Systems Alexandru Andrei alean@ida.liu.se phone: 282698, room: B 3D:439 Lab 2 : System Calls Introduction to – and – making user programs in Nachos User memory vs Kernel memory in Nachos File & Console related syscalls Synchronizing kernel functions and data structures Lab 3 : Memory Management & System Calls 3.1 Memory management with linear page tables ¾Multiprogramming - multiple user processes ¾Page tables ¾Process handling 3.2 Memory management with software TLB alean@ida TTIT61 Lab lesson -2- System Calls Four Components of a Computer System #include <stdio.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> /* System calls are written in bold italic. Type “man 2 sys_call_name” for info in Linux or “man –s 2 sys_call_name” in Solaris */ int fd, n; char buf[1024]; fd = open(“datafile.txt”, O_RDWR); n = read(fd, buf, 1024); printf(“Have read %d bytes: %s\n”, n, buf); lseek(fd, 0, SEEK_SET); write(fd, “Some other text”, strlen(“Some other text”) + 1); write(fd, buf, n); close(fd); alean@ida -3- TTIT61 Lab lesson alean@ida -4- TTIT61 Lab lesson User Programs System Call Execution Located in the Nachos directory: code/test/ Compiled with cross-compiler to MIPS machine code (see code/test/Makefile) MIPS is a RISC architecture, with delayed loads Cross compile – compile in one machine for a different target machine Emulation – runs on MIPS simulator Class Machine emulates the MIPS processor alean@ida -5- TTIT61 Lab lesson alean@ida -6- TTIT61 Lab lesson 1 User Program - Example User Programs (cont.) Simple C programs (any C program that doesn’t use library functions like printf) In nachos-3.4/code/test For a new test program, add it to the Makefile from nachos-3.4/code/test alean@ida TTIT61 Lab lesson -7- Test Program Makefile In code/testprogram/Makefile Use copy paste to add another testprogram #include ”syscall.h” void main(void) { int file_id; char a[10]; for (i=0;i<=8;i++) a[i]=’a’; a[9]=’\0’; Create(a); file_id = Open(a); Write(”some text”, 10, file_id); Close(file_id); Halt(); } alean@ida The User Space Definition of the Syscall Functions .globl Create .ent addiu $2,$0,SC_Create syscall j $31 .end Create start.o: start.s ../userprog/syscall.h $(CPP) $(CPPFLAGS) start.s > strt.s $(AS) $(ASFLAGS) -o start.o strt.s rm strt.s TAB (not spaces) alean@ida Create Create: all: halt shell matmult sort halt.o: halt.c $(CC) $(CFLAGS) -c halt.c halt: halt.o start.o $(LD) $(LDFLAGS) start.o halt.o -o halt.coff ../bin/coff2noff halt.coff halt TTIT61 Lab lesson -9- TTIT61 Lab lesson -8- code/test/start.s .globl Open .ent Open Open: addiu $2,$0,SC_Open syscall j $31 .end Open alean@ida TTIT61 Lab lesson -10- Program Execution Under the Hood The MIPS processor fetches instructions from memory and executes them one by one void Machine::Run() { // code/machine/mipssim.cc Instruction *instr = new Instruction; How does Nachos execute the binary code How does Nachos execute system calls interrupt->setStatus(UserMode); for (;;) { OneInstruction(instr); interrupt->OneTick(); } } alean@ida -11- TTIT61 Lab lesson alean@ida -12- TTIT61 Lab lesson 2 OneInstruction() (cont.) OneInstruction() void Machine::OneInstruction(Instruction *instr){ int raw; int nextLoadReg = 0; int nextLoadValue = 0; if (!machine->ReadMem(registers[PCReg],4,&raw)) return; // exception occurred instr->value = raw; instr->Decode(); pcAfter = registers[NextPCReg] + 4; switch (instr->opCode) { Ænextslide alean@ida switch case case case TTIT61 Lab lesson -13- (instr->opCode) { OP_ADD: … OP_DIV: … OP_SYSCALL: RaiseException(SyscallException, 0); return; alean@ida -14- TTIT61 Lab lesson Exception Handling: System Calls RaiseException() The processor has detected an exception and passes the control to the operating system for exception handling void Machine::RaiseException(//code/machine/machine.cc ExceptionType which, int badVAddr){ // code/userprog/exception.cc void ExceptionHandler(ExceptionType which) { int type = machine->ReadRegister(2); if ((which == SyscallException)&&(type == SC_Halt)){ DEBUG('m', "Exception: %s\n", exceptionNames[which]); registers[BadVAddrReg] = badVAddr; DelayedLoad(0, 0); // finish anything in progress DEBUG('a', "Shutdown, initiated by user program.\n"); interrupt->Halt(); } else interrupt->setStatus(SystemMode); ExceptionHandler(which); //interrupts are enabled at this point interrupt->setStatus(UserMode); printf("Unexpected user mode exception %d %d\n",which,type); } } alean@ida -15- TTIT61 Lab lesson alean@ida Exception Handling: System Calls void ExceptionHandler(ExceptionType which) { int type = machine->ReadRegister(2); if ((which == SyscallException)) { switch (type) { case SC_Halt: { { break; break; break; -17- TTIT61 Lab lesson Exception Handling: System Calls The list of the system call ids (the value from register 2) is in the file code/userprog/syscall.h DEBUG('a', "Shutdown, initiated by user program.\n"); interrupt->Halt(); } case SC_Create: //your code; } case SC_Open: { //your code; } case SC_Read: { //your code; } …. } //end of switch }//end if } alean@ida -16- TTIT61 Lab lesson For example: #define SC_Halt #define SC_Exit #define SC_Exec #define SC_Join #define SC_Create #define SC_Open #define SC_Read #define SC_Write #define SC_Length #define SC_Close alean@ida 0 1 2 3 4 5 6 7 8 9 -18- TTIT61 Lab lesson 3 Exception Handling: System Calls ”syscall” machine code instruction The system call id is stored in register 2 The arguments of the syscall are stored in the registers, starting with register 4 arg1 -- r4, arg2 -- r5, etc. System calls return values in reg. r2 see …/code/test/start.s ExceptionHandler see …/code/userprog/exception.cc ¾SC_Halt already implemented The list of the system calls is in the file code/userprog/syscall.h void Write(char *buffer, int size, OpenFileId id); int Read(char *buffer, int size, OpenFileId id); void Close(int fileid); OpenFileId Open(char *filename); void Exit(int status); SpaceId Exec(char *name); int Join(SpaceId id); void Halt(); alean@ida -19- System Calls and Exception Handling TTIT61 Lab lesson alean@ida Exception Handling: System Calls -20- TTIT61 Lab lesson Two Types of Arguments void ExceptionHandler(ExceptionType which) { … switch (type) { case SC_Create: //read from register 4 the argument //use machine->ReadRegister(4); //create the file //does not return any value //if it would return, it should write //the return value in register 2 break; } … alean@ida -21- TTIT61 Lab lesson Arguments passed by value ¾void Exit(int status); ¾case SC_EXIT: int status=machine->ReadRegister(4); Arguments passed by address (pointers) ¾void Create(char *filename); ¾case SC_EXIT: int address_of_pointer=machine->ReadRegister(4); alean@ida -22- User Programs&User Memory Process1: char buffer[10] Create(buffer); Process2: char buffer[10] Open(buffer); 0000 0004 0008 0000 FFFF Kernel Process 1 AAA0 0000 AAA4 FFFF Process 2 FFFF Memory TTIT61 Lab lesson Important ! User space vs. Kernel Space User address vs. Kernel Address Pointer arguments passed to system calls must be translated to/from user space Example: Create(char* filename) -> UserToKernel Read(char* buf, int size, OpenFileId id) -> KernelToUser Virtual memory vs. Phisycal memory alean@ida -23- TTIT61 Lab lesson alean@ida -24- TTIT61 Lab lesson 4 UserToKernel void UserToKernel(void) { //buffer allocated in kernel space; we copy here //the content of buffer from the user program char* kern_buffer = new char[100]; //address int address = machine->ReadRegister(4); i=0; for (;;) { int data; machine->ReadMem(address + i, 1, &data); filename[i] = data; if (filename[i]==’\0’) break; i++; } } alean@ida -25- machine->ReadMem machine->ReadMem(address ,n_of_bytes, &data) Translates internally the virtual address address to a physical address, reads n_of_bytes from that address and stores them in the variable data machine->WriteMem also available in Nachos TTIT61 Lab lesson alean@ida -26- OpenFile FileSystem class FileSystem { TTIT61 Lab lesson // code/filesys/filesys.h public: class OpenFile { FileSystem(bool format) {} // code/filesys/openfile.h public: OpenFile(int f) { file = f; currentOffset = 0; } bool Create(char *name, int initialSize) { ~OpenFile() { Close(file); } int fileDescriptor = OpenForWrite(name); if (fileDescriptor == -1) return FALSE; close(fileDescriptor); return TRUE; int Read(char *into, int numBytes) { … } } OpenFile* Open(char *name) { int Write(char *from, int numBytes) { int fileDescriptor = OpenForReadWrite(name, FALSE); if (fileDescriptor == -1) return NULL; return new OpenFile(fileDescriptor); … } …… } } bool Remove(char *name) { return Unlink(name) == 0; } }; alean@ida -27- TTIT61 Lab lesson alean@ida -28- Exception Handling: System Calls TTIT61 Lab lesson Assignment 2 void ExceptionHandler(ExceptionType which) { … switch (type) { case SC_Create: char k_filename[20]; UserToKernel(k_filename); fileSystem->Create(k_filename); break; } … alean@ida -29- TTIT61 Lab lesson Implement and test the system calls related to file & console operations alean@ida -30- TTIT61 Lab lesson 5 Why OpenFileID ? System Calls for File Operations File operations (lab 2) void Create(char* filename) ¾Create an empty file OpenFileId Open(char *filename) ¾open file for read/write int Read(char* buf, int size, OpenFileId id) ¾read from an open file void Write(char* buf, int size, OpenFileId id) ¾write to an open file void Close(int fileid) ¾Close an open file See code/userprog/syscall.h for the list alean@ida -31- #include ”syscall.h” void main(void) { int file_id; char a[10]; for (i=0;i<=8;i++) a[i]=’a’; a[9]=’\0’; Create(a); //a is the name of the file file_id = Open(a); //a is the name of the file Write(”some text”, 10, file_id); //file_id is the OpenFileId Close(file_id); //file_id is the OpenFileId Halt(); } TTIT61 Lab lesson alean@ida File Related System Calls OpenFileId (an integer) Global unique id for each open file ConsoleInput (o_id==0) and ConsoleOutput (o_id==1) The operating system keeps a list of open files Read and Write on an open file or on the Console must be choosen based on the OpenFileId ¾if (o_id>=2) then file; else console; The actual file operations are handled by the FileSystem and OpenFile alean@ida -33- TTIT61 Lab lesson TTIT61 Lab lesson -32- List of Open Files Implement in the kernel a list of files that are open For each open file, store in the list: OpenFileId (this could be the index of your list) OpenFile pointer Pointer to the thread that opened the file (currentThread) ... Open and Close system calls append and remove files from this list alean@ida TTIT61 Lab lesson -34- Console Console Internals Console class constructor: The Console class handles reading/writing from/to the console at the char level One object with type Console must be used throughout Nachos void PutChar(char ch) char GetChar() TODO: introduce the possibility to read/write strings (char* or char[]) ie. synchronize the console operations alean@ida -35- TTIT61 Lab lesson Console( char *readFile, char *writeFile, VoidFunctionPtr readAvail, VoidFunctionPtr writeDone, int callArg ); alean@ida -36- TTIT61 Lab lesson 6 TODO: System Calls for File Operations (Lab2) Console Internals void ReadAvail(int arg) {…} void WriteDone(int arg) {…} console = new Console(NULL,NULL,ReadAvail,WriteDone,0); An example of a console implementation is given in: code/userprog/progtest.cc Hint: use two semaphores to signal the availability of an input character (for Read) and the finishing of a char writing (for Write) alean@ida -37- File operations (lab 2) void Create(char* filename) Create an empty file OpenFileId Open(char *filename) open file for read/write int Read(char* buf, int size, OpenFileId id) void Write(char* buf, int size, OpenFileId id) void Close(int fileid) See …/code/userprog/syscall.h for details Compile and run nachos always in code/userprog directory Test programs implemented and compiled in code/test directory TTIT61 Lab lesson alean@ida Nachos Syscalls Info 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 alean@ida -39- TTIT61 Lab lesson alean@ida -41- TTIT61 Lab lesson TTIT61 Lab lesson Nachos Syscalls Info (cont) Source files: Working dir. is ../code/userprog Central files for assignment ¾”Exception.cc” contains ExceptionHandler function ¾syscall.h - definitions and constants for system calls Files necessary for understanding how Nachos works ¾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 ¾/machine/machine.* - how MIPS emulator works alean@ida -40- TTIT61 Lab lesson Lab 3 - Memory Management Nachos Syscalls Info (cont.) In code/userprog: addrspace.h, addrspace.cc ¾data structures to keep track of executing user programs (address spaces) In code/machine: ¾translate.cc ¾data structures for managing translation from virtual page # -> physical page # ¾used for managing memory on behalf of user programs ¾mipssim.cc (ReadMem, Writemem, Translate, and OneInstruction) -38- 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 alean@ida -42- TTIT61 Lab lesson 7 Lab 3 - Memory Management (cont.) 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) alean@ida TTIT61 Lab lesson -43- User Programs Reside in user address space Communication with the kernel through system calls (open file, start a new process, etc.) Only one user program in the lab #2 alean@ida User Level Processes When executing a user program, Nachos creates an address space Copies the content of the instructions’ and initialized variable segments into the address space. Uninitialized variable section are not read from the file (as it contains all 0’s). alean@ida TTIT61 Lab lesson -45- TTIT61 Lab lesson -44- Process Creation Nachos process are formed by: Creating an address space Allocating physical memory for the AddrSpace 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. progtest.cc to see an example alean@ida TTIT61 Lab lesson -46- Lab 2 - User Programs Memory Organization Memory Addresses in Nachos Address space of a Nachos process Pages •Page number •Same size •Allocation unit for a process machine machine->mainMemory Adress space of user program. Other machine variables thread 1 thread 2 thread variables thread variables (”Physical” memory alean@ida of the MIPS machine) -47- TTIT61 Lab lesson Machine.h contains the details regarding the memory size, number of pages, page size, etc. alean@ida -48- TTIT61 Lab lesson 8 Lab 3 - Page tables in lab 2 Lab 3 - Page tables in lab 3 Running process Running process Machine object Physical Memory 0 2 Physical Memory 1 Machine object n-1 1 pageTable pageTable 2 0 ... Process1 ... 3 n Process1 Only one process running Every process gets the same physical pages alean@ida -49- TTIT61 Lab lesson More than one process running bitMap can be used for allocation alean@ida Process2 -50- Lab 3 - Address spaces Address spaces (AddrSpace object) AddrSpace constructor allocates memory pages In lab 2 all memory is assigned to one address space In lab 3 several address spaces will exist Use a BitMap object to keep track of free pages ¾BitMap(int nitems) // total number of items ¾Mark // mark (allocate) a position ¾Clear // clear (free) a position ¾Test // check if a position is marked ¾Find // find a free position and mark it alean@ida -51- TTIT61 Lab lesson Lab 3 - Address spaces AddrSpace constructor Takes an executable (filename) Allocates memory, sets up page table ¾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 alean@ida -52- Noff Executable Format Noff: Nachos Object File Format Compare with coff(Unix), com, exe (DOS) Noff consists of four parts ¾Noff header (information concerning segments below) ¾Code segment – program residence ¾Initialized variables segment ¾Unitialized variables segment alean@ida -53- TTIT61 Lab lesson TTIT61 Lab lesson TTIT61 Lab lesson 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 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 alean@ida -54- TTIT61 Lab lesson 9 Address Spaces Segments: code ¾the executable machine code initData ¾initialized data (constant strings etc) uninitData ¾Declared but unassigned data (empty arrays etc). ¾Not stored in the file. alean@ida TTIT61 Lab lesson -55- Memory Management TODO: Allocate/Deallocate physical pages (addrspace) Setting translation tables Implement Exec, Exit, Join Tests: Simultaneously running user programs Using pre-emptive switches between threads alean@ida -57- TTIT61 Lab lesson TODO: Lab 3 System Calls for Exit & Join int Join(SpaceId id) exits the calling user process returns ”status” to the parent process -59- The Segment object: virtualAddr ¾start address of segment in the address space inFileAddr ¾offset of segment in the file size ¾size of the segment alean@ida -56- TTIT61 Lab lesson TODO: Syscalls for Mem. Management. (Lab 3) 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) alean@ida -58- TTIT61 Lab lesson Lab 3: System Calls for Memory Management void Fork(void (* func)()) --optional Start a new user level thread in the same address space Execute a function func in that thread void Yield() --optional Switch to another thread, in this address space or in another (i.e. another process) See …/code/userprog/syscall.h Roadmap through Nachos, section 4 Wait for the process with id id to exit Return the exit status of that process void Exit(int status) alean@ida Lab 3 - Address Space TTIT61 Lab lesson alean@ida -60- TTIT61 Lab lesson 10