thread_context.hh revision 7678:f19b6a3a8cec
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Kevin Lim
29 */
30
31#ifndef __CPU_THREAD_CONTEXT_HH__
32#define __CPU_THREAD_CONTEXT_HH__
33
34#include "arch/registers.hh"
35#include "arch/types.hh"
36#include "base/types.hh"
37#include "config/full_system.hh"
38#include "config/the_isa.hh"
39#include "sim/serialize.hh"
40
41// @todo: Figure out a more architecture independent way to obtain the ITB and
42// DTB pointers.
43namespace TheISA
44{
45    class TLB;
46}
47class BaseCPU;
48class EndQuiesceEvent;
49class Event;
50class TranslatingPort;
51class FunctionalPort;
52class VirtualPort;
53class Process;
54class System;
55namespace TheISA {
56    namespace Kernel {
57        class Statistics;
58    };
59};
60
61/**
62 * ThreadContext is the external interface to all thread state for
63 * anything outside of the CPU. It provides all accessor methods to
64 * state that might be needed by external objects, ranging from
65 * register values to things such as kernel stats. It is an abstract
66 * base class; the CPU can create its own ThreadContext by either
67 * deriving from it, or using the templated ProxyThreadContext.
68 *
69 * The ThreadContext is slightly different than the ExecContext.  The
70 * ThreadContext provides access to an individual thread's state; an
71 * ExecContext provides ISA access to the CPU (meaning it is
72 * implicitly multithreaded on SMT systems).  Additionally the
73 * ThreadState is an abstract class that exactly defines the
74 * interface; the ExecContext is a more implicit interface that must
75 * be implemented so that the ISA can access whatever state it needs.
76 */
77class ThreadContext
78{
79  protected:
80    typedef TheISA::MachInst MachInst;
81    typedef TheISA::IntReg IntReg;
82    typedef TheISA::FloatReg FloatReg;
83    typedef TheISA::FloatRegBits FloatRegBits;
84    typedef TheISA::MiscReg MiscReg;
85  public:
86
87    enum Status
88    {
89        /// Running.  Instructions should be executed only when
90        /// the context is in this state.
91        Active,
92
93        /// Temporarily inactive.  Entered while waiting for
94        /// synchronization, etc.
95        Suspended,
96
97        /// Permanently shut down.  Entered when target executes
98        /// m5exit pseudo-instruction.  When all contexts enter
99        /// this state, the simulation will terminate.
100        Halted
101    };
102
103    virtual ~ThreadContext() { };
104
105    virtual BaseCPU *getCpuPtr() = 0;
106
107    virtual int cpuId() = 0;
108
109    virtual int threadId() = 0;
110
111    virtual void setThreadId(int id) = 0;
112
113    virtual int contextId() = 0;
114
115    virtual void setContextId(int id) = 0;
116
117    virtual TheISA::TLB *getITBPtr() = 0;
118
119    virtual TheISA::TLB *getDTBPtr() = 0;
120
121    virtual System *getSystemPtr() = 0;
122
123#if FULL_SYSTEM
124    virtual TheISA::Kernel::Statistics *getKernelStats() = 0;
125
126    virtual FunctionalPort *getPhysPort() = 0;
127
128    virtual VirtualPort *getVirtPort() = 0;
129
130    virtual void connectMemPorts(ThreadContext *tc) = 0;
131#else
132    virtual TranslatingPort *getMemPort() = 0;
133
134    virtual Process *getProcessPtr() = 0;
135#endif
136
137    virtual Status status() const = 0;
138
139    virtual void setStatus(Status new_status) = 0;
140
141    /// Set the status to Active.  Optional delay indicates number of
142    /// cycles to wait before beginning execution.
143    virtual void activate(int delay = 1) = 0;
144
145    /// Set the status to Suspended.
146    virtual void suspend(int delay = 0) = 0;
147
148    /// Set the status to Halted.
149    virtual void halt(int delay = 0) = 0;
150
151#if FULL_SYSTEM
152    virtual void dumpFuncProfile() = 0;
153#endif
154
155    virtual void takeOverFrom(ThreadContext *old_context) = 0;
156
157    virtual void regStats(const std::string &name) = 0;
158
159    virtual void serialize(std::ostream &os) = 0;
160    virtual void unserialize(Checkpoint *cp, const std::string &section) = 0;
161
162#if FULL_SYSTEM
163    virtual EndQuiesceEvent *getQuiesceEvent() = 0;
164
165    // Not necessarily the best location for these...
166    // Having an extra function just to read these is obnoxious
167    virtual Tick readLastActivate() = 0;
168    virtual Tick readLastSuspend() = 0;
169
170    virtual void profileClear() = 0;
171    virtual void profileSample() = 0;
172#endif
173
174    // Also somewhat obnoxious.  Really only used for the TLB fault.
175    // However, may be quite useful in SPARC.
176    virtual TheISA::MachInst getInst() = 0;
177
178    virtual void copyArchRegs(ThreadContext *tc) = 0;
179
180    virtual void clearArchRegs() = 0;
181
182    //
183    // New accessors for new decoder.
184    //
185    virtual uint64_t readIntReg(int reg_idx) = 0;
186
187    virtual FloatReg readFloatReg(int reg_idx) = 0;
188
189    virtual FloatRegBits readFloatRegBits(int reg_idx) = 0;
190
191    virtual void setIntReg(int reg_idx, uint64_t val) = 0;
192
193    virtual void setFloatReg(int reg_idx, FloatReg val) = 0;
194
195    virtual void setFloatRegBits(int reg_idx, FloatRegBits val) = 0;
196
197    virtual uint64_t readPC() = 0;
198
199    virtual void setPC(uint64_t val) = 0;
200
201    virtual uint64_t readNextPC() = 0;
202
203    virtual void setNextPC(uint64_t val) = 0;
204
205    virtual uint64_t readNextNPC() = 0;
206
207    virtual void setNextNPC(uint64_t val) = 0;
208
209    virtual uint64_t readMicroPC() = 0;
210
211    virtual void setMicroPC(uint64_t val) = 0;
212
213    virtual uint64_t readNextMicroPC() = 0;
214
215    virtual void setNextMicroPC(uint64_t val) = 0;
216
217    virtual MiscReg readMiscRegNoEffect(int misc_reg) = 0;
218
219    virtual MiscReg readMiscReg(int misc_reg) = 0;
220
221    virtual void setMiscRegNoEffect(int misc_reg, const MiscReg &val) = 0;
222
223    virtual void setMiscReg(int misc_reg, const MiscReg &val) = 0;
224
225    virtual int flattenIntIndex(int reg) = 0;
226    virtual int flattenFloatIndex(int reg) = 0;
227
228    virtual uint64_t
229    readRegOtherThread(int misc_reg, ThreadID tid)
230    {
231        return 0;
232    }
233
234    virtual void
235    setRegOtherThread(int misc_reg, const MiscReg &val, ThreadID tid)
236    {
237    }
238
239    // Also not necessarily the best location for these two.  Hopefully will go
240    // away once we decide upon where st cond failures goes.
241    virtual unsigned readStCondFailures() = 0;
242
243    virtual void setStCondFailures(unsigned sc_failures) = 0;
244
245    // Only really makes sense for old CPU model.  Still could be useful though.
246    virtual bool misspeculating() = 0;
247
248#if !FULL_SYSTEM
249    // Same with st cond failures.
250    virtual Counter readFuncExeInst() = 0;
251
252    virtual void syscall(int64_t callnum) = 0;
253
254    // This function exits the thread context in the CPU and returns
255    // 1 if the CPU has no more active threads (meaning it's OK to exit);
256    // Used in syscall-emulation mode when a  thread calls the exit syscall.
257    virtual int exit() { return 1; };
258#endif
259
260    /** function to compare two thread contexts (for debugging) */
261    static void compare(ThreadContext *one, ThreadContext *two);
262};
263
264/**
265 * ProxyThreadContext class that provides a way to implement a
266 * ThreadContext without having to derive from it. ThreadContext is an
267 * abstract class, so anything that derives from it and uses its
268 * interface will pay the overhead of virtual function calls.  This
269 * class is created to enable a user-defined Thread object to be used
270 * wherever ThreadContexts are used, without paying the overhead of
271 * virtual function calls when it is used by itself.  See
272 * simple_thread.hh for an example of this.
273 */
274template <class TC>
275class ProxyThreadContext : public ThreadContext
276{
277  public:
278    ProxyThreadContext(TC *actual_tc)
279    { actualTC = actual_tc; }
280
281  private:
282    TC *actualTC;
283
284  public:
285
286    BaseCPU *getCpuPtr() { return actualTC->getCpuPtr(); }
287
288    int cpuId() { return actualTC->cpuId(); }
289
290    int threadId() { return actualTC->threadId(); }
291
292    void setThreadId(int id) { return actualTC->setThreadId(id); }
293
294    int contextId() { return actualTC->contextId(); }
295
296    void setContextId(int id) { actualTC->setContextId(id); }
297
298    TheISA::TLB *getITBPtr() { return actualTC->getITBPtr(); }
299
300    TheISA::TLB *getDTBPtr() { return actualTC->getDTBPtr(); }
301
302    System *getSystemPtr() { return actualTC->getSystemPtr(); }
303
304#if FULL_SYSTEM
305    TheISA::Kernel::Statistics *getKernelStats()
306    { return actualTC->getKernelStats(); }
307
308    FunctionalPort *getPhysPort() { return actualTC->getPhysPort(); }
309
310    VirtualPort *getVirtPort() { return actualTC->getVirtPort(); }
311
312    void connectMemPorts(ThreadContext *tc) { actualTC->connectMemPorts(tc); }
313#else
314    TranslatingPort *getMemPort() { return actualTC->getMemPort(); }
315
316    Process *getProcessPtr() { return actualTC->getProcessPtr(); }
317#endif
318
319    Status status() const { return actualTC->status(); }
320
321    void setStatus(Status new_status) { actualTC->setStatus(new_status); }
322
323    /// Set the status to Active.  Optional delay indicates number of
324    /// cycles to wait before beginning execution.
325    void activate(int delay = 1) { actualTC->activate(delay); }
326
327    /// Set the status to Suspended.
328    void suspend(int delay = 0) { actualTC->suspend(); }
329
330    /// Set the status to Halted.
331    void halt(int delay = 0) { actualTC->halt(); }
332
333#if FULL_SYSTEM
334    void dumpFuncProfile() { actualTC->dumpFuncProfile(); }
335#endif
336
337    void takeOverFrom(ThreadContext *oldContext)
338    { actualTC->takeOverFrom(oldContext); }
339
340    void regStats(const std::string &name) { actualTC->regStats(name); }
341
342    void serialize(std::ostream &os) { actualTC->serialize(os); }
343    void unserialize(Checkpoint *cp, const std::string &section)
344    { actualTC->unserialize(cp, section); }
345
346#if FULL_SYSTEM
347    EndQuiesceEvent *getQuiesceEvent() { return actualTC->getQuiesceEvent(); }
348
349    Tick readLastActivate() { return actualTC->readLastActivate(); }
350    Tick readLastSuspend() { return actualTC->readLastSuspend(); }
351
352    void profileClear() { return actualTC->profileClear(); }
353    void profileSample() { return actualTC->profileSample(); }
354#endif
355    // @todo: Do I need this?
356    MachInst getInst() { return actualTC->getInst(); }
357
358    // @todo: Do I need this?
359    void copyArchRegs(ThreadContext *tc) { actualTC->copyArchRegs(tc); }
360
361    void clearArchRegs() { actualTC->clearArchRegs(); }
362
363    //
364    // New accessors for new decoder.
365    //
366    uint64_t readIntReg(int reg_idx)
367    { return actualTC->readIntReg(reg_idx); }
368
369    FloatReg readFloatReg(int reg_idx)
370    { return actualTC->readFloatReg(reg_idx); }
371
372    FloatRegBits readFloatRegBits(int reg_idx)
373    { return actualTC->readFloatRegBits(reg_idx); }
374
375    void setIntReg(int reg_idx, uint64_t val)
376    { actualTC->setIntReg(reg_idx, val); }
377
378    void setFloatReg(int reg_idx, FloatReg val)
379    { actualTC->setFloatReg(reg_idx, val); }
380
381    void setFloatRegBits(int reg_idx, FloatRegBits val)
382    { actualTC->setFloatRegBits(reg_idx, val); }
383
384    uint64_t readPC() { return actualTC->readPC(); }
385
386    void setPC(uint64_t val) { actualTC->setPC(val); }
387
388    uint64_t readNextPC() { return actualTC->readNextPC(); }
389
390    void setNextPC(uint64_t val) { actualTC->setNextPC(val); }
391
392    uint64_t readNextNPC() { return actualTC->readNextNPC(); }
393
394    void setNextNPC(uint64_t val) { actualTC->setNextNPC(val); }
395
396    uint64_t readMicroPC() { return actualTC->readMicroPC(); }
397
398    void setMicroPC(uint64_t val) { actualTC->setMicroPC(val); }
399
400    uint64_t readNextMicroPC() { return actualTC->readMicroPC(); }
401
402    void setNextMicroPC(uint64_t val) { actualTC->setNextMicroPC(val); }
403
404    bool readPredicate() { return actualTC->readPredicate(); }
405
406    void setPredicate(bool val)
407    { actualTC->setPredicate(val); }
408
409    MiscReg readMiscRegNoEffect(int misc_reg)
410    { return actualTC->readMiscRegNoEffect(misc_reg); }
411
412    MiscReg readMiscReg(int misc_reg)
413    { return actualTC->readMiscReg(misc_reg); }
414
415    void setMiscRegNoEffect(int misc_reg, const MiscReg &val)
416    { return actualTC->setMiscRegNoEffect(misc_reg, val); }
417
418    void setMiscReg(int misc_reg, const MiscReg &val)
419    { return actualTC->setMiscReg(misc_reg, val); }
420
421    int flattenIntIndex(int reg)
422    { return actualTC->flattenIntIndex(reg); }
423
424    int flattenFloatIndex(int reg)
425    { return actualTC->flattenFloatIndex(reg); }
426
427    unsigned readStCondFailures()
428    { return actualTC->readStCondFailures(); }
429
430    void setStCondFailures(unsigned sc_failures)
431    { actualTC->setStCondFailures(sc_failures); }
432
433    // @todo: Fix this!
434    bool misspeculating() { return actualTC->misspeculating(); }
435
436#if !FULL_SYSTEM
437    void syscall(int64_t callnum)
438    { actualTC->syscall(callnum); }
439
440    Counter readFuncExeInst() { return actualTC->readFuncExeInst(); }
441#endif
442};
443
444#endif
445