Nachos Introduction & Project 1
Speaker: Chien-Chung Ho 2010/10/07
Outline
Overview
Installing Nachos
Project 1
Submission
Outline
Overview
Installing Nachos
Project 1
Submission
Nachos Basics
Not Another Completely Heuristic Operating System
http://www.cs.washington.edu/homes/tom/nachos/
Purposes
Teach monolithic kernel design and implementation
Do experiments
Facts
Real hardware is difficult to handle
May break if handled wrong
Approaches
Use a virtual MIPS machine
Provide basic OS elements
The Architecture of Nachos
Nachos
User Thread 1 User Thread 2 User Thread 3
Nachos Kernel
(Threads, File System, …)
Simulated MIPS Machine (CPU, Memory, Devices, …)
Nachos 4.0
Nachos simulates MIPS architecture on the host system (Unix / Linux / Windows / MacOS X)
User programs need a cross-complier (target MIPS)
Nachos appears as a single threaded process to the host operating system
Nachos 4.0 (Cont’d)
A skeletal OS
Threads
User‐level processes
Virtual memory
Interrupt‐driven I/O devices
2 execution modes
Nachos kernel
Executes when
Nachos starts up
A user‐program causes a hardware trap (page fault, system call, etc.)
MIPS simulator is initialized and started by Nachos kernel
Source Tree
Root directory
C++ introduction to teach how to write C++
Root directory of Nachos’s source code
File system Nachos’s library MIPS machine
In/Out message queues
Nachos’s sample user programs Threads
User programs’ interfaces
The tool to convert user programs from MIPS’s COFF into Nachos’s NOFF format (NOFF: Nachos Object File Format)
Building directories for different systems (platform-dependent)
Source code of Nachos kernel and MIPS simulator
System Start-Up Flowchart
./nachos start
code/threads/main.cc main
Create kernel object Initialize the system Invoke user programs
System halts and terminates
Load user programs Run user programs
Create “main” thread
Create & initialize MIPS machine Initialize the ready queue, scheduler, console and timer
Start up interrupt handing Run some system testing
System Start-Up
main(int argc, char **argv) //* code/threads/main.cc
Parse parameters
E.g., >nachos –x a.out, where –x a.out is a parameter.
Create and initialize nachos kernel
Create kernel object
kernel = new Kernel(argc, argv);
Initialize the system
kernel‐>Initialize(); //* code/threads/kernel.cc Kernel::Initialize()
Run some tests if requested
E.g., kernel‐>ThreadSelfTest(); to test threads
Run a user program if any. Otherwise terminate Nachos
How to Run a User Program
If we specify “‐x” as Nachos’s parameter, it means that we want to run a user program:
Create an address space to run a user program
AddrSpace *space = new AddrSpace; // in code/userprog/addrspace.cc
Create address translation table (page table) and set up the translation from program memory to physical memory.
By default, the mapping is 1 to 1 since we are only uniprogramming->have a single unsegmented page table.
Load the whole user program into MIPS machine’s main memory
space‐>Load(userProgName); // AddrSpace::Load()
Execute the user program
space‐>Execute(); // AddrSpace::Execute()
Jump to run the user program
kernel‐>machine‐>Run();
System Call Procedure
Related Code for User Processes
code/test/start.S
Start-up assembly code for every user program of Nachos
code/userprog/syscall.h
Definitions of system call prototypes
code/userprog/exception.cc
The handler for system calls and other exceptions are here
Starting of a User Program
code/test/start.S
Define what is needed for a user program
Starting point
User program’s system call functions
Startup assembly code of every user program of Nachos
Initialize and run a C user program by jumping to its main()
The Process of a System Call in a User Program
The user program (defined in code/test/start.S)
Stores the system codes in Register 2
Stores the arguments in Register 4 ,5, 6, and 7
ExceptionHandler in code/userprog/exception.cc
Fetches system call number in Register 2.
Is the entry point into the Nachos kernel from user programs
Syscall
exceptions
Adds new exception handler’s switch case in switch … case section
The system call codes are executed
PC increases (PCReg)
Outline
Overview
Installing Nachos
Project 1
Submission
Nachos Development Environment
Tools: gcc/g++, patch, wget,…
Nachos source code
MIPS cross compiler
Translator coff2noff
Setting path of the toolchain and Makefiles
Installation Guide
Installing Nachos on Linux in workstation R217
http://newslab.csie.ntu.edu.tw/course/OS2010/files/nachos/217(64-bit).htm
Installation on X86 32‐bit machine
http://newslab.csie.ntu.edu.tw/course/OS2006/files/nachos/217.htm
Make sure your project can be executed on workstations in R217 (64-bit)
Outline
Overview
Installing Nachos
Project 1
Submission
Declare a System Call
Declare a new system call in code/userprog/syscall.h
Define a new system call ID
E.g., #define SC_Add 42
Declare the interface for Nachos system calls, which will be called by the user program.
E.g., int Add(int op1, int op2);
Add the low level operation to support the new declared system call in code/test/start.S
From an Exception Handler to a Specific System call
In ExceptionHandler in code/userprog/execption.cc
An Example of System Call
In code/userprog/ksyscall.h int SysAdd(int op1, int op2) {
return op1 + op2;
}
Test Program
Put your test.c in code/test/
int main() {
int j, k = 0;
j=OSAdd(20, 10);
/*System call print: The return value of OSAdd(10,20) is 30*/
k=OSSub(j,15);
/*System call print: The return value of OSSub(30,15) is 15 */
OSAdd(j,k);
/*System call print: The return value of OSAdd(30,15) is 45*/
Halt();
/* not reached */
}
Makefile
For example: We want to compile a user program called test.c
You can edit code/test/Makefile to add a new compiling rule for it (Output file is test in the following example)
Type “make” to compile it
Results
Outline
Overview
Installing Nachos
Project 1
Submission
Submission
2 members in a team
Email your team member list to ntuos2010@gmail.com
Subject: [OS分組] 王小明 R12345678、張小美 R98765432
Deadline: 2010/10/20 21:00
For the delayed submission, deduct two points for each day
Check your team number ## in website after 10/11 17:00
Upload your team project to the FTP
ftp server: 140.112.28.113 /os2010 /2010os
Pack the “NachOS‐4.0/code” and your report in a file named by
“OSPJ1_Group##.tar.gz”
The report is limited within 2 A4 pages
Grading: code 70% and report 30%. Please highlight your bonus
Note
Do not copy homework
Keep your code secret
On any quetsions
Write email to ask TAs
黃柏鈞: b91048@csie.ntu.edu.tw
何建忠: r99922110@csie.ntu.edu.tw 陳煒栩: wish520angela@hotmail.com 鄭聖威: r99922123@csie.ntu.edu.tw
Come to R438 during TA hours