tac0S
Template Affectional Command Operating System
machine.h
Go to the documentation of this file.
1 
20 #ifndef MACHINE_H
21 #define MACHINE_H
22 
23 #include "copyright.h"
24 #include "utility.h"
25 #include "translate.h"
26 #include "disk.h"
27 
28 // Definitions related to the size, and format of user memory
29 
30 #define PageSize SectorSize // set the page size equal to
31  // the disk sector size, for
32  // simplicity
33 
34 #define NumPhysPages 32
35 #define MemorySize (NumPhysPages * PageSize)
36 #define TLBSize 4 // if there is a TLB, make it small
37 
38 enum ExceptionType { NoException, // Everything ok!
39  SyscallException, // A program executed a system call.
40  PageFaultException, // No valid translation found
41  ReadOnlyException, // Write attempted to page marked
42  // "read-only"
43  BusErrorException, // Translation resulted in an
44  // invalid physical address
45  AddressErrorException, // Unaligned reference or one that
46  // was beyond the end of the
47  // address space
48  OverflowException, // Integer overflow in add or sub.
49  IllegalInstrException, // Unimplemented or reserved instr.
50 
51  NumExceptionTypes,
52  CharInsteadOfInt, // L'utilisateur a saisi un caractère lors de la saisie d'entier
53  IntOutOfBounds // |int| > 10^10-1
54 };
55 
56 // User program CPU state. The full set of MIPS registers, plus a few
57 // more because we need to be able to start/stop a user program between
58 // any two instructions (thus we need to keep track of things like load
59 // delay slots, etc.)
60 
61 #define StackReg 29 // User's stack pointer
62 #define RetAddrReg 31 // Holds return address for procedure calls
63 #define NumGPRegs 32 // 32 general purpose registers on MIPS
64 #define HiReg 32 // Double register to hold multiply result
65 #define LoReg 33
66 #define PCReg 34 // Current program counter
67 #define NextPCReg 35 // Next program counter (for branch delay)
68 #define PrevPCReg 36 // Previous program counter (for debugging)
69 #define LoadReg 37 // The register target of a delayed load.
70 #define LoadValueReg 38 // The value to be loaded by a delayed load.
71 #define BadVAddrReg 39 // The failing virtual address on an exception
72 
73 #define NumTotalRegs 40
74 
81 
82 class Instruction {
83  public:
84  void Decode(); // decode the binary representation of the instruction
85 
86  unsigned int value; // binary representation of the instruction
87 
88  unsigned char opCode; // Type of instruction. This is NOT the same as the
89  // opcode field from the instruction: see defs in mips.h
90  unsigned char rs, rt, rd; // Three registers from instruction.
91  int extra; // Immediate or target or shamt field or offset.
92  // Immediates are sign-extended.
93 };
94 
107 
108 class Machine {
109  public:
110  Machine(bool debug); // Initialize the simulation of the hardware
111  // for running user programs
112  ~Machine(); // De-allocate the data structures
113 
114 // Routines callable by the Nachos kernel
115  void Run(void * fExit); // Run a user program
116 
117  int ReadRegister(int num); // read the contents of a CPU register
118 
119  void WriteRegister(int num, int value);
120  // store a value into a CPU register
121 
122 
123 // Routines internal to the machine simulation -- DO NOT call these
124 
125  void OneInstruction(Instruction *instr);
126  // Run one instruction of a user program.
127  void DelayedLoad(int nextReg, int nextVal);
128  // Do a pending delayed load (modifying a reg)
129 
130  bool ReadMem(int addr, int size, int* value);
131  bool WriteMem(int addr, int size, int value);
132  // Read or write 1, 2, or 4 bytes of virtual
133  // memory (at addr). Return FALSE if a
134  // correct translation couldn't be found.
135 
136  ExceptionType Translate(int virtAddr, int* physAddr, int size,bool writing);
137  // Translate an address, and check for
138  // alignment. Set the use and dirty bits in
139  // the translation entry appropriately,
140  // and return an exception code if the
141  // translation couldn't be completed.
142 
143  void RaiseException(ExceptionType which, int badVAddr);
144  // Trap to the Nachos kernel, because of a
145  // system call or other exception.
146 
147  void Debugger(); // invoke the user program debugger
148  void DumpState(); // print the user CPU and memory state
149 
150 
151 // Data structures -- all of these are accessible to Nachos kernel code.
152 // "public" for convenience.
153 //
154 // Note that *all* communication between the user program and the kernel
155 // are in terms of these data structures.
156 
157  char *mainMemory; // physical memory to store user program,
158  // code and data, while executing
159  int registers[NumTotalRegs]; // CPU registers, for executing user programs
160 
161 
162 // NOTE: the hardware translation of virtual addresses in the user program
163 // to physical addresses (relative to the beginning of "mainMemory")
164 // can be controlled by one of:
165 // a traditional linear page table
166 // a software-loaded translation lookaside buffer (tlb) -- a cache of
167 // mappings of virtual page #'s to physical page #'s
168 //
169 // If "tlb" is NULL, the linear page table is used
170 // If "tlb" is non-NULL, the Nachos kernel is responsible for managing
171 // the contents of the TLB. But the kernel can use any data structure
172 // it wants (eg, segmented paging) for handling TLB cache misses.
173 //
174 // For simplicity, both the page table pointer and the TLB pointer are
175 // public. However, while there can be multiple page tables (one per address
176 // space, stored in memory), there is only one TLB (implemented in hardware).
177 // Thus the TLB pointer should be considered as *read-only*, although
178 // the contents of the TLB are free to be modified by the kernel software.
179 
180  TranslationEntry *tlb; // this pointer should be considered
181  // "read-only" to Nachos kernel code
182 
183  TranslationEntry *pageTable;
184  unsigned int pageTableSize;
185 
186  private:
187  bool singleStep; // drop back into the debugger after each
188  // simulated instruction
189  int runUntilTime; // drop back into the debugger when simulated
190  // time reaches this value
191 };
192 
193 extern void ExceptionHandler(ExceptionType which);
194  // Entry point into Nachos for handling
195  // user system calls and exceptions
196  // Defined in exception.cc
197 
198 
199 // Routines for converting Words and Short Words to and from the
200 // simulated machine's format of little endian. If the host machine
201 // is little endian (DEC and Intel), these end up being NOPs.
202 //
203 // What is stored in each format:
204 // host byte ordering:
205 // kernel data structures
206 // user registers
207 // simulated machine byte ordering:
208 // contents of main memory
209 
210 unsigned int WordToHost(unsigned int word);
211 unsigned short ShortToHost(unsigned short shortword);
212 unsigned int WordToMachine(unsigned int word);
213 unsigned short ShortToMachine(unsigned short shortword);
214 
215 #endif // MACHINE_H
void Decode()
Instruction::Decode Decode a MIPS instruction.
Definition: mipssim.cc:647
Data structures for managing the translation.
The following class defines the simulated host workstation hardware, as seen by user programs – the ...
Definition: machine.h:108
The following class defines an entry in a translation table – either in a page table or a TLB...
Definition: translate.h:33
The following class defines an instruction, represented in both undecoded binary form decoded to iden...
Definition: machine.h:82
Miscellaneous useful definitions, including debugging routines.
void ExceptionHandler(ExceptionType which)
ExceptionHandler Entry point into the Nachos kernel. Called when a user program is executing...
Definition: exception.cc:386
unsigned int WordToHost(unsigned int word)
Routines for converting Words and Short Words to and from the simulated machine's format of little en...
Definition: coff2noff.c:45
Data structures to emulate a physical disk