cpu: Add HTM Instruction Flags
[gem5.git] / src / sim / syscall_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_SYSCALL_ABI_HH__
29 #define __SIM_SYSCALL_ABI_HH__
30
31 #include "base/types.hh"
32 #include "cpu/thread_context.hh"
33 #include "sim/guest_abi.hh"
34 #include "sim/syscall_return.hh"
35
36 class SyscallDesc;
37
38 namespace GuestABI
39 {
40
41 // Does this normally 64 bit data type shrink down to 32 bits for 32 bit ABIs?
42 template <typename T, typename Enabled=void>
43 struct IsConforming : public std::false_type {};
44
45 template <>
46 struct IsConforming<Addr> : public std::true_type {};
47
48 } // namespace GuestABI
49
50 struct GenericSyscallABI
51 {
52 using State = int;
53 };
54
55 struct GenericSyscallABI64 : public GenericSyscallABI
56 {};
57
58 struct GenericSyscallABI32 : public GenericSyscallABI
59 {
60 // Is this argument too big for a single register?
61 template <typename T, typename Enabled=void>
62 struct IsWide;
63
64 template <typename T>
65 struct IsWide<T, typename std::enable_if<
66 std::is_integral<T>::value &&
67 (sizeof(T) < sizeof(uint64_t) ||
68 GuestABI::IsConforming<T>::value)>::type>
69 {
70 static const bool value = false;
71 };
72
73 template <typename T>
74 struct IsWide<T, typename std::enable_if<
75 std::is_integral<T>::value &&
76 sizeof(T) == sizeof(uint64_t) &&
77 !GuestABI::IsConforming<T>::value>::type>
78 {
79 static const bool value = true;
80 };
81
82 // Read two registers and merge them into one value.
83 static uint64_t
84 mergeRegs(ThreadContext *tc, RegIndex low_idx, RegIndex high_idx)
85 {
86 RegVal low = tc->readIntReg(low_idx);
87 RegVal high = tc->readIntReg(high_idx);
88 return insertBits(low, 63, 32, high);
89 }
90 };
91
92 namespace GuestABI
93 {
94
95 // For 64 bit systems, return syscall args directly.
96 template <typename ABI, typename Arg>
97 struct Argument<ABI, Arg,
98 typename std::enable_if<
99 std::is_base_of<GenericSyscallABI64, ABI>::value &&
100 std::is_integral<Arg>::value>::type>
101 {
102 static Arg
103 get(ThreadContext *tc, typename ABI::State &state)
104 {
105 panic_if(state >= ABI::ArgumentRegs.size(),
106 "Ran out of syscall argument registers.");
107 return tc->readIntReg(ABI::ArgumentRegs[state++]);
108 }
109 };
110
111 // For 32 bit systems, return small enough syscall args directly. Large
112 // arguments aren't handled generically.
113 template <typename ABI, typename Arg>
114 struct Argument<ABI, Arg,
115 typename std::enable_if<!ABI::template IsWide<Arg>::value>::type>
116 {
117 static Arg
118 get(ThreadContext *tc, typename ABI::State &state)
119 {
120 panic_if(state >= ABI::ArgumentRegs.size(),
121 "Ran out of syscall argument registers.");
122 return bits(tc->readIntReg(ABI::ArgumentRegs[state++]), 31, 0);
123 }
124 };
125
126 } // namespace GuestABI
127
128 #endif // __SIM_SYSCALL_ABI_HH__