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