nachos.machine
Class Processor

java.lang.Object
  extended by nachos.machine.Processor

public final class Processor
extends Object

The Processor class simulates a MIPS processor that supports a subset of the R3000 instruction set. Specifically, the processor lacks all coprocessor support, and can only execute in user mode. Address translation information is accessed via the API. The API also allows a kernel to set an exception handler to be called on any user mode exception.

The Processor API is re-entrant, so a single simulated processor can be shared by multiple user threads.

An instance of a Processor also includes pages of physical memory accessible to user programs, the size of which is fixed by the constructor.


Field Summary
static int exceptionAddressError
          Caused by an access to a misaligned virtual address.
static int exceptionBusError
          Caused by an access to an invalid physical page.
static int exceptionIllegalInstruction
          Caused by an attempt to execute an illegal instruction.
static String[] exceptionNames
          The names of the CPU exceptions.
static int exceptionOverflow
          Caused by an overflow by a signed operation.
static int exceptionPageFault
          Caused by an access to an invalid virtual page.
static int exceptionReadOnly
          Caused by a write access to a read-only virtual page.
static int exceptionSyscall
          Caused by a syscall instruction.
static int exceptionTLBMiss
          Caused by an access to a virtual page not mapped by any TLB entry.
static int maxPages
          Number of pages in a 32-bit address space.
static int numUserRegisters
          The total number of software-accessible CPU registers.
static int pageSize
          Size of a page, in bytes.
static int regA0
          Index of argument register 0.
static int regA1
          Index of argument register 1.
static int regA2
          Index of argument register 2.
static int regA3
          Index of argument register 3.
static int regBadVAddr
          Index of the exception bad virtual address register.
static int regCause
          Index of the exception cause register.
static int regHi
          Index of the high register, used for multiplication and division.
static int regLo
          Index of the low register, used for multiplication and division.
static int regNextPC
          Index of the next program counter register.
static int regPC
          Index of the program counter register.
static int regRA
          Index of the return address register.
static int regSP
          Index of the stack pointer register.
static int regV0
          Index of return value register 0.
static int regV1
          Index of return value register 1.
 
Constructor Summary
Processor(Privilege privilege, int numPhysPages)
          Allocate a new MIPS processor, with the specified amount of memory.
 
Method Summary
 void advancePC()
          Advance the PC to the next instruction.
 Runnable getExceptionHandler()
          Get the exception handler, set by the last call to setExceptionHandler().
 byte[] getMemory()
          Return a reference to the physical memory array.
 int getNumPhysPages()
          Return the number of pages of physical memory attached to this simulated processor.
 TranslationEntry[] getPageTable()
          Get the current page table, set by the last call to setPageTable().
 int getTLBSize()
          Return the number of entries in this processor's TLB.
 boolean hasTLB()
          Test whether this processor uses a software-managed TLB, or single-level paging.
static int makeAddress(int page, int offset)
          Concatenate a page number and an offset into an address.
static int offsetFromAddress(int address)
          Extract the offset component from an address.
static int pageFromAddress(int address)
          Extract the page number component from a 32-bit address.
 int readRegister(int number)
          Read and return the contents of the specified CPU register.
 TranslationEntry readTLBEntry(int number)
          Returns the specified TLB entry.
 void run()
          Start executing instructions at the current PC.
 void setExceptionHandler(Runnable exceptionHandler)
          Set the exception handler, called whenever a user exception occurs.
 void setPageTable(TranslationEntry[] pageTable)
          Set the page table pointer.
 void writeRegister(int number, int value)
          Write the specified value into the specified CPU register.
 void writeTLBEntry(int number, TranslationEntry entry)
          Fill the specified TLB entry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

exceptionSyscall

public static final int exceptionSyscall
Caused by a syscall instruction.

See Also:
Constant Field Values

exceptionPageFault

public static final int exceptionPageFault
Caused by an access to an invalid virtual page.

See Also:
Constant Field Values

exceptionTLBMiss

public static final int exceptionTLBMiss
Caused by an access to a virtual page not mapped by any TLB entry.

See Also:
Constant Field Values

exceptionReadOnly

public static final int exceptionReadOnly
Caused by a write access to a read-only virtual page.

See Also:
Constant Field Values

exceptionBusError

public static final int exceptionBusError
Caused by an access to an invalid physical page.

See Also:
Constant Field Values

exceptionAddressError

public static final int exceptionAddressError
Caused by an access to a misaligned virtual address.

See Also:
Constant Field Values

exceptionOverflow

public static final int exceptionOverflow
Caused by an overflow by a signed operation.

See Also:
Constant Field Values

exceptionIllegalInstruction

public static final int exceptionIllegalInstruction
Caused by an attempt to execute an illegal instruction.

See Also:
Constant Field Values

exceptionNames

public static final String[] exceptionNames
The names of the CPU exceptions.


regV0

public static final int regV0
Index of return value register 0.

See Also:
Constant Field Values

regV1

public static final int regV1
Index of return value register 1.

See Also:
Constant Field Values

regA0

public static final int regA0
Index of argument register 0.

See Also:
Constant Field Values

regA1

public static final int regA1
Index of argument register 1.

See Also:
Constant Field Values

regA2

public static final int regA2
Index of argument register 2.

See Also:
Constant Field Values

regA3

public static final int regA3
Index of argument register 3.

See Also:
Constant Field Values

regSP

public static final int regSP
Index of the stack pointer register.

See Also:
Constant Field Values

regRA

public static final int regRA
Index of the return address register.

See Also:
Constant Field Values

regLo

public static final int regLo
Index of the low register, used for multiplication and division.

See Also:
Constant Field Values

regHi

public static final int regHi
Index of the high register, used for multiplication and division.

See Also:
Constant Field Values

regPC

public static final int regPC
Index of the program counter register.

See Also:
Constant Field Values

regNextPC

public static final int regNextPC
Index of the next program counter register.

See Also:
Constant Field Values

regCause

public static final int regCause
Index of the exception cause register.

See Also:
Constant Field Values

regBadVAddr

public static final int regBadVAddr
Index of the exception bad virtual address register.

See Also:
Constant Field Values

numUserRegisters

public static final int numUserRegisters
The total number of software-accessible CPU registers.

See Also:
Constant Field Values

pageSize

public static final int pageSize
Size of a page, in bytes.

See Also:
Constant Field Values

maxPages

public static final int maxPages
Number of pages in a 32-bit address space.

See Also:
Constant Field Values
Constructor Detail

Processor

public Processor(Privilege privilege,
                 int numPhysPages)
Allocate a new MIPS processor, with the specified amount of memory.

Parameters:
privilege - encapsulates privileged access to the Nachos machine.
numPhysPages - the number of pages of physical memory to attach.
Method Detail

setExceptionHandler

public void setExceptionHandler(Runnable exceptionHandler)
Set the exception handler, called whenever a user exception occurs.

When the exception handler is called, interrupts will be enabled, and the CPU cause register will specify the cause of the exception (see the exception* constants).

Parameters:
exceptionHandler - the kernel exception handler.

getExceptionHandler

public Runnable getExceptionHandler()
Get the exception handler, set by the last call to setExceptionHandler().

Returns:
the exception handler.

run

public void run()
Start executing instructions at the current PC. Never returns.


readRegister

public int readRegister(int number)
Read and return the contents of the specified CPU register.

Parameters:
number - the register to read.
Returns:
the value of the register.

writeRegister

public void writeRegister(int number,
                          int value)
Write the specified value into the specified CPU register.

Parameters:
number - the register to write.
value - the value to write.

hasTLB

public boolean hasTLB()
Test whether this processor uses a software-managed TLB, or single-level paging.

If false, this processor directly supports single-level paging; use setPageTable().

If true, this processor has a software-managed TLB; use getTLBSize(), readTLBEntry(), and writeTLBEntry().

Using a method associated with the wrong address translation mechanism will result in an assertion failure.

Returns:
true if this processor has a software-managed TLB.

getPageTable

public TranslationEntry[] getPageTable()
Get the current page table, set by the last call to setPageTable().

Returns:
the current page table.

setPageTable

public void setPageTable(TranslationEntry[] pageTable)
Set the page table pointer. All further address translations will use the specified page table. The size of the current address space will be determined from the length of the page table array.

Parameters:
pageTable - the page table to use.

getTLBSize

public int getTLBSize()
Return the number of entries in this processor's TLB.

Returns:
the number of entries in this processor's TLB.

readTLBEntry

public TranslationEntry readTLBEntry(int number)
Returns the specified TLB entry.

Parameters:
number - the index into the TLB.
Returns:
the contents of the specified TLB entry.

writeTLBEntry

public void writeTLBEntry(int number,
                          TranslationEntry entry)
Fill the specified TLB entry.

The TLB is fully associative, so the location of an entry within the TLB does not affect anything.

Parameters:
number - the index into the TLB.
entry - the new contents of the TLB entry.

getNumPhysPages

public int getNumPhysPages()
Return the number of pages of physical memory attached to this simulated processor.

Returns:
the number of pages of physical memory.

getMemory

public byte[] getMemory()
Return a reference to the physical memory array. The size of this array is pageSize * getNumPhysPages().

Returns:
the main memory array.

makeAddress

public static int makeAddress(int page,
                              int offset)
Concatenate a page number and an offset into an address.

Parameters:
page - the page number. Must be between 0 and (232 / pageSize) - 1.
offset - the offset within the page. Must be between 0 and pageSize - 1.
Returns:
a 32-bit address consisting of the specified page and offset.

pageFromAddress

public static int pageFromAddress(int address)
Extract the page number component from a 32-bit address.

Parameters:
address - the 32-bit address.
Returns:
the page number component of the address.

offsetFromAddress

public static int offsetFromAddress(int address)
Extract the offset component from an address.

Parameters:
address - the 32-bit address.
Returns:
the offset component of the address.

advancePC

public void advancePC()
Advance the PC to the next instruction.

Transfer the contents of the nextPC register into the PC register, and then add 4 to the value in the nextPC register. Same as advancePC(readRegister(regNextPC)+4).

Use after handling a syscall exception so that the processor will move on to the next instruction.