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