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