1 /* 2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "asm/assembler.hpp" 27 #include "assembler_x86.inline.hpp" 28 #include "interpreter/interpreter.hpp" 29 #include "nativeInst_x86.hpp" 30 #include "oops/instanceOop.hpp" 31 #include "oops/methodOop.hpp" 32 #include "oops/objArrayKlass.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "prims/methodHandles.hpp" 35 #include "runtime/frame.inline.hpp" 36 #include "runtime/handles.inline.hpp" 37 #include "runtime/sharedRuntime.hpp" 38 #include "runtime/stubCodeGenerator.hpp" 39 #include "runtime/stubRoutines.hpp" 40 #include "utilities/top.hpp" 41 #ifdef TARGET_OS_FAMILY_linux 42 # include "thread_linux.inline.hpp" 43 #endif 44 #ifdef TARGET_OS_FAMILY_solaris 45 # include "thread_solaris.inline.hpp" 46 #endif 47 #ifdef TARGET_OS_FAMILY_windows 48 # include "thread_windows.inline.hpp" 49 #endif 50 #ifdef TARGET_OS_FAMILY_bsd 51 # include "thread_bsd.inline.hpp" 52 #endif 53 #ifdef COMPILER2 54 #include "opto/runtime.hpp" 55 #endif 56 57 // Declaration and definition of StubGenerator (no .hpp file). 58 // For a more detailed description of the stub routine structure 59 // see the comment in stubRoutines.hpp 60 61 #define __ _masm-> 62 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8) 63 #define a__ ((Assembler*)_masm)-> 64 65 #ifdef PRODUCT 66 #define BLOCK_COMMENT(str) /* nothing */ 67 #else 68 #define BLOCK_COMMENT(str) __ block_comment(str) 69 #endif 70 71 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":") 72 const int MXCSR_MASK = 0xFFC0; // Mask out any pending exceptions 73 74 // Stub Code definitions 75 76 static address handle_unsafe_access() { 77 JavaThread* thread = JavaThread::current(); 78 address pc = thread->saved_exception_pc(); 79 // pc is the instruction which we must emulate 80 // doing a no-op is fine: return garbage from the load 81 // therefore, compute npc 82 address npc = Assembler::locate_next_instruction(pc); 83 84 // request an async exception 85 thread->set_pending_unsafe_access_error(); 86 87 // return address of next instruction to execute 88 return npc; 89 } 90 91 class StubGenerator: public StubCodeGenerator { 92 private: 93 94 #ifdef PRODUCT 95 #define inc_counter_np(counter) (0) 96 #else 97 void inc_counter_np_(int& counter) { 98 // This can destroy rscratch1 if counter is far from the code cache 99 __ incrementl(ExternalAddress((address)&counter)); 100 } 101 #define inc_counter_np(counter) \ 102 BLOCK_COMMENT("inc_counter " #counter); \ 103 inc_counter_np_(counter); 104 #endif 105 106 // Call stubs are used to call Java from C 107 // 108 // Linux Arguments: 109 // c_rarg0: call wrapper address address 110 // c_rarg1: result address 111 // c_rarg2: result type BasicType 112 // c_rarg3: method methodOop 113 // c_rarg4: (interpreter) entry point address 114 // c_rarg5: parameters intptr_t* 115 // 16(rbp): parameter size (in words) int 116 // 24(rbp): thread Thread* 117 // 118 // [ return_from_Java ] <--- rsp 119 // [ argument word n ] 120 // ... 121 // -12 [ argument word 1 ] 122 // -11 [ saved r15 ] <--- rsp_after_call 123 // -10 [ saved r14 ] 124 // -9 [ saved r13 ] 125 // -8 [ saved r12 ] 126 // -7 [ saved rbx ] 127 // -6 [ call wrapper ] 128 // -5 [ result ] 129 // -4 [ result type ] 130 // -3 [ method ] 131 // -2 [ entry point ] 132 // -1 [ parameters ] 133 // 0 [ saved rbp ] <--- rbp 134 // 1 [ return address ] 135 // 2 [ parameter size ] 136 // 3 [ thread ] 137 // 138 // Windows Arguments: 139 // c_rarg0: call wrapper address address 140 // c_rarg1: result address 141 // c_rarg2: result type BasicType 142 // c_rarg3: method methodOop 143 // 48(rbp): (interpreter) entry point address 144 // 56(rbp): parameters intptr_t* 145 // 64(rbp): parameter size (in words) int 146 // 72(rbp): thread Thread* 147 // 148 // [ return_from_Java ] <--- rsp 149 // [ argument word n ] 150 // ... 151 // -28 [ argument word 1 ] 152 // -27 [ saved xmm15 ] <--- rsp_after_call 153 // [ saved xmm7-xmm14 ] 154 // -9 [ saved xmm6 ] (each xmm register takes 2 slots) 155 // -7 [ saved r15 ] 156 // -6 [ saved r14 ] 157 // -5 [ saved r13 ] 158 // -4 [ saved r12 ] 159 // -3 [ saved rdi ] 160 // -2 [ saved rsi ] 161 // -1 [ saved rbx ] 162 // 0 [ saved rbp ] <--- rbp 163 // 1 [ return address ] 164 // 2 [ call wrapper ] 165 // 3 [ result ] 166 // 4 [ result type ] 167 // 5 [ method ] 168 // 6 [ entry point ] 169 // 7 [ parameters ] 170 // 8 [ parameter size ] 171 // 9 [ thread ] 172 // 173 // Windows reserves the callers stack space for arguments 1-4. 174 // We spill c_rarg0-c_rarg3 to this space. 175 176 // Call stub stack layout word offsets from rbp 177 enum call_stub_layout { 178 #ifdef _WIN64 179 xmm_save_first = 6, // save from xmm6 180 xmm_save_last = 15, // to xmm15 181 xmm_save_base = -9, 182 rsp_after_call_off = xmm_save_base - 2 * (xmm_save_last - xmm_save_first), // -27 183 r15_off = -7, 184 r14_off = -6, 185 r13_off = -5, 186 r12_off = -4, 187 rdi_off = -3, 188 rsi_off = -2, 189 rbx_off = -1, 190 rbp_off = 0, 191 retaddr_off = 1, 192 call_wrapper_off = 2, 193 result_off = 3, 194 result_type_off = 4, 195 method_off = 5, 196 entry_point_off = 6, 197 parameters_off = 7, 198 parameter_size_off = 8, 199 thread_off = 9 200 #else 201 rsp_after_call_off = -12, 202 mxcsr_off = rsp_after_call_off, 203 r15_off = -11, 204 r14_off = -10, 205 r13_off = -9, 206 r12_off = -8, 207 rbx_off = -7, 208 call_wrapper_off = -6, 209 result_off = -5, 210 result_type_off = -4, 211 method_off = -3, 212 entry_point_off = -2, 213 parameters_off = -1, 214 rbp_off = 0, 215 retaddr_off = 1, 216 parameter_size_off = 2, 217 thread_off = 3 218 #endif 219 }; 220 221 #ifdef _WIN64 222 Address xmm_save(int reg) { 223 assert(reg >= xmm_save_first && reg <= xmm_save_last, "XMM register number out of range"); 224 return Address(rbp, (xmm_save_base - (reg - xmm_save_first) * 2) * wordSize); 225 } 226 #endif 227 228 address generate_call_stub(address& return_address) { 229 assert((int)frame::entry_frame_after_call_words == -(int)rsp_after_call_off + 1 && 230 (int)frame::entry_frame_call_wrapper_offset == (int)call_wrapper_off, 231 "adjust this code"); 232 StubCodeMark mark(this, "StubRoutines", "call_stub"); 233 address start = __ pc(); 234 235 // same as in generate_catch_exception()! 236 const Address rsp_after_call(rbp, rsp_after_call_off * wordSize); 237 238 const Address call_wrapper (rbp, call_wrapper_off * wordSize); 239 const Address result (rbp, result_off * wordSize); 240 const Address result_type (rbp, result_type_off * wordSize); 241 const Address method (rbp, method_off * wordSize); 242 const Address entry_point (rbp, entry_point_off * wordSize); 243 const Address parameters (rbp, parameters_off * wordSize); 244 const Address parameter_size(rbp, parameter_size_off * wordSize); 245 246 // same as in generate_catch_exception()! 247 const Address thread (rbp, thread_off * wordSize); 248 249 const Address r15_save(rbp, r15_off * wordSize); 250 const Address r14_save(rbp, r14_off * wordSize); 251 const Address r13_save(rbp, r13_off * wordSize); 252 const Address r12_save(rbp, r12_off * wordSize); 253 const Address rbx_save(rbp, rbx_off * wordSize); 254 255 // stub code 256 __ enter(); 257 __ subptr(rsp, -rsp_after_call_off * wordSize); 258 259 // save register parameters 260 #ifndef _WIN64 261 __ movptr(parameters, c_rarg5); // parameters 262 __ movptr(entry_point, c_rarg4); // entry_point 263 #endif 264 265 __ movptr(method, c_rarg3); // method 266 __ movl(result_type, c_rarg2); // result type 267 __ movptr(result, c_rarg1); // result 268 __ movptr(call_wrapper, c_rarg0); // call wrapper 269 270 // save regs belonging to calling function 271 __ movptr(rbx_save, rbx); 272 __ movptr(r12_save, r12); 273 __ movptr(r13_save, r13); 274 __ movptr(r14_save, r14); 275 __ movptr(r15_save, r15); 276 #ifdef _WIN64 277 for (int i = 6; i <= 15; i++) { 278 __ movdqu(xmm_save(i), as_XMMRegister(i)); 279 } 280 281 const Address rdi_save(rbp, rdi_off * wordSize); 282 const Address rsi_save(rbp, rsi_off * wordSize); 283 284 __ movptr(rsi_save, rsi); 285 __ movptr(rdi_save, rdi); 286 #else 287 const Address mxcsr_save(rbp, mxcsr_off * wordSize); 288 { 289 Label skip_ldmx; 290 __ stmxcsr(mxcsr_save); 291 __ movl(rax, mxcsr_save); 292 __ andl(rax, MXCSR_MASK); // Only check control and mask bits 293 ExternalAddress mxcsr_std(StubRoutines::x86::mxcsr_std()); 294 __ cmp32(rax, mxcsr_std); 295 __ jcc(Assembler::equal, skip_ldmx); 296 __ ldmxcsr(mxcsr_std); 297 __ bind(skip_ldmx); 298 } 299 #endif 300 301 // Load up thread register 302 __ movptr(r15_thread, thread); 303 __ reinit_heapbase(); 304 305 #ifdef ASSERT 306 // make sure we have no pending exceptions 307 { 308 Label L; 309 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD); 310 __ jcc(Assembler::equal, L); 311 __ stop("StubRoutines::call_stub: entered with pending exception"); 312 __ bind(L); 313 } 314 #endif 315 316 // pass parameters if any 317 BLOCK_COMMENT("pass parameters if any"); 318 Label parameters_done; 319 __ movl(c_rarg3, parameter_size); 320 __ testl(c_rarg3, c_rarg3); 321 __ jcc(Assembler::zero, parameters_done); 322 323 Label loop; 324 __ movptr(c_rarg2, parameters); // parameter pointer 325 __ movl(c_rarg1, c_rarg3); // parameter counter is in c_rarg1 326 __ BIND(loop); 327 __ movptr(rax, Address(c_rarg2, 0));// get parameter 328 __ addptr(c_rarg2, wordSize); // advance to next parameter 329 __ decrementl(c_rarg1); // decrement counter 330 __ push(rax); // pass parameter 331 __ jcc(Assembler::notZero, loop); 332 333 // call Java function 334 __ BIND(parameters_done); 335 __ movptr(rbx, method); // get methodOop 336 __ movptr(c_rarg1, entry_point); // get entry_point 337 __ mov(r13, rsp); // set sender sp 338 BLOCK_COMMENT("call Java function"); 339 __ call(c_rarg1); 340 341 BLOCK_COMMENT("call_stub_return_address:"); 342 return_address = __ pc(); 343 344 // store result depending on type (everything that is not 345 // T_OBJECT, T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT) 346 __ movptr(c_rarg0, result); 347 Label is_long, is_float, is_double, exit; 348 __ movl(c_rarg1, result_type); 349 __ cmpl(c_rarg1, T_OBJECT); 350 __ jcc(Assembler::equal, is_long); 351 __ cmpl(c_rarg1, T_LONG); 352 __ jcc(Assembler::equal, is_long); 353 __ cmpl(c_rarg1, T_FLOAT); 354 __ jcc(Assembler::equal, is_float); 355 __ cmpl(c_rarg1, T_DOUBLE); 356 __ jcc(Assembler::equal, is_double); 357 358 // handle T_INT case 359 __ movl(Address(c_rarg0, 0), rax); 360 361 __ BIND(exit); 362 363 // pop parameters 364 __ lea(rsp, rsp_after_call); 365 366 #ifdef ASSERT 367 // verify that threads correspond 368 { 369 Label L, S; 370 __ cmpptr(r15_thread, thread); 371 __ jcc(Assembler::notEqual, S); 372 __ get_thread(rbx); 373 __ cmpptr(r15_thread, rbx); 374 __ jcc(Assembler::equal, L); 375 __ bind(S); 376 __ jcc(Assembler::equal, L); 377 __ stop("StubRoutines::call_stub: threads must correspond"); 378 __ bind(L); 379 } 380 #endif 381 382 // restore regs belonging to calling function 383 #ifdef _WIN64 384 for (int i = 15; i >= 6; i--) { 385 __ movdqu(as_XMMRegister(i), xmm_save(i)); 386 } 387 #endif 388 __ movptr(r15, r15_save); 389 __ movptr(r14, r14_save); 390 __ movptr(r13, r13_save); 391 __ movptr(r12, r12_save); 392 __ movptr(rbx, rbx_save); 393 394 #ifdef _WIN64 395 __ movptr(rdi, rdi_save); 396 __ movptr(rsi, rsi_save); 397 #else 398 __ ldmxcsr(mxcsr_save); 399 #endif 400 401 // restore rsp 402 __ addptr(rsp, -rsp_after_call_off * wordSize); 403 404 // return 405 __ pop(rbp); 406 __ ret(0); 407 408 // handle return types different from T_INT 409 __ BIND(is_long); 410 __ movq(Address(c_rarg0, 0), rax); 411 __ jmp(exit); 412 413 __ BIND(is_float); 414 __ movflt(Address(c_rarg0, 0), xmm0); 415 __ jmp(exit); 416 417 __ BIND(is_double); 418 __ movdbl(Address(c_rarg0, 0), xmm0); 419 __ jmp(exit); 420 421 return start; 422 } 423 424 // Return point for a Java call if there's an exception thrown in 425 // Java code. The exception is caught and transformed into a 426 // pending exception stored in JavaThread that can be tested from 427 // within the VM. 428 // 429 // Note: Usually the parameters are removed by the callee. In case 430 // of an exception crossing an activation frame boundary, that is 431 // not the case if the callee is compiled code => need to setup the 432 // rsp. 433 // 434 // rax: exception oop 435 436 address generate_catch_exception() { 437 StubCodeMark mark(this, "StubRoutines", "catch_exception"); 438 address start = __ pc(); 439 440 // same as in generate_call_stub(): 441 const Address rsp_after_call(rbp, rsp_after_call_off * wordSize); 442 const Address thread (rbp, thread_off * wordSize); 443 444 #ifdef ASSERT 445 // verify that threads correspond 446 { 447 Label L, S; 448 __ cmpptr(r15_thread, thread); 449 __ jcc(Assembler::notEqual, S); 450 __ get_thread(rbx); 451 __ cmpptr(r15_thread, rbx); 452 __ jcc(Assembler::equal, L); 453 __ bind(S); 454 __ stop("StubRoutines::catch_exception: threads must correspond"); 455 __ bind(L); 456 } 457 #endif 458 459 // set pending exception 460 __ verify_oop(rax); 461 462 __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax); 463 __ lea(rscratch1, ExternalAddress((address)__FILE__)); 464 __ movptr(Address(r15_thread, Thread::exception_file_offset()), rscratch1); 465 __ movl(Address(r15_thread, Thread::exception_line_offset()), (int) __LINE__); 466 467 // complete return to VM 468 assert(StubRoutines::_call_stub_return_address != NULL, 469 "_call_stub_return_address must have been generated before"); 470 __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address)); 471 472 return start; 473 } 474 475 // Continuation point for runtime calls returning with a pending 476 // exception. The pending exception check happened in the runtime 477 // or native call stub. The pending exception in Thread is 478 // converted into a Java-level exception. 479 // 480 // Contract with Java-level exception handlers: 481 // rax: exception 482 // rdx: throwing pc 483 // 484 // NOTE: At entry of this stub, exception-pc must be on stack !! 485 486 address generate_forward_exception() { 487 StubCodeMark mark(this, "StubRoutines", "forward exception"); 488 address start = __ pc(); 489 490 // Upon entry, the sp points to the return address returning into 491 // Java (interpreted or compiled) code; i.e., the return address 492 // becomes the throwing pc. 493 // 494 // Arguments pushed before the runtime call are still on the stack 495 // but the exception handler will reset the stack pointer -> 496 // ignore them. A potential result in registers can be ignored as 497 // well. 498 499 #ifdef ASSERT 500 // make sure this code is only executed if there is a pending exception 501 { 502 Label L; 503 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t) NULL); 504 __ jcc(Assembler::notEqual, L); 505 __ stop("StubRoutines::forward exception: no pending exception (1)"); 506 __ bind(L); 507 } 508 #endif 509 510 // compute exception handler into rbx 511 __ movptr(c_rarg0, Address(rsp, 0)); 512 BLOCK_COMMENT("call exception_handler_for_return_address"); 513 __ call_VM_leaf(CAST_FROM_FN_PTR(address, 514 SharedRuntime::exception_handler_for_return_address), 515 r15_thread, c_rarg0); 516 __ mov(rbx, rax); 517 518 // setup rax & rdx, remove return address & clear pending exception 519 __ pop(rdx); 520 __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset())); 521 __ movptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD); 522 523 #ifdef ASSERT 524 // make sure exception is set 525 { 526 Label L; 527 __ testptr(rax, rax); 528 __ jcc(Assembler::notEqual, L); 529 __ stop("StubRoutines::forward exception: no pending exception (2)"); 530 __ bind(L); 531 } 532 #endif 533 534 // continue at exception handler (return address removed) 535 // rax: exception 536 // rbx: exception handler 537 // rdx: throwing pc 538 __ verify_oop(rax); 539 __ jmp(rbx); 540 541 return start; 542 } 543 544 // Support for jint atomic::xchg(jint exchange_value, volatile jint* dest) 545 // 546 // Arguments : 547 // c_rarg0: exchange_value 548 // c_rarg0: dest 549 // 550 // Result: 551 // *dest <- ex, return (orig *dest) 552 address generate_atomic_xchg() { 553 StubCodeMark mark(this, "StubRoutines", "atomic_xchg"); 554 address start = __ pc(); 555 556 __ movl(rax, c_rarg0); // Copy to eax we need a return value anyhow 557 __ xchgl(rax, Address(c_rarg1, 0)); // automatic LOCK 558 __ ret(0); 559 560 return start; 561 } 562 563 // Support for intptr_t atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) 564 // 565 // Arguments : 566 // c_rarg0: exchange_value 567 // c_rarg1: dest 568 // 569 // Result: 570 // *dest <- ex, return (orig *dest) 571 address generate_atomic_xchg_ptr() { 572 StubCodeMark mark(this, "StubRoutines", "atomic_xchg_ptr"); 573 address start = __ pc(); 574 575 __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow 576 __ xchgptr(rax, Address(c_rarg1, 0)); // automatic LOCK 577 __ ret(0); 578 579 return start; 580 } 581 582 // Support for jint atomic::atomic_cmpxchg(jint exchange_value, volatile jint* dest, 583 // jint compare_value) 584 // 585 // Arguments : 586 // c_rarg0: exchange_value 587 // c_rarg1: dest 588 // c_rarg2: compare_value 589 // 590 // Result: 591 // if ( compare_value == *dest ) { 592 // *dest = exchange_value 593 // return compare_value; 594 // else 595 // return *dest; 596 address generate_atomic_cmpxchg() { 597 StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg"); 598 address start = __ pc(); 599 600 __ movl(rax, c_rarg2); 601 if ( os::is_MP() ) __ lock(); 602 __ cmpxchgl(c_rarg0, Address(c_rarg1, 0)); 603 __ ret(0); 604 605 return start; 606 } 607 608 // Support for jint atomic::atomic_cmpxchg_long(jlong exchange_value, 609 // volatile jlong* dest, 610 // jlong compare_value) 611 // Arguments : 612 // c_rarg0: exchange_value 613 // c_rarg1: dest 614 // c_rarg2: compare_value 615 // 616 // Result: 617 // if ( compare_value == *dest ) { 618 // *dest = exchange_value 619 // return compare_value; 620 // else 621 // return *dest; 622 address generate_atomic_cmpxchg_long() { 623 StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_long"); 624 address start = __ pc(); 625 626 __ movq(rax, c_rarg2); 627 if ( os::is_MP() ) __ lock(); 628 __ cmpxchgq(c_rarg0, Address(c_rarg1, 0)); 629 __ ret(0); 630 631 return start; 632 } 633 634 // Support for jint atomic::add(jint add_value, volatile jint* dest) 635 // 636 // Arguments : 637 // c_rarg0: add_value 638 // c_rarg1: dest 639 // 640 // Result: 641 // *dest += add_value 642 // return *dest; 643 address generate_atomic_add() { 644 StubCodeMark mark(this, "StubRoutines", "atomic_add"); 645 address start = __ pc(); 646 647 __ movl(rax, c_rarg0); 648 if ( os::is_MP() ) __ lock(); 649 __ xaddl(Address(c_rarg1, 0), c_rarg0); 650 __ addl(rax, c_rarg0); 651 __ ret(0); 652 653 return start; 654 } 655 656 // Support for intptr_t atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) 657 // 658 // Arguments : 659 // c_rarg0: add_value 660 // c_rarg1: dest 661 // 662 // Result: 663 // *dest += add_value 664 // return *dest; 665 address generate_atomic_add_ptr() { 666 StubCodeMark mark(this, "StubRoutines", "atomic_add_ptr"); 667 address start = __ pc(); 668 669 __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow 670 if ( os::is_MP() ) __ lock(); 671 __ xaddptr(Address(c_rarg1, 0), c_rarg0); 672 __ addptr(rax, c_rarg0); 673 __ ret(0); 674 675 return start; 676 } 677 678 // Support for intptr_t OrderAccess::fence() 679 // 680 // Arguments : 681 // 682 // Result: 683 address generate_orderaccess_fence() { 684 StubCodeMark mark(this, "StubRoutines", "orderaccess_fence"); 685 address start = __ pc(); 686 __ membar(Assembler::StoreLoad); 687 __ ret(0); 688 689 return start; 690 } 691 692 // Support for intptr_t get_previous_fp() 693 // 694 // This routine is used to find the previous frame pointer for the 695 // caller (current_frame_guess). This is used as part of debugging 696 // ps() is seemingly lost trying to find frames. 697 // This code assumes that caller current_frame_guess) has a frame. 698 address generate_get_previous_fp() { 699 StubCodeMark mark(this, "StubRoutines", "get_previous_fp"); 700 const Address old_fp(rbp, 0); 701 const Address older_fp(rax, 0); 702 address start = __ pc(); 703 704 __ enter(); 705 __ movptr(rax, old_fp); // callers fp 706 __ movptr(rax, older_fp); // the frame for ps() 707 __ pop(rbp); 708 __ ret(0); 709 710 return start; 711 } 712 713 // Support for intptr_t get_previous_sp() 714 // 715 // This routine is used to find the previous stack pointer for the 716 // caller. 717 address generate_get_previous_sp() { 718 StubCodeMark mark(this, "StubRoutines", "get_previous_sp"); 719 address start = __ pc(); 720 721 __ movptr(rax, rsp); 722 __ addptr(rax, 8); // return address is at the top of the stack. 723 __ ret(0); 724 725 return start; 726 } 727 728 //---------------------------------------------------------------------------------------------------- 729 // Support for void verify_mxcsr() 730 // 731 // This routine is used with -Xcheck:jni to verify that native 732 // JNI code does not return to Java code without restoring the 733 // MXCSR register to our expected state. 734 735 address generate_verify_mxcsr() { 736 StubCodeMark mark(this, "StubRoutines", "verify_mxcsr"); 737 address start = __ pc(); 738 739 const Address mxcsr_save(rsp, 0); 740 741 if (CheckJNICalls) { 742 Label ok_ret; 743 __ push(rax); 744 __ subptr(rsp, wordSize); // allocate a temp location 745 __ stmxcsr(mxcsr_save); 746 __ movl(rax, mxcsr_save); 747 __ andl(rax, MXCSR_MASK); // Only check control and mask bits 748 __ cmpl(rax, *(int *)(StubRoutines::x86::mxcsr_std())); 749 __ jcc(Assembler::equal, ok_ret); 750 751 __ warn("MXCSR changed by native JNI code, use -XX:+RestoreMXCSROnJNICall"); 752 753 __ ldmxcsr(ExternalAddress(StubRoutines::x86::mxcsr_std())); 754 755 __ bind(ok_ret); 756 __ addptr(rsp, wordSize); 757 __ pop(rax); 758 } 759 760 __ ret(0); 761 762 return start; 763 } 764 765 address generate_f2i_fixup() { 766 StubCodeMark mark(this, "StubRoutines", "f2i_fixup"); 767 Address inout(rsp, 5 * wordSize); // return address + 4 saves 768 769 address start = __ pc(); 770 771 Label L; 772 773 __ push(rax); 774 __ push(c_rarg3); 775 __ push(c_rarg2); 776 __ push(c_rarg1); 777 778 __ movl(rax, 0x7f800000); 779 __ xorl(c_rarg3, c_rarg3); 780 __ movl(c_rarg2, inout); 781 __ movl(c_rarg1, c_rarg2); 782 __ andl(c_rarg1, 0x7fffffff); 783 __ cmpl(rax, c_rarg1); // NaN? -> 0 784 __ jcc(Assembler::negative, L); 785 __ testl(c_rarg2, c_rarg2); // signed ? min_jint : max_jint 786 __ movl(c_rarg3, 0x80000000); 787 __ movl(rax, 0x7fffffff); 788 __ cmovl(Assembler::positive, c_rarg3, rax); 789 790 __ bind(L); 791 __ movptr(inout, c_rarg3); 792 793 __ pop(c_rarg1); 794 __ pop(c_rarg2); 795 __ pop(c_rarg3); 796 __ pop(rax); 797 798 __ ret(0); 799 800 return start; 801 } 802 803 address generate_f2l_fixup() { 804 StubCodeMark mark(this, "StubRoutines", "f2l_fixup"); 805 Address inout(rsp, 5 * wordSize); // return address + 4 saves 806 address start = __ pc(); 807 808 Label L; 809 810 __ push(rax); 811 __ push(c_rarg3); 812 __ push(c_rarg2); 813 __ push(c_rarg1); 814 815 __ movl(rax, 0x7f800000); 816 __ xorl(c_rarg3, c_rarg3); 817 __ movl(c_rarg2, inout); 818 __ movl(c_rarg1, c_rarg2); 819 __ andl(c_rarg1, 0x7fffffff); 820 __ cmpl(rax, c_rarg1); // NaN? -> 0 821 __ jcc(Assembler::negative, L); 822 __ testl(c_rarg2, c_rarg2); // signed ? min_jlong : max_jlong 823 __ mov64(c_rarg3, 0x8000000000000000); 824 __ mov64(rax, 0x7fffffffffffffff); 825 __ cmov(Assembler::positive, c_rarg3, rax); 826 827 __ bind(L); 828 __ movptr(inout, c_rarg3); 829 830 __ pop(c_rarg1); 831 __ pop(c_rarg2); 832 __ pop(c_rarg3); 833 __ pop(rax); 834 835 __ ret(0); 836 837 return start; 838 } 839 840 address generate_d2i_fixup() { 841 StubCodeMark mark(this, "StubRoutines", "d2i_fixup"); 842 Address inout(rsp, 6 * wordSize); // return address + 5 saves 843 844 address start = __ pc(); 845 846 Label L; 847 848 __ push(rax); 849 __ push(c_rarg3); 850 __ push(c_rarg2); 851 __ push(c_rarg1); 852 __ push(c_rarg0); 853 854 __ movl(rax, 0x7ff00000); 855 __ movq(c_rarg2, inout); 856 __ movl(c_rarg3, c_rarg2); 857 __ mov(c_rarg1, c_rarg2); 858 __ mov(c_rarg0, c_rarg2); 859 __ negl(c_rarg3); 860 __ shrptr(c_rarg1, 0x20); 861 __ orl(c_rarg3, c_rarg2); 862 __ andl(c_rarg1, 0x7fffffff); 863 __ xorl(c_rarg2, c_rarg2); 864 __ shrl(c_rarg3, 0x1f); 865 __ orl(c_rarg1, c_rarg3); 866 __ cmpl(rax, c_rarg1); 867 __ jcc(Assembler::negative, L); // NaN -> 0 868 __ testptr(c_rarg0, c_rarg0); // signed ? min_jint : max_jint 869 __ movl(c_rarg2, 0x80000000); 870 __ movl(rax, 0x7fffffff); 871 __ cmov(Assembler::positive, c_rarg2, rax); 872 873 __ bind(L); 874 __ movptr(inout, c_rarg2); 875 876 __ pop(c_rarg0); 877 __ pop(c_rarg1); 878 __ pop(c_rarg2); 879 __ pop(c_rarg3); 880 __ pop(rax); 881 882 __ ret(0); 883 884 return start; 885 } 886 887 address generate_d2l_fixup() { 888 StubCodeMark mark(this, "StubRoutines", "d2l_fixup"); 889 Address inout(rsp, 6 * wordSize); // return address + 5 saves 890 891 address start = __ pc(); 892 893 Label L; 894 895 __ push(rax); 896 __ push(c_rarg3); 897 __ push(c_rarg2); 898 __ push(c_rarg1); 899 __ push(c_rarg0); 900 901 __ movl(rax, 0x7ff00000); 902 __ movq(c_rarg2, inout); 903 __ movl(c_rarg3, c_rarg2); 904 __ mov(c_rarg1, c_rarg2); 905 __ mov(c_rarg0, c_rarg2); 906 __ negl(c_rarg3); 907 __ shrptr(c_rarg1, 0x20); 908 __ orl(c_rarg3, c_rarg2); 909 __ andl(c_rarg1, 0x7fffffff); 910 __ xorl(c_rarg2, c_rarg2); 911 __ shrl(c_rarg3, 0x1f); 912 __ orl(c_rarg1, c_rarg3); 913 __ cmpl(rax, c_rarg1); 914 __ jcc(Assembler::negative, L); // NaN -> 0 915 __ testq(c_rarg0, c_rarg0); // signed ? min_jlong : max_jlong 916 __ mov64(c_rarg2, 0x8000000000000000); 917 __ mov64(rax, 0x7fffffffffffffff); 918 __ cmovq(Assembler::positive, c_rarg2, rax); 919 920 __ bind(L); 921 __ movq(inout, c_rarg2); 922 923 __ pop(c_rarg0); 924 __ pop(c_rarg1); 925 __ pop(c_rarg2); 926 __ pop(c_rarg3); 927 __ pop(rax); 928 929 __ ret(0); 930 931 return start; 932 } 933 934 address generate_fp_mask(const char *stub_name, int64_t mask) { 935 __ align(CodeEntryAlignment); 936 StubCodeMark mark(this, "StubRoutines", stub_name); 937 address start = __ pc(); 938 939 __ emit_data64( mask, relocInfo::none ); 940 __ emit_data64( mask, relocInfo::none ); 941 942 return start; 943 } 944 945 // The following routine generates a subroutine to throw an 946 // asynchronous UnknownError when an unsafe access gets a fault that 947 // could not be reasonably prevented by the programmer. (Example: 948 // SIGBUS/OBJERR.) 949 address generate_handler_for_unsafe_access() { 950 StubCodeMark mark(this, "StubRoutines", "handler_for_unsafe_access"); 951 address start = __ pc(); 952 953 __ push(0); // hole for return address-to-be 954 __ pusha(); // push registers 955 Address next_pc(rsp, RegisterImpl::number_of_registers * BytesPerWord); 956 957 // FIXME: this probably needs alignment logic 958 959 __ subptr(rsp, frame::arg_reg_save_area_bytes); 960 BLOCK_COMMENT("call handle_unsafe_access"); 961 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, handle_unsafe_access))); 962 __ addptr(rsp, frame::arg_reg_save_area_bytes); 963 964 __ movptr(next_pc, rax); // stuff next address 965 __ popa(); 966 __ ret(0); // jump to next address 967 968 return start; 969 } 970 971 // Non-destructive plausibility checks for oops 972 // 973 // Arguments: 974 // all args on stack! 975 // 976 // Stack after saving c_rarg3: 977 // [tos + 0]: saved c_rarg3 978 // [tos + 1]: saved c_rarg2 979 // [tos + 2]: saved r12 (several TemplateTable methods use it) 980 // [tos + 3]: saved flags 981 // [tos + 4]: return address 982 // * [tos + 5]: error message (char*) 983 // * [tos + 6]: object to verify (oop) 984 // * [tos + 7]: saved rax - saved by caller and bashed 985 // * [tos + 8]: saved r10 (rscratch1) - saved by caller 986 // * = popped on exit 987 address generate_verify_oop() { 988 StubCodeMark mark(this, "StubRoutines", "verify_oop"); 989 address start = __ pc(); 990 991 Label exit, error; 992 993 __ pushf(); 994 __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr())); 995 996 __ push(r12); 997 998 // save c_rarg2 and c_rarg3 999 __ push(c_rarg2); 1000 __ push(c_rarg3); 1001 1002 enum { 1003 // After previous pushes. 1004 oop_to_verify = 6 * wordSize, 1005 saved_rax = 7 * wordSize, 1006 saved_r10 = 8 * wordSize, 1007 1008 // Before the call to MacroAssembler::debug(), see below. 1009 return_addr = 16 * wordSize, 1010 error_msg = 17 * wordSize 1011 }; 1012 1013 // get object 1014 __ movptr(rax, Address(rsp, oop_to_verify)); 1015 1016 // make sure object is 'reasonable' 1017 __ testptr(rax, rax); 1018 __ jcc(Assembler::zero, exit); // if obj is NULL it is OK 1019 // Check if the oop is in the right area of memory 1020 __ movptr(c_rarg2, rax); 1021 __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_mask()); 1022 __ andptr(c_rarg2, c_rarg3); 1023 __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_bits()); 1024 __ cmpptr(c_rarg2, c_rarg3); 1025 __ jcc(Assembler::notZero, error); 1026 1027 // set r12 to heapbase for load_klass() 1028 __ reinit_heapbase(); 1029 1030 // make sure klass is 'reasonable' 1031 __ load_klass(rax, rax); // get klass 1032 __ testptr(rax, rax); 1033 __ jcc(Assembler::zero, error); // if klass is NULL it is broken 1034 // Check if the klass is in the right area of memory 1035 __ mov(c_rarg2, rax); 1036 __ movptr(c_rarg3, (intptr_t) Universe::verify_klass_mask()); 1037 __ andptr(c_rarg2, c_rarg3); 1038 __ movptr(c_rarg3, (intptr_t) Universe::verify_klass_bits()); 1039 __ cmpptr(c_rarg2, c_rarg3); 1040 __ jcc(Assembler::notZero, error); 1041 1042 // make sure klass' klass is 'reasonable' 1043 __ load_klass(rax, rax); 1044 __ testptr(rax, rax); 1045 __ jcc(Assembler::zero, error); // if klass' klass is NULL it is broken 1046 // Check if the klass' klass is in the right area of memory 1047 __ movptr(c_rarg3, (intptr_t) Universe::verify_klass_mask()); 1048 __ andptr(rax, c_rarg3); 1049 __ movptr(c_rarg3, (intptr_t) Universe::verify_klass_bits()); 1050 __ cmpptr(rax, c_rarg3); 1051 __ jcc(Assembler::notZero, error); 1052 1053 // return if everything seems ok 1054 __ bind(exit); 1055 __ movptr(rax, Address(rsp, saved_rax)); // get saved rax back 1056 __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back 1057 __ pop(c_rarg3); // restore c_rarg3 1058 __ pop(c_rarg2); // restore c_rarg2 1059 __ pop(r12); // restore r12 1060 __ popf(); // restore flags 1061 __ ret(4 * wordSize); // pop caller saved stuff 1062 1063 // handle errors 1064 __ bind(error); 1065 __ movptr(rax, Address(rsp, saved_rax)); // get saved rax back 1066 __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back 1067 __ pop(c_rarg3); // get saved c_rarg3 back 1068 __ pop(c_rarg2); // get saved c_rarg2 back 1069 __ pop(r12); // get saved r12 back 1070 __ popf(); // get saved flags off stack -- 1071 // will be ignored 1072 1073 __ pusha(); // push registers 1074 // (rip is already 1075 // already pushed) 1076 // debug(char* msg, int64_t pc, int64_t regs[]) 1077 // We've popped the registers we'd saved (c_rarg3, c_rarg2 and flags), and 1078 // pushed all the registers, so now the stack looks like: 1079 // [tos + 0] 16 saved registers 1080 // [tos + 16] return address 1081 // * [tos + 17] error message (char*) 1082 // * [tos + 18] object to verify (oop) 1083 // * [tos + 19] saved rax - saved by caller and bashed 1084 // * [tos + 20] saved r10 (rscratch1) - saved by caller 1085 // * = popped on exit 1086 1087 __ movptr(c_rarg0, Address(rsp, error_msg)); // pass address of error message 1088 __ movptr(c_rarg1, Address(rsp, return_addr)); // pass return address 1089 __ movq(c_rarg2, rsp); // pass address of regs on stack 1090 __ mov(r12, rsp); // remember rsp 1091 __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows 1092 __ andptr(rsp, -16); // align stack as required by ABI 1093 BLOCK_COMMENT("call MacroAssembler::debug"); 1094 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug64))); 1095 __ mov(rsp, r12); // restore rsp 1096 __ popa(); // pop registers (includes r12) 1097 __ ret(4 * wordSize); // pop caller saved stuff 1098 1099 return start; 1100 } 1101 1102 // 1103 // Verify that a register contains clean 32-bits positive value 1104 // (high 32-bits are 0) so it could be used in 64-bits shifts. 1105 // 1106 // Input: 1107 // Rint - 32-bits value 1108 // Rtmp - scratch 1109 // 1110 void assert_clean_int(Register Rint, Register Rtmp) { 1111 #ifdef ASSERT 1112 Label L; 1113 assert_different_registers(Rtmp, Rint); 1114 __ movslq(Rtmp, Rint); 1115 __ cmpq(Rtmp, Rint); 1116 __ jcc(Assembler::equal, L); 1117 __ stop("high 32-bits of int value are not 0"); 1118 __ bind(L); 1119 #endif 1120 } 1121 1122 // Generate overlap test for array copy stubs 1123 // 1124 // Input: 1125 // c_rarg0 - from 1126 // c_rarg1 - to 1127 // c_rarg2 - element count 1128 // 1129 // Output: 1130 // rax - &from[element count - 1] 1131 // 1132 void array_overlap_test(address no_overlap_target, Address::ScaleFactor sf) { 1133 assert(no_overlap_target != NULL, "must be generated"); 1134 array_overlap_test(no_overlap_target, NULL, sf); 1135 } 1136 void array_overlap_test(Label& L_no_overlap, Address::ScaleFactor sf) { 1137 array_overlap_test(NULL, &L_no_overlap, sf); 1138 } 1139 void array_overlap_test(address no_overlap_target, Label* NOLp, Address::ScaleFactor sf) { 1140 const Register from = c_rarg0; 1141 const Register to = c_rarg1; 1142 const Register count = c_rarg2; 1143 const Register end_from = rax; 1144 1145 __ cmpptr(to, from); 1146 __ lea(end_from, Address(from, count, sf, 0)); 1147 if (NOLp == NULL) { 1148 ExternalAddress no_overlap(no_overlap_target); 1149 __ jump_cc(Assembler::belowEqual, no_overlap); 1150 __ cmpptr(to, end_from); 1151 __ jump_cc(Assembler::aboveEqual, no_overlap); 1152 } else { 1153 __ jcc(Assembler::belowEqual, (*NOLp)); 1154 __ cmpptr(to, end_from); 1155 __ jcc(Assembler::aboveEqual, (*NOLp)); 1156 } 1157 } 1158 1159 // Shuffle first three arg regs on Windows into Linux/Solaris locations. 1160 // 1161 // Outputs: 1162 // rdi - rcx 1163 // rsi - rdx 1164 // rdx - r8 1165 // rcx - r9 1166 // 1167 // Registers r9 and r10 are used to save rdi and rsi on Windows, which latter 1168 // are non-volatile. r9 and r10 should not be used by the caller. 1169 // 1170 void setup_arg_regs(int nargs = 3) { 1171 const Register saved_rdi = r9; 1172 const Register saved_rsi = r10; 1173 assert(nargs == 3 || nargs == 4, "else fix"); 1174 #ifdef _WIN64 1175 assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9, 1176 "unexpected argument registers"); 1177 if (nargs >= 4) 1178 __ mov(rax, r9); // r9 is also saved_rdi 1179 __ movptr(saved_rdi, rdi); 1180 __ movptr(saved_rsi, rsi); 1181 __ mov(rdi, rcx); // c_rarg0 1182 __ mov(rsi, rdx); // c_rarg1 1183 __ mov(rdx, r8); // c_rarg2 1184 if (nargs >= 4) 1185 __ mov(rcx, rax); // c_rarg3 (via rax) 1186 #else 1187 assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx, 1188 "unexpected argument registers"); 1189 #endif 1190 } 1191 1192 void restore_arg_regs() { 1193 const Register saved_rdi = r9; 1194 const Register saved_rsi = r10; 1195 #ifdef _WIN64 1196 __ movptr(rdi, saved_rdi); 1197 __ movptr(rsi, saved_rsi); 1198 #endif 1199 } 1200 1201 // Generate code for an array write pre barrier 1202 // 1203 // addr - starting address 1204 // count - element count 1205 // tmp - scratch register 1206 // 1207 // Destroy no registers! 1208 // 1209 void gen_write_ref_array_pre_barrier(Register addr, Register count, bool dest_uninitialized) { 1210 BarrierSet* bs = Universe::heap()->barrier_set(); 1211 switch (bs->kind()) { 1212 case BarrierSet::G1SATBCT: 1213 case BarrierSet::G1SATBCTLogging: 1214 // With G1, don't generate the call if we statically know that the target in uninitialized 1215 if (!dest_uninitialized) { 1216 __ pusha(); // push registers 1217 if (count == c_rarg0) { 1218 if (addr == c_rarg1) { 1219 // exactly backwards!! 1220 __ xchgptr(c_rarg1, c_rarg0); 1221 } else { 1222 __ movptr(c_rarg1, count); 1223 __ movptr(c_rarg0, addr); 1224 } 1225 } else { 1226 __ movptr(c_rarg0, addr); 1227 __ movptr(c_rarg1, count); 1228 } 1229 __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre), 2); 1230 __ popa(); 1231 } 1232 break; 1233 case BarrierSet::CardTableModRef: 1234 case BarrierSet::CardTableExtension: 1235 case BarrierSet::ModRef: 1236 break; 1237 default: 1238 ShouldNotReachHere(); 1239 1240 } 1241 } 1242 1243 // 1244 // Generate code for an array write post barrier 1245 // 1246 // Input: 1247 // start - register containing starting address of destination array 1248 // count - elements count 1249 // scratch - scratch register 1250 // 1251 // The input registers are overwritten. 1252 // 1253 void gen_write_ref_array_post_barrier(Register start, Register count, Register scratch) { 1254 assert_different_registers(start, count, scratch); 1255 BarrierSet* bs = Universe::heap()->barrier_set(); 1256 switch (bs->kind()) { 1257 case BarrierSet::G1SATBCT: 1258 case BarrierSet::G1SATBCTLogging: 1259 { 1260 __ pusha(); // push registers (overkill) 1261 if (c_rarg0 == count) { // On win64 c_rarg0 == rcx 1262 assert_different_registers(c_rarg1, start); 1263 __ mov(c_rarg1, count); 1264 __ mov(c_rarg0, start); 1265 } else { 1266 assert_different_registers(c_rarg0, count); 1267 __ mov(c_rarg0, start); 1268 __ mov(c_rarg1, count); 1269 } 1270 __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post), 2); 1271 __ popa(); 1272 } 1273 break; 1274 case BarrierSet::CardTableModRef: 1275 case BarrierSet::CardTableExtension: 1276 { 1277 CardTableModRefBS* ct = (CardTableModRefBS*)bs; 1278 assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code"); 1279 1280 Label L_loop; 1281 const Register end = count; 1282 1283 __ leaq(end, Address(start, count, TIMES_OOP, 0)); // end == start+count*oop_size 1284 __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive 1285 __ shrptr(start, CardTableModRefBS::card_shift); 1286 __ shrptr(end, CardTableModRefBS::card_shift); 1287 __ subptr(end, start); // end --> cards count 1288 1289 int64_t disp = (int64_t) ct->byte_map_base; 1290 __ mov64(scratch, disp); 1291 __ addptr(start, scratch); 1292 __ BIND(L_loop); 1293 __ movb(Address(start, count, Address::times_1), 0); 1294 __ decrement(count); 1295 __ jcc(Assembler::greaterEqual, L_loop); 1296 } 1297 break; 1298 default: 1299 ShouldNotReachHere(); 1300 1301 } 1302 } 1303 1304 1305 // Copy big chunks forward 1306 // 1307 // Inputs: 1308 // end_from - source arrays end address 1309 // end_to - destination array end address 1310 // qword_count - 64-bits element count, negative 1311 // to - scratch 1312 // L_copy_bytes - entry label 1313 // L_copy_8_bytes - exit label 1314 // 1315 void copy_bytes_forward(Register end_from, Register end_to, 1316 Register qword_count, Register to, 1317 Label& L_copy_bytes, Label& L_copy_8_bytes) { 1318 DEBUG_ONLY(__ stop("enter at entry label, not here")); 1319 Label L_loop; 1320 __ align(OptoLoopAlignment); 1321 if (UseUnalignedLoadStores) { 1322 Label L_end; 1323 // Copy 64-bytes per iteration 1324 __ BIND(L_loop); 1325 if (UseAVX >= 2) { 1326 __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56)); 1327 __ vmovdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0); 1328 __ vmovdqu(xmm1, Address(end_from, qword_count, Address::times_8, -24)); 1329 __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm1); 1330 } else { 1331 __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56)); 1332 __ movdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0); 1333 __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, -40)); 1334 __ movdqu(Address(end_to, qword_count, Address::times_8, -40), xmm1); 1335 __ movdqu(xmm2, Address(end_from, qword_count, Address::times_8, -24)); 1336 __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm2); 1337 __ movdqu(xmm3, Address(end_from, qword_count, Address::times_8, - 8)); 1338 __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm3); 1339 } 1340 __ BIND(L_copy_bytes); 1341 __ addptr(qword_count, 8); 1342 __ jcc(Assembler::lessEqual, L_loop); 1343 __ subptr(qword_count, 4); // sub(8) and add(4) 1344 __ jccb(Assembler::greater, L_end); 1345 // Copy trailing 32 bytes 1346 if (UseAVX >= 2) { 1347 __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24)); 1348 __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0); 1349 } else { 1350 __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24)); 1351 __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0); 1352 __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, - 8)); 1353 __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm1); 1354 } 1355 __ addptr(qword_count, 4); 1356 __ BIND(L_end); 1357 if (UseAVX >= 2) { 1358 // clean upper bits of YMM registers 1359 __ vzeroupper(); 1360 } 1361 } else { 1362 // Copy 32-bytes per iteration 1363 __ BIND(L_loop); 1364 __ movq(to, Address(end_from, qword_count, Address::times_8, -24)); 1365 __ movq(Address(end_to, qword_count, Address::times_8, -24), to); 1366 __ movq(to, Address(end_from, qword_count, Address::times_8, -16)); 1367 __ movq(Address(end_to, qword_count, Address::times_8, -16), to); 1368 __ movq(to, Address(end_from, qword_count, Address::times_8, - 8)); 1369 __ movq(Address(end_to, qword_count, Address::times_8, - 8), to); 1370 __ movq(to, Address(end_from, qword_count, Address::times_8, - 0)); 1371 __ movq(Address(end_to, qword_count, Address::times_8, - 0), to); 1372 1373 __ BIND(L_copy_bytes); 1374 __ addptr(qword_count, 4); 1375 __ jcc(Assembler::lessEqual, L_loop); 1376 } 1377 __ subptr(qword_count, 4); 1378 __ jcc(Assembler::less, L_copy_8_bytes); // Copy trailing qwords 1379 } 1380 1381 // Copy big chunks backward 1382 // 1383 // Inputs: 1384 // from - source arrays address 1385 // dest - destination array address 1386 // qword_count - 64-bits element count 1387 // to - scratch 1388 // L_copy_bytes - entry label 1389 // L_copy_8_bytes - exit label 1390 // 1391 void copy_bytes_backward(Register from, Register dest, 1392 Register qword_count, Register to, 1393 Label& L_copy_bytes, Label& L_copy_8_bytes) { 1394 DEBUG_ONLY(__ stop("enter at entry label, not here")); 1395 Label L_loop; 1396 __ align(OptoLoopAlignment); 1397 if (UseUnalignedLoadStores) { 1398 Label L_end; 1399 // Copy 64-bytes per iteration 1400 __ BIND(L_loop); 1401 if (UseAVX >= 2) { 1402 __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 32)); 1403 __ vmovdqu(Address(dest, qword_count, Address::times_8, 32), xmm0); 1404 __ vmovdqu(xmm1, Address(from, qword_count, Address::times_8, 0)); 1405 __ vmovdqu(Address(dest, qword_count, Address::times_8, 0), xmm1); 1406 } else { 1407 __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 48)); 1408 __ movdqu(Address(dest, qword_count, Address::times_8, 48), xmm0); 1409 __ movdqu(xmm1, Address(from, qword_count, Address::times_8, 32)); 1410 __ movdqu(Address(dest, qword_count, Address::times_8, 32), xmm1); 1411 __ movdqu(xmm2, Address(from, qword_count, Address::times_8, 16)); 1412 __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm2); 1413 __ movdqu(xmm3, Address(from, qword_count, Address::times_8, 0)); 1414 __ movdqu(Address(dest, qword_count, Address::times_8, 0), xmm3); 1415 } 1416 __ BIND(L_copy_bytes); 1417 __ subptr(qword_count, 8); 1418 __ jcc(Assembler::greaterEqual, L_loop); 1419 1420 __ addptr(qword_count, 4); // add(8) and sub(4) 1421 __ jccb(Assembler::less, L_end); 1422 // Copy trailing 32 bytes 1423 if (UseAVX >= 2) { 1424 __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 0)); 1425 __ vmovdqu(Address(dest, qword_count, Address::times_8, 0), xmm0); 1426 } else { 1427 __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 16)); 1428 __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm0); 1429 __ movdqu(xmm1, Address(from, qword_count, Address::times_8, 0)); 1430 __ movdqu(Address(dest, qword_count, Address::times_8, 0), xmm1); 1431 } 1432 __ subptr(qword_count, 4); 1433 __ BIND(L_end); 1434 if (UseAVX >= 2) { 1435 // clean upper bits of YMM registers 1436 __ vzeroupper(); 1437 } 1438 } else { 1439 // Copy 32-bytes per iteration 1440 __ BIND(L_loop); 1441 __ movq(to, Address(from, qword_count, Address::times_8, 24)); 1442 __ movq(Address(dest, qword_count, Address::times_8, 24), to); 1443 __ movq(to, Address(from, qword_count, Address::times_8, 16)); 1444 __ movq(Address(dest, qword_count, Address::times_8, 16), to); 1445 __ movq(to, Address(from, qword_count, Address::times_8, 8)); 1446 __ movq(Address(dest, qword_count, Address::times_8, 8), to); 1447 __ movq(to, Address(from, qword_count, Address::times_8, 0)); 1448 __ movq(Address(dest, qword_count, Address::times_8, 0), to); 1449 1450 __ BIND(L_copy_bytes); 1451 __ subptr(qword_count, 4); 1452 __ jcc(Assembler::greaterEqual, L_loop); 1453 } 1454 __ addptr(qword_count, 4); 1455 __ jcc(Assembler::greater, L_copy_8_bytes); // Copy trailing qwords 1456 } 1457 1458 1459 // Arguments: 1460 // aligned - true => Input and output aligned on a HeapWord == 8-byte boundary 1461 // ignored 1462 // name - stub name string 1463 // 1464 // Inputs: 1465 // c_rarg0 - source array address 1466 // c_rarg1 - destination array address 1467 // c_rarg2 - element count, treated as ssize_t, can be zero 1468 // 1469 // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries, 1470 // we let the hardware handle it. The one to eight bytes within words, 1471 // dwords or qwords that span cache line boundaries will still be loaded 1472 // and stored atomically. 1473 // 1474 // Side Effects: 1475 // disjoint_byte_copy_entry is set to the no-overlap entry point 1476 // used by generate_conjoint_byte_copy(). 1477 // 1478 address generate_disjoint_byte_copy(bool aligned, address* entry, const char *name) { 1479 __ align(CodeEntryAlignment); 1480 StubCodeMark mark(this, "StubRoutines", name); 1481 address start = __ pc(); 1482 1483 Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes; 1484 Label L_copy_byte, L_exit; 1485 const Register from = rdi; // source array address 1486 const Register to = rsi; // destination array address 1487 const Register count = rdx; // elements count 1488 const Register byte_count = rcx; 1489 const Register qword_count = count; 1490 const Register end_from = from; // source array end address 1491 const Register end_to = to; // destination array end address 1492 // End pointers are inclusive, and if count is not zero they point 1493 // to the last unit copied: end_to[0] := end_from[0] 1494 1495 __ enter(); // required for proper stackwalking of RuntimeStub frame 1496 assert_clean_int(c_rarg2, rax); // Make sure 'count' is clean int. 1497 1498 if (entry != NULL) { 1499 *entry = __ pc(); 1500 // caller can pass a 64-bit byte count here (from Unsafe.copyMemory) 1501 BLOCK_COMMENT("Entry:"); 1502 } 1503 1504 setup_arg_regs(); // from => rdi, to => rsi, count => rdx 1505 // r9 and r10 may be used to save non-volatile registers 1506 1507 // 'from', 'to' and 'count' are now valid 1508 __ movptr(byte_count, count); 1509 __ shrptr(count, 3); // count => qword_count 1510 1511 // Copy from low to high addresses. Use 'to' as scratch. 1512 __ lea(end_from, Address(from, qword_count, Address::times_8, -8)); 1513 __ lea(end_to, Address(to, qword_count, Address::times_8, -8)); 1514 __ negptr(qword_count); // make the count negative 1515 __ jmp(L_copy_bytes); 1516 1517 // Copy trailing qwords 1518 __ BIND(L_copy_8_bytes); 1519 __ movq(rax, Address(end_from, qword_count, Address::times_8, 8)); 1520 __ movq(Address(end_to, qword_count, Address::times_8, 8), rax); 1521 __ increment(qword_count); 1522 __ jcc(Assembler::notZero, L_copy_8_bytes); 1523 1524 // Check for and copy trailing dword 1525 __ BIND(L_copy_4_bytes); 1526 __ testl(byte_count, 4); 1527 __ jccb(Assembler::zero, L_copy_2_bytes); 1528 __ movl(rax, Address(end_from, 8)); 1529 __ movl(Address(end_to, 8), rax); 1530 1531 __ addptr(end_from, 4); 1532 __ addptr(end_to, 4); 1533 1534 // Check for and copy trailing word 1535 __ BIND(L_copy_2_bytes); 1536 __ testl(byte_count, 2); 1537 __ jccb(Assembler::zero, L_copy_byte); 1538 __ movw(rax, Address(end_from, 8)); 1539 __ movw(Address(end_to, 8), rax); 1540 1541 __ addptr(end_from, 2); 1542 __ addptr(end_to, 2); 1543 1544 // Check for and copy trailing byte 1545 __ BIND(L_copy_byte); 1546 __ testl(byte_count, 1); 1547 __ jccb(Assembler::zero, L_exit); 1548 __ movb(rax, Address(end_from, 8)); 1549 __ movb(Address(end_to, 8), rax); 1550 1551 __ BIND(L_exit); 1552 restore_arg_regs(); 1553 inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free 1554 __ xorptr(rax, rax); // return 0 1555 __ leave(); // required for proper stackwalking of RuntimeStub frame 1556 __ ret(0); 1557 1558 // Copy in multi-bytes chunks 1559 copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes); 1560 __ jmp(L_copy_4_bytes); 1561 1562 return start; 1563 } 1564 1565 // Arguments: 1566 // aligned - true => Input and output aligned on a HeapWord == 8-byte boundary 1567 // ignored 1568 // name - stub name string 1569 // 1570 // Inputs: 1571 // c_rarg0 - source array address 1572 // c_rarg1 - destination array address 1573 // c_rarg2 - element count, treated as ssize_t, can be zero 1574 // 1575 // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries, 1576 // we let the hardware handle it. The one to eight bytes within words, 1577 // dwords or qwords that span cache line boundaries will still be loaded 1578 // and stored atomically. 1579 // 1580 address generate_conjoint_byte_copy(bool aligned, address nooverlap_target, 1581 address* entry, const char *name) { 1582 __ align(CodeEntryAlignment); 1583 StubCodeMark mark(this, "StubRoutines", name); 1584 address start = __ pc(); 1585 1586 Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes; 1587 const Register from = rdi; // source array address 1588 const Register to = rsi; // destination array address 1589 const Register count = rdx; // elements count 1590 const Register byte_count = rcx; 1591 const Register qword_count = count; 1592 1593 __ enter(); // required for proper stackwalking of RuntimeStub frame 1594 assert_clean_int(c_rarg2, rax); // Make sure 'count' is clean int. 1595 1596 if (entry != NULL) { 1597 *entry = __ pc(); 1598 // caller can pass a 64-bit byte count here (from Unsafe.copyMemory) 1599 BLOCK_COMMENT("Entry:"); 1600 } 1601 1602 array_overlap_test(nooverlap_target, Address::times_1); 1603 setup_arg_regs(); // from => rdi, to => rsi, count => rdx 1604 // r9 and r10 may be used to save non-volatile registers 1605 1606 // 'from', 'to' and 'count' are now valid 1607 __ movptr(byte_count, count); 1608 __ shrptr(count, 3); // count => qword_count 1609 1610 // Copy from high to low addresses. 1611 1612 // Check for and copy trailing byte 1613 __ testl(byte_count, 1); 1614 __ jcc(Assembler::zero, L_copy_2_bytes); 1615 __ movb(rax, Address(from, byte_count, Address::times_1, -1)); 1616 __ movb(Address(to, byte_count, Address::times_1, -1), rax); 1617 __ decrement(byte_count); // Adjust for possible trailing word 1618 1619 // Check for and copy trailing word 1620 __ BIND(L_copy_2_bytes); 1621 __ testl(byte_count, 2); 1622 __ jcc(Assembler::zero, L_copy_4_bytes); 1623 __ movw(rax, Address(from, byte_count, Address::times_1, -2)); 1624 __ movw(Address(to, byte_count, Address::times_1, -2), rax); 1625 1626 // Check for and copy trailing dword 1627 __ BIND(L_copy_4_bytes); 1628 __ testl(byte_count, 4); 1629 __ jcc(Assembler::zero, L_copy_bytes); 1630 __ movl(rax, Address(from, qword_count, Address::times_8)); 1631 __ movl(Address(to, qword_count, Address::times_8), rax); 1632 __ jmp(L_copy_bytes); 1633 1634 // Copy trailing qwords 1635 __ BIND(L_copy_8_bytes); 1636 __ movq(rax, Address(from, qword_count, Address::times_8, -8)); 1637 __ movq(Address(to, qword_count, Address::times_8, -8), rax); 1638 __ decrement(qword_count); 1639 __ jcc(Assembler::notZero, L_copy_8_bytes); 1640 1641 restore_arg_regs(); 1642 inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free 1643 __ xorptr(rax, rax); // return 0 1644 __ leave(); // required for proper stackwalking of RuntimeStub frame 1645 __ ret(0); 1646 1647 // Copy in multi-bytes chunks 1648 copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes); 1649 1650 restore_arg_regs(); 1651 inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free 1652 __ xorptr(rax, rax); // return 0 1653 __ leave(); // required for proper stackwalking of RuntimeStub frame 1654 __ ret(0); 1655 1656 return start; 1657 } 1658 1659 // Arguments: 1660 // aligned - true => Input and output aligned on a HeapWord == 8-byte boundary 1661 // ignored 1662 // name - stub name string 1663 // 1664 // Inputs: 1665 // c_rarg0 - source array address 1666 // c_rarg1 - destination array address 1667 // c_rarg2 - element count, treated as ssize_t, can be zero 1668 // 1669 // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we 1670 // let the hardware handle it. The two or four words within dwords 1671 // or qwords that span cache line boundaries will still be loaded 1672 // and stored atomically. 1673 // 1674 // Side Effects: 1675 // disjoint_short_copy_entry is set to the no-overlap entry point 1676 // used by generate_conjoint_short_copy(). 1677 // 1678 address generate_disjoint_short_copy(bool aligned, address *entry, const char *name) { 1679 __ align(CodeEntryAlignment); 1680 StubCodeMark mark(this, "StubRoutines", name); 1681 address start = __ pc(); 1682 1683 Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes,L_copy_2_bytes,L_exit; 1684 const Register from = rdi; // source array address 1685 const Register to = rsi; // destination array address 1686 const Register count = rdx; // elements count 1687 const Register word_count = rcx; 1688 const Register qword_count = count; 1689 const Register end_from = from; // source array end address 1690 const Register end_to = to; // destination array end address 1691 // End pointers are inclusive, and if count is not zero they point 1692 // to the last unit copied: end_to[0] := end_from[0] 1693 1694 __ enter(); // required for proper stackwalking of RuntimeStub frame 1695 assert_clean_int(c_rarg2, rax); // Make sure 'count' is clean int. 1696 1697 if (entry != NULL) { 1698 *entry = __ pc(); 1699 // caller can pass a 64-bit byte count here (from Unsafe.copyMemory) 1700 BLOCK_COMMENT("Entry:"); 1701 } 1702 1703 setup_arg_regs(); // from => rdi, to => rsi, count => rdx 1704 // r9 and r10 may be used to save non-volatile registers 1705 1706 // 'from', 'to' and 'count' are now valid 1707 __ movptr(word_count, count); 1708 __ shrptr(count, 2); // count => qword_count 1709 1710 // Copy from low to high addresses. Use 'to' as scratch. 1711 __ lea(end_from, Address(from, qword_count, Address::times_8, -8)); 1712 __ lea(end_to, Address(to, qword_count, Address::times_8, -8)); 1713 __ negptr(qword_count); 1714 __ jmp(L_copy_bytes); 1715 1716 // Copy trailing qwords 1717 __ BIND(L_copy_8_bytes); 1718 __ movq(rax, Address(end_from, qword_count, Address::times_8, 8)); 1719 __ movq(Address(end_to, qword_count, Address::times_8, 8), rax); 1720 __ increment(qword_count); 1721 __ jcc(Assembler::notZero, L_copy_8_bytes); 1722 1723 // Original 'dest' is trashed, so we can't use it as a 1724 // base register for a possible trailing word copy 1725 1726 // Check for and copy trailing dword 1727 __ BIND(L_copy_4_bytes); 1728 __ testl(word_count, 2); 1729 __ jccb(Assembler::zero, L_copy_2_bytes); 1730 __ movl(rax, Address(end_from, 8)); 1731 __ movl(Address(end_to, 8), rax); 1732 1733 __ addptr(end_from, 4); 1734 __ addptr(end_to, 4); 1735 1736 // Check for and copy trailing word 1737 __ BIND(L_copy_2_bytes); 1738 __ testl(word_count, 1); 1739 __ jccb(Assembler::zero, L_exit); 1740 __ movw(rax, Address(end_from, 8)); 1741 __ movw(Address(end_to, 8), rax); 1742 1743 __ BIND(L_exit); 1744 restore_arg_regs(); 1745 inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free 1746 __ xorptr(rax, rax); // return 0 1747 __ leave(); // required for proper stackwalking of RuntimeStub frame 1748 __ ret(0); 1749 1750 // Copy in multi-bytes chunks 1751 copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes); 1752 __ jmp(L_copy_4_bytes); 1753 1754 return start; 1755 } 1756 1757 address generate_fill(BasicType t, bool aligned, const char *name) { 1758 __ align(CodeEntryAlignment); 1759 StubCodeMark mark(this, "StubRoutines", name); 1760 address start = __ pc(); 1761 1762 BLOCK_COMMENT("Entry:"); 1763 1764 const Register to = c_rarg0; // source array address 1765 const Register value = c_rarg1; // value 1766 const Register count = c_rarg2; // elements count 1767 1768 __ enter(); // required for proper stackwalking of RuntimeStub frame 1769 1770 __ generate_fill(t, aligned, to, value, count, rax, xmm0); 1771 1772 __ leave(); // required for proper stackwalking of RuntimeStub frame 1773 __ ret(0); 1774 return start; 1775 } 1776 1777 // Arguments: 1778 // aligned - true => Input and output aligned on a HeapWord == 8-byte boundary 1779 // ignored 1780 // name - stub name string 1781 // 1782 // Inputs: 1783 // c_rarg0 - source array address 1784 // c_rarg1 - destination array address 1785 // c_rarg2 - element count, treated as ssize_t, can be zero 1786 // 1787 // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we 1788 // let the hardware handle it. The two or four words within dwords 1789 // or qwords that span cache line boundaries will still be loaded 1790 // and stored atomically. 1791 // 1792 address generate_conjoint_short_copy(bool aligned, address nooverlap_target, 1793 address *entry, const char *name) { 1794 __ align(CodeEntryAlignment); 1795 StubCodeMark mark(this, "StubRoutines", name); 1796 address start = __ pc(); 1797 1798 Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes; 1799 const Register from = rdi; // source array address 1800 const Register to = rsi; // destination array address 1801 const Register count = rdx; // elements count 1802 const Register word_count = rcx; 1803 const Register qword_count = count; 1804 1805 __ enter(); // required for proper stackwalking of RuntimeStub frame 1806 assert_clean_int(c_rarg2, rax); // Make sure 'count' is clean int. 1807 1808 if (entry != NULL) { 1809 *entry = __ pc(); 1810 // caller can pass a 64-bit byte count here (from Unsafe.copyMemory) 1811 BLOCK_COMMENT("Entry:"); 1812 } 1813 1814 array_overlap_test(nooverlap_target, Address::times_2); 1815 setup_arg_regs(); // from => rdi, to => rsi, count => rdx 1816 // r9 and r10 may be used to save non-volatile registers 1817 1818 // 'from', 'to' and 'count' are now valid 1819 __ movptr(word_count, count); 1820 __ shrptr(count, 2); // count => qword_count 1821 1822 // Copy from high to low addresses. Use 'to' as scratch. 1823 1824 // Check for and copy trailing word 1825 __ testl(word_count, 1); 1826 __ jccb(Assembler::zero, L_copy_4_bytes); 1827 __ movw(rax, Address(from, word_count, Address::times_2, -2)); 1828 __ movw(Address(to, word_count, Address::times_2, -2), rax); 1829 1830 // Check for and copy trailing dword 1831 __ BIND(L_copy_4_bytes); 1832 __ testl(word_count, 2); 1833 __ jcc(Assembler::zero, L_copy_bytes); 1834 __ movl(rax, Address(from, qword_count, Address::times_8)); 1835 __ movl(Address(to, qword_count, Address::times_8), rax); 1836 __ jmp(L_copy_bytes); 1837 1838 // Copy trailing qwords 1839 __ BIND(L_copy_8_bytes); 1840 __ movq(rax, Address(from, qword_count, Address::times_8, -8)); 1841 __ movq(Address(to, qword_count, Address::times_8, -8), rax); 1842 __ decrement(qword_count); 1843 __ jcc(Assembler::notZero, L_copy_8_bytes); 1844 1845 restore_arg_regs(); 1846 inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free 1847 __ xorptr(rax, rax); // return 0 1848 __ leave(); // required for proper stackwalking of RuntimeStub frame 1849 __ ret(0); 1850 1851 // Copy in multi-bytes chunks 1852 copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes); 1853 1854 restore_arg_regs(); 1855 inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free 1856 __ xorptr(rax, rax); // return 0 1857 __ leave(); // required for proper stackwalking of RuntimeStub frame 1858 __ ret(0); 1859 1860 return start; 1861 } 1862 1863 // Arguments: 1864 // aligned - true => Input and output aligned on a HeapWord == 8-byte boundary 1865 // ignored 1866 // is_oop - true => oop array, so generate store check code 1867 // name - stub name string 1868 // 1869 // Inputs: 1870 // c_rarg0 - source array address 1871 // c_rarg1 - destination array address 1872 // c_rarg2 - element count, treated as ssize_t, can be zero 1873 // 1874 // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let 1875 // the hardware handle it. The two dwords within qwords that span 1876 // cache line boundaries will still be loaded and stored atomicly. 1877 // 1878 // Side Effects: 1879 // disjoint_int_copy_entry is set to the no-overlap entry point 1880 // used by generate_conjoint_int_oop_copy(). 1881 // 1882 address generate_disjoint_int_oop_copy(bool aligned, bool is_oop, address* entry, 1883 const char *name, bool dest_uninitialized = false) { 1884 __ align(CodeEntryAlignment); 1885 StubCodeMark mark(this, "StubRoutines", name); 1886 address start = __ pc(); 1887 1888 Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_exit; 1889 const Register from = rdi; // source array address 1890 const Register to = rsi; // destination array address 1891 const Register count = rdx; // elements count 1892 const Register dword_count = rcx; 1893 const Register qword_count = count; 1894 const Register end_from = from; // source array end address 1895 const Register end_to = to; // destination array end address 1896 const Register saved_to = r11; // saved destination array address 1897 // End pointers are inclusive, and if count is not zero they point 1898 // to the last unit copied: end_to[0] := end_from[0] 1899 1900 __ enter(); // required for proper stackwalking of RuntimeStub frame 1901 assert_clean_int(c_rarg2, rax); // Make sure 'count' is clean int. 1902 1903 if (entry != NULL) { 1904 *entry = __ pc(); 1905 // caller can pass a 64-bit byte count here (from Unsafe.copyMemory) 1906 BLOCK_COMMENT("Entry:"); 1907 } 1908 1909 setup_arg_regs(); // from => rdi, to => rsi, count => rdx 1910 // r9 and r10 may be used to save non-volatile registers 1911 if (is_oop) { 1912 __ movq(saved_to, to); 1913 gen_write_ref_array_pre_barrier(to, count, dest_uninitialized); 1914 } 1915 1916 // 'from', 'to' and 'count' are now valid 1917 __ movptr(dword_count, count); 1918 __ shrptr(count, 1); // count => qword_count 1919 1920 // Copy from low to high addresses. Use 'to' as scratch. 1921 __ lea(end_from, Address(from, qword_count, Address::times_8, -8)); 1922 __ lea(end_to, Address(to, qword_count, Address::times_8, -8)); 1923 __ negptr(qword_count); 1924 __ jmp(L_copy_bytes); 1925 1926 // Copy trailing qwords 1927 __ BIND(L_copy_8_bytes); 1928 __ movq(rax, Address(end_from, qword_count, Address::times_8, 8)); 1929 __ movq(Address(end_to, qword_count, Address::times_8, 8), rax); 1930 __ increment(qword_count); 1931 __ jcc(Assembler::notZero, L_copy_8_bytes); 1932 1933 // Check for and copy trailing dword 1934 __ BIND(L_copy_4_bytes); 1935 __ testl(dword_count, 1); // Only byte test since the value is 0 or 1 1936 __ jccb(Assembler::zero, L_exit); 1937 __ movl(rax, Address(end_from, 8)); 1938 __ movl(Address(end_to, 8), rax); 1939 1940 __ BIND(L_exit); 1941 if (is_oop) { 1942 gen_write_ref_array_post_barrier(saved_to, dword_count, rax); 1943 } 1944 restore_arg_regs(); 1945 inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free 1946 __ xorptr(rax, rax); // return 0 1947 __ leave(); // required for proper stackwalking of RuntimeStub frame 1948 __ ret(0); 1949 1950 // Copy in multi-bytes chunks 1951 copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes); 1952 __ jmp(L_copy_4_bytes); 1953 1954 return start; 1955 } 1956 1957 // Arguments: 1958 // aligned - true => Input and output aligned on a HeapWord == 8-byte boundary 1959 // ignored 1960 // is_oop - true => oop array, so generate store check code 1961 // name - stub name string 1962 // 1963 // Inputs: 1964 // c_rarg0 - source array address 1965 // c_rarg1 - destination array address 1966 // c_rarg2 - element count, treated as ssize_t, can be zero 1967 // 1968 // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let 1969 // the hardware handle it. The two dwords within qwords that span 1970 // cache line boundaries will still be loaded and stored atomicly. 1971 // 1972 address generate_conjoint_int_oop_copy(bool aligned, bool is_oop, address nooverlap_target, 1973 address *entry, const char *name, 1974 bool dest_uninitialized = false) { 1975 __ align(CodeEntryAlignment); 1976 StubCodeMark mark(this, "StubRoutines", name); 1977 address start = __ pc(); 1978 1979 Label L_copy_bytes, L_copy_8_bytes, L_copy_2_bytes, L_exit; 1980 const Register from = rdi; // source array address 1981 const Register to = rsi; // destination array address 1982 const Register count = rdx; // elements count 1983 const Register dword_count = rcx; 1984 const Register qword_count = count; 1985 1986 __ enter(); // required for proper stackwalking of RuntimeStub frame 1987 assert_clean_int(c_rarg2, rax); // Make sure 'count' is clean int. 1988 1989 if (entry != NULL) { 1990 *entry = __ pc(); 1991 // caller can pass a 64-bit byte count here (from Unsafe.copyMemory) 1992 BLOCK_COMMENT("Entry:"); 1993 } 1994 1995 array_overlap_test(nooverlap_target, Address::times_4); 1996 setup_arg_regs(); // from => rdi, to => rsi, count => rdx 1997 // r9 and r10 may be used to save non-volatile registers 1998 1999 if (is_oop) { 2000 // no registers are destroyed by this call 2001 gen_write_ref_array_pre_barrier(to, count, dest_uninitialized); 2002 } 2003 2004 assert_clean_int(count, rax); // Make sure 'count' is clean int. 2005 // 'from', 'to' and 'count' are now valid 2006 __ movptr(dword_count, count); 2007 __ shrptr(count, 1); // count => qword_count 2008 2009 // Copy from high to low addresses. Use 'to' as scratch. 2010 2011 // Check for and copy trailing dword 2012 __ testl(dword_count, 1); 2013 __ jcc(Assembler::zero, L_copy_bytes); 2014 __ movl(rax, Address(from, dword_count, Address::times_4, -4)); 2015 __ movl(Address(to, dword_count, Address::times_4, -4), rax); 2016 __ jmp(L_copy_bytes); 2017 2018 // Copy trailing qwords 2019 __ BIND(L_copy_8_bytes); 2020 __ movq(rax, Address(from, qword_count, Address::times_8, -8)); 2021 __ movq(Address(to, qword_count, Address::times_8, -8), rax); 2022 __ decrement(qword_count); 2023 __ jcc(Assembler::notZero, L_copy_8_bytes); 2024 2025 if (is_oop) { 2026 __ jmp(L_exit); 2027 } 2028 restore_arg_regs(); 2029 inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free 2030 __ xorptr(rax, rax); // return 0 2031 __ leave(); // required for proper stackwalking of RuntimeStub frame 2032 __ ret(0); 2033 2034 // Copy in multi-bytes chunks 2035 copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes); 2036 2037 __ BIND(L_exit); 2038 if (is_oop) { 2039 gen_write_ref_array_post_barrier(to, dword_count, rax); 2040 } 2041 restore_arg_regs(); 2042 inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free 2043 __ xorptr(rax, rax); // return 0 2044 __ leave(); // required for proper stackwalking of RuntimeStub frame 2045 __ ret(0); 2046 2047 return start; 2048 } 2049 2050 // Arguments: 2051 // aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes 2052 // ignored 2053 // is_oop - true => oop array, so generate store check code 2054 // name - stub name string 2055 // 2056 // Inputs: 2057 // c_rarg0 - source array address 2058 // c_rarg1 - destination array address 2059 // c_rarg2 - element count, treated as ssize_t, can be zero 2060 // 2061 // Side Effects: 2062 // disjoint_oop_copy_entry or disjoint_long_copy_entry is set to the 2063 // no-overlap entry point used by generate_conjoint_long_oop_copy(). 2064 // 2065 address generate_disjoint_long_oop_copy(bool aligned, bool is_oop, address *entry, 2066 const char *name, bool dest_uninitialized = false) { 2067 __ align(CodeEntryAlignment); 2068 StubCodeMark mark(this, "StubRoutines", name); 2069 address start = __ pc(); 2070 2071 Label L_copy_bytes, L_copy_8_bytes, L_exit; 2072 const Register from = rdi; // source array address 2073 const Register to = rsi; // destination array address 2074 const Register qword_count = rdx; // elements count 2075 const Register end_from = from; // source array end address 2076 const Register end_to = rcx; // destination array end address 2077 const Register saved_to = to; 2078 const Register saved_count = r11; 2079 // End pointers are inclusive, and if count is not zero they point 2080 // to the last unit copied: end_to[0] := end_from[0] 2081 2082 __ enter(); // required for proper stackwalking of RuntimeStub frame 2083 // Save no-overlap entry point for generate_conjoint_long_oop_copy() 2084 assert_clean_int(c_rarg2, rax); // Make sure 'count' is clean int. 2085 2086 if (entry != NULL) { 2087 *entry = __ pc(); 2088 // caller can pass a 64-bit byte count here (from Unsafe.copyMemory) 2089 BLOCK_COMMENT("Entry:"); 2090 } 2091 2092 setup_arg_regs(); // from => rdi, to => rsi, count => rdx 2093 // r9 and r10 may be used to save non-volatile registers 2094 // 'from', 'to' and 'qword_count' are now valid 2095 if (is_oop) { 2096 // Save to and count for store barrier 2097 __ movptr(saved_count, qword_count); 2098 // no registers are destroyed by this call 2099 gen_write_ref_array_pre_barrier(to, qword_count, dest_uninitialized); 2100 } 2101 2102 // Copy from low to high addresses. Use 'to' as scratch. 2103 __ lea(end_from, Address(from, qword_count, Address::times_8, -8)); 2104 __ lea(end_to, Address(to, qword_count, Address::times_8, -8)); 2105 __ negptr(qword_count); 2106 __ jmp(L_copy_bytes); 2107 2108 // Copy trailing qwords 2109 __ BIND(L_copy_8_bytes); 2110 __ movq(rax, Address(end_from, qword_count, Address::times_8, 8)); 2111 __ movq(Address(end_to, qword_count, Address::times_8, 8), rax); 2112 __ increment(qword_count); 2113 __ jcc(Assembler::notZero, L_copy_8_bytes); 2114 2115 if (is_oop) { 2116 __ jmp(L_exit); 2117 } else { 2118 restore_arg_regs(); 2119 inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free 2120 __ xorptr(rax, rax); // return 0 2121 __ leave(); // required for proper stackwalking of RuntimeStub frame 2122 __ ret(0); 2123 } 2124 2125 // Copy in multi-bytes chunks 2126 copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes); 2127 2128 if (is_oop) { 2129 __ BIND(L_exit); 2130 gen_write_ref_array_post_barrier(saved_to, saved_count, rax); 2131 } 2132 restore_arg_regs(); 2133 if (is_oop) { 2134 inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free 2135 } else { 2136 inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free 2137 } 2138 __ xorptr(rax, rax); // return 0 2139 __ leave(); // required for proper stackwalking of RuntimeStub frame 2140 __ ret(0); 2141 2142 return start; 2143 } 2144 2145 // Arguments: 2146 // aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes 2147 // ignored 2148 // is_oop - true => oop array, so generate store check code 2149 // name - stub name string 2150 // 2151 // Inputs: 2152 // c_rarg0 - source array address 2153 // c_rarg1 - destination array address 2154 // c_rarg2 - element count, treated as ssize_t, can be zero 2155 // 2156 address generate_conjoint_long_oop_copy(bool aligned, bool is_oop, 2157 address nooverlap_target, address *entry, 2158 const char *name, bool dest_uninitialized = false) { 2159 __ align(CodeEntryAlignment); 2160 StubCodeMark mark(this, "StubRoutines", name); 2161 address start = __ pc(); 2162 2163 Label L_copy_bytes, L_copy_8_bytes, L_exit; 2164 const Register from = rdi; // source array address 2165 const Register to = rsi; // destination array address 2166 const Register qword_count = rdx; // elements count 2167 const Register saved_count = rcx; 2168 2169 __ enter(); // required for proper stackwalking of RuntimeStub frame 2170 assert_clean_int(c_rarg2, rax); // Make sure 'count' is clean int. 2171 2172 if (entry != NULL) { 2173 *entry = __ pc(); 2174 // caller can pass a 64-bit byte count here (from Unsafe.copyMemory) 2175 BLOCK_COMMENT("Entry:"); 2176 } 2177 2178 array_overlap_test(nooverlap_target, Address::times_8); 2179 setup_arg_regs(); // from => rdi, to => rsi, count => rdx 2180 // r9 and r10 may be used to save non-volatile registers 2181 // 'from', 'to' and 'qword_count' are now valid 2182 if (is_oop) { 2183 // Save to and count for store barrier 2184 __ movptr(saved_count, qword_count); 2185 // No registers are destroyed by this call 2186 gen_write_ref_array_pre_barrier(to, saved_count, dest_uninitialized); 2187 } 2188 2189 __ jmp(L_copy_bytes); 2190 2191 // Copy trailing qwords 2192 __ BIND(L_copy_8_bytes); 2193 __ movq(rax, Address(from, qword_count, Address::times_8, -8)); 2194 __ movq(Address(to, qword_count, Address::times_8, -8), rax); 2195 __ decrement(qword_count); 2196 __ jcc(Assembler::notZero, L_copy_8_bytes); 2197 2198 if (is_oop) { 2199 __ jmp(L_exit); 2200 } else { 2201 restore_arg_regs(); 2202 inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free 2203 __ xorptr(rax, rax); // return 0 2204 __ leave(); // required for proper stackwalking of RuntimeStub frame 2205 __ ret(0); 2206 } 2207 2208 // Copy in multi-bytes chunks 2209 copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes); 2210 2211 if (is_oop) { 2212 __ BIND(L_exit); 2213 gen_write_ref_array_post_barrier(to, saved_count, rax); 2214 } 2215 restore_arg_regs(); 2216 if (is_oop) { 2217 inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free 2218 } else { 2219 inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free 2220 } 2221 __ xorptr(rax, rax); // return 0 2222 __ leave(); // required for proper stackwalking of RuntimeStub frame 2223 __ ret(0); 2224 2225 return start; 2226 } 2227 2228 2229 // Helper for generating a dynamic type check. 2230 // Smashes no registers. 2231 void generate_type_check(Register sub_klass, 2232 Register super_check_offset, 2233 Register super_klass, 2234 Label& L_success) { 2235 assert_different_registers(sub_klass, super_check_offset, super_klass); 2236 2237 BLOCK_COMMENT("type_check:"); 2238 2239 Label L_miss; 2240 2241 __ check_klass_subtype_fast_path(sub_klass, super_klass, noreg, &L_success, &L_miss, NULL, 2242 super_check_offset); 2243 __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg, &L_success, NULL); 2244 2245 // Fall through on failure! 2246 __ BIND(L_miss); 2247 } 2248 2249 // 2250 // Generate checkcasting array copy stub 2251 // 2252 // Input: 2253 // c_rarg0 - source array address 2254 // c_rarg1 - destination array address 2255 // c_rarg2 - element count, treated as ssize_t, can be zero 2256 // c_rarg3 - size_t ckoff (super_check_offset) 2257 // not Win64 2258 // c_rarg4 - oop ckval (super_klass) 2259 // Win64 2260 // rsp+40 - oop ckval (super_klass) 2261 // 2262 // Output: 2263 // rax == 0 - success 2264 // rax == -1^K - failure, where K is partial transfer count 2265 // 2266 address generate_checkcast_copy(const char *name, address *entry, 2267 bool dest_uninitialized = false) { 2268 2269 Label L_load_element, L_store_element, L_do_card_marks, L_done; 2270 2271 // Input registers (after setup_arg_regs) 2272 const Register from = rdi; // source array address 2273 const Register to = rsi; // destination array address 2274 const Register length = rdx; // elements count 2275 const Register ckoff = rcx; // super_check_offset 2276 const Register ckval = r8; // super_klass 2277 2278 // Registers used as temps (r13, r14 are save-on-entry) 2279 const Register end_from = from; // source array end address 2280 const Register end_to = r13; // destination array end address 2281 const Register count = rdx; // -(count_remaining) 2282 const Register r14_length = r14; // saved copy of length 2283 // End pointers are inclusive, and if length is not zero they point 2284 // to the last unit copied: end_to[0] := end_from[0] 2285 2286 const Register rax_oop = rax; // actual oop copied 2287 const Register r11_klass = r11; // oop._klass 2288 2289 //--------------------------------------------------------------- 2290 // Assembler stub will be used for this call to arraycopy 2291 // if the two arrays are subtypes of Object[] but the 2292 // destination array type is not equal to or a supertype 2293 // of the source type. Each element must be separately 2294 // checked. 2295 2296 __ align(CodeEntryAlignment); 2297 StubCodeMark mark(this, "StubRoutines", name); 2298 address start = __ pc(); 2299 2300 __ enter(); // required for proper stackwalking of RuntimeStub frame 2301 2302 #ifdef ASSERT 2303 // caller guarantees that the arrays really are different 2304 // otherwise, we would have to make conjoint checks 2305 { Label L; 2306 array_overlap_test(L, TIMES_OOP); 2307 __ stop("checkcast_copy within a single array"); 2308 __ bind(L); 2309 } 2310 #endif //ASSERT 2311 2312 setup_arg_regs(4); // from => rdi, to => rsi, length => rdx 2313 // ckoff => rcx, ckval => r8 2314 // r9 and r10 may be used to save non-volatile registers 2315 #ifdef _WIN64 2316 // last argument (#4) is on stack on Win64 2317 __ movptr(ckval, Address(rsp, 6 * wordSize)); 2318 #endif 2319 2320 // Caller of this entry point must set up the argument registers. 2321 if (entry != NULL) { 2322 *entry = __ pc(); 2323 BLOCK_COMMENT("Entry:"); 2324 } 2325 2326 // allocate spill slots for r13, r14 2327 enum { 2328 saved_r13_offset, 2329 saved_r14_offset, 2330 saved_rbp_offset 2331 }; 2332 __ subptr(rsp, saved_rbp_offset * wordSize); 2333 __ movptr(Address(rsp, saved_r13_offset * wordSize), r13); 2334 __ movptr(Address(rsp, saved_r14_offset * wordSize), r14); 2335 2336 // check that int operands are properly extended to size_t 2337 assert_clean_int(length, rax); 2338 assert_clean_int(ckoff, rax); 2339 2340 #ifdef ASSERT 2341 BLOCK_COMMENT("assert consistent ckoff/ckval"); 2342 // The ckoff and ckval must be mutually consistent, 2343 // even though caller generates both. 2344 { Label L; 2345 int sco_offset = in_bytes(Klass::super_check_offset_offset()); 2346 __ cmpl(ckoff, Address(ckval, sco_offset)); 2347 __ jcc(Assembler::equal, L); 2348 __ stop("super_check_offset inconsistent"); 2349 __ bind(L); 2350 } 2351 #endif //ASSERT 2352 2353 // Loop-invariant addresses. They are exclusive end pointers. 2354 Address end_from_addr(from, length, TIMES_OOP, 0); 2355 Address end_to_addr(to, length, TIMES_OOP, 0); 2356 // Loop-variant addresses. They assume post-incremented count < 0. 2357 Address from_element_addr(end_from, count, TIMES_OOP, 0); 2358 Address to_element_addr(end_to, count, TIMES_OOP, 0); 2359 2360 gen_write_ref_array_pre_barrier(to, count, dest_uninitialized); 2361 2362 // Copy from low to high addresses, indexed from the end of each array. 2363 __ lea(end_from, end_from_addr); 2364 __ lea(end_to, end_to_addr); 2365 __ movptr(r14_length, length); // save a copy of the length 2366 assert(length == count, ""); // else fix next line: 2367 __ negptr(count); // negate and test the length 2368 __ jcc(Assembler::notZero, L_load_element); 2369 2370 // Empty array: Nothing to do. 2371 __ xorptr(rax, rax); // return 0 on (trivial) success 2372 __ jmp(L_done); 2373 2374 // ======== begin loop ======== 2375 // (Loop is rotated; its entry is L_load_element.) 2376 // Loop control: 2377 // for (count = -count; count != 0; count++) 2378 // Base pointers src, dst are biased by 8*(count-1),to last element. 2379 __ align(OptoLoopAlignment); 2380 2381 __ BIND(L_store_element); 2382 __ store_heap_oop(to_element_addr, rax_oop); // store the oop 2383 __ increment(count); // increment the count toward zero 2384 __ jcc(Assembler::zero, L_do_card_marks); 2385 2386 // ======== loop entry is here ======== 2387 __ BIND(L_load_element); 2388 __ load_heap_oop(rax_oop, from_element_addr); // load the oop 2389 __ testptr(rax_oop, rax_oop); 2390 __ jcc(Assembler::zero, L_store_element); 2391 2392 __ load_klass(r11_klass, rax_oop);// query the object klass 2393 generate_type_check(r11_klass, ckoff, ckval, L_store_element); 2394 // ======== end loop ======== 2395 2396 // It was a real error; we must depend on the caller to finish the job. 2397 // Register rdx = -1 * number of *remaining* oops, r14 = *total* oops. 2398 // Emit GC store barriers for the oops we have copied (r14 + rdx), 2399 // and report their number to the caller. 2400 assert_different_registers(rax, r14_length, count, to, end_to, rcx, rscratch1); 2401 Label L_post_barrier; 2402 __ addptr(r14_length, count); // K = (original - remaining) oops 2403 __ movptr(rax, r14_length); // save the value 2404 __ notptr(rax); // report (-1^K) to caller (does not affect flags) 2405 __ jccb(Assembler::notZero, L_post_barrier); 2406 __ jmp(L_done); // K == 0, nothing was copied, skip post barrier 2407 2408 // Come here on success only. 2409 __ BIND(L_do_card_marks); 2410 __ xorptr(rax, rax); // return 0 on success 2411 2412 __ BIND(L_post_barrier); 2413 gen_write_ref_array_post_barrier(to, r14_length, rscratch1); 2414 2415 // Common exit point (success or failure). 2416 __ BIND(L_done); 2417 __ movptr(r13, Address(rsp, saved_r13_offset * wordSize)); 2418 __ movptr(r14, Address(rsp, saved_r14_offset * wordSize)); 2419 restore_arg_regs(); 2420 inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr); // Update counter after rscratch1 is free 2421 __ leave(); // required for proper stackwalking of RuntimeStub frame 2422 __ ret(0); 2423 2424 return start; 2425 } 2426 2427 // 2428 // Generate 'unsafe' array copy stub 2429 // Though just as safe as the other stubs, it takes an unscaled 2430 // size_t argument instead of an element count. 2431 // 2432 // Input: 2433 // c_rarg0 - source array address 2434 // c_rarg1 - destination array address 2435 // c_rarg2 - byte count, treated as ssize_t, can be zero 2436 // 2437 // Examines the alignment of the operands and dispatches 2438 // to a long, int, short, or byte copy loop. 2439 // 2440 address generate_unsafe_copy(const char *name, 2441 address byte_copy_entry, address short_copy_entry, 2442 address int_copy_entry, address long_copy_entry) { 2443 2444 Label L_long_aligned, L_int_aligned, L_short_aligned; 2445 2446 // Input registers (before setup_arg_regs) 2447 const Register from = c_rarg0; // source array address 2448 const Register to = c_rarg1; // destination array address 2449 const Register size = c_rarg2; // byte count (size_t) 2450 2451 // Register used as a temp 2452 const Register bits = rax; // test copy of low bits 2453 2454 __ align(CodeEntryAlignment); 2455 StubCodeMark mark(this, "StubRoutines", name); 2456 address start = __ pc(); 2457 2458 __ enter(); // required for proper stackwalking of RuntimeStub frame 2459 2460 // bump this on entry, not on exit: 2461 inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr); 2462 2463 __ mov(bits, from); 2464 __ orptr(bits, to); 2465 __ orptr(bits, size); 2466 2467 __ testb(bits, BytesPerLong-1); 2468 __ jccb(Assembler::zero, L_long_aligned); 2469 2470 __ testb(bits, BytesPerInt-1); 2471 __ jccb(Assembler::zero, L_int_aligned); 2472 2473 __ testb(bits, BytesPerShort-1); 2474 __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry)); 2475 2476 __ BIND(L_short_aligned); 2477 __ shrptr(size, LogBytesPerShort); // size => short_count 2478 __ jump(RuntimeAddress(short_copy_entry)); 2479 2480 __ BIND(L_int_aligned); 2481 __ shrptr(size, LogBytesPerInt); // size => int_count 2482 __ jump(RuntimeAddress(int_copy_entry)); 2483 2484 __ BIND(L_long_aligned); 2485 __ shrptr(size, LogBytesPerLong); // size => qword_count 2486 __ jump(RuntimeAddress(long_copy_entry)); 2487 2488 return start; 2489 } 2490 2491 // Perform range checks on the proposed arraycopy. 2492 // Kills temp, but nothing else. 2493 // Also, clean the sign bits of src_pos and dst_pos. 2494 void arraycopy_range_checks(Register src, // source array oop (c_rarg0) 2495 Register src_pos, // source position (c_rarg1) 2496 Register dst, // destination array oo (c_rarg2) 2497 Register dst_pos, // destination position (c_rarg3) 2498 Register length, 2499 Register temp, 2500 Label& L_failed) { 2501 BLOCK_COMMENT("arraycopy_range_checks:"); 2502 2503 // if (src_pos + length > arrayOop(src)->length()) FAIL; 2504 __ movl(temp, length); 2505 __ addl(temp, src_pos); // src_pos + length 2506 __ cmpl(temp, Address(src, arrayOopDesc::length_offset_in_bytes())); 2507 __ jcc(Assembler::above, L_failed); 2508 2509 // if (dst_pos + length > arrayOop(dst)->length()) FAIL; 2510 __ movl(temp, length); 2511 __ addl(temp, dst_pos); // dst_pos + length 2512 __ cmpl(temp, Address(dst, arrayOopDesc::length_offset_in_bytes())); 2513 __ jcc(Assembler::above, L_failed); 2514 2515 // Have to clean up high 32-bits of 'src_pos' and 'dst_pos'. 2516 // Move with sign extension can be used since they are positive. 2517 __ movslq(src_pos, src_pos); 2518 __ movslq(dst_pos, dst_pos); 2519 2520 BLOCK_COMMENT("arraycopy_range_checks done"); 2521 } 2522 2523 // 2524 // Generate generic array copy stubs 2525 // 2526 // Input: 2527 // c_rarg0 - src oop 2528 // c_rarg1 - src_pos (32-bits) 2529 // c_rarg2 - dst oop 2530 // c_rarg3 - dst_pos (32-bits) 2531 // not Win64 2532 // c_rarg4 - element count (32-bits) 2533 // Win64 2534 // rsp+40 - element count (32-bits) 2535 // 2536 // Output: 2537 // rax == 0 - success 2538 // rax == -1^K - failure, where K is partial transfer count 2539 // 2540 address generate_generic_copy(const char *name, 2541 address byte_copy_entry, address short_copy_entry, 2542 address int_copy_entry, address oop_copy_entry, 2543 address long_copy_entry, address checkcast_copy_entry) { 2544 2545 Label L_failed, L_failed_0, L_objArray; 2546 Label L_copy_bytes, L_copy_shorts, L_copy_ints, L_copy_longs; 2547 2548 // Input registers 2549 const Register src = c_rarg0; // source array oop 2550 const Register src_pos = c_rarg1; // source position 2551 const Register dst = c_rarg2; // destination array oop 2552 const Register dst_pos = c_rarg3; // destination position 2553 #ifndef _WIN64 2554 const Register length = c_rarg4; 2555 #else 2556 const Address length(rsp, 6 * wordSize); // elements count is on stack on Win64 2557 #endif 2558 2559 { int modulus = CodeEntryAlignment; 2560 int target = modulus - 5; // 5 = sizeof jmp(L_failed) 2561 int advance = target - (__ offset() % modulus); 2562 if (advance < 0) advance += modulus; 2563 if (advance > 0) __ nop(advance); 2564 } 2565 StubCodeMark mark(this, "StubRoutines", name); 2566 2567 // Short-hop target to L_failed. Makes for denser prologue code. 2568 __ BIND(L_failed_0); 2569 __ jmp(L_failed); 2570 assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed"); 2571 2572 __ align(CodeEntryAlignment); 2573 address start = __ pc(); 2574 2575 __ enter(); // required for proper stackwalking of RuntimeStub frame 2576 2577 // bump this on entry, not on exit: 2578 inc_counter_np(SharedRuntime::_generic_array_copy_ctr); 2579 2580 //----------------------------------------------------------------------- 2581 // Assembler stub will be used for this call to arraycopy 2582 // if the following conditions are met: 2583 // 2584 // (1) src and dst must not be null. 2585 // (2) src_pos must not be negative. 2586 // (3) dst_pos must not be negative. 2587 // (4) length must not be negative. 2588 // (5) src klass and dst klass should be the same and not NULL. 2589 // (6) src and dst should be arrays. 2590 // (7) src_pos + length must not exceed length of src. 2591 // (8) dst_pos + length must not exceed length of dst. 2592 // 2593 2594 // if (src == NULL) return -1; 2595 __ testptr(src, src); // src oop 2596 size_t j1off = __ offset(); 2597 __ jccb(Assembler::zero, L_failed_0); 2598 2599 // if (src_pos < 0) return -1; 2600 __ testl(src_pos, src_pos); // src_pos (32-bits) 2601 __ jccb(Assembler::negative, L_failed_0); 2602 2603 // if (dst == NULL) return -1; 2604 __ testptr(dst, dst); // dst oop 2605 __ jccb(Assembler::zero, L_failed_0); 2606 2607 // if (dst_pos < 0) return -1; 2608 __ testl(dst_pos, dst_pos); // dst_pos (32-bits) 2609 size_t j4off = __ offset(); 2610 __ jccb(Assembler::negative, L_failed_0); 2611 2612 // The first four tests are very dense code, 2613 // but not quite dense enough to put four 2614 // jumps in a 16-byte instruction fetch buffer. 2615 // That's good, because some branch predicters 2616 // do not like jumps so close together. 2617 // Make sure of this. 2618 guarantee(((j1off ^ j4off) & ~15) != 0, "I$ line of 1st & 4th jumps"); 2619 2620 // registers used as temp 2621 const Register r11_length = r11; // elements count to copy 2622 const Register r10_src_klass = r10; // array klass 2623 2624 // if (length < 0) return -1; 2625 __ movl(r11_length, length); // length (elements count, 32-bits value) 2626 __ testl(r11_length, r11_length); 2627 __ jccb(Assembler::negative, L_failed_0); 2628 2629 __ load_klass(r10_src_klass, src); 2630 #ifdef ASSERT 2631 // assert(src->klass() != NULL); 2632 { 2633 BLOCK_COMMENT("assert klasses not null {"); 2634 Label L1, L2; 2635 __ testptr(r10_src_klass, r10_src_klass); 2636 __ jcc(Assembler::notZero, L2); // it is broken if klass is NULL 2637 __ bind(L1); 2638 __ stop("broken null klass"); 2639 __ bind(L2); 2640 __ load_klass(rax, dst); 2641 __ cmpq(rax, 0); 2642 __ jcc(Assembler::equal, L1); // this would be broken also 2643 BLOCK_COMMENT("} assert klasses not null done"); 2644 } 2645 #endif 2646 2647 // Load layout helper (32-bits) 2648 // 2649 // |array_tag| | header_size | element_type | |log2_element_size| 2650 // 32 30 24 16 8 2 0 2651 // 2652 // array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0 2653 // 2654 2655 const int lh_offset = in_bytes(Klass::layout_helper_offset()); 2656 2657 // Handle objArrays completely differently... 2658 const jint objArray_lh = Klass::array_layout_helper(T_OBJECT); 2659 __ cmpl(Address(r10_src_klass, lh_offset), objArray_lh); 2660 __ jcc(Assembler::equal, L_objArray); 2661 2662 // if (src->klass() != dst->klass()) return -1; 2663 __ load_klass(rax, dst); 2664 __ cmpq(r10_src_klass, rax); 2665 __ jcc(Assembler::notEqual, L_failed); 2666 2667 const Register rax_lh = rax; // layout helper 2668 __ movl(rax_lh, Address(r10_src_klass, lh_offset)); 2669 2670 // if (!src->is_Array()) return -1; 2671 __ cmpl(rax_lh, Klass::_lh_neutral_value); 2672 __ jcc(Assembler::greaterEqual, L_failed); 2673 2674 // At this point, it is known to be a typeArray (array_tag 0x3). 2675 #ifdef ASSERT 2676 { 2677 BLOCK_COMMENT("assert primitive array {"); 2678 Label L; 2679 __ cmpl(rax_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift)); 2680 __ jcc(Assembler::greaterEqual, L); 2681 __ stop("must be a primitive array"); 2682 __ bind(L); 2683 BLOCK_COMMENT("} assert primitive array done"); 2684 } 2685 #endif 2686 2687 arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length, 2688 r10, L_failed); 2689 2690 // typeArrayKlass 2691 // 2692 // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize); 2693 // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize); 2694 // 2695 2696 const Register r10_offset = r10; // array offset 2697 const Register rax_elsize = rax_lh; // element size 2698 2699 __ movl(r10_offset, rax_lh); 2700 __ shrl(r10_offset, Klass::_lh_header_size_shift); 2701 __ andptr(r10_offset, Klass::_lh_header_size_mask); // array_offset 2702 __ addptr(src, r10_offset); // src array offset 2703 __ addptr(dst, r10_offset); // dst array offset 2704 BLOCK_COMMENT("choose copy loop based on element size"); 2705 __ andl(rax_lh, Klass::_lh_log2_element_size_mask); // rax_lh -> rax_elsize 2706 2707 // next registers should be set before the jump to corresponding stub 2708 const Register from = c_rarg0; // source array address 2709 const Register to = c_rarg1; // destination array address 2710 const Register count = c_rarg2; // elements count 2711 2712 // 'from', 'to', 'count' registers should be set in such order 2713 // since they are the same as 'src', 'src_pos', 'dst'. 2714 2715 __ BIND(L_copy_bytes); 2716 __ cmpl(rax_elsize, 0); 2717 __ jccb(Assembler::notEqual, L_copy_shorts); 2718 __ lea(from, Address(src, src_pos, Address::times_1, 0));// src_addr 2719 __ lea(to, Address(dst, dst_pos, Address::times_1, 0));// dst_addr 2720 __ movl2ptr(count, r11_length); // length 2721 __ jump(RuntimeAddress(byte_copy_entry)); 2722 2723 __ BIND(L_copy_shorts); 2724 __ cmpl(rax_elsize, LogBytesPerShort); 2725 __ jccb(Assembler::notEqual, L_copy_ints); 2726 __ lea(from, Address(src, src_pos, Address::times_2, 0));// src_addr 2727 __ lea(to, Address(dst, dst_pos, Address::times_2, 0));// dst_addr 2728 __ movl2ptr(count, r11_length); // length 2729 __ jump(RuntimeAddress(short_copy_entry)); 2730 2731 __ BIND(L_copy_ints); 2732 __ cmpl(rax_elsize, LogBytesPerInt); 2733 __ jccb(Assembler::notEqual, L_copy_longs); 2734 __ lea(from, Address(src, src_pos, Address::times_4, 0));// src_addr 2735 __ lea(to, Address(dst, dst_pos, Address::times_4, 0));// dst_addr 2736 __ movl2ptr(count, r11_length); // length 2737 __ jump(RuntimeAddress(int_copy_entry)); 2738 2739 __ BIND(L_copy_longs); 2740 #ifdef ASSERT 2741 { 2742 BLOCK_COMMENT("assert long copy {"); 2743 Label L; 2744 __ cmpl(rax_elsize, LogBytesPerLong); 2745 __ jcc(Assembler::equal, L); 2746 __ stop("must be long copy, but elsize is wrong"); 2747 __ bind(L); 2748 BLOCK_COMMENT("} assert long copy done"); 2749 } 2750 #endif 2751 __ lea(from, Address(src, src_pos, Address::times_8, 0));// src_addr 2752 __ lea(to, Address(dst, dst_pos, Address::times_8, 0));// dst_addr 2753 __ movl2ptr(count, r11_length); // length 2754 __ jump(RuntimeAddress(long_copy_entry)); 2755 2756 // objArrayKlass 2757 __ BIND(L_objArray); 2758 // live at this point: r10_src_klass, r11_length, src[_pos], dst[_pos] 2759 2760 Label L_plain_copy, L_checkcast_copy; 2761 // test array classes for subtyping 2762 __ load_klass(rax, dst); 2763 __ cmpq(r10_src_klass, rax); // usual case is exact equality 2764 __ jcc(Assembler::notEqual, L_checkcast_copy); 2765 2766 // Identically typed arrays can be copied without element-wise checks. 2767 arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length, 2768 r10, L_failed); 2769 2770 __ lea(from, Address(src, src_pos, TIMES_OOP, 2771 arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr 2772 __ lea(to, Address(dst, dst_pos, TIMES_OOP, 2773 arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr 2774 __ movl2ptr(count, r11_length); // length 2775 __ BIND(L_plain_copy); 2776 __ jump(RuntimeAddress(oop_copy_entry)); 2777 2778 __ BIND(L_checkcast_copy); 2779 // live at this point: r10_src_klass, r11_length, rax (dst_klass) 2780 { 2781 // Before looking at dst.length, make sure dst is also an objArray. 2782 __ cmpl(Address(rax, lh_offset), objArray_lh); 2783 __ jcc(Assembler::notEqual, L_failed); 2784 2785 // It is safe to examine both src.length and dst.length. 2786 arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length, 2787 rax, L_failed); 2788 2789 const Register r11_dst_klass = r11; 2790 __ load_klass(r11_dst_klass, dst); // reload 2791 2792 // Marshal the base address arguments now, freeing registers. 2793 __ lea(from, Address(src, src_pos, TIMES_OOP, 2794 arrayOopDesc::base_offset_in_bytes(T_OBJECT))); 2795 __ lea(to, Address(dst, dst_pos, TIMES_OOP, 2796 arrayOopDesc::base_offset_in_bytes(T_OBJECT))); 2797 __ movl(count, length); // length (reloaded) 2798 Register sco_temp = c_rarg3; // this register is free now 2799 assert_different_registers(from, to, count, sco_temp, 2800 r11_dst_klass, r10_src_klass); 2801 assert_clean_int(count, sco_temp); 2802 2803 // Generate the type check. 2804 const int sco_offset = in_bytes(Klass::super_check_offset_offset()); 2805 __ movl(sco_temp, Address(r11_dst_klass, sco_offset)); 2806 assert_clean_int(sco_temp, rax); 2807 generate_type_check(r10_src_klass, sco_temp, r11_dst_klass, L_plain_copy); 2808 2809 // Fetch destination element klass from the objArrayKlass header. 2810 int ek_offset = in_bytes(objArrayKlass::element_klass_offset()); 2811 __ movptr(r11_dst_klass, Address(r11_dst_klass, ek_offset)); 2812 __ movl( sco_temp, Address(r11_dst_klass, sco_offset)); 2813 assert_clean_int(sco_temp, rax); 2814 2815 // the checkcast_copy loop needs two extra arguments: 2816 assert(c_rarg3 == sco_temp, "#3 already in place"); 2817 // Set up arguments for checkcast_copy_entry. 2818 setup_arg_regs(4); 2819 __ movptr(r8, r11_dst_klass); // dst.klass.element_klass, r8 is c_rarg4 on Linux/Solaris 2820 __ jump(RuntimeAddress(checkcast_copy_entry)); 2821 } 2822 2823 __ BIND(L_failed); 2824 __ xorptr(rax, rax); 2825 __ notptr(rax); // return -1 2826 __ leave(); // required for proper stackwalking of RuntimeStub frame 2827 __ ret(0); 2828 2829 return start; 2830 } 2831 2832 void generate_arraycopy_stubs() { 2833 address entry; 2834 address entry_jbyte_arraycopy; 2835 address entry_jshort_arraycopy; 2836 address entry_jint_arraycopy; 2837 address entry_oop_arraycopy; 2838 address entry_jlong_arraycopy; 2839 address entry_checkcast_arraycopy; 2840 2841 StubRoutines::_jbyte_disjoint_arraycopy = generate_disjoint_byte_copy(false, &entry, 2842 "jbyte_disjoint_arraycopy"); 2843 StubRoutines::_jbyte_arraycopy = generate_conjoint_byte_copy(false, entry, &entry_jbyte_arraycopy, 2844 "jbyte_arraycopy"); 2845 2846 StubRoutines::_jshort_disjoint_arraycopy = generate_disjoint_short_copy(false, &entry, 2847 "jshort_disjoint_arraycopy"); 2848 StubRoutines::_jshort_arraycopy = generate_conjoint_short_copy(false, entry, &entry_jshort_arraycopy, 2849 "jshort_arraycopy"); 2850 2851 StubRoutines::_jint_disjoint_arraycopy = generate_disjoint_int_oop_copy(false, false, &entry, 2852 "jint_disjoint_arraycopy"); 2853 StubRoutines::_jint_arraycopy = generate_conjoint_int_oop_copy(false, false, entry, 2854 &entry_jint_arraycopy, "jint_arraycopy"); 2855 2856 StubRoutines::_jlong_disjoint_arraycopy = generate_disjoint_long_oop_copy(false, false, &entry, 2857 "jlong_disjoint_arraycopy"); 2858 StubRoutines::_jlong_arraycopy = generate_conjoint_long_oop_copy(false, false, entry, 2859 &entry_jlong_arraycopy, "jlong_arraycopy"); 2860 2861 2862 if (UseCompressedOops) { 2863 StubRoutines::_oop_disjoint_arraycopy = generate_disjoint_int_oop_copy(false, true, &entry, 2864 "oop_disjoint_arraycopy"); 2865 StubRoutines::_oop_arraycopy = generate_conjoint_int_oop_copy(false, true, entry, 2866 &entry_oop_arraycopy, "oop_arraycopy"); 2867 StubRoutines::_oop_disjoint_arraycopy_uninit = generate_disjoint_int_oop_copy(false, true, &entry, 2868 "oop_disjoint_arraycopy_uninit", 2869 /*dest_uninitialized*/true); 2870 StubRoutines::_oop_arraycopy_uninit = generate_conjoint_int_oop_copy(false, true, entry, 2871 NULL, "oop_arraycopy_uninit", 2872 /*dest_uninitialized*/true); 2873 } else { 2874 StubRoutines::_oop_disjoint_arraycopy = generate_disjoint_long_oop_copy(false, true, &entry, 2875 "oop_disjoint_arraycopy"); 2876 StubRoutines::_oop_arraycopy = generate_conjoint_long_oop_copy(false, true, entry, 2877 &entry_oop_arraycopy, "oop_arraycopy"); 2878 StubRoutines::_oop_disjoint_arraycopy_uninit = generate_disjoint_long_oop_copy(false, true, &entry, 2879 "oop_disjoint_arraycopy_uninit", 2880 /*dest_uninitialized*/true); 2881 StubRoutines::_oop_arraycopy_uninit = generate_conjoint_long_oop_copy(false, true, entry, 2882 NULL, "oop_arraycopy_uninit", 2883 /*dest_uninitialized*/true); 2884 } 2885 2886 StubRoutines::_checkcast_arraycopy = generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy); 2887 StubRoutines::_checkcast_arraycopy_uninit = generate_checkcast_copy("checkcast_arraycopy_uninit", NULL, 2888 /*dest_uninitialized*/true); 2889 2890 StubRoutines::_unsafe_arraycopy = generate_unsafe_copy("unsafe_arraycopy", 2891 entry_jbyte_arraycopy, 2892 entry_jshort_arraycopy, 2893 entry_jint_arraycopy, 2894 entry_jlong_arraycopy); 2895 StubRoutines::_generic_arraycopy = generate_generic_copy("generic_arraycopy", 2896 entry_jbyte_arraycopy, 2897 entry_jshort_arraycopy, 2898 entry_jint_arraycopy, 2899 entry_oop_arraycopy, 2900 entry_jlong_arraycopy, 2901 entry_checkcast_arraycopy); 2902 2903 StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill"); 2904 StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill"); 2905 StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill"); 2906 StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill"); 2907 StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill"); 2908 StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill"); 2909 2910 // We don't generate specialized code for HeapWord-aligned source 2911 // arrays, so just use the code we've already generated 2912 StubRoutines::_arrayof_jbyte_disjoint_arraycopy = StubRoutines::_jbyte_disjoint_arraycopy; 2913 StubRoutines::_arrayof_jbyte_arraycopy = StubRoutines::_jbyte_arraycopy; 2914 2915 StubRoutines::_arrayof_jshort_disjoint_arraycopy = StubRoutines::_jshort_disjoint_arraycopy; 2916 StubRoutines::_arrayof_jshort_arraycopy = StubRoutines::_jshort_arraycopy; 2917 2918 StubRoutines::_arrayof_jint_disjoint_arraycopy = StubRoutines::_jint_disjoint_arraycopy; 2919 StubRoutines::_arrayof_jint_arraycopy = StubRoutines::_jint_arraycopy; 2920 2921 StubRoutines::_arrayof_jlong_disjoint_arraycopy = StubRoutines::_jlong_disjoint_arraycopy; 2922 StubRoutines::_arrayof_jlong_arraycopy = StubRoutines::_jlong_arraycopy; 2923 2924 StubRoutines::_arrayof_oop_disjoint_arraycopy = StubRoutines::_oop_disjoint_arraycopy; 2925 StubRoutines::_arrayof_oop_arraycopy = StubRoutines::_oop_arraycopy; 2926 2927 StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit = StubRoutines::_oop_disjoint_arraycopy_uninit; 2928 StubRoutines::_arrayof_oop_arraycopy_uninit = StubRoutines::_oop_arraycopy_uninit; 2929 } 2930 2931 void generate_math_stubs() { 2932 { 2933 StubCodeMark mark(this, "StubRoutines", "log"); 2934 StubRoutines::_intrinsic_log = (double (*)(double)) __ pc(); 2935 2936 __ subq(rsp, 8); 2937 __ movdbl(Address(rsp, 0), xmm0); 2938 __ fld_d(Address(rsp, 0)); 2939 __ flog(); 2940 __ fstp_d(Address(rsp, 0)); 2941 __ movdbl(xmm0, Address(rsp, 0)); 2942 __ addq(rsp, 8); 2943 __ ret(0); 2944 } 2945 { 2946 StubCodeMark mark(this, "StubRoutines", "log10"); 2947 StubRoutines::_intrinsic_log10 = (double (*)(double)) __ pc(); 2948 2949 __ subq(rsp, 8); 2950 __ movdbl(Address(rsp, 0), xmm0); 2951 __ fld_d(Address(rsp, 0)); 2952 __ flog10(); 2953 __ fstp_d(Address(rsp, 0)); 2954 __ movdbl(xmm0, Address(rsp, 0)); 2955 __ addq(rsp, 8); 2956 __ ret(0); 2957 } 2958 { 2959 StubCodeMark mark(this, "StubRoutines", "sin"); 2960 StubRoutines::_intrinsic_sin = (double (*)(double)) __ pc(); 2961 2962 __ subq(rsp, 8); 2963 __ movdbl(Address(rsp, 0), xmm0); 2964 __ fld_d(Address(rsp, 0)); 2965 __ trigfunc('s'); 2966 __ fstp_d(Address(rsp, 0)); 2967 __ movdbl(xmm0, Address(rsp, 0)); 2968 __ addq(rsp, 8); 2969 __ ret(0); 2970 } 2971 { 2972 StubCodeMark mark(this, "StubRoutines", "cos"); 2973 StubRoutines::_intrinsic_cos = (double (*)(double)) __ pc(); 2974 2975 __ subq(rsp, 8); 2976 __ movdbl(Address(rsp, 0), xmm0); 2977 __ fld_d(Address(rsp, 0)); 2978 __ trigfunc('c'); 2979 __ fstp_d(Address(rsp, 0)); 2980 __ movdbl(xmm0, Address(rsp, 0)); 2981 __ addq(rsp, 8); 2982 __ ret(0); 2983 } 2984 { 2985 StubCodeMark mark(this, "StubRoutines", "tan"); 2986 StubRoutines::_intrinsic_tan = (double (*)(double)) __ pc(); 2987 2988 __ subq(rsp, 8); 2989 __ movdbl(Address(rsp, 0), xmm0); 2990 __ fld_d(Address(rsp, 0)); 2991 __ trigfunc('t'); 2992 __ fstp_d(Address(rsp, 0)); 2993 __ movdbl(xmm0, Address(rsp, 0)); 2994 __ addq(rsp, 8); 2995 __ ret(0); 2996 } 2997 { 2998 StubCodeMark mark(this, "StubRoutines", "exp"); 2999 StubRoutines::_intrinsic_exp = (double (*)(double)) __ pc(); 3000 3001 __ subq(rsp, 8); 3002 __ movdbl(Address(rsp, 0), xmm0); 3003 __ fld_d(Address(rsp, 0)); 3004 __ exp_with_fallback(0); 3005 __ fstp_d(Address(rsp, 0)); 3006 __ movdbl(xmm0, Address(rsp, 0)); 3007 __ addq(rsp, 8); 3008 __ ret(0); 3009 } 3010 { 3011 StubCodeMark mark(this, "StubRoutines", "pow"); 3012 StubRoutines::_intrinsic_pow = (double (*)(double,double)) __ pc(); 3013 3014 __ subq(rsp, 8); 3015 __ movdbl(Address(rsp, 0), xmm1); 3016 __ fld_d(Address(rsp, 0)); 3017 __ movdbl(Address(rsp, 0), xmm0); 3018 __ fld_d(Address(rsp, 0)); 3019 __ pow_with_fallback(0); 3020 __ fstp_d(Address(rsp, 0)); 3021 __ movdbl(xmm0, Address(rsp, 0)); 3022 __ addq(rsp, 8); 3023 __ ret(0); 3024 } 3025 } 3026 3027 // AES intrinsic stubs 3028 enum {AESBlockSize = 16}; 3029 3030 address generate_key_shuffle_mask() { 3031 __ align(16); 3032 StubCodeMark mark(this, "StubRoutines", "key_shuffle_mask"); 3033 address start = __ pc(); 3034 __ emit_data64( 0x0405060700010203, relocInfo::none ); 3035 __ emit_data64( 0x0c0d0e0f08090a0b, relocInfo::none ); 3036 return start; 3037 } 3038 3039 // Utility routine for loading a 128-bit key word in little endian format 3040 // can optionally specify that the shuffle mask is already in an xmmregister 3041 void load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) { 3042 __ movdqu(xmmdst, Address(key, offset)); 3043 if (xmm_shuf_mask != NULL) { 3044 __ pshufb(xmmdst, xmm_shuf_mask); 3045 } else { 3046 __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr())); 3047 } 3048 } 3049 3050 // Arguments: 3051 // 3052 // Inputs: 3053 // c_rarg0 - source byte array address 3054 // c_rarg1 - destination byte array address 3055 // c_rarg2 - K (key) in little endian int array 3056 // 3057 address generate_aescrypt_encryptBlock() { 3058 assert(UseAES, "need AES instructions and misaligned SSE support"); 3059 __ align(CodeEntryAlignment); 3060 StubCodeMark mark(this, "StubRoutines", "aescrypt_encryptBlock"); 3061 Label L_doLast; 3062 address start = __ pc(); 3063 3064 const Register from = c_rarg0; // source array address 3065 const Register to = c_rarg1; // destination array address 3066 const Register key = c_rarg2; // key array address 3067 const Register keylen = rax; 3068 3069 const XMMRegister xmm_result = xmm0; 3070 const XMMRegister xmm_key_shuf_mask = xmm1; 3071 // On win64 xmm6-xmm15 must be preserved so don't use them. 3072 const XMMRegister xmm_temp1 = xmm2; 3073 const XMMRegister xmm_temp2 = xmm3; 3074 const XMMRegister xmm_temp3 = xmm4; 3075 const XMMRegister xmm_temp4 = xmm5; 3076 3077 __ enter(); // required for proper stackwalking of RuntimeStub frame 3078 3079 // keylen could be only {11, 13, 15} * 4 = {44, 52, 60} 3080 __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT))); 3081 3082 __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr())); 3083 __ movdqu(xmm_result, Address(from, 0)); // get 16 bytes of input 3084 3085 // For encryption, the java expanded key ordering is just what we need 3086 // we don't know if the key is aligned, hence not using load-execute form 3087 3088 load_key(xmm_temp1, key, 0x00, xmm_key_shuf_mask); 3089 __ pxor(xmm_result, xmm_temp1); 3090 3091 load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask); 3092 load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask); 3093 load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask); 3094 load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask); 3095 3096 __ aesenc(xmm_result, xmm_temp1); 3097 __ aesenc(xmm_result, xmm_temp2); 3098 __ aesenc(xmm_result, xmm_temp3); 3099 __ aesenc(xmm_result, xmm_temp4); 3100 3101 load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask); 3102 load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask); 3103 load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask); 3104 load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask); 3105 3106 __ aesenc(xmm_result, xmm_temp1); 3107 __ aesenc(xmm_result, xmm_temp2); 3108 __ aesenc(xmm_result, xmm_temp3); 3109 __ aesenc(xmm_result, xmm_temp4); 3110 3111 load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask); 3112 load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask); 3113 3114 __ cmpl(keylen, 44); 3115 __ jccb(Assembler::equal, L_doLast); 3116 3117 __ aesenc(xmm_result, xmm_temp1); 3118 __ aesenc(xmm_result, xmm_temp2); 3119 3120 load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask); 3121 load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask); 3122 3123 __ cmpl(keylen, 52); 3124 __ jccb(Assembler::equal, L_doLast); 3125 3126 __ aesenc(xmm_result, xmm_temp1); 3127 __ aesenc(xmm_result, xmm_temp2); 3128 3129 load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask); 3130 load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask); 3131 3132 __ BIND(L_doLast); 3133 __ aesenc(xmm_result, xmm_temp1); 3134 __ aesenclast(xmm_result, xmm_temp2); 3135 __ movdqu(Address(to, 0), xmm_result); // store the result 3136 __ xorptr(rax, rax); // return 0 3137 __ leave(); // required for proper stackwalking of RuntimeStub frame 3138 __ ret(0); 3139 3140 return start; 3141 } 3142 3143 3144 // Arguments: 3145 // 3146 // Inputs: 3147 // c_rarg0 - source byte array address 3148 // c_rarg1 - destination byte array address 3149 // c_rarg2 - K (key) in little endian int array 3150 // 3151 address generate_aescrypt_decryptBlock() { 3152 assert(UseAES, "need AES instructions and misaligned SSE support"); 3153 __ align(CodeEntryAlignment); 3154 StubCodeMark mark(this, "StubRoutines", "aescrypt_decryptBlock"); 3155 Label L_doLast; 3156 address start = __ pc(); 3157 3158 const Register from = c_rarg0; // source array address 3159 const Register to = c_rarg1; // destination array address 3160 const Register key = c_rarg2; // key array address 3161 const Register keylen = rax; 3162 3163 const XMMRegister xmm_result = xmm0; 3164 const XMMRegister xmm_key_shuf_mask = xmm1; 3165 // On win64 xmm6-xmm15 must be preserved so don't use them. 3166 const XMMRegister xmm_temp1 = xmm2; 3167 const XMMRegister xmm_temp2 = xmm3; 3168 const XMMRegister xmm_temp3 = xmm4; 3169 const XMMRegister xmm_temp4 = xmm5; 3170 3171 __ enter(); // required for proper stackwalking of RuntimeStub frame 3172 3173 // keylen could be only {11, 13, 15} * 4 = {44, 52, 60} 3174 __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT))); 3175 3176 __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr())); 3177 __ movdqu(xmm_result, Address(from, 0)); 3178 3179 // for decryption java expanded key ordering is rotated one position from what we want 3180 // so we start from 0x10 here and hit 0x00 last 3181 // we don't know if the key is aligned, hence not using load-execute form 3182 load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask); 3183 load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask); 3184 load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask); 3185 load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask); 3186 3187 __ pxor (xmm_result, xmm_temp1); 3188 __ aesdec(xmm_result, xmm_temp2); 3189 __ aesdec(xmm_result, xmm_temp3); 3190 __ aesdec(xmm_result, xmm_temp4); 3191 3192 load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask); 3193 load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask); 3194 load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask); 3195 load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask); 3196 3197 __ aesdec(xmm_result, xmm_temp1); 3198 __ aesdec(xmm_result, xmm_temp2); 3199 __ aesdec(xmm_result, xmm_temp3); 3200 __ aesdec(xmm_result, xmm_temp4); 3201 3202 load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask); 3203 load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask); 3204 load_key(xmm_temp3, key, 0x00, xmm_key_shuf_mask); 3205 3206 __ cmpl(keylen, 44); 3207 __ jccb(Assembler::equal, L_doLast); 3208 3209 __ aesdec(xmm_result, xmm_temp1); 3210 __ aesdec(xmm_result, xmm_temp2); 3211 3212 load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask); 3213 load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask); 3214 3215 __ cmpl(keylen, 52); 3216 __ jccb(Assembler::equal, L_doLast); 3217 3218 __ aesdec(xmm_result, xmm_temp1); 3219 __ aesdec(xmm_result, xmm_temp2); 3220 3221 load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask); 3222 load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask); 3223 3224 __ BIND(L_doLast); 3225 __ aesdec(xmm_result, xmm_temp1); 3226 __ aesdec(xmm_result, xmm_temp2); 3227 3228 // for decryption the aesdeclast operation is always on key+0x00 3229 __ aesdeclast(xmm_result, xmm_temp3); 3230 __ movdqu(Address(to, 0), xmm_result); // store the result 3231 __ xorptr(rax, rax); // return 0 3232 __ leave(); // required for proper stackwalking of RuntimeStub frame 3233 __ ret(0); 3234 3235 return start; 3236 } 3237 3238 3239 // Arguments: 3240 // 3241 // Inputs: 3242 // c_rarg0 - source byte array address 3243 // c_rarg1 - destination byte array address 3244 // c_rarg2 - K (key) in little endian int array 3245 // c_rarg3 - r vector byte array address 3246 // c_rarg4 - input length 3247 // 3248 address generate_cipherBlockChaining_encryptAESCrypt() { 3249 assert(UseAES, "need AES instructions and misaligned SSE support"); 3250 __ align(CodeEntryAlignment); 3251 StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_encryptAESCrypt"); 3252 address start = __ pc(); 3253 3254 Label L_exit, L_key_192_256, L_key_256, L_loopTop_128, L_loopTop_192, L_loopTop_256; 3255 const Register from = c_rarg0; // source array address 3256 const Register to = c_rarg1; // destination array address 3257 const Register key = c_rarg2; // key array address 3258 const Register rvec = c_rarg3; // r byte array initialized from initvector array address 3259 // and left with the results of the last encryption block 3260 #ifndef _WIN64 3261 const Register len_reg = c_rarg4; // src len (must be multiple of blocksize 16) 3262 #else 3263 const Address len_mem(rsp, 6 * wordSize); // length is on stack on Win64 3264 const Register len_reg = r10; // pick the first volatile windows register 3265 #endif 3266 const Register pos = rax; 3267 3268 // xmm register assignments for the loops below 3269 const XMMRegister xmm_result = xmm0; 3270 const XMMRegister xmm_temp = xmm1; 3271 // keys 0-10 preloaded into xmm2-xmm12 3272 const int XMM_REG_NUM_KEY_FIRST = 2; 3273 const int XMM_REG_NUM_KEY_LAST = 15; 3274 const XMMRegister xmm_key0 = as_XMMRegister(XMM_REG_NUM_KEY_FIRST); 3275 const XMMRegister xmm_key10 = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+10); 3276 const XMMRegister xmm_key11 = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+11); 3277 const XMMRegister xmm_key12 = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+12); 3278 const XMMRegister xmm_key13 = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+13); 3279 3280 __ enter(); // required for proper stackwalking of RuntimeStub frame 3281 3282 #ifdef _WIN64 3283 // on win64, fill len_reg from stack position 3284 __ movl(len_reg, len_mem); 3285 // save the xmm registers which must be preserved 6-15 3286 __ subptr(rsp, -rsp_after_call_off * wordSize); 3287 for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) { 3288 __ movdqu(xmm_save(i), as_XMMRegister(i)); 3289 } 3290 #endif 3291 3292 const XMMRegister xmm_key_shuf_mask = xmm_temp; // used temporarily to swap key bytes up front 3293 __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr())); 3294 // load up xmm regs xmm2 thru xmm12 with key 0x00 - 0xa0 3295 for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x00; rnum <= XMM_REG_NUM_KEY_FIRST+10; rnum++) { 3296 load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask); 3297 offset += 0x10; 3298 } 3299 __ movdqu(xmm_result, Address(rvec, 0x00)); // initialize xmm_result with r vec 3300 3301 // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256)) 3302 __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT))); 3303 __ cmpl(rax, 44); 3304 __ jcc(Assembler::notEqual, L_key_192_256); 3305 3306 // 128 bit code follows here 3307 __ movptr(pos, 0); 3308 __ align(OptoLoopAlignment); 3309 3310 __ BIND(L_loopTop_128); 3311 __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of input 3312 __ pxor (xmm_result, xmm_temp); // xor with the current r vector 3313 __ pxor (xmm_result, xmm_key0); // do the aes rounds 3314 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_FIRST + 9; rnum++) { 3315 __ aesenc(xmm_result, as_XMMRegister(rnum)); 3316 } 3317 __ aesenclast(xmm_result, xmm_key10); 3318 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output 3319 // no need to store r to memory until we exit 3320 __ addptr(pos, AESBlockSize); 3321 __ subptr(len_reg, AESBlockSize); 3322 __ jcc(Assembler::notEqual, L_loopTop_128); 3323 3324 __ BIND(L_exit); 3325 __ movdqu(Address(rvec, 0), xmm_result); // final value of r stored in rvec of CipherBlockChaining object 3326 3327 #ifdef _WIN64 3328 // restore xmm regs belonging to calling function 3329 for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) { 3330 __ movdqu(as_XMMRegister(i), xmm_save(i)); 3331 } 3332 #endif 3333 __ movl(rax, 0); // return 0 (why?) 3334 __ leave(); // required for proper stackwalking of RuntimeStub frame 3335 __ ret(0); 3336 3337 __ BIND(L_key_192_256); 3338 // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256) 3339 load_key(xmm_key11, key, 0xb0, xmm_key_shuf_mask); 3340 load_key(xmm_key12, key, 0xc0, xmm_key_shuf_mask); 3341 __ cmpl(rax, 52); 3342 __ jcc(Assembler::notEqual, L_key_256); 3343 3344 // 192-bit code follows here (could be changed to use more xmm registers) 3345 __ movptr(pos, 0); 3346 __ align(OptoLoopAlignment); 3347 3348 __ BIND(L_loopTop_192); 3349 __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of input 3350 __ pxor (xmm_result, xmm_temp); // xor with the current r vector 3351 __ pxor (xmm_result, xmm_key0); // do the aes rounds 3352 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_FIRST + 11; rnum++) { 3353 __ aesenc(xmm_result, as_XMMRegister(rnum)); 3354 } 3355 __ aesenclast(xmm_result, xmm_key12); 3356 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output 3357 // no need to store r to memory until we exit 3358 __ addptr(pos, AESBlockSize); 3359 __ subptr(len_reg, AESBlockSize); 3360 __ jcc(Assembler::notEqual, L_loopTop_192); 3361 __ jmp(L_exit); 3362 3363 __ BIND(L_key_256); 3364 // 256-bit code follows here (could be changed to use more xmm registers) 3365 load_key(xmm_key13, key, 0xd0, xmm_key_shuf_mask); 3366 __ movptr(pos, 0); 3367 __ align(OptoLoopAlignment); 3368 3369 __ BIND(L_loopTop_256); 3370 __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of input 3371 __ pxor (xmm_result, xmm_temp); // xor with the current r vector 3372 __ pxor (xmm_result, xmm_key0); // do the aes rounds 3373 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_FIRST + 13; rnum++) { 3374 __ aesenc(xmm_result, as_XMMRegister(rnum)); 3375 } 3376 load_key(xmm_temp, key, 0xe0); 3377 __ aesenclast(xmm_result, xmm_temp); 3378 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output 3379 // no need to store r to memory until we exit 3380 __ addptr(pos, AESBlockSize); 3381 __ subptr(len_reg, AESBlockSize); 3382 __ jcc(Assembler::notEqual, L_loopTop_256); 3383 __ jmp(L_exit); 3384 3385 return start; 3386 } 3387 3388 3389 3390 // This is a version of CBC/AES Decrypt which does 4 blocks in a loop at a time 3391 // to hide instruction latency 3392 // 3393 // Arguments: 3394 // 3395 // Inputs: 3396 // c_rarg0 - source byte array address 3397 // c_rarg1 - destination byte array address 3398 // c_rarg2 - K (key) in little endian int array 3399 // c_rarg3 - r vector byte array address 3400 // c_rarg4 - input length 3401 // 3402 3403 address generate_cipherBlockChaining_decryptAESCrypt_Parallel() { 3404 assert(UseAES, "need AES instructions and misaligned SSE support"); 3405 __ align(CodeEntryAlignment); 3406 StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt"); 3407 address start = __ pc(); 3408 3409 Label L_exit, L_key_192_256, L_key_256; 3410 Label L_singleBlock_loopTop_128, L_multiBlock_loopTop_128; 3411 Label L_singleBlock_loopTop_192, L_singleBlock_loopTop_256; 3412 const Register from = c_rarg0; // source array address 3413 const Register to = c_rarg1; // destination array address 3414 const Register key = c_rarg2; // key array address 3415 const Register rvec = c_rarg3; // r byte array initialized from initvector array address 3416 // and left with the results of the last encryption block 3417 #ifndef _WIN64 3418 const Register len_reg = c_rarg4; // src len (must be multiple of blocksize 16) 3419 #else 3420 const Address len_mem(rsp, 6 * wordSize); // length is on stack on Win64 3421 const Register len_reg = r10; // pick the first volatile windows register 3422 #endif 3423 const Register pos = rax; 3424 3425 // keys 0-10 preloaded into xmm2-xmm12 3426 const int XMM_REG_NUM_KEY_FIRST = 5; 3427 const int XMM_REG_NUM_KEY_LAST = 15; 3428 const XMMRegister xmm_key_first = as_XMMRegister(XMM_REG_NUM_KEY_FIRST); 3429 const XMMRegister xmm_key_last = as_XMMRegister(XMM_REG_NUM_KEY_LAST); 3430 3431 __ enter(); // required for proper stackwalking of RuntimeStub frame 3432 3433 #ifdef _WIN64 3434 // on win64, fill len_reg from stack position 3435 __ movl(len_reg, len_mem); 3436 // save the xmm registers which must be preserved 6-15 3437 __ subptr(rsp, -rsp_after_call_off * wordSize); 3438 for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) { 3439 __ movdqu(xmm_save(i), as_XMMRegister(i)); 3440 } 3441 #endif 3442 // the java expanded key ordering is rotated one position from what we want 3443 // so we start from 0x10 here and hit 0x00 last 3444 const XMMRegister xmm_key_shuf_mask = xmm1; // used temporarily to swap key bytes up front 3445 __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr())); 3446 // load up xmm regs 5 thru 15 with key 0x10 - 0xa0 - 0x00 3447 for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x10; rnum < XMM_REG_NUM_KEY_LAST; rnum++) { 3448 load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask); 3449 offset += 0x10; 3450 } 3451 load_key(xmm_key_last, key, 0x00, xmm_key_shuf_mask); 3452 3453 const XMMRegister xmm_prev_block_cipher = xmm1; // holds cipher of previous block 3454 3455 // registers holding the four results in the parallelized loop 3456 const XMMRegister xmm_result0 = xmm0; 3457 const XMMRegister xmm_result1 = xmm2; 3458 const XMMRegister xmm_result2 = xmm3; 3459 const XMMRegister xmm_result3 = xmm4; 3460 3461 __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00)); // initialize with initial rvec 3462 3463 // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256)) 3464 __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT))); 3465 __ cmpl(rax, 44); 3466 __ jcc(Assembler::notEqual, L_key_192_256); 3467 3468 3469 // 128-bit code follows here, parallelized 3470 __ movptr(pos, 0); 3471 __ align(OptoLoopAlignment); 3472 __ BIND(L_multiBlock_loopTop_128); 3473 __ cmpptr(len_reg, 4*AESBlockSize); // see if at least 4 blocks left 3474 __ jcc(Assembler::less, L_singleBlock_loopTop_128); 3475 3476 __ movdqu(xmm_result0, Address(from, pos, Address::times_1, 0*AESBlockSize)); // get next 4 blocks into xmmresult registers 3477 __ movdqu(xmm_result1, Address(from, pos, Address::times_1, 1*AESBlockSize)); 3478 __ movdqu(xmm_result2, Address(from, pos, Address::times_1, 2*AESBlockSize)); 3479 __ movdqu(xmm_result3, Address(from, pos, Address::times_1, 3*AESBlockSize)); 3480 3481 #define DoFour(opc, src_reg) \ 3482 __ opc(xmm_result0, src_reg); \ 3483 __ opc(xmm_result1, src_reg); \ 3484 __ opc(xmm_result2, src_reg); \ 3485 __ opc(xmm_result3, src_reg); 3486 3487 DoFour(pxor, xmm_key_first); 3488 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) { 3489 DoFour(aesdec, as_XMMRegister(rnum)); 3490 } 3491 DoFour(aesdeclast, xmm_key_last); 3492 // for each result, xor with the r vector of previous cipher block 3493 __ pxor(xmm_result0, xmm_prev_block_cipher); 3494 __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 0*AESBlockSize)); 3495 __ pxor(xmm_result1, xmm_prev_block_cipher); 3496 __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 1*AESBlockSize)); 3497 __ pxor(xmm_result2, xmm_prev_block_cipher); 3498 __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 2*AESBlockSize)); 3499 __ pxor(xmm_result3, xmm_prev_block_cipher); 3500 __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 3*AESBlockSize)); // this will carry over to next set of blocks 3501 3502 __ movdqu(Address(to, pos, Address::times_1, 0*AESBlockSize), xmm_result0); // store 4 results into the next 64 bytes of output 3503 __ movdqu(Address(to, pos, Address::times_1, 1*AESBlockSize), xmm_result1); 3504 __ movdqu(Address(to, pos, Address::times_1, 2*AESBlockSize), xmm_result2); 3505 __ movdqu(Address(to, pos, Address::times_1, 3*AESBlockSize), xmm_result3); 3506 3507 __ addptr(pos, 4*AESBlockSize); 3508 __ subptr(len_reg, 4*AESBlockSize); 3509 __ jmp(L_multiBlock_loopTop_128); 3510 3511 // registers used in the non-parallelized loops 3512 // xmm register assignments for the loops below 3513 const XMMRegister xmm_result = xmm0; 3514 const XMMRegister xmm_prev_block_cipher_save = xmm2; 3515 const XMMRegister xmm_key11 = xmm3; 3516 const XMMRegister xmm_key12 = xmm4; 3517 const XMMRegister xmm_temp = xmm4; 3518 3519 __ align(OptoLoopAlignment); 3520 __ BIND(L_singleBlock_loopTop_128); 3521 __ cmpptr(len_reg, 0); // any blocks left?? 3522 __ jcc(Assembler::equal, L_exit); 3523 __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input 3524 __ movdqa(xmm_prev_block_cipher_save, xmm_result); // save for next r vector 3525 __ pxor (xmm_result, xmm_key_first); // do the aes dec rounds 3526 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) { 3527 __ aesdec(xmm_result, as_XMMRegister(rnum)); 3528 } 3529 __ aesdeclast(xmm_result, xmm_key_last); 3530 __ pxor (xmm_result, xmm_prev_block_cipher); // xor with the current r vector 3531 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output 3532 // no need to store r to memory until we exit 3533 __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save); // set up next r vector with cipher input from this block 3534 3535 __ addptr(pos, AESBlockSize); 3536 __ subptr(len_reg, AESBlockSize); 3537 __ jmp(L_singleBlock_loopTop_128); 3538 3539 3540 __ BIND(L_exit); 3541 __ movdqu(Address(rvec, 0), xmm_prev_block_cipher); // final value of r stored in rvec of CipherBlockChaining object 3542 #ifdef _WIN64 3543 // restore regs belonging to calling function 3544 for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) { 3545 __ movdqu(as_XMMRegister(i), xmm_save(i)); 3546 } 3547 #endif 3548 __ movl(rax, 0); // return 0 (why?) 3549 __ leave(); // required for proper stackwalking of RuntimeStub frame 3550 __ ret(0); 3551 3552 3553 __ BIND(L_key_192_256); 3554 // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256) 3555 load_key(xmm_key11, key, 0xb0); 3556 __ cmpl(rax, 52); 3557 __ jcc(Assembler::notEqual, L_key_256); 3558 3559 // 192-bit code follows here (could be optimized to use parallelism) 3560 load_key(xmm_key12, key, 0xc0); // 192-bit key goes up to c0 3561 __ movptr(pos, 0); 3562 __ align(OptoLoopAlignment); 3563 3564 __ BIND(L_singleBlock_loopTop_192); 3565 __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input 3566 __ movdqa(xmm_prev_block_cipher_save, xmm_result); // save for next r vector 3567 __ pxor (xmm_result, xmm_key_first); // do the aes dec rounds 3568 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) { 3569 __ aesdec(xmm_result, as_XMMRegister(rnum)); 3570 } 3571 __ aesdec(xmm_result, xmm_key11); 3572 __ aesdec(xmm_result, xmm_key12); 3573 __ aesdeclast(xmm_result, xmm_key_last); // xmm15 always came from key+0 3574 __ pxor (xmm_result, xmm_prev_block_cipher); // xor with the current r vector 3575 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output 3576 // no need to store r to memory until we exit 3577 __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save); // set up next r vector with cipher input from this block 3578 __ addptr(pos, AESBlockSize); 3579 __ subptr(len_reg, AESBlockSize); 3580 __ jcc(Assembler::notEqual,L_singleBlock_loopTop_192); 3581 __ jmp(L_exit); 3582 3583 __ BIND(L_key_256); 3584 // 256-bit code follows here (could be optimized to use parallelism) 3585 __ movptr(pos, 0); 3586 __ align(OptoLoopAlignment); 3587 3588 __ BIND(L_singleBlock_loopTop_256); 3589 __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input 3590 __ movdqa(xmm_prev_block_cipher_save, xmm_result); // save for next r vector 3591 __ pxor (xmm_result, xmm_key_first); // do the aes dec rounds 3592 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) { 3593 __ aesdec(xmm_result, as_XMMRegister(rnum)); 3594 } 3595 __ aesdec(xmm_result, xmm_key11); 3596 load_key(xmm_temp, key, 0xc0); 3597 __ aesdec(xmm_result, xmm_temp); 3598 load_key(xmm_temp, key, 0xd0); 3599 __ aesdec(xmm_result, xmm_temp); 3600 load_key(xmm_temp, key, 0xe0); // 256-bit key goes up to e0 3601 __ aesdec(xmm_result, xmm_temp); 3602 __ aesdeclast(xmm_result, xmm_key_last); // xmm15 came from key+0 3603 __ pxor (xmm_result, xmm_prev_block_cipher); // xor with the current r vector 3604 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output 3605 // no need to store r to memory until we exit 3606 __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save); // set up next r vector with cipher input from this block 3607 __ addptr(pos, AESBlockSize); 3608 __ subptr(len_reg, AESBlockSize); 3609 __ jcc(Assembler::notEqual,L_singleBlock_loopTop_256); 3610 __ jmp(L_exit); 3611 3612 return start; 3613 } 3614 3615 3616 3617 #undef __ 3618 #define __ masm-> 3619 3620 // Continuation point for throwing of implicit exceptions that are 3621 // not handled in the current activation. Fabricates an exception 3622 // oop and initiates normal exception dispatching in this 3623 // frame. Since we need to preserve callee-saved values (currently 3624 // only for C2, but done for C1 as well) we need a callee-saved oop 3625 // map and therefore have to make these stubs into RuntimeStubs 3626 // rather than BufferBlobs. If the compiler needs all registers to 3627 // be preserved between the fault point and the exception handler 3628 // then it must assume responsibility for that in 3629 // AbstractCompiler::continuation_for_implicit_null_exception or 3630 // continuation_for_implicit_division_by_zero_exception. All other 3631 // implicit exceptions (e.g., NullPointerException or 3632 // AbstractMethodError on entry) are either at call sites or 3633 // otherwise assume that stack unwinding will be initiated, so 3634 // caller saved registers were assumed volatile in the compiler. 3635 address generate_throw_exception(const char* name, 3636 address runtime_entry, 3637 Register arg1 = noreg, 3638 Register arg2 = noreg) { 3639 // Information about frame layout at time of blocking runtime call. 3640 // Note that we only have to preserve callee-saved registers since 3641 // the compilers are responsible for supplying a continuation point 3642 // if they expect all registers to be preserved. 3643 enum layout { 3644 rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt, 3645 rbp_off2, 3646 return_off, 3647 return_off2, 3648 framesize // inclusive of return address 3649 }; 3650 3651 int insts_size = 512; 3652 int locs_size = 64; 3653 3654 CodeBuffer code(name, insts_size, locs_size); 3655 OopMapSet* oop_maps = new OopMapSet(); 3656 MacroAssembler* masm = new MacroAssembler(&code); 3657 3658 address start = __ pc(); 3659 3660 // This is an inlined and slightly modified version of call_VM 3661 // which has the ability to fetch the return PC out of 3662 // thread-local storage and also sets up last_Java_sp slightly 3663 // differently than the real call_VM 3664 3665 __ enter(); // required for proper stackwalking of RuntimeStub frame 3666 3667 assert(is_even(framesize/2), "sp not 16-byte aligned"); 3668 3669 // return address and rbp are already in place 3670 __ subptr(rsp, (framesize-4) << LogBytesPerInt); // prolog 3671 3672 int frame_complete = __ pc() - start; 3673 3674 // Set up last_Java_sp and last_Java_fp 3675 address the_pc = __ pc(); 3676 __ set_last_Java_frame(rsp, rbp, the_pc); 3677 __ andptr(rsp, -(StackAlignmentInBytes)); // Align stack 3678 3679 // Call runtime 3680 if (arg1 != noreg) { 3681 assert(arg2 != c_rarg1, "clobbered"); 3682 __ movptr(c_rarg1, arg1); 3683 } 3684 if (arg2 != noreg) { 3685 __ movptr(c_rarg2, arg2); 3686 } 3687 __ movptr(c_rarg0, r15_thread); 3688 BLOCK_COMMENT("call runtime_entry"); 3689 __ call(RuntimeAddress(runtime_entry)); 3690 3691 // Generate oop map 3692 OopMap* map = new OopMap(framesize, 0); 3693 3694 oop_maps->add_gc_map(the_pc - start, map); 3695 3696 __ reset_last_Java_frame(true, true); 3697 3698 __ leave(); // required for proper stackwalking of RuntimeStub frame 3699 3700 // check for pending exceptions 3701 #ifdef ASSERT 3702 Label L; 3703 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), 3704 (int32_t) NULL_WORD); 3705 __ jcc(Assembler::notEqual, L); 3706 __ should_not_reach_here(); 3707 __ bind(L); 3708 #endif // ASSERT 3709 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry())); 3710 3711 3712 // codeBlob framesize is in words (not VMRegImpl::slot_size) 3713 RuntimeStub* stub = 3714 RuntimeStub::new_runtime_stub(name, 3715 &code, 3716 frame_complete, 3717 (framesize >> (LogBytesPerWord - LogBytesPerInt)), 3718 oop_maps, false); 3719 return stub->entry_point(); 3720 } 3721 3722 // Initialization 3723 void generate_initial() { 3724 // Generates all stubs and initializes the entry points 3725 3726 // This platform-specific stub is needed by generate_call_stub() 3727 StubRoutines::x86::_mxcsr_std = generate_fp_mask("mxcsr_std", 0x0000000000001F80); 3728 3729 // entry points that exist in all platforms Note: This is code 3730 // that could be shared among different platforms - however the 3731 // benefit seems to be smaller than the disadvantage of having a 3732 // much more complicated generator structure. See also comment in 3733 // stubRoutines.hpp. 3734 3735 StubRoutines::_forward_exception_entry = generate_forward_exception(); 3736 3737 StubRoutines::_call_stub_entry = 3738 generate_call_stub(StubRoutines::_call_stub_return_address); 3739 3740 // is referenced by megamorphic call 3741 StubRoutines::_catch_exception_entry = generate_catch_exception(); 3742 3743 // atomic calls 3744 StubRoutines::_atomic_xchg_entry = generate_atomic_xchg(); 3745 StubRoutines::_atomic_xchg_ptr_entry = generate_atomic_xchg_ptr(); 3746 StubRoutines::_atomic_cmpxchg_entry = generate_atomic_cmpxchg(); 3747 StubRoutines::_atomic_cmpxchg_long_entry = generate_atomic_cmpxchg_long(); 3748 StubRoutines::_atomic_add_entry = generate_atomic_add(); 3749 StubRoutines::_atomic_add_ptr_entry = generate_atomic_add_ptr(); 3750 StubRoutines::_fence_entry = generate_orderaccess_fence(); 3751 3752 StubRoutines::_handler_for_unsafe_access_entry = 3753 generate_handler_for_unsafe_access(); 3754 3755 // platform dependent 3756 StubRoutines::x86::_get_previous_fp_entry = generate_get_previous_fp(); 3757 StubRoutines::x86::_get_previous_sp_entry = generate_get_previous_sp(); 3758 3759 StubRoutines::x86::_verify_mxcsr_entry = generate_verify_mxcsr(); 3760 3761 // Build this early so it's available for the interpreter. 3762 StubRoutines::_throw_StackOverflowError_entry = 3763 generate_throw_exception("StackOverflowError throw_exception", 3764 CAST_FROM_FN_PTR(address, 3765 SharedRuntime:: 3766 throw_StackOverflowError)); 3767 } 3768 3769 void generate_all() { 3770 // Generates all stubs and initializes the entry points 3771 3772 // These entry points require SharedInfo::stack0 to be set up in 3773 // non-core builds and need to be relocatable, so they each 3774 // fabricate a RuntimeStub internally. 3775 StubRoutines::_throw_AbstractMethodError_entry = 3776 generate_throw_exception("AbstractMethodError throw_exception", 3777 CAST_FROM_FN_PTR(address, 3778 SharedRuntime:: 3779 throw_AbstractMethodError)); 3780 3781 StubRoutines::_throw_IncompatibleClassChangeError_entry = 3782 generate_throw_exception("IncompatibleClassChangeError throw_exception", 3783 CAST_FROM_FN_PTR(address, 3784 SharedRuntime:: 3785 throw_IncompatibleClassChangeError)); 3786 3787 StubRoutines::_throw_NullPointerException_at_call_entry = 3788 generate_throw_exception("NullPointerException at call throw_exception", 3789 CAST_FROM_FN_PTR(address, 3790 SharedRuntime:: 3791 throw_NullPointerException_at_call)); 3792 3793 // entry points that are platform specific 3794 StubRoutines::x86::_f2i_fixup = generate_f2i_fixup(); 3795 StubRoutines::x86::_f2l_fixup = generate_f2l_fixup(); 3796 StubRoutines::x86::_d2i_fixup = generate_d2i_fixup(); 3797 StubRoutines::x86::_d2l_fixup = generate_d2l_fixup(); 3798 3799 StubRoutines::x86::_float_sign_mask = generate_fp_mask("float_sign_mask", 0x7FFFFFFF7FFFFFFF); 3800 StubRoutines::x86::_float_sign_flip = generate_fp_mask("float_sign_flip", 0x8000000080000000); 3801 StubRoutines::x86::_double_sign_mask = generate_fp_mask("double_sign_mask", 0x7FFFFFFFFFFFFFFF); 3802 StubRoutines::x86::_double_sign_flip = generate_fp_mask("double_sign_flip", 0x8000000000000000); 3803 3804 // support for verify_oop (must happen after universe_init) 3805 StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop(); 3806 3807 // arraycopy stubs used by compilers 3808 generate_arraycopy_stubs(); 3809 3810 generate_math_stubs(); 3811 3812 // don't bother generating these AES intrinsic stubs unless global flag is set 3813 if (UseAESIntrinsics) { 3814 StubRoutines::x86::_key_shuffle_mask_addr = generate_key_shuffle_mask(); // needed by the others 3815 3816 StubRoutines::_aescrypt_encryptBlock = generate_aescrypt_encryptBlock(); 3817 StubRoutines::_aescrypt_decryptBlock = generate_aescrypt_decryptBlock(); 3818 StubRoutines::_cipherBlockChaining_encryptAESCrypt = generate_cipherBlockChaining_encryptAESCrypt(); 3819 StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptAESCrypt_Parallel(); 3820 } 3821 } 3822 3823 public: 3824 StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) { 3825 if (all) { 3826 generate_all(); 3827 } else { 3828 generate_initial(); 3829 } 3830 } 3831 }; // end class declaration 3832 3833 void StubGenerator_generate(CodeBuffer* code, bool all) { 3834 StubGenerator g(code, all); 3835 }