gem5  v22.1.0.0
guest_abi.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Google Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef __SIM_GUEST_ABI_HH__
29 #define __SIM_GUEST_ABI_HH__
30 
31 #include <functional>
32 
35 #include "sim/guest_abi/layout.hh"
36 #include "sim/guest_abi/varargs.hh"
37 
38 namespace gem5
39 {
40 
41 class ThreadContext;
42 
43 // These functions wrap a simulator level function with the given signature.
44 // The wrapper takes one argument, a thread context to extract arguments from
45 // and write a result (if any) back to. For convenience, the wrapper also
46 // returns the result of the wrapped function.
47 
48 template <typename ABI, bool store_ret, typename Ret, typename ...Args>
49 Ret
51  std::function<Ret(ThreadContext *, Args...)> target)
52 {
53  // Default construct a State to track consumed resources. Built in
54  // types will be zero initialized.
55  auto state = guest_abi::initializeState<ABI>(tc);
56  guest_abi::prepareForFunction<ABI, Ret, Args...>(tc, state);
57  return guest_abi::callFrom<ABI, Ret, store_ret, Args...>(tc, state,
58  target);
59 }
60 
61 template <typename ABI, typename Ret, typename ...Args>
62 Ret
64  std::function<Ret(ThreadContext *, Args...)> target)
65 {
66  return invokeSimcall<ABI, true>(tc, target);
67 }
68 
69 template <typename ABI, bool store_ret, typename Ret, typename ...Args>
70 Ret
71 invokeSimcall(ThreadContext *tc, Ret (*target)(ThreadContext *, Args...))
72 {
73  return invokeSimcall<ABI, store_ret>(
74  tc, std::function<Ret(ThreadContext *, Args...)>(target));
75 }
76 
77 template <typename ABI, typename Ret, typename ...Args>
78 Ret
79 invokeSimcall(ThreadContext *tc, Ret (*target)(ThreadContext *, Args...))
80 {
81  return invokeSimcall<ABI, true>(tc, target);
82 }
83 
84 template <typename ABI, typename ...Args>
85 void
87  std::function<void(ThreadContext *, Args...)> target)
88 {
89  // Default construct a State to track consumed resources. Built in
90  // types will be zero initialized.
91  auto state = guest_abi::initializeState<ABI>(tc);
92  guest_abi::prepareForArguments<ABI, Args...>(tc, state);
93  guest_abi::callFrom<ABI, void, false, Args...>(tc, state, target);
94 }
95 
96 template <typename ABI, typename ...Args>
97 void
98 invokeSimcall(ThreadContext *tc, void (*target)(ThreadContext *, Args...))
99 {
100  invokeSimcall<ABI>(
101  tc, std::function<void(ThreadContext *, Args...)>(target));
102 }
103 
104 
105 // These functions also wrap a simulator level function. Instead of running the
106 // function, they return a string which shows what arguments the function would
107 // be invoked with if it were called from the given context.
108 
109 template <typename ABI, typename Ret, typename ...Args>
110 std::string
111 dumpSimcall(std::string name, ThreadContext *tc,
112  std::function<Ret(ThreadContext *, Args...)> target=
113  std::function<Ret(ThreadContext *, Args...)>())
114 {
115  auto state = guest_abi::initializeState<ABI>(tc);
116  std::ostringstream ss;
117 
118  guest_abi::prepareForFunction<ABI, Ret, Args...>(tc, state);
119  ss << name;
120  guest_abi::dumpArgsFrom<ABI, Ret, Args...>(ss, tc, state);
121  return ss.str();
122 }
123 
124 template <typename ABI, typename Ret, typename ...Args>
125 std::string
126 dumpSimcall(std::string name, ThreadContext *tc,
127  Ret (*target)(ThreadContext *, Args...))
128 {
129  return dumpSimcall<ABI>(
130  name, tc, std::function<Ret(ThreadContext *, Args...)>(target));
131 }
132 
133 } // namespace gem5
134 
135 #endif // __SIM_GUEST_ABI_HH__
ThreadContext is the external interface to all thread state for anything outside of the CPU.
atomic_var_t state
Definition: helpers.cc:188
static void prepareForFunction(ThreadContext *tc, typename ABI::State &state)
Definition: layout.hh:124
static void prepareForArguments([[maybe_unused]] ThreadContext *tc, typename ABI::State &state)
Definition: layout.hh:116
static Ret callFrom(ThreadContext *tc, typename ABI::State &state, std::function< Ret(ThreadContext *, Args...)> target)
Definition: dispatch.hh:82
static void dumpArgsFrom(std::ostream &os, [[maybe_unused]] ThreadContext *tc, typename ABI::State &state)
Definition: dispatch.hh:103
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
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
const std::string & name()
Definition: trace.cc:49
std::stringstream ss
Definition: trace.test.cc:45

Generated on Wed Dec 21 2022 10:22:39 for gem5 by doxygen 1.9.1