gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
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
56namespace gem5
57{
58
59class SyscallDesc;
60
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) :
96 {}
97
98 virtual ~SyscallDesc() = default;
99
100 void retrySyscall(ThreadContext *tc);
101
102 private:
104 std::string _name;
105 int _num;
106
107 void setupRetry(ThreadContext *tc);
108 void handleReturn(ThreadContext *tc, const SyscallReturn &ret);
109
113};
114
115/*
116 * This SyscallDesc subclass template adapts a given syscall implementation so
117 * that some arguments can come from the simulator (desc, num and tc) while the
118 * rest can come from the guest using the guest_abi mechanism.
119 */
120template <typename ABI>
122{
123 private:
124 // Aliases to make the code below a little more concise.
125 template <typename ...Args>
127 std::function<SyscallReturn(SyscallDesc *, ThreadContext *, Args...)>;
128
129 template <typename ...Args>
131 SyscallReturn (*)(SyscallDesc *, ThreadContext *, Args...);
132
133
134 // Wrap an executor with guest arguments with a normal executor that gets
135 // those additional arguments from the guest context.
136 template <typename ...Args>
137 static inline Executor
139 {
140 return [target](SyscallDesc *desc,
142 // Create a partial function which will stick desc to the front of
143 // the parameter list.
144 auto partial = [target,desc](
145 ThreadContext *tc, Args... args) -> SyscallReturn {
146 return target(desc, tc, args...);
147 };
148
149 // Use invokeSimcall to gather the other arguments based on the
150 // given ABI and pass them to the syscall implementation.
151 return invokeSimcall<ABI, false, SyscallReturn, Args...>(tc,
152 std::function<SyscallReturn(ThreadContext *, Args...)>(
153 partial));
154 };
155 }
156
157 template <typename ...Args>
158 static inline Dumper
160 {
161 return [](std::string name, ThreadContext *tc) -> std::string {
162 return dumpSimcall<ABI, SyscallReturn, Args...>(name, tc);
163 };
164 }
165
166 public:
167 // Constructors which plumb in buildExecutor.
168 template <typename ...Args>
169 SyscallDescABI(int num, const char *name, ABIExecutor<Args...> target) :
170 SyscallDesc(num, name, buildExecutor<Args...>(target),
171 buildDumper<Args...>())
172 {}
173
174 template <typename ...Args>
175 SyscallDescABI(int num, const char *name, ABIExecutorPtr<Args...> target) :
176 SyscallDescABI(num, name, ABIExecutor<Args...>(target))
177 {}
178
182
183 void
184 returnInto(ThreadContext *tc, const SyscallReturn &ret) override
185 {
187 }
188};
189
190template <typename ABI>
192{
193 private:
194 std::map<int, SyscallDescABI<ABI>> _descs;
195
196 public:
197 SyscallDescTable(std::initializer_list<SyscallDescABI<ABI>> descs)
198 {
199 for (auto &desc: descs) {
200 auto res = _descs.insert({desc.num(), desc});
201 panic_if(!res.second, "Failed to insert desc %s", desc.name());
202 }
203 }
204
206 *get(int num, bool fatal_if_missing=true)
207 {
208 auto it = _descs.find(num);
209 if (it == _descs.end()) {
210 if (fatal_if_missing)
211 fatal("Syscall %d out of range", num);
212 else
213 return nullptr;
214 }
215 return &it->second;
216 }
217};
218
219} // namespace gem5
220
221#endif // __SIM_SYSCALL_DESC_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
std::function< SyscallReturn(SyscallDesc *, ThreadContext *, Args...)> ABIExecutor
SyscallDescABI(int num, const char *name)
SyscallDescABI(int num, const char *name, ABIExecutor< Args... > target)
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...
SyscallDescABI(int num, const char *name, ABIExecutorPtr< Args... > target)
SyscallReturn(*)(SyscallDesc *, ThreadContext *, Args...) ABIExecutorPtr
static Dumper buildDumper()
static Executor buildExecutor(ABIExecutor< Args... > target)
SyscallDesc * get(int num, bool fatal_if_missing=true)
std::map< int, SyscallDescABI< ABI > > _descs
SyscallDescTable(std::initializer_list< SyscallDescABI< ABI > > descs)
This class provides the wrapper interface for the system call implementations which are defined in th...
std::string name() const
SyscallDesc(int num, const char *name, Executor exec, Dumper dump)
std::function< SyscallReturn(SyscallDesc *, ThreadContext *)> Executor
virtual ~SyscallDesc()=default
std::string _name
System call name (e.g., open, mmap, clone, socket, etc.)
std::function< std::string(std::string, ThreadContext *)> Dumper
void handleReturn(ThreadContext *tc, const SyscallReturn &ret)
void setupRetry(ThreadContext *tc)
void doSyscall(ThreadContext *tc)
Interface for invoking the system call funcion pointer.
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...
Executor executor
Mechanism for ISAs to connect to the emul function definitions.
void retrySyscall(ThreadContext *tc)
This class represents the return value from an emulated system call, including any errno setting.
ThreadContext is the external interface to all thread state for anything outside of the CPU.
#define fatal(...)
This implements a cprintf based fatal() function.
Definition logging.hh:232
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:246
void dump()
Dump all statistics data to the registered outputs.
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
std::string dumpSimcall(std::string name, ThreadContext *tc, std::function< Ret(ThreadContext *, Args...)> target=std::function< Ret(ThreadContext *, Args...)>())
Definition guest_abi.hh:111
Ret invokeSimcall(ThreadContext *tc, std::function< Ret(ThreadContext *, Args...)> target)
Definition guest_abi.hh:50
SyscallReturn unimplementedFunc(SyscallDesc *desc, ThreadContext *tc)
Handler for unimplemented syscalls that we haven't thought about.

Generated on Mon Oct 27 2025 04:13:04 for gem5 by doxygen 1.14.0