base.hh revision 1371
1/*
2 * Copyright (c) 2002-2004 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_SIMPLE_CPU_SIMPLE_CPU_HH__
30#define __CPU_SIMPLE_CPU_SIMPLE_CPU_HH__
31
32#include "base/statistics.hh"
33#include "cpu/base_cpu.hh"
34#include "cpu/exec_context.hh"
35#include "cpu/pc_event.hh"
36#include "cpu/static_inst.hh"
37#include "sim/eventq.hh"
38
39// forward declarations
40#ifdef FULL_SYSTEM
41class Processor;
42class AlphaITB;
43class AlphaDTB;
44class PhysicalMemory;
45
46class RemoteGDB;
47class GDBListener;
48
49#else
50
51class Process;
52
53#endif // FULL_SYSTEM
54
55class MemInterface;
56class Checkpoint;
57
58namespace Trace {
59    class InstRecord;
60}
61
62class SimpleCPU : public BaseCPU
63{
64  public:
65    // main simulation loop (one cycle)
66    void tick();
67
68  private:
69    struct TickEvent : public Event
70    {
71        SimpleCPU *cpu;
72        int multiplier;
73
74        TickEvent(SimpleCPU *c);
75        void process();
76        const char *description();
77    };
78
79    TickEvent tickEvent;
80
81    /// Schedule tick event, regardless of its current state.
82    void scheduleTickEvent(int delay)
83    {
84        if (tickEvent.squashed())
85            tickEvent.reschedule(curTick + delay);
86        else if (!tickEvent.scheduled())
87            tickEvent.schedule(curTick + delay);
88    }
89
90    /// Unschedule tick event, regardless of its current state.
91    void unscheduleTickEvent()
92    {
93        if (tickEvent.scheduled())
94            tickEvent.squash();
95    }
96
97  public:
98    void setTickMultiplier(int multiplier)
99    {
100        tickEvent.multiplier = multiplier;
101    }
102
103  private:
104    Trace::InstRecord *traceData;
105
106  public:
107    //
108    enum Status {
109        Running,
110        Idle,
111        IcacheMissStall,
112        IcacheMissComplete,
113        DcacheMissStall,
114        SwitchedOut
115    };
116
117  private:
118    Status _status;
119
120  public:
121    void post_interrupt(int int_num, int index);
122
123    void zero_fill_64(Addr addr) {
124      static int warned = 0;
125      if (!warned) {
126        warn ("WH64 is not implemented");
127        warned = 1;
128      }
129    };
130
131#ifdef FULL_SYSTEM
132
133    SimpleCPU(const std::string &_name,
134              System *_system,
135              Counter max_insts_any_thread, Counter max_insts_all_threads,
136              Counter max_loads_any_thread, Counter max_loads_all_threads,
137              AlphaITB *itb, AlphaDTB *dtb, FunctionalMemory *mem,
138              MemInterface *icache_interface, MemInterface *dcache_interface,
139              bool _def_reg, Tick freq,
140              bool _function_trace, Tick _function_trace_start);
141
142#else
143
144    SimpleCPU(const std::string &_name, Process *_process,
145              Counter max_insts_any_thread,
146              Counter max_insts_all_threads,
147              Counter max_loads_any_thread,
148              Counter max_loads_all_threads,
149              MemInterface *icache_interface, MemInterface *dcache_interface,
150              bool _def_reg,
151              bool _function_trace, Tick _function_trace_start);
152
153#endif
154
155    virtual ~SimpleCPU();
156
157    // execution context
158    ExecContext *xc;
159
160    void switchOut();
161    void takeOverFrom(BaseCPU *oldCPU);
162
163#ifdef FULL_SYSTEM
164    Addr dbg_vtophys(Addr addr);
165
166    bool interval_stats;
167#endif
168
169    // L1 instruction cache
170    MemInterface *icacheInterface;
171
172    // L1 data cache
173    MemInterface *dcacheInterface;
174
175    // current instruction
176    MachInst inst;
177
178    // Refcounted pointer to the one memory request.
179    MemReqPtr memReq;
180
181    class CacheCompletionEvent : public Event
182    {
183      private:
184        SimpleCPU *cpu;
185
186      public:
187        CacheCompletionEvent(SimpleCPU *_cpu);
188
189        virtual void process();
190        virtual const char *description();
191    };
192
193    CacheCompletionEvent cacheCompletionEvent;
194
195    Status status() const { return _status; }
196
197    virtual void activateContext(int thread_num, int delay);
198    virtual void suspendContext(int thread_num);
199    virtual void deallocateContext(int thread_num);
200    virtual void haltContext(int thread_num);
201
202    // statistics
203    virtual void regStats();
204    virtual void resetStats();
205
206    // number of simulated instructions
207    Counter numInst;
208    Counter startNumInst;
209    Stats::Scalar<> numInsts;
210
211    virtual Counter totalInstructions() const
212    {
213        return numInst - startNumInst;
214    }
215
216    // number of simulated memory references
217    Stats::Scalar<> numMemRefs;
218
219    // number of simulated loads
220    Counter numLoad;
221    Counter startNumLoad;
222
223    // number of idle cycles
224    Stats::Average<> notIdleFraction;
225    Stats::Formula idleFraction;
226
227    // number of cycles stalled for I-cache misses
228    Stats::Scalar<> icacheStallCycles;
229    Counter lastIcacheStall;
230
231    // number of cycles stalled for D-cache misses
232    Stats::Scalar<> dcacheStallCycles;
233    Counter lastDcacheStall;
234
235    void processCacheCompletion();
236
237    virtual void serialize(std::ostream &os);
238    virtual void unserialize(Checkpoint *cp, const std::string &section);
239
240    template <class T>
241    Fault read(Addr addr, T &data, unsigned flags);
242
243    template <class T>
244    Fault write(T data, Addr addr, unsigned flags, uint64_t *res);
245
246    // These functions are only used in CPU models that split
247    // effective address computation from the actual memory access.
248    void setEA(Addr EA) { panic("SimpleCPU::setEA() not implemented\n"); }
249    Addr getEA() 	{ panic("SimpleCPU::getEA() not implemented\n"); }
250
251    void prefetch(Addr addr, unsigned flags)
252    {
253        // need to do this...
254    }
255
256    void writeHint(Addr addr, int size, unsigned flags)
257    {
258        // need to do this...
259    }
260
261    Fault copySrcTranslate(Addr src);
262
263    Fault copy(Addr dest);
264
265    // The register accessor methods provide the index of the
266    // instruction's operand (e.g., 0 or 1), not the architectural
267    // register index, to simplify the implementation of register
268    // renaming.  We find the architectural register index by indexing
269    // into the instruction's own operand index table.  Note that a
270    // raw pointer to the StaticInst is provided instead of a
271    // ref-counted StaticInstPtr to redice overhead.  This is fine as
272    // long as these methods don't copy the pointer into any long-term
273    // storage (which is pretty hard to imagine they would have reason
274    // to do).
275
276    uint64_t readIntReg(StaticInst<TheISA> *si, int idx)
277    {
278        return xc->readIntReg(si->srcRegIdx(idx));
279    }
280
281    float readFloatRegSingle(StaticInst<TheISA> *si, int idx)
282    {
283        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
284        return xc->readFloatRegSingle(reg_idx);
285    }
286
287    double readFloatRegDouble(StaticInst<TheISA> *si, int idx)
288    {
289        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
290        return xc->readFloatRegDouble(reg_idx);
291    }
292
293    uint64_t readFloatRegInt(StaticInst<TheISA> *si, int idx)
294    {
295        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
296        return xc->readFloatRegInt(reg_idx);
297    }
298
299    void setIntReg(StaticInst<TheISA> *si, int idx, uint64_t val)
300    {
301        xc->setIntReg(si->destRegIdx(idx), val);
302    }
303
304    void setFloatRegSingle(StaticInst<TheISA> *si, int idx, float val)
305    {
306        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
307        xc->setFloatRegSingle(reg_idx, val);
308    }
309
310    void setFloatRegDouble(StaticInst<TheISA> *si, int idx, double val)
311    {
312        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
313        xc->setFloatRegDouble(reg_idx, val);
314    }
315
316    void setFloatRegInt(StaticInst<TheISA> *si, int idx, uint64_t val)
317    {
318        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
319        xc->setFloatRegInt(reg_idx, val);
320    }
321
322    uint64_t readPC() { return xc->readPC(); }
323    void setNextPC(uint64_t val) { xc->setNextPC(val); }
324
325    uint64_t readUniq() { return xc->readUniq(); }
326    void setUniq(uint64_t val) { xc->setUniq(val); }
327
328    uint64_t readFpcr() { return xc->readFpcr(); }
329    void setFpcr(uint64_t val) { xc->setFpcr(val); }
330
331#ifdef FULL_SYSTEM
332    uint64_t readIpr(int idx, Fault &fault) { return xc->readIpr(idx, fault); }
333    Fault setIpr(int idx, uint64_t val) { return xc->setIpr(idx, val); }
334    Fault hwrei() { return xc->hwrei(); }
335    int readIntrFlag() { return xc->readIntrFlag(); }
336    void setIntrFlag(int val) { xc->setIntrFlag(val); }
337    bool inPalMode() { return xc->inPalMode(); }
338    void ev5_trap(Fault fault) { xc->ev5_trap(fault); }
339    bool simPalCheck(int palFunc) { return xc->simPalCheck(palFunc); }
340#else
341    void syscall() { xc->syscall(); }
342#endif
343
344    bool misspeculating() { return xc->misspeculating(); }
345    ExecContext *xcBase() { return xc; }
346};
347
348#endif // __CPU_SIMPLE_CPU_SIMPLE_CPU_HH__
349