1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "asm_support_x86_64.S" 18 19#define MANAGED_ARGS_SAVE_SIZE /*xmm0-xmm7*/ 8 * 8 + /*padding*/ 8 + /* GPR args */ 6 * 8 20 21MACRO0(SAVE_MANAGED_ARGS_INCREASE_FRAME) 22 // Return address is on the stack. 23 PUSH_ARG r9 24 PUSH_ARG r8 25 PUSH_ARG rcx 26 PUSH_ARG rdx 27 PUSH_ARG rsi 28 PUSH_ARG rdi 29 INCREASE_FRAME (/*FPRs*/ 8 * 8 + /*padding*/ 8) 30 movsd %xmm0, 0(%rsp) 31 movsd %xmm1, 8(%rsp) 32 movsd %xmm2, 16(%rsp) 33 movsd %xmm3, 24(%rsp) 34 movsd %xmm4, 32(%rsp) 35 movsd %xmm5, 40(%rsp) 36 movsd %xmm6, 48(%rsp) 37 movsd %xmm7, 56(%rsp) 38END_MACRO 39 40MACRO0(RESTORE_MANAGED_ARGS_DECREASE_FRAME) 41 movsd 0(%rsp), %xmm0 42 movsd 8(%rsp), %xmm1 43 movsd 16(%rsp), %xmm2 44 movsd 24(%rsp), %xmm3 45 movsd 32(%rsp), %xmm4 46 movsd 40(%rsp), %xmm5 47 movsd 48(%rsp), %xmm6 48 movsd 56(%rsp), %xmm7 49 DECREASE_FRAME (/*FPRs*/ 8 * 8 + /*padding*/ 8) 50 POP_ARG rdi 51 POP_ARG rsi 52 POP_ARG rdx 53 POP_ARG rcx 54 POP_ARG r8 55 POP_ARG r9 56END_MACRO 57 58MACRO3(JNI_SAVE_MANAGED_ARGS_TRAMPOLINE, name, cxx_name, arg1) 59DEFINE_FUNCTION \name 60 // Note: Managed callee-save registers have been saved by the JNI stub. 61 // Save register args RDI, RSI, RDX, RCX, R8, R9, mmx0-mmx7 and align stack. 62 SAVE_MANAGED_ARGS_INCREASE_FRAME 63 // Call `cxx_name()`. 64 .ifnc \arg1, none 65 mov REG_VAR(arg1), %rdi // Pass arg1. 66 .endif 67 call CALLVAR(cxx_name) // Call cxx_name(...). 68 // Restore register args RDI, RSI, RDX, RCX, R8, R9, mmx0-mmx7 and return. 69 RESTORE_MANAGED_ARGS_DECREASE_FRAME 70 ret 71END_FUNCTION \name 72END_MACRO 73 74MACRO4(JNI_SAVE_RETURN_VALUE_TRAMPOLINE, name, cxx_name, arg1, arg2) 75DEFINE_FUNCTION \name 76 // Save return registers and return address. 77 PUSH_ARG rax 78 INCREASE_FRAME /*mmx0*/ 8 + /*padding*/ 8 79 movsd %xmm0, 0(%rsp) 80 // Call `cxx_name()`. 81 mov REG_VAR(arg1), %rdi // Pass arg1. 82 .ifnc \arg2, none 83 mov REG_VAR(arg2), %rsi // Pass arg2. 84 .endif 85 call CALLVAR(cxx_name) // Call cxx_name(...). 86 // Restore return registers and return. 87 movsd 0(%rsp), %xmm0 88 DECREASE_FRAME /*mmx0*/ 8 + /*padding*/ 8 89 POP_ARG rax 90 ret 91END_FUNCTION \name 92END_MACRO 93 94 /* 95 * Jni dlsym lookup stub for @CriticalNative. 96 */ 97DEFINE_FUNCTION art_jni_dlsym_lookup_critical_stub 98 // The hidden arg holding the tagged method (bit 0 set means GenericJNI) is RAX. 99 // For Generic JNI we already have a managed frame, so we reuse the art_jni_dlsym_lookup_stub. 100 testq LITERAL(1), %rax 101 jnz art_jni_dlsym_lookup_stub 102 103 // Save GPR args and method. 104 PUSH_ARG r9 105 PUSH_ARG r8 106 PUSH_ARG rdi 107 PUSH_ARG rsi 108 PUSH_ARG rdx 109 PUSH_ARG rcx 110 PUSH_ARG rax 111 // Create space for FPR args. 112 INCREASE_FRAME 8 * 8 113 // Save FPRs. 114 movq %xmm0, 0(%rsp) 115 movq %xmm1, 8(%rsp) 116 movq %xmm2, 16(%rsp) 117 movq %xmm3, 24(%rsp) 118 movq %xmm4, 32(%rsp) 119 movq %xmm5, 40(%rsp) 120 movq %xmm6, 48(%rsp) 121 movq %xmm7, 56(%rsp) 122 // Note: It's the caller's responsibility to preserve xmm12-xmm15 as the tail call 123 // to native shall always risk clobbering those. 124 125 // Call artCriticalNativeFrameSize(method, caller_pc). 126 movq %rax, %rdi // Pass the method from hidden arg. 127 movq 120(%rsp), %rsi // Pass caller PC. 128 call SYMBOL(artCriticalNativeFrameSize) 129 130 // Restore registers. 131 movq 0(%rsp), %xmm0 132 movq 8(%rsp), %xmm1 133 movq 16(%rsp), %xmm2 134 movq 24(%rsp), %xmm3 135 movq 32(%rsp), %xmm4 136 movq 40(%rsp), %xmm5 137 movq 48(%rsp), %xmm6 138 movq 56(%rsp), %xmm7 139 DECREASE_FRAME 8 * 8 140 POP_ARG r10 // Restore method to R10. 141 POP_ARG rcx 142 POP_ARG rdx 143 POP_ARG rsi 144 POP_ARG rdi 145 POP_ARG r8 146 POP_ARG r9 147 148 // Load caller PC to R11 and redefine return PC for CFI. 149 movq (%rsp), %r11 150 CFI_REGISTER(%rip, %r11) 151 152 // Reserve space for a SaveRefsAndArgs managed frame, either for the actual runtime 153 // method or for a GenericJNI frame which is similar but has a native method and a tag. 154 INCREASE_FRAME FRAME_SIZE_SAVE_REFS_AND_ARGS - __SIZEOF_POINTER__ 155 156 // Calculate the number of QWORDs to move. 157 shrq LITERAL(3), %rax 158 jz .Lcritical_skip_copy_args 159 160 // Save RDI, RSI, RCX so that we can use them for moving stack args. 161 PUSH_ARG rdi 162 PUSH_ARG rsi 163 PUSH_ARG rcx 164 165 // Move the stack args. 166 movq %rax, %rcx 167 leaq 3 * __SIZEOF_POINTER__(%rsp), %rdi 168 leaq FRAME_SIZE_SAVE_REFS_AND_ARGS(%rdi), %rsi 169 rep movsq 170 171 // Restore RDI, RSI, RCX. 172 POP_ARG rcx 173 POP_ARG rsi 174 POP_ARG rdi 175 176.Lcritical_skip_copy_args: 177 // Calculate the base address of the managed frame. 178 leaq (%rsp, %rax, 8), %rax 179 180 // Spill registers for the SaveRefsAndArgs frame above the stack args. 181 // Note that the runtime shall not examine the args here, otherwise we would have to 182 // move them in registers and stack to account for the difference between managed and 183 // native ABIs. Do not update CFI while we hold the frame address in RAX and the values 184 // in registers are unchanged. 185 movq %r15, 192(%rax) 186 movq %r14, 184(%rax) 187 movq %r13, 176(%rax) 188 movq %r12, 168(%rax) 189 movq %r9, 160(%rax) 190 movq %r8, 152(%rax) 191 movq %rsi, 144(%rax) 192 movq %rbp, 136(%rax) 193 movq %rbx, 128(%rax) 194 movq %rdx, 120(%rax) 195 movq %rcx, 112(%rax) 196 movq %xmm0, 16(%rax) 197 movq %xmm1, 24(%rax) 198 movq %xmm2, 32(%rax) 199 movq %xmm3, 40(%rax) 200 movq %xmm4, 48(%rax) 201 movq %xmm5, 56(%rax) 202 movq %xmm6, 64(%rax) 203 movq %xmm7, 72(%rax) 204 // Skip managed ABI callee-saves xmm12-xmm15. 205 206 // Move the managed frame address to native callee-save register RBP and update CFI. 207 movq %rax, %rbp 208 CFI_EXPRESSION_BREG CFI_REG(r15), CFI_REG(rbp), 192 209 CFI_EXPRESSION_BREG CFI_REG(r14), CFI_REG(rbp), 184 210 CFI_EXPRESSION_BREG CFI_REG(r13), CFI_REG(rbp), 176 211 CFI_EXPRESSION_BREG CFI_REG(r12), CFI_REG(rbp), 168 212 // Skip args r9, r8, rsi. 213 CFI_EXPRESSION_BREG CFI_REG(rbp), CFI_REG(rbp), 136 214 CFI_EXPRESSION_BREG CFI_REG(rbx), CFI_REG(rbp), 128 215 // Skip args rdx, rcx. 216 // Skip args xmm0-xmm7. 217 218 leaq 1(%rbp), %rax // Prepare managed SP tagged for a GenericJNI frame. 219 testl LITERAL(ACCESS_FLAGS_METHOD_IS_NATIVE), ART_METHOD_ACCESS_FLAGS_OFFSET(%r10) 220 jnz .Lcritical_skip_prepare_runtime_method 221 222 // Save the return PC for managed stack walk. 223 // (When coming from a compiled stub, the correct return PC is already there.) 224 movq %r11, FRAME_SIZE_SAVE_REFS_AND_ARGS - __SIZEOF_POINTER__(%rbp) 225 226 // Replace the target method with the SaveRefsAndArgs runtime method. 227 LOAD_RUNTIME_INSTANCE r10 228 movq RUNTIME_SAVE_REFS_AND_ARGS_METHOD_OFFSET(%r10), %r10 229 230 movq %rbp, %rax // Prepare untagged managed SP for the runtime method. 231 232.Lcritical_skip_prepare_runtime_method: 233 // Store the method on the bottom of the managed frame. 234 movq %r10, (%rbp) 235 236 // Place (maybe tagged) managed SP in Thread::Current()->top_quick_frame. 237 movq %rax, %gs:THREAD_TOP_QUICK_FRAME_OFFSET 238 239 // Save our return PC in the padding. 240 movq %r11, __SIZEOF_POINTER__(%rbp) 241 CFI_EXPRESSION_BREG CFI_REG(rip), CFI_REG(rbp), __SIZEOF_POINTER__ 242 243 // Preserve the native arg register RDI in callee-save register RBX which was saved above. 244 movq %rdi, %rbx 245 246 // Call artFindNativeMethodRunnable() 247 movq %gs:THREAD_SELF_OFFSET, %rdi // pass Thread::Current() 248 call SYMBOL(artFindNativeMethodRunnable) // (Thread*) 249 250 // Check for exception. 251 test %rax, %rax 252 CFI_REMEMBER_STATE 253 jz .Lcritical_deliver_exception 254 255 // Restore the native arg register RDI. 256 movq %rbx, %rdi 257 258 // Remember our return PC in R11. 259 movq __SIZEOF_POINTER__(%rbp), %r11 260 CFI_REGISTER(%rip, %r11) 261 262 // Remember the frame base address in r10 but do not redefine CFI. 263 movq %rbp, %r10 264 265 // Restore the frame. We shall not need the method anymore. 266 movq 16(%rbp), %xmm0 267 movq 24(%rbp), %xmm1 268 movq 32(%rbp), %xmm2 269 movq 40(%rbp), %xmm3 270 movq 48(%rbp), %xmm4 271 movq 56(%rbp), %xmm5 272 movq 64(%rbp), %xmm6 273 movq 72(%rbp), %xmm7 274 // Skip managed callee-saves xmm12-xmm15. 275 movq 112(%rbp), %rcx 276 movq 120(%rbp), %rdx 277 RESTORE_REG_BASE rbp, rbx, 128 278 // Delay restoring RBP as it's the managed frame base. 279 movq 144(%rbp), %rsi 280 movq 152(%rbp), %r8 281 movq 160(%rbp), %r9 282 RESTORE_REG_BASE rbp, r12, 168 283 RESTORE_REG_BASE rbp, r13, 176 284 RESTORE_REG_BASE rbp, r14, 184 285 RESTORE_REG_BASE rbp, r15, 192 286 // Restore RBP last. 287 RESTORE_REG_BASE rbp, rbp, 136 288 289 cmp %r10, %rsp 290 je .Lcritical_skip_copy_args_back 291 292 // Save RDI, RSI, RCX so that we can use them for moving stack args. 293 PUSH_ARG rdi 294 PUSH_ARG rsi 295 PUSH_ARG rcx 296 297 // Calculate the number of QWORDs to move. 298 leaq -3 * __SIZEOF_POINTER__(%r10), %rcx 299 subq %rsp, %rcx 300 shrq LITERAL(3), %rcx 301 302 // Move the stack args. 303 leaq -__SIZEOF_POINTER__(%r10), %rsi 304 leaq FRAME_SIZE_SAVE_REFS_AND_ARGS - __SIZEOF_POINTER__(%r10), %rdi 305 std 306 rep movsq 307 cld 308 309 // Restore RDI, RSI, RCX. 310 POP_ARG rcx 311 POP_ARG rsi 312 POP_ARG rdi 313 314.Lcritical_skip_copy_args_back: 315 // Remove the frame reservation. 316 DECREASE_FRAME FRAME_SIZE_SAVE_REFS_AND_ARGS - __SIZEOF_POINTER__ 317 318 // Store our return PC. 319 movq %r11, (%rsp) 320 CFI_REL_OFFSET(%rip, 0) 321 322 // Do the tail call. 323 jmp *%rax 324 325.Lcritical_deliver_exception: 326 CFI_RESTORE_STATE_AND_DEF_CFA %rbp, FRAME_SIZE_SAVE_REFS_AND_ARGS 327 DELIVER_PENDING_EXCEPTION_FRAME_READY 328END_FUNCTION art_jni_dlsym_lookup_critical_stub 329 330 /* 331 * Read barrier for the method's declaring class needed by JNI stub for static methods. 332 * (We're using a pointer to the declaring class in `ArtMethod` as `jclass`.) 333 */ 334JNI_SAVE_MANAGED_ARGS_TRAMPOLINE art_jni_read_barrier, artJniReadBarrier, none 335 336 /* 337 * Trampoline to `artJniMethodStart()` that preserves all managed arguments. 338 */ 339JNI_SAVE_MANAGED_ARGS_TRAMPOLINE art_jni_method_start, artJniMethodStart, gs:THREAD_SELF_OFFSET 340 341 /* 342 * Trampoline to `artJniMethodEntryHook` that preserves all managed arguments. 343 */ 344JNI_SAVE_MANAGED_ARGS_TRAMPOLINE \ 345 art_jni_method_entry_hook, artJniMethodEntryHook, gs:THREAD_SELF_OFFSET 346 347 /* 348 * Trampoline to `artJniMonitoredMethodStart()` that preserves all managed arguments. 349 */ 350JNI_SAVE_MANAGED_ARGS_TRAMPOLINE \ 351 art_jni_monitored_method_start, artJniMonitoredMethodStart, gs:THREAD_SELF_OFFSET 352 353 /* 354 * Trampoline to `artJniMethodEnd()` that preserves all return registers. 355 */ 356JNI_SAVE_RETURN_VALUE_TRAMPOLINE art_jni_method_end, artJniMethodEnd, gs:THREAD_SELF_OFFSET, none 357 358 /* 359 * Trampoline to `artJniMonitoredMethodEnd()` that preserves all return registers. 360 */ 361JNI_SAVE_RETURN_VALUE_TRAMPOLINE \ 362 art_jni_monitored_method_end, artJniMonitoredMethodEnd, gs:THREAD_SELF_OFFSET, none 363 364 /* 365 * Entry from JNI stub that tries to lock the object in a fast path and 366 * calls `artLockObjectFromCode()` (the same as for managed code) for the 367 * difficult cases, may block for GC. 368 * Custom calling convention: 369 * RBX holds the non-null object to lock. 370 * Callee-save registers have been saved and can be used as temporaries (except RBX). 371 * All argument registers need to be preserved. 372 */ 373DEFINE_FUNCTION art_jni_lock_object 374 LOCK_OBJECT_FAST_PATH rbx, ebp, art_jni_lock_object_no_inline 375END_FUNCTION art_jni_lock_object 376 377 /* 378 * Entry from JNI stub that calls `artLockObjectFromCode()` 379 * (the same as for managed code), may block for GC. 380 * Custom calling convention: 381 * RBX holds the non-null object to lock. 382 * Callee-save registers have been saved and can be used as temporaries (except RBX). 383 * All argument registers need to be preserved. 384 */ 385DEFINE_FUNCTION art_jni_lock_object_no_inline 386 // This is also the slow path for art_jni_lock_object. 387 // Save register args RDI, RSI, RDX, RCX, R8, R9, mmx0-mmx7 and align stack. 388 SAVE_MANAGED_ARGS_INCREASE_FRAME 389 // Call `artLockObjectFromCode()` 390 movq %rbx, %rdi // Pass the object to lock. 391 movq %gs:THREAD_SELF_OFFSET, %rsi // Pass Thread::Current(). 392 call SYMBOL(artLockObjectFromCode) // (object, Thread*) 393 // Check result. 394 testl %eax, %eax 395 jnz 1f 396 // Restore register args RDI, RSI, RDX, RCX, R8, R9, mmx0-mmx7 and return. 397 RESTORE_MANAGED_ARGS_DECREASE_FRAME 398 ret 399 .cfi_adjust_cfa_offset MANAGED_ARGS_SAVE_SIZE 4001: 401 // All args are irrelevant when throwing an exception. Remove the spill area except for new 402 // padding to align stack. 403 DECREASE_FRAME MANAGED_ARGS_SAVE_SIZE - /*new padding*/ 8 404 // Rely on the JNI transition frame constructed in the JNI stub. 405 movq %gs:THREAD_SELF_OFFSET, %rdi // Pass Thread::Current(). 406 call SYMBOL(artDeliverPendingExceptionFromCode) // (Thread*) 407 movq %rax, %rdi // pass Context* 408 call SYMBOL(art_quick_do_long_jump) 409 UNREACHABLE 410END_FUNCTION art_jni_lock_object_no_inline 411 412 /* 413 * Entry from JNI stub that tries to unlock the object in a fast path and calls 414 * `artJniUnlockObject()` for the difficult cases. Note that failure to unlock 415 * is fatal, so we do not need to check for exceptions in the slow path. 416 * Custom calling convention: 417 * RBX holds the non-null object to unlock. 418 * Callee-save registers have been saved and can be used as temporaries (except RBX). 419 * Return registers RAX and mmx0 need to be preserved. 420 */ 421DEFINE_FUNCTION art_jni_unlock_object 422 movq %rax, %r12 // Preserve RAX in a different register. 423 UNLOCK_OBJECT_FAST_PATH rbx, ebp, /*saved_rax*/ r12, .Lunlock_object_jni_slow 424 425 .Lunlock_object_jni_slow: 426 movq %r12, %rax // Restore RAX. 427 jmp SYMBOL(art_jni_unlock_object_no_inline) 428END_FUNCTION art_jni_unlock_object 429 430 /* 431 * Entry from JNI stub that calls `artJniUnlockObject()`. Note that failure to 432 * unlock is fatal, so we do not need to check for exceptions. 433 * Custom calling convention: 434 * RBX holds the non-null object to unlock. 435 * Callee-save registers have been saved and can be used as temporaries (except RBX). 436 * Return registers RAX and mmx0 need to be preserved. 437 */ 438 // This is also the slow path for art_jni_unlock_object. 439JNI_SAVE_RETURN_VALUE_TRAMPOLINE \ 440 art_jni_unlock_object_no_inline, artJniUnlockObject, rbx, gs:THREAD_SELF_OFFSET 441