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 the arguments for a native call.
GetNativeOutArgsSize(size_t num_fp_args,size_t num_non_fp_args)50 inline size_t GetNativeOutArgsSize(size_t num_fp_args, size_t num_non_fp_args) {
51   // Account for FP arguments passed through Xmm0..Xmm7.
52   size_t num_stack_fp_args =
53       num_fp_args - std::min(kMaxFloatOrDoubleRegisterArguments, num_fp_args);
54   // Account for other (integer) arguments passed through GPR (RDI, RSI, RDX, RCX, R8, R9).
55   size_t num_stack_non_fp_args =
56       num_non_fp_args - std::min(kMaxIntLikeRegisterArguments, num_non_fp_args);
57   static_assert(kFramePointerSize == kMmxSpillSize);
58   return (num_stack_fp_args + num_stack_non_fp_args) * kFramePointerSize;
59 }
60 
61 // Get stack args size for @CriticalNative method calls.
GetCriticalNativeCallArgsSize(const char * shorty,uint32_t shorty_len)62 inline size_t GetCriticalNativeCallArgsSize(const char* shorty, uint32_t shorty_len) {
63   DCHECK_EQ(shorty_len, strlen(shorty));
64 
65   size_t num_fp_args =
66       std::count_if(shorty + 1, shorty + shorty_len, [](char c) { return c == 'F' || c == 'D'; });
67   size_t num_non_fp_args = shorty_len - 1u - num_fp_args;
68 
69   return GetNativeOutArgsSize(num_fp_args, num_non_fp_args);
70 }
71 
72 // Get the frame size for @CriticalNative method stub.
73 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeStubFrameSize(const char * shorty,uint32_t shorty_len)74 inline size_t GetCriticalNativeStubFrameSize(const char* shorty, uint32_t shorty_len) {
75   // The size of outgoing arguments.
76   size_t size = GetCriticalNativeCallArgsSize(shorty, shorty_len);
77 
78   // We always need to spill xmm12-xmm15 as they are managed callee-saves
79   // but not native callee-saves.
80   size += kAlwaysSpilledMmxRegisters * kMmxSpillSize;
81   // Add return address size.
82   size += kFramePointerSize;
83 
84   return RoundUp(size, kNativeStackAlignment);
85 }
86 
87 // Get the frame size for direct call to a @CriticalNative method.
88 // This must match the size of the extra frame emitted by the compiler at the native call site.
GetCriticalNativeDirectCallFrameSize(const char * shorty,uint32_t shorty_len)89 inline size_t GetCriticalNativeDirectCallFrameSize(const char* shorty, uint32_t shorty_len) {
90   // The size of outgoing arguments.
91   size_t size = GetCriticalNativeCallArgsSize(shorty, shorty_len);
92 
93   // No return PC to save, zero- and sign-extension are handled by the caller.
94   return RoundUp(size, kNativeStackAlignment);
95 }
96 
97 }  // namespace x86_64
98 }  // namespace art
99 
100 #endif  // ART_RUNTIME_ARCH_X86_64_JNI_FRAME_X86_64_H_
101 
102