47 #include "debug/Semihosting.hh" 51 #include "params/ArmSemihosting.hh" 57 { 0x01, {
"SYS_OPEN", &ArmSemihosting::callOpen, 3, 3 } },
58 { 0x02, {
"SYS_CLOSE", &ArmSemihosting::callClose, 1, 1 } },
62 { 0x03, {
"SYS_WRITEC", &ArmSemihosting::callWriteC, 0, 0 } },
63 { 0x04, {
"SYS_WRITE0", &ArmSemihosting::callWrite0, 1, 1 } },
65 { 0x05, {
"SYS_WRITE", &ArmSemihosting::callWrite, 3, 3 } },
66 { 0x06, {
"SYS_READ", &ArmSemihosting::callRead, 3, 3 } },
67 { 0x07, {
"SYS_READC", &ArmSemihosting::callReadC, 0, 0 } },
68 { 0x08, {
"SYS_ISERROR", &ArmSemihosting::callIsError, 1, 1 } },
69 { 0x09, {
"SYS_ISTTY", &ArmSemihosting::callIsTTY, 1, 1 } },
70 { 0x0A, {
"SYS_SEEK", &ArmSemihosting::callSeek, 2, 2 } },
71 { 0x0C, {
"SYS_FLEN", &ArmSemihosting::callFLen, 1, 1 } },
72 { 0x0D, {
"SYS_TMPNAM", &ArmSemihosting::callTmpNam, 3, 3 } },
73 { 0x0E, {
"SYS_REMOVE", &ArmSemihosting::callRemove, 2, 2} },
74 { 0x0F, {
"SYS_RENAME", &ArmSemihosting::callRename, 4, 4} },
75 { 0x10, {
"SYS_CLOCK", &ArmSemihosting::callClock, 0, 0} },
76 { 0x11, {
"SYS_TIME", &ArmSemihosting::callTime, 0, 0} },
77 { 0x12, {
"SYS_SYSTEM", &ArmSemihosting::callSystem, 2, 2} },
78 { 0x13, {
"SYS_ERRNO", &ArmSemihosting::callErrno, 0, 0 } },
79 { 0x15, {
"SYS_GET_CMDLINE", &ArmSemihosting::callGetCmdLine, 2, 2} },
80 { 0x16, {
"SYS_HEAPINFO", &ArmSemihosting::callHeapInfo, 1, 1} },
83 { 0x18, {
"SYS_EXIT", &ArmSemihosting::callExit, 0, 2 } },
84 { 0x20, {
"SYS_EXIT_EXTENDED", &ArmSemihosting::callExitExtended, 2, 2 } },
86 { 0x30, {
"SYS_ELAPSED", &ArmSemihosting::callElapsed, 0, 0 } },
87 { 0x31, {
"SYS_TICKFREQ", &ArmSemihosting::callTickFreq, 0, 0 } },
91 "r",
"rb",
"r+",
"r+b",
92 "w",
"wb",
"w+",
"w+b",
93 "a",
"ab",
"a+",
"a+b",
97 { 0x20000,
"semi:ADP_Stopped_BranchThroughZero" },
98 { 0x20001,
"semi:ADP_Stopped_UndefinedInstr" },
99 { 0x20002,
"semi:ADP_Stopped_SoftwareInterrupt" },
100 { 0x20003,
"semi:ADP_Stopped_PrefetchAbort" },
101 { 0x20004,
"semi:ADP_Stopped_DataAbort" },
102 { 0x20005,
"semi:ADP_Stopped_AddressException" },
103 { 0x20006,
"semi:ADP_Stopped_IRQ" },
104 { 0x20007,
"semi:ADP_Stopped_FIQ" },
106 { 0x20020,
"semi:ADP_Stopped_BreakPoint" },
107 { 0x20021,
"semi:ADP_Stopped_WatchPoint" },
108 { 0x20022,
"semi:ADP_Stopped_StepComplete" },
109 { 0x20023,
"semi:ADP_Stopped_RunTimeErrorUnknown" },
110 { 0x20024,
"semi:ADP_Stopped_InternalError" },
111 { 0x20025,
"semi:ADP_Stopped_UserInterruption" },
112 { 0x20026,
"semi:ADP_Stopped_ApplicationExit" },
113 { 0x20027,
"semi:ADP_Stopped_StackOverflow" },
114 { 0x20028,
"semi:ADP_Stopped_DivisionByZero" },
115 { 0x20029,
"semi:ADP_Stopped_DivisionByZero" },
120 0x53, 0x48, 0x46, 0x42,
128 {
"stdout", ::stdout},
130 {
"stderr", ::stderr},
135 cmdLine(p->cmd_line),
136 memReserve(p->mem_reserve),
137 stackSize(p->stack_size),
138 timeBase([p]{
struct tm t = p->time;
return mkutctime(&t); }()),
142 p->files_root_dir.back() !=
'/' ?
143 p->files_root_dir +
'/' :
p->files_root_dir),
151 files.push_back(
nullptr);
154 inform(
"Semihosting: Shifting elapsed ticks by %i bits.",
163 warn(
"Unknown aarch64 semihosting call: op = 0x%x, param = 0x%x",
168 warn(
"Unimplemented aarch64 semihosting call: " 169 "%s (op = 0x%x, param = 0x%x)",
170 call->
name, op, param);
179 DPRINTF(Semihosting,
"Semihosting call64: %s(0x%x)\n", call->
name, param);
182 argv[
i + 1] = proxy.
read<uint64_t>(param +
i * 8, endian);
183 DPRINTF(Semihosting,
"\t: 0x%x\n", argv[i + 1]);
186 auto ret_errno = (this->*call->
call)(tc,
true, argv);
188 DPRINTF(Semihosting,
"\t ->: 0x%x, %i\n",
189 ret_errno.first, ret_errno.second);
190 return ret_errno.first;
198 warn(
"Unknown aarch32 semihosting call: op = 0x%x, param = 0x%x",
203 warn(
"Unimplemented aarch32 semihosting call: " 204 "%s (op = 0x%x, param = 0x%x)",
205 call->
name, op, param);
214 DPRINTF(Semihosting,
"Semihosting call32: %s(0x%x)\n", call->
name, param);
217 argv[
i + 1] = proxy.
read<uint32_t>(param +
i * 4, endian);
218 DPRINTF(Semihosting,
"\t: 0x%x\n", argv[i + 1]);
221 auto ret_errno = (this->*call->
call)(tc,
false, argv);
223 DPRINTF(Semihosting,
"\t ->: 0x%x, %i\n",
224 ret_errno.first, ret_errno.second);
225 return ret_errno.first;
234 for (
int i = 0;
i <
files.size();
i++) {
249 paramIn(cp,
"num_files", num_files);
250 files.resize(num_files);
251 for (
int i = 0;
i < num_files;
i++)
280 return std::string(buf.data());
287 const Addr name_base = argv[1];
289 const Addr name_size = argv[3];
291 DPRINTF(Semihosting,
"Semihosting SYS_OPEN(0x%x, %i[%s], %i)\n",
292 name_base, argv[2], mode ? mode :
"-", name_size);
293 if (!mode || !name_base)
296 std::string fname =
readString(tc, name_base, name_size);
297 if (!fname.empty() && fname.front() !=
'/')
300 std::unique_ptr<ArmSemihosting::FileBase> file =
302 int64_t ret = file->open();
303 DPRINTF(Semihosting,
"Semihosting SYS_OPEN(\"%s\", %i[%s]): %i\n",
304 fname, argv[2], mode, ret);
308 files.push_back(std::move(file));
317 if (argv[1] >
files.size()) {
318 DPRINTF(Semihosting,
"Semihosting SYS_CLOSE(%i): Illegal file\n");
322 std::unique_ptr<FileBase> &file =
files[argv[1]];
323 int64_t error = file->close();
324 DPRINTF(Semihosting,
"Semihosting SYS_CLOSE(%i[%s]): %i\n",
325 argv[1], file->fileName(), error);
331 files[argv[1]].reset();
342 DPRINTF(Semihosting,
"Semihosting SYS_WRITEC('%c')\n", c);
352 DPRINTF(Semihosting,
"Semihosting SYS_WRITE0(...)\n");
369 if (argv[1] >
files.size() || !
files[argv[1]])
375 int64_t ret =
files[argv[1]]->write(buffer.data(), buffer.size());
390 if (argv[1] >
files.size() || !
files[argv[1]])
394 int64_t ret =
files[argv[1]]->read(buffer.data(), buffer.size());
398 panic_if(ret > buffer.size(),
"Read longer than buffer size.");
403 return retOK(argv[3] - ret);
411 return retOK((
char)std::cin.
get());
420 const int64_t
status = (int64_t)(
aarch64 ? argv[1] :sext<32>(argv[1]));
422 return retOK(status < 0 ? 1 : 0);
429 if (argv[1] >
files.size() || !
files[argv[1]])
432 int64_t ret =
files[argv[1]]->isTTY();
436 return retOK(ret ? 1 : 0);
444 if (argv[1] >
files.size() || !
files[argv[1]])
447 int64_t ret =
files[argv[1]]->seek(argv[2]);
459 if (argv[1] >
files.size() || !
files[argv[1]])
462 int64_t ret =
files[argv[1]]->flen();
474 const Addr guest_buf = argv[1];
476 const uint64_t max_len = argv[3];
479 char *
path = tmpnam(buf.data());
483 const size_t path_len = strlen(path);
484 if (path_len >= max_len)
495 std::string fname =
readString(tc, argv[1], argv[2]);
497 if (
remove(fname.c_str()) != 0) {
508 std::string from =
readString(tc, argv[1], argv[2]);
509 std::string to =
readString(tc, argv[3], argv[4]);
511 if (rename(from.c_str(), to.c_str()) != 0) {
536 const std::string cmd =
readString(tc, argv[1], argv[2]);
537 warn(
"Semihosting: SYS_SYSTEM not implemented. Guest tried to run: %s\n",
555 if (
cmdLine.size() + 1 < argv[2]) {
561 proxy.
write<uint64_t>(argv[0] + 1 * 8,
cmdLine.size(), endian);
563 proxy.
write<uint32_t>(argv[0] + 1 * 4,
cmdLine.size(), endian);
576 fatal_if(memories.size() < 1,
"No memories reported from System");
577 warn_if(memories.size() > 1,
"Multiple physical memory ranges available. " 578 "Using first range heap/stack.");
581 Addr mem_end = memory.end();
585 const Addr phys_max = (1
ULL << 32) - 1;
587 "Physical memory out of range for a 32-bit guest.");
588 if (mem_end > phys_max) {
589 warn(
"Some physical memory out of range for a 32-bit guest.");
595 "Physical memory too small to fit desired stack and a heap.");
597 const Addr heap_base = mem_start;
599 const Addr stack_base = (mem_end + 1) & ~0x7ULL;
600 const Addr stack_limit = heap_limit;
603 inform(
"Reporting heap/stack info to guest:\n" 604 "\tHeap base: 0x%x\n" 605 "\tHeap limit: 0x%x\n" 606 "\tStack base: 0x%x\n" 607 "\tStack limit: 0x%x\n",
608 heap_base, heap_limit, stack_base, stack_limit);
614 proxy.
write<uint64_t>(base + 0 * 8, heap_base, endian);
615 proxy.
write<uint64_t>(base + 1 * 8, heap_limit, endian);
616 proxy.
write<uint64_t>(base + 2 * 8, stack_base, endian);
617 proxy.
write<uint64_t>(base + 3 * 8, stack_limit, endian);
619 proxy.
write<uint32_t>(base + 0 * 4, heap_base, endian);
620 proxy.
write<uint32_t>(base + 1 * 4, heap_limit, endian);
621 proxy.
write<uint32_t>(base + 2 * 4, stack_base, endian);
622 proxy.
write<uint32_t>(base + 3 * 4, stack_limit, endian);
671 proxy.
write<uint64_t>(argv[0], tick, endian);
673 proxy.
write<uint32_t>(argv[0] + 0 * 4, tick, endian);
674 proxy.
write<uint32_t>(argv[0] + 1 * 4, tick >> 32, endian);
691 auto it =
calls.find(op);
692 if (it ==
calls.end())
701 const std::string &
name,
const char *
mode)
705 FILE *
f = fopen(name.c_str(),
mode);
707 fatal(
"Failed to open %s (%s): %s\n",
708 stream_name, name, strerror(errno));
716 std::unique_ptr<ArmSemihosting::FileBase>
720 std::unique_ptr<FileBase> file;
721 if (fname ==
":semihosting-features") {
724 file.reset(
new File(parent, fname.c_str(),
mode));
730 std::unique_ptr<ArmSemihosting::FileBase>
734 std::unique_ptr<FileBase> file;
741 std::string fname,
mode;
744 file = create(parent, fname, mode.c_str());
746 file->unserialize(cp);
835 const char *
_name,
const char *_perms)
850 panic_if(
file,
"Trying to open an already open file.\n");
852 if (
_name ==
":tt") {
853 if (
mode[0] ==
'r') {
855 }
else if (
mode[0] ==
'w') {
857 }
else if (
mode[0] ==
'a') {
860 warn(
"Unknown file mode for the ':tt' special file");
864 std::string real_mode(this->
mode);
867 if (in_cpt && real_mode[0] ==
'w')
870 file = fopen(
_name.c_str(), real_mode.c_str());
873 return file ? 0 : -errno;
879 panic_if(!
file,
"Trying to close an already closed file.\n");
902 size_t ret = fread(buffer, 1, size,
file);
906 return ferror(
file) ? -EINVAL : 0;
918 size_t ret = fwrite(buffer, 1, size,
file);
934 if (fseek(
file, _pos, SEEK_SET) == 0)
944 long pos = ftell(
file);
948 if (fseek(
file, 0, SEEK_END) != 0)
955 if (fseek(
file, pos, SEEK_SET) != 0)
969 panic_if(pos < 0,
"Failed to get file position.");
986 if (fseek(
file, pos, SEEK_SET) != 0) {
987 fatal(
"Failed seek to current position (%i) in '%s'", pos,
_name);
994 ArmSemihostingParams::create()
Implementation of the ':semihosting-features' magic file.
virtual System * getSystemPtr()=0
int64_t flen() override
Get the length of a file in bytes.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
#define fatal(...)
This implements a cprintf based fatal() function.
RetErrno(ArmSemihosting::* call)(ThreadContext *tc, bool aarch64, std::vector< uint64_t > &argv)
Pointer to call implementation.
void serialize(CheckpointOut &cp) const override
Serialize an object.
void serialize(CheckpointOut &cp) const override
Serialize an object.
bool isTTY() const override
Check if a file corresponds to a TTY device.
void serialize(CheckpointOut &cp) const override
Serialize an object.
static FILE * getSTDIO(const char *stream_name, const std::string &name, const char *mode)
MasterPort & getSystemPort()
Get a reference to the system port that can be used by non-structural simulation objects like process...
static std::stack< std::string > path
ArmSemihosting(const ArmSemihostingParams *p)
static std::unique_ptr< FileBase > create(ArmSemihosting &parent, const std::string &fname, const char *mode)
std::vector< std::unique_ptr< FileBase > > files
static const std::map< const std::string, FILE * > stdioMap
void unserialize(CheckpointIn &cp) override
Unserialize an object.
void serialize(CheckpointOut &cp) const override
Serialize an object.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
int argc64
Number of aarch32 arguments to read from guest memory.
static RetErrno retError(SemiErrno e)
PortProxy & physProxy(ThreadContext *tc)
T read(Addr address) const
Read sizeof(T) bytes from address and return as object T.
const char * name
Call name.
File(ArmSemihosting &_parent, const char *name, const char *mode)
double s
These variables equal the number of ticks in the unit of time they're named after in a double...
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
virtual int64_t read(uint8_t *buffer, uint64_t size)
Read data from file.
ThreadContext is the external interface to all thread state for anything outside of the CPU...
virtual int64_t seek(uint64_t pos)
Seek to an absolute position in the file.
static const std::vector< const char * > fmodes
const std::string cmdLine
int64_t write(const uint8_t *buffer, uint64_t size) override
Write data to file.
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
#define UNSERIALIZE_SCALAR(scalar)
virtual PortProxy & getPhysProxy()=0
Tick curTick()
The current simulated tick.
int64_t seek(uint64_t pos) override
Seek to an absolute position in the file.
std::string csprintf(const char *format, const Args &...args)
Internal state for open files.
int64_t seek(uint64_t pos) override
Seek to an absolute position in the file.
ByteOrder byteOrder(ThreadContext *tc)
void writeBlob(Addr addr, const void *p, int size) const
Same as tryWriteBlob, but insists on success.
Semihosting for AArch32 and AArch64.
uint64_t semiTick(Tick tick) const
int64_t read(uint8_t *buffer, uint64_t size) override
Read data from file.
void paramOut(CheckpointOut &cp, const string &name, ExtMachInst const &machInst)
This object is a proxy for a structural port, to be used for debug accesses to secure memory...
uint32_t call32(ThreadContext *tc, uint32_t op, uint32_t param)
Perform an Arm Semihosting call from aarch32 code.
static const std::map< uint32_t, SemiCall > calls
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
AddrRangeList getConfAddrRanges() const
Get the memory ranges for all memories that are to be reported to the configuration table...
int64_t openImpl(bool unserialize)
std::unique_ptr< PortProxy > physProxyS
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
std::pair< uint64_t, SemiErrno > RetErrno
int64_t read(uint8_t *buffer, uint64_t size) override
Read data from file.
The physical memory encapsulates all memories in the system and provides basic functionality for acce...
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
#define ULL(N)
uint64_t constant
virtual const std::string name() const
std::string readString(ThreadContext *tc, Addr ptr, size_t len)
static RetErrno retOK(uint64_t r)
void readBlob(Addr addr, void *p, int size) const
Higher level interfaces based on the above.
PortProxy Object Declaration.
#define SERIALIZE_SCALAR(scalar)
static const std::vector< uint8_t > features
This object is a proxy for a port or other object which implements the functional response protocol...
void unserialize(CheckpointIn &cp) override
Unserialize an object.
void exitSimLoop(const std::string &message, int exit_code, Tick when, Tick repeat, bool serialize)
Schedule an event to exit the simulation loop (returning to Python) at the end of the current cycle (...
static const std::map< uint64_t, const char * > exitCodes
std::ostream CheckpointOut
static const SemiCall * getCall(uint32_t op, bool aarch64)
bool implemented64() const
Is call implemented in aarch64?
void semiExit(uint64_t code, uint64_t subcode)
bool sectionExists(const std::string §ion)
static const std::string & currentSection()
Get the fully-qualified name of the active section.
PhysicalMemory & getPhysMem()
Get a pointer to access the physical memory of the system.
const unsigned tickShift
Number of bits to right shift gem5 ticks to fit in a uint32_t.
void paramIn(CheckpointIn &cp, const string &name, ExtMachInst &machInst)
const time_t timeBase
Base time when the simulation started.
unsigned calcTickShift() const
void write(Addr address, const T &data) const
Write object T to address.
bool implemented32() const
Is call implemented in aarch32?
int argc32
Number of aarch32 arguments to read from guest memory.
Semihosting call information structure.
bool inSecureState(ThreadContext *tc)
virtual int64_t write(const uint8_t *buffer, uint64_t size)
Write data to file.
Scoped checkpoint section helper class.
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
virtual int64_t flen()
Get the length of a file in bytes.
time_t mkutctime(struct tm *time)
FileFeatures(ArmSemihosting &_parent, const char *name, const char *mode)
Abstract superclass for simulation objects.
uint64_t call64(ThreadContext *tc, uint32_t op, uint64_t param)
Perform an Arm Semihosting call from aarch64 code.
int64_t close() override
Close the file.
unsigned int cacheLineSize() const
Get the cache line size of the system.