thread_context.hh revision 5714
12817Sksewell@umich.edu/*
22817Sksewell@umich.edu * Copyright (c) 2004-2006 The Regents of The University of Michigan
32817Sksewell@umich.edu * All rights reserved.
42817Sksewell@umich.edu *
52817Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
62817Sksewell@umich.edu * modification, are permitted provided that the following conditions are
72817Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
82817Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
92817Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
102817Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
112817Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
122817Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
132817Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
142817Sksewell@umich.edu * this software without specific prior written permission.
152817Sksewell@umich.edu *
162817Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172817Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182817Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192817Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202817Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212817Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222817Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232817Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242817Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252817Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262817Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272817Sksewell@umich.edu *
282817Sksewell@umich.edu * Authors: Kevin Lim
292817Sksewell@umich.edu */
302817Sksewell@umich.edu
312817Sksewell@umich.edu#ifndef __CPU_O3_THREAD_CONTEXT_HH__
322817Sksewell@umich.edu#define __CPU_O3_THREAD_CONTEXT_HH__
332817Sksewell@umich.edu
342935Sksewell@umich.edu#include "cpu/thread_context.hh"
352817Sksewell@umich.edu#include "cpu/o3/isa_specific.hh"
362817Sksewell@umich.edu
372834Sksewell@umich.educlass EndQuiesceEvent;
382834Sksewell@umich.edunamespace Kernel {
392834Sksewell@umich.edu    class Statistics;
402834Sksewell@umich.edu};
412834Sksewell@umich.edu
422834Sksewell@umich.educlass TranslatingPort;
432834Sksewell@umich.edu
442817Sksewell@umich.edu/**
452817Sksewell@umich.edu * Derived ThreadContext class for use with the O3CPU.  It
462817Sksewell@umich.edu * provides the interface for any external objects to access a
472817Sksewell@umich.edu * single thread's state and some general CPU state.  Any time
482817Sksewell@umich.edu * external objects try to update state through this interface,
492817Sksewell@umich.edu * the CPU will create an event to squash all in-flight
502817Sksewell@umich.edu * instructions in order to ensure state is maintained correctly.
512817Sksewell@umich.edu * It must be defined specifically for the O3CPU because
522817Sksewell@umich.edu * not all architectural state is located within the O3ThreadState
532817Sksewell@umich.edu * (such as the commit PC, and registers), and specific actions
542817Sksewell@umich.edu * must be taken when using this interface (such as squashing all
552817Sksewell@umich.edu * in-flight instructions when doing a write to this interface).
562817Sksewell@umich.edu */
572817Sksewell@umich.edutemplate <class Impl>
582817Sksewell@umich.educlass O3ThreadContext : public ThreadContext
592817Sksewell@umich.edu{
602817Sksewell@umich.edu  public:
612817Sksewell@umich.edu    typedef typename Impl::O3CPU O3CPU;
622817Sksewell@umich.edu
632817Sksewell@umich.edu   /** Pointer to the CPU. */
642817Sksewell@umich.edu    O3CPU *cpu;
652817Sksewell@umich.edu
662817Sksewell@umich.edu    /** Pointer to the thread state that this TC corrseponds to. */
672817Sksewell@umich.edu    O3ThreadState<Impl> *thread;
682817Sksewell@umich.edu
693784Sgblack@eecs.umich.edu    /** Returns a pointer to the ITB. */
703789Sgblack@eecs.umich.edu    TheISA::ITB *getITBPtr() { return cpu->itb; }
713784Sgblack@eecs.umich.edu
723784Sgblack@eecs.umich.edu    /** Returns a pointer to the DTB. */
733789Sgblack@eecs.umich.edu    TheISA::DTB *getDTBPtr() { return cpu->dtb; }
743784Sgblack@eecs.umich.edu
752817Sksewell@umich.edu    /** Returns a pointer to this CPU. */
762817Sksewell@umich.edu    virtual BaseCPU *getCpuPtr() { return cpu; }
772817Sksewell@umich.edu
782817Sksewell@umich.edu    /** Reads this CPU's ID. */
795712Shsul@eecs.umich.edu    virtual int cpuId() { return cpu->cpuId(); }
802817Sksewell@umich.edu
815714Shsul@eecs.umich.edu    virtual int contextId() { return thread->contextId(); }
825714Shsul@eecs.umich.edu
835714Shsul@eecs.umich.edu    virtual void setContextId(int id) { thread->setContextId(id); }
845714Shsul@eecs.umich.edu
852817Sksewell@umich.edu#if FULL_SYSTEM
862817Sksewell@umich.edu    /** Returns a pointer to the system. */
872817Sksewell@umich.edu    virtual System *getSystemPtr() { return cpu->system; }
882817Sksewell@umich.edu
892817Sksewell@umich.edu    /** Returns a pointer to physical memory. */
902817Sksewell@umich.edu    virtual PhysicalMemory *getPhysMemPtr() { return cpu->physmem; }
912817Sksewell@umich.edu
922817Sksewell@umich.edu    /** Returns a pointer to this thread's kernel statistics. */
933548Sgblack@eecs.umich.edu    virtual TheISA::Kernel::Statistics *getKernelStats()
942817Sksewell@umich.edu    { return thread->kernelStats; }
952817Sksewell@umich.edu
962817Sksewell@umich.edu    virtual FunctionalPort *getPhysPort() { return thread->getPhysPort(); }
972817Sksewell@umich.edu
985499Ssaidi@eecs.umich.edu    virtual VirtualPort *getVirtPort();
993675Sktlim@umich.edu
1005497Ssaidi@eecs.umich.edu    virtual void connectMemPorts(ThreadContext *tc) { thread->connectMemPorts(tc); }
1012817Sksewell@umich.edu#else
1022817Sksewell@umich.edu    virtual TranslatingPort *getMemPort() { return thread->getMemPort(); }
1032817Sksewell@umich.edu
1042817Sksewell@umich.edu    /** Returns a pointer to this thread's process. */
1052817Sksewell@umich.edu    virtual Process *getProcessPtr() { return thread->getProcessPtr(); }
1062817Sksewell@umich.edu#endif
1072817Sksewell@umich.edu    /** Returns this thread's status. */
1082817Sksewell@umich.edu    virtual Status status() const { return thread->status(); }
1092817Sksewell@umich.edu
1102817Sksewell@umich.edu    /** Sets this thread's status. */
1112817Sksewell@umich.edu    virtual void setStatus(Status new_status)
1122817Sksewell@umich.edu    { thread->setStatus(new_status); }
1132817Sksewell@umich.edu
1142817Sksewell@umich.edu    /** Set the status to Active.  Optional delay indicates number of
1152817Sksewell@umich.edu     * cycles to wait before beginning execution. */
1162817Sksewell@umich.edu    virtual void activate(int delay = 1);
1172817Sksewell@umich.edu
1182817Sksewell@umich.edu    /** Set the status to Suspended. */
1195250Sksewell@umich.edu    virtual void suspend(int delay = 0);
1202817Sksewell@umich.edu
1212817Sksewell@umich.edu    /** Set the status to Unallocated. */
1222875Sksewell@umich.edu    virtual void deallocate(int delay = 0);
1232817Sksewell@umich.edu
1242817Sksewell@umich.edu    /** Set the status to Halted. */
1255250Sksewell@umich.edu    virtual void halt(int delay = 0);
1262817Sksewell@umich.edu
1272817Sksewell@umich.edu#if FULL_SYSTEM
1282817Sksewell@umich.edu    /** Dumps the function profiling information.
1292817Sksewell@umich.edu     * @todo: Implement.
1302817Sksewell@umich.edu     */
1312817Sksewell@umich.edu    virtual void dumpFuncProfile();
1322817Sksewell@umich.edu#endif
1332817Sksewell@umich.edu    /** Takes over execution of a thread from another CPU. */
1342817Sksewell@umich.edu    virtual void takeOverFrom(ThreadContext *old_context);
1352817Sksewell@umich.edu
1362817Sksewell@umich.edu    /** Registers statistics associated with this TC. */
1372817Sksewell@umich.edu    virtual void regStats(const std::string &name);
1382817Sksewell@umich.edu
1392817Sksewell@umich.edu    /** Serializes state. */
1402817Sksewell@umich.edu    virtual void serialize(std::ostream &os);
1412817Sksewell@umich.edu    /** Unserializes state. */
1422817Sksewell@umich.edu    virtual void unserialize(Checkpoint *cp, const std::string &section);
1432817Sksewell@umich.edu
1442817Sksewell@umich.edu#if FULL_SYSTEM
1452817Sksewell@umich.edu    /** Reads the last tick that this thread was activated on. */
1462817Sksewell@umich.edu    virtual Tick readLastActivate();
1472817Sksewell@umich.edu    /** Reads the last tick that this thread was suspended on. */
1482817Sksewell@umich.edu    virtual Tick readLastSuspend();
1492817Sksewell@umich.edu
1502817Sksewell@umich.edu    /** Clears the function profiling information. */
1512817Sksewell@umich.edu    virtual void profileClear();
1522817Sksewell@umich.edu    /** Samples the function profiling information. */
1532817Sksewell@umich.edu    virtual void profileSample();
1542817Sksewell@umich.edu#endif
1552817Sksewell@umich.edu    /** Returns this thread's ID number. */
1562817Sksewell@umich.edu    virtual int getThreadNum() { return thread->readTid(); }
1572817Sksewell@umich.edu
1582817Sksewell@umich.edu    /** Returns the instruction this thread is currently committing.
1592817Sksewell@umich.edu     *  Only used when an instruction faults.
1602817Sksewell@umich.edu     */
1612817Sksewell@umich.edu    virtual TheISA::MachInst getInst();
1622817Sksewell@umich.edu
1632817Sksewell@umich.edu    /** Copies the architectural registers from another TC into this TC. */
1642817Sksewell@umich.edu    virtual void copyArchRegs(ThreadContext *tc);
1652817Sksewell@umich.edu
1662817Sksewell@umich.edu    /** Resets all architectural registers to 0. */
1672817Sksewell@umich.edu    virtual void clearArchRegs();
1682817Sksewell@umich.edu
1692817Sksewell@umich.edu    /** Reads an integer register. */
1702817Sksewell@umich.edu    virtual uint64_t readIntReg(int reg_idx);
1712817Sksewell@umich.edu
1722817Sksewell@umich.edu    virtual FloatReg readFloatReg(int reg_idx, int width);
1732817Sksewell@umich.edu
1742817Sksewell@umich.edu    virtual FloatReg readFloatReg(int reg_idx);
1752817Sksewell@umich.edu
1762817Sksewell@umich.edu    virtual FloatRegBits readFloatRegBits(int reg_idx, int width);
1772817Sksewell@umich.edu
1782817Sksewell@umich.edu    virtual FloatRegBits readFloatRegBits(int reg_idx);
1792817Sksewell@umich.edu
1802817Sksewell@umich.edu    /** Sets an integer register to a value. */
1812817Sksewell@umich.edu    virtual void setIntReg(int reg_idx, uint64_t val);
1822817Sksewell@umich.edu
1832817Sksewell@umich.edu    virtual void setFloatReg(int reg_idx, FloatReg val, int width);
1842817Sksewell@umich.edu
1852817Sksewell@umich.edu    virtual void setFloatReg(int reg_idx, FloatReg val);
1862817Sksewell@umich.edu
1872817Sksewell@umich.edu    virtual void setFloatRegBits(int reg_idx, FloatRegBits val, int width);
1882817Sksewell@umich.edu
1892817Sksewell@umich.edu    virtual void setFloatRegBits(int reg_idx, FloatRegBits val);
1902817Sksewell@umich.edu
1912817Sksewell@umich.edu    /** Reads this thread's PC. */
1922817Sksewell@umich.edu    virtual uint64_t readPC()
1932817Sksewell@umich.edu    { return cpu->readPC(thread->readTid()); }
1942817Sksewell@umich.edu
1952817Sksewell@umich.edu    /** Sets this thread's PC. */
1962817Sksewell@umich.edu    virtual void setPC(uint64_t val);
1972817Sksewell@umich.edu
1982817Sksewell@umich.edu    /** Reads this thread's next PC. */
1992817Sksewell@umich.edu    virtual uint64_t readNextPC()
2002817Sksewell@umich.edu    { return cpu->readNextPC(thread->readTid()); }
2012817Sksewell@umich.edu
2022817Sksewell@umich.edu    /** Sets this thread's next PC. */
2032817Sksewell@umich.edu    virtual void setNextPC(uint64_t val);
2042817Sksewell@umich.edu
2055259Sksewell@umich.edu    virtual uint64_t readMicroPC()
2065259Sksewell@umich.edu    { return cpu->readMicroPC(thread->readTid()); }
2075259Sksewell@umich.edu
2085259Sksewell@umich.edu    virtual void setMicroPC(uint64_t val);
2095259Sksewell@umich.edu
2105259Sksewell@umich.edu    virtual uint64_t readNextMicroPC()
2115259Sksewell@umich.edu    { return cpu->readNextMicroPC(thread->readTid()); }
2125259Sksewell@umich.edu
2135259Sksewell@umich.edu    virtual void setNextMicroPC(uint64_t val);
2145259Sksewell@umich.edu
2152817Sksewell@umich.edu    /** Reads a miscellaneous register. */
2164172Ssaidi@eecs.umich.edu    virtual MiscReg readMiscRegNoEffect(int misc_reg)
2174172Ssaidi@eecs.umich.edu    { return cpu->readMiscRegNoEffect(misc_reg, thread->readTid()); }
2184172Ssaidi@eecs.umich.edu
2194172Ssaidi@eecs.umich.edu    /** Reads a misc. register, including any side-effects the
2204172Ssaidi@eecs.umich.edu     * read might have as defined by the architecture. */
2212817Sksewell@umich.edu    virtual MiscReg readMiscReg(int misc_reg)
2222817Sksewell@umich.edu    { return cpu->readMiscReg(misc_reg, thread->readTid()); }
2232817Sksewell@umich.edu
2242817Sksewell@umich.edu    /** Sets a misc. register. */
2254172Ssaidi@eecs.umich.edu    virtual void setMiscRegNoEffect(int misc_reg, const MiscReg &val);
2262817Sksewell@umich.edu
2272817Sksewell@umich.edu    /** Sets a misc. register, including any side-effects the
2282817Sksewell@umich.edu     * write might have as defined by the architecture. */
2294172Ssaidi@eecs.umich.edu    virtual void setMiscReg(int misc_reg, const MiscReg &val);
2302817Sksewell@umich.edu
2312817Sksewell@umich.edu    /** Returns the number of consecutive store conditional failures. */
2322817Sksewell@umich.edu    // @todo: Figure out where these store cond failures should go.
2332817Sksewell@umich.edu    virtual unsigned readStCondFailures()
2342817Sksewell@umich.edu    { return thread->storeCondFailures; }
2352817Sksewell@umich.edu
2362817Sksewell@umich.edu    /** Sets the number of consecutive store conditional failures. */
2372817Sksewell@umich.edu    virtual void setStCondFailures(unsigned sc_failures)
2382817Sksewell@umich.edu    { thread->storeCondFailures = sc_failures; }
2392817Sksewell@umich.edu
2402817Sksewell@umich.edu    // Only really makes sense for old CPU model.  Lots of code
2412817Sksewell@umich.edu    // outside the CPU still checks this function, so it will
2422817Sksewell@umich.edu    // always return false to keep everything working.
2432817Sksewell@umich.edu    /** Checks if the thread is misspeculating.  Because it is
2442817Sksewell@umich.edu     * very difficult to determine if the thread is
2452817Sksewell@umich.edu     * misspeculating, this is set as false. */
2462817Sksewell@umich.edu    virtual bool misspeculating() { return false; }
2472817Sksewell@umich.edu
2482817Sksewell@umich.edu#if !FULL_SYSTEM
2492817Sksewell@umich.edu    /** Gets a syscall argument by index. */
2502817Sksewell@umich.edu    virtual IntReg getSyscallArg(int i);
2512817Sksewell@umich.edu
2522817Sksewell@umich.edu    /** Sets a syscall argument. */
2532817Sksewell@umich.edu    virtual void setSyscallArg(int i, IntReg val);
2542817Sksewell@umich.edu
2552817Sksewell@umich.edu    /** Sets the syscall return value. */
2562817Sksewell@umich.edu    virtual void setSyscallReturn(SyscallReturn return_value);
2572817Sksewell@umich.edu
2582817Sksewell@umich.edu    /** Executes a syscall in SE mode. */
2592817Sksewell@umich.edu    virtual void syscall(int64_t callnum)
2602817Sksewell@umich.edu    { return cpu->syscall(callnum, thread->readTid()); }
2612817Sksewell@umich.edu
2622817Sksewell@umich.edu    /** Reads the funcExeInst counter. */
2632817Sksewell@umich.edu    virtual Counter readFuncExeInst() { return thread->funcExeInst; }
2645595Sgblack@eecs.umich.edu#else
2655595Sgblack@eecs.umich.edu    /** Returns pointer to the quiesce event. */
2665595Sgblack@eecs.umich.edu    virtual EndQuiesceEvent *getQuiesceEvent()
2675595Sgblack@eecs.umich.edu    {
2685595Sgblack@eecs.umich.edu        return this->thread->quiesceEvent;
2695595Sgblack@eecs.umich.edu    }
2702817Sksewell@umich.edu#endif
2715595Sgblack@eecs.umich.edu
2725595Sgblack@eecs.umich.edu    virtual uint64_t readNextNPC()
2735595Sgblack@eecs.umich.edu    {
2745595Sgblack@eecs.umich.edu        return this->cpu->readNextNPC(this->thread->readTid());
2755595Sgblack@eecs.umich.edu    }
2765595Sgblack@eecs.umich.edu
2775595Sgblack@eecs.umich.edu    virtual void setNextNPC(uint64_t val)
2785595Sgblack@eecs.umich.edu    {
2795595Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA
2805595Sgblack@eecs.umich.edu        panic("Not supported on Alpha!");
2815595Sgblack@eecs.umich.edu#endif
2825595Sgblack@eecs.umich.edu        this->cpu->setNextNPC(val, this->thread->readTid());
2835595Sgblack@eecs.umich.edu    }
2845595Sgblack@eecs.umich.edu
2855595Sgblack@eecs.umich.edu    /** This function exits the thread context in the CPU and returns
2865595Sgblack@eecs.umich.edu     * 1 if the CPU has no more active threads (meaning it's OK to exit);
2875595Sgblack@eecs.umich.edu     * Used in syscall-emulation mode when a thread executes the 'exit'
2885595Sgblack@eecs.umich.edu     * syscall.
2895595Sgblack@eecs.umich.edu     */
2905595Sgblack@eecs.umich.edu    virtual int exit()
2915595Sgblack@eecs.umich.edu    {
2925595Sgblack@eecs.umich.edu        this->deallocate();
2935595Sgblack@eecs.umich.edu
2945595Sgblack@eecs.umich.edu        // If there are still threads executing in the system
2955595Sgblack@eecs.umich.edu        if (this->cpu->numActiveThreads())
2965595Sgblack@eecs.umich.edu            return 0; // don't exit simulation
2975595Sgblack@eecs.umich.edu        else
2985595Sgblack@eecs.umich.edu            return 1; // exit simulation
2995595Sgblack@eecs.umich.edu    }
3002817Sksewell@umich.edu};
3012817Sksewell@umich.edu
3022817Sksewell@umich.edu#endif
303