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"
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"
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
} },
81 { "SYS_GET_CMDLINE", &ArmSemihosting::callGetCmdLine
} },
82 { SYS_HEAPINFO
, { "SYS_HEAPINFO", &ArmSemihosting::callHeapInfo32
,
83 &ArmSemihosting::callHeapInfo64
} },
85 { SYS_EXIT
, { "SYS_EXIT", &ArmSemihosting::callExit32
,
86 &ArmSemihosting::callExit64
} },
88 { "SYS_EXIT_EXTENDED", &ArmSemihosting::callExitExtended
} },
90 { SYS_ELAPSED
, { "SYS_ELAPSED", &ArmSemihosting::callElapsed32
,
91 &ArmSemihosting::callElapsed64
} },
92 { SYS_TICKFREQ
, { "SYS_TICKFREQ", &ArmSemihosting::callTickFreq
} },
94 { "SYS_GEM5_PSEUDO_OP", &ArmSemihosting::callGem5PseudoOp32
,
95 &ArmSemihosting::callGem5PseudoOp64
} },
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",
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" },
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" },
127 const std::vector
<uint8_t> ArmSemihosting::features
{
128 0x53, 0x48, 0x46, 0x42, // Magic
129 0x3, // EXT_EXIT_EXTENDED, EXT_STDOUT_STDERR
132 const std::map
<const std::string
, FILE *> ArmSemihosting::stdioMap
{
136 {"stdout", ::stdout
},
138 {"stderr", ::stderr
},
141 ArmSemihosting::ArmSemihosting(const ArmSemihostingParams
&p
)
144 memReserve(p
.mem_reserve
),
145 stackSize(p
.stack_size
),
146 timeBase([p
]{ struct tm t
= p
.time
; return mkutctime(&t
); }()),
147 tickShift(calcTickShift()),
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"))
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);
162 inform("Semihosting: Shifting elapsed ticks by %i bits.",
167 ArmSemihosting::call64(ThreadContext
*tc
, bool gem5_ops
)
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
);
176 auto it
= calls
.find(op
);
177 if (it
== calls
.end()) {
178 unrecognizedCall
<Abi64
>(
179 tc
, "Unknown aarch64 semihosting call: op = 0x%x", op
);
182 const SemiCall
&call
= it
->second
;
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
);
193 ArmSemihosting::call32(ThreadContext
*tc
, bool gem5_ops
)
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
);
202 auto it
= calls
.find(op
);
203 if (it
== calls
.end()) {
204 unrecognizedCall
<Abi32
>(
205 tc
, "Unknown aarch32 semihosting call: op = 0x%x", op
);
208 const SemiCall
&call
= it
->second
;
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
);
219 ArmSemihosting::serialize(CheckpointOut
&cp
) const
221 SERIALIZE_SCALAR(semiErrno
);
223 paramOut(cp
, "num_files", files
.size());
224 for (int i
= 0; i
< files
.size(); i
++) {
229 files
[i
]->serializeSection(cp
, csprintf("file%i", i
));
234 ArmSemihosting::unserialize(CheckpointIn
&cp
)
236 UNSERIALIZE_SCALAR(semiErrno
);
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
));
246 ArmSemihosting::portProxy(ThreadContext
*tc
)
248 static std::unique_ptr
<PortProxy
> port_proxy_s
;
249 static System
*secure_sys
= nullptr;
251 if (ArmISA::isSecure(tc
)) {
252 System
*sys
= tc
->getSystemPtr();
253 if (sys
!= secure_sys
) {
256 new TranslatingPortProxy(tc
, Request::SECURE
));
259 new SETranslatingPortProxy(
260 tc
, SETranslatingPortProxy::NextPage
,
265 return *port_proxy_s
;
267 return tc
->getVirtProxy();
273 ArmSemihosting::readString(ThreadContext
*tc
, Addr ptr
, size_t len
)
275 std::vector
<char> buf(len
+ 1);
278 portProxy(tc
).readBlob(ptr
, buf
.data(), len
);
280 return std::string(buf
.data());
283 ArmSemihosting::RetErrno
284 ArmSemihosting::callOpen(ThreadContext
*tc
, const Addr name_base
,
285 int fmode
, size_t name_size
)
287 const char *mode
= fmode
< fmodes
.size() ? fmodes
[fmode
] : nullptr;
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
);
294 std::string fname
= readString(tc
, name_base
, name_size
);
295 if (!fname
.empty() && fname
.front() != '/')
296 fname
= filesRootDir
+ fname
;
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
);
304 return retError(-ret
);
306 files
.push_back(std::move(file
));
307 return retOK(files
.size() - 1);
311 ArmSemihosting::RetErrno
312 ArmSemihosting::callClose(ThreadContext
*tc
, Handle handle
)
314 if (handle
> files
.size()) {
315 DPRINTF(Semihosting
, "Semihosting SYS_CLOSE(%i): Illegal file\n");
316 return retError(EBADF
);
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
);
324 return retError(-error
);
326 // Zap the pointer and free the entry in the file table as
328 files
[handle
].reset();
333 ArmSemihosting::RetErrno
334 ArmSemihosting::callWriteC(ThreadContext
*tc
, InPlaceArg arg
)
336 const char c
= portProxy(tc
).read
<char>(arg
.addr
);
338 DPRINTF(Semihosting
, "Semihosting SYS_WRITEC('%c')\n", c
);
344 ArmSemihosting::RetErrno
345 ArmSemihosting::callWrite0(ThreadContext
*tc
, InPlaceArg arg
)
347 DPRINTF(Semihosting
, "Semihosting SYS_WRITE0(...)\n");
348 PortProxy
&proxy
= portProxy(tc
);
350 proxy
.readString(str
, arg
.addr
);
351 std::cout
.write(str
.c_str(), str
.size());
356 ArmSemihosting::RetErrno
357 ArmSemihosting::callWrite(ThreadContext
*tc
, Handle handle
, Addr addr
,
360 if (handle
> files
.size() || !files
[handle
])
361 return RetErrno(size
, EBADF
);
363 std::vector
<uint8_t> buffer(size
);
364 portProxy(tc
).readBlob(addr
, buffer
.data(), buffer
.size());
366 int64_t ret
= files
[handle
]->write(buffer
.data(), buffer
.size());
368 // No bytes written (we're returning the number of bytes not
370 return RetErrno(size
, -ret
);
372 // Return the number of bytes not written
373 return RetErrno(size
- ret
, 0);
377 ArmSemihosting::RetErrno
378 ArmSemihosting::callRead(ThreadContext
*tc
, Handle handle
, Addr addr
,
381 if (handle
> files
.size() || !files
[handle
])
382 return RetErrno(size
, EBADF
);
384 std::vector
<uint8_t> buffer(size
);
385 int64_t ret
= files
[handle
]->read(buffer
.data(), buffer
.size());
387 return RetErrno(size
, -ret
);
389 panic_if(ret
> buffer
.size(), "Read longer than buffer size.");
391 portProxy(tc
).writeBlob(addr
, buffer
.data(), ret
);
393 // Return the number of bytes not written
394 return retOK(size
- ret
);
398 ArmSemihosting::RetErrno
399 ArmSemihosting::callReadC(ThreadContext
*tc
)
401 return retOK((char)std::cin
.get());
404 ArmSemihosting::RetErrno
405 ArmSemihosting::callIsError(ThreadContext
*tc
, int64_t status
)
407 return retOK(status
< 0 ? 1 : 0);
410 ArmSemihosting::RetErrno
411 ArmSemihosting::callIsTTY(ThreadContext
*tc
, Handle handle
)
413 if (handle
> files
.size() || !files
[handle
])
414 return retError(EBADF
);
416 int64_t ret
= files
[handle
]->isTTY();
418 return retError(-ret
);
420 return retOK(ret
? 1 : 0);
424 ArmSemihosting::RetErrno
425 ArmSemihosting::callSeek(ThreadContext
*tc
, Handle handle
, uint64_t pos
)
427 if (handle
> files
.size() || !files
[handle
])
428 return retError(EBADF
);
430 int64_t ret
= files
[handle
]->seek(pos
);
432 return retError(-ret
);
438 ArmSemihosting::RetErrno
439 ArmSemihosting::callFLen(ThreadContext
*tc
, Handle handle
)
441 if (handle
> files
.size() || !files
[handle
])
442 return retError(EBADF
);
444 int64_t ret
= files
[handle
]->flen();
446 return retError(-ret
);
452 ArmSemihosting::RetErrno
453 ArmSemihosting::callTmpNam(ThreadContext
*tc
, Addr addr
, uint64_t id
,
456 std::string path
= "";
457 int64_t unlink_call_ret
= 0;
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
));
466 const size_t path_len
= path
.length();
467 if (path_len
>= size
)
468 return retError(ENOSPC
);
470 portProxy(tc
).writeBlob(addr
, path
.c_str(), path_len
+ 1);
474 ArmSemihosting::RetErrno
475 ArmSemihosting::callRemove(ThreadContext
*tc
, Addr name_base
, size_t name_size
)
477 std::string fname
= readString(tc
, name_base
, name_size
);
479 if (remove(fname
.c_str()) != 0) {
480 return retError(errno
);
486 ArmSemihosting::RetErrno
487 ArmSemihosting::callRename(ThreadContext
*tc
, Addr from_addr
, size_t from_size
,
488 Addr to_addr
, size_t to_size
)
490 std::string from
= readString(tc
, from_addr
, from_size
);
491 std::string to
= readString(tc
, to_addr
, to_size
);
493 if (rename(from
.c_str(), to
.c_str()) != 0) {
494 return retError(errno
);
500 ArmSemihosting::RetErrno
501 ArmSemihosting::callClock(ThreadContext
*tc
)
503 return retOK(curTick() / (SimClock::Int::s
/ 100));
506 ArmSemihosting::RetErrno
507 ArmSemihosting::callTime(ThreadContext
*tc
)
509 return retOK(timeBase
+ round(curTick() / SimClock::Float::s
));
512 ArmSemihosting::RetErrno
513 ArmSemihosting::callSystem(ThreadContext
*tc
, Addr cmd_addr
, size_t cmd_size
)
515 const std::string cmd
= readString(tc
, cmd_addr
, cmd_size
);
516 warn("Semihosting: SYS_SYSTEM not implemented. Guest tried to run: %s\n",
518 return retError(EINVAL
);
522 ArmSemihosting::RetErrno
523 ArmSemihosting::callErrno(ThreadContext
*tc
)
525 // Preserve errno by returning it in errno as well.
526 return RetErrno(semiErrno
, semiErrno
);
529 ArmSemihosting::RetErrno
530 ArmSemihosting::callGetCmdLine(ThreadContext
*tc
, Addr addr
,
533 PortProxy
&proxy
= portProxy(tc
);
534 ByteOrder endian
= ArmISA::byteOrder(tc
);
535 size_t size
= size_arg
.read(tc
, endian
);
537 if (cmdLine
.size() + 1 < size
) {
538 proxy
.writeBlob(addr
, cmdLine
.c_str(), cmdLine
.size() + 1);
539 size_arg
.write(tc
, cmdLine
.size(), endian
);
547 ArmSemihosting::gatherHeapInfo(ThreadContext
*tc
, bool aarch64
,
548 Addr
&heap_base
, Addr
&heap_limit
,
549 Addr
&stack_base
, Addr
&stack_limit
)
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();
560 // Make sure that 32-bit guests can access their memory.
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.");
571 fatal_if(mem_start
+ stackSize
>= mem_end
,
572 "Physical memory too small to fit desired stack and a heap.");
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
;
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
);
587 ArmSemihosting::RetErrno
588 ArmSemihosting::callHeapInfo32(ThreadContext
*tc
, Addr block_addr
)
590 uint64_t heap_base
, heap_limit
, stack_base
, stack_limit
;
591 gatherHeapInfo(tc
, false, heap_base
, heap_limit
, stack_base
, stack_limit
);
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
597 portProxy(tc
).write(block_addr
, block
, ArmISA::byteOrder(tc
));
602 ArmSemihosting::RetErrno
603 ArmSemihosting::callHeapInfo64(ThreadContext
*tc
, Addr block_addr
)
605 uint64_t heap_base
, heap_limit
, stack_base
, stack_limit
;
606 gatherHeapInfo(tc
, true, heap_base
, heap_limit
, stack_base
, stack_limit
);
608 std::array
<uint64_t, 4> block
= {{
609 heap_base
, heap_limit
, stack_base
, stack_limit
611 portProxy(tc
).write(block_addr
, block
, ArmISA::byteOrder(tc
));
616 ArmSemihosting::RetErrno
617 ArmSemihosting::callExit32(ThreadContext
*tc
, InPlaceArg code
)
619 semiExit(code
.addr
, 0);
623 ArmSemihosting::RetErrno
624 ArmSemihosting::callExit64(ThreadContext
*tc
, uint64_t code
, uint64_t subcode
)
626 semiExit(code
, subcode
);
630 ArmSemihosting::RetErrno
631 ArmSemihosting::callExitExtended(ThreadContext
*tc
,
632 uint64_t code
, uint64_t subcode
)
634 semiExit(code
, subcode
);
639 ArmSemihosting::semiExit(uint64_t code
, uint64_t subcode
)
641 auto it
= exitCodes
.find(code
);
642 if (it
!= exitCodes
.end()) {
643 exitSimLoop(it
->second
, subcode
);
645 exitSimLoop(csprintf("semi:0x%x", code
), subcode
);
650 ArmSemihosting::RetErrno
651 ArmSemihosting::callElapsed32(ThreadContext
*tc
, InPlaceArg low
,
654 ByteOrder endian
= ArmISA::byteOrder(tc
);
655 uint64_t tick
= semiTick(curTick());
657 low
.write(tc
, tick
, endian
);
658 high
.write(tc
, tick
>> 32, endian
);
664 ArmSemihosting::RetErrno
665 ArmSemihosting::callElapsed64(ThreadContext
*tc
, InPlaceArg ticks
)
667 ticks
.write(tc
, semiTick(curTick()), ArmISA::byteOrder(tc
));
672 ArmSemihosting::RetErrno
673 ArmSemihosting::callTickFreq(ThreadContext
*tc
)
675 return retOK(semiTick(SimClock::Frequency
));
679 struct SemiPseudoAbi32
: public ArmSemihosting::Abi32
681 class State
: public ArmSemihosting::Abi32::State
684 State(const ThreadContext
*tc
) : ArmSemihosting::Abi32::State(tc
)
686 // Use getAddr() to skip the func number in the first slot.
692 struct SemiPseudoAbi64
: public ArmSemihosting::Abi64
694 class State
: public ArmSemihosting::Abi64::State
697 State(const ThreadContext
*tc
) : ArmSemihosting::Abi64::State(tc
)
699 // Use getAddr() to skip the func number in the first slot.
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
>
714 template <typename T
>
715 struct Argument
<SemiPseudoAbi64
, T
> :
716 public Argument
<ArmSemihosting::Abi64
, T
>
719 } // namespace GuestABI
721 ArmSemihosting::RetErrno
722 ArmSemihosting::callGem5PseudoOp32(ThreadContext
*tc
, uint32_t encoded_func
)
725 PseudoInst::decodeAddrOffset(encoded_func
, func
);
728 if (PseudoInst::pseudoInst
<SemiPseudoAbi32
>(tc
, func
, ret
))
731 return retError(EINVAL
);
734 ArmSemihosting::RetErrno
735 ArmSemihosting::callGem5PseudoOp64(ThreadContext
*tc
, uint64_t encoded_func
)
738 PseudoInst::decodeAddrOffset(encoded_func
, func
);
741 if (PseudoInst::pseudoInst
<SemiPseudoAbi64
>(tc
, func
, ret
))
744 return retError(EINVAL
);
748 ArmSemihosting::getSTDIO(const char *stream_name
,
749 const std::string
&name
, const char *mode
)
751 auto it
= stdioMap
.find(name
);
752 if (it
== stdioMap
.end()) {
753 FILE *f
= fopen(name
.c_str(), mode
);
755 fatal("Failed to open %s (%s): %s\n",
756 stream_name
, name
, strerror(errno
));
764 std::unique_ptr
<ArmSemihosting::FileBase
>
765 ArmSemihosting::FileBase::create(
766 ArmSemihosting
&parent
, const std::string
&fname
, const char *mode
)
768 std::unique_ptr
<FileBase
> file
;
769 if (fname
== ":semihosting-features") {
770 file
.reset(new FileFeatures(parent
, fname
.c_str(), mode
));
772 file
.reset(new File(parent
, fname
.c_str(), mode
));
778 std::unique_ptr
<ArmSemihosting::FileBase
>
779 ArmSemihosting::FileBase::create(ArmSemihosting
&parent
,
780 CheckpointIn
&cp
, const std::string
&sec
)
782 std::unique_ptr
<FileBase
> file
;
783 ScopedCheckpointSection
_sec(cp
, sec
);
785 // Was the file open when the checkpoint was created?
786 if (!cp
.sectionExists(Serializable::currentSection()))
789 std::string fname
, mode
;
790 paramIn(cp
, "name", fname
);
791 paramIn(cp
, "mode", mode
);
792 file
= create(parent
, fname
, mode
.c_str());
794 file
->unserialize(cp
);
800 ArmSemihosting::FileBase::serialize(CheckpointOut
&cp
) const
802 paramOut(cp
, "name", _name
);
803 SERIALIZE_SCALAR(mode
);
807 ArmSemihosting::FileBase::unserialize(CheckpointIn
&cp
)
809 /* Unserialization of name and mode happens in
810 * ArmSemihosting::FileBase::create() */
814 ArmSemihosting::FileBase::read(uint8_t *buffer
, uint64_t size
)
820 ArmSemihosting::FileBase::write(const uint8_t *buffer
, uint64_t size
)
826 ArmSemihosting::FileBase::seek(uint64_t pos
)
832 ArmSemihosting::FileBase::flen()
838 ArmSemihosting::FileFeatures::FileFeatures(
839 ArmSemihosting
&_parent
, const char *_name
, const char *_mode
)
840 : FileBase(_parent
, _name
, _mode
)
845 ArmSemihosting::FileFeatures::read(uint8_t *buffer
, uint64_t size
)
849 for (; pos
< size
&& pos
< ArmSemihosting::features
.size(); pos
++)
850 buffer
[len
++] = ArmSemihosting::features
[pos
];
856 ArmSemihosting::FileFeatures::seek(uint64_t _pos
)
858 if (_pos
< ArmSemihosting::features
.size()) {
867 ArmSemihosting::FileFeatures::serialize(CheckpointOut
&cp
) const
869 FileBase::serialize(cp
);
870 SERIALIZE_SCALAR(pos
);
874 ArmSemihosting::FileFeatures::unserialize(CheckpointIn
&cp
)
876 FileBase::unserialize(cp
);
877 UNSERIALIZE_SCALAR(pos
);
882 ArmSemihosting::File::File(ArmSemihosting
&_parent
,
883 const char *_name
, const char *_perms
)
884 : FileBase(_parent
, _name
, _perms
),
889 ArmSemihosting::File::~File()
896 ArmSemihosting::File::openImpl(bool in_cpt
)
898 panic_if(file
, "Trying to open an already open file.\n");
900 if (_name
== ":tt") {
901 if (mode
[0] == 'r') {
903 } else if (mode
[0] == 'w') {
904 file
= parent
.stdout
;
905 } else if (mode
[0] == 'a') {
906 file
= parent
.stderr
;
908 warn("Unknown file mode for the ':tt' special file");
912 std::string
real_mode(this->mode
);
913 // Avoid truncating the file if we are restoring from a
915 if (in_cpt
&& real_mode
[0] == 'w')
918 file
= fopen(_name
.c_str(), real_mode
.c_str());
921 return file
? 0 : -errno
;
925 ArmSemihosting::File::close()
927 panic_if(!file
, "Trying to close an already closed file.\n");
938 ArmSemihosting::File::isTTY() const
940 return file
== parent
.stdout
||
941 file
== parent
.stderr
||
942 file
== parent
.stdin
;
946 ArmSemihosting::File::read(uint8_t *buffer
, uint64_t size
)
948 panic_if(!file
, "Trying to read from a closed file");
950 size_t ret
= fread(buffer
, 1, size
, file
);
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;
961 ArmSemihosting::File::write(const uint8_t *buffer
, uint64_t size
)
963 panic_if(!file
, "Trying to write to a closed file");
966 size_t ret
= fwrite(buffer
, 1, size
, file
);
968 // Assume errors are due to invalid file operations (e.g.,
969 // writing a read-only stream).
977 ArmSemihosting::File::seek(uint64_t _pos
)
979 panic_if(!file
, "Trying to seek in a closed file");
982 if (fseek(file
, _pos
, SEEK_SET
) == 0)
989 ArmSemihosting::File::flen()
992 long pos
= ftell(file
);
996 if (fseek(file
, 0, SEEK_END
) != 0)
999 long len
= ftell(file
);
1003 if (fseek(file
, pos
, SEEK_SET
) != 0)
1011 ArmSemihosting::File::serialize(CheckpointOut
&cp
) const
1013 FileBase::serialize(cp
);
1016 long pos
= file
? ftell(file
) : 0;
1017 panic_if(pos
< 0, "Failed to get file position.");
1018 SERIALIZE_SCALAR(pos
);
1023 ArmSemihosting::File::unserialize(CheckpointIn
&cp
)
1025 FileBase::unserialize(cp
);
1027 if (openImpl(true) < 0) {
1028 fatal("Failed to open file: %s", _name
);
1033 UNSERIALIZE_SCALAR(pos
);
1034 if (fseek(file
, pos
, SEEK_SET
) != 0) {
1035 fatal("Failed seek to current position (%i) in '%s'", pos
, _name
);
1041 operator << (std::ostream
&os
, const ArmSemihosting::InPlaceArg
&ipa
)
1043 ccprintf(os
, "[%#x-%#x)", ipa
.addr
, ipa
.addr
+ ipa
.size
- 1);
1049 ArmSemihostingParams::create() const
1051 return new ArmSemihosting(*this);