7 Task2
7 Task2
7 Task2
Feroz Salam
Imperial College London
1 / 17
Introduction
Goal
Get user programs safely working on Pintos
Tasks
i Set up stack and pass arguments ii Implement process waiting iii Implement system calls
2 / 17
Getting Started
Testing
Important to read Testing section again
With a large number of test cases (70+), its useful to know how to run single tests independently.
3 / 17
Important Files
Files to modify:
Most of your work will be done in the userprog directory. i process.c
Handles loading, execution and exit of processes Set up stack code goes here Process waiting code goes here
ii syscall.c
All system calls go here Initial code exits immediately
iii exception.c
Contains exception handling code May/may not modify this depending on implementation
4 / 17
Important Files
ii threads/vaddr.h
is user vaddr() used to validate user provided pointers
iii lib/string.c
strtok r() used to tokenise strings
5 / 17
6 / 17
userprog/process.c
process execute() creates a thread that runs start process(filename,...) start process() -> load(filename) load() does all remaining work to load user process (setting up stack, data, code, etc.)
7 / 17
Project Requirements
Passing arguments Safe memory access Process waiting System calls (a long list) Process termination messages Denying writes to les in use as executables
Note
This is just a list of work that you will have to do, not an order of implementation.
8 / 17
Parsing Arguments
What?
Have to parse command line string cp foo.txt ../bar.txt into
cp foo.txt ../bar.txt
How?
Any way you like, really. Just FYI, strtok r() in lib/string.c might be useful
Where?
Anywhere before you load the process, obviously Spec says in process execute()
Feroz Salam (Imperial College London) Pintos Task 2: User Programs February 10, 2010 9 / 17
Where?
In start process() after the interrupt frame (containing the address of the stack pointer) is initialized
Feroz Salam (Imperial College London) Pintos Task 2: User Programs February 10, 2010 10 / 17
When an user program makes a system call, the kernel will have to deal dereferencing any pointers required to pass arguments. This can be problematic because of
i NULL pointers ii pointers to unmapped user memory iii pointers to kernel addresses
Dealing with such cases is easy - once identied, kill the process causing the problem and free any resources held by it. Watch out for special problems posed by buers and strings
11 / 17
System Calls
System calls allow user processes to ask the kernel to execute operations that they dont have permission to execute.
In Pintos
You will have to implement the syscall handler() in syscall.c First, read the syscall number at the stack pointer (f->esp) Read the arguments above the stack pointer Pass to the appropriate function (syscall numbers are dened in lib/syscall-nr.h) Return result to f->eax Try to avoid duplicating code all over the handler, neater solutions mean fewer mistakes and code that is easier to debug
Feroz Salam (Imperial College London) Pintos Task 2: User Programs February 10, 2010 13 / 17
Using lesys
Pintos comes with a basic, built-in lesys implementation in file.h and filesys.h. You do not need to modify this Syscalls use le descriptors (int), but the lesystem implementation uses pointers to le structs. Mappings are left to you. When modifying les in the lesystem, make sure that no other processes are able to make changes. No ner synchronisation is expected. Special le descriptors for the console: STDOUT FILENO for writing to console STDIN FILENO for reading from console
14 / 17
Process Waiting
process wait()
Calling function blocks (using synchronisation) waiting for the child process with the provided pid to exit. wait system call is trivial once it is implemented Returns the exit status of the child (or (-1) if error) Most work of all the functions Read the specication carefully before starting work on this, and ensure you start work on process wait() as soon as possible
15 / 17
Code that is currently running should not be modied Use file deny write to prevent writes to an open le Use file allow write to re-enable writes Executables should be kept open and unwriteable for as long as the process is running
16 / 17
17 / 17