Transcript CSE120 D5
CSE120 Discussion 5
Xinxin Jin
Where Are We?
Now we have finished some thread mechanisms to support
Nachos kernel
Next, we want to enable user-level program to access these
kernel routines via system calls
Prjoect 2 Introduction
You will touch two parts of the code base:
• The operating system running java programs as you saw in
project 1 (mainly /userprog class)
• User programs written in C and compiled into MIPS machine
code (under /test directory).
Project 2 Tasks
Implement file system calls
• creat,open,read,write,close,and unlink, documented in
test/syscall.h
Support multiprogramming
– Memory partitioning for multi-processes
Implement process control system calls
• exec,join,and exit, also documented in syscall.h
Task 1: Implement File System Calls
creat
- Create a file and return a file descriptor
close
– Close a file
unlink
– Delete a file
open
– Open a file and return a file descriptor, an integer specifying the position of this
open file in the table of open files for the current process
read
– Read data from an open file to user process
write
– Write data to an open file from user process
Don’t be scared by the lengthy instructions.
We will reach the end step by step !
My Suggestions
Understand the the workflow of syscall in Nachos
Read /test/syscall.h. Understand the functionality and the
arguments of each syscall.
Add syscall handler at the right place
Start from the simplest prototype of each syscall
• leave the complex restrictions alone for the first implementation
Add the restrictions back piece by piece
Don’t forget more sanity checks !
• When does this syscall fail ?
Recap: System Call
Nachos Syscall: User space program
Syscall interface (defined in syscall.h)
Void halt()
Syscall code (defined in syscall.h)
#define syscallHalt
0
Match syscall name with syscall code (start.s)
SYSCALLSTUB(halt, syscallHalt)
The instruction that traps to nachos kernel (start.s)
syscall
Compile User progarm
Set up MIPS cross compiler on linux machine (see
instructions on the website)
• Avoid running the compiler on Windows or Mac
Run “make” under /test
• Generate *.coff executable file for MIPS processor
Add your own program
• Write hello.c under /test
• Add hello to the end of TARGETS in Makefile
• Run “make hello”, you will see hello.coff
Nachos Syscall: The Kernel Program
Some changed fields in nachos.conf
Machine.stubFileSystem = true
-- You need to access the stubFileSystem returned by Machine.stubFileSystem()
--The filesystem access /test directory (same as root dir on linux machine)
Machine.console = true
--Now we have a simulated console for standard input and standoutput
Processor.numPhysPages = 64
-- The total number of available physical pages of the machine
Kernel.shellProgram = halt.coff
--The kernel executes the user program halt.coff by default
-- Run nachos –x hello.coff to overrwite the default user progarm
Kernel.processClassName = nachos.userprog.UserProcess
-- Manage user process. You will heavily work on this class
Kernel.kernel = nachos.userprog.UserKernel
-- The nachos kernel for project2
/userprog Package
UserKernel.java
• Nachos kernel that supports multiprogramming
UserProcess.java
• Manage the state of user processes, method handleSyscall is called
to handle system calls made by user programs
UThead.java
• User level thread, a subclass of Kthread, contains a reference to a
UserProcess
SynchConsole.java
– Simulate a console for stdin and stdout
Sycall Handler in Kernel
The entry point of syscall handler
• handleSyscall(int syscall, int a0, int a1, int a2, int a3)
The syscall code (e.g. syscallHalt) defined in
UserProcess.java is consistent with the one defined
syscall.h
System Call Execution Flow Summary
Let’s Get Started
Read the description of creat() and open() in syscall.h
Add syscall handlers of these two
• Leave the method body empty for now
Pass Parameter by Virtual Address
“Pass by value” vs.“Pass by address”
• read (int fd, char *buffer, int size)
• buffer is passed as virtual address, which is a pointer to an
array of bytes, so use readVirtualMemory and
writeVirtualMemory for these parameters.
open(char *fileName)
• fileName is passed as a pointer its first character, so use
readVirtualMemoryString to get this parameter.
Support Concurrently Open Files
Your implementation should support at least 16
concurrently open files per process
Each file that a process has opened should have a unique
file descriptor associated with it
• If a program call open() on the same file twice, just return
different file descriptors
A given file descriptor can be reused if the file associated
with it is closed
Open File Descriptor Table
File descriptor is used by user program to refer
to a file
• creat and open return a file descriptor to user
program
• this descriptor will be used for the following
read/write
FD table is an array associated with every
process
• each entry corresponds to one open file
• creat and open add an entry to the
table, return the descriptor to user
program
• close delete an entry from the table
Support Large Data Transfer In read()/write()
You cannot allocate unlimited buffer size
What is the appropricate buffer size to pass data between the
file and memory ?
Syscall Parameter Sanity Check
OS should never allow a user program to screw up the
kernel or other user programs
For example, in read( int fd, char *buffer, int size ) , what
kinds of parameters should be rejected ?
• Is the file descriptor valid ?
• Is size valid ?
• Is buffer and (buffer + size) in this process’s virtual address
space ?