2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef __CPU_BASE_DYN_INST_HH__
30 #define __CPU_BASE_DYN_INST_HH__
35 #include "base/fast_alloc.hh"
36 #include "base/trace.hh"
37 #include "config/full_system.hh"
38 #include "cpu/exetrace.hh"
39 #include "cpu/inst_seq.hh"
40 #include "cpu/o3/comm.hh"
41 #include "cpu/static_inst.hh"
42 #include "encumbered/cpu/full/bpred_update.hh"
43 #include "encumbered/cpu/full/op_class.hh"
44 #include "encumbered/cpu/full/spec_memory.hh"
45 #include "encumbered/cpu/full/spec_state.hh"
46 #include "encumbered/mem/functional/main.hh"
50 * Defines a dynamic instruction context.
53 // Forward declaration.
58 class BaseDynInst : public FastAlloc, public RefCounted
61 // Typedef for the CPU.
62 typedef typename Impl::FullCPU FullCPU;
64 //Typedef to get the ISA.
65 typedef typename Impl::ISA ISA;
67 /// Binary machine instruction type.
68 typedef typename ISA::MachInst MachInst;
69 /// Memory address type.
70 typedef typename ISA::Addr Addr;
71 /// Logical register index type.
72 typedef typename ISA::RegIndex RegIndex;
73 /// Integer register index type.
74 typedef typename ISA::IntReg IntReg;
77 MaxInstSrcRegs = ISA::MaxInstSrcRegs, //< Max source regs
78 MaxInstDestRegs = ISA::MaxInstDestRegs, //< Max dest regs
81 /** The static inst used by this dyn inst. */
82 StaticInstPtr<ISA> staticInst;
84 ////////////////////////////////////////////
86 // INSTRUCTION EXECUTION
88 ////////////////////////////////////////////
89 Trace::InstRecord *traceData;
92 Fault read(Addr addr, T &data, unsigned flags);
95 Fault write(T data, Addr addr, unsigned flags,
98 void prefetch(Addr addr, unsigned flags);
99 void writeHint(Addr addr, int size, unsigned flags);
100 Fault copySrcTranslate(Addr src);
101 Fault copy(Addr dest);
103 /** @todo: Consider making this private. */
105 /** Is this instruction valid. */
108 /** The sequence number of the instruction. */
111 /** How many source registers are ready. */
114 /** Is the instruction completed. */
117 /** Can this instruction issue. */
120 /** Has this instruction issued. */
123 /** Has this instruction executed (or made it through execute) yet. */
126 /** Can this instruction commit. */
129 /** Is this instruction squashed. */
132 /** Is this instruction squashed in the instruction queue. */
135 /** Is this a recover instruction. */
138 /** Is this a thread blocking instruction. */
139 bool blockingInst; /* this inst has called thread_block() */
141 /** Is this a thread syncrhonization instruction. */
144 /** The thread this instruction is from. */
147 /** data address space ID, for loads & stores. */
150 /** Pointer to the FullCPU object. */
153 /** Pointer to the exec context. Will not exist in the final version. */
156 /** The kind of fault this instruction has generated. */
159 /** The effective virtual address (lds & stores only). */
162 /** The effective physical address. */
165 /** Effective virtual address for a copy source. */
168 /** Effective physical address for a copy source. */
169 Addr copySrcPhysEffAddr;
171 /** The memory request flags (from translation). */
172 unsigned memReqFlags;
174 /** The size of the data to be stored. */
177 /** The data to be stored. */
186 /** The result of the instruction; assumes for now that there's only one
187 * destination register.
191 /** PC of this instruction. */
194 /** Next non-speculative PC. It is not filled in at fetch, but rather
195 * once the target of the branch is truly known (either decode or
200 /** Predicted next PC. */
203 /** Count of total number of dynamic instructions. */
204 static int instcount;
206 /** Whether or not the source register is ready. Not sure this should be
207 * here vs. the derived class.
209 bool _readySrcRegIdx[MaxInstSrcRegs];
212 /** BaseDynInst constructor given a binary instruction. */
213 BaseDynInst(MachInst inst, Addr PC, Addr Pred_PC, InstSeqNum seq_num,
216 /** BaseDynInst constructor given a static inst pointer. */
217 BaseDynInst(StaticInstPtr<ISA> &_staticInst);
219 /** BaseDynInst destructor. */
223 /** Function to initialize variables in the constructors. */
228 trace_mem(Fault fault, // last fault
229 MemCmd cmd, // last command
230 Addr addr, // virtual address of access
231 void *p, // memory accessed
232 int nbytes); // access size
234 /** Dumps out contents of this BaseDynInst. */
237 /** Dumps out contents of this BaseDynInst into given string. */
238 void dump(std::string &outstring);
240 /** Returns the fault type. */
241 Fault getFault() { return fault; }
243 /** Checks whether or not this instruction has had its branch target
244 * calculated yet. For now it is not utilized and is hacked to be
247 bool doneTargCalc() { return false; }
249 /** Returns the next PC. This could be the speculative next PC if it is
250 * called prior to the actual branch target being calculated.
252 Addr readNextPC() { return nextPC; }
254 /** Set the predicted target of this current instruction. */
255 void setPredTarg(Addr predicted_PC) { predPC = predicted_PC; }
257 /** Returns the predicted target of the branch. */
258 Addr readPredTarg() { return predPC; }
260 /** Returns whether the instruction was predicted taken or not. */
262 return( predPC != (PC + sizeof(MachInst) ) );
265 /** Returns whether the instruction mispredicted. */
266 bool mispredicted() { return (predPC != nextPC); }
269 // Instruction types. Forward checks to StaticInst object.
271 bool isNop() const { return staticInst->isNop(); }
272 bool isMemRef() const { return staticInst->isMemRef(); }
273 bool isLoad() const { return staticInst->isLoad(); }
274 bool isStore() const { return staticInst->isStore(); }
275 bool isInstPrefetch() const { return staticInst->isInstPrefetch(); }
276 bool isDataPrefetch() const { return staticInst->isDataPrefetch(); }
277 bool isCopy() const { return staticInst->isCopy(); }
278 bool isInteger() const { return staticInst->isInteger(); }
279 bool isFloating() const { return staticInst->isFloating(); }
280 bool isControl() const { return staticInst->isControl(); }
281 bool isCall() const { return staticInst->isCall(); }
282 bool isReturn() const { return staticInst->isReturn(); }
283 bool isDirectCtrl() const { return staticInst->isDirectCtrl(); }
284 bool isIndirectCtrl() const { return staticInst->isIndirectCtrl(); }
285 bool isCondCtrl() const { return staticInst->isCondCtrl(); }
286 bool isUncondCtrl() const { return staticInst->isUncondCtrl(); }
287 bool isThreadSync() const { return staticInst->isThreadSync(); }
288 bool isSerializing() const { return staticInst->isSerializing(); }
289 bool isMemBarrier() const { return staticInst->isMemBarrier(); }
290 bool isWriteBarrier() const { return staticInst->isWriteBarrier(); }
291 bool isNonSpeculative() const { return staticInst->isNonSpeculative(); }
293 /** Returns the opclass of this instruction. */
294 OpClass opClass() const { return staticInst->opClass(); }
296 /** Returns the branch target address. */
297 Addr branchTarget() const { return staticInst->branchTarget(PC); }
299 /** Number of source registers. */
300 int8_t numSrcRegs() const { return staticInst->numSrcRegs(); }
302 /** Number of destination registers. */
303 int8_t numDestRegs() const { return staticInst->numDestRegs(); }
305 // the following are used to track physical register usage
306 // for machines with separate int & FP reg files
307 int8_t numFPDestRegs() const { return staticInst->numFPDestRegs(); }
308 int8_t numIntDestRegs() const { return staticInst->numIntDestRegs(); }
310 /** Returns the logical register index of the i'th destination register. */
311 RegIndex destRegIdx(int i) const
313 return staticInst->destRegIdx(i);
316 /** Returns the logical register index of the i'th source register. */
317 RegIndex srcRegIdx(int i) const
319 return staticInst->srcRegIdx(i);
322 /** Returns the result of an integer instruction. */
323 uint64_t readIntResult() { return instResult.integer; }
325 /** Returns the result of a floating point instruction. */
326 float readFloatResult() { return instResult.fp; }
328 /** Returns the result of a floating point (double) instruction. */
329 double readDoubleResult() { return instResult.dbl; }
332 /** Records that one of the source registers is ready. */
333 void markSrcRegReady()
336 if(readyRegs == numSrcRegs()) {
341 /** Marks a specific register as ready.
342 * @todo: Move this to .cc file.
344 void markSrcRegReady(RegIndex src_idx)
348 _readySrcRegIdx[src_idx] = 1;
350 if(readyRegs == numSrcRegs()) {
355 /** Returns if a source register is ready. */
356 bool isReadySrcRegIdx(int idx) const
358 return this->_readySrcRegIdx[idx];
361 /** Sets this instruction as completed. */
362 void setCompleted() { completed = true; }
364 /** Returns whethe or not this instruction is completed. */
365 bool isCompleted() const { return completed; }
367 /** Sets this instruction as ready to issue. */
368 void setCanIssue() { canIssue = true; }
370 /** Returns whether or not this instruction is ready to issue. */
371 bool readyToIssue() const { return canIssue; }
373 /** Sets this instruction as issued from the IQ. */
374 void setIssued() { issued = true; }
376 /** Returns whether or not this instruction has issued. */
377 bool isIssued() const { return issued; }
379 /** Sets this instruction as executed. */
380 void setExecuted() { executed = true; }
382 /** Returns whether or not this instruction has executed. */
383 bool isExecuted() const { return executed; }
385 /** Sets this instruction as ready to commit. */
386 void setCanCommit() { canCommit = true; }
388 /** Clears this instruction as being ready to commit. */
389 void clearCanCommit() { canCommit = false; }
391 /** Returns whether or not this instruction is ready to commit. */
392 bool readyToCommit() const { return canCommit; }
394 /** Sets this instruction as squashed. */
395 void setSquashed() { squashed = true; }
397 /** Returns whether or not this instruction is squashed. */
398 bool isSquashed() const { return squashed; }
400 /** Sets this instruction as squashed in the IQ. */
401 void setSquashedInIQ() { squashedInIQ = true; }
403 /** Returns whether or not this instruction is squashed in the IQ. */
404 bool isSquashedInIQ() const { return squashedInIQ; }
406 /** Read the PC of this instruction. */
407 const Addr readPC() const { return PC; }
409 /** Set the next PC of this instruction (its actual target). */
410 void setNextPC(uint64_t val) { nextPC = val; }
412 /** Returns the exec context.
413 * @todo: Remove this once the ExecContext is no longer used.
415 ExecContext *xcBase() { return xc; }
418 /** Instruction effective address.
419 * @todo: Consider if this is necessary or not.
422 /** Whether or not the effective address calculation is completed.
423 * @todo: Consider if this is necessary or not.
428 /** Sets the effective address. */
429 void setEA(Addr &ea) { instEffAddr = ea; eaCalcDone = true; }
431 /** Returns the effective address. */
432 const Addr &getEA() const { return instEffAddr; }
434 /** Returns whether or not the eff. addr. calculation has been completed. */
435 bool doneEACalc() { return eaCalcDone; }
437 /** Returns whether or not the eff. addr. source registers are ready. */
441 /** Load queue index. */
444 /** Store queue index. */
451 BaseDynInst<Impl>::read(Addr addr, T &data, unsigned flags)
453 MemReqPtr req = new MemReq(addr, xc, sizeof(T), flags);
456 fault = cpu->translateDataReadReq(req);
458 // Record key MemReq parameters so we can generate another one
459 // just like it for the timing access without calling translate()
460 // again (which might mess up the TLB).
461 // Do I ever really need this? -KTL 3/05
462 effAddr = req->vaddr;
463 physEffAddr = req->paddr;
464 memReqFlags = req->flags;
468 * Replace the disjoint functional memory with a unified one and remove
472 req->paddr = req->vaddr;
475 if (fault == No_Fault) {
476 fault = cpu->read(req, data, lqIdx);
478 // Return a fixed value to keep simulation deterministic even
479 // along misspeculated paths.
484 traceData->setAddr(addr);
485 traceData->setData(data);
494 BaseDynInst<Impl>::write(T data, Addr addr, unsigned flags, uint64_t *res)
497 traceData->setAddr(addr);
498 traceData->setData(data);
501 MemReqPtr req = new MemReq(addr, xc, sizeof(T), flags);
505 fault = cpu->translateDataWriteReq(req);
507 // Record key MemReq parameters so we can generate another one
508 // just like it for the timing access without calling translate()
509 // again (which might mess up the TLB).
510 effAddr = req->vaddr;
511 physEffAddr = req->paddr;
512 memReqFlags = req->flags;
516 * Replace the disjoint functional memory with a unified one and remove
520 req->paddr = req->vaddr;
523 if (fault == No_Fault) {
524 fault = cpu->write(req, data, sqIdx);
528 // always return some result to keep misspeculated paths
529 // (which will ignore faults) deterministic
530 *res = (fault == No_Fault) ? req->result : 0;
536 #endif // __CPU_BASE_DYN_INST_HH__