53892b5d196c8fe6d8928074b83e2d760d995735
2 * Copyright (c) 2010-2012, 2015, 2017 ARM Limited
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.
14 * Copyright (c) 2011 Advanced Micro Devices, Inc.
15 * Copyright (c) 2003-2006 The Regents of The University of Michigan
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * Authors: Nathan Binkert
44 #include "sim/pseudo_inst.hh"
54 #include <gem5/asm/generic/m5ops.h>
56 #include "arch/pseudo_inst.hh"
57 #include "arch/utility.hh"
58 #include "arch/vtophys.hh"
59 #include "base/debug.hh"
60 #include "base/output.hh"
61 #include "config/the_isa.hh"
62 #include "cpu/base.hh"
63 #include "cpu/quiesce_event.hh"
64 #include "cpu/thread_context.hh"
65 #include "debug/Loader.hh"
66 #include "debug/PseudoInst.hh"
67 #include "debug/Quiesce.hh"
68 #include "debug/WorkItems.hh"
69 #include "dev/net/dist_iface.hh"
70 #include "kern/kernel_stats.hh"
71 #include "params/BaseCPU.hh"
72 #include "sim/full_system.hh"
73 #include "sim/initparam_keys.hh"
74 #include "sim/process.hh"
75 #include "sim/serialize.hh"
76 #include "sim/sim_events.hh"
77 #include "sim/sim_exit.hh"
78 #include "sim/stat_control.hh"
79 #include "sim/stats.hh"
80 #include "sim/system.hh"
81 #include "sim/vptr.hh"
85 using namespace Stats
;
86 using namespace TheISA
;
88 namespace PseudoInst
{
91 panicFsOnlyPseudoInst(const char *name
)
93 panic("Pseudo inst \"%s\" is only available in Full System mode.");
97 pseudoInst(ThreadContext
*tc
, uint8_t func
, uint8_t subfunc
)
101 DPRINTF(PseudoInst
, "PseudoInst::pseudoInst(%i, %i)\n", func
, subfunc
);
103 // We need to do this in a slightly convoluted way since
104 // getArgument() might have side-effects on arg_num. We could have
105 // used the Argument class, but due to the possible side effects
106 // from getArgument, it'd most likely break.
108 for (int i
= 0; i
< sizeof(args
) / sizeof(*args
); ++i
) {
109 args
[arg_num
] = getArgument(tc
, arg_num
, sizeof(uint64_t), false);
122 case M5OP_QUIESCE_NS
:
123 quiesceNs(tc
, args
[0]);
126 case M5OP_QUIESCE_CYCLE
:
127 quiesceCycles(tc
, args
[0]);
130 case M5OP_QUIESCE_TIME
:
131 return quiesceTime(tc
);
137 wakeCPU(tc
, args
[0]);
145 m5fail(tc
, args
[0], args
[1]);
148 case M5OP_INIT_PARAM
:
149 return initParam(tc
, args
[0], args
[1]);
151 case M5OP_LOAD_SYMBOL
:
155 case M5OP_RESET_STATS
:
156 resetstats(tc
, args
[0], args
[1]);
159 case M5OP_DUMP_STATS
:
160 dumpstats(tc
, args
[0], args
[1]);
163 case M5OP_DUMP_RESET_STATS
:
164 dumpresetstats(tc
, args
[0], args
[1]);
167 case M5OP_CHECKPOINT
:
168 m5checkpoint(tc
, args
[0], args
[1]);
171 case M5OP_WRITE_FILE
:
172 return writefile(tc
, args
[0], args
[1], args
[2], args
[3]);
175 return readfile(tc
, args
[0], args
[1], args
[2]);
177 case M5OP_DEBUG_BREAK
:
181 case M5OP_SWITCH_CPU
:
185 case M5OP_ADD_SYMBOL
:
186 addsymbol(tc
, args
[0], args
[1]);
190 panic("M5 panic instruction called at %s\n", tc
->pcState());
192 case M5OP_WORK_BEGIN
:
193 workbegin(tc
, args
[0], args
[1]);
197 workend(tc
, args
[0], args
[1]);
205 warn("Unimplemented m5 op (0x%x)\n", func
);
208 /* SE mode functions */
209 case M5OP_SE_SYSCALL
:
213 case M5OP_SE_PAGE_FAULT
:
217 /* dist-gem5 functions */
218 case M5OP_DIST_TOGGLE_SYNC
:
223 warn("Unhandled m5 op: 0x%x\n", func
);
231 arm(ThreadContext
*tc
)
233 DPRINTF(PseudoInst
, "PseudoInst::arm()\n");
235 panicFsOnlyPseudoInst("arm");
237 if (tc
->getKernelStats())
238 tc
->getKernelStats()->arm();
242 quiesce(ThreadContext
*tc
)
244 DPRINTF(PseudoInst
, "PseudoInst::quiesce()\n");
249 quiesceSkip(ThreadContext
*tc
)
251 DPRINTF(PseudoInst
, "PseudoInst::quiesceSkip()\n");
252 tc
->quiesceTick(tc
->getCpuPtr()->nextCycle() + 1);
256 quiesceNs(ThreadContext
*tc
, uint64_t ns
)
258 DPRINTF(PseudoInst
, "PseudoInst::quiesceNs(%i)\n", ns
);
259 tc
->quiesceTick(curTick() + SimClock::Int::ns
* ns
);
263 quiesceCycles(ThreadContext
*tc
, uint64_t cycles
)
265 DPRINTF(PseudoInst
, "PseudoInst::quiesceCycles(%i)\n", cycles
);
266 tc
->quiesceTick(tc
->getCpuPtr()->clockEdge(Cycles(cycles
)));
270 quiesceTime(ThreadContext
*tc
)
272 DPRINTF(PseudoInst
, "PseudoInst::quiesceTime()\n");
274 return (tc
->readLastActivate() - tc
->readLastSuspend()) /
279 rpns(ThreadContext
*tc
)
281 DPRINTF(PseudoInst
, "PseudoInst::rpns()\n");
282 return curTick() / SimClock::Int::ns
;
286 wakeCPU(ThreadContext
*tc
, uint64_t cpuid
)
288 DPRINTF(PseudoInst
, "PseudoInst::wakeCPU(%i)\n", cpuid
);
289 System
*sys
= tc
->getSystemPtr();
291 if (sys
->numContexts() <= cpuid
) {
292 warn("PseudoInst::wakeCPU(%i), cpuid greater than number of contexts"
293 "(%i)\n",cpuid
, sys
->numContexts());
297 ThreadContext
*other_tc
= sys
->threadContexts
[cpuid
];
298 if (other_tc
->status() == ThreadContext::Suspended
)
299 other_tc
->activate();
303 m5exit(ThreadContext
*tc
, Tick delay
)
305 DPRINTF(PseudoInst
, "PseudoInst::m5exit(%i)\n", delay
);
306 if (DistIface::readyToExit(delay
)) {
307 Tick when
= curTick() + delay
* SimClock::Int::ns
;
308 exitSimLoop("m5_exit instruction encountered", 0, when
, 0, true);
313 m5fail(ThreadContext
*tc
, Tick delay
, uint64_t code
)
315 DPRINTF(PseudoInst
, "PseudoInst::m5fail(%i, %i)\n", delay
, code
);
316 Tick when
= curTick() + delay
* SimClock::Int::ns
;
317 exitSimLoop("m5_fail instruction encountered", code
, when
, 0, true);
321 loadsymbol(ThreadContext
*tc
)
323 DPRINTF(PseudoInst
, "PseudoInst::loadsymbol()\n");
325 panicFsOnlyPseudoInst("loadsymbol");
327 const string
&filename
= tc
->getCpuPtr()->system
->params()->symbolfile
;
328 if (filename
.empty()) {
333 ifstream
file(filename
.c_str());
336 fatal("file error: Can't open symbol table file %s\n", filename
);
338 while (!file
.eof()) {
339 getline(file
, buffer
);
344 string::size_type idx
= buffer
.find(' ');
345 if (idx
== string::npos
)
348 string address
= "0x" + buffer
.substr(0, idx
);
353 // Skip over letter and space
354 string symbol
= buffer
.substr(idx
+ 3);
360 if (!to_number(address
, addr
))
363 if (!tc
->getSystemPtr()->kernelSymtab
->insert(addr
, symbol
))
367 DPRINTF(Loader
, "Loaded symbol: %s @ %#llx\n", symbol
, addr
);
373 addsymbol(ThreadContext
*tc
, Addr addr
, Addr symbolAddr
)
375 DPRINTF(PseudoInst
, "PseudoInst::addsymbol(0x%x, 0x%x)\n",
378 panicFsOnlyPseudoInst("addSymbol");
381 tc
->getVirtProxy().readString(symbol
, symbolAddr
);
383 DPRINTF(Loader
, "Loaded symbol: %s @ %#llx\n", symbol
, addr
);
385 tc
->getSystemPtr()->kernelSymtab
->insert(addr
,symbol
);
386 debugSymbolTable
->insert(addr
,symbol
);
390 initParam(ThreadContext
*tc
, uint64_t key_str1
, uint64_t key_str2
)
392 DPRINTF(PseudoInst
, "PseudoInst::initParam() key:%s%s\n", (char *)&key_str1
,
395 panicFsOnlyPseudoInst("initParam");
399 // The key parameter string is passed in via two 64-bit registers. We copy
400 // out the characters from the 64-bit integer variables here and concatenate
401 // them in the key_str character buffer
402 const int len
= 2 * sizeof(uint64_t) + 1;
404 memset(key_str
, '\0', len
);
406 assert(key_str2
== 0);
408 strncpy(key_str
, (char *)&key_str1
, sizeof(uint64_t));
411 if (strlen(key_str
) == sizeof(uint64_t)) {
412 strncpy(key_str
+ sizeof(uint64_t), (char *)&key_str2
,
415 assert(key_str2
== 0);
418 // Compare the key parameter with the known values to select the return
421 if (strcmp(key_str
, InitParamKey::DEFAULT
) == 0) {
422 val
= tc
->getCpuPtr()->system
->init_param
;
423 } else if (strcmp(key_str
, InitParamKey::DIST_RANK
) == 0) {
424 val
= DistIface::rankParam();
425 } else if (strcmp(key_str
, InitParamKey::DIST_SIZE
) == 0) {
426 val
= DistIface::sizeParam();
428 panic("Unknown key for initparam pseudo instruction:\"%s\"", key_str
);
435 resetstats(ThreadContext
*tc
, Tick delay
, Tick period
)
437 DPRINTF(PseudoInst
, "PseudoInst::resetstats(%i, %i)\n", delay
, period
);
438 if (!tc
->getCpuPtr()->params()->do_statistics_insts
)
442 Tick when
= curTick() + delay
* SimClock::Int::ns
;
443 Tick repeat
= period
* SimClock::Int::ns
;
445 Stats::schedStatEvent(false, true, when
, repeat
);
449 dumpstats(ThreadContext
*tc
, Tick delay
, Tick period
)
451 DPRINTF(PseudoInst
, "PseudoInst::dumpstats(%i, %i)\n", delay
, period
);
452 if (!tc
->getCpuPtr()->params()->do_statistics_insts
)
456 Tick when
= curTick() + delay
* SimClock::Int::ns
;
457 Tick repeat
= period
* SimClock::Int::ns
;
459 Stats::schedStatEvent(true, false, when
, repeat
);
463 dumpresetstats(ThreadContext
*tc
, Tick delay
, Tick period
)
465 DPRINTF(PseudoInst
, "PseudoInst::dumpresetstats(%i, %i)\n", delay
, period
);
466 if (!tc
->getCpuPtr()->params()->do_statistics_insts
)
470 Tick when
= curTick() + delay
* SimClock::Int::ns
;
471 Tick repeat
= period
* SimClock::Int::ns
;
473 Stats::schedStatEvent(true, true, when
, repeat
);
477 m5checkpoint(ThreadContext
*tc
, Tick delay
, Tick period
)
479 DPRINTF(PseudoInst
, "PseudoInst::m5checkpoint(%i, %i)\n", delay
, period
);
480 if (!tc
->getCpuPtr()->params()->do_checkpoint_insts
)
483 if (DistIface::readyToCkpt(delay
, period
)) {
484 Tick when
= curTick() + delay
* SimClock::Int::ns
;
485 Tick repeat
= period
* SimClock::Int::ns
;
486 exitSimLoop("checkpoint", 0, when
, repeat
);
491 readfile(ThreadContext
*tc
, Addr vaddr
, uint64_t len
, uint64_t offset
)
493 DPRINTF(PseudoInst
, "PseudoInst::readfile(0x%x, 0x%x, 0x%x)\n",
496 panicFsOnlyPseudoInst("readfile");
500 const string
&file
= tc
->getSystemPtr()->params()->readfile
;
507 int fd
= ::open(file
.c_str(), O_RDONLY
, 0);
509 panic("could not open file %s\n", file
);
511 if (::lseek(fd
, offset
, SEEK_SET
) < 0)
512 panic("could not seek: %s", strerror(errno
));
514 char *buf
= new char[len
];
517 int bytes
= ::read(fd
, p
, len
);
527 tc
->getVirtProxy().writeBlob(vaddr
, buf
, result
);
533 writefile(ThreadContext
*tc
, Addr vaddr
, uint64_t len
, uint64_t offset
,
536 DPRINTF(PseudoInst
, "PseudoInst::writefile(0x%x, 0x%x, 0x%x, 0x%x)\n",
537 vaddr
, len
, offset
, filename_addr
);
539 // copy out target filename
540 std::string filename
;
541 tc
->getVirtProxy().readString(filename
, filename_addr
);
545 // create a new file (truncate)
546 out
= simout
.create(filename
, true, true);
548 // do not truncate file if offset is non-zero
549 // (ios::in flag is required as well to keep the existing data
550 // intact, otherwise existing data will be zeroed out.)
551 out
= simout
.open(filename
, ios::in
| ios::out
| ios::binary
, true);
554 ostream
*os(out
->stream());
556 panic("could not open file %s\n", filename
);
561 // copy out data and write to file
562 char *buf
= new char[len
];
563 tc
->getVirtProxy().readBlob(vaddr
, buf
, len
);
565 if (os
->fail() || os
->bad())
566 panic("Error while doing writefile!\n");
576 debugbreak(ThreadContext
*tc
)
578 DPRINTF(PseudoInst
, "PseudoInst::debugbreak()\n");
583 switchcpu(ThreadContext
*tc
)
585 DPRINTF(PseudoInst
, "PseudoInst::switchcpu()\n");
586 exitSimLoop("switchcpu");
590 * This function is executed when the simulation is executing the syscall
591 * handler in System Emulation mode.
594 m5Syscall(ThreadContext
*tc
)
596 DPRINTF(PseudoInst
, "PseudoInst::m5Syscall()\n");
602 togglesync(ThreadContext
*tc
)
604 DPRINTF(PseudoInst
, "PseudoInst::togglesync()\n");
605 DistIface::toggleSync(tc
);
609 // This function is executed when annotated work items begin. Depending on
610 // what the user specified at the command line, the simulation may exit and/or
611 // take a checkpoint when a certain work item begins.
614 workbegin(ThreadContext
*tc
, uint64_t workid
, uint64_t threadid
)
616 DPRINTF(PseudoInst
, "PseudoInst::workbegin(%i, %i)\n", workid
, threadid
);
617 System
*sys
= tc
->getSystemPtr();
618 const System::Params
*params
= sys
->params();
620 if (params
->exit_on_work_items
) {
621 exitSimLoop("workbegin", static_cast<int>(workid
));
625 DPRINTF(WorkItems
, "Work Begin workid: %d, threadid %d\n", workid
,
627 tc
->getCpuPtr()->workItemBegin();
628 sys
->workItemBegin(threadid
, workid
);
631 // If specified, determine if this is the specific work item the user
634 if (params
->work_item_id
== -1 || params
->work_item_id
== workid
) {
636 uint64_t systemWorkBeginCount
= sys
->incWorkItemsBegin();
637 int cpuId
= tc
->getCpuPtr()->cpuId();
639 if (params
->work_cpus_ckpt_count
!= 0 &&
640 sys
->markWorkItem(cpuId
) >= params
->work_cpus_ckpt_count
) {
642 // If active cpus equals checkpoint count, create checkpoint
644 exitSimLoop("checkpoint");
647 if (systemWorkBeginCount
== params
->work_begin_ckpt_count
) {
649 // Note: the string specified as the cause of the exit event must
650 // exactly equal "checkpoint" inorder to create a checkpoint
652 exitSimLoop("checkpoint");
655 if (systemWorkBeginCount
== params
->work_begin_exit_count
) {
657 // If a certain number of work items started, exit simulation
659 exitSimLoop("work started count reach");
662 if (cpuId
== params
->work_begin_cpu_id_exit
) {
664 // If work started on the cpu id specified, exit simulation
666 exitSimLoop("work started on specific cpu");
672 // This function is executed when annotated work items end. Depending on
673 // what the user specified at the command line, the simulation may exit and/or
674 // take a checkpoint when a certain work item ends.
677 workend(ThreadContext
*tc
, uint64_t workid
, uint64_t threadid
)
679 DPRINTF(PseudoInst
, "PseudoInst::workend(%i, %i)\n", workid
, threadid
);
680 System
*sys
= tc
->getSystemPtr();
681 const System::Params
*params
= sys
->params();
683 if (params
->exit_on_work_items
) {
684 exitSimLoop("workend", static_cast<int>(workid
));
688 DPRINTF(WorkItems
, "Work End workid: %d, threadid %d\n", workid
, threadid
);
689 tc
->getCpuPtr()->workItemEnd();
690 sys
->workItemEnd(threadid
, workid
);
693 // If specified, determine if this is the specific work item the user
696 if (params
->work_item_id
== -1 || params
->work_item_id
== workid
) {
698 uint64_t systemWorkEndCount
= sys
->incWorkItemsEnd();
699 int cpuId
= tc
->getCpuPtr()->cpuId();
701 if (params
->work_cpus_ckpt_count
!= 0 &&
702 sys
->markWorkItem(cpuId
) >= params
->work_cpus_ckpt_count
) {
704 // If active cpus equals checkpoint count, create checkpoint
706 exitSimLoop("checkpoint");
709 if (params
->work_end_ckpt_count
!= 0 &&
710 systemWorkEndCount
== params
->work_end_ckpt_count
) {
712 // If total work items completed equals checkpoint count, create
715 exitSimLoop("checkpoint");
718 if (params
->work_end_exit_count
!= 0 &&
719 systemWorkEndCount
== params
->work_end_exit_count
) {
721 // If total work items completed equals exit count, exit simulation
723 exitSimLoop("work items exit count reached");
728 } // namespace PseudoInst