hw4 test
This commit is contained in:
@@ -172,13 +172,19 @@ ConsoleOutput::PutChar(char ch)
|
||||
kernel->interrupt->Schedule(this, ConsoleTime, ConsoleWriteInt);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// ConsoleOutput::PutInt()
|
||||
// Write a int to the simulated display, schedule an interrupt
|
||||
// to occur in the future, and return.
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
void
|
||||
ConsoleOutput::PutInt(int value)
|
||||
{
|
||||
ASSERT(putBusy == FALSE);
|
||||
char *printStr = (char*)malloc(sizeof(char)*15);
|
||||
char * printStr = (char*)malloc(sizeof(char) * 15);
|
||||
sprintf(printStr, "%d\n", value);
|
||||
WriteFile(writeFileNo, printStr, strlen(printStr)*sizeof(char));
|
||||
WriteFile(writeFileNo, printStr, strlen(printStr) * sizeof(char));
|
||||
putBusy = TRUE;
|
||||
kernel->interrupt->Schedule(this, ConsoleTime, ConsoleWriteInt);
|
||||
}
|
||||
|
||||
@@ -76,7 +76,9 @@ class ConsoleOutput : public CallBackObj {
|
||||
void PutChar(char ch); // Write "ch" to the console display,
|
||||
// and return immediately. "callWhenDone"
|
||||
// will called when the I/O completes.
|
||||
void PutInt(int n);
|
||||
|
||||
void PutInt(int value);
|
||||
|
||||
void CallBack(); // Invoked when next character can be put
|
||||
// out to the display.
|
||||
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
|
||||
#include "copyright.h"
|
||||
#include "interrupt.h"
|
||||
#include "main.h"
|
||||
#include "synchconsole.h"
|
||||
#include "main.h"
|
||||
|
||||
// String definitions for debugging messages
|
||||
|
||||
@@ -341,7 +341,7 @@ static void
|
||||
PrintPending (PendingInterrupt *pending)
|
||||
{
|
||||
cout << "Interrupt handler "<< intTypeNames[pending->type];
|
||||
cout << ", scheduled at " << pending->when << endl;
|
||||
cout << ", scheduled at " << pending->when;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
@@ -360,8 +360,31 @@ Interrupt::DumpState()
|
||||
cout << "\nEnd of pending interrupts\n";
|
||||
}
|
||||
|
||||
void
|
||||
Interrupt::PrintInt(int value)
|
||||
void Interrupt::PrintInt(int value)
|
||||
{
|
||||
kernel->synchConsoleOut->PutInt(value);
|
||||
}
|
||||
|
||||
OpenFileId
|
||||
Interrupt::OpenFile(char *filename)
|
||||
{
|
||||
return kernel->fileSystem->OpenFiles(filename);
|
||||
}
|
||||
|
||||
int
|
||||
Interrupt::WriteFile(char *buffer, int size, OpenFileId fd)
|
||||
{
|
||||
return kernel->fileSystem->WriteFile(buffer, size, fd);
|
||||
}
|
||||
|
||||
int
|
||||
Interrupt::CloseFile(OpenFileId fd)
|
||||
{
|
||||
return kernel->fileSystem->CloseFile(fd);
|
||||
}
|
||||
|
||||
int
|
||||
Interrupt::ReadFile(char *buffer, int size, OpenFileId fd)
|
||||
{
|
||||
return kernel->fileSystem->ReadFile(buffer, size, fd);
|
||||
}
|
||||
|
||||
@@ -37,35 +37,41 @@
|
||||
#include "list.h"
|
||||
#include "callback.h"
|
||||
|
||||
#include "filesys.h"
|
||||
|
||||
typedef int OpenFileId;
|
||||
|
||||
// Interrupts can be disabled (IntOff) or enabled (IntOn)
|
||||
enum IntStatus { IntOff, IntOn };
|
||||
|
||||
// Nachos can be running kernel code (SystemMode), user code (UserMode),
|
||||
// or there can be no runnable thread, because the ready list
|
||||
// is empty (IdleMode).
|
||||
enum MachineStatus {IdleMode, SystemMode, UserMode};
|
||||
enum MachineStatus { IdleMode, SystemMode, UserMode };
|
||||
|
||||
// IntType records which hardware device generated an interrupt.
|
||||
// In Nachos, we support a hardware timer device, a disk, a console
|
||||
// display and keyboard, and a network.
|
||||
enum IntType { TimerInt, DiskInt, ConsoleWriteInt, ConsoleReadInt,
|
||||
NetworkSendInt, NetworkRecvInt};
|
||||
enum IntType {
|
||||
TimerInt, DiskInt, ConsoleWriteInt, ConsoleReadInt,
|
||||
NetworkSendInt, NetworkRecvInt
|
||||
};
|
||||
|
||||
// The following class defines an interrupt that is scheduled
|
||||
// to occur in the future. The internal data structures are
|
||||
// left public to make it simpler to manipulate.
|
||||
|
||||
class PendingInterrupt {
|
||||
public:
|
||||
PendingInterrupt(CallBackObj *callOnInt, int time, IntType kind);
|
||||
// initialize an interrupt that will
|
||||
// occur in the future
|
||||
public:
|
||||
PendingInterrupt(CallBackObj* callOnInt, int time, IntType kind);
|
||||
// initialize an interrupt that will
|
||||
// occur in the future
|
||||
|
||||
CallBackObj *callOnInterrupt;// The object (in the hardware device
|
||||
// emulator) to call when the interrupt occurs
|
||||
|
||||
int when; // When the interrupt is supposed to fire
|
||||
IntType type; // for debugging
|
||||
CallBackObj* callOnInterrupt;// The object (in the hardware device
|
||||
// emulator) to call when the interrupt occurs
|
||||
|
||||
int when; // When the interrupt is supposed to fire
|
||||
IntType type; // for debugging
|
||||
};
|
||||
|
||||
// The following class defines the data structures for the simulation
|
||||
@@ -74,72 +80,76 @@ class PendingInterrupt {
|
||||
// in the future.
|
||||
|
||||
class Interrupt {
|
||||
public:
|
||||
Interrupt(); // initialize the interrupt simulation
|
||||
~Interrupt(); // de-allocate data structures
|
||||
|
||||
IntStatus SetLevel(IntStatus level);
|
||||
// Disable or enable interrupts
|
||||
// and return previous setting.
|
||||
public:
|
||||
Interrupt(); // initialize the interrupt simulation
|
||||
~Interrupt(); // de-allocate data structures
|
||||
|
||||
void Enable() { (void) SetLevel(IntOn); }
|
||||
// Enable interrupts.
|
||||
IntStatus getLevel() {return level;}
|
||||
// Return whether interrupts
|
||||
// are enabled or disabled
|
||||
|
||||
void Idle(); // The ready queue is empty, roll
|
||||
// simulated time forward until the
|
||||
// next interrupt
|
||||
IntStatus SetLevel(IntStatus level);
|
||||
// Disable or enable interrupts
|
||||
// and return previous setting.
|
||||
|
||||
void Halt(); // quit and print out stats
|
||||
void Enable() { (void)SetLevel(IntOn); }
|
||||
// Enable interrupts.
|
||||
IntStatus getLevel() { return level; }
|
||||
// Return whether interrupts
|
||||
// are enabled or disabled
|
||||
|
||||
void PrintInt(int number);
|
||||
int CreateFile(char *filename);
|
||||
|
||||
void YieldOnReturn(); // cause a context switch on return
|
||||
// from an interrupt handler
|
||||
void Idle(); // The ready queue is empty, roll
|
||||
// simulated time forward until the
|
||||
// next interrupt
|
||||
|
||||
MachineStatus getStatus() { return status; }
|
||||
void setStatus(MachineStatus st) { status = st; }
|
||||
// idle, kernel, user
|
||||
void Halt(); // quit and print out stats
|
||||
|
||||
void DumpState(); // Print interrupt state
|
||||
|
||||
void PrintInt(int number);
|
||||
int CreateFile(char* filename);
|
||||
OpenFileId OpenFile(char* filename);
|
||||
int WriteFile(char* buffer, int size, OpenFileId fd);
|
||||
int CloseFile(OpenFileId fd);
|
||||
int ReadFile(char* buffer, int size, OpenFileId fd);
|
||||
|
||||
// NOTE: the following are internal to the hardware simulation code.
|
||||
// DO NOT call these directly. I should make them "private",
|
||||
// but they need to be public since they are called by the
|
||||
// hardware device simulators.
|
||||
void YieldOnReturn(); // cause a context switch on return
|
||||
// from an interrupt handler
|
||||
|
||||
void Schedule(CallBackObj *callTo, int when, IntType type);
|
||||
// Schedule an interrupt to occur
|
||||
// at time "when". This is called
|
||||
// by the hardware device simulators.
|
||||
|
||||
void OneTick(); // Advance simulated time
|
||||
MachineStatus getStatus() { return status; }
|
||||
void setStatus(MachineStatus st) { status = st; }
|
||||
// idle, kernel, user
|
||||
|
||||
private:
|
||||
IntStatus level; // are interrupts enabled or disabled?
|
||||
SortedList<PendingInterrupt *> *pending;
|
||||
// the list of interrupts scheduled
|
||||
// to occur in the future
|
||||
//int writeFileNo; //UNIX file emulating the display
|
||||
bool inHandler; // TRUE if we are running an interrupt handler
|
||||
//bool putBusy; // Is a PrintInt operation in progress
|
||||
//If so, you cannoot do another one
|
||||
bool yieldOnReturn; // TRUE if we are to context switch
|
||||
// on return from the interrupt handler
|
||||
MachineStatus status; // idle, kernel mode, user mode
|
||||
void DumpState(); // Print interrupt state
|
||||
|
||||
// these functions are internal to the interrupt simulation code
|
||||
|
||||
bool CheckIfDue(bool advanceClock);
|
||||
// Check if any interrupts are supposed
|
||||
// to occur now, and if so, do them
|
||||
// NOTE: the following are internal to the hardware simulation code.
|
||||
// DO NOT call these directly. I should make them "private",
|
||||
// but they need to be public since they are called by the
|
||||
// hardware device simulators.
|
||||
|
||||
void ChangeLevel(IntStatus old, // SetLevel, without advancing the
|
||||
IntStatus now); // simulated time
|
||||
void Schedule(CallBackObj* callTo, int when, IntType type);
|
||||
// Schedule an interrupt to occur
|
||||
// at time "when". This is called
|
||||
// by the hardware device simulators.
|
||||
|
||||
void OneTick(); // Advance simulated time
|
||||
|
||||
private:
|
||||
IntStatus level; // are interrupts enabled or disabled?
|
||||
SortedList<PendingInterrupt*>* pending;
|
||||
// the list of interrupts scheduled
|
||||
// to occur in the future
|
||||
//int writeFileNo; //UNIX file emulating the display
|
||||
bool inHandler; // TRUE if we are running an interrupt handler
|
||||
//bool putBusy; // Is a PrintInt operation in progress
|
||||
//If so, you cannoot do another one
|
||||
bool yieldOnReturn; // TRUE if we are to context switch
|
||||
// on return from the interrupt handler
|
||||
MachineStatus status; // idle, kernel mode, user mode
|
||||
|
||||
// these functions are internal to the interrupt simulation code
|
||||
|
||||
bool CheckIfDue(bool advanceClock);
|
||||
// Check if any interrupts are supposed
|
||||
// to occur now, and if so, do them
|
||||
|
||||
void ChangeLevel(IntStatus old, // SetLevel, without advancing the
|
||||
IntStatus now); // simulated time
|
||||
};
|
||||
|
||||
#endif // INTERRRUPT_H
|
||||
|
||||
@@ -13,17 +13,10 @@
|
||||
|
||||
// Textual names of the exceptions that can be generated by user program
|
||||
// execution, for debugging.
|
||||
static char* exceptionNames[] = {
|
||||
"no exception",
|
||||
"syscall",
|
||||
"page fault/no TLB entry",
|
||||
"page read only",
|
||||
"bus error",
|
||||
"address error",
|
||||
"overflow",
|
||||
"illegal instruction",
|
||||
"bad memory allocation"
|
||||
};
|
||||
static char* exceptionNames[] = { "no exception", "syscall",
|
||||
"page fault/no TLB entry", "page read only",
|
||||
"bus error", "address error", "overflow",
|
||||
"illegal instruction" };
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// CheckEndian
|
||||
|
||||
@@ -28,34 +28,34 @@
|
||||
// Definitions related to the size, and format of user memory
|
||||
|
||||
const int PageSize = 128; // set the page size equal to
|
||||
// the disk sector size, for simplicity
|
||||
// the disk sector size, for simplicity
|
||||
|
||||
//
|
||||
// You are allowed to change this value.
|
||||
// Doing so will change the number of pages of physical memory
|
||||
// available on the simulated machine.
|
||||
//
|
||||
//
|
||||
// You are allowed to change this value.
|
||||
// Doing so will change the number of pages of physical memory
|
||||
// available on the simulated machine.
|
||||
//
|
||||
const int NumPhysPages = 128;
|
||||
|
||||
const int MemorySize = (NumPhysPages * PageSize);
|
||||
const int TLBSize = 4; // if there is a TLB, make it small
|
||||
|
||||
enum ExceptionType {
|
||||
NoException, // Everything ok!
|
||||
SyscallException, // A program executed a system call.
|
||||
PageFaultException, // No valid translation found
|
||||
ReadOnlyException, // Write attempted to page marked
|
||||
// "read-only"
|
||||
BusErrorException, // Translation resulted in an
|
||||
// invalid physical address
|
||||
AddressErrorException, // Unaligned reference or one that
|
||||
// was beyond the end of the
|
||||
// address space
|
||||
OverflowException, // Integer overflow in add or sub.
|
||||
IllegalInstrException, // Unimplemented or reserved instr.
|
||||
MemoryLimitException, // Bad allocation
|
||||
enum ExceptionType { NoException, // Everything ok!
|
||||
SyscallException, // A program executed a system call.
|
||||
PageFaultException, // No valid translation found
|
||||
ReadOnlyException, // Write attempted to page marked
|
||||
// "read-only"
|
||||
BusErrorException, // Translation resulted in an
|
||||
// invalid physical address
|
||||
AddressErrorException, // Unaligned reference or one that
|
||||
// was beyond the end of the
|
||||
// address space
|
||||
OverflowException, // Integer overflow in add or sub.
|
||||
IllegalInstrException, // Unimplemented or reserved instr.
|
||||
|
||||
MemoryLimitException, // Insufficient memory
|
||||
|
||||
NumExceptionTypes
|
||||
NumExceptionTypes
|
||||
};
|
||||
|
||||
// User program CPU state. The full set of MIPS registers, plus a few
|
||||
@@ -96,97 +96,97 @@ class Interrupt;
|
||||
class Machine {
|
||||
public:
|
||||
Machine(bool debug); // Initialize the simulation of the hardware
|
||||
// for running user programs
|
||||
// for running user programs
|
||||
~Machine(); // De-allocate the data structures
|
||||
|
||||
// Routines callable by the Nachos kernel
|
||||
// Routines callable by the Nachos kernel
|
||||
void Run(); // Run a user program
|
||||
|
||||
int ReadRegister(int num); // read the contents of a CPU register
|
||||
|
||||
void WriteRegister(int num, int value);
|
||||
// store a value into a CPU register
|
||||
// store a value into a CPU register
|
||||
|
||||
// Data structures accessible to the Nachos kernel -- main memory and the
|
||||
// page table/TLB.
|
||||
//
|
||||
// Note that *all* communication between the user program and the kernel
|
||||
// are in terms of these data structures (plus the CPU registers).
|
||||
// Data structures accessible to the Nachos kernel -- main memory and the
|
||||
// page table/TLB.
|
||||
//
|
||||
// Note that *all* communication between the user program and the kernel
|
||||
// are in terms of these data structures (plus the CPU registers).
|
||||
|
||||
char *mainMemory; // physical memory to store user program,
|
||||
// code and data, while executing
|
||||
// code and data, while executing
|
||||
|
||||
// NOTE: the hardware translation of virtual addresses in the user program
|
||||
// to physical addresses (relative to the beginning of "mainMemory")
|
||||
// can be controlled by one of:
|
||||
// a traditional linear page table
|
||||
// a software-loaded translation lookaside buffer (tlb) -- a cache of
|
||||
// mappings of virtual page #'s to physical page #'s
|
||||
//
|
||||
// If "tlb" is NULL, the linear page table is used
|
||||
// If "tlb" is non-NULL, the Nachos kernel is responsible for managing
|
||||
// the contents of the TLB. But the kernel can use any data structure
|
||||
// it wants (eg, segmented paging) for handling TLB cache misses.
|
||||
//
|
||||
// For simplicity, both the page table pointer and the TLB pointer are
|
||||
// public. However, while there can be multiple page tables (one per address
|
||||
// space, stored in memory), there is only one TLB (implemented in hardware).
|
||||
// Thus the TLB pointer should be considered as *read-only*, although
|
||||
// the contents of the TLB are free to be modified by the kernel software.
|
||||
// NOTE: the hardware translation of virtual addresses in the user program
|
||||
// to physical addresses (relative to the beginning of "mainMemory")
|
||||
// can be controlled by one of:
|
||||
// a traditional linear page table
|
||||
// a software-loaded translation lookaside buffer (tlb) -- a cache of
|
||||
// mappings of virtual page #'s to physical page #'s
|
||||
//
|
||||
// If "tlb" is NULL, the linear page table is used
|
||||
// If "tlb" is non-NULL, the Nachos kernel is responsible for managing
|
||||
// the contents of the TLB. But the kernel can use any data structure
|
||||
// it wants (eg, segmented paging) for handling TLB cache misses.
|
||||
//
|
||||
// For simplicity, both the page table pointer and the TLB pointer are
|
||||
// public. However, while there can be multiple page tables (one per address
|
||||
// space, stored in memory), there is only one TLB (implemented in hardware).
|
||||
// Thus the TLB pointer should be considered as *read-only*, although
|
||||
// the contents of the TLB are free to be modified by the kernel software.
|
||||
|
||||
TranslationEntry *tlb; // this pointer should be considered
|
||||
// "read-only" to Nachos kernel code
|
||||
// "read-only" to Nachos kernel code
|
||||
|
||||
TranslationEntry *pageTable;
|
||||
unsigned int pageTableSize;
|
||||
|
||||
bool ReadMem(int addr, int size, int* value);
|
||||
bool WriteMem(int addr, int size, int value);
|
||||
// Read or write 1, 2, or 4 bytes of virtual
|
||||
// memory (at addr). Return FALSE if a
|
||||
// correct translation couldn't be found.
|
||||
// Read or write 1, 2, or 4 bytes of virtual
|
||||
// memory (at addr). Return FALSE if a
|
||||
// correct translation couldn't be found.
|
||||
private:
|
||||
|
||||
// Routines internal to the machine simulation -- DO NOT call these directly
|
||||
// Routines internal to the machine simulation -- DO NOT call these directly
|
||||
void DelayedLoad(int nextReg, int nextVal);
|
||||
// Do a pending delayed load (modifying a reg)
|
||||
// Do a pending delayed load (modifying a reg)
|
||||
|
||||
void OneInstruction(Instruction *instr);
|
||||
// Run one instruction of a user program.
|
||||
|
||||
// Run one instruction of a user program.
|
||||
|
||||
|
||||
|
||||
ExceptionType Translate(int virtAddr, int* physAddr, int size,bool writing);
|
||||
// Translate an address, and check for
|
||||
// alignment. Set the use and dirty bits in
|
||||
// the translation entry appropriately,
|
||||
// and return an exception code if the
|
||||
// translation couldn't be completed.
|
||||
// Translate an address, and check for
|
||||
// alignment. Set the use and dirty bits in
|
||||
// the translation entry appropriately,
|
||||
// and return an exception code if the
|
||||
// translation couldn't be completed.
|
||||
|
||||
void RaiseException(ExceptionType which, int badVAddr);
|
||||
// Trap to the Nachos kernel, because of a
|
||||
// system call or other exception.
|
||||
// Trap to the Nachos kernel, because of a
|
||||
// system call or other exception.
|
||||
|
||||
void Debugger(); // invoke the user program debugger
|
||||
void DumpState(); // print the user CPU and memory state
|
||||
|
||||
|
||||
// Internal data structures
|
||||
// Internal data structures
|
||||
|
||||
int registers[NumTotalRegs]; // CPU registers, for executing user programs
|
||||
|
||||
bool singleStep; // drop back into the debugger after each
|
||||
// simulated instruction
|
||||
// simulated instruction
|
||||
int runUntilTime; // drop back into the debugger when simulated
|
||||
// time reaches this value
|
||||
// time reaches this value
|
||||
|
||||
friend class Interrupt; // calls DelayedLoad()
|
||||
};
|
||||
|
||||
extern void ExceptionHandler(ExceptionType which);
|
||||
// Entry point into Nachos for handling
|
||||
// user system calls and exceptions
|
||||
// Defined in exception.cc
|
||||
// Entry point into Nachos for handling
|
||||
// user system calls and exceptions
|
||||
// Defined in exception.cc
|
||||
|
||||
|
||||
// Routines for converting Words and Short Words to and from the
|
||||
|
||||
@@ -20,25 +20,25 @@
|
||||
// The fields in this class are public to make it easier to update.
|
||||
|
||||
class Statistics {
|
||||
public:
|
||||
int totalTicks; // Total time running Nachos
|
||||
int idleTicks; // Time spent idle (no threads to run)
|
||||
int systemTicks; // Time spent executing system code
|
||||
int userTicks; // Time spent executing user code
|
||||
// (this is also equal to # of
|
||||
// user instructions executed)
|
||||
public:
|
||||
int totalTicks; // Total time running Nachos
|
||||
int idleTicks; // Time spent idle (no threads to run)
|
||||
int systemTicks; // Time spent executing system code
|
||||
int userTicks; // Time spent executing user code
|
||||
// (this is also equal to # of
|
||||
// user instructions executed)
|
||||
|
||||
int numDiskReads; // number of disk read requests
|
||||
int numDiskWrites; // number of disk write requests
|
||||
int numConsoleCharsRead; // number of characters read from the keyboard
|
||||
int numConsoleCharsWritten; // number of characters written to the display
|
||||
int numPageFaults; // number of virtual memory page faults
|
||||
int numPacketsSent; // number of packets sent over the network
|
||||
int numPacketsRecvd; // number of packets received over the network
|
||||
int numDiskReads; // number of disk read requests
|
||||
int numDiskWrites; // number of disk write requests
|
||||
int numConsoleCharsRead; // number of characters read from the keyboard
|
||||
int numConsoleCharsWritten; // number of characters written to the display
|
||||
int numPageFaults; // number of virtual memory page faults
|
||||
int numPacketsSent; // number of packets sent over the network
|
||||
int numPacketsRecvd; // number of packets received over the network
|
||||
|
||||
Statistics(); // initialize everything to zero
|
||||
Statistics(); // initialize everything to zero
|
||||
|
||||
void Print(); // print collected statistics
|
||||
void Print(); // print collected statistics
|
||||
};
|
||||
|
||||
// Constants used to reflect the relative time an operation would
|
||||
@@ -49,12 +49,15 @@ class Statistics {
|
||||
// in the kernel measured by the number of calls to enable interrupts,
|
||||
// these time constants are none too exact.
|
||||
|
||||
const int UserTick = 1; // advance for each user-level instruction
|
||||
const int SystemTick = 10; // advance each time interrupts are enabled
|
||||
const int UserTick = 1; // advance for each user-level instruction
|
||||
const int SystemTick = 10; // advance each time interrupts are enabled
|
||||
const int RotationTime = 500; // time disk takes to rotate one sector
|
||||
const int SeekTime = 500; // time disk takes to seek past one track
|
||||
const int ConsoleTime = 100; // time to read or write one character
|
||||
const int NetworkTime = 100; // time to send or receive one packet
|
||||
const int TimerTicks = 100; // (average) time between timer interrupts
|
||||
const int SeekTime = 500; // time disk takes to seek past one track
|
||||
|
||||
// MP4 MODIFIED
|
||||
const int ConsoleTime = 1; // time to read or write one character
|
||||
|
||||
const int NetworkTime = 100; // time to send or receive one packet
|
||||
const int TimerTicks = 100; // (average) time between timer interrupts
|
||||
|
||||
#endif // STATS_H
|
||||
|
||||
@@ -85,38 +85,38 @@ ShortToMachine(unsigned short shortword) { return ShortToHost(shortword); }
|
||||
bool
|
||||
Machine::ReadMem(int addr, int size, int *value)
|
||||
{
|
||||
int data;
|
||||
ExceptionType exception;
|
||||
int physicalAddress;
|
||||
int data;
|
||||
ExceptionType exception;
|
||||
int physicalAddress;
|
||||
|
||||
DEBUG(dbgAddr, "Reading VA " << addr << ", size " << size);
|
||||
|
||||
exception = Translate(addr, &physicalAddress, size, FALSE);
|
||||
if (exception != NoException) {
|
||||
RaiseException(exception, addr);
|
||||
return FALSE;
|
||||
}
|
||||
switch (size) {
|
||||
case 1:
|
||||
data = mainMemory[physicalAddress];
|
||||
*value = data;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
data = *(unsigned short *) &mainMemory[physicalAddress];
|
||||
*value = ShortToHost(data);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
data = *(unsigned int *) &mainMemory[physicalAddress];
|
||||
*value = WordToHost(data);
|
||||
break;
|
||||
|
||||
DEBUG(dbgAddr, "Reading VA " << addr << ", size " << size);
|
||||
|
||||
exception = Translate(addr, &physicalAddress, size, FALSE);
|
||||
if (exception != NoException) {
|
||||
RaiseException(exception, addr);
|
||||
return FALSE;
|
||||
}
|
||||
switch (size) {
|
||||
case 1:
|
||||
data = mainMemory[physicalAddress];
|
||||
*value = data;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
data = *(unsigned short *) &mainMemory[physicalAddress];
|
||||
*value = ShortToHost(data);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
data = *(unsigned int *) &mainMemory[physicalAddress];
|
||||
*value = WordToHost(data);
|
||||
break;
|
||||
|
||||
default: ASSERT(FALSE);
|
||||
}
|
||||
|
||||
DEBUG(dbgAddr, "\tvalue read = " << *value);
|
||||
return (TRUE);
|
||||
default: ASSERT(FALSE);
|
||||
}
|
||||
|
||||
DEBUG(dbgAddr, "\tvalue read = " << *value);
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
@@ -135,35 +135,35 @@ Machine::ReadMem(int addr, int size, int *value)
|
||||
bool
|
||||
Machine::WriteMem(int addr, int size, int value)
|
||||
{
|
||||
ExceptionType exception;
|
||||
int physicalAddress;
|
||||
ExceptionType exception;
|
||||
int physicalAddress;
|
||||
|
||||
DEBUG(dbgAddr, "Writing VA " << addr << ", size " << size << ", value " << value);
|
||||
|
||||
DEBUG(dbgAddr, "Writing VA " << addr << ", size " << size << ", value " << value);
|
||||
exception = Translate(addr, &physicalAddress, size, TRUE);
|
||||
if (exception != NoException) {
|
||||
RaiseException(exception, addr);
|
||||
return FALSE;
|
||||
}
|
||||
switch (size) {
|
||||
case 1:
|
||||
mainMemory[physicalAddress] = (unsigned char) (value & 0xff);
|
||||
break;
|
||||
|
||||
exception = Translate(addr, &physicalAddress, size, TRUE);
|
||||
if (exception != NoException) {
|
||||
RaiseException(exception, addr);
|
||||
return FALSE;
|
||||
}
|
||||
switch (size) {
|
||||
case 1:
|
||||
mainMemory[physicalAddress] = (unsigned char) (value & 0xff);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
*(unsigned short *) &mainMemory[physicalAddress]
|
||||
= ShortToMachine((unsigned short) (value & 0xffff));
|
||||
break;
|
||||
|
||||
case 4:
|
||||
*(unsigned int *) &mainMemory[physicalAddress]
|
||||
= WordToMachine((unsigned int) value);
|
||||
break;
|
||||
|
||||
default: ASSERT(FALSE);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
case 2:
|
||||
*(unsigned short *) &mainMemory[physicalAddress]
|
||||
= ShortToMachine((unsigned short) (value & 0xffff));
|
||||
break;
|
||||
|
||||
case 4:
|
||||
*(unsigned int *) &mainMemory[physicalAddress]
|
||||
= WordToMachine((unsigned int) value);
|
||||
break;
|
||||
|
||||
default: ASSERT(FALSE);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
@@ -184,75 +184,67 @@ Machine::WriteMem(int addr, int size, int value)
|
||||
ExceptionType
|
||||
Machine::Translate(int virtAddr, int* physAddr, int size, bool writing)
|
||||
{
|
||||
int i;
|
||||
unsigned int vpn, offset;
|
||||
TranslationEntry *entry;
|
||||
unsigned int pageFrame;
|
||||
int i;
|
||||
unsigned int vpn, offset;
|
||||
TranslationEntry *entry;
|
||||
unsigned int pageFrame;
|
||||
|
||||
DEBUG(dbgAddr, "\tTranslate " << virtAddr << (writing ? " , write" : " , read"));
|
||||
DEBUG(dbgAddr, "\tTranslate " << virtAddr << (writing ? " , write" : " , read"));
|
||||
|
||||
// check for alignment errors
|
||||
if (((size == 4) && (virtAddr & 0x3)) || ((size == 2) && (virtAddr & 0x1))){
|
||||
DEBUG(dbgAddr, "Alignment problem at " << virtAddr << ", size " << size);
|
||||
return AddressErrorException;
|
||||
}
|
||||
// we must have either a TLB or a page table, but not both!
|
||||
ASSERT(tlb == NULL || pageTable == NULL);
|
||||
ASSERT(tlb != NULL || pageTable != NULL);
|
||||
|
||||
// calculate the virtual page number, and offset within the page,
|
||||
// from the virtual address
|
||||
vpn = (unsigned) virtAddr / PageSize;
|
||||
offset = (unsigned) virtAddr % PageSize;
|
||||
|
||||
if (tlb == NULL) { // => page table => vpn is index into table
|
||||
if (vpn >= pageTableSize) {
|
||||
DEBUG(dbgAddr, "Illegal virtual page # " << virtAddr);
|
||||
DEBUG(dbgAddr, "vpn, pageTableSize, NumPhysPages: " << vpn << ' ' << pageTableSize << ' ' << NumPhysPages);
|
||||
return AddressErrorException;
|
||||
} else if (!pageTable[vpn].valid) {
|
||||
DEBUG(dbgAddr, "Invalid virtual page # " << virtAddr);
|
||||
return PageFaultException;
|
||||
// check for alignment errors
|
||||
if (((size == 4) && (virtAddr & 0x3)) || ((size == 2) && (virtAddr & 0x1))){
|
||||
DEBUG(dbgAddr, "Alignment problem at " << virtAddr << ", size " << size);
|
||||
return AddressErrorException;
|
||||
}
|
||||
entry = &pageTable[vpn];
|
||||
} else {
|
||||
for (entry = NULL, i = 0; i < TLBSize; i++)
|
||||
if (tlb[i].valid && (tlb[i].virtualPage == ((int)vpn))) {
|
||||
entry = &tlb[i]; // FOUND!
|
||||
break;
|
||||
}
|
||||
if (entry == NULL) { // not found
|
||||
DEBUG(dbgAddr, "Invalid TLB entry for this virtual page!");
|
||||
return PageFaultException; // really, this is a TLB fault,
|
||||
// the page may be in memory,
|
||||
// but not in the TLB
|
||||
}
|
||||
}
|
||||
// we must have either a TLB or a page table, but not both!
|
||||
ASSERT(tlb == NULL || pageTable == NULL);
|
||||
ASSERT(tlb != NULL || pageTable != NULL);
|
||||
|
||||
if (entry->readOnly && writing) { // trying to write to a read-only page
|
||||
DEBUG(dbgAddr, "Write to read-only page at " << virtAddr);
|
||||
return ReadOnlyException;
|
||||
}
|
||||
pageFrame = entry->physicalPage;
|
||||
if (pageFrame == -1) {
|
||||
pageFrame = entry->physicalPage = kernel->frameTable->Allocate();
|
||||
if (pageFrame == -1) {
|
||||
DEBUG(dbgAddr, "Memory Limit exceeded");
|
||||
return MemoryLimitException;
|
||||
// calculate the virtual page number, and offset within the page,
|
||||
// from the virtual address
|
||||
vpn = (unsigned) virtAddr / PageSize;
|
||||
offset = (unsigned) virtAddr % PageSize;
|
||||
|
||||
if (tlb == NULL) { // => page table => vpn is index into table
|
||||
if (vpn >= pageTableSize) {
|
||||
DEBUG(dbgAddr, "Illegal virtual page # " << virtAddr);
|
||||
return AddressErrorException;
|
||||
} else if (!pageTable[vpn].valid) {
|
||||
DEBUG(dbgAddr, "Invalid virtual page # " << virtAddr);
|
||||
return PageFaultException;
|
||||
}
|
||||
entry = &pageTable[vpn];
|
||||
} else {
|
||||
for (entry = NULL, i = 0; i < TLBSize; i++)
|
||||
if (tlb[i].valid && (tlb[i].virtualPage == ((int)vpn))) {
|
||||
entry = &tlb[i]; // FOUND!
|
||||
break;
|
||||
}
|
||||
if (entry == NULL) { // not found
|
||||
DEBUG(dbgAddr, "Invalid TLB entry for this virtual page!");
|
||||
return PageFaultException; // really, this is a TLB fault,
|
||||
// the page may be in memory,
|
||||
// but not in the TLB
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if the pageFrame is too big, there is something really wrong!
|
||||
// An invalid translation was loaded into the page table or TLB.
|
||||
if (pageFrame >= NumPhysPages) {
|
||||
DEBUG(dbgAddr, "Illegal pageframe " << pageFrame);
|
||||
return BusErrorException;
|
||||
}
|
||||
entry->use = TRUE; // set the use, dirty bits
|
||||
if (writing)
|
||||
entry->dirty = TRUE;
|
||||
*physAddr = pageFrame * PageSize + offset;
|
||||
ASSERT((*physAddr >= 0) && ((*physAddr + size) <= MemorySize));
|
||||
DEBUG(dbgAddr, "phys addr = " << *physAddr);
|
||||
return NoException;
|
||||
if (entry->readOnly && writing) { // trying to write to a read-only page
|
||||
DEBUG(dbgAddr, "Write to read-only page at " << virtAddr);
|
||||
return ReadOnlyException;
|
||||
}
|
||||
pageFrame = entry->physicalPage;
|
||||
|
||||
// if the pageFrame is too big, there is something really wrong!
|
||||
// An invalid translation was loaded into the page table or TLB.
|
||||
if (pageFrame >= NumPhysPages) {
|
||||
DEBUG(dbgAddr, "Illegal pageframe " << pageFrame);
|
||||
return BusErrorException;
|
||||
}
|
||||
entry->use = TRUE; // set the use, dirty bits
|
||||
if (writing)
|
||||
entry->dirty = TRUE;
|
||||
*physAddr = pageFrame * PageSize + offset;
|
||||
ASSERT((*physAddr >= 0) && ((*physAddr + size) <= MemorySize));
|
||||
DEBUG(dbgAddr, "phys addr = " << *physAddr);
|
||||
return NoException;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user