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