simple_thread.hh revision 5222:bb733a878f85
110447Snilay@cs.wisc.edu/*
210447Snilay@cs.wisc.edu * Copyright (c) 2001-2006 The Regents of The University of Michigan
310447Snilay@cs.wisc.edu * All rights reserved.
410447Snilay@cs.wisc.edu *
510447Snilay@cs.wisc.edu * Redistribution and use in source and binary forms, with or without
610447Snilay@cs.wisc.edu * modification, are permitted provided that the following conditions are
710447Snilay@cs.wisc.edu * met: redistributions of source code must retain the above copyright
810447Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer;
910447Snilay@cs.wisc.edu * redistributions in binary form must reproduce the above copyright
1010447Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer in the
1110447Snilay@cs.wisc.edu * documentation and/or other materials provided with the distribution;
1210447Snilay@cs.wisc.edu * neither the name of the copyright holders nor the names of its
1310447Snilay@cs.wisc.edu * contributors may be used to endorse or promote products derived from
1410447Snilay@cs.wisc.edu * this software without specific prior written permission.
1510447Snilay@cs.wisc.edu *
1610447Snilay@cs.wisc.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710447Snilay@cs.wisc.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810447Snilay@cs.wisc.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910447Snilay@cs.wisc.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010447Snilay@cs.wisc.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110447Snilay@cs.wisc.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210447Snilay@cs.wisc.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310447Snilay@cs.wisc.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410447Snilay@cs.wisc.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510447Snilay@cs.wisc.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610447Snilay@cs.wisc.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710447Snilay@cs.wisc.edu *
2810447Snilay@cs.wisc.edu * Authors: Steve Reinhardt
2910447Snilay@cs.wisc.edu *          Nathan Binkert
3010447Snilay@cs.wisc.edu */
3110447Snilay@cs.wisc.edu
3210447Snilay@cs.wisc.edu#ifndef __CPU_SIMPLE_THREAD_HH__
3310447Snilay@cs.wisc.edu#define __CPU_SIMPLE_THREAD_HH__
3410447Snilay@cs.wisc.edu
3510447Snilay@cs.wisc.edu#include "arch/isa_traits.hh"
3610447Snilay@cs.wisc.edu#include "arch/regfile.hh"
3710447Snilay@cs.wisc.edu#include "arch/syscallreturn.hh"
3810447Snilay@cs.wisc.edu#include "arch/tlb.hh"
3910447Snilay@cs.wisc.edu#include "config/full_system.hh"
4010447Snilay@cs.wisc.edu#include "cpu/thread_context.hh"
4110447Snilay@cs.wisc.edu#include "cpu/thread_state.hh"
4210447Snilay@cs.wisc.edu#include "mem/request.hh"
4310447Snilay@cs.wisc.edu#include "sim/byteswap.hh"
4410447Snilay@cs.wisc.edu#include "sim/eventq.hh"
4510447Snilay@cs.wisc.edu#include "sim/host.hh"
4610447Snilay@cs.wisc.edu#include "sim/serialize.hh"
4710447Snilay@cs.wisc.edu
4810447Snilay@cs.wisc.educlass BaseCPU;
4910447Snilay@cs.wisc.edu
5010447Snilay@cs.wisc.edu#if FULL_SYSTEM
5110447Snilay@cs.wisc.edu
5210447Snilay@cs.wisc.edu#include "sim/system.hh"
5310447Snilay@cs.wisc.edu
5410447Snilay@cs.wisc.educlass FunctionProfile;
5510447Snilay@cs.wisc.educlass ProfileNode;
5610447Snilay@cs.wisc.educlass FunctionalPort;
5710447Snilay@cs.wisc.educlass PhysicalPort;
5810447Snilay@cs.wisc.edu
5910447Snilay@cs.wisc.edunamespace TheISA {
6010447Snilay@cs.wisc.edu    namespace Kernel {
6110447Snilay@cs.wisc.edu        class Statistics;
6210447Snilay@cs.wisc.edu    };
6310447Snilay@cs.wisc.edu};
6410447Snilay@cs.wisc.edu
6510447Snilay@cs.wisc.edu#else // !FULL_SYSTEM
6610447Snilay@cs.wisc.edu
6710447Snilay@cs.wisc.edu#include "sim/process.hh"
6810447Snilay@cs.wisc.edu#include "mem/page_table.hh"
6910447Snilay@cs.wisc.educlass TranslatingPort;
7010447Snilay@cs.wisc.edu
7110447Snilay@cs.wisc.edu#endif // FULL_SYSTEM
7210447Snilay@cs.wisc.edu
7310447Snilay@cs.wisc.edu/**
7410447Snilay@cs.wisc.edu * The SimpleThread object provides a combination of the ThreadState
7510447Snilay@cs.wisc.edu * object and the ThreadContext interface. It implements the
7610447Snilay@cs.wisc.edu * ThreadContext interface so that a ProxyThreadContext class can be
7710447Snilay@cs.wisc.edu * made using SimpleThread as the template parameter (see
7810447Snilay@cs.wisc.edu * thread_context.hh). It adds to the ThreadState object by adding all
7910447Snilay@cs.wisc.edu * the objects needed for simple functional execution, including a
8010447Snilay@cs.wisc.edu * simple architectural register file, and pointers to the ITB and DTB
8110447Snilay@cs.wisc.edu * in full system mode. For CPU models that do not need more advanced
8210447Snilay@cs.wisc.edu * ways to hold state (i.e. a separate physical register file, or
8310447Snilay@cs.wisc.edu * separate fetch and commit PC's), this SimpleThread class provides
8410447Snilay@cs.wisc.edu * all the necessary state for full architecture-level functional
8510447Snilay@cs.wisc.edu * simulation.  See the AtomicSimpleCPU or TimingSimpleCPU for
8610447Snilay@cs.wisc.edu * examples.
8710447Snilay@cs.wisc.edu */
8810447Snilay@cs.wisc.edu
8910447Snilay@cs.wisc.educlass SimpleThread : public ThreadState
9010447Snilay@cs.wisc.edu{
9110447Snilay@cs.wisc.edu  protected:
9210447Snilay@cs.wisc.edu    typedef TheISA::RegFile RegFile;
9310447Snilay@cs.wisc.edu    typedef TheISA::MachInst MachInst;
9410447Snilay@cs.wisc.edu    typedef TheISA::MiscRegFile MiscRegFile;
9510447Snilay@cs.wisc.edu    typedef TheISA::MiscReg MiscReg;
9610447Snilay@cs.wisc.edu    typedef TheISA::FloatReg FloatReg;
9710447Snilay@cs.wisc.edu    typedef TheISA::FloatRegBits FloatRegBits;
9810447Snilay@cs.wisc.edu  public:
9910447Snilay@cs.wisc.edu    typedef ThreadContext::Status Status;
10010447Snilay@cs.wisc.edu
10110447Snilay@cs.wisc.edu  protected:
10210447Snilay@cs.wisc.edu    RegFile regs;	// correct-path register context
10310447Snilay@cs.wisc.edu
10410447Snilay@cs.wisc.edu  public:
10510447Snilay@cs.wisc.edu    // pointer to CPU associated with this SimpleThread
10610447Snilay@cs.wisc.edu    BaseCPU *cpu;
10710447Snilay@cs.wisc.edu
10810447Snilay@cs.wisc.edu    ProxyThreadContext<SimpleThread> *tc;
10910447Snilay@cs.wisc.edu
11010447Snilay@cs.wisc.edu    System *system;
11110447Snilay@cs.wisc.edu
11210447Snilay@cs.wisc.edu    TheISA::ITB *itb;
11310447Snilay@cs.wisc.edu    TheISA::DTB *dtb;
11410447Snilay@cs.wisc.edu
11510447Snilay@cs.wisc.edu    // constructor: initialize SimpleThread from given process structure
11610447Snilay@cs.wisc.edu#if FULL_SYSTEM
11710447Snilay@cs.wisc.edu    SimpleThread(BaseCPU *_cpu, int _thread_num, System *_system,
11810447Snilay@cs.wisc.edu                 TheISA::ITB *_itb, TheISA::DTB *_dtb,
11910447Snilay@cs.wisc.edu                 bool use_kernel_stats = true);
12010447Snilay@cs.wisc.edu#else
12110447Snilay@cs.wisc.edu    SimpleThread(BaseCPU *_cpu, int _thread_num, Process *_process,
12210447Snilay@cs.wisc.edu                 TheISA::ITB *_itb, TheISA::DTB *_dtb, int _asid);
12310447Snilay@cs.wisc.edu#endif
12410447Snilay@cs.wisc.edu
12510447Snilay@cs.wisc.edu    SimpleThread();
12610447Snilay@cs.wisc.edu
12710447Snilay@cs.wisc.edu    virtual ~SimpleThread();
12810447Snilay@cs.wisc.edu
12910447Snilay@cs.wisc.edu    virtual void takeOverFrom(ThreadContext *oldContext);
130
131    void regStats(const std::string &name);
132
133    void copyTC(ThreadContext *context);
134
135    void copyState(ThreadContext *oldContext);
136
137    void serialize(std::ostream &os);
138    void unserialize(Checkpoint *cp, const std::string &section);
139
140    /***************************************************************
141     *  SimpleThread functions to provide CPU with access to various
142     *  state, and to provide address translation methods.
143     **************************************************************/
144
145    /** Returns the pointer to this SimpleThread's ThreadContext. Used
146     *  when a ThreadContext must be passed to objects outside of the
147     *  CPU.
148     */
149    ThreadContext *getTC() { return tc; }
150
151    Fault translateInstReq(RequestPtr &req)
152    {
153        return itb->translate(req, tc);
154    }
155
156    Fault translateDataReadReq(RequestPtr &req)
157    {
158        return dtb->translate(req, tc, false);
159    }
160
161    Fault translateDataWriteReq(RequestPtr &req)
162    {
163        return dtb->translate(req, tc, true);
164    }
165
166#if FULL_SYSTEM
167    int getInstAsid() { return regs.instAsid(); }
168    int getDataAsid() { return regs.dataAsid(); }
169
170    void dumpFuncProfile();
171
172    Fault hwrei();
173
174    bool simPalCheck(int palFunc);
175
176#endif
177
178    /*******************************************
179     * ThreadContext interface functions.
180     ******************************************/
181
182    BaseCPU *getCpuPtr() { return cpu; }
183
184    int getThreadNum() { return tid; }
185
186    TheISA::ITB *getITBPtr() { return itb; }
187
188    TheISA::DTB *getDTBPtr() { return dtb; }
189
190#if FULL_SYSTEM
191    System *getSystemPtr() { return system; }
192
193    FunctionalPort *getPhysPort() { return physPort; }
194
195    /** Return a virtual port. If no thread context is specified then a static
196     * port is returned. Otherwise a port is created and returned. It must be
197     * deleted by deleteVirtPort(). */
198    VirtualPort *getVirtPort(ThreadContext *tc);
199
200    void delVirtPort(VirtualPort *vp);
201#endif
202
203    Status status() const { return _status; }
204
205    void setStatus(Status newStatus) { _status = newStatus; }
206
207    /// Set the status to Active.  Optional delay indicates number of
208    /// cycles to wait before beginning execution.
209    void activate(int delay = 1);
210
211    /// Set the status to Suspended.
212    void suspend();
213
214    /// Set the status to Unallocated.
215    void deallocate();
216
217    /// Set the status to Halted.
218    void halt();
219
220    virtual bool misspeculating();
221
222    Fault instRead(RequestPtr &req)
223    {
224        panic("instRead not implemented");
225        // return funcPhysMem->read(req, inst);
226        return NoFault;
227    }
228
229    void copyArchRegs(ThreadContext *tc);
230
231    void clearArchRegs() { regs.clear(); }
232
233    //
234    // New accessors for new decoder.
235    //
236    uint64_t readIntReg(int reg_idx)
237    {
238        int flatIndex = TheISA::flattenIntIndex(getTC(), reg_idx);
239        return regs.readIntReg(flatIndex);
240    }
241
242    FloatReg readFloatReg(int reg_idx, int width)
243    {
244        int flatIndex = TheISA::flattenFloatIndex(getTC(), reg_idx);
245        return regs.readFloatReg(flatIndex, width);
246    }
247
248    FloatReg readFloatReg(int reg_idx)
249    {
250        int flatIndex = TheISA::flattenFloatIndex(getTC(), reg_idx);
251        return regs.readFloatReg(flatIndex);
252    }
253
254    FloatRegBits readFloatRegBits(int reg_idx, int width)
255    {
256        int flatIndex = TheISA::flattenFloatIndex(getTC(), reg_idx);
257        return regs.readFloatRegBits(flatIndex, width);
258    }
259
260    FloatRegBits readFloatRegBits(int reg_idx)
261    {
262        int flatIndex = TheISA::flattenFloatIndex(getTC(), reg_idx);
263        return regs.readFloatRegBits(flatIndex);
264    }
265
266    void setIntReg(int reg_idx, uint64_t val)
267    {
268        int flatIndex = TheISA::flattenIntIndex(getTC(), reg_idx);
269        regs.setIntReg(flatIndex, val);
270    }
271
272    void setFloatReg(int reg_idx, FloatReg val, int width)
273    {
274        int flatIndex = TheISA::flattenFloatIndex(getTC(), reg_idx);
275        regs.setFloatReg(flatIndex, val, width);
276    }
277
278    void setFloatReg(int reg_idx, FloatReg val)
279    {
280        int flatIndex = TheISA::flattenFloatIndex(getTC(), reg_idx);
281        regs.setFloatReg(flatIndex, val);
282    }
283
284    void setFloatRegBits(int reg_idx, FloatRegBits val, int width)
285    {
286        int flatIndex = TheISA::flattenFloatIndex(getTC(), reg_idx);
287        regs.setFloatRegBits(flatIndex, val, width);
288    }
289
290    void setFloatRegBits(int reg_idx, FloatRegBits val)
291    {
292        int flatIndex = TheISA::flattenFloatIndex(getTC(), reg_idx);
293        regs.setFloatRegBits(flatIndex, val);
294    }
295
296    uint64_t readPC()
297    {
298        return regs.readPC();
299    }
300
301    void setPC(uint64_t val)
302    {
303        regs.setPC(val);
304    }
305
306    uint64_t readMicroPC()
307    {
308        return microPC;
309    }
310
311    void setMicroPC(uint64_t val)
312    {
313        microPC = val;
314    }
315
316    uint64_t readNextPC()
317    {
318        return regs.readNextPC();
319    }
320
321    void setNextPC(uint64_t val)
322    {
323        regs.setNextPC(val);
324    }
325
326    uint64_t readNextMicroPC()
327    {
328        return nextMicroPC;
329    }
330
331    void setNextMicroPC(uint64_t val)
332    {
333        nextMicroPC = val;
334    }
335
336    uint64_t readNextNPC()
337    {
338        return regs.readNextNPC();
339    }
340
341    void setNextNPC(uint64_t val)
342    {
343        regs.setNextNPC(val);
344    }
345
346    MiscReg readMiscRegNoEffect(int misc_reg, unsigned tid = 0)
347    {
348        return regs.readMiscRegNoEffect(misc_reg);
349    }
350
351    MiscReg readMiscReg(int misc_reg, unsigned tid = 0)
352    {
353        return regs.readMiscReg(misc_reg, tc);
354    }
355
356    void setMiscRegNoEffect(int misc_reg, const MiscReg &val, unsigned tid = 0)
357    {
358        return regs.setMiscRegNoEffect(misc_reg, val);
359    }
360
361    void setMiscReg(int misc_reg, const MiscReg &val, unsigned tid = 0)
362    {
363        return regs.setMiscReg(misc_reg, val, tc);
364    }
365
366    unsigned readStCondFailures() { return storeCondFailures; }
367
368    void setStCondFailures(unsigned sc_failures)
369    { storeCondFailures = sc_failures; }
370
371    void setShadowSet(int css, int tid=0) {
372      regs.setShadowSet(css);
373    }
374
375#if !FULL_SYSTEM
376    TheISA::IntReg getSyscallArg(int i)
377    {
378        assert(i < TheISA::NumArgumentRegs);
379        return regs.readIntReg(TheISA::flattenIntIndex(getTC(),
380                    TheISA::ArgumentReg[i]));
381    }
382
383    // used to shift args for indirect syscall
384    void setSyscallArg(int i, TheISA::IntReg val)
385    {
386        assert(i < TheISA::NumArgumentRegs);
387        regs.setIntReg(TheISA::flattenIntIndex(getTC(),
388                    TheISA::ArgumentReg[i]), val);
389    }
390
391    void setSyscallReturn(SyscallReturn return_value)
392    {
393        TheISA::setSyscallReturn(return_value, getTC());
394    }
395
396    void syscall(int64_t callnum)
397    {
398        process->syscall(callnum, tc);
399    }
400#endif
401
402    void changeRegFileContext(TheISA::RegContextParam param,
403            TheISA::RegContextVal val)
404    {
405        regs.changeContext(param, val);
406    }
407};
408
409
410// for non-speculative execution context, spec_mode is always false
411inline bool
412SimpleThread::misspeculating()
413{
414    return false;
415}
416
417#endif // __CPU_CPU_EXEC_CONTEXT_HH__
418