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_ARM64_JNI_FRAME_ARM64_H_
18 #define ART_RUNTIME_ARCH_ARM64_JNI_FRAME_ARM64_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 arm64 {
29 
30 constexpr size_t kFramePointerSize = static_cast<size_t>(PointerSize::k64);
31 static_assert(kArm64PointerSize == PointerSize::k64, "Unexpected ARM64 pointer size");
32 
33 // The AAPCS64 requires 16-byte alignement. This is the same as the Managed ABI stack alignment.
34 static constexpr size_t kAapcs64StackAlignment = 16u;
35 static_assert(kAapcs64StackAlignment == kStackAlignment);
36 
37 // Up to how many float-like (float, double) args can be in registers.
38 // The rest of the args must go on the stack.
39 constexpr size_t kMaxFloatOrDoubleRegisterArguments = 8u;
40 // Up to how many integer-like (pointers, objects, longs, int, short, bool, etc) args can be
41 // in registers. The rest of the args must go on the stack.
42 constexpr size_t kMaxIntLikeRegisterArguments = 8u;
43 
44 // Get the size of the arguments for a native call.
GetNativeOutArgsSize(size_t num_fp_args,size_t num_non_fp_args)45 inline size_t GetNativeOutArgsSize(size_t num_fp_args, size_t num_non_fp_args) {
46   // Account for FP arguments passed through v0-v7.
47   size_t num_stack_fp_args =
48       num_fp_args - std::min(kMaxFloatOrDoubleRegisterArguments, num_fp_args);
49   // Account for other (integer and pointer) arguments passed through GPR (x0-x7).
50   size_t num_stack_non_fp_args =
51       num_non_fp_args - std::min(kMaxIntLikeRegisterArguments, num_non_fp_args);
52   // Each stack argument takes 8 bytes.
53   return (num_stack_fp_args + num_stack_non_fp_args) * static_cast<size_t>(kArm64PointerSize);
54 }
55 
56 // Get stack args size for @CriticalNative method calls.
GetCriticalNativeCallArgsSize(const char * shorty,uint32_t shorty_len)57 inline size_t GetCriticalNativeCallArgsSize(const char* shorty, uint32_t shorty_len) {
58   DCHECK_EQ(shorty_len, strlen(shorty));
59 
60   size_t num_fp_args =
61       std::count_if(shorty + 1, shorty + shorty_len, [](char c) { return c == 'F' || c == 'D'; });
62   size_t num_non_fp_args = shorty_len - 1u - num_fp_args;
63 
64   return GetNativeOutArgsSize(num_fp_args, num_non_fp_args);
65 }
66 
67 // Get the frame size for @CriticalNative method stub.
68 // This must match the size of the extra frame emitted by the compiler at the native call site.
GetCriticalNativeStubFrameSize(const char * shorty,uint32_t shorty_len)69 inline size_t GetCriticalNativeStubFrameSize(const char* shorty, uint32_t shorty_len) {
70   // The size of outgoing arguments.
71   size_t size = GetCriticalNativeCallArgsSize(shorty, shorty_len);
72 
73   // We can make a tail call if there are no stack args and we do not need
74   // to extend the result. Otherwise, add space for return PC.
75   if (size != 0u || shorty[0] == 'B' || shorty[0] == 'C' || shorty[0] == 'S' || shorty[0] == 'Z') {
76     size += kFramePointerSize;  // We need to spill LR with the args.
77   }
78   return RoundUp(size, kAapcs64StackAlignment);
79 }
80 
81 // Get the frame size for direct call to a @CriticalNative method.
82 // This must match the size of the frame emitted by the JNI compiler at the native call site.
GetCriticalNativeDirectCallFrameSize(const char * shorty,uint32_t shorty_len)83 inline size_t GetCriticalNativeDirectCallFrameSize(const char* shorty, uint32_t shorty_len) {
84   // The size of outgoing arguments.
85   size_t size = GetCriticalNativeCallArgsSize(shorty, shorty_len);
86 
87   // No return PC to save, zero- and sign-extension are handled by the caller.
88   return RoundUp(size, kAapcs64StackAlignment);
89 }
90 
91 }  // namespace arm64
92 }  // namespace art
93 
94 #endif  // ART_RUNTIME_ARCH_ARM64_JNI_FRAME_ARM64_H_
95 
96