Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 17 | #include "jni_compiler.h" |
| 18 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <fstream> |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 21 | #include <ios> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 22 | #include <memory> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method.h" |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 26 | #include "base/arena_allocator.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 27 | #include "base/enums.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | #include "base/logging.h" |
| 29 | #include "base/macros.h" |
| 30 | #include "calling_convention.h" |
| 31 | #include "class_linker.h" |
| 32 | #include "compiled_method.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 33 | #include "debug/dwarf/debug_frame_opcode_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | #include "dex_file-inl.h" |
| 35 | #include "driver/compiler_driver.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 36 | #include "driver/compiler_options.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 37 | #include "entrypoints/quick/quick_entrypoints.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 38 | #include "jni_env_ext.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 39 | #include "memory_region.h" |
| 40 | #include "thread.h" |
| 41 | #include "utils.h" |
| 42 | #include "utils/arm/managed_register_arm.h" |
| 43 | #include "utils/arm64/managed_register_arm64.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 44 | #include "utils/assembler.h" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 45 | #include "utils/jni_macro_assembler.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 46 | #include "utils/managed_register.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 47 | #include "utils/mips/managed_register_mips.h" |
Maja Gagic | 6ea651f | 2015-02-24 16:55:04 +0100 | [diff] [blame] | 48 | #include "utils/mips64/managed_register_mips64.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 49 | #include "utils/x86/managed_register_x86.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 50 | |
| 51 | #define __ jni_asm-> |
| 52 | |
| 53 | namespace art { |
| 54 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 55 | using JniOptimizationFlags = Compiler::JniOptimizationFlags; |
| 56 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 57 | template <PointerSize kPointerSize> |
| 58 | static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 59 | ManagedRuntimeCallingConvention* mr_conv, |
| 60 | JniCallingConvention* jni_conv, |
| 61 | size_t frame_size, size_t out_arg_size); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 62 | template <PointerSize kPointerSize> |
| 63 | static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 64 | JniCallingConvention* jni_conv, |
| 65 | ManagedRegister in_reg); |
| 66 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 67 | template <PointerSize kPointerSize> |
| 68 | static std::unique_ptr<JNIMacroAssembler<kPointerSize>> GetMacroAssembler( |
| 69 | ArenaAllocator* arena, InstructionSet isa, const InstructionSetFeatures* features) { |
| 70 | return JNIMacroAssembler<kPointerSize>::Create(arena, isa, features); |
| 71 | } |
| 72 | |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame] | 73 | enum class JniEntrypoint { |
| 74 | kStart, |
| 75 | kEnd |
| 76 | }; |
| 77 | |
| 78 | template <PointerSize kPointerSize> |
| 79 | static ThreadOffset<kPointerSize> GetJniEntrypointThreadOffset(JniEntrypoint which, |
| 80 | bool reference_return, |
| 81 | bool is_synchronized, |
| 82 | bool is_fast_native) { |
| 83 | if (which == JniEntrypoint::kStart) { // JniMethodStart |
| 84 | ThreadOffset<kPointerSize> jni_start = |
| 85 | is_synchronized |
| 86 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStartSynchronized) |
| 87 | : (is_fast_native |
| 88 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastStart) |
| 89 | : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStart)); |
| 90 | |
| 91 | return jni_start; |
| 92 | } else { // JniMethodEnd |
| 93 | ThreadOffset<kPointerSize> jni_end(-1); |
| 94 | if (reference_return) { |
| 95 | // Pass result. |
| 96 | jni_end = is_synchronized |
| 97 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReferenceSynchronized) |
| 98 | : (is_fast_native |
| 99 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEndWithReference) |
| 100 | : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReference)); |
| 101 | } else { |
| 102 | jni_end = is_synchronized |
| 103 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndSynchronized) |
| 104 | : (is_fast_native |
| 105 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEnd) |
| 106 | : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEnd)); |
| 107 | } |
| 108 | |
| 109 | return jni_end; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 114 | // Generate the JNI bridge for the given method, general contract: |
| 115 | // - Arguments are in the managed runtime format, either on stack or in |
| 116 | // registers, a reference to the method object is supplied as part of this |
| 117 | // convention. |
| 118 | // |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 119 | template <PointerSize kPointerSize> |
| 120 | static CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver, |
| 121 | uint32_t access_flags, |
| 122 | uint32_t method_idx, |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 123 | const DexFile& dex_file, |
| 124 | JniOptimizationFlags optimization_flags) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 125 | const bool is_native = (access_flags & kAccNative) != 0; |
| 126 | CHECK(is_native); |
| 127 | const bool is_static = (access_flags & kAccStatic) != 0; |
| 128 | const bool is_synchronized = (access_flags & kAccSynchronized) != 0; |
| 129 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 130 | InstructionSet instruction_set = driver->GetInstructionSet(); |
Goran Jakovljevic | 8c434dc | 2015-08-26 14:39:44 +0200 | [diff] [blame] | 131 | const InstructionSetFeatures* instruction_set_features = driver->GetInstructionSetFeatures(); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 132 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 133 | // i.e. if the method was annotated with @FastNative |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 134 | const bool is_fast_native = (optimization_flags == Compiler::kFastNative); |
| 135 | |
| 136 | // i.e. if the method was annotated with @CriticalNative |
| 137 | bool is_critical_native = (optimization_flags == Compiler::kCriticalNative); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 138 | |
| 139 | VLOG(jni) << "JniCompile: Method :: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 140 | << dex_file.PrettyMethod(method_idx, /* with signature */ true) |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 141 | << " :: access_flags = " << std::hex << access_flags << std::dec; |
| 142 | |
| 143 | if (UNLIKELY(is_fast_native)) { |
| 144 | VLOG(jni) << "JniCompile: Fast native method detected :: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 145 | << dex_file.PrettyMethod(method_idx, /* with signature */ true); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 148 | if (UNLIKELY(is_critical_native)) { |
| 149 | VLOG(jni) << "JniCompile: Critical native method detected :: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 150 | << dex_file.PrettyMethod(method_idx, /* with signature */ true); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (kIsDebugBuild) { |
| 154 | // Don't allow both @FastNative and @CriticalNative. They are mutually exclusive. |
| 155 | if (UNLIKELY(is_fast_native && is_critical_native)) { |
| 156 | LOG(FATAL) << "JniCompile: Method cannot be both @CriticalNative and @FastNative" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 157 | << dex_file.PrettyMethod(method_idx, /* with_signature */ true); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // @CriticalNative - extra checks: |
| 161 | // -- Don't allow virtual criticals |
| 162 | // -- Don't allow synchronized criticals |
| 163 | // -- Don't allow any objects as parameter or return value |
| 164 | if (UNLIKELY(is_critical_native)) { |
| 165 | CHECK(is_static) |
| 166 | << "@CriticalNative functions cannot be virtual since that would" |
| 167 | << "require passing a reference parameter (this), which is illegal " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 168 | << dex_file.PrettyMethod(method_idx, /* with_signature */ true); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 169 | CHECK(!is_synchronized) |
| 170 | << "@CriticalNative functions cannot be synchronized since that would" |
| 171 | << "require passing a (class and/or this) reference parameter, which is illegal " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 172 | << dex_file.PrettyMethod(method_idx, /* with_signature */ true); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 173 | for (size_t i = 0; i < strlen(shorty); ++i) { |
| 174 | CHECK_NE(Primitive::kPrimNot, Primitive::GetType(shorty[i])) |
| 175 | << "@CriticalNative methods' shorty types must not have illegal references " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 176 | << dex_file.PrettyMethod(method_idx, /* with_signature */ true); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 181 | ArenaPool pool; |
| 182 | ArenaAllocator arena(&pool); |
| 183 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 184 | // Calling conventions used to iterate over parameters to method |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 185 | std::unique_ptr<JniCallingConvention> main_jni_conv = |
| 186 | JniCallingConvention::Create(&arena, |
| 187 | is_static, |
| 188 | is_synchronized, |
| 189 | is_critical_native, |
| 190 | shorty, |
| 191 | instruction_set); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 192 | bool reference_return = main_jni_conv->IsReturnAReference(); |
| 193 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 194 | std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv( |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 195 | ManagedRuntimeCallingConvention::Create( |
| 196 | &arena, is_static, is_synchronized, shorty, instruction_set)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 197 | |
| 198 | // Calling conventions to call into JNI method "end" possibly passing a returned reference, the |
| 199 | // method and the current thread. |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 200 | const char* jni_end_shorty; |
| 201 | if (reference_return && is_synchronized) { |
| 202 | jni_end_shorty = "ILL"; |
| 203 | } else if (reference_return) { |
| 204 | jni_end_shorty = "IL"; |
| 205 | } else if (is_synchronized) { |
| 206 | jni_end_shorty = "VL"; |
| 207 | } else { |
| 208 | jni_end_shorty = "V"; |
| 209 | } |
| 210 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 211 | std::unique_ptr<JniCallingConvention> end_jni_conv( |
| 212 | JniCallingConvention::Create(&arena, |
| 213 | is_static, |
| 214 | is_synchronized, |
| 215 | is_critical_native, |
| 216 | jni_end_shorty, |
| 217 | instruction_set)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 218 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 219 | // Assembler that holds generated instructions |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 220 | std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm = |
| 221 | GetMacroAssembler<kPointerSize>(&arena, instruction_set, instruction_set_features); |
Roland Levillain | 2b03a1f | 2017-06-06 16:09:59 +0100 | [diff] [blame] | 222 | const CompilerOptions& compiler_options = driver->GetCompilerOptions(); |
| 223 | jni_asm->cfi().SetEnabled(compiler_options.GenerateAnyDebugInfo()); |
| 224 | jni_asm->SetEmitRunTimeChecksInDebugMode(compiler_options.EmitRunTimeChecksInDebugMode()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 225 | |
| 226 | // Offsets into data structures |
| 227 | // TODO: if cross compiling these offsets are for the host not the target |
| 228 | const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions)); |
| 229 | const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter)); |
| 230 | const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit)); |
| 231 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 232 | // 1. Build the frame saving all callee saves, Method*, and PC return address. |
| 233 | const size_t frame_size(main_jni_conv->FrameSize()); // Excludes outgoing args. |
Vladimir Marko | 3224838 | 2016-05-19 10:37:24 +0100 | [diff] [blame] | 234 | ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 235 | __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills()); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 236 | DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 237 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 238 | if (LIKELY(!is_critical_native)) { |
| 239 | // NOTE: @CriticalNative methods don't have a HandleScope |
| 240 | // because they can't have any reference parameters or return values. |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 241 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 242 | // 2. Set up the HandleScope |
| 243 | mr_conv->ResetIterator(FrameOffset(frame_size)); |
| 244 | main_jni_conv->ResetIterator(FrameOffset(0)); |
| 245 | __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(), |
| 246 | main_jni_conv->ReferenceCount(), |
| 247 | mr_conv->InterproceduralScratchRegister()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 248 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 249 | __ CopyRawPtrFromThread(main_jni_conv->HandleScopeLinkOffset(), |
| 250 | Thread::TopHandleScopeOffset<kPointerSize>(), |
| 251 | mr_conv->InterproceduralScratchRegister()); |
| 252 | __ StoreStackOffsetToThread(Thread::TopHandleScopeOffset<kPointerSize>(), |
| 253 | main_jni_conv->HandleScopeOffset(), |
| 254 | mr_conv->InterproceduralScratchRegister()); |
| 255 | |
| 256 | // 3. Place incoming reference arguments into handle scope |
| 257 | main_jni_conv->Next(); // Skip JNIEnv* |
| 258 | // 3.5. Create Class argument for static methods out of passed method |
| 259 | if (is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 260 | FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 261 | // Check handle scope offset is within frame |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 262 | CHECK_LT(handle_scope_offset.Uint32Value(), frame_size); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 263 | // Note this LoadRef() doesn't need heap unpoisoning since it's from the ArtMethod. |
| 264 | // Note this LoadRef() does not include read barrier. It will be handled below. |
| 265 | // |
| 266 | // scratchRegister = *method[DeclaringClassOffset()]; |
| 267 | __ LoadRef(main_jni_conv->InterproceduralScratchRegister(), |
| 268 | mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false); |
| 269 | __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false); |
| 270 | // *handleScopeOffset = scratchRegister |
| 271 | __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister()); |
| 272 | main_jni_conv->Next(); // in handle scope so move to next argument |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | } |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 274 | // Place every reference into the handle scope (ignore other parameters). |
| 275 | while (mr_conv->HasNext()) { |
| 276 | CHECK(main_jni_conv->HasNext()); |
| 277 | bool ref_param = main_jni_conv->IsCurrentParamAReference(); |
| 278 | CHECK(!ref_param || mr_conv->IsCurrentParamAReference()); |
| 279 | // References need placing in handle scope and the entry value passing |
| 280 | if (ref_param) { |
| 281 | // Compute handle scope entry, note null is placed in the handle scope but its boxed value |
| 282 | // must be null. |
| 283 | FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
| 284 | // Check handle scope offset is within frame and doesn't run into the saved segment state. |
| 285 | CHECK_LT(handle_scope_offset.Uint32Value(), frame_size); |
| 286 | CHECK_NE(handle_scope_offset.Uint32Value(), |
| 287 | main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value()); |
| 288 | bool input_in_reg = mr_conv->IsCurrentParamInRegister(); |
| 289 | bool input_on_stack = mr_conv->IsCurrentParamOnStack(); |
| 290 | CHECK(input_in_reg || input_on_stack); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 291 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 292 | if (input_in_reg) { |
| 293 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 294 | __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull()); |
| 295 | __ StoreRef(handle_scope_offset, in_reg); |
| 296 | } else if (input_on_stack) { |
| 297 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 298 | __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull()); |
| 299 | __ CopyRef(handle_scope_offset, in_off, |
| 300 | mr_conv->InterproceduralScratchRegister()); |
| 301 | } |
| 302 | } |
| 303 | mr_conv->Next(); |
| 304 | main_jni_conv->Next(); |
| 305 | } |
| 306 | |
| 307 | // 4. Write out the end of the quick frames. |
| 308 | __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>()); |
| 309 | |
| 310 | // NOTE: @CriticalNative does not need to store the stack pointer to the thread |
| 311 | // because garbage collections are disabled within the execution of a |
| 312 | // @CriticalNative method. |
| 313 | // (TODO: We could probably disable it for @FastNative too). |
| 314 | } // if (!is_critical_native) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 315 | |
| 316 | // 5. Move frame down to allow space for out going args. |
| 317 | const size_t main_out_arg_size = main_jni_conv->OutArgSize(); |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 318 | size_t current_out_arg_size = main_out_arg_size; |
| 319 | __ IncreaseFrameSize(main_out_arg_size); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 320 | |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 321 | // Call the read barrier for the declaring class loaded from the method for a static call. |
Igor Murashkin | ae7ff92 | 2016-10-06 14:59:19 -0700 | [diff] [blame] | 322 | // Skip this for @CriticalNative because we didn't build a HandleScope to begin with. |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 323 | // Note that we always have outgoing param space available for at least two params. |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 324 | if (kUseReadBarrier && is_static && !is_critical_native) { |
Igor Murashkin | ae7ff92 | 2016-10-06 14:59:19 -0700 | [diff] [blame] | 325 | const bool kReadBarrierFastPath = |
| 326 | (instruction_set != kMips) && (instruction_set != kMips64); |
| 327 | std::unique_ptr<JNIMacroLabel> skip_cold_path_label; |
| 328 | if (kReadBarrierFastPath) { |
| 329 | skip_cold_path_label = __ CreateLabel(); |
| 330 | // Fast path for supported targets. |
| 331 | // |
| 332 | // Check if gc_is_marking is set -- if it's not, we don't need |
| 333 | // a read barrier so skip it. |
| 334 | __ LoadFromThread(main_jni_conv->InterproceduralScratchRegister(), |
| 335 | Thread::IsGcMarkingOffset<kPointerSize>(), |
| 336 | Thread::IsGcMarkingSize()); |
| 337 | // Jump over the slow path if gc is marking is false. |
| 338 | __ Jump(skip_cold_path_label.get(), |
| 339 | JNIMacroUnaryCondition::kZero, |
| 340 | main_jni_conv->InterproceduralScratchRegister()); |
| 341 | } |
| 342 | |
| 343 | // Construct slow path for read barrier: |
| 344 | // |
| 345 | // Call into the runtime's ReadBarrierJni and have it fix up |
| 346 | // the object address if it was moved. |
| 347 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 348 | ThreadOffset<kPointerSize> read_barrier = QUICK_ENTRYPOINT_OFFSET(kPointerSize, |
| 349 | pReadBarrierJni); |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 350 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 351 | main_jni_conv->Next(); // Skip JNIEnv. |
| 352 | FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
| 353 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 354 | // Pass the handle for the class as the first argument. |
| 355 | if (main_jni_conv->IsCurrentParamOnStack()) { |
| 356 | FrameOffset out_off = main_jni_conv->CurrentParamStackOffset(); |
| 357 | __ CreateHandleScopeEntry(out_off, class_handle_scope_offset, |
| 358 | mr_conv->InterproceduralScratchRegister(), |
| 359 | false); |
| 360 | } else { |
| 361 | ManagedRegister out_reg = main_jni_conv->CurrentParamRegister(); |
| 362 | __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset, |
| 363 | ManagedRegister::NoRegister(), false); |
| 364 | } |
| 365 | main_jni_conv->Next(); |
| 366 | // Pass the current thread as the second argument and call. |
| 367 | if (main_jni_conv->IsCurrentParamInRegister()) { |
| 368 | __ GetCurrentThread(main_jni_conv->CurrentParamRegister()); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 369 | __ Call(main_jni_conv->CurrentParamRegister(), |
| 370 | Offset(read_barrier), |
| 371 | main_jni_conv->InterproceduralScratchRegister()); |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 372 | } else { |
| 373 | __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(), |
| 374 | main_jni_conv->InterproceduralScratchRegister()); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 375 | __ CallFromThread(read_barrier, main_jni_conv->InterproceduralScratchRegister()); |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 376 | } |
| 377 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset. |
Igor Murashkin | ae7ff92 | 2016-10-06 14:59:19 -0700 | [diff] [blame] | 378 | |
| 379 | if (kReadBarrierFastPath) { |
| 380 | __ Bind(skip_cold_path_label.get()); |
| 381 | } |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 384 | // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable |
| 385 | // can occur. The result is the saved JNI local state that is restored by the exit call. We |
| 386 | // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer |
| 387 | // arguments. |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 388 | FrameOffset locked_object_handle_scope_offset(0xBEEFDEAD); |
| 389 | if (LIKELY(!is_critical_native)) { |
| 390 | // Skip this for @CriticalNative methods. They do not call JniMethodStart. |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame] | 391 | ThreadOffset<kPointerSize> jni_start( |
| 392 | GetJniEntrypointThreadOffset<kPointerSize>(JniEntrypoint::kStart, |
| 393 | reference_return, |
| 394 | is_synchronized, |
| 395 | is_fast_native).SizeValue()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 396 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 397 | locked_object_handle_scope_offset = FrameOffset(0); |
| 398 | if (is_synchronized) { |
| 399 | // Pass object for locking. |
| 400 | main_jni_conv->Next(); // Skip JNIEnv. |
| 401 | locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
| 402 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 403 | if (main_jni_conv->IsCurrentParamOnStack()) { |
| 404 | FrameOffset out_off = main_jni_conv->CurrentParamStackOffset(); |
| 405 | __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset, |
| 406 | mr_conv->InterproceduralScratchRegister(), false); |
| 407 | } else { |
| 408 | ManagedRegister out_reg = main_jni_conv->CurrentParamRegister(); |
| 409 | __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset, |
| 410 | ManagedRegister::NoRegister(), false); |
| 411 | } |
| 412 | main_jni_conv->Next(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 413 | } |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 414 | if (main_jni_conv->IsCurrentParamInRegister()) { |
| 415 | __ GetCurrentThread(main_jni_conv->CurrentParamRegister()); |
| 416 | __ Call(main_jni_conv->CurrentParamRegister(), |
| 417 | Offset(jni_start), |
| 418 | main_jni_conv->InterproceduralScratchRegister()); |
| 419 | } else { |
| 420 | __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(), |
| 421 | main_jni_conv->InterproceduralScratchRegister()); |
| 422 | __ CallFromThread(jni_start, main_jni_conv->InterproceduralScratchRegister()); |
| 423 | } |
| 424 | if (is_synchronized) { // Check for exceptions from monitor enter. |
| 425 | __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size); |
| 426 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 427 | } |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 428 | |
| 429 | // Store into stack_frame[saved_cookie_offset] the return value of JniMethodStart. |
| 430 | FrameOffset saved_cookie_offset( |
| 431 | FrameOffset(0xDEADBEEFu)); // @CriticalNative - use obviously bad value for debugging |
| 432 | if (LIKELY(!is_critical_native)) { |
| 433 | saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset(); |
| 434 | __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4 /* sizeof cookie */); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 435 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 436 | |
| 437 | // 7. Iterate over arguments placing values from managed calling convention in |
| 438 | // to the convention required for a native call (shuffling). For references |
| 439 | // place an index/pointer to the reference after checking whether it is |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 440 | // null (which must be encoded as null). |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 441 | // Note: we do this prior to materializing the JNIEnv* and static's jclass to |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 442 | // give as many free registers for the shuffle as possible. |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 443 | mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 444 | uint32_t args_count = 0; |
| 445 | while (mr_conv->HasNext()) { |
| 446 | args_count++; |
| 447 | mr_conv->Next(); |
| 448 | } |
| 449 | |
| 450 | // Do a backward pass over arguments, so that the generated code will be "mov |
| 451 | // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3." |
| 452 | // TODO: A reverse iterator to improve readability. |
| 453 | for (uint32_t i = 0; i < args_count; ++i) { |
| 454 | mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size)); |
| 455 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 456 | |
| 457 | // Skip the extra JNI parameters for now. |
| 458 | if (LIKELY(!is_critical_native)) { |
| 459 | main_jni_conv->Next(); // Skip JNIEnv*. |
| 460 | if (is_static) { |
| 461 | main_jni_conv->Next(); // Skip Class for now. |
| 462 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 463 | } |
| 464 | // Skip to the argument we're interested in. |
| 465 | for (uint32_t j = 0; j < args_count - i - 1; ++j) { |
| 466 | mr_conv->Next(); |
| 467 | main_jni_conv->Next(); |
| 468 | } |
| 469 | CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size); |
| 470 | } |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 471 | if (is_static && !is_critical_native) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 472 | // Create argument for Class |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 473 | mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 474 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 475 | main_jni_conv->Next(); // Skip JNIEnv* |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 476 | FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 477 | if (main_jni_conv->IsCurrentParamOnStack()) { |
| 478 | FrameOffset out_off = main_jni_conv->CurrentParamStackOffset(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 479 | __ CreateHandleScopeEntry(out_off, handle_scope_offset, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 480 | mr_conv->InterproceduralScratchRegister(), |
| 481 | false); |
| 482 | } else { |
| 483 | ManagedRegister out_reg = main_jni_conv->CurrentParamRegister(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 484 | __ CreateHandleScopeEntry(out_reg, handle_scope_offset, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 485 | ManagedRegister::NoRegister(), false); |
| 486 | } |
| 487 | } |
| 488 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 489 | // Set the iterator back to the incoming Method*. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 490 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 491 | if (LIKELY(!is_critical_native)) { |
| 492 | // 8. Create 1st argument, the JNI environment ptr. |
| 493 | // Register that will hold local indirect reference table |
| 494 | if (main_jni_conv->IsCurrentParamInRegister()) { |
| 495 | ManagedRegister jni_env = main_jni_conv->CurrentParamRegister(); |
| 496 | DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister())); |
| 497 | __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset<kPointerSize>()); |
| 498 | } else { |
| 499 | FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset(); |
| 500 | __ CopyRawPtrFromThread(jni_env, |
| 501 | Thread::JniEnvOffset<kPointerSize>(), |
| 502 | main_jni_conv->InterproceduralScratchRegister()); |
| 503 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // 9. Plant call to native code associated with method. |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 507 | MemberOffset jni_entrypoint_offset = |
| 508 | ArtMethod::EntryPointFromJniOffset(InstructionSetPointerSize(instruction_set)); |
| 509 | // FIXME: Not sure if MethodStackOffset will work here. What does it even do? |
| 510 | __ Call(main_jni_conv->MethodStackOffset(), |
| 511 | jni_entrypoint_offset, |
| 512 | // XX: Why not the jni conv scratch register? |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 513 | mr_conv->InterproceduralScratchRegister()); |
| 514 | |
| 515 | // 10. Fix differences in result widths. |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 516 | if (main_jni_conv->RequiresSmallResultTypeExtension()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 517 | if (main_jni_conv->GetReturnType() == Primitive::kPrimByte || |
| 518 | main_jni_conv->GetReturnType() == Primitive::kPrimShort) { |
| 519 | __ SignExtend(main_jni_conv->ReturnRegister(), |
| 520 | Primitive::ComponentSize(main_jni_conv->GetReturnType())); |
| 521 | } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean || |
| 522 | main_jni_conv->GetReturnType() == Primitive::kPrimChar) { |
| 523 | __ ZeroExtend(main_jni_conv->ReturnRegister(), |
| 524 | Primitive::ComponentSize(main_jni_conv->GetReturnType())); |
| 525 | } |
| 526 | } |
| 527 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 528 | // 11. Process return value |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 529 | FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation(); |
| 530 | if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) { |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 531 | if (LIKELY(!is_critical_native)) { |
| 532 | // For normal JNI, store the return value on the stack because the call to |
| 533 | // JniMethodEnd will clobber the return value. It will be restored in (13). |
| 534 | if ((instruction_set == kMips || instruction_set == kMips64) && |
| 535 | main_jni_conv->GetReturnType() == Primitive::kPrimDouble && |
| 536 | return_save_location.Uint32Value() % 8 != 0) { |
| 537 | // Ensure doubles are 8-byte aligned for MIPS |
| 538 | return_save_location = FrameOffset(return_save_location.Uint32Value() |
| 539 | + static_cast<size_t>(kMipsPointerSize)); |
| 540 | // TODO: refactor this into the JniCallingConvention code |
| 541 | // as a return value alignment requirement. |
| 542 | } |
| 543 | CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size); |
| 544 | __ Store(return_save_location, |
| 545 | main_jni_conv->ReturnRegister(), |
| 546 | main_jni_conv->SizeOfReturnValue()); |
| 547 | } else { |
| 548 | // For @CriticalNative only, |
| 549 | // move the JNI return register into the managed return register (if they don't match). |
| 550 | ManagedRegister jni_return_reg = main_jni_conv->ReturnRegister(); |
| 551 | ManagedRegister mr_return_reg = mr_conv->ReturnRegister(); |
| 552 | |
| 553 | // Check if the JNI return register matches the managed return register. |
| 554 | // If they differ, only then do we have to do anything about it. |
| 555 | // Otherwise the return value is already in the right place when we return. |
| 556 | if (!jni_return_reg.Equals(mr_return_reg)) { |
| 557 | // This is typically only necessary on ARM32 due to native being softfloat |
| 558 | // while managed is hardfloat. |
| 559 | // -- For example VMOV {r0, r1} -> D0; VMOV r0 -> S0. |
| 560 | __ Move(mr_return_reg, jni_return_reg, main_jni_conv->SizeOfReturnValue()); |
| 561 | } else if (jni_return_reg.IsNoRegister() && mr_return_reg.IsNoRegister()) { |
| 562 | // Sanity check: If the return value is passed on the stack for some reason, |
| 563 | // then make sure the size matches. |
| 564 | CHECK_EQ(main_jni_conv->SizeOfReturnValue(), mr_conv->SizeOfReturnValue()); |
| 565 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 566 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 569 | // Increase frame size for out args if needed by the end_jni_conv. |
| 570 | const size_t end_out_arg_size = end_jni_conv->OutArgSize(); |
| 571 | if (end_out_arg_size > current_out_arg_size) { |
| 572 | size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size; |
| 573 | current_out_arg_size = end_out_arg_size; |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 574 | // TODO: This is redundant for @CriticalNative but we need to |
| 575 | // conditionally do __DecreaseFrameSize below. |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 576 | __ IncreaseFrameSize(out_arg_size_diff); |
| 577 | saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff); |
| 578 | locked_object_handle_scope_offset = |
| 579 | FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff); |
| 580 | return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff); |
| 581 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 582 | // thread. |
| 583 | end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size)); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 584 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 585 | if (LIKELY(!is_critical_native)) { |
| 586 | // 12. Call JniMethodEnd |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame] | 587 | ThreadOffset<kPointerSize> jni_end( |
| 588 | GetJniEntrypointThreadOffset<kPointerSize>(JniEntrypoint::kEnd, |
| 589 | reference_return, |
| 590 | is_synchronized, |
| 591 | is_fast_native).SizeValue()); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 592 | if (reference_return) { |
| 593 | // Pass result. |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 594 | SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister()); |
| 595 | end_jni_conv->Next(); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 596 | } |
| 597 | // Pass saved local reference state. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 598 | if (end_jni_conv->IsCurrentParamOnStack()) { |
| 599 | FrameOffset out_off = end_jni_conv->CurrentParamStackOffset(); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 600 | __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 601 | } else { |
| 602 | ManagedRegister out_reg = end_jni_conv->CurrentParamRegister(); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 603 | __ Load(out_reg, saved_cookie_offset, 4); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 604 | } |
| 605 | end_jni_conv->Next(); |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 606 | if (is_synchronized) { |
| 607 | // Pass object for unlocking. |
| 608 | if (end_jni_conv->IsCurrentParamOnStack()) { |
| 609 | FrameOffset out_off = end_jni_conv->CurrentParamStackOffset(); |
| 610 | __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset, |
| 611 | end_jni_conv->InterproceduralScratchRegister(), |
| 612 | false); |
| 613 | } else { |
| 614 | ManagedRegister out_reg = end_jni_conv->CurrentParamRegister(); |
| 615 | __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset, |
| 616 | ManagedRegister::NoRegister(), false); |
| 617 | } |
| 618 | end_jni_conv->Next(); |
| 619 | } |
| 620 | if (end_jni_conv->IsCurrentParamInRegister()) { |
| 621 | __ GetCurrentThread(end_jni_conv->CurrentParamRegister()); |
| 622 | __ Call(end_jni_conv->CurrentParamRegister(), |
| 623 | Offset(jni_end), |
| 624 | end_jni_conv->InterproceduralScratchRegister()); |
| 625 | } else { |
| 626 | __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(), |
| 627 | end_jni_conv->InterproceduralScratchRegister()); |
| 628 | __ CallFromThread(jni_end, end_jni_conv->InterproceduralScratchRegister()); |
| 629 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 630 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 631 | // 13. Reload return value |
| 632 | if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) { |
| 633 | __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue()); |
| 634 | // NIT: If it's @CriticalNative then we actually only need to do this IF |
| 635 | // the calling convention's native return register doesn't match the managed convention's |
| 636 | // return register. |
| 637 | } |
| 638 | } // if (!is_critical_native) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 639 | |
| 640 | // 14. Move frame up now we're done with the out arg space. |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 641 | __ DecreaseFrameSize(current_out_arg_size); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 642 | |
| 643 | // 15. Process pending exceptions from JNI call or monitor exit. |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 644 | __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0 /* stack_adjust */); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 645 | |
Mathieu Chartier | 8770e5c | 2013-10-16 14:49:01 -0700 | [diff] [blame] | 646 | // 16. Remove activation - need to restore callee save registers since the GC may have changed |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 647 | // them. |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 648 | DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size)); |
Mathieu Chartier | 8770e5c | 2013-10-16 14:49:01 -0700 | [diff] [blame] | 649 | __ RemoveFrame(frame_size, callee_save_regs); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 650 | DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 651 | |
| 652 | // 17. Finalize code generation |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 653 | __ FinalizeCode(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 654 | size_t cs = __ CodeSize(); |
| 655 | std::vector<uint8_t> managed_code(cs); |
| 656 | MemoryRegion code(&managed_code[0], managed_code.size()); |
| 657 | __ FinalizeInstructions(code); |
David Srbecky | 8c57831 | 2015-04-07 19:46:22 +0100 | [diff] [blame] | 658 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 659 | return CompiledMethod::SwapAllocCompiledMethod(driver, |
| 660 | instruction_set, |
| 661 | ArrayRef<const uint8_t>(managed_code), |
| 662 | frame_size, |
| 663 | main_jni_conv->CoreSpillMask(), |
| 664 | main_jni_conv->FpSpillMask(), |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 665 | /* method_info */ ArrayRef<const uint8_t>(), |
| 666 | /* vmap_table */ ArrayRef<const uint8_t>(), |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 667 | ArrayRef<const uint8_t>(*jni_asm->cfi().data()), |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 668 | ArrayRef<const linker::LinkerPatch>()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 671 | // Copy a single parameter from the managed to the JNI calling convention. |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 672 | template <PointerSize kPointerSize> |
| 673 | static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 674 | ManagedRuntimeCallingConvention* mr_conv, |
| 675 | JniCallingConvention* jni_conv, |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 676 | size_t frame_size, |
| 677 | size_t out_arg_size) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 678 | bool input_in_reg = mr_conv->IsCurrentParamInRegister(); |
| 679 | bool output_in_reg = jni_conv->IsCurrentParamInRegister(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 680 | FrameOffset handle_scope_offset(0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 681 | bool null_allowed = false; |
| 682 | bool ref_param = jni_conv->IsCurrentParamAReference(); |
| 683 | CHECK(!ref_param || mr_conv->IsCurrentParamAReference()); |
| 684 | // input may be in register, on stack or both - but not none! |
| 685 | CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack()); |
| 686 | if (output_in_reg) { // output shouldn't straddle registers and stack |
| 687 | CHECK(!jni_conv->IsCurrentParamOnStack()); |
| 688 | } else { |
| 689 | CHECK(jni_conv->IsCurrentParamOnStack()); |
| 690 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 691 | // References need placing in handle scope and the entry address passing. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 692 | if (ref_param) { |
| 693 | null_allowed = mr_conv->IsCurrentArgPossiblyNull(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 694 | // Compute handle scope offset. Note null is placed in the handle scope but the jobject |
| 695 | // passed to the native code must be null (not a pointer into the handle scope |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 696 | // as with regular references). |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 697 | handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset(); |
| 698 | // Check handle scope offset is within frame. |
| 699 | CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 700 | } |
| 701 | if (input_in_reg && output_in_reg) { |
| 702 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 703 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 704 | if (ref_param) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 705 | __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 706 | } else { |
| 707 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 708 | // regular non-straddling move |
| 709 | __ Move(out_reg, in_reg, mr_conv->CurrentParamSize()); |
| 710 | } else { |
| 711 | UNIMPLEMENTED(FATAL); // we currently don't expect to see this case |
| 712 | } |
| 713 | } |
| 714 | } else if (!input_in_reg && !output_in_reg) { |
| 715 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 716 | if (ref_param) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 717 | __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 718 | null_allowed); |
| 719 | } else { |
| 720 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 721 | size_t param_size = mr_conv->CurrentParamSize(); |
| 722 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 723 | __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size); |
| 724 | } |
| 725 | } else if (!input_in_reg && output_in_reg) { |
| 726 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 727 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 728 | // Check that incoming stack arguments are above the current stack frame. |
| 729 | CHECK_GT(in_off.Uint32Value(), frame_size); |
| 730 | if (ref_param) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 731 | __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 732 | } else { |
| 733 | size_t param_size = mr_conv->CurrentParamSize(); |
| 734 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 735 | __ Load(out_reg, in_off, param_size); |
| 736 | } |
| 737 | } else { |
| 738 | CHECK(input_in_reg && !output_in_reg); |
| 739 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 740 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 741 | // Check outgoing argument is within frame |
| 742 | CHECK_LT(out_off.Uint32Value(), frame_size); |
| 743 | if (ref_param) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 744 | // TODO: recycle value in in_reg rather than reload from handle scope |
| 745 | __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 746 | null_allowed); |
| 747 | } else { |
| 748 | size_t param_size = mr_conv->CurrentParamSize(); |
| 749 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 750 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 751 | // regular non-straddling store |
| 752 | __ Store(out_off, in_reg, param_size); |
| 753 | } else { |
| 754 | // store where input straddles registers and stack |
| 755 | CHECK_EQ(param_size, 8u); |
| 756 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 757 | __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister()); |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 763 | template <PointerSize kPointerSize> |
| 764 | static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 765 | JniCallingConvention* jni_conv, |
| 766 | ManagedRegister in_reg) { |
| 767 | if (jni_conv->IsCurrentParamOnStack()) { |
| 768 | FrameOffset dest = jni_conv->CurrentParamStackOffset(); |
| 769 | __ StoreRawPtr(dest, in_reg); |
| 770 | } else { |
| 771 | if (!jni_conv->CurrentParamRegister().Equals(in_reg)) { |
| 772 | __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize()); |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 777 | CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, |
| 778 | uint32_t access_flags, |
| 779 | uint32_t method_idx, |
| 780 | const DexFile& dex_file, |
| 781 | Compiler::JniOptimizationFlags optimization_flags) { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 782 | if (Is64BitInstructionSet(compiler->GetInstructionSet())) { |
| 783 | return ArtJniCompileMethodInternal<PointerSize::k64>( |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 784 | compiler, access_flags, method_idx, dex_file, optimization_flags); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 785 | } else { |
| 786 | return ArtJniCompileMethodInternal<PointerSize::k32>( |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 787 | compiler, access_flags, method_idx, dex_file, optimization_flags); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 788 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 789 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 790 | |
| 791 | } // namespace art |