gem5 v23.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
semihosting.cc
Go to the documentation of this file.
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
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"
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
61namespace gem5
62{
63
64const std::map<uint32_t, ArmSemihosting::SemiCall> ArmSemihosting::calls{
65 { SYS_OPEN, { "SYS_OPEN", &ArmSemihosting::callOpen } },
66 { SYS_CLOSE, { "SYS_CLOSE", &ArmSemihosting::callClose } },
67 { SYS_WRITEC, { "SYS_WRITEC", &ArmSemihosting::callWriteC } },
68 { SYS_WRITE0, { "SYS_WRITE0", &ArmSemihosting::callWrite0 } },
69 { SYS_WRITE, { "SYS_WRITE", &ArmSemihosting::callWrite } },
70 { SYS_READ, { "SYS_READ", &ArmSemihosting::callRead } },
71 { SYS_READC, { "SYS_READC", &ArmSemihosting::callReadC } },
72 { SYS_ISERROR, { "SYS_ISERROR", &ArmSemihosting::callIsError } },
73 { SYS_ISTTY, { "SYS_ISTTY", &ArmSemihosting::callIsTTY } },
74 { SYS_SEEK, { "SYS_SEEK", &ArmSemihosting::callSeek } },
75 { SYS_FLEN, { "SYS_FLEN", &ArmSemihosting::callFLen } },
76 { SYS_TMPNAM, { "SYS_TMPNAM", &ArmSemihosting::callTmpNam } },
77 { SYS_REMOVE, { "SYS_REMOVE", &ArmSemihosting::callRemove } },
78 { SYS_RENAME, { "SYS_RENAME", &ArmSemihosting::callRename } },
79 { SYS_CLOCK, { "SYS_CLOCK", &ArmSemihosting::callClock } },
80 { SYS_TIME, { "SYS_TIME", &ArmSemihosting::callTime } },
81 { SYS_SYSTEM, { "SYS_SYSTEM", &ArmSemihosting::callSystem } },
82 { SYS_ERRNO, { "SYS_ERRNO", &ArmSemihosting::callErrno } },
83 { SYS_GET_CMDLINE,
84 { "SYS_GET_CMDLINE", &ArmSemihosting::callGetCmdLine } },
85 { SYS_HEAPINFO, { "SYS_HEAPINFO", &ArmSemihosting::callHeapInfo32,
87
88 { SYS_EXIT, { "SYS_EXIT", &ArmSemihosting::callExit32,
90 { SYS_EXIT_EXTENDED,
91 { "SYS_EXIT_EXTENDED", &ArmSemihosting::callExitExtended } },
92
93 { SYS_ELAPSED, { "SYS_ELAPSED", &ArmSemihosting::callElapsed32,
95 { SYS_TICKFREQ, { "SYS_TICKFREQ", &ArmSemihosting::callTickFreq } },
96 { SYS_GEM5_PSEUDO_OP,
97 { "SYS_GEM5_PSEUDO_OP", &ArmSemihosting::callGem5PseudoOp32,
99};
100
102 "r", "rb", "r+", "r+b",
103 "w", "wb", "w+", "w+b",
104 "a", "ab", "a+", "a+b",
105};
106
107const std::map<uint64_t, const char *> ArmSemihosting::exitCodes{
108 { 0x20000, "semi:ADP_Stopped_BranchThroughZero" },
109 { 0x20001, "semi:ADP_Stopped_UndefinedInstr" },
110 { 0x20002, "semi:ADP_Stopped_SoftwareInterrupt" },
111 { 0x20003, "semi:ADP_Stopped_PrefetchAbort" },
112 { 0x20004, "semi:ADP_Stopped_DataAbort" },
113 { 0x20005, "semi:ADP_Stopped_AddressException" },
114 { 0x20006, "semi:ADP_Stopped_IRQ" },
115 { 0x20007, "semi:ADP_Stopped_FIQ" },
116
117 { 0x20020, "semi:ADP_Stopped_BreakPoint" },
118 { 0x20021, "semi:ADP_Stopped_WatchPoint" },
119 { 0x20022, "semi:ADP_Stopped_StepComplete" },
120 { 0x20023, "semi:ADP_Stopped_RunTimeErrorUnknown" },
121 { 0x20024, "semi:ADP_Stopped_InternalError" },
122 { 0x20025, "semi:ADP_Stopped_UserInterruption" },
123 { 0x20026, "semi:ADP_Stopped_ApplicationExit" },
124 { 0x20027, "semi:ADP_Stopped_StackOverflow" },
125 { 0x20028, "semi:ADP_Stopped_DivisionByZero" },
126 { 0x20029, "semi:ADP_Stopped_DivisionByZero" },
127};
128
129
131 0x53, 0x48, 0x46, 0x42, // Magic
132 0x3, // EXT_EXIT_EXTENDED, EXT_STDOUT_STDERR
133};
134
135const std::map<const std::string, FILE *> ArmSemihosting::stdioMap{
136 {"cin", ::stdin},
137 {"stdin", ::stdin},
138 {"cout", ::stdout},
139 {"stdout", ::stdout},
140 {"cerr", ::stderr},
141 {"stderr", ::stderr},
142};
143
144ArmSemihosting::ArmSemihosting(const ArmSemihostingParams &p)
145 : SimObject(p),
146 cmdLine(p.cmd_line),
147 memReserve(p.mem_reserve),
148 stackSize(p.stack_size),
149 timeBase([p]{ struct tm t = p.time; return mkutctime(&t); }()),
150 tickShift(calcTickShift()),
151 semiErrno(0),
152 filesRootDir(!p.files_root_dir.empty() &&
153 p.files_root_dir.back() != '/' ?
154 p.files_root_dir + '/' : p.files_root_dir),
155 stdin(getSTDIO("stdin", p.stdin, "r")),
156 stdout(getSTDIO("stdout", p.stdout, "w")),
157 stderr(p.stderr == p.stdout ?
158 stdout : getSTDIO("stderr", p.stderr, "w"))
159{
160 // Create an empty place-holder file for position 0 as semi-hosting
161 // calls typically expect non-zero file handles.
162 files.push_back(nullptr);
163
164 if (tickShift > 0)
165 inform("Semihosting: Shifting elapsed ticks by %i bits.",
166 tickShift);
167}
168
169bool
171{
173 if (op > MaxStandardOp && !gem5_ops) {
174 unrecognizedCall<Abi64>(
175 tc, "Gem5 semihosting op (0x%x) disabled from here.", op);
176 return false;
177 }
178
179 auto it = calls.find(op);
180 if (it == calls.end()) {
181 unrecognizedCall<Abi64>(
182 tc, "Unknown aarch64 semihosting call: op = 0x%x", op);
183 return false;
184 }
185 const SemiCall &call = it->second;
186
187 DPRINTF(Semihosting, "Semihosting call64: %s\n", call.dump64(tc));
188 auto err = call.call64(this, tc);
189 semiErrno = err.second;
190 DPRINTF(Semihosting, "\t ->: 0x%x, %i\n", err.first, err.second);
191
192 return true;
193}
194
195bool
197{
199 if (op > MaxStandardOp && !gem5_ops) {
200 unrecognizedCall<Abi32>(
201 tc, "Gem5 semihosting op (0x%x) disabled from here.", op);
202 return false;
203 }
204
205 auto it = calls.find(op);
206 if (it == calls.end()) {
207 unrecognizedCall<Abi32>(
208 tc, "Unknown aarch32 semihosting call: op = 0x%x", op);
209 return false;
210 }
211 const SemiCall &call = it->second;
212
213 DPRINTF(Semihosting, "Semihosting call32: %s\n", call.dump32(tc));
214 auto err = call.call32(this, tc);
215 semiErrno = err.second;
216 DPRINTF(Semihosting, "\t ->: 0x%x, %i\n", err.first, err.second);
217
218 return true;
219}
220
221void
223{
225
226 paramOut(cp, "num_files", files.size());
227 for (int i = 0; i < files.size(); i++) {
228 // File closed?
229 if (!files[i])
230 continue;
231
232 files[i]->serializeSection(cp, csprintf("file%i", i));
233 }
234}
235
236void
238{
240
241 size_t num_files;
242 paramIn(cp, "num_files", num_files);
243 files.resize(num_files);
244 for (int i = 0; i < num_files; i++)
245 files[i] = FileBase::create(*this, cp, csprintf("file%i", i));
246}
247
248PortProxy &
250{
251 static std::unique_ptr<PortProxy> port_proxy_s;
252 static std::unique_ptr<PortProxy> port_proxy_ns;
253 static System *secure_sys = nullptr;
254
255 if (ArmISA::isSecure(tc)) {
256 System *sys = tc->getSystemPtr();
257 if (sys != secure_sys) {
258 if (FullSystem) {
259 port_proxy_s.reset(
261 } else {
262 port_proxy_s.reset(
266 }
267 }
268 secure_sys = sys;
269 return *port_proxy_s;
270 } else {
271 if (!port_proxy_ns) {
272 if (FullSystem) {
273 port_proxy_ns.reset(new TranslatingPortProxy(tc));
274 } else {
275 port_proxy_ns.reset(new SETranslatingPortProxy(tc));
276 }
277 }
278
279 return *port_proxy_ns;
280 }
281}
282
283
284std::string
286{
287 std::vector<char> buf(len + 1);
288
289 buf[len] = '\0';
290 portProxy(tc).readBlob(ptr, buf.data(), len);
291
292 return std::string(buf.data());
293}
294
297 int fmode, size_t name_size)
298{
299 const char *mode = fmode < fmodes.size() ? fmodes[fmode] : nullptr;
300
301 DPRINTF(Semihosting, "Semihosting SYS_OPEN(0x%x, %i[%s], %i)\n",
302 name_base, fmode, mode ? mode : "-", name_size);
303 if (!mode || !name_base)
304 return retError(EINVAL);
305
306 std::string fname = readString(tc, name_base, name_size);
307 if (!fname.empty() && fname.front() != '/')
308 fname = filesRootDir + fname;
309
310 std::unique_ptr<ArmSemihosting::FileBase> file =
311 FileBase::create(*this, fname, mode);
312 int64_t ret = file->open();
313 DPRINTF(Semihosting, "Semihosting SYS_OPEN(\"%s\", %i[%s]): %i\n",
314 fname, fmode, mode, ret);
315 if (ret < 0) {
316 return retError(-ret);
317 } else {
318 files.push_back(std::move(file));
319 return retOK(files.size() - 1);
320 }
321}
322
325{
326 if (handle > files.size()) {
327 DPRINTF(Semihosting, "Semihosting SYS_CLOSE(%i): Illegal file\n");
328 return retError(EBADF);
329 }
330
331 std::unique_ptr<FileBase> &file = files[handle];
332 int64_t error = file->close();
333 DPRINTF(Semihosting, "Semihosting SYS_CLOSE(%i[%s]): %i\n",
334 handle, file->fileName(), error);
335 if (error < 0) {
336 return retError(-error);
337 } else {
338 // Zap the pointer and free the entry in the file table as
339 // well.
340 files[handle].reset();
341 return retOK(0);
342 }
343}
344
347{
348 const char c = portProxy(tc).read<char>(arg.addr);
349
350 DPRINTF(Semihosting, "Semihosting SYS_WRITEC('%c')\n", c);
351 std::cout.put(c);
352
353 return retOK(0);
354}
355
358{
359 DPRINTF(Semihosting, "Semihosting SYS_WRITE0(...)\n");
360 PortProxy &proxy = portProxy(tc);
361 std::string str;
362 proxy.readString(str, arg.addr);
363 std::cout.write(str.c_str(), str.size());
364
365 return retOK(0);
366}
367
370 size_t size)
371{
372 if (handle > files.size() || !files[handle])
373 return RetErrno(size, EBADF);
374
375 std::vector<uint8_t> buffer(size);
376 portProxy(tc).readBlob(addr, buffer.data(), buffer.size());
377
378 int64_t ret = files[handle]->write(buffer.data(), buffer.size());
379 if (ret < 0) {
380 // No bytes written (we're returning the number of bytes not
381 // written)
382 return RetErrno(size, -ret);
383 } else {
384 // Return the number of bytes not written
385 return RetErrno(size - ret, 0);
386 }
387}
388
391 size_t size)
392{
393 if (handle > files.size() || !files[handle])
394 return RetErrno(size, EBADF);
395
396 std::vector<uint8_t> buffer(size);
397 int64_t ret = files[handle]->read(buffer.data(), buffer.size());
398 if (ret < 0) {
399 return RetErrno(size, -ret);
400 } else {
401 panic_if(ret > buffer.size(), "Read longer than buffer size.");
402
403 portProxy(tc).writeBlob(addr, buffer.data(), ret);
404
405 // Return the number of bytes not written
406 return retOK(size - ret);
407 }
408}
409
412{
413 return retOK((char)std::cin.get());
414}
415
418{
419 return retOK(status < 0 ? 1 : 0);
420}
421
424{
425 if (handle > files.size() || !files[handle])
426 return retError(EBADF);
427
428 int64_t ret = files[handle]->isTTY();
429 if (ret < 0) {
430 return retError(-ret);
431 } else {
432 return retOK(ret ? 1 : 0);
433 }
434}
435
438{
439 if (handle > files.size() || !files[handle])
440 return retError(EBADF);
441
442 int64_t ret = files[handle]->seek(pos);
443 if (ret < 0) {
444 return retError(-ret);
445 } else {
446 return retOK(0);
447 }
448}
449
452{
453 if (handle > files.size() || !files[handle])
454 return retError(EBADF);
455
456 int64_t ret = files[handle]->flen();
457 if (ret < 0) {
458 return retError(-ret);
459 } else {
460 return retOK(ret);
461 }
462}
463
466 size_t size)
467{
468 std::string path = "";
469 int64_t unlink_call_ret = 0;
470
471 do {
472 path = simout.resolve(csprintf("%s.tmp%05i", name(), tmpNameIndex++));
473 // remove the (potentially existing) file of the given path
474 unlink_call_ret = unlink(path.c_str());
475 // if the file is busy, find another name
476 } while ((unlink_call_ret < 0) && (errno == EBUSY));
477
478 const size_t path_len = path.length();
479 if (path_len >= size)
480 return retError(ENOSPC);
481
482 portProxy(tc).writeBlob(addr, path.c_str(), path_len + 1);
483 return retOK(0);
484}
485
487ArmSemihosting::callRemove(ThreadContext *tc, Addr name_base, size_t name_size)
488{
489 std::string fname = readString(tc, name_base, name_size);
490
491 if (remove(fname.c_str()) != 0) {
492 return retError(errno);
493 } else {
494 return retOK(0);
495 }
496}
497
499ArmSemihosting::callRename(ThreadContext *tc, Addr from_addr, size_t from_size,
500 Addr to_addr, size_t to_size)
501{
502 std::string from = readString(tc, from_addr, from_size);
503 std::string to = readString(tc, to_addr, to_size);
504
505 if (rename(from.c_str(), to.c_str()) != 0) {
506 return retError(errno);
507 } else {
508 return retOK(0);
509 }
510}
511
514{
515 return retOK(curTick() / (sim_clock::as_int::s / 100));
516}
517
520{
521 return retOK(timeBase + round(curTick() / sim_clock::as_float::s));
522}
523
525ArmSemihosting::callSystem(ThreadContext *tc, Addr cmd_addr, size_t cmd_size)
526{
527 const std::string cmd = readString(tc, cmd_addr, cmd_size);
528 warn("Semihosting: SYS_SYSTEM not implemented. Guest tried to run: %s\n",
529 cmd);
530 return retError(EINVAL);
531
532}
533
536{
537 // Preserve errno by returning it in errno as well.
539}
540
543 InPlaceArg size_arg)
544{
545 PortProxy &proxy = portProxy(tc);
546 ByteOrder endian = ArmISA::byteOrder(tc);
547 size_t size = size_arg.read(tc, endian);
548
549 if (cmdLine.size() + 1 < size) {
550 proxy.writeBlob(addr, cmdLine.c_str(), cmdLine.size() + 1);
551 size_arg.write(tc, cmdLine.size(), endian);
552 return retOK(0);
553 } else {
554 return retError(0);
555 }
556}
557
558void
560 Addr &heap_base, Addr &heap_limit,
561 Addr &stack_base, Addr &stack_limit)
562{
563 const memory::PhysicalMemory &phys = tc->getSystemPtr()->getPhysMem();
564 const AddrRangeList memories = phys.getConfAddrRanges();
565 fatal_if(memories.size() < 1, "No memories reported from System");
566 warn_if(memories.size() > 1, "Multiple physical memory ranges available. "
567 "Using first range heap/stack.");
568 const AddrRange mem = *memories.begin();
569 const Addr mem_start = mem.start() + memReserve;
570 Addr mem_end = mem.end();
571
572 // Make sure that 32-bit guests can access their memory.
573 if (!aarch64) {
574 const Addr phys_max = (1ULL << 32) - 1;
575 panic_if(mem_start > phys_max,
576 "Physical memory out of range for a 32-bit guest.");
577 if (mem_end > phys_max) {
578 warn("Some physical memory out of range for a 32-bit guest.");
579 mem_end = phys_max;
580 }
581 }
582
583 fatal_if(mem_start + stackSize >= mem_end,
584 "Physical memory too small to fit desired stack and a heap.");
585
586 heap_base = mem_start;
587 heap_limit = mem_end - stackSize + 1;
588 stack_base = (mem_end + 1) & ~0x7ULL; // 8 byte stack alignment
589 stack_limit = heap_limit;
590
591 inform("Reporting heap/stack info to guest:\n"
592 "\tHeap base: 0x%x\n"
593 "\tHeap limit: 0x%x\n"
594 "\tStack base: 0x%x\n"
595 "\tStack limit: 0x%x\n",
596 heap_base, heap_limit, stack_base, stack_limit);
597}
598
601{
602 uint64_t heap_base, heap_limit, stack_base, stack_limit;
603 gatherHeapInfo(tc, false, heap_base, heap_limit, stack_base, stack_limit);
604
605 std::array<uint32_t, 4> block = {{
606 (uint32_t)heap_base, (uint32_t)heap_limit,
607 (uint32_t)stack_base, (uint32_t)stack_limit
608 }};
609 portProxy(tc).write(block_addr, block, ArmISA::byteOrder(tc));
610
611 return retOK(0);
612}
613
616{
617 uint64_t heap_base, heap_limit, stack_base, stack_limit;
618 gatherHeapInfo(tc, true, heap_base, heap_limit, stack_base, stack_limit);
619
620 std::array<uint64_t, 4> block = {{
621 heap_base, heap_limit, stack_base, stack_limit
622 }};
623 portProxy(tc).write(block_addr, block, ArmISA::byteOrder(tc));
624
625 return retOK(0);
626}
627
630{
631 semiExit(code.addr, 0);
632 return retOK(0);
633}
634
636ArmSemihosting::callExit64(ThreadContext *tc, uint64_t code, uint64_t subcode)
637{
638 semiExit(code, subcode);
639 return retOK(0);
640}
641
644 uint64_t code, uint64_t subcode)
645{
646 semiExit(code, subcode);
647 return retOK(0);
648}
649
650void
651ArmSemihosting::semiExit(uint64_t code, uint64_t subcode)
652{
653 auto it = exitCodes.find(code);
654 if (it != exitCodes.end()) {
655 exitSimLoop(it->second, subcode);
656 } else {
657 exitSimLoop(csprintf("semi:0x%x", code), subcode);
658 }
659}
660
661
665{
666 ByteOrder endian = ArmISA::byteOrder(tc);
667 uint64_t tick = semiTick(curTick());
668
669 low.write(tc, tick, endian);
670 high.write(tc, tick >> 32, endian);
671
672 return retOK(0);
673}
674
675
678{
679 ticks.write(tc, semiTick(curTick()), ArmISA::byteOrder(tc));
680 return retOK(0);
681}
682
683
686{
688}
689
690
692{
694 {
695 public:
696 State(const ThreadContext *tc) : ArmSemihosting::Abi32::State(tc)
697 {
698 // Use getAddr() to skip the func number in the first slot.
699 getAddr();
700 }
701 };
702};
703
705{
707 {
708 public:
709 State(const ThreadContext *tc) : ArmSemihosting::Abi64::State(tc)
710 {
711 // Use getAddr() to skip the func number in the first slot.
712 getAddr();
713 }
714 };
715};
716
717namespace guest_abi
718{
719
720// Handle arguments the same as for semihosting operations. Skipping the first
721// slot is handled internally by the State type.
722template <typename T>
724 public Argument<ArmSemihosting::Abi32, T>
725{};
726template <typename T>
728 public Argument<ArmSemihosting::Abi64, T>
729{};
730
731} // namespace guest_abi
732
735{
736 uint8_t func;
737 pseudo_inst::decodeAddrOffset(encoded_func, func);
738
739 uint64_t ret;
740 if (pseudo_inst::pseudoInst<SemiPseudoAbi32>(tc, func, ret))
741 return retOK(ret);
742 else
743 return retError(EINVAL);
744}
745
748{
749 uint8_t func;
750 pseudo_inst::decodeAddrOffset(encoded_func, func);
751
752 uint64_t ret;
753 if (pseudo_inst::pseudoInst<SemiPseudoAbi64>(tc, func, ret))
754 return retOK(ret);
755 else
756 return retError(EINVAL);
757}
758
759FILE *
760ArmSemihosting::getSTDIO(const char *stream_name,
761 const std::string &name, const char *mode)
762{
763 auto it = stdioMap.find(name);
764 if (it == stdioMap.end()) {
765 FILE *f = fopen(name.c_str(), mode);
766 if (!f) {
767 fatal("Failed to open %s (%s): %s\n",
768 stream_name, name, strerror(errno));
769 }
770 return f;
771 } else {
772 return it->second;
773 }
774}
775
776std::unique_ptr<ArmSemihosting::FileBase>
778 ArmSemihosting &parent, const std::string &fname, const char *mode)
779{
780 std::unique_ptr<FileBase> file;
781 if (fname == ":semihosting-features") {
782 file.reset(new FileFeatures(parent, fname.c_str(), mode));
783 } else {
784 file.reset(new File(parent, fname.c_str(), mode));
785 }
786
787 return file;
788}
789
790std::unique_ptr<ArmSemihosting::FileBase>
792 CheckpointIn &cp, const std::string &sec)
793{
794 std::unique_ptr<FileBase> file;
795 ScopedCheckpointSection _sec(cp, sec);
796
797 // Was the file open when the checkpoint was created?
799 return file;
800
801 std::string fname, mode;
802 paramIn(cp, "name", fname);
803 paramIn(cp, "mode", mode);
804 file = create(parent, fname, mode.c_str());
805 assert(file);
806 file->unserialize(cp);
807
808 return file;
809}
810
811void
813{
814 paramOut(cp, "name", _name);
816}
817
818void
820{
821 /* Unserialization of name and mode happens in
822 * ArmSemihosting::FileBase::create() */
823}
824
825int64_t
826ArmSemihosting::FileBase::read(uint8_t *buffer, uint64_t size)
827{
828 return -EINVAL;
829}
830
831int64_t
832ArmSemihosting::FileBase::write(const uint8_t *buffer, uint64_t size)
833{
834 return -EINVAL;
835}
836
837int64_t
839{
840 return -EINVAL;
841}
842
843int64_t
845{
846 return -EINVAL;
847}
848
849
851 ArmSemihosting &_parent, const char *_name, const char *_mode)
852 : FileBase(_parent, _name, _mode)
853{
854}
855
856int64_t
857ArmSemihosting::FileFeatures::read(uint8_t *buffer, uint64_t size)
858{
859 int64_t len = 0;
860
861 for (; pos < size && pos < ArmSemihosting::features.size(); pos++)
862 buffer[len++] = ArmSemihosting::features[pos];
863
864 return len;
865}
866
867int64_t
869{
870 if (_pos < ArmSemihosting::features.size()) {
871 pos = _pos;
872 return 0;
873 } else {
874 return -ENXIO;
875 }
876}
877
878void
880{
882 SERIALIZE_SCALAR(pos);
883}
884
885void
887{
890}
891
892
893
895 const char *_name, const char *_perms)
896 : FileBase(_parent, _name, _perms),
897 file(nullptr)
898{
899}
900
902{
903 if (file)
904 close();
905}
906
907int64_t
909{
910 panic_if(file, "Trying to open an already open file.\n");
911
912 if (_name == ":tt") {
913 if (mode[0] == 'r') {
914 file = parent.stdin;
915 } else if (mode[0] == 'w') {
916 file = parent.stdout;
917 } else if (mode[0] == 'a') {
918 file = parent.stderr;
919 } else {
920 warn("Unknown file mode for the ':tt' special file");
921 return -EINVAL;
922 }
923 } else {
924 std::string real_mode(this->mode);
925 // Avoid truncating the file if we are restoring from a
926 // checkpoint.
927 if (in_cpt && real_mode[0] == 'w')
928 real_mode[0] = 'a';
929
930 file = fopen(_name.c_str(), real_mode.c_str());
931 }
932
933 return file ? 0 : -errno;
934}
935
936int64_t
938{
939 panic_if(!file, "Trying to close an already closed file.\n");
940
941 if (needClose()) {
942 fclose(file);
943 }
944 file = nullptr;
945
946 return 0;
947}
948
949bool
951{
952 return file == parent.stdout ||
953 file == parent.stderr ||
954 file == parent.stdin;
955}
956
957int64_t
958ArmSemihosting::File::read(uint8_t *buffer, uint64_t size)
959{
960 panic_if(!file, "Trying to read from a closed file");
961
962 size_t ret = fread(buffer, 1, size, file);
963 if (ret == 0) {
964 // Error or EOF. Assume errors are due to invalid file
965 // operations (e.g., reading a write-only stream).
966 return ferror(file) ? -EINVAL : 0;
967 } else {
968 return ret;
969 }
970}
971
972int64_t
973ArmSemihosting::File::write(const uint8_t *buffer, uint64_t size)
974{
975 panic_if(!file, "Trying to write to a closed file");
976
977
978 size_t ret = fwrite(buffer, 1, size, file);
979 if (ret == 0) {
980 // Assume errors are due to invalid file operations (e.g.,
981 // writing a read-only stream).
982 return -EINVAL;
983 } else {
984 return ret;
985 }
986}
987
988int64_t
990{
991 panic_if(!file, "Trying to seek in a closed file");
992
993 errno = 0;
994 if (fseek(file, _pos, SEEK_SET) == 0)
995 return 0;
996 else
997 return -errno;
998}
999
1000int64_t
1002{
1003 errno = 0;
1004 long pos = ftell(file);
1005 if (pos < 0)
1006 return -errno;
1007
1008 if (fseek(file, 0, SEEK_END) != 0)
1009 return -errno;
1010
1011 long len = ftell(file);
1012 if (len < 0)
1013 return -errno;
1014
1015 if (fseek(file, pos, SEEK_SET) != 0)
1016 return -errno;
1017
1018 return len;
1019}
1020
1021
1022void
1024{
1026
1027 if (!isTTY()) {
1028 long pos = file ? ftell(file) : 0;
1029 panic_if(pos < 0, "Failed to get file position.");
1030 SERIALIZE_SCALAR(pos);
1031 }
1032}
1033
1034void
1036{
1038
1039 if (openImpl(true) < 0) {
1040 fatal("Failed to open file: %s", _name);
1041 }
1042
1043 if (!isTTY()) {
1044 long pos = 0;
1045 UNSERIALIZE_SCALAR(pos);
1046 if (fseek(file, pos, SEEK_SET) != 0) {
1047 fatal("Failed seek to current position (%i) in '%s'", pos, _name);
1048 }
1049 }
1050}
1051
1052std::ostream &
1053operator << (std::ostream &os, const ArmSemihosting::InPlaceArg &ipa)
1054{
1055 ccprintf(os, "[%#x-%#x)", ipa.addr, ipa.addr + ipa.size - 1);
1056 return os;
1057}
1058
1059} // namespace gem5
std::string error
#define DPRINTF(x,...)
Definition trace.hh:210
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition addr_range.hh:82
Internal state for open files.
virtual int64_t seek(uint64_t pos)
Seek to an absolute position in the file.
virtual int64_t write(const uint8_t *buffer, uint64_t size)
Write data to file.
virtual int64_t read(uint8_t *buffer, uint64_t size)
Read data from file.
virtual int64_t flen()
Get the length of a file in bytes.
void serialize(CheckpointOut &cp) const override
Serialize an object.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
static std::unique_ptr< FileBase > create(ArmSemihosting &parent, const std::string &fname, const char *mode)
Implementation of the ':semihosting-features' magic file.
FileFeatures(ArmSemihosting &_parent, const char *name, const char *mode)
int64_t seek(uint64_t pos) override
Seek to an absolute position in the file.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
int64_t read(uint8_t *buffer, uint64_t size) override
Read data from file.
void serialize(CheckpointOut &cp) const override
Serialize an object.
File(ArmSemihosting &_parent, const char *name, const char *mode)
int64_t openImpl(bool unserialize)
int64_t write(const uint8_t *buffer, uint64_t size) override
Write data to file.
int64_t seek(uint64_t pos) override
Seek to an absolute position in the file.
int64_t close() override
Close the file.
void serialize(CheckpointOut &cp) const override
Serialize an object.
bool isTTY() const override
Check if a file corresponds to a TTY device.
int64_t read(uint8_t *buffer, uint64_t size) override
Read data from file.
int64_t flen() override
Get the length of a file in bytes.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Semihosting for AArch32 and AArch64.
std::pair< uint64_t, SemiErrno > RetErrno
void serialize(CheckpointOut &cp) const override
Serialize an object.
RetErrno callSeek(ThreadContext *tc, Handle handle, uint64_t pos)
RetErrno callReadC(ThreadContext *tc)
static const std::map< uint32_t, SemiCall > calls
RetErrno callSystem(ThreadContext *tc, Addr cmd_addr, size_t cmd_size)
RetErrno callExitExtended(ThreadContext *tc, uint64_t code, uint64_t subcode)
RetErrno callRename(ThreadContext *tc, Addr from_addr, size_t from_size, Addr to_addr, size_t to_size)
RetErrno callClock(ThreadContext *tc)
bool call32(ThreadContext *tc, bool gem5_ops)
Perform an Arm Semihosting call from aarch32 code.
RetErrno callTickFreq(ThreadContext *tc)
RetErrno callWriteC(ThreadContext *tc, InPlaceArg c)
RetErrno callTime(ThreadContext *tc)
static const std::map< const std::string, FILE * > stdioMap
static RetErrno retOK(uint64_t r)
RetErrno callGetCmdLine(ThreadContext *tc, Addr addr, InPlaceArg size_arg)
RetErrno callIsError(ThreadContext *tc, int64_t status)
std::string filesRootDir
void gatherHeapInfo(ThreadContext *tc, bool aarch64, Addr &heap_base, Addr &heap_limit, Addr &stack_base, Addr &stack_limit)
static PortProxy & portProxy(ThreadContext *tc)
std::string readString(ThreadContext *tc, Addr ptr, size_t len)
RetErrno callWrite0(ThreadContext *tc, InPlaceArg str)
bool call64(ThreadContext *tc, bool gem5_ops)
Perform an Arm Semihosting call from aarch64 code.
RetErrno callElapsed64(ThreadContext *tc, InPlaceArg ticks)
void semiExit(uint64_t code, uint64_t subcode)
const time_t timeBase
Base time when the simulation started.
static FILE * getSTDIO(const char *stream_name, const std::string &name, const char *mode)
static const std::map< uint64_t, const char * > exitCodes
void unserialize(CheckpointIn &cp) override
Unserialize an object.
RetErrno callRemove(ThreadContext *tc, Addr name_base, size_t name_size)
RetErrno callFLen(ThreadContext *tc, Handle handle)
RetErrno callIsTTY(ThreadContext *tc, Handle handle)
RetErrno callClose(ThreadContext *tc, Handle handle)
RetErrno callHeapInfo64(ThreadContext *tc, Addr block_addr)
std::vector< std::unique_ptr< FileBase > > files
RetErrno callExit64(ThreadContext *tc, uint64_t code, uint64_t subcode)
RetErrno callGem5PseudoOp32(ThreadContext *tc, uint32_t encoded_func)
RetErrno callTmpNam(ThreadContext *tc, Addr buffer, uint64_t id, size_t size)
RetErrno callRead(ThreadContext *tc, Handle handle, Addr buffer, size_t size)
static const std::vector< uint8_t > features
RetErrno callHeapInfo32(ThreadContext *tc, Addr block_addr)
ArmSemihosting(const ArmSemihostingParams &p)
uint64_t semiTick(Tick tick) const
static const std::vector< const char * > fmodes
RetErrno callWrite(ThreadContext *tc, Handle handle, Addr buffer, size_t size)
RetErrno callGem5PseudoOp64(ThreadContext *tc, uint64_t encoded_func)
RetErrno callExit32(ThreadContext *tc, InPlaceArg code)
RetErrno callOpen(ThreadContext *tc, const Addr name_base, int fmode, size_t name_size)
static RetErrno retError(SemiErrno e)
RetErrno callElapsed32(ThreadContext *tc, InPlaceArg low, InPlaceArg high)
RetErrno callErrno(ThreadContext *tc)
const std::string cmdLine
const std::string _name
Definition named.hh:41
virtual std::string name() const
Definition named.hh:47
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition output.cc:204
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition port_proxy.hh:87
T read(Addr address) const
Read sizeof(T) bytes from address and return as object T.
void writeBlob(Addr addr, const void *p, int size) const
Same as tryWriteBlob, but insists on success.
void write(Addr address, const T &data) const
Write object T to address.
void readString(std::string &str, Addr addr) const
Same as tryReadString, but insists on success.
void readBlob(Addr addr, void *p, int size) const
Higher level interfaces based on the above.
@ SECURE
The request targets the secure memory space.
Definition request.hh:186
State(const ThreadContext *tc)
State(const ThreadContext *tc)
static std::stack< std::string > path
Definition serialize.hh:315
Abstract superclass for simulation objects.
memory::PhysicalMemory & getPhysMem()
Get a pointer to access the physical memory of the system.
Definition system.hh:342
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual RegVal getReg(const RegId &reg) const
virtual System * getSystemPtr()=0
This proxy attempts to translate virtual addresses using the TLBs.
The physical memory encapsulates all memories in the system and provides basic functionality for acce...
Definition physical.hh:137
AddrRangeList getConfAddrRanges() const
Get the memory ranges for all memories that are to be reported to the configuration table.
Definition physical.cc:279
STL pair class.
Definition stl.hh:58
STL vector class.
Definition stl.hh:37
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
#define fatal(...)
This implements a cprintf based fatal() function.
Definition logging.hh:200
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
static const std::string & currentSection()
Gets the fully-qualified name of the active section.
Definition serialize.cc:130
bool sectionExists(const std::string &section)
Definition serialize.cc:200
#define warn(...)
Definition logging.hh:256
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition logging.hh:283
#define inform(...)
Definition logging.hh:257
constexpr RegId X0
Definition int.hh:240
constexpr RegId R0
Definition int.hh:186
ByteOrder byteOrder(const ThreadContext *tc)
Definition utility.hh:357
Bitfield< 3, 0 > mask
Definition pcstate.hh:63
Bitfield< 18, 16 > len
Bitfield< 4, 0 > mode
Definition misc_types.hh:74
bool isSecure(ThreadContext *tc)
Definition utility.cc:74
Bitfield< 5 > t
Definition misc_types.hh:71
Bitfield< 6 > err
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 5, 0 > status
Bitfield< 29 > c
Definition misc_types.hh:53
Bitfield< 6 > f
Definition misc_types.hh:68
Bitfield< 34 > aarch64
Definition types.hh:81
Bitfield< 0 > p
Bitfield< 25, 21 > to
Definition types.hh:96
Bitfield< 32 > tm
Definition misc.hh:112
Bitfield< 17 > os
Definition misc.hh:810
Bitfield< 4 > op
Definition types.hh:83
Bitfield< 3 > addr
Definition types.hh:84
static void decodeAddrOffset(Addr offset, uint8_t &func)
double s
These variables equal the number of ticks in the unit of time they're named after in a double.
Definition core.cc:51
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
Definition core.cc:47
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Tick curTick()
The universal simulation clock.
Definition cur_tick.hh:46
std::ostream CheckpointOut
Definition serialize.hh:66
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
void paramOut(CheckpointOut &cp, const std::string &name, ExtMachInst const &machInst)
Definition types.cc:40
time_t mkutctime(struct tm *time)
Definition time.cc:154
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
Definition types.cc:72
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition root.cc:220
OutputDirectory simout
Definition output.cc:62
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 (...
Definition sim_events.cc:88
uint64_t RegVal
Definition types.hh:173
std::string csprintf(const char *format, const Args &...args)
Definition cprintf.hh:161
void ccprintf(cp::Print &print)
Definition cprintf.hh:130
#define UNSERIALIZE_SCALAR(scalar)
Definition serialize.hh:575
#define SERIALIZE_SCALAR(scalar)
Definition serialize.hh:568
uint64_t read(ThreadContext *tc, ByteOrder endian)
void write(ThreadContext *tc, uint64_t val, ByteOrder endian)
Semihosting call information structure.
bool_vector8 mem[]
Definition reset_stim.h:43
const std::string & name()
Definition trace.cc:48

Generated on Mon Jul 10 2023 14:24:25 for gem5 by doxygen 1.9.7