pseudo_inst.cc (7914:eee5bb0fb8ea) pseudo_inst.cc (8142:e08035e1a1f6)
1/*
1/*
2 * Copyright (c) 2010 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
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/vtophys.hh"
39#include "base/debug.hh"
40#include "config/full_system.hh"
41#include "config/the_isa.hh"
42#include "cpu/base.hh"
43#include "cpu/thread_context.hh"
44#include "cpu/quiesce_event.hh"
45#include "params/BaseCPU.hh"
46#include "sim/pseudo_inst.hh"
47#include "sim/serialize.hh"
48#include "sim/sim_events.hh"
49#include "sim/sim_exit.hh"
50#include "sim/stat_control.hh"
51#include "sim/stats.hh"
52#include "sim/system.hh"
53
54#if FULL_SYSTEM
55#include "arch/kernel_stats.hh"
56#include "sim/vptr.hh"
57#endif
58
59using namespace std;
60
61using namespace Stats;
62using namespace TheISA;
63
64namespace PseudoInst {
65
66#if FULL_SYSTEM
67
68void
69arm(ThreadContext *tc)
70{
71 if (tc->getKernelStats())
72 tc->getKernelStats()->arm();
73}
74
75void
76quiesce(ThreadContext *tc)
77{
78 if (!tc->getCpuPtr()->params()->do_quiesce)
79 return;
80
81 DPRINTF(Quiesce, "%s: quiesce()\n", tc->getCpuPtr()->name());
82
83 tc->suspend();
84 if (tc->getKernelStats())
85 tc->getKernelStats()->quiesce();
86}
87
88void
14 * Copyright (c) 2003-2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 */
42
43#include <errno.h>
44#include <fcntl.h>
45#include <unistd.h>
46
47#include <fstream>
48#include <string>
49
50#include "arch/vtophys.hh"
51#include "base/debug.hh"
52#include "config/full_system.hh"
53#include "config/the_isa.hh"
54#include "cpu/base.hh"
55#include "cpu/thread_context.hh"
56#include "cpu/quiesce_event.hh"
57#include "params/BaseCPU.hh"
58#include "sim/pseudo_inst.hh"
59#include "sim/serialize.hh"
60#include "sim/sim_events.hh"
61#include "sim/sim_exit.hh"
62#include "sim/stat_control.hh"
63#include "sim/stats.hh"
64#include "sim/system.hh"
65
66#if FULL_SYSTEM
67#include "arch/kernel_stats.hh"
68#include "sim/vptr.hh"
69#endif
70
71using namespace std;
72
73using namespace Stats;
74using namespace TheISA;
75
76namespace PseudoInst {
77
78#if FULL_SYSTEM
79
80void
81arm(ThreadContext *tc)
82{
83 if (tc->getKernelStats())
84 tc->getKernelStats()->arm();
85}
86
87void
88quiesce(ThreadContext *tc)
89{
90 if (!tc->getCpuPtr()->params()->do_quiesce)
91 return;
92
93 DPRINTF(Quiesce, "%s: quiesce()\n", tc->getCpuPtr()->name());
94
95 tc->suspend();
96 if (tc->getKernelStats())
97 tc->getKernelStats()->quiesce();
98}
99
100void
101quiesceSkip(ThreadContext *tc)
102{
103 BaseCPU *cpu = tc->getCpuPtr();
104
105 if (!cpu->params()->do_quiesce)
106 return;
107
108 EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
109
110 Tick resume = curTick() + 1;
111
112 cpu->reschedule(quiesceEvent, resume, true);
113
114 DPRINTF(Quiesce, "%s: quiesceSkip() until %d\n",
115 cpu->name(), resume);
116
117 tc->suspend();
118 if (tc->getKernelStats())
119 tc->getKernelStats()->quiesce();
120}
121
122void
89quiesceNs(ThreadContext *tc, uint64_t ns)
90{
91 BaseCPU *cpu = tc->getCpuPtr();
92
93 if (!cpu->params()->do_quiesce || ns == 0)
94 return;
95
96 EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
97
98 Tick resume = curTick() + SimClock::Int::ns * ns;
99
100 cpu->reschedule(quiesceEvent, resume, true);
101
102 DPRINTF(Quiesce, "%s: quiesceNs(%d) until %d\n",
103 cpu->name(), ns, resume);
104
105 tc->suspend();
106 if (tc->getKernelStats())
107 tc->getKernelStats()->quiesce();
108}
109
110void
111quiesceCycles(ThreadContext *tc, uint64_t cycles)
112{
113 BaseCPU *cpu = tc->getCpuPtr();
114
115 if (!cpu->params()->do_quiesce || cycles == 0)
116 return;
117
118 EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
119
120 Tick resume = curTick() + cpu->ticks(cycles);
121
122 cpu->reschedule(quiesceEvent, resume, true);
123
124 DPRINTF(Quiesce, "%s: quiesceCycles(%d) until %d\n",
125 cpu->name(), cycles, resume);
126
127 tc->suspend();
128 if (tc->getKernelStats())
129 tc->getKernelStats()->quiesce();
130}
131
132uint64_t
133quiesceTime(ThreadContext *tc)
134{
135 return (tc->readLastActivate() - tc->readLastSuspend()) /
136 SimClock::Int::ns;
137}
138
139#endif
140
141uint64_t
142rpns(ThreadContext *tc)
143{
144 return curTick() / SimClock::Int::ns;
145}
146
147void
148wakeCPU(ThreadContext *tc, uint64_t cpuid)
149{
150 System *sys = tc->getSystemPtr();
151 ThreadContext *other_tc = sys->threadContexts[cpuid];
152 if (other_tc->status() == ThreadContext::Suspended)
153 other_tc->activate();
154}
155
156void
157m5exit(ThreadContext *tc, Tick delay)
158{
159 Tick when = curTick() + delay * SimClock::Int::ns;
160 exitSimLoop("m5_exit instruction encountered", 0, when);
161}
162
163#if FULL_SYSTEM
164
165void
166loadsymbol(ThreadContext *tc)
167{
168 const string &filename = tc->getCpuPtr()->system->params()->symbolfile;
169 if (filename.empty()) {
170 return;
171 }
172
173 std::string buffer;
174 ifstream file(filename.c_str());
175
176 if (!file)
177 fatal("file error: Can't open symbol table file %s\n", filename);
178
179 while (!file.eof()) {
180 getline(file, buffer);
181
182 if (buffer.empty())
183 continue;
184
185 string::size_type idx = buffer.find(' ');
186 if (idx == string::npos)
187 continue;
188
189 string address = "0x" + buffer.substr(0, idx);
190 eat_white(address);
191 if (address.empty())
192 continue;
193
194 // Skip over letter and space
195 string symbol = buffer.substr(idx + 3);
196 eat_white(symbol);
197 if (symbol.empty())
198 continue;
199
200 Addr addr;
201 if (!to_number(address, addr))
202 continue;
203
204 if (!tc->getSystemPtr()->kernelSymtab->insert(addr, symbol))
205 continue;
206
207
208 DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
209 }
210 file.close();
211}
212
213void
214addsymbol(ThreadContext *tc, Addr addr, Addr symbolAddr)
215{
216 char symb[100];
217 CopyStringOut(tc, symb, symbolAddr, 100);
218 std::string symbol(symb);
219
220 DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
221
222 tc->getSystemPtr()->kernelSymtab->insert(addr,symbol);
223 debugSymbolTable->insert(addr,symbol);
224}
225
226#endif
227
228
229void
230resetstats(ThreadContext *tc, Tick delay, Tick period)
231{
232 if (!tc->getCpuPtr()->params()->do_statistics_insts)
233 return;
234
235
236 Tick when = curTick() + delay * SimClock::Int::ns;
237 Tick repeat = period * SimClock::Int::ns;
238
239 Stats::schedStatEvent(false, true, when, repeat);
240}
241
242void
243dumpstats(ThreadContext *tc, Tick delay, Tick period)
244{
245 if (!tc->getCpuPtr()->params()->do_statistics_insts)
246 return;
247
248
249 Tick when = curTick() + delay * SimClock::Int::ns;
250 Tick repeat = period * SimClock::Int::ns;
251
252 Stats::schedStatEvent(true, false, when, repeat);
253}
254
255void
256dumpresetstats(ThreadContext *tc, Tick delay, Tick period)
257{
258 if (!tc->getCpuPtr()->params()->do_statistics_insts)
259 return;
260
261
262 Tick when = curTick() + delay * SimClock::Int::ns;
263 Tick repeat = period * SimClock::Int::ns;
264
265 Stats::schedStatEvent(true, true, when, repeat);
266}
267
268void
269m5checkpoint(ThreadContext *tc, Tick delay, Tick period)
270{
271 if (!tc->getCpuPtr()->params()->do_checkpoint_insts)
272 return;
273
274 Tick when = curTick() + delay * SimClock::Int::ns;
275 Tick repeat = period * SimClock::Int::ns;
276
277 exitSimLoop("checkpoint", 0, when, repeat);
278}
279
280#if FULL_SYSTEM
281
282uint64_t
283readfile(ThreadContext *tc, Addr vaddr, uint64_t len, uint64_t offset)
284{
285 const string &file = tc->getSystemPtr()->params()->readfile;
286 if (file.empty()) {
287 return ULL(0);
288 }
289
290 uint64_t result = 0;
291
292 int fd = ::open(file.c_str(), O_RDONLY, 0);
293 if (fd < 0)
294 panic("could not open file %s\n", file);
295
296 if (::lseek(fd, offset, SEEK_SET) < 0)
297 panic("could not seek: %s", strerror(errno));
298
299 char *buf = new char[len];
300 char *p = buf;
301 while (len > 0) {
302 int bytes = ::read(fd, p, len);
303 if (bytes <= 0)
304 break;
305
306 p += bytes;
307 result += bytes;
308 len -= bytes;
309 }
310
311 close(fd);
312 CopyIn(tc, vaddr, buf, result);
313 delete [] buf;
314 return result;
315}
316
317#endif
318
319void
320debugbreak(ThreadContext *tc)
321{
322 debug_break();
323}
324
325void
326switchcpu(ThreadContext *tc)
327{
328 exitSimLoop("switchcpu");
329}
330
331//
332// This function is executed when annotated work items begin. Depending on
333// what the user specified at the command line, the simulation may exit and/or
334// take a checkpoint when a certain work item begins.
335//
336void
337workbegin(ThreadContext *tc, uint64_t workid, uint64_t threadid)
338{
339 tc->getCpuPtr()->workItemBegin();
340 System *sys = tc->getSystemPtr();
341
342 DPRINTF(WorkItems, "Work Begin workid: %d, threadid %d\n", workid,
343 threadid);
344
345 //
346 // If specified, determine if this is the specific work item the user
347 // identified
348 //
349 if (sys->params()->work_item_id == -1 ||
350 sys->params()->work_item_id == workid) {
351
352 uint64_t systemWorkBeginCount = sys->incWorkItemsBegin();
353 int cpuId = tc->getCpuPtr()->cpuId();
354
355 if (sys->params()->work_cpus_ckpt_count != 0 &&
356 sys->markWorkItem(cpuId) >= sys->params()->work_cpus_ckpt_count) {
357 //
358 // If active cpus equals checkpoint count, create checkpoint
359 //
360 Event *event = new SimLoopExitEvent("checkpoint", 0);
361 mainEventQueue.schedule(event, curTick());
362 }
363
364 if (systemWorkBeginCount == sys->params()->work_begin_ckpt_count) {
365 //
366 // Note: the string specified as the cause of the exit event must
367 // exactly equal "checkpoint" inorder to create a checkpoint
368 //
369 Event *event = new SimLoopExitEvent("checkpoint", 0);
370 mainEventQueue.schedule(event, curTick());
371 }
372
373 if (systemWorkBeginCount == sys->params()->work_begin_exit_count) {
374 //
375 // If a certain number of work items started, exit simulation
376 //
377 Event *event = new SimLoopExitEvent("work started count reach", 0);
378 mainEventQueue.schedule(event, curTick());
379 }
380
381 if (tc->getCpuPtr()->cpuId() == sys->params()->work_begin_cpu_id_exit) {
382 //
383 // If work started on the specific cpu id specified, exit simulation
384 //
385 Event *event = new SimLoopExitEvent("work started on specific cpu",
386 0);
387
388 mainEventQueue.schedule(event, curTick() + 1);
389 }
390 }
391}
392
393//
394// This function is executed when annotated work items end. Depending on
395// what the user specified at the command line, the simulation may exit and/or
396// take a checkpoint when a certain work item ends.
397//
398void
399workend(ThreadContext *tc, uint64_t workid, uint64_t threadid)
400{
401 tc->getCpuPtr()->workItemEnd();
402 System *sys = tc->getSystemPtr();
403
404 DPRINTF(WorkItems, "Work End workid: %d, threadid %d\n", workid, threadid);
405
406 //
407 // If specified, determine if this is the specific work item the user
408 // identified
409 //
410 if (sys->params()->work_item_id == -1 ||
411 sys->params()->work_item_id == workid) {
412
413 uint64_t systemWorkEndCount = sys->incWorkItemsEnd();
414 int cpuId = tc->getCpuPtr()->cpuId();
415
416 if (sys->params()->work_cpus_ckpt_count != 0 &&
417 sys->markWorkItem(cpuId) >= sys->params()->work_cpus_ckpt_count) {
418 //
419 // If active cpus equals checkpoint count, create checkpoint
420 //
421 Event *event = new SimLoopExitEvent("checkpoint", 0);
422 mainEventQueue.schedule(event, curTick());
423 }
424
425 if (sys->params()->work_end_ckpt_count != 0 &&
426 systemWorkEndCount == sys->params()->work_end_ckpt_count) {
427 //
428 // If total work items completed equals checkpoint count, create
429 // checkpoint
430 //
431 Event *event = new SimLoopExitEvent("checkpoint", 0);
432 mainEventQueue.schedule(event, curTick());
433 }
434
435 if (sys->params()->work_end_exit_count != 0 &&
436 systemWorkEndCount == sys->params()->work_end_exit_count) {
437 //
438 // If total work items completed equals exit count, exit simulation
439 //
440 Event *event = new SimLoopExitEvent("work items exit count reached",
441 0);
442
443 mainEventQueue.schedule(event, curTick());
444 }
445 }
446}
447
448} // namespace PseudoInst
123quiesceNs(ThreadContext *tc, uint64_t ns)
124{
125 BaseCPU *cpu = tc->getCpuPtr();
126
127 if (!cpu->params()->do_quiesce || ns == 0)
128 return;
129
130 EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
131
132 Tick resume = curTick() + SimClock::Int::ns * ns;
133
134 cpu->reschedule(quiesceEvent, resume, true);
135
136 DPRINTF(Quiesce, "%s: quiesceNs(%d) until %d\n",
137 cpu->name(), ns, resume);
138
139 tc->suspend();
140 if (tc->getKernelStats())
141 tc->getKernelStats()->quiesce();
142}
143
144void
145quiesceCycles(ThreadContext *tc, uint64_t cycles)
146{
147 BaseCPU *cpu = tc->getCpuPtr();
148
149 if (!cpu->params()->do_quiesce || cycles == 0)
150 return;
151
152 EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
153
154 Tick resume = curTick() + cpu->ticks(cycles);
155
156 cpu->reschedule(quiesceEvent, resume, true);
157
158 DPRINTF(Quiesce, "%s: quiesceCycles(%d) until %d\n",
159 cpu->name(), cycles, resume);
160
161 tc->suspend();
162 if (tc->getKernelStats())
163 tc->getKernelStats()->quiesce();
164}
165
166uint64_t
167quiesceTime(ThreadContext *tc)
168{
169 return (tc->readLastActivate() - tc->readLastSuspend()) /
170 SimClock::Int::ns;
171}
172
173#endif
174
175uint64_t
176rpns(ThreadContext *tc)
177{
178 return curTick() / SimClock::Int::ns;
179}
180
181void
182wakeCPU(ThreadContext *tc, uint64_t cpuid)
183{
184 System *sys = tc->getSystemPtr();
185 ThreadContext *other_tc = sys->threadContexts[cpuid];
186 if (other_tc->status() == ThreadContext::Suspended)
187 other_tc->activate();
188}
189
190void
191m5exit(ThreadContext *tc, Tick delay)
192{
193 Tick when = curTick() + delay * SimClock::Int::ns;
194 exitSimLoop("m5_exit instruction encountered", 0, when);
195}
196
197#if FULL_SYSTEM
198
199void
200loadsymbol(ThreadContext *tc)
201{
202 const string &filename = tc->getCpuPtr()->system->params()->symbolfile;
203 if (filename.empty()) {
204 return;
205 }
206
207 std::string buffer;
208 ifstream file(filename.c_str());
209
210 if (!file)
211 fatal("file error: Can't open symbol table file %s\n", filename);
212
213 while (!file.eof()) {
214 getline(file, buffer);
215
216 if (buffer.empty())
217 continue;
218
219 string::size_type idx = buffer.find(' ');
220 if (idx == string::npos)
221 continue;
222
223 string address = "0x" + buffer.substr(0, idx);
224 eat_white(address);
225 if (address.empty())
226 continue;
227
228 // Skip over letter and space
229 string symbol = buffer.substr(idx + 3);
230 eat_white(symbol);
231 if (symbol.empty())
232 continue;
233
234 Addr addr;
235 if (!to_number(address, addr))
236 continue;
237
238 if (!tc->getSystemPtr()->kernelSymtab->insert(addr, symbol))
239 continue;
240
241
242 DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
243 }
244 file.close();
245}
246
247void
248addsymbol(ThreadContext *tc, Addr addr, Addr symbolAddr)
249{
250 char symb[100];
251 CopyStringOut(tc, symb, symbolAddr, 100);
252 std::string symbol(symb);
253
254 DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
255
256 tc->getSystemPtr()->kernelSymtab->insert(addr,symbol);
257 debugSymbolTable->insert(addr,symbol);
258}
259
260#endif
261
262
263void
264resetstats(ThreadContext *tc, Tick delay, Tick period)
265{
266 if (!tc->getCpuPtr()->params()->do_statistics_insts)
267 return;
268
269
270 Tick when = curTick() + delay * SimClock::Int::ns;
271 Tick repeat = period * SimClock::Int::ns;
272
273 Stats::schedStatEvent(false, true, when, repeat);
274}
275
276void
277dumpstats(ThreadContext *tc, Tick delay, Tick period)
278{
279 if (!tc->getCpuPtr()->params()->do_statistics_insts)
280 return;
281
282
283 Tick when = curTick() + delay * SimClock::Int::ns;
284 Tick repeat = period * SimClock::Int::ns;
285
286 Stats::schedStatEvent(true, false, when, repeat);
287}
288
289void
290dumpresetstats(ThreadContext *tc, Tick delay, Tick period)
291{
292 if (!tc->getCpuPtr()->params()->do_statistics_insts)
293 return;
294
295
296 Tick when = curTick() + delay * SimClock::Int::ns;
297 Tick repeat = period * SimClock::Int::ns;
298
299 Stats::schedStatEvent(true, true, when, repeat);
300}
301
302void
303m5checkpoint(ThreadContext *tc, Tick delay, Tick period)
304{
305 if (!tc->getCpuPtr()->params()->do_checkpoint_insts)
306 return;
307
308 Tick when = curTick() + delay * SimClock::Int::ns;
309 Tick repeat = period * SimClock::Int::ns;
310
311 exitSimLoop("checkpoint", 0, when, repeat);
312}
313
314#if FULL_SYSTEM
315
316uint64_t
317readfile(ThreadContext *tc, Addr vaddr, uint64_t len, uint64_t offset)
318{
319 const string &file = tc->getSystemPtr()->params()->readfile;
320 if (file.empty()) {
321 return ULL(0);
322 }
323
324 uint64_t result = 0;
325
326 int fd = ::open(file.c_str(), O_RDONLY, 0);
327 if (fd < 0)
328 panic("could not open file %s\n", file);
329
330 if (::lseek(fd, offset, SEEK_SET) < 0)
331 panic("could not seek: %s", strerror(errno));
332
333 char *buf = new char[len];
334 char *p = buf;
335 while (len > 0) {
336 int bytes = ::read(fd, p, len);
337 if (bytes <= 0)
338 break;
339
340 p += bytes;
341 result += bytes;
342 len -= bytes;
343 }
344
345 close(fd);
346 CopyIn(tc, vaddr, buf, result);
347 delete [] buf;
348 return result;
349}
350
351#endif
352
353void
354debugbreak(ThreadContext *tc)
355{
356 debug_break();
357}
358
359void
360switchcpu(ThreadContext *tc)
361{
362 exitSimLoop("switchcpu");
363}
364
365//
366// This function is executed when annotated work items begin. Depending on
367// what the user specified at the command line, the simulation may exit and/or
368// take a checkpoint when a certain work item begins.
369//
370void
371workbegin(ThreadContext *tc, uint64_t workid, uint64_t threadid)
372{
373 tc->getCpuPtr()->workItemBegin();
374 System *sys = tc->getSystemPtr();
375
376 DPRINTF(WorkItems, "Work Begin workid: %d, threadid %d\n", workid,
377 threadid);
378
379 //
380 // If specified, determine if this is the specific work item the user
381 // identified
382 //
383 if (sys->params()->work_item_id == -1 ||
384 sys->params()->work_item_id == workid) {
385
386 uint64_t systemWorkBeginCount = sys->incWorkItemsBegin();
387 int cpuId = tc->getCpuPtr()->cpuId();
388
389 if (sys->params()->work_cpus_ckpt_count != 0 &&
390 sys->markWorkItem(cpuId) >= sys->params()->work_cpus_ckpt_count) {
391 //
392 // If active cpus equals checkpoint count, create checkpoint
393 //
394 Event *event = new SimLoopExitEvent("checkpoint", 0);
395 mainEventQueue.schedule(event, curTick());
396 }
397
398 if (systemWorkBeginCount == sys->params()->work_begin_ckpt_count) {
399 //
400 // Note: the string specified as the cause of the exit event must
401 // exactly equal "checkpoint" inorder to create a checkpoint
402 //
403 Event *event = new SimLoopExitEvent("checkpoint", 0);
404 mainEventQueue.schedule(event, curTick());
405 }
406
407 if (systemWorkBeginCount == sys->params()->work_begin_exit_count) {
408 //
409 // If a certain number of work items started, exit simulation
410 //
411 Event *event = new SimLoopExitEvent("work started count reach", 0);
412 mainEventQueue.schedule(event, curTick());
413 }
414
415 if (tc->getCpuPtr()->cpuId() == sys->params()->work_begin_cpu_id_exit) {
416 //
417 // If work started on the specific cpu id specified, exit simulation
418 //
419 Event *event = new SimLoopExitEvent("work started on specific cpu",
420 0);
421
422 mainEventQueue.schedule(event, curTick() + 1);
423 }
424 }
425}
426
427//
428// This function is executed when annotated work items end. Depending on
429// what the user specified at the command line, the simulation may exit and/or
430// take a checkpoint when a certain work item ends.
431//
432void
433workend(ThreadContext *tc, uint64_t workid, uint64_t threadid)
434{
435 tc->getCpuPtr()->workItemEnd();
436 System *sys = tc->getSystemPtr();
437
438 DPRINTF(WorkItems, "Work End workid: %d, threadid %d\n", workid, threadid);
439
440 //
441 // If specified, determine if this is the specific work item the user
442 // identified
443 //
444 if (sys->params()->work_item_id == -1 ||
445 sys->params()->work_item_id == workid) {
446
447 uint64_t systemWorkEndCount = sys->incWorkItemsEnd();
448 int cpuId = tc->getCpuPtr()->cpuId();
449
450 if (sys->params()->work_cpus_ckpt_count != 0 &&
451 sys->markWorkItem(cpuId) >= sys->params()->work_cpus_ckpt_count) {
452 //
453 // If active cpus equals checkpoint count, create checkpoint
454 //
455 Event *event = new SimLoopExitEvent("checkpoint", 0);
456 mainEventQueue.schedule(event, curTick());
457 }
458
459 if (sys->params()->work_end_ckpt_count != 0 &&
460 systemWorkEndCount == sys->params()->work_end_ckpt_count) {
461 //
462 // If total work items completed equals checkpoint count, create
463 // checkpoint
464 //
465 Event *event = new SimLoopExitEvent("checkpoint", 0);
466 mainEventQueue.schedule(event, curTick());
467 }
468
469 if (sys->params()->work_end_exit_count != 0 &&
470 systemWorkEndCount == sys->params()->work_end_exit_count) {
471 //
472 // If total work items completed equals exit count, exit simulation
473 //
474 Event *event = new SimLoopExitEvent("work items exit count reached",
475 0);
476
477 mainEventQueue.schedule(event, curTick());
478 }
479 }
480}
481
482} // namespace PseudoInst