1 /*
2 * Copyright (C) 2020 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 #ifndef ART_RUNTIME_ARCH_X86_64_JNI_FRAME_X86_64_H_
18 #define ART_RUNTIME_ARCH_X86_64_JNI_FRAME_X86_64_H_
19
20 #include <string.h>
21
22 #include "arch/instruction_set.h"
23 #include "base/bit_utils.h"
24 #include "base/globals.h"
25 #include "base/logging.h"
26
27 namespace art {
28 namespace x86_64 {
29
30 constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k64);
31 static_assert(kX86_64PointerSize == PointerSize::k64, "Unexpected x86_64 pointer size");
32
33 static constexpr size_t kNativeStackAlignment = 16;
34 static_assert(kNativeStackAlignment == kStackAlignment);
35
36 // We always have to spill registers xmm12-xmm15 which are callee-save
37 // in managed ABI but caller-save in native ABI.
38 constexpr size_t kMmxSpillSize = 8u;
39 constexpr size_t kAlwaysSpilledMmxRegisters = 4;
40
41 // XMM0..XMM7 can be used to pass the first 8 floating args. The rest must go on the stack.
42 // -- Managed and JNI calling conventions.
43 constexpr size_t kMaxFloatOrDoubleRegisterArguments = 8u;
44 // Up to how many integer-like (pointers, objects, longs, int, short, bool, etc) args can be
45 // enregistered. The rest of the args must go on the stack.
46 // -- JNI calling convention only (Managed excludes RDI, so it's actually 5).
47 constexpr size_t kMaxIntLikeRegisterArguments = 6u;
48
49 // Get the size of "out args" for @CriticalNative method stub.
50 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeOutArgsSize(const char * shorty,uint32_t shorty_len)51 inline size_t GetCriticalNativeOutArgsSize(const char* shorty, uint32_t shorty_len) {
52 DCHECK_EQ(shorty_len, strlen(shorty));
53
54 size_t num_fp_args = 0u;
55 for (size_t i = 1; i != shorty_len; ++i) {
56 if (shorty[i] == 'F' || shorty[i] == 'D') {
57 num_fp_args += 1u;
58 }
59 }
60 size_t num_non_fp_args = shorty_len - 1u - num_fp_args;
61
62 // Account for FP arguments passed through Xmm0..Xmm7.
63 size_t num_stack_fp_args =
64 num_fp_args - std::min(kMaxFloatOrDoubleRegisterArguments, num_fp_args);
65 // Account for other (integer) arguments passed through GPR (RDI, RSI, RDX, RCX, R8, R9).
66 size_t num_stack_non_fp_args =
67 num_non_fp_args - std::min(kMaxIntLikeRegisterArguments, num_non_fp_args);
68 // The size of outgoing arguments.
69 static_assert(kFramePointerSize == kMmxSpillSize);
70 size_t size = (num_stack_fp_args + num_stack_non_fp_args) * kFramePointerSize;
71
72 // We always need to spill xmm12-xmm15 as they are managed callee-saves
73 // but not native callee-saves.
74 size += kAlwaysSpilledMmxRegisters * kMmxSpillSize;
75 // Add return address size.
76 size += kFramePointerSize;
77
78 return RoundUp(size, kNativeStackAlignment);
79 }
80
81 } // namespace x86_64
82 } // namespace art
83
84 #endif // ART_RUNTIME_ARCH_X86_64_JNI_FRAME_X86_64_H_
85
86