cpu.hh revision 6022
1/*
2 * Copyright (c) 2006 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 * Authors: Kevin Lim
29 */
30
31#ifndef __CPU_CHECKER_CPU_HH__
32#define __CPU_CHECKER_CPU_HH__
33
34#include <list>
35#include <queue>
36#include <map>
37
38#include "arch/types.hh"
39#include "base/statistics.hh"
40#include "config/full_system.hh"
41#include "cpu/base.hh"
42#include "cpu/base_dyn_inst.hh"
43#include "cpu/simple_thread.hh"
44#include "cpu/pc_event.hh"
45#include "cpu/static_inst.hh"
46#include "sim/eventq.hh"
47
48// forward declarations
49#if FULL_SYSTEM
50namespace TheISA
51{
52    class TLB;
53}
54class Processor;
55class PhysicalMemory;
56
57class RemoteGDB;
58class GDBListener;
59
60#else
61
62class Process;
63
64#endif // FULL_SYSTEM
65template <class>
66class BaseDynInst;
67class CheckerCPUParams;
68class ThreadContext;
69class MemInterface;
70class Checkpoint;
71class Request;
72
73/**
74 * CheckerCPU class.  Dynamically verifies instructions as they are
75 * completed by making sure that the instruction and its results match
76 * the independent execution of the benchmark inside the checker.  The
77 * checker verifies instructions in order, regardless of the order in
78 * which instructions complete.  There are certain results that can
79 * not be verified, specifically the result of a store conditional or
80 * the values of uncached accesses.  In these cases, and with
81 * instructions marked as "IsUnverifiable", the checker assumes that
82 * the value from the main CPU's execution is correct and simply
83 * copies that value.  It provides a CheckerThreadContext (see
84 * checker/thread_context.hh) that provides hooks for updating the
85 * Checker's state through any ThreadContext accesses.  This allows the
86 * checker to be able to correctly verify instructions, even with
87 * external accesses to the ThreadContext that change state.
88 */
89class CheckerCPU : public BaseCPU
90{
91  protected:
92    typedef TheISA::MachInst MachInst;
93    typedef TheISA::FloatReg FloatReg;
94    typedef TheISA::FloatRegBits FloatRegBits;
95    typedef TheISA::MiscReg MiscReg;
96  public:
97    virtual void init();
98
99  public:
100    typedef CheckerCPUParams Params;
101    const Params *params() const
102    { return reinterpret_cast<const Params *>(_params); }
103    CheckerCPU(Params *p);
104    virtual ~CheckerCPU();
105
106    Process *process;
107
108    void setSystem(System *system);
109
110    System *systemPtr;
111
112    void setIcachePort(Port *icache_port);
113
114    Port *icachePort;
115
116    void setDcachePort(Port *dcache_port);
117
118    Port *dcachePort;
119
120    virtual Port *getPort(const std::string &name, int idx)
121    {
122        panic("Not supported on checker!");
123        return NULL;
124    }
125
126  public:
127    // Primary thread being run.
128    SimpleThread *thread;
129
130    ThreadContext *tc;
131
132    TheISA::TLB *itb;
133    TheISA::TLB *dtb;
134
135#if FULL_SYSTEM
136    Addr dbg_vtophys(Addr addr);
137#endif
138
139    union Result {
140        uint64_t integer;
141//        float fp;
142        double dbl;
143    };
144
145    Result result;
146
147    // current instruction
148    MachInst machInst;
149
150    // Pointer to the one memory request.
151    RequestPtr memReq;
152
153    StaticInstPtr curStaticInst;
154
155    // number of simulated instructions
156    Counter numInst;
157    Counter startNumInst;
158
159    std::queue<int> miscRegIdxs;
160
161    virtual Counter totalInstructions() const
162    {
163        return 0;
164    }
165
166    // number of simulated loads
167    Counter numLoad;
168    Counter startNumLoad;
169
170    virtual void serialize(std::ostream &os);
171    virtual void unserialize(Checkpoint *cp, const std::string &section);
172
173    template <class T>
174    Fault read(Addr addr, T &data, unsigned flags);
175
176    template <class T>
177    Fault write(T data, Addr addr, unsigned flags, uint64_t *res);
178
179    // These functions are only used in CPU models that split
180    // effective address computation from the actual memory access.
181    void setEA(Addr EA) { panic("SimpleCPU::setEA() not implemented\n"); }
182    Addr getEA()        { panic("SimpleCPU::getEA() not implemented\n"); }
183
184    void prefetch(Addr addr, unsigned flags)
185    {
186        // need to do this...
187    }
188
189    void writeHint(Addr addr, int size, unsigned flags)
190    {
191        // need to do this...
192    }
193
194    Fault copySrcTranslate(Addr src);
195
196    Fault copy(Addr dest);
197
198    // The register accessor methods provide the index of the
199    // instruction's operand (e.g., 0 or 1), not the architectural
200    // register index, to simplify the implementation of register
201    // renaming.  We find the architectural register index by indexing
202    // into the instruction's own operand index table.  Note that a
203    // raw pointer to the StaticInst is provided instead of a
204    // ref-counted StaticInstPtr to redice overhead.  This is fine as
205    // long as these methods don't copy the pointer into any long-term
206    // storage (which is pretty hard to imagine they would have reason
207    // to do).
208
209    uint64_t readIntRegOperand(const StaticInst *si, int idx)
210    {
211        return thread->readIntReg(si->srcRegIdx(idx));
212    }
213
214    FloatReg readFloatRegOperand(const StaticInst *si, int idx, int width)
215    {
216        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
217        return thread->readFloatReg(reg_idx, width);
218    }
219
220    FloatReg readFloatRegOperand(const StaticInst *si, int idx)
221    {
222        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
223        return thread->readFloatReg(reg_idx);
224    }
225
226    FloatRegBits readFloatRegOperandBits(const StaticInst *si, int idx,
227                                         int width)
228    {
229        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
230        return thread->readFloatRegBits(reg_idx, width);
231    }
232
233    FloatRegBits readFloatRegOperandBits(const StaticInst *si, int idx)
234    {
235        int reg_idx = si->srcRegIdx(idx) - TheISA::FP_Base_DepTag;
236        return thread->readFloatRegBits(reg_idx);
237    }
238
239    void setIntRegOperand(const StaticInst *si, int idx, uint64_t val)
240    {
241        thread->setIntReg(si->destRegIdx(idx), val);
242        result.integer = val;
243    }
244
245    void setFloatRegOperand(const StaticInst *si, int idx, FloatReg val,
246                            int width)
247    {
248        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
249        thread->setFloatReg(reg_idx, val, width);
250        switch(width) {
251          case 32:
252            result.dbl = (double)val;
253            break;
254          case 64:
255            result.dbl = val;
256            break;
257        };
258    }
259
260    void setFloatRegOperand(const StaticInst *si, int idx, FloatReg val)
261    {
262        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
263        thread->setFloatReg(reg_idx, val);
264        result.dbl = (double)val;
265    }
266
267    void setFloatRegOperandBits(const StaticInst *si, int idx,
268                                FloatRegBits val, int width)
269    {
270        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
271        thread->setFloatRegBits(reg_idx, val, width);
272        result.integer = val;
273    }
274
275    void setFloatRegOperandBits(const StaticInst *si, int idx,
276                                FloatRegBits val)
277    {
278        int reg_idx = si->destRegIdx(idx) - TheISA::FP_Base_DepTag;
279        thread->setFloatRegBits(reg_idx, val);
280        result.integer = val;
281    }
282
283    uint64_t readPC() { return thread->readPC(); }
284
285    uint64_t readNextPC() { return thread->readNextPC(); }
286
287    void setNextPC(uint64_t val) {
288        thread->setNextPC(val);
289    }
290
291    MiscReg readMiscRegNoEffect(int misc_reg)
292    {
293        return thread->readMiscRegNoEffect(misc_reg);
294    }
295
296    MiscReg readMiscReg(int misc_reg)
297    {
298        return thread->readMiscReg(misc_reg);
299    }
300
301    void setMiscRegNoEffect(int misc_reg, const MiscReg &val)
302    {
303        result.integer = val;
304        miscRegIdxs.push(misc_reg);
305        return thread->setMiscRegNoEffect(misc_reg, val);
306    }
307
308    void setMiscReg(int misc_reg, const MiscReg &val)
309    {
310        miscRegIdxs.push(misc_reg);
311        return thread->setMiscReg(misc_reg, val);
312    }
313
314    void recordPCChange(uint64_t val) { changedPC = true; newPC = val; }
315    void recordNextPCChange(uint64_t val) { changedNextPC = true; }
316
317    void demapPage(Addr vaddr, uint64_t asn)
318    {
319        this->itb->demapPage(vaddr, asn);
320        this->dtb->demapPage(vaddr, asn);
321    }
322
323    void demapInstPage(Addr vaddr, uint64_t asn)
324    {
325        this->itb->demapPage(vaddr, asn);
326    }
327
328    void demapDataPage(Addr vaddr, uint64_t asn)
329    {
330        this->dtb->demapPage(vaddr, asn);
331    }
332
333#if FULL_SYSTEM
334    Fault hwrei() { return thread->hwrei(); }
335    void ev5_trap(Fault fault) { fault->invoke(tc); }
336    bool simPalCheck(int palFunc) { return thread->simPalCheck(palFunc); }
337#else
338    // Assume that the normal CPU's call to syscall was successful.
339    // The checker's state would have already been updated by the syscall.
340    void syscall(uint64_t callnum) { }
341#endif
342
343    void handleError()
344    {
345        if (exitOnError)
346            dumpAndExit();
347    }
348
349    bool checkFlags(Request *req);
350
351    void dumpAndExit();
352
353    ThreadContext *tcBase() { return tc; }
354    SimpleThread *threadBase() { return thread; }
355
356    Result unverifiedResult;
357    Request *unverifiedReq;
358    uint8_t *unverifiedMemData;
359
360    bool changedPC;
361    bool willChangePC;
362    uint64_t newPC;
363    bool changedNextPC;
364    bool exitOnError;
365    bool updateOnError;
366    bool warnOnlyOnLoadError;
367
368    InstSeqNum youngestSN;
369};
370
371/**
372 * Templated Checker class.  This Checker class is templated on the
373 * DynInstPtr of the instruction type that will be verified.  Proper
374 * template instantiations of the Checker must be placed at the bottom
375 * of checker/cpu.cc.
376 */
377template <class DynInstPtr>
378class Checker : public CheckerCPU
379{
380  public:
381    Checker(Params *p)
382        : CheckerCPU(p), updateThisCycle(false), unverifiedInst(NULL)
383    { }
384
385    void switchOut();
386    void takeOverFrom(BaseCPU *oldCPU);
387
388    void verify(DynInstPtr &inst);
389
390    void validateInst(DynInstPtr &inst);
391    void validateExecution(DynInstPtr &inst);
392    void validateState();
393
394    void copyResult(DynInstPtr &inst);
395
396  private:
397    void handleError(DynInstPtr &inst)
398    {
399        if (exitOnError) {
400            dumpAndExit(inst);
401        } else if (updateOnError) {
402            updateThisCycle = true;
403        }
404    }
405
406    void dumpAndExit(DynInstPtr &inst);
407
408    bool updateThisCycle;
409
410    DynInstPtr unverifiedInst;
411
412    std::list<DynInstPtr> instList;
413    typedef typename std::list<DynInstPtr>::iterator InstListIt;
414    void dumpInsts();
415};
416
417#endif // __CPU_CHECKER_CPU_HH__
418