misc: Standardize the way create() constructs SimObjects.
[gem5.git] / src / arch / arm / semihosting.cc
1 /*
2 * Copyright (c) 2018, 2019 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 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include "arch/arm/semihosting.hh"
39
40 #include <unistd.h>
41
42 #include <cerrno>
43 #include <cstdio>
44
45 #include "arch/arm/utility.hh"
46 #include "base/logging.hh"
47 #include "base/output.hh"
48 #include "base/time.hh"
49 #include "debug/Semihosting.hh"
50 #include "dev/serial/serial.hh"
51 #include "mem/physical.hh"
52 #include "mem/se_translating_port_proxy.hh"
53 #include "mem/translating_port_proxy.hh"
54 #include "params/ArmSemihosting.hh"
55 #include "sim/byteswap.hh"
56 #include "sim/full_system.hh"
57 #include "sim/pseudo_inst.hh"
58 #include "sim/sim_exit.hh"
59 #include "sim/system.hh"
60
61 const std::map<uint32_t, ArmSemihosting::SemiCall> ArmSemihosting::calls{
62 { SYS_OPEN, { "SYS_OPEN", &ArmSemihosting::callOpen } },
63 { SYS_CLOSE, { "SYS_CLOSE", &ArmSemihosting::callClose } },
64 { SYS_WRITEC, { "SYS_WRITEC", &ArmSemihosting::callWriteC } },
65 { SYS_WRITE0, { "SYS_WRITE0", &ArmSemihosting::callWrite0 } },
66 { SYS_WRITE, { "SYS_WRITE", &ArmSemihosting::callWrite } },
67 { SYS_READ, { "SYS_READ", &ArmSemihosting::callRead } },
68 { SYS_READC, { "SYS_READC", &ArmSemihosting::callReadC } },
69 { SYS_ISERROR, { "SYS_ISERROR", &ArmSemihosting::callIsError } },
70 { SYS_ISTTY, { "SYS_ISTTY", &ArmSemihosting::callIsTTY } },
71 { SYS_SEEK, { "SYS_SEEK", &ArmSemihosting::callSeek } },
72 { SYS_FLEN, { "SYS_FLEN", &ArmSemihosting::callFLen } },
73 { SYS_TMPNAM, { "SYS_TMPNAM", &ArmSemihosting::callTmpNam } },
74 { SYS_REMOVE, { "SYS_REMOVE", &ArmSemihosting::callRemove } },
75 { SYS_RENAME, { "SYS_RENAME", &ArmSemihosting::callRename } },
76 { SYS_CLOCK, { "SYS_CLOCK", &ArmSemihosting::callClock } },
77 { SYS_TIME, { "SYS_TIME", &ArmSemihosting::callTime } },
78 { SYS_SYSTEM, { "SYS_SYSTEM", &ArmSemihosting::callSystem } },
79 { SYS_ERRNO, { "SYS_ERRNO", &ArmSemihosting::callErrno } },
80 { SYS_GET_CMDLINE,
81 { "SYS_GET_CMDLINE", &ArmSemihosting::callGetCmdLine } },
82 { SYS_HEAPINFO, { "SYS_HEAPINFO", &ArmSemihosting::callHeapInfo32,
83 &ArmSemihosting::callHeapInfo64 } },
84
85 { SYS_EXIT, { "SYS_EXIT", &ArmSemihosting::callExit32,
86 &ArmSemihosting::callExit64} },
87 { SYS_EXIT_EXTENDED,
88 { "SYS_EXIT_EXTENDED", &ArmSemihosting::callExitExtended } },
89
90 { SYS_ELAPSED, { "SYS_ELAPSED", &ArmSemihosting::callElapsed32,
91 &ArmSemihosting::callElapsed64 } },
92 { SYS_TICKFREQ, { "SYS_TICKFREQ", &ArmSemihosting::callTickFreq } },
93 { SYS_GEM5_PSEUDO_OP,
94 { "SYS_GEM5_PSEUDO_OP", &ArmSemihosting::callGem5PseudoOp32,
95 &ArmSemihosting::callGem5PseudoOp64 } },
96 };
97
98 const std::vector<const char *> ArmSemihosting::fmodes{
99 "r", "rb", "r+", "r+b",
100 "w", "wb", "w+", "w+b",
101 "a", "ab", "a+", "a+b",
102 };
103
104 const std::map<uint64_t, const char *> ArmSemihosting::exitCodes{
105 { 0x20000, "semi:ADP_Stopped_BranchThroughZero" },
106 { 0x20001, "semi:ADP_Stopped_UndefinedInstr" },
107 { 0x20002, "semi:ADP_Stopped_SoftwareInterrupt" },
108 { 0x20003, "semi:ADP_Stopped_PrefetchAbort" },
109 { 0x20004, "semi:ADP_Stopped_DataAbort" },
110 { 0x20005, "semi:ADP_Stopped_AddressException" },
111 { 0x20006, "semi:ADP_Stopped_IRQ" },
112 { 0x20007, "semi:ADP_Stopped_FIQ" },
113
114 { 0x20020, "semi:ADP_Stopped_BreakPoint" },
115 { 0x20021, "semi:ADP_Stopped_WatchPoint" },
116 { 0x20022, "semi:ADP_Stopped_StepComplete" },
117 { 0x20023, "semi:ADP_Stopped_RunTimeErrorUnknown" },
118 { 0x20024, "semi:ADP_Stopped_InternalError" },
119 { 0x20025, "semi:ADP_Stopped_UserInterruption" },
120 { 0x20026, "semi:ADP_Stopped_ApplicationExit" },
121 { 0x20027, "semi:ADP_Stopped_StackOverflow" },
122 { 0x20028, "semi:ADP_Stopped_DivisionByZero" },
123 { 0x20029, "semi:ADP_Stopped_DivisionByZero" },
124 };
125
126
127 const std::vector<uint8_t> ArmSemihosting::features{
128 0x53, 0x48, 0x46, 0x42, // Magic
129 0x3, // EXT_EXIT_EXTENDED, EXT_STDOUT_STDERR
130 };
131
132 const std::map<const std::string, FILE *> ArmSemihosting::stdioMap{
133 {"cin", ::stdin},
134 {"stdin", ::stdin},
135 {"cout", ::stdout},
136 {"stdout", ::stdout},
137 {"cerr", ::stderr},
138 {"stderr", ::stderr},
139 };
140
141 ArmSemihosting::ArmSemihosting(const ArmSemihostingParams &p)
142 : SimObject(p),
143 cmdLine(p.cmd_line),
144 memReserve(p.mem_reserve),
145 stackSize(p.stack_size),
146 timeBase([p]{ struct tm t = p.time; return mkutctime(&t); }()),
147 tickShift(calcTickShift()),
148 semiErrno(0),
149 filesRootDir(!p.files_root_dir.empty() &&
150 p.files_root_dir.back() != '/' ?
151 p.files_root_dir + '/' : p.files_root_dir),
152 stdin(getSTDIO("stdin", p.stdin, "r")),
153 stdout(getSTDIO("stdout", p.stdout, "w")),
154 stderr(p.stderr == p.stdout ?
155 stdout : getSTDIO("stderr", p.stderr, "w"))
156 {
157 // Create an empty place-holder file for position 0 as semi-hosting
158 // calls typically expect non-zero file handles.
159 files.push_back(nullptr);
160
161 if (tickShift > 0)
162 inform("Semihosting: Shifting elapsed ticks by %i bits.",
163 tickShift);
164 }
165
166 bool
167 ArmSemihosting::call64(ThreadContext *tc, bool gem5_ops)
168 {
169 RegVal op = tc->readIntReg(ArmISA::INTREG_X0 & mask(32));
170 if (op > MaxStandardOp && !gem5_ops) {
171 unrecognizedCall<Abi64>(
172 tc, "Gem5 semihosting op (0x%x) disabled from here.", op);
173 return false;
174 }
175
176 auto it = calls.find(op);
177 if (it == calls.end()) {
178 unrecognizedCall<Abi64>(
179 tc, "Unknown aarch64 semihosting call: op = 0x%x", op);
180 return false;
181 }
182 const SemiCall &call = it->second;
183
184 DPRINTF(Semihosting, "Semihosting call64: %s\n", call.dump64(tc));
185 auto err = call.call64(this, tc);
186 semiErrno = err.second;
187 DPRINTF(Semihosting, "\t ->: 0x%x, %i\n", err.first, err.second);
188
189 return true;
190 }
191
192 bool
193 ArmSemihosting::call32(ThreadContext *tc, bool gem5_ops)
194 {
195 RegVal op = tc->readIntReg(ArmISA::INTREG_R0);
196 if (op > MaxStandardOp && !gem5_ops) {
197 unrecognizedCall<Abi32>(
198 tc, "Gem5 semihosting op (0x%x) disabled from here.", op);
199 return false;
200 }
201
202 auto it = calls.find(op);
203 if (it == calls.end()) {
204 unrecognizedCall<Abi32>(
205 tc, "Unknown aarch32 semihosting call: op = 0x%x", op);
206 return false;
207 }
208 const SemiCall &call = it->second;
209
210 DPRINTF(Semihosting, "Semihosting call32: %s\n", call.dump32(tc));
211 auto err = call.call32(this, tc);
212 semiErrno = err.second;
213 DPRINTF(Semihosting, "\t ->: 0x%x, %i\n", err.first, err.second);
214
215 return true;
216 }
217
218 void
219 ArmSemihosting::serialize(CheckpointOut &cp) const
220 {
221 SERIALIZE_SCALAR(semiErrno);
222
223 paramOut(cp, "num_files", files.size());
224 for (int i = 0; i < files.size(); i++) {
225 // File closed?
226 if (!files[i])
227 continue;
228
229 files[i]->serializeSection(cp, csprintf("file%i", i));
230 }
231 }
232
233 void
234 ArmSemihosting::unserialize(CheckpointIn &cp)
235 {
236 UNSERIALIZE_SCALAR(semiErrno);
237
238 size_t num_files;
239 paramIn(cp, "num_files", num_files);
240 files.resize(num_files);
241 for (int i = 0; i < num_files; i++)
242 files[i] = FileBase::create(*this, cp, csprintf("file%i", i));
243 }
244
245 PortProxy &
246 ArmSemihosting::portProxy(ThreadContext *tc)
247 {
248 static std::unique_ptr<PortProxy> port_proxy_s;
249 static System *secure_sys = nullptr;
250
251 if (ArmISA::isSecure(tc)) {
252 System *sys = tc->getSystemPtr();
253 if (sys != secure_sys) {
254 if (FullSystem) {
255 port_proxy_s.reset(
256 new TranslatingPortProxy(tc, Request::SECURE));
257 } else {
258 port_proxy_s.reset(
259 new SETranslatingPortProxy(
260 tc, SETranslatingPortProxy::NextPage,
261 Request::SECURE));
262 }
263 }
264 secure_sys = sys;
265 return *port_proxy_s;
266 } else {
267 return tc->getVirtProxy();
268 }
269 }
270
271
272 std::string
273 ArmSemihosting::readString(ThreadContext *tc, Addr ptr, size_t len)
274 {
275 std::vector<char> buf(len + 1);
276
277 buf[len] = '\0';
278 portProxy(tc).readBlob(ptr, buf.data(), len);
279
280 return std::string(buf.data());
281 }
282
283 ArmSemihosting::RetErrno
284 ArmSemihosting::callOpen(ThreadContext *tc, const Addr name_base,
285 int fmode, size_t name_size)
286 {
287 const char *mode = fmode < fmodes.size() ? fmodes[fmode] : nullptr;
288
289 DPRINTF(Semihosting, "Semihosting SYS_OPEN(0x%x, %i[%s], %i)\n",
290 name_base, fmode, mode ? mode : "-", name_size);
291 if (!mode || !name_base)
292 return retError(EINVAL);
293
294 std::string fname = readString(tc, name_base, name_size);
295 if (!fname.empty() && fname.front() != '/')
296 fname = filesRootDir + fname;
297
298 std::unique_ptr<ArmSemihosting::FileBase> file =
299 FileBase::create(*this, fname, mode);
300 int64_t ret = file->open();
301 DPRINTF(Semihosting, "Semihosting SYS_OPEN(\"%s\", %i[%s]): %i\n",
302 fname, fmode, mode, ret);
303 if (ret < 0) {
304 return retError(-ret);
305 } else {
306 files.push_back(std::move(file));
307 return retOK(files.size() - 1);
308 }
309 }
310
311 ArmSemihosting::RetErrno
312 ArmSemihosting::callClose(ThreadContext *tc, Handle handle)
313 {
314 if (handle > files.size()) {
315 DPRINTF(Semihosting, "Semihosting SYS_CLOSE(%i): Illegal file\n");
316 return retError(EBADF);
317 }
318
319 std::unique_ptr<FileBase> &file = files[handle];
320 int64_t error = file->close();
321 DPRINTF(Semihosting, "Semihosting SYS_CLOSE(%i[%s]): %i\n",
322 handle, file->fileName(), error);
323 if (error < 0) {
324 return retError(-error);
325 } else {
326 // Zap the pointer and free the entry in the file table as
327 // well.
328 files[handle].reset();
329 return retOK(0);
330 }
331 }
332
333 ArmSemihosting::RetErrno
334 ArmSemihosting::callWriteC(ThreadContext *tc, InPlaceArg arg)
335 {
336 const char c = portProxy(tc).read<char>(arg.addr);
337
338 DPRINTF(Semihosting, "Semihosting SYS_WRITEC('%c')\n", c);
339 std::cout.put(c);
340
341 return retOK(0);
342 }
343
344 ArmSemihosting::RetErrno
345 ArmSemihosting::callWrite0(ThreadContext *tc, InPlaceArg arg)
346 {
347 DPRINTF(Semihosting, "Semihosting SYS_WRITE0(...)\n");
348 PortProxy &proxy = portProxy(tc);
349 std::string str;
350 proxy.readString(str, arg.addr);
351 std::cout.write(str.c_str(), str.size());
352
353 return retOK(0);
354 }
355
356 ArmSemihosting::RetErrno
357 ArmSemihosting::callWrite(ThreadContext *tc, Handle handle, Addr addr,
358 size_t size)
359 {
360 if (handle > files.size() || !files[handle])
361 return RetErrno(size, EBADF);
362
363 std::vector<uint8_t> buffer(size);
364 portProxy(tc).readBlob(addr, buffer.data(), buffer.size());
365
366 int64_t ret = files[handle]->write(buffer.data(), buffer.size());
367 if (ret < 0) {
368 // No bytes written (we're returning the number of bytes not
369 // written)
370 return RetErrno(size, -ret);
371 } else {
372 // Return the number of bytes not written
373 return RetErrno(size - ret, 0);
374 }
375 }
376
377 ArmSemihosting::RetErrno
378 ArmSemihosting::callRead(ThreadContext *tc, Handle handle, Addr addr,
379 size_t size)
380 {
381 if (handle > files.size() || !files[handle])
382 return RetErrno(size, EBADF);
383
384 std::vector<uint8_t> buffer(size);
385 int64_t ret = files[handle]->read(buffer.data(), buffer.size());
386 if (ret < 0) {
387 return RetErrno(size, -ret);
388 } else {
389 panic_if(ret > buffer.size(), "Read longer than buffer size.");
390
391 portProxy(tc).writeBlob(addr, buffer.data(), ret);
392
393 // Return the number of bytes not written
394 return retOK(size - ret);
395 }
396 }
397
398 ArmSemihosting::RetErrno
399 ArmSemihosting::callReadC(ThreadContext *tc)
400 {
401 return retOK((char)std::cin.get());
402 }
403
404 ArmSemihosting::RetErrno
405 ArmSemihosting::callIsError(ThreadContext *tc, int64_t status)
406 {
407 return retOK(status < 0 ? 1 : 0);
408 }
409
410 ArmSemihosting::RetErrno
411 ArmSemihosting::callIsTTY(ThreadContext *tc, Handle handle)
412 {
413 if (handle > files.size() || !files[handle])
414 return retError(EBADF);
415
416 int64_t ret = files[handle]->isTTY();
417 if (ret < 0) {
418 return retError(-ret);
419 } else {
420 return retOK(ret ? 1 : 0);
421 }
422 }
423
424 ArmSemihosting::RetErrno
425 ArmSemihosting::callSeek(ThreadContext *tc, Handle handle, uint64_t pos)
426 {
427 if (handle > files.size() || !files[handle])
428 return retError(EBADF);
429
430 int64_t ret = files[handle]->seek(pos);
431 if (ret < 0) {
432 return retError(-ret);
433 } else {
434 return retOK(0);
435 }
436 }
437
438 ArmSemihosting::RetErrno
439 ArmSemihosting::callFLen(ThreadContext *tc, Handle handle)
440 {
441 if (handle > files.size() || !files[handle])
442 return retError(EBADF);
443
444 int64_t ret = files[handle]->flen();
445 if (ret < 0) {
446 return retError(-ret);
447 } else {
448 return retOK(ret);
449 }
450 }
451
452 ArmSemihosting::RetErrno
453 ArmSemihosting::callTmpNam(ThreadContext *tc, Addr addr, uint64_t id,
454 size_t size)
455 {
456 std::string path = "";
457 int64_t unlink_call_ret = 0;
458
459 do {
460 path = simout.resolve(csprintf("%s.tmp%05i", name(), tmpNameIndex++));
461 // remove the (potentially existing) file of the given path
462 unlink_call_ret = unlink(path.c_str());
463 // if the file is busy, find another name
464 } while ((unlink_call_ret < 0) && (errno == EBUSY));
465
466 const size_t path_len = path.length();
467 if (path_len >= size)
468 return retError(ENOSPC);
469
470 portProxy(tc).writeBlob(addr, path.c_str(), path_len + 1);
471 return retOK(0);
472 }
473
474 ArmSemihosting::RetErrno
475 ArmSemihosting::callRemove(ThreadContext *tc, Addr name_base, size_t name_size)
476 {
477 std::string fname = readString(tc, name_base, name_size);
478
479 if (remove(fname.c_str()) != 0) {
480 return retError(errno);
481 } else {
482 return retOK(0);
483 }
484 }
485
486 ArmSemihosting::RetErrno
487 ArmSemihosting::callRename(ThreadContext *tc, Addr from_addr, size_t from_size,
488 Addr to_addr, size_t to_size)
489 {
490 std::string from = readString(tc, from_addr, from_size);
491 std::string to = readString(tc, to_addr, to_size);
492
493 if (rename(from.c_str(), to.c_str()) != 0) {
494 return retError(errno);
495 } else {
496 return retOK(0);
497 }
498 }
499
500 ArmSemihosting::RetErrno
501 ArmSemihosting::callClock(ThreadContext *tc)
502 {
503 return retOK(curTick() / (SimClock::Int::s / 100));
504 }
505
506 ArmSemihosting::RetErrno
507 ArmSemihosting::callTime(ThreadContext *tc)
508 {
509 return retOK(timeBase + round(curTick() / SimClock::Float::s));
510 }
511
512 ArmSemihosting::RetErrno
513 ArmSemihosting::callSystem(ThreadContext *tc, Addr cmd_addr, size_t cmd_size)
514 {
515 const std::string cmd = readString(tc, cmd_addr, cmd_size);
516 warn("Semihosting: SYS_SYSTEM not implemented. Guest tried to run: %s\n",
517 cmd);
518 return retError(EINVAL);
519
520 }
521
522 ArmSemihosting::RetErrno
523 ArmSemihosting::callErrno(ThreadContext *tc)
524 {
525 // Preserve errno by returning it in errno as well.
526 return RetErrno(semiErrno, semiErrno);
527 }
528
529 ArmSemihosting::RetErrno
530 ArmSemihosting::callGetCmdLine(ThreadContext *tc, Addr addr,
531 InPlaceArg size_arg)
532 {
533 PortProxy &proxy = portProxy(tc);
534 ByteOrder endian = ArmISA::byteOrder(tc);
535 size_t size = size_arg.read(tc, endian);
536
537 if (cmdLine.size() + 1 < size) {
538 proxy.writeBlob(addr, cmdLine.c_str(), cmdLine.size() + 1);
539 size_arg.write(tc, cmdLine.size(), endian);
540 return retOK(0);
541 } else {
542 return retError(0);
543 }
544 }
545
546 void
547 ArmSemihosting::gatherHeapInfo(ThreadContext *tc, bool aarch64,
548 Addr &heap_base, Addr &heap_limit,
549 Addr &stack_base, Addr &stack_limit)
550 {
551 const PhysicalMemory &phys = tc->getSystemPtr()->getPhysMem();
552 const AddrRangeList memories = phys.getConfAddrRanges();
553 fatal_if(memories.size() < 1, "No memories reported from System");
554 warn_if(memories.size() > 1, "Multiple physical memory ranges available. "
555 "Using first range heap/stack.");
556 const AddrRange memory = *memories.begin();
557 const Addr mem_start = memory.start() + memReserve;
558 Addr mem_end = memory.end();
559
560 // Make sure that 32-bit guests can access their memory.
561 if (!aarch64) {
562 const Addr phys_max = (1ULL << 32) - 1;
563 panic_if(mem_start > phys_max,
564 "Physical memory out of range for a 32-bit guest.");
565 if (mem_end > phys_max) {
566 warn("Some physical memory out of range for a 32-bit guest.");
567 mem_end = phys_max;
568 }
569 }
570
571 fatal_if(mem_start + stackSize >= mem_end,
572 "Physical memory too small to fit desired stack and a heap.");
573
574 heap_base = mem_start;
575 heap_limit = mem_end - stackSize + 1;
576 stack_base = (mem_end + 1) & ~0x7ULL; // 8 byte stack alignment
577 stack_limit = heap_limit;
578
579 inform("Reporting heap/stack info to guest:\n"
580 "\tHeap base: 0x%x\n"
581 "\tHeap limit: 0x%x\n"
582 "\tStack base: 0x%x\n"
583 "\tStack limit: 0x%x\n",
584 heap_base, heap_limit, stack_base, stack_limit);
585 }
586
587 ArmSemihosting::RetErrno
588 ArmSemihosting::callHeapInfo32(ThreadContext *tc, Addr block_addr)
589 {
590 uint64_t heap_base, heap_limit, stack_base, stack_limit;
591 gatherHeapInfo(tc, false, heap_base, heap_limit, stack_base, stack_limit);
592
593 std::array<uint32_t, 4> block = {{
594 (uint32_t)heap_base, (uint32_t)heap_limit,
595 (uint32_t)stack_base, (uint32_t)stack_limit
596 }};
597 portProxy(tc).write(block_addr, block, ArmISA::byteOrder(tc));
598
599 return retOK(0);
600 }
601
602 ArmSemihosting::RetErrno
603 ArmSemihosting::callHeapInfo64(ThreadContext *tc, Addr block_addr)
604 {
605 uint64_t heap_base, heap_limit, stack_base, stack_limit;
606 gatherHeapInfo(tc, true, heap_base, heap_limit, stack_base, stack_limit);
607
608 std::array<uint64_t, 4> block = {{
609 heap_base, heap_limit, stack_base, stack_limit
610 }};
611 portProxy(tc).write(block_addr, block, ArmISA::byteOrder(tc));
612
613 return retOK(0);
614 }
615
616 ArmSemihosting::RetErrno
617 ArmSemihosting::callExit32(ThreadContext *tc, InPlaceArg code)
618 {
619 semiExit(code.addr, 0);
620 return retOK(0);
621 }
622
623 ArmSemihosting::RetErrno
624 ArmSemihosting::callExit64(ThreadContext *tc, uint64_t code, uint64_t subcode)
625 {
626 semiExit(code, subcode);
627 return retOK(0);
628 }
629
630 ArmSemihosting::RetErrno
631 ArmSemihosting::callExitExtended(ThreadContext *tc,
632 uint64_t code, uint64_t subcode)
633 {
634 semiExit(code, subcode);
635 return retOK(0);
636 }
637
638 void
639 ArmSemihosting::semiExit(uint64_t code, uint64_t subcode)
640 {
641 auto it = exitCodes.find(code);
642 if (it != exitCodes.end()) {
643 exitSimLoop(it->second, subcode);
644 } else {
645 exitSimLoop(csprintf("semi:0x%x", code), subcode);
646 }
647 }
648
649
650 ArmSemihosting::RetErrno
651 ArmSemihosting::callElapsed32(ThreadContext *tc, InPlaceArg low,
652 InPlaceArg high)
653 {
654 ByteOrder endian = ArmISA::byteOrder(tc);
655 uint64_t tick = semiTick(curTick());
656
657 low.write(tc, tick, endian);
658 high.write(tc, tick >> 32, endian);
659
660 return retOK(0);
661 }
662
663
664 ArmSemihosting::RetErrno
665 ArmSemihosting::callElapsed64(ThreadContext *tc, InPlaceArg ticks)
666 {
667 ticks.write(tc, semiTick(curTick()), ArmISA::byteOrder(tc));
668 return retOK(0);
669 }
670
671
672 ArmSemihosting::RetErrno
673 ArmSemihosting::callTickFreq(ThreadContext *tc)
674 {
675 return retOK(semiTick(SimClock::Frequency));
676 }
677
678
679 struct SemiPseudoAbi32 : public ArmSemihosting::Abi32
680 {
681 class State : public ArmSemihosting::Abi32::State
682 {
683 public:
684 State(const ThreadContext *tc) : ArmSemihosting::Abi32::State(tc)
685 {
686 // Use getAddr() to skip the func number in the first slot.
687 getAddr();
688 }
689 };
690 };
691
692 struct SemiPseudoAbi64 : public ArmSemihosting::Abi64
693 {
694 class State : public ArmSemihosting::Abi64::State
695 {
696 public:
697 State(const ThreadContext *tc) : ArmSemihosting::Abi64::State(tc)
698 {
699 // Use getAddr() to skip the func number in the first slot.
700 getAddr();
701 }
702 };
703 };
704
705 namespace GuestABI
706 {
707
708 // Handle arguments the same as for semihosting operations. Skipping the first
709 // slot is handled internally by the State type.
710 template <typename T>
711 struct Argument<SemiPseudoAbi32, T> :
712 public Argument<ArmSemihosting::Abi32, T>
713 {};
714 template <typename T>
715 struct Argument<SemiPseudoAbi64, T> :
716 public Argument<ArmSemihosting::Abi64, T>
717 {};
718
719 } // namespace GuestABI
720
721 ArmSemihosting::RetErrno
722 ArmSemihosting::callGem5PseudoOp32(ThreadContext *tc, uint32_t encoded_func)
723 {
724 uint8_t func;
725 PseudoInst::decodeAddrOffset(encoded_func, func);
726
727 uint64_t ret;
728 if (PseudoInst::pseudoInst<SemiPseudoAbi32>(tc, func, ret))
729 return retOK(ret);
730 else
731 return retError(EINVAL);
732 }
733
734 ArmSemihosting::RetErrno
735 ArmSemihosting::callGem5PseudoOp64(ThreadContext *tc, uint64_t encoded_func)
736 {
737 uint8_t func;
738 PseudoInst::decodeAddrOffset(encoded_func, func);
739
740 uint64_t ret;
741 if (PseudoInst::pseudoInst<SemiPseudoAbi64>(tc, func, ret))
742 return retOK(ret);
743 else
744 return retError(EINVAL);
745 }
746
747 FILE *
748 ArmSemihosting::getSTDIO(const char *stream_name,
749 const std::string &name, const char *mode)
750 {
751 auto it = stdioMap.find(name);
752 if (it == stdioMap.end()) {
753 FILE *f = fopen(name.c_str(), mode);
754 if (!f) {
755 fatal("Failed to open %s (%s): %s\n",
756 stream_name, name, strerror(errno));
757 }
758 return f;
759 } else {
760 return it->second;
761 }
762 }
763
764 std::unique_ptr<ArmSemihosting::FileBase>
765 ArmSemihosting::FileBase::create(
766 ArmSemihosting &parent, const std::string &fname, const char *mode)
767 {
768 std::unique_ptr<FileBase> file;
769 if (fname == ":semihosting-features") {
770 file.reset(new FileFeatures(parent, fname.c_str(), mode));
771 } else {
772 file.reset(new File(parent, fname.c_str(), mode));
773 }
774
775 return file;
776 }
777
778 std::unique_ptr<ArmSemihosting::FileBase>
779 ArmSemihosting::FileBase::create(ArmSemihosting &parent,
780 CheckpointIn &cp, const std::string &sec)
781 {
782 std::unique_ptr<FileBase> file;
783 ScopedCheckpointSection _sec(cp, sec);
784
785 // Was the file open when the checkpoint was created?
786 if (!cp.sectionExists(Serializable::currentSection()))
787 return file;
788
789 std::string fname, mode;
790 paramIn(cp, "name", fname);
791 paramIn(cp, "mode", mode);
792 file = create(parent, fname, mode.c_str());
793 assert(file);
794 file->unserialize(cp);
795
796 return file;
797 }
798
799 void
800 ArmSemihosting::FileBase::serialize(CheckpointOut &cp) const
801 {
802 paramOut(cp, "name", _name);
803 SERIALIZE_SCALAR(mode);
804 }
805
806 void
807 ArmSemihosting::FileBase::unserialize(CheckpointIn &cp)
808 {
809 /* Unserialization of name and mode happens in
810 * ArmSemihosting::FileBase::create() */
811 }
812
813 int64_t
814 ArmSemihosting::FileBase::read(uint8_t *buffer, uint64_t size)
815 {
816 return -EINVAL;
817 }
818
819 int64_t
820 ArmSemihosting::FileBase::write(const uint8_t *buffer, uint64_t size)
821 {
822 return -EINVAL;
823 }
824
825 int64_t
826 ArmSemihosting::FileBase::seek(uint64_t pos)
827 {
828 return -EINVAL;
829 }
830
831 int64_t
832 ArmSemihosting::FileBase::flen()
833 {
834 return -EINVAL;
835 }
836
837
838 ArmSemihosting::FileFeatures::FileFeatures(
839 ArmSemihosting &_parent, const char *_name, const char *_mode)
840 : FileBase(_parent, _name, _mode)
841 {
842 }
843
844 int64_t
845 ArmSemihosting::FileFeatures::read(uint8_t *buffer, uint64_t size)
846 {
847 int64_t len = 0;
848
849 for (; pos < size && pos < ArmSemihosting::features.size(); pos++)
850 buffer[len++] = ArmSemihosting::features[pos];
851
852 return len;
853 }
854
855 int64_t
856 ArmSemihosting::FileFeatures::seek(uint64_t _pos)
857 {
858 if (_pos < ArmSemihosting::features.size()) {
859 pos = _pos;
860 return 0;
861 } else {
862 return -ENXIO;
863 }
864 }
865
866 void
867 ArmSemihosting::FileFeatures::serialize(CheckpointOut &cp) const
868 {
869 FileBase::serialize(cp);
870 SERIALIZE_SCALAR(pos);
871 }
872
873 void
874 ArmSemihosting::FileFeatures::unserialize(CheckpointIn &cp)
875 {
876 FileBase::unserialize(cp);
877 UNSERIALIZE_SCALAR(pos);
878 }
879
880
881
882 ArmSemihosting::File::File(ArmSemihosting &_parent,
883 const char *_name, const char *_perms)
884 : FileBase(_parent, _name, _perms),
885 file(nullptr)
886 {
887 }
888
889 ArmSemihosting::File::~File()
890 {
891 if (file)
892 close();
893 }
894
895 int64_t
896 ArmSemihosting::File::openImpl(bool in_cpt)
897 {
898 panic_if(file, "Trying to open an already open file.\n");
899
900 if (_name == ":tt") {
901 if (mode[0] == 'r') {
902 file = parent.stdin;
903 } else if (mode[0] == 'w') {
904 file = parent.stdout;
905 } else if (mode[0] == 'a') {
906 file = parent.stderr;
907 } else {
908 warn("Unknown file mode for the ':tt' special file");
909 return -EINVAL;
910 }
911 } else {
912 std::string real_mode(this->mode);
913 // Avoid truncating the file if we are restoring from a
914 // checkpoint.
915 if (in_cpt && real_mode[0] == 'w')
916 real_mode[0] = 'a';
917
918 file = fopen(_name.c_str(), real_mode.c_str());
919 }
920
921 return file ? 0 : -errno;
922 }
923
924 int64_t
925 ArmSemihosting::File::close()
926 {
927 panic_if(!file, "Trying to close an already closed file.\n");
928
929 if (needClose()) {
930 fclose(file);
931 }
932 file = nullptr;
933
934 return 0;
935 }
936
937 bool
938 ArmSemihosting::File::isTTY() const
939 {
940 return file == parent.stdout ||
941 file == parent.stderr ||
942 file == parent.stdin;
943 }
944
945 int64_t
946 ArmSemihosting::File::read(uint8_t *buffer, uint64_t size)
947 {
948 panic_if(!file, "Trying to read from a closed file");
949
950 size_t ret = fread(buffer, 1, size, file);
951 if (ret == 0) {
952 // Error or EOF. Assume errors are due to invalid file
953 // operations (e.g., reading a write-only stream).
954 return ferror(file) ? -EINVAL : 0;
955 } else {
956 return ret;
957 }
958 }
959
960 int64_t
961 ArmSemihosting::File::write(const uint8_t *buffer, uint64_t size)
962 {
963 panic_if(!file, "Trying to write to a closed file");
964
965
966 size_t ret = fwrite(buffer, 1, size, file);
967 if (ret == 0) {
968 // Assume errors are due to invalid file operations (e.g.,
969 // writing a read-only stream).
970 return -EINVAL;
971 } else {
972 return ret;
973 }
974 }
975
976 int64_t
977 ArmSemihosting::File::seek(uint64_t _pos)
978 {
979 panic_if(!file, "Trying to seek in a closed file");
980
981 errno = 0;
982 if (fseek(file, _pos, SEEK_SET) == 0)
983 return 0;
984 else
985 return -errno;
986 }
987
988 int64_t
989 ArmSemihosting::File::flen()
990 {
991 errno = 0;
992 long pos = ftell(file);
993 if (pos < 0)
994 return -errno;
995
996 if (fseek(file, 0, SEEK_END) != 0)
997 return -errno;
998
999 long len = ftell(file);
1000 if (len < 0)
1001 return -errno;
1002
1003 if (fseek(file, pos, SEEK_SET) != 0)
1004 return -errno;
1005
1006 return len;
1007 }
1008
1009
1010 void
1011 ArmSemihosting::File::serialize(CheckpointOut &cp) const
1012 {
1013 FileBase::serialize(cp);
1014
1015 if (!isTTY()) {
1016 long pos = file ? ftell(file) : 0;
1017 panic_if(pos < 0, "Failed to get file position.");
1018 SERIALIZE_SCALAR(pos);
1019 }
1020 }
1021
1022 void
1023 ArmSemihosting::File::unserialize(CheckpointIn &cp)
1024 {
1025 FileBase::unserialize(cp);
1026
1027 if (openImpl(true) < 0) {
1028 fatal("Failed to open file: %s", _name);
1029 }
1030
1031 if (!isTTY()) {
1032 long pos = 0;
1033 UNSERIALIZE_SCALAR(pos);
1034 if (fseek(file, pos, SEEK_SET) != 0) {
1035 fatal("Failed seek to current position (%i) in '%s'", pos, _name);
1036 }
1037 }
1038 }
1039
1040 std::ostream &
1041 operator << (std::ostream &os, const ArmSemihosting::InPlaceArg &ipa)
1042 {
1043 ccprintf(os, "[%#x-%#x)", ipa.addr, ipa.addr + ipa.size - 1);
1044 return os;
1045 }
1046
1047
1048 ArmSemihosting *
1049 ArmSemihostingParams::create() const
1050 {
1051 return new ArmSemihosting(*this);
1052 }