gem5 v24.0.0.0
Loading...
Searching...
No Matches
semihosting.hh
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
38#ifndef __ARCH_RISCV_SEMIHOSTING_HH__
39#define __ARCH_RISCV_SEMIHOSTING_HH__
40
42#include "arch/riscv/isa.hh"
44#include "cpu/thread_context.hh"
45#include "sim/guest_abi.hh"
46#include "sim/sim_object.hh"
47
48namespace gem5
49{
50
51struct RiscvSemihostingParams;
52class SerialDevice;
53
56{
57 public:
58 enum class Opcode : uint32_t
59 {
60 // https://github.com/riscv-software-src/riscv-semihosting/blob/main/
61 // riscv-semihosting-spec.adoc#21-semihosting-trap-instruction-sequence
62 Prefix = 0x01f01013, // slli x0, x0, 0x1f Entry NOP
63 EBreak = 0x00100073, // ebreak Break to debugger
64 Suffix = 0x40705013, // srai x0, x0, 7 NOP encoding semihosting
65 };
67 PortProxy &
68 portProxy(ThreadContext *tc) const override
69 {
70 return portProxyImpl(tc);
71 }
72 ByteOrder
73 byteOrder(ThreadContext *tc) const override
74 {
75 return ByteOrder::little;
76 }
77
78 template <typename ArgType>
80 {
81 using UintPtr = ArgType;
82
83 class State : public StateBase<ArgType, RiscvSemihosting>
84 {
85 public:
86 explicit
87 State(const ThreadContext *tc) :
89 tc->getReg(RiscvISA::ArgumentRegs[1]),
90 [](const ThreadContext *) {
91 return ByteOrder::little;
92 })
93 {}
94 };
95 };
96
97 struct Abi64 : public RiscvSemihostingAbi<uint64_t>
98 {};
99 struct Abi32 : public RiscvSemihostingAbi<uint32_t>
100 {};
101
103 {
104 SYS_OPEN = 0x01,
105 SYS_CLOSE = 0x02,
108 SYS_WRITE = 0x05,
109 SYS_READ = 0x06,
110 SYS_READC = 0x07,
112 SYS_ISTTY = 0x09,
113 SYS_SEEK = 0x0A,
114 SYS_FLEN = 0x0C,
118 SYS_CLOCK = 0x10,
119 SYS_TIME = 0x11,
121 SYS_ERRNO = 0x13,
124 SYS_EXIT = 0x18,
128
130
131 SYS_GEM5_PSEUDO_OP = 0x100
132 };
133
135
136 explicit RiscvSemihosting(const RiscvSemihostingParams &p);
137
140 bool call(ThreadContext *tc);
141 protected:
142 bool call64(ThreadContext *tc);
143 bool call32(ThreadContext *tc);
144 static const std::map<uint32_t, SemiCall> calls;
145};
146
147namespace guest_abi
148{
149
150template <typename Arg>
151struct Argument<RiscvSemihosting::Abi64, Arg,
152 typename std::enable_if_t<std::is_integral_v<Arg>>>
153{
154 static Arg
155 get(ThreadContext *tc, RiscvSemihosting::Abi64::State &state)
156 {
157 return state.get(tc);
158 }
159};
160
161template <typename Arg>
162struct Argument<RiscvSemihosting::Abi32, Arg,
163 typename std::enable_if_t<std::is_integral_v<Arg>>>
164{
165 static Arg
166 get(ThreadContext *tc, RiscvSemihosting::Abi32::State &state)
167 {
168 if (std::is_signed_v<Arg>)
169 return sext<32>(state.get(tc));
170 else
171 return state.get(tc);
172 }
173};
174
175template <typename Abi>
176struct Argument<Abi, RiscvSemihosting::InPlaceArg,
177 typename std::enable_if_t<
178 std::is_base_of_v<RiscvSemihosting::AbiBase, Abi>>>
179{
181 get(ThreadContext *tc, typename Abi::State &state)
182 {
184 state.getAddr(), sizeof(typename Abi::State::ArgType));
185 }
186};
187
188template <typename Abi>
189struct Result<Abi, RiscvSemihosting::RetErrno>
190{
191 static void
196};
197
198} // namespace guest_abi
199} // namespace gem5
200
201#endif // __ARCH_RISCV_SEMIHOSTING_HH__
Semihosting for AArch32, AArch64, RISCV-32 and RISCV-64: https://github.com/ARM-software/abi-aa/blob/...
The ExecContext is an abstract base class the provides the interface used by the ISA to manipulate th...
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition port_proxy.hh:87
Semihosting for RV32 and RV64.
bool call64(ThreadContext *tc)
static PortProxy & portProxyImpl(ThreadContext *tc)
PortProxy & portProxy(ThreadContext *tc) const override
RiscvSemihosting(const RiscvSemihostingParams &p)
static const std::map< uint32_t, SemiCall > calls
SemiCallBase< RiscvSemihosting, Abi32, Abi64 > SemiCall
bool call(ThreadContext *tc)
ByteOrder byteOrder(ThreadContext *tc) const override
bool isSemihostingEBreak(ExecContext *xc)
Perform a RISC-V Semihosting call.
bool call32(ThreadContext *tc)
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual void setReg(const RegId &reg, RegVal val)
STL pair class.
Definition stl.hh:58
constexpr uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition bitfield.hh:129
atomic_var_t state
Definition helpers.cc:211
Bitfield< 6 > err
Bitfield< 0 > p
constexpr RegId ArgumentRegs[]
Definition int.hh:147
constexpr auto & ReturnValueReg
Definition int.hh:143
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Semihosting call information structure.
static void store(ThreadContext *tc, const RiscvSemihosting::RetErrno &err)

Generated on Tue Jun 18 2024 16:23:57 for gem5 by doxygen 1.11.0