thread_context.hh revision 13500:6e0a2a7c6d8c
1/*
2 * Copyright (c) 2011-2012, 2016 ARM Limited
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder.  You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2006 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Kevin Lim
42 */
43
44#ifndef __CPU_THREAD_CONTEXT_HH__
45#define __CPU_THREAD_CONTEXT_HH__
46
47#include <iostream>
48#include <string>
49
50#include "arch/registers.hh"
51#include "arch/types.hh"
52#include "base/types.hh"
53#include "config/the_isa.hh"
54#include "cpu/reg_class.hh"
55
56// @todo: Figure out a more architecture independent way to obtain the ITB and
57// DTB pointers.
58namespace TheISA
59{
60    class Decoder;
61}
62class BaseCPU;
63class BaseTLB;
64class CheckerCPU;
65class Checkpoint;
66class EndQuiesceEvent;
67class SETranslatingPortProxy;
68class FSTranslatingPortProxy;
69class PortProxy;
70class Process;
71class System;
72namespace TheISA {
73    namespace Kernel {
74        class Statistics;
75    }
76}
77
78/**
79 * ThreadContext is the external interface to all thread state for
80 * anything outside of the CPU. It provides all accessor methods to
81 * state that might be needed by external objects, ranging from
82 * register values to things such as kernel stats. It is an abstract
83 * base class; the CPU can create its own ThreadContext by either
84 * deriving from it, or using the templated ProxyThreadContext.
85 *
86 * The ThreadContext is slightly different than the ExecContext.  The
87 * ThreadContext provides access to an individual thread's state; an
88 * ExecContext provides ISA access to the CPU (meaning it is
89 * implicitly multithreaded on SMT systems).  Additionally the
90 * ThreadState is an abstract class that exactly defines the
91 * interface; the ExecContext is a more implicit interface that must
92 * be implemented so that the ISA can access whatever state it needs.
93 */
94class ThreadContext
95{
96  protected:
97    typedef TheISA::MachInst MachInst;
98    typedef TheISA::IntReg IntReg;
99    typedef TheISA::FloatReg FloatReg;
100    typedef TheISA::FloatRegBits FloatRegBits;
101    typedef TheISA::CCReg CCReg;
102    typedef TheISA::MiscReg MiscReg;
103    using VecRegContainer = TheISA::VecRegContainer;
104    using VecElem = TheISA::VecElem;
105  public:
106
107    enum Status
108    {
109        /// Running.  Instructions should be executed only when
110        /// the context is in this state.
111        Active,
112
113        /// Temporarily inactive.  Entered while waiting for
114        /// synchronization, etc.
115        Suspended,
116
117        /// Permanently shut down.  Entered when target executes
118        /// m5exit pseudo-instruction.  When all contexts enter
119        /// this state, the simulation will terminate.
120        Halted
121    };
122
123    virtual ~ThreadContext() { };
124
125    virtual BaseCPU *getCpuPtr() = 0;
126
127    virtual int cpuId() const = 0;
128
129    virtual uint32_t socketId() const = 0;
130
131    virtual int threadId() const = 0;
132
133    virtual void setThreadId(int id) = 0;
134
135    virtual int contextId() const = 0;
136
137    virtual void setContextId(int id) = 0;
138
139    virtual BaseTLB *getITBPtr() = 0;
140
141    virtual BaseTLB *getDTBPtr() = 0;
142
143    virtual CheckerCPU *getCheckerCpuPtr() = 0;
144
145    virtual TheISA::Decoder *getDecoderPtr() = 0;
146
147    virtual System *getSystemPtr() = 0;
148
149    virtual TheISA::Kernel::Statistics *getKernelStats() = 0;
150
151    virtual PortProxy &getPhysProxy() = 0;
152
153    virtual FSTranslatingPortProxy &getVirtProxy() = 0;
154
155    /**
156     * Initialise the physical and virtual port proxies and tie them to
157     * the data port of the CPU.
158     *
159     * tc ThreadContext for the virtual-to-physical translation
160     */
161    virtual void initMemProxies(ThreadContext *tc) = 0;
162
163    virtual SETranslatingPortProxy &getMemProxy() = 0;
164
165    virtual Process *getProcessPtr() = 0;
166
167    virtual void setProcessPtr(Process *p) = 0;
168
169    virtual Status status() const = 0;
170
171    virtual void setStatus(Status new_status) = 0;
172
173    /// Set the status to Active.
174    virtual void activate() = 0;
175
176    /// Set the status to Suspended.
177    virtual void suspend() = 0;
178
179    /// Set the status to Halted.
180    virtual void halt() = 0;
181
182    /// Quiesce thread context
183    void quiesce();
184
185    /// Quiesce, suspend, and schedule activate at resume
186    void quiesceTick(Tick resume);
187
188    virtual void dumpFuncProfile() = 0;
189
190    virtual void takeOverFrom(ThreadContext *old_context) = 0;
191
192    virtual void regStats(const std::string &name) = 0;
193
194    virtual EndQuiesceEvent *getQuiesceEvent() = 0;
195
196    // Not necessarily the best location for these...
197    // Having an extra function just to read these is obnoxious
198    virtual Tick readLastActivate() = 0;
199    virtual Tick readLastSuspend() = 0;
200
201    virtual void profileClear() = 0;
202    virtual void profileSample() = 0;
203
204    virtual void copyArchRegs(ThreadContext *tc) = 0;
205
206    virtual void clearArchRegs() = 0;
207
208    //
209    // New accessors for new decoder.
210    //
211    virtual uint64_t readIntReg(int reg_idx) = 0;
212
213    virtual FloatRegBits readFloatRegBits(int reg_idx) = 0;
214
215    virtual const VecRegContainer& readVecReg(const RegId& reg) const = 0;
216    virtual VecRegContainer& getWritableVecReg(const RegId& reg) = 0;
217
218    /** Vector Register Lane Interfaces. */
219    /** @{ */
220    /** Reads source vector 8bit operand. */
221    virtual ConstVecLane8
222    readVec8BitLaneReg(const RegId& reg) const = 0;
223
224    /** Reads source vector 16bit operand. */
225    virtual ConstVecLane16
226    readVec16BitLaneReg(const RegId& reg) const = 0;
227
228    /** Reads source vector 32bit operand. */
229    virtual ConstVecLane32
230    readVec32BitLaneReg(const RegId& reg) const = 0;
231
232    /** Reads source vector 64bit operand. */
233    virtual ConstVecLane64
234    readVec64BitLaneReg(const RegId& reg) const = 0;
235
236    /** Write a lane of the destination vector register. */
237    virtual void setVecLane(const RegId& reg,
238            const LaneData<LaneSize::Byte>& val) = 0;
239    virtual void setVecLane(const RegId& reg,
240            const LaneData<LaneSize::TwoByte>& val) = 0;
241    virtual void setVecLane(const RegId& reg,
242            const LaneData<LaneSize::FourByte>& val) = 0;
243    virtual void setVecLane(const RegId& reg,
244            const LaneData<LaneSize::EightByte>& val) = 0;
245    /** @} */
246
247    virtual const VecElem& readVecElem(const RegId& reg) const = 0;
248
249    virtual CCReg readCCReg(int reg_idx) = 0;
250
251    virtual void setIntReg(int reg_idx, uint64_t val) = 0;
252
253    virtual void setFloatRegBits(int reg_idx, FloatRegBits val) = 0;
254
255    virtual void setVecReg(const RegId& reg, const VecRegContainer& val) = 0;
256
257    virtual void setVecElem(const RegId& reg, const VecElem& val) = 0;
258
259    virtual void setCCReg(int reg_idx, CCReg val) = 0;
260
261    virtual TheISA::PCState pcState() = 0;
262
263    virtual void pcState(const TheISA::PCState &val) = 0;
264
265    void
266    setNPC(Addr val)
267    {
268        TheISA::PCState pc_state = pcState();
269        pc_state.setNPC(val);
270        pcState(pc_state);
271    }
272
273    virtual void pcStateNoRecord(const TheISA::PCState &val) = 0;
274
275    virtual Addr instAddr() = 0;
276
277    virtual Addr nextInstAddr() = 0;
278
279    virtual MicroPC microPC() = 0;
280
281    virtual MiscReg readMiscRegNoEffect(int misc_reg) const = 0;
282
283    virtual MiscReg readMiscReg(int misc_reg) = 0;
284
285    virtual void setMiscRegNoEffect(int misc_reg, const MiscReg &val) = 0;
286
287    virtual void setMiscReg(int misc_reg, const MiscReg &val) = 0;
288
289    virtual RegId flattenRegId(const RegId& regId) const = 0;
290
291    virtual uint64_t
292    readRegOtherThread(const RegId& misc_reg, ThreadID tid)
293    {
294        return 0;
295    }
296
297    virtual void
298    setRegOtherThread(const RegId& misc_reg, const MiscReg &val, ThreadID tid)
299    {
300    }
301
302    // Also not necessarily the best location for these two.  Hopefully will go
303    // away once we decide upon where st cond failures goes.
304    virtual unsigned readStCondFailures() = 0;
305
306    virtual void setStCondFailures(unsigned sc_failures) = 0;
307
308    // Same with st cond failures.
309    virtual Counter readFuncExeInst() = 0;
310
311    virtual void syscall(int64_t callnum, Fault *fault) = 0;
312
313    // This function exits the thread context in the CPU and returns
314    // 1 if the CPU has no more active threads (meaning it's OK to exit);
315    // Used in syscall-emulation mode when a  thread calls the exit syscall.
316    virtual int exit() { return 1; };
317
318    /** function to compare two thread contexts (for debugging) */
319    static void compare(ThreadContext *one, ThreadContext *two);
320
321    /** @{ */
322    /**
323     * Flat register interfaces
324     *
325     * Some architectures have different registers visible in
326     * different modes. Such architectures "flatten" a register (see
327     * flattenRegId()) to map it into the
328     * gem5 register file. This interface provides a flat interface to
329     * the underlying register file, which allows for example
330     * serialization code to access all registers.
331     */
332
333    virtual uint64_t readIntRegFlat(int idx) = 0;
334    virtual void setIntRegFlat(int idx, uint64_t val) = 0;
335
336    virtual FloatRegBits readFloatRegBitsFlat(int idx) = 0;
337    virtual void setFloatRegBitsFlat(int idx, FloatRegBits val) = 0;
338
339    virtual const VecRegContainer& readVecRegFlat(int idx) const = 0;
340    virtual VecRegContainer& getWritableVecRegFlat(int idx) = 0;
341    virtual void setVecRegFlat(int idx, const VecRegContainer& val) = 0;
342
343    virtual const VecElem& readVecElemFlat(const RegIndex& idx,
344                                           const ElemIndex& elemIdx) const = 0;
345    virtual void setVecElemFlat(const RegIndex& idx, const ElemIndex& elemIdx,
346                                const VecElem& val) = 0;
347
348    virtual CCReg readCCRegFlat(int idx) = 0;
349    virtual void setCCRegFlat(int idx, CCReg val) = 0;
350    /** @} */
351
352};
353
354/**
355 * ProxyThreadContext class that provides a way to implement a
356 * ThreadContext without having to derive from it. ThreadContext is an
357 * abstract class, so anything that derives from it and uses its
358 * interface will pay the overhead of virtual function calls.  This
359 * class is created to enable a user-defined Thread object to be used
360 * wherever ThreadContexts are used, without paying the overhead of
361 * virtual function calls when it is used by itself.  See
362 * simple_thread.hh for an example of this.
363 */
364template <class TC>
365class ProxyThreadContext : public ThreadContext
366{
367  public:
368    ProxyThreadContext(TC *actual_tc)
369    { actualTC = actual_tc; }
370
371  private:
372    TC *actualTC;
373
374  public:
375
376    BaseCPU *getCpuPtr() { return actualTC->getCpuPtr(); }
377
378    int cpuId() const { return actualTC->cpuId(); }
379
380    uint32_t socketId() const { return actualTC->socketId(); }
381
382    int threadId() const { return actualTC->threadId(); }
383
384    void setThreadId(int id) { actualTC->setThreadId(id); }
385
386    int contextId() const { return actualTC->contextId(); }
387
388    void setContextId(int id) { actualTC->setContextId(id); }
389
390    BaseTLB *getITBPtr() { return actualTC->getITBPtr(); }
391
392    BaseTLB *getDTBPtr() { return actualTC->getDTBPtr(); }
393
394    CheckerCPU *getCheckerCpuPtr() { return actualTC->getCheckerCpuPtr(); }
395
396    TheISA::Decoder *getDecoderPtr() { return actualTC->getDecoderPtr(); }
397
398    System *getSystemPtr() { return actualTC->getSystemPtr(); }
399
400    TheISA::Kernel::Statistics *getKernelStats()
401    { return actualTC->getKernelStats(); }
402
403    PortProxy &getPhysProxy() { return actualTC->getPhysProxy(); }
404
405    FSTranslatingPortProxy &getVirtProxy() { return actualTC->getVirtProxy(); }
406
407    void initMemProxies(ThreadContext *tc) { actualTC->initMemProxies(tc); }
408
409    SETranslatingPortProxy &getMemProxy() { return actualTC->getMemProxy(); }
410
411    Process *getProcessPtr() { return actualTC->getProcessPtr(); }
412
413    void setProcessPtr(Process *p) { actualTC->setProcessPtr(p); }
414
415    Status status() const { return actualTC->status(); }
416
417    void setStatus(Status new_status) { actualTC->setStatus(new_status); }
418
419    /// Set the status to Active.
420    void activate() { actualTC->activate(); }
421
422    /// Set the status to Suspended.
423    void suspend() { actualTC->suspend(); }
424
425    /// Set the status to Halted.
426    void halt() { actualTC->halt(); }
427
428    /// Quiesce thread context
429    void quiesce() { actualTC->quiesce(); }
430
431    /// Quiesce, suspend, and schedule activate at resume
432    void quiesceTick(Tick resume) { actualTC->quiesceTick(resume); }
433
434    void dumpFuncProfile() { actualTC->dumpFuncProfile(); }
435
436    void takeOverFrom(ThreadContext *oldContext)
437    { actualTC->takeOverFrom(oldContext); }
438
439    void regStats(const std::string &name) { actualTC->regStats(name); }
440
441    EndQuiesceEvent *getQuiesceEvent() { return actualTC->getQuiesceEvent(); }
442
443    Tick readLastActivate() { return actualTC->readLastActivate(); }
444    Tick readLastSuspend() { return actualTC->readLastSuspend(); }
445
446    void profileClear() { return actualTC->profileClear(); }
447    void profileSample() { return actualTC->profileSample(); }
448
449    // @todo: Do I need this?
450    void copyArchRegs(ThreadContext *tc) { actualTC->copyArchRegs(tc); }
451
452    void clearArchRegs() { actualTC->clearArchRegs(); }
453
454    //
455    // New accessors for new decoder.
456    //
457    uint64_t readIntReg(int reg_idx)
458    { return actualTC->readIntReg(reg_idx); }
459
460    FloatRegBits readFloatRegBits(int reg_idx)
461    { return actualTC->readFloatRegBits(reg_idx); }
462
463    const VecRegContainer& readVecReg(const RegId& reg) const
464    { return actualTC->readVecReg(reg); }
465
466    VecRegContainer& getWritableVecReg(const RegId& reg)
467    { return actualTC->getWritableVecReg(reg); }
468
469    /** Vector Register Lane Interfaces. */
470    /** @{ */
471    /** Reads source vector 8bit operand. */
472    ConstVecLane8
473    readVec8BitLaneReg(const RegId& reg) const
474    { return actualTC->readVec8BitLaneReg(reg); }
475
476    /** Reads source vector 16bit operand. */
477    ConstVecLane16
478    readVec16BitLaneReg(const RegId& reg) const
479    { return actualTC->readVec16BitLaneReg(reg); }
480
481    /** Reads source vector 32bit operand. */
482    ConstVecLane32
483    readVec32BitLaneReg(const RegId& reg) const
484    { return actualTC->readVec32BitLaneReg(reg); }
485
486    /** Reads source vector 64bit operand. */
487    ConstVecLane64
488    readVec64BitLaneReg(const RegId& reg) const
489    { return actualTC->readVec64BitLaneReg(reg); }
490
491    /** Write a lane of the destination vector register. */
492    virtual void setVecLane(const RegId& reg,
493            const LaneData<LaneSize::Byte>& val)
494    { return actualTC->setVecLane(reg, val); }
495    virtual void setVecLane(const RegId& reg,
496            const LaneData<LaneSize::TwoByte>& val)
497    { return actualTC->setVecLane(reg, val); }
498    virtual void setVecLane(const RegId& reg,
499            const LaneData<LaneSize::FourByte>& val)
500    { return actualTC->setVecLane(reg, val); }
501    virtual void setVecLane(const RegId& reg,
502            const LaneData<LaneSize::EightByte>& val)
503    { return actualTC->setVecLane(reg, val); }
504    /** @} */
505
506    const VecElem& readVecElem(const RegId& reg) const
507    { return actualTC->readVecElem(reg); }
508
509    CCReg readCCReg(int reg_idx)
510    { return actualTC->readCCReg(reg_idx); }
511
512    void setIntReg(int reg_idx, uint64_t val)
513    { actualTC->setIntReg(reg_idx, val); }
514
515    void setFloatRegBits(int reg_idx, FloatRegBits val)
516    { actualTC->setFloatRegBits(reg_idx, val); }
517
518    void setVecReg(const RegId& reg, const VecRegContainer& val)
519    { actualTC->setVecReg(reg, val); }
520
521    void setVecElem(const RegId& reg, const VecElem& val)
522    { actualTC->setVecElem(reg, val); }
523
524    void setCCReg(int reg_idx, CCReg val)
525    { actualTC->setCCReg(reg_idx, val); }
526
527    TheISA::PCState pcState() { return actualTC->pcState(); }
528
529    void pcState(const TheISA::PCState &val) { actualTC->pcState(val); }
530
531    void pcStateNoRecord(const TheISA::PCState &val) { actualTC->pcState(val); }
532
533    Addr instAddr() { return actualTC->instAddr(); }
534    Addr nextInstAddr() { return actualTC->nextInstAddr(); }
535    MicroPC microPC() { return actualTC->microPC(); }
536
537    bool readPredicate() { return actualTC->readPredicate(); }
538
539    void setPredicate(bool val)
540    { actualTC->setPredicate(val); }
541
542    MiscReg readMiscRegNoEffect(int misc_reg) const
543    { return actualTC->readMiscRegNoEffect(misc_reg); }
544
545    MiscReg readMiscReg(int misc_reg)
546    { return actualTC->readMiscReg(misc_reg); }
547
548    void setMiscRegNoEffect(int misc_reg, const MiscReg &val)
549    { return actualTC->setMiscRegNoEffect(misc_reg, val); }
550
551    void setMiscReg(int misc_reg, const MiscReg &val)
552    { return actualTC->setMiscReg(misc_reg, val); }
553
554    RegId flattenRegId(const RegId& regId) const
555    { return actualTC->flattenRegId(regId); }
556
557    unsigned readStCondFailures()
558    { return actualTC->readStCondFailures(); }
559
560    void setStCondFailures(unsigned sc_failures)
561    { actualTC->setStCondFailures(sc_failures); }
562
563    void syscall(int64_t callnum, Fault *fault)
564    { actualTC->syscall(callnum, fault); }
565
566    Counter readFuncExeInst() { return actualTC->readFuncExeInst(); }
567
568    uint64_t readIntRegFlat(int idx)
569    { return actualTC->readIntRegFlat(idx); }
570
571    void setIntRegFlat(int idx, uint64_t val)
572    { actualTC->setIntRegFlat(idx, val); }
573
574    FloatRegBits readFloatRegBitsFlat(int idx)
575    { return actualTC->readFloatRegBitsFlat(idx); }
576
577    void setFloatRegBitsFlat(int idx, FloatRegBits val)
578    { actualTC->setFloatRegBitsFlat(idx, val); }
579
580    const VecRegContainer& readVecRegFlat(int id) const
581    { return actualTC->readVecRegFlat(id); }
582
583    VecRegContainer& getWritableVecRegFlat(int id)
584    { return actualTC->getWritableVecRegFlat(id); }
585
586    void setVecRegFlat(int idx, const VecRegContainer& val)
587    { actualTC->setVecRegFlat(idx, val); }
588
589    const VecElem& readVecElemFlat(const RegIndex& id,
590                                   const ElemIndex& elemIndex) const
591    { return actualTC->readVecElemFlat(id, elemIndex); }
592
593    void setVecElemFlat(const RegIndex& id, const ElemIndex& elemIndex,
594                        const VecElem& val)
595    { actualTC->setVecElemFlat(id, elemIndex, val); }
596
597    CCReg readCCRegFlat(int idx)
598    { return actualTC->readCCRegFlat(idx); }
599
600    void setCCRegFlat(int idx, CCReg val)
601    { actualTC->setCCRegFlat(idx, val); }
602};
603
604/** @{ */
605/**
606 * Thread context serialization helpers
607 *
608 * These helper functions provide a way to the data in a
609 * ThreadContext. They are provided as separate helper function since
610 * implementing them as members of the ThreadContext interface would
611 * be confusing when the ThreadContext is exported via a proxy.
612 */
613
614void serialize(ThreadContext &tc, CheckpointOut &cp);
615void unserialize(ThreadContext &tc, CheckpointIn &cp);
616
617/** @} */
618
619
620/**
621 * Copy state between thread contexts in preparation for CPU handover.
622 *
623 * @note This method modifies the old thread contexts as well as the
624 * new thread context. The old thread context will have its quiesce
625 * event descheduled if it is scheduled and its status set to halted.
626 *
627 * @param new_tc Destination ThreadContext.
628 * @param old_tc Source ThreadContext.
629 */
630void takeOverFrom(ThreadContext &new_tc, ThreadContext &old_tc);
631
632#endif
633