gem5  v21.1.0.2
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  private:
100  std::string _name;
101  int _num;
102 
106 };
107 
108 /*
109  * This SyscallDesc subclass template adapts a given syscall implementation so
110  * that some arguments can come from the simulator (desc, num and tc) while the
111  * rest can come from the guest using the guest_abi mechanism.
112  */
113 template <typename ABI>
115 {
116  private:
117  // Aliases to make the code below a little more concise.
118  template <typename ...Args>
119  using ABIExecutor =
120  std::function<SyscallReturn(SyscallDesc *, ThreadContext *, Args...)>;
121 
122  template <typename ...Args>
123  using ABIExecutorPtr =
125 
126 
127  // Wrap an executor with guest arguments with a normal executor that gets
128  // those additional arguments from the guest context.
129  template <typename ...Args>
130  static inline Executor
132  {
133  return [target](SyscallDesc *desc,
134  ThreadContext *tc) -> SyscallReturn {
135  // Create a partial function which will stick desc to the front of
136  // the parameter list.
137  auto partial = [target,desc](
138  ThreadContext *tc, Args... args) -> SyscallReturn {
139  return target(desc, tc, args...);
140  };
141 
142  // Use invokeSimcall to gather the other arguments based on the
143  // given ABI and pass them to the syscall implementation.
144  return invokeSimcall<ABI, SyscallReturn, Args...>(tc,
145  std::function<SyscallReturn(ThreadContext *, Args...)>(
146  partial));
147  };
148  }
149 
150  template <typename ...Args>
151  static inline Dumper
153  {
154  return [](std::string name, ThreadContext *tc) -> std::string {
155  return dumpSimcall<ABI, SyscallReturn, Args...>(name, tc);
156  };
157  }
158 
159  public:
160  // Constructors which plumb in buildExecutor.
161  template <typename ...Args>
162  SyscallDescABI(int num, const char *name, ABIExecutor<Args...> target) :
163  SyscallDesc(num, name, buildExecutor<Args...>(target),
164  buildDumper<Args...>())
165  {}
166 
167  template <typename ...Args>
168  SyscallDescABI(int num, const char *name, ABIExecutorPtr<Args...> target) :
169  SyscallDescABI(num, name, ABIExecutor<Args...>(target))
170  {}
171 
172  SyscallDescABI(int num, const char *name) :
174  {}
175 
176  void
177  returnInto(ThreadContext *tc, const SyscallReturn &ret) override
178  {
180  }
181 };
182 
183 template <typename ABI>
185 {
186  private:
187  std::map<int, SyscallDescABI<ABI>> _descs;
188 
189  public:
190  SyscallDescTable(std::initializer_list<SyscallDescABI<ABI>> descs)
191  {
192  for (auto &desc: descs) {
193  auto res = _descs.insert({desc.num(), desc});
194  panic_if(!res.second, "Failed to insert desc %s", desc.name());
195  }
196  }
197 
199  *get(int num, bool fatal_if_missing=true)
200  {
201  auto it = _descs.find(num);
202  if (it == _descs.end()) {
203  if (fatal_if_missing)
204  fatal("Syscall %d out of range", num);
205  else
206  return nullptr;
207  }
208  return &it->second;
209  }
210 };
211 
212 } // namespace gem5
213 
214 #endif // __SIM_SYSCALL_DESC_HH__
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:189
gem5::SyscallDescABI
Definition: syscall_desc.hh:114
gem5::SyscallDesc::_num
int _num
Definition: syscall_desc.hh:101
gem5::SyscallDescTable::_descs
std::map< int, SyscallDescABI< ABI > > _descs
Definition: syscall_desc.hh:187
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:152
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:104
gem5::SyscallDesc::doSyscall
void doSyscall(ThreadContext *tc)
Interface for invoking the system call funcion pointer.
Definition: syscall_desc.cc:41
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:162
syscall_return.hh
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:93
gem5::SyscallDesc::SyscallDesc
SyscallDesc(int num, const char *name, Executor exec, Dumper dump)
Definition: syscall_desc.hh:94
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:120
gem5::statistics::dump
void dump()
Dump all statistics data to the registered outputs.
Definition: statistics.cc:295
process.hh
gem5::SyscallDescABI::ABIExecutorPtr
SyscallReturn(*)(SyscallDesc *, ThreadContext *, Args...) ABIExecutorPtr
Definition: syscall_desc.hh:124
gem5::SyscallDescABI::buildExecutor
static Executor buildExecutor(ABIExecutor< Args... > target)
Definition: syscall_desc.hh:131
gem5::SyscallDescTable
Definition: syscall_desc.hh:184
gem5::SyscallDesc::dumper
Dumper dumper
Definition: syscall_desc.hh:105
gem5::SyscallDescABI::SyscallDescABI
SyscallDescABI(int num, const char *name)
Definition: syscall_desc.hh:172
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:203
types.hh
gem5::SyscallDesc::_name
std::string _name
System call name (e.g., open, mmap, clone, socket, etc.)
Definition: syscall_desc.hh:100
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:199
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:64
gem5::SyscallDescTable::SyscallDescTable
SyscallDescTable(std::initializer_list< SyscallDescABI< ABI >> descs)
Definition: syscall_desc.hh:190
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
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:177
gem5::SyscallDescABI::SyscallDescABI
SyscallDescABI(int num, const char *name, ABIExecutorPtr< Args... > target)
Definition: syscall_desc.hh:168
gem5::unimplementedFunc
SyscallReturn unimplementedFunc(SyscallDesc *desc, ThreadContext *tc)
Handler for unimplemented syscalls that we haven't thought about.
Definition: syscall_emul.cc:64

Generated on Tue Sep 21 2021 12:25:48 for gem5 by doxygen 1.8.17