pseudo_inst.cc revision 3617
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 * Authors: Nathan Binkert
29 */
30
31#include <errno.h>
32#include <fcntl.h>
33#include <unistd.h>
34
35#include <string>
36
37#include "arch/vtophys.hh"
38#include "base/annotate.hh"
39#include "cpu/base.hh"
40#include "cpu/thread_context.hh"
41#include "cpu/quiesce_event.hh"
42#include "arch/kernel_stats.hh"
43#include "sim/pseudo_inst.hh"
44#include "sim/serialize.hh"
45#include "sim/sim_exit.hh"
46#include "sim/stat_control.hh"
47#include "sim/stats.hh"
48#include "sim/system.hh"
49#include "sim/debug.hh"
50#include "sim/vptr.hh"
51
52using namespace std;
53
54using namespace Stats;
55using namespace TheISA;
56
57namespace AlphaPseudo
58{
59    void
60    arm(ThreadContext *tc)
61    {
62        if (tc->getKernelStats())
63            tc->getKernelStats()->arm();
64    }
65
66    void
67    quiesce(ThreadContext *tc)
68    {
69        if (!tc->getCpuPtr()->params->do_quiesce)
70            return;
71
72        DPRINTF(Quiesce, "%s: quiesce()\n", tc->getCpuPtr()->name());
73
74        tc->suspend();
75        if (tc->getKernelStats())
76            tc->getKernelStats()->quiesce();
77    }
78
79    void
80    quiesceNs(ThreadContext *tc, uint64_t ns)
81    {
82        if (!tc->getCpuPtr()->params->do_quiesce || ns == 0)
83            return;
84
85        EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
86
87        Tick resume = curTick + Clock::Int::ns * ns;
88
89        if (quiesceEvent->scheduled())
90            quiesceEvent->reschedule(resume);
91        else
92            quiesceEvent->schedule(resume);
93
94        DPRINTF(Quiesce, "%s: quiesceNs(%d) until %d\n",
95                tc->getCpuPtr()->name(), ns, resume);
96
97        tc->suspend();
98        if (tc->getKernelStats())
99            tc->getKernelStats()->quiesce();
100    }
101
102    void
103    quiesceCycles(ThreadContext *tc, uint64_t cycles)
104    {
105        if (!tc->getCpuPtr()->params->do_quiesce || cycles == 0)
106            return;
107
108        EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
109
110        Tick resume = curTick + tc->getCpuPtr()->cycles(cycles);
111
112        if (quiesceEvent->scheduled())
113            quiesceEvent->reschedule(resume);
114        else
115            quiesceEvent->schedule(resume);
116
117        DPRINTF(Quiesce, "%s: quiesceCycles(%d) until %d\n",
118                tc->getCpuPtr()->name(), cycles, resume);
119
120        tc->suspend();
121        if (tc->getKernelStats())
122            tc->getKernelStats()->quiesce();
123    }
124
125    uint64_t
126    quiesceTime(ThreadContext *tc)
127    {
128        return (tc->readLastActivate() - tc->readLastSuspend()) / Clock::Int::ns;
129    }
130
131    void
132    m5exit_old(ThreadContext *tc)
133    {
134        exitSimLoop("m5_exit_old instruction encountered");
135    }
136
137    void
138    m5exit(ThreadContext *tc, Tick delay)
139    {
140        Tick when = curTick + delay * Clock::Int::ns;
141        schedExitSimLoop("m5_exit instruction encountered", when);
142    }
143
144    void
145    loadsymbol(ThreadContext *tc)
146    {
147        const string &filename = tc->getCpuPtr()->system->params()->symbolfile;
148        if (filename.empty()) {
149            return;
150        }
151
152        std::string buffer;
153        ifstream file(filename.c_str());
154
155        if (!file)
156            fatal("file error: Can't open symbol table file %s\n", filename);
157
158        while (!file.eof()) {
159            getline(file, buffer);
160
161            if (buffer.empty())
162                continue;
163
164            int idx = buffer.find(' ');
165            if (idx == string::npos)
166                continue;
167
168            string address = "0x" + buffer.substr(0, idx);
169            eat_white(address);
170            if (address.empty())
171                continue;
172
173            // Skip over letter and space
174            string symbol = buffer.substr(idx + 3);
175            eat_white(symbol);
176            if (symbol.empty())
177                continue;
178
179            Addr addr;
180            if (!to_number(address, addr))
181                continue;
182
183            if (!tc->getSystemPtr()->kernelSymtab->insert(addr, symbol))
184                continue;
185
186
187            DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
188        }
189        file.close();
190    }
191
192    void
193    resetstats(ThreadContext *tc, Tick delay, Tick period)
194    {
195        if (!tc->getCpuPtr()->params->do_statistics_insts)
196            return;
197
198
199        Tick when = curTick + delay * Clock::Int::ns;
200        Tick repeat = period * Clock::Int::ns;
201
202        using namespace Stats;
203        SetupEvent(Reset, when, repeat);
204    }
205
206    void
207    dumpstats(ThreadContext *tc, Tick delay, Tick period)
208    {
209        if (!tc->getCpuPtr()->params->do_statistics_insts)
210            return;
211
212
213        Tick when = curTick + delay * Clock::Int::ns;
214        Tick repeat = period * Clock::Int::ns;
215
216        using namespace Stats;
217        SetupEvent(Dump, when, repeat);
218    }
219
220    void
221    addsymbol(ThreadContext *tc, Addr addr, Addr symbolAddr)
222    {
223        char symb[100];
224        CopyStringOut(tc, symb, symbolAddr, 100);
225        std::string symbol(symb);
226
227        DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
228
229        tc->getSystemPtr()->kernelSymtab->insert(addr,symbol);
230    }
231
232    void
233    anBegin(ThreadContext *tc, uint64_t cur)
234    {
235        Annotate::annotations.add(tc->getSystemPtr(), 0, cur >> 32, cur &
236                0xFFFFFFFF, 0,0);
237    }
238
239    void
240    anWait(ThreadContext *tc, uint64_t cur, uint64_t wait)
241    {
242        Annotate::annotations.add(tc->getSystemPtr(), 0, cur >> 32, cur &
243                0xFFFFFFFF, wait >> 32, wait & 0xFFFFFFFF);
244    }
245
246
247    void
248    dumpresetstats(ThreadContext *tc, Tick delay, Tick period)
249    {
250        if (!tc->getCpuPtr()->params->do_statistics_insts)
251            return;
252
253
254        Tick when = curTick + delay * Clock::Int::ns;
255        Tick repeat = period * Clock::Int::ns;
256
257        using namespace Stats;
258        SetupEvent(Dump|Reset, when, repeat);
259    }
260
261    void
262    m5checkpoint(ThreadContext *tc, Tick delay, Tick period)
263    {
264        if (!tc->getCpuPtr()->params->do_checkpoint_insts)
265            return;
266
267        Tick when = curTick + delay * Clock::Int::ns;
268        Tick repeat = period * Clock::Int::ns;
269
270        schedExitSimLoop("checkpoint", when, repeat);
271    }
272
273    uint64_t
274    readfile(ThreadContext *tc, Addr vaddr, uint64_t len, uint64_t offset)
275    {
276        const string &file = tc->getSystemPtr()->params()->readfile;
277        if (file.empty()) {
278            return ULL(0);
279        }
280
281        uint64_t result = 0;
282
283        int fd = ::open(file.c_str(), O_RDONLY, 0);
284        if (fd < 0)
285            panic("could not open file %s\n", file);
286
287        if (::lseek(fd, offset, SEEK_SET) < 0)
288            panic("could not seek: %s", strerror(errno));
289
290        char *buf = new char[len];
291        char *p = buf;
292        while (len > 0) {
293            int bytes = ::read(fd, p, len);
294            if (bytes <= 0)
295                break;
296
297            p += bytes;
298            result += bytes;
299            len -= bytes;
300        }
301
302        close(fd);
303        CopyIn(tc, vaddr, buf, result);
304        delete [] buf;
305        return result;
306    }
307
308    void debugbreak(ThreadContext *tc)
309    {
310        debug_break();
311    }
312
313    void switchcpu(ThreadContext *tc)
314    {
315        exitSimLoop("switchcpu");
316    }
317}
318