1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "calling_convention_x86.h"
18
19 #include <android-base/logging.h>
20
21 #include "arch/instruction_set.h"
22 #include "arch/x86/jni_frame_x86.h"
23 #include "utils/x86/managed_register_x86.h"
24
25 namespace art HIDDEN {
26 namespace x86 {
27
28 static constexpr ManagedRegister kManagedCoreArgumentRegisters[] = {
29 X86ManagedRegister::FromCpuRegister(EAX),
30 X86ManagedRegister::FromCpuRegister(ECX),
31 X86ManagedRegister::FromCpuRegister(EDX),
32 X86ManagedRegister::FromCpuRegister(EBX),
33 };
34 static constexpr size_t kManagedCoreArgumentRegistersCount =
35 arraysize(kManagedCoreArgumentRegisters);
36 static constexpr size_t kManagedFpArgumentRegistersCount = 4u;
37
38 static constexpr ManagedRegister kCalleeSaveRegisters[] = {
39 // Core registers.
40 X86ManagedRegister::FromCpuRegister(EBP),
41 X86ManagedRegister::FromCpuRegister(ESI),
42 X86ManagedRegister::FromCpuRegister(EDI),
43 // No hard float callee saves.
44 };
45
46 template <size_t size>
CalculateCoreCalleeSpillMask(const ManagedRegister (& callee_saves)[size])47 static constexpr uint32_t CalculateCoreCalleeSpillMask(
48 const ManagedRegister (&callee_saves)[size]) {
49 // The spilled PC gets a special marker.
50 uint32_t result = 1 << kNumberOfCpuRegisters;
51 for (auto&& r : callee_saves) {
52 if (r.AsX86().IsCpuRegister()) {
53 result |= (1 << r.AsX86().AsCpuRegister());
54 }
55 }
56 return result;
57 }
58
59 static constexpr uint32_t kCoreCalleeSpillMask = CalculateCoreCalleeSpillMask(kCalleeSaveRegisters);
60 static constexpr uint32_t kFpCalleeSpillMask = 0u;
61
62 static constexpr ManagedRegister kNativeCalleeSaveRegisters[] = {
63 // Core registers.
64 X86ManagedRegister::FromCpuRegister(EBX),
65 X86ManagedRegister::FromCpuRegister(EBP),
66 X86ManagedRegister::FromCpuRegister(ESI),
67 X86ManagedRegister::FromCpuRegister(EDI),
68 // No hard float callee saves.
69 };
70
71 static constexpr uint32_t kNativeCoreCalleeSpillMask =
72 CalculateCoreCalleeSpillMask(kNativeCalleeSaveRegisters);
73 static constexpr uint32_t kNativeFpCalleeSpillMask = 0u;
74
75 // Calling convention
76
CalleeSaveScratchRegisters() const77 ArrayRef<const ManagedRegister> X86JniCallingConvention::CalleeSaveScratchRegisters() const {
78 DCHECK(!IsCriticalNative());
79 // All managed callee-save registers are available.
80 static_assert((kCoreCalleeSpillMask & ~kNativeCoreCalleeSpillMask) == 0u);
81 static_assert(kFpCalleeSpillMask == 0u);
82 return ArrayRef<const ManagedRegister>(kCalleeSaveRegisters);
83 }
84
ArgumentScratchRegisters() const85 ArrayRef<const ManagedRegister> X86JniCallingConvention::ArgumentScratchRegisters() const {
86 DCHECK(!IsCriticalNative());
87 // Exclude return registers (EAX/EDX) even if unused. Using the same scratch registers helps
88 // making more JNI stubs identical for better reuse, such as deduplicating them in oat files.
89 // Due to the odd ordering of argument registers, use a separate register array.
90 static constexpr ManagedRegister kArgumentScratchRegisters[] = {
91 X86ManagedRegister::FromCpuRegister(ECX),
92 X86ManagedRegister::FromCpuRegister(EBX),
93 };
94 static_assert(kManagedCoreArgumentRegisters[1].Equals(kArgumentScratchRegisters[0]));
95 static_assert(kManagedCoreArgumentRegisters[3].Equals(kArgumentScratchRegisters[1]));
96 ArrayRef<const ManagedRegister> scratch_regs(kArgumentScratchRegisters);
97 DCHECK(std::none_of(scratch_regs.begin(),
98 scratch_regs.end(),
99 [return_reg = ReturnRegister().AsX86()](ManagedRegister reg) {
100 return return_reg.Overlaps(reg.AsX86());
101 }));
102 return scratch_regs;
103 }
104
ReturnRegisterForShorty(std::string_view shorty,bool jni)105 static ManagedRegister ReturnRegisterForShorty(std::string_view shorty, bool jni) {
106 if (shorty[0] == 'F' || shorty[0] == 'D') {
107 if (jni) {
108 return X86ManagedRegister::FromX87Register(ST0);
109 } else {
110 return X86ManagedRegister::FromXmmRegister(XMM0);
111 }
112 } else if (shorty[0] == 'J') {
113 return X86ManagedRegister::FromRegisterPair(EAX_EDX);
114 } else if (shorty[0] == 'V') {
115 return ManagedRegister::NoRegister();
116 } else {
117 return X86ManagedRegister::FromCpuRegister(EAX);
118 }
119 }
120
ReturnRegister() const121 ManagedRegister X86ManagedRuntimeCallingConvention::ReturnRegister() const {
122 return ReturnRegisterForShorty(GetShorty(), false);
123 }
124
ReturnRegister() const125 ManagedRegister X86JniCallingConvention::ReturnRegister() const {
126 return ReturnRegisterForShorty(GetShorty(), true);
127 }
128
IntReturnRegister() const129 ManagedRegister X86JniCallingConvention::IntReturnRegister() const {
130 return X86ManagedRegister::FromCpuRegister(EAX);
131 }
132
133 // Managed runtime calling convention
134
MethodRegister()135 ManagedRegister X86ManagedRuntimeCallingConvention::MethodRegister() {
136 return X86ManagedRegister::FromCpuRegister(EAX);
137 }
138
ArgumentRegisterForMethodExitHook()139 ManagedRegister X86ManagedRuntimeCallingConvention::ArgumentRegisterForMethodExitHook() {
140 return X86ManagedRegister::FromCpuRegister(EBX);
141 }
142
ResetIterator(FrameOffset displacement)143 void X86ManagedRuntimeCallingConvention::ResetIterator(FrameOffset displacement) {
144 ManagedRuntimeCallingConvention::ResetIterator(displacement);
145 gpr_arg_count_ = 1u; // Skip EAX for ArtMethod*
146 }
147
Next()148 void X86ManagedRuntimeCallingConvention::Next() {
149 if (!IsCurrentParamAFloatOrDouble()) {
150 gpr_arg_count_ += IsCurrentParamALong() ? 2u : 1u;
151 }
152 ManagedRuntimeCallingConvention::Next();
153 }
154
IsCurrentParamInRegister()155 bool X86ManagedRuntimeCallingConvention::IsCurrentParamInRegister() {
156 if (IsCurrentParamAFloatOrDouble()) {
157 return itr_float_and_doubles_ < kManagedFpArgumentRegistersCount;
158 } else {
159 // Don't split a long between the last register and the stack.
160 size_t extra_regs = IsCurrentParamALong() ? 1u : 0u;
161 return gpr_arg_count_ + extra_regs < kManagedCoreArgumentRegistersCount;
162 }
163 }
164
IsCurrentParamOnStack()165 bool X86ManagedRuntimeCallingConvention::IsCurrentParamOnStack() {
166 return !IsCurrentParamInRegister();
167 }
168
CurrentParamRegister()169 ManagedRegister X86ManagedRuntimeCallingConvention::CurrentParamRegister() {
170 DCHECK(IsCurrentParamInRegister());
171 if (IsCurrentParamAFloatOrDouble()) {
172 // First four float parameters are passed via XMM0..XMM3
173 XmmRegister reg = static_cast<XmmRegister>(XMM0 + itr_float_and_doubles_);
174 return X86ManagedRegister::FromXmmRegister(reg);
175 } else {
176 if (IsCurrentParamALong()) {
177 switch (gpr_arg_count_) {
178 case 1:
179 static_assert(kManagedCoreArgumentRegisters[1].AsX86().AsCpuRegister() == ECX);
180 static_assert(kManagedCoreArgumentRegisters[2].AsX86().AsCpuRegister() == EDX);
181 return X86ManagedRegister::FromRegisterPair(ECX_EDX);
182 case 2:
183 static_assert(kManagedCoreArgumentRegisters[2].AsX86().AsCpuRegister() == EDX);
184 static_assert(kManagedCoreArgumentRegisters[3].AsX86().AsCpuRegister() == EBX);
185 return X86ManagedRegister::FromRegisterPair(EDX_EBX);
186 default:
187 LOG(FATAL) << "UNREACHABLE";
188 UNREACHABLE();
189 }
190 } else {
191 return kManagedCoreArgumentRegisters[gpr_arg_count_];
192 }
193 }
194 }
195
CurrentParamStackOffset()196 FrameOffset X86ManagedRuntimeCallingConvention::CurrentParamStackOffset() {
197 return FrameOffset(displacement_.Int32Value() + // displacement
198 kFramePointerSize + // Method*
199 (itr_slots_ * kFramePointerSize)); // offset into in args
200 }
201
202 // JNI calling convention
203
X86JniCallingConvention(bool is_static,bool is_synchronized,bool is_fast_native,bool is_critical_native,std::string_view shorty)204 X86JniCallingConvention::X86JniCallingConvention(bool is_static,
205 bool is_synchronized,
206 bool is_fast_native,
207 bool is_critical_native,
208 std::string_view shorty)
209 : JniCallingConvention(is_static,
210 is_synchronized,
211 is_fast_native,
212 is_critical_native,
213 shorty,
214 kX86PointerSize) {
215 }
216
CoreSpillMask() const217 uint32_t X86JniCallingConvention::CoreSpillMask() const {
218 return is_critical_native_ ? 0u : kCoreCalleeSpillMask;
219 }
220
FpSpillMask() const221 uint32_t X86JniCallingConvention::FpSpillMask() const {
222 return is_critical_native_ ? 0u : kFpCalleeSpillMask;
223 }
224
FrameSize() const225 size_t X86JniCallingConvention::FrameSize() const {
226 if (is_critical_native_) {
227 CHECK(!SpillsMethod());
228 CHECK(!HasLocalReferenceSegmentState());
229 return 0u; // There is no managed frame for @CriticalNative.
230 }
231
232 // Method*, PC return address and callee save area size, local reference segment state
233 DCHECK(SpillsMethod());
234 const size_t method_ptr_size = static_cast<size_t>(kX86PointerSize);
235 const size_t pc_return_addr_size = kFramePointerSize;
236 const size_t callee_save_area_size = CalleeSaveRegisters().size() * kFramePointerSize;
237 size_t total_size = method_ptr_size + pc_return_addr_size + callee_save_area_size;
238
239 DCHECK(HasLocalReferenceSegmentState());
240 // Cookie is saved in one of the spilled registers.
241
242 return RoundUp(total_size, kStackAlignment);
243 }
244
OutFrameSize() const245 size_t X86JniCallingConvention::OutFrameSize() const {
246 // The size of outgoing arguments.
247 size_t size = GetNativeOutArgsSize(/*num_args=*/ NumberOfExtraArgumentsForJni() + NumArgs(),
248 NumLongOrDoubleArgs());
249
250 // @CriticalNative can use tail call as all managed callee saves are preserved by AAPCS.
251 static_assert((kCoreCalleeSpillMask & ~kNativeCoreCalleeSpillMask) == 0u);
252 static_assert((kFpCalleeSpillMask & ~kNativeFpCalleeSpillMask) == 0u);
253
254 if (UNLIKELY(IsCriticalNative())) {
255 // Add return address size for @CriticalNative.
256 // For normal native the return PC is part of the managed stack frame instead of out args.
257 size += kFramePointerSize;
258 // For @CriticalNative, we can make a tail call if there are no stack args
259 // and the return type is not FP type (needs moving from ST0 to MMX0) and
260 // we do not need to extend the result.
261 bool return_type_ok = GetShorty()[0] == 'I' || GetShorty()[0] == 'J' || GetShorty()[0] == 'V';
262 DCHECK_EQ(
263 return_type_ok,
264 GetShorty()[0] != 'F' && GetShorty()[0] != 'D' && !RequiresSmallResultTypeExtension());
265 if (return_type_ok && size == kFramePointerSize) {
266 // Note: This is not aligned to kNativeStackAlignment but that's OK for tail call.
267 static_assert(kFramePointerSize < kNativeStackAlignment);
268 // The stub frame size is considered 0 in the callee where the return PC is a part of
269 // the callee frame but it is kPointerSize in the compiled stub before the tail call.
270 DCHECK_EQ(0u, GetCriticalNativeStubFrameSize(GetShorty()));
271 return kFramePointerSize;
272 }
273 }
274
275 size_t out_args_size = RoundUp(size, kNativeStackAlignment);
276 if (UNLIKELY(IsCriticalNative())) {
277 DCHECK_EQ(out_args_size, GetCriticalNativeStubFrameSize(GetShorty()));
278 }
279 return out_args_size;
280 }
281
CalleeSaveRegisters() const282 ArrayRef<const ManagedRegister> X86JniCallingConvention::CalleeSaveRegisters() const {
283 if (UNLIKELY(IsCriticalNative())) {
284 // Do not spill anything, whether tail call or not (return PC is already on the stack).
285 return ArrayRef<const ManagedRegister>();
286 } else {
287 return ArrayRef<const ManagedRegister>(kCalleeSaveRegisters);
288 }
289 }
290
IsCurrentParamInRegister()291 bool X86JniCallingConvention::IsCurrentParamInRegister() {
292 return false; // Everything is passed by stack.
293 }
294
IsCurrentParamOnStack()295 bool X86JniCallingConvention::IsCurrentParamOnStack() {
296 return true; // Everything is passed by stack.
297 }
298
CurrentParamRegister()299 ManagedRegister X86JniCallingConvention::CurrentParamRegister() {
300 LOG(FATAL) << "Should not reach here";
301 UNREACHABLE();
302 }
303
CurrentParamStackOffset()304 FrameOffset X86JniCallingConvention::CurrentParamStackOffset() {
305 return
306 FrameOffset(displacement_.Int32Value() - OutFrameSize() + (itr_slots_ * kFramePointerSize));
307 }
308
LockingArgumentRegister() const309 ManagedRegister X86JniCallingConvention::LockingArgumentRegister() const {
310 DCHECK(!IsFastNative());
311 DCHECK(!IsCriticalNative());
312 DCHECK(IsSynchronized());
313 // The callee-save register is EBP is suitable as a locking argument.
314 static_assert(kCalleeSaveRegisters[0].Equals(X86ManagedRegister::FromCpuRegister(EBP)));
315 return X86ManagedRegister::FromCpuRegister(EBP);
316 }
317
HiddenArgumentRegister() const318 ManagedRegister X86JniCallingConvention::HiddenArgumentRegister() const {
319 CHECK(IsCriticalNative());
320 // EAX is neither managed callee-save, nor argument register, nor scratch register.
321 DCHECK(std::none_of(kCalleeSaveRegisters,
322 kCalleeSaveRegisters + std::size(kCalleeSaveRegisters),
323 [](ManagedRegister callee_save) constexpr {
324 return callee_save.Equals(X86ManagedRegister::FromCpuRegister(EAX));
325 }));
326 return X86ManagedRegister::FromCpuRegister(EAX);
327 }
328
UseTailCall() const329 bool X86JniCallingConvention::UseTailCall() const {
330 CHECK(IsCriticalNative());
331 return OutFrameSize() == kFramePointerSize;
332 }
333
334 } // namespace x86
335 } // namespace art
336