nachos.userprog
Class UserProcess

java.lang.Object
  extended by nachos.userprog.UserProcess
Direct Known Subclasses:
VMProcess

public class UserProcess
extends Object

Encapsulates the state of a user process that is not contained in its user thread (or threads). This includes its address translation state, a file table, and information about the program being executed.

This class is extended by other classes to support additional functionality (such as additional syscalls).

See Also:
VMProcess, NetProcess

Field Summary
protected  Coff coff
          The program being run by this process.
protected  int numPages
          The number of contiguous pages occupied by the program.
protected  TranslationEntry[] pageTable
          This process's page table.
protected  int stackPages
          The number of pages in the program's stack.
 
Constructor Summary
UserProcess()
          Allocate a new process.
 
Method Summary
 boolean execute(String name, String[] args)
          Execute the specified program with the specified arguments.
 void handleException(int cause)
          Handle a user exception.
 int handleSyscall(int syscall, int a0, int a1, int a2, int a3)
          Handle a syscall exception.
 void initRegisters()
          Initialize the processor's registers in preparation for running the program loaded into this process.
protected  boolean loadSections()
          Allocates memory for this process, and loads the COFF sections into memory.
static UserProcess newUserProcess()
          Allocate and return a new process of the correct class.
 int readVirtualMemory(int vaddr, byte[] data)
          Transfer data from this process's virtual memory to all of the specified array.
 int readVirtualMemory(int vaddr, byte[] data, int offset, int length)
          Transfer data from this process's virtual memory to the specified array.
 String readVirtualMemoryString(int vaddr, int maxLength)
          Read a null-terminated string from this process's virtual memory.
 void restoreState()
          Restore the state of this process after a context switch.
 void saveState()
          Save the state of this process in preparation for a context switch.
protected  void unloadSections()
          Release any resources allocated by loadSections().
 int writeVirtualMemory(int vaddr, byte[] data)
          Transfer all data from the specified array to this process's virtual memory.
 int writeVirtualMemory(int vaddr, byte[] data, int offset, int length)
          Transfer data from the specified array to this process's virtual memory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

coff

protected Coff coff
The program being run by this process.


pageTable

protected TranslationEntry[] pageTable
This process's page table.


numPages

protected int numPages
The number of contiguous pages occupied by the program.


stackPages

protected final int stackPages
The number of pages in the program's stack.

See Also:
Constant Field Values
Constructor Detail

UserProcess

public UserProcess()
Allocate a new process.

Method Detail

newUserProcess

public static UserProcess newUserProcess()
Allocate and return a new process of the correct class. The class name is specified by the nachos.conf key Kernel.processClassName.

Returns:
a new process of the correct class.

execute

public boolean execute(String name,
                       String[] args)
Execute the specified program with the specified arguments. Attempts to load the program, and then forks a thread to run it.

Parameters:
name - the name of the file containing the executable.
args - the arguments to pass to the executable.
Returns:
true if the program was successfully executed.

saveState

public void saveState()
Save the state of this process in preparation for a context switch. Called by UThread.saveState().


restoreState

public void restoreState()
Restore the state of this process after a context switch. Called by UThread.restoreState().


readVirtualMemoryString

public String readVirtualMemoryString(int vaddr,
                                      int maxLength)
Read a null-terminated string from this process's virtual memory. Read at most maxLength + 1 bytes from the specified address, search for the null terminator, and convert it to a java.lang.String, without including the null terminator. If no null terminator is found, returns null.

Parameters:
vaddr - the starting virtual address of the null-terminated string.
maxLength - the maximum number of characters in the string, not including the null terminator.
Returns:
the string read, or null if no null terminator was found.

readVirtualMemory

public int readVirtualMemory(int vaddr,
                             byte[] data)
Transfer data from this process's virtual memory to all of the specified array. Same as readVirtualMemory(vaddr, data, 0, data.length).

Parameters:
vaddr - the first byte of virtual memory to read.
data - the array where the data will be stored.
Returns:
the number of bytes successfully transferred.

readVirtualMemory

public int readVirtualMemory(int vaddr,
                             byte[] data,
                             int offset,
                             int length)
Transfer data from this process's virtual memory to the specified array. This method handles address translation details. This method must not destroy the current process if an error occurs, but instead should return the number of bytes successfully copied (or zero if no data could be copied).

Parameters:
vaddr - the first byte of virtual memory to read.
data - the array where the data will be stored.
offset - the first byte to write in the array.
length - the number of bytes to transfer from virtual memory to the array.
Returns:
the number of bytes successfully transferred.

writeVirtualMemory

public int writeVirtualMemory(int vaddr,
                              byte[] data)
Transfer all data from the specified array to this process's virtual memory. Same as writeVirtualMemory(vaddr, data, 0, data.length).

Parameters:
vaddr - the first byte of virtual memory to write.
data - the array containing the data to transfer.
Returns:
the number of bytes successfully transferred.

writeVirtualMemory

public int writeVirtualMemory(int vaddr,
                              byte[] data,
                              int offset,
                              int length)
Transfer data from the specified array to this process's virtual memory. This method handles address translation details. This method must not destroy the current process if an error occurs, but instead should return the number of bytes successfully copied (or zero if no data could be copied).

Parameters:
vaddr - the first byte of virtual memory to write.
data - the array containing the data to transfer.
offset - the first byte to transfer from the array.
length - the number of bytes to transfer from the array to virtual memory.
Returns:
the number of bytes successfully transferred.

loadSections

protected boolean loadSections()
Allocates memory for this process, and loads the COFF sections into memory. If this returns successfully, the process will definitely be run (this is the last step in process initialization that can fail).

Returns:
true if the sections were successfully loaded.

unloadSections

protected void unloadSections()
Release any resources allocated by loadSections().


initRegisters

public void initRegisters()
Initialize the processor's registers in preparation for running the program loaded into this process. Set the PC register to point at the start function, set the stack pointer register to point at the top of the stack, set the A0 and A1 registers to argc and argv, respectively, and initialize all other registers to 0.


handleSyscall

public int handleSyscall(int syscall,
                         int a0,
                         int a1,
                         int a2,
                         int a3)
Handle a syscall exception. Called by handleException(). The syscall argument identifies which syscall the user executed:
syscall#syscall prototype
0void halt();
1void exit(int status);
2int exec(char *name, int argc, char **argv);
3int join(int pid, int *status);
4int creat(char *name);
5int open(char *name);
6int read(int fd, char *buffer, int size);
7int write(int fd, char *buffer, int size);
8int close(int fd);
9int unlink(char *name);

Parameters:
syscall - the syscall number.
a0 - the first syscall argument.
a1 - the second syscall argument.
a2 - the third syscall argument.
a3 - the fourth syscall argument.
Returns:
the value to be returned to the user.

handleException

public void handleException(int cause)
Handle a user exception. Called by UserKernel.exceptionHandler(). The cause argument identifies which exception occurred; see the Processor.exceptionZZZ constants.

Parameters:
cause - the user exception that occurred.