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_ARM_SEMIHOSTING_HH__
39#define __ARCH_ARM_SEMIHOSTING_HH__
40
41#include <cstdio>
42#include <functional>
43#include <map>
44#include <memory>
45#include <utility>
46#include <vector>
47
48#include "arch/arm/regs/int.hh"
49#include "arch/arm/utility.hh"
51#include "cpu/thread_context.hh"
52#include "mem/port_proxy.hh"
53#include "sim/core.hh"
54#include "sim/guest_abi.hh"
55#include "sim/pseudo_inst.hh"
56#include "sim/sim_object.hh"
57
58namespace gem5
59{
60
61struct ArmSemihostingParams;
62class SerialDevice;
63
66{
67 public:
68 enum
69 {
70 // Standard ARM immediate values which trigger semihosting.
71 T32Imm = 0xAB,
72 A32Imm = 0x123456,
73 A64Imm = 0xF000,
74
75 // The immediate value which enables gem5 semihosting calls. Use the
76 // standard value for thumb.
77 Gem5Imm = 0x5D57
78 };
79
81 PortProxy &portProxy(ThreadContext *tc) const override
82 {
83 return portProxyImpl(tc);
84 }
85 ByteOrder byteOrder(ThreadContext *tc) const override
86 {
87 return ArmISA::byteOrder(tc);
88 }
89
90 struct Abi64 : public AbiBase
91 {
92 using UintPtr = uint64_t;
93
94 class State : public StateBase<Abi64::UintPtr, ArmSemihosting>
95 {
96 public:
97 // For 64 bit semihosting, the params are pointer to by X1.
98 explicit
99 State(const ThreadContext *tc) :
101 tc->getReg(ArmISA::int_reg::X1), &ArmISA::byteOrder)
102 {}
103 };
104 };
105
106 struct Abi32 : public AbiBase
107 {
108 using UintPtr = uint32_t;
109
110 class State : public StateBase<Abi32::UintPtr, ArmSemihosting>
111 {
112 public:
113 // For 32 bit semihosting, the params are pointer to by R1.
114 explicit
115 State(const ThreadContext *tc) :
117 tc->getReg(ArmISA::int_reg::R1), &ArmISA::byteOrder)
118 {}
119 };
120 };
121
123 {
124 SYS_OPEN = 0x01,
125 SYS_CLOSE = 0x02,
128 SYS_WRITE = 0x05,
129 SYS_READ = 0x06,
130 SYS_READC = 0x07,
132 SYS_ISTTY = 0x09,
133 SYS_SEEK = 0x0A,
134 SYS_FLEN = 0x0C,
138 SYS_CLOCK = 0x10,
139 SYS_TIME = 0x11,
141 SYS_ERRNO = 0x13,
144 SYS_EXIT = 0x18,
148
150
151 SYS_GEM5_PSEUDO_OP = 0x100
152 };
153
155
156 explicit ArmSemihosting(const ArmSemihostingParams &p);
157
159 bool call64(ThreadContext *tc, bool gem5_ops);
161 bool call32(ThreadContext *tc, bool gem5_ops);
162
163 protected:
164 RetErrno callGem5PseudoOp32(ThreadContext *tc, uint32_t encoded_func);
165 RetErrno callGem5PseudoOp64(ThreadContext *tc, uint64_t encoded_func);
166
167 static const std::map<uint32_t, SemiCall> calls;
168};
169
170namespace guest_abi
171{
172
173template <typename Arg>
174struct Argument<ArmSemihosting::Abi64, Arg,
175 typename std::enable_if_t<
176 (std::is_integral_v<Arg> ||
177 std::is_same<Arg,pseudo_inst::GuestAddr>::value)>>
178{
179 static Arg
181 {
182 return (Arg)state.get(tc);
183 }
184};
185
186template <typename Arg>
187struct Argument<ArmSemihosting::Abi32, Arg,
188 typename std::enable_if_t<
189 (std::is_integral_v<Arg> ||
190 std::is_same<Arg,pseudo_inst::GuestAddr>::value)>>
191{
192 static Arg
194 {
195 if (std::is_signed_v<Arg>) {
196 return (Arg)sext<32>(state.get(tc));
197 }
198 else {
199 return (Arg)state.get(tc);
200 }
201 }
202};
203
204template <typename Abi>
205struct Argument<Abi, ArmSemihosting::InPlaceArg, typename std::enable_if_t<
206 std::is_base_of_v<ArmSemihosting::AbiBase, Abi>>>
207{
209 get(ThreadContext *tc, typename Abi::State &state)
210 {
212 state.getAddr(), sizeof(typename Abi::State::ArgType));
213 }
214};
215
216template <>
218{
219 static void
224};
225
226template <>
228{
229 static void
234};
235
236} // namespace guest_abi
237} // namespace gem5
238
239#endif // __ARCH_ARM_SEMIHOSTING_HH__
State(const ThreadContext *tc)
State(const ThreadContext *tc)
Semihosting for AArch32 and AArch64.
static const std::map< uint32_t, SemiCall > calls
bool call32(ThreadContext *tc, bool gem5_ops)
Perform an Arm Semihosting call from aarch32 code.
ByteOrder byteOrder(ThreadContext *tc) const override
static PortProxy & portProxyImpl(ThreadContext *tc)
bool call64(ThreadContext *tc, bool gem5_ops)
Perform an Arm Semihosting call from aarch64 code.
SemiCallBase< ArmSemihosting, Abi32, Abi64 > SemiCall
RetErrno callGem5PseudoOp32(ThreadContext *tc, uint32_t encoded_func)
ArmSemihosting(const ArmSemihostingParams &p)
RetErrno callGem5PseudoOp64(ThreadContext *tc, uint64_t encoded_func)
PortProxy & portProxy(ThreadContext *tc) const override
Semihosting for AArch32, AArch64, RISCV-32 and RISCV-64: https://github.com/ARM-software/abi-aa/blob/...
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition port_proxy.hh:87
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
constexpr RegId X0
Definition int.hh:240
constexpr RegId R0
Definition int.hh:186
ByteOrder byteOrder(const ThreadContext *tc)
Definition utility.hh:359
Bitfield< 6 > err
Bitfield< 0 > p
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
PortProxy Object Declaration.
Semihosting call information structure.
static void store(ThreadContext *tc, const ArmSemihosting::RetErrno &err)
static void store(ThreadContext *tc, const ArmSemihosting::RetErrno &err)

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