cpu: Add HTM Instruction Flags
[gem5.git] / src / sim / guest_abi.hh
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
33 #include "sim/guest_abi/definition.hh"
34 #include "sim/guest_abi/dispatch.hh"
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
47 invokeSimcall(ThreadContext *tc,
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
59 invokeSimcall(ThreadContext *tc,
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
82 invokeSimcall(ThreadContext *tc,
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__