thread_context.hh revision 2680
12SN/A/*
22190SN/A * Copyright (c) 2006 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665SN/A *
282665SN/A * Authors: Kevin Lim
292SN/A */
302SN/A
312680Sktlim@umich.edu#ifndef __CPU_THREAD_CONTEXT_HH__
322680Sktlim@umich.edu#define __CPU_THREAD_CONTEXT_HH__
332SN/A
341858SN/A#include "config/full_system.hh"
352423SN/A#include "mem/request.hh"
362190SN/A#include "sim/faults.hh"
3756SN/A#include "sim/host.hh"
38217SN/A#include "sim/serialize.hh"
392036SN/A#include "sim/byteswap.hh"
402SN/A
412190SN/A// @todo: Figure out a more architecture independent way to obtain the ITB and
422190SN/A// DTB pointers.
432190SN/Aclass AlphaDTB;
442190SN/Aclass AlphaITB;
452190SN/Aclass BaseCPU;
462313SN/Aclass EndQuiesceEvent;
472235SN/Aclass Event;
482423SN/Aclass TranslatingPort;
492521SN/Aclass FunctionalPort;
502521SN/Aclass VirtualPort;
512190SN/Aclass Process;
522190SN/Aclass System;
532330SN/Anamespace Kernel {
542330SN/A    class Statistics;
552330SN/A};
562SN/A
572680Sktlim@umich.edu/**
582680Sktlim@umich.edu * ThreadContext is the external interface to all thread state for
592680Sktlim@umich.edu * anything outside of the CPU. It provides all accessor methods to
602680Sktlim@umich.edu * state that might be needed by external objects, ranging from
612680Sktlim@umich.edu * register values to things such as kernel stats. It is an abstract
622680Sktlim@umich.edu * base class; the CPU can create its own ThreadContext by either
632680Sktlim@umich.edu * deriving from it, or using the templated ProxyThreadContext.
642680Sktlim@umich.edu *
652680Sktlim@umich.edu * The ThreadContext is slightly different than the ExecContext.  The
662680Sktlim@umich.edu * ThreadContext provides access to an individual thread's state; an
672680Sktlim@umich.edu * ExecContext provides ISA access to the CPU (meaning it is
682680Sktlim@umich.edu * implicitly multithreaded on MT systems).  Additionally the
692680Sktlim@umich.edu * ThreadState is an abstract class that exactly defines the
702680Sktlim@umich.edu * interface; the ExecContext is a more implicit interface that must
712680Sktlim@umich.edu * be implemented so that the ISA can access whatever state it needs.
722680Sktlim@umich.edu */
732680Sktlim@umich.educlass ThreadContext
742SN/A{
752107SN/A  protected:
762107SN/A    typedef TheISA::RegFile RegFile;
772107SN/A    typedef TheISA::MachInst MachInst;
782190SN/A    typedef TheISA::IntReg IntReg;
792455SN/A    typedef TheISA::FloatReg FloatReg;
802455SN/A    typedef TheISA::FloatRegBits FloatRegBits;
812107SN/A    typedef TheISA::MiscRegFile MiscRegFile;
822159SN/A    typedef TheISA::MiscReg MiscReg;
832SN/A  public:
84246SN/A    enum Status
85246SN/A    {
86246SN/A        /// Initialized but not running yet.  All CPUs start in
87246SN/A        /// this state, but most transition to Active on cycle 1.
88246SN/A        /// In MP or SMT systems, non-primary contexts will stay
89246SN/A        /// in this state until a thread is assigned to them.
90246SN/A        Unallocated,
91246SN/A
92246SN/A        /// Running.  Instructions should be executed only when
93246SN/A        /// the context is in this state.
94246SN/A        Active,
95246SN/A
96246SN/A        /// Temporarily inactive.  Entered while waiting for
972190SN/A        /// synchronization, etc.
98246SN/A        Suspended,
99246SN/A
100246SN/A        /// Permanently shut down.  Entered when target executes
101246SN/A        /// m5exit pseudo-instruction.  When all contexts enter
102246SN/A        /// this state, the simulation will terminate.
103246SN/A        Halted
104246SN/A    };
1052SN/A
1062680Sktlim@umich.edu    virtual ~ThreadContext() { };
1072423SN/A
1082190SN/A    virtual BaseCPU *getCpuPtr() = 0;
109180SN/A
1102190SN/A    virtual void setCpuId(int id) = 0;
1112190SN/A
1122190SN/A    virtual int readCpuId() = 0;
1132190SN/A
1142190SN/A#if FULL_SYSTEM
1152190SN/A    virtual System *getSystemPtr() = 0;
1162190SN/A
1172190SN/A    virtual AlphaITB *getITBPtr() = 0;
1182190SN/A
1192190SN/A    virtual AlphaDTB * getDTBPtr() = 0;
1202521SN/A
1212330SN/A    virtual Kernel::Statistics *getKernelStats() = 0;
1222654SN/A
1232521SN/A    virtual FunctionalPort *getPhysPort() = 0;
1242521SN/A
1252680Sktlim@umich.edu    virtual VirtualPort *getVirtPort(ThreadContext *tc = NULL) = 0;
1262521SN/A
1272521SN/A    virtual void delVirtPort(VirtualPort *vp) = 0;
1282190SN/A#else
1292518SN/A    virtual TranslatingPort *getMemPort() = 0;
1302518SN/A
1312190SN/A    virtual Process *getProcessPtr() = 0;
1322190SN/A#endif
1332190SN/A
1342190SN/A    virtual Status status() const = 0;
1352159SN/A
1362235SN/A    virtual void setStatus(Status new_status) = 0;
1372103SN/A
138393SN/A    /// Set the status to Active.  Optional delay indicates number of
139393SN/A    /// cycles to wait before beginning execution.
1402190SN/A    virtual void activate(int delay = 1) = 0;
141393SN/A
142393SN/A    /// Set the status to Suspended.
1432190SN/A    virtual void suspend() = 0;
144393SN/A
145393SN/A    /// Set the status to Unallocated.
1462190SN/A    virtual void deallocate() = 0;
147393SN/A
148393SN/A    /// Set the status to Halted.
1492190SN/A    virtual void halt() = 0;
1502159SN/A
1512159SN/A#if FULL_SYSTEM
1522190SN/A    virtual void dumpFuncProfile() = 0;
1532159SN/A#endif
1542159SN/A
1552680Sktlim@umich.edu    virtual void takeOverFrom(ThreadContext *old_context) = 0;
1562159SN/A
1572190SN/A    virtual void regStats(const std::string &name) = 0;
1582159SN/A
1592190SN/A    virtual void serialize(std::ostream &os) = 0;
1602190SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section) = 0;
1612159SN/A
1622235SN/A#if FULL_SYSTEM
1632313SN/A    virtual EndQuiesceEvent *getQuiesceEvent() = 0;
1642235SN/A
1652235SN/A    // Not necessarily the best location for these...
1662235SN/A    // Having an extra function just to read these is obnoxious
1672235SN/A    virtual Tick readLastActivate() = 0;
1682235SN/A    virtual Tick readLastSuspend() = 0;
1692254SN/A
1702254SN/A    virtual void profileClear() = 0;
1712254SN/A    virtual void profileSample() = 0;
1722235SN/A#endif
1732235SN/A
1742190SN/A    virtual int getThreadNum() = 0;
1752159SN/A
1762235SN/A    // Also somewhat obnoxious.  Really only used for the TLB fault.
1772254SN/A    // However, may be quite useful in SPARC.
1782190SN/A    virtual TheISA::MachInst getInst() = 0;
1792159SN/A
1802680Sktlim@umich.edu    virtual void copyArchRegs(ThreadContext *tc) = 0;
1812159SN/A
1822190SN/A    virtual void clearArchRegs() = 0;
1832159SN/A
1842159SN/A    //
1852159SN/A    // New accessors for new decoder.
1862159SN/A    //
1872190SN/A    virtual uint64_t readIntReg(int reg_idx) = 0;
1882159SN/A
1892455SN/A    virtual FloatReg readFloatReg(int reg_idx, int width) = 0;
1902159SN/A
1912455SN/A    virtual FloatReg readFloatReg(int reg_idx) = 0;
1922159SN/A
1932455SN/A    virtual FloatRegBits readFloatRegBits(int reg_idx, int width) = 0;
1942455SN/A
1952455SN/A    virtual FloatRegBits readFloatRegBits(int reg_idx) = 0;
1962159SN/A
1972190SN/A    virtual void setIntReg(int reg_idx, uint64_t val) = 0;
1982159SN/A
1992455SN/A    virtual void setFloatReg(int reg_idx, FloatReg val, int width) = 0;
2002159SN/A
2012455SN/A    virtual void setFloatReg(int reg_idx, FloatReg val) = 0;
2022159SN/A
2032455SN/A    virtual void setFloatRegBits(int reg_idx, FloatRegBits val) = 0;
2042455SN/A
2052455SN/A    virtual void setFloatRegBits(int reg_idx, FloatRegBits val, int width) = 0;
2062159SN/A
2072190SN/A    virtual uint64_t readPC() = 0;
2082159SN/A
2092190SN/A    virtual void setPC(uint64_t val) = 0;
2102159SN/A
2112190SN/A    virtual uint64_t readNextPC() = 0;
2122159SN/A
2132190SN/A    virtual void setNextPC(uint64_t val) = 0;
2142159SN/A
2152447SN/A    virtual uint64_t readNextNPC() = 0;
2162447SN/A
2172447SN/A    virtual void setNextNPC(uint64_t val) = 0;
2182447SN/A
2192190SN/A    virtual MiscReg readMiscReg(int misc_reg) = 0;
2202159SN/A
2212190SN/A    virtual MiscReg readMiscRegWithEffect(int misc_reg, Fault &fault) = 0;
2222190SN/A
2232190SN/A    virtual Fault setMiscReg(int misc_reg, const MiscReg &val) = 0;
2242190SN/A
2252190SN/A    virtual Fault setMiscRegWithEffect(int misc_reg, const MiscReg &val) = 0;
2262190SN/A
2272235SN/A    // Also not necessarily the best location for these two.  Hopefully will go
2282235SN/A    // away once we decide upon where st cond failures goes.
2292190SN/A    virtual unsigned readStCondFailures() = 0;
2302190SN/A
2312190SN/A    virtual void setStCondFailures(unsigned sc_failures) = 0;
2322159SN/A
2332159SN/A#if FULL_SYSTEM
2342190SN/A    virtual bool inPalMode() = 0;
2352159SN/A#endif
2362159SN/A
2372235SN/A    // Only really makes sense for old CPU model.  Still could be useful though.
2382190SN/A    virtual bool misspeculating() = 0;
2392190SN/A
2402159SN/A#if !FULL_SYSTEM
2412190SN/A    virtual IntReg getSyscallArg(int i) = 0;
2422159SN/A
2432159SN/A    // used to shift args for indirect syscall
2442190SN/A    virtual void setSyscallArg(int i, IntReg val) = 0;
2452159SN/A
2462190SN/A    virtual void setSyscallReturn(SyscallReturn return_value) = 0;
2472159SN/A
2482190SN/A
2492235SN/A    // Same with st cond failures.
2502190SN/A    virtual Counter readFuncExeInst() = 0;
2512159SN/A#endif
2522525SN/A
2532525SN/A    virtual void changeRegFileContext(RegFile::ContextParam param,
2542525SN/A            RegFile::ContextVal val) = 0;
2552159SN/A};
2562159SN/A
2572680Sktlim@umich.edutemplate <class TC>
2582680Sktlim@umich.educlass ProxyThreadContext : public ThreadContext
2592190SN/A{
2602190SN/A  public:
2612680Sktlim@umich.edu    ProxyThreadContext(TC *actual_tc)
2622680Sktlim@umich.edu    { actualTC = actual_tc; }
2632159SN/A
2642190SN/A  private:
2652680Sktlim@umich.edu    TC *actualTC;
2662SN/A
2672SN/A  public:
2682SN/A
2692680Sktlim@umich.edu    BaseCPU *getCpuPtr() { return actualTC->getCpuPtr(); }
2702SN/A
2712680Sktlim@umich.edu    void setCpuId(int id) { actualTC->setCpuId(id); }
272716SN/A
2732680Sktlim@umich.edu    int readCpuId() { return actualTC->readCpuId(); }
2742SN/A
2751858SN/A#if FULL_SYSTEM
2762680Sktlim@umich.edu    System *getSystemPtr() { return actualTC->getSystemPtr(); }
2772SN/A
2782680Sktlim@umich.edu    AlphaITB *getITBPtr() { return actualTC->getITBPtr(); }
2791917SN/A
2802680Sktlim@umich.edu    AlphaDTB *getDTBPtr() { return actualTC->getDTBPtr(); }
2812521SN/A
2822680Sktlim@umich.edu    Kernel::Statistics *getKernelStats() { return actualTC->getKernelStats(); }
2832654SN/A
2842680Sktlim@umich.edu    FunctionalPort *getPhysPort() { return actualTC->getPhysPort(); }
2852521SN/A
2862680Sktlim@umich.edu    VirtualPort *getVirtPort(ThreadContext *tc = NULL) { return actualTC->getVirtPort(tc); }
2872521SN/A
2882680Sktlim@umich.edu    void delVirtPort(VirtualPort *vp) { return actualTC->delVirtPort(vp); }
2892SN/A#else
2902680Sktlim@umich.edu    TranslatingPort *getMemPort() { return actualTC->getMemPort(); }
2912518SN/A
2922680Sktlim@umich.edu    Process *getProcessPtr() { return actualTC->getProcessPtr(); }
2932SN/A#endif
2942SN/A
2952680Sktlim@umich.edu    Status status() const { return actualTC->status(); }
296595SN/A
2972680Sktlim@umich.edu    void setStatus(Status new_status) { actualTC->setStatus(new_status); }
2982SN/A
2992190SN/A    /// Set the status to Active.  Optional delay indicates number of
3002190SN/A    /// cycles to wait before beginning execution.
3012680Sktlim@umich.edu    void activate(int delay = 1) { actualTC->activate(delay); }
3022SN/A
3032190SN/A    /// Set the status to Suspended.
3042680Sktlim@umich.edu    void suspend() { actualTC->suspend(); }
3052SN/A
3062190SN/A    /// Set the status to Unallocated.
3072680Sktlim@umich.edu    void deallocate() { actualTC->deallocate(); }
3082SN/A
3092190SN/A    /// Set the status to Halted.
3102680Sktlim@umich.edu    void halt() { actualTC->halt(); }
311217SN/A
3121858SN/A#if FULL_SYSTEM
3132680Sktlim@umich.edu    void dumpFuncProfile() { actualTC->dumpFuncProfile(); }
3142190SN/A#endif
3152190SN/A
3162680Sktlim@umich.edu    void takeOverFrom(ThreadContext *oldContext)
3172680Sktlim@umich.edu    { actualTC->takeOverFrom(oldContext); }
3182190SN/A
3192680Sktlim@umich.edu    void regStats(const std::string &name) { actualTC->regStats(name); }
3202190SN/A
3212680Sktlim@umich.edu    void serialize(std::ostream &os) { actualTC->serialize(os); }
3222190SN/A    void unserialize(Checkpoint *cp, const std::string &section)
3232680Sktlim@umich.edu    { actualTC->unserialize(cp, section); }
3242190SN/A
3252235SN/A#if FULL_SYSTEM
3262680Sktlim@umich.edu    EndQuiesceEvent *getQuiesceEvent() { return actualTC->getQuiesceEvent(); }
3272235SN/A
3282680Sktlim@umich.edu    Tick readLastActivate() { return actualTC->readLastActivate(); }
3292680Sktlim@umich.edu    Tick readLastSuspend() { return actualTC->readLastSuspend(); }
3302254SN/A
3312680Sktlim@umich.edu    void profileClear() { return actualTC->profileClear(); }
3322680Sktlim@umich.edu    void profileSample() { return actualTC->profileSample(); }
3332235SN/A#endif
3342235SN/A
3352680Sktlim@umich.edu    int getThreadNum() { return actualTC->getThreadNum(); }
3362190SN/A
3372190SN/A    // @todo: Do I need this?
3382680Sktlim@umich.edu    MachInst getInst() { return actualTC->getInst(); }
3392SN/A
3402190SN/A    // @todo: Do I need this?
3412680Sktlim@umich.edu    void copyArchRegs(ThreadContext *tc) { actualTC->copyArchRegs(tc); }
3422SN/A
3432680Sktlim@umich.edu    void clearArchRegs() { actualTC->clearArchRegs(); }
344716SN/A
3452SN/A    //
3462SN/A    // New accessors for new decoder.
3472SN/A    //
3482SN/A    uint64_t readIntReg(int reg_idx)
3492680Sktlim@umich.edu    { return actualTC->readIntReg(reg_idx); }
3502SN/A
3512455SN/A    FloatReg readFloatReg(int reg_idx, int width)
3522680Sktlim@umich.edu    { return actualTC->readFloatReg(reg_idx, width); }
3532SN/A
3542455SN/A    FloatReg readFloatReg(int reg_idx)
3552680Sktlim@umich.edu    { return actualTC->readFloatReg(reg_idx); }
3562SN/A
3572455SN/A    FloatRegBits readFloatRegBits(int reg_idx, int width)
3582680Sktlim@umich.edu    { return actualTC->readFloatRegBits(reg_idx, width); }
3592455SN/A
3602455SN/A    FloatRegBits readFloatRegBits(int reg_idx)
3612680Sktlim@umich.edu    { return actualTC->readFloatRegBits(reg_idx); }
3622SN/A
3632SN/A    void setIntReg(int reg_idx, uint64_t val)
3642680Sktlim@umich.edu    { actualTC->setIntReg(reg_idx, val); }
3652SN/A
3662455SN/A    void setFloatReg(int reg_idx, FloatReg val, int width)
3672680Sktlim@umich.edu    { actualTC->setFloatReg(reg_idx, val, width); }
3682SN/A
3692455SN/A    void setFloatReg(int reg_idx, FloatReg val)
3702680Sktlim@umich.edu    { actualTC->setFloatReg(reg_idx, val); }
3712SN/A
3722455SN/A    void setFloatRegBits(int reg_idx, FloatRegBits val, int width)
3732680Sktlim@umich.edu    { actualTC->setFloatRegBits(reg_idx, val, width); }
3742455SN/A
3752455SN/A    void setFloatRegBits(int reg_idx, FloatRegBits val)
3762680Sktlim@umich.edu    { actualTC->setFloatRegBits(reg_idx, val); }
3772SN/A
3782680Sktlim@umich.edu    uint64_t readPC() { return actualTC->readPC(); }
3792SN/A
3802680Sktlim@umich.edu    void setPC(uint64_t val) { actualTC->setPC(val); }
3812206SN/A
3822680Sktlim@umich.edu    uint64_t readNextPC() { return actualTC->readNextPC(); }
3832252SN/A
3842680Sktlim@umich.edu    void setNextPC(uint64_t val) { actualTC->setNextPC(val); }
3852SN/A
3862680Sktlim@umich.edu    uint64_t readNextNPC() { return actualTC->readNextNPC(); }
3872447SN/A
3882680Sktlim@umich.edu    void setNextNPC(uint64_t val) { actualTC->setNextNPC(val); }
3892447SN/A
3902159SN/A    MiscReg readMiscReg(int misc_reg)
3912680Sktlim@umich.edu    { return actualTC->readMiscReg(misc_reg); }
3922SN/A
3932159SN/A    MiscReg readMiscRegWithEffect(int misc_reg, Fault &fault)
3942680Sktlim@umich.edu    { return actualTC->readMiscRegWithEffect(misc_reg, fault); }
3952SN/A
3962159SN/A    Fault setMiscReg(int misc_reg, const MiscReg &val)
3972680Sktlim@umich.edu    { return actualTC->setMiscReg(misc_reg, val); }
3982SN/A
3992159SN/A    Fault setMiscRegWithEffect(int misc_reg, const MiscReg &val)
4002680Sktlim@umich.edu    { return actualTC->setMiscRegWithEffect(misc_reg, val); }
4012190SN/A
4022190SN/A    unsigned readStCondFailures()
4032680Sktlim@umich.edu    { return actualTC->readStCondFailures(); }
4042190SN/A
4052190SN/A    void setStCondFailures(unsigned sc_failures)
4062680Sktlim@umich.edu    { actualTC->setStCondFailures(sc_failures); }
4071858SN/A#if FULL_SYSTEM
4082680Sktlim@umich.edu    bool inPalMode() { return actualTC->inPalMode(); }
4092SN/A#endif
4102SN/A
4112190SN/A    // @todo: Fix this!
4122680Sktlim@umich.edu    bool misspeculating() { return actualTC->misspeculating(); }
4132190SN/A
4141858SN/A#if !FULL_SYSTEM
4152680Sktlim@umich.edu    IntReg getSyscallArg(int i) { return actualTC->getSyscallArg(i); }
416360SN/A
417360SN/A    // used to shift args for indirect syscall
4182190SN/A    void setSyscallArg(int i, IntReg val)
4192680Sktlim@umich.edu    { actualTC->setSyscallArg(i, val); }
420360SN/A
4211450SN/A    void setSyscallReturn(SyscallReturn return_value)
4222680Sktlim@umich.edu    { actualTC->setSyscallReturn(return_value); }
423360SN/A
4242190SN/A
4252680Sktlim@umich.edu    Counter readFuncExeInst() { return actualTC->readFuncExeInst(); }
4262SN/A#endif
4272525SN/A
4282525SN/A    void changeRegFileContext(RegFile::ContextParam param,
4292525SN/A            RegFile::ContextVal val)
4302525SN/A    {
4312680Sktlim@umich.edu        actualTC->changeRegFileContext(param, val);
4322525SN/A    }
4332SN/A};
4342SN/A
4352190SN/A#endif
436