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