simple_thread.hh revision 7678:f19b6a3a8cec
16019Shines@cs.fsu.edu/*
27091Sgblack@eecs.umich.edu * Copyright (c) 2001-2006 The Regents of The University of Michigan
37091Sgblack@eecs.umich.edu * All rights reserved.
47091Sgblack@eecs.umich.edu *
57091Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
67091Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
77091Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
87091Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
97091Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
107091Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
117091Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
127091Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137091Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
146019Shines@cs.fsu.edu * this software without specific prior written permission.
156019Shines@cs.fsu.edu *
166019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276019Shines@cs.fsu.edu *
286019Shines@cs.fsu.edu * Authors: Steve Reinhardt
296019Shines@cs.fsu.edu *          Nathan Binkert
306019Shines@cs.fsu.edu */
316019Shines@cs.fsu.edu
326019Shines@cs.fsu.edu#ifndef __CPU_SIMPLE_THREAD_HH__
336019Shines@cs.fsu.edu#define __CPU_SIMPLE_THREAD_HH__
346019Shines@cs.fsu.edu
356019Shines@cs.fsu.edu#include "arch/isa.hh"
366019Shines@cs.fsu.edu#include "arch/isa_traits.hh"
376019Shines@cs.fsu.edu#include "arch/registers.hh"
386019Shines@cs.fsu.edu#include "arch/tlb.hh"
396019Shines@cs.fsu.edu#include "arch/types.hh"
406019Shines@cs.fsu.edu#include "base/types.hh"
416019Shines@cs.fsu.edu#include "config/full_system.hh"
426019Shines@cs.fsu.edu#include "config/the_isa.hh"
436019Shines@cs.fsu.edu#include "cpu/thread_context.hh"
446019Shines@cs.fsu.edu#include "cpu/thread_state.hh"
456019Shines@cs.fsu.edu#include "mem/request.hh"
466019Shines@cs.fsu.edu#include "sim/byteswap.hh"
476019Shines@cs.fsu.edu#include "sim/eventq.hh"
486019Shines@cs.fsu.edu#include "sim/serialize.hh"
496019Shines@cs.fsu.edu
507639Sgblack@eecs.umich.educlass BaseCPU;
516019Shines@cs.fsu.edu
526019Shines@cs.fsu.edu#if FULL_SYSTEM
536019Shines@cs.fsu.edu
546019Shines@cs.fsu.edu#include "sim/system.hh"
556312Sgblack@eecs.umich.edu
566312Sgblack@eecs.umich.educlass FunctionProfile;
577147Sgblack@eecs.umich.educlass ProfileNode;
586312Sgblack@eecs.umich.educlass FunctionalPort;
596312Sgblack@eecs.umich.educlass PhysicalPort;
607186Sgblack@eecs.umich.edu
617186Sgblack@eecs.umich.edunamespace TheISA {
627186Sgblack@eecs.umich.edu    namespace Kernel {
637186Sgblack@eecs.umich.edu        class Statistics;
646312Sgblack@eecs.umich.edu    };
657093Sgblack@eecs.umich.edu};
666312Sgblack@eecs.umich.edu
676312Sgblack@eecs.umich.edu#else // !FULL_SYSTEM
687148Sgblack@eecs.umich.edu
697148Sgblack@eecs.umich.edu#include "sim/process.hh"
707148Sgblack@eecs.umich.edu#include "mem/page_table.hh"
717148Sgblack@eecs.umich.educlass TranslatingPort;
727184Sgblack@eecs.umich.edu
737184Sgblack@eecs.umich.edu#endif // FULL_SYSTEM
747289Sgblack@eecs.umich.edu
757289Sgblack@eecs.umich.edu/**
767289Sgblack@eecs.umich.edu * The SimpleThread object provides a combination of the ThreadState
777289Sgblack@eecs.umich.edu * object and the ThreadContext interface. It implements the
787184Sgblack@eecs.umich.edu * ThreadContext interface so that a ProxyThreadContext class can be
797184Sgblack@eecs.umich.edu * made using SimpleThread as the template parameter (see
807184Sgblack@eecs.umich.edu * thread_context.hh). It adds to the ThreadState object by adding all
817184Sgblack@eecs.umich.edu * the objects needed for simple functional execution, including a
827184Sgblack@eecs.umich.edu * simple architectural register file, and pointers to the ITB and DTB
837184Sgblack@eecs.umich.edu * in full system mode. For CPU models that do not need more advanced
847093Sgblack@eecs.umich.edu * ways to hold state (i.e. a separate physical register file, or
857093Sgblack@eecs.umich.edu * separate fetch and commit PC's), this SimpleThread class provides
867093Sgblack@eecs.umich.edu * all the necessary state for full architecture-level functional
877148Sgblack@eecs.umich.edu * simulation.  See the AtomicSimpleCPU or TimingSimpleCPU for
887151Sgblack@eecs.umich.edu * examples.
896312Sgblack@eecs.umich.edu */
906312Sgblack@eecs.umich.edu
916019Shines@cs.fsu.educlass SimpleThread : public ThreadState
927119Sgblack@eecs.umich.edu{
937288Sgblack@eecs.umich.edu  protected:
947119Sgblack@eecs.umich.edu    typedef TheISA::MachInst MachInst;
957327Sgblack@eecs.umich.edu    typedef TheISA::MiscReg MiscReg;
967327Sgblack@eecs.umich.edu    typedef TheISA::FloatReg FloatReg;
977327Sgblack@eecs.umich.edu    typedef TheISA::FloatRegBits FloatRegBits;
987327Sgblack@eecs.umich.edu  public:
997327Sgblack@eecs.umich.edu    typedef ThreadContext::Status Status;
1007639Sgblack@eecs.umich.edu
1017639Sgblack@eecs.umich.edu  protected:
1027639Sgblack@eecs.umich.edu    union {
1037639Sgblack@eecs.umich.edu        FloatReg f[TheISA::NumFloatRegs];
1047639Sgblack@eecs.umich.edu        FloatRegBits i[TheISA::NumFloatRegs];
1057639Sgblack@eecs.umich.edu    } floatRegs;
1067639Sgblack@eecs.umich.edu    TheISA::IntReg intRegs[TheISA::NumIntRegs];
1077639Sgblack@eecs.umich.edu    TheISA::ISA isa;    // one "instance" of the current ISA.
1087639Sgblack@eecs.umich.edu
1097639Sgblack@eecs.umich.edu    /** The current microcode pc for the currently executing macro
1107639Sgblack@eecs.umich.edu     * operation.
1117639Sgblack@eecs.umich.edu     */
1127303Sgblack@eecs.umich.edu    MicroPC microPC;
1137303Sgblack@eecs.umich.edu
1147288Sgblack@eecs.umich.edu    /** The next microcode pc for the currently executing macro
1157279Sgblack@eecs.umich.edu     * operation.
1167327Sgblack@eecs.umich.edu     */
1177327Sgblack@eecs.umich.edu    MicroPC nextMicroPC;
1187327Sgblack@eecs.umich.edu
1197327Sgblack@eecs.umich.edu    /** The current pc.
1207327Sgblack@eecs.umich.edu     */
1217288Sgblack@eecs.umich.edu    Addr PC;
1227148Sgblack@eecs.umich.edu
1237288Sgblack@eecs.umich.edu    /** The next pc.
1247184Sgblack@eecs.umich.edu     */
1257310Sgblack@eecs.umich.edu    Addr nextPC;
1267310Sgblack@eecs.umich.edu
1277310Sgblack@eecs.umich.edu    /** The next next pc.
1287288Sgblack@eecs.umich.edu     */
1297288Sgblack@eecs.umich.edu    Addr nextNPC;
1307186Sgblack@eecs.umich.edu
1317119Sgblack@eecs.umich.edu    /** Did this instruction execute or is it predicated false */
1327119Sgblack@eecs.umich.edu    bool predicate;
1337288Sgblack@eecs.umich.edu
1347137Sgblack@eecs.umich.edu  public:
1357327Sgblack@eecs.umich.edu    // pointer to CPU associated with this SimpleThread
1367327Sgblack@eecs.umich.edu    BaseCPU *cpu;
1377327Sgblack@eecs.umich.edu
1387327Sgblack@eecs.umich.edu    ProxyThreadContext<SimpleThread> *tc;
1397327Sgblack@eecs.umich.edu
1407639Sgblack@eecs.umich.edu    System *system;
1417639Sgblack@eecs.umich.edu
1427639Sgblack@eecs.umich.edu    TheISA::TLB *itb;
1437639Sgblack@eecs.umich.edu    TheISA::TLB *dtb;
1447639Sgblack@eecs.umich.edu
1457639Sgblack@eecs.umich.edu    // constructor: initialize SimpleThread from given process structure
1467639Sgblack@eecs.umich.edu#if FULL_SYSTEM
1477639Sgblack@eecs.umich.edu    SimpleThread(BaseCPU *_cpu, int _thread_num, System *_system,
1487639Sgblack@eecs.umich.edu                 TheISA::TLB *_itb, TheISA::TLB *_dtb,
1497639Sgblack@eecs.umich.edu                 bool use_kernel_stats = true);
1507639Sgblack@eecs.umich.edu#else
1517639Sgblack@eecs.umich.edu    SimpleThread(BaseCPU *_cpu, int _thread_num, Process *_process,
1527288Sgblack@eecs.umich.edu                 TheISA::TLB *_itb, TheISA::TLB *_dtb);
1537288Sgblack@eecs.umich.edu#endif
1547137Sgblack@eecs.umich.edu
1557327Sgblack@eecs.umich.edu    SimpleThread();
1567327Sgblack@eecs.umich.edu
1577327Sgblack@eecs.umich.edu    virtual ~SimpleThread();
1587327Sgblack@eecs.umich.edu
1597327Sgblack@eecs.umich.edu    virtual void takeOverFrom(ThreadContext *oldContext);
1607288Sgblack@eecs.umich.edu
1617241Sgblack@eecs.umich.edu    void regStats(const std::string &name);
1627288Sgblack@eecs.umich.edu
1637137Sgblack@eecs.umich.edu    void copyTC(ThreadContext *context);
1647288Sgblack@eecs.umich.edu
1657160Sgblack@eecs.umich.edu    void copyState(ThreadContext *oldContext);
1667288Sgblack@eecs.umich.edu
1677160Sgblack@eecs.umich.edu    void serialize(std::ostream &os);
1687288Sgblack@eecs.umich.edu    void unserialize(Checkpoint *cp, const std::string &section);
1697160Sgblack@eecs.umich.edu
1707288Sgblack@eecs.umich.edu    /***************************************************************
1717160Sgblack@eecs.umich.edu     *  SimpleThread functions to provide CPU with access to various
1726019Shines@cs.fsu.edu     *  state.
1737288Sgblack@eecs.umich.edu     **************************************************************/
1746312Sgblack@eecs.umich.edu
1757288Sgblack@eecs.umich.edu    /** Returns the pointer to this SimpleThread's ThreadContext. Used
1767288Sgblack@eecs.umich.edu     *  when a ThreadContext must be passed to objects outside of the
1777288Sgblack@eecs.umich.edu     *  CPU.
1787288Sgblack@eecs.umich.edu     */
1796019Shines@cs.fsu.edu    ThreadContext *getTC() { return tc; }
1807288Sgblack@eecs.umich.edu
1817288Sgblack@eecs.umich.edu    void demapPage(Addr vaddr, uint64_t asn)
1827422Sgblack@eecs.umich.edu    {
1837422Sgblack@eecs.umich.edu        itb->demapPage(vaddr, asn);
1847422Sgblack@eecs.umich.edu        dtb->demapPage(vaddr, asn);
1856019Shines@cs.fsu.edu    }
1866308Sgblack@eecs.umich.edu
1877288Sgblack@eecs.umich.edu    void demapInstPage(Addr vaddr, uint64_t asn)
1887288Sgblack@eecs.umich.edu    {
1897207Sgblack@eecs.umich.edu        itb->demapPage(vaddr, asn);
1907288Sgblack@eecs.umich.edu    }
1917288Sgblack@eecs.umich.edu
1927639Sgblack@eecs.umich.edu    void demapDataPage(Addr vaddr, uint64_t asn)
1936308Sgblack@eecs.umich.edu    {
1946019Shines@cs.fsu.edu        dtb->demapPage(vaddr, asn);
1957288Sgblack@eecs.umich.edu    }
1967288Sgblack@eecs.umich.edu
1977288Sgblack@eecs.umich.edu#if FULL_SYSTEM
1986019Shines@cs.fsu.edu    void dumpFuncProfile();
1996019Shines@cs.fsu.edu
2007288Sgblack@eecs.umich.edu    Fault hwrei();
2016019Shines@cs.fsu.edu
2027288Sgblack@eecs.umich.edu    bool simPalCheck(int palFunc);
2037408Sgblack@eecs.umich.edu
2047288Sgblack@eecs.umich.edu#endif
2057288Sgblack@eecs.umich.edu
2067288Sgblack@eecs.umich.edu    /*******************************************
2077288Sgblack@eecs.umich.edu     * ThreadContext interface functions.
2087288Sgblack@eecs.umich.edu     ******************************************/
2097361Sgblack@eecs.umich.edu
2107350SAli.Saidi@ARM.com    BaseCPU *getCpuPtr() { return cpu; }
2117410Sgblack@eecs.umich.edu
2127288Sgblack@eecs.umich.edu    TheISA::TLB *getITBPtr() { return itb; }
2137093Sgblack@eecs.umich.edu
2147288Sgblack@eecs.umich.edu    TheISA::TLB *getDTBPtr() { return dtb; }
2157151Sgblack@eecs.umich.edu
2167288Sgblack@eecs.umich.edu    System *getSystemPtr() { return system; }
2177148Sgblack@eecs.umich.edu
2186019Shines@cs.fsu.edu#if FULL_SYSTEM
219    FunctionalPort *getPhysPort() { return physPort; }
220
221    /** Return a virtual port. This port cannot be cached locally in an object.
222     * After a CPU switch it may point to the wrong memory object which could
223     * mean stale data.
224     */
225    VirtualPort *getVirtPort() { return virtPort; }
226#endif
227
228    Status status() const { return _status; }
229
230    void setStatus(Status newStatus) { _status = newStatus; }
231
232    /// Set the status to Active.  Optional delay indicates number of
233    /// cycles to wait before beginning execution.
234    void activate(int delay = 1);
235
236    /// Set the status to Suspended.
237    void suspend();
238
239    /// Set the status to Halted.
240    void halt();
241
242    virtual bool misspeculating();
243
244    void copyArchRegs(ThreadContext *tc);
245
246    void clearArchRegs()
247    {
248        microPC = 0;
249        nextMicroPC = 1;
250        PC = nextPC = nextNPC = 0;
251        memset(intRegs, 0, sizeof(intRegs));
252        memset(floatRegs.i, 0, sizeof(floatRegs.i));
253        isa.clear();
254    }
255
256    //
257    // New accessors for new decoder.
258    //
259    uint64_t readIntReg(int reg_idx)
260    {
261        int flatIndex = isa.flattenIntIndex(reg_idx);
262        assert(flatIndex < TheISA::NumIntRegs);
263        uint64_t regVal = intRegs[flatIndex];
264        DPRINTF(IntRegs, "Reading int reg %d (%d) as %#x.\n",
265                reg_idx, flatIndex, regVal);
266        return regVal;
267    }
268
269    FloatReg readFloatReg(int reg_idx)
270    {
271        int flatIndex = isa.flattenFloatIndex(reg_idx);
272        assert(flatIndex < TheISA::NumFloatRegs);
273        FloatReg regVal = floatRegs.f[flatIndex];
274        DPRINTF(FloatRegs, "Reading float reg %d (%d) as %f, %#x.\n",
275                reg_idx, flatIndex, regVal, floatRegs.i[flatIndex]);
276        return regVal;
277    }
278
279    FloatRegBits readFloatRegBits(int reg_idx)
280    {
281        int flatIndex = isa.flattenFloatIndex(reg_idx);
282        assert(flatIndex < TheISA::NumFloatRegs);
283        FloatRegBits regVal = floatRegs.i[flatIndex];
284        DPRINTF(FloatRegs, "Reading float reg %d (%d) bits as %#x, %f.\n",
285                reg_idx, flatIndex, regVal, floatRegs.f[flatIndex]);
286        return regVal;
287    }
288
289    void setIntReg(int reg_idx, uint64_t val)
290    {
291        int flatIndex = isa.flattenIntIndex(reg_idx);
292        assert(flatIndex < TheISA::NumIntRegs);
293        DPRINTF(IntRegs, "Setting int reg %d (%d) to %#x.\n",
294                reg_idx, flatIndex, val);
295        intRegs[flatIndex] = val;
296    }
297
298    void setFloatReg(int reg_idx, FloatReg val)
299    {
300        int flatIndex = isa.flattenFloatIndex(reg_idx);
301        assert(flatIndex < TheISA::NumFloatRegs);
302        floatRegs.f[flatIndex] = val;
303        DPRINTF(FloatRegs, "Setting float reg %d (%d) to %f, %#x.\n",
304                reg_idx, flatIndex, val, floatRegs.i[flatIndex]);
305    }
306
307    void setFloatRegBits(int reg_idx, FloatRegBits val)
308    {
309        int flatIndex = isa.flattenFloatIndex(reg_idx);
310        assert(flatIndex < TheISA::NumFloatRegs);
311        floatRegs.i[flatIndex] = val;
312        DPRINTF(FloatRegs, "Setting float reg %d (%d) bits to %#x, %#f.\n",
313                reg_idx, flatIndex, val, floatRegs.f[flatIndex]);
314    }
315
316    uint64_t readPC()
317    {
318        return PC;
319    }
320
321    void setPC(uint64_t val)
322    {
323        PC = val;
324    }
325
326    uint64_t readMicroPC()
327    {
328        return microPC;
329    }
330
331    void setMicroPC(uint64_t val)
332    {
333        microPC = val;
334    }
335
336    uint64_t readNextPC()
337    {
338        return nextPC;
339    }
340
341    void setNextPC(uint64_t val)
342    {
343        nextPC = val;
344    }
345
346    uint64_t readNextMicroPC()
347    {
348        return nextMicroPC;
349    }
350
351    void setNextMicroPC(uint64_t val)
352    {
353        nextMicroPC = val;
354    }
355
356    uint64_t readNextNPC()
357    {
358#if ISA_HAS_DELAY_SLOT
359        return nextNPC;
360#else
361        return nextPC + sizeof(TheISA::MachInst);
362#endif
363    }
364
365    void setNextNPC(uint64_t val)
366    {
367#if ISA_HAS_DELAY_SLOT
368        nextNPC = val;
369#endif
370    }
371
372    bool readPredicate()
373    {
374        return predicate;
375    }
376
377    void setPredicate(bool val)
378    {
379        predicate = val;
380    }
381
382    MiscReg
383    readMiscRegNoEffect(int misc_reg, ThreadID tid = 0)
384    {
385        return isa.readMiscRegNoEffect(misc_reg);
386    }
387
388    MiscReg
389    readMiscReg(int misc_reg, ThreadID tid = 0)
390    {
391        return isa.readMiscReg(misc_reg, tc);
392    }
393
394    void
395    setMiscRegNoEffect(int misc_reg, const MiscReg &val, ThreadID tid = 0)
396    {
397        return isa.setMiscRegNoEffect(misc_reg, val);
398    }
399
400    void
401    setMiscReg(int misc_reg, const MiscReg &val, ThreadID tid = 0)
402    {
403        return isa.setMiscReg(misc_reg, val, tc);
404    }
405
406    int
407    flattenIntIndex(int reg)
408    {
409        return isa.flattenIntIndex(reg);
410    }
411
412    int
413    flattenFloatIndex(int reg)
414    {
415        return isa.flattenFloatIndex(reg);
416    }
417
418    unsigned readStCondFailures() { return storeCondFailures; }
419
420    void setStCondFailures(unsigned sc_failures)
421    { storeCondFailures = sc_failures; }
422
423#if !FULL_SYSTEM
424    void syscall(int64_t callnum)
425    {
426        process->syscall(callnum, tc);
427    }
428#endif
429};
430
431
432// for non-speculative execution context, spec_mode is always false
433inline bool
434SimpleThread::misspeculating()
435{
436    return false;
437}
438
439#endif // __CPU_CPU_EXEC_CONTEXT_HH__
440