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