thread_context.hh revision 10934:5af8f40d8f2c
1/*
2 * Copyright (c) 2011-2012 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
55// @todo: Figure out a more architecture independent way to obtain the ITB and
56// DTB pointers.
57namespace TheISA
58{
59    class Decoder;
60    class TLB;
61}
62class BaseCPU;
63class CheckerCPU;
64class Checkpoint;
65class EndQuiesceEvent;
66class SETranslatingPortProxy;
67class FSTranslatingPortProxy;
68class PortProxy;
69class Process;
70class System;
71namespace TheISA {
72    namespace Kernel {
73        class Statistics;
74    }
75}
76
77/**
78 * ThreadContext is the external interface to all thread state for
79 * anything outside of the CPU. It provides all accessor methods to
80 * state that might be needed by external objects, ranging from
81 * register values to things such as kernel stats. It is an abstract
82 * base class; the CPU can create its own ThreadContext by either
83 * deriving from it, or using the templated ProxyThreadContext.
84 *
85 * The ThreadContext is slightly different than the ExecContext.  The
86 * ThreadContext provides access to an individual thread's state; an
87 * ExecContext provides ISA access to the CPU (meaning it is
88 * implicitly multithreaded on SMT systems).  Additionally the
89 * ThreadState is an abstract class that exactly defines the
90 * interface; the ExecContext is a more implicit interface that must
91 * be implemented so that the ISA can access whatever state it needs.
92 */
93class ThreadContext
94{
95  protected:
96    typedef TheISA::MachInst MachInst;
97    typedef TheISA::IntReg IntReg;
98    typedef TheISA::FloatReg FloatReg;
99    typedef TheISA::FloatRegBits FloatRegBits;
100    typedef TheISA::CCReg CCReg;
101    typedef TheISA::VectorReg VectorReg;
102    typedef TheISA::MiscReg MiscReg;
103  public:
104
105    enum Status
106    {
107        /// Running.  Instructions should be executed only when
108        /// the context is in this state.
109        Active,
110
111        /// Temporarily inactive.  Entered while waiting for
112        /// synchronization, etc.
113        Suspended,
114
115        /// Permanently shut down.  Entered when target executes
116        /// m5exit pseudo-instruction.  When all contexts enter
117        /// this state, the simulation will terminate.
118        Halted
119    };
120
121    virtual ~ThreadContext() { };
122
123    virtual BaseCPU *getCpuPtr() = 0;
124
125    virtual int cpuId() const = 0;
126
127    virtual uint32_t socketId() const = 0;
128
129    virtual int threadId() const = 0;
130
131    virtual void setThreadId(int id) = 0;
132
133    virtual int contextId() const = 0;
134
135    virtual void setContextId(int id) = 0;
136
137    virtual TheISA::TLB *getITBPtr() = 0;
138
139    virtual TheISA::TLB *getDTBPtr() = 0;
140
141    virtual CheckerCPU *getCheckerCpuPtr() = 0;
142
143    virtual TheISA::Decoder *getDecoderPtr() = 0;
144
145    virtual System *getSystemPtr() = 0;
146
147    virtual TheISA::Kernel::Statistics *getKernelStats() = 0;
148
149    virtual PortProxy &getPhysProxy() = 0;
150
151    virtual FSTranslatingPortProxy &getVirtProxy() = 0;
152
153    /**
154     * Initialise the physical and virtual port proxies and tie them to
155     * the data port of the CPU.
156     *
157     * tc ThreadContext for the virtual-to-physical translation
158     */
159    virtual void initMemProxies(ThreadContext *tc) = 0;
160
161    virtual SETranslatingPortProxy &getMemProxy() = 0;
162
163    virtual Process *getProcessPtr() = 0;
164
165    virtual Status status() const = 0;
166
167    virtual void setStatus(Status new_status) = 0;
168
169    /// Set the status to Active.
170    virtual void activate() = 0;
171
172    /// Set the status to Suspended.
173    virtual void suspend() = 0;
174
175    /// Set the status to Halted.
176    virtual void halt() = 0;
177
178    virtual void dumpFuncProfile() = 0;
179
180    virtual void takeOverFrom(ThreadContext *old_context) = 0;
181
182    virtual void regStats(const std::string &name) = 0;
183
184    virtual EndQuiesceEvent *getQuiesceEvent() = 0;
185
186    // Not necessarily the best location for these...
187    // Having an extra function just to read these is obnoxious
188    virtual Tick readLastActivate() = 0;
189    virtual Tick readLastSuspend() = 0;
190
191    virtual void profileClear() = 0;
192    virtual void profileSample() = 0;
193
194    virtual void copyArchRegs(ThreadContext *tc) = 0;
195
196    virtual void clearArchRegs() = 0;
197
198    //
199    // New accessors for new decoder.
200    //
201    virtual uint64_t readIntReg(int reg_idx) = 0;
202
203    virtual FloatReg readFloatReg(int reg_idx) = 0;
204
205    virtual FloatRegBits readFloatRegBits(int reg_idx) = 0;
206
207    virtual CCReg readCCReg(int reg_idx) = 0;
208
209    virtual const VectorReg &readVectorReg(int reg_idx) = 0;
210
211    virtual void setIntReg(int reg_idx, uint64_t val) = 0;
212
213    virtual void setFloatReg(int reg_idx, FloatReg val) = 0;
214
215    virtual void setFloatRegBits(int reg_idx, FloatRegBits val) = 0;
216
217    virtual void setCCReg(int reg_idx, CCReg val) = 0;
218
219    virtual void setVectorReg(int reg_idx, const VectorReg &val) = 0;
220
221    virtual TheISA::PCState pcState() = 0;
222
223    virtual void pcState(const TheISA::PCState &val) = 0;
224
225    virtual void pcStateNoRecord(const TheISA::PCState &val) = 0;
226
227    virtual Addr instAddr() = 0;
228
229    virtual Addr nextInstAddr() = 0;
230
231    virtual MicroPC microPC() = 0;
232
233    virtual MiscReg readMiscRegNoEffect(int misc_reg) const = 0;
234
235    virtual MiscReg readMiscReg(int misc_reg) = 0;
236
237    virtual void setMiscRegNoEffect(int misc_reg, const MiscReg &val) = 0;
238
239    virtual void setMiscReg(int misc_reg, const MiscReg &val) = 0;
240
241    virtual int flattenIntIndex(int reg) = 0;
242    virtual int flattenFloatIndex(int reg) = 0;
243    virtual int flattenCCIndex(int reg) = 0;
244    virtual int flattenVectorIndex(int reg) = 0;
245    virtual int flattenMiscIndex(int reg) = 0;
246
247    virtual uint64_t
248    readRegOtherThread(int misc_reg, ThreadID tid)
249    {
250        return 0;
251    }
252
253    virtual void
254    setRegOtherThread(int misc_reg, const MiscReg &val, ThreadID tid)
255    {
256    }
257
258    // Also not necessarily the best location for these two.  Hopefully will go
259    // away once we decide upon where st cond failures goes.
260    virtual unsigned readStCondFailures() = 0;
261
262    virtual void setStCondFailures(unsigned sc_failures) = 0;
263
264    // Same with st cond failures.
265    virtual Counter readFuncExeInst() = 0;
266
267    virtual void syscall(int64_t callnum) = 0;
268
269    // This function exits the thread context in the CPU and returns
270    // 1 if the CPU has no more active threads (meaning it's OK to exit);
271    // Used in syscall-emulation mode when a  thread calls the exit syscall.
272    virtual int exit() { return 1; };
273
274    /** function to compare two thread contexts (for debugging) */
275    static void compare(ThreadContext *one, ThreadContext *two);
276
277    /** @{ */
278    /**
279     * Flat register interfaces
280     *
281     * Some architectures have different registers visible in
282     * different modes. Such architectures "flatten" a register (see
283     * flattenIntIndex() and flattenFloatIndex()) to map it into the
284     * gem5 register file. This interface provides a flat interface to
285     * the underlying register file, which allows for example
286     * serialization code to access all registers.
287     */
288
289    virtual uint64_t readIntRegFlat(int idx) = 0;
290    virtual void setIntRegFlat(int idx, uint64_t val) = 0;
291
292    virtual FloatReg readFloatRegFlat(int idx) = 0;
293    virtual void setFloatRegFlat(int idx, FloatReg val) = 0;
294
295    virtual FloatRegBits readFloatRegBitsFlat(int idx) = 0;
296    virtual void setFloatRegBitsFlat(int idx, FloatRegBits val) = 0;
297
298    virtual CCReg readCCRegFlat(int idx) = 0;
299    virtual void setCCRegFlat(int idx, CCReg val) = 0;
300
301    virtual const VectorReg &readVectorRegFlat(int idx) = 0;
302    virtual void setVectorRegFlat(int idx, const VectorReg &val) = 0;
303    /** @} */
304
305};
306
307/**
308 * ProxyThreadContext class that provides a way to implement a
309 * ThreadContext without having to derive from it. ThreadContext is an
310 * abstract class, so anything that derives from it and uses its
311 * interface will pay the overhead of virtual function calls.  This
312 * class is created to enable a user-defined Thread object to be used
313 * wherever ThreadContexts are used, without paying the overhead of
314 * virtual function calls when it is used by itself.  See
315 * simple_thread.hh for an example of this.
316 */
317template <class TC>
318class ProxyThreadContext : public ThreadContext
319{
320  public:
321    ProxyThreadContext(TC *actual_tc)
322    { actualTC = actual_tc; }
323
324  private:
325    TC *actualTC;
326
327  public:
328
329    BaseCPU *getCpuPtr() { return actualTC->getCpuPtr(); }
330
331    int cpuId() const { return actualTC->cpuId(); }
332
333    uint32_t socketId() const { return actualTC->socketId(); }
334
335    int threadId() const { return actualTC->threadId(); }
336
337    void setThreadId(int id) { actualTC->setThreadId(id); }
338
339    int contextId() const { return actualTC->contextId(); }
340
341    void setContextId(int id) { actualTC->setContextId(id); }
342
343    TheISA::TLB *getITBPtr() { return actualTC->getITBPtr(); }
344
345    TheISA::TLB *getDTBPtr() { return actualTC->getDTBPtr(); }
346
347    CheckerCPU *getCheckerCpuPtr() { return actualTC->getCheckerCpuPtr(); }
348
349    TheISA::Decoder *getDecoderPtr() { return actualTC->getDecoderPtr(); }
350
351    System *getSystemPtr() { return actualTC->getSystemPtr(); }
352
353    TheISA::Kernel::Statistics *getKernelStats()
354    { return actualTC->getKernelStats(); }
355
356    PortProxy &getPhysProxy() { return actualTC->getPhysProxy(); }
357
358    FSTranslatingPortProxy &getVirtProxy() { return actualTC->getVirtProxy(); }
359
360    void initMemProxies(ThreadContext *tc) { actualTC->initMemProxies(tc); }
361
362    SETranslatingPortProxy &getMemProxy() { return actualTC->getMemProxy(); }
363
364    Process *getProcessPtr() { return actualTC->getProcessPtr(); }
365
366    Status status() const { return actualTC->status(); }
367
368    void setStatus(Status new_status) { actualTC->setStatus(new_status); }
369
370    /// Set the status to Active.
371    void activate() { actualTC->activate(); }
372
373    /// Set the status to Suspended.
374    void suspend() { actualTC->suspend(); }
375
376    /// Set the status to Halted.
377    void halt() { actualTC->halt(); }
378
379    void dumpFuncProfile() { actualTC->dumpFuncProfile(); }
380
381    void takeOverFrom(ThreadContext *oldContext)
382    { actualTC->takeOverFrom(oldContext); }
383
384    void regStats(const std::string &name) { actualTC->regStats(name); }
385
386    EndQuiesceEvent *getQuiesceEvent() { return actualTC->getQuiesceEvent(); }
387
388    Tick readLastActivate() { return actualTC->readLastActivate(); }
389    Tick readLastSuspend() { return actualTC->readLastSuspend(); }
390
391    void profileClear() { return actualTC->profileClear(); }
392    void profileSample() { return actualTC->profileSample(); }
393
394    // @todo: Do I need this?
395    void copyArchRegs(ThreadContext *tc) { actualTC->copyArchRegs(tc); }
396
397    void clearArchRegs() { actualTC->clearArchRegs(); }
398
399    //
400    // New accessors for new decoder.
401    //
402    uint64_t readIntReg(int reg_idx)
403    { return actualTC->readIntReg(reg_idx); }
404
405    FloatReg readFloatReg(int reg_idx)
406    { return actualTC->readFloatReg(reg_idx); }
407
408    FloatRegBits readFloatRegBits(int reg_idx)
409    { return actualTC->readFloatRegBits(reg_idx); }
410
411    CCReg readCCReg(int reg_idx)
412    { return actualTC->readCCReg(reg_idx); }
413
414    const VectorReg &readVectorReg(int reg_idx)
415    { return actualTC->readVectorReg(reg_idx); }
416
417    void setIntReg(int reg_idx, uint64_t val)
418    { actualTC->setIntReg(reg_idx, val); }
419
420    void setFloatReg(int reg_idx, FloatReg val)
421    { actualTC->setFloatReg(reg_idx, val); }
422
423    void setFloatRegBits(int reg_idx, FloatRegBits val)
424    { actualTC->setFloatRegBits(reg_idx, val); }
425
426    void setCCReg(int reg_idx, CCReg val)
427    { actualTC->setCCReg(reg_idx, val); }
428
429    void setVectorReg(int reg_idx, const VectorReg &val)
430    { actualTC->setVectorReg(reg_idx, val); }
431
432    TheISA::PCState pcState() { return actualTC->pcState(); }
433
434    void pcState(const TheISA::PCState &val) { actualTC->pcState(val); }
435
436    void pcStateNoRecord(const TheISA::PCState &val) { actualTC->pcState(val); }
437
438    Addr instAddr() { return actualTC->instAddr(); }
439    Addr nextInstAddr() { return actualTC->nextInstAddr(); }
440    MicroPC microPC() { return actualTC->microPC(); }
441
442    bool readPredicate() { return actualTC->readPredicate(); }
443
444    void setPredicate(bool val)
445    { actualTC->setPredicate(val); }
446
447    MiscReg readMiscRegNoEffect(int misc_reg) const
448    { return actualTC->readMiscRegNoEffect(misc_reg); }
449
450    MiscReg readMiscReg(int misc_reg)
451    { return actualTC->readMiscReg(misc_reg); }
452
453    void setMiscRegNoEffect(int misc_reg, const MiscReg &val)
454    { return actualTC->setMiscRegNoEffect(misc_reg, val); }
455
456    void setMiscReg(int misc_reg, const MiscReg &val)
457    { return actualTC->setMiscReg(misc_reg, val); }
458
459    int flattenIntIndex(int reg)
460    { return actualTC->flattenIntIndex(reg); }
461
462    int flattenFloatIndex(int reg)
463    { return actualTC->flattenFloatIndex(reg); }
464
465    int flattenCCIndex(int reg)
466    { return actualTC->flattenCCIndex(reg); }
467
468    int flattenVectorIndex(int reg)
469    { return actualTC->flattenVectorIndex(reg); }
470
471    int flattenMiscIndex(int reg)
472    { return actualTC->flattenMiscIndex(reg); }
473
474    unsigned readStCondFailures()
475    { return actualTC->readStCondFailures(); }
476
477    void setStCondFailures(unsigned sc_failures)
478    { actualTC->setStCondFailures(sc_failures); }
479
480    void syscall(int64_t callnum)
481    { actualTC->syscall(callnum); }
482
483    Counter readFuncExeInst() { return actualTC->readFuncExeInst(); }
484
485    uint64_t readIntRegFlat(int idx)
486    { return actualTC->readIntRegFlat(idx); }
487
488    void setIntRegFlat(int idx, uint64_t val)
489    { actualTC->setIntRegFlat(idx, val); }
490
491    FloatReg readFloatRegFlat(int idx)
492    { return actualTC->readFloatRegFlat(idx); }
493
494    void setFloatRegFlat(int idx, FloatReg val)
495    { actualTC->setFloatRegFlat(idx, val); }
496
497    FloatRegBits readFloatRegBitsFlat(int idx)
498    { return actualTC->readFloatRegBitsFlat(idx); }
499
500    void setFloatRegBitsFlat(int idx, FloatRegBits val)
501    { actualTC->setFloatRegBitsFlat(idx, val); }
502
503    CCReg readCCRegFlat(int idx)
504    { return actualTC->readCCRegFlat(idx); }
505
506    void setCCRegFlat(int idx, CCReg val)
507    { actualTC->setCCRegFlat(idx, val); }
508
509    const VectorReg &readVectorRegFlat(int idx)
510    { return actualTC->readVectorRegFlat(idx); }
511
512    void setVectorRegFlat(int idx, const VectorReg &val)
513    { actualTC->setVectorRegFlat(idx, val); }
514};
515
516/** @{ */
517/**
518 * Thread context serialization helpers
519 *
520 * These helper functions provide a way to the data in a
521 * ThreadContext. They are provided as separate helper function since
522 * implementing them as members of the ThreadContext interface would
523 * be confusing when the ThreadContext is exported via a proxy.
524 */
525
526void serialize(ThreadContext &tc, CheckpointOut &cp);
527void unserialize(ThreadContext &tc, CheckpointIn &cp);
528
529/** @} */
530
531
532/**
533 * Copy state between thread contexts in preparation for CPU handover.
534 *
535 * @note This method modifies the old thread contexts as well as the
536 * new thread context. The old thread context will have its quiesce
537 * event descheduled if it is scheduled and its status set to halted.
538 *
539 * @param new_tc Destination ThreadContext.
540 * @param old_tc Source ThreadContext.
541 */
542void takeOverFrom(ThreadContext &new_tc, ThreadContext &old_tc);
543
544#endif
545