gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Andreas Sandberg
38  */
39 #ifndef __ARCH_ARM_SEMIHOSTING_HH__
40 #define __ARCH_ARM_SEMIHOSTING_HH__
41 
42 #include <cstdio>
43 #include <map>
44 #include <memory>
45 #include <utility>
46 #include <vector>
47 
48 #include "sim/sim_object.hh"
49 
50 struct ArmSemihostingParams;
51 class PortProxy;
52 class SerialDevice;
53 class ThreadContext;
54 
69 class ArmSemihosting : public SimObject
70 {
71  public:
72  ArmSemihosting(const ArmSemihostingParams *p);
73 
75  uint64_t call64(ThreadContext *tc, uint32_t op, uint64_t param);
77  uint32_t call32(ThreadContext *tc, uint32_t op, uint32_t param);
78 
79  public: // SimObject and related interfaces
80  void serialize(CheckpointOut &cp) const override;
81  void unserialize(CheckpointIn &cp) override;
82 
83  protected: // Configuration
84  const std::string cmdLine;
86  const Addr stackSize;
87 
92  const time_t timeBase;
93 
95  const unsigned tickShift;
96 
97  protected: // Internal state
98  typedef uint64_t SemiErrno;
99  SemiErrno semiErrno;
100 
101  protected: // File IO
115  class FileBase : public Serializable
116  {
117  public:
118  FileBase(ArmSemihosting &_parent, const char *name, const char *_mode)
119  : parent(_parent), _name(name), mode(_mode) {}
120  virtual ~FileBase() {};
121 
122  FileBase() = delete;
123  FileBase(FileBase &) = delete;
124 
125  static std::unique_ptr<FileBase> create(
126  ArmSemihosting &parent, const std::string &fname,
127  const char *mode);
128  static std::unique_ptr<FileBase> create(
129  ArmSemihosting &parent, CheckpointIn &cp, const std::string &sec);
130 
131  void serialize(CheckpointOut &cp) const override;
132  void unserialize(CheckpointIn &cp) override;
133 
134  const std::string &fileName() { return _name; }
135 
136  public:
152  virtual int64_t open() { return 0; }
153 
159  virtual int64_t close() { return 0; }
160 
166  virtual bool isTTY() const { return false; }
167 
173  virtual int64_t read(uint8_t *buffer, uint64_t size);
174 
180  virtual int64_t write(const uint8_t *buffer, uint64_t size);
181 
188  virtual int64_t seek(uint64_t pos);
189 
195  virtual int64_t flen();
196 
199  protected:
201  std::string _name;
202  std::string mode;
203  };
204 
206  class FileFeatures : public FileBase
207  {
208  public:
209  FileFeatures(ArmSemihosting &_parent,
210  const char *name, const char *mode);
211 
212  void serialize(CheckpointOut &cp) const override;
213  void unserialize(CheckpointIn &cp) override;
214 
215  int64_t read(uint8_t *buffer, uint64_t size) override;
216  int64_t seek(uint64_t pos) override;
217 
218  protected:
219  size_t pos;
220  };
221 
222  class File : public FileBase
223  {
224  public:
225  File(ArmSemihosting &_parent, const char *name, const char *mode);
226  ~File();
227 
228  void serialize(CheckpointOut &cp) const override;
229  void unserialize(CheckpointIn &cp) override;
230 
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;
238 
239  protected:
240  int64_t openImpl(bool unserialize);
241  bool needClose() const { return !isTTY(); }
242 
243  FILE *file;
244  };
245 
246  std::string filesRootDir;
248  FILE *stdin;
249  FILE *stdout;
250  FILE *stderr;
251 
252  protected: // Helper functions
253  unsigned calcTickShift() const {
254  int msb = findMsbSet(SimClock::Frequency);
255  return msb > 31 ? msb - 31 : 0;
256  }
257  uint64_t semiTick(Tick tick) const {
258  return tick >> tickShift;
259  }
260  void semiExit(uint64_t code, uint64_t subcode);
262  std::string readString(ThreadContext *tc, Addr ptr, size_t len);
263 
264  std::unique_ptr<PortProxy> physProxyS;
265 
266  private:
268  static RetErrno retError(SemiErrno e) {
269  return RetErrno((uint64_t)-1, e);
270  }
271 
272  static RetErrno retOK(uint64_t r) {
273  return RetErrno(r, 0);
274  }
275 
284  struct SemiCall
285  {
287  const char *name;
288 
301  std::vector<uint64_t> &argv);
302 
305  int argc32;
308  int argc64;
309 
311  bool implemented32() const { return call && argc32 >= 0; }
313  bool implemented64() const { return call && argc64 >= 0; }
314  };
315 
316 #define SEMI_CALL(N) \
317  RetErrno call ## N (ThreadContext *tc, \
318  bool aarch64, std::vector<uint64_t> &argv)
319 
320  SEMI_CALL(Open);
321  SEMI_CALL(Close);
322  SEMI_CALL(WriteC);
323  SEMI_CALL(Write0);
324  SEMI_CALL(Write);
325  SEMI_CALL(Read);
326  SEMI_CALL(ReadC);
327  SEMI_CALL(IsError);
328  SEMI_CALL(IsTTY);
329  SEMI_CALL(Seek);
330  SEMI_CALL(FLen);
331  SEMI_CALL(TmpNam);
332  SEMI_CALL(Remove);
333  SEMI_CALL(Rename);
334  SEMI_CALL(Clock);
335  SEMI_CALL(Time);
336  SEMI_CALL(System);
337  SEMI_CALL(Errno);
338  SEMI_CALL(GetCmdLine);
339  SEMI_CALL(HeapInfo);
340  SEMI_CALL(Exit);
341  SEMI_CALL(ExitExtended);
342 
343  SEMI_CALL(Elapsed);
344  SEMI_CALL(TickFreq);
345 
346 #undef SEMI_CALL
347 
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);
351 
352  static const std::map<uint32_t, SemiCall> calls;
354  static const std::map<uint64_t, const char *> exitCodes;
356  static const std::map<const std::string, FILE *> stdioMap;
357 };
358 
359 #endif // __ARCH_ARM_SEMIHOSTING_HH__
Implementation of the &#39;:semihosting-features&#39; magic file.
Definition: semihosting.hh:206
STL pair class.
Definition: stl.hh:61
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: semihosting.cc:229
static FILE * getSTDIO(const char *stream_name, const std::string &name, const char *mode)
Definition: semihosting.cc:700
ArmSemihosting(const ArmSemihostingParams *p)
Definition: semihosting.cc:133
static std::unique_ptr< FileBase > create(ArmSemihosting &parent, const std::string &fname, const char *mode)
Definition: semihosting.cc:717
std::vector< std::unique_ptr< FileBase > > files
Definition: semihosting.hh:247
static const std::map< const std::string, FILE * > stdioMap
Definition: semihosting.hh:356
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: semihosting.cc:752
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: semihosting.cc:244
int argc64
Number of aarch32 arguments to read from guest memory.
Definition: semihosting.hh:308
static RetErrno retError(SemiErrno e)
Definition: semihosting.hh:268
Definition: time.hh:48
PortProxy & physProxy(ThreadContext *tc)
Definition: semihosting.cc:256
const char * name
Call name.
Definition: semihosting.hh:287
Definition: system.hh:77
Definition: cprintf.cc:42
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
Definition: core.cc:49
virtual int64_t read(uint8_t *buffer, uint64_t size)
Read data from file.
Definition: semihosting.cc:766
ThreadContext is the external interface to all thread state for anything outside of the CPU...
virtual int64_t seek(uint64_t pos)
Seek to an absolute position in the file.
Definition: semihosting.cc:778
STL vector class.
Definition: stl.hh:40
virtual int64_t open()
Open the the file.
Definition: semihosting.hh:152
static const std::vector< const char * > fmodes
Definition: semihosting.hh:353
const Addr memReserve
Definition: semihosting.hh:85
const std::string cmdLine
Definition: semihosting.hh:84
bool needClose() const
Definition: semihosting.hh:241
Internal state for open files.
Definition: semihosting.hh:115
virtual bool isTTY() const
Check if a file corresponds to a TTY device.
Definition: semihosting.hh:166
uint64_t Tick
Tick count type.
Definition: types.hh:63
const std::string & fileName()
Definition: semihosting.hh:134
Semihosting for AArch32 and AArch64.
Definition: semihosting.hh:69
uint64_t semiTick(Tick tick) const
Definition: semihosting.hh:257
uint32_t call32(ThreadContext *tc, uint32_t op, uint32_t param)
Perform an Arm Semihosting call from aarch32 code.
Definition: semihosting.cc:194
static const std::map< uint32_t, SemiCall > calls
Definition: semihosting.hh:352
Bitfield< 18, 16 > len
FileBase(ArmSemihosting &_parent, const char *name, const char *_mode)
Definition: semihosting.hh:118
std::unique_ptr< PortProxy > physProxyS
Definition: semihosting.hh:264
std::pair< uint64_t, SemiErrno > RetErrno
Definition: semihosting.hh:267
Base class for serial devices such as terminals.
Definition: serial.hh:92
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
virtual const std::string name() const
Definition: sim_object.hh:120
std::string readString(ThreadContext *tc, Addr ptr, size_t len)
Definition: semihosting.cc:273
static RetErrno retOK(uint64_t r)
Definition: semihosting.hh:272
Bitfield< 34 > aarch64
Definition: types.hh:91
Basic support for object serialization.
Definition: serialize.hh:153
static const std::vector< uint8_t > features
Definition: semihosting.hh:355
This object is a proxy for a port or other object which implements the functional response protocol...
Definition: port_proxy.hh:82
Bitfield< 9 > e
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: semihosting.cc:759
const Addr stackSize
Definition: semihosting.hh:86
static const std::map< uint64_t, const char * > exitCodes
Definition: semihosting.hh:354
std::ostream CheckpointOut
Definition: serialize.hh:68
static const SemiCall * getCall(uint32_t op, bool aarch64)
Definition: semihosting.cc:689
bool implemented64() const
Is call implemented in aarch64?
Definition: semihosting.hh:313
void semiExit(uint64_t code, uint64_t subcode)
Definition: semihosting.cc:651
virtual int64_t close()
Close the file.
Definition: semihosting.hh:159
int findMsbSet(uint64_t val)
Returns the bit position of the MSB that is set in the input.
Definition: bitfield.hh:204
uint64_t SemiErrno
Definition: semihosting.hh:98
const unsigned tickShift
Number of bits to right shift gem5 ticks to fit in a uint32_t.
Definition: semihosting.hh:95
const time_t timeBase
Base time when the simulation started.
Definition: semihosting.hh:92
int64_t open() override
Open the the file.
Definition: semihosting.hh:231
unsigned calcTickShift() const
Definition: semihosting.hh:253
bool implemented32() const
Is call implemented in aarch32?
Definition: semihosting.hh:311
int argc32
Number of aarch32 arguments to read from guest memory.
Definition: semihosting.hh:305
Semihosting call information structure.
Definition: semihosting.hh:284
Bitfield< 4 > op
Definition: types.hh:80
virtual int64_t write(const uint8_t *buffer, uint64_t size)
Write data to file.
Definition: semihosting.cc:772
Bitfield< 0 > p
virtual int64_t flen()
Get the length of a file in bytes.
Definition: semihosting.cc:784
Abstract superclass for simulation objects.
Definition: sim_object.hh:96
SemiErrno semiErrno
Definition: semihosting.hh:99
ArmSemihosting & parent
Definition: semihosting.hh:200
uint64_t call64(ThreadContext *tc, uint32_t op, uint64_t param)
Perform an Arm Semihosting call from aarch64 code.
Definition: semihosting.cc:159
std::string filesRootDir
Definition: semihosting.hh:246

Generated on Fri Feb 28 2020 16:26:57 for gem5 by doxygen 1.8.13