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.
31 #ifndef __CPU_BASE_DYN_INST_HH__
32 #define __CPU_BASE_DYN_INST_HH__
37 #include "arch/faults.hh"
38 #include "base/fast_alloc.hh"
39 #include "base/trace.hh"
40 #include "config/full_system.hh"
41 #include "cpu/exetrace.hh"
42 #include "cpu/inst_seq.hh"
43 #include "cpu/op_class.hh"
44 #include "cpu/static_inst.hh"
45 #include "mem/packet.hh"
46 #include "sim/system.hh"
48 #include "encumbered/cpu/full/bpred_update.hh"
49 #include "encumbered/cpu/full/spec_memory.hh"
50 #include "encumbered/cpu/full/spec_state.hh"
51 #include "encumbered/mem/functional/main.hh"
56 * Defines a dynamic instruction context.
59 // Forward declaration.
63 class BaseDynInst : public FastAlloc, public RefCounted
66 // Typedef for the CPU.
67 typedef typename Impl::FullCPU FullCPU;
68 typedef typename FullCPU::ImplState ImplState;
70 // Binary machine instruction type.
71 typedef TheISA::MachInst MachInst;
72 // Extended machine instruction type
73 typedef TheISA::ExtMachInst ExtMachInst;
74 // Logical register index type.
75 typedef TheISA::RegIndex RegIndex;
76 // Integer register index type.
77 typedef TheISA::IntReg IntReg;
79 // The DynInstPtr type.
80 typedef typename Impl::DynInstPtr DynInstPtr;
82 // The list of instructions iterator type.
83 typedef typename std::list<DynInstPtr>::iterator ListIt;
86 MaxInstSrcRegs = TheISA::MaxInstSrcRegs, /// Max source regs
87 MaxInstDestRegs = TheISA::MaxInstDestRegs, /// Max dest regs
90 /** The StaticInst used by this BaseDynInst. */
91 StaticInstPtr staticInst;
93 ////////////////////////////////////////////
95 // INSTRUCTION EXECUTION
97 ////////////////////////////////////////////
98 /** InstRecord that tracks this instructions. */
99 Trace::InstRecord *traceData;
102 * Does a read to a given address.
103 * @param addr The address to read.
104 * @param data The read's data is written into this parameter.
105 * @param flags The request's flags.
106 * @return Returns any fault due to the read.
109 Fault read(Addr addr, T &data, unsigned flags);
112 * Does a write to a given address.
113 * @param data The data to be written.
114 * @param addr The address to write to.
115 * @param flags The request's flags.
116 * @param res The result of the write (for load locked/store conditionals).
117 * @return Returns any fault due to the write.
120 Fault write(T data, Addr addr, unsigned flags,
123 void prefetch(Addr addr, unsigned flags);
124 void writeHint(Addr addr, int size, unsigned flags);
125 Fault copySrcTranslate(Addr src);
126 Fault copy(Addr dest);
128 /** @todo: Consider making this private. */
130 /** The sequence number of the instruction. */
133 /** Is the instruction in the IQ */
136 /** Is the instruction in the ROB */
139 /** Is the instruction in the LSQ */
142 /** Is the instruction completed. */
145 /** Is the instruction's result ready. */
148 /** Can this instruction issue. */
151 /** Has this instruction issued. */
154 /** Has this instruction executed (or made it through execute) yet. */
157 /** Can this instruction commit. */
160 /** Is this instruction committed. */
163 /** Is this instruction squashed. */
166 /** Is this instruction squashed in the instruction queue. */
169 /** Is this instruction squashed in the instruction queue. */
172 /** Is this instruction squashed in the instruction queue. */
175 /** Is this a recover instruction. */
178 /** Is this a thread blocking instruction. */
179 bool blockingInst; /* this inst has called thread_block() */
181 /** Is this a thread syncrhonization instruction. */
184 /** The thread this instruction is from. */
187 /** data address space ID, for loads & stores. */
190 /** How many source registers are ready. */
193 /** Pointer to the FullCPU object. */
196 /** Pointer to the exec context. */
199 /** The kind of fault this instruction has generated. */
202 /** The memory request. */
209 /** The effective virtual address (lds & stores only). */
212 /** The effective physical address. */
215 /** Effective virtual address for a copy source. */
218 /** Effective physical address for a copy source. */
219 Addr copySrcPhysEffAddr;
221 /** The memory request flags (from translation). */
222 unsigned memReqFlags;
224 /** The size of the data to be stored. */
227 /** The data to be stored. */
236 /** The result of the instruction; assumes for now that there's only one
237 * destination register.
241 /** PC of this instruction. */
244 /** Next non-speculative PC. It is not filled in at fetch, but rather
245 * once the target of the branch is truly known (either decode or
250 /** Predicted next PC. */
253 /** Count of total number of dynamic instructions. */
254 static int instcount;
260 /** Whether or not the source register is ready.
261 * @todo: Not sure this should be here vs the derived class.
263 bool _readySrcRegIdx[MaxInstSrcRegs];
266 /** BaseDynInst constructor given a binary instruction.
267 * @param inst The binary instruction.
268 * @param PC The PC of the instruction.
269 * @param pred_PC The predicted next PC.
270 * @param seq_num The sequence number of the instruction.
271 * @param cpu Pointer to the instruction's CPU.
273 BaseDynInst(ExtMachInst inst, Addr PC, Addr pred_PC, InstSeqNum seq_num,
276 /** BaseDynInst constructor given a StaticInst pointer.
277 * @param _staticInst The StaticInst for this BaseDynInst.
279 BaseDynInst(StaticInstPtr &_staticInst);
281 /** BaseDynInst destructor. */
285 /** Function to initialize variables in the constructors. */
290 * @todo: Make this function work; currently it is a dummy function.
291 * @param fault Last fault.
292 * @param cmd Last command.
293 * @param addr Virtual address of access.
294 * @param p Memory accessed.
295 * @param nbytes Access size.
298 // trace_mem(Fault fault,
304 /** Dumps out contents of this BaseDynInst. */
307 /** Dumps out contents of this BaseDynInst into given string. */
308 void dump(std::string &outstring);
310 /** Returns the fault type. */
311 Fault getFault() { return fault; }
313 /** Checks whether or not this instruction has had its branch target
314 * calculated yet. For now it is not utilized and is hacked to be
316 * @todo: Actually use this instruction.
318 bool doneTargCalc() { return false; }
320 /** Returns the next PC. This could be the speculative next PC if it is
321 * called prior to the actual branch target being calculated.
323 Addr readNextPC() { return nextPC; }
325 /** Set the predicted target of this current instruction. */
326 void setPredTarg(Addr predicted_PC) { predPC = predicted_PC; }
328 /** Returns the predicted target of the branch. */
329 Addr readPredTarg() { return predPC; }
331 /** Returns whether the instruction was predicted taken or not. */
332 bool predTaken() { return predPC != (PC + sizeof(MachInst)); }
334 /** Returns whether the instruction mispredicted. */
335 bool mispredicted() { return predPC != nextPC; }
338 // Instruction types. Forward checks to StaticInst object.
340 bool isNop() const { return staticInst->isNop(); }
341 bool isMemRef() const { return staticInst->isMemRef(); }
342 bool isLoad() const { return staticInst->isLoad(); }
343 bool isStore() const { return staticInst->isStore(); }
344 bool isStoreConditional() const
345 { return staticInst->isStoreConditional(); }
346 bool isInstPrefetch() const { return staticInst->isInstPrefetch(); }
347 bool isDataPrefetch() const { return staticInst->isDataPrefetch(); }
348 bool isCopy() const { return staticInst->isCopy(); }
349 bool isInteger() const { return staticInst->isInteger(); }
350 bool isFloating() const { return staticInst->isFloating(); }
351 bool isControl() const { return staticInst->isControl(); }
352 bool isCall() const { return staticInst->isCall(); }
353 bool isReturn() const { return staticInst->isReturn(); }
354 bool isDirectCtrl() const { return staticInst->isDirectCtrl(); }
355 bool isIndirectCtrl() const { return staticInst->isIndirectCtrl(); }
356 bool isCondCtrl() const { return staticInst->isCondCtrl(); }
357 bool isUncondCtrl() const { return staticInst->isUncondCtrl(); }
358 bool isThreadSync() const { return staticInst->isThreadSync(); }
359 bool isSerializing() const { return staticInst->isSerializing(); }
360 bool isSerializeBefore() const
361 { return staticInst->isSerializeBefore() || serializeBefore; }
362 bool isSerializeAfter() const
363 { return staticInst->isSerializeAfter() || serializeAfter; }
364 bool isMemBarrier() const { return staticInst->isMemBarrier(); }
365 bool isWriteBarrier() const { return staticInst->isWriteBarrier(); }
366 bool isNonSpeculative() const { return staticInst->isNonSpeculative(); }
367 bool isQuiesce() const { return staticInst->isQuiesce(); }
368 bool isIprAccess() const { return staticInst->isIprAccess(); }
369 bool isUnverifiable() const { return staticInst->isUnverifiable(); }
371 /** Temporarily sets this instruction as a serialize before instruction. */
372 void setSerializeBefore() { serializeBefore = true; }
374 /** Clears the serializeBefore part of this instruction. */
375 void clearSerializeBefore() { serializeBefore = false; }
377 /** Checks if this serializeBefore is only temporarily set. */
378 bool isTempSerializeBefore() { return serializeBefore; }
380 /** Tracks if instruction has been externally set as serializeBefore. */
381 bool serializeBefore;
383 /** Temporarily sets this instruction as a serialize after instruction. */
384 void setSerializeAfter() { serializeAfter = true; }
386 /** Clears the serializeAfter part of this instruction.*/
387 void clearSerializeAfter() { serializeAfter = false; }
389 /** Checks if this serializeAfter is only temporarily set. */
390 bool isTempSerializeAfter() { return serializeAfter; }
392 /** Tracks if instruction has been externally set as serializeAfter. */
395 /** Checks if the serialization part of this instruction has been
396 * handled. This does not apply to the temporary serializing
397 * state; it only applies to this instruction's own permanent
400 bool isSerializeHandled() { return serializeHandled; }
402 /** Sets the serialization part of this instruction as handled. */
403 void setSerializeHandled() { serializeHandled = true; }
405 /** Whether or not the serialization of this instruction has been handled. */
406 bool serializeHandled;
408 /** Returns the opclass of this instruction. */
409 OpClass opClass() const { return staticInst->opClass(); }
411 /** Returns the branch target address. */
412 Addr branchTarget() const { return staticInst->branchTarget(PC); }
414 /** Returns the number of source registers. */
415 int8_t numSrcRegs() const { return staticInst->numSrcRegs(); }
417 /** Returns the number of destination registers. */
418 int8_t numDestRegs() const { return staticInst->numDestRegs(); }
420 // the following are used to track physical register usage
421 // for machines with separate int & FP reg files
422 int8_t numFPDestRegs() const { return staticInst->numFPDestRegs(); }
423 int8_t numIntDestRegs() const { return staticInst->numIntDestRegs(); }
425 /** Returns the logical register index of the i'th destination register. */
426 RegIndex destRegIdx(int i) const { return staticInst->destRegIdx(i); }
428 /** Returns the logical register index of the i'th source register. */
429 RegIndex srcRegIdx(int i) const { return staticInst->srcRegIdx(i); }
431 /** Returns the result of an integer instruction. */
432 uint64_t readIntResult() { return instResult.integer; }
434 /** Returns the result of a floating point instruction. */
435 float readFloatResult() { return instResult.fp; }
437 /** Returns the result of a floating point (double) instruction. */
438 double readDoubleResult() { return instResult.dbl; }
440 void setIntReg(const StaticInst *si, int idx, uint64_t val)
442 instResult.integer = val;
445 void setFloatRegSingle(const StaticInst *si, int idx, float val)
450 void setFloatRegDouble(const StaticInst *si, int idx, double val)
452 instResult.dbl = val;
455 void setFloatRegInt(const StaticInst *si, int idx, uint64_t val)
457 instResult.integer = val;
460 /** Records that one of the source registers is ready. */
461 void markSrcRegReady();
463 /** Marks a specific register as ready. */
464 void markSrcRegReady(RegIndex src_idx);
466 /** Returns if a source register is ready. */
467 bool isReadySrcRegIdx(int idx) const
469 return this->_readySrcRegIdx[idx];
472 /** Sets this instruction as completed. */
473 void setCompleted() { completed = true; }
475 /** Returns whether or not this instruction is completed. */
476 bool isCompleted() const { return completed; }
478 void setResultReady() { resultReady = true; }
480 bool isResultReady() const { return resultReady; }
482 /** Sets this instruction as ready to issue. */
483 void setCanIssue() { canIssue = true; }
485 /** Returns whether or not this instruction is ready to issue. */
486 bool readyToIssue() const { return canIssue; }
488 /** Sets this instruction as issued from the IQ. */
489 void setIssued() { issued = true; }
491 /** Returns whether or not this instruction has issued. */
492 bool isIssued() const { return issued; }
494 /** Sets this instruction as executed. */
495 void setExecuted() { executed = true; }
497 /** Returns whether or not this instruction has executed. */
498 bool isExecuted() const { return executed; }
500 /** Sets this instruction as ready to commit. */
501 void setCanCommit() { canCommit = true; }
503 /** Clears this instruction as being ready to commit. */
504 void clearCanCommit() { canCommit = false; }
506 /** Returns whether or not this instruction is ready to commit. */
507 bool readyToCommit() const { return canCommit; }
509 /** Sets this instruction as committed. */
510 void setCommitted() { committed = true; }
512 /** Returns whether or not this instruction is committed. */
513 bool isCommitted() const { return committed; }
515 /** Sets this instruction as squashed. */
516 void setSquashed() { squashed = true; }
518 /** Returns whether or not this instruction is squashed. */
519 bool isSquashed() const { return squashed; }
521 //Instruction Queue Entry
522 //-----------------------
523 /** Sets this instruction as a entry the IQ. */
524 void setInIQ() { iqEntry = true; }
526 /** Sets this instruction as a entry the IQ. */
527 void removeInIQ() { iqEntry = false; }
529 /** Sets this instruction as squashed in the IQ. */
530 void setSquashedInIQ() { squashedInIQ = true; squashed = true;}
532 /** Returns whether or not this instruction is squashed in the IQ. */
533 bool isSquashedInIQ() const { return squashedInIQ; }
535 /** Returns whether or not this instruction has issued. */
536 bool isInIQ() const { return iqEntry; }
539 //Load / Store Queue Functions
540 //-----------------------
541 /** Sets this instruction as a entry the LSQ. */
542 void setInLSQ() { lsqEntry = true; }
544 /** Sets this instruction as a entry the LSQ. */
545 void removeInLSQ() { lsqEntry = false; }
547 /** Sets this instruction as squashed in the LSQ. */
548 void setSquashedInLSQ() { squashedInLSQ = true;}
550 /** Returns whether or not this instruction is squashed in the LSQ. */
551 bool isSquashedInLSQ() const { return squashedInLSQ; }
553 /** Returns whether or not this instruction is in the LSQ. */
554 bool isInLSQ() const { return lsqEntry; }
557 //Reorder Buffer Functions
558 //-----------------------
559 /** Sets this instruction as a entry the ROB. */
560 void setInROB() { robEntry = true; }
562 /** Sets this instruction as a entry the ROB. */
563 void removeInROB() { robEntry = false; }
565 /** Sets this instruction as squashed in the ROB. */
566 void setSquashedInROB() { squashedInROB = true; }
568 /** Returns whether or not this instruction is squashed in the ROB. */
569 bool isSquashedInROB() const { return squashedInROB; }
571 /** Returns whether or not this instruction is in the ROB. */
572 bool isInROB() const { return robEntry; }
574 /** Read the PC of this instruction. */
575 const Addr readPC() const { return PC; }
577 /** Set the next PC of this instruction (its actual target). */
578 void setNextPC(uint64_t val)
581 // instResult.integer = val;
584 void setASID(short addr_space_id) { asid = addr_space_id; }
586 void setThread(unsigned tid) { threadNumber = tid; }
588 void setState(ImplState *state) { thread = state; }
590 /** Returns the exec context.
591 * @todo: Remove this once the ExecContext is no longer used.
593 ExecContext *xcBase() { return thread->getXCProxy(); }
596 /** Instruction effective address.
597 * @todo: Consider if this is necessary or not.
601 /** Whether or not the effective address calculation is completed.
602 * @todo: Consider if this is necessary or not.
607 /** Sets the effective address. */
608 void setEA(Addr &ea) { instEffAddr = ea; eaCalcDone = true; }
610 /** Returns the effective address. */
611 const Addr &getEA() const { return instEffAddr; }
613 /** Returns whether or not the eff. addr. calculation has been completed. */
614 bool doneEACalc() { return eaCalcDone; }
616 /** Returns whether or not the eff. addr. source registers are ready. */
619 /** Whether or not the memory operation is done. */
623 /** Load queue index. */
626 /** Store queue index. */
631 /** Iterator pointing to this BaseDynInst in the list of all insts. */
634 /** Returns iterator to this instruction in the list of all insts. */
635 ListIt &getInstListIt() { return instListIt; }
637 /** Sets iterator for this instruction in the list of all insts. */
638 void setInstListIt(ListIt _instListIt) { instListIt = _instListIt; }
644 BaseDynInst<Impl>::read(Addr addr, T &data, unsigned flags)
647 panic("Not supposed to re-execute with split mem ops!");
648 fault = cpu->read(req, data, lqIdx);
653 req->setVirt(asid, addr, sizeof(T), flags, this->PC);
654 req->setThreadContext(thread->cpuId, threadNumber);
656 if ((req->getVaddr() & (TheISA::VMPageSize - 1)) + req->getSize() >
657 TheISA::VMPageSize) {
658 return TheISA::genAlignmentFault();
661 fault = cpu->translateDataReadReq(req);
663 effAddr = req->getVaddr();
664 physEffAddr = req->getPaddr();
665 memReqFlags = req->getFlags();
667 if (fault == NoFault) {
669 if (cpu->system->memctrl->badaddr(physEffAddr)) {
670 fault = TheISA::genMachineCheckFault();
674 fault = cpu->read(req, data, lqIdx);
677 fault = cpu->read(req, data, lqIdx);
680 // Return a fixed value to keep simulation deterministic even
681 // along misspeculated paths.
684 // Commit will have to clean up whatever happened. Set this
685 // instruction as executed.
690 traceData->setAddr(addr);
691 traceData->setData(data);
700 BaseDynInst<Impl>::write(T data, Addr addr, unsigned flags, uint64_t *res)
703 traceData->setAddr(addr);
704 traceData->setData(data);
708 req->setVirt(asid, addr, sizeof(T), flags, this->PC);
709 req->setThreadContext(thread->cpuId, threadNumber);
711 if ((req->getVaddr() & (TheISA::VMPageSize - 1)) + req->getSize() >
712 TheISA::VMPageSize) {
713 return TheISA::genAlignmentFault();
716 fault = cpu->translateDataWriteReq(req);
718 effAddr = req->getVaddr();
719 physEffAddr = req->getPaddr();
720 memReqFlags = req->getFlags();
722 if (fault == NoFault) {
724 if (cpu->system->memctrl->badaddr(physEffAddr)) {
725 fault = TheISA::genMachineCheckFault();
727 fault = cpu->write(req, data, sqIdx);
730 fault = cpu->write(req, data, sqIdx);
735 // always return some result to keep misspeculated paths
736 // (which will ignore faults) deterministic
737 *res = (fault == NoFault) ? req->getScResult() : 0;
743 #endif // __CPU_BASE_DYN_INST_HH__