base.hh revision 1400
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 width;
73
74        TickEvent(SimpleCPU *c, int w);
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  private:
98    Trace::InstRecord *traceData;
99
100  public:
101    //
102    enum Status {
103        Running,
104        Idle,
105        IcacheMissStall,
106        IcacheMissComplete,
107        DcacheMissStall,
108        SwitchedOut
109    };
110
111  private:
112    Status _status;
113
114  public:
115    void post_interrupt(int int_num, int index);
116
117    void zero_fill_64(Addr addr) {
118      static int warned = 0;
119      if (!warned) {
120        warn ("WH64 is not implemented");
121        warned = 1;
122      }
123    };
124
125  public:
126    struct Params : public BaseCPU::Params
127    {
128        MemInterface *icache_interface;
129        MemInterface *dcache_interface;
130        int width;
131#ifdef FULL_SYSTEM
132        AlphaITB *itb;
133        AlphaDTB *dtb;
134        FunctionalMemory *mem;
135#else
136        Process *process;
137#endif
138    };
139    SimpleCPU(Params *params);
140    virtual ~SimpleCPU();
141
142  public:
143    // execution context
144    ExecContext *xc;
145
146    void switchOut();
147    void takeOverFrom(BaseCPU *oldCPU);
148
149#ifdef FULL_SYSTEM
150    Addr dbg_vtophys(Addr addr);
151
152    bool interval_stats;
153#endif
154
155    // L1 instruction cache
156    MemInterface *icacheInterface;
157
158    // L1 data cache
159    MemInterface *dcacheInterface;
160
161    // current instruction
162    MachInst inst;
163
164    // Refcounted pointer to the one memory request.
165    MemReqPtr memReq;
166
167    class CacheCompletionEvent : public Event
168    {
169      private:
170        SimpleCPU *cpu;
171
172      public:
173        CacheCompletionEvent(SimpleCPU *_cpu);
174
175        virtual void process();
176        virtual const char *description();
177    };
178
179    CacheCompletionEvent cacheCompletionEvent;
180
181    Status status() const { return _status; }
182
183    virtual void activateContext(int thread_num, int delay);
184    virtual void suspendContext(int thread_num);
185    virtual void deallocateContext(int thread_num);
186    virtual void haltContext(int thread_num);
187
188    // statistics
189    virtual void regStats();
190    virtual void resetStats();
191
192    // number of simulated instructions
193    Counter numInst;
194    Counter startNumInst;
195    Stats::Scalar<> numInsts;
196
197    virtual Counter totalInstructions() const
198    {
199        return numInst - startNumInst;
200    }
201
202    // number of simulated memory references
203    Stats::Scalar<> numMemRefs;
204
205    // number of simulated loads
206    Counter numLoad;
207    Counter startNumLoad;
208
209    // number of idle cycles
210    Stats::Average<> notIdleFraction;
211    Stats::Formula idleFraction;
212
213    // number of cycles stalled for I-cache misses
214    Stats::Scalar<> icacheStallCycles;
215    Counter lastIcacheStall;
216
217    // number of cycles stalled for D-cache misses
218    Stats::Scalar<> dcacheStallCycles;
219    Counter lastDcacheStall;
220
221    void processCacheCompletion();
222
223    virtual void serialize(std::ostream &os);
224    virtual void unserialize(Checkpoint *cp, const std::string &section);
225
226    template <class T>
227    Fault read(Addr addr, T &data, unsigned flags);
228
229    template <class T>
230    Fault write(T data, Addr addr, unsigned flags, uint64_t *res);
231
232    // These functions are only used in CPU models that split
233    // effective address computation from the actual memory access.
234    void setEA(Addr EA) { panic("SimpleCPU::setEA() not implemented\n"); }
235    Addr getEA() 	{ panic("SimpleCPU::getEA() not implemented\n"); }
236
237    void prefetch(Addr addr, unsigned flags)
238    {
239        // need to do this...
240    }
241
242    void writeHint(Addr addr, int size, unsigned flags)
243    {
244        // need to do this...
245    }
246
247    Fault copySrcTranslate(Addr src);
248
249    Fault copy(Addr dest);
250
251    // The register accessor methods provide the index of the
252    // instruction's operand (e.g., 0 or 1), not the architectural
253    // register index, to simplify the implementation of register
254    // renaming.  We find the architectural register index by indexing
255    // into the instruction's own operand index table.  Note that a
256    // raw pointer to the StaticInst is provided instead of a
257    // ref-counted StaticInstPtr to redice overhead.  This is fine as
258    // long as these methods don't copy the pointer into any long-term
259    // storage (which is pretty hard to imagine they would have reason
260    // to do).
261
262    uint64_t readIntReg(StaticInst<TheISA> *si, int idx)
263    {
264        return xc->readIntReg(si->srcRegIdx(idx));
265    }
266
267    float readFloatRegSingle(StaticInst<TheISA> *si, int idx)
268    {
269        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
270        return xc->readFloatRegSingle(reg_idx);
271    }
272
273    double readFloatRegDouble(StaticInst<TheISA> *si, int idx)
274    {
275        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
276        return xc->readFloatRegDouble(reg_idx);
277    }
278
279    uint64_t readFloatRegInt(StaticInst<TheISA> *si, int idx)
280    {
281        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
282        return xc->readFloatRegInt(reg_idx);
283    }
284
285    void setIntReg(StaticInst<TheISA> *si, int idx, uint64_t val)
286    {
287        xc->setIntReg(si->destRegIdx(idx), val);
288    }
289
290    void setFloatRegSingle(StaticInst<TheISA> *si, int idx, float val)
291    {
292        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
293        xc->setFloatRegSingle(reg_idx, val);
294    }
295
296    void setFloatRegDouble(StaticInst<TheISA> *si, int idx, double val)
297    {
298        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
299        xc->setFloatRegDouble(reg_idx, val);
300    }
301
302    void setFloatRegInt(StaticInst<TheISA> *si, int idx, uint64_t val)
303    {
304        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
305        xc->setFloatRegInt(reg_idx, val);
306    }
307
308    uint64_t readPC() { return xc->readPC(); }
309    void setNextPC(uint64_t val) { xc->setNextPC(val); }
310
311    uint64_t readUniq() { return xc->readUniq(); }
312    void setUniq(uint64_t val) { xc->setUniq(val); }
313
314    uint64_t readFpcr() { return xc->readFpcr(); }
315    void setFpcr(uint64_t val) { xc->setFpcr(val); }
316
317#ifdef FULL_SYSTEM
318    uint64_t readIpr(int idx, Fault &fault) { return xc->readIpr(idx, fault); }
319    Fault setIpr(int idx, uint64_t val) { return xc->setIpr(idx, val); }
320    Fault hwrei() { return xc->hwrei(); }
321    int readIntrFlag() { return xc->readIntrFlag(); }
322    void setIntrFlag(int val) { xc->setIntrFlag(val); }
323    bool inPalMode() { return xc->inPalMode(); }
324    void ev5_trap(Fault fault) { xc->ev5_trap(fault); }
325    bool simPalCheck(int palFunc) { return xc->simPalCheck(palFunc); }
326#else
327    void syscall() { xc->syscall(); }
328#endif
329
330    bool misspeculating() { return xc->misspeculating(); }
331    ExecContext *xcBase() { return xc; }
332};
333
334#endif // __CPU_SIMPLE_CPU_SIMPLE_CPU_HH__
335