1 /*
   2  * Copyright (c) 1999, 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 "gc/shared/cardTable.hpp"
  29 #include "gc/shared/cardTableModRefBS.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "nativeInst_x86.hpp"
  32 #include "oops/instanceOop.hpp"
  33 #include "oops/method.hpp"
  34 #include "oops/objArrayKlass.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "prims/methodHandles.hpp"
  37 #include "runtime/frame.inline.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "runtime/sharedRuntime.hpp"
  40 #include "runtime/stubCodeGenerator.hpp"
  41 #include "runtime/stubRoutines.hpp"
  42 #include "runtime/thread.inline.hpp"
  43 #ifdef COMPILER2
  44 #include "opto/runtime.hpp"
  45 #endif
  46 
  47 // Declaration and definition of StubGenerator (no .hpp file).
  48 // For a more detailed description of the stub routine structure
  49 // see the comment in stubRoutines.hpp
  50 
  51 #define __ _masm->
  52 #define a__ ((Assembler*)_masm)->
  53 
  54 #ifdef PRODUCT
  55 #define BLOCK_COMMENT(str) /* nothing */
  56 #else
  57 #define BLOCK_COMMENT(str) __ block_comment(str)
  58 #endif
  59 
  60 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  61 
  62 const int MXCSR_MASK  = 0xFFC0;  // Mask out any pending exceptions
  63 const int FPU_CNTRL_WRD_MASK = 0xFFFF;
  64 
  65 // -------------------------------------------------------------------------------------------------------------------------
  66 // Stub Code definitions
  67 
  68 class StubGenerator: public StubCodeGenerator {
  69  private:
  70 
  71 #ifdef PRODUCT
  72 #define inc_counter_np(counter) ((void)0)
  73 #else
  74   void inc_counter_np_(int& counter) {
  75     __ incrementl(ExternalAddress((address)&counter));
  76   }
  77 #define inc_counter_np(counter) \
  78   BLOCK_COMMENT("inc_counter " #counter); \
  79   inc_counter_np_(counter);
  80 #endif //PRODUCT
  81 
  82   void inc_copy_counter_np(BasicType t) {
  83 #ifndef PRODUCT
  84     switch (t) {
  85     case T_BYTE:    inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); return;
  86     case T_SHORT:   inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); return;
  87     case T_INT:     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); return;
  88     case T_LONG:    inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); return;
  89     case T_OBJECT:  inc_counter_np(SharedRuntime::_oop_array_copy_ctr); return;
  90     }
  91     ShouldNotReachHere();
  92 #endif //PRODUCT
  93   }
  94 
  95   //------------------------------------------------------------------------------------------------------------------------
  96   // Call stubs are used to call Java from C
  97   //
  98   //    [ return_from_Java     ] <--- rsp
  99   //    [ argument word n      ]
 100   //      ...
 101   // -N [ argument word 1      ]
 102   // -7 [ Possible padding for stack alignment ]
 103   // -6 [ Possible padding for stack alignment ]
 104   // -5 [ Possible padding for stack alignment ]
 105   // -4 [ mxcsr save           ] <--- rsp_after_call
 106   // -3 [ saved rbx,            ]
 107   // -2 [ saved rsi            ]
 108   // -1 [ saved rdi            ]
 109   //  0 [ saved rbp,            ] <--- rbp,
 110   //  1 [ return address       ]
 111   //  2 [ ptr. to call wrapper ]
 112   //  3 [ result               ]
 113   //  4 [ result_type          ]
 114   //  5 [ method               ]
 115   //  6 [ entry_point          ]
 116   //  7 [ parameters           ]
 117   //  8 [ parameter_size       ]
 118   //  9 [ thread               ]
 119 
 120 
 121   address generate_call_stub(address& return_address) {
 122     StubCodeMark mark(this, "StubRoutines", "call_stub");
 123     address start = __ pc();
 124 
 125     // stub code parameters / addresses
 126     assert(frame::entry_frame_call_wrapper_offset == 2, "adjust this code");
 127     bool  sse_save = false;
 128     const Address rsp_after_call(rbp, -4 * wordSize); // same as in generate_catch_exception()!
 129     const int     locals_count_in_bytes  (4*wordSize);
 130     const Address mxcsr_save    (rbp, -4 * wordSize);
 131     const Address saved_rbx     (rbp, -3 * wordSize);
 132     const Address saved_rsi     (rbp, -2 * wordSize);
 133     const Address saved_rdi     (rbp, -1 * wordSize);
 134     const Address result        (rbp,  3 * wordSize);
 135     const Address result_type   (rbp,  4 * wordSize);
 136     const Address method        (rbp,  5 * wordSize);
 137     const Address entry_point   (rbp,  6 * wordSize);
 138     const Address parameters    (rbp,  7 * wordSize);
 139     const Address parameter_size(rbp,  8 * wordSize);
 140     const Address thread        (rbp,  9 * wordSize); // same as in generate_catch_exception()!
 141     sse_save =  UseSSE > 0;
 142 
 143     // stub code
 144     __ enter();
 145     __ movptr(rcx, parameter_size);              // parameter counter
 146     __ shlptr(rcx, Interpreter::logStackElementSize); // convert parameter count to bytes
 147     __ addptr(rcx, locals_count_in_bytes);       // reserve space for register saves
 148     __ subptr(rsp, rcx);
 149     __ andptr(rsp, -(StackAlignmentInBytes));    // Align stack
 150 
 151     // save rdi, rsi, & rbx, according to C calling conventions
 152     __ movptr(saved_rdi, rdi);
 153     __ movptr(saved_rsi, rsi);
 154     __ movptr(saved_rbx, rbx);
 155 
 156     // provide initial value for required masks
 157     if (UseAVX > 2) {
 158       __ movl(rbx, 0xffff);
 159       __ kmovwl(k1, rbx);
 160     }
 161 
 162     // save and initialize %mxcsr
 163     if (sse_save) {
 164       Label skip_ldmx;
 165       __ stmxcsr(mxcsr_save);
 166       __ movl(rax, mxcsr_save);
 167       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 168       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 169       __ cmp32(rax, mxcsr_std);
 170       __ jcc(Assembler::equal, skip_ldmx);
 171       __ ldmxcsr(mxcsr_std);
 172       __ bind(skip_ldmx);
 173     }
 174 
 175     // make sure the control word is correct.
 176     __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
 177 
 178 #ifdef ASSERT
 179     // make sure we have no pending exceptions
 180     { Label L;
 181       __ movptr(rcx, thread);
 182       __ cmpptr(Address(rcx, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 183       __ jcc(Assembler::equal, L);
 184       __ stop("StubRoutines::call_stub: entered with pending exception");
 185       __ bind(L);
 186     }
 187 #endif
 188 
 189     // pass parameters if any
 190     BLOCK_COMMENT("pass parameters if any");
 191     Label parameters_done;
 192     __ movl(rcx, parameter_size);  // parameter counter
 193     __ testl(rcx, rcx);
 194     __ jcc(Assembler::zero, parameters_done);
 195 
 196     // parameter passing loop
 197 
 198     Label loop;
 199     // Copy Java parameters in reverse order (receiver last)
 200     // Note that the argument order is inverted in the process
 201     // source is rdx[rcx: N-1..0]
 202     // dest   is rsp[rbx: 0..N-1]
 203 
 204     __ movptr(rdx, parameters);          // parameter pointer
 205     __ xorptr(rbx, rbx);
 206 
 207     __ BIND(loop);
 208 
 209     // get parameter
 210     __ movptr(rax, Address(rdx, rcx, Interpreter::stackElementScale(), -wordSize));
 211     __ movptr(Address(rsp, rbx, Interpreter::stackElementScale(),
 212                     Interpreter::expr_offset_in_bytes(0)), rax);          // store parameter
 213     __ increment(rbx);
 214     __ decrement(rcx);
 215     __ jcc(Assembler::notZero, loop);
 216 
 217     // call Java function
 218     __ BIND(parameters_done);
 219     __ movptr(rbx, method);           // get Method*
 220     __ movptr(rax, entry_point);      // get entry_point
 221     __ mov(rsi, rsp);                 // set sender sp
 222     BLOCK_COMMENT("call Java function");
 223     __ call(rax);
 224 
 225     BLOCK_COMMENT("call_stub_return_address:");
 226     return_address = __ pc();
 227 
 228 #ifdef COMPILER2
 229     {
 230       Label L_skip;
 231       if (UseSSE >= 2) {
 232         __ verify_FPU(0, "call_stub_return");
 233       } else {
 234         for (int i = 1; i < 8; i++) {
 235           __ ffree(i);
 236         }
 237 
 238         // UseSSE <= 1 so double result should be left on TOS
 239         __ movl(rsi, result_type);
 240         __ cmpl(rsi, T_DOUBLE);
 241         __ jcc(Assembler::equal, L_skip);
 242         if (UseSSE == 0) {
 243           // UseSSE == 0 so float result should be left on TOS
 244           __ cmpl(rsi, T_FLOAT);
 245           __ jcc(Assembler::equal, L_skip);
 246         }
 247         __ ffree(0);
 248       }
 249       __ BIND(L_skip);
 250     }
 251 #endif // COMPILER2
 252 
 253     // store result depending on type
 254     // (everything that is not T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
 255     __ movptr(rdi, result);
 256     Label is_long, is_float, is_double, exit;
 257     __ movl(rsi, result_type);
 258     __ cmpl(rsi, T_LONG);
 259     __ jcc(Assembler::equal, is_long);
 260     __ cmpl(rsi, T_FLOAT);
 261     __ jcc(Assembler::equal, is_float);
 262     __ cmpl(rsi, T_DOUBLE);
 263     __ jcc(Assembler::equal, is_double);
 264 
 265     // handle T_INT case
 266     __ movl(Address(rdi, 0), rax);
 267     __ BIND(exit);
 268 
 269     // check that FPU stack is empty
 270     __ verify_FPU(0, "generate_call_stub");
 271 
 272     // pop parameters
 273     __ lea(rsp, rsp_after_call);
 274 
 275     // restore %mxcsr
 276     if (sse_save) {
 277       __ ldmxcsr(mxcsr_save);
 278     }
 279 
 280     // restore rdi, rsi and rbx,
 281     __ movptr(rbx, saved_rbx);
 282     __ movptr(rsi, saved_rsi);
 283     __ movptr(rdi, saved_rdi);
 284     __ addptr(rsp, 4*wordSize);
 285 
 286     // return
 287     __ pop(rbp);
 288     __ ret(0);
 289 
 290     // handle return types different from T_INT
 291     __ BIND(is_long);
 292     __ movl(Address(rdi, 0 * wordSize), rax);
 293     __ movl(Address(rdi, 1 * wordSize), rdx);
 294     __ jmp(exit);
 295 
 296     __ BIND(is_float);
 297     // interpreter uses xmm0 for return values
 298     if (UseSSE >= 1) {
 299       __ movflt(Address(rdi, 0), xmm0);
 300     } else {
 301       __ fstp_s(Address(rdi, 0));
 302     }
 303     __ jmp(exit);
 304 
 305     __ BIND(is_double);
 306     // interpreter uses xmm0 for return values
 307     if (UseSSE >= 2) {
 308       __ movdbl(Address(rdi, 0), xmm0);
 309     } else {
 310       __ fstp_d(Address(rdi, 0));
 311     }
 312     __ jmp(exit);
 313 
 314     return start;
 315   }
 316 
 317 
 318   //------------------------------------------------------------------------------------------------------------------------
 319   // Return point for a Java call if there's an exception thrown in Java code.
 320   // The exception is caught and transformed into a pending exception stored in
 321   // JavaThread that can be tested from within the VM.
 322   //
 323   // Note: Usually the parameters are removed by the callee. In case of an exception
 324   //       crossing an activation frame boundary, that is not the case if the callee
 325   //       is compiled code => need to setup the rsp.
 326   //
 327   // rax,: exception oop
 328 
 329   address generate_catch_exception() {
 330     StubCodeMark mark(this, "StubRoutines", "catch_exception");
 331     const Address rsp_after_call(rbp, -4 * wordSize); // same as in generate_call_stub()!
 332     const Address thread        (rbp,  9 * wordSize); // same as in generate_call_stub()!
 333     address start = __ pc();
 334 
 335     // get thread directly
 336     __ movptr(rcx, thread);
 337 #ifdef ASSERT
 338     // verify that threads correspond
 339     { Label L;
 340       __ get_thread(rbx);
 341       __ cmpptr(rbx, rcx);
 342       __ jcc(Assembler::equal, L);
 343       __ stop("StubRoutines::catch_exception: threads must correspond");
 344       __ bind(L);
 345     }
 346 #endif
 347     // set pending exception
 348     __ verify_oop(rax);
 349     __ movptr(Address(rcx, Thread::pending_exception_offset()), rax          );
 350     __ lea(Address(rcx, Thread::exception_file_offset   ()),
 351            ExternalAddress((address)__FILE__));
 352     __ movl(Address(rcx, Thread::exception_line_offset   ()), __LINE__ );
 353     // complete return to VM
 354     assert(StubRoutines::_call_stub_return_address != NULL, "_call_stub_return_address must have been generated before");
 355     __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
 356 
 357     return start;
 358   }
 359 
 360 
 361   //------------------------------------------------------------------------------------------------------------------------
 362   // Continuation point for runtime calls returning with a pending exception.
 363   // The pending exception check happened in the runtime or native call stub.
 364   // The pending exception in Thread is converted into a Java-level exception.
 365   //
 366   // Contract with Java-level exception handlers:
 367   // rax: exception
 368   // rdx: throwing pc
 369   //
 370   // NOTE: At entry of this stub, exception-pc must be on stack !!
 371 
 372   address generate_forward_exception() {
 373     StubCodeMark mark(this, "StubRoutines", "forward exception");
 374     address start = __ pc();
 375     const Register thread = rcx;
 376 
 377     // other registers used in this stub
 378     const Register exception_oop = rax;
 379     const Register handler_addr  = rbx;
 380     const Register exception_pc  = rdx;
 381 
 382     // Upon entry, the sp points to the return address returning into Java
 383     // (interpreted or compiled) code; i.e., the return address becomes the
 384     // throwing pc.
 385     //
 386     // Arguments pushed before the runtime call are still on the stack but
 387     // the exception handler will reset the stack pointer -> ignore them.
 388     // A potential result in registers can be ignored as well.
 389 
 390 #ifdef ASSERT
 391     // make sure this code is only executed if there is a pending exception
 392     { Label L;
 393       __ get_thread(thread);
 394       __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 395       __ jcc(Assembler::notEqual, L);
 396       __ stop("StubRoutines::forward exception: no pending exception (1)");
 397       __ bind(L);
 398     }
 399 #endif
 400 
 401     // compute exception handler into rbx,
 402     __ get_thread(thread);
 403     __ movptr(exception_pc, Address(rsp, 0));
 404     BLOCK_COMMENT("call exception_handler_for_return_address");
 405     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), thread, exception_pc);
 406     __ mov(handler_addr, rax);
 407 
 408     // setup rax & rdx, remove return address & clear pending exception
 409     __ get_thread(thread);
 410     __ pop(exception_pc);
 411     __ movptr(exception_oop, Address(thread, Thread::pending_exception_offset()));
 412     __ movptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
 413 
 414 #ifdef ASSERT
 415     // make sure exception is set
 416     { Label L;
 417       __ testptr(exception_oop, exception_oop);
 418       __ jcc(Assembler::notEqual, L);
 419       __ stop("StubRoutines::forward exception: no pending exception (2)");
 420       __ bind(L);
 421     }
 422 #endif
 423 
 424     // Verify that there is really a valid exception in RAX.
 425     __ verify_oop(exception_oop);
 426 
 427     // continue at exception handler (return address removed)
 428     // rax: exception
 429     // rbx: exception handler
 430     // rdx: throwing pc
 431     __ jmp(handler_addr);
 432 
 433     return start;
 434   }
 435 
 436 
 437   //----------------------------------------------------------------------------------------------------
 438   // Support for int32_t Atomic::xchg(int32_t exchange_value, volatile int32_t* dest)
 439   //
 440   // xchg exists as far back as 8086, lock needed for MP only
 441   // Stack layout immediately after call:
 442   //
 443   // 0 [ret addr ] <--- rsp
 444   // 1 [  ex     ]
 445   // 2 [  dest   ]
 446   //
 447   // Result:   *dest <- ex, return (old *dest)
 448   //
 449   // Note: win32 does not currently use this code
 450 
 451   address generate_atomic_xchg() {
 452     StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
 453     address start = __ pc();
 454 
 455     __ push(rdx);
 456     Address exchange(rsp, 2 * wordSize);
 457     Address dest_addr(rsp, 3 * wordSize);
 458     __ movl(rax, exchange);
 459     __ movptr(rdx, dest_addr);
 460     __ xchgl(rax, Address(rdx, 0));
 461     __ pop(rdx);
 462     __ ret(0);
 463 
 464     return start;
 465   }
 466 
 467   //----------------------------------------------------------------------------------------------------
 468   // Support for void verify_mxcsr()
 469   //
 470   // This routine is used with -Xcheck:jni to verify that native
 471   // JNI code does not return to Java code without restoring the
 472   // MXCSR register to our expected state.
 473 
 474 
 475   address generate_verify_mxcsr() {
 476     StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
 477     address start = __ pc();
 478 
 479     const Address mxcsr_save(rsp, 0);
 480 
 481     if (CheckJNICalls && UseSSE > 0 ) {
 482       Label ok_ret;
 483       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 484       __ push(rax);
 485       __ subptr(rsp, wordSize);      // allocate a temp location
 486       __ stmxcsr(mxcsr_save);
 487       __ movl(rax, mxcsr_save);
 488       __ andl(rax, MXCSR_MASK);
 489       __ cmp32(rax, mxcsr_std);
 490       __ jcc(Assembler::equal, ok_ret);
 491 
 492       __ warn("MXCSR changed by native JNI code.");
 493 
 494       __ ldmxcsr(mxcsr_std);
 495 
 496       __ bind(ok_ret);
 497       __ addptr(rsp, wordSize);
 498       __ pop(rax);
 499     }
 500 
 501     __ ret(0);
 502 
 503     return start;
 504   }
 505 
 506 
 507   //---------------------------------------------------------------------------
 508   // Support for void verify_fpu_cntrl_wrd()
 509   //
 510   // This routine is used with -Xcheck:jni to verify that native
 511   // JNI code does not return to Java code without restoring the
 512   // FP control word to our expected state.
 513 
 514   address generate_verify_fpu_cntrl_wrd() {
 515     StubCodeMark mark(this, "StubRoutines", "verify_spcw");
 516     address start = __ pc();
 517 
 518     const Address fpu_cntrl_wrd_save(rsp, 0);
 519 
 520     if (CheckJNICalls) {
 521       Label ok_ret;
 522       __ push(rax);
 523       __ subptr(rsp, wordSize);      // allocate a temp location
 524       __ fnstcw(fpu_cntrl_wrd_save);
 525       __ movl(rax, fpu_cntrl_wrd_save);
 526       __ andl(rax, FPU_CNTRL_WRD_MASK);
 527       ExternalAddress fpu_std(StubRoutines::addr_fpu_cntrl_wrd_std());
 528       __ cmp32(rax, fpu_std);
 529       __ jcc(Assembler::equal, ok_ret);
 530 
 531       __ warn("Floating point control word changed by native JNI code.");
 532 
 533       __ fldcw(fpu_std);
 534 
 535       __ bind(ok_ret);
 536       __ addptr(rsp, wordSize);
 537       __ pop(rax);
 538     }
 539 
 540     __ ret(0);
 541 
 542     return start;
 543   }
 544 
 545   //---------------------------------------------------------------------------
 546   // Wrapper for slow-case handling of double-to-integer conversion
 547   // d2i or f2i fast case failed either because it is nan or because
 548   // of under/overflow.
 549   // Input:  FPU TOS: float value
 550   // Output: rax, (rdx): integer (long) result
 551 
 552   address generate_d2i_wrapper(BasicType t, address fcn) {
 553     StubCodeMark mark(this, "StubRoutines", "d2i_wrapper");
 554     address start = __ pc();
 555 
 556   // Capture info about frame layout
 557   enum layout { FPUState_off         = 0,
 558                 rbp_off              = FPUStateSizeInWords,
 559                 rdi_off,
 560                 rsi_off,
 561                 rcx_off,
 562                 rbx_off,
 563                 saved_argument_off,
 564                 saved_argument_off2, // 2nd half of double
 565                 framesize
 566   };
 567 
 568   assert(FPUStateSizeInWords == 27, "update stack layout");
 569 
 570     // Save outgoing argument to stack across push_FPU_state()
 571     __ subptr(rsp, wordSize * 2);
 572     __ fstp_d(Address(rsp, 0));
 573 
 574     // Save CPU & FPU state
 575     __ push(rbx);
 576     __ push(rcx);
 577     __ push(rsi);
 578     __ push(rdi);
 579     __ push(rbp);
 580     __ push_FPU_state();
 581 
 582     // push_FPU_state() resets the FP top of stack
 583     // Load original double into FP top of stack
 584     __ fld_d(Address(rsp, saved_argument_off * wordSize));
 585     // Store double into stack as outgoing argument
 586     __ subptr(rsp, wordSize*2);
 587     __ fst_d(Address(rsp, 0));
 588 
 589     // Prepare FPU for doing math in C-land
 590     __ empty_FPU_stack();
 591     // Call the C code to massage the double.  Result in EAX
 592     if (t == T_INT)
 593       { BLOCK_COMMENT("SharedRuntime::d2i"); }
 594     else if (t == T_LONG)
 595       { BLOCK_COMMENT("SharedRuntime::d2l"); }
 596     __ call_VM_leaf( fcn, 2 );
 597 
 598     // Restore CPU & FPU state
 599     __ pop_FPU_state();
 600     __ pop(rbp);
 601     __ pop(rdi);
 602     __ pop(rsi);
 603     __ pop(rcx);
 604     __ pop(rbx);
 605     __ addptr(rsp, wordSize * 2);
 606 
 607     __ ret(0);
 608 
 609     return start;
 610   }
 611 
 612 
 613   //----------------------------------------------------------------------------------------------------
 614   // Non-destructive plausibility checks for oops
 615 
 616   address generate_verify_oop() {
 617     StubCodeMark mark(this, "StubRoutines", "verify_oop");
 618     address start = __ pc();
 619 
 620     // Incoming arguments on stack after saving rax,:
 621     //
 622     // [tos    ]: saved rdx
 623     // [tos + 1]: saved EFLAGS
 624     // [tos + 2]: return address
 625     // [tos + 3]: char* error message
 626     // [tos + 4]: oop   object to verify
 627     // [tos + 5]: saved rax, - saved by caller and bashed
 628 
 629     Label exit, error;
 630     __ pushf();
 631     __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
 632     __ push(rdx);                                // save rdx
 633     // make sure object is 'reasonable'
 634     __ movptr(rax, Address(rsp, 4 * wordSize));    // get object
 635     __ testptr(rax, rax);
 636     __ jcc(Assembler::zero, exit);               // if obj is NULL it is ok
 637 
 638     // Check if the oop is in the right area of memory
 639     const int oop_mask = Universe::verify_oop_mask();
 640     const int oop_bits = Universe::verify_oop_bits();
 641     __ mov(rdx, rax);
 642     __ andptr(rdx, oop_mask);
 643     __ cmpptr(rdx, oop_bits);
 644     __ jcc(Assembler::notZero, error);
 645 
 646     // make sure klass is 'reasonable', which is not zero.
 647     __ movptr(rax, Address(rax, oopDesc::klass_offset_in_bytes())); // get klass
 648     __ testptr(rax, rax);
 649     __ jcc(Assembler::zero, error);              // if klass is NULL it is broken
 650 
 651     // return if everything seems ok
 652     __ bind(exit);
 653     __ movptr(rax, Address(rsp, 5 * wordSize));  // get saved rax, back
 654     __ pop(rdx);                                 // restore rdx
 655     __ popf();                                   // restore EFLAGS
 656     __ ret(3 * wordSize);                        // pop arguments
 657 
 658     // handle errors
 659     __ bind(error);
 660     __ movptr(rax, Address(rsp, 5 * wordSize));  // get saved rax, back
 661     __ pop(rdx);                                 // get saved rdx back
 662     __ popf();                                   // get saved EFLAGS off stack -- will be ignored
 663     __ pusha();                                  // push registers (eip = return address & msg are already pushed)
 664     BLOCK_COMMENT("call MacroAssembler::debug");
 665     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug32)));
 666     __ popa();
 667     __ ret(3 * wordSize);                        // pop arguments
 668     return start;
 669   }
 670 
 671   //
 672   //  Generate pre-barrier for array stores
 673   //
 674   //  Input:
 675   //     start   -  starting address
 676   //     count   -  element count
 677   void  gen_write_ref_array_pre_barrier(Register start, Register count, bool uninitialized_target) {
 678     assert_different_registers(start, count);
 679     BarrierSet* bs = Universe::heap()->barrier_set();
 680     switch (bs->kind()) {
 681       case BarrierSet::G1BarrierSet:
 682         // With G1, don't generate the call if we statically know that the target in uninitialized
 683         if (!uninitialized_target) {
 684           Register thread = rax;
 685           Label filtered;
 686           __ push(thread);
 687           __ get_thread(thread);
 688           Address in_progress(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
 689                                                SATBMarkQueue::byte_offset_of_active()));
 690           // Is marking active?
 691           if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 692             __ cmpl(in_progress, 0);
 693           } else {
 694             assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 695             __ cmpb(in_progress, 0);
 696           }
 697           __ pop(thread);
 698           __ jcc(Assembler::equal, filtered);
 699 
 700            __ pusha();                      // push registers
 701            __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre),
 702                            start, count);
 703            __ popa();
 704 
 705            __ bind(filtered);
 706          }
 707         break;
 708 #endif // INCLUDE_ALL_GCS
 709       case BarrierSet::CardTableModRef:
 710         break;
 711       default      :
 712         ShouldNotReachHere();
 713 
 714     }
 715   }
 716 
 717 
 718   //
 719   // Generate a post-barrier for an array store
 720   //
 721   //     start    -  starting address
 722   //     count    -  element count
 723   //
 724   //  The two input registers are overwritten.
 725   //
 726   void  gen_write_ref_array_post_barrier(Register start, Register count) {
 727     BarrierSet* bs = Universe::heap()->barrier_set();
 728     assert_different_registers(start, count);
 729     switch (bs->kind()) {
 730       case BarrierSet::G1BarrierSet:
 731         {
 732           __ pusha();                      // push registers
 733           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post),
 734                           start, count);
 735           __ popa();
 736         }
 737         break;
 738 #endif // INCLUDE_ALL_GCS
 739 
 740       case BarrierSet::CardTableModRef:
 741         {
 742           CardTableModRefBS* ctbs = barrier_set_cast<CardTableModRefBS>(bs);
 743           CardTable* ct = ctbs->card_table();
 744           assert(sizeof(*ct->byte_map_base()) == sizeof(jbyte), "adjust this code");
 745 
 746           Label L_loop;
 747           const Register end = count;  // elements count; end == start+count-1
 748           assert_different_registers(start, end);
 749 
 750           __ lea(end,  Address(start, count, Address::times_ptr, -wordSize));
 751           __ shrptr(start, CardTable::card_shift);
 752           __ shrptr(end,   CardTable::card_shift);
 753           __ subptr(end, start); // end --> count
 754         __ BIND(L_loop);
 755         intptr_t disp = (intptr_t) ct->byte_map_base();
 756           Address cardtable(start, count, Address::times_1, disp);
 757           __ movb(cardtable, 0);
 758           __ decrement(count);
 759           __ jcc(Assembler::greaterEqual, L_loop);
 760         }
 761         break;
 762       case BarrierSet::ModRef:
 763         break;
 764       default      :
 765         ShouldNotReachHere();
 766 
 767     }
 768   }
 769 
 770 
 771   // Copy 64 bytes chunks
 772   //
 773   // Inputs:
 774   //   from        - source array address
 775   //   to_from     - destination array address - from
 776   //   qword_count - 8-bytes element count, negative
 777   //
 778   void xmm_copy_forward(Register from, Register to_from, Register qword_count) {
 779     assert( UseSSE >= 2, "supported cpu only" );
 780     Label L_copy_64_bytes_loop, L_copy_64_bytes, L_copy_8_bytes, L_exit;
 781     if (UseAVX > 2) {
 782       __ push(rbx);
 783       __ movl(rbx, 0xffff);
 784       __ kmovwl(k1, rbx);
 785       __ pop(rbx);
 786     }
 787     // Copy 64-byte chunks
 788     __ jmpb(L_copy_64_bytes);
 789     __ align(OptoLoopAlignment);
 790   __ BIND(L_copy_64_bytes_loop);
 791 
 792     if (UseUnalignedLoadStores) {
 793       if (UseAVX > 2) {
 794         __ evmovdqul(xmm0, Address(from, 0), Assembler::AVX_512bit);
 795         __ evmovdqul(Address(from, to_from, Address::times_1, 0), xmm0, Assembler::AVX_512bit);
 796       } else if (UseAVX == 2) {
 797         __ vmovdqu(xmm0, Address(from,  0));
 798         __ vmovdqu(Address(from, to_from, Address::times_1,  0), xmm0);
 799         __ vmovdqu(xmm1, Address(from, 32));
 800         __ vmovdqu(Address(from, to_from, Address::times_1, 32), xmm1);
 801       } else {
 802         __ movdqu(xmm0, Address(from, 0));
 803         __ movdqu(Address(from, to_from, Address::times_1, 0), xmm0);
 804         __ movdqu(xmm1, Address(from, 16));
 805         __ movdqu(Address(from, to_from, Address::times_1, 16), xmm1);
 806         __ movdqu(xmm2, Address(from, 32));
 807         __ movdqu(Address(from, to_from, Address::times_1, 32), xmm2);
 808         __ movdqu(xmm3, Address(from, 48));
 809         __ movdqu(Address(from, to_from, Address::times_1, 48), xmm3);
 810       }
 811     } else {
 812       __ movq(xmm0, Address(from, 0));
 813       __ movq(Address(from, to_from, Address::times_1, 0), xmm0);
 814       __ movq(xmm1, Address(from, 8));
 815       __ movq(Address(from, to_from, Address::times_1, 8), xmm1);
 816       __ movq(xmm2, Address(from, 16));
 817       __ movq(Address(from, to_from, Address::times_1, 16), xmm2);
 818       __ movq(xmm3, Address(from, 24));
 819       __ movq(Address(from, to_from, Address::times_1, 24), xmm3);
 820       __ movq(xmm4, Address(from, 32));
 821       __ movq(Address(from, to_from, Address::times_1, 32), xmm4);
 822       __ movq(xmm5, Address(from, 40));
 823       __ movq(Address(from, to_from, Address::times_1, 40), xmm5);
 824       __ movq(xmm6, Address(from, 48));
 825       __ movq(Address(from, to_from, Address::times_1, 48), xmm6);
 826       __ movq(xmm7, Address(from, 56));
 827       __ movq(Address(from, to_from, Address::times_1, 56), xmm7);
 828     }
 829 
 830     __ addl(from, 64);
 831   __ BIND(L_copy_64_bytes);
 832     __ subl(qword_count, 8);
 833     __ jcc(Assembler::greaterEqual, L_copy_64_bytes_loop);
 834 
 835     if (UseUnalignedLoadStores && (UseAVX == 2)) {
 836       // clean upper bits of YMM registers
 837       __ vpxor(xmm0, xmm0);
 838       __ vpxor(xmm1, xmm1);
 839     }
 840     __ addl(qword_count, 8);
 841     __ jccb(Assembler::zero, L_exit);
 842     //
 843     // length is too short, just copy qwords
 844     //
 845   __ BIND(L_copy_8_bytes);
 846     __ movq(xmm0, Address(from, 0));
 847     __ movq(Address(from, to_from, Address::times_1), xmm0);
 848     __ addl(from, 8);
 849     __ decrement(qword_count);
 850     __ jcc(Assembler::greater, L_copy_8_bytes);
 851   __ BIND(L_exit);
 852   }
 853 
 854   // Copy 64 bytes chunks
 855   //
 856   // Inputs:
 857   //   from        - source array address
 858   //   to_from     - destination array address - from
 859   //   qword_count - 8-bytes element count, negative
 860   //
 861   void mmx_copy_forward(Register from, Register to_from, Register qword_count) {
 862     assert( VM_Version::supports_mmx(), "supported cpu only" );
 863     Label L_copy_64_bytes_loop, L_copy_64_bytes, L_copy_8_bytes, L_exit;
 864     // Copy 64-byte chunks
 865     __ jmpb(L_copy_64_bytes);
 866     __ align(OptoLoopAlignment);
 867   __ BIND(L_copy_64_bytes_loop);
 868     __ movq(mmx0, Address(from, 0));
 869     __ movq(mmx1, Address(from, 8));
 870     __ movq(mmx2, Address(from, 16));
 871     __ movq(Address(from, to_from, Address::times_1, 0), mmx0);
 872     __ movq(mmx3, Address(from, 24));
 873     __ movq(Address(from, to_from, Address::times_1, 8), mmx1);
 874     __ movq(mmx4, Address(from, 32));
 875     __ movq(Address(from, to_from, Address::times_1, 16), mmx2);
 876     __ movq(mmx5, Address(from, 40));
 877     __ movq(Address(from, to_from, Address::times_1, 24), mmx3);
 878     __ movq(mmx6, Address(from, 48));
 879     __ movq(Address(from, to_from, Address::times_1, 32), mmx4);
 880     __ movq(mmx7, Address(from, 56));
 881     __ movq(Address(from, to_from, Address::times_1, 40), mmx5);
 882     __ movq(Address(from, to_from, Address::times_1, 48), mmx6);
 883     __ movq(Address(from, to_from, Address::times_1, 56), mmx7);
 884     __ addptr(from, 64);
 885   __ BIND(L_copy_64_bytes);
 886     __ subl(qword_count, 8);
 887     __ jcc(Assembler::greaterEqual, L_copy_64_bytes_loop);
 888     __ addl(qword_count, 8);
 889     __ jccb(Assembler::zero, L_exit);
 890     //
 891     // length is too short, just copy qwords
 892     //
 893   __ BIND(L_copy_8_bytes);
 894     __ movq(mmx0, Address(from, 0));
 895     __ movq(Address(from, to_from, Address::times_1), mmx0);
 896     __ addptr(from, 8);
 897     __ decrement(qword_count);
 898     __ jcc(Assembler::greater, L_copy_8_bytes);
 899   __ BIND(L_exit);
 900     __ emms();
 901   }
 902 
 903   address generate_disjoint_copy(BasicType t, bool aligned,
 904                                  Address::ScaleFactor sf,
 905                                  address* entry, const char *name,
 906                                  bool dest_uninitialized = false) {
 907     __ align(CodeEntryAlignment);
 908     StubCodeMark mark(this, "StubRoutines", name);
 909     address start = __ pc();
 910 
 911     Label L_0_count, L_exit, L_skip_align1, L_skip_align2, L_copy_byte;
 912     Label L_copy_2_bytes, L_copy_4_bytes, L_copy_64_bytes;
 913 
 914     int shift = Address::times_ptr - sf;
 915 
 916     const Register from     = rsi;  // source array address
 917     const Register to       = rdi;  // destination array address
 918     const Register count    = rcx;  // elements count
 919     const Register to_from  = to;   // (to - from)
 920     const Register saved_to = rdx;  // saved destination array address
 921 
 922     __ enter(); // required for proper stackwalking of RuntimeStub frame
 923     __ push(rsi);
 924     __ push(rdi);
 925     __ movptr(from , Address(rsp, 12+ 4));
 926     __ movptr(to   , Address(rsp, 12+ 8));
 927     __ movl(count, Address(rsp, 12+ 12));
 928 
 929     if (entry != NULL) {
 930       *entry = __ pc(); // Entry point from conjoint arraycopy stub.
 931       BLOCK_COMMENT("Entry:");
 932     }
 933 
 934     if (t == T_OBJECT) {
 935       __ testl(count, count);
 936       __ jcc(Assembler::zero, L_0_count);
 937       gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
 938       __ mov(saved_to, to);          // save 'to'
 939     }
 940 
 941     __ subptr(to, from); // to --> to_from
 942     __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
 943     __ jcc(Assembler::below, L_copy_4_bytes); // use unsigned cmp
 944     if (!UseUnalignedLoadStores && !aligned && (t == T_BYTE || t == T_SHORT)) {
 945       // align source address at 4 bytes address boundary
 946       if (t == T_BYTE) {
 947         // One byte misalignment happens only for byte arrays
 948         __ testl(from, 1);
 949         __ jccb(Assembler::zero, L_skip_align1);
 950         __ movb(rax, Address(from, 0));
 951         __ movb(Address(from, to_from, Address::times_1, 0), rax);
 952         __ increment(from);
 953         __ decrement(count);
 954       __ BIND(L_skip_align1);
 955       }
 956       // Two bytes misalignment happens only for byte and short (char) arrays
 957       __ testl(from, 2);
 958       __ jccb(Assembler::zero, L_skip_align2);
 959       __ movw(rax, Address(from, 0));
 960       __ movw(Address(from, to_from, Address::times_1, 0), rax);
 961       __ addptr(from, 2);
 962       __ subl(count, 1<<(shift-1));
 963     __ BIND(L_skip_align2);
 964     }
 965     if (!VM_Version::supports_mmx()) {
 966       __ mov(rax, count);      // save 'count'
 967       __ shrl(count, shift); // bytes count
 968       __ addptr(to_from, from);// restore 'to'
 969       __ rep_mov();
 970       __ subptr(to_from, from);// restore 'to_from'
 971       __ mov(count, rax);      // restore 'count'
 972       __ jmpb(L_copy_2_bytes); // all dwords were copied
 973     } else {
 974       if (!UseUnalignedLoadStores) {
 975         // align to 8 bytes, we know we are 4 byte aligned to start
 976         __ testptr(from, 4);
 977         __ jccb(Assembler::zero, L_copy_64_bytes);
 978         __ movl(rax, Address(from, 0));
 979         __ movl(Address(from, to_from, Address::times_1, 0), rax);
 980         __ addptr(from, 4);
 981         __ subl(count, 1<<shift);
 982       }
 983     __ BIND(L_copy_64_bytes);
 984       __ mov(rax, count);
 985       __ shrl(rax, shift+1);  // 8 bytes chunk count
 986       //
 987       // Copy 8-byte chunks through MMX registers, 8 per iteration of the loop
 988       //
 989       if (UseXMMForArrayCopy) {
 990         xmm_copy_forward(from, to_from, rax);
 991       } else {
 992         mmx_copy_forward(from, to_from, rax);
 993       }
 994     }
 995     // copy tailing dword
 996   __ BIND(L_copy_4_bytes);
 997     __ testl(count, 1<<shift);
 998     __ jccb(Assembler::zero, L_copy_2_bytes);
 999     __ movl(rax, Address(from, 0));
1000     __ movl(Address(from, to_from, Address::times_1, 0), rax);
1001     if (t == T_BYTE || t == T_SHORT) {
1002       __ addptr(from, 4);
1003     __ BIND(L_copy_2_bytes);
1004       // copy tailing word
1005       __ testl(count, 1<<(shift-1));
1006       __ jccb(Assembler::zero, L_copy_byte);
1007       __ movw(rax, Address(from, 0));
1008       __ movw(Address(from, to_from, Address::times_1, 0), rax);
1009       if (t == T_BYTE) {
1010         __ addptr(from, 2);
1011       __ BIND(L_copy_byte);
1012         // copy tailing byte
1013         __ testl(count, 1);
1014         __ jccb(Assembler::zero, L_exit);
1015         __ movb(rax, Address(from, 0));
1016         __ movb(Address(from, to_from, Address::times_1, 0), rax);
1017       __ BIND(L_exit);
1018       } else {
1019       __ BIND(L_copy_byte);
1020       }
1021     } else {
1022     __ BIND(L_copy_2_bytes);
1023     }
1024 
1025     if (t == T_OBJECT) {
1026       __ movl(count, Address(rsp, 12+12)); // reread 'count'
1027       __ mov(to, saved_to); // restore 'to'
1028       gen_write_ref_array_post_barrier(to, count);
1029     __ BIND(L_0_count);
1030     }
1031     inc_copy_counter_np(t);
1032     __ pop(rdi);
1033     __ pop(rsi);
1034     __ leave(); // required for proper stackwalking of RuntimeStub frame
1035     __ vzeroupper();
1036     __ xorptr(rax, rax); // return 0
1037     __ ret(0);
1038     return start;
1039   }
1040 
1041 
1042   address generate_fill(BasicType t, bool aligned, const char *name) {
1043     __ align(CodeEntryAlignment);
1044     StubCodeMark mark(this, "StubRoutines", name);
1045     address start = __ pc();
1046 
1047     BLOCK_COMMENT("Entry:");
1048 
1049     const Register to       = rdi;  // source array address
1050     const Register value    = rdx;  // value
1051     const Register count    = rsi;  // elements count
1052 
1053     __ enter(); // required for proper stackwalking of RuntimeStub frame
1054     __ push(rsi);
1055     __ push(rdi);
1056     __ movptr(to   , Address(rsp, 12+ 4));
1057     __ movl(value, Address(rsp, 12+ 8));
1058     __ movl(count, Address(rsp, 12+ 12));
1059 
1060     __ generate_fill(t, aligned, to, value, count, rax, xmm0);
1061 
1062     __ pop(rdi);
1063     __ pop(rsi);
1064     __ leave(); // required for proper stackwalking of RuntimeStub frame
1065     __ ret(0);
1066     return start;
1067   }
1068 
1069   address generate_conjoint_copy(BasicType t, bool aligned,
1070                                  Address::ScaleFactor sf,
1071                                  address nooverlap_target,
1072                                  address* entry, const char *name,
1073                                  bool dest_uninitialized = false) {
1074     __ align(CodeEntryAlignment);
1075     StubCodeMark mark(this, "StubRoutines", name);
1076     address start = __ pc();
1077 
1078     Label L_0_count, L_exit, L_skip_align1, L_skip_align2, L_copy_byte;
1079     Label L_copy_2_bytes, L_copy_4_bytes, L_copy_8_bytes, L_copy_8_bytes_loop;
1080 
1081     int shift = Address::times_ptr - sf;
1082 
1083     const Register src   = rax;  // source array address
1084     const Register dst   = rdx;  // destination array address
1085     const Register from  = rsi;  // source array address
1086     const Register to    = rdi;  // destination array address
1087     const Register count = rcx;  // elements count
1088     const Register end   = rax;  // array end address
1089 
1090     __ enter(); // required for proper stackwalking of RuntimeStub frame
1091     __ push(rsi);
1092     __ push(rdi);
1093     __ movptr(src  , Address(rsp, 12+ 4));   // from
1094     __ movptr(dst  , Address(rsp, 12+ 8));   // to
1095     __ movl2ptr(count, Address(rsp, 12+12)); // count
1096 
1097     if (entry != NULL) {
1098       *entry = __ pc(); // Entry point from generic arraycopy stub.
1099       BLOCK_COMMENT("Entry:");
1100     }
1101 
1102     // nooverlap_target expects arguments in rsi and rdi.
1103     __ mov(from, src);
1104     __ mov(to  , dst);
1105 
1106     // arrays overlap test: dispatch to disjoint stub if necessary.
1107     RuntimeAddress nooverlap(nooverlap_target);
1108     __ cmpptr(dst, src);
1109     __ lea(end, Address(src, count, sf, 0)); // src + count * elem_size
1110     __ jump_cc(Assembler::belowEqual, nooverlap);
1111     __ cmpptr(dst, end);
1112     __ jump_cc(Assembler::aboveEqual, nooverlap);
1113 
1114     if (t == T_OBJECT) {
1115       __ testl(count, count);
1116       __ jcc(Assembler::zero, L_0_count);
1117       gen_write_ref_array_pre_barrier(dst, count, dest_uninitialized);
1118     }
1119 
1120     // copy from high to low
1121     __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
1122     __ jcc(Assembler::below, L_copy_4_bytes); // use unsigned cmp
1123     if (t == T_BYTE || t == T_SHORT) {
1124       // Align the end of destination array at 4 bytes address boundary
1125       __ lea(end, Address(dst, count, sf, 0));
1126       if (t == T_BYTE) {
1127         // One byte misalignment happens only for byte arrays
1128         __ testl(end, 1);
1129         __ jccb(Assembler::zero, L_skip_align1);
1130         __ decrement(count);
1131         __ movb(rdx, Address(from, count, sf, 0));
1132         __ movb(Address(to, count, sf, 0), rdx);
1133       __ BIND(L_skip_align1);
1134       }
1135       // Two bytes misalignment happens only for byte and short (char) arrays
1136       __ testl(end, 2);
1137       __ jccb(Assembler::zero, L_skip_align2);
1138       __ subptr(count, 1<<(shift-1));
1139       __ movw(rdx, Address(from, count, sf, 0));
1140       __ movw(Address(to, count, sf, 0), rdx);
1141     __ BIND(L_skip_align2);
1142       __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
1143       __ jcc(Assembler::below, L_copy_4_bytes);
1144     }
1145 
1146     if (!VM_Version::supports_mmx()) {
1147       __ std();
1148       __ mov(rax, count); // Save 'count'
1149       __ mov(rdx, to);    // Save 'to'
1150       __ lea(rsi, Address(from, count, sf, -4));
1151       __ lea(rdi, Address(to  , count, sf, -4));
1152       __ shrptr(count, shift); // bytes count
1153       __ rep_mov();
1154       __ cld();
1155       __ mov(count, rax); // restore 'count'
1156       __ andl(count, (1<<shift)-1);      // mask the number of rest elements
1157       __ movptr(from, Address(rsp, 12+4)); // reread 'from'
1158       __ mov(to, rdx);   // restore 'to'
1159       __ jmpb(L_copy_2_bytes); // all dword were copied
1160    } else {
1161       // Align to 8 bytes the end of array. It is aligned to 4 bytes already.
1162       __ testptr(end, 4);
1163       __ jccb(Assembler::zero, L_copy_8_bytes);
1164       __ subl(count, 1<<shift);
1165       __ movl(rdx, Address(from, count, sf, 0));
1166       __ movl(Address(to, count, sf, 0), rdx);
1167       __ jmpb(L_copy_8_bytes);
1168 
1169       __ align(OptoLoopAlignment);
1170       // Move 8 bytes
1171     __ BIND(L_copy_8_bytes_loop);
1172       if (UseXMMForArrayCopy) {
1173         __ movq(xmm0, Address(from, count, sf, 0));
1174         __ movq(Address(to, count, sf, 0), xmm0);
1175       } else {
1176         __ movq(mmx0, Address(from, count, sf, 0));
1177         __ movq(Address(to, count, sf, 0), mmx0);
1178       }
1179     __ BIND(L_copy_8_bytes);
1180       __ subl(count, 2<<shift);
1181       __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
1182       __ addl(count, 2<<shift);
1183       if (!UseXMMForArrayCopy) {
1184         __ emms();
1185       }
1186     }
1187   __ BIND(L_copy_4_bytes);
1188     // copy prefix qword
1189     __ testl(count, 1<<shift);
1190     __ jccb(Assembler::zero, L_copy_2_bytes);
1191     __ movl(rdx, Address(from, count, sf, -4));
1192     __ movl(Address(to, count, sf, -4), rdx);
1193 
1194     if (t == T_BYTE || t == T_SHORT) {
1195         __ subl(count, (1<<shift));
1196       __ BIND(L_copy_2_bytes);
1197         // copy prefix dword
1198         __ testl(count, 1<<(shift-1));
1199         __ jccb(Assembler::zero, L_copy_byte);
1200         __ movw(rdx, Address(from, count, sf, -2));
1201         __ movw(Address(to, count, sf, -2), rdx);
1202         if (t == T_BYTE) {
1203           __ subl(count, 1<<(shift-1));
1204         __ BIND(L_copy_byte);
1205           // copy prefix byte
1206           __ testl(count, 1);
1207           __ jccb(Assembler::zero, L_exit);
1208           __ movb(rdx, Address(from, 0));
1209           __ movb(Address(to, 0), rdx);
1210         __ BIND(L_exit);
1211         } else {
1212         __ BIND(L_copy_byte);
1213         }
1214     } else {
1215     __ BIND(L_copy_2_bytes);
1216     }
1217     if (t == T_OBJECT) {
1218       __ movl2ptr(count, Address(rsp, 12+12)); // reread count
1219       gen_write_ref_array_post_barrier(to, count);
1220     __ BIND(L_0_count);
1221     }
1222     inc_copy_counter_np(t);
1223     __ pop(rdi);
1224     __ pop(rsi);
1225     __ leave(); // required for proper stackwalking of RuntimeStub frame
1226     __ xorptr(rax, rax); // return 0
1227     __ ret(0);
1228     return start;
1229   }
1230 
1231 
1232   address generate_disjoint_long_copy(address* entry, const char *name) {
1233     __ align(CodeEntryAlignment);
1234     StubCodeMark mark(this, "StubRoutines", name);
1235     address start = __ pc();
1236 
1237     Label L_copy_8_bytes, L_copy_8_bytes_loop;
1238     const Register from       = rax;  // source array address
1239     const Register to         = rdx;  // destination array address
1240     const Register count      = rcx;  // elements count
1241     const Register to_from    = rdx;  // (to - from)
1242 
1243     __ enter(); // required for proper stackwalking of RuntimeStub frame
1244     __ movptr(from , Address(rsp, 8+0));       // from
1245     __ movptr(to   , Address(rsp, 8+4));       // to
1246     __ movl2ptr(count, Address(rsp, 8+8));     // count
1247 
1248     *entry = __ pc(); // Entry point from conjoint arraycopy stub.
1249     BLOCK_COMMENT("Entry:");
1250 
1251     __ subptr(to, from); // to --> to_from
1252     if (VM_Version::supports_mmx()) {
1253       if (UseXMMForArrayCopy) {
1254         xmm_copy_forward(from, to_from, count);
1255       } else {
1256         mmx_copy_forward(from, to_from, count);
1257       }
1258     } else {
1259       __ jmpb(L_copy_8_bytes);
1260       __ align(OptoLoopAlignment);
1261     __ BIND(L_copy_8_bytes_loop);
1262       __ fild_d(Address(from, 0));
1263       __ fistp_d(Address(from, to_from, Address::times_1));
1264       __ addptr(from, 8);
1265     __ BIND(L_copy_8_bytes);
1266       __ decrement(count);
1267       __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
1268     }
1269     inc_copy_counter_np(T_LONG);
1270     __ leave(); // required for proper stackwalking of RuntimeStub frame
1271     __ vzeroupper();
1272     __ xorptr(rax, rax); // return 0
1273     __ ret(0);
1274     return start;
1275   }
1276 
1277   address generate_conjoint_long_copy(address nooverlap_target,
1278                                       address* entry, const char *name) {
1279     __ align(CodeEntryAlignment);
1280     StubCodeMark mark(this, "StubRoutines", name);
1281     address start = __ pc();
1282 
1283     Label L_copy_8_bytes, L_copy_8_bytes_loop;
1284     const Register from       = rax;  // source array address
1285     const Register to         = rdx;  // destination array address
1286     const Register count      = rcx;  // elements count
1287     const Register end_from   = rax;  // source array end address
1288 
1289     __ enter(); // required for proper stackwalking of RuntimeStub frame
1290     __ movptr(from , Address(rsp, 8+0));       // from
1291     __ movptr(to   , Address(rsp, 8+4));       // to
1292     __ movl2ptr(count, Address(rsp, 8+8));     // count
1293 
1294     *entry = __ pc(); // Entry point from generic arraycopy stub.
1295     BLOCK_COMMENT("Entry:");
1296 
1297     // arrays overlap test
1298     __ cmpptr(to, from);
1299     RuntimeAddress nooverlap(nooverlap_target);
1300     __ jump_cc(Assembler::belowEqual, nooverlap);
1301     __ lea(end_from, Address(from, count, Address::times_8, 0));
1302     __ cmpptr(to, end_from);
1303     __ movptr(from, Address(rsp, 8));  // from
1304     __ jump_cc(Assembler::aboveEqual, nooverlap);
1305 
1306     __ jmpb(L_copy_8_bytes);
1307 
1308     __ align(OptoLoopAlignment);
1309   __ BIND(L_copy_8_bytes_loop);
1310     if (VM_Version::supports_mmx()) {
1311       if (UseXMMForArrayCopy) {
1312         __ movq(xmm0, Address(from, count, Address::times_8));
1313         __ movq(Address(to, count, Address::times_8), xmm0);
1314       } else {
1315         __ movq(mmx0, Address(from, count, Address::times_8));
1316         __ movq(Address(to, count, Address::times_8), mmx0);
1317       }
1318     } else {
1319       __ fild_d(Address(from, count, Address::times_8));
1320       __ fistp_d(Address(to, count, Address::times_8));
1321     }
1322   __ BIND(L_copy_8_bytes);
1323     __ decrement(count);
1324     __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
1325 
1326     if (VM_Version::supports_mmx() && !UseXMMForArrayCopy) {
1327       __ emms();
1328     }
1329     inc_copy_counter_np(T_LONG);
1330     __ leave(); // required for proper stackwalking of RuntimeStub frame
1331     __ xorptr(rax, rax); // return 0
1332     __ ret(0);
1333     return start;
1334   }
1335 
1336 
1337   // Helper for generating a dynamic type check.
1338   // The sub_klass must be one of {rbx, rdx, rsi}.
1339   // The temp is killed.
1340   void generate_type_check(Register sub_klass,
1341                            Address& super_check_offset_addr,
1342                            Address& super_klass_addr,
1343                            Register temp,
1344                            Label* L_success, Label* L_failure) {
1345     BLOCK_COMMENT("type_check:");
1346 
1347     Label L_fallthrough;
1348 #define LOCAL_JCC(assembler_con, label_ptr)                             \
1349     if (label_ptr != NULL)  __ jcc(assembler_con, *(label_ptr));        \
1350     else                    __ jcc(assembler_con, L_fallthrough) /*omit semi*/
1351 
1352     // The following is a strange variation of the fast path which requires
1353     // one less register, because needed values are on the argument stack.
1354     // __ check_klass_subtype_fast_path(sub_klass, *super_klass*, temp,
1355     //                                  L_success, L_failure, NULL);
1356     assert_different_registers(sub_klass, temp);
1357 
1358     int sc_offset = in_bytes(Klass::secondary_super_cache_offset());
1359 
1360     // if the pointers are equal, we are done (e.g., String[] elements)
1361     __ cmpptr(sub_klass, super_klass_addr);
1362     LOCAL_JCC(Assembler::equal, L_success);
1363 
1364     // check the supertype display:
1365     __ movl2ptr(temp, super_check_offset_addr);
1366     Address super_check_addr(sub_klass, temp, Address::times_1, 0);
1367     __ movptr(temp, super_check_addr); // load displayed supertype
1368     __ cmpptr(temp, super_klass_addr); // test the super type
1369     LOCAL_JCC(Assembler::equal, L_success);
1370 
1371     // if it was a primary super, we can just fail immediately
1372     __ cmpl(super_check_offset_addr, sc_offset);
1373     LOCAL_JCC(Assembler::notEqual, L_failure);
1374 
1375     // The repne_scan instruction uses fixed registers, which will get spilled.
1376     // We happen to know this works best when super_klass is in rax.
1377     Register super_klass = temp;
1378     __ movptr(super_klass, super_klass_addr);
1379     __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg,
1380                                      L_success, L_failure);
1381 
1382     __ bind(L_fallthrough);
1383 
1384     if (L_success == NULL) { BLOCK_COMMENT("L_success:"); }
1385     if (L_failure == NULL) { BLOCK_COMMENT("L_failure:"); }
1386 
1387 #undef LOCAL_JCC
1388   }
1389 
1390   //
1391   //  Generate checkcasting array copy stub
1392   //
1393   //  Input:
1394   //    4(rsp)   - source array address
1395   //    8(rsp)   - destination array address
1396   //   12(rsp)   - element count, can be zero
1397   //   16(rsp)   - size_t ckoff (super_check_offset)
1398   //   20(rsp)   - oop ckval (super_klass)
1399   //
1400   //  Output:
1401   //    rax, ==  0  -  success
1402   //    rax, == -1^K - failure, where K is partial transfer count
1403   //
1404   address generate_checkcast_copy(const char *name, address* entry, bool dest_uninitialized = false) {
1405     __ align(CodeEntryAlignment);
1406     StubCodeMark mark(this, "StubRoutines", name);
1407     address start = __ pc();
1408 
1409     Label L_load_element, L_store_element, L_do_card_marks, L_done;
1410 
1411     // register use:
1412     //  rax, rdx, rcx -- loop control (end_from, end_to, count)
1413     //  rdi, rsi      -- element access (oop, klass)
1414     //  rbx,           -- temp
1415     const Register from       = rax;    // source array address
1416     const Register to         = rdx;    // destination array address
1417     const Register length     = rcx;    // elements count
1418     const Register elem       = rdi;    // each oop copied
1419     const Register elem_klass = rsi;    // each elem._klass (sub_klass)
1420     const Register temp       = rbx;    // lone remaining temp
1421 
1422     __ enter(); // required for proper stackwalking of RuntimeStub frame
1423 
1424     __ push(rsi);
1425     __ push(rdi);
1426     __ push(rbx);
1427 
1428     Address   from_arg(rsp, 16+ 4);     // from
1429     Address     to_arg(rsp, 16+ 8);     // to
1430     Address length_arg(rsp, 16+12);     // elements count
1431     Address  ckoff_arg(rsp, 16+16);     // super_check_offset
1432     Address  ckval_arg(rsp, 16+20);     // super_klass
1433 
1434     // Load up:
1435     __ movptr(from,     from_arg);
1436     __ movptr(to,         to_arg);
1437     __ movl2ptr(length, length_arg);
1438 
1439     if (entry != NULL) {
1440       *entry = __ pc(); // Entry point from generic arraycopy stub.
1441       BLOCK_COMMENT("Entry:");
1442     }
1443 
1444     //---------------------------------------------------------------
1445     // Assembler stub will be used for this call to arraycopy
1446     // if the two arrays are subtypes of Object[] but the
1447     // destination array type is not equal to or a supertype
1448     // of the source type.  Each element must be separately
1449     // checked.
1450 
1451     // Loop-invariant addresses.  They are exclusive end pointers.
1452     Address end_from_addr(from, length, Address::times_ptr, 0);
1453     Address   end_to_addr(to,   length, Address::times_ptr, 0);
1454 
1455     Register end_from = from;           // re-use
1456     Register end_to   = to;             // re-use
1457     Register count    = length;         // re-use
1458 
1459     // Loop-variant addresses.  They assume post-incremented count < 0.
1460     Address from_element_addr(end_from, count, Address::times_ptr, 0);
1461     Address   to_element_addr(end_to,   count, Address::times_ptr, 0);
1462     Address elem_klass_addr(elem, oopDesc::klass_offset_in_bytes());
1463 
1464     // Copy from low to high addresses, indexed from the end of each array.
1465     gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
1466     __ lea(end_from, end_from_addr);
1467     __ lea(end_to,   end_to_addr);
1468     assert(length == count, "");        // else fix next line:
1469     __ negptr(count);                   // negate and test the length
1470     __ jccb(Assembler::notZero, L_load_element);
1471 
1472     // Empty array:  Nothing to do.
1473     __ xorptr(rax, rax);                  // return 0 on (trivial) success
1474     __ jmp(L_done);
1475 
1476     // ======== begin loop ========
1477     // (Loop is rotated; its entry is L_load_element.)
1478     // Loop control:
1479     //   for (count = -count; count != 0; count++)
1480     // Base pointers src, dst are biased by 8*count,to last element.
1481     __ align(OptoLoopAlignment);
1482 
1483     __ BIND(L_store_element);
1484     __ movptr(to_element_addr, elem);     // store the oop
1485     __ increment(count);                // increment the count toward zero
1486     __ jccb(Assembler::zero, L_do_card_marks);
1487 
1488     // ======== loop entry is here ========
1489     __ BIND(L_load_element);
1490     __ movptr(elem, from_element_addr);   // load the oop
1491     __ testptr(elem, elem);
1492     __ jccb(Assembler::zero, L_store_element);
1493 
1494     // (Could do a trick here:  Remember last successful non-null
1495     // element stored and make a quick oop equality check on it.)
1496 
1497     __ movptr(elem_klass, elem_klass_addr); // query the object klass
1498     generate_type_check(elem_klass, ckoff_arg, ckval_arg, temp,
1499                         &L_store_element, NULL);
1500     // (On fall-through, we have failed the element type check.)
1501     // ======== end loop ========
1502 
1503     // It was a real error; we must depend on the caller to finish the job.
1504     // Register "count" = -1 * number of *remaining* oops, length_arg = *total* oops.
1505     // Emit GC store barriers for the oops we have copied (length_arg + count),
1506     // and report their number to the caller.
1507     assert_different_registers(to, count, rax);
1508     Label L_post_barrier;
1509     __ addl(count, length_arg);         // transfers = (length - remaining)
1510     __ movl2ptr(rax, count);            // save the value
1511     __ notptr(rax);                     // report (-1^K) to caller (does not affect flags)
1512     __ jccb(Assembler::notZero, L_post_barrier);
1513     __ jmp(L_done); // K == 0, nothing was copied, skip post barrier
1514 
1515     // Come here on success only.
1516     __ BIND(L_do_card_marks);
1517     __ xorptr(rax, rax);                // return 0 on success
1518     __ movl2ptr(count, length_arg);
1519 
1520     __ BIND(L_post_barrier);
1521     __ movptr(to, to_arg);              // reload
1522     gen_write_ref_array_post_barrier(to, count);
1523 
1524     // Common exit point (success or failure).
1525     __ BIND(L_done);
1526     __ pop(rbx);
1527     __ pop(rdi);
1528     __ pop(rsi);
1529     inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr);
1530     __ leave(); // required for proper stackwalking of RuntimeStub frame
1531     __ ret(0);
1532 
1533     return start;
1534   }
1535 
1536   //
1537   //  Generate 'unsafe' array copy stub
1538   //  Though just as safe as the other stubs, it takes an unscaled
1539   //  size_t argument instead of an element count.
1540   //
1541   //  Input:
1542   //    4(rsp)   - source array address
1543   //    8(rsp)   - destination array address
1544   //   12(rsp)   - byte count, can be zero
1545   //
1546   //  Output:
1547   //    rax, ==  0  -  success
1548   //    rax, == -1  -  need to call System.arraycopy
1549   //
1550   // Examines the alignment of the operands and dispatches
1551   // to a long, int, short, or byte copy loop.
1552   //
1553   address generate_unsafe_copy(const char *name,
1554                                address byte_copy_entry,
1555                                address short_copy_entry,
1556                                address int_copy_entry,
1557                                address long_copy_entry) {
1558 
1559     Label L_long_aligned, L_int_aligned, L_short_aligned;
1560 
1561     __ align(CodeEntryAlignment);
1562     StubCodeMark mark(this, "StubRoutines", name);
1563     address start = __ pc();
1564 
1565     const Register from       = rax;  // source array address
1566     const Register to         = rdx;  // destination array address
1567     const Register count      = rcx;  // elements count
1568 
1569     __ enter(); // required for proper stackwalking of RuntimeStub frame
1570     __ push(rsi);
1571     __ push(rdi);
1572     Address  from_arg(rsp, 12+ 4);      // from
1573     Address    to_arg(rsp, 12+ 8);      // to
1574     Address count_arg(rsp, 12+12);      // byte count
1575 
1576     // Load up:
1577     __ movptr(from ,  from_arg);
1578     __ movptr(to   ,    to_arg);
1579     __ movl2ptr(count, count_arg);
1580 
1581     // bump this on entry, not on exit:
1582     inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
1583 
1584     const Register bits = rsi;
1585     __ mov(bits, from);
1586     __ orptr(bits, to);
1587     __ orptr(bits, count);
1588 
1589     __ testl(bits, BytesPerLong-1);
1590     __ jccb(Assembler::zero, L_long_aligned);
1591 
1592     __ testl(bits, BytesPerInt-1);
1593     __ jccb(Assembler::zero, L_int_aligned);
1594 
1595     __ testl(bits, BytesPerShort-1);
1596     __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
1597 
1598     __ BIND(L_short_aligned);
1599     __ shrptr(count, LogBytesPerShort); // size => short_count
1600     __ movl(count_arg, count);          // update 'count'
1601     __ jump(RuntimeAddress(short_copy_entry));
1602 
1603     __ BIND(L_int_aligned);
1604     __ shrptr(count, LogBytesPerInt); // size => int_count
1605     __ movl(count_arg, count);          // update 'count'
1606     __ jump(RuntimeAddress(int_copy_entry));
1607 
1608     __ BIND(L_long_aligned);
1609     __ shrptr(count, LogBytesPerLong); // size => qword_count
1610     __ movl(count_arg, count);          // update 'count'
1611     __ pop(rdi); // Do pops here since jlong_arraycopy stub does not do it.
1612     __ pop(rsi);
1613     __ jump(RuntimeAddress(long_copy_entry));
1614 
1615     return start;
1616   }
1617 
1618 
1619   // Perform range checks on the proposed arraycopy.
1620   // Smashes src_pos and dst_pos.  (Uses them up for temps.)
1621   void arraycopy_range_checks(Register src,
1622                               Register src_pos,
1623                               Register dst,
1624                               Register dst_pos,
1625                               Address& length,
1626                               Label& L_failed) {
1627     BLOCK_COMMENT("arraycopy_range_checks:");
1628     const Register src_end = src_pos;   // source array end position
1629     const Register dst_end = dst_pos;   // destination array end position
1630     __ addl(src_end, length); // src_pos + length
1631     __ addl(dst_end, length); // dst_pos + length
1632 
1633     //  if (src_pos + length > arrayOop(src)->length() ) FAIL;
1634     __ cmpl(src_end, Address(src, arrayOopDesc::length_offset_in_bytes()));
1635     __ jcc(Assembler::above, L_failed);
1636 
1637     //  if (dst_pos + length > arrayOop(dst)->length() ) FAIL;
1638     __ cmpl(dst_end, Address(dst, arrayOopDesc::length_offset_in_bytes()));
1639     __ jcc(Assembler::above, L_failed);
1640 
1641     BLOCK_COMMENT("arraycopy_range_checks done");
1642   }
1643 
1644 
1645   //
1646   //  Generate generic array copy stubs
1647   //
1648   //  Input:
1649   //     4(rsp)    -  src oop
1650   //     8(rsp)    -  src_pos
1651   //    12(rsp)    -  dst oop
1652   //    16(rsp)    -  dst_pos
1653   //    20(rsp)    -  element count
1654   //
1655   //  Output:
1656   //    rax, ==  0  -  success
1657   //    rax, == -1^K - failure, where K is partial transfer count
1658   //
1659   address generate_generic_copy(const char *name,
1660                                 address entry_jbyte_arraycopy,
1661                                 address entry_jshort_arraycopy,
1662                                 address entry_jint_arraycopy,
1663                                 address entry_oop_arraycopy,
1664                                 address entry_jlong_arraycopy,
1665                                 address entry_checkcast_arraycopy) {
1666     Label L_failed, L_failed_0, L_objArray;
1667 
1668     { int modulus = CodeEntryAlignment;
1669       int target  = modulus - 5; // 5 = sizeof jmp(L_failed)
1670       int advance = target - (__ offset() % modulus);
1671       if (advance < 0)  advance += modulus;
1672       if (advance > 0)  __ nop(advance);
1673     }
1674     StubCodeMark mark(this, "StubRoutines", name);
1675 
1676     // Short-hop target to L_failed.  Makes for denser prologue code.
1677     __ BIND(L_failed_0);
1678     __ jmp(L_failed);
1679     assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
1680 
1681     __ align(CodeEntryAlignment);
1682     address start = __ pc();
1683 
1684     __ enter(); // required for proper stackwalking of RuntimeStub frame
1685     __ push(rsi);
1686     __ push(rdi);
1687 
1688     // bump this on entry, not on exit:
1689     inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
1690 
1691     // Input values
1692     Address SRC     (rsp, 12+ 4);
1693     Address SRC_POS (rsp, 12+ 8);
1694     Address DST     (rsp, 12+12);
1695     Address DST_POS (rsp, 12+16);
1696     Address LENGTH  (rsp, 12+20);
1697 
1698     //-----------------------------------------------------------------------
1699     // Assembler stub will be used for this call to arraycopy
1700     // if the following conditions are met:
1701     //
1702     // (1) src and dst must not be null.
1703     // (2) src_pos must not be negative.
1704     // (3) dst_pos must not be negative.
1705     // (4) length  must not be negative.
1706     // (5) src klass and dst klass should be the same and not NULL.
1707     // (6) src and dst should be arrays.
1708     // (7) src_pos + length must not exceed length of src.
1709     // (8) dst_pos + length must not exceed length of dst.
1710     //
1711 
1712     const Register src     = rax;       // source array oop
1713     const Register src_pos = rsi;
1714     const Register dst     = rdx;       // destination array oop
1715     const Register dst_pos = rdi;
1716     const Register length  = rcx;       // transfer count
1717 
1718     //  if (src == NULL) return -1;
1719     __ movptr(src, SRC);      // src oop
1720     __ testptr(src, src);
1721     __ jccb(Assembler::zero, L_failed_0);
1722 
1723     //  if (src_pos < 0) return -1;
1724     __ movl2ptr(src_pos, SRC_POS);  // src_pos
1725     __ testl(src_pos, src_pos);
1726     __ jccb(Assembler::negative, L_failed_0);
1727 
1728     //  if (dst == NULL) return -1;
1729     __ movptr(dst, DST);      // dst oop
1730     __ testptr(dst, dst);
1731     __ jccb(Assembler::zero, L_failed_0);
1732 
1733     //  if (dst_pos < 0) return -1;
1734     __ movl2ptr(dst_pos, DST_POS);  // dst_pos
1735     __ testl(dst_pos, dst_pos);
1736     __ jccb(Assembler::negative, L_failed_0);
1737 
1738     //  if (length < 0) return -1;
1739     __ movl2ptr(length, LENGTH);   // length
1740     __ testl(length, length);
1741     __ jccb(Assembler::negative, L_failed_0);
1742 
1743     //  if (src->klass() == NULL) return -1;
1744     Address src_klass_addr(src, oopDesc::klass_offset_in_bytes());
1745     Address dst_klass_addr(dst, oopDesc::klass_offset_in_bytes());
1746     const Register rcx_src_klass = rcx;    // array klass
1747     __ movptr(rcx_src_klass, Address(src, oopDesc::klass_offset_in_bytes()));
1748 
1749 #ifdef ASSERT
1750     //  assert(src->klass() != NULL);
1751     BLOCK_COMMENT("assert klasses not null");
1752     { Label L1, L2;
1753       __ testptr(rcx_src_klass, rcx_src_klass);
1754       __ jccb(Assembler::notZero, L2);   // it is broken if klass is NULL
1755       __ bind(L1);
1756       __ stop("broken null klass");
1757       __ bind(L2);
1758       __ cmpptr(dst_klass_addr, (int32_t)NULL_WORD);
1759       __ jccb(Assembler::equal, L1);      // this would be broken also
1760       BLOCK_COMMENT("assert done");
1761     }
1762 #endif //ASSERT
1763 
1764     // Load layout helper (32-bits)
1765     //
1766     //  |array_tag|     | header_size | element_type |     |log2_element_size|
1767     // 32        30    24            16              8     2                 0
1768     //
1769     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
1770     //
1771 
1772     int lh_offset = in_bytes(Klass::layout_helper_offset());
1773     Address src_klass_lh_addr(rcx_src_klass, lh_offset);
1774 
1775     // Handle objArrays completely differently...
1776     jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
1777     __ cmpl(src_klass_lh_addr, objArray_lh);
1778     __ jcc(Assembler::equal, L_objArray);
1779 
1780     //  if (src->klass() != dst->klass()) return -1;
1781     __ cmpptr(rcx_src_klass, dst_klass_addr);
1782     __ jccb(Assembler::notEqual, L_failed_0);
1783 
1784     const Register rcx_lh = rcx;  // layout helper
1785     assert(rcx_lh == rcx_src_klass, "known alias");
1786     __ movl(rcx_lh, src_klass_lh_addr);
1787 
1788     //  if (!src->is_Array()) return -1;
1789     __ cmpl(rcx_lh, Klass::_lh_neutral_value);
1790     __ jcc(Assembler::greaterEqual, L_failed_0); // signed cmp
1791 
1792     // At this point, it is known to be a typeArray (array_tag 0x3).
1793 #ifdef ASSERT
1794     { Label L;
1795       __ cmpl(rcx_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
1796       __ jcc(Assembler::greaterEqual, L); // signed cmp
1797       __ stop("must be a primitive array");
1798       __ bind(L);
1799     }
1800 #endif
1801 
1802     assert_different_registers(src, src_pos, dst, dst_pos, rcx_lh);
1803     arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
1804 
1805     // TypeArrayKlass
1806     //
1807     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
1808     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
1809     //
1810     const Register rsi_offset = rsi; // array offset
1811     const Register src_array  = src; // src array offset
1812     const Register dst_array  = dst; // dst array offset
1813     const Register rdi_elsize = rdi; // log2 element size
1814 
1815     __ mov(rsi_offset, rcx_lh);
1816     __ shrptr(rsi_offset, Klass::_lh_header_size_shift);
1817     __ andptr(rsi_offset, Klass::_lh_header_size_mask);   // array_offset
1818     __ addptr(src_array, rsi_offset);  // src array offset
1819     __ addptr(dst_array, rsi_offset);  // dst array offset
1820     __ andptr(rcx_lh, Klass::_lh_log2_element_size_mask); // log2 elsize
1821 
1822     // next registers should be set before the jump to corresponding stub
1823     const Register from       = src; // source array address
1824     const Register to         = dst; // destination array address
1825     const Register count      = rcx; // elements count
1826     // some of them should be duplicated on stack
1827 #define FROM   Address(rsp, 12+ 4)
1828 #define TO     Address(rsp, 12+ 8)   // Not used now
1829 #define COUNT  Address(rsp, 12+12)   // Only for oop arraycopy
1830 
1831     BLOCK_COMMENT("scale indexes to element size");
1832     __ movl2ptr(rsi, SRC_POS);  // src_pos
1833     __ shlptr(rsi);             // src_pos << rcx (log2 elsize)
1834     assert(src_array == from, "");
1835     __ addptr(from, rsi);       // from = src_array + SRC_POS << log2 elsize
1836     __ movl2ptr(rdi, DST_POS);  // dst_pos
1837     __ shlptr(rdi);             // dst_pos << rcx (log2 elsize)
1838     assert(dst_array == to, "");
1839     __ addptr(to,  rdi);        // to   = dst_array + DST_POS << log2 elsize
1840     __ movptr(FROM, from);      // src_addr
1841     __ mov(rdi_elsize, rcx_lh); // log2 elsize
1842     __ movl2ptr(count, LENGTH); // elements count
1843 
1844     BLOCK_COMMENT("choose copy loop based on element size");
1845     __ cmpl(rdi_elsize, 0);
1846 
1847     __ jump_cc(Assembler::equal, RuntimeAddress(entry_jbyte_arraycopy));
1848     __ cmpl(rdi_elsize, LogBytesPerShort);
1849     __ jump_cc(Assembler::equal, RuntimeAddress(entry_jshort_arraycopy));
1850     __ cmpl(rdi_elsize, LogBytesPerInt);
1851     __ jump_cc(Assembler::equal, RuntimeAddress(entry_jint_arraycopy));
1852 #ifdef ASSERT
1853     __ cmpl(rdi_elsize, LogBytesPerLong);
1854     __ jccb(Assembler::notEqual, L_failed);
1855 #endif
1856     __ pop(rdi); // Do pops here since jlong_arraycopy stub does not do it.
1857     __ pop(rsi);
1858     __ jump(RuntimeAddress(entry_jlong_arraycopy));
1859 
1860   __ BIND(L_failed);
1861     __ xorptr(rax, rax);
1862     __ notptr(rax); // return -1
1863     __ pop(rdi);
1864     __ pop(rsi);
1865     __ leave(); // required for proper stackwalking of RuntimeStub frame
1866     __ ret(0);
1867 
1868     // ObjArrayKlass
1869   __ BIND(L_objArray);
1870     // live at this point:  rcx_src_klass, src[_pos], dst[_pos]
1871 
1872     Label L_plain_copy, L_checkcast_copy;
1873     //  test array classes for subtyping
1874     __ cmpptr(rcx_src_klass, dst_klass_addr); // usual case is exact equality
1875     __ jccb(Assembler::notEqual, L_checkcast_copy);
1876 
1877     // Identically typed arrays can be copied without element-wise checks.
1878     assert_different_registers(src, src_pos, dst, dst_pos, rcx_src_klass);
1879     arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
1880 
1881   __ BIND(L_plain_copy);
1882     __ movl2ptr(count, LENGTH); // elements count
1883     __ movl2ptr(src_pos, SRC_POS);  // reload src_pos
1884     __ lea(from, Address(src, src_pos, Address::times_ptr,
1885                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
1886     __ movl2ptr(dst_pos, DST_POS);  // reload dst_pos
1887     __ lea(to,   Address(dst, dst_pos, Address::times_ptr,
1888                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
1889     __ movptr(FROM,  from);   // src_addr
1890     __ movptr(TO,    to);     // dst_addr
1891     __ movl(COUNT, count);  // count
1892     __ jump(RuntimeAddress(entry_oop_arraycopy));
1893 
1894   __ BIND(L_checkcast_copy);
1895     // live at this point:  rcx_src_klass, dst[_pos], src[_pos]
1896     {
1897       // Handy offsets:
1898       int  ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
1899       int sco_offset = in_bytes(Klass::super_check_offset_offset());
1900 
1901       Register rsi_dst_klass = rsi;
1902       Register rdi_temp      = rdi;
1903       assert(rsi_dst_klass == src_pos, "expected alias w/ src_pos");
1904       assert(rdi_temp      == dst_pos, "expected alias w/ dst_pos");
1905       Address dst_klass_lh_addr(rsi_dst_klass, lh_offset);
1906 
1907       // Before looking at dst.length, make sure dst is also an objArray.
1908       __ movptr(rsi_dst_klass, dst_klass_addr);
1909       __ cmpl(dst_klass_lh_addr, objArray_lh);
1910       __ jccb(Assembler::notEqual, L_failed);
1911 
1912       // It is safe to examine both src.length and dst.length.
1913       __ movl2ptr(src_pos, SRC_POS);        // reload rsi
1914       arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
1915       // (Now src_pos and dst_pos are killed, but not src and dst.)
1916 
1917       // We'll need this temp (don't forget to pop it after the type check).
1918       __ push(rbx);
1919       Register rbx_src_klass = rbx;
1920 
1921       __ mov(rbx_src_klass, rcx_src_klass); // spill away from rcx
1922       __ movptr(rsi_dst_klass, dst_klass_addr);
1923       Address super_check_offset_addr(rsi_dst_klass, sco_offset);
1924       Label L_fail_array_check;
1925       generate_type_check(rbx_src_klass,
1926                           super_check_offset_addr, dst_klass_addr,
1927                           rdi_temp, NULL, &L_fail_array_check);
1928       // (On fall-through, we have passed the array type check.)
1929       __ pop(rbx);
1930       __ jmp(L_plain_copy);
1931 
1932       __ BIND(L_fail_array_check);
1933       // Reshuffle arguments so we can call checkcast_arraycopy:
1934 
1935       // match initial saves for checkcast_arraycopy
1936       // push(rsi);    // already done; see above
1937       // push(rdi);    // already done; see above
1938       // push(rbx);    // already done; see above
1939 
1940       // Marshal outgoing arguments now, freeing registers.
1941       Address   from_arg(rsp, 16+ 4);   // from
1942       Address     to_arg(rsp, 16+ 8);   // to
1943       Address length_arg(rsp, 16+12);   // elements count
1944       Address  ckoff_arg(rsp, 16+16);   // super_check_offset
1945       Address  ckval_arg(rsp, 16+20);   // super_klass
1946 
1947       Address SRC_POS_arg(rsp, 16+ 8);
1948       Address DST_POS_arg(rsp, 16+16);
1949       Address  LENGTH_arg(rsp, 16+20);
1950       // push rbx, changed the incoming offsets (why not just use rbp,??)
1951       // assert(SRC_POS_arg.disp() == SRC_POS.disp() + 4, "");
1952 
1953       __ movptr(rbx, Address(rsi_dst_klass, ek_offset));
1954       __ movl2ptr(length, LENGTH_arg);    // reload elements count
1955       __ movl2ptr(src_pos, SRC_POS_arg);  // reload src_pos
1956       __ movl2ptr(dst_pos, DST_POS_arg);  // reload dst_pos
1957 
1958       __ movptr(ckval_arg, rbx);          // destination element type
1959       __ movl(rbx, Address(rbx, sco_offset));
1960       __ movl(ckoff_arg, rbx);          // corresponding class check offset
1961 
1962       __ movl(length_arg, length);      // outgoing length argument
1963 
1964       __ lea(from, Address(src, src_pos, Address::times_ptr,
1965                             arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
1966       __ movptr(from_arg, from);
1967 
1968       __ lea(to, Address(dst, dst_pos, Address::times_ptr,
1969                           arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
1970       __ movptr(to_arg, to);
1971       __ jump(RuntimeAddress(entry_checkcast_arraycopy));
1972     }
1973 
1974     return start;
1975   }
1976 
1977   void generate_arraycopy_stubs() {
1978     address entry;
1979     address entry_jbyte_arraycopy;
1980     address entry_jshort_arraycopy;
1981     address entry_jint_arraycopy;
1982     address entry_oop_arraycopy;
1983     address entry_jlong_arraycopy;
1984     address entry_checkcast_arraycopy;
1985 
1986     StubRoutines::_arrayof_jbyte_disjoint_arraycopy =
1987         generate_disjoint_copy(T_BYTE,  true, Address::times_1, &entry,
1988                                "arrayof_jbyte_disjoint_arraycopy");
1989     StubRoutines::_arrayof_jbyte_arraycopy =
1990         generate_conjoint_copy(T_BYTE,  true, Address::times_1,  entry,
1991                                NULL, "arrayof_jbyte_arraycopy");
1992     StubRoutines::_jbyte_disjoint_arraycopy =
1993         generate_disjoint_copy(T_BYTE, false, Address::times_1, &entry,
1994                                "jbyte_disjoint_arraycopy");
1995     StubRoutines::_jbyte_arraycopy =
1996         generate_conjoint_copy(T_BYTE, false, Address::times_1,  entry,
1997                                &entry_jbyte_arraycopy, "jbyte_arraycopy");
1998 
1999     StubRoutines::_arrayof_jshort_disjoint_arraycopy =
2000         generate_disjoint_copy(T_SHORT,  true, Address::times_2, &entry,
2001                                "arrayof_jshort_disjoint_arraycopy");
2002     StubRoutines::_arrayof_jshort_arraycopy =
2003         generate_conjoint_copy(T_SHORT,  true, Address::times_2,  entry,
2004                                NULL, "arrayof_jshort_arraycopy");
2005     StubRoutines::_jshort_disjoint_arraycopy =
2006         generate_disjoint_copy(T_SHORT, false, Address::times_2, &entry,
2007                                "jshort_disjoint_arraycopy");
2008     StubRoutines::_jshort_arraycopy =
2009         generate_conjoint_copy(T_SHORT, false, Address::times_2,  entry,
2010                                &entry_jshort_arraycopy, "jshort_arraycopy");
2011 
2012     // Next arrays are always aligned on 4 bytes at least.
2013     StubRoutines::_jint_disjoint_arraycopy =
2014         generate_disjoint_copy(T_INT, true, Address::times_4, &entry,
2015                                "jint_disjoint_arraycopy");
2016     StubRoutines::_jint_arraycopy =
2017         generate_conjoint_copy(T_INT, true, Address::times_4,  entry,
2018                                &entry_jint_arraycopy, "jint_arraycopy");
2019 
2020     StubRoutines::_oop_disjoint_arraycopy =
2021         generate_disjoint_copy(T_OBJECT, true, Address::times_ptr, &entry,
2022                                "oop_disjoint_arraycopy");
2023     StubRoutines::_oop_arraycopy =
2024         generate_conjoint_copy(T_OBJECT, true, Address::times_ptr,  entry,
2025                                &entry_oop_arraycopy, "oop_arraycopy");
2026 
2027     StubRoutines::_oop_disjoint_arraycopy_uninit =
2028         generate_disjoint_copy(T_OBJECT, true, Address::times_ptr, &entry,
2029                                "oop_disjoint_arraycopy_uninit",
2030                                /*dest_uninitialized*/true);
2031     StubRoutines::_oop_arraycopy_uninit =
2032         generate_conjoint_copy(T_OBJECT, true, Address::times_ptr,  entry,
2033                                NULL, "oop_arraycopy_uninit",
2034                                /*dest_uninitialized*/true);
2035 
2036     StubRoutines::_jlong_disjoint_arraycopy =
2037         generate_disjoint_long_copy(&entry, "jlong_disjoint_arraycopy");
2038     StubRoutines::_jlong_arraycopy =
2039         generate_conjoint_long_copy(entry, &entry_jlong_arraycopy,
2040                                     "jlong_arraycopy");
2041 
2042     StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
2043     StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
2044     StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
2045     StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
2046     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
2047     StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
2048 
2049     StubRoutines::_arrayof_jint_disjoint_arraycopy       = StubRoutines::_jint_disjoint_arraycopy;
2050     StubRoutines::_arrayof_oop_disjoint_arraycopy        = StubRoutines::_oop_disjoint_arraycopy;
2051     StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit = StubRoutines::_oop_disjoint_arraycopy_uninit;
2052     StubRoutines::_arrayof_jlong_disjoint_arraycopy      = StubRoutines::_jlong_disjoint_arraycopy;
2053 
2054     StubRoutines::_arrayof_jint_arraycopy       = StubRoutines::_jint_arraycopy;
2055     StubRoutines::_arrayof_oop_arraycopy        = StubRoutines::_oop_arraycopy;
2056     StubRoutines::_arrayof_oop_arraycopy_uninit = StubRoutines::_oop_arraycopy_uninit;
2057     StubRoutines::_arrayof_jlong_arraycopy      = StubRoutines::_jlong_arraycopy;
2058 
2059     StubRoutines::_checkcast_arraycopy =
2060         generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy);
2061     StubRoutines::_checkcast_arraycopy_uninit =
2062         generate_checkcast_copy("checkcast_arraycopy_uninit", NULL, /*dest_uninitialized*/true);
2063 
2064     StubRoutines::_unsafe_arraycopy =
2065         generate_unsafe_copy("unsafe_arraycopy",
2066                                entry_jbyte_arraycopy,
2067                                entry_jshort_arraycopy,
2068                                entry_jint_arraycopy,
2069                                entry_jlong_arraycopy);
2070 
2071     StubRoutines::_generic_arraycopy =
2072         generate_generic_copy("generic_arraycopy",
2073                                entry_jbyte_arraycopy,
2074                                entry_jshort_arraycopy,
2075                                entry_jint_arraycopy,
2076                                entry_oop_arraycopy,
2077                                entry_jlong_arraycopy,
2078                                entry_checkcast_arraycopy);
2079   }
2080 
2081   // AES intrinsic stubs
2082   enum {AESBlockSize = 16};
2083 
2084   address generate_key_shuffle_mask() {
2085     __ align(16);
2086     StubCodeMark mark(this, "StubRoutines", "key_shuffle_mask");
2087     address start = __ pc();
2088     __ emit_data(0x00010203, relocInfo::none, 0 );
2089     __ emit_data(0x04050607, relocInfo::none, 0 );
2090     __ emit_data(0x08090a0b, relocInfo::none, 0 );
2091     __ emit_data(0x0c0d0e0f, relocInfo::none, 0 );
2092     return start;
2093   }
2094 
2095   address generate_counter_shuffle_mask() {
2096     __ align(16);
2097     StubCodeMark mark(this, "StubRoutines", "counter_shuffle_mask");
2098     address start = __ pc();
2099     __ emit_data(0x0c0d0e0f, relocInfo::none, 0);
2100     __ emit_data(0x08090a0b, relocInfo::none, 0);
2101     __ emit_data(0x04050607, relocInfo::none, 0);
2102     __ emit_data(0x00010203, relocInfo::none, 0);
2103     return start;
2104   }
2105 
2106   // Utility routine for loading a 128-bit key word in little endian format
2107   // can optionally specify that the shuffle mask is already in an xmmregister
2108   void load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
2109     __ movdqu(xmmdst, Address(key, offset));
2110     if (xmm_shuf_mask != NULL) {
2111       __ pshufb(xmmdst, xmm_shuf_mask);
2112     } else {
2113       __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2114     }
2115   }
2116 
2117   // aesenc using specified key+offset
2118   // can optionally specify that the shuffle mask is already in an xmmregister
2119   void aes_enc_key(XMMRegister xmmdst, XMMRegister xmmtmp, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
2120     load_key(xmmtmp, key, offset, xmm_shuf_mask);
2121     __ aesenc(xmmdst, xmmtmp);
2122   }
2123 
2124   // aesdec using specified key+offset
2125   // can optionally specify that the shuffle mask is already in an xmmregister
2126   void aes_dec_key(XMMRegister xmmdst, XMMRegister xmmtmp, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
2127     load_key(xmmtmp, key, offset, xmm_shuf_mask);
2128     __ aesdec(xmmdst, xmmtmp);
2129   }
2130 
2131   // Utility routine for increase 128bit counter (iv in CTR mode)
2132   //  XMM_128bit,  D3, D2, D1, D0
2133   void inc_counter(Register reg, XMMRegister xmmdst, int inc_delta, Label& next_block) {
2134     __ pextrd(reg, xmmdst, 0x0);
2135     __ addl(reg, inc_delta);
2136     __ pinsrd(xmmdst, reg, 0x0);
2137     __ jcc(Assembler::carryClear, next_block); // jump if no carry
2138 
2139     __ pextrd(reg, xmmdst, 0x01); // Carry-> D1
2140     __ addl(reg, 0x01);
2141     __ pinsrd(xmmdst, reg, 0x01);
2142     __ jcc(Assembler::carryClear, next_block); // jump if no carry
2143 
2144     __ pextrd(reg, xmmdst, 0x02); // Carry-> D2
2145     __ addl(reg, 0x01);
2146     __ pinsrd(xmmdst, reg, 0x02);
2147     __ jcc(Assembler::carryClear, next_block); // jump if no carry
2148 
2149     __ pextrd(reg, xmmdst, 0x03); // Carry -> D3
2150     __ addl(reg, 0x01);
2151     __ pinsrd(xmmdst, reg, 0x03);
2152 
2153     __ BIND(next_block);          // next instruction
2154   }
2155 
2156 
2157   // Arguments:
2158   //
2159   // Inputs:
2160   //   c_rarg0   - source byte array address
2161   //   c_rarg1   - destination byte array address
2162   //   c_rarg2   - K (key) in little endian int array
2163   //
2164   address generate_aescrypt_encryptBlock() {
2165     assert(UseAES, "need AES instructions and misaligned SSE support");
2166     __ align(CodeEntryAlignment);
2167     StubCodeMark mark(this, "StubRoutines", "aescrypt_encryptBlock");
2168     Label L_doLast;
2169     address start = __ pc();
2170 
2171     const Register from        = rdx;      // source array address
2172     const Register to          = rdx;      // destination array address
2173     const Register key         = rcx;      // key array address
2174     const Register keylen      = rax;
2175     const Address  from_param(rbp, 8+0);
2176     const Address  to_param  (rbp, 8+4);
2177     const Address  key_param (rbp, 8+8);
2178 
2179     const XMMRegister xmm_result = xmm0;
2180     const XMMRegister xmm_key_shuf_mask = xmm1;
2181     const XMMRegister xmm_temp1  = xmm2;
2182     const XMMRegister xmm_temp2  = xmm3;
2183     const XMMRegister xmm_temp3  = xmm4;
2184     const XMMRegister xmm_temp4  = xmm5;
2185 
2186     __ enter();   // required for proper stackwalking of RuntimeStub frame
2187 
2188     // For EVEX with VL and BW, provide a standard mask, VL = 128 will guide the merge
2189     // context for the registers used, where all instructions below are using 128-bit mode
2190     // On EVEX without VL and BW, these instructions will all be AVX.
2191     if (VM_Version::supports_avx512vlbw()) {
2192       __ movl(rdx, 0xffff);
2193       __ kmovdl(k1, rdx);
2194     }
2195 
2196     __ movptr(from, from_param);
2197     __ movptr(key, key_param);
2198 
2199     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
2200     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
2201 
2202     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2203     __ movdqu(xmm_result, Address(from, 0));  // get 16 bytes of input
2204     __ movptr(to, to_param);
2205 
2206     // For encryption, the java expanded key ordering is just what we need
2207 
2208     load_key(xmm_temp1, key, 0x00, xmm_key_shuf_mask);
2209     __ pxor(xmm_result, xmm_temp1);
2210 
2211     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
2212     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
2213     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
2214     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
2215 
2216     __ aesenc(xmm_result, xmm_temp1);
2217     __ aesenc(xmm_result, xmm_temp2);
2218     __ aesenc(xmm_result, xmm_temp3);
2219     __ aesenc(xmm_result, xmm_temp4);
2220 
2221     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
2222     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
2223     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
2224     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
2225 
2226     __ aesenc(xmm_result, xmm_temp1);
2227     __ aesenc(xmm_result, xmm_temp2);
2228     __ aesenc(xmm_result, xmm_temp3);
2229     __ aesenc(xmm_result, xmm_temp4);
2230 
2231     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
2232     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
2233 
2234     __ cmpl(keylen, 44);
2235     __ jccb(Assembler::equal, L_doLast);
2236 
2237     __ aesenc(xmm_result, xmm_temp1);
2238     __ aesenc(xmm_result, xmm_temp2);
2239 
2240     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
2241     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
2242 
2243     __ cmpl(keylen, 52);
2244     __ jccb(Assembler::equal, L_doLast);
2245 
2246     __ aesenc(xmm_result, xmm_temp1);
2247     __ aesenc(xmm_result, xmm_temp2);
2248 
2249     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
2250     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
2251 
2252     __ BIND(L_doLast);
2253     __ aesenc(xmm_result, xmm_temp1);
2254     __ aesenclast(xmm_result, xmm_temp2);
2255     __ movdqu(Address(to, 0), xmm_result);        // store the result
2256     __ xorptr(rax, rax); // return 0
2257     __ leave(); // required for proper stackwalking of RuntimeStub frame
2258     __ ret(0);
2259 
2260     return start;
2261   }
2262 
2263 
2264   // Arguments:
2265   //
2266   // Inputs:
2267   //   c_rarg0   - source byte array address
2268   //   c_rarg1   - destination byte array address
2269   //   c_rarg2   - K (key) in little endian int array
2270   //
2271   address generate_aescrypt_decryptBlock() {
2272     assert(UseAES, "need AES instructions and misaligned SSE support");
2273     __ align(CodeEntryAlignment);
2274     StubCodeMark mark(this, "StubRoutines", "aescrypt_decryptBlock");
2275     Label L_doLast;
2276     address start = __ pc();
2277 
2278     const Register from        = rdx;      // source array address
2279     const Register to          = rdx;      // destination array address
2280     const Register key         = rcx;      // key array address
2281     const Register keylen      = rax;
2282     const Address  from_param(rbp, 8+0);
2283     const Address  to_param  (rbp, 8+4);
2284     const Address  key_param (rbp, 8+8);
2285 
2286     const XMMRegister xmm_result = xmm0;
2287     const XMMRegister xmm_key_shuf_mask = xmm1;
2288     const XMMRegister xmm_temp1  = xmm2;
2289     const XMMRegister xmm_temp2  = xmm3;
2290     const XMMRegister xmm_temp3  = xmm4;
2291     const XMMRegister xmm_temp4  = xmm5;
2292 
2293     __ enter(); // required for proper stackwalking of RuntimeStub frame
2294 
2295     // For EVEX with VL and BW, provide a standard mask, VL = 128 will guide the merge
2296     // context for the registers used, where all instructions below are using 128-bit mode
2297     // On EVEX without VL and BW, these instructions will all be AVX.
2298     if (VM_Version::supports_avx512vlbw()) {
2299       __ movl(rdx, 0xffff);
2300       __ kmovdl(k1, rdx);
2301     }
2302 
2303     __ movptr(from, from_param);
2304     __ movptr(key, key_param);
2305 
2306     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
2307     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
2308 
2309     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2310     __ movdqu(xmm_result, Address(from, 0));
2311     __ movptr(to, to_param);
2312 
2313     // for decryption java expanded key ordering is rotated one position from what we want
2314     // so we start from 0x10 here and hit 0x00 last
2315     // we don't know if the key is aligned, hence not using load-execute form
2316     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
2317     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
2318     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
2319     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
2320 
2321     __ pxor  (xmm_result, xmm_temp1);
2322     __ aesdec(xmm_result, xmm_temp2);
2323     __ aesdec(xmm_result, xmm_temp3);
2324     __ aesdec(xmm_result, xmm_temp4);
2325 
2326     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
2327     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
2328     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
2329     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
2330 
2331     __ aesdec(xmm_result, xmm_temp1);
2332     __ aesdec(xmm_result, xmm_temp2);
2333     __ aesdec(xmm_result, xmm_temp3);
2334     __ aesdec(xmm_result, xmm_temp4);
2335 
2336     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
2337     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
2338     load_key(xmm_temp3, key, 0x00, xmm_key_shuf_mask);
2339 
2340     __ cmpl(keylen, 44);
2341     __ jccb(Assembler::equal, L_doLast);
2342 
2343     __ aesdec(xmm_result, xmm_temp1);
2344     __ aesdec(xmm_result, xmm_temp2);
2345 
2346     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
2347     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
2348 
2349     __ cmpl(keylen, 52);
2350     __ jccb(Assembler::equal, L_doLast);
2351 
2352     __ aesdec(xmm_result, xmm_temp1);
2353     __ aesdec(xmm_result, xmm_temp2);
2354 
2355     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
2356     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
2357 
2358     __ BIND(L_doLast);
2359     __ aesdec(xmm_result, xmm_temp1);
2360     __ aesdec(xmm_result, xmm_temp2);
2361 
2362     // for decryption the aesdeclast operation is always on key+0x00
2363     __ aesdeclast(xmm_result, xmm_temp3);
2364     __ movdqu(Address(to, 0), xmm_result);  // store the result
2365     __ xorptr(rax, rax); // return 0
2366     __ leave(); // required for proper stackwalking of RuntimeStub frame
2367     __ ret(0);
2368 
2369     return start;
2370   }
2371 
2372   void handleSOERegisters(bool saving) {
2373     const int saveFrameSizeInBytes = 4 * wordSize;
2374     const Address saved_rbx     (rbp, -3 * wordSize);
2375     const Address saved_rsi     (rbp, -2 * wordSize);
2376     const Address saved_rdi     (rbp, -1 * wordSize);
2377 
2378     if (saving) {
2379       __ subptr(rsp, saveFrameSizeInBytes);
2380       __ movptr(saved_rsi, rsi);
2381       __ movptr(saved_rdi, rdi);
2382       __ movptr(saved_rbx, rbx);
2383     } else {
2384       // restoring
2385       __ movptr(rsi, saved_rsi);
2386       __ movptr(rdi, saved_rdi);
2387       __ movptr(rbx, saved_rbx);
2388     }
2389   }
2390 
2391   // Arguments:
2392   //
2393   // Inputs:
2394   //   c_rarg0   - source byte array address
2395   //   c_rarg1   - destination byte array address
2396   //   c_rarg2   - K (key) in little endian int array
2397   //   c_rarg3   - r vector byte array address
2398   //   c_rarg4   - input length
2399   //
2400   // Output:
2401   //   rax       - input length
2402   //
2403   address generate_cipherBlockChaining_encryptAESCrypt() {
2404     assert(UseAES, "need AES instructions and misaligned SSE support");
2405     __ align(CodeEntryAlignment);
2406     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_encryptAESCrypt");
2407     address start = __ pc();
2408 
2409     Label L_exit, L_key_192_256, L_key_256, L_loopTop_128, L_loopTop_192, L_loopTop_256;
2410     const Register from        = rsi;      // source array address
2411     const Register to          = rdx;      // destination array address
2412     const Register key         = rcx;      // key array address
2413     const Register rvec        = rdi;      // r byte array initialized from initvector array address
2414                                            // and left with the results of the last encryption block
2415     const Register len_reg     = rbx;      // src len (must be multiple of blocksize 16)
2416     const Register pos         = rax;
2417 
2418     // xmm register assignments for the loops below
2419     const XMMRegister xmm_result = xmm0;
2420     const XMMRegister xmm_temp   = xmm1;
2421     // first 6 keys preloaded into xmm2-xmm7
2422     const int XMM_REG_NUM_KEY_FIRST = 2;
2423     const int XMM_REG_NUM_KEY_LAST  = 7;
2424     const XMMRegister xmm_key0   = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
2425 
2426     __ enter(); // required for proper stackwalking of RuntimeStub frame
2427     handleSOERegisters(true /*saving*/);
2428 
2429     // For EVEX with VL and BW, provide a standard mask, VL = 128 will guide the merge
2430     // context for the registers used, where all instructions below are using 128-bit mode
2431     // On EVEX without VL and BW, these instructions will all be AVX.
2432     if (VM_Version::supports_avx512vlbw()) {
2433       __ movl(rdx, 0xffff);
2434       __ kmovdl(k1, rdx);
2435     }
2436 
2437     // load registers from incoming parameters
2438     const Address  from_param(rbp, 8+0);
2439     const Address  to_param  (rbp, 8+4);
2440     const Address  key_param (rbp, 8+8);
2441     const Address  rvec_param (rbp, 8+12);
2442     const Address  len_param  (rbp, 8+16);
2443     __ movptr(from , from_param);
2444     __ movptr(to   , to_param);
2445     __ movptr(key  , key_param);
2446     __ movptr(rvec , rvec_param);
2447     __ movptr(len_reg , len_param);
2448 
2449     const XMMRegister xmm_key_shuf_mask = xmm_temp;  // used temporarily to swap key bytes up front
2450     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2451     // load up xmm regs 2 thru 7 with keys 0-5
2452     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x00; rnum  <= XMM_REG_NUM_KEY_LAST; rnum++) {
2453       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
2454       offset += 0x10;
2455     }
2456 
2457     __ movdqu(xmm_result, Address(rvec, 0x00));   // initialize xmm_result with r vec
2458 
2459     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
2460     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
2461     __ cmpl(rax, 44);
2462     __ jcc(Assembler::notEqual, L_key_192_256);
2463 
2464     // 128 bit code follows here
2465     __ movl(pos, 0);
2466     __ align(OptoLoopAlignment);
2467     __ BIND(L_loopTop_128);
2468     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
2469     __ pxor  (xmm_result, xmm_temp);                                // xor with the current r vector
2470 
2471     __ pxor  (xmm_result, xmm_key0);                                // do the aes rounds
2472     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_LAST; rnum++) {
2473       __ aesenc(xmm_result, as_XMMRegister(rnum));
2474     }
2475     for (int key_offset = 0x60; key_offset <= 0x90; key_offset += 0x10) {
2476       aes_enc_key(xmm_result, xmm_temp, key, key_offset);
2477     }
2478     load_key(xmm_temp, key, 0xa0);
2479     __ aesenclast(xmm_result, xmm_temp);
2480 
2481     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
2482     // no need to store r to memory until we exit
2483     __ addptr(pos, AESBlockSize);
2484     __ subptr(len_reg, AESBlockSize);
2485     __ jcc(Assembler::notEqual, L_loopTop_128);
2486 
2487     __ BIND(L_exit);
2488     __ movdqu(Address(rvec, 0), xmm_result);     // final value of r stored in rvec of CipherBlockChaining object
2489 
2490     handleSOERegisters(false /*restoring*/);
2491     __ movptr(rax, len_param); // return length
2492     __ leave();                                  // required for proper stackwalking of RuntimeStub frame
2493     __ ret(0);
2494 
2495     __ BIND(L_key_192_256);
2496     // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
2497     __ cmpl(rax, 52);
2498     __ jcc(Assembler::notEqual, L_key_256);
2499 
2500     // 192-bit code follows here (could be changed to use more xmm registers)
2501     __ movl(pos, 0);
2502     __ align(OptoLoopAlignment);
2503     __ BIND(L_loopTop_192);
2504     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
2505     __ pxor  (xmm_result, xmm_temp);                                // xor with the current r vector
2506 
2507     __ pxor  (xmm_result, xmm_key0);                                // do the aes rounds
2508     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_LAST; rnum++) {
2509       __ aesenc(xmm_result, as_XMMRegister(rnum));
2510     }
2511     for (int key_offset = 0x60; key_offset <= 0xb0; key_offset += 0x10) {
2512       aes_enc_key(xmm_result, xmm_temp, key, key_offset);
2513     }
2514     load_key(xmm_temp, key, 0xc0);
2515     __ aesenclast(xmm_result, xmm_temp);
2516 
2517     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);   // store into the next 16 bytes of output
2518     // no need to store r to memory until we exit
2519     __ addptr(pos, AESBlockSize);
2520     __ subptr(len_reg, AESBlockSize);
2521     __ jcc(Assembler::notEqual, L_loopTop_192);
2522     __ jmp(L_exit);
2523 
2524     __ BIND(L_key_256);
2525     // 256-bit code follows here (could be changed to use more xmm registers)
2526     __ movl(pos, 0);
2527     __ align(OptoLoopAlignment);
2528     __ BIND(L_loopTop_256);
2529     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
2530     __ pxor  (xmm_result, xmm_temp);                                // xor with the current r vector
2531 
2532     __ pxor  (xmm_result, xmm_key0);                                // do the aes rounds
2533     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_LAST; rnum++) {
2534       __ aesenc(xmm_result, as_XMMRegister(rnum));
2535     }
2536     for (int key_offset = 0x60; key_offset <= 0xd0; key_offset += 0x10) {
2537       aes_enc_key(xmm_result, xmm_temp, key, key_offset);
2538     }
2539     load_key(xmm_temp, key, 0xe0);
2540     __ aesenclast(xmm_result, xmm_temp);
2541 
2542     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);   // store into the next 16 bytes of output
2543     // no need to store r to memory until we exit
2544     __ addptr(pos, AESBlockSize);
2545     __ subptr(len_reg, AESBlockSize);
2546     __ jcc(Assembler::notEqual, L_loopTop_256);
2547     __ jmp(L_exit);
2548 
2549     return start;
2550   }
2551 
2552 
2553   // CBC AES Decryption.
2554   // In 32-bit stub, because of lack of registers we do not try to parallelize 4 blocks at a time.
2555   //
2556   // Arguments:
2557   //
2558   // Inputs:
2559   //   c_rarg0   - source byte array address
2560   //   c_rarg1   - destination byte array address
2561   //   c_rarg2   - K (key) in little endian int array
2562   //   c_rarg3   - r vector byte array address
2563   //   c_rarg4   - input length
2564   //
2565   // Output:
2566   //   rax       - input length
2567   //
2568 
2569   address generate_cipherBlockChaining_decryptAESCrypt_Parallel() {
2570     assert(UseAES, "need AES instructions and misaligned SSE support");
2571     __ align(CodeEntryAlignment);
2572     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt");
2573     address start = __ pc();
2574 
2575     const Register from        = rsi;      // source array address
2576     const Register to          = rdx;      // destination array address
2577     const Register key         = rcx;      // key array address
2578     const Register rvec        = rdi;      // r byte array initialized from initvector array address
2579                                            // and left with the results of the last encryption block
2580     const Register len_reg     = rbx;      // src len (must be multiple of blocksize 16)
2581     const Register pos         = rax;
2582 
2583     const int PARALLEL_FACTOR = 4;
2584     const int ROUNDS[3] = { 10, 12, 14 }; //aes rounds for key128, key192, key256
2585 
2586     Label L_exit;
2587     Label L_singleBlock_loopTop[3]; //128, 192, 256
2588     Label L_multiBlock_loopTop[3]; //128, 192, 256
2589 
2590     const XMMRegister xmm_prev_block_cipher = xmm0; // holds cipher of previous block
2591     const XMMRegister xmm_key_shuf_mask = xmm1;
2592 
2593     const XMMRegister xmm_key_tmp0 = xmm2;
2594     const XMMRegister xmm_key_tmp1 = xmm3;
2595 
2596     // registers holding the six results in the parallelized loop
2597     const XMMRegister xmm_result0 = xmm4;
2598     const XMMRegister xmm_result1 = xmm5;
2599     const XMMRegister xmm_result2 = xmm6;
2600     const XMMRegister xmm_result3 = xmm7;
2601 
2602     __ enter(); // required for proper stackwalking of RuntimeStub frame
2603     handleSOERegisters(true /*saving*/);
2604 
2605     // For EVEX with VL and BW, provide a standard mask, VL = 128 will guide the merge
2606     // context for the registers used, where all instructions below are using 128-bit mode
2607     // On EVEX without VL and BW, these instructions will all be AVX.
2608     if (VM_Version::supports_avx512vlbw()) {
2609       __ movl(rdx, 0xffff);
2610       __ kmovdl(k1, rdx);
2611     }
2612 
2613     // load registers from incoming parameters
2614     const Address  from_param(rbp, 8+0);
2615     const Address  to_param  (rbp, 8+4);
2616     const Address  key_param (rbp, 8+8);
2617     const Address  rvec_param (rbp, 8+12);
2618     const Address  len_param  (rbp, 8+16);
2619 
2620     __ movptr(from , from_param);
2621     __ movptr(to   , to_param);
2622     __ movptr(key  , key_param);
2623     __ movptr(rvec , rvec_param);
2624     __ movptr(len_reg , len_param);
2625 
2626     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2627     __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00)); // initialize with initial rvec
2628 
2629     __ xorptr(pos, pos);
2630 
2631     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
2632     // rvec is reused
2633     __ movl(rvec, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
2634     __ cmpl(rvec, 52);
2635     __ jcc(Assembler::equal, L_multiBlock_loopTop[1]);
2636     __ cmpl(rvec, 60);
2637     __ jcc(Assembler::equal, L_multiBlock_loopTop[2]);
2638 
2639 #define DoFour(opc, src_reg)           \
2640   __ opc(xmm_result0, src_reg);         \
2641   __ opc(xmm_result1, src_reg);         \
2642   __ opc(xmm_result2, src_reg);         \
2643   __ opc(xmm_result3, src_reg);         \
2644 
2645     for (int k = 0; k < 3; ++k) {
2646       __ align(OptoLoopAlignment);
2647       __ BIND(L_multiBlock_loopTop[k]);
2648       __ cmpptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // see if at least 4 blocks left
2649       __ jcc(Assembler::less, L_singleBlock_loopTop[k]);
2650 
2651       __ movdqu(xmm_result0, Address(from, pos, Address::times_1, 0 * AESBlockSize)); // get next 4 blocks into xmmresult registers
2652       __ movdqu(xmm_result1, Address(from, pos, Address::times_1, 1 * AESBlockSize));
2653       __ movdqu(xmm_result2, Address(from, pos, Address::times_1, 2 * AESBlockSize));
2654       __ movdqu(xmm_result3, Address(from, pos, Address::times_1, 3 * AESBlockSize));
2655 
2656       // the java expanded key ordering is rotated one position from what we want
2657       // so we start from 0x10 here and hit 0x00 last
2658       load_key(xmm_key_tmp0, key, 0x10, xmm_key_shuf_mask);
2659       DoFour(pxor, xmm_key_tmp0); //xor with first key
2660       // do the aes dec rounds
2661       for (int rnum = 1; rnum <= ROUNDS[k];) {
2662         //load two keys at a time
2663         //k1->0x20, ..., k9->0xa0, k10->0x00
2664         load_key(xmm_key_tmp1, key, (rnum + 1) * 0x10, xmm_key_shuf_mask);
2665         load_key(xmm_key_tmp0, key, ((rnum + 2) % (ROUNDS[k] + 1)) * 0x10, xmm_key_shuf_mask); // hit 0x00 last!
2666         DoFour(aesdec, xmm_key_tmp1);
2667         rnum++;
2668         if (rnum != ROUNDS[k]) {
2669           DoFour(aesdec, xmm_key_tmp0);
2670         }
2671         else {
2672           DoFour(aesdeclast, xmm_key_tmp0);
2673         }
2674         rnum++;
2675       }
2676 
2677       // for each result, xor with the r vector of previous cipher block
2678       __ pxor(xmm_result0, xmm_prev_block_cipher);
2679       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 0 * AESBlockSize));
2680       __ pxor(xmm_result1, xmm_prev_block_cipher);
2681       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 1 * AESBlockSize));
2682       __ pxor(xmm_result2, xmm_prev_block_cipher);
2683       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 2 * AESBlockSize));
2684       __ pxor(xmm_result3, xmm_prev_block_cipher);
2685       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 3 * AESBlockSize)); // this will carry over to next set of blocks
2686 
2687             // store 4 results into the next 64 bytes of output
2688        __ movdqu(Address(to, pos, Address::times_1, 0 * AESBlockSize), xmm_result0);
2689        __ movdqu(Address(to, pos, Address::times_1, 1 * AESBlockSize), xmm_result1);
2690        __ movdqu(Address(to, pos, Address::times_1, 2 * AESBlockSize), xmm_result2);
2691        __ movdqu(Address(to, pos, Address::times_1, 3 * AESBlockSize), xmm_result3);
2692 
2693        __ addptr(pos, 4 * AESBlockSize);
2694        __ subptr(len_reg, 4 * AESBlockSize);
2695        __ jmp(L_multiBlock_loopTop[k]);
2696 
2697        //singleBlock starts here
2698        __ align(OptoLoopAlignment);
2699        __ BIND(L_singleBlock_loopTop[k]);
2700        __ cmpptr(len_reg, 0); // any blocks left?
2701        __ jcc(Assembler::equal, L_exit);
2702        __ movdqu(xmm_result0, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input
2703        __ movdqa(xmm_result1, xmm_result0);
2704 
2705        load_key(xmm_key_tmp0, key, 0x10, xmm_key_shuf_mask);
2706        __ pxor(xmm_result0, xmm_key_tmp0);
2707        // do the aes dec rounds
2708        for (int rnum = 1; rnum < ROUNDS[k]; rnum++) {
2709          // the java expanded key ordering is rotated one position from what we want
2710          load_key(xmm_key_tmp0, key, (rnum + 1) * 0x10, xmm_key_shuf_mask);
2711          __ aesdec(xmm_result0, xmm_key_tmp0);
2712        }
2713        load_key(xmm_key_tmp0, key, 0x00, xmm_key_shuf_mask);
2714        __ aesdeclast(xmm_result0, xmm_key_tmp0);
2715        __ pxor(xmm_result0, xmm_prev_block_cipher); // xor with the current r vector
2716        __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result0); // store into the next 16 bytes of output
2717        // no need to store r to memory until we exit
2718        __ movdqa(xmm_prev_block_cipher, xmm_result1); // set up next r vector with cipher input from this block
2719 
2720        __ addptr(pos, AESBlockSize);
2721        __ subptr(len_reg, AESBlockSize);
2722        __ jmp(L_singleBlock_loopTop[k]);
2723     }//for 128/192/256
2724 
2725     __ BIND(L_exit);
2726     __ movptr(rvec, rvec_param);                        // restore this since reused earlier
2727     __ movdqu(Address(rvec, 0), xmm_prev_block_cipher); // final value of r stored in rvec of CipherBlockChaining object
2728     handleSOERegisters(false /*restoring*/);
2729     __ movptr(rax, len_param);                          // return length
2730     __ leave();                                         // required for proper stackwalking of RuntimeStub frame
2731     __ ret(0);
2732 
2733     return start;
2734   }
2735 
2736   // CTR AES crypt.
2737   // In 32-bit stub, parallelize 4 blocks at a time
2738   // Arguments:
2739   //
2740   // Inputs:
2741   //   c_rarg0   - source byte array address
2742   //   c_rarg1   - destination byte array address
2743   //   c_rarg2   - K (key) in little endian int array
2744   //   c_rarg3   - counter vector byte array address
2745   //   c_rarg4   - input length
2746   //
2747   // Output:
2748   //   rax       - input length
2749   //
2750   address generate_counterMode_AESCrypt_Parallel() {
2751     assert(UseAES, "need AES instructions and misaligned SSE support");
2752     __ align(CodeEntryAlignment);
2753     StubCodeMark mark(this, "StubRoutines", "counterMode_AESCrypt");
2754     address start = __ pc();
2755     const Register from        = rsi;      // source array address
2756     const Register to          = rdx;      // destination array address
2757     const Register key         = rcx;      // key array address
2758     const Register counter     = rdi;      // counter byte array initialized from initvector array address
2759                                            // and updated with the incremented counter in the end
2760     const Register len_reg     = rbx;
2761     const Register pos         = rax;
2762 
2763     __ enter(); // required for proper stackwalking of RuntimeStub frame
2764     handleSOERegisters(true /*saving*/); // save rbx, rsi, rdi
2765 
2766     // For EVEX with VL and BW, provide a standard mask, VL = 128 will guide the merge
2767     // context for the registers used, where all instructions below are using 128-bit mode
2768     // On EVEX without VL and BW, these instructions will all be AVX.
2769     if (VM_Version::supports_avx512vlbw()) {
2770       __ movl(rdx, 0xffff);
2771       __ kmovdl(k1, rdx);
2772     }
2773 
2774     // load registers from incoming parameters
2775     const Address  from_param(rbp, 8+0);
2776     const Address  to_param  (rbp, 8+4);
2777     const Address  key_param (rbp, 8+8);
2778     const Address  rvec_param (rbp, 8+12);
2779     const Address  len_param  (rbp, 8+16);
2780     const Address  saved_counter_param(rbp, 8 + 20);
2781     const Address  used_addr_param(rbp, 8 + 24);
2782 
2783     __ movptr(from , from_param);
2784     __ movptr(to   , to_param);
2785     __ movptr(len_reg , len_param);
2786 
2787     // Use the partially used encrpyted counter from last invocation
2788     Label L_exit_preLoop, L_preLoop_start;
2789 
2790     // Use the registers 'counter' and 'key' here in this preloop
2791     // to hold of last 2 params 'used' and 'saved_encCounter_start'
2792     Register used = counter;
2793     Register saved_encCounter_start = key;
2794     Register used_addr = saved_encCounter_start;
2795 
2796     __ movptr(used_addr, used_addr_param);
2797     __ movptr(used, Address(used_addr, 0));
2798     __ movptr(saved_encCounter_start, saved_counter_param);
2799 
2800     __ BIND(L_preLoop_start);
2801     __ cmpptr(used, 16);
2802     __ jcc(Assembler::aboveEqual, L_exit_preLoop);
2803     __ cmpptr(len_reg, 0);
2804     __ jcc(Assembler::lessEqual, L_exit_preLoop);
2805     __ movb(rax, Address(saved_encCounter_start, used));
2806     __ xorb(rax, Address(from, 0));
2807     __ movb(Address(to, 0), rax);
2808     __ addptr(from, 1);
2809     __ addptr(to, 1);
2810     __ addptr(used, 1);
2811     __ subptr(len_reg, 1);
2812 
2813     __ jmp(L_preLoop_start);
2814 
2815     __ BIND(L_exit_preLoop);
2816     __ movptr(used_addr, used_addr_param);
2817     __ movptr(used_addr, used_addr_param);
2818     __ movl(Address(used_addr, 0), used);
2819 
2820     // load the parameters 'key' and 'counter'
2821     __ movptr(key, key_param);
2822     __ movptr(counter, rvec_param);
2823 
2824     // xmm register assignments for the loops below
2825     const XMMRegister xmm_curr_counter      = xmm0;
2826     const XMMRegister xmm_counter_shuf_mask = xmm1;  // need to be reloaded
2827     const XMMRegister xmm_key_shuf_mask     = xmm2;  // need to be reloaded
2828     const XMMRegister xmm_key               = xmm3;
2829     const XMMRegister xmm_result0           = xmm4;
2830     const XMMRegister xmm_result1           = xmm5;
2831     const XMMRegister xmm_result2           = xmm6;
2832     const XMMRegister xmm_result3           = xmm7;
2833     const XMMRegister xmm_from0             = xmm1;   //reuse XMM register
2834     const XMMRegister xmm_from1             = xmm2;
2835     const XMMRegister xmm_from2             = xmm3;
2836     const XMMRegister xmm_from3             = xmm4;
2837 
2838     //for key_128, key_192, key_256
2839     const int rounds[3] = {10, 12, 14};
2840     Label L_singleBlockLoopTop[3];
2841     Label L_multiBlock_loopTop[3];
2842     Label L_key192_top, L_key256_top;
2843     Label L_incCounter[3][4]; // 3: different key length,  4: 4 blocks at a time
2844     Label L_incCounter_single[3]; //for single block, key128, key192, key256
2845     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];
2846     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];
2847 
2848     Label L_exit;
2849     const int PARALLEL_FACTOR = 4;  //because of the limited register number
2850 
2851     // initialize counter with initial counter
2852     __ movdqu(xmm_curr_counter, Address(counter, 0x00));
2853     __ movdqu(xmm_counter_shuf_mask, ExternalAddress(StubRoutines::x86::counter_shuffle_mask_addr()));
2854     __ pshufb(xmm_curr_counter, xmm_counter_shuf_mask); //counter is shuffled for increase
2855 
2856     // key length could be only {11, 13, 15} * 4 = {44, 52, 60}
2857     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2858     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
2859     __ cmpl(rax, 52);
2860     __ jcc(Assembler::equal, L_key192_top);
2861     __ cmpl(rax, 60);
2862     __ jcc(Assembler::equal, L_key256_top);
2863 
2864     //key128 begins here
2865     __ movptr(pos, 0); // init pos before L_multiBlock_loopTop
2866 
2867 #define CTR_DoFour(opc, src_reg)               \
2868     __ opc(xmm_result0, src_reg);              \
2869     __ opc(xmm_result1, src_reg);              \
2870     __ opc(xmm_result2, src_reg);              \
2871     __ opc(xmm_result3, src_reg);
2872 
2873     // k == 0 :  generate code for key_128
2874     // k == 1 :  generate code for key_192
2875     // k == 2 :  generate code for key_256
2876     for (int k = 0; k < 3; ++k) {
2877       //multi blocks starts here
2878       __ align(OptoLoopAlignment);
2879       __ BIND(L_multiBlock_loopTop[k]);
2880       __ cmpptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // see if at least PARALLEL_FACTOR blocks left
2881       __ jcc(Assembler::less, L_singleBlockLoopTop[k]);
2882 
2883       __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2884       __ movdqu(xmm_counter_shuf_mask, ExternalAddress(StubRoutines::x86::counter_shuffle_mask_addr()));
2885 
2886       //load, then increase counters
2887       CTR_DoFour(movdqa, xmm_curr_counter);
2888       __ push(rbx);
2889       inc_counter(rbx, xmm_result1, 0x01, L_incCounter[k][0]);
2890       inc_counter(rbx, xmm_result2, 0x02, L_incCounter[k][1]);
2891       inc_counter(rbx, xmm_result3, 0x03, L_incCounter[k][2]);
2892       inc_counter(rbx, xmm_curr_counter, 0x04, L_incCounter[k][3]);
2893       __ pop (rbx);
2894 
2895       load_key(xmm_key, key, 0x00, xmm_key_shuf_mask); // load Round 0 key. interleaving for better performance
2896 
2897       CTR_DoFour(pshufb, xmm_counter_shuf_mask); // after increased, shuffled counters back for PXOR
2898       CTR_DoFour(pxor, xmm_key);   //PXOR with Round 0 key
2899 
2900       for (int i = 1; i < rounds[k]; ++i) {
2901         load_key(xmm_key, key, (0x10 * i), xmm_key_shuf_mask);
2902         CTR_DoFour(aesenc, xmm_key);
2903       }
2904       load_key(xmm_key, key, (0x10 * rounds[k]), xmm_key_shuf_mask);
2905       CTR_DoFour(aesenclast, xmm_key);
2906 
2907       // get next PARALLEL_FACTOR blocks into xmm_from registers
2908       __ movdqu(xmm_from0, Address(from, pos, Address::times_1, 0 * AESBlockSize));
2909       __ movdqu(xmm_from1, Address(from, pos, Address::times_1, 1 * AESBlockSize));
2910       __ movdqu(xmm_from2, Address(from, pos, Address::times_1, 2 * AESBlockSize));
2911 
2912       // PXOR with input text
2913       __ pxor(xmm_result0, xmm_from0); //result0 is xmm4
2914       __ pxor(xmm_result1, xmm_from1);
2915       __ pxor(xmm_result2, xmm_from2);
2916 
2917       // store PARALLEL_FACTOR results into the next 64 bytes of output
2918       __ movdqu(Address(to, pos, Address::times_1, 0 * AESBlockSize), xmm_result0);
2919       __ movdqu(Address(to, pos, Address::times_1, 1 * AESBlockSize), xmm_result1);
2920       __ movdqu(Address(to, pos, Address::times_1, 2 * AESBlockSize), xmm_result2);
2921 
2922       // do it here after xmm_result0 is saved, because xmm_from3 reuse the same register of xmm_result0.
2923       __ movdqu(xmm_from3, Address(from, pos, Address::times_1, 3 * AESBlockSize));
2924       __ pxor(xmm_result3, xmm_from3);
2925       __ movdqu(Address(to, pos, Address::times_1, 3 * AESBlockSize), xmm_result3);
2926 
2927       __ addptr(pos, PARALLEL_FACTOR * AESBlockSize); // increase the length of crypt text
2928       __ subptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // decrease the remaining length
2929       __ jmp(L_multiBlock_loopTop[k]);
2930 
2931       // singleBlock starts here
2932       __ align(OptoLoopAlignment);
2933       __ BIND(L_singleBlockLoopTop[k]);
2934       __ cmpptr(len_reg, 0);
2935       __ jcc(Assembler::equal, L_exit);
2936       __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2937       __ movdqu(xmm_counter_shuf_mask, ExternalAddress(StubRoutines::x86::counter_shuffle_mask_addr()));
2938       __ movdqa(xmm_result0, xmm_curr_counter);
2939       load_key(xmm_key, key, 0x00, xmm_key_shuf_mask);
2940       __ push(rbx);//rbx is used for increasing counter
2941       inc_counter(rbx, xmm_curr_counter, 0x01, L_incCounter_single[k]);
2942       __ pop (rbx);
2943       __ pshufb(xmm_result0, xmm_counter_shuf_mask);
2944       __ pxor(xmm_result0, xmm_key);
2945       for (int i = 1; i < rounds[k]; i++) {
2946         load_key(xmm_key, key, (0x10 * i), xmm_key_shuf_mask);
2947         __ aesenc(xmm_result0, xmm_key);
2948       }
2949       load_key(xmm_key, key, (0x10 * rounds[k]), xmm_key_shuf_mask);
2950       __ aesenclast(xmm_result0, xmm_key);
2951       __ cmpptr(len_reg, AESBlockSize);
2952       __ jcc(Assembler::less, L_processTail_insr[k]);
2953         __ movdqu(xmm_from0, Address(from, pos, Address::times_1, 0 * AESBlockSize));
2954         __ pxor(xmm_result0, xmm_from0);
2955         __ movdqu(Address(to, pos, Address::times_1, 0 * AESBlockSize), xmm_result0);
2956         __ addptr(pos, AESBlockSize);
2957         __ subptr(len_reg, AESBlockSize);
2958         __ jmp(L_singleBlockLoopTop[k]);
2959 
2960       __ BIND(L_processTail_insr[k]);                                               // Process the tail part of the input array
2961         __ addptr(pos, len_reg);                                                    // 1. Insert bytes from src array into xmm_from0 register
2962         __ testptr(len_reg, 8);
2963         __ jcc(Assembler::zero, L_processTail_4_insr[k]);
2964           __ subptr(pos,8);
2965           __ pinsrd(xmm_from0, Address(from, pos), 0);
2966           __ pinsrd(xmm_from0, Address(from, pos, Address::times_1, 4), 1);
2967         __ BIND(L_processTail_4_insr[k]);
2968         __ testptr(len_reg, 4);
2969         __ jcc(Assembler::zero, L_processTail_2_insr[k]);
2970           __ subptr(pos,4);
2971           __ pslldq(xmm_from0, 4);
2972           __ pinsrd(xmm_from0, Address(from, pos), 0);
2973         __ BIND(L_processTail_2_insr[k]);
2974         __ testptr(len_reg, 2);
2975         __ jcc(Assembler::zero, L_processTail_1_insr[k]);
2976           __ subptr(pos, 2);
2977           __ pslldq(xmm_from0, 2);
2978           __ pinsrw(xmm_from0, Address(from, pos), 0);
2979         __ BIND(L_processTail_1_insr[k]);
2980         __ testptr(len_reg, 1);
2981         __ jcc(Assembler::zero, L_processTail_exit_insr[k]);
2982           __ subptr(pos, 1);
2983           __ pslldq(xmm_from0, 1);
2984           __ pinsrb(xmm_from0, Address(from, pos), 0);
2985         __ BIND(L_processTail_exit_insr[k]);
2986 
2987         __ movptr(saved_encCounter_start, saved_counter_param);
2988         __ movdqu(Address(saved_encCounter_start, 0), xmm_result0);               // 2. Perform pxor of the encrypted counter and plaintext Bytes.
2989         __ pxor(xmm_result0, xmm_from0);                                          //    Also the encrypted counter is saved for next invocation.
2990 
2991         __ testptr(len_reg, 8);
2992         __ jcc(Assembler::zero, L_processTail_4_extr[k]);                        // 3. Extract bytes from xmm_result0 into the dest. array
2993           __ pextrd(Address(to, pos), xmm_result0, 0);
2994           __ pextrd(Address(to, pos, Address::times_1, 4), xmm_result0, 1);
2995           __ psrldq(xmm_result0, 8);
2996           __ addptr(pos, 8);
2997         __ BIND(L_processTail_4_extr[k]);
2998         __ testptr(len_reg, 4);
2999         __ jcc(Assembler::zero, L_processTail_2_extr[k]);
3000           __ pextrd(Address(to, pos), xmm_result0, 0);
3001           __ psrldq(xmm_result0, 4);
3002           __ addptr(pos, 4);
3003         __ BIND(L_processTail_2_extr[k]);
3004         __ testptr(len_reg, 2);
3005         __ jcc(Assembler::zero, L_processTail_1_extr[k]);
3006           __ pextrb(Address(to, pos), xmm_result0, 0);
3007           __ pextrb(Address(to, pos, Address::times_1, 1), xmm_result0, 1);
3008           __ psrldq(xmm_result0, 2);
3009           __ addptr(pos, 2);
3010         __ BIND(L_processTail_1_extr[k]);
3011         __ testptr(len_reg, 1);
3012         __ jcc(Assembler::zero, L_processTail_exit_extr[k]);
3013           __ pextrb(Address(to, pos), xmm_result0, 0);
3014 
3015         __ BIND(L_processTail_exit_extr[k]);
3016         __ movptr(used_addr, used_addr_param);
3017         __ movl(Address(used_addr, 0), len_reg);
3018         __ jmp(L_exit);
3019     }
3020 
3021     __ BIND(L_exit);
3022     __ movdqu(xmm_counter_shuf_mask, ExternalAddress(StubRoutines::x86::counter_shuffle_mask_addr()));
3023     __ pshufb(xmm_curr_counter, xmm_counter_shuf_mask); //counter is shuffled back.
3024     __ movdqu(Address(counter, 0), xmm_curr_counter); //save counter back
3025     handleSOERegisters(false /*restoring*/);
3026     __ movptr(rax, len_param); // return length
3027     __ leave();                // required for proper stackwalking of RuntimeStub frame
3028     __ ret(0);
3029 
3030     __ BIND (L_key192_top);
3031     __ movptr(pos, 0); // init pos before L_multiBlock_loopTop
3032     __ jmp(L_multiBlock_loopTop[1]); //key192
3033 
3034     __ BIND (L_key256_top);
3035     __ movptr(pos, 0); // init pos before L_multiBlock_loopTop
3036     __ jmp(L_multiBlock_loopTop[2]); //key192
3037 
3038     return start;
3039   }
3040 
3041   address generate_upper_word_mask() {
3042     __ align(64);
3043     StubCodeMark mark(this, "StubRoutines", "upper_word_mask");
3044     address start = __ pc();
3045     __ emit_data(0x00000000, relocInfo::none, 0);
3046     __ emit_data(0x00000000, relocInfo::none, 0);
3047     __ emit_data(0x00000000, relocInfo::none, 0);
3048     __ emit_data(0xFFFFFFFF, relocInfo::none, 0);
3049     return start;
3050   }
3051 
3052   address generate_shuffle_byte_flip_mask() {
3053     __ align(64);
3054     StubCodeMark mark(this, "StubRoutines", "shuffle_byte_flip_mask");
3055     address start = __ pc();
3056     __ emit_data(0x0c0d0e0f, relocInfo::none, 0);
3057     __ emit_data(0x08090a0b, relocInfo::none, 0);
3058     __ emit_data(0x04050607, relocInfo::none, 0);
3059     __ emit_data(0x00010203, relocInfo::none, 0);
3060     return start;
3061   }
3062 
3063   // ofs and limit are use for multi-block byte array.
3064   // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
3065   address generate_sha1_implCompress(bool multi_block, const char *name) {
3066     __ align(CodeEntryAlignment);
3067     StubCodeMark mark(this, "StubRoutines", name);
3068     address start = __ pc();
3069 
3070     Register buf   = rax;
3071     Register state = rdx;
3072     Register ofs   = rcx;
3073     Register limit = rdi;
3074 
3075     const Address  buf_param(rbp, 8 + 0);
3076     const Address  state_param(rbp, 8 + 4);
3077     const Address  ofs_param(rbp, 8 + 8);
3078     const Address  limit_param(rbp, 8 + 12);
3079 
3080     const XMMRegister abcd = xmm0;
3081     const XMMRegister e0 = xmm1;
3082     const XMMRegister e1 = xmm2;
3083     const XMMRegister msg0 = xmm3;
3084 
3085     const XMMRegister msg1 = xmm4;
3086     const XMMRegister msg2 = xmm5;
3087     const XMMRegister msg3 = xmm6;
3088     const XMMRegister shuf_mask = xmm7;
3089 
3090     __ enter();
3091     __ subptr(rsp, 8 * wordSize);
3092     if (multi_block) {
3093       __ push(limit);
3094     }
3095     __ movptr(buf, buf_param);
3096     __ movptr(state, state_param);
3097     if (multi_block) {
3098       __ movptr(ofs, ofs_param);
3099       __ movptr(limit, limit_param);
3100     }
3101 
3102     __ fast_sha1(abcd, e0, e1, msg0, msg1, msg2, msg3, shuf_mask,
3103       buf, state, ofs, limit, rsp, multi_block);
3104 
3105     if (multi_block) {
3106       __ pop(limit);
3107     }
3108     __ addptr(rsp, 8 * wordSize);
3109     __ leave();
3110     __ ret(0);
3111     return start;
3112   }
3113 
3114   address generate_pshuffle_byte_flip_mask() {
3115     __ align(64);
3116     StubCodeMark mark(this, "StubRoutines", "pshuffle_byte_flip_mask");
3117     address start = __ pc();
3118     __ emit_data(0x00010203, relocInfo::none, 0);
3119     __ emit_data(0x04050607, relocInfo::none, 0);
3120     __ emit_data(0x08090a0b, relocInfo::none, 0);
3121     __ emit_data(0x0c0d0e0f, relocInfo::none, 0);
3122     return start;
3123   }
3124 
3125   // ofs and limit are use for multi-block byte array.
3126   // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
3127  address generate_sha256_implCompress(bool multi_block, const char *name) {
3128     __ align(CodeEntryAlignment);
3129     StubCodeMark mark(this, "StubRoutines", name);
3130     address start = __ pc();
3131 
3132     Register buf = rbx;
3133     Register state = rsi;
3134     Register ofs = rdx;
3135     Register limit = rcx;
3136 
3137     const Address  buf_param(rbp, 8 + 0);
3138     const Address  state_param(rbp, 8 + 4);
3139     const Address  ofs_param(rbp, 8 + 8);
3140     const Address  limit_param(rbp, 8 + 12);
3141 
3142     const XMMRegister msg = xmm0;
3143     const XMMRegister state0 = xmm1;
3144     const XMMRegister state1 = xmm2;
3145     const XMMRegister msgtmp0 = xmm3;
3146 
3147     const XMMRegister msgtmp1 = xmm4;
3148     const XMMRegister msgtmp2 = xmm5;
3149     const XMMRegister msgtmp3 = xmm6;
3150     const XMMRegister msgtmp4 = xmm7;
3151 
3152     __ enter();
3153     __ subptr(rsp, 8 * wordSize);
3154     handleSOERegisters(true /*saving*/);
3155     __ movptr(buf, buf_param);
3156     __ movptr(state, state_param);
3157     if (multi_block) {
3158      __ movptr(ofs, ofs_param);
3159      __ movptr(limit, limit_param);
3160     }
3161 
3162     __ fast_sha256(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
3163       buf, state, ofs, limit, rsp, multi_block);
3164 
3165     handleSOERegisters(false);
3166     __ addptr(rsp, 8 * wordSize);
3167     __ leave();
3168     __ ret(0);
3169     return start;
3170   }
3171 
3172   // byte swap x86 long
3173   address generate_ghash_long_swap_mask() {
3174     __ align(CodeEntryAlignment);
3175     StubCodeMark mark(this, "StubRoutines", "ghash_long_swap_mask");
3176     address start = __ pc();
3177     __ emit_data(0x0b0a0908, relocInfo::none, 0);
3178     __ emit_data(0x0f0e0d0c, relocInfo::none, 0);
3179     __ emit_data(0x03020100, relocInfo::none, 0);
3180     __ emit_data(0x07060504, relocInfo::none, 0);
3181 
3182   return start;
3183   }
3184 
3185   // byte swap x86 byte array
3186   address generate_ghash_byte_swap_mask() {
3187     __ align(CodeEntryAlignment);
3188     StubCodeMark mark(this, "StubRoutines", "ghash_byte_swap_mask");
3189     address start = __ pc();
3190     __ emit_data(0x0c0d0e0f, relocInfo::none, 0);
3191     __ emit_data(0x08090a0b, relocInfo::none, 0);
3192     __ emit_data(0x04050607, relocInfo::none, 0);
3193     __ emit_data(0x00010203, relocInfo::none, 0);
3194   return start;
3195   }
3196 
3197   /* Single and multi-block ghash operations */
3198   address generate_ghash_processBlocks() {
3199     assert(UseGHASHIntrinsics, "need GHASH intrinsics and CLMUL support");
3200     __ align(CodeEntryAlignment);
3201     Label L_ghash_loop, L_exit;
3202     StubCodeMark mark(this, "StubRoutines", "ghash_processBlocks");
3203     address start = __ pc();
3204 
3205     const Register state        = rdi;
3206     const Register subkeyH      = rsi;
3207     const Register data         = rdx;
3208     const Register blocks       = rcx;
3209 
3210     const Address  state_param(rbp, 8+0);
3211     const Address  subkeyH_param(rbp, 8+4);
3212     const Address  data_param(rbp, 8+8);
3213     const Address  blocks_param(rbp, 8+12);
3214 
3215     const XMMRegister xmm_temp0 = xmm0;
3216     const XMMRegister xmm_temp1 = xmm1;
3217     const XMMRegister xmm_temp2 = xmm2;
3218     const XMMRegister xmm_temp3 = xmm3;
3219     const XMMRegister xmm_temp4 = xmm4;
3220     const XMMRegister xmm_temp5 = xmm5;
3221     const XMMRegister xmm_temp6 = xmm6;
3222     const XMMRegister xmm_temp7 = xmm7;
3223 
3224     __ enter();
3225     handleSOERegisters(true);  // Save registers
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(rdx, 0xffff);
3232       __ kmovdl(k1, rdx);
3233     }
3234 
3235     __ movptr(state, state_param);
3236     __ movptr(subkeyH, subkeyH_param);
3237     __ movptr(data, data_param);
3238     __ movptr(blocks, blocks_param);
3239 
3240     __ movdqu(xmm_temp0, Address(state, 0));
3241     __ pshufb(xmm_temp0, ExternalAddress(StubRoutines::x86::ghash_long_swap_mask_addr()));
3242 
3243     __ movdqu(xmm_temp1, Address(subkeyH, 0));
3244     __ pshufb(xmm_temp1, ExternalAddress(StubRoutines::x86::ghash_long_swap_mask_addr()));
3245 
3246     __ BIND(L_ghash_loop);
3247     __ movdqu(xmm_temp2, Address(data, 0));
3248     __ pshufb(xmm_temp2, ExternalAddress(StubRoutines::x86::ghash_byte_swap_mask_addr()));
3249 
3250     __ pxor(xmm_temp0, xmm_temp2);
3251 
3252     //
3253     // Multiply with the hash key
3254     //
3255     __ movdqu(xmm_temp3, xmm_temp0);
3256     __ pclmulqdq(xmm_temp3, xmm_temp1, 0);      // xmm3 holds a0*b0
3257     __ movdqu(xmm_temp4, xmm_temp0);
3258     __ pclmulqdq(xmm_temp4, xmm_temp1, 16);     // xmm4 holds a0*b1
3259 
3260     __ movdqu(xmm_temp5, xmm_temp0);
3261     __ pclmulqdq(xmm_temp5, xmm_temp1, 1);      // xmm5 holds a1*b0
3262     __ movdqu(xmm_temp6, xmm_temp0);
3263     __ pclmulqdq(xmm_temp6, xmm_temp1, 17);     // xmm6 holds a1*b1
3264 
3265     __ pxor(xmm_temp4, xmm_temp5);      // xmm4 holds a0*b1 + a1*b0
3266 
3267     __ movdqu(xmm_temp5, xmm_temp4);    // move the contents of xmm4 to xmm5
3268     __ psrldq(xmm_temp4, 8);    // shift by xmm4 64 bits to the right
3269     __ pslldq(xmm_temp5, 8);    // shift by xmm5 64 bits to the left
3270     __ pxor(xmm_temp3, xmm_temp5);
3271     __ pxor(xmm_temp6, xmm_temp4);      // Register pair <xmm6:xmm3> holds the result
3272                                         // of the carry-less multiplication of
3273                                         // xmm0 by xmm1.
3274 
3275     // We shift the result of the multiplication by one bit position
3276     // to the left to cope for the fact that the bits are reversed.
3277     __ movdqu(xmm_temp7, xmm_temp3);
3278     __ movdqu(xmm_temp4, xmm_temp6);
3279     __ pslld (xmm_temp3, 1);
3280     __ pslld(xmm_temp6, 1);
3281     __ psrld(xmm_temp7, 31);
3282     __ psrld(xmm_temp4, 31);
3283     __ movdqu(xmm_temp5, xmm_temp7);
3284     __ pslldq(xmm_temp4, 4);
3285     __ pslldq(xmm_temp7, 4);
3286     __ psrldq(xmm_temp5, 12);
3287     __ por(xmm_temp3, xmm_temp7);
3288     __ por(xmm_temp6, xmm_temp4);
3289     __ por(xmm_temp6, xmm_temp5);
3290 
3291     //
3292     // First phase of the reduction
3293     //
3294     // Move xmm3 into xmm4, xmm5, xmm7 in order to perform the shifts
3295     // independently.
3296     __ movdqu(xmm_temp7, xmm_temp3);
3297     __ movdqu(xmm_temp4, xmm_temp3);
3298     __ movdqu(xmm_temp5, xmm_temp3);
3299     __ pslld(xmm_temp7, 31);    // packed right shift shifting << 31
3300     __ pslld(xmm_temp4, 30);    // packed right shift shifting << 30
3301     __ pslld(xmm_temp5, 25);    // packed right shift shifting << 25
3302     __ pxor(xmm_temp7, xmm_temp4);      // xor the shifted versions
3303     __ pxor(xmm_temp7, xmm_temp5);
3304     __ movdqu(xmm_temp4, xmm_temp7);
3305     __ pslldq(xmm_temp7, 12);
3306     __ psrldq(xmm_temp4, 4);
3307     __ pxor(xmm_temp3, xmm_temp7);      // first phase of the reduction complete
3308 
3309     //
3310     // Second phase of the reduction
3311     //
3312     // Make 3 copies of xmm3 in xmm2, xmm5, xmm7 for doing these
3313     // shift operations.
3314     __ movdqu(xmm_temp2, xmm_temp3);
3315     __ movdqu(xmm_temp7, xmm_temp3);
3316     __ movdqu(xmm_temp5, xmm_temp3);
3317     __ psrld(xmm_temp2, 1);     // packed left shifting >> 1
3318     __ psrld(xmm_temp7, 2);     // packed left shifting >> 2
3319     __ psrld(xmm_temp5, 7);     // packed left shifting >> 7
3320     __ pxor(xmm_temp2, xmm_temp7);      // xor the shifted versions
3321     __ pxor(xmm_temp2, xmm_temp5);
3322     __ pxor(xmm_temp2, xmm_temp4);
3323     __ pxor(xmm_temp3, xmm_temp2);
3324     __ pxor(xmm_temp6, xmm_temp3);      // the result is in xmm6
3325 
3326     __ decrement(blocks);
3327     __ jcc(Assembler::zero, L_exit);
3328     __ movdqu(xmm_temp0, xmm_temp6);
3329     __ addptr(data, 16);
3330     __ jmp(L_ghash_loop);
3331 
3332     __ BIND(L_exit);
3333        // Byte swap 16-byte result
3334     __ pshufb(xmm_temp6, ExternalAddress(StubRoutines::x86::ghash_long_swap_mask_addr()));
3335     __ movdqu(Address(state, 0), xmm_temp6);   // store the result
3336 
3337     handleSOERegisters(false);  // restore registers
3338     __ leave();
3339     __ ret(0);
3340     return start;
3341   }
3342 
3343   /**
3344    *  Arguments:
3345    *
3346    * Inputs:
3347    *   rsp(4)   - int crc
3348    *   rsp(8)   - byte* buf
3349    *   rsp(12)  - int length
3350    *
3351    * Ouput:
3352    *       rax   - int crc result
3353    */
3354   address generate_updateBytesCRC32() {
3355     assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions");
3356 
3357     __ align(CodeEntryAlignment);
3358     StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32");
3359 
3360     address start = __ pc();
3361 
3362     const Register crc   = rdx;  // crc
3363     const Register buf   = rsi;  // source java byte array address
3364     const Register len   = rcx;  // length
3365     const Register table = rdi;  // crc_table address (reuse register)
3366     const Register tmp   = rbx;
3367     assert_different_registers(crc, buf, len, table, tmp, rax);
3368 
3369     BLOCK_COMMENT("Entry:");
3370     __ enter(); // required for proper stackwalking of RuntimeStub frame
3371     __ push(rsi);
3372     __ push(rdi);
3373     __ push(rbx);
3374 
3375     Address crc_arg(rbp, 8 + 0);
3376     Address buf_arg(rbp, 8 + 4);
3377     Address len_arg(rbp, 8 + 8);
3378 
3379     // Load up:
3380     __ movl(crc,   crc_arg);
3381     __ movptr(buf, buf_arg);
3382     __ movl(len,   len_arg);
3383 
3384     __ kernel_crc32(crc, buf, len, table, tmp);
3385 
3386     __ movl(rax, crc);
3387     __ pop(rbx);
3388     __ pop(rdi);
3389     __ pop(rsi);
3390     __ vzeroupper();
3391     __ leave(); // required for proper stackwalking of RuntimeStub frame
3392     __ ret(0);
3393 
3394     return start;
3395   }
3396 
3397   /**
3398   *  Arguments:
3399   *
3400   * Inputs:
3401   *   rsp(4)   - int crc
3402   *   rsp(8)   - byte* buf
3403   *   rsp(12)  - int length
3404   *   rsp(16)  - table_start - optional (present only when doing a library_calll,
3405   *              not used by x86 algorithm)
3406   *
3407   * Ouput:
3408   *       rax  - int crc result
3409   */
3410   address generate_updateBytesCRC32C(bool is_pclmulqdq_supported) {
3411     assert(UseCRC32CIntrinsics, "need SSE4_2");
3412     __ align(CodeEntryAlignment);
3413     StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32C");
3414     address start = __ pc();
3415     const Register crc = rax;  // crc
3416     const Register buf = rcx;  // source java byte array address
3417     const Register len = rdx;  // length
3418     const Register d = rbx;
3419     const Register g = rsi;
3420     const Register h = rdi;
3421     const Register empty = 0; // will never be used, in order not
3422                               // to change a signature for crc32c_IPL_Alg2_Alt2
3423                               // between 64/32 I'm just keeping it here
3424     assert_different_registers(crc, buf, len, d, g, h);
3425 
3426     BLOCK_COMMENT("Entry:");
3427     __ enter(); // required for proper stackwalking of RuntimeStub frame
3428     Address crc_arg(rsp, 4 + 4 + 0); // ESP+4 +
3429                                      // we need to add additional 4 because __ enter
3430                                      // have just pushed ebp on a stack
3431     Address buf_arg(rsp, 4 + 4 + 4);
3432     Address len_arg(rsp, 4 + 4 + 8);
3433       // Load up:
3434       __ movl(crc, crc_arg);
3435       __ movl(buf, buf_arg);
3436       __ movl(len, len_arg);
3437       __ push(d);
3438       __ push(g);
3439       __ push(h);
3440       __ crc32c_ipl_alg2_alt2(crc, buf, len,
3441                               d, g, h,
3442                               empty, empty, empty,
3443                               xmm0, xmm1, xmm2,
3444                               is_pclmulqdq_supported);
3445       __ pop(h);
3446       __ pop(g);
3447       __ pop(d);
3448     __ vzeroupper();
3449     __ leave(); // required for proper stackwalking of RuntimeStub frame
3450     __ ret(0);
3451 
3452     return start;
3453   }
3454 
3455  address generate_libmExp() {
3456     StubCodeMark mark(this, "StubRoutines", "libmExp");
3457 
3458     address start = __ pc();
3459 
3460     const XMMRegister x0  = xmm0;
3461     const XMMRegister x1  = xmm1;
3462     const XMMRegister x2  = xmm2;
3463     const XMMRegister x3  = xmm3;
3464 
3465     const XMMRegister x4  = xmm4;
3466     const XMMRegister x5  = xmm5;
3467     const XMMRegister x6  = xmm6;
3468     const XMMRegister x7  = xmm7;
3469 
3470     const Register tmp   = rbx;
3471 
3472     BLOCK_COMMENT("Entry:");
3473     __ enter(); // required for proper stackwalking of RuntimeStub frame
3474     __ fast_exp(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
3475     __ leave(); // required for proper stackwalking of RuntimeStub frame
3476     __ ret(0);
3477 
3478     return start;
3479 
3480   }
3481 
3482  address generate_libmLog() {
3483    StubCodeMark mark(this, "StubRoutines", "libmLog");
3484 
3485    address start = __ pc();
3486 
3487    const XMMRegister x0 = xmm0;
3488    const XMMRegister x1 = xmm1;
3489    const XMMRegister x2 = xmm2;
3490    const XMMRegister x3 = xmm3;
3491 
3492    const XMMRegister x4 = xmm4;
3493    const XMMRegister x5 = xmm5;
3494    const XMMRegister x6 = xmm6;
3495    const XMMRegister x7 = xmm7;
3496 
3497    const Register tmp = rbx;
3498 
3499    BLOCK_COMMENT("Entry:");
3500    __ enter(); // required for proper stackwalking of RuntimeStub frame
3501    __ fast_log(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
3502    __ leave(); // required for proper stackwalking of RuntimeStub frame
3503    __ ret(0);
3504 
3505    return start;
3506 
3507  }
3508 
3509  address generate_libmLog10() {
3510    StubCodeMark mark(this, "StubRoutines", "libmLog10");
3511 
3512    address start = __ pc();
3513 
3514    const XMMRegister x0 = xmm0;
3515    const XMMRegister x1 = xmm1;
3516    const XMMRegister x2 = xmm2;
3517    const XMMRegister x3 = xmm3;
3518 
3519    const XMMRegister x4 = xmm4;
3520    const XMMRegister x5 = xmm5;
3521    const XMMRegister x6 = xmm6;
3522    const XMMRegister x7 = xmm7;
3523 
3524    const Register tmp = rbx;
3525 
3526    BLOCK_COMMENT("Entry:");
3527    __ enter(); // required for proper stackwalking of RuntimeStub frame
3528    __ fast_log10(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
3529    __ leave(); // required for proper stackwalking of RuntimeStub frame
3530    __ ret(0);
3531 
3532    return start;
3533 
3534  }
3535 
3536  address generate_libmPow() {
3537    StubCodeMark mark(this, "StubRoutines", "libmPow");
3538 
3539    address start = __ pc();
3540 
3541    const XMMRegister x0 = xmm0;
3542    const XMMRegister x1 = xmm1;
3543    const XMMRegister x2 = xmm2;
3544    const XMMRegister x3 = xmm3;
3545 
3546    const XMMRegister x4 = xmm4;
3547    const XMMRegister x5 = xmm5;
3548    const XMMRegister x6 = xmm6;
3549    const XMMRegister x7 = xmm7;
3550 
3551    const Register tmp = rbx;
3552 
3553    BLOCK_COMMENT("Entry:");
3554    __ enter(); // required for proper stackwalking of RuntimeStub frame
3555    __ fast_pow(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
3556    __ leave(); // required for proper stackwalking of RuntimeStub frame
3557    __ ret(0);
3558 
3559    return start;
3560 
3561  }
3562 
3563  address generate_libm_reduce_pi04l() {
3564    StubCodeMark mark(this, "StubRoutines", "libm_reduce_pi04l");
3565 
3566    address start = __ pc();
3567 
3568    BLOCK_COMMENT("Entry:");
3569    __ libm_reduce_pi04l(rax, rcx, rdx, rbx, rsi, rdi, rbp, rsp);
3570 
3571    return start;
3572 
3573  }
3574 
3575  address generate_libm_sin_cos_huge() {
3576    StubCodeMark mark(this, "StubRoutines", "libm_sin_cos_huge");
3577 
3578    address start = __ pc();
3579 
3580    const XMMRegister x0 = xmm0;
3581    const XMMRegister x1 = xmm1;
3582 
3583    BLOCK_COMMENT("Entry:");
3584    __ libm_sincos_huge(x0, x1, rax, rcx, rdx, rbx, rsi, rdi, rbp, rsp);
3585 
3586    return start;
3587 
3588  }
3589 
3590  address generate_libmSin() {
3591    StubCodeMark mark(this, "StubRoutines", "libmSin");
3592 
3593    address start = __ pc();
3594 
3595    const XMMRegister x0 = xmm0;
3596    const XMMRegister x1 = xmm1;
3597    const XMMRegister x2 = xmm2;
3598    const XMMRegister x3 = xmm3;
3599 
3600    const XMMRegister x4 = xmm4;
3601    const XMMRegister x5 = xmm5;
3602    const XMMRegister x6 = xmm6;
3603    const XMMRegister x7 = xmm7;
3604 
3605    BLOCK_COMMENT("Entry:");
3606    __ enter(); // required for proper stackwalking of RuntimeStub frame
3607    __ fast_sin(x0, x1, x2, x3, x4, x5, x6, x7, rax, rbx, rdx);
3608    __ leave(); // required for proper stackwalking of RuntimeStub frame
3609    __ ret(0);
3610 
3611    return start;
3612 
3613  }
3614 
3615  address generate_libmCos() {
3616    StubCodeMark mark(this, "StubRoutines", "libmCos");
3617 
3618    address start = __ pc();
3619 
3620    const XMMRegister x0 = xmm0;
3621    const XMMRegister x1 = xmm1;
3622    const XMMRegister x2 = xmm2;
3623    const XMMRegister x3 = xmm3;
3624 
3625    const XMMRegister x4 = xmm4;
3626    const XMMRegister x5 = xmm5;
3627    const XMMRegister x6 = xmm6;
3628    const XMMRegister x7 = xmm7;
3629 
3630    const Register tmp = rbx;
3631 
3632    BLOCK_COMMENT("Entry:");
3633    __ enter(); // required for proper stackwalking of RuntimeStub frame
3634    __ fast_cos(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
3635    __ leave(); // required for proper stackwalking of RuntimeStub frame
3636    __ ret(0);
3637 
3638    return start;
3639 
3640  }
3641 
3642  address generate_libm_tan_cot_huge() {
3643    StubCodeMark mark(this, "StubRoutines", "libm_tan_cot_huge");
3644 
3645    address start = __ pc();
3646 
3647    const XMMRegister x0 = xmm0;
3648    const XMMRegister x1 = xmm1;
3649 
3650    BLOCK_COMMENT("Entry:");
3651    __ libm_tancot_huge(x0, x1, rax, rcx, rdx, rbx, rsi, rdi, rbp, rsp);
3652 
3653    return start;
3654 
3655  }
3656 
3657  address generate_libmTan() {
3658    StubCodeMark mark(this, "StubRoutines", "libmTan");
3659 
3660    address start = __ pc();
3661 
3662    const XMMRegister x0 = xmm0;
3663    const XMMRegister x1 = xmm1;
3664    const XMMRegister x2 = xmm2;
3665    const XMMRegister x3 = xmm3;
3666 
3667    const XMMRegister x4 = xmm4;
3668    const XMMRegister x5 = xmm5;
3669    const XMMRegister x6 = xmm6;
3670    const XMMRegister x7 = xmm7;
3671 
3672    const Register tmp = rbx;
3673 
3674    BLOCK_COMMENT("Entry:");
3675    __ enter(); // required for proper stackwalking of RuntimeStub frame
3676    __ fast_tan(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
3677    __ leave(); // required for proper stackwalking of RuntimeStub frame
3678    __ ret(0);
3679 
3680    return start;
3681 
3682  }
3683 
3684   // Safefetch stubs.
3685   void generate_safefetch(const char* name, int size, address* entry,
3686                           address* fault_pc, address* continuation_pc) {
3687     // safefetch signatures:
3688     //   int      SafeFetch32(int*      adr, int      errValue);
3689     //   intptr_t SafeFetchN (intptr_t* adr, intptr_t errValue);
3690 
3691     StubCodeMark mark(this, "StubRoutines", name);
3692 
3693     // Entry point, pc or function descriptor.
3694     *entry = __ pc();
3695 
3696     __ movl(rax, Address(rsp, 0x8));
3697     __ movl(rcx, Address(rsp, 0x4));
3698     // Load *adr into eax, may fault.
3699     *fault_pc = __ pc();
3700     switch (size) {
3701       case 4:
3702         // int32_t
3703         __ movl(rax, Address(rcx, 0));
3704         break;
3705       case 8:
3706         // int64_t
3707         Unimplemented();
3708         break;
3709       default:
3710         ShouldNotReachHere();
3711     }
3712 
3713     // Return errValue or *adr.
3714     *continuation_pc = __ pc();
3715     __ ret(0);
3716   }
3717 
3718  public:
3719   // Information about frame layout at time of blocking runtime call.
3720   // Note that we only have to preserve callee-saved registers since
3721   // the compilers are responsible for supplying a continuation point
3722   // if they expect all registers to be preserved.
3723   enum layout {
3724     thread_off,    // last_java_sp
3725     arg1_off,
3726     arg2_off,
3727     rbp_off,       // callee saved register
3728     ret_pc,
3729     framesize
3730   };
3731 
3732  private:
3733 
3734 #undef  __
3735 #define __ masm->
3736 
3737   //------------------------------------------------------------------------------------------------------------------------
3738   // Continuation point for throwing of implicit exceptions that are not handled in
3739   // the current activation. Fabricates an exception oop and initiates normal
3740   // exception dispatching in this frame.
3741   //
3742   // Previously the compiler (c2) allowed for callee save registers on Java calls.
3743   // This is no longer true after adapter frames were removed but could possibly
3744   // be brought back in the future if the interpreter code was reworked and it
3745   // was deemed worthwhile. The comment below was left to describe what must
3746   // happen here if callee saves were resurrected. As it stands now this stub
3747   // could actually be a vanilla BufferBlob and have now oopMap at all.
3748   // Since it doesn't make much difference we've chosen to leave it the
3749   // way it was in the callee save days and keep the comment.
3750 
3751   // If we need to preserve callee-saved values we need a callee-saved oop map and
3752   // therefore have to make these stubs into RuntimeStubs rather than BufferBlobs.
3753   // If the compiler needs all registers to be preserved between the fault
3754   // point and the exception handler then it must assume responsibility for that in
3755   // AbstractCompiler::continuation_for_implicit_null_exception or
3756   // continuation_for_implicit_division_by_zero_exception. All other implicit
3757   // exceptions (e.g., NullPointerException or AbstractMethodError on entry) are
3758   // either at call sites or otherwise assume that stack unwinding will be initiated,
3759   // so caller saved registers were assumed volatile in the compiler.
3760   address generate_throw_exception(const char* name, address runtime_entry,
3761                                    Register arg1 = noreg, Register arg2 = noreg) {
3762 
3763     int insts_size = 256;
3764     int locs_size  = 32;
3765 
3766     CodeBuffer code(name, insts_size, locs_size);
3767     OopMapSet* oop_maps  = new OopMapSet();
3768     MacroAssembler* masm = new MacroAssembler(&code);
3769 
3770     address start = __ pc();
3771 
3772     // This is an inlined and slightly modified version of call_VM
3773     // which has the ability to fetch the return PC out of
3774     // thread-local storage and also sets up last_Java_sp slightly
3775     // differently than the real call_VM
3776     Register java_thread = rbx;
3777     __ get_thread(java_thread);
3778 
3779     __ enter(); // required for proper stackwalking of RuntimeStub frame
3780 
3781     // pc and rbp, already pushed
3782     __ subptr(rsp, (framesize-2) * wordSize); // prolog
3783 
3784     // Frame is now completed as far as size and linkage.
3785 
3786     int frame_complete = __ pc() - start;
3787 
3788     // push java thread (becomes first argument of C function)
3789     __ movptr(Address(rsp, thread_off * wordSize), java_thread);
3790     if (arg1 != noreg) {
3791       __ movptr(Address(rsp, arg1_off * wordSize), arg1);
3792     }
3793     if (arg2 != noreg) {
3794       assert(arg1 != noreg, "missing reg arg");
3795       __ movptr(Address(rsp, arg2_off * wordSize), arg2);
3796     }
3797 
3798     // Set up last_Java_sp and last_Java_fp
3799     __ set_last_Java_frame(java_thread, rsp, rbp, NULL);
3800 
3801     // Call runtime
3802     BLOCK_COMMENT("call runtime_entry");
3803     __ call(RuntimeAddress(runtime_entry));
3804     // Generate oop map
3805     OopMap* map =  new OopMap(framesize, 0);
3806     oop_maps->add_gc_map(__ pc() - start, map);
3807 
3808     // restore the thread (cannot use the pushed argument since arguments
3809     // may be overwritten by C code generated by an optimizing compiler);
3810     // however can use the register value directly if it is callee saved.
3811     __ get_thread(java_thread);
3812 
3813     __ reset_last_Java_frame(java_thread, true);
3814 
3815     __ leave(); // required for proper stackwalking of RuntimeStub frame
3816 
3817     // check for pending exceptions
3818 #ifdef ASSERT
3819     Label L;
3820     __ cmpptr(Address(java_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
3821     __ jcc(Assembler::notEqual, L);
3822     __ should_not_reach_here();
3823     __ bind(L);
3824 #endif /* ASSERT */
3825     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
3826 
3827 
3828     RuntimeStub* stub = RuntimeStub::new_runtime_stub(name, &code, frame_complete, framesize, oop_maps, false);
3829     return stub->entry_point();
3830   }
3831 
3832 
3833   void create_control_words() {
3834     // Round to nearest, 53-bit mode, exceptions masked
3835     StubRoutines::_fpu_cntrl_wrd_std   = 0x027F;
3836     // Round to zero, 53-bit mode, exception mased
3837     StubRoutines::_fpu_cntrl_wrd_trunc = 0x0D7F;
3838     // Round to nearest, 24-bit mode, exceptions masked
3839     StubRoutines::_fpu_cntrl_wrd_24    = 0x007F;
3840     // Round to nearest, 64-bit mode, exceptions masked
3841     StubRoutines::_fpu_cntrl_wrd_64    = 0x037F;
3842     // Round to nearest, 64-bit mode, exceptions masked
3843     StubRoutines::_mxcsr_std           = 0x1F80;
3844     // Note: the following two constants are 80-bit values
3845     //       layout is critical for correct loading by FPU.
3846     // Bias for strict fp multiply/divide
3847     StubRoutines::_fpu_subnormal_bias1[0]= 0x00000000; // 2^(-15360) == 0x03ff 8000 0000 0000 0000
3848     StubRoutines::_fpu_subnormal_bias1[1]= 0x80000000;
3849     StubRoutines::_fpu_subnormal_bias1[2]= 0x03ff;
3850     // Un-Bias for strict fp multiply/divide
3851     StubRoutines::_fpu_subnormal_bias2[0]= 0x00000000; // 2^(+15360) == 0x7bff 8000 0000 0000 0000
3852     StubRoutines::_fpu_subnormal_bias2[1]= 0x80000000;
3853     StubRoutines::_fpu_subnormal_bias2[2]= 0x7bff;
3854   }
3855 
3856   //---------------------------------------------------------------------------
3857   // Initialization
3858 
3859   void generate_initial() {
3860     // Generates all stubs and initializes the entry points
3861 
3862     //------------------------------------------------------------------------------------------------------------------------
3863     // entry points that exist in all platforms
3864     // Note: This is code that could be shared among different platforms - however the benefit seems to be smaller than
3865     //       the disadvantage of having a much more complicated generator structure. See also comment in stubRoutines.hpp.
3866     StubRoutines::_forward_exception_entry      = generate_forward_exception();
3867 
3868     StubRoutines::_call_stub_entry              =
3869       generate_call_stub(StubRoutines::_call_stub_return_address);
3870     // is referenced by megamorphic call
3871     StubRoutines::_catch_exception_entry        = generate_catch_exception();
3872 
3873     // These are currently used by Solaris/Intel
3874     StubRoutines::_atomic_xchg_entry            = generate_atomic_xchg();
3875 
3876     // platform dependent
3877     create_control_words();
3878 
3879     StubRoutines::x86::_verify_mxcsr_entry                 = generate_verify_mxcsr();
3880     StubRoutines::x86::_verify_fpu_cntrl_wrd_entry         = generate_verify_fpu_cntrl_wrd();
3881     StubRoutines::_d2i_wrapper                              = generate_d2i_wrapper(T_INT,
3882                                                                                    CAST_FROM_FN_PTR(address, SharedRuntime::d2i));
3883     StubRoutines::_d2l_wrapper                              = generate_d2i_wrapper(T_LONG,
3884                                                                                    CAST_FROM_FN_PTR(address, SharedRuntime::d2l));
3885 
3886     // Build this early so it's available for the interpreter
3887     StubRoutines::_throw_StackOverflowError_entry          = generate_throw_exception("StackOverflowError throw_exception",
3888                                                                                       CAST_FROM_FN_PTR(address, SharedRuntime::throw_StackOverflowError));
3889     StubRoutines::_throw_delayed_StackOverflowError_entry  = generate_throw_exception("delayed StackOverflowError throw_exception",
3890                                                                                       CAST_FROM_FN_PTR(address, SharedRuntime::throw_delayed_StackOverflowError));
3891 
3892     if (UseCRC32Intrinsics) {
3893       // set table address before stub generation which use it
3894       StubRoutines::_crc_table_adr = (address)StubRoutines::x86::_crc_table;
3895       StubRoutines::_updateBytesCRC32 = generate_updateBytesCRC32();
3896     }
3897 
3898     if (UseCRC32CIntrinsics) {
3899       bool supports_clmul = VM_Version::supports_clmul();
3900       StubRoutines::x86::generate_CRC32C_table(supports_clmul);
3901       StubRoutines::_crc32c_table_addr = (address)StubRoutines::x86::_crc32c_table;
3902       StubRoutines::_updateBytesCRC32C = generate_updateBytesCRC32C(supports_clmul);
3903     }
3904     if (VM_Version::supports_sse2() && UseLibmIntrinsic && InlineIntrinsics) {
3905       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin) ||
3906           vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos) ||
3907           vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtan)) {
3908         StubRoutines::x86::_L_2il0floatpacket_0_adr = (address)StubRoutines::x86::_L_2il0floatpacket_0;
3909         StubRoutines::x86::_Pi4Inv_adr = (address)StubRoutines::x86::_Pi4Inv;
3910         StubRoutines::x86::_Pi4x3_adr = (address)StubRoutines::x86::_Pi4x3;
3911         StubRoutines::x86::_Pi4x4_adr = (address)StubRoutines::x86::_Pi4x4;
3912         StubRoutines::x86::_ones_adr = (address)StubRoutines::x86::_ones;
3913       }
3914       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dexp)) {
3915         StubRoutines::_dexp = generate_libmExp();
3916       }
3917       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog)) {
3918         StubRoutines::_dlog = generate_libmLog();
3919       }
3920       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog10)) {
3921         StubRoutines::_dlog10 = generate_libmLog10();
3922       }
3923       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dpow)) {
3924         StubRoutines::_dpow = generate_libmPow();
3925       }
3926       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin) ||
3927         vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos) ||
3928         vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtan)) {
3929         StubRoutines::_dlibm_reduce_pi04l = generate_libm_reduce_pi04l();
3930       }
3931       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin) ||
3932         vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos)) {
3933         StubRoutines::_dlibm_sin_cos_huge = generate_libm_sin_cos_huge();
3934       }
3935       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin)) {
3936         StubRoutines::_dsin = generate_libmSin();
3937       }
3938       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos)) {
3939         StubRoutines::_dcos = generate_libmCos();
3940       }
3941       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtan)) {
3942         StubRoutines::_dlibm_tan_cot_huge = generate_libm_tan_cot_huge();
3943         StubRoutines::_dtan = generate_libmTan();
3944       }
3945     }
3946   }
3947 
3948   void generate_all() {
3949     // Generates all stubs and initializes the entry points
3950 
3951     // These entry points require SharedInfo::stack0 to be set up in non-core builds
3952     // and need to be relocatable, so they each fabricate a RuntimeStub internally.
3953     StubRoutines::_throw_AbstractMethodError_entry         = generate_throw_exception("AbstractMethodError throw_exception",          CAST_FROM_FN_PTR(address, SharedRuntime::throw_AbstractMethodError));
3954     StubRoutines::_throw_IncompatibleClassChangeError_entry= generate_throw_exception("IncompatibleClassChangeError throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_IncompatibleClassChangeError));
3955     StubRoutines::_throw_NullPointerException_at_call_entry= generate_throw_exception("NullPointerException at call throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_NullPointerException_at_call));
3956 
3957     //------------------------------------------------------------------------------------------------------------------------
3958     // entry points that are platform specific
3959 
3960     // support for verify_oop (must happen after universe_init)
3961     StubRoutines::_verify_oop_subroutine_entry     = generate_verify_oop();
3962 
3963     // arraycopy stubs used by compilers
3964     generate_arraycopy_stubs();
3965 
3966     // don't bother generating these AES intrinsic stubs unless global flag is set
3967     if (UseAESIntrinsics) {
3968       StubRoutines::x86::_key_shuffle_mask_addr = generate_key_shuffle_mask();  // might be needed by the others
3969 
3970       StubRoutines::_aescrypt_encryptBlock = generate_aescrypt_encryptBlock();
3971       StubRoutines::_aescrypt_decryptBlock = generate_aescrypt_decryptBlock();
3972       StubRoutines::_cipherBlockChaining_encryptAESCrypt = generate_cipherBlockChaining_encryptAESCrypt();
3973       StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptAESCrypt_Parallel();
3974     }
3975 
3976     if (UseAESCTRIntrinsics) {
3977       StubRoutines::x86::_counter_shuffle_mask_addr = generate_counter_shuffle_mask();
3978       StubRoutines::_counterMode_AESCrypt = generate_counterMode_AESCrypt_Parallel();
3979     }
3980 
3981     if (UseSHA1Intrinsics) {
3982       StubRoutines::x86::_upper_word_mask_addr = generate_upper_word_mask();
3983       StubRoutines::x86::_shuffle_byte_flip_mask_addr = generate_shuffle_byte_flip_mask();
3984       StubRoutines::_sha1_implCompress = generate_sha1_implCompress(false, "sha1_implCompress");
3985       StubRoutines::_sha1_implCompressMB = generate_sha1_implCompress(true, "sha1_implCompressMB");
3986     }
3987     if (UseSHA256Intrinsics) {
3988       StubRoutines::x86::_k256_adr = (address)StubRoutines::x86::_k256;
3989       StubRoutines::x86::_pshuffle_byte_flip_mask_addr = generate_pshuffle_byte_flip_mask();
3990       StubRoutines::_sha256_implCompress = generate_sha256_implCompress(false, "sha256_implCompress");
3991       StubRoutines::_sha256_implCompressMB = generate_sha256_implCompress(true, "sha256_implCompressMB");
3992     }
3993 
3994     // Generate GHASH intrinsics code
3995     if (UseGHASHIntrinsics) {
3996       StubRoutines::x86::_ghash_long_swap_mask_addr = generate_ghash_long_swap_mask();
3997       StubRoutines::x86::_ghash_byte_swap_mask_addr = generate_ghash_byte_swap_mask();
3998       StubRoutines::_ghash_processBlocks = generate_ghash_processBlocks();
3999     }
4000 
4001     // Safefetch stubs.
4002     generate_safefetch("SafeFetch32", sizeof(int), &StubRoutines::_safefetch32_entry,
4003                                                    &StubRoutines::_safefetch32_fault_pc,
4004                                                    &StubRoutines::_safefetch32_continuation_pc);
4005     StubRoutines::_safefetchN_entry           = StubRoutines::_safefetch32_entry;
4006     StubRoutines::_safefetchN_fault_pc        = StubRoutines::_safefetch32_fault_pc;
4007     StubRoutines::_safefetchN_continuation_pc = StubRoutines::_safefetch32_continuation_pc;
4008   }
4009 
4010 
4011  public:
4012   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
4013     if (all) {
4014       generate_all();
4015     } else {
4016       generate_initial();
4017     }
4018   }
4019 }; // end class declaration
4020 
4021 
4022 void StubGenerator_generate(CodeBuffer* code, bool all) {
4023   StubGenerator g(code, all);
4024 }