1 /*
   2  * Copyright (c) 2003, 2019, 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 "ci/ciUtilities.hpp"
  29 #include "gc/shared/barrierSet.hpp"
  30 #include "gc/shared/barrierSetAssembler.hpp"
  31 #include "gc/shared/barrierSetNMethod.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "nativeInst_x86.hpp"
  34 #include "oops/instanceOop.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/objArrayKlass.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "prims/methodHandles.hpp"
  39 #include "runtime/frame.inline.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 #include "runtime/sharedRuntime.hpp"
  42 #include "runtime/stubCodeGenerator.hpp"
  43 #include "runtime/stubRoutines.hpp"
  44 #include "runtime/thread.inline.hpp"
  45 #ifdef COMPILER2
  46 #include "opto/runtime.hpp"
  47 #endif
  48 #if INCLUDE_ZGC
  49 #include "gc/z/zThreadLocalData.hpp"
  50 #endif
  51 
  52 // Declaration and definition of StubGenerator (no .hpp file).
  53 // For a more detailed description of the stub routine structure
  54 // see the comment in stubRoutines.hpp
  55 
  56 #define __ _masm->
  57 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
  58 #define a__ ((Assembler*)_masm)->
  59 
  60 #ifdef PRODUCT
  61 #define BLOCK_COMMENT(str) /* nothing */
  62 #else
  63 #define BLOCK_COMMENT(str) __ block_comment(str)
  64 #endif
  65 
  66 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  67 const int MXCSR_MASK = 0xFFC0;  // Mask out any pending exceptions
  68 
  69 // Stub Code definitions
  70 
  71 class StubGenerator: public StubCodeGenerator {
  72  private:
  73 
  74 #ifdef PRODUCT
  75 #define inc_counter_np(counter) ((void)0)
  76 #else
  77   void inc_counter_np_(int& counter) {
  78     // This can destroy rscratch1 if counter is far from the code cache
  79     __ incrementl(ExternalAddress((address)&counter));
  80   }
  81 #define inc_counter_np(counter) \
  82   BLOCK_COMMENT("inc_counter " #counter); \
  83   inc_counter_np_(counter);
  84 #endif
  85 
  86   // Call stubs are used to call Java from C
  87   //
  88   // Linux Arguments:
  89   //    c_rarg0:   call wrapper address                   address
  90   //    c_rarg1:   result                                 address
  91   //    c_rarg2:   result type                            BasicType
  92   //    c_rarg3:   method                                 Method*
  93   //    c_rarg4:   (interpreter) entry point              address
  94   //    c_rarg5:   parameters                             intptr_t*
  95   //    16(rbp): parameter size (in words)              int
  96   //    24(rbp): thread                                 Thread*
  97   //
  98   //     [ return_from_Java     ] <--- rsp
  99   //     [ argument word n      ]
 100   //      ...
 101   // -12 [ argument word 1      ]
 102   // -11 [ saved r15            ] <--- rsp_after_call
 103   // -10 [ saved r14            ]
 104   //  -9 [ saved r13            ]
 105   //  -8 [ saved r12            ]
 106   //  -7 [ saved rbx            ]
 107   //  -6 [ call wrapper         ]
 108   //  -5 [ result               ]
 109   //  -4 [ result type          ]
 110   //  -3 [ method               ]
 111   //  -2 [ entry point          ]
 112   //  -1 [ parameters           ]
 113   //   0 [ saved rbp            ] <--- rbp
 114   //   1 [ return address       ]
 115   //   2 [ parameter size       ]
 116   //   3 [ thread               ]
 117   //
 118   // Windows Arguments:
 119   //    c_rarg0:   call wrapper address                   address
 120   //    c_rarg1:   result                                 address
 121   //    c_rarg2:   result type                            BasicType
 122   //    c_rarg3:   method                                 Method*
 123   //    48(rbp): (interpreter) entry point              address
 124   //    56(rbp): parameters                             intptr_t*
 125   //    64(rbp): parameter size (in words)              int
 126   //    72(rbp): thread                                 Thread*
 127   //
 128   //     [ return_from_Java     ] <--- rsp
 129   //     [ argument word n      ]
 130   //      ...
 131   // -60 [ argument word 1      ]
 132   // -59 [ saved xmm31          ] <--- rsp after_call
 133   //     [ saved xmm16-xmm30    ] (EVEX enabled, else the space is blank)
 134   // -27 [ saved xmm15          ]
 135   //     [ saved xmm7-xmm14     ]
 136   //  -9 [ saved xmm6           ] (each xmm register takes 2 slots)
 137   //  -7 [ saved r15            ]
 138   //  -6 [ saved r14            ]
 139   //  -5 [ saved r13            ]
 140   //  -4 [ saved r12            ]
 141   //  -3 [ saved rdi            ]
 142   //  -2 [ saved rsi            ]
 143   //  -1 [ saved rbx            ]
 144   //   0 [ saved rbp            ] <--- rbp
 145   //   1 [ return address       ]
 146   //   2 [ call wrapper         ]
 147   //   3 [ result               ]
 148   //   4 [ result type          ]
 149   //   5 [ method               ]
 150   //   6 [ entry point          ]
 151   //   7 [ parameters           ]
 152   //   8 [ parameter size       ]
 153   //   9 [ thread               ]
 154   //
 155   //    Windows reserves the callers stack space for arguments 1-4.
 156   //    We spill c_rarg0-c_rarg3 to this space.
 157 
 158   // Call stub stack layout word offsets from rbp
 159   enum call_stub_layout {
 160 #ifdef _WIN64
 161     xmm_save_first     = 6,  // save from xmm6
 162     xmm_save_last      = 31, // to xmm31
 163     xmm_save_base      = -9,
 164     rsp_after_call_off = xmm_save_base - 2 * (xmm_save_last - xmm_save_first), // -27
 165     r15_off            = -7,
 166     r14_off            = -6,
 167     r13_off            = -5,
 168     r12_off            = -4,
 169     rdi_off            = -3,
 170     rsi_off            = -2,
 171     rbx_off            = -1,
 172     rbp_off            =  0,
 173     retaddr_off        =  1,
 174     call_wrapper_off   =  2,
 175     result_off         =  3,
 176     result_type_off    =  4,
 177     method_off         =  5,
 178     entry_point_off    =  6,
 179     parameters_off     =  7,
 180     parameter_size_off =  8,
 181     thread_off         =  9
 182 #else
 183     rsp_after_call_off = -12,
 184     mxcsr_off          = rsp_after_call_off,
 185     r15_off            = -11,
 186     r14_off            = -10,
 187     r13_off            = -9,
 188     r12_off            = -8,
 189     rbx_off            = -7,
 190     call_wrapper_off   = -6,
 191     result_off         = -5,
 192     result_type_off    = -4,
 193     method_off         = -3,
 194     entry_point_off    = -2,
 195     parameters_off     = -1,
 196     rbp_off            =  0,
 197     retaddr_off        =  1,
 198     parameter_size_off =  2,
 199     thread_off         =  3
 200 #endif
 201   };
 202 
 203 #ifdef _WIN64
 204   Address xmm_save(int reg) {
 205     assert(reg >= xmm_save_first && reg <= xmm_save_last, "XMM register number out of range");
 206     return Address(rbp, (xmm_save_base - (reg - xmm_save_first) * 2) * wordSize);
 207   }
 208 #endif
 209 
 210   address generate_call_stub(address& return_address) {
 211     assert((int)frame::entry_frame_after_call_words == -(int)rsp_after_call_off + 1 &&
 212            (int)frame::entry_frame_call_wrapper_offset == (int)call_wrapper_off,
 213            "adjust this code");
 214     StubCodeMark mark(this, "StubRoutines", "call_stub");
 215     address start = __ pc();
 216 
 217     // same as in generate_catch_exception()!
 218     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 219 
 220     const Address call_wrapper  (rbp, call_wrapper_off   * wordSize);
 221     const Address result        (rbp, result_off         * wordSize);
 222     const Address result_type   (rbp, result_type_off    * wordSize);
 223     const Address method        (rbp, method_off         * wordSize);
 224     const Address entry_point   (rbp, entry_point_off    * wordSize);
 225     const Address parameters    (rbp, parameters_off     * wordSize);
 226     const Address parameter_size(rbp, parameter_size_off * wordSize);
 227 
 228     // same as in generate_catch_exception()!
 229     const Address thread        (rbp, thread_off         * wordSize);
 230 
 231     const Address r15_save(rbp, r15_off * wordSize);
 232     const Address r14_save(rbp, r14_off * wordSize);
 233     const Address r13_save(rbp, r13_off * wordSize);
 234     const Address r12_save(rbp, r12_off * wordSize);
 235     const Address rbx_save(rbp, rbx_off * wordSize);
 236 
 237     // stub code
 238     __ enter();
 239     __ subptr(rsp, -rsp_after_call_off * wordSize);
 240 
 241     // save register parameters
 242 #ifndef _WIN64
 243     __ movptr(parameters,   c_rarg5); // parameters
 244     __ movptr(entry_point,  c_rarg4); // entry_point
 245 #endif
 246 
 247     __ movptr(method,       c_rarg3); // method
 248     __ movl(result_type,  c_rarg2);   // result type
 249     __ movptr(result,       c_rarg1); // result
 250     __ movptr(call_wrapper, c_rarg0); // call wrapper
 251 
 252     // save regs belonging to calling function
 253     __ movptr(rbx_save, rbx);
 254     __ movptr(r12_save, r12);
 255     __ movptr(r13_save, r13);
 256     __ movptr(r14_save, r14);
 257     __ movptr(r15_save, r15);
 258 
 259 #ifdef _WIN64
 260     int last_reg = 15;
 261     if (UseAVX > 2) {
 262       last_reg = 31;
 263     }
 264     if (VM_Version::supports_evex()) {
 265       for (int i = xmm_save_first; i <= last_reg; i++) {
 266         __ vextractf32x4(xmm_save(i), as_XMMRegister(i), 0);
 267       }
 268     } else {
 269       for (int i = xmm_save_first; i <= last_reg; i++) {
 270         __ movdqu(xmm_save(i), as_XMMRegister(i));
 271       }
 272     }
 273 
 274     const Address rdi_save(rbp, rdi_off * wordSize);
 275     const Address rsi_save(rbp, rsi_off * wordSize);
 276 
 277     __ movptr(rsi_save, rsi);
 278     __ movptr(rdi_save, rdi);
 279 #else
 280     const Address mxcsr_save(rbp, mxcsr_off * wordSize);
 281     {
 282       Label skip_ldmx;
 283       __ stmxcsr(mxcsr_save);
 284       __ movl(rax, mxcsr_save);
 285       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 286       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 287       __ cmp32(rax, mxcsr_std);
 288       __ jcc(Assembler::equal, skip_ldmx);
 289       __ ldmxcsr(mxcsr_std);
 290       __ bind(skip_ldmx);
 291     }
 292 #endif
 293 
 294     // Load up thread register
 295     __ movptr(r15_thread, thread);
 296     __ reinit_heapbase();
 297 
 298 #ifdef ASSERT
 299     // make sure we have no pending exceptions
 300     {
 301       Label L;
 302       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 303       __ jcc(Assembler::equal, L);
 304       __ stop("StubRoutines::call_stub: entered with pending exception");
 305       __ bind(L);
 306     }
 307 #endif
 308 
 309     // pass parameters if any
 310     BLOCK_COMMENT("pass parameters if any");
 311     Label parameters_done;
 312     __ movl(c_rarg3, parameter_size);
 313     __ testl(c_rarg3, c_rarg3);
 314     __ jcc(Assembler::zero, parameters_done);
 315 
 316     Label loop;
 317     __ movptr(c_rarg2, parameters);       // parameter pointer
 318     __ movl(c_rarg1, c_rarg3);            // parameter counter is in c_rarg1
 319     __ BIND(loop);
 320     __ movptr(rax, Address(c_rarg2, 0));// get parameter
 321     __ addptr(c_rarg2, wordSize);       // advance to next parameter
 322     __ decrementl(c_rarg1);             // decrement counter
 323     __ push(rax);                       // pass parameter
 324     __ jcc(Assembler::notZero, loop);
 325 
 326     // call Java function
 327     __ BIND(parameters_done);
 328     __ movptr(rbx, method);             // get Method*
 329     __ movptr(c_rarg1, entry_point);    // get entry_point
 330     __ mov(r13, rsp);                   // set sender sp
 331     BLOCK_COMMENT("call Java function");
 332     __ call(c_rarg1);
 333 
 334     BLOCK_COMMENT("call_stub_return_address:");
 335     return_address = __ pc();
 336 
 337     // store result depending on type (everything that is not
 338     // T_OBJECT, T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
 339     __ movptr(c_rarg0, result);
 340     Label is_long, is_float, is_double, exit;
 341     __ movl(c_rarg1, result_type);
 342     __ cmpl(c_rarg1, T_OBJECT);
 343     __ jcc(Assembler::equal, is_long);
 344     __ cmpl(c_rarg1, T_LONG);
 345     __ jcc(Assembler::equal, is_long);
 346     __ cmpl(c_rarg1, T_FLOAT);
 347     __ jcc(Assembler::equal, is_float);
 348     __ cmpl(c_rarg1, T_DOUBLE);
 349     __ jcc(Assembler::equal, is_double);
 350 
 351     // handle T_INT case
 352     __ movl(Address(c_rarg0, 0), rax);
 353 
 354     __ BIND(exit);
 355 
 356     // pop parameters
 357     __ lea(rsp, rsp_after_call);
 358 
 359 #ifdef ASSERT
 360     // verify that threads correspond
 361     {
 362      Label L1, L2, L3;
 363       __ cmpptr(r15_thread, thread);
 364       __ jcc(Assembler::equal, L1);
 365       __ stop("StubRoutines::call_stub: r15_thread is corrupted");
 366       __ bind(L1);
 367       __ get_thread(rbx);
 368       __ cmpptr(r15_thread, thread);
 369       __ jcc(Assembler::equal, L2);
 370       __ stop("StubRoutines::call_stub: r15_thread is modified by call");
 371       __ bind(L2);
 372       __ cmpptr(r15_thread, rbx);
 373       __ jcc(Assembler::equal, L3);
 374       __ stop("StubRoutines::call_stub: threads must correspond");
 375       __ bind(L3);
 376     }
 377 #endif
 378 
 379     // restore regs belonging to calling function
 380 #ifdef _WIN64
 381     // emit the restores for xmm regs
 382     if (VM_Version::supports_evex()) {
 383       for (int i = xmm_save_first; i <= last_reg; i++) {
 384         __ vinsertf32x4(as_XMMRegister(i), as_XMMRegister(i), xmm_save(i), 0);
 385       }
 386     } else {
 387       for (int i = xmm_save_first; i <= last_reg; i++) {
 388         __ movdqu(as_XMMRegister(i), xmm_save(i));
 389       }
 390     }
 391 #endif
 392     __ movptr(r15, r15_save);
 393     __ movptr(r14, r14_save);
 394     __ movptr(r13, r13_save);
 395     __ movptr(r12, r12_save);
 396     __ movptr(rbx, rbx_save);
 397 
 398 #ifdef _WIN64
 399     __ movptr(rdi, rdi_save);
 400     __ movptr(rsi, rsi_save);
 401 #else
 402     __ ldmxcsr(mxcsr_save);
 403 #endif
 404 
 405     // restore rsp
 406     __ addptr(rsp, -rsp_after_call_off * wordSize);
 407 
 408     // return
 409     __ vzeroupper();
 410     __ pop(rbp);
 411     __ ret(0);
 412 
 413     // handle return types different from T_INT
 414     __ BIND(is_long);
 415     __ movq(Address(c_rarg0, 0), rax);
 416     __ jmp(exit);
 417 
 418     __ BIND(is_float);
 419     __ movflt(Address(c_rarg0, 0), xmm0);
 420     __ jmp(exit);
 421 
 422     __ BIND(is_double);
 423     __ movdbl(Address(c_rarg0, 0), xmm0);
 424     __ jmp(exit);
 425 
 426     return start;
 427   }
 428 
 429   // Return point for a Java call if there's an exception thrown in
 430   // Java code.  The exception is caught and transformed into a
 431   // pending exception stored in JavaThread that can be tested from
 432   // within the VM.
 433   //
 434   // Note: Usually the parameters are removed by the callee. In case
 435   // of an exception crossing an activation frame boundary, that is
 436   // not the case if the callee is compiled code => need to setup the
 437   // rsp.
 438   //
 439   // rax: exception oop
 440 
 441   address generate_catch_exception() {
 442     StubCodeMark mark(this, "StubRoutines", "catch_exception");
 443     address start = __ pc();
 444 
 445     // same as in generate_call_stub():
 446     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 447     const Address thread        (rbp, thread_off         * wordSize);
 448 
 449 #ifdef ASSERT
 450     // verify that threads correspond
 451     {
 452       Label L1, L2, L3;
 453       __ cmpptr(r15_thread, thread);
 454       __ jcc(Assembler::equal, L1);
 455       __ stop("StubRoutines::catch_exception: r15_thread is corrupted");
 456       __ bind(L1);
 457       __ get_thread(rbx);
 458       __ cmpptr(r15_thread, thread);
 459       __ jcc(Assembler::equal, L2);
 460       __ stop("StubRoutines::catch_exception: r15_thread is modified by call");
 461       __ bind(L2);
 462       __ cmpptr(r15_thread, rbx);
 463       __ jcc(Assembler::equal, L3);
 464       __ stop("StubRoutines::catch_exception: threads must correspond");
 465       __ bind(L3);
 466     }
 467 #endif
 468 
 469     // set pending exception
 470     __ verify_oop(rax);
 471 
 472     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
 473     __ lea(rscratch1, ExternalAddress((address)__FILE__));
 474     __ movptr(Address(r15_thread, Thread::exception_file_offset()), rscratch1);
 475     __ movl(Address(r15_thread, Thread::exception_line_offset()), (int)  __LINE__);
 476 
 477     // complete return to VM
 478     assert(StubRoutines::_call_stub_return_address != NULL,
 479            "_call_stub_return_address must have been generated before");
 480     __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
 481 
 482     return start;
 483   }
 484 
 485   // Continuation point for runtime calls returning with a pending
 486   // exception.  The pending exception check happened in the runtime
 487   // or native call stub.  The pending exception in Thread is
 488   // converted into a Java-level exception.
 489   //
 490   // Contract with Java-level exception handlers:
 491   // rax: exception
 492   // rdx: throwing pc
 493   //
 494   // NOTE: At entry of this stub, exception-pc must be on stack !!
 495 
 496   address generate_forward_exception() {
 497     StubCodeMark mark(this, "StubRoutines", "forward exception");
 498     address start = __ pc();
 499 
 500     // Upon entry, the sp points to the return address returning into
 501     // Java (interpreted or compiled) code; i.e., the return address
 502     // becomes the throwing pc.
 503     //
 504     // Arguments pushed before the runtime call are still on the stack
 505     // but the exception handler will reset the stack pointer ->
 506     // ignore them.  A potential result in registers can be ignored as
 507     // well.
 508 
 509 #ifdef ASSERT
 510     // make sure this code is only executed if there is a pending exception
 511     {
 512       Label L;
 513       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t) NULL);
 514       __ jcc(Assembler::notEqual, L);
 515       __ stop("StubRoutines::forward exception: no pending exception (1)");
 516       __ bind(L);
 517     }
 518 #endif
 519 
 520     // compute exception handler into rbx
 521     __ movptr(c_rarg0, Address(rsp, 0));
 522     BLOCK_COMMENT("call exception_handler_for_return_address");
 523     __ call_VM_leaf(CAST_FROM_FN_PTR(address,
 524                          SharedRuntime::exception_handler_for_return_address),
 525                     r15_thread, c_rarg0);
 526     __ mov(rbx, rax);
 527 
 528     // setup rax & rdx, remove return address & clear pending exception
 529     __ pop(rdx);
 530     __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
 531     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 532 
 533 #ifdef ASSERT
 534     // make sure exception is set
 535     {
 536       Label L;
 537       __ testptr(rax, rax);
 538       __ jcc(Assembler::notEqual, L);
 539       __ stop("StubRoutines::forward exception: no pending exception (2)");
 540       __ bind(L);
 541     }
 542 #endif
 543 
 544     // continue at exception handler (return address removed)
 545     // rax: exception
 546     // rbx: exception handler
 547     // rdx: throwing pc
 548     __ verify_oop(rax);
 549     __ jmp(rbx);
 550 
 551     return start;
 552   }
 553 
 554   // Support for jint atomic::xchg(jint exchange_value, volatile jint* dest)
 555   //
 556   // Arguments :
 557   //    c_rarg0: exchange_value
 558   //    c_rarg0: dest
 559   //
 560   // Result:
 561   //    *dest <- ex, return (orig *dest)
 562   address generate_atomic_xchg() {
 563     StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
 564     address start = __ pc();
 565 
 566     __ movl(rax, c_rarg0); // Copy to eax we need a return value anyhow
 567     __ xchgl(rax, Address(c_rarg1, 0)); // automatic LOCK
 568     __ ret(0);
 569 
 570     return start;
 571   }
 572 
 573   // Support for intptr_t atomic::xchg_long(jlong exchange_value, volatile jlong* dest)
 574   //
 575   // Arguments :
 576   //    c_rarg0: exchange_value
 577   //    c_rarg1: dest
 578   //
 579   // Result:
 580   //    *dest <- ex, return (orig *dest)
 581   address generate_atomic_xchg_long() {
 582     StubCodeMark mark(this, "StubRoutines", "atomic_xchg_long");
 583     address start = __ pc();
 584 
 585     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 586     __ xchgptr(rax, Address(c_rarg1, 0)); // automatic LOCK
 587     __ ret(0);
 588 
 589     return start;
 590   }
 591 
 592   // Support for jint atomic::atomic_cmpxchg(jint exchange_value, volatile jint* dest,
 593   //                                         jint compare_value)
 594   //
 595   // Arguments :
 596   //    c_rarg0: exchange_value
 597   //    c_rarg1: dest
 598   //    c_rarg2: compare_value
 599   //
 600   // Result:
 601   //    if ( compare_value == *dest ) {
 602   //       *dest = exchange_value
 603   //       return compare_value;
 604   //    else
 605   //       return *dest;
 606   address generate_atomic_cmpxchg() {
 607     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg");
 608     address start = __ pc();
 609 
 610     __ movl(rax, c_rarg2);
 611     __ lock();
 612     __ cmpxchgl(c_rarg0, Address(c_rarg1, 0));
 613     __ ret(0);
 614 
 615     return start;
 616   }
 617 
 618   // Support for int8_t atomic::atomic_cmpxchg(int8_t exchange_value, volatile int8_t* dest,
 619   //                                           int8_t compare_value)
 620   //
 621   // Arguments :
 622   //    c_rarg0: exchange_value
 623   //    c_rarg1: dest
 624   //    c_rarg2: compare_value
 625   //
 626   // Result:
 627   //    if ( compare_value == *dest ) {
 628   //       *dest = exchange_value
 629   //       return compare_value;
 630   //    else
 631   //       return *dest;
 632   address generate_atomic_cmpxchg_byte() {
 633     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_byte");
 634     address start = __ pc();
 635 
 636     __ movsbq(rax, c_rarg2);
 637     __ lock();
 638     __ cmpxchgb(c_rarg0, Address(c_rarg1, 0));
 639     __ ret(0);
 640 
 641     return start;
 642   }
 643 
 644   // Support for int64_t atomic::atomic_cmpxchg(int64_t exchange_value,
 645   //                                            volatile int64_t* dest,
 646   //                                            int64_t compare_value)
 647   // Arguments :
 648   //    c_rarg0: exchange_value
 649   //    c_rarg1: dest
 650   //    c_rarg2: compare_value
 651   //
 652   // Result:
 653   //    if ( compare_value == *dest ) {
 654   //       *dest = exchange_value
 655   //       return compare_value;
 656   //    else
 657   //       return *dest;
 658   address generate_atomic_cmpxchg_long() {
 659     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_long");
 660     address start = __ pc();
 661 
 662     __ movq(rax, c_rarg2);
 663     __ lock();
 664     __ cmpxchgq(c_rarg0, Address(c_rarg1, 0));
 665     __ ret(0);
 666 
 667     return start;
 668   }
 669 
 670   // Support for jint atomic::add(jint add_value, volatile jint* dest)
 671   //
 672   // Arguments :
 673   //    c_rarg0: add_value
 674   //    c_rarg1: dest
 675   //
 676   // Result:
 677   //    *dest += add_value
 678   //    return *dest;
 679   address generate_atomic_add() {
 680     StubCodeMark mark(this, "StubRoutines", "atomic_add");
 681     address start = __ pc();
 682 
 683     __ movl(rax, c_rarg0);
 684     __ lock();
 685     __ xaddl(Address(c_rarg1, 0), c_rarg0);
 686     __ addl(rax, c_rarg0);
 687     __ ret(0);
 688 
 689     return start;
 690   }
 691 
 692   // Support for intptr_t atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest)
 693   //
 694   // Arguments :
 695   //    c_rarg0: add_value
 696   //    c_rarg1: dest
 697   //
 698   // Result:
 699   //    *dest += add_value
 700   //    return *dest;
 701   address generate_atomic_add_long() {
 702     StubCodeMark mark(this, "StubRoutines", "atomic_add_long");
 703     address start = __ pc();
 704 
 705     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 706     __ lock();
 707     __ xaddptr(Address(c_rarg1, 0), c_rarg0);
 708     __ addptr(rax, c_rarg0);
 709     __ ret(0);
 710 
 711     return start;
 712   }
 713 
 714   // Support for intptr_t OrderAccess::fence()
 715   //
 716   // Arguments :
 717   //
 718   // Result:
 719   address generate_orderaccess_fence() {
 720     StubCodeMark mark(this, "StubRoutines", "orderaccess_fence");
 721     address start = __ pc();
 722     __ membar(Assembler::StoreLoad);
 723     __ ret(0);
 724 
 725     return start;
 726   }
 727 
 728   // Support for intptr_t get_previous_fp()
 729   //
 730   // This routine is used to find the previous frame pointer for the
 731   // caller (current_frame_guess). This is used as part of debugging
 732   // ps() is seemingly lost trying to find frames.
 733   // This code assumes that caller current_frame_guess) has a frame.
 734   address generate_get_previous_fp() {
 735     StubCodeMark mark(this, "StubRoutines", "get_previous_fp");
 736     const Address old_fp(rbp, 0);
 737     const Address older_fp(rax, 0);
 738     address start = __ pc();
 739 
 740     __ enter();
 741     __ movptr(rax, old_fp); // callers fp
 742     __ movptr(rax, older_fp); // the frame for ps()
 743     __ pop(rbp);
 744     __ ret(0);
 745 
 746     return start;
 747   }
 748 
 749   // Support for intptr_t get_previous_sp()
 750   //
 751   // This routine is used to find the previous stack pointer for the
 752   // caller.
 753   address generate_get_previous_sp() {
 754     StubCodeMark mark(this, "StubRoutines", "get_previous_sp");
 755     address start = __ pc();
 756 
 757     __ movptr(rax, rsp);
 758     __ addptr(rax, 8); // return address is at the top of the stack.
 759     __ ret(0);
 760 
 761     return start;
 762   }
 763 
 764   //----------------------------------------------------------------------------------------------------
 765   // Support for void verify_mxcsr()
 766   //
 767   // This routine is used with -Xcheck:jni to verify that native
 768   // JNI code does not return to Java code without restoring the
 769   // MXCSR register to our expected state.
 770 
 771   address generate_verify_mxcsr() {
 772     StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
 773     address start = __ pc();
 774 
 775     const Address mxcsr_save(rsp, 0);
 776 
 777     if (CheckJNICalls) {
 778       Label ok_ret;
 779       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 780       __ push(rax);
 781       __ subptr(rsp, wordSize);      // allocate a temp location
 782       __ stmxcsr(mxcsr_save);
 783       __ movl(rax, mxcsr_save);
 784       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 785       __ cmp32(rax, mxcsr_std);
 786       __ jcc(Assembler::equal, ok_ret);
 787 
 788       __ warn("MXCSR changed by native JNI code, use -XX:+RestoreMXCSROnJNICall");
 789 
 790       __ ldmxcsr(mxcsr_std);
 791 
 792       __ bind(ok_ret);
 793       __ addptr(rsp, wordSize);
 794       __ pop(rax);
 795     }
 796 
 797     __ ret(0);
 798 
 799     return start;
 800   }
 801 
 802   address generate_f2i_fixup() {
 803     StubCodeMark mark(this, "StubRoutines", "f2i_fixup");
 804     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 805 
 806     address start = __ pc();
 807 
 808     Label L;
 809 
 810     __ push(rax);
 811     __ push(c_rarg3);
 812     __ push(c_rarg2);
 813     __ push(c_rarg1);
 814 
 815     __ movl(rax, 0x7f800000);
 816     __ xorl(c_rarg3, c_rarg3);
 817     __ movl(c_rarg2, inout);
 818     __ movl(c_rarg1, c_rarg2);
 819     __ andl(c_rarg1, 0x7fffffff);
 820     __ cmpl(rax, c_rarg1); // NaN? -> 0
 821     __ jcc(Assembler::negative, L);
 822     __ testl(c_rarg2, c_rarg2); // signed ? min_jint : max_jint
 823     __ movl(c_rarg3, 0x80000000);
 824     __ movl(rax, 0x7fffffff);
 825     __ cmovl(Assembler::positive, c_rarg3, rax);
 826 
 827     __ bind(L);
 828     __ movptr(inout, c_rarg3);
 829 
 830     __ pop(c_rarg1);
 831     __ pop(c_rarg2);
 832     __ pop(c_rarg3);
 833     __ pop(rax);
 834 
 835     __ ret(0);
 836 
 837     return start;
 838   }
 839 
 840   address generate_f2l_fixup() {
 841     StubCodeMark mark(this, "StubRoutines", "f2l_fixup");
 842     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 843     address start = __ pc();
 844 
 845     Label L;
 846 
 847     __ push(rax);
 848     __ push(c_rarg3);
 849     __ push(c_rarg2);
 850     __ push(c_rarg1);
 851 
 852     __ movl(rax, 0x7f800000);
 853     __ xorl(c_rarg3, c_rarg3);
 854     __ movl(c_rarg2, inout);
 855     __ movl(c_rarg1, c_rarg2);
 856     __ andl(c_rarg1, 0x7fffffff);
 857     __ cmpl(rax, c_rarg1); // NaN? -> 0
 858     __ jcc(Assembler::negative, L);
 859     __ testl(c_rarg2, c_rarg2); // signed ? min_jlong : max_jlong
 860     __ mov64(c_rarg3, 0x8000000000000000);
 861     __ mov64(rax, 0x7fffffffffffffff);
 862     __ cmov(Assembler::positive, c_rarg3, rax);
 863 
 864     __ bind(L);
 865     __ movptr(inout, c_rarg3);
 866 
 867     __ pop(c_rarg1);
 868     __ pop(c_rarg2);
 869     __ pop(c_rarg3);
 870     __ pop(rax);
 871 
 872     __ ret(0);
 873 
 874     return start;
 875   }
 876 
 877   address generate_d2i_fixup() {
 878     StubCodeMark mark(this, "StubRoutines", "d2i_fixup");
 879     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 880 
 881     address start = __ pc();
 882 
 883     Label L;
 884 
 885     __ push(rax);
 886     __ push(c_rarg3);
 887     __ push(c_rarg2);
 888     __ push(c_rarg1);
 889     __ push(c_rarg0);
 890 
 891     __ movl(rax, 0x7ff00000);
 892     __ movq(c_rarg2, inout);
 893     __ movl(c_rarg3, c_rarg2);
 894     __ mov(c_rarg1, c_rarg2);
 895     __ mov(c_rarg0, c_rarg2);
 896     __ negl(c_rarg3);
 897     __ shrptr(c_rarg1, 0x20);
 898     __ orl(c_rarg3, c_rarg2);
 899     __ andl(c_rarg1, 0x7fffffff);
 900     __ xorl(c_rarg2, c_rarg2);
 901     __ shrl(c_rarg3, 0x1f);
 902     __ orl(c_rarg1, c_rarg3);
 903     __ cmpl(rax, c_rarg1);
 904     __ jcc(Assembler::negative, L); // NaN -> 0
 905     __ testptr(c_rarg0, c_rarg0); // signed ? min_jint : max_jint
 906     __ movl(c_rarg2, 0x80000000);
 907     __ movl(rax, 0x7fffffff);
 908     __ cmov(Assembler::positive, c_rarg2, rax);
 909 
 910     __ bind(L);
 911     __ movptr(inout, c_rarg2);
 912 
 913     __ pop(c_rarg0);
 914     __ pop(c_rarg1);
 915     __ pop(c_rarg2);
 916     __ pop(c_rarg3);
 917     __ pop(rax);
 918 
 919     __ ret(0);
 920 
 921     return start;
 922   }
 923 
 924   address generate_d2l_fixup() {
 925     StubCodeMark mark(this, "StubRoutines", "d2l_fixup");
 926     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 927 
 928     address start = __ pc();
 929 
 930     Label L;
 931 
 932     __ push(rax);
 933     __ push(c_rarg3);
 934     __ push(c_rarg2);
 935     __ push(c_rarg1);
 936     __ push(c_rarg0);
 937 
 938     __ movl(rax, 0x7ff00000);
 939     __ movq(c_rarg2, inout);
 940     __ movl(c_rarg3, c_rarg2);
 941     __ mov(c_rarg1, c_rarg2);
 942     __ mov(c_rarg0, c_rarg2);
 943     __ negl(c_rarg3);
 944     __ shrptr(c_rarg1, 0x20);
 945     __ orl(c_rarg3, c_rarg2);
 946     __ andl(c_rarg1, 0x7fffffff);
 947     __ xorl(c_rarg2, c_rarg2);
 948     __ shrl(c_rarg3, 0x1f);
 949     __ orl(c_rarg1, c_rarg3);
 950     __ cmpl(rax, c_rarg1);
 951     __ jcc(Assembler::negative, L); // NaN -> 0
 952     __ testq(c_rarg0, c_rarg0); // signed ? min_jlong : max_jlong
 953     __ mov64(c_rarg2, 0x8000000000000000);
 954     __ mov64(rax, 0x7fffffffffffffff);
 955     __ cmovq(Assembler::positive, c_rarg2, rax);
 956 
 957     __ bind(L);
 958     __ movq(inout, c_rarg2);
 959 
 960     __ pop(c_rarg0);
 961     __ pop(c_rarg1);
 962     __ pop(c_rarg2);
 963     __ pop(c_rarg3);
 964     __ pop(rax);
 965 
 966     __ ret(0);
 967 
 968     return start;
 969   }
 970 
 971   address generate_fp_mask(const char *stub_name, int64_t mask) {
 972     __ align(CodeEntryAlignment);
 973     StubCodeMark mark(this, "StubRoutines", stub_name);
 974     address start = __ pc();
 975 
 976     __ emit_data64( mask, relocInfo::none );
 977     __ emit_data64( mask, relocInfo::none );
 978 
 979     return start;
 980   }
 981 
 982   // Non-destructive plausibility checks for oops
 983   //
 984   // Arguments:
 985   //    all args on stack!
 986   //
 987   // Stack after saving c_rarg3:
 988   //    [tos + 0]: saved c_rarg3
 989   //    [tos + 1]: saved c_rarg2
 990   //    [tos + 2]: saved r12 (several TemplateTable methods use it)
 991   //    [tos + 3]: saved flags
 992   //    [tos + 4]: return address
 993   //  * [tos + 5]: error message (char*)
 994   //  * [tos + 6]: object to verify (oop)
 995   //  * [tos + 7]: saved rax - saved by caller and bashed
 996   //  * [tos + 8]: saved r10 (rscratch1) - saved by caller
 997   //  * = popped on exit
 998   address generate_verify_oop() {
 999     StubCodeMark mark(this, "StubRoutines", "verify_oop");
1000     address start = __ pc();
1001 
1002     Label exit, error;
1003 
1004     __ pushf();
1005     __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
1006 
1007     __ push(r12);
1008 
1009     // save c_rarg2 and c_rarg3
1010     __ push(c_rarg2);
1011     __ push(c_rarg3);
1012 
1013     enum {
1014            // After previous pushes.
1015            oop_to_verify = 6 * wordSize,
1016            saved_rax     = 7 * wordSize,
1017            saved_r10     = 8 * wordSize,
1018 
1019            // Before the call to MacroAssembler::debug(), see below.
1020            return_addr   = 16 * wordSize,
1021            error_msg     = 17 * wordSize
1022     };
1023 
1024     // get object
1025     __ movptr(rax, Address(rsp, oop_to_verify));
1026 
1027     // make sure object is 'reasonable'
1028     __ testptr(rax, rax);
1029     __ jcc(Assembler::zero, exit); // if obj is NULL it is OK
1030 
1031 #if INCLUDE_ZGC
1032     if (UseZGC) {
1033       // Check if metadata bits indicate a bad oop
1034       __ testptr(rax, Address(r15_thread, ZThreadLocalData::address_bad_mask_offset()));
1035       __ jcc(Assembler::notZero, error);
1036     }
1037 #endif
1038 
1039     // Check if the oop is in the right area of memory
1040     __ movptr(c_rarg2, rax);
1041     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_mask());
1042     __ andptr(c_rarg2, c_rarg3);
1043     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_bits());
1044     __ cmpptr(c_rarg2, c_rarg3);
1045     __ jcc(Assembler::notZero, error);
1046 
1047     // set r12 to heapbase for load_klass()
1048     __ reinit_heapbase();
1049 
1050     // make sure klass is 'reasonable', which is not zero.
1051     __ load_klass(rax, rax);  // get klass
1052     __ testptr(rax, rax);
1053     __ jcc(Assembler::zero, error); // if klass is NULL it is broken
1054 
1055     // return if everything seems ok
1056     __ bind(exit);
1057     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1058     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1059     __ pop(c_rarg3);                             // restore c_rarg3
1060     __ pop(c_rarg2);                             // restore c_rarg2
1061     __ pop(r12);                                 // restore r12
1062     __ popf();                                   // restore flags
1063     __ ret(4 * wordSize);                        // pop caller saved stuff
1064 
1065     // handle errors
1066     __ bind(error);
1067     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1068     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1069     __ pop(c_rarg3);                             // get saved c_rarg3 back
1070     __ pop(c_rarg2);                             // get saved c_rarg2 back
1071     __ pop(r12);                                 // get saved r12 back
1072     __ popf();                                   // get saved flags off stack --
1073                                                  // will be ignored
1074 
1075     __ pusha();                                  // push registers
1076                                                  // (rip is already
1077                                                  // already pushed)
1078     // debug(char* msg, int64_t pc, int64_t regs[])
1079     // We've popped the registers we'd saved (c_rarg3, c_rarg2 and flags), and
1080     // pushed all the registers, so now the stack looks like:
1081     //     [tos +  0] 16 saved registers
1082     //     [tos + 16] return address
1083     //   * [tos + 17] error message (char*)
1084     //   * [tos + 18] object to verify (oop)
1085     //   * [tos + 19] saved rax - saved by caller and bashed
1086     //   * [tos + 20] saved r10 (rscratch1) - saved by caller
1087     //   * = popped on exit
1088 
1089     __ movptr(c_rarg0, Address(rsp, error_msg));    // pass address of error message
1090     __ movptr(c_rarg1, Address(rsp, return_addr));  // pass return address
1091     __ movq(c_rarg2, rsp);                          // pass address of regs on stack
1092     __ mov(r12, rsp);                               // remember rsp
1093     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1094     __ andptr(rsp, -16);                            // align stack as required by ABI
1095     BLOCK_COMMENT("call MacroAssembler::debug");
1096     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug64)));
1097     __ mov(rsp, r12);                               // restore rsp
1098     __ popa();                                      // pop registers (includes r12)
1099     __ ret(4 * wordSize);                           // pop caller saved stuff
1100 
1101     return start;
1102   }
1103 
1104   //
1105   // Verify that a register contains clean 32-bits positive value
1106   // (high 32-bits are 0) so it could be used in 64-bits shifts.
1107   //
1108   //  Input:
1109   //    Rint  -  32-bits value
1110   //    Rtmp  -  scratch
1111   //
1112   void assert_clean_int(Register Rint, Register Rtmp) {
1113 #ifdef ASSERT
1114     Label L;
1115     assert_different_registers(Rtmp, Rint);
1116     __ movslq(Rtmp, Rint);
1117     __ cmpq(Rtmp, Rint);
1118     __ jcc(Assembler::equal, L);
1119     __ stop("high 32-bits of int value are not 0");
1120     __ bind(L);
1121 #endif
1122   }
1123 
1124   //  Generate overlap test for array copy stubs
1125   //
1126   //  Input:
1127   //     c_rarg0 - from
1128   //     c_rarg1 - to
1129   //     c_rarg2 - element count
1130   //
1131   //  Output:
1132   //     rax   - &from[element count - 1]
1133   //
1134   void array_overlap_test(address no_overlap_target, Address::ScaleFactor sf) {
1135     assert(no_overlap_target != NULL, "must be generated");
1136     array_overlap_test(no_overlap_target, NULL, sf);
1137   }
1138   void array_overlap_test(Label& L_no_overlap, Address::ScaleFactor sf) {
1139     array_overlap_test(NULL, &L_no_overlap, sf);
1140   }
1141   void array_overlap_test(address no_overlap_target, Label* NOLp, Address::ScaleFactor sf) {
1142     const Register from     = c_rarg0;
1143     const Register to       = c_rarg1;
1144     const Register count    = c_rarg2;
1145     const Register end_from = rax;
1146 
1147     __ cmpptr(to, from);
1148     __ lea(end_from, Address(from, count, sf, 0));
1149     if (NOLp == NULL) {
1150       ExternalAddress no_overlap(no_overlap_target);
1151       __ jump_cc(Assembler::belowEqual, no_overlap);
1152       __ cmpptr(to, end_from);
1153       __ jump_cc(Assembler::aboveEqual, no_overlap);
1154     } else {
1155       __ jcc(Assembler::belowEqual, (*NOLp));
1156       __ cmpptr(to, end_from);
1157       __ jcc(Assembler::aboveEqual, (*NOLp));
1158     }
1159   }
1160 
1161   // Shuffle first three arg regs on Windows into Linux/Solaris locations.
1162   //
1163   // Outputs:
1164   //    rdi - rcx
1165   //    rsi - rdx
1166   //    rdx - r8
1167   //    rcx - r9
1168   //
1169   // Registers r9 and r10 are used to save rdi and rsi on Windows, which latter
1170   // are non-volatile.  r9 and r10 should not be used by the caller.
1171   //
1172   DEBUG_ONLY(bool regs_in_thread;)
1173 
1174   void setup_arg_regs(int nargs = 3) {
1175     const Register saved_rdi = r9;
1176     const Register saved_rsi = r10;
1177     assert(nargs == 3 || nargs == 4, "else fix");
1178 #ifdef _WIN64
1179     assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1180            "unexpected argument registers");
1181     if (nargs >= 4)
1182       __ mov(rax, r9);  // r9 is also saved_rdi
1183     __ movptr(saved_rdi, rdi);
1184     __ movptr(saved_rsi, rsi);
1185     __ mov(rdi, rcx); // c_rarg0
1186     __ mov(rsi, rdx); // c_rarg1
1187     __ mov(rdx, r8);  // c_rarg2
1188     if (nargs >= 4)
1189       __ mov(rcx, rax); // c_rarg3 (via rax)
1190 #else
1191     assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1192            "unexpected argument registers");
1193 #endif
1194     DEBUG_ONLY(regs_in_thread = false;)
1195   }
1196 
1197   void restore_arg_regs() {
1198     assert(!regs_in_thread, "wrong call to restore_arg_regs");
1199     const Register saved_rdi = r9;
1200     const Register saved_rsi = r10;
1201 #ifdef _WIN64
1202     __ movptr(rdi, saved_rdi);
1203     __ movptr(rsi, saved_rsi);
1204 #endif
1205   }
1206 
1207   // This is used in places where r10 is a scratch register, and can
1208   // be adapted if r9 is needed also.
1209   void setup_arg_regs_using_thread() {
1210     const Register saved_r15 = r9;
1211 #ifdef _WIN64
1212     __ mov(saved_r15, r15);  // r15 is callee saved and needs to be restored
1213     __ get_thread(r15_thread);
1214     assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1215            "unexpected argument registers");
1216     __ movptr(Address(r15_thread, in_bytes(JavaThread::windows_saved_rdi_offset())), rdi);
1217     __ movptr(Address(r15_thread, in_bytes(JavaThread::windows_saved_rsi_offset())), rsi);
1218 
1219     __ mov(rdi, rcx); // c_rarg0
1220     __ mov(rsi, rdx); // c_rarg1
1221     __ mov(rdx, r8);  // c_rarg2
1222 #else
1223     assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1224            "unexpected argument registers");
1225 #endif
1226     DEBUG_ONLY(regs_in_thread = true;)
1227   }
1228 
1229   void restore_arg_regs_using_thread() {
1230     assert(regs_in_thread, "wrong call to restore_arg_regs");
1231     const Register saved_r15 = r9;
1232 #ifdef _WIN64
1233     __ get_thread(r15_thread);
1234     __ movptr(rsi, Address(r15_thread, in_bytes(JavaThread::windows_saved_rsi_offset())));
1235     __ movptr(rdi, Address(r15_thread, in_bytes(JavaThread::windows_saved_rdi_offset())));
1236     __ mov(r15, saved_r15);  // r15 is callee saved and needs to be restored
1237 #endif
1238   }
1239 
1240   // Copy big chunks forward
1241   //
1242   // Inputs:
1243   //   end_from     - source arrays end address
1244   //   end_to       - destination array end address
1245   //   qword_count  - 64-bits element count, negative
1246   //   to           - scratch
1247   //   L_copy_bytes - entry label
1248   //   L_copy_8_bytes  - exit  label
1249   //
1250   void copy_bytes_forward(Register end_from, Register end_to,
1251                              Register qword_count, Register to,
1252                              Label& L_copy_bytes, Label& L_copy_8_bytes) {
1253     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1254     Label L_loop;
1255     __ align(OptoLoopAlignment);
1256     if (UseUnalignedLoadStores) {
1257       Label L_end;
1258       // Copy 64-bytes per iteration
1259       __ BIND(L_loop);
1260       if (UseAVX > 2) {
1261         __ evmovdqul(xmm0, Address(end_from, qword_count, Address::times_8, -56), Assembler::AVX_512bit);
1262         __ evmovdqul(Address(end_to, qword_count, Address::times_8, -56), xmm0, Assembler::AVX_512bit);
1263       } else if (UseAVX == 2) {
1264         __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56));
1265         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0);
1266         __ vmovdqu(xmm1, Address(end_from, qword_count, Address::times_8, -24));
1267         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm1);
1268       } else {
1269         __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56));
1270         __ movdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0);
1271         __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, -40));
1272         __ movdqu(Address(end_to, qword_count, Address::times_8, -40), xmm1);
1273         __ movdqu(xmm2, Address(end_from, qword_count, Address::times_8, -24));
1274         __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm2);
1275         __ movdqu(xmm3, Address(end_from, qword_count, Address::times_8, - 8));
1276         __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm3);
1277       }
1278       __ BIND(L_copy_bytes);
1279       __ addptr(qword_count, 8);
1280       __ jcc(Assembler::lessEqual, L_loop);
1281       __ subptr(qword_count, 4);  // sub(8) and add(4)
1282       __ jccb(Assembler::greater, L_end);
1283       // Copy trailing 32 bytes
1284       if (UseAVX >= 2) {
1285         __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1286         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1287       } else {
1288         __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1289         __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1290         __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, - 8));
1291         __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm1);
1292       }
1293       __ addptr(qword_count, 4);
1294       __ BIND(L_end);
1295       if (UseAVX >= 2) {
1296         // clean upper bits of YMM registers
1297         __ vpxor(xmm0, xmm0);
1298         __ vpxor(xmm1, xmm1);
1299       }
1300     } else {
1301       // Copy 32-bytes per iteration
1302       __ BIND(L_loop);
1303       __ movq(to, Address(end_from, qword_count, Address::times_8, -24));
1304       __ movq(Address(end_to, qword_count, Address::times_8, -24), to);
1305       __ movq(to, Address(end_from, qword_count, Address::times_8, -16));
1306       __ movq(Address(end_to, qword_count, Address::times_8, -16), to);
1307       __ movq(to, Address(end_from, qword_count, Address::times_8, - 8));
1308       __ movq(Address(end_to, qword_count, Address::times_8, - 8), to);
1309       __ movq(to, Address(end_from, qword_count, Address::times_8, - 0));
1310       __ movq(Address(end_to, qword_count, Address::times_8, - 0), to);
1311 
1312       __ BIND(L_copy_bytes);
1313       __ addptr(qword_count, 4);
1314       __ jcc(Assembler::lessEqual, L_loop);
1315     }
1316     __ subptr(qword_count, 4);
1317     __ jcc(Assembler::less, L_copy_8_bytes); // Copy trailing qwords
1318   }
1319 
1320   // Copy big chunks backward
1321   //
1322   // Inputs:
1323   //   from         - source arrays address
1324   //   dest         - destination array address
1325   //   qword_count  - 64-bits element count
1326   //   to           - scratch
1327   //   L_copy_bytes - entry label
1328   //   L_copy_8_bytes  - exit  label
1329   //
1330   void copy_bytes_backward(Register from, Register dest,
1331                               Register qword_count, Register to,
1332                               Label& L_copy_bytes, Label& L_copy_8_bytes) {
1333     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1334     Label L_loop;
1335     __ align(OptoLoopAlignment);
1336     if (UseUnalignedLoadStores) {
1337       Label L_end;
1338       // Copy 64-bytes per iteration
1339       __ BIND(L_loop);
1340       if (UseAVX > 2) {
1341         __ evmovdqul(xmm0, Address(from, qword_count, Address::times_8, 0), Assembler::AVX_512bit);
1342         __ evmovdqul(Address(dest, qword_count, Address::times_8, 0), xmm0, Assembler::AVX_512bit);
1343       } else if (UseAVX == 2) {
1344         __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 32));
1345         __ vmovdqu(Address(dest, qword_count, Address::times_8, 32), xmm0);
1346         __ vmovdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1347         __ vmovdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1348       } else {
1349         __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 48));
1350         __ movdqu(Address(dest, qword_count, Address::times_8, 48), xmm0);
1351         __ movdqu(xmm1, Address(from, qword_count, Address::times_8, 32));
1352         __ movdqu(Address(dest, qword_count, Address::times_8, 32), xmm1);
1353         __ movdqu(xmm2, Address(from, qword_count, Address::times_8, 16));
1354         __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm2);
1355         __ movdqu(xmm3, Address(from, qword_count, Address::times_8,  0));
1356         __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm3);
1357       }
1358       __ BIND(L_copy_bytes);
1359       __ subptr(qword_count, 8);
1360       __ jcc(Assembler::greaterEqual, L_loop);
1361 
1362       __ addptr(qword_count, 4);  // add(8) and sub(4)
1363       __ jccb(Assembler::less, L_end);
1364       // Copy trailing 32 bytes
1365       if (UseAVX >= 2) {
1366         __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 0));
1367         __ vmovdqu(Address(dest, qword_count, Address::times_8, 0), xmm0);
1368       } else {
1369         __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 16));
1370         __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm0);
1371         __ movdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1372         __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1373       }
1374       __ subptr(qword_count, 4);
1375       __ BIND(L_end);
1376       if (UseAVX >= 2) {
1377         // clean upper bits of YMM registers
1378         __ vpxor(xmm0, xmm0);
1379         __ vpxor(xmm1, xmm1);
1380       }
1381     } else {
1382       // Copy 32-bytes per iteration
1383       __ BIND(L_loop);
1384       __ movq(to, Address(from, qword_count, Address::times_8, 24));
1385       __ movq(Address(dest, qword_count, Address::times_8, 24), to);
1386       __ movq(to, Address(from, qword_count, Address::times_8, 16));
1387       __ movq(Address(dest, qword_count, Address::times_8, 16), to);
1388       __ movq(to, Address(from, qword_count, Address::times_8,  8));
1389       __ movq(Address(dest, qword_count, Address::times_8,  8), to);
1390       __ movq(to, Address(from, qword_count, Address::times_8,  0));
1391       __ movq(Address(dest, qword_count, Address::times_8,  0), to);
1392 
1393       __ BIND(L_copy_bytes);
1394       __ subptr(qword_count, 4);
1395       __ jcc(Assembler::greaterEqual, L_loop);
1396     }
1397     __ addptr(qword_count, 4);
1398     __ jcc(Assembler::greater, L_copy_8_bytes); // Copy trailing qwords
1399   }
1400 
1401 
1402   // Arguments:
1403   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1404   //             ignored
1405   //   name    - stub name string
1406   //
1407   // Inputs:
1408   //   c_rarg0   - source array address
1409   //   c_rarg1   - destination array address
1410   //   c_rarg2   - element count, treated as ssize_t, can be zero
1411   //
1412   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1413   // we let the hardware handle it.  The one to eight bytes within words,
1414   // dwords or qwords that span cache line boundaries will still be loaded
1415   // and stored atomically.
1416   //
1417   // Side Effects:
1418   //   disjoint_byte_copy_entry is set to the no-overlap entry point
1419   //   used by generate_conjoint_byte_copy().
1420   //
1421   address generate_disjoint_byte_copy(bool aligned, address* entry, const char *name) {
1422     __ align(CodeEntryAlignment);
1423     StubCodeMark mark(this, "StubRoutines", name);
1424     address start = __ pc();
1425 
1426     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1427     Label L_copy_byte, L_exit;
1428     const Register from        = rdi;  // source array address
1429     const Register to          = rsi;  // destination array address
1430     const Register count       = rdx;  // elements count
1431     const Register byte_count  = rcx;
1432     const Register qword_count = count;
1433     const Register end_from    = from; // source array end address
1434     const Register end_to      = to;   // destination array end address
1435     // End pointers are inclusive, and if count is not zero they point
1436     // to the last unit copied:  end_to[0] := end_from[0]
1437 
1438     __ enter(); // required for proper stackwalking of RuntimeStub frame
1439     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1440 
1441     if (entry != NULL) {
1442       *entry = __ pc();
1443        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1444       BLOCK_COMMENT("Entry:");
1445     }
1446 
1447     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1448                       // r9 and r10 may be used to save non-volatile registers
1449 
1450     // 'from', 'to' and 'count' are now valid
1451     __ movptr(byte_count, count);
1452     __ shrptr(count, 3); // count => qword_count
1453 
1454     // Copy from low to high addresses.  Use 'to' as scratch.
1455     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1456     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1457     __ negptr(qword_count); // make the count negative
1458     __ jmp(L_copy_bytes);
1459 
1460     // Copy trailing qwords
1461   __ BIND(L_copy_8_bytes);
1462     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1463     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1464     __ increment(qword_count);
1465     __ jcc(Assembler::notZero, L_copy_8_bytes);
1466 
1467     // Check for and copy trailing dword
1468   __ BIND(L_copy_4_bytes);
1469     __ testl(byte_count, 4);
1470     __ jccb(Assembler::zero, L_copy_2_bytes);
1471     __ movl(rax, Address(end_from, 8));
1472     __ movl(Address(end_to, 8), rax);
1473 
1474     __ addptr(end_from, 4);
1475     __ addptr(end_to, 4);
1476 
1477     // Check for and copy trailing word
1478   __ BIND(L_copy_2_bytes);
1479     __ testl(byte_count, 2);
1480     __ jccb(Assembler::zero, L_copy_byte);
1481     __ movw(rax, Address(end_from, 8));
1482     __ movw(Address(end_to, 8), rax);
1483 
1484     __ addptr(end_from, 2);
1485     __ addptr(end_to, 2);
1486 
1487     // Check for and copy trailing byte
1488   __ BIND(L_copy_byte);
1489     __ testl(byte_count, 1);
1490     __ jccb(Assembler::zero, L_exit);
1491     __ movb(rax, Address(end_from, 8));
1492     __ movb(Address(end_to, 8), rax);
1493 
1494   __ BIND(L_exit);
1495     restore_arg_regs();
1496     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1497     __ xorptr(rax, rax); // return 0
1498     __ vzeroupper();
1499     __ leave(); // required for proper stackwalking of RuntimeStub frame
1500     __ ret(0);
1501 
1502     // Copy in multi-bytes chunks
1503     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1504     __ jmp(L_copy_4_bytes);
1505 
1506     return start;
1507   }
1508 
1509   // Arguments:
1510   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1511   //             ignored
1512   //   name    - stub name string
1513   //
1514   // Inputs:
1515   //   c_rarg0   - source array address
1516   //   c_rarg1   - destination array address
1517   //   c_rarg2   - element count, treated as ssize_t, can be zero
1518   //
1519   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1520   // we let the hardware handle it.  The one to eight bytes within words,
1521   // dwords or qwords that span cache line boundaries will still be loaded
1522   // and stored atomically.
1523   //
1524   address generate_conjoint_byte_copy(bool aligned, address nooverlap_target,
1525                                       address* entry, const char *name) {
1526     __ align(CodeEntryAlignment);
1527     StubCodeMark mark(this, "StubRoutines", name);
1528     address start = __ pc();
1529 
1530     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1531     const Register from        = rdi;  // source array address
1532     const Register to          = rsi;  // destination array address
1533     const Register count       = rdx;  // elements count
1534     const Register byte_count  = rcx;
1535     const Register qword_count = count;
1536 
1537     __ enter(); // required for proper stackwalking of RuntimeStub frame
1538     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1539 
1540     if (entry != NULL) {
1541       *entry = __ pc();
1542       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1543       BLOCK_COMMENT("Entry:");
1544     }
1545 
1546     array_overlap_test(nooverlap_target, Address::times_1);
1547     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1548                       // r9 and r10 may be used to save non-volatile registers
1549 
1550     // 'from', 'to' and 'count' are now valid
1551     __ movptr(byte_count, count);
1552     __ shrptr(count, 3);   // count => qword_count
1553 
1554     // Copy from high to low addresses.
1555 
1556     // Check for and copy trailing byte
1557     __ testl(byte_count, 1);
1558     __ jcc(Assembler::zero, L_copy_2_bytes);
1559     __ movb(rax, Address(from, byte_count, Address::times_1, -1));
1560     __ movb(Address(to, byte_count, Address::times_1, -1), rax);
1561     __ decrement(byte_count); // Adjust for possible trailing word
1562 
1563     // Check for and copy trailing word
1564   __ BIND(L_copy_2_bytes);
1565     __ testl(byte_count, 2);
1566     __ jcc(Assembler::zero, L_copy_4_bytes);
1567     __ movw(rax, Address(from, byte_count, Address::times_1, -2));
1568     __ movw(Address(to, byte_count, Address::times_1, -2), rax);
1569 
1570     // Check for and copy trailing dword
1571   __ BIND(L_copy_4_bytes);
1572     __ testl(byte_count, 4);
1573     __ jcc(Assembler::zero, L_copy_bytes);
1574     __ movl(rax, Address(from, qword_count, Address::times_8));
1575     __ movl(Address(to, qword_count, Address::times_8), rax);
1576     __ jmp(L_copy_bytes);
1577 
1578     // Copy trailing qwords
1579   __ BIND(L_copy_8_bytes);
1580     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1581     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1582     __ decrement(qword_count);
1583     __ jcc(Assembler::notZero, L_copy_8_bytes);
1584 
1585     restore_arg_regs();
1586     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1587     __ xorptr(rax, rax); // return 0
1588     __ vzeroupper();
1589     __ leave(); // required for proper stackwalking of RuntimeStub frame
1590     __ ret(0);
1591 
1592     // Copy in multi-bytes chunks
1593     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1594 
1595     restore_arg_regs();
1596     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1597     __ xorptr(rax, rax); // return 0
1598     __ vzeroupper();
1599     __ leave(); // required for proper stackwalking of RuntimeStub frame
1600     __ ret(0);
1601 
1602     return start;
1603   }
1604 
1605   // Arguments:
1606   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1607   //             ignored
1608   //   name    - stub name string
1609   //
1610   // Inputs:
1611   //   c_rarg0   - source array address
1612   //   c_rarg1   - destination array address
1613   //   c_rarg2   - element count, treated as ssize_t, can be zero
1614   //
1615   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1616   // let the hardware handle it.  The two or four words within dwords
1617   // or qwords that span cache line boundaries will still be loaded
1618   // and stored atomically.
1619   //
1620   // Side Effects:
1621   //   disjoint_short_copy_entry is set to the no-overlap entry point
1622   //   used by generate_conjoint_short_copy().
1623   //
1624   address generate_disjoint_short_copy(bool aligned, address *entry, const char *name) {
1625     __ align(CodeEntryAlignment);
1626     StubCodeMark mark(this, "StubRoutines", name);
1627     address start = __ pc();
1628 
1629     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes,L_copy_2_bytes,L_exit;
1630     const Register from        = rdi;  // source array address
1631     const Register to          = rsi;  // destination array address
1632     const Register count       = rdx;  // elements count
1633     const Register word_count  = rcx;
1634     const Register qword_count = count;
1635     const Register end_from    = from; // source array end address
1636     const Register end_to      = to;   // destination array end address
1637     // End pointers are inclusive, and if count is not zero they point
1638     // to the last unit copied:  end_to[0] := end_from[0]
1639 
1640     __ enter(); // required for proper stackwalking of RuntimeStub frame
1641     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1642 
1643     if (entry != NULL) {
1644       *entry = __ pc();
1645       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1646       BLOCK_COMMENT("Entry:");
1647     }
1648 
1649     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1650                       // r9 and r10 may be used to save non-volatile registers
1651 
1652     // 'from', 'to' and 'count' are now valid
1653     __ movptr(word_count, count);
1654     __ shrptr(count, 2); // count => qword_count
1655 
1656     // Copy from low to high addresses.  Use 'to' as scratch.
1657     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1658     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1659     __ negptr(qword_count);
1660     __ jmp(L_copy_bytes);
1661 
1662     // Copy trailing qwords
1663   __ BIND(L_copy_8_bytes);
1664     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1665     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1666     __ increment(qword_count);
1667     __ jcc(Assembler::notZero, L_copy_8_bytes);
1668 
1669     // Original 'dest' is trashed, so we can't use it as a
1670     // base register for a possible trailing word copy
1671 
1672     // Check for and copy trailing dword
1673   __ BIND(L_copy_4_bytes);
1674     __ testl(word_count, 2);
1675     __ jccb(Assembler::zero, L_copy_2_bytes);
1676     __ movl(rax, Address(end_from, 8));
1677     __ movl(Address(end_to, 8), rax);
1678 
1679     __ addptr(end_from, 4);
1680     __ addptr(end_to, 4);
1681 
1682     // Check for and copy trailing word
1683   __ BIND(L_copy_2_bytes);
1684     __ testl(word_count, 1);
1685     __ jccb(Assembler::zero, L_exit);
1686     __ movw(rax, Address(end_from, 8));
1687     __ movw(Address(end_to, 8), rax);
1688 
1689   __ BIND(L_exit);
1690     restore_arg_regs();
1691     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1692     __ xorptr(rax, rax); // return 0
1693     __ vzeroupper();
1694     __ leave(); // required for proper stackwalking of RuntimeStub frame
1695     __ ret(0);
1696 
1697     // Copy in multi-bytes chunks
1698     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1699     __ jmp(L_copy_4_bytes);
1700 
1701     return start;
1702   }
1703 
1704   address generate_fill(BasicType t, bool aligned, const char *name) {
1705     __ align(CodeEntryAlignment);
1706     StubCodeMark mark(this, "StubRoutines", name);
1707     address start = __ pc();
1708 
1709     BLOCK_COMMENT("Entry:");
1710 
1711     const Register to       = c_rarg0;  // source array address
1712     const Register value    = c_rarg1;  // value
1713     const Register count    = c_rarg2;  // elements count
1714 
1715     __ enter(); // required for proper stackwalking of RuntimeStub frame
1716 
1717     __ generate_fill(t, aligned, to, value, count, rax, xmm0);
1718 
1719     __ vzeroupper();
1720     __ leave(); // required for proper stackwalking of RuntimeStub frame
1721     __ ret(0);
1722     return start;
1723   }
1724 
1725   // Arguments:
1726   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1727   //             ignored
1728   //   name    - stub name string
1729   //
1730   // Inputs:
1731   //   c_rarg0   - source array address
1732   //   c_rarg1   - destination array address
1733   //   c_rarg2   - element count, treated as ssize_t, can be zero
1734   //
1735   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1736   // let the hardware handle it.  The two or four words within dwords
1737   // or qwords that span cache line boundaries will still be loaded
1738   // and stored atomically.
1739   //
1740   address generate_conjoint_short_copy(bool aligned, address nooverlap_target,
1741                                        address *entry, const char *name) {
1742     __ align(CodeEntryAlignment);
1743     StubCodeMark mark(this, "StubRoutines", name);
1744     address start = __ pc();
1745 
1746     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes;
1747     const Register from        = rdi;  // source array address
1748     const Register to          = rsi;  // destination array address
1749     const Register count       = rdx;  // elements count
1750     const Register word_count  = rcx;
1751     const Register qword_count = count;
1752 
1753     __ enter(); // required for proper stackwalking of RuntimeStub frame
1754     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1755 
1756     if (entry != NULL) {
1757       *entry = __ pc();
1758       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1759       BLOCK_COMMENT("Entry:");
1760     }
1761 
1762     array_overlap_test(nooverlap_target, Address::times_2);
1763     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1764                       // r9 and r10 may be used to save non-volatile registers
1765 
1766     // 'from', 'to' and 'count' are now valid
1767     __ movptr(word_count, count);
1768     __ shrptr(count, 2); // count => qword_count
1769 
1770     // Copy from high to low addresses.  Use 'to' as scratch.
1771 
1772     // Check for and copy trailing word
1773     __ testl(word_count, 1);
1774     __ jccb(Assembler::zero, L_copy_4_bytes);
1775     __ movw(rax, Address(from, word_count, Address::times_2, -2));
1776     __ movw(Address(to, word_count, Address::times_2, -2), rax);
1777 
1778     // Check for and copy trailing dword
1779   __ BIND(L_copy_4_bytes);
1780     __ testl(word_count, 2);
1781     __ jcc(Assembler::zero, L_copy_bytes);
1782     __ movl(rax, Address(from, qword_count, Address::times_8));
1783     __ movl(Address(to, qword_count, Address::times_8), rax);
1784     __ jmp(L_copy_bytes);
1785 
1786     // Copy trailing qwords
1787   __ BIND(L_copy_8_bytes);
1788     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1789     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1790     __ decrement(qword_count);
1791     __ jcc(Assembler::notZero, L_copy_8_bytes);
1792 
1793     restore_arg_regs();
1794     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1795     __ xorptr(rax, rax); // return 0
1796     __ vzeroupper();
1797     __ leave(); // required for proper stackwalking of RuntimeStub frame
1798     __ ret(0);
1799 
1800     // Copy in multi-bytes chunks
1801     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1802 
1803     restore_arg_regs();
1804     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1805     __ xorptr(rax, rax); // return 0
1806     __ vzeroupper();
1807     __ leave(); // required for proper stackwalking of RuntimeStub frame
1808     __ ret(0);
1809 
1810     return start;
1811   }
1812 
1813   // Arguments:
1814   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1815   //             ignored
1816   //   is_oop  - true => oop array, so generate store check code
1817   //   name    - stub name string
1818   //
1819   // Inputs:
1820   //   c_rarg0   - source array address
1821   //   c_rarg1   - destination array address
1822   //   c_rarg2   - element count, treated as ssize_t, can be zero
1823   //
1824   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1825   // the hardware handle it.  The two dwords within qwords that span
1826   // cache line boundaries will still be loaded and stored atomicly.
1827   //
1828   // Side Effects:
1829   //   disjoint_int_copy_entry is set to the no-overlap entry point
1830   //   used by generate_conjoint_int_oop_copy().
1831   //
1832   address generate_disjoint_int_oop_copy(bool aligned, bool is_oop, address* entry,
1833                                          const char *name, bool dest_uninitialized = false) {
1834     __ align(CodeEntryAlignment);
1835     StubCodeMark mark(this, "StubRoutines", name);
1836     address start = __ pc();
1837 
1838     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_exit;
1839     const Register from        = rdi;  // source array address
1840     const Register to          = rsi;  // destination array address
1841     const Register count       = rdx;  // elements count
1842     const Register dword_count = rcx;
1843     const Register qword_count = count;
1844     const Register end_from    = from; // source array end address
1845     const Register end_to      = to;   // destination array end address
1846     // End pointers are inclusive, and if count is not zero they point
1847     // to the last unit copied:  end_to[0] := end_from[0]
1848 
1849     __ enter(); // required for proper stackwalking of RuntimeStub frame
1850     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1851 
1852     if (entry != NULL) {
1853       *entry = __ pc();
1854       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1855       BLOCK_COMMENT("Entry:");
1856     }
1857 
1858     setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
1859                                    // r9 is used to save r15_thread
1860 
1861     DecoratorSet decorators = IN_HEAP | IS_ARRAY | ARRAYCOPY_DISJOINT;
1862     if (dest_uninitialized) {
1863       decorators |= IS_DEST_UNINITIALIZED;
1864     }
1865     if (aligned) {
1866       decorators |= ARRAYCOPY_ALIGNED;
1867     }
1868 
1869     BasicType type = is_oop ? T_OBJECT : T_INT;
1870     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
1871     bs->arraycopy_prologue(_masm, decorators, type, from, to, count);
1872 
1873     // 'from', 'to' and 'count' are now valid
1874     __ movptr(dword_count, count);
1875     __ shrptr(count, 1); // count => qword_count
1876 
1877     // Copy from low to high addresses.  Use 'to' as scratch.
1878     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1879     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1880     __ negptr(qword_count);
1881     __ jmp(L_copy_bytes);
1882 
1883     // Copy trailing qwords
1884   __ BIND(L_copy_8_bytes);
1885     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1886     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1887     __ increment(qword_count);
1888     __ jcc(Assembler::notZero, L_copy_8_bytes);
1889 
1890     // Check for and copy trailing dword
1891   __ BIND(L_copy_4_bytes);
1892     __ testl(dword_count, 1); // Only byte test since the value is 0 or 1
1893     __ jccb(Assembler::zero, L_exit);
1894     __ movl(rax, Address(end_from, 8));
1895     __ movl(Address(end_to, 8), rax);
1896 
1897   __ BIND(L_exit);
1898     bs->arraycopy_epilogue(_masm, decorators, type, from, to, dword_count);
1899     restore_arg_regs_using_thread();
1900     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
1901     __ vzeroupper();
1902     __ xorptr(rax, rax); // return 0
1903     __ leave(); // required for proper stackwalking of RuntimeStub frame
1904     __ ret(0);
1905 
1906     // Copy in multi-bytes chunks
1907     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1908     __ jmp(L_copy_4_bytes);
1909 
1910     return start;
1911   }
1912 
1913   // Arguments:
1914   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1915   //             ignored
1916   //   is_oop  - true => oop array, so generate store check code
1917   //   name    - stub name string
1918   //
1919   // Inputs:
1920   //   c_rarg0   - source array address
1921   //   c_rarg1   - destination array address
1922   //   c_rarg2   - element count, treated as ssize_t, can be zero
1923   //
1924   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1925   // the hardware handle it.  The two dwords within qwords that span
1926   // cache line boundaries will still be loaded and stored atomicly.
1927   //
1928   address generate_conjoint_int_oop_copy(bool aligned, bool is_oop, address nooverlap_target,
1929                                          address *entry, const char *name,
1930                                          bool dest_uninitialized = false) {
1931     __ align(CodeEntryAlignment);
1932     StubCodeMark mark(this, "StubRoutines", name);
1933     address start = __ pc();
1934 
1935     Label L_copy_bytes, L_copy_8_bytes, L_exit;
1936     const Register from        = rdi;  // source array address
1937     const Register to          = rsi;  // destination array address
1938     const Register count       = rdx;  // elements count
1939     const Register dword_count = rcx;
1940     const Register qword_count = count;
1941 
1942     __ enter(); // required for proper stackwalking of RuntimeStub frame
1943     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1944 
1945     if (entry != NULL) {
1946       *entry = __ pc();
1947        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1948       BLOCK_COMMENT("Entry:");
1949     }
1950 
1951     array_overlap_test(nooverlap_target, Address::times_4);
1952     setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
1953                                    // r9 is used to save r15_thread
1954 
1955     DecoratorSet decorators = IN_HEAP | IS_ARRAY;
1956     if (dest_uninitialized) {
1957       decorators |= IS_DEST_UNINITIALIZED;
1958     }
1959     if (aligned) {
1960       decorators |= ARRAYCOPY_ALIGNED;
1961     }
1962 
1963     BasicType type = is_oop ? T_OBJECT : T_INT;
1964     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
1965     // no registers are destroyed by this call
1966     bs->arraycopy_prologue(_masm, decorators, type, from, to, count);
1967 
1968     assert_clean_int(count, rax); // Make sure 'count' is clean int.
1969     // 'from', 'to' and 'count' are now valid
1970     __ movptr(dword_count, count);
1971     __ shrptr(count, 1); // count => qword_count
1972 
1973     // Copy from high to low addresses.  Use 'to' as scratch.
1974 
1975     // Check for and copy trailing dword
1976     __ testl(dword_count, 1);
1977     __ jcc(Assembler::zero, L_copy_bytes);
1978     __ movl(rax, Address(from, dword_count, Address::times_4, -4));
1979     __ movl(Address(to, dword_count, Address::times_4, -4), rax);
1980     __ jmp(L_copy_bytes);
1981 
1982     // Copy trailing qwords
1983   __ BIND(L_copy_8_bytes);
1984     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1985     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1986     __ decrement(qword_count);
1987     __ jcc(Assembler::notZero, L_copy_8_bytes);
1988 
1989     if (is_oop) {
1990       __ jmp(L_exit);
1991     }
1992     restore_arg_regs_using_thread();
1993     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
1994     __ xorptr(rax, rax); // return 0
1995     __ vzeroupper();
1996     __ leave(); // required for proper stackwalking of RuntimeStub frame
1997     __ ret(0);
1998 
1999     // Copy in multi-bytes chunks
2000     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2001 
2002   __ BIND(L_exit);
2003     bs->arraycopy_epilogue(_masm, decorators, type, from, to, dword_count);
2004     restore_arg_regs_using_thread();
2005     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
2006     __ xorptr(rax, rax); // return 0
2007     __ vzeroupper();
2008     __ leave(); // required for proper stackwalking of RuntimeStub frame
2009     __ ret(0);
2010 
2011     return start;
2012   }
2013 
2014   // Arguments:
2015   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2016   //             ignored
2017   //   is_oop  - true => oop array, so generate store check code
2018   //   name    - stub name string
2019   //
2020   // Inputs:
2021   //   c_rarg0   - source array address
2022   //   c_rarg1   - destination array address
2023   //   c_rarg2   - element count, treated as ssize_t, can be zero
2024   //
2025  // Side Effects:
2026   //   disjoint_oop_copy_entry or disjoint_long_copy_entry is set to the
2027   //   no-overlap entry point used by generate_conjoint_long_oop_copy().
2028   //
2029   address generate_disjoint_long_oop_copy(bool aligned, bool is_oop, address *entry,
2030                                           const char *name, bool dest_uninitialized = false) {
2031     __ align(CodeEntryAlignment);
2032     StubCodeMark mark(this, "StubRoutines", name);
2033     address start = __ pc();
2034 
2035     Label L_copy_bytes, L_copy_8_bytes, L_exit;
2036     const Register from        = rdi;  // source array address
2037     const Register to          = rsi;  // destination array address
2038     const Register qword_count = rdx;  // elements count
2039     const Register end_from    = from; // source array end address
2040     const Register end_to      = rcx;  // destination array end address
2041     const Register saved_count = r11;
2042     // End pointers are inclusive, and if count is not zero they point
2043     // to the last unit copied:  end_to[0] := end_from[0]
2044 
2045     __ enter(); // required for proper stackwalking of RuntimeStub frame
2046     // Save no-overlap entry point for generate_conjoint_long_oop_copy()
2047     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2048 
2049     if (entry != NULL) {
2050       *entry = __ pc();
2051       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2052       BLOCK_COMMENT("Entry:");
2053     }
2054 
2055     setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
2056                                      // r9 is used to save r15_thread
2057     // 'from', 'to' and 'qword_count' are now valid
2058 
2059     DecoratorSet decorators = IN_HEAP | IS_ARRAY | ARRAYCOPY_DISJOINT;
2060     if (dest_uninitialized) {
2061       decorators |= IS_DEST_UNINITIALIZED;
2062     }
2063     if (aligned) {
2064       decorators |= ARRAYCOPY_ALIGNED;
2065     }
2066 
2067     BasicType type = is_oop ? T_OBJECT : T_LONG;
2068     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
2069     bs->arraycopy_prologue(_masm, decorators, type, from, to, qword_count);
2070 
2071     // Copy from low to high addresses.  Use 'to' as scratch.
2072     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
2073     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
2074     __ negptr(qword_count);
2075     __ jmp(L_copy_bytes);
2076 
2077     // Copy trailing qwords
2078   __ BIND(L_copy_8_bytes);
2079     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
2080     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
2081     __ increment(qword_count);
2082     __ jcc(Assembler::notZero, L_copy_8_bytes);
2083 
2084     if (is_oop) {
2085       __ jmp(L_exit);
2086     } else {
2087       restore_arg_regs_using_thread();
2088       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2089       __ xorptr(rax, rax); // return 0
2090       __ vzeroupper();
2091       __ leave(); // required for proper stackwalking of RuntimeStub frame
2092       __ ret(0);
2093     }
2094 
2095     // Copy in multi-bytes chunks
2096     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2097 
2098     __ BIND(L_exit);
2099     bs->arraycopy_epilogue(_masm, decorators, type, from, to, qword_count);
2100     restore_arg_regs_using_thread();
2101     if (is_oop) {
2102       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
2103     } else {
2104       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2105     }
2106     __ vzeroupper();
2107     __ xorptr(rax, rax); // return 0
2108     __ leave(); // required for proper stackwalking of RuntimeStub frame
2109     __ ret(0);
2110 
2111     return start;
2112   }
2113 
2114   // Arguments:
2115   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2116   //             ignored
2117   //   is_oop  - true => oop array, so generate store check code
2118   //   name    - stub name string
2119   //
2120   // Inputs:
2121   //   c_rarg0   - source array address
2122   //   c_rarg1   - destination array address
2123   //   c_rarg2   - element count, treated as ssize_t, can be zero
2124   //
2125   address generate_conjoint_long_oop_copy(bool aligned, bool is_oop,
2126                                           address nooverlap_target, address *entry,
2127                                           const char *name, bool dest_uninitialized = false) {
2128     __ align(CodeEntryAlignment);
2129     StubCodeMark mark(this, "StubRoutines", name);
2130     address start = __ pc();
2131 
2132     Label L_copy_bytes, L_copy_8_bytes, L_exit;
2133     const Register from        = rdi;  // source array address
2134     const Register to          = rsi;  // destination array address
2135     const Register qword_count = rdx;  // elements count
2136     const Register saved_count = rcx;
2137 
2138     __ enter(); // required for proper stackwalking of RuntimeStub frame
2139     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2140 
2141     if (entry != NULL) {
2142       *entry = __ pc();
2143       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2144       BLOCK_COMMENT("Entry:");
2145     }
2146 
2147     array_overlap_test(nooverlap_target, Address::times_8);
2148     setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
2149                                    // r9 is used to save r15_thread
2150     // 'from', 'to' and 'qword_count' are now valid
2151 
2152     DecoratorSet decorators = IN_HEAP | IS_ARRAY;
2153     if (dest_uninitialized) {
2154       decorators |= IS_DEST_UNINITIALIZED;
2155     }
2156     if (aligned) {
2157       decorators |= ARRAYCOPY_ALIGNED;
2158     }
2159 
2160     BasicType type = is_oop ? T_OBJECT : T_LONG;
2161     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
2162     bs->arraycopy_prologue(_masm, decorators, type, from, to, qword_count);
2163 
2164     __ jmp(L_copy_bytes);
2165 
2166     // Copy trailing qwords
2167   __ BIND(L_copy_8_bytes);
2168     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
2169     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
2170     __ decrement(qword_count);
2171     __ jcc(Assembler::notZero, L_copy_8_bytes);
2172 
2173     if (is_oop) {
2174       __ jmp(L_exit);
2175     } else {
2176       restore_arg_regs_using_thread();
2177       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2178       __ xorptr(rax, rax); // return 0
2179       __ vzeroupper();
2180       __ leave(); // required for proper stackwalking of RuntimeStub frame
2181       __ ret(0);
2182     }
2183 
2184     // Copy in multi-bytes chunks
2185     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2186 
2187     __ BIND(L_exit);
2188     bs->arraycopy_epilogue(_masm, decorators, type, from, to, qword_count);
2189     restore_arg_regs_using_thread();
2190     if (is_oop) {
2191       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
2192     } else {
2193       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2194     }
2195     __ vzeroupper();
2196     __ xorptr(rax, rax); // return 0
2197     __ leave(); // required for proper stackwalking of RuntimeStub frame
2198     __ ret(0);
2199 
2200     return start;
2201   }
2202 
2203 
2204   // Helper for generating a dynamic type check.
2205   // Smashes no registers.
2206   void generate_type_check(Register sub_klass,
2207                            Register super_check_offset,
2208                            Register super_klass,
2209                            Label& L_success) {
2210     assert_different_registers(sub_klass, super_check_offset, super_klass);
2211 
2212     BLOCK_COMMENT("type_check:");
2213 
2214     Label L_miss;
2215 
2216     __ check_klass_subtype_fast_path(sub_klass, super_klass, noreg,        &L_success, &L_miss, NULL,
2217                                      super_check_offset);
2218     __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg, &L_success, NULL);
2219 
2220     // Fall through on failure!
2221     __ BIND(L_miss);
2222   }
2223 
2224   //
2225   //  Generate checkcasting array copy stub
2226   //
2227   //  Input:
2228   //    c_rarg0   - source array address
2229   //    c_rarg1   - destination array address
2230   //    c_rarg2   - element count, treated as ssize_t, can be zero
2231   //    c_rarg3   - size_t ckoff (super_check_offset)
2232   // not Win64
2233   //    c_rarg4   - oop ckval (super_klass)
2234   // Win64
2235   //    rsp+40    - oop ckval (super_klass)
2236   //
2237   //  Output:
2238   //    rax ==  0  -  success
2239   //    rax == -1^K - failure, where K is partial transfer count
2240   //
2241   address generate_checkcast_copy(const char *name, address *entry,
2242                                   bool dest_uninitialized = false) {
2243 
2244     Label L_load_element, L_store_element, L_do_card_marks, L_done;
2245 
2246     // Input registers (after setup_arg_regs)
2247     const Register from        = rdi;   // source array address
2248     const Register to          = rsi;   // destination array address
2249     const Register length      = rdx;   // elements count
2250     const Register ckoff       = rcx;   // super_check_offset
2251     const Register ckval       = r8;    // super_klass
2252 
2253     // Registers used as temps (r13, r14 are save-on-entry)
2254     const Register end_from    = from;  // source array end address
2255     const Register end_to      = r13;   // destination array end address
2256     const Register count       = rdx;   // -(count_remaining)
2257     const Register r14_length  = r14;   // saved copy of length
2258     // End pointers are inclusive, and if length is not zero they point
2259     // to the last unit copied:  end_to[0] := end_from[0]
2260 
2261     const Register rax_oop    = rax;    // actual oop copied
2262     const Register r11_klass  = r11;    // oop._klass
2263 
2264     //---------------------------------------------------------------
2265     // Assembler stub will be used for this call to arraycopy
2266     // if the two arrays are subtypes of Object[] but the
2267     // destination array type is not equal to or a supertype
2268     // of the source type.  Each element must be separately
2269     // checked.
2270 
2271     __ align(CodeEntryAlignment);
2272     StubCodeMark mark(this, "StubRoutines", name);
2273     address start = __ pc();
2274 
2275     __ enter(); // required for proper stackwalking of RuntimeStub frame
2276 
2277 #ifdef ASSERT
2278     // caller guarantees that the arrays really are different
2279     // otherwise, we would have to make conjoint checks
2280     { Label L;
2281       array_overlap_test(L, TIMES_OOP);
2282       __ stop("checkcast_copy within a single array");
2283       __ bind(L);
2284     }
2285 #endif //ASSERT
2286 
2287     setup_arg_regs(4); // from => rdi, to => rsi, length => rdx
2288                        // ckoff => rcx, ckval => r8
2289                        // r9 and r10 may be used to save non-volatile registers
2290 #ifdef _WIN64
2291     // last argument (#4) is on stack on Win64
2292     __ movptr(ckval, Address(rsp, 6 * wordSize));
2293 #endif
2294 
2295     // Caller of this entry point must set up the argument registers.
2296     if (entry != NULL) {
2297       *entry = __ pc();
2298       BLOCK_COMMENT("Entry:");
2299     }
2300 
2301     // allocate spill slots for r13, r14
2302     enum {
2303       saved_r13_offset,
2304       saved_r14_offset,
2305       saved_r10_offset,
2306       saved_rbp_offset
2307     };
2308     __ subptr(rsp, saved_rbp_offset * wordSize);
2309     __ movptr(Address(rsp, saved_r13_offset * wordSize), r13);
2310     __ movptr(Address(rsp, saved_r14_offset * wordSize), r14);
2311     __ movptr(Address(rsp, saved_r10_offset * wordSize), r10);
2312 
2313 #ifdef ASSERT
2314       Label L2;
2315       __ get_thread(r14);
2316       __ cmpptr(r15_thread, r14);
2317       __ jcc(Assembler::equal, L2);
2318       __ stop("StubRoutines::call_stub: r15_thread is modified by call");
2319       __ bind(L2);
2320 #endif // ASSERT
2321 
2322     // check that int operands are properly extended to size_t
2323     assert_clean_int(length, rax);
2324     assert_clean_int(ckoff, rax);
2325 
2326 #ifdef ASSERT
2327     BLOCK_COMMENT("assert consistent ckoff/ckval");
2328     // The ckoff and ckval must be mutually consistent,
2329     // even though caller generates both.
2330     { Label L;
2331       int sco_offset = in_bytes(Klass::super_check_offset_offset());
2332       __ cmpl(ckoff, Address(ckval, sco_offset));
2333       __ jcc(Assembler::equal, L);
2334       __ stop("super_check_offset inconsistent");
2335       __ bind(L);
2336     }
2337 #endif //ASSERT
2338 
2339     // Loop-invariant addresses.  They are exclusive end pointers.
2340     Address end_from_addr(from, length, TIMES_OOP, 0);
2341     Address   end_to_addr(to,   length, TIMES_OOP, 0);
2342     // Loop-variant addresses.  They assume post-incremented count < 0.
2343     Address from_element_addr(end_from, count, TIMES_OOP, 0);
2344     Address   to_element_addr(end_to,   count, TIMES_OOP, 0);
2345 
2346     // Note: checkcast arraycopy is always disjoint. If it were not, then we wouldn't
2347     // need to checkcast.
2348     DecoratorSet decorators = IN_HEAP | IS_ARRAY | ARRAYCOPY_CHECKCAST | ARRAYCOPY_DISJOINT;
2349     if (dest_uninitialized) {
2350       decorators |= IS_DEST_UNINITIALIZED;
2351     }
2352 
2353     BasicType type = T_OBJECT;
2354     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
2355     bs->arraycopy_prologue(_masm, decorators, type, from, to, count);
2356 
2357     // Copy from low to high addresses, indexed from the end of each array.
2358     __ lea(end_from, end_from_addr);
2359     __ lea(end_to,   end_to_addr);
2360     __ movptr(r14_length, length);        // save a copy of the length
2361     assert(length == count, "");          // else fix next line:
2362     __ negptr(count);                     // negate and test the length
2363     __ jcc(Assembler::notZero, L_load_element);
2364 
2365     // Empty array:  Nothing to do.
2366     __ xorptr(rax, rax);                  // return 0 on (trivial) success
2367     __ jmp(L_done);
2368 
2369     // ======== begin loop ========
2370     // (Loop is rotated; its entry is L_load_element.)
2371     // Loop control:
2372     //   for (count = -count; count != 0; count++)
2373     // Base pointers src, dst are biased by 8*(count-1),to last element.
2374     __ align(OptoLoopAlignment);
2375 
2376     __ BIND(L_store_element);
2377     __ store_heap_oop(to_element_addr, rax_oop, noreg, noreg, AS_RAW);  // store the oop
2378     __ increment(count);               // increment the count toward zero
2379     __ jcc(Assembler::zero, L_do_card_marks);
2380 
2381     // ======== loop entry is here ========
2382     __ BIND(L_load_element);
2383     __ load_heap_oop(rax_oop, from_element_addr, noreg, noreg, AS_RAW); // load the oop
2384     __ testptr(rax_oop, rax_oop);
2385     __ jcc(Assembler::zero, L_store_element);
2386 
2387     __ load_klass(r11_klass, rax_oop);// query the object klass
2388     generate_type_check(r11_klass, ckoff, ckval, L_store_element);
2389     // ======== end loop ========
2390 
2391     // It was a real error; we must depend on the caller to finish the job.
2392     // Register rdx = -1 * number of *remaining* oops, r14 = *total* oops.
2393     // Emit GC store barriers for the oops we have copied (r14 + rdx),
2394     // and report their number to the caller.
2395     assert_different_registers(rax, r14_length, count, to, end_to, rcx, rscratch1);
2396     Label L_post_barrier;
2397     __ addptr(r14_length, count);     // K = (original - remaining) oops
2398     __ movptr(rax, r14_length);       // save the value
2399     __ notptr(rax);                   // report (-1^K) to caller (does not affect flags)
2400     __ jccb(Assembler::notZero, L_post_barrier);
2401     __ jmp(L_done); // K == 0, nothing was copied, skip post barrier
2402 
2403     // Come here on success only.
2404     __ BIND(L_do_card_marks);
2405     __ xorptr(rax, rax);              // return 0 on success
2406 
2407     __ BIND(L_post_barrier);
2408     bs->arraycopy_epilogue(_masm, decorators, type, from, to, r14_length);
2409 
2410     // Common exit point (success or failure).
2411     __ BIND(L_done);
2412     __ movptr(r13, Address(rsp, saved_r13_offset * wordSize));
2413     __ movptr(r14, Address(rsp, saved_r14_offset * wordSize));
2414     __ movptr(r10, Address(rsp, saved_r10_offset * wordSize));
2415     restore_arg_regs();
2416     inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr); // Update counter after rscratch1 is free
2417     __ leave(); // required for proper stackwalking of RuntimeStub frame
2418     __ ret(0);
2419 
2420     return start;
2421   }
2422 
2423   //
2424   //  Generate 'unsafe' array copy stub
2425   //  Though just as safe as the other stubs, it takes an unscaled
2426   //  size_t argument instead of an element count.
2427   //
2428   //  Input:
2429   //    c_rarg0   - source array address
2430   //    c_rarg1   - destination array address
2431   //    c_rarg2   - byte count, treated as ssize_t, can be zero
2432   //
2433   // Examines the alignment of the operands and dispatches
2434   // to a long, int, short, or byte copy loop.
2435   //
2436   address generate_unsafe_copy(const char *name,
2437                                address byte_copy_entry, address short_copy_entry,
2438                                address int_copy_entry, address long_copy_entry) {
2439 
2440     Label L_long_aligned, L_int_aligned, L_short_aligned;
2441 
2442     // Input registers (before setup_arg_regs)
2443     const Register from        = c_rarg0;  // source array address
2444     const Register to          = c_rarg1;  // destination array address
2445     const Register size        = c_rarg2;  // byte count (size_t)
2446 
2447     // Register used as a temp
2448     const Register bits        = rax;      // test copy of low bits
2449 
2450     __ align(CodeEntryAlignment);
2451     StubCodeMark mark(this, "StubRoutines", name);
2452     address start = __ pc();
2453 
2454     __ enter(); // required for proper stackwalking of RuntimeStub frame
2455 
2456     // bump this on entry, not on exit:
2457     inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
2458 
2459     __ mov(bits, from);
2460     __ orptr(bits, to);
2461     __ orptr(bits, size);
2462 
2463     __ testb(bits, BytesPerLong-1);
2464     __ jccb(Assembler::zero, L_long_aligned);
2465 
2466     __ testb(bits, BytesPerInt-1);
2467     __ jccb(Assembler::zero, L_int_aligned);
2468 
2469     __ testb(bits, BytesPerShort-1);
2470     __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
2471 
2472     __ BIND(L_short_aligned);
2473     __ shrptr(size, LogBytesPerShort); // size => short_count
2474     __ jump(RuntimeAddress(short_copy_entry));
2475 
2476     __ BIND(L_int_aligned);
2477     __ shrptr(size, LogBytesPerInt); // size => int_count
2478     __ jump(RuntimeAddress(int_copy_entry));
2479 
2480     __ BIND(L_long_aligned);
2481     __ shrptr(size, LogBytesPerLong); // size => qword_count
2482     __ jump(RuntimeAddress(long_copy_entry));
2483 
2484     return start;
2485   }
2486 
2487   // Perform range checks on the proposed arraycopy.
2488   // Kills temp, but nothing else.
2489   // Also, clean the sign bits of src_pos and dst_pos.
2490   void arraycopy_range_checks(Register src,     // source array oop (c_rarg0)
2491                               Register src_pos, // source position (c_rarg1)
2492                               Register dst,     // destination array oo (c_rarg2)
2493                               Register dst_pos, // destination position (c_rarg3)
2494                               Register length,
2495                               Register temp,
2496                               Label& L_failed) {
2497     BLOCK_COMMENT("arraycopy_range_checks:");
2498 
2499     //  if (src_pos + length > arrayOop(src)->length())  FAIL;
2500     __ movl(temp, length);
2501     __ addl(temp, src_pos);             // src_pos + length
2502     __ cmpl(temp, Address(src, arrayOopDesc::length_offset_in_bytes()));
2503     __ jcc(Assembler::above, L_failed);
2504 
2505     //  if (dst_pos + length > arrayOop(dst)->length())  FAIL;
2506     __ movl(temp, length);
2507     __ addl(temp, dst_pos);             // dst_pos + length
2508     __ cmpl(temp, Address(dst, arrayOopDesc::length_offset_in_bytes()));
2509     __ jcc(Assembler::above, L_failed);
2510 
2511     // Have to clean up high 32-bits of 'src_pos' and 'dst_pos'.
2512     // Move with sign extension can be used since they are positive.
2513     __ movslq(src_pos, src_pos);
2514     __ movslq(dst_pos, dst_pos);
2515 
2516     BLOCK_COMMENT("arraycopy_range_checks done");
2517   }
2518 
2519   //
2520   //  Generate generic array copy stubs
2521   //
2522   //  Input:
2523   //    c_rarg0    -  src oop
2524   //    c_rarg1    -  src_pos (32-bits)
2525   //    c_rarg2    -  dst oop
2526   //    c_rarg3    -  dst_pos (32-bits)
2527   // not Win64
2528   //    c_rarg4    -  element count (32-bits)
2529   // Win64
2530   //    rsp+40     -  element count (32-bits)
2531   //
2532   //  Output:
2533   //    rax ==  0  -  success
2534   //    rax == -1^K - failure, where K is partial transfer count
2535   //
2536   address generate_generic_copy(const char *name,
2537                                 address byte_copy_entry, address short_copy_entry,
2538                                 address int_copy_entry, address oop_copy_entry,
2539                                 address long_copy_entry, address checkcast_copy_entry) {
2540 
2541     Label L_failed, L_failed_0, L_objArray;
2542     Label L_copy_bytes, L_copy_shorts, L_copy_ints, L_copy_longs;
2543 
2544     // Input registers
2545     const Register src        = c_rarg0;  // source array oop
2546     const Register src_pos    = c_rarg1;  // source position
2547     const Register dst        = c_rarg2;  // destination array oop
2548     const Register dst_pos    = c_rarg3;  // destination position
2549 #ifndef _WIN64
2550     const Register length     = c_rarg4;
2551 #else
2552     const Address  length(rsp, 6 * wordSize);  // elements count is on stack on Win64
2553 #endif
2554 
2555     { int modulus = CodeEntryAlignment;
2556       int target  = modulus - 5; // 5 = sizeof jmp(L_failed)
2557       int advance = target - (__ offset() % modulus);
2558       if (advance < 0)  advance += modulus;
2559       if (advance > 0)  __ nop(advance);
2560     }
2561     StubCodeMark mark(this, "StubRoutines", name);
2562 
2563     // Short-hop target to L_failed.  Makes for denser prologue code.
2564     __ BIND(L_failed_0);
2565     __ jmp(L_failed);
2566     assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
2567 
2568     __ align(CodeEntryAlignment);
2569     address start = __ pc();
2570 
2571     __ enter(); // required for proper stackwalking of RuntimeStub frame
2572 
2573     // bump this on entry, not on exit:
2574     inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
2575 
2576     //-----------------------------------------------------------------------
2577     // Assembler stub will be used for this call to arraycopy
2578     // if the following conditions are met:
2579     //
2580     // (1) src and dst must not be null.
2581     // (2) src_pos must not be negative.
2582     // (3) dst_pos must not be negative.
2583     // (4) length  must not be negative.
2584     // (5) src klass and dst klass should be the same and not NULL.
2585     // (6) src and dst should be arrays.
2586     // (7) src_pos + length must not exceed length of src.
2587     // (8) dst_pos + length must not exceed length of dst.
2588     //
2589 
2590     //  if (src == NULL) return -1;
2591     __ testptr(src, src);         // src oop
2592     size_t j1off = __ offset();
2593     __ jccb(Assembler::zero, L_failed_0);
2594 
2595     //  if (src_pos < 0) return -1;
2596     __ testl(src_pos, src_pos); // src_pos (32-bits)
2597     __ jccb(Assembler::negative, L_failed_0);
2598 
2599     //  if (dst == NULL) return -1;
2600     __ testptr(dst, dst);         // dst oop
2601     __ jccb(Assembler::zero, L_failed_0);
2602 
2603     //  if (dst_pos < 0) return -1;
2604     __ testl(dst_pos, dst_pos); // dst_pos (32-bits)
2605     size_t j4off = __ offset();
2606     __ jccb(Assembler::negative, L_failed_0);
2607 
2608     // The first four tests are very dense code,
2609     // but not quite dense enough to put four
2610     // jumps in a 16-byte instruction fetch buffer.
2611     // That's good, because some branch predicters
2612     // do not like jumps so close together.
2613     // Make sure of this.
2614     guarantee(((j1off ^ j4off) & ~15) != 0, "I$ line of 1st & 4th jumps");
2615 
2616     // registers used as temp
2617     const Register r11_length    = r11; // elements count to copy
2618     const Register r10_src_klass = r10; // array klass
2619 
2620     //  if (length < 0) return -1;
2621     __ movl(r11_length, length);        // length (elements count, 32-bits value)
2622     __ testl(r11_length, r11_length);
2623     __ jccb(Assembler::negative, L_failed_0);
2624 
2625     __ load_klass(r10_src_klass, src);
2626 #ifdef ASSERT
2627     //  assert(src->klass() != NULL);
2628     {
2629       BLOCK_COMMENT("assert klasses not null {");
2630       Label L1, L2;
2631       __ testptr(r10_src_klass, r10_src_klass);
2632       __ jcc(Assembler::notZero, L2);   // it is broken if klass is NULL
2633       __ bind(L1);
2634       __ stop("broken null klass");
2635       __ bind(L2);
2636       __ load_klass(rax, dst);
2637       __ cmpq(rax, 0);
2638       __ jcc(Assembler::equal, L1);     // this would be broken also
2639       BLOCK_COMMENT("} assert klasses not null done");
2640     }
2641 #endif
2642 
2643     // Load layout helper (32-bits)
2644     //
2645     //  |array_tag|     | header_size | element_type |     |log2_element_size|
2646     // 32        30    24            16              8     2                 0
2647     //
2648     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
2649     //
2650 
2651     const int lh_offset = in_bytes(Klass::layout_helper_offset());
2652 
2653     // Handle objArrays completely differently...
2654     const jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2655     __ cmpl(Address(r10_src_klass, lh_offset), objArray_lh);
2656     __ jcc(Assembler::equal, L_objArray);
2657 
2658     //  if (src->klass() != dst->klass()) return -1;
2659     __ load_klass(rax, dst);
2660     __ cmpq(r10_src_klass, rax);
2661     __ jcc(Assembler::notEqual, L_failed);
2662 
2663     const Register rax_lh = rax;  // layout helper
2664     __ movl(rax_lh, Address(r10_src_klass, lh_offset));
2665 
2666     //  if (!src->is_Array()) return -1;
2667     __ cmpl(rax_lh, Klass::_lh_neutral_value);
2668     __ jcc(Assembler::greaterEqual, L_failed);
2669 
2670     // At this point, it is known to be a typeArray (array_tag 0x3).
2671 #ifdef ASSERT
2672     {
2673       BLOCK_COMMENT("assert primitive array {");
2674       Label L;
2675       __ cmpl(rax_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
2676       __ jcc(Assembler::greaterEqual, L);
2677       __ stop("must be a primitive array");
2678       __ bind(L);
2679       BLOCK_COMMENT("} assert primitive array done");
2680     }
2681 #endif
2682 
2683     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2684                            r10, L_failed);
2685 
2686     // TypeArrayKlass
2687     //
2688     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
2689     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
2690     //
2691 
2692     const Register r10_offset = r10;    // array offset
2693     const Register rax_elsize = rax_lh; // element size
2694 
2695     __ movl(r10_offset, rax_lh);
2696     __ shrl(r10_offset, Klass::_lh_header_size_shift);
2697     __ andptr(r10_offset, Klass::_lh_header_size_mask);   // array_offset
2698     __ addptr(src, r10_offset);           // src array offset
2699     __ addptr(dst, r10_offset);           // dst array offset
2700     BLOCK_COMMENT("choose copy loop based on element size");
2701     __ andl(rax_lh, Klass::_lh_log2_element_size_mask); // rax_lh -> rax_elsize
2702 
2703     // next registers should be set before the jump to corresponding stub
2704     const Register from     = c_rarg0;  // source array address
2705     const Register to       = c_rarg1;  // destination array address
2706     const Register count    = c_rarg2;  // elements count
2707 
2708     // 'from', 'to', 'count' registers should be set in such order
2709     // since they are the same as 'src', 'src_pos', 'dst'.
2710 
2711   __ BIND(L_copy_bytes);
2712     __ cmpl(rax_elsize, 0);
2713     __ jccb(Assembler::notEqual, L_copy_shorts);
2714     __ lea(from, Address(src, src_pos, Address::times_1, 0));// src_addr
2715     __ lea(to,   Address(dst, dst_pos, Address::times_1, 0));// dst_addr
2716     __ movl2ptr(count, r11_length); // length
2717     __ jump(RuntimeAddress(byte_copy_entry));
2718 
2719   __ BIND(L_copy_shorts);
2720     __ cmpl(rax_elsize, LogBytesPerShort);
2721     __ jccb(Assembler::notEqual, L_copy_ints);
2722     __ lea(from, Address(src, src_pos, Address::times_2, 0));// src_addr
2723     __ lea(to,   Address(dst, dst_pos, Address::times_2, 0));// dst_addr
2724     __ movl2ptr(count, r11_length); // length
2725     __ jump(RuntimeAddress(short_copy_entry));
2726 
2727   __ BIND(L_copy_ints);
2728     __ cmpl(rax_elsize, LogBytesPerInt);
2729     __ jccb(Assembler::notEqual, L_copy_longs);
2730     __ lea(from, Address(src, src_pos, Address::times_4, 0));// src_addr
2731     __ lea(to,   Address(dst, dst_pos, Address::times_4, 0));// dst_addr
2732     __ movl2ptr(count, r11_length); // length
2733     __ jump(RuntimeAddress(int_copy_entry));
2734 
2735   __ BIND(L_copy_longs);
2736 #ifdef ASSERT
2737     {
2738       BLOCK_COMMENT("assert long copy {");
2739       Label L;
2740       __ cmpl(rax_elsize, LogBytesPerLong);
2741       __ jcc(Assembler::equal, L);
2742       __ stop("must be long copy, but elsize is wrong");
2743       __ bind(L);
2744       BLOCK_COMMENT("} assert long copy done");
2745     }
2746 #endif
2747     __ lea(from, Address(src, src_pos, Address::times_8, 0));// src_addr
2748     __ lea(to,   Address(dst, dst_pos, Address::times_8, 0));// dst_addr
2749     __ movl2ptr(count, r11_length); // length
2750     __ jump(RuntimeAddress(long_copy_entry));
2751 
2752     // ObjArrayKlass
2753   __ BIND(L_objArray);
2754     // live at this point:  r10_src_klass, r11_length, src[_pos], dst[_pos]
2755 
2756     Label L_plain_copy, L_checkcast_copy;
2757     //  test array classes for subtyping
2758     __ load_klass(rax, dst);
2759     __ cmpq(r10_src_klass, rax); // usual case is exact equality
2760     __ jcc(Assembler::notEqual, L_checkcast_copy);
2761 
2762     // Identically typed arrays can be copied without element-wise checks.
2763     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2764                            r10, L_failed);
2765 
2766     __ lea(from, Address(src, src_pos, TIMES_OOP,
2767                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
2768     __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2769                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
2770     __ movl2ptr(count, r11_length); // length
2771   __ BIND(L_plain_copy);
2772     __ jump(RuntimeAddress(oop_copy_entry));
2773 
2774   __ BIND(L_checkcast_copy);
2775     // live at this point:  r10_src_klass, r11_length, rax (dst_klass)
2776     {
2777       // Before looking at dst.length, make sure dst is also an objArray.
2778       __ cmpl(Address(rax, lh_offset), objArray_lh);
2779       __ jcc(Assembler::notEqual, L_failed);
2780 
2781       // It is safe to examine both src.length and dst.length.
2782       arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2783                              rax, L_failed);
2784 
2785       const Register r11_dst_klass = r11;
2786       __ load_klass(r11_dst_klass, dst); // reload
2787 
2788       // Marshal the base address arguments now, freeing registers.
2789       __ lea(from, Address(src, src_pos, TIMES_OOP,
2790                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2791       __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2792                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2793       __ movl(count, length);           // length (reloaded)
2794       Register sco_temp = c_rarg3;      // this register is free now
2795       assert_different_registers(from, to, count, sco_temp,
2796                                  r11_dst_klass, r10_src_klass);
2797       assert_clean_int(count, sco_temp);
2798 
2799       // Generate the type check.
2800       const int sco_offset = in_bytes(Klass::super_check_offset_offset());
2801       __ movl(sco_temp, Address(r11_dst_klass, sco_offset));
2802       assert_clean_int(sco_temp, rax);
2803       generate_type_check(r10_src_klass, sco_temp, r11_dst_klass, L_plain_copy);
2804 
2805       // Fetch destination element klass from the ObjArrayKlass header.
2806       int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
2807       __ movptr(r11_dst_klass, Address(r11_dst_klass, ek_offset));
2808       __ movl(  sco_temp,      Address(r11_dst_klass, sco_offset));
2809       assert_clean_int(sco_temp, rax);
2810 
2811       // the checkcast_copy loop needs two extra arguments:
2812       assert(c_rarg3 == sco_temp, "#3 already in place");
2813       // Set up arguments for checkcast_copy_entry.
2814       setup_arg_regs(4);
2815       __ movptr(r8, r11_dst_klass);  // dst.klass.element_klass, r8 is c_rarg4 on Linux/Solaris
2816       __ jump(RuntimeAddress(checkcast_copy_entry));
2817     }
2818 
2819   __ BIND(L_failed);
2820     __ xorptr(rax, rax);
2821     __ notptr(rax); // return -1
2822     __ leave();   // required for proper stackwalking of RuntimeStub frame
2823     __ ret(0);
2824 
2825     return start;
2826   }
2827 
2828   void generate_arraycopy_stubs() {
2829     address entry;
2830     address entry_jbyte_arraycopy;
2831     address entry_jshort_arraycopy;
2832     address entry_jint_arraycopy;
2833     address entry_oop_arraycopy;
2834     address entry_jlong_arraycopy;
2835     address entry_checkcast_arraycopy;
2836 
2837     StubRoutines::_jbyte_disjoint_arraycopy  = generate_disjoint_byte_copy(false, &entry,
2838                                                                            "jbyte_disjoint_arraycopy");
2839     StubRoutines::_jbyte_arraycopy           = generate_conjoint_byte_copy(false, entry, &entry_jbyte_arraycopy,
2840                                                                            "jbyte_arraycopy");
2841 
2842     StubRoutines::_jshort_disjoint_arraycopy = generate_disjoint_short_copy(false, &entry,
2843                                                                             "jshort_disjoint_arraycopy");
2844     StubRoutines::_jshort_arraycopy          = generate_conjoint_short_copy(false, entry, &entry_jshort_arraycopy,
2845                                                                             "jshort_arraycopy");
2846 
2847     StubRoutines::_jint_disjoint_arraycopy   = generate_disjoint_int_oop_copy(false, false, &entry,
2848                                                                               "jint_disjoint_arraycopy");
2849     StubRoutines::_jint_arraycopy            = generate_conjoint_int_oop_copy(false, false, entry,
2850                                                                               &entry_jint_arraycopy, "jint_arraycopy");
2851 
2852     StubRoutines::_jlong_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, false, &entry,
2853                                                                                "jlong_disjoint_arraycopy");
2854     StubRoutines::_jlong_arraycopy           = generate_conjoint_long_oop_copy(false, false, entry,
2855                                                                                &entry_jlong_arraycopy, "jlong_arraycopy");
2856 
2857 
2858     if (UseCompressedOops) {
2859       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_int_oop_copy(false, true, &entry,
2860                                                                               "oop_disjoint_arraycopy");
2861       StubRoutines::_oop_arraycopy           = generate_conjoint_int_oop_copy(false, true, entry,
2862                                                                               &entry_oop_arraycopy, "oop_arraycopy");
2863       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_int_oop_copy(false, true, &entry,
2864                                                                                      "oop_disjoint_arraycopy_uninit",
2865                                                                                      /*dest_uninitialized*/true);
2866       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_int_oop_copy(false, true, entry,
2867                                                                                      NULL, "oop_arraycopy_uninit",
2868                                                                                      /*dest_uninitialized*/true);
2869     } else {
2870       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, true, &entry,
2871                                                                                "oop_disjoint_arraycopy");
2872       StubRoutines::_oop_arraycopy           = generate_conjoint_long_oop_copy(false, true, entry,
2873                                                                                &entry_oop_arraycopy, "oop_arraycopy");
2874       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_long_oop_copy(false, true, &entry,
2875                                                                                       "oop_disjoint_arraycopy_uninit",
2876                                                                                       /*dest_uninitialized*/true);
2877       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_long_oop_copy(false, true, entry,
2878                                                                                       NULL, "oop_arraycopy_uninit",
2879                                                                                       /*dest_uninitialized*/true);
2880     }
2881 
2882     StubRoutines::_checkcast_arraycopy        = generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy);
2883     StubRoutines::_checkcast_arraycopy_uninit = generate_checkcast_copy("checkcast_arraycopy_uninit", NULL,
2884                                                                         /*dest_uninitialized*/true);
2885 
2886     StubRoutines::_unsafe_arraycopy    = generate_unsafe_copy("unsafe_arraycopy",
2887                                                               entry_jbyte_arraycopy,
2888                                                               entry_jshort_arraycopy,
2889                                                               entry_jint_arraycopy,
2890                                                               entry_jlong_arraycopy);
2891     StubRoutines::_generic_arraycopy   = generate_generic_copy("generic_arraycopy",
2892                                                                entry_jbyte_arraycopy,
2893                                                                entry_jshort_arraycopy,
2894                                                                entry_jint_arraycopy,
2895                                                                entry_oop_arraycopy,
2896                                                                entry_jlong_arraycopy,
2897                                                                entry_checkcast_arraycopy);
2898 
2899     StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
2900     StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
2901     StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
2902     StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
2903     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
2904     StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
2905 
2906     // We don't generate specialized code for HeapWord-aligned source
2907     // arrays, so just use the code we've already generated
2908     StubRoutines::_arrayof_jbyte_disjoint_arraycopy  = StubRoutines::_jbyte_disjoint_arraycopy;
2909     StubRoutines::_arrayof_jbyte_arraycopy           = StubRoutines::_jbyte_arraycopy;
2910 
2911     StubRoutines::_arrayof_jshort_disjoint_arraycopy = StubRoutines::_jshort_disjoint_arraycopy;
2912     StubRoutines::_arrayof_jshort_arraycopy          = StubRoutines::_jshort_arraycopy;
2913 
2914     StubRoutines::_arrayof_jint_disjoint_arraycopy   = StubRoutines::_jint_disjoint_arraycopy;
2915     StubRoutines::_arrayof_jint_arraycopy            = StubRoutines::_jint_arraycopy;
2916 
2917     StubRoutines::_arrayof_jlong_disjoint_arraycopy  = StubRoutines::_jlong_disjoint_arraycopy;
2918     StubRoutines::_arrayof_jlong_arraycopy           = StubRoutines::_jlong_arraycopy;
2919 
2920     StubRoutines::_arrayof_oop_disjoint_arraycopy    = StubRoutines::_oop_disjoint_arraycopy;
2921     StubRoutines::_arrayof_oop_arraycopy             = StubRoutines::_oop_arraycopy;
2922 
2923     StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit    = StubRoutines::_oop_disjoint_arraycopy_uninit;
2924     StubRoutines::_arrayof_oop_arraycopy_uninit             = StubRoutines::_oop_arraycopy_uninit;
2925   }
2926 
2927   // AES intrinsic stubs
2928   enum {AESBlockSize = 16};
2929 
2930   address generate_key_shuffle_mask() {
2931     __ align(16);
2932     StubCodeMark mark(this, "StubRoutines", "key_shuffle_mask");
2933     address start = __ pc();
2934     __ emit_data64( 0x0405060700010203, relocInfo::none );
2935     __ emit_data64( 0x0c0d0e0f08090a0b, relocInfo::none );
2936     return start;
2937   }
2938 
2939   address generate_counter_shuffle_mask() {
2940     __ align(16);
2941     StubCodeMark mark(this, "StubRoutines", "counter_shuffle_mask");
2942     address start = __ pc();
2943     __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
2944     __ emit_data64(0x0001020304050607, relocInfo::none);
2945     return start;
2946   }
2947 
2948   // Utility routine for loading a 128-bit key word in little endian format
2949   // can optionally specify that the shuffle mask is already in an xmmregister
2950   void load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
2951     __ movdqu(xmmdst, Address(key, offset));
2952     if (xmm_shuf_mask != NULL) {
2953       __ pshufb(xmmdst, xmm_shuf_mask);
2954     } else {
2955       __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2956     }
2957   }
2958 
2959   // Utility routine for increase 128bit counter (iv in CTR mode)
2960   void inc_counter(Register reg, XMMRegister xmmdst, int inc_delta, Label& next_block) {
2961     __ pextrq(reg, xmmdst, 0x0);
2962     __ addq(reg, inc_delta);
2963     __ pinsrq(xmmdst, reg, 0x0);
2964     __ jcc(Assembler::carryClear, next_block); // jump if no carry
2965     __ pextrq(reg, xmmdst, 0x01); // Carry
2966     __ addq(reg, 0x01);
2967     __ pinsrq(xmmdst, reg, 0x01); //Carry end
2968     __ BIND(next_block);          // next instruction
2969   }
2970 
2971   // Arguments:
2972   //
2973   // Inputs:
2974   //   c_rarg0   - source byte array address
2975   //   c_rarg1   - destination byte array address
2976   //   c_rarg2   - K (key) in little endian int array
2977   //
2978   address generate_aescrypt_encryptBlock() {
2979     assert(UseAES, "need AES instructions and misaligned SSE support");
2980     __ align(CodeEntryAlignment);
2981     StubCodeMark mark(this, "StubRoutines", "aescrypt_encryptBlock");
2982     Label L_doLast;
2983     address start = __ pc();
2984 
2985     const Register from        = c_rarg0;  // source array address
2986     const Register to          = c_rarg1;  // destination array address
2987     const Register key         = c_rarg2;  // key array address
2988     const Register keylen      = rax;
2989 
2990     const XMMRegister xmm_result = xmm0;
2991     const XMMRegister xmm_key_shuf_mask = xmm1;
2992     // On win64 xmm6-xmm15 must be preserved so don't use them.
2993     const XMMRegister xmm_temp1  = xmm2;
2994     const XMMRegister xmm_temp2  = xmm3;
2995     const XMMRegister xmm_temp3  = xmm4;
2996     const XMMRegister xmm_temp4  = xmm5;
2997 
2998     __ enter(); // required for proper stackwalking of RuntimeStub frame
2999 
3000     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
3001     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3002 
3003     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3004     __ movdqu(xmm_result, Address(from, 0));  // get 16 bytes of input
3005 
3006     // For encryption, the java expanded key ordering is just what we need
3007     // we don't know if the key is aligned, hence not using load-execute form
3008 
3009     load_key(xmm_temp1, key, 0x00, xmm_key_shuf_mask);
3010     __ pxor(xmm_result, xmm_temp1);
3011 
3012     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
3013     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
3014     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
3015     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
3016 
3017     __ aesenc(xmm_result, xmm_temp1);
3018     __ aesenc(xmm_result, xmm_temp2);
3019     __ aesenc(xmm_result, xmm_temp3);
3020     __ aesenc(xmm_result, xmm_temp4);
3021 
3022     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
3023     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
3024     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
3025     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
3026 
3027     __ aesenc(xmm_result, xmm_temp1);
3028     __ aesenc(xmm_result, xmm_temp2);
3029     __ aesenc(xmm_result, xmm_temp3);
3030     __ aesenc(xmm_result, xmm_temp4);
3031 
3032     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
3033     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
3034 
3035     __ cmpl(keylen, 44);
3036     __ jccb(Assembler::equal, L_doLast);
3037 
3038     __ aesenc(xmm_result, xmm_temp1);
3039     __ aesenc(xmm_result, xmm_temp2);
3040 
3041     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
3042     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
3043 
3044     __ cmpl(keylen, 52);
3045     __ jccb(Assembler::equal, L_doLast);
3046 
3047     __ aesenc(xmm_result, xmm_temp1);
3048     __ aesenc(xmm_result, xmm_temp2);
3049 
3050     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
3051     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
3052 
3053     __ BIND(L_doLast);
3054     __ aesenc(xmm_result, xmm_temp1);
3055     __ aesenclast(xmm_result, xmm_temp2);
3056     __ movdqu(Address(to, 0), xmm_result);        // store the result
3057     __ xorptr(rax, rax); // return 0
3058     __ leave(); // required for proper stackwalking of RuntimeStub frame
3059     __ ret(0);
3060 
3061     return start;
3062   }
3063 
3064 
3065   // Arguments:
3066   //
3067   // Inputs:
3068   //   c_rarg0   - source byte array address
3069   //   c_rarg1   - destination byte array address
3070   //   c_rarg2   - K (key) in little endian int array
3071   //
3072   address generate_aescrypt_decryptBlock() {
3073     assert(UseAES, "need AES instructions and misaligned SSE support");
3074     __ align(CodeEntryAlignment);
3075     StubCodeMark mark(this, "StubRoutines", "aescrypt_decryptBlock");
3076     Label L_doLast;
3077     address start = __ pc();
3078 
3079     const Register from        = c_rarg0;  // source array address
3080     const Register to          = c_rarg1;  // destination array address
3081     const Register key         = c_rarg2;  // key array address
3082     const Register keylen      = rax;
3083 
3084     const XMMRegister xmm_result = xmm0;
3085     const XMMRegister xmm_key_shuf_mask = xmm1;
3086     // On win64 xmm6-xmm15 must be preserved so don't use them.
3087     const XMMRegister xmm_temp1  = xmm2;
3088     const XMMRegister xmm_temp2  = xmm3;
3089     const XMMRegister xmm_temp3  = xmm4;
3090     const XMMRegister xmm_temp4  = xmm5;
3091 
3092     __ enter(); // required for proper stackwalking of RuntimeStub frame
3093 
3094     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
3095     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3096 
3097     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3098     __ movdqu(xmm_result, Address(from, 0));
3099 
3100     // for decryption java expanded key ordering is rotated one position from what we want
3101     // so we start from 0x10 here and hit 0x00 last
3102     // we don't know if the key is aligned, hence not using load-execute form
3103     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
3104     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
3105     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
3106     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
3107 
3108     __ pxor  (xmm_result, xmm_temp1);
3109     __ aesdec(xmm_result, xmm_temp2);
3110     __ aesdec(xmm_result, xmm_temp3);
3111     __ aesdec(xmm_result, xmm_temp4);
3112 
3113     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
3114     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
3115     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
3116     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
3117 
3118     __ aesdec(xmm_result, xmm_temp1);
3119     __ aesdec(xmm_result, xmm_temp2);
3120     __ aesdec(xmm_result, xmm_temp3);
3121     __ aesdec(xmm_result, xmm_temp4);
3122 
3123     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
3124     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
3125     load_key(xmm_temp3, key, 0x00, xmm_key_shuf_mask);
3126 
3127     __ cmpl(keylen, 44);
3128     __ jccb(Assembler::equal, L_doLast);
3129 
3130     __ aesdec(xmm_result, xmm_temp1);
3131     __ aesdec(xmm_result, xmm_temp2);
3132 
3133     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
3134     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
3135 
3136     __ cmpl(keylen, 52);
3137     __ jccb(Assembler::equal, L_doLast);
3138 
3139     __ aesdec(xmm_result, xmm_temp1);
3140     __ aesdec(xmm_result, xmm_temp2);
3141 
3142     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
3143     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
3144 
3145     __ BIND(L_doLast);
3146     __ aesdec(xmm_result, xmm_temp1);
3147     __ aesdec(xmm_result, xmm_temp2);
3148 
3149     // for decryption the aesdeclast operation is always on key+0x00
3150     __ aesdeclast(xmm_result, xmm_temp3);
3151     __ movdqu(Address(to, 0), xmm_result);  // store the result
3152     __ xorptr(rax, rax); // return 0
3153     __ leave(); // required for proper stackwalking of RuntimeStub frame
3154     __ ret(0);
3155 
3156     return start;
3157   }
3158 
3159 
3160   // Arguments:
3161   //
3162   // Inputs:
3163   //   c_rarg0   - source byte array address
3164   //   c_rarg1   - destination byte array address
3165   //   c_rarg2   - K (key) in little endian int array
3166   //   c_rarg3   - r vector byte array address
3167   //   c_rarg4   - input length
3168   //
3169   // Output:
3170   //   rax       - input length
3171   //
3172   address generate_cipherBlockChaining_encryptAESCrypt() {
3173     assert(UseAES, "need AES instructions and misaligned SSE support");
3174     __ align(CodeEntryAlignment);
3175     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_encryptAESCrypt");
3176     address start = __ pc();
3177 
3178     Label L_exit, L_key_192_256, L_key_256, L_loopTop_128, L_loopTop_192, L_loopTop_256;
3179     const Register from        = c_rarg0;  // source array address
3180     const Register to          = c_rarg1;  // destination array address
3181     const Register key         = c_rarg2;  // key array address
3182     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
3183                                            // and left with the results of the last encryption block
3184 #ifndef _WIN64
3185     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
3186 #else
3187     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
3188     const Register len_reg     = r11;      // pick the volatile windows register
3189 #endif
3190     const Register pos         = rax;
3191 
3192     // xmm register assignments for the loops below
3193     const XMMRegister xmm_result = xmm0;
3194     const XMMRegister xmm_temp   = xmm1;
3195     // keys 0-10 preloaded into xmm2-xmm12
3196     const int XMM_REG_NUM_KEY_FIRST = 2;
3197     const int XMM_REG_NUM_KEY_LAST  = 15;
3198     const XMMRegister xmm_key0   = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
3199     const XMMRegister xmm_key10  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+10);
3200     const XMMRegister xmm_key11  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+11);
3201     const XMMRegister xmm_key12  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+12);
3202     const XMMRegister xmm_key13  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+13);
3203 
3204     __ enter(); // required for proper stackwalking of RuntimeStub frame
3205 
3206 #ifdef _WIN64
3207     // on win64, fill len_reg from stack position
3208     __ movl(len_reg, len_mem);
3209 #else
3210     __ push(len_reg); // Save
3211 #endif
3212 
3213     const XMMRegister xmm_key_shuf_mask = xmm_temp;  // used temporarily to swap key bytes up front
3214     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3215     // load up xmm regs xmm2 thru xmm12 with key 0x00 - 0xa0
3216     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x00; rnum <= XMM_REG_NUM_KEY_FIRST+10; rnum++) {
3217       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
3218       offset += 0x10;
3219     }
3220     __ movdqu(xmm_result, Address(rvec, 0x00));   // initialize xmm_result with r vec
3221 
3222     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
3223     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3224     __ cmpl(rax, 44);
3225     __ jcc(Assembler::notEqual, L_key_192_256);
3226 
3227     // 128 bit code follows here
3228     __ movptr(pos, 0);
3229     __ align(OptoLoopAlignment);
3230 
3231     __ BIND(L_loopTop_128);
3232     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3233     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3234     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3235     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_FIRST + 9; rnum++) {
3236       __ aesenc(xmm_result, as_XMMRegister(rnum));
3237     }
3238     __ aesenclast(xmm_result, xmm_key10);
3239     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3240     // no need to store r to memory until we exit
3241     __ addptr(pos, AESBlockSize);
3242     __ subptr(len_reg, AESBlockSize);
3243     __ jcc(Assembler::notEqual, L_loopTop_128);
3244 
3245     __ BIND(L_exit);
3246     __ movdqu(Address(rvec, 0), xmm_result);     // final value of r stored in rvec of CipherBlockChaining object
3247 
3248 #ifdef _WIN64
3249     __ movl(rax, len_mem);
3250 #else
3251     __ pop(rax); // return length
3252 #endif
3253     __ leave(); // required for proper stackwalking of RuntimeStub frame
3254     __ ret(0);
3255 
3256     __ BIND(L_key_192_256);
3257     // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
3258     load_key(xmm_key11, key, 0xb0, xmm_key_shuf_mask);
3259     load_key(xmm_key12, key, 0xc0, xmm_key_shuf_mask);
3260     __ cmpl(rax, 52);
3261     __ jcc(Assembler::notEqual, L_key_256);
3262 
3263     // 192-bit code follows here (could be changed to use more xmm registers)
3264     __ movptr(pos, 0);
3265     __ align(OptoLoopAlignment);
3266 
3267     __ BIND(L_loopTop_192);
3268     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3269     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3270     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3271     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 11; rnum++) {
3272       __ aesenc(xmm_result, as_XMMRegister(rnum));
3273     }
3274     __ aesenclast(xmm_result, xmm_key12);
3275     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3276     // no need to store r to memory until we exit
3277     __ addptr(pos, AESBlockSize);
3278     __ subptr(len_reg, AESBlockSize);
3279     __ jcc(Assembler::notEqual, L_loopTop_192);
3280     __ jmp(L_exit);
3281 
3282     __ BIND(L_key_256);
3283     // 256-bit code follows here (could be changed to use more xmm registers)
3284     load_key(xmm_key13, key, 0xd0, xmm_key_shuf_mask);
3285     __ movptr(pos, 0);
3286     __ align(OptoLoopAlignment);
3287 
3288     __ BIND(L_loopTop_256);
3289     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3290     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3291     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3292     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 13; rnum++) {
3293       __ aesenc(xmm_result, as_XMMRegister(rnum));
3294     }
3295     load_key(xmm_temp, key, 0xe0);
3296     __ aesenclast(xmm_result, xmm_temp);
3297     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3298     // no need to store r to memory until we exit
3299     __ addptr(pos, AESBlockSize);
3300     __ subptr(len_reg, AESBlockSize);
3301     __ jcc(Assembler::notEqual, L_loopTop_256);
3302     __ jmp(L_exit);
3303 
3304     return start;
3305   }
3306 
3307   // Safefetch stubs.
3308   void generate_safefetch(const char* name, int size, address* entry,
3309                           address* fault_pc, address* continuation_pc) {
3310     // safefetch signatures:
3311     //   int      SafeFetch32(int*      adr, int      errValue);
3312     //   intptr_t SafeFetchN (intptr_t* adr, intptr_t errValue);
3313     //
3314     // arguments:
3315     //   c_rarg0 = adr
3316     //   c_rarg1 = errValue
3317     //
3318     // result:
3319     //   PPC_RET  = *adr or errValue
3320 
3321     StubCodeMark mark(this, "StubRoutines", name);
3322 
3323     // Entry point, pc or function descriptor.
3324     *entry = __ pc();
3325 
3326     // Load *adr into c_rarg1, may fault.
3327     *fault_pc = __ pc();
3328     switch (size) {
3329       case 4:
3330         // int32_t
3331         __ movl(c_rarg1, Address(c_rarg0, 0));
3332         break;
3333       case 8:
3334         // int64_t
3335         __ movq(c_rarg1, Address(c_rarg0, 0));
3336         break;
3337       default:
3338         ShouldNotReachHere();
3339     }
3340 
3341     // return errValue or *adr
3342     *continuation_pc = __ pc();
3343     __ movq(rax, c_rarg1);
3344     __ ret(0);
3345   }
3346 
3347   // This is a version of CBC/AES Decrypt which does 4 blocks in a loop at a time
3348   // to hide instruction latency
3349   //
3350   // Arguments:
3351   //
3352   // Inputs:
3353   //   c_rarg0   - source byte array address
3354   //   c_rarg1   - destination byte array address
3355   //   c_rarg2   - K (key) in little endian int array
3356   //   c_rarg3   - r vector byte array address
3357   //   c_rarg4   - input length
3358   //
3359   // Output:
3360   //   rax       - input length
3361   //
3362   address generate_cipherBlockChaining_decryptAESCrypt_Parallel() {
3363     assert(UseAES, "need AES instructions and misaligned SSE support");
3364     __ align(CodeEntryAlignment);
3365     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt");
3366     address start = __ pc();
3367 
3368     const Register from        = c_rarg0;  // source array address
3369     const Register to          = c_rarg1;  // destination array address
3370     const Register key         = c_rarg2;  // key array address
3371     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
3372                                            // and left with the results of the last encryption block
3373 #ifndef _WIN64
3374     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
3375 #else
3376     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
3377     const Register len_reg     = r11;      // pick the volatile windows register
3378 #endif
3379     const Register pos         = rax;
3380 
3381     const int PARALLEL_FACTOR = 4;
3382     const int ROUNDS[3] = { 10, 12, 14 }; // aes rounds for key128, key192, key256
3383 
3384     Label L_exit;
3385     Label L_singleBlock_loopTopHead[3]; // 128, 192, 256
3386     Label L_singleBlock_loopTopHead2[3]; // 128, 192, 256
3387     Label L_singleBlock_loopTop[3]; // 128, 192, 256
3388     Label L_multiBlock_loopTopHead[3]; // 128, 192, 256
3389     Label L_multiBlock_loopTop[3]; // 128, 192, 256
3390 
3391     // keys 0-10 preloaded into xmm5-xmm15
3392     const int XMM_REG_NUM_KEY_FIRST = 5;
3393     const int XMM_REG_NUM_KEY_LAST  = 15;
3394     const XMMRegister xmm_key_first = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
3395     const XMMRegister xmm_key_last  = as_XMMRegister(XMM_REG_NUM_KEY_LAST);
3396 
3397     __ enter(); // required for proper stackwalking of RuntimeStub frame
3398 
3399 #ifdef _WIN64
3400     // on win64, fill len_reg from stack position
3401     __ movl(len_reg, len_mem);
3402 #else
3403     __ push(len_reg); // Save
3404 #endif
3405     __ push(rbx);
3406     // the java expanded key ordering is rotated one position from what we want
3407     // so we start from 0x10 here and hit 0x00 last
3408     const XMMRegister xmm_key_shuf_mask = xmm1;  // used temporarily to swap key bytes up front
3409     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3410     // load up xmm regs 5 thru 15 with key 0x10 - 0xa0 - 0x00
3411     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x10; rnum < XMM_REG_NUM_KEY_LAST; rnum++) {
3412       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
3413       offset += 0x10;
3414     }
3415     load_key(xmm_key_last, key, 0x00, xmm_key_shuf_mask);
3416 
3417     const XMMRegister xmm_prev_block_cipher = xmm1;  // holds cipher of previous block
3418 
3419     // registers holding the four results in the parallelized loop
3420     const XMMRegister xmm_result0 = xmm0;
3421     const XMMRegister xmm_result1 = xmm2;
3422     const XMMRegister xmm_result2 = xmm3;
3423     const XMMRegister xmm_result3 = xmm4;
3424 
3425     __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00));   // initialize with initial rvec
3426 
3427     __ xorptr(pos, pos);
3428 
3429     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
3430     __ movl(rbx, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3431     __ cmpl(rbx, 52);
3432     __ jcc(Assembler::equal, L_multiBlock_loopTopHead[1]);
3433     __ cmpl(rbx, 60);
3434     __ jcc(Assembler::equal, L_multiBlock_loopTopHead[2]);
3435 
3436 #define DoFour(opc, src_reg)           \
3437   __ opc(xmm_result0, src_reg);         \
3438   __ opc(xmm_result1, src_reg);         \
3439   __ opc(xmm_result2, src_reg);         \
3440   __ opc(xmm_result3, src_reg);         \
3441 
3442     for (int k = 0; k < 3; ++k) {
3443       __ BIND(L_multiBlock_loopTopHead[k]);
3444       if (k != 0) {
3445         __ cmpptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // see if at least 4 blocks left
3446         __ jcc(Assembler::less, L_singleBlock_loopTopHead2[k]);
3447       }
3448       if (k == 1) {
3449         __ subptr(rsp, 6 * wordSize);
3450         __ movdqu(Address(rsp, 0), xmm15); //save last_key from xmm15
3451         load_key(xmm15, key, 0xb0); // 0xb0; 192-bit key goes up to 0xc0
3452         __ movdqu(Address(rsp, 2 * wordSize), xmm15);
3453         load_key(xmm1, key, 0xc0);  // 0xc0;
3454         __ movdqu(Address(rsp, 4 * wordSize), xmm1);
3455       } else if (k == 2) {
3456         __ subptr(rsp, 10 * wordSize);
3457         __ movdqu(Address(rsp, 0), xmm15); //save last_key from xmm15
3458         load_key(xmm15, key, 0xd0); // 0xd0; 256-bit key goes upto 0xe0
3459         __ movdqu(Address(rsp, 6 * wordSize), xmm15);
3460         load_key(xmm1, key, 0xe0);  // 0xe0;
3461         __ movdqu(Address(rsp, 8 * wordSize), xmm1);
3462         load_key(xmm15, key, 0xb0); // 0xb0;
3463         __ movdqu(Address(rsp, 2 * wordSize), xmm15);
3464         load_key(xmm1, key, 0xc0);  // 0xc0;
3465         __ movdqu(Address(rsp, 4 * wordSize), xmm1);
3466       }
3467       __ align(OptoLoopAlignment);
3468       __ BIND(L_multiBlock_loopTop[k]);
3469       __ cmpptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // see if at least 4 blocks left
3470       __ jcc(Assembler::less, L_singleBlock_loopTopHead[k]);
3471 
3472       if  (k != 0) {
3473         __ movdqu(xmm15, Address(rsp, 2 * wordSize));
3474         __ movdqu(xmm1, Address(rsp, 4 * wordSize));
3475       }
3476 
3477       __ movdqu(xmm_result0, Address(from, pos, Address::times_1, 0 * AESBlockSize)); // get next 4 blocks into xmmresult registers
3478       __ movdqu(xmm_result1, Address(from, pos, Address::times_1, 1 * AESBlockSize));
3479       __ movdqu(xmm_result2, Address(from, pos, Address::times_1, 2 * AESBlockSize));
3480       __ movdqu(xmm_result3, Address(from, pos, Address::times_1, 3 * AESBlockSize));
3481 
3482       DoFour(pxor, xmm_key_first);
3483       if (k == 0) {
3484         for (int rnum = 1; rnum < ROUNDS[k]; rnum++) {
3485           DoFour(aesdec, as_XMMRegister(rnum + XMM_REG_NUM_KEY_FIRST));
3486         }
3487         DoFour(aesdeclast, xmm_key_last);
3488       } else if (k == 1) {
3489         for (int rnum = 1; rnum <= ROUNDS[k]-2; rnum++) {
3490           DoFour(aesdec, as_XMMRegister(rnum + XMM_REG_NUM_KEY_FIRST));
3491         }
3492         __ movdqu(xmm_key_last, Address(rsp, 0)); // xmm15 needs to be loaded again.
3493         DoFour(aesdec, xmm1);  // key : 0xc0
3494         __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00));  // xmm1 needs to be loaded again
3495         DoFour(aesdeclast, xmm_key_last);
3496       } else if (k == 2) {
3497         for (int rnum = 1; rnum <= ROUNDS[k] - 4; rnum++) {
3498           DoFour(aesdec, as_XMMRegister(rnum + XMM_REG_NUM_KEY_FIRST));
3499         }
3500         DoFour(aesdec, xmm1);  // key : 0xc0
3501         __ movdqu(xmm15, Address(rsp, 6 * wordSize));
3502         __ movdqu(xmm1, Address(rsp, 8 * wordSize));
3503         DoFour(aesdec, xmm15);  // key : 0xd0
3504         __ movdqu(xmm_key_last, Address(rsp, 0)); // xmm15 needs to be loaded again.
3505         DoFour(aesdec, xmm1);  // key : 0xe0
3506         __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00));  // xmm1 needs to be loaded again
3507         DoFour(aesdeclast, xmm_key_last);
3508       }
3509 
3510       // for each result, xor with the r vector of previous cipher block
3511       __ pxor(xmm_result0, xmm_prev_block_cipher);
3512       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 0 * AESBlockSize));
3513       __ pxor(xmm_result1, xmm_prev_block_cipher);
3514       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 1 * AESBlockSize));
3515       __ pxor(xmm_result2, xmm_prev_block_cipher);
3516       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 2 * AESBlockSize));
3517       __ pxor(xmm_result3, xmm_prev_block_cipher);
3518       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 3 * AESBlockSize));   // this will carry over to next set of blocks
3519       if (k != 0) {
3520         __ movdqu(Address(rvec, 0x00), xmm_prev_block_cipher);
3521       }
3522 
3523       __ movdqu(Address(to, pos, Address::times_1, 0 * AESBlockSize), xmm_result0);     // store 4 results into the next 64 bytes of output
3524       __ movdqu(Address(to, pos, Address::times_1, 1 * AESBlockSize), xmm_result1);
3525       __ movdqu(Address(to, pos, Address::times_1, 2 * AESBlockSize), xmm_result2);
3526       __ movdqu(Address(to, pos, Address::times_1, 3 * AESBlockSize), xmm_result3);
3527 
3528       __ addptr(pos, PARALLEL_FACTOR * AESBlockSize);
3529       __ subptr(len_reg, PARALLEL_FACTOR * AESBlockSize);
3530       __ jmp(L_multiBlock_loopTop[k]);
3531 
3532       // registers used in the non-parallelized loops
3533       // xmm register assignments for the loops below
3534       const XMMRegister xmm_result = xmm0;
3535       const XMMRegister xmm_prev_block_cipher_save = xmm2;
3536       const XMMRegister xmm_key11 = xmm3;
3537       const XMMRegister xmm_key12 = xmm4;
3538       const XMMRegister key_tmp = xmm4;
3539 
3540       __ BIND(L_singleBlock_loopTopHead[k]);
3541       if (k == 1) {
3542         __ addptr(rsp, 6 * wordSize);
3543       } else if (k == 2) {
3544         __ addptr(rsp, 10 * wordSize);
3545       }
3546       __ cmpptr(len_reg, 0); // any blocks left??
3547       __ jcc(Assembler::equal, L_exit);
3548       __ BIND(L_singleBlock_loopTopHead2[k]);
3549       if (k == 1) {
3550         load_key(xmm_key11, key, 0xb0); // 0xb0; 192-bit key goes upto 0xc0
3551         load_key(xmm_key12, key, 0xc0); // 0xc0; 192-bit key goes upto 0xc0
3552       }
3553       if (k == 2) {
3554         load_key(xmm_key11, key, 0xb0); // 0xb0; 256-bit key goes upto 0xe0
3555       }
3556       __ align(OptoLoopAlignment);
3557       __ BIND(L_singleBlock_loopTop[k]);
3558       __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input
3559       __ movdqa(xmm_prev_block_cipher_save, xmm_result); // save for next r vector
3560       __ pxor(xmm_result, xmm_key_first); // do the aes dec rounds
3561       for (int rnum = 1; rnum <= 9 ; rnum++) {
3562           __ aesdec(xmm_result, as_XMMRegister(rnum + XMM_REG_NUM_KEY_FIRST));
3563       }
3564       if (k == 1) {
3565         __ aesdec(xmm_result, xmm_key11);
3566         __ aesdec(xmm_result, xmm_key12);
3567       }
3568       if (k == 2) {
3569         __ aesdec(xmm_result, xmm_key11);
3570         load_key(key_tmp, key, 0xc0);
3571         __ aesdec(xmm_result, key_tmp);
3572         load_key(key_tmp, key, 0xd0);
3573         __ aesdec(xmm_result, key_tmp);
3574         load_key(key_tmp, key, 0xe0);
3575         __ aesdec(xmm_result, key_tmp);
3576       }
3577 
3578       __ aesdeclast(xmm_result, xmm_key_last); // xmm15 always came from key+0
3579       __ pxor(xmm_result, xmm_prev_block_cipher); // xor with the current r vector
3580       __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output
3581       // no need to store r to memory until we exit
3582       __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save); // set up next r vector with cipher input from this block
3583       __ addptr(pos, AESBlockSize);
3584       __ subptr(len_reg, AESBlockSize);
3585       __ jcc(Assembler::notEqual, L_singleBlock_loopTop[k]);
3586       if (k != 2) {
3587         __ jmp(L_exit);
3588       }
3589     } //for 128/192/256
3590 
3591     __ BIND(L_exit);
3592     __ movdqu(Address(rvec, 0), xmm_prev_block_cipher);     // final value of r stored in rvec of CipherBlockChaining object
3593     __ pop(rbx);
3594 #ifdef _WIN64
3595     __ movl(rax, len_mem);
3596 #else
3597     __ pop(rax); // return length
3598 #endif
3599     __ leave(); // required for proper stackwalking of RuntimeStub frame
3600     __ ret(0);
3601     return start;
3602 }
3603 
3604   address generate_upper_word_mask() {
3605     __ align(64);
3606     StubCodeMark mark(this, "StubRoutines", "upper_word_mask");
3607     address start = __ pc();
3608     __ emit_data64(0x0000000000000000, relocInfo::none);
3609     __ emit_data64(0xFFFFFFFF00000000, relocInfo::none);
3610     return start;
3611   }
3612 
3613   address generate_shuffle_byte_flip_mask() {
3614     __ align(64);
3615     StubCodeMark mark(this, "StubRoutines", "shuffle_byte_flip_mask");
3616     address start = __ pc();
3617     __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
3618     __ emit_data64(0x0001020304050607, relocInfo::none);
3619     return start;
3620   }
3621 
3622   // ofs and limit are use for multi-block byte array.
3623   // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
3624   address generate_sha1_implCompress(bool multi_block, const char *name) {
3625     __ align(CodeEntryAlignment);
3626     StubCodeMark mark(this, "StubRoutines", name);
3627     address start = __ pc();
3628 
3629     Register buf = c_rarg0;
3630     Register state = c_rarg1;
3631     Register ofs = c_rarg2;
3632     Register limit = c_rarg3;
3633 
3634     const XMMRegister abcd = xmm0;
3635     const XMMRegister e0 = xmm1;
3636     const XMMRegister e1 = xmm2;
3637     const XMMRegister msg0 = xmm3;
3638 
3639     const XMMRegister msg1 = xmm4;
3640     const XMMRegister msg2 = xmm5;
3641     const XMMRegister msg3 = xmm6;
3642     const XMMRegister shuf_mask = xmm7;
3643 
3644     __ enter();
3645 
3646     __ subptr(rsp, 4 * wordSize);
3647 
3648     __ fast_sha1(abcd, e0, e1, msg0, msg1, msg2, msg3, shuf_mask,
3649       buf, state, ofs, limit, rsp, multi_block);
3650 
3651     __ addptr(rsp, 4 * wordSize);
3652 
3653     __ leave();
3654     __ ret(0);
3655     return start;
3656   }
3657 
3658   address generate_pshuffle_byte_flip_mask() {
3659     __ align(64);
3660     StubCodeMark mark(this, "StubRoutines", "pshuffle_byte_flip_mask");
3661     address start = __ pc();
3662     __ emit_data64(0x0405060700010203, relocInfo::none);
3663     __ emit_data64(0x0c0d0e0f08090a0b, relocInfo::none);
3664 
3665     if (VM_Version::supports_avx2()) {
3666       __ emit_data64(0x0405060700010203, relocInfo::none); // second copy
3667       __ emit_data64(0x0c0d0e0f08090a0b, relocInfo::none);
3668       // _SHUF_00BA
3669       __ emit_data64(0x0b0a090803020100, relocInfo::none);
3670       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3671       __ emit_data64(0x0b0a090803020100, relocInfo::none);
3672       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3673       // _SHUF_DC00
3674       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3675       __ emit_data64(0x0b0a090803020100, relocInfo::none);
3676       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3677       __ emit_data64(0x0b0a090803020100, relocInfo::none);
3678     }
3679 
3680     return start;
3681   }
3682 
3683   //Mask for byte-swapping a couple of qwords in an XMM register using (v)pshufb.
3684   address generate_pshuffle_byte_flip_mask_sha512() {
3685     __ align(32);
3686     StubCodeMark mark(this, "StubRoutines", "pshuffle_byte_flip_mask_sha512");
3687     address start = __ pc();
3688     if (VM_Version::supports_avx2()) {
3689       __ emit_data64(0x0001020304050607, relocInfo::none); // PSHUFFLE_BYTE_FLIP_MASK
3690       __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
3691       __ emit_data64(0x1011121314151617, relocInfo::none);
3692       __ emit_data64(0x18191a1b1c1d1e1f, relocInfo::none);
3693       __ emit_data64(0x0000000000000000, relocInfo::none); //MASK_YMM_LO
3694       __ emit_data64(0x0000000000000000, relocInfo::none);
3695       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3696       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3697     }
3698 
3699     return start;
3700   }
3701 
3702 // ofs and limit are use for multi-block byte array.
3703 // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
3704   address generate_sha256_implCompress(bool multi_block, const char *name) {
3705     assert(VM_Version::supports_sha() || VM_Version::supports_avx2(), "");
3706     __ align(CodeEntryAlignment);
3707     StubCodeMark mark(this, "StubRoutines", name);
3708     address start = __ pc();
3709 
3710     Register buf = c_rarg0;
3711     Register state = c_rarg1;
3712     Register ofs = c_rarg2;
3713     Register limit = c_rarg3;
3714 
3715     const XMMRegister msg = xmm0;
3716     const XMMRegister state0 = xmm1;
3717     const XMMRegister state1 = xmm2;
3718     const XMMRegister msgtmp0 = xmm3;
3719 
3720     const XMMRegister msgtmp1 = xmm4;
3721     const XMMRegister msgtmp2 = xmm5;
3722     const XMMRegister msgtmp3 = xmm6;
3723     const XMMRegister msgtmp4 = xmm7;
3724 
3725     const XMMRegister shuf_mask = xmm8;
3726 
3727     __ enter();
3728 
3729     __ subptr(rsp, 4 * wordSize);
3730 
3731     if (VM_Version::supports_sha()) {
3732       __ fast_sha256(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
3733         buf, state, ofs, limit, rsp, multi_block, shuf_mask);
3734     } else if (VM_Version::supports_avx2()) {
3735       __ sha256_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
3736         buf, state, ofs, limit, rsp, multi_block, shuf_mask);
3737     }
3738     __ addptr(rsp, 4 * wordSize);
3739     __ vzeroupper();
3740     __ leave();
3741     __ ret(0);
3742     return start;
3743   }
3744 
3745   address generate_sha512_implCompress(bool multi_block, const char *name) {
3746     assert(VM_Version::supports_avx2(), "");
3747     assert(VM_Version::supports_bmi2(), "");
3748     __ align(CodeEntryAlignment);
3749     StubCodeMark mark(this, "StubRoutines", name);
3750     address start = __ pc();
3751 
3752     Register buf = c_rarg0;
3753     Register state = c_rarg1;
3754     Register ofs = c_rarg2;
3755     Register limit = c_rarg3;
3756 
3757     const XMMRegister msg = xmm0;
3758     const XMMRegister state0 = xmm1;
3759     const XMMRegister state1 = xmm2;
3760     const XMMRegister msgtmp0 = xmm3;
3761     const XMMRegister msgtmp1 = xmm4;
3762     const XMMRegister msgtmp2 = xmm5;
3763     const XMMRegister msgtmp3 = xmm6;
3764     const XMMRegister msgtmp4 = xmm7;
3765 
3766     const XMMRegister shuf_mask = xmm8;
3767 
3768     __ enter();
3769 
3770     __ sha512_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
3771     buf, state, ofs, limit, rsp, multi_block, shuf_mask);
3772 
3773     __ vzeroupper();
3774     __ leave();
3775     __ ret(0);
3776     return start;
3777   }
3778 
3779   // This is a version of CTR/AES crypt which does 6 blocks in a loop at a time
3780   // to hide instruction latency
3781   //
3782   // Arguments:
3783   //
3784   // Inputs:
3785   //   c_rarg0   - source byte array address
3786   //   c_rarg1   - destination byte array address
3787   //   c_rarg2   - K (key) in little endian int array
3788   //   c_rarg3   - counter vector byte array address
3789   //   Linux
3790   //     c_rarg4   -          input length
3791   //     c_rarg5   -          saved encryptedCounter start
3792   //     rbp + 6 * wordSize - saved used length
3793   //   Windows
3794   //     rbp + 6 * wordSize - input length
3795   //     rbp + 7 * wordSize - saved encryptedCounter start
3796   //     rbp + 8 * wordSize - saved used length
3797   //
3798   // Output:
3799   //   rax       - input length
3800   //
3801   address generate_counterMode_AESCrypt_Parallel() {
3802     assert(UseAES, "need AES instructions and misaligned SSE support");
3803     __ align(CodeEntryAlignment);
3804     StubCodeMark mark(this, "StubRoutines", "counterMode_AESCrypt");
3805     address start = __ pc();
3806     const Register from = c_rarg0; // source array address
3807     const Register to = c_rarg1; // destination array address
3808     const Register key = c_rarg2; // key array address
3809     const Register counter = c_rarg3; // counter byte array initialized from counter array address
3810                                       // and updated with the incremented counter in the end
3811 #ifndef _WIN64
3812     const Register len_reg = c_rarg4;
3813     const Register saved_encCounter_start = c_rarg5;
3814     const Register used_addr = r10;
3815     const Address  used_mem(rbp, 2 * wordSize);
3816     const Register used = r11;
3817 #else
3818     const Address len_mem(rbp, 6 * wordSize); // length is on stack on Win64
3819     const Address saved_encCounter_mem(rbp, 7 * wordSize); // length is on stack on Win64
3820     const Address used_mem(rbp, 8 * wordSize); // length is on stack on Win64
3821     const Register len_reg = r10; // pick the first volatile windows register
3822     const Register saved_encCounter_start = r11;
3823     const Register used_addr = r13;
3824     const Register used = r14;
3825 #endif
3826     const Register pos = rax;
3827 
3828     const int PARALLEL_FACTOR = 6;
3829     const XMMRegister xmm_counter_shuf_mask = xmm0;
3830     const XMMRegister xmm_key_shuf_mask = xmm1; // used temporarily to swap key bytes up front
3831     const XMMRegister xmm_curr_counter = xmm2;
3832 
3833     const XMMRegister xmm_key_tmp0 = xmm3;
3834     const XMMRegister xmm_key_tmp1 = xmm4;
3835 
3836     // registers holding the four results in the parallelized loop
3837     const XMMRegister xmm_result0 = xmm5;
3838     const XMMRegister xmm_result1 = xmm6;
3839     const XMMRegister xmm_result2 = xmm7;
3840     const XMMRegister xmm_result3 = xmm8;
3841     const XMMRegister xmm_result4 = xmm9;
3842     const XMMRegister xmm_result5 = xmm10;
3843 
3844     const XMMRegister xmm_from0 = xmm11;
3845     const XMMRegister xmm_from1 = xmm12;
3846     const XMMRegister xmm_from2 = xmm13;
3847     const XMMRegister xmm_from3 = xmm14; //the last one is xmm14. we have to preserve it on WIN64.
3848     const XMMRegister xmm_from4 = xmm3; //reuse xmm3~4. Because xmm_key_tmp0~1 are useless when loading input text
3849     const XMMRegister xmm_from5 = xmm4;
3850 
3851     //for key_128, key_192, key_256
3852     const int rounds[3] = {10, 12, 14};
3853     Label L_exit_preLoop, L_preLoop_start;
3854     Label L_multiBlock_loopTop[3];
3855     Label L_singleBlockLoopTop[3];
3856     Label L__incCounter[3][6]; //for 6 blocks
3857     Label L__incCounter_single[3]; //for single block, key128, key192, key256
3858     Label L_processTail_insr[3], L_processTail_4_insr[3], L_processTail_2_insr[3], L_processTail_1_insr[3], L_processTail_exit_insr[3];
3859     Label L_processTail_4_extr[3], L_processTail_2_extr[3], L_processTail_1_extr[3], L_processTail_exit_extr[3];
3860 
3861     Label L_exit;
3862 
3863     __ enter(); // required for proper stackwalking of RuntimeStub frame
3864 
3865 #ifdef _WIN64
3866     // allocate spill slots for r13, r14
3867     enum {
3868         saved_r13_offset,
3869         saved_r14_offset
3870     };
3871     __ subptr(rsp, 2 * wordSize);
3872     __ movptr(Address(rsp, saved_r13_offset * wordSize), r13);
3873     __ movptr(Address(rsp, saved_r14_offset * wordSize), r14);
3874 
3875     // on win64, fill len_reg from stack position
3876     __ movl(len_reg, len_mem);
3877     __ movptr(saved_encCounter_start, saved_encCounter_mem);
3878     __ movptr(used_addr, used_mem);
3879     __ movl(used, Address(used_addr, 0));
3880 #else
3881     __ push(len_reg); // Save
3882     __ movptr(used_addr, used_mem);
3883     __ movl(used, Address(used_addr, 0));
3884 #endif
3885 
3886     __ push(rbx); // Save RBX
3887     __ movdqu(xmm_curr_counter, Address(counter, 0x00)); // initialize counter with initial counter
3888     __ movdqu(xmm_counter_shuf_mask, ExternalAddress(StubRoutines::x86::counter_shuffle_mask_addr()), pos); // pos as scratch
3889     __ pshufb(xmm_curr_counter, xmm_counter_shuf_mask); //counter is shuffled
3890     __ movptr(pos, 0);
3891 
3892     // Use the partially used encrpyted counter from last invocation
3893     __ BIND(L_preLoop_start);
3894     __ cmpptr(used, 16);
3895     __ jcc(Assembler::aboveEqual, L_exit_preLoop);
3896       __ cmpptr(len_reg, 0);
3897       __ jcc(Assembler::lessEqual, L_exit_preLoop);
3898       __ movb(rbx, Address(saved_encCounter_start, used));
3899       __ xorb(rbx, Address(from, pos));
3900       __ movb(Address(to, pos), rbx);
3901       __ addptr(pos, 1);
3902       __ addptr(used, 1);
3903       __ subptr(len_reg, 1);
3904 
3905     __ jmp(L_preLoop_start);
3906 
3907     __ BIND(L_exit_preLoop);
3908     __ movl(Address(used_addr, 0), used);
3909 
3910     // key length could be only {11, 13, 15} * 4 = {44, 52, 60}
3911     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()), rbx); // rbx as scratch
3912     __ movl(rbx, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3913     __ cmpl(rbx, 52);
3914     __ jcc(Assembler::equal, L_multiBlock_loopTop[1]);
3915     __ cmpl(rbx, 60);
3916     __ jcc(Assembler::equal, L_multiBlock_loopTop[2]);
3917 
3918 #define CTR_DoSix(opc, src_reg)                \
3919     __ opc(xmm_result0, src_reg);              \
3920     __ opc(xmm_result1, src_reg);              \
3921     __ opc(xmm_result2, src_reg);              \
3922     __ opc(xmm_result3, src_reg);              \
3923     __ opc(xmm_result4, src_reg);              \
3924     __ opc(xmm_result5, src_reg);
3925 
3926     // k == 0 :  generate code for key_128
3927     // k == 1 :  generate code for key_192
3928     // k == 2 :  generate code for key_256
3929     for (int k = 0; k < 3; ++k) {
3930       //multi blocks starts here
3931       __ align(OptoLoopAlignment);
3932       __ BIND(L_multiBlock_loopTop[k]);
3933       __ cmpptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // see if at least PARALLEL_FACTOR blocks left
3934       __ jcc(Assembler::less, L_singleBlockLoopTop[k]);
3935       load_key(xmm_key_tmp0, key, 0x00, xmm_key_shuf_mask);
3936 
3937       //load, then increase counters
3938       CTR_DoSix(movdqa, xmm_curr_counter);
3939       inc_counter(rbx, xmm_result1, 0x01, L__incCounter[k][0]);
3940       inc_counter(rbx, xmm_result2, 0x02, L__incCounter[k][1]);
3941       inc_counter(rbx, xmm_result3, 0x03, L__incCounter[k][2]);
3942       inc_counter(rbx, xmm_result4, 0x04, L__incCounter[k][3]);
3943       inc_counter(rbx, xmm_result5,  0x05, L__incCounter[k][4]);
3944       inc_counter(rbx, xmm_curr_counter, 0x06, L__incCounter[k][5]);
3945       CTR_DoSix(pshufb, xmm_counter_shuf_mask); // after increased, shuffled counters back for PXOR
3946       CTR_DoSix(pxor, xmm_key_tmp0);   //PXOR with Round 0 key
3947 
3948       //load two ROUND_KEYs at a time
3949       for (int i = 1; i < rounds[k]; ) {
3950         load_key(xmm_key_tmp1, key, (0x10 * i), xmm_key_shuf_mask);
3951         load_key(xmm_key_tmp0, key, (0x10 * (i+1)), xmm_key_shuf_mask);
3952         CTR_DoSix(aesenc, xmm_key_tmp1);
3953         i++;
3954         if (i != rounds[k]) {
3955           CTR_DoSix(aesenc, xmm_key_tmp0);
3956         } else {
3957           CTR_DoSix(aesenclast, xmm_key_tmp0);
3958         }
3959         i++;
3960       }
3961 
3962       // get next PARALLEL_FACTOR blocks into xmm_result registers
3963       __ movdqu(xmm_from0, Address(from, pos, Address::times_1, 0 * AESBlockSize));
3964       __ movdqu(xmm_from1, Address(from, pos, Address::times_1, 1 * AESBlockSize));
3965       __ movdqu(xmm_from2, Address(from, pos, Address::times_1, 2 * AESBlockSize));
3966       __ movdqu(xmm_from3, Address(from, pos, Address::times_1, 3 * AESBlockSize));
3967       __ movdqu(xmm_from4, Address(from, pos, Address::times_1, 4 * AESBlockSize));
3968       __ movdqu(xmm_from5, Address(from, pos, Address::times_1, 5 * AESBlockSize));
3969 
3970       __ pxor(xmm_result0, xmm_from0);
3971       __ pxor(xmm_result1, xmm_from1);
3972       __ pxor(xmm_result2, xmm_from2);
3973       __ pxor(xmm_result3, xmm_from3);
3974       __ pxor(xmm_result4, xmm_from4);
3975       __ pxor(xmm_result5, xmm_from5);
3976 
3977       // store 6 results into the next 64 bytes of output
3978       __ movdqu(Address(to, pos, Address::times_1, 0 * AESBlockSize), xmm_result0);
3979       __ movdqu(Address(to, pos, Address::times_1, 1 * AESBlockSize), xmm_result1);
3980       __ movdqu(Address(to, pos, Address::times_1, 2 * AESBlockSize), xmm_result2);
3981       __ movdqu(Address(to, pos, Address::times_1, 3 * AESBlockSize), xmm_result3);
3982       __ movdqu(Address(to, pos, Address::times_1, 4 * AESBlockSize), xmm_result4);
3983       __ movdqu(Address(to, pos, Address::times_1, 5 * AESBlockSize), xmm_result5);
3984 
3985       __ addptr(pos, PARALLEL_FACTOR * AESBlockSize); // increase the length of crypt text
3986       __ subptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // decrease the remaining length
3987       __ jmp(L_multiBlock_loopTop[k]);
3988 
3989       // singleBlock starts here
3990       __ align(OptoLoopAlignment);
3991       __ BIND(L_singleBlockLoopTop[k]);
3992       __ cmpptr(len_reg, 0);
3993       __ jcc(Assembler::lessEqual, L_exit);
3994       load_key(xmm_key_tmp0, key, 0x00, xmm_key_shuf_mask);
3995       __ movdqa(xmm_result0, xmm_curr_counter);
3996       inc_counter(rbx, xmm_curr_counter, 0x01, L__incCounter_single[k]);
3997       __ pshufb(xmm_result0, xmm_counter_shuf_mask);
3998       __ pxor(xmm_result0, xmm_key_tmp0);
3999       for (int i = 1; i < rounds[k]; i++) {
4000         load_key(xmm_key_tmp0, key, (0x10 * i), xmm_key_shuf_mask);
4001         __ aesenc(xmm_result0, xmm_key_tmp0);
4002       }
4003       load_key(xmm_key_tmp0, key, (rounds[k] * 0x10), xmm_key_shuf_mask);
4004       __ aesenclast(xmm_result0, xmm_key_tmp0);
4005       __ cmpptr(len_reg, AESBlockSize);
4006       __ jcc(Assembler::less, L_processTail_insr[k]);
4007         __ movdqu(xmm_from0, Address(from, pos, Address::times_1, 0 * AESBlockSize));
4008         __ pxor(xmm_result0, xmm_from0);
4009         __ movdqu(Address(to, pos, Address::times_1, 0 * AESBlockSize), xmm_result0);
4010         __ addptr(pos, AESBlockSize);
4011         __ subptr(len_reg, AESBlockSize);
4012         __ jmp(L_singleBlockLoopTop[k]);
4013       __ BIND(L_processTail_insr[k]);                               // Process the tail part of the input array
4014         __ addptr(pos, len_reg);                                    // 1. Insert bytes from src array into xmm_from0 register
4015         __ testptr(len_reg, 8);
4016         __ jcc(Assembler::zero, L_processTail_4_insr[k]);
4017           __ subptr(pos,8);
4018           __ pinsrq(xmm_from0, Address(from, pos), 0);
4019         __ BIND(L_processTail_4_insr[k]);
4020         __ testptr(len_reg, 4);
4021         __ jcc(Assembler::zero, L_processTail_2_insr[k]);
4022           __ subptr(pos,4);
4023           __ pslldq(xmm_from0, 4);
4024           __ pinsrd(xmm_from0, Address(from, pos), 0);
4025         __ BIND(L_processTail_2_insr[k]);
4026         __ testptr(len_reg, 2);
4027         __ jcc(Assembler::zero, L_processTail_1_insr[k]);
4028           __ subptr(pos, 2);
4029           __ pslldq(xmm_from0, 2);
4030           __ pinsrw(xmm_from0, Address(from, pos), 0);
4031         __ BIND(L_processTail_1_insr[k]);
4032         __ testptr(len_reg, 1);
4033         __ jcc(Assembler::zero, L_processTail_exit_insr[k]);
4034           __ subptr(pos, 1);
4035           __ pslldq(xmm_from0, 1);
4036           __ pinsrb(xmm_from0, Address(from, pos), 0);
4037         __ BIND(L_processTail_exit_insr[k]);
4038 
4039         __ movdqu(Address(saved_encCounter_start, 0), xmm_result0);  // 2. Perform pxor of the encrypted counter and plaintext Bytes.
4040         __ pxor(xmm_result0, xmm_from0);                             //    Also the encrypted counter is saved for next invocation.
4041 
4042         __ testptr(len_reg, 8);
4043         __ jcc(Assembler::zero, L_processTail_4_extr[k]);            // 3. Extract bytes from xmm_result0 into the dest. array
4044           __ pextrq(Address(to, pos), xmm_result0, 0);
4045           __ psrldq(xmm_result0, 8);
4046           __ addptr(pos, 8);
4047         __ BIND(L_processTail_4_extr[k]);
4048         __ testptr(len_reg, 4);
4049         __ jcc(Assembler::zero, L_processTail_2_extr[k]);
4050           __ pextrd(Address(to, pos), xmm_result0, 0);
4051           __ psrldq(xmm_result0, 4);
4052           __ addptr(pos, 4);
4053         __ BIND(L_processTail_2_extr[k]);
4054         __ testptr(len_reg, 2);
4055         __ jcc(Assembler::zero, L_processTail_1_extr[k]);
4056           __ pextrw(Address(to, pos), xmm_result0, 0);
4057           __ psrldq(xmm_result0, 2);
4058           __ addptr(pos, 2);
4059         __ BIND(L_processTail_1_extr[k]);
4060         __ testptr(len_reg, 1);
4061         __ jcc(Assembler::zero, L_processTail_exit_extr[k]);
4062           __ pextrb(Address(to, pos), xmm_result0, 0);
4063 
4064         __ BIND(L_processTail_exit_extr[k]);
4065         __ movl(Address(used_addr, 0), len_reg);
4066         __ jmp(L_exit);
4067 
4068     }
4069 
4070     __ BIND(L_exit);
4071     __ pshufb(xmm_curr_counter, xmm_counter_shuf_mask); //counter is shuffled back.
4072     __ movdqu(Address(counter, 0), xmm_curr_counter); //save counter back
4073     __ pop(rbx); // pop the saved RBX.
4074 #ifdef _WIN64
4075     __ movl(rax, len_mem);
4076     __ movptr(r13, Address(rsp, saved_r13_offset * wordSize));
4077     __ movptr(r14, Address(rsp, saved_r14_offset * wordSize));
4078     __ addptr(rsp, 2 * wordSize);
4079 #else
4080     __ pop(rax); // return 'len'
4081 #endif
4082     __ leave(); // required for proper stackwalking of RuntimeStub frame
4083     __ ret(0);
4084     return start;
4085   }
4086 
4087 void roundDec(XMMRegister xmm_reg) {
4088   __ vaesdec(xmm1, xmm1, xmm_reg, Assembler::AVX_512bit);
4089   __ vaesdec(xmm2, xmm2, xmm_reg, Assembler::AVX_512bit);
4090   __ vaesdec(xmm3, xmm3, xmm_reg, Assembler::AVX_512bit);
4091   __ vaesdec(xmm4, xmm4, xmm_reg, Assembler::AVX_512bit);
4092   __ vaesdec(xmm5, xmm5, xmm_reg, Assembler::AVX_512bit);
4093   __ vaesdec(xmm6, xmm6, xmm_reg, Assembler::AVX_512bit);
4094   __ vaesdec(xmm7, xmm7, xmm_reg, Assembler::AVX_512bit);
4095   __ vaesdec(xmm8, xmm8, xmm_reg, Assembler::AVX_512bit);
4096 }
4097 
4098 void roundDeclast(XMMRegister xmm_reg) {
4099   __ vaesdeclast(xmm1, xmm1, xmm_reg, Assembler::AVX_512bit);
4100   __ vaesdeclast(xmm2, xmm2, xmm_reg, Assembler::AVX_512bit);
4101   __ vaesdeclast(xmm3, xmm3, xmm_reg, Assembler::AVX_512bit);
4102   __ vaesdeclast(xmm4, xmm4, xmm_reg, Assembler::AVX_512bit);
4103   __ vaesdeclast(xmm5, xmm5, xmm_reg, Assembler::AVX_512bit);
4104   __ vaesdeclast(xmm6, xmm6, xmm_reg, Assembler::AVX_512bit);
4105   __ vaesdeclast(xmm7, xmm7, xmm_reg, Assembler::AVX_512bit);
4106   __ vaesdeclast(xmm8, xmm8, xmm_reg, Assembler::AVX_512bit);
4107 }
4108 
4109   void ev_load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask = NULL) {
4110     __ movdqu(xmmdst, Address(key, offset));
4111     if (xmm_shuf_mask != NULL) {
4112       __ pshufb(xmmdst, xmm_shuf_mask);
4113     } else {
4114       __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
4115     }
4116     __ evshufi64x2(xmmdst, xmmdst, xmmdst, 0x0, Assembler::AVX_512bit);
4117 
4118   }
4119 
4120 address generate_cipherBlockChaining_decryptVectorAESCrypt() {
4121     assert(VM_Version::supports_vaes(), "need AES instructions and misaligned SSE support");
4122     __ align(CodeEntryAlignment);
4123     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt");
4124     address start = __ pc();
4125 
4126     const Register from = c_rarg0;  // source array address
4127     const Register to = c_rarg1;  // destination array address
4128     const Register key = c_rarg2;  // key array address
4129     const Register rvec = c_rarg3;  // r byte array initialized from initvector array address
4130     // and left with the results of the last encryption block
4131 #ifndef _WIN64
4132     const Register len_reg = c_rarg4;  // src len (must be multiple of blocksize 16)
4133 #else
4134     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
4135     const Register len_reg = r11;      // pick the volatile windows register
4136 #endif
4137 
4138     Label Loop, Loop1, L_128, L_256, L_192, KEY_192, KEY_256, Loop2, Lcbc_dec_rem_loop,
4139           Lcbc_dec_rem_last, Lcbc_dec_ret, Lcbc_dec_rem, Lcbc_exit;
4140 
4141     __ enter();
4142 
4143 #ifdef _WIN64
4144   // on win64, fill len_reg from stack position
4145     __ movl(len_reg, len_mem);
4146 #else
4147     __ push(len_reg); // Save
4148 #endif
4149     __ push(rbx);
4150     __ vzeroupper();
4151 
4152     // Temporary variable declaration for swapping key bytes
4153     const XMMRegister xmm_key_shuf_mask = xmm1;
4154     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
4155 
4156     // Calculate number of rounds from key size: 44 for 10-rounds, 52 for 12-rounds, 60 for 14-rounds
4157     const Register rounds = rbx;
4158     __ movl(rounds, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
4159 
4160     const XMMRegister IV = xmm0;
4161     // Load IV and broadcast value to 512-bits
4162     __ evbroadcasti64x2(IV, Address(rvec, 0), Assembler::AVX_512bit);
4163 
4164     // Temporary variables for storing round keys
4165     const XMMRegister RK0 = xmm30;
4166     const XMMRegister RK1 = xmm9;
4167     const XMMRegister RK2 = xmm18;
4168     const XMMRegister RK3 = xmm19;
4169     const XMMRegister RK4 = xmm20;
4170     const XMMRegister RK5 = xmm21;
4171     const XMMRegister RK6 = xmm22;
4172     const XMMRegister RK7 = xmm23;
4173     const XMMRegister RK8 = xmm24;
4174     const XMMRegister RK9 = xmm25;
4175     const XMMRegister RK10 = xmm26;
4176 
4177      // Load and shuffle key
4178     // the java expanded key ordering is rotated one position from what we want
4179     // so we start from 1*16 here and hit 0*16 last
4180     ev_load_key(RK1, key, 1 * 16, xmm_key_shuf_mask);
4181     ev_load_key(RK2, key, 2 * 16, xmm_key_shuf_mask);
4182     ev_load_key(RK3, key, 3 * 16, xmm_key_shuf_mask);
4183     ev_load_key(RK4, key, 4 * 16, xmm_key_shuf_mask);
4184     ev_load_key(RK5, key, 5 * 16, xmm_key_shuf_mask);
4185     ev_load_key(RK6, key, 6 * 16, xmm_key_shuf_mask);
4186     ev_load_key(RK7, key, 7 * 16, xmm_key_shuf_mask);
4187     ev_load_key(RK8, key, 8 * 16, xmm_key_shuf_mask);
4188     ev_load_key(RK9, key, 9 * 16, xmm_key_shuf_mask);
4189     ev_load_key(RK10, key, 10 * 16, xmm_key_shuf_mask);
4190     ev_load_key(RK0, key, 0*16, xmm_key_shuf_mask);
4191 
4192     // Variables for storing source cipher text
4193     const XMMRegister S0 = xmm10;
4194     const XMMRegister S1 = xmm11;
4195     const XMMRegister S2 = xmm12;
4196     const XMMRegister S3 = xmm13;
4197     const XMMRegister S4 = xmm14;
4198     const XMMRegister S5 = xmm15;
4199     const XMMRegister S6 = xmm16;
4200     const XMMRegister S7 = xmm17;
4201 
4202     // Variables for storing decrypted text
4203     const XMMRegister B0 = xmm1;
4204     const XMMRegister B1 = xmm2;
4205     const XMMRegister B2 = xmm3;
4206     const XMMRegister B3 = xmm4;
4207     const XMMRegister B4 = xmm5;
4208     const XMMRegister B5 = xmm6;
4209     const XMMRegister B6 = xmm7;
4210     const XMMRegister B7 = xmm8;
4211 
4212     __ cmpl(rounds, 44);
4213     __ jcc(Assembler::greater, KEY_192);
4214     __ jmp(Loop);
4215 
4216     __ BIND(KEY_192);
4217     const XMMRegister RK11 = xmm27;
4218     const XMMRegister RK12 = xmm28;
4219     ev_load_key(RK11, key, 11*16, xmm_key_shuf_mask);
4220     ev_load_key(RK12, key, 12*16, xmm_key_shuf_mask);
4221 
4222     __ cmpl(rounds, 52);
4223     __ jcc(Assembler::greater, KEY_256);
4224     __ jmp(Loop);
4225 
4226     __ BIND(KEY_256);
4227     const XMMRegister RK13 = xmm29;
4228     const XMMRegister RK14 = xmm31;
4229     ev_load_key(RK13, key, 13*16, xmm_key_shuf_mask);
4230     ev_load_key(RK14, key, 14*16, xmm_key_shuf_mask);
4231 
4232     __ BIND(Loop);
4233     __ cmpl(len_reg, 512);
4234     __ jcc(Assembler::below, Lcbc_dec_rem);
4235     __ BIND(Loop1);
4236     __ subl(len_reg, 512);
4237     __ evmovdquq(S0, Address(from, 0 * 64), Assembler::AVX_512bit);
4238     __ evmovdquq(S1, Address(from, 1 * 64), Assembler::AVX_512bit);
4239     __ evmovdquq(S2, Address(from, 2 * 64), Assembler::AVX_512bit);
4240     __ evmovdquq(S3, Address(from, 3 * 64), Assembler::AVX_512bit);
4241     __ evmovdquq(S4, Address(from, 4 * 64), Assembler::AVX_512bit);
4242     __ evmovdquq(S5, Address(from, 5 * 64), Assembler::AVX_512bit);
4243     __ evmovdquq(S6, Address(from, 6 * 64), Assembler::AVX_512bit);
4244     __ evmovdquq(S7, Address(from, 7 * 64), Assembler::AVX_512bit);
4245     __ leaq(from, Address(from, 8 * 64));
4246 
4247     __ evpxorq(B0, S0, RK1, Assembler::AVX_512bit);
4248     __ evpxorq(B1, S1, RK1, Assembler::AVX_512bit);
4249     __ evpxorq(B2, S2, RK1, Assembler::AVX_512bit);
4250     __ evpxorq(B3, S3, RK1, Assembler::AVX_512bit);
4251     __ evpxorq(B4, S4, RK1, Assembler::AVX_512bit);
4252     __ evpxorq(B5, S5, RK1, Assembler::AVX_512bit);
4253     __ evpxorq(B6, S6, RK1, Assembler::AVX_512bit);
4254     __ evpxorq(B7, S7, RK1, Assembler::AVX_512bit);
4255 
4256     __ evalignq(IV, S0, IV, 0x06);
4257     __ evalignq(S0, S1, S0, 0x06);
4258     __ evalignq(S1, S2, S1, 0x06);
4259     __ evalignq(S2, S3, S2, 0x06);
4260     __ evalignq(S3, S4, S3, 0x06);
4261     __ evalignq(S4, S5, S4, 0x06);
4262     __ evalignq(S5, S6, S5, 0x06);
4263     __ evalignq(S6, S7, S6, 0x06);
4264 
4265     roundDec(RK2);
4266     roundDec(RK3);
4267     roundDec(RK4);
4268     roundDec(RK5);
4269     roundDec(RK6);
4270     roundDec(RK7);
4271     roundDec(RK8);
4272     roundDec(RK9);
4273     roundDec(RK10);
4274 
4275     __ cmpl(rounds, 44);
4276     __ jcc(Assembler::belowEqual, L_128);
4277     roundDec(RK11);
4278     roundDec(RK12);
4279 
4280     __ cmpl(rounds, 52);
4281     __ jcc(Assembler::belowEqual, L_192);
4282     roundDec(RK13);
4283     roundDec(RK14);
4284 
4285     __ BIND(L_256);
4286     roundDeclast(RK0);
4287     __ jmp(Loop2);
4288 
4289     __ BIND(L_128);
4290     roundDeclast(RK0);
4291     __ jmp(Loop2);
4292 
4293     __ BIND(L_192);
4294     roundDeclast(RK0);
4295 
4296     __ BIND(Loop2);
4297     __ evpxorq(B0, B0, IV, Assembler::AVX_512bit);
4298     __ evpxorq(B1, B1, S0, Assembler::AVX_512bit);
4299     __ evpxorq(B2, B2, S1, Assembler::AVX_512bit);
4300     __ evpxorq(B3, B3, S2, Assembler::AVX_512bit);
4301     __ evpxorq(B4, B4, S3, Assembler::AVX_512bit);
4302     __ evpxorq(B5, B5, S4, Assembler::AVX_512bit);
4303     __ evpxorq(B6, B6, S5, Assembler::AVX_512bit);
4304     __ evpxorq(B7, B7, S6, Assembler::AVX_512bit);
4305     __ evmovdquq(IV, S7, Assembler::AVX_512bit);
4306 
4307     __ evmovdquq(Address(to, 0 * 64), B0, Assembler::AVX_512bit);
4308     __ evmovdquq(Address(to, 1 * 64), B1, Assembler::AVX_512bit);
4309     __ evmovdquq(Address(to, 2 * 64), B2, Assembler::AVX_512bit);
4310     __ evmovdquq(Address(to, 3 * 64), B3, Assembler::AVX_512bit);
4311     __ evmovdquq(Address(to, 4 * 64), B4, Assembler::AVX_512bit);
4312     __ evmovdquq(Address(to, 5 * 64), B5, Assembler::AVX_512bit);
4313     __ evmovdquq(Address(to, 6 * 64), B6, Assembler::AVX_512bit);
4314     __ evmovdquq(Address(to, 7 * 64), B7, Assembler::AVX_512bit);
4315     __ leaq(to, Address(to, 8 * 64));
4316     __ jmp(Loop);
4317 
4318     __ BIND(Lcbc_dec_rem);
4319     __ evshufi64x2(IV, IV, IV, 0x03, Assembler::AVX_512bit);
4320 
4321     __ BIND(Lcbc_dec_rem_loop);
4322     __ subl(len_reg, 16);
4323     __ jcc(Assembler::carrySet, Lcbc_dec_ret);
4324 
4325     __ movdqu(S0, Address(from, 0));
4326     __ evpxorq(B0, S0, RK1, Assembler::AVX_512bit);
4327     __ vaesdec(B0, B0, RK2, Assembler::AVX_512bit);
4328     __ vaesdec(B0, B0, RK3, Assembler::AVX_512bit);
4329     __ vaesdec(B0, B0, RK4, Assembler::AVX_512bit);
4330     __ vaesdec(B0, B0, RK5, Assembler::AVX_512bit);
4331     __ vaesdec(B0, B0, RK6, Assembler::AVX_512bit);
4332     __ vaesdec(B0, B0, RK7, Assembler::AVX_512bit);
4333     __ vaesdec(B0, B0, RK8, Assembler::AVX_512bit);
4334     __ vaesdec(B0, B0, RK9, Assembler::AVX_512bit);
4335     __ vaesdec(B0, B0, RK10, Assembler::AVX_512bit);
4336     __ cmpl(rounds, 44);
4337     __ jcc(Assembler::belowEqual, Lcbc_dec_rem_last);
4338 
4339     __ vaesdec(B0, B0, RK11, Assembler::AVX_512bit);
4340     __ vaesdec(B0, B0, RK12, Assembler::AVX_512bit);
4341     __ cmpl(rounds, 52);
4342     __ jcc(Assembler::belowEqual, Lcbc_dec_rem_last);
4343 
4344     __ vaesdec(B0, B0, RK13, Assembler::AVX_512bit);
4345     __ vaesdec(B0, B0, RK14, Assembler::AVX_512bit);
4346 
4347     __ BIND(Lcbc_dec_rem_last);
4348     __ vaesdeclast(B0, B0, RK0, Assembler::AVX_512bit);
4349 
4350     __ evpxorq(B0, B0, IV, Assembler::AVX_512bit);
4351     __ evmovdquq(IV, S0, Assembler::AVX_512bit);
4352     __ movdqu(Address(to, 0), B0);
4353     __ leaq(from, Address(from, 16));
4354     __ leaq(to, Address(to, 16));
4355     __ jmp(Lcbc_dec_rem_loop);
4356 
4357     __ BIND(Lcbc_dec_ret);
4358     __ movdqu(Address(rvec, 0), IV);
4359 
4360     // Zero out the round keys
4361     __ evpxorq(RK0, RK0, RK0, Assembler::AVX_512bit);
4362     __ evpxorq(RK1, RK1, RK1, Assembler::AVX_512bit);
4363     __ evpxorq(RK2, RK2, RK2, Assembler::AVX_512bit);
4364     __ evpxorq(RK3, RK3, RK3, Assembler::AVX_512bit);
4365     __ evpxorq(RK4, RK4, RK4, Assembler::AVX_512bit);
4366     __ evpxorq(RK5, RK5, RK5, Assembler::AVX_512bit);
4367     __ evpxorq(RK6, RK6, RK6, Assembler::AVX_512bit);
4368     __ evpxorq(RK7, RK7, RK7, Assembler::AVX_512bit);
4369     __ evpxorq(RK8, RK8, RK8, Assembler::AVX_512bit);
4370     __ evpxorq(RK9, RK9, RK9, Assembler::AVX_512bit);
4371     __ evpxorq(RK10, RK10, RK10, Assembler::AVX_512bit);
4372     __ cmpl(rounds, 44);
4373     __ jcc(Assembler::belowEqual, Lcbc_exit);
4374     __ evpxorq(RK11, RK11, RK11, Assembler::AVX_512bit);
4375     __ evpxorq(RK12, RK12, RK12, Assembler::AVX_512bit);
4376     __ cmpl(rounds, 52);
4377     __ jcc(Assembler::belowEqual, Lcbc_exit);
4378     __ evpxorq(RK13, RK13, RK13, Assembler::AVX_512bit);
4379     __ evpxorq(RK14, RK14, RK14, Assembler::AVX_512bit);
4380 
4381     __ BIND(Lcbc_exit);
4382     __ pop(rbx);
4383 #ifdef _WIN64
4384     __ movl(rax, len_mem);
4385 #else
4386     __ pop(rax); // return length
4387 #endif
4388     __ leave(); // required for proper stackwalking of RuntimeStub frame
4389     __ ret(0);
4390     return start;
4391 }
4392 
4393 // Polynomial x^128+x^127+x^126+x^121+1
4394 address ghash_polynomial_addr() {
4395     __ align(CodeEntryAlignment);
4396     StubCodeMark mark(this, "StubRoutines", "_ghash_poly_addr");
4397     address start = __ pc();
4398     __ emit_data64(0x0000000000000001, relocInfo::none);
4399     __ emit_data64(0xc200000000000000, relocInfo::none);
4400     return start;
4401 }
4402 
4403 address ghash_shufflemask_addr() {
4404     __ align(CodeEntryAlignment);
4405     StubCodeMark mark(this, "StubRoutines", "_ghash_shuffmask_addr");
4406     address start = __ pc();
4407     __ emit_data64(0x0f0f0f0f0f0f0f0f, relocInfo::none);
4408     __ emit_data64(0x0f0f0f0f0f0f0f0f, relocInfo::none);
4409     return start;
4410 }
4411 
4412 // Ghash single and multi block operations using AVX instructions
4413 address generate_avx_ghash_processBlocks() {
4414     __ align(CodeEntryAlignment);
4415 
4416     StubCodeMark mark(this, "StubRoutines", "ghash_processBlocks");
4417     address start = __ pc();
4418 
4419     // arguments
4420     const Register state = c_rarg0;
4421     const Register htbl = c_rarg1;
4422     const Register data = c_rarg2;
4423     const Register blocks = c_rarg3;
4424     __ enter();
4425    // Save state before entering routine
4426     __ avx_ghash(state, htbl, data, blocks);
4427     __ leave(); // required for proper stackwalking of RuntimeStub frame
4428     __ ret(0);
4429     return start;
4430 }
4431 
4432   // byte swap x86 long
4433   address generate_ghash_long_swap_mask() {
4434     __ align(CodeEntryAlignment);
4435     StubCodeMark mark(this, "StubRoutines", "ghash_long_swap_mask");
4436     address start = __ pc();
4437     __ emit_data64(0x0f0e0d0c0b0a0908, relocInfo::none );
4438     __ emit_data64(0x0706050403020100, relocInfo::none );
4439   return start;
4440   }
4441 
4442   // byte swap x86 byte array
4443   address generate_ghash_byte_swap_mask() {
4444     __ align(CodeEntryAlignment);
4445     StubCodeMark mark(this, "StubRoutines", "ghash_byte_swap_mask");
4446     address start = __ pc();
4447     __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none );
4448     __ emit_data64(0x0001020304050607, relocInfo::none );
4449   return start;
4450   }
4451 
4452   /* Single and multi-block ghash operations */
4453   address generate_ghash_processBlocks() {
4454     __ align(CodeEntryAlignment);
4455     Label L_ghash_loop, L_exit;
4456     StubCodeMark mark(this, "StubRoutines", "ghash_processBlocks");
4457     address start = __ pc();
4458 
4459     const Register state        = c_rarg0;
4460     const Register subkeyH      = c_rarg1;
4461     const Register data         = c_rarg2;
4462     const Register blocks       = c_rarg3;
4463 
4464     const XMMRegister xmm_temp0 = xmm0;
4465     const XMMRegister xmm_temp1 = xmm1;
4466     const XMMRegister xmm_temp2 = xmm2;
4467     const XMMRegister xmm_temp3 = xmm3;
4468     const XMMRegister xmm_temp4 = xmm4;
4469     const XMMRegister xmm_temp5 = xmm5;
4470     const XMMRegister xmm_temp6 = xmm6;
4471     const XMMRegister xmm_temp7 = xmm7;
4472     const XMMRegister xmm_temp8 = xmm8;
4473     const XMMRegister xmm_temp9 = xmm9;
4474     const XMMRegister xmm_temp10 = xmm10;
4475 
4476     __ enter();
4477 
4478     __ movdqu(xmm_temp10, ExternalAddress(StubRoutines::x86::ghash_long_swap_mask_addr()));
4479 
4480     __ movdqu(xmm_temp0, Address(state, 0));
4481     __ pshufb(xmm_temp0, xmm_temp10);
4482 
4483 
4484     __ BIND(L_ghash_loop);
4485     __ movdqu(xmm_temp2, Address(data, 0));
4486     __ pshufb(xmm_temp2, ExternalAddress(StubRoutines::x86::ghash_byte_swap_mask_addr()));
4487 
4488     __ movdqu(xmm_temp1, Address(subkeyH, 0));
4489     __ pshufb(xmm_temp1, xmm_temp10);
4490 
4491     __ pxor(xmm_temp0, xmm_temp2);
4492 
4493     //
4494     // Multiply with the hash key
4495     //
4496     __ movdqu(xmm_temp3, xmm_temp0);
4497     __ pclmulqdq(xmm_temp3, xmm_temp1, 0);      // xmm3 holds a0*b0
4498     __ movdqu(xmm_temp4, xmm_temp0);
4499     __ pclmulqdq(xmm_temp4, xmm_temp1, 16);     // xmm4 holds a0*b1
4500 
4501     __ movdqu(xmm_temp5, xmm_temp0);
4502     __ pclmulqdq(xmm_temp5, xmm_temp1, 1);      // xmm5 holds a1*b0
4503     __ movdqu(xmm_temp6, xmm_temp0);
4504     __ pclmulqdq(xmm_temp6, xmm_temp1, 17);     // xmm6 holds a1*b1
4505 
4506     __ pxor(xmm_temp4, xmm_temp5);      // xmm4 holds a0*b1 + a1*b0
4507 
4508     __ movdqu(xmm_temp5, xmm_temp4);    // move the contents of xmm4 to xmm5
4509     __ psrldq(xmm_temp4, 8);    // shift by xmm4 64 bits to the right
4510     __ pslldq(xmm_temp5, 8);    // shift by xmm5 64 bits to the left
4511     __ pxor(xmm_temp3, xmm_temp5);
4512     __ pxor(xmm_temp6, xmm_temp4);      // Register pair <xmm6:xmm3> holds the result
4513                                         // of the carry-less multiplication of
4514                                         // xmm0 by xmm1.
4515 
4516     // We shift the result of the multiplication by one bit position
4517     // to the left to cope for the fact that the bits are reversed.
4518     __ movdqu(xmm_temp7, xmm_temp3);
4519     __ movdqu(xmm_temp8, xmm_temp6);
4520     __ pslld(xmm_temp3, 1);
4521     __ pslld(xmm_temp6, 1);
4522     __ psrld(xmm_temp7, 31);
4523     __ psrld(xmm_temp8, 31);
4524     __ movdqu(xmm_temp9, xmm_temp7);
4525     __ pslldq(xmm_temp8, 4);
4526     __ pslldq(xmm_temp7, 4);
4527     __ psrldq(xmm_temp9, 12);
4528     __ por(xmm_temp3, xmm_temp7);
4529     __ por(xmm_temp6, xmm_temp8);
4530     __ por(xmm_temp6, xmm_temp9);
4531 
4532     //
4533     // First phase of the reduction
4534     //
4535     // Move xmm3 into xmm7, xmm8, xmm9 in order to perform the shifts
4536     // independently.
4537     __ movdqu(xmm_temp7, xmm_temp3);
4538     __ movdqu(xmm_temp8, xmm_temp3);
4539     __ movdqu(xmm_temp9, xmm_temp3);
4540     __ pslld(xmm_temp7, 31);    // packed right shift shifting << 31
4541     __ pslld(xmm_temp8, 30);    // packed right shift shifting << 30
4542     __ pslld(xmm_temp9, 25);    // packed right shift shifting << 25
4543     __ pxor(xmm_temp7, xmm_temp8);      // xor the shifted versions
4544     __ pxor(xmm_temp7, xmm_temp9);
4545     __ movdqu(xmm_temp8, xmm_temp7);
4546     __ pslldq(xmm_temp7, 12);
4547     __ psrldq(xmm_temp8, 4);
4548     __ pxor(xmm_temp3, xmm_temp7);      // first phase of the reduction complete
4549 
4550     //
4551     // Second phase of the reduction
4552     //
4553     // Make 3 copies of xmm3 in xmm2, xmm4, xmm5 for doing these
4554     // shift operations.
4555     __ movdqu(xmm_temp2, xmm_temp3);
4556     __ movdqu(xmm_temp4, xmm_temp3);
4557     __ movdqu(xmm_temp5, xmm_temp3);
4558     __ psrld(xmm_temp2, 1);     // packed left shifting >> 1
4559     __ psrld(xmm_temp4, 2);     // packed left shifting >> 2
4560     __ psrld(xmm_temp5, 7);     // packed left shifting >> 7
4561     __ pxor(xmm_temp2, xmm_temp4);      // xor the shifted versions
4562     __ pxor(xmm_temp2, xmm_temp5);
4563     __ pxor(xmm_temp2, xmm_temp8);
4564     __ pxor(xmm_temp3, xmm_temp2);
4565     __ pxor(xmm_temp6, xmm_temp3);      // the result is in xmm6
4566 
4567     __ decrement(blocks);
4568     __ jcc(Assembler::zero, L_exit);
4569     __ movdqu(xmm_temp0, xmm_temp6);
4570     __ addptr(data, 16);
4571     __ jmp(L_ghash_loop);
4572 
4573     __ BIND(L_exit);
4574     __ pshufb(xmm_temp6, xmm_temp10);          // Byte swap 16-byte result
4575     __ movdqu(Address(state, 0), xmm_temp6);   // store the result
4576     __ leave();
4577     __ ret(0);
4578     return start;
4579   }
4580 
4581   //base64 character set
4582   address base64_charset_addr() {
4583     __ align(CodeEntryAlignment);
4584     StubCodeMark mark(this, "StubRoutines", "base64_charset");
4585     address start = __ pc();
4586     __ emit_data64(0x0000004200000041, relocInfo::none);
4587     __ emit_data64(0x0000004400000043, relocInfo::none);
4588     __ emit_data64(0x0000004600000045, relocInfo::none);
4589     __ emit_data64(0x0000004800000047, relocInfo::none);
4590     __ emit_data64(0x0000004a00000049, relocInfo::none);
4591     __ emit_data64(0x0000004c0000004b, relocInfo::none);
4592     __ emit_data64(0x0000004e0000004d, relocInfo::none);
4593     __ emit_data64(0x000000500000004f, relocInfo::none);
4594     __ emit_data64(0x0000005200000051, relocInfo::none);
4595     __ emit_data64(0x0000005400000053, relocInfo::none);
4596     __ emit_data64(0x0000005600000055, relocInfo::none);
4597     __ emit_data64(0x0000005800000057, relocInfo::none);
4598     __ emit_data64(0x0000005a00000059, relocInfo::none);
4599     __ emit_data64(0x0000006200000061, relocInfo::none);
4600     __ emit_data64(0x0000006400000063, relocInfo::none);
4601     __ emit_data64(0x0000006600000065, relocInfo::none);
4602     __ emit_data64(0x0000006800000067, relocInfo::none);
4603     __ emit_data64(0x0000006a00000069, relocInfo::none);
4604     __ emit_data64(0x0000006c0000006b, relocInfo::none);
4605     __ emit_data64(0x0000006e0000006d, relocInfo::none);
4606     __ emit_data64(0x000000700000006f, relocInfo::none);
4607     __ emit_data64(0x0000007200000071, relocInfo::none);
4608     __ emit_data64(0x0000007400000073, relocInfo::none);
4609     __ emit_data64(0x0000007600000075, relocInfo::none);
4610     __ emit_data64(0x0000007800000077, relocInfo::none);
4611     __ emit_data64(0x0000007a00000079, relocInfo::none);
4612     __ emit_data64(0x0000003100000030, relocInfo::none);
4613     __ emit_data64(0x0000003300000032, relocInfo::none);
4614     __ emit_data64(0x0000003500000034, relocInfo::none);
4615     __ emit_data64(0x0000003700000036, relocInfo::none);
4616     __ emit_data64(0x0000003900000038, relocInfo::none);
4617     __ emit_data64(0x0000002f0000002b, relocInfo::none);
4618     return start;
4619   }
4620 
4621   //base64 url character set
4622   address base64url_charset_addr() {
4623     __ align(CodeEntryAlignment);
4624     StubCodeMark mark(this, "StubRoutines", "base64url_charset");
4625     address start = __ pc();
4626     __ emit_data64(0x0000004200000041, relocInfo::none);
4627     __ emit_data64(0x0000004400000043, relocInfo::none);
4628     __ emit_data64(0x0000004600000045, relocInfo::none);
4629     __ emit_data64(0x0000004800000047, relocInfo::none);
4630     __ emit_data64(0x0000004a00000049, relocInfo::none);
4631     __ emit_data64(0x0000004c0000004b, relocInfo::none);
4632     __ emit_data64(0x0000004e0000004d, relocInfo::none);
4633     __ emit_data64(0x000000500000004f, relocInfo::none);
4634     __ emit_data64(0x0000005200000051, relocInfo::none);
4635     __ emit_data64(0x0000005400000053, relocInfo::none);
4636     __ emit_data64(0x0000005600000055, relocInfo::none);
4637     __ emit_data64(0x0000005800000057, relocInfo::none);
4638     __ emit_data64(0x0000005a00000059, relocInfo::none);
4639     __ emit_data64(0x0000006200000061, relocInfo::none);
4640     __ emit_data64(0x0000006400000063, relocInfo::none);
4641     __ emit_data64(0x0000006600000065, relocInfo::none);
4642     __ emit_data64(0x0000006800000067, relocInfo::none);
4643     __ emit_data64(0x0000006a00000069, relocInfo::none);
4644     __ emit_data64(0x0000006c0000006b, relocInfo::none);
4645     __ emit_data64(0x0000006e0000006d, relocInfo::none);
4646     __ emit_data64(0x000000700000006f, relocInfo::none);
4647     __ emit_data64(0x0000007200000071, relocInfo::none);
4648     __ emit_data64(0x0000007400000073, relocInfo::none);
4649     __ emit_data64(0x0000007600000075, relocInfo::none);
4650     __ emit_data64(0x0000007800000077, relocInfo::none);
4651     __ emit_data64(0x0000007a00000079, relocInfo::none);
4652     __ emit_data64(0x0000003100000030, relocInfo::none);
4653     __ emit_data64(0x0000003300000032, relocInfo::none);
4654     __ emit_data64(0x0000003500000034, relocInfo::none);
4655     __ emit_data64(0x0000003700000036, relocInfo::none);
4656     __ emit_data64(0x0000003900000038, relocInfo::none);
4657     __ emit_data64(0x0000005f0000002d, relocInfo::none);
4658 
4659     return start;
4660   }
4661 
4662   address base64_bswap_mask_addr() {
4663     __ align(CodeEntryAlignment);
4664     StubCodeMark mark(this, "StubRoutines", "bswap_mask_base64");
4665     address start = __ pc();
4666     __ emit_data64(0x0504038002010080, relocInfo::none);
4667     __ emit_data64(0x0b0a098008070680, relocInfo::none);
4668     __ emit_data64(0x0908078006050480, relocInfo::none);
4669     __ emit_data64(0x0f0e0d800c0b0a80, relocInfo::none);
4670     __ emit_data64(0x0605048003020180, relocInfo::none);
4671     __ emit_data64(0x0c0b0a8009080780, relocInfo::none);
4672     __ emit_data64(0x0504038002010080, relocInfo::none);
4673     __ emit_data64(0x0b0a098008070680, relocInfo::none);
4674 
4675     return start;
4676   }
4677 
4678   address base64_right_shift_mask_addr() {
4679     __ align(CodeEntryAlignment);
4680     StubCodeMark mark(this, "StubRoutines", "right_shift_mask");
4681     address start = __ pc();
4682     __ emit_data64(0x0006000400020000, relocInfo::none);
4683     __ emit_data64(0x0006000400020000, relocInfo::none);
4684     __ emit_data64(0x0006000400020000, relocInfo::none);
4685     __ emit_data64(0x0006000400020000, relocInfo::none);
4686     __ emit_data64(0x0006000400020000, relocInfo::none);
4687     __ emit_data64(0x0006000400020000, relocInfo::none);
4688     __ emit_data64(0x0006000400020000, relocInfo::none);
4689     __ emit_data64(0x0006000400020000, relocInfo::none);
4690 
4691     return start;
4692   }
4693 
4694   address base64_left_shift_mask_addr() {
4695     __ align(CodeEntryAlignment);
4696     StubCodeMark mark(this, "StubRoutines", "left_shift_mask");
4697     address start = __ pc();
4698     __ emit_data64(0x0000000200040000, relocInfo::none);
4699     __ emit_data64(0x0000000200040000, relocInfo::none);
4700     __ emit_data64(0x0000000200040000, relocInfo::none);
4701     __ emit_data64(0x0000000200040000, relocInfo::none);
4702     __ emit_data64(0x0000000200040000, relocInfo::none);
4703     __ emit_data64(0x0000000200040000, relocInfo::none);
4704     __ emit_data64(0x0000000200040000, relocInfo::none);
4705     __ emit_data64(0x0000000200040000, relocInfo::none);
4706 
4707     return start;
4708   }
4709 
4710   address base64_and_mask_addr() {
4711     __ align(CodeEntryAlignment);
4712     StubCodeMark mark(this, "StubRoutines", "and_mask");
4713     address start = __ pc();
4714     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4715     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4716     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4717     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4718     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4719     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4720     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4721     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4722     return start;
4723   }
4724 
4725   address base64_gather_mask_addr() {
4726     __ align(CodeEntryAlignment);
4727     StubCodeMark mark(this, "StubRoutines", "gather_mask");
4728     address start = __ pc();
4729     __ emit_data64(0xffffffffffffffff, relocInfo::none);
4730     return start;
4731   }
4732 
4733 // Code for generating Base64 encoding.
4734 // Intrinsic function prototype in Base64.java:
4735 // private void encodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp, boolean isURL) {
4736   address generate_base64_encodeBlock() {
4737     __ align(CodeEntryAlignment);
4738     StubCodeMark mark(this, "StubRoutines", "implEncode");
4739     address start = __ pc();
4740     __ enter();
4741 
4742     // Save callee-saved registers before using them
4743     __ push(r12);
4744     __ push(r13);
4745     __ push(r14);
4746     __ push(r15);
4747 
4748     // arguments
4749     const Register source = c_rarg0; // Source Array
4750     const Register start_offset = c_rarg1; // start offset
4751     const Register end_offset = c_rarg2; // end offset
4752     const Register dest = c_rarg3; // destination array
4753 
4754 #ifndef _WIN64
4755     const Register dp = c_rarg4;  // Position for writing to dest array
4756     const Register isURL = c_rarg5;// Base64 or URL character set
4757 #else
4758     const Address  dp_mem(rbp, 6 * wordSize);  // length is on stack on Win64
4759     const Address isURL_mem(rbp, 7 * wordSize);
4760     const Register isURL = r10;      // pick the volatile windows register
4761     const Register dp = r12;
4762     __ movl(dp, dp_mem);
4763     __ movl(isURL, isURL_mem);
4764 #endif
4765 
4766     const Register length = r14;
4767     Label L_process80, L_process32, L_process3, L_exit, L_processdata;
4768 
4769     // calculate length from offsets
4770     __ movl(length, end_offset);
4771     __ subl(length, start_offset);
4772     __ cmpl(length, 0);
4773     __ jcc(Assembler::lessEqual, L_exit);
4774 
4775     __ lea(r11, ExternalAddress(StubRoutines::x86::base64_charset_addr()));
4776     // check if base64 charset(isURL=0) or base64 url charset(isURL=1) needs to be loaded
4777     __ cmpl(isURL, 0);
4778     __ jcc(Assembler::equal, L_processdata);
4779     __ lea(r11, ExternalAddress(StubRoutines::x86::base64url_charset_addr()));
4780 
4781     // load masks required for encoding data
4782     __ BIND(L_processdata);
4783     __ movdqu(xmm16, ExternalAddress(StubRoutines::x86::base64_gather_mask_addr()));
4784     // Set 64 bits of K register.
4785     __ evpcmpeqb(k3, xmm16, xmm16, Assembler::AVX_512bit);
4786     __ evmovdquq(xmm12, ExternalAddress(StubRoutines::x86::base64_bswap_mask_addr()), Assembler::AVX_256bit, r13);
4787     __ evmovdquq(xmm13, ExternalAddress(StubRoutines::x86::base64_right_shift_mask_addr()), Assembler::AVX_512bit, r13);
4788     __ evmovdquq(xmm14, ExternalAddress(StubRoutines::x86::base64_left_shift_mask_addr()), Assembler::AVX_512bit, r13);
4789     __ evmovdquq(xmm15, ExternalAddress(StubRoutines::x86::base64_and_mask_addr()), Assembler::AVX_512bit, r13);
4790 
4791     // Vector Base64 implementation, producing 96 bytes of encoded data
4792     __ BIND(L_process80);
4793     __ cmpl(length, 80);
4794     __ jcc(Assembler::below, L_process32);
4795     __ evmovdquq(xmm0, Address(source, start_offset, Address::times_1, 0), Assembler::AVX_256bit);
4796     __ evmovdquq(xmm1, Address(source, start_offset, Address::times_1, 24), Assembler::AVX_256bit);
4797     __ evmovdquq(xmm2, Address(source, start_offset, Address::times_1, 48), Assembler::AVX_256bit);
4798 
4799     //permute the input data in such a manner that we have continuity of the source
4800     __ vpermq(xmm3, xmm0, 148, Assembler::AVX_256bit);
4801     __ vpermq(xmm4, xmm1, 148, Assembler::AVX_256bit);
4802     __ vpermq(xmm5, xmm2, 148, Assembler::AVX_256bit);
4803 
4804     //shuffle input and group 3 bytes of data and to it add 0 as the 4th byte.
4805     //we can deal with 12 bytes at a time in a 128 bit register
4806     __ vpshufb(xmm3, xmm3, xmm12, Assembler::AVX_256bit);
4807     __ vpshufb(xmm4, xmm4, xmm12, Assembler::AVX_256bit);
4808     __ vpshufb(xmm5, xmm5, xmm12, Assembler::AVX_256bit);
4809 
4810     //convert byte to word. Each 128 bit register will have 6 bytes for processing
4811     __ vpmovzxbw(xmm3, xmm3, Assembler::AVX_512bit);
4812     __ vpmovzxbw(xmm4, xmm4, Assembler::AVX_512bit);
4813     __ vpmovzxbw(xmm5, xmm5, Assembler::AVX_512bit);
4814 
4815     // Extract bits in the following pattern 6, 4+2, 2+4, 6 to convert 3, 8 bit numbers to 4, 6 bit numbers
4816     __ evpsrlvw(xmm0, xmm3, xmm13,  Assembler::AVX_512bit);
4817     __ evpsrlvw(xmm1, xmm4, xmm13, Assembler::AVX_512bit);
4818     __ evpsrlvw(xmm2, xmm5, xmm13, Assembler::AVX_512bit);
4819 
4820     __ evpsllvw(xmm3, xmm3, xmm14, Assembler::AVX_512bit);
4821     __ evpsllvw(xmm4, xmm4, xmm14, Assembler::AVX_512bit);
4822     __ evpsllvw(xmm5, xmm5, xmm14, Assembler::AVX_512bit);
4823 
4824     __ vpsrlq(xmm0, xmm0, 8, Assembler::AVX_512bit);
4825     __ vpsrlq(xmm1, xmm1, 8, Assembler::AVX_512bit);
4826     __ vpsrlq(xmm2, xmm2, 8, Assembler::AVX_512bit);
4827 
4828     __ vpsllq(xmm3, xmm3, 8, Assembler::AVX_512bit);
4829     __ vpsllq(xmm4, xmm4, 8, Assembler::AVX_512bit);
4830     __ vpsllq(xmm5, xmm5, 8, Assembler::AVX_512bit);
4831 
4832     __ vpandq(xmm3, xmm3, xmm15, Assembler::AVX_512bit);
4833     __ vpandq(xmm4, xmm4, xmm15, Assembler::AVX_512bit);
4834     __ vpandq(xmm5, xmm5, xmm15, Assembler::AVX_512bit);
4835 
4836     // Get the final 4*6 bits base64 encoding
4837     __ vporq(xmm3, xmm3, xmm0, Assembler::AVX_512bit);
4838     __ vporq(xmm4, xmm4, xmm1, Assembler::AVX_512bit);
4839     __ vporq(xmm5, xmm5, xmm2, Assembler::AVX_512bit);
4840 
4841     // Shift
4842     __ vpsrlq(xmm3, xmm3, 8, Assembler::AVX_512bit);
4843     __ vpsrlq(xmm4, xmm4, 8, Assembler::AVX_512bit);
4844     __ vpsrlq(xmm5, xmm5, 8, Assembler::AVX_512bit);
4845 
4846     // look up 6 bits in the base64 character set to fetch the encoding
4847     // we are converting word to dword as gather instructions need dword indices for looking up encoding
4848     __ vextracti64x4(xmm6, xmm3, 0);
4849     __ vpmovzxwd(xmm0, xmm6, Assembler::AVX_512bit);
4850     __ vextracti64x4(xmm6, xmm3, 1);
4851     __ vpmovzxwd(xmm1, xmm6, Assembler::AVX_512bit);
4852 
4853     __ vextracti64x4(xmm6, xmm4, 0);
4854     __ vpmovzxwd(xmm2, xmm6, Assembler::AVX_512bit);
4855     __ vextracti64x4(xmm6, xmm4, 1);
4856     __ vpmovzxwd(xmm3, xmm6, Assembler::AVX_512bit);
4857 
4858     __ vextracti64x4(xmm4, xmm5, 0);
4859     __ vpmovzxwd(xmm6, xmm4, Assembler::AVX_512bit);
4860 
4861     __ vextracti64x4(xmm4, xmm5, 1);
4862     __ vpmovzxwd(xmm7, xmm4, Assembler::AVX_512bit);
4863 
4864     __ kmovql(k2, k3);
4865     __ evpgatherdd(xmm4, k2, Address(r11, xmm0, Address::times_4, 0), Assembler::AVX_512bit);
4866     __ kmovql(k2, k3);
4867     __ evpgatherdd(xmm5, k2, Address(r11, xmm1, Address::times_4, 0), Assembler::AVX_512bit);
4868     __ kmovql(k2, k3);
4869     __ evpgatherdd(xmm8, k2, Address(r11, xmm2, Address::times_4, 0), Assembler::AVX_512bit);
4870     __ kmovql(k2, k3);
4871     __ evpgatherdd(xmm9, k2, Address(r11, xmm3, Address::times_4, 0), Assembler::AVX_512bit);
4872     __ kmovql(k2, k3);
4873     __ evpgatherdd(xmm10, k2, Address(r11, xmm6, Address::times_4, 0), Assembler::AVX_512bit);
4874     __ kmovql(k2, k3);
4875     __ evpgatherdd(xmm11, k2, Address(r11, xmm7, Address::times_4, 0), Assembler::AVX_512bit);
4876 
4877     //Down convert dword to byte. Final output is 16*6 = 96 bytes long
4878     __ evpmovdb(Address(dest, dp, Address::times_1, 0), xmm4, Assembler::AVX_512bit);
4879     __ evpmovdb(Address(dest, dp, Address::times_1, 16), xmm5, Assembler::AVX_512bit);
4880     __ evpmovdb(Address(dest, dp, Address::times_1, 32), xmm8, Assembler::AVX_512bit);
4881     __ evpmovdb(Address(dest, dp, Address::times_1, 48), xmm9, Assembler::AVX_512bit);
4882     __ evpmovdb(Address(dest, dp, Address::times_1, 64), xmm10, Assembler::AVX_512bit);
4883     __ evpmovdb(Address(dest, dp, Address::times_1, 80), xmm11, Assembler::AVX_512bit);
4884 
4885     __ addq(dest, 96);
4886     __ addq(source, 72);
4887     __ subq(length, 72);
4888     __ jmp(L_process80);
4889 
4890     // Vector Base64 implementation generating 32 bytes of encoded data
4891     __ BIND(L_process32);
4892     __ cmpl(length, 32);
4893     __ jcc(Assembler::below, L_process3);
4894     __ evmovdquq(xmm0, Address(source, start_offset), Assembler::AVX_256bit);
4895     __ vpermq(xmm0, xmm0, 148, Assembler::AVX_256bit);
4896     __ vpshufb(xmm6, xmm0, xmm12, Assembler::AVX_256bit);
4897     __ vpmovzxbw(xmm6, xmm6, Assembler::AVX_512bit);
4898     __ evpsrlvw(xmm2, xmm6, xmm13, Assembler::AVX_512bit);
4899     __ evpsllvw(xmm3, xmm6, xmm14, Assembler::AVX_512bit);
4900 
4901     __ vpsrlq(xmm2, xmm2, 8, Assembler::AVX_512bit);
4902     __ vpsllq(xmm3, xmm3, 8, Assembler::AVX_512bit);
4903     __ vpandq(xmm3, xmm3, xmm15, Assembler::AVX_512bit);
4904     __ vporq(xmm1, xmm2, xmm3, Assembler::AVX_512bit);
4905     __ vpsrlq(xmm1, xmm1, 8, Assembler::AVX_512bit);
4906     __ vextracti64x4(xmm9, xmm1, 0);
4907     __ vpmovzxwd(xmm6, xmm9, Assembler::AVX_512bit);
4908     __ vextracti64x4(xmm9, xmm1, 1);
4909     __ vpmovzxwd(xmm5, xmm9,  Assembler::AVX_512bit);
4910     __ kmovql(k2, k3);
4911     __ evpgatherdd(xmm8, k2, Address(r11, xmm6, Address::times_4, 0), Assembler::AVX_512bit);
4912     __ kmovql(k2, k3);
4913     __ evpgatherdd(xmm10, k2, Address(r11, xmm5, Address::times_4, 0), Assembler::AVX_512bit);
4914     __ evpmovdb(Address(dest, dp, Address::times_1, 0), xmm8, Assembler::AVX_512bit);
4915     __ evpmovdb(Address(dest, dp, Address::times_1, 16), xmm10, Assembler::AVX_512bit);
4916     __ subq(length, 24);
4917     __ addq(dest, 32);
4918     __ addq(source, 24);
4919     __ jmp(L_process32);
4920 
4921     // Scalar data processing takes 3 bytes at a time and produces 4 bytes of encoded data
4922     /* This code corresponds to the scalar version of the following snippet in Base64.java
4923     ** int bits = (src[sp0++] & 0xff) << 16 |(src[sp0++] & 0xff) << 8 |(src[sp0++] & 0xff);
4924     ** dst[dp0++] = (byte)base64[(bits >> > 18) & 0x3f];
4925     ** dst[dp0++] = (byte)base64[(bits >> > 12) & 0x3f];
4926     ** dst[dp0++] = (byte)base64[(bits >> > 6) & 0x3f];
4927     ** dst[dp0++] = (byte)base64[bits & 0x3f];*/
4928     __ BIND(L_process3);
4929     __ cmpl(length, 3);
4930     __ jcc(Assembler::below, L_exit);
4931     // Read 1 byte at a time
4932     __ movzbl(rax, Address(source, start_offset));
4933     __ shll(rax, 0x10);
4934     __ movl(r15, rax);
4935     __ movzbl(rax, Address(source, start_offset, Address::times_1, 1));
4936     __ shll(rax, 0x8);
4937     __ movzwl(rax, rax);
4938     __ orl(r15, rax);
4939     __ movzbl(rax, Address(source, start_offset, Address::times_1, 2));
4940     __ orl(rax, r15);
4941     // Save 3 bytes read in r15
4942     __ movl(r15, rax);
4943     __ shrl(rax, 0x12);
4944     __ andl(rax, 0x3f);
4945     // rax contains the index, r11 contains base64 lookup table
4946     __ movb(rax, Address(r11, rax, Address::times_4));
4947     // Write the encoded byte to destination
4948     __ movb(Address(dest, dp, Address::times_1, 0), rax);
4949     __ movl(rax, r15);
4950     __ shrl(rax, 0xc);
4951     __ andl(rax, 0x3f);
4952     __ movb(rax, Address(r11, rax, Address::times_4));
4953     __ movb(Address(dest, dp, Address::times_1, 1), rax);
4954     __ movl(rax, r15);
4955     __ shrl(rax, 0x6);
4956     __ andl(rax, 0x3f);
4957     __ movb(rax, Address(r11, rax, Address::times_4));
4958     __ movb(Address(dest, dp, Address::times_1, 2), rax);
4959     __ movl(rax, r15);
4960     __ andl(rax, 0x3f);
4961     __ movb(rax, Address(r11, rax, Address::times_4));
4962     __ movb(Address(dest, dp, Address::times_1, 3), rax);
4963     __ subl(length, 3);
4964     __ addq(dest, 4);
4965     __ addq(source, 3);
4966     __ jmp(L_process3);
4967     __ BIND(L_exit);
4968     __ pop(r15);
4969     __ pop(r14);
4970     __ pop(r13);
4971     __ pop(r12);
4972     __ leave();
4973     __ ret(0);
4974     return start;
4975   }
4976 
4977   /**
4978    *  Arguments:
4979    *
4980    * Inputs:
4981    *   c_rarg0   - int crc
4982    *   c_rarg1   - byte* buf
4983    *   c_rarg2   - int length
4984    *
4985    * Ouput:
4986    *       rax   - int crc result
4987    */
4988   address generate_updateBytesCRC32() {
4989     assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions");
4990 
4991     __ align(CodeEntryAlignment);
4992     StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32");
4993 
4994     address start = __ pc();
4995     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
4996     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
4997     // rscratch1: r10
4998     const Register crc   = c_rarg0;  // crc
4999     const Register buf   = c_rarg1;  // source java byte array address
5000     const Register len   = c_rarg2;  // length
5001     const Register table = c_rarg3;  // crc_table address (reuse register)
5002     const Register tmp   = r11;
5003     assert_different_registers(crc, buf, len, table, tmp, rax);
5004 
5005     BLOCK_COMMENT("Entry:");
5006     __ enter(); // required for proper stackwalking of RuntimeStub frame
5007 
5008     __ kernel_crc32(crc, buf, len, table, tmp);
5009 
5010     __ movl(rax, crc);
5011     __ vzeroupper();
5012     __ leave(); // required for proper stackwalking of RuntimeStub frame
5013     __ ret(0);
5014 
5015     return start;
5016   }
5017 
5018   /**
5019   *  Arguments:
5020   *
5021   * Inputs:
5022   *   c_rarg0   - int crc
5023   *   c_rarg1   - byte* buf
5024   *   c_rarg2   - long length
5025   *   c_rarg3   - table_start - optional (present only when doing a library_call,
5026   *              not used by x86 algorithm)
5027   *
5028   * Ouput:
5029   *       rax   - int crc result
5030   */
5031   address generate_updateBytesCRC32C(bool is_pclmulqdq_supported) {
5032       assert(UseCRC32CIntrinsics, "need SSE4_2");
5033       __ align(CodeEntryAlignment);
5034       StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32C");
5035       address start = __ pc();
5036       //reg.arg        int#0        int#1        int#2        int#3        int#4        int#5        float regs
5037       //Windows        RCX          RDX          R8           R9           none         none         XMM0..XMM3
5038       //Lin / Sol      RDI          RSI          RDX          RCX          R8           R9           XMM0..XMM7
5039       const Register crc = c_rarg0;  // crc
5040       const Register buf = c_rarg1;  // source java byte array address
5041       const Register len = c_rarg2;  // length
5042       const Register a = rax;
5043       const Register j = r9;
5044       const Register k = r10;
5045       const Register l = r11;
5046 #ifdef _WIN64
5047       const Register y = rdi;
5048       const Register z = rsi;
5049 #else
5050       const Register y = rcx;
5051       const Register z = r8;
5052 #endif
5053       assert_different_registers(crc, buf, len, a, j, k, l, y, z);
5054 
5055       BLOCK_COMMENT("Entry:");
5056       __ enter(); // required for proper stackwalking of RuntimeStub frame
5057 #ifdef _WIN64
5058       __ push(y);
5059       __ push(z);
5060 #endif
5061       __ crc32c_ipl_alg2_alt2(crc, buf, len,
5062                               a, j, k,
5063                               l, y, z,
5064                               c_farg0, c_farg1, c_farg2,
5065                               is_pclmulqdq_supported);
5066       __ movl(rax, crc);
5067 #ifdef _WIN64
5068       __ pop(z);
5069       __ pop(y);
5070 #endif
5071       __ vzeroupper();
5072       __ leave(); // required for proper stackwalking of RuntimeStub frame
5073       __ ret(0);
5074 
5075       return start;
5076   }
5077 
5078   /**
5079    *  Arguments:
5080    *
5081    *  Input:
5082    *    c_rarg0   - x address
5083    *    c_rarg1   - x length
5084    *    c_rarg2   - y address
5085    *    c_rarg3   - y length
5086    * not Win64
5087    *    c_rarg4   - z address
5088    *    c_rarg5   - z length
5089    * Win64
5090    *    rsp+40    - z address
5091    *    rsp+48    - z length
5092    */
5093   address generate_multiplyToLen() {
5094     __ align(CodeEntryAlignment);
5095     StubCodeMark mark(this, "StubRoutines", "multiplyToLen");
5096 
5097     address start = __ pc();
5098     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
5099     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
5100     const Register x     = rdi;
5101     const Register xlen  = rax;
5102     const Register y     = rsi;
5103     const Register ylen  = rcx;
5104     const Register z     = r8;
5105     const Register zlen  = r11;
5106 
5107     // Next registers will be saved on stack in multiply_to_len().
5108     const Register tmp1  = r12;
5109     const Register tmp2  = r13;
5110     const Register tmp3  = r14;
5111     const Register tmp4  = r15;
5112     const Register tmp5  = rbx;
5113 
5114     BLOCK_COMMENT("Entry:");
5115     __ enter(); // required for proper stackwalking of RuntimeStub frame
5116 
5117 #ifndef _WIN64
5118     __ movptr(zlen, r9); // Save r9 in r11 - zlen
5119 #endif
5120     setup_arg_regs(4); // x => rdi, xlen => rsi, y => rdx
5121                        // ylen => rcx, z => r8, zlen => r11
5122                        // r9 and r10 may be used to save non-volatile registers
5123 #ifdef _WIN64
5124     // last 2 arguments (#4, #5) are on stack on Win64
5125     __ movptr(z, Address(rsp, 6 * wordSize));
5126     __ movptr(zlen, Address(rsp, 7 * wordSize));
5127 #endif
5128 
5129     __ movptr(xlen, rsi);
5130     __ movptr(y,    rdx);
5131     __ multiply_to_len(x, xlen, y, ylen, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5);
5132 
5133     restore_arg_regs();
5134 
5135     __ leave(); // required for proper stackwalking of RuntimeStub frame
5136     __ ret(0);
5137 
5138     return start;
5139   }
5140 
5141   /**
5142   *  Arguments:
5143   *
5144   *  Input:
5145   *    c_rarg0   - obja     address
5146   *    c_rarg1   - objb     address
5147   *    c_rarg3   - length   length
5148   *    c_rarg4   - scale    log2_array_indxscale
5149   *
5150   *  Output:
5151   *        rax   - int >= mismatched index, < 0 bitwise complement of tail
5152   */
5153   address generate_vectorizedMismatch() {
5154     __ align(CodeEntryAlignment);
5155     StubCodeMark mark(this, "StubRoutines", "vectorizedMismatch");
5156     address start = __ pc();
5157 
5158     BLOCK_COMMENT("Entry:");
5159     __ enter();
5160 
5161 #ifdef _WIN64  // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
5162     const Register scale = c_rarg0;  //rcx, will exchange with r9
5163     const Register objb = c_rarg1;   //rdx
5164     const Register length = c_rarg2; //r8
5165     const Register obja = c_rarg3;   //r9
5166     __ xchgq(obja, scale);  //now obja and scale contains the correct contents
5167 
5168     const Register tmp1 = r10;
5169     const Register tmp2 = r11;
5170 #endif
5171 #ifndef _WIN64 // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
5172     const Register obja = c_rarg0;   //U:rdi
5173     const Register objb = c_rarg1;   //U:rsi
5174     const Register length = c_rarg2; //U:rdx
5175     const Register scale = c_rarg3;  //U:rcx
5176     const Register tmp1 = r8;
5177     const Register tmp2 = r9;
5178 #endif
5179     const Register result = rax; //return value
5180     const XMMRegister vec0 = xmm0;
5181     const XMMRegister vec1 = xmm1;
5182     const XMMRegister vec2 = xmm2;
5183 
5184     __ vectorized_mismatch(obja, objb, length, scale, result, tmp1, tmp2, vec0, vec1, vec2);
5185 
5186     __ vzeroupper();
5187     __ leave();
5188     __ ret(0);
5189 
5190     return start;
5191   }
5192 
5193 /**
5194    *  Arguments:
5195    *
5196   //  Input:
5197   //    c_rarg0   - x address
5198   //    c_rarg1   - x length
5199   //    c_rarg2   - z address
5200   //    c_rarg3   - z lenth
5201    *
5202    */
5203   address generate_squareToLen() {
5204 
5205     __ align(CodeEntryAlignment);
5206     StubCodeMark mark(this, "StubRoutines", "squareToLen");
5207 
5208     address start = __ pc();
5209     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
5210     // Unix:  rdi, rsi, rdx, rcx (c_rarg0, c_rarg1, ...)
5211     const Register x      = rdi;
5212     const Register len    = rsi;
5213     const Register z      = r8;
5214     const Register zlen   = rcx;
5215 
5216    const Register tmp1      = r12;
5217    const Register tmp2      = r13;
5218    const Register tmp3      = r14;
5219    const Register tmp4      = r15;
5220    const Register tmp5      = rbx;
5221 
5222     BLOCK_COMMENT("Entry:");
5223     __ enter(); // required for proper stackwalking of RuntimeStub frame
5224 
5225     setup_arg_regs(4); // x => rdi, len => rsi, z => rdx
5226                        // zlen => rcx
5227                        // r9 and r10 may be used to save non-volatile registers
5228     __ movptr(r8, rdx);
5229     __ square_to_len(x, len, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
5230 
5231     restore_arg_regs();
5232 
5233     __ leave(); // required for proper stackwalking of RuntimeStub frame
5234     __ ret(0);
5235 
5236     return start;
5237   }
5238 
5239   address generate_method_entry_barrier() {
5240     __ align(CodeEntryAlignment);
5241     StubCodeMark mark(this, "StubRoutines", "nmethod_entry_barrier");
5242 
5243     Label deoptimize_label;
5244 
5245     address start = __ pc();
5246 
5247     __ push(-1); // cookie, this is used for writing the new rsp when deoptimizing
5248 
5249     BLOCK_COMMENT("Entry:");
5250     __ enter(); // save rbp
5251 
5252     // save c_rarg0, because we want to use that value.
5253     // We could do without it but then we depend on the number of slots used by pusha
5254     __ push(c_rarg0);
5255 
5256     __ lea(c_rarg0, Address(rsp, wordSize * 3)); // 1 for cookie, 1 for rbp, 1 for c_rarg0 - this should be the return address
5257 
5258     __ pusha();
5259 
5260     // The method may have floats as arguments, and we must spill them before calling
5261     // the VM runtime.
5262     assert(Argument::n_float_register_parameters_j == 8, "Assumption");
5263     const int xmm_size = wordSize * 2;
5264     const int xmm_spill_size = xmm_size * Argument::n_float_register_parameters_j;
5265     __ subptr(rsp, xmm_spill_size);
5266     __ movdqu(Address(rsp, xmm_size * 7), xmm7);
5267     __ movdqu(Address(rsp, xmm_size * 6), xmm6);
5268     __ movdqu(Address(rsp, xmm_size * 5), xmm5);
5269     __ movdqu(Address(rsp, xmm_size * 4), xmm4);
5270     __ movdqu(Address(rsp, xmm_size * 3), xmm3);
5271     __ movdqu(Address(rsp, xmm_size * 2), xmm2);
5272     __ movdqu(Address(rsp, xmm_size * 1), xmm1);
5273     __ movdqu(Address(rsp, xmm_size * 0), xmm0);
5274 
5275     __ call_VM_leaf(CAST_FROM_FN_PTR(address, static_cast<int (*)(address*)>(BarrierSetNMethod::nmethod_stub_entry_barrier)), 1);
5276 
5277     __ movdqu(xmm0, Address(rsp, xmm_size * 0));
5278     __ movdqu(xmm1, Address(rsp, xmm_size * 1));
5279     __ movdqu(xmm2, Address(rsp, xmm_size * 2));
5280     __ movdqu(xmm3, Address(rsp, xmm_size * 3));
5281     __ movdqu(xmm4, Address(rsp, xmm_size * 4));
5282     __ movdqu(xmm5, Address(rsp, xmm_size * 5));
5283     __ movdqu(xmm6, Address(rsp, xmm_size * 6));
5284     __ movdqu(xmm7, Address(rsp, xmm_size * 7));
5285     __ addptr(rsp, xmm_spill_size);
5286 
5287     __ cmpl(rax, 1); // 1 means deoptimize
5288     __ jcc(Assembler::equal, deoptimize_label);
5289 
5290     __ popa();
5291     __ pop(c_rarg0);
5292 
5293     __ leave();
5294 
5295     __ addptr(rsp, 1 * wordSize); // cookie
5296     __ ret(0);
5297 
5298 
5299     __ BIND(deoptimize_label);
5300 
5301     __ popa();
5302     __ pop(c_rarg0);
5303 
5304     __ leave();
5305 
5306     // this can be taken out, but is good for verification purposes. getting a SIGSEGV
5307     // here while still having a correct stack is valuable
5308     __ testptr(rsp, Address(rsp, 0));
5309 
5310     __ movptr(rsp, Address(rsp, 0)); // new rsp was written in the barrier
5311     __ jmp(Address(rsp, -1 * wordSize)); // jmp target should be callers verified_entry_point
5312 
5313     return start;
5314   }
5315 
5316    /**
5317    *  Arguments:
5318    *
5319    *  Input:
5320    *    c_rarg0   - out address
5321    *    c_rarg1   - in address
5322    *    c_rarg2   - offset
5323    *    c_rarg3   - len
5324    * not Win64
5325    *    c_rarg4   - k
5326    * Win64
5327    *    rsp+40    - k
5328    */
5329   address generate_mulAdd() {
5330     __ align(CodeEntryAlignment);
5331     StubCodeMark mark(this, "StubRoutines", "mulAdd");
5332 
5333     address start = __ pc();
5334     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
5335     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
5336     const Register out     = rdi;
5337     const Register in      = rsi;
5338     const Register offset  = r11;
5339     const Register len     = rcx;
5340     const Register k       = r8;
5341 
5342     // Next registers will be saved on stack in mul_add().
5343     const Register tmp1  = r12;
5344     const Register tmp2  = r13;
5345     const Register tmp3  = r14;
5346     const Register tmp4  = r15;
5347     const Register tmp5  = rbx;
5348 
5349     BLOCK_COMMENT("Entry:");
5350     __ enter(); // required for proper stackwalking of RuntimeStub frame
5351 
5352     setup_arg_regs(4); // out => rdi, in => rsi, offset => rdx
5353                        // len => rcx, k => r8
5354                        // r9 and r10 may be used to save non-volatile registers
5355 #ifdef _WIN64
5356     // last argument is on stack on Win64
5357     __ movl(k, Address(rsp, 6 * wordSize));
5358 #endif
5359     __ movptr(r11, rdx);  // move offset in rdx to offset(r11)
5360     __ mul_add(out, in, offset, len, k, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
5361 
5362     restore_arg_regs();
5363 
5364     __ leave(); // required for proper stackwalking of RuntimeStub frame
5365     __ ret(0);
5366 
5367     return start;
5368   }
5369 
5370   address generate_libmExp() {
5371     StubCodeMark mark(this, "StubRoutines", "libmExp");
5372 
5373     address start = __ pc();
5374 
5375     const XMMRegister x0  = xmm0;
5376     const XMMRegister x1  = xmm1;
5377     const XMMRegister x2  = xmm2;
5378     const XMMRegister x3  = xmm3;
5379 
5380     const XMMRegister x4  = xmm4;
5381     const XMMRegister x5  = xmm5;
5382     const XMMRegister x6  = xmm6;
5383     const XMMRegister x7  = xmm7;
5384 
5385     const Register tmp   = r11;
5386 
5387     BLOCK_COMMENT("Entry:");
5388     __ enter(); // required for proper stackwalking of RuntimeStub frame
5389 
5390     __ fast_exp(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
5391 
5392     __ leave(); // required for proper stackwalking of RuntimeStub frame
5393     __ ret(0);
5394 
5395     return start;
5396 
5397   }
5398 
5399   address generate_libmLog() {
5400     StubCodeMark mark(this, "StubRoutines", "libmLog");
5401 
5402     address start = __ pc();
5403 
5404     const XMMRegister x0 = xmm0;
5405     const XMMRegister x1 = xmm1;
5406     const XMMRegister x2 = xmm2;
5407     const XMMRegister x3 = xmm3;
5408 
5409     const XMMRegister x4 = xmm4;
5410     const XMMRegister x5 = xmm5;
5411     const XMMRegister x6 = xmm6;
5412     const XMMRegister x7 = xmm7;
5413 
5414     const Register tmp1 = r11;
5415     const Register tmp2 = r8;
5416 
5417     BLOCK_COMMENT("Entry:");
5418     __ enter(); // required for proper stackwalking of RuntimeStub frame
5419 
5420     __ fast_log(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp1, tmp2);
5421 
5422     __ leave(); // required for proper stackwalking of RuntimeStub frame
5423     __ ret(0);
5424 
5425     return start;
5426 
5427   }
5428 
5429   address generate_libmLog10() {
5430     StubCodeMark mark(this, "StubRoutines", "libmLog10");
5431 
5432     address start = __ pc();
5433 
5434     const XMMRegister x0 = xmm0;
5435     const XMMRegister x1 = xmm1;
5436     const XMMRegister x2 = xmm2;
5437     const XMMRegister x3 = xmm3;
5438 
5439     const XMMRegister x4 = xmm4;
5440     const XMMRegister x5 = xmm5;
5441     const XMMRegister x6 = xmm6;
5442     const XMMRegister x7 = xmm7;
5443 
5444     const Register tmp = r11;
5445 
5446     BLOCK_COMMENT("Entry:");
5447     __ enter(); // required for proper stackwalking of RuntimeStub frame
5448 
5449     __ fast_log10(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
5450 
5451     __ leave(); // required for proper stackwalking of RuntimeStub frame
5452     __ ret(0);
5453 
5454     return start;
5455 
5456   }
5457 
5458   address generate_libmPow() {
5459     StubCodeMark mark(this, "StubRoutines", "libmPow");
5460 
5461     address start = __ pc();
5462 
5463     const XMMRegister x0 = xmm0;
5464     const XMMRegister x1 = xmm1;
5465     const XMMRegister x2 = xmm2;
5466     const XMMRegister x3 = xmm3;
5467 
5468     const XMMRegister x4 = xmm4;
5469     const XMMRegister x5 = xmm5;
5470     const XMMRegister x6 = xmm6;
5471     const XMMRegister x7 = xmm7;
5472 
5473     const Register tmp1 = r8;
5474     const Register tmp2 = r9;
5475     const Register tmp3 = r10;
5476     const Register tmp4 = r11;
5477 
5478     BLOCK_COMMENT("Entry:");
5479     __ enter(); // required for proper stackwalking of RuntimeStub frame
5480 
5481     __ fast_pow(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp1, tmp2, tmp3, tmp4);
5482 
5483     __ leave(); // required for proper stackwalking of RuntimeStub frame
5484     __ ret(0);
5485 
5486     return start;
5487 
5488   }
5489 
5490   address generate_libmSin() {
5491     StubCodeMark mark(this, "StubRoutines", "libmSin");
5492 
5493     address start = __ pc();
5494 
5495     const XMMRegister x0 = xmm0;
5496     const XMMRegister x1 = xmm1;
5497     const XMMRegister x2 = xmm2;
5498     const XMMRegister x3 = xmm3;
5499 
5500     const XMMRegister x4 = xmm4;
5501     const XMMRegister x5 = xmm5;
5502     const XMMRegister x6 = xmm6;
5503     const XMMRegister x7 = xmm7;
5504 
5505     const Register tmp1 = r8;
5506     const Register tmp2 = r9;
5507     const Register tmp3 = r10;
5508     const Register tmp4 = r11;
5509 
5510     BLOCK_COMMENT("Entry:");
5511     __ enter(); // required for proper stackwalking of RuntimeStub frame
5512 
5513 #ifdef _WIN64
5514     __ push(rsi);
5515     __ push(rdi);
5516 #endif
5517     __ fast_sin(x0, x1, x2, x3, x4, x5, x6, x7, rax, rbx, rcx, rdx, tmp1, tmp2, tmp3, tmp4);
5518 
5519 #ifdef _WIN64
5520     __ pop(rdi);
5521     __ pop(rsi);
5522 #endif
5523 
5524     __ leave(); // required for proper stackwalking of RuntimeStub frame
5525     __ ret(0);
5526 
5527     return start;
5528 
5529   }
5530 
5531   address generate_libmCos() {
5532     StubCodeMark mark(this, "StubRoutines", "libmCos");
5533 
5534     address start = __ pc();
5535 
5536     const XMMRegister x0 = xmm0;
5537     const XMMRegister x1 = xmm1;
5538     const XMMRegister x2 = xmm2;
5539     const XMMRegister x3 = xmm3;
5540 
5541     const XMMRegister x4 = xmm4;
5542     const XMMRegister x5 = xmm5;
5543     const XMMRegister x6 = xmm6;
5544     const XMMRegister x7 = xmm7;
5545 
5546     const Register tmp1 = r8;
5547     const Register tmp2 = r9;
5548     const Register tmp3 = r10;
5549     const Register tmp4 = r11;
5550 
5551     BLOCK_COMMENT("Entry:");
5552     __ enter(); // required for proper stackwalking of RuntimeStub frame
5553 
5554 #ifdef _WIN64
5555     __ push(rsi);
5556     __ push(rdi);
5557 #endif
5558     __ fast_cos(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp1, tmp2, tmp3, tmp4);
5559 
5560 #ifdef _WIN64
5561     __ pop(rdi);
5562     __ pop(rsi);
5563 #endif
5564 
5565     __ leave(); // required for proper stackwalking of RuntimeStub frame
5566     __ ret(0);
5567 
5568     return start;
5569 
5570   }
5571 
5572   address generate_libmTan() {
5573     StubCodeMark mark(this, "StubRoutines", "libmTan");
5574 
5575     address start = __ pc();
5576 
5577     const XMMRegister x0 = xmm0;
5578     const XMMRegister x1 = xmm1;
5579     const XMMRegister x2 = xmm2;
5580     const XMMRegister x3 = xmm3;
5581 
5582     const XMMRegister x4 = xmm4;
5583     const XMMRegister x5 = xmm5;
5584     const XMMRegister x6 = xmm6;
5585     const XMMRegister x7 = xmm7;
5586 
5587     const Register tmp1 = r8;
5588     const Register tmp2 = r9;
5589     const Register tmp3 = r10;
5590     const Register tmp4 = r11;
5591 
5592     BLOCK_COMMENT("Entry:");
5593     __ enter(); // required for proper stackwalking of RuntimeStub frame
5594 
5595 #ifdef _WIN64
5596     __ push(rsi);
5597     __ push(rdi);
5598 #endif
5599     __ fast_tan(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp1, tmp2, tmp3, tmp4);
5600 
5601 #ifdef _WIN64
5602     __ pop(rdi);
5603     __ pop(rsi);
5604 #endif
5605 
5606     __ leave(); // required for proper stackwalking of RuntimeStub frame
5607     __ ret(0);
5608 
5609     return start;
5610 
5611   }
5612 
5613 #undef __
5614 #define __ masm->
5615 
5616   // Continuation point for throwing of implicit exceptions that are
5617   // not handled in the current activation. Fabricates an exception
5618   // oop and initiates normal exception dispatching in this
5619   // frame. Since we need to preserve callee-saved values (currently
5620   // only for C2, but done for C1 as well) we need a callee-saved oop
5621   // map and therefore have to make these stubs into RuntimeStubs
5622   // rather than BufferBlobs.  If the compiler needs all registers to
5623   // be preserved between the fault point and the exception handler
5624   // then it must assume responsibility for that in
5625   // AbstractCompiler::continuation_for_implicit_null_exception or
5626   // continuation_for_implicit_division_by_zero_exception. All other
5627   // implicit exceptions (e.g., NullPointerException or
5628   // AbstractMethodError on entry) are either at call sites or
5629   // otherwise assume that stack unwinding will be initiated, so
5630   // caller saved registers were assumed volatile in the compiler.
5631   address generate_throw_exception(const char* name,
5632                                    address runtime_entry,
5633                                    Register arg1 = noreg,
5634                                    Register arg2 = noreg) {
5635     // Information about frame layout at time of blocking runtime call.
5636     // Note that we only have to preserve callee-saved registers since
5637     // the compilers are responsible for supplying a continuation point
5638     // if they expect all registers to be preserved.
5639     enum layout {
5640       rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
5641       rbp_off2,
5642       return_off,
5643       return_off2,
5644       framesize // inclusive of return address
5645     };
5646 
5647     int insts_size = 512;
5648     int locs_size  = 64;
5649 
5650     CodeBuffer code(name, insts_size, locs_size);
5651     OopMapSet* oop_maps  = new OopMapSet();
5652     MacroAssembler* masm = new MacroAssembler(&code);
5653 
5654     address start = __ pc();
5655 
5656     // This is an inlined and slightly modified version of call_VM
5657     // which has the ability to fetch the return PC out of
5658     // thread-local storage and also sets up last_Java_sp slightly
5659     // differently than the real call_VM
5660 
5661     __ enter(); // required for proper stackwalking of RuntimeStub frame
5662 
5663     assert(is_even(framesize/2), "sp not 16-byte aligned");
5664 
5665     // return address and rbp are already in place
5666     __ subptr(rsp, (framesize-4) << LogBytesPerInt); // prolog
5667 
5668     int frame_complete = __ pc() - start;
5669 
5670     // Set up last_Java_sp and last_Java_fp
5671     address the_pc = __ pc();
5672     __ set_last_Java_frame(rsp, rbp, the_pc);
5673     __ andptr(rsp, -(StackAlignmentInBytes));    // Align stack
5674 
5675     // Call runtime
5676     if (arg1 != noreg) {
5677       assert(arg2 != c_rarg1, "clobbered");
5678       __ movptr(c_rarg1, arg1);
5679     }
5680     if (arg2 != noreg) {
5681       __ movptr(c_rarg2, arg2);
5682     }
5683     __ movptr(c_rarg0, r15_thread);
5684     BLOCK_COMMENT("call runtime_entry");
5685     __ call(RuntimeAddress(runtime_entry));
5686 
5687     // Generate oop map
5688     OopMap* map = new OopMap(framesize, 0);
5689 
5690     oop_maps->add_gc_map(the_pc - start, map);
5691 
5692     __ reset_last_Java_frame(true);
5693 
5694     __ leave(); // required for proper stackwalking of RuntimeStub frame
5695 
5696     // check for pending exceptions
5697 #ifdef ASSERT
5698     Label L;
5699     __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()),
5700             (int32_t) NULL_WORD);
5701     __ jcc(Assembler::notEqual, L);
5702     __ should_not_reach_here();
5703     __ bind(L);
5704 #endif // ASSERT
5705     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
5706 
5707 
5708     // codeBlob framesize is in words (not VMRegImpl::slot_size)
5709     RuntimeStub* stub =
5710       RuntimeStub::new_runtime_stub(name,
5711                                     &code,
5712                                     frame_complete,
5713                                     (framesize >> (LogBytesPerWord - LogBytesPerInt)),
5714                                     oop_maps, false);
5715     return stub->entry_point();
5716   }
5717 
5718   void create_control_words() {
5719     // Round to nearest, 53-bit mode, exceptions masked
5720     StubRoutines::_fpu_cntrl_wrd_std   = 0x027F;
5721     // Round to zero, 53-bit mode, exception mased
5722     StubRoutines::_fpu_cntrl_wrd_trunc = 0x0D7F;
5723     // Round to nearest, 24-bit mode, exceptions masked
5724     StubRoutines::_fpu_cntrl_wrd_24    = 0x007F;
5725     // Round to nearest, 64-bit mode, exceptions masked
5726     StubRoutines::_mxcsr_std           = 0x1F80;
5727     // Note: the following two constants are 80-bit values
5728     //       layout is critical for correct loading by FPU.
5729     // Bias for strict fp multiply/divide
5730     StubRoutines::_fpu_subnormal_bias1[0]= 0x00000000; // 2^(-15360) == 0x03ff 8000 0000 0000 0000
5731     StubRoutines::_fpu_subnormal_bias1[1]= 0x80000000;
5732     StubRoutines::_fpu_subnormal_bias1[2]= 0x03ff;
5733     // Un-Bias for strict fp multiply/divide
5734     StubRoutines::_fpu_subnormal_bias2[0]= 0x00000000; // 2^(+15360) == 0x7bff 8000 0000 0000 0000
5735     StubRoutines::_fpu_subnormal_bias2[1]= 0x80000000;
5736     StubRoutines::_fpu_subnormal_bias2[2]= 0x7bff;
5737   }
5738 
5739   // Initialization
5740   void generate_initial() {
5741     // Generates all stubs and initializes the entry points
5742 
5743     // This platform-specific settings are needed by generate_call_stub()
5744     create_control_words();
5745 
5746     // entry points that exist in all platforms Note: This is code
5747     // that could be shared among different platforms - however the
5748     // benefit seems to be smaller than the disadvantage of having a
5749     // much more complicated generator structure. See also comment in
5750     // stubRoutines.hpp.
5751 
5752     StubRoutines::_forward_exception_entry = generate_forward_exception();
5753 
5754     StubRoutines::_call_stub_entry =
5755       generate_call_stub(StubRoutines::_call_stub_return_address);
5756 
5757     // is referenced by megamorphic call
5758     StubRoutines::_catch_exception_entry = generate_catch_exception();
5759 
5760     // atomic calls
5761     StubRoutines::_atomic_xchg_entry          = generate_atomic_xchg();
5762     StubRoutines::_atomic_xchg_long_entry     = generate_atomic_xchg_long();
5763     StubRoutines::_atomic_cmpxchg_entry       = generate_atomic_cmpxchg();
5764     StubRoutines::_atomic_cmpxchg_byte_entry  = generate_atomic_cmpxchg_byte();
5765     StubRoutines::_atomic_cmpxchg_long_entry  = generate_atomic_cmpxchg_long();
5766     StubRoutines::_atomic_add_entry           = generate_atomic_add();
5767     StubRoutines::_atomic_add_long_entry      = generate_atomic_add_long();
5768     StubRoutines::_fence_entry                = generate_orderaccess_fence();
5769 
5770     // platform dependent
5771     StubRoutines::x86::_get_previous_fp_entry = generate_get_previous_fp();
5772     StubRoutines::x86::_get_previous_sp_entry = generate_get_previous_sp();
5773 
5774     StubRoutines::x86::_verify_mxcsr_entry    = generate_verify_mxcsr();
5775 
5776     // Build this early so it's available for the interpreter.
5777     StubRoutines::_throw_StackOverflowError_entry =
5778       generate_throw_exception("StackOverflowError throw_exception",
5779                                CAST_FROM_FN_PTR(address,
5780                                                 SharedRuntime::
5781                                                 throw_StackOverflowError));
5782     StubRoutines::_throw_delayed_StackOverflowError_entry =
5783       generate_throw_exception("delayed StackOverflowError throw_exception",
5784                                CAST_FROM_FN_PTR(address,
5785                                                 SharedRuntime::
5786                                                 throw_delayed_StackOverflowError));
5787     if (UseCRC32Intrinsics) {
5788       // set table address before stub generation which use it
5789       StubRoutines::_crc_table_adr = (address)StubRoutines::x86::_crc_table;
5790       StubRoutines::_updateBytesCRC32 = generate_updateBytesCRC32();
5791     }
5792 
5793     if (UseCRC32CIntrinsics) {
5794       bool supports_clmul = VM_Version::supports_clmul();
5795       StubRoutines::x86::generate_CRC32C_table(supports_clmul);
5796       StubRoutines::_crc32c_table_addr = (address)StubRoutines::x86::_crc32c_table;
5797       StubRoutines::_updateBytesCRC32C = generate_updateBytesCRC32C(supports_clmul);
5798     }
5799     if (VM_Version::supports_sse2() && UseLibmIntrinsic && InlineIntrinsics) {
5800       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin) ||
5801           vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos) ||
5802           vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtan)) {
5803         StubRoutines::x86::_ONEHALF_adr = (address)StubRoutines::x86::_ONEHALF;
5804         StubRoutines::x86::_P_2_adr = (address)StubRoutines::x86::_P_2;
5805         StubRoutines::x86::_SC_4_adr = (address)StubRoutines::x86::_SC_4;
5806         StubRoutines::x86::_Ctable_adr = (address)StubRoutines::x86::_Ctable;
5807         StubRoutines::x86::_SC_2_adr = (address)StubRoutines::x86::_SC_2;
5808         StubRoutines::x86::_SC_3_adr = (address)StubRoutines::x86::_SC_3;
5809         StubRoutines::x86::_SC_1_adr = (address)StubRoutines::x86::_SC_1;
5810         StubRoutines::x86::_PI_INV_TABLE_adr = (address)StubRoutines::x86::_PI_INV_TABLE;
5811         StubRoutines::x86::_PI_4_adr = (address)StubRoutines::x86::_PI_4;
5812         StubRoutines::x86::_PI32INV_adr = (address)StubRoutines::x86::_PI32INV;
5813         StubRoutines::x86::_SIGN_MASK_adr = (address)StubRoutines::x86::_SIGN_MASK;
5814         StubRoutines::x86::_P_1_adr = (address)StubRoutines::x86::_P_1;
5815         StubRoutines::x86::_P_3_adr = (address)StubRoutines::x86::_P_3;
5816         StubRoutines::x86::_NEG_ZERO_adr = (address)StubRoutines::x86::_NEG_ZERO;
5817       }
5818       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dexp)) {
5819         StubRoutines::_dexp = generate_libmExp();
5820       }
5821       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog)) {
5822         StubRoutines::_dlog = generate_libmLog();
5823       }
5824       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog10)) {
5825         StubRoutines::_dlog10 = generate_libmLog10();
5826       }
5827       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dpow)) {
5828         StubRoutines::_dpow = generate_libmPow();
5829       }
5830       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin)) {
5831         StubRoutines::_dsin = generate_libmSin();
5832       }
5833       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos)) {
5834         StubRoutines::_dcos = generate_libmCos();
5835       }
5836       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtan)) {
5837         StubRoutines::_dtan = generate_libmTan();
5838       }
5839     }
5840   }
5841 
5842   void generate_all() {
5843     // Generates all stubs and initializes the entry points
5844 
5845     // These entry points require SharedInfo::stack0 to be set up in
5846     // non-core builds and need to be relocatable, so they each
5847     // fabricate a RuntimeStub internally.
5848     StubRoutines::_throw_AbstractMethodError_entry =
5849       generate_throw_exception("AbstractMethodError throw_exception",
5850                                CAST_FROM_FN_PTR(address,
5851                                                 SharedRuntime::
5852                                                 throw_AbstractMethodError));
5853 
5854     StubRoutines::_throw_IncompatibleClassChangeError_entry =
5855       generate_throw_exception("IncompatibleClassChangeError throw_exception",
5856                                CAST_FROM_FN_PTR(address,
5857                                                 SharedRuntime::
5858                                                 throw_IncompatibleClassChangeError));
5859 
5860     StubRoutines::_throw_NullPointerException_at_call_entry =
5861       generate_throw_exception("NullPointerException at call throw_exception",
5862                                CAST_FROM_FN_PTR(address,
5863                                                 SharedRuntime::
5864                                                 throw_NullPointerException_at_call));
5865 
5866     // entry points that are platform specific
5867     StubRoutines::x86::_f2i_fixup = generate_f2i_fixup();
5868     StubRoutines::x86::_f2l_fixup = generate_f2l_fixup();
5869     StubRoutines::x86::_d2i_fixup = generate_d2i_fixup();
5870     StubRoutines::x86::_d2l_fixup = generate_d2l_fixup();
5871 
5872     StubRoutines::x86::_float_sign_mask  = generate_fp_mask("float_sign_mask",  0x7FFFFFFF7FFFFFFF);
5873     StubRoutines::x86::_float_sign_flip  = generate_fp_mask("float_sign_flip",  0x8000000080000000);
5874     StubRoutines::x86::_double_sign_mask = generate_fp_mask("double_sign_mask", 0x7FFFFFFFFFFFFFFF);
5875     StubRoutines::x86::_double_sign_flip = generate_fp_mask("double_sign_flip", 0x8000000000000000);
5876 
5877     // support for verify_oop (must happen after universe_init)
5878     StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
5879 
5880     // arraycopy stubs used by compilers
5881     generate_arraycopy_stubs();
5882 
5883     // don't bother generating these AES intrinsic stubs unless global flag is set
5884     if (UseAESIntrinsics) {
5885       StubRoutines::x86::_key_shuffle_mask_addr = generate_key_shuffle_mask();  // needed by the others
5886       StubRoutines::_aescrypt_encryptBlock = generate_aescrypt_encryptBlock();
5887       StubRoutines::_aescrypt_decryptBlock = generate_aescrypt_decryptBlock();
5888       StubRoutines::_cipherBlockChaining_encryptAESCrypt = generate_cipherBlockChaining_encryptAESCrypt();
5889       if (VM_Version::supports_vaes() &&  VM_Version::supports_avx512vl() && VM_Version::supports_avx512dq() ) {
5890         StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptVectorAESCrypt();
5891       } else {
5892         StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptAESCrypt_Parallel();
5893       }
5894     }
5895     if (UseAESCTRIntrinsics){
5896       StubRoutines::x86::_counter_shuffle_mask_addr = generate_counter_shuffle_mask();
5897       StubRoutines::_counterMode_AESCrypt = generate_counterMode_AESCrypt_Parallel();
5898     }
5899 
5900     if (UseSHA1Intrinsics) {
5901       StubRoutines::x86::_upper_word_mask_addr = generate_upper_word_mask();
5902       StubRoutines::x86::_shuffle_byte_flip_mask_addr = generate_shuffle_byte_flip_mask();
5903       StubRoutines::_sha1_implCompress = generate_sha1_implCompress(false, "sha1_implCompress");
5904       StubRoutines::_sha1_implCompressMB = generate_sha1_implCompress(true, "sha1_implCompressMB");
5905     }
5906     if (UseSHA256Intrinsics) {
5907       StubRoutines::x86::_k256_adr = (address)StubRoutines::x86::_k256;
5908       char* dst = (char*)StubRoutines::x86::_k256_W;
5909       char* src = (char*)StubRoutines::x86::_k256;
5910       for (int ii = 0; ii < 16; ++ii) {
5911         memcpy(dst + 32 * ii,      src + 16 * ii, 16);
5912         memcpy(dst + 32 * ii + 16, src + 16 * ii, 16);
5913       }
5914       StubRoutines::x86::_k256_W_adr = (address)StubRoutines::x86::_k256_W;
5915       StubRoutines::x86::_pshuffle_byte_flip_mask_addr = generate_pshuffle_byte_flip_mask();
5916       StubRoutines::_sha256_implCompress = generate_sha256_implCompress(false, "sha256_implCompress");
5917       StubRoutines::_sha256_implCompressMB = generate_sha256_implCompress(true, "sha256_implCompressMB");
5918     }
5919     if (UseSHA512Intrinsics) {
5920       StubRoutines::x86::_k512_W_addr = (address)StubRoutines::x86::_k512_W;
5921       StubRoutines::x86::_pshuffle_byte_flip_mask_addr_sha512 = generate_pshuffle_byte_flip_mask_sha512();
5922       StubRoutines::_sha512_implCompress = generate_sha512_implCompress(false, "sha512_implCompress");
5923       StubRoutines::_sha512_implCompressMB = generate_sha512_implCompress(true, "sha512_implCompressMB");
5924     }
5925 
5926     // Generate GHASH intrinsics code
5927     if (UseGHASHIntrinsics) {
5928     StubRoutines::x86::_ghash_long_swap_mask_addr = generate_ghash_long_swap_mask();
5929     StubRoutines::x86::_ghash_byte_swap_mask_addr = generate_ghash_byte_swap_mask();
5930       if (VM_Version::supports_avx()) {
5931         StubRoutines::x86::_ghash_shuffmask_addr = ghash_shufflemask_addr();
5932         StubRoutines::x86::_ghash_poly_addr = ghash_polynomial_addr();
5933         StubRoutines::_ghash_processBlocks = generate_avx_ghash_processBlocks();
5934       } else {
5935         StubRoutines::_ghash_processBlocks = generate_ghash_processBlocks();
5936       }
5937     }
5938 
5939     if (UseBASE64Intrinsics) {
5940       StubRoutines::x86::_and_mask = base64_and_mask_addr();
5941       StubRoutines::x86::_bswap_mask = base64_bswap_mask_addr();
5942       StubRoutines::x86::_base64_charset = base64_charset_addr();
5943       StubRoutines::x86::_url_charset = base64url_charset_addr();
5944       StubRoutines::x86::_gather_mask = base64_gather_mask_addr();
5945       StubRoutines::x86::_left_shift_mask = base64_left_shift_mask_addr();
5946       StubRoutines::x86::_right_shift_mask = base64_right_shift_mask_addr();
5947       StubRoutines::_base64_encodeBlock = generate_base64_encodeBlock();
5948     }
5949 
5950     // Safefetch stubs.
5951     generate_safefetch("SafeFetch32", sizeof(int),     &StubRoutines::_safefetch32_entry,
5952                                                        &StubRoutines::_safefetch32_fault_pc,
5953                                                        &StubRoutines::_safefetch32_continuation_pc);
5954     generate_safefetch("SafeFetchN", sizeof(intptr_t), &StubRoutines::_safefetchN_entry,
5955                                                        &StubRoutines::_safefetchN_fault_pc,
5956                                                        &StubRoutines::_safefetchN_continuation_pc);
5957 
5958     BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
5959     if (bs_nm != NULL) {
5960       StubRoutines::x86::_method_entry_barrier = generate_method_entry_barrier();
5961     }
5962 #ifdef COMPILER2
5963     if (UseMultiplyToLenIntrinsic) {
5964       StubRoutines::_multiplyToLen = generate_multiplyToLen();
5965     }
5966     if (UseSquareToLenIntrinsic) {
5967       StubRoutines::_squareToLen = generate_squareToLen();
5968     }
5969     if (UseMulAddIntrinsic) {
5970       StubRoutines::_mulAdd = generate_mulAdd();
5971     }
5972 #ifndef _WINDOWS
5973     if (UseMontgomeryMultiplyIntrinsic) {
5974       StubRoutines::_montgomeryMultiply
5975         = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_multiply);
5976     }
5977     if (UseMontgomerySquareIntrinsic) {
5978       StubRoutines::_montgomerySquare
5979         = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_square);
5980     }
5981 #endif // WINDOWS
5982 #endif // COMPILER2
5983 
5984     if (UseVectorizedMismatchIntrinsic) {
5985       StubRoutines::_vectorizedMismatch = generate_vectorizedMismatch();
5986     }
5987   }
5988 
5989  public:
5990   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
5991     if (all) {
5992       generate_all();
5993     } else {
5994       generate_initial();
5995     }
5996   }
5997 }; // end class declaration
5998 
5999 void StubGenerator_generate(CodeBuffer* code, bool all) {
6000   StubGenerator g(code, all);
6001 }