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.
37 * Authors: Andreas Sandberg
39 #ifndef __ARCH_ARM_SEMIHOSTING_HH__
40 #define __ARCH_ARM_SEMIHOSTING_HH__
48 #include "sim/sim_object.hh"
50 struct ArmSemihostingParams;
56 * Semihosting for AArch32 and AArch64
58 * This class implements the Arm semihosting interface. This interface
59 * allows baremetal code access service, such as IO, from the
60 * simulator. It is conceptually a simplified version of gem5's more
61 * general syscall emulation mode.
63 * Exits calls (SYS_EXIT, SYS_EXIT_EXTENDED) from the guest get
64 * translated into simualtion exits. Well-known exit codes are
65 * translated to messages on the form 'semi:ADP_.*' while unknown
66 * codes are returned in hex ('semi:0x..'). The subcode is reported in
67 * the gem5 exit event.
69 class ArmSemihosting : public SimObject
72 ArmSemihosting(const ArmSemihostingParams *p);
74 /** Perform an Arm Semihosting call from aarch64 code. */
75 uint64_t call64(ThreadContext *tc, uint32_t op, uint64_t param);
76 /** Perform an Arm Semihosting call from aarch32 code. */
77 uint32_t call32(ThreadContext *tc, uint32_t op, uint32_t param);
79 public: // SimObject and related interfaces
80 void serialize(CheckpointOut &cp) const override;
81 void unserialize(CheckpointIn &cp) override;
83 protected: // Configuration
84 const std::string cmdLine;
85 const Addr memReserve;
89 * Base time when the simulation started. This is used to
90 * calculate the time of date when the guest call SYS_TIME.
92 const time_t timeBase;
94 /** Number of bits to right shift gem5 ticks to fit in a uint32_t */
95 const unsigned tickShift;
97 protected: // Internal state
98 typedef uint64_t SemiErrno;
101 protected: // File IO
103 * Internal state for open files
105 * This class describes the internal state of a file opened
106 * through the semihosting interface.
108 * A file instance is normally created using one of the
109 * ArmSemihosting::FileBase::create() factory methods. These
110 * methods handle some the magic file names in the Arm Semihosting
111 * specification and instantiate the right implementation. For the
112 * same, when unserializing a checkpoint, the create method must
113 * be used to unserialize a new instance of a file descriptor.
115 class FileBase : public Serializable
118 FileBase(ArmSemihosting &_parent, const char *name, const char *_mode)
119 : parent(_parent), _name(name), mode(_mode) {}
120 virtual ~FileBase() {};
123 FileBase(FileBase &) = delete;
125 static std::unique_ptr<FileBase> create(
126 ArmSemihosting &parent, const std::string &fname,
128 static std::unique_ptr<FileBase> create(
129 ArmSemihosting &parent, CheckpointIn &cp, const std::string &sec);
131 void serialize(CheckpointOut &cp) const override;
132 void unserialize(CheckpointIn &cp) override;
134 const std::string &fileName() { return _name; }
138 * Semihosting file IO interfaces
140 * These interfaces implement common IO functionality in the
141 * Semihosting interface.
143 * All functions return a negative value that corresponds to a
144 * UNIX errno value when they fail and >=0 on success.
150 * @return <0 on error (-errno), 0 on success.
152 virtual int64_t open() { return 0; }
157 * @return <0 on error (-errno), 0 on success.
159 virtual int64_t close() { return 0; }
162 * Check if a file corresponds to a TTY device.
164 * @return True if the file is a TTY, false otherwise.
166 virtual bool isTTY() const { return false; }
169 * Read data from file.
171 * @return <0 on error (-errno), bytes read on success (0 for EOF).
173 virtual int64_t read(uint8_t *buffer, uint64_t size);
176 * Write data to file.
178 * @return <0 on error (-errno), bytes written on success.
180 virtual int64_t write(const uint8_t *buffer, uint64_t size);
183 * Seek to an absolute position in the file.
185 * @param pos Byte offset from start of file.
186 * @return <0 on error (-errno), 0 on success.
188 virtual int64_t seek(uint64_t pos);
191 * Get the length of a file in bytes.
193 * @return <0 on error (-errno), length on success
195 virtual int64_t flen();
200 ArmSemihosting &parent;
205 /** Implementation of the ':semihosting-features' magic file. */
206 class FileFeatures : public FileBase
209 FileFeatures(ArmSemihosting &_parent,
210 const char *name, const char *mode);
212 void serialize(CheckpointOut &cp) const override;
213 void unserialize(CheckpointIn &cp) override;
215 int64_t read(uint8_t *buffer, uint64_t size) override;
216 int64_t seek(uint64_t pos) override;
222 class File : public FileBase
225 File(ArmSemihosting &_parent, const char *name, const char *mode);
228 void serialize(CheckpointOut &cp) const override;
229 void unserialize(CheckpointIn &cp) override;
231 int64_t open() override { return openImpl(false); }
232 int64_t close() override;
233 bool isTTY() const override;
234 int64_t read(uint8_t *buffer, uint64_t size) override;
235 int64_t write(const uint8_t *buffer, uint64_t size) override;
236 int64_t seek(uint64_t pos) override;
237 int64_t flen() override;
240 int64_t openImpl(bool unserialize);
241 bool needClose() const { return !isTTY(); }
246 std::string filesRootDir;
247 std::vector<std::unique_ptr<FileBase>> files;
252 protected: // Helper functions
253 unsigned calcTickShift() const {
254 int msb = findMsbSet(SimClock::Frequency);
255 return msb > 31 ? msb - 31 : 0;
257 uint64_t semiTick(Tick tick) const {
258 return tick >> tickShift;
260 void semiExit(uint64_t code, uint64_t subcode);
261 PortProxy &physProxy(ThreadContext *tc);
262 std::string readString(ThreadContext *tc, Addr ptr, size_t len);
264 std::unique_ptr<PortProxy> physProxyS;
267 typedef std::pair<uint64_t, SemiErrno> RetErrno;
268 static RetErrno retError(SemiErrno e) {
269 return RetErrno((uint64_t)-1, e);
272 static RetErrno retOK(uint64_t r) {
273 return RetErrno(r, 0);
277 * Semihosting call information structure.
279 * This structure describes how a semi-hosting call is
280 * implemented. It contains debug information (e.g., the name of
281 * the call), a pointer to the implementation, and information
282 * needed to read its parameters from guest memory.
290 * Pointer to call implementation
292 * @param tc ThreadContext pointer for caller
293 * @param aarch64 True if in aarc64 mode, false otherwise.
294 * @parma argv Argument vector. argv[0] always corresponds to
295 * the pointer to the argument list. Remaining
296 * entries are read as consecutive words starting
297 * at the address pointed to by argv[0].
298 * @return a (return value, errno) pair
300 RetErrno (ArmSemihosting::*call)(ThreadContext *tc, bool aarch64,
301 std::vector<uint64_t> &argv);
303 /** Number of aarch32 arguments to read from guest memory. -1
304 * if unimplemented.*/
306 /** Number of aarch32 arguments to read from guest memory. -1
307 * if unimplemented.*/
310 /** Is call implemented in aarch32? */
311 bool implemented32() const { return call && argc32 >= 0; }
312 /** Is call implemented in aarch64? */
313 bool implemented64() const { return call && argc64 >= 0; }
316 #define SEMI_CALL(N) \
317 RetErrno call ## N (ThreadContext *tc, \
318 bool aarch64, std::vector<uint64_t> &argv)
338 SEMI_CALL(GetCmdLine);
341 SEMI_CALL(ExitExtended);
348 static const SemiCall *getCall(uint32_t op, bool aarch64);
349 static FILE *getSTDIO(const char *stream_name,
350 const std::string &name, const char *mode);
352 static const std::map<uint32_t, SemiCall> calls;
353 static const std::vector<const char *> fmodes;
354 static const std::map<uint64_t, const char *> exitCodes;
355 static const std::vector<uint8_t> features;
356 static const std::map<const std::string, FILE *> stdioMap;
359 #endif // __ARCH_ARM_SEMIHOSTING_HH__