pseudo_inst.cc revision 2081
1/*
2 * Copyright (c) 2003-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#include <errno.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <cstdio>
33
34#include <string>
35
36#include "sim/pseudo_inst.hh"
37#include "targetarch/vtophys.hh"
38#include "cpu/base.hh"
39#include "cpu/sampler/sampler.hh"
40#include "cpu/exec_context.hh"
41#include "kern/kernel_stats.hh"
42#include "sim/param.hh"
43#include "sim/serialize.hh"
44#include "sim/sim_exit.hh"
45#include "sim/stat_control.hh"
46#include "sim/stats.hh"
47#include "sim/system.hh"
48#include "sim/debug.hh"
49#include "sim/vptr.hh"
50
51using namespace std;
52
53extern Sampler *SampCPU;
54
55using namespace Stats;
56
57namespace AlphaPseudo
58{
59    bool doStatisticsInsts;
60    bool doCheckpointInsts;
61    bool doQuiesce;
62
63    void
64    arm(ExecContext *xc)
65    {
66        xc->kernelStats->arm();
67    }
68
69    void
70    quiesce(ExecContext *xc)
71    {
72        if (!doQuiesce)
73            return;
74
75        xc->suspend();
76        xc->kernelStats->quiesce();
77    }
78
79    void
80    ivlb(ExecContext *xc)
81    {
82        xc->kernelStats->ivlb();
83    }
84
85    void
86    ivle(ExecContext *xc)
87    {
88    }
89
90    void
91    m5exit_old(ExecContext *xc)
92    {
93        SimExit(curTick, "m5_exit_old instruction encountered");
94    }
95
96    void
97    m5exit(ExecContext *xc, Tick delay)
98    {
99        Tick when = curTick + delay * Clock::Int::ns;
100        SimExit(when, "m5_exit instruction encountered");
101    }
102
103    void
104    resetstats(ExecContext *xc, Tick delay, Tick period)
105    {
106        if (!doStatisticsInsts)
107            return;
108
109
110        Tick when = curTick + delay * Clock::Int::ns;
111        Tick repeat = period * Clock::Int::ns;
112
113        using namespace Stats;
114        SetupEvent(Reset, when, repeat);
115    }
116
117    void
118    dumpstats(ExecContext *xc, Tick delay, Tick period)
119    {
120        if (!doStatisticsInsts)
121            return;
122
123
124        Tick when = curTick + delay * Clock::Int::ns;
125        Tick repeat = period * Clock::Int::ns;
126
127        using namespace Stats;
128        SetupEvent(Dump, when, repeat);
129    }
130
131    void
132    addsymbol(ExecContext *xc, Addr addr, Addr symbolAddr)
133    {
134        char symb[100];
135        CopyString(xc, symb, symbolAddr, 100);
136        std::string symbol(symb);
137
138        DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
139
140        xc->system->kernelSymtab->insert(addr,symbol);
141    }
142
143    void
144    dumpresetstats(ExecContext *xc, Tick delay, Tick period)
145    {
146        if (!doStatisticsInsts)
147            return;
148
149
150        Tick when = curTick + delay * Clock::Int::ns;
151        Tick repeat = period * Clock::Int::ns;
152
153        using namespace Stats;
154        SetupEvent(Dump|Reset, when, repeat);
155    }
156
157    void
158    m5checkpoint(ExecContext *xc, Tick delay, Tick period)
159    {
160        if (!doCheckpointInsts)
161            return;
162
163
164        Tick when = curTick + delay * Clock::Int::ns;
165        Tick repeat = period * Clock::Int::ns;
166
167        Checkpoint::setup(when, repeat);
168    }
169
170    uint64_t
171    readfile(ExecContext *xc, Addr vaddr, uint64_t len, uint64_t offset)
172    {
173        const string &file = xc->cpu->system->params->readfile;
174        if (file.empty()) {
175            return ULL(0);
176        }
177
178        uint64_t result = 0;
179
180        int fd = ::open(file.c_str(), O_RDONLY, 0);
181        if (fd < 0)
182            panic("could not open file %s\n", file);
183
184        if (::lseek(fd, offset, SEEK_SET) < 0)
185            panic("could not seek: %s", strerror(errno));
186
187        char *buf = new char[len];
188        char *p = buf;
189        while (len > 0) {
190            int bytes = ::read(fd, p, len);
191            if (bytes <= 0)
192                break;
193
194            p += bytes;
195            result += bytes;
196            len -= bytes;
197        }
198
199        close(fd);
200        CopyIn(xc, vaddr, buf, result);
201        delete [] buf;
202        return result;
203    }
204
205    class Context : public ParamContext
206    {
207      public:
208        Context(const string &section) : ParamContext(section) {}
209        void checkParams();
210    };
211
212    Context context("pseudo_inst");
213
214    Param<bool> __quiesce(&context, "quiesce",
215                          "enable quiesce instructions",
216                          true);
217    Param<bool> __statistics(&context, "statistics",
218                             "enable statistics pseudo instructions",
219                             true);
220    Param<bool> __checkpoint(&context, "checkpoint",
221                             "enable checkpoint pseudo instructions",
222                             true);
223
224    void
225    Context::checkParams()
226    {
227        doQuiesce = __quiesce;
228        doStatisticsInsts = __statistics;
229        doCheckpointInsts = __checkpoint;
230    }
231
232    void debugbreak(ExecContext *xc)
233    {
234        debug_break();
235    }
236
237    void switchcpu(ExecContext *xc)
238    {
239        if (SampCPU)
240            SampCPU->switchCPUs();
241    }
242}
243