• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "base/macros.h"
27 
28 namespace art HIDDEN {
29 namespace x86_64 {
30 
31 constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k64);
32 static_assert(kX86_64PointerSize == PointerSize::k64, "Unexpected x86_64 pointer size");
33 
34 static constexpr size_t kNativeStackAlignment = 16;
35 static_assert(kNativeStackAlignment == kStackAlignment);
36 
37 // We always have to spill registers xmm12-xmm15 which are callee-save
38 // in managed ABI but caller-save in native ABI.
39 constexpr size_t kMmxSpillSize = 8u;
40 constexpr size_t kAlwaysSpilledMmxRegisters = 4;
41 
42 // XMM0..XMM7 can be used to pass the first 8 floating args. The rest must go on the stack.
43 // -- Managed and JNI calling conventions.
44 constexpr size_t kMaxFloatOrDoubleRegisterArguments = 8u;
45 // Up to how many integer-like (pointers, objects, longs, int, short, bool, etc) args can be
46 // enregistered. The rest of the args must go on the stack.
47 // -- JNI calling convention only (Managed excludes RDI, so it's actually 5).
48 constexpr size_t kMaxIntLikeRegisterArguments = 6u;
49 
50 // Get the size of the arguments for a native call.
GetNativeOutArgsSize(size_t num_fp_args,size_t num_non_fp_args)51 inline size_t GetNativeOutArgsSize(size_t num_fp_args, size_t num_non_fp_args) {
52   // Account for FP arguments passed through Xmm0..Xmm7.
53   size_t num_stack_fp_args =
54       num_fp_args - std::min(kMaxFloatOrDoubleRegisterArguments, num_fp_args);
55   // Account for other (integer) arguments passed through GPR (RDI, RSI, RDX, RCX, R8, R9).
56   size_t num_stack_non_fp_args =
57       num_non_fp_args - std::min(kMaxIntLikeRegisterArguments, num_non_fp_args);
58   static_assert(kFramePointerSize == kMmxSpillSize);
59   return (num_stack_fp_args + num_stack_non_fp_args) * kFramePointerSize;
60 }
61 
62 // Get stack args size for @CriticalNative method calls.
GetCriticalNativeCallArgsSize(std::string_view shorty)63 inline size_t GetCriticalNativeCallArgsSize(std::string_view shorty) {
64   size_t num_fp_args =
65       std::count_if(shorty.begin() + 1, shorty.end(), [](char c) { return c == 'F' || c == 'D'; });
66   size_t num_non_fp_args = shorty.length() - 1u - num_fp_args;
67 
68   return GetNativeOutArgsSize(num_fp_args, num_non_fp_args);
69 }
70 
71 // Get the frame size for @CriticalNative method stub.
72 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeStubFrameSize(std::string_view shorty)73 inline size_t GetCriticalNativeStubFrameSize(std::string_view shorty) {
74   // The size of outgoing arguments.
75   size_t size = GetCriticalNativeCallArgsSize(shorty);
76 
77   // We always need to spill xmm12-xmm15 as they are managed callee-saves
78   // but not native callee-saves.
79   size += kAlwaysSpilledMmxRegisters * kMmxSpillSize;
80   // Add return address size.
81   size += kFramePointerSize;
82 
83   return RoundUp(size, kNativeStackAlignment);
84 }
85 
86 // Get the frame size for direct call to a @CriticalNative method.
87 // This must match the size of the extra frame emitted by the compiler at the native call site.
GetCriticalNativeDirectCallFrameSize(std::string_view shorty)88 inline size_t GetCriticalNativeDirectCallFrameSize(std::string_view shorty) {
89   // The size of outgoing arguments.
90   size_t size = GetCriticalNativeCallArgsSize(shorty);
91 
92   // No return PC to save, zero- and sign-extension are handled by the caller.
93   return RoundUp(size, kNativeStackAlignment);
94 }
95 
96 }  // namespace x86_64
97 }  // namespace art
98 
99 #endif  // ART_RUNTIME_ARCH_X86_64_JNI_FRAME_X86_64_H_
100