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