2 * Copyright (c) 2018, 2019 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 * 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.
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.
38 #include "arch/arm/semihosting.hh"
42 #include "arch/arm/utility.hh"
43 #include "base/logging.hh"
44 #include "base/time.hh"
45 #include "debug/Semihosting.hh"
46 #include "dev/serial/serial.hh"
47 #include "mem/physical.hh"
48 #include "mem/se_translating_port_proxy.hh"
49 #include "mem/translating_port_proxy.hh"
50 #include "params/ArmSemihosting.hh"
51 #include "sim/byteswap.hh"
52 #include "sim/full_system.hh"
53 #include "sim/pseudo_inst.hh"
54 #include "sim/sim_exit.hh"
55 #include "sim/system.hh"
57 const std::map
<uint32_t, ArmSemihosting::SemiCall
> ArmSemihosting::calls
{
58 { SYS_OPEN
, { "SYS_OPEN", &ArmSemihosting::callOpen
} },
59 { SYS_CLOSE
, { "SYS_CLOSE", &ArmSemihosting::callClose
} },
60 { SYS_WRITEC
, { "SYS_WRITEC", &ArmSemihosting::callWriteC
} },
61 { SYS_WRITE0
, { "SYS_WRITE0", &ArmSemihosting::callWrite0
} },
62 { SYS_WRITE
, { "SYS_WRITE", &ArmSemihosting::callWrite
} },
63 { SYS_READ
, { "SYS_READ", &ArmSemihosting::callRead
} },
64 { SYS_READC
, { "SYS_READC", &ArmSemihosting::callReadC
} },
65 { SYS_ISERROR
, { "SYS_ISERROR", &ArmSemihosting::callIsError
} },
66 { SYS_ISTTY
, { "SYS_ISTTY", &ArmSemihosting::callIsTTY
} },
67 { SYS_SEEK
, { "SYS_SEEK", &ArmSemihosting::callSeek
} },
68 { SYS_FLEN
, { "SYS_FLEN", &ArmSemihosting::callFLen
} },
69 { SYS_TMPNAM
, { "SYS_TMPNAM", &ArmSemihosting::callTmpNam
} },
70 { SYS_REMOVE
, { "SYS_REMOVE", &ArmSemihosting::callRemove
} },
71 { SYS_RENAME
, { "SYS_RENAME", &ArmSemihosting::callRename
} },
72 { SYS_CLOCK
, { "SYS_CLOCK", &ArmSemihosting::callClock
} },
73 { SYS_TIME
, { "SYS_TIME", &ArmSemihosting::callTime
} },
74 { SYS_SYSTEM
, { "SYS_SYSTEM", &ArmSemihosting::callSystem
} },
75 { SYS_ERRNO
, { "SYS_ERRNO", &ArmSemihosting::callErrno
} },
77 { "SYS_GET_CMDLINE", &ArmSemihosting::callGetCmdLine
} },
78 { SYS_HEAPINFO
, { "SYS_HEAPINFO", &ArmSemihosting::callHeapInfo32
,
79 &ArmSemihosting::callHeapInfo64
} },
81 { SYS_EXIT
, { "SYS_EXIT", &ArmSemihosting::callExit32
,
82 &ArmSemihosting::callExit64
} },
84 { "SYS_EXIT_EXTENDED", &ArmSemihosting::callExitExtended
} },
86 { SYS_ELAPSED
, { "SYS_ELAPSED", &ArmSemihosting::callElapsed32
,
87 &ArmSemihosting::callElapsed64
} },
88 { SYS_TICKFREQ
, { "SYS_TICKFREQ", &ArmSemihosting::callTickFreq
} },
90 { "SYS_GEM5_PSEUDO_OP", &ArmSemihosting::callGem5PseudoOp32
,
91 &ArmSemihosting::callGem5PseudoOp64
} },
94 const std::vector
<const char *> ArmSemihosting::fmodes
{
95 "r", "rb", "r+", "r+b",
96 "w", "wb", "w+", "w+b",
97 "a", "ab", "a+", "a+b",
100 const std::map
<uint64_t, const char *> ArmSemihosting::exitCodes
{
101 { 0x20000, "semi:ADP_Stopped_BranchThroughZero" },
102 { 0x20001, "semi:ADP_Stopped_UndefinedInstr" },
103 { 0x20002, "semi:ADP_Stopped_SoftwareInterrupt" },
104 { 0x20003, "semi:ADP_Stopped_PrefetchAbort" },
105 { 0x20004, "semi:ADP_Stopped_DataAbort" },
106 { 0x20005, "semi:ADP_Stopped_AddressException" },
107 { 0x20006, "semi:ADP_Stopped_IRQ" },
108 { 0x20007, "semi:ADP_Stopped_FIQ" },
110 { 0x20020, "semi:ADP_Stopped_BreakPoint" },
111 { 0x20021, "semi:ADP_Stopped_WatchPoint" },
112 { 0x20022, "semi:ADP_Stopped_StepComplete" },
113 { 0x20023, "semi:ADP_Stopped_RunTimeErrorUnknown" },
114 { 0x20024, "semi:ADP_Stopped_InternalError" },
115 { 0x20025, "semi:ADP_Stopped_UserInterruption" },
116 { 0x20026, "semi:ADP_Stopped_ApplicationExit" },
117 { 0x20027, "semi:ADP_Stopped_StackOverflow" },
118 { 0x20028, "semi:ADP_Stopped_DivisionByZero" },
119 { 0x20029, "semi:ADP_Stopped_DivisionByZero" },
123 const std::vector
<uint8_t> ArmSemihosting::features
{
124 0x53, 0x48, 0x46, 0x42, // Magic
125 0x3, // EXT_EXIT_EXTENDED, EXT_STDOUT_STDERR
128 const std::map
<const std::string
, FILE *> ArmSemihosting::stdioMap
{
132 {"stdout", ::stdout
},
134 {"stderr", ::stderr
},
137 ArmSemihosting::ArmSemihosting(const ArmSemihostingParams
*p
)
139 cmdLine(p
->cmd_line
),
140 memReserve(p
->mem_reserve
),
141 stackSize(p
->stack_size
),
142 timeBase([p
]{ struct tm t
= p
->time
; return mkutctime(&t
); }()),
143 tickShift(calcTickShift()),
145 filesRootDir(!p
->files_root_dir
.empty() &&
146 p
->files_root_dir
.back() != '/' ?
147 p
->files_root_dir
+ '/' : p
->files_root_dir
),
148 stdin(getSTDIO("stdin", p
->stdin
, "r")),
149 stdout(getSTDIO("stdout", p
->stdout
, "w")),
150 stderr(p
->stderr
== p
->stdout
?
151 stdout
: getSTDIO("stderr", p
->stderr
, "w"))
153 // Create an empty place-holder file for position 0 as semi-hosting
154 // calls typically expect non-zero file handles.
155 files
.push_back(nullptr);
158 inform("Semihosting: Shifting elapsed ticks by %i bits.",
163 ArmSemihosting::call64(ThreadContext
*tc
, bool gem5_ops
)
165 RegVal op
= tc
->readIntReg(ArmISA::INTREG_X0
& mask(32));
166 if (op
> MaxStandardOp
&& !gem5_ops
) {
167 unrecognizedCall
<Abi64
>(
168 tc
, "Gem5 semihosting op (0x%x) disabled from here.", op
);
172 auto it
= calls
.find(op
);
173 if (it
== calls
.end()) {
174 unrecognizedCall
<Abi64
>(
175 tc
, "Unknown aarch64 semihosting call: op = 0x%x", op
);
178 const SemiCall
&call
= it
->second
;
180 DPRINTF(Semihosting
, "Semihosting call64: %s\n", call
.dump64(tc
));
181 auto err
= call
.call64(this, tc
);
182 semiErrno
= err
.second
;
183 DPRINTF(Semihosting
, "\t ->: 0x%x, %i\n", err
.first
, err
.second
);
189 ArmSemihosting::call32(ThreadContext
*tc
, bool gem5_ops
)
191 RegVal op
= tc
->readIntReg(ArmISA::INTREG_R0
);
192 if (op
> MaxStandardOp
&& !gem5_ops
) {
193 unrecognizedCall
<Abi32
>(
194 tc
, "Gem5 semihosting op (0x%x) disabled from here.", op
);
198 auto it
= calls
.find(op
);
199 if (it
== calls
.end()) {
200 unrecognizedCall
<Abi32
>(
201 tc
, "Unknown aarch32 semihosting call: op = 0x%x", op
);
204 const SemiCall
&call
= it
->second
;
206 DPRINTF(Semihosting
, "Semihosting call32: %s\n", call
.dump32(tc
));
207 auto err
= call
.call32(this, tc
);
208 semiErrno
= err
.second
;
209 DPRINTF(Semihosting
, "\t ->: 0x%x, %i\n", err
.first
, err
.second
);
215 ArmSemihosting::serialize(CheckpointOut
&cp
) const
217 SERIALIZE_SCALAR(semiErrno
);
219 paramOut(cp
, "num_files", files
.size());
220 for (int i
= 0; i
< files
.size(); i
++) {
225 files
[i
]->serializeSection(cp
, csprintf("file%i", i
));
230 ArmSemihosting::unserialize(CheckpointIn
&cp
)
232 UNSERIALIZE_SCALAR(semiErrno
);
235 paramIn(cp
, "num_files", num_files
);
236 files
.resize(num_files
);
237 for (int i
= 0; i
< num_files
; i
++)
238 files
[i
] = FileBase::create(*this, cp
, csprintf("file%i", i
));
242 ArmSemihosting::portProxy(ThreadContext
*tc
)
244 static std::unique_ptr
<PortProxy
> port_proxy_s
;
245 static System
*secure_sys
= nullptr;
247 if (ArmISA::inSecureState(tc
)) {
248 System
*sys
= tc
->getSystemPtr();
249 if (sys
!= secure_sys
) {
252 new TranslatingPortProxy(tc
, Request::SECURE
));
255 new SETranslatingPortProxy(
256 tc
, SETranslatingPortProxy::NextPage
,
261 return *port_proxy_s
;
263 return tc
->getVirtProxy();
269 ArmSemihosting::readString(ThreadContext
*tc
, Addr ptr
, size_t len
)
271 std::vector
<char> buf(len
+ 1);
274 portProxy(tc
).readBlob(ptr
, buf
.data(), len
);
276 return std::string(buf
.data());
279 ArmSemihosting::RetErrno
280 ArmSemihosting::callOpen(ThreadContext
*tc
, const Addr name_base
,
281 int fmode
, size_t name_size
)
283 const char *mode
= fmode
< fmodes
.size() ? fmodes
[fmode
] : nullptr;
285 DPRINTF(Semihosting
, "Semihosting SYS_OPEN(0x%x, %i[%s], %i)\n",
286 name_base
, fmode
, mode
? mode
: "-", name_size
);
287 if (!mode
|| !name_base
)
288 return retError(EINVAL
);
290 std::string fname
= readString(tc
, name_base
, name_size
);
291 if (!fname
.empty() && fname
.front() != '/')
292 fname
= filesRootDir
+ fname
;
294 std::unique_ptr
<ArmSemihosting::FileBase
> file
=
295 FileBase::create(*this, fname
, mode
);
296 int64_t ret
= file
->open();
297 DPRINTF(Semihosting
, "Semihosting SYS_OPEN(\"%s\", %i[%s]): %i\n",
298 fname
, fmode
, mode
, ret
);
300 return retError(-ret
);
302 files
.push_back(std::move(file
));
303 return retOK(files
.size() - 1);
307 ArmSemihosting::RetErrno
308 ArmSemihosting::callClose(ThreadContext
*tc
, Handle handle
)
310 if (handle
> files
.size()) {
311 DPRINTF(Semihosting
, "Semihosting SYS_CLOSE(%i): Illegal file\n");
312 return retError(EBADF
);
315 std::unique_ptr
<FileBase
> &file
= files
[handle
];
316 int64_t error
= file
->close();
317 DPRINTF(Semihosting
, "Semihosting SYS_CLOSE(%i[%s]): %i\n",
318 handle
, file
->fileName(), error
);
320 return retError(-error
);
322 // Zap the pointer and free the entry in the file table as
324 files
[handle
].reset();
329 ArmSemihosting::RetErrno
330 ArmSemihosting::callWriteC(ThreadContext
*tc
, InPlaceArg arg
)
332 const char c
= portProxy(tc
).read
<char>(arg
.addr
);
334 DPRINTF(Semihosting
, "Semihosting SYS_WRITEC('%c')\n", c
);
340 ArmSemihosting::RetErrno
341 ArmSemihosting::callWrite0(ThreadContext
*tc
, InPlaceArg arg
)
343 DPRINTF(Semihosting
, "Semihosting SYS_WRITE0(...)\n");
344 PortProxy
&proxy
= portProxy(tc
);
346 proxy
.readString(str
, arg
.addr
);
347 std::cout
.write(str
.c_str(), str
.size());
352 ArmSemihosting::RetErrno
353 ArmSemihosting::callWrite(ThreadContext
*tc
, Handle handle
, Addr addr
,
356 if (handle
> files
.size() || !files
[handle
])
357 return RetErrno(size
, EBADF
);
359 std::vector
<uint8_t> buffer(size
);
360 portProxy(tc
).readBlob(addr
, buffer
.data(), buffer
.size());
362 int64_t ret
= files
[handle
]->write(buffer
.data(), buffer
.size());
364 // No bytes written (we're returning the number of bytes not
366 return RetErrno(size
, -ret
);
368 // Return the number of bytes not written
369 return RetErrno(size
- ret
, 0);
373 ArmSemihosting::RetErrno
374 ArmSemihosting::callRead(ThreadContext
*tc
, Handle handle
, Addr addr
,
377 if (handle
> files
.size() || !files
[handle
])
378 return RetErrno(size
, EBADF
);
380 std::vector
<uint8_t> buffer(size
);
381 int64_t ret
= files
[handle
]->read(buffer
.data(), buffer
.size());
383 return RetErrno(size
, -ret
);
385 panic_if(ret
> buffer
.size(), "Read longer than buffer size.");
387 portProxy(tc
).writeBlob(addr
, buffer
.data(), ret
);
389 // Return the number of bytes not written
390 return retOK(size
- ret
);
394 ArmSemihosting::RetErrno
395 ArmSemihosting::callReadC(ThreadContext
*tc
)
397 return retOK((char)std::cin
.get());
400 ArmSemihosting::RetErrno
401 ArmSemihosting::callIsError(ThreadContext
*tc
, int64_t status
)
403 return retOK(status
< 0 ? 1 : 0);
406 ArmSemihosting::RetErrno
407 ArmSemihosting::callIsTTY(ThreadContext
*tc
, Handle handle
)
409 if (handle
> files
.size() || !files
[handle
])
410 return retError(EBADF
);
412 int64_t ret
= files
[handle
]->isTTY();
414 return retError(-ret
);
416 return retOK(ret
? 1 : 0);
420 ArmSemihosting::RetErrno
421 ArmSemihosting::callSeek(ThreadContext
*tc
, Handle handle
, uint64_t pos
)
423 if (handle
> files
.size() || !files
[handle
])
424 return retError(EBADF
);
426 int64_t ret
= files
[handle
]->seek(pos
);
428 return retError(-ret
);
434 ArmSemihosting::RetErrno
435 ArmSemihosting::callFLen(ThreadContext
*tc
, Handle handle
)
437 if (handle
> files
.size() || !files
[handle
])
438 return retError(EBADF
);
440 int64_t ret
= files
[handle
]->flen();
442 return retError(-ret
);
448 ArmSemihosting::RetErrno
449 ArmSemihosting::callTmpNam(ThreadContext
*tc
, Addr addr
, uint64_t id
,
452 std::vector
<char> buf(L_tmpnam
);
453 char *path
= tmpnam(buf
.data());
455 return retError(EINVAL
);
457 const size_t path_len
= strlen(path
);
458 if (path_len
>= size
)
459 return retError(ENOSPC
);
461 portProxy(tc
).writeBlob(addr
, path
, path_len
+ 1);
465 ArmSemihosting::RetErrno
466 ArmSemihosting::callRemove(ThreadContext
*tc
, Addr name_base
, size_t name_size
)
468 std::string fname
= readString(tc
, name_base
, name_size
);
470 if (remove(fname
.c_str()) != 0) {
471 return retError(errno
);
477 ArmSemihosting::RetErrno
478 ArmSemihosting::callRename(ThreadContext
*tc
, Addr from_addr
, size_t from_size
,
479 Addr to_addr
, size_t to_size
)
481 std::string from
= readString(tc
, from_addr
, from_size
);
482 std::string to
= readString(tc
, to_addr
, to_size
);
484 if (rename(from
.c_str(), to
.c_str()) != 0) {
485 return retError(errno
);
491 ArmSemihosting::RetErrno
492 ArmSemihosting::callClock(ThreadContext
*tc
)
494 return retOK(curTick() / (SimClock::Int::s
/ 100));
497 ArmSemihosting::RetErrno
498 ArmSemihosting::callTime(ThreadContext
*tc
)
500 return retOK(timeBase
+ round(curTick() / SimClock::Float::s
));
503 ArmSemihosting::RetErrno
504 ArmSemihosting::callSystem(ThreadContext
*tc
, Addr cmd_addr
, size_t cmd_size
)
506 const std::string cmd
= readString(tc
, cmd_addr
, cmd_size
);
507 warn("Semihosting: SYS_SYSTEM not implemented. Guest tried to run: %s\n",
509 return retError(EINVAL
);
513 ArmSemihosting::RetErrno
514 ArmSemihosting::callErrno(ThreadContext
*tc
)
516 // Preserve errno by returning it in errno as well.
517 return RetErrno(semiErrno
, semiErrno
);
520 ArmSemihosting::RetErrno
521 ArmSemihosting::callGetCmdLine(ThreadContext
*tc
, Addr addr
,
524 PortProxy
&proxy
= portProxy(tc
);
525 ByteOrder endian
= ArmISA::byteOrder(tc
);
526 size_t size
= size_arg
.read(tc
, endian
);
528 if (cmdLine
.size() + 1 < size
) {
529 proxy
.writeBlob(addr
, cmdLine
.c_str(), cmdLine
.size() + 1);
530 size_arg
.write(tc
, cmdLine
.size(), endian
);
538 ArmSemihosting::gatherHeapInfo(ThreadContext
*tc
, bool aarch64
,
539 Addr
&heap_base
, Addr
&heap_limit
,
540 Addr
&stack_base
, Addr
&stack_limit
)
542 const PhysicalMemory
&phys
= tc
->getSystemPtr()->getPhysMem();
543 const AddrRangeList memories
= phys
.getConfAddrRanges();
544 fatal_if(memories
.size() < 1, "No memories reported from System");
545 warn_if(memories
.size() > 1, "Multiple physical memory ranges available. "
546 "Using first range heap/stack.");
547 const AddrRange memory
= *memories
.begin();
548 const Addr mem_start
= memory
.start() + memReserve
;
549 Addr mem_end
= memory
.end();
551 // Make sure that 32-bit guests can access their memory.
553 const Addr phys_max
= (1ULL << 32) - 1;
554 panic_if(mem_start
> phys_max
,
555 "Physical memory out of range for a 32-bit guest.");
556 if (mem_end
> phys_max
) {
557 warn("Some physical memory out of range for a 32-bit guest.");
562 fatal_if(mem_start
+ stackSize
>= mem_end
,
563 "Physical memory too small to fit desired stack and a heap.");
565 heap_base
= mem_start
;
566 heap_limit
= mem_end
- stackSize
+ 1;
567 stack_base
= (mem_end
+ 1) & ~0x7ULL
; // 8 byte stack alignment
568 stack_limit
= heap_limit
;
570 inform("Reporting heap/stack info to guest:\n"
571 "\tHeap base: 0x%x\n"
572 "\tHeap limit: 0x%x\n"
573 "\tStack base: 0x%x\n"
574 "\tStack limit: 0x%x\n",
575 heap_base
, heap_limit
, stack_base
, stack_limit
);
578 ArmSemihosting::RetErrno
579 ArmSemihosting::callHeapInfo32(ThreadContext
*tc
, Addr block_addr
)
581 uint64_t heap_base
, heap_limit
, stack_base
, stack_limit
;
582 gatherHeapInfo(tc
, false, heap_base
, heap_limit
, stack_base
, stack_limit
);
584 std::array
<uint32_t, 4> block
= {{
585 (uint32_t)heap_base
, (uint32_t)heap_limit
,
586 (uint32_t)stack_base
, (uint32_t)stack_limit
588 portProxy(tc
).write(block_addr
, block
, ArmISA::byteOrder(tc
));
593 ArmSemihosting::RetErrno
594 ArmSemihosting::callHeapInfo64(ThreadContext
*tc
, Addr block_addr
)
596 uint64_t heap_base
, heap_limit
, stack_base
, stack_limit
;
597 gatherHeapInfo(tc
, true, heap_base
, heap_limit
, stack_base
, stack_limit
);
599 std::array
<uint64_t, 4> block
= {{
600 heap_base
, heap_limit
, stack_base
, stack_limit
602 portProxy(tc
).write(block_addr
, block
, ArmISA::byteOrder(tc
));
607 ArmSemihosting::RetErrno
608 ArmSemihosting::callExit32(ThreadContext
*tc
, InPlaceArg code
)
610 semiExit(code
.addr
, 0);
614 ArmSemihosting::RetErrno
615 ArmSemihosting::callExit64(ThreadContext
*tc
, uint64_t code
, uint64_t subcode
)
617 semiExit(code
, subcode
);
621 ArmSemihosting::RetErrno
622 ArmSemihosting::callExitExtended(ThreadContext
*tc
,
623 uint64_t code
, uint64_t subcode
)
625 semiExit(code
, subcode
);
630 ArmSemihosting::semiExit(uint64_t code
, uint64_t subcode
)
632 auto it
= exitCodes
.find(code
);
633 if (it
!= exitCodes
.end()) {
634 exitSimLoop(it
->second
, subcode
);
636 exitSimLoop(csprintf("semi:0x%x", code
), subcode
);
641 ArmSemihosting::RetErrno
642 ArmSemihosting::callElapsed32(ThreadContext
*tc
, InPlaceArg low
,
645 ByteOrder endian
= ArmISA::byteOrder(tc
);
646 uint64_t tick
= semiTick(curTick());
648 low
.write(tc
, tick
, endian
);
649 high
.write(tc
, tick
>> 32, endian
);
655 ArmSemihosting::RetErrno
656 ArmSemihosting::callElapsed64(ThreadContext
*tc
, InPlaceArg ticks
)
658 ticks
.write(tc
, semiTick(curTick()), ArmISA::byteOrder(tc
));
663 ArmSemihosting::RetErrno
664 ArmSemihosting::callTickFreq(ThreadContext
*tc
)
666 return retOK(semiTick(SimClock::Frequency
));
670 struct SemiPseudoAbi32
: public ArmSemihosting::Abi32
672 class State
: public ArmSemihosting::Abi32::State
675 State(const ThreadContext
*tc
) : ArmSemihosting::Abi32::State(tc
)
677 // Use getAddr() to skip the func number in the first slot.
683 struct SemiPseudoAbi64
: public ArmSemihosting::Abi64
685 class State
: public ArmSemihosting::Abi64::State
688 State(const ThreadContext
*tc
) : ArmSemihosting::Abi64::State(tc
)
690 // Use getAddr() to skip the func number in the first slot.
699 // Ignore return values since those will be handled by semihosting.
700 template <typename T
>
701 struct Result
<SemiPseudoAbi32
, T
>
703 static void store(ThreadContext
*tc
, const T
&ret
) {}
705 template <typename T
>
706 struct Result
<SemiPseudoAbi64
, T
>
708 static void store(ThreadContext
*tc
, const T
&ret
) {}
711 // Handle arguments the same as for semihosting operations. Skipping the first
712 // slot is handled internally by the State type.
713 template <typename T
>
714 struct Argument
<SemiPseudoAbi32
, T
> :
715 public Argument
<ArmSemihosting::Abi32
, T
>
717 template <typename T
>
718 struct Argument
<SemiPseudoAbi64
, T
> :
719 public Argument
<ArmSemihosting::Abi64
, T
>
722 } // namespace GuestABI
724 ArmSemihosting::RetErrno
725 ArmSemihosting::callGem5PseudoOp32(ThreadContext
*tc
, uint32_t encoded_func
)
728 PseudoInst::decodeAddrOffset(encoded_func
, func
);
731 if (PseudoInst::pseudoInst
<SemiPseudoAbi32
>(tc
, func
, ret
))
734 return retError(EINVAL
);
737 ArmSemihosting::RetErrno
738 ArmSemihosting::callGem5PseudoOp64(ThreadContext
*tc
, uint64_t encoded_func
)
741 PseudoInst::decodeAddrOffset(encoded_func
, func
);
744 if (PseudoInst::pseudoInst
<SemiPseudoAbi64
>(tc
, func
, ret
))
747 return retError(EINVAL
);
751 ArmSemihosting::getSTDIO(const char *stream_name
,
752 const std::string
&name
, const char *mode
)
754 auto it
= stdioMap
.find(name
);
755 if (it
== stdioMap
.end()) {
756 FILE *f
= fopen(name
.c_str(), mode
);
758 fatal("Failed to open %s (%s): %s\n",
759 stream_name
, name
, strerror(errno
));
767 std::unique_ptr
<ArmSemihosting::FileBase
>
768 ArmSemihosting::FileBase::create(
769 ArmSemihosting
&parent
, const std::string
&fname
, const char *mode
)
771 std::unique_ptr
<FileBase
> file
;
772 if (fname
== ":semihosting-features") {
773 file
.reset(new FileFeatures(parent
, fname
.c_str(), mode
));
775 file
.reset(new File(parent
, fname
.c_str(), mode
));
781 std::unique_ptr
<ArmSemihosting::FileBase
>
782 ArmSemihosting::FileBase::create(ArmSemihosting
&parent
,
783 CheckpointIn
&cp
, const std::string
&sec
)
785 std::unique_ptr
<FileBase
> file
;
786 ScopedCheckpointSection
_sec(cp
, sec
);
788 // Was the file open when the checkpoint was created?
789 if (!cp
.sectionExists(Serializable::currentSection()))
792 std::string fname
, mode
;
793 paramIn(cp
, "name", fname
);
794 paramIn(cp
, "mode", mode
);
795 file
= create(parent
, fname
, mode
.c_str());
797 file
->unserialize(cp
);
803 ArmSemihosting::FileBase::serialize(CheckpointOut
&cp
) const
805 paramOut(cp
, "name", _name
);
806 SERIALIZE_SCALAR(mode
);
810 ArmSemihosting::FileBase::unserialize(CheckpointIn
&cp
)
812 /* Unserialization of name and mode happens in
813 * ArmSemihosting::FileBase::create() */
817 ArmSemihosting::FileBase::read(uint8_t *buffer
, uint64_t size
)
823 ArmSemihosting::FileBase::write(const uint8_t *buffer
, uint64_t size
)
829 ArmSemihosting::FileBase::seek(uint64_t pos
)
835 ArmSemihosting::FileBase::flen()
841 ArmSemihosting::FileFeatures::FileFeatures(
842 ArmSemihosting
&_parent
, const char *_name
, const char *_mode
)
843 : FileBase(_parent
, _name
, _mode
)
848 ArmSemihosting::FileFeatures::read(uint8_t *buffer
, uint64_t size
)
852 for (; pos
< size
&& pos
< ArmSemihosting::features
.size(); pos
++)
853 buffer
[len
++] = ArmSemihosting::features
[pos
];
859 ArmSemihosting::FileFeatures::seek(uint64_t _pos
)
861 if (_pos
< ArmSemihosting::features
.size()) {
870 ArmSemihosting::FileFeatures::serialize(CheckpointOut
&cp
) const
872 FileBase::serialize(cp
);
873 SERIALIZE_SCALAR(pos
);
877 ArmSemihosting::FileFeatures::unserialize(CheckpointIn
&cp
)
879 FileBase::unserialize(cp
);
880 UNSERIALIZE_SCALAR(pos
);
885 ArmSemihosting::File::File(ArmSemihosting
&_parent
,
886 const char *_name
, const char *_perms
)
887 : FileBase(_parent
, _name
, _perms
),
892 ArmSemihosting::File::~File()
899 ArmSemihosting::File::openImpl(bool in_cpt
)
901 panic_if(file
, "Trying to open an already open file.\n");
903 if (_name
== ":tt") {
904 if (mode
[0] == 'r') {
906 } else if (mode
[0] == 'w') {
907 file
= parent
.stdout
;
908 } else if (mode
[0] == 'a') {
909 file
= parent
.stderr
;
911 warn("Unknown file mode for the ':tt' special file");
915 std::string
real_mode(this->mode
);
916 // Avoid truncating the file if we are restoring from a
918 if (in_cpt
&& real_mode
[0] == 'w')
921 file
= fopen(_name
.c_str(), real_mode
.c_str());
924 return file
? 0 : -errno
;
928 ArmSemihosting::File::close()
930 panic_if(!file
, "Trying to close an already closed file.\n");
941 ArmSemihosting::File::isTTY() const
943 return file
== parent
.stdout
||
944 file
== parent
.stderr
||
945 file
== parent
.stdin
;
949 ArmSemihosting::File::read(uint8_t *buffer
, uint64_t size
)
951 panic_if(!file
, "Trying to read from a closed file");
953 size_t ret
= fread(buffer
, 1, size
, file
);
955 // Error or EOF. Assume errors are due to invalid file
956 // operations (e.g., reading a write-only stream).
957 return ferror(file
) ? -EINVAL
: 0;
964 ArmSemihosting::File::write(const uint8_t *buffer
, uint64_t size
)
966 panic_if(!file
, "Trying to write to a closed file");
969 size_t ret
= fwrite(buffer
, 1, size
, file
);
971 // Assume errors are due to invalid file operations (e.g.,
972 // writing a read-only stream).
980 ArmSemihosting::File::seek(uint64_t _pos
)
982 panic_if(!file
, "Trying to seek in a closed file");
985 if (fseek(file
, _pos
, SEEK_SET
) == 0)
992 ArmSemihosting::File::flen()
995 long pos
= ftell(file
);
999 if (fseek(file
, 0, SEEK_END
) != 0)
1002 long len
= ftell(file
);
1006 if (fseek(file
, pos
, SEEK_SET
) != 0)
1014 ArmSemihosting::File::serialize(CheckpointOut
&cp
) const
1016 FileBase::serialize(cp
);
1019 long pos
= file
? ftell(file
) : 0;
1020 panic_if(pos
< 0, "Failed to get file position.");
1021 SERIALIZE_SCALAR(pos
);
1026 ArmSemihosting::File::unserialize(CheckpointIn
&cp
)
1028 FileBase::unserialize(cp
);
1030 if (openImpl(true) < 0) {
1031 fatal("Failed to open file: %s", _name
);
1036 UNSERIALIZE_SCALAR(pos
);
1037 if (fseek(file
, pos
, SEEK_SET
) != 0) {
1038 fatal("Failed seek to current position (%i) in '%s'", pos
, _name
);
1044 operator << (std::ostream
&os
, const ArmSemihosting::InPlaceArg
&ipa
)
1046 ccprintf(os
, "[%#x-%#x)", ipa
.addr
, ipa
.addr
+ ipa
.size
- 1);
1052 ArmSemihostingParams::create()
1054 return new ArmSemihosting(this);