2 * Copyright 2019 Google, Inc.
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.
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.
28 #ifndef __SIM_SYSCALL_ABI_HH__
29 #define __SIM_SYSCALL_ABI_HH__
31 #include "base/types.hh"
32 #include "cpu/thread_context.hh"
33 #include "sim/guest_abi.hh"
34 #include "sim/syscall_return.hh"
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 {};
46 struct IsConforming<Addr> : public std::true_type {};
48 } // namespace GuestABI
50 struct GenericSyscallABI
55 struct GenericSyscallABI64 : public GenericSyscallABI
58 struct GenericSyscallABI32 : public GenericSyscallABI
60 // Is this argument too big for a single register?
61 template <typename T, typename Enabled=void>
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>
70 static const bool value = false;
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>
79 static const bool value = true;
82 // Read two registers and merge them into one value.
84 mergeRegs(ThreadContext *tc, RegIndex low_idx, RegIndex high_idx)
86 RegVal low = tc->readIntReg(low_idx);
87 RegVal high = tc->readIntReg(high_idx);
88 return insertBits(low, 63, 32, high);
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>
103 get(ThreadContext *tc, typename ABI::State &state)
105 panic_if(state >= ABI::ArgumentRegs.size(),
106 "Ran out of syscall argument registers.");
107 return tc->readIntReg(ABI::ArgumentRegs[state++]);
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>
118 get(ThreadContext *tc, typename ABI::State &state)
120 panic_if(state >= ABI::ArgumentRegs.size(),
121 "Ran out of syscall argument registers.");
122 return bits(tc->readIntReg(ABI::ArgumentRegs[state++]), 31, 0);
126 } // namespace GuestABI
128 #endif // __SIM_SYSCALL_ABI_HH__