pseudo_inst.cc revision 2235
1/*
2 * Copyright (c) 2003-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
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 "arch/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;
56using namespace TheISA;
57
58namespace AlphaPseudo
59{
60    bool doStatisticsInsts;
61    bool doCheckpointInsts;
62    bool doQuiesce;
63
64    void
65    arm(ExecContext *xc)
66    {
67        xc->getCpuPtr()->kernelStats->arm();
68    }
69
70    void
71    quiesce(ExecContext *xc)
72    {
73        if (!doQuiesce)
74            return;
75
76        xc->suspend();
77        xc->getCpuPtr()->kernelStats->quiesce();
78    }
79
80    void
81    quiesceNs(ExecContext *xc, uint64_t ns)
82    {
83        if (!doQuiesce || ns == 0)
84            return;
85
86        Event *quiesceEvent = xc->getQuiesceEvent();
87
88        if (quiesceEvent->scheduled())
89            quiesceEvent->reschedule(curTick + Clock::Int::ns * ns);
90        else
91            quiesceEvent->schedule(curTick + Clock::Int::ns * ns);
92
93        xc->suspend();
94        xc->getCpuPtr()->kernelStats->quiesce();
95    }
96
97    void
98    quiesceCycles(ExecContext *xc, uint64_t cycles)
99    {
100        if (!doQuiesce || cycles == 0)
101            return;
102
103        Event *quiesceEvent = xc->getQuiesceEvent();
104
105        if (quiesceEvent->scheduled())
106            quiesceEvent->reschedule(curTick +
107                                     xc->getCpuPtr()->cycles(cycles));
108        else
109            quiesceEvent->schedule(curTick +
110                                   xc->getCpuPtr()->cycles(cycles));
111
112        xc->suspend();
113        xc->getCpuPtr()->kernelStats->quiesce();
114    }
115
116    uint64_t
117    quiesceTime(ExecContext *xc)
118    {
119        return (xc->readLastActivate() - xc->readLastSuspend()) / Clock::Int::ns;
120    }
121
122    void
123    ivlb(ExecContext *xc)
124    {
125        xc->getCpuPtr()->kernelStats->ivlb();
126    }
127
128    void
129    ivle(ExecContext *xc)
130    {
131    }
132
133    void
134    m5exit_old(ExecContext *xc)
135    {
136        SimExit(curTick, "m5_exit_old instruction encountered");
137    }
138
139    void
140    m5exit(ExecContext *xc, Tick delay)
141    {
142        Tick when = curTick + delay * Clock::Int::ns;
143        SimExit(when, "m5_exit instruction encountered");
144    }
145
146    void
147    resetstats(ExecContext *xc, Tick delay, Tick period)
148    {
149        if (!doStatisticsInsts)
150            return;
151
152
153        Tick when = curTick + delay * Clock::Int::ns;
154        Tick repeat = period * Clock::Int::ns;
155
156        using namespace Stats;
157        SetupEvent(Reset, when, repeat);
158    }
159
160    void
161    dumpstats(ExecContext *xc, Tick delay, Tick period)
162    {
163        if (!doStatisticsInsts)
164            return;
165
166
167        Tick when = curTick + delay * Clock::Int::ns;
168        Tick repeat = period * Clock::Int::ns;
169
170        using namespace Stats;
171        SetupEvent(Dump, when, repeat);
172    }
173
174    void
175    addsymbol(ExecContext *xc, Addr addr, Addr symbolAddr)
176    {
177        char symb[100];
178        CopyString(xc, symb, symbolAddr, 100);
179        std::string symbol(symb);
180
181        DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
182
183        xc->getSystemPtr()->kernelSymtab->insert(addr,symbol);
184    }
185
186    void
187    dumpresetstats(ExecContext *xc, Tick delay, Tick period)
188    {
189        if (!doStatisticsInsts)
190            return;
191
192
193        Tick when = curTick + delay * Clock::Int::ns;
194        Tick repeat = period * Clock::Int::ns;
195
196        using namespace Stats;
197        SetupEvent(Dump|Reset, when, repeat);
198    }
199
200    void
201    m5checkpoint(ExecContext *xc, Tick delay, Tick period)
202    {
203        if (!doCheckpointInsts)
204            return;
205
206
207        Tick when = curTick + delay * Clock::Int::ns;
208        Tick repeat = period * Clock::Int::ns;
209
210        Checkpoint::setup(when, repeat);
211    }
212
213    uint64_t
214    readfile(ExecContext *xc, Addr vaddr, uint64_t len, uint64_t offset)
215    {
216        const string &file = xc->getCpuPtr()->system->params()->readfile;
217        if (file.empty()) {
218            return ULL(0);
219        }
220
221        uint64_t result = 0;
222
223        int fd = ::open(file.c_str(), O_RDONLY, 0);
224        if (fd < 0)
225            panic("could not open file %s\n", file);
226
227        if (::lseek(fd, offset, SEEK_SET) < 0)
228            panic("could not seek: %s", strerror(errno));
229
230        char *buf = new char[len];
231        char *p = buf;
232        while (len > 0) {
233            int bytes = ::read(fd, p, len);
234            if (bytes <= 0)
235                break;
236
237            p += bytes;
238            result += bytes;
239            len -= bytes;
240        }
241
242        close(fd);
243        CopyIn(xc, vaddr, buf, result);
244        delete [] buf;
245        return result;
246    }
247
248    class Context : public ParamContext
249    {
250      public:
251        Context(const string &section) : ParamContext(section) {}
252        void checkParams();
253    };
254
255    Context context("pseudo_inst");
256
257    Param<bool> __quiesce(&context, "quiesce",
258                          "enable quiesce instructions",
259                          true);
260    Param<bool> __statistics(&context, "statistics",
261                             "enable statistics pseudo instructions",
262                             true);
263    Param<bool> __checkpoint(&context, "checkpoint",
264                             "enable checkpoint pseudo instructions",
265                             true);
266
267    void
268    Context::checkParams()
269    {
270        doQuiesce = __quiesce;
271        doStatisticsInsts = __statistics;
272        doCheckpointInsts = __checkpoint;
273    }
274
275    void debugbreak(ExecContext *xc)
276    {
277        debug_break();
278    }
279
280    void switchcpu(ExecContext *xc)
281    {
282        if (SampCPU)
283            SampCPU->switchCPUs();
284    }
285}
286