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/static_inst.hh"
41 #include "encumbered/cpu/full/op_class.hh"
42 #include "mem/functional/memory_control.hh"
43 #include "sim/system.hh"
45 #include "encumbered/cpu/full/bpred_update.hh"
46 #include "encumbered/cpu/full/spec_memory.hh"
47 #include "encumbered/cpu/full/spec_state.hh"
48 #include "encumbered/mem/functional/main.hh"
53 * Defines a dynamic instruction context.
56 // Forward declaration.
60 class BaseDynInst : public FastAlloc, public RefCounted
63 // Typedef for the CPU.
64 typedef typename Impl::FullCPU FullCPU;
65 typedef typename FullCPU::ImplState ImplState;
67 // Binary machine instruction type.
68 typedef TheISA::MachInst MachInst;
69 // Extended machine instruction type
70 typedef TheISA::ExtMachInst ExtMachInst;
71 // Logical register index type.
72 typedef TheISA::RegIndex RegIndex;
73 // Integer register index type.
74 typedef TheISA::IntReg IntReg;
76 // The DynInstPtr type.
77 typedef typename Impl::DynInstPtr DynInstPtr;
79 // The list of instructions iterator type.
80 typedef typename std::list<DynInstPtr>::iterator ListIt;
83 MaxInstSrcRegs = TheISA::MaxInstSrcRegs, /// Max source regs
84 MaxInstDestRegs = TheISA::MaxInstDestRegs, /// Max dest regs
87 /** The StaticInst used by this BaseDynInst. */
88 StaticInstPtr staticInst;
90 ////////////////////////////////////////////
92 // INSTRUCTION EXECUTION
94 ////////////////////////////////////////////
95 /** InstRecord that tracks this instructions. */
96 Trace::InstRecord *traceData;
99 * Does a read to a given address.
100 * @param addr The address to read.
101 * @param data The read's data is written into this parameter.
102 * @param flags The request's flags.
103 * @return Returns any fault due to the read.
106 Fault read(Addr addr, T &data, unsigned flags);
109 * Does a write to a given address.
110 * @param data The data to be written.
111 * @param addr The address to write to.
112 * @param flags The request's flags.
113 * @param res The result of the write (for load locked/store conditionals).
114 * @return Returns any fault due to the write.
117 Fault write(T data, Addr addr, unsigned flags,
120 void prefetch(Addr addr, unsigned flags);
121 void writeHint(Addr addr, int size, unsigned flags);
122 Fault copySrcTranslate(Addr src);
123 Fault copy(Addr dest);
125 /** @todo: Consider making this private. */
127 /** The sequence number of the instruction. */
130 /** Is the instruction in the IQ */
133 /** Is the instruction in the ROB */
136 /** Is the instruction in the LSQ */
139 /** Is the instruction completed. */
142 /** Can this instruction issue. */
145 /** Has this instruction issued. */
148 /** Has this instruction executed (or made it through execute) yet. */
151 /** Can this instruction commit. */
154 /** Is this instruction committed. */
157 /** Is this instruction squashed. */
160 /** Is this instruction squashed in the instruction queue. */
163 /** Is this instruction squashed in the instruction queue. */
166 /** Is this instruction squashed in the instruction queue. */
169 /** Is this a recover instruction. */
172 /** Is this a thread blocking instruction. */
173 bool blockingInst; /* this inst has called thread_block() */
175 /** Is this a thread syncrhonization instruction. */
178 /** The thread this instruction is from. */
181 /** data address space ID, for loads & stores. */
184 /** How many source registers are ready. */
187 /** Pointer to the FullCPU object. */
190 /** Pointer to the exec context. Will not exist in the final version. */
193 /** The kind of fault this instruction has generated. */
196 /** The memory request. */
199 /** The effective virtual address (lds & stores only). */
202 /** The effective physical address. */
205 /** Effective virtual address for a copy source. */
208 /** Effective physical address for a copy source. */
209 Addr copySrcPhysEffAddr;
211 /** The memory request flags (from translation). */
212 unsigned memReqFlags;
214 /** The size of the data to be stored. */
217 /** The data to be stored. */
226 /** The result of the instruction; assumes for now that there's only one
227 * destination register.
231 /** PC of this instruction. */
234 /** Next non-speculative PC. It is not filled in at fetch, but rather
235 * once the target of the branch is truly known (either decode or
240 /** Predicted next PC. */
243 /** Count of total number of dynamic instructions. */
244 static int instcount;
250 /** Whether or not the source register is ready.
251 * @todo: Not sure this should be here vs the derived class.
253 bool _readySrcRegIdx[MaxInstSrcRegs];
256 /** BaseDynInst constructor given a binary instruction.
257 * @param inst The binary instruction.
258 * @param PC The PC of the instruction.
259 * @param pred_PC The predicted next PC.
260 * @param seq_num The sequence number of the instruction.
261 * @param cpu Pointer to the instruction's CPU.
263 BaseDynInst(ExtMachInst inst, Addr PC, Addr pred_PC, InstSeqNum seq_num,
266 /** BaseDynInst constructor given a StaticInst pointer.
267 * @param _staticInst The StaticInst for this BaseDynInst.
269 BaseDynInst(StaticInstPtr &_staticInst);
271 /** BaseDynInst destructor. */
275 /** Function to initialize variables in the constructors. */
280 * @todo: Make this function work; currently it is a dummy function.
281 * @param fault Last fault.
282 * @param cmd Last command.
283 * @param addr Virtual address of access.
284 * @param p Memory accessed.
285 * @param nbytes Access size.
288 trace_mem(Fault fault,
294 /** Dumps out contents of this BaseDynInst. */
297 /** Dumps out contents of this BaseDynInst into given string. */
298 void dump(std::string &outstring);
300 /** Returns the fault type. */
301 Fault getFault() { return fault; }
303 /** Checks whether or not this instruction has had its branch target
304 * calculated yet. For now it is not utilized and is hacked to be
306 * @todo: Actually use this instruction.
308 bool doneTargCalc() { return false; }
310 /** Returns the next PC. This could be the speculative next PC if it is
311 * called prior to the actual branch target being calculated.
313 Addr readNextPC() { return nextPC; }
315 /** Set the predicted target of this current instruction. */
316 void setPredTarg(Addr predicted_PC) { predPC = predicted_PC; }
318 /** Returns the predicted target of the branch. */
319 Addr readPredTarg() { return predPC; }
321 /** Returns whether the instruction was predicted taken or not. */
322 bool predTaken() { return predPC != (PC + sizeof(MachInst)); }
324 /** Returns whether the instruction mispredicted. */
325 bool mispredicted() { return predPC != nextPC; }
328 // Instruction types. Forward checks to StaticInst object.
330 bool isNop() const { return staticInst->isNop(); }
331 bool isMemRef() const { return staticInst->isMemRef(); }
332 bool isLoad() const { return staticInst->isLoad(); }
333 bool isStore() const { return staticInst->isStore(); }
334 bool isInstPrefetch() const { return staticInst->isInstPrefetch(); }
335 bool isDataPrefetch() const { return staticInst->isDataPrefetch(); }
336 bool isCopy() const { return staticInst->isCopy(); }
337 bool isInteger() const { return staticInst->isInteger(); }
338 bool isFloating() const { return staticInst->isFloating(); }
339 bool isControl() const { return staticInst->isControl(); }
340 bool isCall() const { return staticInst->isCall(); }
341 bool isReturn() const { return staticInst->isReturn(); }
342 bool isDirectCtrl() const { return staticInst->isDirectCtrl(); }
343 bool isIndirectCtrl() const { return staticInst->isIndirectCtrl(); }
344 bool isCondCtrl() const { return staticInst->isCondCtrl(); }
345 bool isUncondCtrl() const { return staticInst->isUncondCtrl(); }
346 bool isThreadSync() const { return staticInst->isThreadSync(); }
347 bool isSerializing() const { return staticInst->isSerializing(); }
348 bool isSerializeBefore() const
349 { return staticInst->isSerializeBefore() || serializeBefore; }
350 bool isSerializeAfter() const
351 { return staticInst->isSerializeAfter() || serializeAfter; }
352 bool isMemBarrier() const { return staticInst->isMemBarrier(); }
353 bool isWriteBarrier() const { return staticInst->isWriteBarrier(); }
354 bool isNonSpeculative() const { return staticInst->isNonSpeculative(); }
355 bool isQuiesce() const { return staticInst->isQuiesce(); }
357 /** Temporarily sets this instruction as a serialize before instruction. */
358 void setSerializeBefore() { serializeBefore = true; }
360 /** Clears the serializeBefore part of this instruction. */
361 void clearSerializeBefore() { serializeBefore = false; }
363 /** Checks if this serializeBefore is only temporarily set. */
364 bool isTempSerializeBefore() { return serializeBefore; }
366 /** Tracks if instruction has been externally set as serializeBefore. */
367 bool serializeBefore;
369 /** Temporarily sets this instruction as a serialize after instruction. */
370 void setSerializeAfter() { serializeAfter = true; }
372 /** Clears the serializeAfter part of this instruction.*/
373 void clearSerializeAfter() { serializeAfter = false; }
375 /** Checks if this serializeAfter is only temporarily set. */
376 bool isTempSerializeAfter() { return serializeAfter; }
378 /** Tracks if instruction has been externally set as serializeAfter. */
381 /** Checks if the serialization part of this instruction has been
382 * handled. This does not apply to the temporary serializing
383 * state; it only applies to this instruction's own permanent
386 bool isSerializeHandled() { return serializeHandled; }
388 /** Sets the serialization part of this instruction as handled. */
389 void setSerializeHandled() { serializeHandled = true; }
391 /** Whether or not the serialization of this instruction has been handled. */
392 bool serializeHandled;
394 /** Returns the opclass of this instruction. */
395 OpClass opClass() const { return staticInst->opClass(); }
397 /** Returns the branch target address. */
398 Addr branchTarget() const { return staticInst->branchTarget(PC); }
400 /** Returns the number of source registers. */
401 int8_t numSrcRegs() const { return staticInst->numSrcRegs(); }
403 /** Returns the number of destination registers. */
404 int8_t numDestRegs() const { return staticInst->numDestRegs(); }
406 // the following are used to track physical register usage
407 // for machines with separate int & FP reg files
408 int8_t numFPDestRegs() const { return staticInst->numFPDestRegs(); }
409 int8_t numIntDestRegs() const { return staticInst->numIntDestRegs(); }
411 /** Returns the logical register index of the i'th destination register. */
412 RegIndex destRegIdx(int i) const { return staticInst->destRegIdx(i); }
414 /** Returns the logical register index of the i'th source register. */
415 RegIndex srcRegIdx(int i) const { return staticInst->srcRegIdx(i); }
417 /** Returns the result of an integer instruction. */
418 uint64_t readIntResult() { return instResult.integer; }
420 /** Returns the result of a floating point instruction. */
421 float readFloatResult() { return instResult.fp; }
423 /** Returns the result of a floating point (double) instruction. */
424 double readDoubleResult() { return instResult.dbl; }
427 /** Records that one of the source registers is ready. */
428 void markSrcRegReady();
430 /** Marks a specific register as ready.
431 * @todo: Move this to .cc file.
433 void markSrcRegReady(RegIndex src_idx);
435 /** Returns if a source register is ready. */
436 bool isReadySrcRegIdx(int idx) const
438 return this->_readySrcRegIdx[idx];
441 /** Sets this instruction as completed. */
442 void setCompleted() { completed = true; }
444 /** Returns whether or not this instruction is completed. */
445 bool isCompleted() const { return completed; }
447 /** Sets this instruction as ready to issue. */
448 void setCanIssue() { canIssue = true; }
450 /** Returns whether or not this instruction is ready to issue. */
451 bool readyToIssue() const { return canIssue; }
453 /** Sets this instruction as issued from the IQ. */
454 void setIssued() { issued = true; }
456 /** Returns whether or not this instruction has issued. */
457 bool isIssued() const { return issued; }
459 /** Sets this instruction as executed. */
460 void setExecuted() { executed = true; }
462 /** Returns whether or not this instruction has executed. */
463 bool isExecuted() const { return executed; }
465 /** Sets this instruction as ready to commit. */
466 void setCanCommit() { canCommit = true; }
468 /** Clears this instruction as being ready to commit. */
469 void clearCanCommit() { canCommit = false; }
471 /** Returns whether or not this instruction is ready to commit. */
472 bool readyToCommit() const { return canCommit; }
474 /** Sets this instruction as committed. */
475 void setCommitted() { committed = true; }
477 /** Returns whether or not this instruction is committed. */
478 bool isCommitted() const { return committed; }
480 /** Sets this instruction as squashed. */
481 void setSquashed() { squashed = true; }
483 /** Returns whether or not this instruction is squashed. */
484 bool isSquashed() const { return squashed; }
486 //Instruction Queue Entry
487 //-----------------------
488 /** Sets this instruction as a entry the IQ. */
489 void setInIQ() { iqEntry = true; }
491 /** Sets this instruction as a entry the IQ. */
492 void removeInIQ() { iqEntry = false; }
494 /** Sets this instruction as squashed in the IQ. */
495 void setSquashedInIQ() { squashedInIQ = true; squashed = true;}
497 /** Returns whether or not this instruction is squashed in the IQ. */
498 bool isSquashedInIQ() const { return squashedInIQ; }
500 /** Returns whether or not this instruction has issued. */
501 bool isInIQ() const { return iqEntry; }
504 //Load / Store Queue Functions
505 //-----------------------
506 /** Sets this instruction as a entry the LSQ. */
507 void setInLSQ() { lsqEntry = true; }
509 /** Sets this instruction as a entry the LSQ. */
510 void removeInLSQ() { lsqEntry = false; }
512 /** Sets this instruction as squashed in the LSQ. */
513 void setSquashedInLSQ() { squashedInLSQ = true;}
515 /** Returns whether or not this instruction is squashed in the LSQ. */
516 bool isSquashedInLSQ() const { return squashedInLSQ; }
518 /** Returns whether or not this instruction is in the LSQ. */
519 bool isInLSQ() const { return lsqEntry; }
522 //Reorder Buffer Functions
523 //-----------------------
524 /** Sets this instruction as a entry the ROB. */
525 void setInROB() { robEntry = true; }
527 /** Sets this instruction as a entry the ROB. */
528 void removeInROB() { robEntry = false; }
530 /** Sets this instruction as squashed in the ROB. */
531 void setSquashedInROB() { squashedInROB = true; }
533 /** Returns whether or not this instruction is squashed in the ROB. */
534 bool isSquashedInROB() const { return squashedInROB; }
536 /** Returns whether or not this instruction is in the ROB. */
537 bool isInROB() const { return robEntry; }
539 /** Read the PC of this instruction. */
540 const Addr readPC() const { return PC; }
542 /** Set the next PC of this instruction (its actual target). */
543 void setNextPC(uint64_t val) { nextPC = val; }
545 void setASID(short addr_space_id) { asid = addr_space_id; }
547 void setThread(unsigned tid) { threadNumber = tid; }
549 void setState(ImplState *state) { thread = state; }
551 /** Returns the exec context.
552 * @todo: Remove this once the ExecContext is no longer used.
554 ExecContext *xcBase() { return thread->getXCProxy(); }
557 /** Instruction effective address.
558 * @todo: Consider if this is necessary or not.
562 /** Whether or not the effective address calculation is completed.
563 * @todo: Consider if this is necessary or not.
568 /** Sets the effective address. */
569 void setEA(Addr &ea) { instEffAddr = ea; eaCalcDone = true; }
571 /** Returns the effective address. */
572 const Addr &getEA() const { return req->vaddr; }
574 /** Returns whether or not the eff. addr. calculation has been completed. */
575 bool doneEACalc() { return eaCalcDone; }
577 /** Returns whether or not the eff. addr. source registers are ready. */
580 /** Whether or not the memory operation is done. */
584 /** Load queue index. */
587 /** Store queue index. */
592 /** Iterator pointing to this BaseDynInst in the list of all insts. */
595 /** Returns iterator to this instruction in the list of all insts. */
596 ListIt &getInstListIt() { return instListIt; }
598 /** Sets iterator for this instruction in the list of all insts. */
599 void setInstListIt(ListIt _instListIt) { instListIt = _instListIt; }
605 BaseDynInst<Impl>::read(Addr addr, T &data, unsigned flags)
608 fault = cpu->read(req, data, lqIdx);
612 req = new MemReq(addr, thread->getXCProxy(), sizeof(T), flags);
614 req->thread_num = threadNumber;
617 if ((req->vaddr & (TheISA::VMPageSize - 1)) + req->size >
618 TheISA::VMPageSize) {
619 return TheISA::genAlignmentFault();
622 fault = cpu->translateDataReadReq(req);
624 effAddr = req->vaddr;
625 physEffAddr = req->paddr;
626 memReqFlags = req->flags;
628 if (fault == NoFault) {
630 if (cpu->system->memctrl->badaddr(physEffAddr)) {
631 fault = TheISA::genMachineCheckFault();
635 fault = cpu->read(req, data, lqIdx);
638 fault = cpu->read(req, data, lqIdx);
641 // Return a fixed value to keep simulation deterministic even
642 // along misspeculated paths.
645 // Commit will have to clean up whatever happened. Set this
646 // instruction as executed.
651 traceData->setAddr(addr);
652 traceData->setData(data);
661 BaseDynInst<Impl>::write(T data, Addr addr, unsigned flags, uint64_t *res)
664 traceData->setAddr(addr);
665 traceData->setData(data);
668 req = new MemReq(addr, thread->getXCProxy(), sizeof(T), flags);
671 req->thread_num = threadNumber;
674 if ((req->vaddr & (TheISA::VMPageSize - 1)) + req->size >
675 TheISA::VMPageSize) {
676 return TheISA::genAlignmentFault();
679 fault = cpu->translateDataWriteReq(req);
681 effAddr = req->vaddr;
682 physEffAddr = req->paddr;
683 memReqFlags = req->flags;
685 if (fault == NoFault) {
687 if (cpu->system->memctrl->badaddr(physEffAddr)) {
688 fault = TheISA::genMachineCheckFault();
690 fault = cpu->write(req, data, sqIdx);
693 fault = cpu->write(req, data, sqIdx);
698 // always return some result to keep misspeculated paths
699 // (which will ignore faults) deterministic
700 *res = (fault == NoFault) ? req->result : 0;
706 #endif // __CPU_BASE_DYN_INST_HH__