Nachos Project 1 Lecturer: Hao-Hua Chu TA: Chun-Po Wang (Artoo) Date: 2008/09/19 Projects of this course 1. System call implementation (10/6 due) • • 2. CPU Scheduling (10/20 due) • 3. Implement priority scheduling to replace the FCFS scheduling Multiprogramming (11/24 due) • 4. 5. Be familiar with Nachos Implement various system calls for user programs To make multiple programs coexist in memory and implement paging mechanism File Systems? (12/15 due) Disks? (1/5 due) 2 Project 1 • Implement System Calls – Print(char *msg) – PrintNum(int n) 3 Summary • Motivation & Objective • Introduction of Nachos – User-level processes – System calls – Related source files • Requirement • Submission 4 Motivation & Objective • System calls are the interfaces that user programs can ask services from kernel – Eg. We cannot access hardwares like the monitor directly, so we need some means to print text on the screen – File I/O, Process Control, etc. 5 Motivation & Objective (cont.) • Be familiar with Nachos – How to build – How to run – How to modify • For the first two questions, please read the slide “Nachos Overview” 6 Introduction of Nachos • User-level processes – How a user program is loaded and run? • System calls – How a system call is called? – How to implement one? • Related source files – Which files do we need to trace and modify? 7 Nachos startup 8 Nachos system calls 9 Exception • Transfer control from user mode to the Nachos kernel – System call – Divide by zero – Access invalid memory address – etc. 10 Implementation • We use the system call “Add()” as an example – code/test/add – Add(42,23) returns 65 • Define the system call code in code/userprog/syscall.h – #define SC_Add 43 • Declare the interface for user programs in code/userprog/syscall.h – int Add(int op1, int op2); 11 Implementation (cont.) • Implement the system call function body for user program – This function must put the corresponding code/test/start.S: system call code to the register, then run .globl Add MIPS instruction “syscall” .ent –Add When the MIPS machine in Nachos see Add: this command, it will run the addiu $2,$0,SC_Add corresponding system call syscall – So, the function body is implemented in j $31 assembly code/test/start.S MIPS .end Add 12 Implementation (cont.) • Implement the real stuff in ExceptionHandler() in code/userprog/execption.cc – “syscall” would raise an exception, and kernel use ExceptionHandler() to handle it – The arguments of the system call is in register 4~7 of the MIPS machine, and we should put the return value to register 2 – We need to increment register PC before we return • This is the only file you need to send in this project – Because the implementation in other files would be the same 13 case SC_Add: DEBUG(dbgSys, "Add " << kernel->machine->ReadRegister(4) << " + " << kernel->machine->ReadRegister(5) << "\n"); Implementation (cont.) int result; // SysAdd(a,b) returns a+b result = SysAdd((int)kernel->machine->ReadRegister(4), (int)kernel->machine->ReadRegister(5)); DEBUG(dbgSys, "Add returning with " << result << "\n"); kernel->machine->WriteRegister(2, (int)result); /* Modify return point */ { /* set previous program counter (debugging only)*/ kernel->machine->WriteRegister(PrevPCReg, kernel->machine->ReadRegister(PCReg)); /* set program counter to next instruction (all Instructions are 4 byte wide)*/ kernel->machine->WriteRegister(PCReg, kernel->machine->ReadRegister(PCReg) + 4); /* set next program counter for brach execution */ kernel->machine->WriteRegister(NextPCReg, kernel->machine->ReadRegister(PCReg)+4); } return; 14 Requirement • Implement following system calls – Print(char *msg) // Print a string on the screen – PrintNum(int n) // Print a number on the screen • Write a 2-page report – Don’t just paste your code, I’ll read it myself – State the problem you have encountered and how you solve it – Or anything else • If your project submission can’t compile and execute on Linux in Workstation Room 217, we will consider it as fail. – Please contact me to apply a workstation account if you need it. 15 The tools you need • Read a string from the memory of MIPS machine – Registers only store numbers, so a string is sent by its address – Use “kernel->machine->ReadMem()” to read data from an address – You can assume that the string would be terminated by ‘\0’ • Print characters to screen – Use “kernel->synchConsoleOut->PutChar()” – DO NOT USE cout, printf, or something like that! Let Nachos do its work! • Convert a number to a string – You can use sprintf() in stdio.h 16 Test files • We supply a test file “print.c” on our webpage – To build it, you need to modify code/test/Makefile, or download it from our webpage – Type “make” under code/test/ to compile it – Type “../build.linux/nachos –x print” to run – The output would be simple enough for you to verify it :D 17 Submission • Two people in one group (Write down the name and student ID of all members in the report) The file you need to send: • 1. 2. A report in .pdf or .doc exception.cc tar zcvf os_hw1_bXXXXXXXX_bOOOOOOOO.tar.gz report.doc exception.cc • Send your files – – Tar your files to an archieve named: os_hw1_bXXXXXXXX_bOOOOOOOO.tar.gz E-mail to artoo@csie.ntu.edu.tw with following title: [os_hw1] bXXXXXXXX_bOOOOOOOO 18 Submission (cont.) • Deadline: 10/6 24:00 – Delayed submission would have penalty! • DO NOT COPY!! Protect your code well!! 19