gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
syscall_desc.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2013, 2015 ARM Limited
3  * Copyright (c) 2015-2016 Advanced Micro Devices, Inc.
4  * All rights reserved
5  *
6  * The license below extends only to copyright in the software and shall
7  * not be construed as granting a license to any other intellectual
8  * property including but not limited to intellectual property relating
9  * to a hardware implementation of the functionality of the software
10  * licensed hereunder. You may use the software subject to the license
11  * terms below provided that you ensure that this notice is replicated
12  * unmodified and in its entirety in all distributions of the software,
13  * modified or unmodified, in source code or in binary form.
14  *
15  * Copyright (c) 2003-2005 The Regents of The University of Michigan
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met: redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer;
22  * redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution;
25  * neither the name of the copyright holders nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #ifndef __SIM_SYSCALL_DESC_HH__
43 #define __SIM_SYSCALL_DESC_HH__
44 
45 #include <functional>
46 #include <map>
47 #include <string>
48 
49 #include "base/logging.hh"
50 #include "base/types.hh"
51 #include "cpu/thread_context.hh"
52 #include "sim/guest_abi.hh"
53 #include "sim/process.hh"
54 #include "sim/syscall_return.hh"
55 
56 namespace gem5
57 {
58 
59 class SyscallDesc;
60 
61 SyscallReturn unimplementedFunc(SyscallDesc *desc, ThreadContext *tc);
62 
70 {
71  public:
78  void doSyscall(ThreadContext *tc);
79 
80  std::string name() const { return _name; }
81  int num() const { return _num; }
82 
87  virtual void returnInto(ThreadContext *tc, const SyscallReturn &ret) = 0;
88 
89  protected:
90  using Executor =
91  std::function<SyscallReturn(SyscallDesc *, ThreadContext *)>;
92  using Dumper = std::function<std::string(std::string, ThreadContext *)>;
93 
94  SyscallDesc(int num, const char *name, Executor exec, Dumper dump) :
95  _name(name), _num(num), executor(exec), dumper(dump)
96  {}
97 
98  void retrySyscall(ThreadContext *tc);
99 
100  private:
102  std::string _name;
103  int _num;
104 
105  void setupRetry(ThreadContext *tc);
106  void handleReturn(ThreadContext *tc, const SyscallReturn &ret);
107 
111 };
112 
113 /*
114  * This SyscallDesc subclass template adapts a given syscall implementation so
115  * that some arguments can come from the simulator (desc, num and tc) while the
116  * rest can come from the guest using the guest_abi mechanism.
117  */
118 template <typename ABI>
120 {
121  private:
122  // Aliases to make the code below a little more concise.
123  template <typename ...Args>
124  using ABIExecutor =
125  std::function<SyscallReturn(SyscallDesc *, ThreadContext *, Args...)>;
126 
127  template <typename ...Args>
128  using ABIExecutorPtr =
130 
131 
132  // Wrap an executor with guest arguments with a normal executor that gets
133  // those additional arguments from the guest context.
134  template <typename ...Args>
135  static inline Executor
137  {
138  return [target](SyscallDesc *desc,
139  ThreadContext *tc) -> SyscallReturn {
140  // Create a partial function which will stick desc to the front of
141  // the parameter list.
142  auto partial = [target,desc](
143  ThreadContext *tc, Args... args) -> SyscallReturn {
144  return target(desc, tc, args...);
145  };
146 
147  // Use invokeSimcall to gather the other arguments based on the
148  // given ABI and pass them to the syscall implementation.
149  return invokeSimcall<ABI, false, SyscallReturn, Args...>(tc,
150  std::function<SyscallReturn(ThreadContext *, Args...)>(
151  partial));
152  };
153  }
154 
155  template <typename ...Args>
156  static inline Dumper
158  {
159  return [](std::string name, ThreadContext *tc) -> std::string {
160  return dumpSimcall<ABI, SyscallReturn, Args...>(name, tc);
161  };
162  }
163 
164  public:
165  // Constructors which plumb in buildExecutor.
166  template <typename ...Args>
167  SyscallDescABI(int num, const char *name, ABIExecutor<Args...> target) :
168  SyscallDesc(num, name, buildExecutor<Args...>(target),
169  buildDumper<Args...>())
170  {}
171 
172  template <typename ...Args>
173  SyscallDescABI(int num, const char *name, ABIExecutorPtr<Args...> target) :
174  SyscallDescABI(num, name, ABIExecutor<Args...>(target))
175  {}
176 
177  SyscallDescABI(int num, const char *name) :
179  {}
180 
181  void
182  returnInto(ThreadContext *tc, const SyscallReturn &ret) override
183  {
185  }
186 };
187 
188 template <typename ABI>
190 {
191  private:
192  std::map<int, SyscallDescABI<ABI>> _descs;
193 
194  public:
195  SyscallDescTable(std::initializer_list<SyscallDescABI<ABI>> descs)
196  {
197  for (auto &desc: descs) {
198  auto res = _descs.insert({desc.num(), desc});
199  panic_if(!res.second, "Failed to insert desc %s", desc.name());
200  }
201  }
202 
204  *get(int num, bool fatal_if_missing=true)
205  {
206  auto it = _descs.find(num);
207  if (it == _descs.end()) {
208  if (fatal_if_missing)
209  fatal("Syscall %d out of range", num);
210  else
211  return nullptr;
212  }
213  return &it->second;
214  }
215 };
216 
217 } // namespace gem5
218 
219 #endif // __SIM_SYSCALL_DESC_HH__
gem5::SyscallDesc::retrySyscall
void retrySyscall(ThreadContext *tc)
Definition: syscall_desc.cc:61
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:200
gem5::SyscallDescABI
Definition: syscall_desc.hh:119
gem5::SyscallDesc::_num
int _num
Definition: syscall_desc.hh:103
gem5::SyscallDescTable::_descs
std::map< int, SyscallDescABI< ABI > > _descs
Definition: syscall_desc.hh:192
gem5::SyscallReturn
This class represents the return value from an emulated system call, including any errno setting.
Definition: syscall_return.hh:55
gem5::SyscallDescABI::buildDumper
static Dumper buildDumper()
Definition: syscall_desc.hh:157
gem5::SyscallDesc::returnInto
virtual void returnInto(ThreadContext *tc, const SyscallReturn &ret)=0
For use within the system call executor if new threads are created and need something returned into t...
gem5::SyscallDesc::executor
Executor executor
Mechanism for ISAs to connect to the emul function definitions.
Definition: syscall_desc.hh:109
gem5::SyscallDesc::doSyscall
void doSyscall(ThreadContext *tc)
Interface for invoking the system call funcion pointer.
Definition: syscall_desc.cc:42
gem5::SyscallDesc::Dumper
std::function< std::string(std::string, ThreadContext *)> Dumper
Definition: syscall_desc.hh:92
gem5::SyscallDescABI::SyscallDescABI
SyscallDescABI(int num, const char *name, ABIExecutor< Args... > target)
Definition: syscall_desc.hh:167
syscall_return.hh
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
gem5::SyscallDesc::SyscallDesc
SyscallDesc(int num, const char *name, Executor exec, Dumper dump)
Definition: syscall_desc.hh:94
gem5::SyscallDesc::setupRetry
void setupRetry(ThreadContext *tc)
Definition: syscall_desc.cc:80
gem5::dumpSimcall
std::string dumpSimcall(std::string name, ThreadContext *tc, std::function< Ret(ThreadContext *, Args...)> target=std::function< Ret(ThreadContext *, Args...)>())
Definition: guest_abi.hh:111
gem5::SyscallDescABI::ABIExecutor
std::function< SyscallReturn(SyscallDesc *, ThreadContext *, Args...)> ABIExecutor
Definition: syscall_desc.hh:125
gem5::statistics::dump
void dump()
Dump all statistics data to the registered outputs.
Definition: statistics.cc:300
process.hh
gem5::SyscallDescABI::ABIExecutorPtr
SyscallReturn(*)(SyscallDesc *, ThreadContext *, Args...) ABIExecutorPtr
Definition: syscall_desc.hh:129
gem5::SyscallDescABI::buildExecutor
static Executor buildExecutor(ABIExecutor< Args... > target)
Definition: syscall_desc.hh:136
gem5::SyscallDescTable
Definition: syscall_desc.hh:189
gem5::SyscallDesc::dumper
Dumper dumper
Definition: syscall_desc.hh:110
gem5::SyscallDescABI::SyscallDescABI
SyscallDescABI(int num, const char *name)
Definition: syscall_desc.hh:177
gem5::SyscallDesc::handleReturn
void handleReturn(ThreadContext *tc, const SyscallReturn &ret)
Definition: syscall_desc.cc:94
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:214
types.hh
gem5::SyscallDesc::_name
std::string _name
System call name (e.g., open, mmap, clone, socket, etc.)
Definition: syscall_desc.hh:102
gem5::SyscallDesc::Executor
std::function< SyscallReturn(SyscallDesc *, ThreadContext *)> Executor
Definition: syscall_desc.hh:91
guest_abi.hh
gem5::SyscallDescTable::get
SyscallDesc * get(int num, bool fatal_if_missing=true)
Definition: syscall_desc.hh:204
logging.hh
gem5::SyscallDesc
This class provides the wrapper interface for the system call implementations which are defined in th...
Definition: syscall_desc.hh:69
gem5::SyscallDesc::num
int num() const
Definition: syscall_desc.hh:81
gem5::guest_abi::Result
Definition: definition.hh:63
gem5::SyscallDescTable::SyscallDescTable
SyscallDescTable(std::initializer_list< SyscallDescABI< ABI >> descs)
Definition: syscall_desc.hh:195
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::SyscallDesc::name
std::string name() const
Definition: syscall_desc.hh:80
gem5::invokeSimcall
Ret invokeSimcall(ThreadContext *tc, std::function< Ret(ThreadContext *, Args...)> target)
Definition: guest_abi.hh:50
thread_context.hh
gem5::SyscallDescABI::returnInto
void returnInto(ThreadContext *tc, const SyscallReturn &ret) override
For use within the system call executor if new threads are created and need something returned into t...
Definition: syscall_desc.hh:182
gem5::SyscallDescABI::SyscallDescABI
SyscallDescABI(int num, const char *name, ABIExecutorPtr< Args... > target)
Definition: syscall_desc.hh:173
gem5::unimplementedFunc
SyscallReturn unimplementedFunc(SyscallDesc *desc, ThreadContext *tc)
Handler for unimplemented syscalls that we haven't thought about.
Definition: syscall_emul.cc:65

Generated on Sun Jul 30 2023 01:57:00 for gem5 by doxygen 1.8.17