gem5  v20.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 class ThreadContext;
39 
40 // These functions wrap a simulator level function with the given signature.
41 // The wrapper takes one argument, a thread context to extract arguments from
42 // and write a result (if any) back to. For convenience, the wrapper also
43 // returns the result of the wrapped function.
44 
45 template <typename ABI, bool store_ret, typename Ret, typename ...Args>
46 Ret
48  std::function<Ret(ThreadContext *, Args...)> target)
49 {
50  // Default construct a State to track consumed resources. Built in
51  // types will be zero initialized.
52  auto state = GuestABI::initializeState<ABI>(tc);
53  GuestABI::prepareForFunction<ABI, Ret, Args...>(tc, state);
54  return GuestABI::callFrom<ABI, store_ret, Ret, Args...>(tc, state, target);
55 }
56 
57 template <typename ABI, typename Ret, typename ...Args>
58 Ret
60  std::function<Ret(ThreadContext *, Args...)> target)
61 {
62  return invokeSimcall<ABI, true>(tc, target);
63 }
64 
65 template <typename ABI, bool store_ret, typename Ret, typename ...Args>
66 Ret
67 invokeSimcall(ThreadContext *tc, Ret (*target)(ThreadContext *, Args...))
68 {
69  return invokeSimcall<ABI, store_ret>(
70  tc, std::function<Ret(ThreadContext *, Args...)>(target));
71 }
72 
73 template <typename ABI, typename Ret, typename ...Args>
74 Ret
75 invokeSimcall(ThreadContext *tc, Ret (*target)(ThreadContext *, Args...))
76 {
77  return invokeSimcall<ABI, true>(tc, target);
78 }
79 
80 template <typename ABI, typename ...Args>
81 void
83  std::function<void(ThreadContext *, Args...)> target)
84 {
85  // Default construct a State to track consumed resources. Built in
86  // types will be zero initialized.
87  auto state = GuestABI::initializeState<ABI>(tc);
88  GuestABI::prepareForArguments<ABI, Args...>(tc, state);
89  GuestABI::callFrom<ABI, Args...>(tc, state, target);
90 }
91 
92 template <typename ABI, typename ...Args>
93 void
94 invokeSimcall(ThreadContext *tc, void (*target)(ThreadContext *, Args...))
95 {
96  invokeSimcall<ABI>(
97  tc, std::function<void(ThreadContext *, Args...)>(target));
98 }
99 
100 
101 // These functions also wrap a simulator level function. Instead of running the
102 // function, they return a string which shows what arguments the function would
103 // be invoked with if it were called from the given context.
104 
105 template <typename ABI, typename Ret, typename ...Args>
106 std::string
107 dumpSimcall(std::string name, ThreadContext *tc,
108  std::function<Ret(ThreadContext *, Args...)> target=
109  std::function<Ret(ThreadContext *, Args...)>())
110 {
111  auto state = GuestABI::initializeState<ABI>(tc);
112  std::ostringstream ss;
113 
114  GuestABI::prepareForFunction<ABI, Ret, Args...>(tc, state);
115  ss << name;
116  GuestABI::dumpArgsFrom<ABI, Ret, Args...>(0, ss, tc, state);
117  return ss.str();
118 }
119 
120 template <typename ABI, typename Ret, typename ...Args>
121 std::string
122 dumpSimcall(std::string name, ThreadContext *tc,
123  Ret (*target)(ThreadContext *, Args...))
124 {
125  return dumpSimcall<ABI>(
126  name, tc, std::function<Ret(ThreadContext *, Args...)>(target));
127 }
128 
129 #endif // __SIM_GUEST_ABI_HH__
invokeSimcall
Ret invokeSimcall(ThreadContext *tc, std::function< Ret(ThreadContext *, Args...)> target)
Definition: guest_abi.hh:47
dispatch.hh
definition.hh
varargs.hh
GuestABI::callFrom
static std::enable_if<!std::is_void< Ret >::value &&store_ret, Ret >::type callFrom(ThreadContext *tc, typename ABI::State &state, std::function< Ret(ThreadContext *)> target)
Definition: dispatch.hh:58
GuestABI::prepareForArguments
static void prepareForArguments(ThreadContext *tc, typename ABI::State &state)
Definition: layout.hh:112
ArmISA::ss
Bitfield< 21 > ss
Definition: miscregs_types.hh:56
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
name
const std::string & name()
Definition: trace.cc:50
GuestABI::dumpArgsFrom
static void dumpArgsFrom(int count, std::ostream &os, ThreadContext *tc, typename ABI::State &state)
Definition: dispatch.hh:135
GuestABI::prepareForFunction
static void prepareForFunction(ThreadContext *tc, typename ABI::State &state)
Definition: layout.hh:127
layout.hh
dumpSimcall
std::string dumpSimcall(std::string name, ThreadContext *tc, std::function< Ret(ThreadContext *, Args...)> target=std::function< Ret(ThreadContext *, Args...)>())
Definition: guest_abi.hh:107

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17