thread_context.hh revision 2159
1/*
2 * Copyright (c) 2001-2005 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
29#ifndef __CPU_EXEC_CONTEXT_HH__
30#define __CPU_EXEC_CONTEXT_HH__
31
32#include "config/full_system.hh"
33#include "mem/functional/functional.hh"
34#include "mem/mem_req.hh"
35#include "sim/host.hh"
36#include "sim/serialize.hh"
37#include "arch/isa_traits.hh"
38//#include "arch/isa_registers.hh"
39#include "sim/byteswap.hh"
40
41// forward declaration: see functional_memory.hh
42class FunctionalMemory;
43class PhysicalMemory;
44class BaseCPU;
45
46#if FULL_SYSTEM
47
48#include "sim/system.hh"
49#include "targetarch/alpha_memory.hh"
50
51class FunctionProfile;
52class ProfileNode;
53class MemoryController;
54namespace Kernel { class Binning; class Statistics; }
55
56#else // !FULL_SYSTEM
57
58#include "sim/process.hh"
59
60#endif // FULL_SYSTEM
61
62//
63// The ExecContext object represents a functional context for
64// instruction execution.  It incorporates everything required for
65// architecture-level functional simulation of a single thread.
66//
67
68class ExecContext
69{
70  protected:
71    typedef TheISA::RegFile RegFile;
72    typedef TheISA::MachInst MachInst;
73    typedef TheISA::MiscRegFile MiscRegFile;
74    typedef TheISA::MiscReg MiscReg;
75  public:
76    enum Status
77    {
78        /// Initialized but not running yet.  All CPUs start in
79        /// this state, but most transition to Active on cycle 1.
80        /// In MP or SMT systems, non-primary contexts will stay
81        /// in this state until a thread is assigned to them.
82        Unallocated,
83
84        /// Running.  Instructions should be executed only when
85        /// the context is in this state.
86        Active,
87
88        /// Temporarily inactive.  Entered while waiting for
89        /// initialization,synchronization, etc.
90        Suspended,
91
92        /// Permanently shut down.  Entered when target executes
93        /// m5exit pseudo-instruction.  When all contexts enter
94        /// this state, the simulation will terminate.
95        Halted
96    };
97
98  private:
99    Status _status;
100
101  public:
102    Status status() const { return _status; }
103
104    void setStatus(Status newStatus) { _status = newStatus; }
105
106    /// Set the status to Active.  Optional delay indicates number of
107    /// cycles to wait before beginning execution.
108    void activate(int delay = 1);
109
110    /// Set the status to Suspended.
111    void suspend();
112
113    /// Set the status to Unallocated.
114    void deallocate();
115
116    /// Set the status to Halted.
117    void halt();
118
119  public:
120    RegFile regs;	// correct-path register context
121
122    // pointer to CPU associated with this context
123    BaseCPU *cpu;
124
125    // Current instruction
126    MachInst inst;
127
128    // Index of hardware thread context on the CPU that this represents.
129    int thread_num;
130
131    // ID of this context w.r.t. the System or Process object to which
132    // it belongs.  For full-system mode, this is the system CPU ID.
133    int cpu_id;
134
135#if FULL_SYSTEM
136    FunctionalMemory *mem;
137    AlphaITB *itb;
138    AlphaDTB *dtb;
139    System *system;
140
141    // the following two fields are redundant, since we can always
142    // look them up through the system pointer, but we'll leave them
143    // here for now for convenience
144    MemoryController *memctrl;
145    PhysicalMemory *physmem;
146
147    Kernel::Binning *kernelBinning;
148    Kernel::Statistics *kernelStats;
149    bool bin;
150    bool fnbin;
151
152    FunctionProfile *profile;
153    ProfileNode *profileNode;
154    Addr profilePC;
155    void dumpFuncProfile();
156
157#else
158    Process *process;
159
160    FunctionalMemory *mem;	// functional storage for process address space
161
162    // Address space ID.  Note that this is used for TIMING cache
163    // simulation only; all functional memory accesses should use
164    // one of the FunctionalMemory pointers above.
165    short asid;
166
167#endif
168
169    /**
170     * Temporary storage to pass the source address from copy_load to
171     * copy_store.
172     * @todo Remove this temporary when we have a better way to do it.
173     */
174    Addr copySrcAddr;
175    /**
176     * Temp storage for the physical source address of a copy.
177     * @todo Remove this temporary when we have a better way to do it.
178     */
179    Addr copySrcPhysAddr;
180
181
182    /*
183     * number of executed instructions, for matching with syscall trace
184     * points in EIO files.
185     */
186    Counter func_exe_inst;
187
188    //
189    // Count failed store conditionals so we can warn of apparent
190    // application deadlock situations.
191    unsigned storeCondFailures;
192
193    // constructor: initialize context from given process structure
194#if FULL_SYSTEM
195    ExecContext(BaseCPU *_cpu, int _thread_num, System *_system,
196                AlphaITB *_itb, AlphaDTB *_dtb, FunctionalMemory *_dem);
197#else
198    ExecContext(BaseCPU *_cpu, int _thread_num, Process *_process, int _asid);
199    ExecContext(BaseCPU *_cpu, int _thread_num, FunctionalMemory *_mem,
200                int _asid);
201#endif
202    virtual ~ExecContext();
203
204    virtual void takeOverFrom(ExecContext *oldContext);
205
206    void regStats(const std::string &name);
207
208    void serialize(std::ostream &os);
209    void unserialize(Checkpoint *cp, const std::string &section);
210
211#if FULL_SYSTEM
212    bool validInstAddr(Addr addr) { return true; }
213    bool validDataAddr(Addr addr) { return true; }
214    int getInstAsid() { return regs.instAsid(); }
215    int getDataAsid() { return regs.dataAsid(); }
216
217    Fault translateInstReq(MemReqPtr &req)
218    {
219        return itb->translate(req);
220    }
221
222    Fault translateDataReadReq(MemReqPtr &req)
223    {
224        return dtb->translate(req, false);
225    }
226
227    Fault translateDataWriteReq(MemReqPtr &req)
228    {
229        return dtb->translate(req, true);
230    }
231
232#else
233    bool validInstAddr(Addr addr)
234    { return process->validInstAddr(addr); }
235
236    bool validDataAddr(Addr addr)
237    { return process->validDataAddr(addr); }
238
239    int getInstAsid() { return asid; }
240    int getDataAsid() { return asid; }
241
242    Fault dummyTranslation(MemReqPtr &req)
243    {
244#if 0
245        assert((req->vaddr >> 48 & 0xffff) == 0);
246#endif
247
248        // put the asid in the upper 16 bits of the paddr
249        req->paddr = req->vaddr & ~((Addr)0xffff << sizeof(Addr) * 8 - 16);
250        req->paddr = req->paddr | (Addr)req->asid << sizeof(Addr) * 8 - 16;
251        return NoFault;
252    }
253    Fault translateInstReq(MemReqPtr &req)
254    {
255        return dummyTranslation(req);
256    }
257    Fault translateDataReadReq(MemReqPtr &req)
258    {
259        return dummyTranslation(req);
260    }
261    Fault translateDataWriteReq(MemReqPtr &req)
262    {
263        return dummyTranslation(req);
264    }
265
266#endif
267
268    template <class T>
269    Fault read(MemReqPtr &req, T &data)
270    {
271#if FULL_SYSTEM && defined(TARGET_ALPHA)
272        if (req->flags & LOCKED) {
273            MiscRegFile *cregs = &req->xc->regs.miscRegs;
274            cregs->setReg(TheISA::Lock_Addr_DepTag, req->paddr);
275            cregs->setReg(TheISA::Lock_Flag_DepTag, true);
276        }
277#endif
278
279        Fault error;
280        error = mem->read(req, data);
281        data = LittleEndianGuest::gtoh(data);
282        return error;
283    }
284
285    template <class T>
286    Fault write(MemReqPtr &req, T &data)
287    {
288#if FULL_SYSTEM && defined(TARGET_ALPHA)
289
290        MiscRegFile *cregs;
291
292        // If this is a store conditional, act appropriately
293        if (req->flags & LOCKED) {
294            cregs = &req->xc->regs.miscRegs;
295
296            if (req->flags & UNCACHEABLE) {
297                // Don't update result register (see stq_c in isa_desc)
298                req->result = 2;
299                req->xc->storeCondFailures = 0;//Needed? [RGD]
300            } else {
301                bool lock_flag = cregs->readReg(TheISA::Lock_Flag_DepTag);
302                Addr lock_addr = cregs->readReg(TheISA::Lock_Addr_DepTag);
303                req->result = lock_flag;
304                if (!lock_flag ||
305                    ((lock_addr & ~0xf) != (req->paddr & ~0xf))) {
306                    cregs->setReg(TheISA::Lock_Flag_DepTag, false);
307                    if (((++req->xc->storeCondFailures) % 100000) == 0) {
308                        std::cerr << "Warning: "
309                                  << req->xc->storeCondFailures
310                                  << " consecutive store conditional failures "
311                                  << "on cpu " << req->xc->cpu_id
312                                  << std::endl;
313                    }
314                    return NoFault;
315                }
316                else req->xc->storeCondFailures = 0;
317            }
318        }
319
320        // Need to clear any locked flags on other proccessors for
321        // this address.  Only do this for succsful Store Conditionals
322        // and all other stores (WH64?).  Unsuccessful Store
323        // Conditionals would have returned above, and wouldn't fall
324        // through.
325        for (int i = 0; i < system->execContexts.size(); i++){
326            cregs = &system->execContexts[i]->regs.miscRegs;
327            if ((cregs->readReg(TheISA::Lock_Addr_DepTag) & ~0xf) ==
328                (req->paddr & ~0xf)) {
329                cregs->setReg(TheISA::Lock_Flag_DepTag, false);
330            }
331        }
332
333#endif
334        return mem->write(req, (T)LittleEndianGuest::htog(data));
335    }
336
337    virtual bool misspeculating();
338
339
340    MachInst getInst() { return inst; }
341
342    void setInst(MachInst new_inst)
343    {
344        inst = new_inst;
345    }
346
347    Fault instRead(MemReqPtr &req)
348    {
349        return mem->read(req, inst);
350    }
351
352    //
353    // New accessors for new decoder.
354    //
355    uint64_t readIntReg(int reg_idx)
356    {
357        return regs.intRegFile[reg_idx];
358    }
359
360    float readFloatRegSingle(int reg_idx)
361    {
362        return (float)regs.floatRegFile.d[reg_idx];
363    }
364
365    double readFloatRegDouble(int reg_idx)
366    {
367        return regs.floatRegFile.d[reg_idx];
368    }
369
370    uint64_t readFloatRegInt(int reg_idx)
371    {
372        return regs.floatRegFile.q[reg_idx];
373    }
374
375    void setIntReg(int reg_idx, uint64_t val)
376    {
377        regs.intRegFile[reg_idx] = val;
378    }
379
380    void setFloatRegSingle(int reg_idx, float val)
381    {
382        regs.floatRegFile.d[reg_idx] = (double)val;
383    }
384
385    void setFloatRegDouble(int reg_idx, double val)
386    {
387        regs.floatRegFile.d[reg_idx] = val;
388    }
389
390    void setFloatRegInt(int reg_idx, uint64_t val)
391    {
392        regs.floatRegFile.q[reg_idx] = val;
393    }
394
395    uint64_t readPC()
396    {
397        return regs.pc;
398    }
399
400    void setNextPC(uint64_t val)
401    {
402        regs.npc = val;
403    }
404
405    MiscReg readMiscReg(int misc_reg)
406    {
407        return regs.miscRegs.readReg(misc_reg);
408    }
409
410    MiscReg readMiscRegWithEffect(int misc_reg, Fault &fault)
411    {
412        return regs.miscRegs.readRegWithEffect(misc_reg, fault, this);
413    }
414
415    Fault setMiscReg(int misc_reg, const MiscReg &val)
416    {
417        return regs.miscRegs.setReg(misc_reg, val);
418    }
419
420    Fault setMiscRegWithEffect(int misc_reg, const MiscReg &val)
421    {
422        return regs.miscRegs.setRegWithEffect(misc_reg, val, this);
423    }
424
425#if FULL_SYSTEM
426    int readIntrFlag() { return regs.intrflag; }
427    void setIntrFlag(int val) { regs.intrflag = val; }
428    Fault hwrei();
429    bool inPalMode() { return AlphaISA::PcPAL(regs.pc); }
430    void ev5_trap(Fault fault);
431    bool simPalCheck(int palFunc);
432#endif
433
434    /** Meant to be more generic trap function to be
435     *  called when an instruction faults.
436     *  @param fault The fault generated by executing the instruction.
437     *  @todo How to do this properly so it's dependent upon ISA only?
438     */
439
440    void trap(Fault fault);
441
442#if !FULL_SYSTEM
443    TheISA::IntReg getSyscallArg(int i)
444    {
445        return regs.intRegFile[TheISA::ArgumentReg0 + i];
446    }
447
448    // used to shift args for indirect syscall
449    void setSyscallArg(int i, TheISA::IntReg val)
450    {
451        regs.intRegFile[TheISA::ArgumentReg0 + i] = val;
452    }
453
454    void setSyscallReturn(SyscallReturn return_value)
455    {
456        // check for error condition.  Alpha syscall convention is to
457        // indicate success/failure in reg a3 (r19) and put the
458        // return value itself in the standard return value reg (v0).
459        const int RegA3 = 19;	// only place this is used
460        if (return_value.successful()) {
461            // no error
462            regs.intRegFile[RegA3] = 0;
463            regs.intRegFile[TheISA::ReturnValueReg] = return_value.value();
464        } else {
465            // got an error, return details
466            regs.intRegFile[RegA3] = (TheISA::IntReg) -1;
467            regs.intRegFile[TheISA::ReturnValueReg] = -return_value.value();
468        }
469    }
470
471    void syscall()
472    {
473        process->syscall(this);
474    }
475#endif
476};
477
478
479// for non-speculative execution context, spec_mode is always false
480inline bool
481ExecContext::misspeculating()
482{
483    return false;
484}
485
486#endif // __CPU_EXEC_CONTEXT_HH__
487