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