base_dyn_inst.hh revision 8817
111986Sandreas.sandberg@arm.com/*
211986Sandreas.sandberg@arm.com * Copyright (c) 2011 ARM Limited
311986Sandreas.sandberg@arm.com * All rights reserved.
411986Sandreas.sandberg@arm.com *
511986Sandreas.sandberg@arm.com * The license below extends only to copyright in the software and shall
611986Sandreas.sandberg@arm.com * not be construed as granting a license to any other intellectual
711986Sandreas.sandberg@arm.com * property including but not limited to intellectual property relating
811986Sandreas.sandberg@arm.com * to a hardware implementation of the functionality of the software
911986Sandreas.sandberg@arm.com * licensed hereunder.  You may use the software subject to the license
1011986Sandreas.sandberg@arm.com * terms below provided that you ensure that this notice is replicated
1111986Sandreas.sandberg@arm.com * unmodified and in its entirety in all distributions of the software,
1211986Sandreas.sandberg@arm.com * modified or unmodified, in source code or in binary form.
1311986Sandreas.sandberg@arm.com *
1411986Sandreas.sandberg@arm.com * Copyright (c) 2004-2006 The Regents of The University of Michigan
1511986Sandreas.sandberg@arm.com * Copyright (c) 2009 The University of Edinburgh
1611986Sandreas.sandberg@arm.com * All rights reserved.
1711986Sandreas.sandberg@arm.com *
1811986Sandreas.sandberg@arm.com * Redistribution and use in source and binary forms, with or without
1911986Sandreas.sandberg@arm.com * modification, are permitted provided that the following conditions are
2011986Sandreas.sandberg@arm.com * met: redistributions of source code must retain the above copyright
2111986Sandreas.sandberg@arm.com * notice, this list of conditions and the following disclaimer;
2211986Sandreas.sandberg@arm.com * redistributions in binary form must reproduce the above copyright
2311986Sandreas.sandberg@arm.com * notice, this list of conditions and the following disclaimer in the
2411986Sandreas.sandberg@arm.com * documentation and/or other materials provided with the distribution;
2511986Sandreas.sandberg@arm.com * neither the name of the copyright holders nor the names of its
2611986Sandreas.sandberg@arm.com * contributors may be used to endorse or promote products derived from
2711986Sandreas.sandberg@arm.com * this software without specific prior written permission.
2812037Sandreas.sandberg@arm.com *
2911986Sandreas.sandberg@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3011986Sandreas.sandberg@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3111986Sandreas.sandberg@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3211986Sandreas.sandberg@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3311986Sandreas.sandberg@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3411986Sandreas.sandberg@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3511986Sandreas.sandberg@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3611986Sandreas.sandberg@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3711986Sandreas.sandberg@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3811986Sandreas.sandberg@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3911986Sandreas.sandberg@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4011986Sandreas.sandberg@arm.com *
4111986Sandreas.sandberg@arm.com * Authors: Kevin Lim
4211986Sandreas.sandberg@arm.com *          Timothy M. Jones
4311986Sandreas.sandberg@arm.com */
4411986Sandreas.sandberg@arm.com
4512037Sandreas.sandberg@arm.com#ifndef __CPU_BASE_DYN_INST_HH__
4611986Sandreas.sandberg@arm.com#define __CPU_BASE_DYN_INST_HH__
4711986Sandreas.sandberg@arm.com
4811986Sandreas.sandberg@arm.com#include <bitset>
4911986Sandreas.sandberg@arm.com#include <list>
5011986Sandreas.sandberg@arm.com#include <string>
5111986Sandreas.sandberg@arm.com#include <queue>
5211986Sandreas.sandberg@arm.com
5311986Sandreas.sandberg@arm.com#include "arch/utility.hh"
5411986Sandreas.sandberg@arm.com#include "base/fast_alloc.hh"
5511986Sandreas.sandberg@arm.com#include "base/trace.hh"
5611986Sandreas.sandberg@arm.com#include "config/the_isa.hh"
5711986Sandreas.sandberg@arm.com#include "config/use_checker.hh"
5811986Sandreas.sandberg@arm.com#include "cpu/o3/comm.hh"
5911986Sandreas.sandberg@arm.com#include "cpu/exetrace.hh"
6011986Sandreas.sandberg@arm.com#include "cpu/inst_seq.hh"
6111986Sandreas.sandberg@arm.com#include "cpu/op_class.hh"
6211986Sandreas.sandberg@arm.com#include "cpu/static_inst.hh"
6311986Sandreas.sandberg@arm.com#include "cpu/translation.hh"
6411986Sandreas.sandberg@arm.com#include "mem/packet.hh"
6511986Sandreas.sandberg@arm.com#include "sim/byteswap.hh"
6611986Sandreas.sandberg@arm.com#include "sim/fault_fwd.hh"
6711986Sandreas.sandberg@arm.com#include "sim/system.hh"
6811986Sandreas.sandberg@arm.com#include "sim/tlb.hh"
6911986Sandreas.sandberg@arm.com
7011986Sandreas.sandberg@arm.com/**
7111986Sandreas.sandberg@arm.com * @file
7211986Sandreas.sandberg@arm.com * Defines a dynamic instruction context.
7311986Sandreas.sandberg@arm.com */
7411986Sandreas.sandberg@arm.com
7511986Sandreas.sandberg@arm.comtemplate <class Impl>
7612391Sjason@lowepower.comclass BaseDynInst : public FastAlloc, public RefCounted
7712391Sjason@lowepower.com{
7811986Sandreas.sandberg@arm.com  public:
7911986Sandreas.sandberg@arm.com    // Typedef for the CPU.
8011986Sandreas.sandberg@arm.com    typedef typename Impl::CPUType ImplCPU;
8111986Sandreas.sandberg@arm.com    typedef typename ImplCPU::ImplState ImplState;
8211986Sandreas.sandberg@arm.com
8311986Sandreas.sandberg@arm.com    // Logical register index type.
8411986Sandreas.sandberg@arm.com    typedef TheISA::RegIndex RegIndex;
8511986Sandreas.sandberg@arm.com    // Integer register type.
8611986Sandreas.sandberg@arm.com    typedef TheISA::IntReg IntReg;
8711986Sandreas.sandberg@arm.com    // Floating point register type.
8811986Sandreas.sandberg@arm.com    typedef TheISA::FloatReg FloatReg;
8911986Sandreas.sandberg@arm.com
9011986Sandreas.sandberg@arm.com    // The DynInstPtr type.
9111986Sandreas.sandberg@arm.com    typedef typename Impl::DynInstPtr DynInstPtr;
9211986Sandreas.sandberg@arm.com    typedef RefCountingPtr<BaseDynInst<Impl> > BaseDynInstPtr;
9311986Sandreas.sandberg@arm.com
9411986Sandreas.sandberg@arm.com    // The list of instructions iterator type.
9511986Sandreas.sandberg@arm.com    typedef typename std::list<DynInstPtr>::iterator ListIt;
9611986Sandreas.sandberg@arm.com
9711986Sandreas.sandberg@arm.com    enum {
9811986Sandreas.sandberg@arm.com        MaxInstSrcRegs = TheISA::MaxInstSrcRegs,        /// Max source regs
9911986Sandreas.sandberg@arm.com        MaxInstDestRegs = TheISA::MaxInstDestRegs,      /// Max dest regs
10011986Sandreas.sandberg@arm.com    };
10112391Sjason@lowepower.com
10212391Sjason@lowepower.com    /** The StaticInst used by this BaseDynInst. */
10311986Sandreas.sandberg@arm.com    StaticInstPtr staticInst;
10411986Sandreas.sandberg@arm.com    StaticInstPtr macroop;
10511986Sandreas.sandberg@arm.com
10611986Sandreas.sandberg@arm.com    ////////////////////////////////////////////
10711986Sandreas.sandberg@arm.com    //
10811986Sandreas.sandberg@arm.com    // INSTRUCTION EXECUTION
10911986Sandreas.sandberg@arm.com    //
11012391Sjason@lowepower.com    ////////////////////////////////////////////
11112391Sjason@lowepower.com    /** InstRecord that tracks this instructions. */
11212391Sjason@lowepower.com    Trace::InstRecord *traceData;
11312391Sjason@lowepower.com
11412391Sjason@lowepower.com    void demapPage(Addr vaddr, uint64_t asn)
11512391Sjason@lowepower.com    {
11611986Sandreas.sandberg@arm.com        cpu->demapPage(vaddr, asn);
11711986Sandreas.sandberg@arm.com    }
11811986Sandreas.sandberg@arm.com    void demapInstPage(Addr vaddr, uint64_t asn)
11911986Sandreas.sandberg@arm.com    {
12011986Sandreas.sandberg@arm.com        cpu->demapPage(vaddr, asn);
12111986Sandreas.sandberg@arm.com    }
12211986Sandreas.sandberg@arm.com    void demapDataPage(Addr vaddr, uint64_t asn)
12311986Sandreas.sandberg@arm.com    {
12411986Sandreas.sandberg@arm.com        cpu->demapPage(vaddr, asn);
12512391Sjason@lowepower.com    }
12612391Sjason@lowepower.com
12712391Sjason@lowepower.com    Fault readMem(Addr addr, uint8_t *data, unsigned size, unsigned flags);
12811986Sandreas.sandberg@arm.com
12911986Sandreas.sandberg@arm.com    Fault writeMem(uint8_t *data, unsigned size,
13011986Sandreas.sandberg@arm.com                   Addr addr, unsigned flags, uint64_t *res);
13112391Sjason@lowepower.com
13211986Sandreas.sandberg@arm.com    /** Splits a request in two if it crosses a dcache block. */
13312391Sjason@lowepower.com    void splitRequest(RequestPtr req, RequestPtr &sreqLow,
13412391Sjason@lowepower.com                      RequestPtr &sreqHigh);
13512391Sjason@lowepower.com
13611986Sandreas.sandberg@arm.com    /** Initiate a DTB address translation. */
13712391Sjason@lowepower.com    void initiateTranslation(RequestPtr req, RequestPtr sreqLow,
13812391Sjason@lowepower.com                             RequestPtr sreqHigh, uint64_t *res,
13912391Sjason@lowepower.com                             BaseTLB::Mode mode);
14012391Sjason@lowepower.com
14112391Sjason@lowepower.com    /** Finish a DTB address translation. */
14212391Sjason@lowepower.com    void finishTranslation(WholeTranslationState *state);
14312391Sjason@lowepower.com
14412391Sjason@lowepower.com    /** True if the DTB address translation has started. */
14512391Sjason@lowepower.com    bool translationStarted;
14612391Sjason@lowepower.com
14712391Sjason@lowepower.com    /** True if the DTB address translation has completed. */
14812391Sjason@lowepower.com    bool translationCompleted;
14912391Sjason@lowepower.com
15011986Sandreas.sandberg@arm.com    /** True if this address was found to match a previous load and they issued
15111986Sandreas.sandberg@arm.com     * out of order. If that happend, then it's only a problem if an incoming
15211986Sandreas.sandberg@arm.com     * snoop invalidate modifies the line, in which case we need to squash.
15311986Sandreas.sandberg@arm.com     * If nothing modified the line the order doesn't matter.
15411986Sandreas.sandberg@arm.com     */
15511986Sandreas.sandberg@arm.com    bool possibleLoadViolation;
15611986Sandreas.sandberg@arm.com
15711986Sandreas.sandberg@arm.com    /** True if the address hit a external snoop while sitting in the LSQ.
15811986Sandreas.sandberg@arm.com     * If this is true and a older instruction sees it, this instruction must
15911986Sandreas.sandberg@arm.com     * reexecute
16011986Sandreas.sandberg@arm.com     */
16111986Sandreas.sandberg@arm.com    bool hitExternalSnoop;
16211986Sandreas.sandberg@arm.com
16311986Sandreas.sandberg@arm.com    /**
16411986Sandreas.sandberg@arm.com     * Returns true if the DTB address translation is being delayed due to a hw
16511986Sandreas.sandberg@arm.com     * page table walk.
16611986Sandreas.sandberg@arm.com     */
16711986Sandreas.sandberg@arm.com    bool isTranslationDelayed() const
16811986Sandreas.sandberg@arm.com    {
16911986Sandreas.sandberg@arm.com        return (translationStarted && !translationCompleted);
17011986Sandreas.sandberg@arm.com    }
17111986Sandreas.sandberg@arm.com
17211986Sandreas.sandberg@arm.com    /**
17311986Sandreas.sandberg@arm.com     * Saved memory requests (needed when the DTB address translation is
17411986Sandreas.sandberg@arm.com     * delayed due to a hw page table walk).
17511986Sandreas.sandberg@arm.com     */
17611986Sandreas.sandberg@arm.com    RequestPtr savedReq;
17711986Sandreas.sandberg@arm.com    RequestPtr savedSreqLow;
17811986Sandreas.sandberg@arm.com    RequestPtr savedSreqHigh;
17911986Sandreas.sandberg@arm.com
18011986Sandreas.sandberg@arm.com#if USE_CHECKER
18111986Sandreas.sandberg@arm.com    // Need a copy of main request pointer to verify on writes.
18211986Sandreas.sandberg@arm.com    RequestPtr reqToVerify;
18311986Sandreas.sandberg@arm.com#endif //USE_CHECKER
18411986Sandreas.sandberg@arm.com
18511986Sandreas.sandberg@arm.com    /** @todo: Consider making this private. */
18611986Sandreas.sandberg@arm.com  public:
18711986Sandreas.sandberg@arm.com    /** The sequence number of the instruction. */
18811986Sandreas.sandberg@arm.com    InstSeqNum seqNum;
18911986Sandreas.sandberg@arm.com
19011986Sandreas.sandberg@arm.com    enum Status {
19111986Sandreas.sandberg@arm.com        IqEntry,                 /// Instruction is in the IQ
19211986Sandreas.sandberg@arm.com        RobEntry,                /// Instruction is in the ROB
19311986Sandreas.sandberg@arm.com        LsqEntry,                /// Instruction is in the LSQ
19411986Sandreas.sandberg@arm.com        Completed,               /// Instruction has completed
19511986Sandreas.sandberg@arm.com        ResultReady,             /// Instruction has its result
19611986Sandreas.sandberg@arm.com        CanIssue,                /// Instruction can issue and execute
19711986Sandreas.sandberg@arm.com        Issued,                  /// Instruction has issued
19811986Sandreas.sandberg@arm.com        Executed,                /// Instruction has executed
19911986Sandreas.sandberg@arm.com        CanCommit,               /// Instruction can commit
20011986Sandreas.sandberg@arm.com        AtCommit,                /// Instruction has reached commit
20111986Sandreas.sandberg@arm.com        Committed,               /// Instruction has committed
20211986Sandreas.sandberg@arm.com        Squashed,                /// Instruction is squashed
20311986Sandreas.sandberg@arm.com        SquashedInIQ,            /// Instruction is squashed in the IQ
20411986Sandreas.sandberg@arm.com        SquashedInLSQ,           /// Instruction is squashed in the LSQ
20511986Sandreas.sandberg@arm.com        SquashedInROB,           /// Instruction is squashed in the ROB
20611986Sandreas.sandberg@arm.com        RecoverInst,             /// Is a recover instruction
20711986Sandreas.sandberg@arm.com        BlockingInst,            /// Is a blocking instruction
20811986Sandreas.sandberg@arm.com        ThreadsyncWait,          /// Is a thread synchronization instruction
20911986Sandreas.sandberg@arm.com        SerializeBefore,         /// Needs to serialize on
21011986Sandreas.sandberg@arm.com                                 /// instructions ahead of it
21111986Sandreas.sandberg@arm.com        SerializeAfter,          /// Needs to serialize instructions behind it
21211986Sandreas.sandberg@arm.com        SerializeHandled,        /// Serialization has been handled
21311986Sandreas.sandberg@arm.com        NumStatus
21411986Sandreas.sandberg@arm.com    };
21511986Sandreas.sandberg@arm.com
21611986Sandreas.sandberg@arm.com    /** The status of this BaseDynInst.  Several bits can be set. */
21711986Sandreas.sandberg@arm.com    std::bitset<NumStatus> status;
21811986Sandreas.sandberg@arm.com
21911986Sandreas.sandberg@arm.com    /** The thread this instruction is from. */
22011986Sandreas.sandberg@arm.com    ThreadID threadNumber;
22111986Sandreas.sandberg@arm.com
22211986Sandreas.sandberg@arm.com    /** data address space ID, for loads & stores. */
22311986Sandreas.sandberg@arm.com    short asid;
22411986Sandreas.sandberg@arm.com
22511986Sandreas.sandberg@arm.com    /** How many source registers are ready. */
22611986Sandreas.sandberg@arm.com    unsigned readyRegs;
22711986Sandreas.sandberg@arm.com
22811986Sandreas.sandberg@arm.com    /** Pointer to the Impl's CPU object. */
22911986Sandreas.sandberg@arm.com    ImplCPU *cpu;
23011986Sandreas.sandberg@arm.com
23111986Sandreas.sandberg@arm.com    /** Pointer to the thread state. */
23211986Sandreas.sandberg@arm.com    ImplState *thread;
23311986Sandreas.sandberg@arm.com
23411986Sandreas.sandberg@arm.com    /** The kind of fault this instruction has generated. */
23511986Sandreas.sandberg@arm.com    Fault fault;
23611986Sandreas.sandberg@arm.com
23711986Sandreas.sandberg@arm.com    /** Pointer to the data for the memory access. */
23811986Sandreas.sandberg@arm.com    uint8_t *memData;
23911986Sandreas.sandberg@arm.com
24011986Sandreas.sandberg@arm.com    /** The effective virtual address (lds & stores only). */
24111986Sandreas.sandberg@arm.com    Addr effAddr;
24211986Sandreas.sandberg@arm.com
24311986Sandreas.sandberg@arm.com    /** The size of the request */
24411986Sandreas.sandberg@arm.com    Addr effSize;
24511986Sandreas.sandberg@arm.com
24611986Sandreas.sandberg@arm.com    /** Is the effective virtual address valid. */
24711986Sandreas.sandberg@arm.com    bool effAddrValid;
24811986Sandreas.sandberg@arm.com
24911986Sandreas.sandberg@arm.com    /** The effective physical address. */
25011986Sandreas.sandberg@arm.com    Addr physEffAddr;
25111986Sandreas.sandberg@arm.com
25211986Sandreas.sandberg@arm.com    /** The memory request flags (from translation). */
25311986Sandreas.sandberg@arm.com    unsigned memReqFlags;
25411986Sandreas.sandberg@arm.com
25511986Sandreas.sandberg@arm.com    union Result {
25611986Sandreas.sandberg@arm.com        uint64_t integer;
25711986Sandreas.sandberg@arm.com        double dbl;
25811986Sandreas.sandberg@arm.com        void set(uint64_t i) { integer = i; }
25911986Sandreas.sandberg@arm.com        void set(double d) { dbl = d; }
26011986Sandreas.sandberg@arm.com        void get(uint64_t& i) { i = integer; }
26111986Sandreas.sandberg@arm.com        void get(double& d) { d = dbl; }
26211986Sandreas.sandberg@arm.com    };
26311986Sandreas.sandberg@arm.com
26411986Sandreas.sandberg@arm.com    /** The result of the instruction; assumes an instruction can have many
26511986Sandreas.sandberg@arm.com     *  destination registers.
26611986Sandreas.sandberg@arm.com     */
26711986Sandreas.sandberg@arm.com    std::queue<Result> instResult;
26811986Sandreas.sandberg@arm.com
26911986Sandreas.sandberg@arm.com    /** Records changes to result? */
27012391Sjason@lowepower.com    bool recordResult;
27111986Sandreas.sandberg@arm.com
27211986Sandreas.sandberg@arm.com    /** Did this instruction execute, or is it predicated false */
27311986Sandreas.sandberg@arm.com    bool predicate;
27411986Sandreas.sandberg@arm.com
27511986Sandreas.sandberg@arm.com  protected:
27611986Sandreas.sandberg@arm.com    /** PC state for this instruction. */
27711986Sandreas.sandberg@arm.com    TheISA::PCState pc;
27811986Sandreas.sandberg@arm.com
27911986Sandreas.sandberg@arm.com    /** Predicted PC state after this instruction. */
28011986Sandreas.sandberg@arm.com    TheISA::PCState predPC;
28111986Sandreas.sandberg@arm.com
28211986Sandreas.sandberg@arm.com    /** If this is a branch that was predicted taken */
28311986Sandreas.sandberg@arm.com    bool predTaken;
28411986Sandreas.sandberg@arm.com
28511986Sandreas.sandberg@arm.com  public:
28611986Sandreas.sandberg@arm.com
28711986Sandreas.sandberg@arm.com#ifdef DEBUG
28811986Sandreas.sandberg@arm.com    void dumpSNList();
28911986Sandreas.sandberg@arm.com#endif
29011986Sandreas.sandberg@arm.com
29111986Sandreas.sandberg@arm.com    /** Whether or not the source register is ready.
29211986Sandreas.sandberg@arm.com     *  @todo: Not sure this should be here vs the derived class.
29311986Sandreas.sandberg@arm.com     */
294    bool _readySrcRegIdx[MaxInstSrcRegs];
295
296  protected:
297    /** Flattened register index of the destination registers of this
298     *  instruction.
299     */
300    TheISA::RegIndex _flatDestRegIdx[TheISA::MaxInstDestRegs];
301
302    /** Flattened register index of the source registers of this
303     *  instruction.
304     */
305    TheISA::RegIndex _flatSrcRegIdx[TheISA::MaxInstSrcRegs];
306
307    /** Physical register index of the destination registers of this
308     *  instruction.
309     */
310    PhysRegIndex _destRegIdx[TheISA::MaxInstDestRegs];
311
312    /** Physical register index of the source registers of this
313     *  instruction.
314     */
315    PhysRegIndex _srcRegIdx[TheISA::MaxInstSrcRegs];
316
317    /** Physical register index of the previous producers of the
318     *  architected destinations.
319     */
320    PhysRegIndex _prevDestRegIdx[TheISA::MaxInstDestRegs];
321
322  public:
323
324    /** Returns the physical register index of the i'th destination
325     *  register.
326     */
327    PhysRegIndex renamedDestRegIdx(int idx) const
328    {
329        return _destRegIdx[idx];
330    }
331
332    /** Returns the physical register index of the i'th source register. */
333    PhysRegIndex renamedSrcRegIdx(int idx) const
334    {
335        return _srcRegIdx[idx];
336    }
337
338    /** Returns the flattened register index of the i'th destination
339     *  register.
340     */
341    TheISA::RegIndex flattenedDestRegIdx(int idx) const
342    {
343        return _flatDestRegIdx[idx];
344    }
345
346    /** Returns the flattened register index of the i'th source register */
347    TheISA::RegIndex flattenedSrcRegIdx(int idx) const
348    {
349        return _flatSrcRegIdx[idx];
350    }
351
352    /** Returns the physical register index of the previous physical register
353     *  that remapped to the same logical register index.
354     */
355    PhysRegIndex prevDestRegIdx(int idx) const
356    {
357        return _prevDestRegIdx[idx];
358    }
359
360    /** Renames a destination register to a physical register.  Also records
361     *  the previous physical register that the logical register mapped to.
362     */
363    void renameDestReg(int idx,
364                       PhysRegIndex renamed_dest,
365                       PhysRegIndex previous_rename)
366    {
367        _destRegIdx[idx] = renamed_dest;
368        _prevDestRegIdx[idx] = previous_rename;
369    }
370
371    /** Renames a source logical register to the physical register which
372     *  has/will produce that logical register's result.
373     *  @todo: add in whether or not the source register is ready.
374     */
375    void renameSrcReg(int idx, PhysRegIndex renamed_src)
376    {
377        _srcRegIdx[idx] = renamed_src;
378    }
379
380    /** Flattens a source architectural register index into a logical index.
381     */
382    void flattenSrcReg(int idx, TheISA::RegIndex flattened_src)
383    {
384        _flatSrcRegIdx[idx] = flattened_src;
385    }
386
387    /** Flattens a destination architectural register index into a logical
388     * index.
389     */
390    void flattenDestReg(int idx, TheISA::RegIndex flattened_dest)
391    {
392        _flatDestRegIdx[idx] = flattened_dest;
393    }
394    /** BaseDynInst constructor given a binary instruction.
395     *  @param staticInst A StaticInstPtr to the underlying instruction.
396     *  @param pc The PC state for the instruction.
397     *  @param predPC The predicted next PC state for the instruction.
398     *  @param seq_num The sequence number of the instruction.
399     *  @param cpu Pointer to the instruction's CPU.
400     */
401    BaseDynInst(StaticInstPtr staticInst, StaticInstPtr macroop,
402                TheISA::PCState pc, TheISA::PCState predPC,
403                InstSeqNum seq_num, ImplCPU *cpu);
404
405    /** BaseDynInst constructor given a StaticInst pointer.
406     *  @param _staticInst The StaticInst for this BaseDynInst.
407     */
408    BaseDynInst(StaticInstPtr staticInst, StaticInstPtr macroop);
409
410    /** BaseDynInst destructor. */
411    ~BaseDynInst();
412
413  private:
414    /** Function to initialize variables in the constructors. */
415    void initVars();
416
417  public:
418    /** Dumps out contents of this BaseDynInst. */
419    void dump();
420
421    /** Dumps out contents of this BaseDynInst into given string. */
422    void dump(std::string &outstring);
423
424    /** Read this CPU's ID. */
425    int cpuId() { return cpu->cpuId(); }
426
427    /** Read this context's system-wide ID **/
428    int contextId() { return thread->contextId(); }
429
430    /** Returns the fault type. */
431    Fault getFault() { return fault; }
432
433    /** Checks whether or not this instruction has had its branch target
434     *  calculated yet.  For now it is not utilized and is hacked to be
435     *  always false.
436     *  @todo: Actually use this instruction.
437     */
438    bool doneTargCalc() { return false; }
439
440    /** Set the predicted target of this current instruction. */
441    void setPredTarg(const TheISA::PCState &_predPC)
442    {
443        predPC = _predPC;
444    }
445
446    const TheISA::PCState &readPredTarg() { return predPC; }
447
448    /** Returns the predicted PC immediately after the branch. */
449    Addr predInstAddr() { return predPC.instAddr(); }
450
451    /** Returns the predicted PC two instructions after the branch */
452    Addr predNextInstAddr() { return predPC.nextInstAddr(); }
453
454    /** Returns the predicted micro PC after the branch */
455    Addr predMicroPC() { return predPC.microPC(); }
456
457    /** Returns whether the instruction was predicted taken or not. */
458    bool readPredTaken()
459    {
460        return predTaken;
461    }
462
463    void setPredTaken(bool predicted_taken)
464    {
465        predTaken = predicted_taken;
466    }
467
468    /** Returns whether the instruction mispredicted. */
469    bool mispredicted()
470    {
471        TheISA::PCState tempPC = pc;
472        TheISA::advancePC(tempPC, staticInst);
473        return !(tempPC == predPC);
474    }
475
476    //
477    //  Instruction types.  Forward checks to StaticInst object.
478    //
479    bool isNop()          const { return staticInst->isNop(); }
480    bool isMemRef()       const { return staticInst->isMemRef(); }
481    bool isLoad()         const { return staticInst->isLoad(); }
482    bool isStore()        const { return staticInst->isStore(); }
483    bool isStoreConditional() const
484    { return staticInst->isStoreConditional(); }
485    bool isInstPrefetch() const { return staticInst->isInstPrefetch(); }
486    bool isDataPrefetch() const { return staticInst->isDataPrefetch(); }
487    bool isInteger()      const { return staticInst->isInteger(); }
488    bool isFloating()     const { return staticInst->isFloating(); }
489    bool isControl()      const { return staticInst->isControl(); }
490    bool isCall()         const { return staticInst->isCall(); }
491    bool isReturn()       const { return staticInst->isReturn(); }
492    bool isDirectCtrl()   const { return staticInst->isDirectCtrl(); }
493    bool isIndirectCtrl() const { return staticInst->isIndirectCtrl(); }
494    bool isCondCtrl()     const { return staticInst->isCondCtrl(); }
495    bool isUncondCtrl()   const { return staticInst->isUncondCtrl(); }
496    bool isCondDelaySlot() const { return staticInst->isCondDelaySlot(); }
497    bool isThreadSync()   const { return staticInst->isThreadSync(); }
498    bool isSerializing()  const { return staticInst->isSerializing(); }
499    bool isSerializeBefore() const
500    { return staticInst->isSerializeBefore() || status[SerializeBefore]; }
501    bool isSerializeAfter() const
502    { return staticInst->isSerializeAfter() || status[SerializeAfter]; }
503    bool isSquashAfter() const { return staticInst->isSquashAfter(); }
504    bool isMemBarrier()   const { return staticInst->isMemBarrier(); }
505    bool isWriteBarrier() const { return staticInst->isWriteBarrier(); }
506    bool isNonSpeculative() const { return staticInst->isNonSpeculative(); }
507    bool isQuiesce() const { return staticInst->isQuiesce(); }
508    bool isIprAccess() const { return staticInst->isIprAccess(); }
509    bool isUnverifiable() const { return staticInst->isUnverifiable(); }
510    bool isSyscall() const { return staticInst->isSyscall(); }
511    bool isMacroop() const { return staticInst->isMacroop(); }
512    bool isMicroop() const { return staticInst->isMicroop(); }
513    bool isDelayedCommit() const { return staticInst->isDelayedCommit(); }
514    bool isLastMicroop() const { return staticInst->isLastMicroop(); }
515    bool isFirstMicroop() const { return staticInst->isFirstMicroop(); }
516    bool isMicroBranch() const { return staticInst->isMicroBranch(); }
517
518    /** Temporarily sets this instruction as a serialize before instruction. */
519    void setSerializeBefore() { status.set(SerializeBefore); }
520
521    /** Clears the serializeBefore part of this instruction. */
522    void clearSerializeBefore() { status.reset(SerializeBefore); }
523
524    /** Checks if this serializeBefore is only temporarily set. */
525    bool isTempSerializeBefore() { return status[SerializeBefore]; }
526
527    /** Temporarily sets this instruction as a serialize after instruction. */
528    void setSerializeAfter() { status.set(SerializeAfter); }
529
530    /** Clears the serializeAfter part of this instruction.*/
531    void clearSerializeAfter() { status.reset(SerializeAfter); }
532
533    /** Checks if this serializeAfter is only temporarily set. */
534    bool isTempSerializeAfter() { return status[SerializeAfter]; }
535
536    /** Sets the serialization part of this instruction as handled. */
537    void setSerializeHandled() { status.set(SerializeHandled); }
538
539    /** Checks if the serialization part of this instruction has been
540     *  handled.  This does not apply to the temporary serializing
541     *  state; it only applies to this instruction's own permanent
542     *  serializing state.
543     */
544    bool isSerializeHandled() { return status[SerializeHandled]; }
545
546    /** Returns the opclass of this instruction. */
547    OpClass opClass() const { return staticInst->opClass(); }
548
549    /** Returns the branch target address. */
550    TheISA::PCState branchTarget() const
551    { return staticInst->branchTarget(pc); }
552
553    /** Returns the number of source registers. */
554    int8_t numSrcRegs() const { return staticInst->numSrcRegs(); }
555
556    /** Returns the number of destination registers. */
557    int8_t numDestRegs() const { return staticInst->numDestRegs(); }
558
559    // the following are used to track physical register usage
560    // for machines with separate int & FP reg files
561    int8_t numFPDestRegs()  const { return staticInst->numFPDestRegs(); }
562    int8_t numIntDestRegs() const { return staticInst->numIntDestRegs(); }
563
564    /** Returns the logical register index of the i'th destination register. */
565    RegIndex destRegIdx(int i) const { return staticInst->destRegIdx(i); }
566
567    /** Returns the logical register index of the i'th source register. */
568    RegIndex srcRegIdx(int i) const { return staticInst->srcRegIdx(i); }
569
570    /** Pops a result off the instResult queue */
571    template <class T>
572    void popResult(T& t)
573    {
574        if (!instResult.empty()) {
575            instResult.front().get(t);
576            instResult.pop();
577        }
578    }
579
580    /** Read the most recent result stored by this instruction */
581    template <class T>
582    void readResult(T& t)
583    {
584        instResult.back().get(t);
585    }
586
587    /** Pushes a result onto the instResult queue */
588    template <class T>
589    void setResult(T t)
590    {
591        if (recordResult) {
592            Result instRes;
593            instRes.set(t);
594            instResult.push(instRes);
595        }
596    }
597
598    /** Records an integer register being set to a value. */
599    void setIntRegOperand(const StaticInst *si, int idx, uint64_t val)
600    {
601        setResult<uint64_t>(val);
602    }
603
604    /** Records an fp register being set to a value. */
605    void setFloatRegOperand(const StaticInst *si, int idx, FloatReg val,
606                            int width)
607    {
608        if (width == 32 || width == 64) {
609            setResult<double>(val);
610        } else {
611            panic("Unsupported width!");
612        }
613    }
614
615    /** Records an fp register being set to a value. */
616    void setFloatRegOperand(const StaticInst *si, int idx, FloatReg val)
617    {
618        setResult<double>(val);
619    }
620
621    /** Records an fp register being set to an integer value. */
622    void setFloatRegOperandBits(const StaticInst *si, int idx, uint64_t val,
623                                int width)
624    {
625        setResult<uint64_t>(val);
626    }
627
628    /** Records an fp register being set to an integer value. */
629    void setFloatRegOperandBits(const StaticInst *si, int idx, uint64_t val)
630    {
631        setResult<uint64_t>(val);
632    }
633
634    /** Records that one of the source registers is ready. */
635    void markSrcRegReady();
636
637    /** Marks a specific register as ready. */
638    void markSrcRegReady(RegIndex src_idx);
639
640    /** Returns if a source register is ready. */
641    bool isReadySrcRegIdx(int idx) const
642    {
643        return this->_readySrcRegIdx[idx];
644    }
645
646    /** Sets this instruction as completed. */
647    void setCompleted() { status.set(Completed); }
648
649    /** Returns whether or not this instruction is completed. */
650    bool isCompleted() const { return status[Completed]; }
651
652    /** Marks the result as ready. */
653    void setResultReady() { status.set(ResultReady); }
654
655    /** Returns whether or not the result is ready. */
656    bool isResultReady() const { return status[ResultReady]; }
657
658    /** Sets this instruction as ready to issue. */
659    void setCanIssue() { status.set(CanIssue); }
660
661    /** Returns whether or not this instruction is ready to issue. */
662    bool readyToIssue() const { return status[CanIssue]; }
663
664    /** Clears this instruction being able to issue. */
665    void clearCanIssue() { status.reset(CanIssue); }
666
667    /** Sets this instruction as issued from the IQ. */
668    void setIssued() { status.set(Issued); }
669
670    /** Returns whether or not this instruction has issued. */
671    bool isIssued() const { return status[Issued]; }
672
673    /** Clears this instruction as being issued. */
674    void clearIssued() { status.reset(Issued); }
675
676    /** Sets this instruction as executed. */
677    void setExecuted() { status.set(Executed); }
678
679    /** Returns whether or not this instruction has executed. */
680    bool isExecuted() const { return status[Executed]; }
681
682    /** Sets this instruction as ready to commit. */
683    void setCanCommit() { status.set(CanCommit); }
684
685    /** Clears this instruction as being ready to commit. */
686    void clearCanCommit() { status.reset(CanCommit); }
687
688    /** Returns whether or not this instruction is ready to commit. */
689    bool readyToCommit() const { return status[CanCommit]; }
690
691    void setAtCommit() { status.set(AtCommit); }
692
693    bool isAtCommit() { return status[AtCommit]; }
694
695    /** Sets this instruction as committed. */
696    void setCommitted() { status.set(Committed); }
697
698    /** Returns whether or not this instruction is committed. */
699    bool isCommitted() const { return status[Committed]; }
700
701    /** Sets this instruction as squashed. */
702    void setSquashed() { status.set(Squashed); }
703
704    /** Returns whether or not this instruction is squashed. */
705    bool isSquashed() const { return status[Squashed]; }
706
707    //Instruction Queue Entry
708    //-----------------------
709    /** Sets this instruction as a entry the IQ. */
710    void setInIQ() { status.set(IqEntry); }
711
712    /** Sets this instruction as a entry the IQ. */
713    void clearInIQ() { status.reset(IqEntry); }
714
715    /** Returns whether or not this instruction has issued. */
716    bool isInIQ() const { return status[IqEntry]; }
717
718    /** Sets this instruction as squashed in the IQ. */
719    void setSquashedInIQ() { status.set(SquashedInIQ); status.set(Squashed);}
720
721    /** Returns whether or not this instruction is squashed in the IQ. */
722    bool isSquashedInIQ() const { return status[SquashedInIQ]; }
723
724
725    //Load / Store Queue Functions
726    //-----------------------
727    /** Sets this instruction as a entry the LSQ. */
728    void setInLSQ() { status.set(LsqEntry); }
729
730    /** Sets this instruction as a entry the LSQ. */
731    void removeInLSQ() { status.reset(LsqEntry); }
732
733    /** Returns whether or not this instruction is in the LSQ. */
734    bool isInLSQ() const { return status[LsqEntry]; }
735
736    /** Sets this instruction as squashed in the LSQ. */
737    void setSquashedInLSQ() { status.set(SquashedInLSQ);}
738
739    /** Returns whether or not this instruction is squashed in the LSQ. */
740    bool isSquashedInLSQ() const { return status[SquashedInLSQ]; }
741
742
743    //Reorder Buffer Functions
744    //-----------------------
745    /** Sets this instruction as a entry the ROB. */
746    void setInROB() { status.set(RobEntry); }
747
748    /** Sets this instruction as a entry the ROB. */
749    void clearInROB() { status.reset(RobEntry); }
750
751    /** Returns whether or not this instruction is in the ROB. */
752    bool isInROB() const { return status[RobEntry]; }
753
754    /** Sets this instruction as squashed in the ROB. */
755    void setSquashedInROB() { status.set(SquashedInROB); }
756
757    /** Returns whether or not this instruction is squashed in the ROB. */
758    bool isSquashedInROB() const { return status[SquashedInROB]; }
759
760    /** Read the PC state of this instruction. */
761    const TheISA::PCState pcState() const { return pc; }
762
763    /** Set the PC state of this instruction. */
764    const void pcState(const TheISA::PCState &val) { pc = val; }
765
766    /** Read the PC of this instruction. */
767    const Addr instAddr() const { return pc.instAddr(); }
768
769    /** Read the PC of the next instruction. */
770    const Addr nextInstAddr() const { return pc.nextInstAddr(); }
771
772    /**Read the micro PC of this instruction. */
773    const Addr microPC() const { return pc.microPC(); }
774
775    bool readPredicate()
776    {
777        return predicate;
778    }
779
780    void setPredicate(bool val)
781    {
782        predicate = val;
783
784        if (traceData) {
785            traceData->setPredicate(val);
786        }
787    }
788
789    /** Sets the ASID. */
790    void setASID(short addr_space_id) { asid = addr_space_id; }
791
792    /** Sets the thread id. */
793    void setTid(ThreadID tid) { threadNumber = tid; }
794
795    /** Sets the pointer to the thread state. */
796    void setThreadState(ImplState *state) { thread = state; }
797
798    /** Returns the thread context. */
799    ThreadContext *tcBase() { return thread->getTC(); }
800
801  private:
802    /** Instruction effective address.
803     *  @todo: Consider if this is necessary or not.
804     */
805    Addr instEffAddr;
806
807    /** Whether or not the effective address calculation is completed.
808     *  @todo: Consider if this is necessary or not.
809     */
810    bool eaCalcDone;
811
812    /** Is this instruction's memory access uncacheable. */
813    bool isUncacheable;
814
815    /** Has this instruction generated a memory request. */
816    bool reqMade;
817
818  public:
819    /** Sets the effective address. */
820    void setEA(Addr &ea) { instEffAddr = ea; eaCalcDone = true; }
821
822    /** Returns the effective address. */
823    const Addr &getEA() const { return instEffAddr; }
824
825    /** Returns whether or not the eff. addr. calculation has been completed. */
826    bool doneEACalc() { return eaCalcDone; }
827
828    /** Returns whether or not the eff. addr. source registers are ready. */
829    bool eaSrcsReady();
830
831    /** Whether or not the memory operation is done. */
832    bool memOpDone;
833
834    /** Is this instruction's memory access uncacheable. */
835    bool uncacheable() { return isUncacheable; }
836
837    /** Has this instruction generated a memory request. */
838    bool hasRequest() { return reqMade; }
839
840  public:
841    /** Load queue index. */
842    int16_t lqIdx;
843
844    /** Store queue index. */
845    int16_t sqIdx;
846
847    /** Iterator pointing to this BaseDynInst in the list of all insts. */
848    ListIt instListIt;
849
850    /** Returns iterator to this instruction in the list of all insts. */
851    ListIt &getInstListIt() { return instListIt; }
852
853    /** Sets iterator for this instruction in the list of all insts. */
854    void setInstListIt(ListIt _instListIt) { instListIt = _instListIt; }
855
856  public:
857    /** Returns the number of consecutive store conditional failures. */
858    unsigned readStCondFailures()
859    { return thread->storeCondFailures; }
860
861    /** Sets the number of consecutive store conditional failures. */
862    void setStCondFailures(unsigned sc_failures)
863    { thread->storeCondFailures = sc_failures; }
864};
865
866template<class Impl>
867Fault
868BaseDynInst<Impl>::readMem(Addr addr, uint8_t *data,
869                           unsigned size, unsigned flags)
870{
871    reqMade = true;
872    Request *req = NULL;
873    Request *sreqLow = NULL;
874    Request *sreqHigh = NULL;
875
876    if (reqMade && translationStarted) {
877        req = savedReq;
878        sreqLow = savedSreqLow;
879        sreqHigh = savedSreqHigh;
880    } else {
881        req = new Request(asid, addr, size, flags, this->pc.instAddr(),
882                          thread->contextId(), threadNumber);
883
884        // Only split the request if the ISA supports unaligned accesses.
885        if (TheISA::HasUnalignedMemAcc) {
886            splitRequest(req, sreqLow, sreqHigh);
887        }
888        initiateTranslation(req, sreqLow, sreqHigh, NULL, BaseTLB::Read);
889    }
890
891    if (translationCompleted) {
892        if (fault == NoFault) {
893            effAddr = req->getVaddr();
894            effSize = size;
895            effAddrValid = true;
896#if USE_CHECKER
897            if (reqToVerify != NULL) {
898                delete reqToVerify;
899            }
900            reqToVerify = new Request(*req);
901#endif //USE_CHECKER
902            fault = cpu->read(req, sreqLow, sreqHigh, data, lqIdx);
903        } else {
904            // Commit will have to clean up whatever happened.  Set this
905            // instruction as executed.
906            this->setExecuted();
907        }
908
909        if (fault != NoFault) {
910            // Return a fixed value to keep simulation deterministic even
911            // along misspeculated paths.
912            if (data)
913                bzero(data, size);
914        }
915    }
916
917    if (traceData) {
918        traceData->setAddr(addr);
919    }
920
921    return fault;
922}
923
924template<class Impl>
925Fault
926BaseDynInst<Impl>::writeMem(uint8_t *data, unsigned size,
927                            Addr addr, unsigned flags, uint64_t *res)
928{
929    if (traceData) {
930        traceData->setAddr(addr);
931    }
932
933    reqMade = true;
934    Request *req = NULL;
935    Request *sreqLow = NULL;
936    Request *sreqHigh = NULL;
937
938    if (reqMade && translationStarted) {
939        req = savedReq;
940        sreqLow = savedSreqLow;
941        sreqHigh = savedSreqHigh;
942    } else {
943        req = new Request(asid, addr, size, flags, this->pc.instAddr(),
944                          thread->contextId(), threadNumber);
945
946        // Only split the request if the ISA supports unaligned accesses.
947        if (TheISA::HasUnalignedMemAcc) {
948            splitRequest(req, sreqLow, sreqHigh);
949        }
950        initiateTranslation(req, sreqLow, sreqHigh, res, BaseTLB::Write);
951    }
952
953    if (fault == NoFault && translationCompleted) {
954        effAddr = req->getVaddr();
955        effSize = size;
956        effAddrValid = true;
957#if USE_CHECKER
958        if (reqToVerify != NULL) {
959            delete reqToVerify;
960        }
961        reqToVerify = new Request(*req);
962#endif // USE_CHECKER
963        fault = cpu->write(req, sreqLow, sreqHigh, data, sqIdx);
964    }
965
966    return fault;
967}
968
969template<class Impl>
970inline void
971BaseDynInst<Impl>::splitRequest(RequestPtr req, RequestPtr &sreqLow,
972                                RequestPtr &sreqHigh)
973{
974    // Check to see if the request crosses the next level block boundary.
975    unsigned block_size = cpu->getDcachePort()->peerBlockSize();
976    Addr addr = req->getVaddr();
977    Addr split_addr = roundDown(addr + req->getSize() - 1, block_size);
978    assert(split_addr <= addr || split_addr - addr < block_size);
979
980    // Spans two blocks.
981    if (split_addr > addr) {
982        req->splitOnVaddr(split_addr, sreqLow, sreqHigh);
983    }
984}
985
986template<class Impl>
987inline void
988BaseDynInst<Impl>::initiateTranslation(RequestPtr req, RequestPtr sreqLow,
989                                       RequestPtr sreqHigh, uint64_t *res,
990                                       BaseTLB::Mode mode)
991{
992    translationStarted = true;
993
994    if (!TheISA::HasUnalignedMemAcc || sreqLow == NULL) {
995        WholeTranslationState *state =
996            new WholeTranslationState(req, NULL, res, mode);
997
998        // One translation if the request isn't split.
999        DataTranslation<BaseDynInstPtr> *trans =
1000            new DataTranslation<BaseDynInstPtr>(this, state);
1001        cpu->dtb->translateTiming(req, thread->getTC(), trans, mode);
1002        if (!translationCompleted) {
1003            // Save memory requests.
1004            savedReq = state->mainReq;
1005            savedSreqLow = state->sreqLow;
1006            savedSreqHigh = state->sreqHigh;
1007        }
1008    } else {
1009        WholeTranslationState *state =
1010            new WholeTranslationState(req, sreqLow, sreqHigh, NULL, res, mode);
1011
1012        // Two translations when the request is split.
1013        DataTranslation<BaseDynInstPtr> *stransLow =
1014            new DataTranslation<BaseDynInstPtr>(this, state, 0);
1015        DataTranslation<BaseDynInstPtr> *stransHigh =
1016            new DataTranslation<BaseDynInstPtr>(this, state, 1);
1017
1018        cpu->dtb->translateTiming(sreqLow, thread->getTC(), stransLow, mode);
1019        cpu->dtb->translateTiming(sreqHigh, thread->getTC(), stransHigh, mode);
1020        if (!translationCompleted) {
1021            // Save memory requests.
1022            savedReq = state->mainReq;
1023            savedSreqLow = state->sreqLow;
1024            savedSreqHigh = state->sreqHigh;
1025        }
1026    }
1027}
1028
1029template<class Impl>
1030inline void
1031BaseDynInst<Impl>::finishTranslation(WholeTranslationState *state)
1032{
1033    fault = state->getFault();
1034
1035    if (state->isUncacheable())
1036        isUncacheable = true;
1037
1038    if (fault == NoFault) {
1039        physEffAddr = state->getPaddr();
1040        memReqFlags = state->getFlags();
1041
1042        if (state->mainReq->isCondSwap()) {
1043            assert(state->res);
1044            state->mainReq->setExtraData(*state->res);
1045        }
1046
1047    } else {
1048        state->deleteReqs();
1049    }
1050    delete state;
1051
1052    translationCompleted = true;
1053}
1054
1055#endif // __CPU_BASE_DYN_INST_HH__
1056