src/cpu/x86/vm/stubGenerator_x86_32.cpp

Fri, 16 Aug 2019 16:50:17 +0200

author
eosterlund
date
Fri, 16 Aug 2019 16:50:17 +0200
changeset 9834
bb1da64b0492
parent 9789
e55d4d896e30
child 9806
758c07667682
permissions
-rw-r--r--

8229345: Memory leak due to vtable stubs not being shared on SPARC
Reviewed-by: mdoerr, dholmes, kvn

duke@435 1 /*
drchase@5353 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
twisti@4318 26 #include "asm/macroAssembler.hpp"
twisti@4318 27 #include "asm/macroAssembler.inline.hpp"
stefank@2314 28 #include "interpreter/interpreter.hpp"
stefank@2314 29 #include "nativeInst_x86.hpp"
stefank@2314 30 #include "oops/instanceOop.hpp"
coleenp@4037 31 #include "oops/method.hpp"
stefank@2314 32 #include "oops/objArrayKlass.hpp"
stefank@2314 33 #include "oops/oop.inline.hpp"
stefank@2314 34 #include "prims/methodHandles.hpp"
stefank@2314 35 #include "runtime/frame.inline.hpp"
stefank@2314 36 #include "runtime/handles.inline.hpp"
stefank@2314 37 #include "runtime/sharedRuntime.hpp"
stefank@2314 38 #include "runtime/stubCodeGenerator.hpp"
stefank@2314 39 #include "runtime/stubRoutines.hpp"
stefank@4299 40 #include "runtime/thread.inline.hpp"
stefank@2314 41 #include "utilities/top.hpp"
stefank@2314 42 #ifdef COMPILER2
stefank@2314 43 #include "opto/runtime.hpp"
stefank@2314 44 #endif
duke@435 45
duke@435 46 // Declaration and definition of StubGenerator (no .hpp file).
duke@435 47 // For a more detailed description of the stub routine structure
duke@435 48 // see the comment in stubRoutines.hpp
duke@435 49
duke@435 50 #define __ _masm->
never@739 51 #define a__ ((Assembler*)_masm)->
duke@435 52
duke@435 53 #ifdef PRODUCT
duke@435 54 #define BLOCK_COMMENT(str) /* nothing */
duke@435 55 #else
duke@435 56 #define BLOCK_COMMENT(str) __ block_comment(str)
duke@435 57 #endif
duke@435 58
duke@435 59 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
duke@435 60
duke@435 61 const int MXCSR_MASK = 0xFFC0; // Mask out any pending exceptions
duke@435 62 const int FPU_CNTRL_WRD_MASK = 0xFFFF;
duke@435 63
duke@435 64 // -------------------------------------------------------------------------------------------------------------------------
duke@435 65 // Stub Code definitions
duke@435 66
duke@435 67 static address handle_unsafe_access() {
duke@435 68 JavaThread* thread = JavaThread::current();
duke@435 69 address pc = thread->saved_exception_pc();
duke@435 70 // pc is the instruction which we must emulate
duke@435 71 // doing a no-op is fine: return garbage from the load
duke@435 72 // therefore, compute npc
duke@435 73 address npc = Assembler::locate_next_instruction(pc);
duke@435 74
duke@435 75 // request an async exception
duke@435 76 thread->set_pending_unsafe_access_error();
duke@435 77
duke@435 78 // return address of next instruction to execute
duke@435 79 return npc;
duke@435 80 }
duke@435 81
duke@435 82 class StubGenerator: public StubCodeGenerator {
duke@435 83 private:
duke@435 84
duke@435 85 #ifdef PRODUCT
ccheung@5259 86 #define inc_counter_np(counter) ((void)0)
duke@435 87 #else
duke@435 88 void inc_counter_np_(int& counter) {
never@739 89 __ incrementl(ExternalAddress((address)&counter));
duke@435 90 }
duke@435 91 #define inc_counter_np(counter) \
duke@435 92 BLOCK_COMMENT("inc_counter " #counter); \
duke@435 93 inc_counter_np_(counter);
duke@435 94 #endif //PRODUCT
duke@435 95
duke@435 96 void inc_copy_counter_np(BasicType t) {
duke@435 97 #ifndef PRODUCT
duke@435 98 switch (t) {
duke@435 99 case T_BYTE: inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); return;
duke@435 100 case T_SHORT: inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); return;
duke@435 101 case T_INT: inc_counter_np(SharedRuntime::_jint_array_copy_ctr); return;
duke@435 102 case T_LONG: inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); return;
duke@435 103 case T_OBJECT: inc_counter_np(SharedRuntime::_oop_array_copy_ctr); return;
duke@435 104 }
duke@435 105 ShouldNotReachHere();
duke@435 106 #endif //PRODUCT
duke@435 107 }
duke@435 108
duke@435 109 //------------------------------------------------------------------------------------------------------------------------
duke@435 110 // Call stubs are used to call Java from C
duke@435 111 //
duke@435 112 // [ return_from_Java ] <--- rsp
duke@435 113 // [ argument word n ]
duke@435 114 // ...
duke@435 115 // -N [ argument word 1 ]
duke@435 116 // -7 [ Possible padding for stack alignment ]
duke@435 117 // -6 [ Possible padding for stack alignment ]
duke@435 118 // -5 [ Possible padding for stack alignment ]
duke@435 119 // -4 [ mxcsr save ] <--- rsp_after_call
duke@435 120 // -3 [ saved rbx, ]
duke@435 121 // -2 [ saved rsi ]
duke@435 122 // -1 [ saved rdi ]
duke@435 123 // 0 [ saved rbp, ] <--- rbp,
duke@435 124 // 1 [ return address ]
duke@435 125 // 2 [ ptr. to call wrapper ]
duke@435 126 // 3 [ result ]
duke@435 127 // 4 [ result_type ]
duke@435 128 // 5 [ method ]
duke@435 129 // 6 [ entry_point ]
duke@435 130 // 7 [ parameters ]
duke@435 131 // 8 [ parameter_size ]
duke@435 132 // 9 [ thread ]
duke@435 133
duke@435 134
duke@435 135 address generate_call_stub(address& return_address) {
duke@435 136 StubCodeMark mark(this, "StubRoutines", "call_stub");
duke@435 137 address start = __ pc();
duke@435 138
duke@435 139 // stub code parameters / addresses
duke@435 140 assert(frame::entry_frame_call_wrapper_offset == 2, "adjust this code");
duke@435 141 bool sse_save = false;
duke@435 142 const Address rsp_after_call(rbp, -4 * wordSize); // same as in generate_catch_exception()!
duke@435 143 const int locals_count_in_bytes (4*wordSize);
duke@435 144 const Address mxcsr_save (rbp, -4 * wordSize);
duke@435 145 const Address saved_rbx (rbp, -3 * wordSize);
duke@435 146 const Address saved_rsi (rbp, -2 * wordSize);
duke@435 147 const Address saved_rdi (rbp, -1 * wordSize);
duke@435 148 const Address result (rbp, 3 * wordSize);
duke@435 149 const Address result_type (rbp, 4 * wordSize);
duke@435 150 const Address method (rbp, 5 * wordSize);
duke@435 151 const Address entry_point (rbp, 6 * wordSize);
duke@435 152 const Address parameters (rbp, 7 * wordSize);
duke@435 153 const Address parameter_size(rbp, 8 * wordSize);
duke@435 154 const Address thread (rbp, 9 * wordSize); // same as in generate_catch_exception()!
duke@435 155 sse_save = UseSSE > 0;
duke@435 156
duke@435 157 // stub code
duke@435 158 __ enter();
never@739 159 __ movptr(rcx, parameter_size); // parameter counter
twisti@1861 160 __ shlptr(rcx, Interpreter::logStackElementSize); // convert parameter count to bytes
never@739 161 __ addptr(rcx, locals_count_in_bytes); // reserve space for register saves
never@739 162 __ subptr(rsp, rcx);
never@739 163 __ andptr(rsp, -(StackAlignmentInBytes)); // Align stack
duke@435 164
duke@435 165 // save rdi, rsi, & rbx, according to C calling conventions
never@739 166 __ movptr(saved_rdi, rdi);
never@739 167 __ movptr(saved_rsi, rsi);
never@739 168 __ movptr(saved_rbx, rbx);
duke@435 169 // save and initialize %mxcsr
duke@435 170 if (sse_save) {
duke@435 171 Label skip_ldmx;
duke@435 172 __ stmxcsr(mxcsr_save);
duke@435 173 __ movl(rax, mxcsr_save);
duke@435 174 __ andl(rax, MXCSR_MASK); // Only check control and mask bits
duke@435 175 ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
duke@435 176 __ cmp32(rax, mxcsr_std);
duke@435 177 __ jcc(Assembler::equal, skip_ldmx);
duke@435 178 __ ldmxcsr(mxcsr_std);
duke@435 179 __ bind(skip_ldmx);
duke@435 180 }
duke@435 181
duke@435 182 // make sure the control word is correct.
duke@435 183 __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
duke@435 184
duke@435 185 #ifdef ASSERT
duke@435 186 // make sure we have no pending exceptions
duke@435 187 { Label L;
never@739 188 __ movptr(rcx, thread);
never@739 189 __ cmpptr(Address(rcx, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
duke@435 190 __ jcc(Assembler::equal, L);
duke@435 191 __ stop("StubRoutines::call_stub: entered with pending exception");
duke@435 192 __ bind(L);
duke@435 193 }
duke@435 194 #endif
duke@435 195
duke@435 196 // pass parameters if any
duke@435 197 BLOCK_COMMENT("pass parameters if any");
duke@435 198 Label parameters_done;
duke@435 199 __ movl(rcx, parameter_size); // parameter counter
duke@435 200 __ testl(rcx, rcx);
duke@435 201 __ jcc(Assembler::zero, parameters_done);
duke@435 202
duke@435 203 // parameter passing loop
duke@435 204
duke@435 205 Label loop;
duke@435 206 // Copy Java parameters in reverse order (receiver last)
duke@435 207 // Note that the argument order is inverted in the process
duke@435 208 // source is rdx[rcx: N-1..0]
duke@435 209 // dest is rsp[rbx: 0..N-1]
duke@435 210
never@739 211 __ movptr(rdx, parameters); // parameter pointer
never@739 212 __ xorptr(rbx, rbx);
duke@435 213
duke@435 214 __ BIND(loop);
duke@435 215
duke@435 216 // get parameter
never@739 217 __ movptr(rax, Address(rdx, rcx, Interpreter::stackElementScale(), -wordSize));
never@739 218 __ movptr(Address(rsp, rbx, Interpreter::stackElementScale(),
duke@435 219 Interpreter::expr_offset_in_bytes(0)), rax); // store parameter
duke@435 220 __ increment(rbx);
duke@435 221 __ decrement(rcx);
duke@435 222 __ jcc(Assembler::notZero, loop);
duke@435 223
duke@435 224 // call Java function
duke@435 225 __ BIND(parameters_done);
coleenp@4037 226 __ movptr(rbx, method); // get Method*
never@739 227 __ movptr(rax, entry_point); // get entry_point
never@739 228 __ mov(rsi, rsp); // set sender sp
duke@435 229 BLOCK_COMMENT("call Java function");
duke@435 230 __ call(rax);
duke@435 231
duke@435 232 BLOCK_COMMENT("call_stub_return_address:");
duke@435 233 return_address = __ pc();
duke@435 234
twisti@2552 235 #ifdef COMPILER2
twisti@2552 236 {
twisti@2552 237 Label L_skip;
twisti@2552 238 if (UseSSE >= 2) {
twisti@2552 239 __ verify_FPU(0, "call_stub_return");
twisti@2552 240 } else {
twisti@2552 241 for (int i = 1; i < 8; i++) {
twisti@2552 242 __ ffree(i);
twisti@2552 243 }
duke@435 244
twisti@2552 245 // UseSSE <= 1 so double result should be left on TOS
twisti@2552 246 __ movl(rsi, result_type);
twisti@2552 247 __ cmpl(rsi, T_DOUBLE);
twisti@2552 248 __ jcc(Assembler::equal, L_skip);
twisti@2552 249 if (UseSSE == 0) {
twisti@2552 250 // UseSSE == 0 so float result should be left on TOS
twisti@2552 251 __ cmpl(rsi, T_FLOAT);
twisti@2552 252 __ jcc(Assembler::equal, L_skip);
twisti@2552 253 }
twisti@2552 254 __ ffree(0);
twisti@2552 255 }
twisti@2552 256 __ BIND(L_skip);
twisti@2552 257 }
twisti@2552 258 #endif // COMPILER2
duke@435 259
duke@435 260 // store result depending on type
duke@435 261 // (everything that is not T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
never@739 262 __ movptr(rdi, result);
duke@435 263 Label is_long, is_float, is_double, exit;
duke@435 264 __ movl(rsi, result_type);
duke@435 265 __ cmpl(rsi, T_LONG);
duke@435 266 __ jcc(Assembler::equal, is_long);
duke@435 267 __ cmpl(rsi, T_FLOAT);
duke@435 268 __ jcc(Assembler::equal, is_float);
duke@435 269 __ cmpl(rsi, T_DOUBLE);
duke@435 270 __ jcc(Assembler::equal, is_double);
duke@435 271
duke@435 272 // handle T_INT case
duke@435 273 __ movl(Address(rdi, 0), rax);
duke@435 274 __ BIND(exit);
duke@435 275
duke@435 276 // check that FPU stack is empty
duke@435 277 __ verify_FPU(0, "generate_call_stub");
duke@435 278
duke@435 279 // pop parameters
never@739 280 __ lea(rsp, rsp_after_call);
duke@435 281
duke@435 282 // restore %mxcsr
duke@435 283 if (sse_save) {
duke@435 284 __ ldmxcsr(mxcsr_save);
duke@435 285 }
duke@435 286
duke@435 287 // restore rdi, rsi and rbx,
never@739 288 __ movptr(rbx, saved_rbx);
never@739 289 __ movptr(rsi, saved_rsi);
never@739 290 __ movptr(rdi, saved_rdi);
never@739 291 __ addptr(rsp, 4*wordSize);
duke@435 292
duke@435 293 // return
never@739 294 __ pop(rbp);
duke@435 295 __ ret(0);
duke@435 296
duke@435 297 // handle return types different from T_INT
duke@435 298 __ BIND(is_long);
duke@435 299 __ movl(Address(rdi, 0 * wordSize), rax);
duke@435 300 __ movl(Address(rdi, 1 * wordSize), rdx);
duke@435 301 __ jmp(exit);
duke@435 302
duke@435 303 __ BIND(is_float);
duke@435 304 // interpreter uses xmm0 for return values
duke@435 305 if (UseSSE >= 1) {
duke@435 306 __ movflt(Address(rdi, 0), xmm0);
duke@435 307 } else {
duke@435 308 __ fstp_s(Address(rdi, 0));
duke@435 309 }
duke@435 310 __ jmp(exit);
duke@435 311
duke@435 312 __ BIND(is_double);
duke@435 313 // interpreter uses xmm0 for return values
duke@435 314 if (UseSSE >= 2) {
duke@435 315 __ movdbl(Address(rdi, 0), xmm0);
duke@435 316 } else {
duke@435 317 __ fstp_d(Address(rdi, 0));
duke@435 318 }
duke@435 319 __ jmp(exit);
duke@435 320
duke@435 321 return start;
duke@435 322 }
duke@435 323
duke@435 324
duke@435 325 //------------------------------------------------------------------------------------------------------------------------
duke@435 326 // Return point for a Java call if there's an exception thrown in Java code.
duke@435 327 // The exception is caught and transformed into a pending exception stored in
duke@435 328 // JavaThread that can be tested from within the VM.
duke@435 329 //
duke@435 330 // Note: Usually the parameters are removed by the callee. In case of an exception
duke@435 331 // crossing an activation frame boundary, that is not the case if the callee
duke@435 332 // is compiled code => need to setup the rsp.
duke@435 333 //
duke@435 334 // rax,: exception oop
duke@435 335
duke@435 336 address generate_catch_exception() {
duke@435 337 StubCodeMark mark(this, "StubRoutines", "catch_exception");
duke@435 338 const Address rsp_after_call(rbp, -4 * wordSize); // same as in generate_call_stub()!
duke@435 339 const Address thread (rbp, 9 * wordSize); // same as in generate_call_stub()!
duke@435 340 address start = __ pc();
duke@435 341
duke@435 342 // get thread directly
never@739 343 __ movptr(rcx, thread);
duke@435 344 #ifdef ASSERT
duke@435 345 // verify that threads correspond
duke@435 346 { Label L;
duke@435 347 __ get_thread(rbx);
never@739 348 __ cmpptr(rbx, rcx);
duke@435 349 __ jcc(Assembler::equal, L);
duke@435 350 __ stop("StubRoutines::catch_exception: threads must correspond");
duke@435 351 __ bind(L);
duke@435 352 }
duke@435 353 #endif
duke@435 354 // set pending exception
duke@435 355 __ verify_oop(rax);
never@739 356 __ movptr(Address(rcx, Thread::pending_exception_offset()), rax );
duke@435 357 __ lea(Address(rcx, Thread::exception_file_offset ()),
duke@435 358 ExternalAddress((address)__FILE__));
duke@435 359 __ movl(Address(rcx, Thread::exception_line_offset ()), __LINE__ );
duke@435 360 // complete return to VM
duke@435 361 assert(StubRoutines::_call_stub_return_address != NULL, "_call_stub_return_address must have been generated before");
duke@435 362 __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
duke@435 363
duke@435 364 return start;
duke@435 365 }
duke@435 366
duke@435 367
duke@435 368 //------------------------------------------------------------------------------------------------------------------------
duke@435 369 // Continuation point for runtime calls returning with a pending exception.
duke@435 370 // The pending exception check happened in the runtime or native call stub.
duke@435 371 // The pending exception in Thread is converted into a Java-level exception.
duke@435 372 //
duke@435 373 // Contract with Java-level exception handlers:
twisti@1730 374 // rax: exception
duke@435 375 // rdx: throwing pc
duke@435 376 //
duke@435 377 // NOTE: At entry of this stub, exception-pc must be on stack !!
duke@435 378
duke@435 379 address generate_forward_exception() {
duke@435 380 StubCodeMark mark(this, "StubRoutines", "forward exception");
duke@435 381 address start = __ pc();
twisti@1730 382 const Register thread = rcx;
twisti@1730 383
twisti@1730 384 // other registers used in this stub
twisti@1730 385 const Register exception_oop = rax;
twisti@1730 386 const Register handler_addr = rbx;
twisti@1730 387 const Register exception_pc = rdx;
duke@435 388
duke@435 389 // Upon entry, the sp points to the return address returning into Java
duke@435 390 // (interpreted or compiled) code; i.e., the return address becomes the
duke@435 391 // throwing pc.
duke@435 392 //
duke@435 393 // Arguments pushed before the runtime call are still on the stack but
duke@435 394 // the exception handler will reset the stack pointer -> ignore them.
duke@435 395 // A potential result in registers can be ignored as well.
duke@435 396
duke@435 397 #ifdef ASSERT
duke@435 398 // make sure this code is only executed if there is a pending exception
duke@435 399 { Label L;
twisti@1730 400 __ get_thread(thread);
twisti@1730 401 __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
duke@435 402 __ jcc(Assembler::notEqual, L);
duke@435 403 __ stop("StubRoutines::forward exception: no pending exception (1)");
duke@435 404 __ bind(L);
duke@435 405 }
duke@435 406 #endif
duke@435 407
duke@435 408 // compute exception handler into rbx,
twisti@1730 409 __ get_thread(thread);
twisti@1730 410 __ movptr(exception_pc, Address(rsp, 0));
duke@435 411 BLOCK_COMMENT("call exception_handler_for_return_address");
twisti@1730 412 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), thread, exception_pc);
twisti@1730 413 __ mov(handler_addr, rax);
duke@435 414
twisti@1730 415 // setup rax & rdx, remove return address & clear pending exception
twisti@1730 416 __ get_thread(thread);
twisti@1730 417 __ pop(exception_pc);
twisti@1730 418 __ movptr(exception_oop, Address(thread, Thread::pending_exception_offset()));
twisti@1730 419 __ movptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
duke@435 420
duke@435 421 #ifdef ASSERT
duke@435 422 // make sure exception is set
duke@435 423 { Label L;
twisti@1730 424 __ testptr(exception_oop, exception_oop);
duke@435 425 __ jcc(Assembler::notEqual, L);
duke@435 426 __ stop("StubRoutines::forward exception: no pending exception (2)");
duke@435 427 __ bind(L);
duke@435 428 }
duke@435 429 #endif
duke@435 430
twisti@1730 431 // Verify that there is really a valid exception in RAX.
twisti@1730 432 __ verify_oop(exception_oop);
twisti@1730 433
duke@435 434 // continue at exception handler (return address removed)
twisti@1730 435 // rax: exception
twisti@1730 436 // rbx: exception handler
duke@435 437 // rdx: throwing pc
twisti@1730 438 __ jmp(handler_addr);
duke@435 439
duke@435 440 return start;
duke@435 441 }
duke@435 442
duke@435 443
duke@435 444 //----------------------------------------------------------------------------------------------------
duke@435 445 // Support for jint Atomic::xchg(jint exchange_value, volatile jint* dest)
duke@435 446 //
duke@435 447 // xchg exists as far back as 8086, lock needed for MP only
duke@435 448 // Stack layout immediately after call:
duke@435 449 //
duke@435 450 // 0 [ret addr ] <--- rsp
duke@435 451 // 1 [ ex ]
duke@435 452 // 2 [ dest ]
duke@435 453 //
duke@435 454 // Result: *dest <- ex, return (old *dest)
duke@435 455 //
duke@435 456 // Note: win32 does not currently use this code
duke@435 457
duke@435 458 address generate_atomic_xchg() {
duke@435 459 StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
duke@435 460 address start = __ pc();
duke@435 461
never@739 462 __ push(rdx);
duke@435 463 Address exchange(rsp, 2 * wordSize);
duke@435 464 Address dest_addr(rsp, 3 * wordSize);
duke@435 465 __ movl(rax, exchange);
never@739 466 __ movptr(rdx, dest_addr);
never@739 467 __ xchgl(rax, Address(rdx, 0));
never@739 468 __ pop(rdx);
duke@435 469 __ ret(0);
duke@435 470
duke@435 471 return start;
duke@435 472 }
duke@435 473
duke@435 474 //----------------------------------------------------------------------------------------------------
duke@435 475 // Support for void verify_mxcsr()
duke@435 476 //
duke@435 477 // This routine is used with -Xcheck:jni to verify that native
duke@435 478 // JNI code does not return to Java code without restoring the
duke@435 479 // MXCSR register to our expected state.
duke@435 480
duke@435 481
duke@435 482 address generate_verify_mxcsr() {
duke@435 483 StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
duke@435 484 address start = __ pc();
duke@435 485
duke@435 486 const Address mxcsr_save(rsp, 0);
duke@435 487
duke@435 488 if (CheckJNICalls && UseSSE > 0 ) {
duke@435 489 Label ok_ret;
duke@435 490 ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
never@739 491 __ push(rax);
never@739 492 __ subptr(rsp, wordSize); // allocate a temp location
duke@435 493 __ stmxcsr(mxcsr_save);
duke@435 494 __ movl(rax, mxcsr_save);
duke@435 495 __ andl(rax, MXCSR_MASK);
duke@435 496 __ cmp32(rax, mxcsr_std);
duke@435 497 __ jcc(Assembler::equal, ok_ret);
duke@435 498
duke@435 499 __ warn("MXCSR changed by native JNI code.");
duke@435 500
duke@435 501 __ ldmxcsr(mxcsr_std);
duke@435 502
duke@435 503 __ bind(ok_ret);
never@739 504 __ addptr(rsp, wordSize);
never@739 505 __ pop(rax);
duke@435 506 }
duke@435 507
duke@435 508 __ ret(0);
duke@435 509
duke@435 510 return start;
duke@435 511 }
duke@435 512
duke@435 513
duke@435 514 //---------------------------------------------------------------------------
duke@435 515 // Support for void verify_fpu_cntrl_wrd()
duke@435 516 //
duke@435 517 // This routine is used with -Xcheck:jni to verify that native
duke@435 518 // JNI code does not return to Java code without restoring the
duke@435 519 // FP control word to our expected state.
duke@435 520
duke@435 521 address generate_verify_fpu_cntrl_wrd() {
duke@435 522 StubCodeMark mark(this, "StubRoutines", "verify_spcw");
duke@435 523 address start = __ pc();
duke@435 524
duke@435 525 const Address fpu_cntrl_wrd_save(rsp, 0);
duke@435 526
duke@435 527 if (CheckJNICalls) {
duke@435 528 Label ok_ret;
never@739 529 __ push(rax);
never@739 530 __ subptr(rsp, wordSize); // allocate a temp location
duke@435 531 __ fnstcw(fpu_cntrl_wrd_save);
duke@435 532 __ movl(rax, fpu_cntrl_wrd_save);
duke@435 533 __ andl(rax, FPU_CNTRL_WRD_MASK);
duke@435 534 ExternalAddress fpu_std(StubRoutines::addr_fpu_cntrl_wrd_std());
duke@435 535 __ cmp32(rax, fpu_std);
duke@435 536 __ jcc(Assembler::equal, ok_ret);
duke@435 537
duke@435 538 __ warn("Floating point control word changed by native JNI code.");
duke@435 539
duke@435 540 __ fldcw(fpu_std);
duke@435 541
duke@435 542 __ bind(ok_ret);
never@739 543 __ addptr(rsp, wordSize);
never@739 544 __ pop(rax);
duke@435 545 }
duke@435 546
duke@435 547 __ ret(0);
duke@435 548
duke@435 549 return start;
duke@435 550 }
duke@435 551
duke@435 552 //---------------------------------------------------------------------------
duke@435 553 // Wrapper for slow-case handling of double-to-integer conversion
duke@435 554 // d2i or f2i fast case failed either because it is nan or because
duke@435 555 // of under/overflow.
duke@435 556 // Input: FPU TOS: float value
duke@435 557 // Output: rax, (rdx): integer (long) result
duke@435 558
duke@435 559 address generate_d2i_wrapper(BasicType t, address fcn) {
duke@435 560 StubCodeMark mark(this, "StubRoutines", "d2i_wrapper");
duke@435 561 address start = __ pc();
duke@435 562
duke@435 563 // Capture info about frame layout
duke@435 564 enum layout { FPUState_off = 0,
duke@435 565 rbp_off = FPUStateSizeInWords,
duke@435 566 rdi_off,
duke@435 567 rsi_off,
duke@435 568 rcx_off,
duke@435 569 rbx_off,
duke@435 570 saved_argument_off,
duke@435 571 saved_argument_off2, // 2nd half of double
duke@435 572 framesize
duke@435 573 };
duke@435 574
duke@435 575 assert(FPUStateSizeInWords == 27, "update stack layout");
duke@435 576
duke@435 577 // Save outgoing argument to stack across push_FPU_state()
never@739 578 __ subptr(rsp, wordSize * 2);
duke@435 579 __ fstp_d(Address(rsp, 0));
duke@435 580
duke@435 581 // Save CPU & FPU state
never@739 582 __ push(rbx);
never@739 583 __ push(rcx);
never@739 584 __ push(rsi);
never@739 585 __ push(rdi);
never@739 586 __ push(rbp);
duke@435 587 __ push_FPU_state();
duke@435 588
duke@435 589 // push_FPU_state() resets the FP top of stack
duke@435 590 // Load original double into FP top of stack
duke@435 591 __ fld_d(Address(rsp, saved_argument_off * wordSize));
duke@435 592 // Store double into stack as outgoing argument
never@739 593 __ subptr(rsp, wordSize*2);
duke@435 594 __ fst_d(Address(rsp, 0));
duke@435 595
duke@435 596 // Prepare FPU for doing math in C-land
duke@435 597 __ empty_FPU_stack();
duke@435 598 // Call the C code to massage the double. Result in EAX
duke@435 599 if (t == T_INT)
duke@435 600 { BLOCK_COMMENT("SharedRuntime::d2i"); }
duke@435 601 else if (t == T_LONG)
duke@435 602 { BLOCK_COMMENT("SharedRuntime::d2l"); }
duke@435 603 __ call_VM_leaf( fcn, 2 );
duke@435 604
duke@435 605 // Restore CPU & FPU state
duke@435 606 __ pop_FPU_state();
never@739 607 __ pop(rbp);
never@739 608 __ pop(rdi);
never@739 609 __ pop(rsi);
never@739 610 __ pop(rcx);
never@739 611 __ pop(rbx);
never@739 612 __ addptr(rsp, wordSize * 2);
duke@435 613
duke@435 614 __ ret(0);
duke@435 615
duke@435 616 return start;
duke@435 617 }
duke@435 618
duke@435 619
duke@435 620 //---------------------------------------------------------------------------
duke@435 621 // The following routine generates a subroutine to throw an asynchronous
duke@435 622 // UnknownError when an unsafe access gets a fault that could not be
duke@435 623 // reasonably prevented by the programmer. (Example: SIGBUS/OBJERR.)
duke@435 624 address generate_handler_for_unsafe_access() {
duke@435 625 StubCodeMark mark(this, "StubRoutines", "handler_for_unsafe_access");
duke@435 626 address start = __ pc();
duke@435 627
never@739 628 __ push(0); // hole for return address-to-be
never@739 629 __ pusha(); // push registers
duke@435 630 Address next_pc(rsp, RegisterImpl::number_of_registers * BytesPerWord);
duke@435 631 BLOCK_COMMENT("call handle_unsafe_access");
duke@435 632 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, handle_unsafe_access)));
never@739 633 __ movptr(next_pc, rax); // stuff next address
never@739 634 __ popa();
duke@435 635 __ ret(0); // jump to next address
duke@435 636
duke@435 637 return start;
duke@435 638 }
duke@435 639
duke@435 640
duke@435 641 //----------------------------------------------------------------------------------------------------
duke@435 642 // Non-destructive plausibility checks for oops
duke@435 643
duke@435 644 address generate_verify_oop() {
duke@435 645 StubCodeMark mark(this, "StubRoutines", "verify_oop");
duke@435 646 address start = __ pc();
duke@435 647
duke@435 648 // Incoming arguments on stack after saving rax,:
duke@435 649 //
duke@435 650 // [tos ]: saved rdx
duke@435 651 // [tos + 1]: saved EFLAGS
duke@435 652 // [tos + 2]: return address
duke@435 653 // [tos + 3]: char* error message
duke@435 654 // [tos + 4]: oop object to verify
duke@435 655 // [tos + 5]: saved rax, - saved by caller and bashed
duke@435 656
duke@435 657 Label exit, error;
never@739 658 __ pushf();
never@739 659 __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
never@739 660 __ push(rdx); // save rdx
duke@435 661 // make sure object is 'reasonable'
never@739 662 __ movptr(rax, Address(rsp, 4 * wordSize)); // get object
never@739 663 __ testptr(rax, rax);
duke@435 664 __ jcc(Assembler::zero, exit); // if obj is NULL it is ok
duke@435 665
duke@435 666 // Check if the oop is in the right area of memory
duke@435 667 const int oop_mask = Universe::verify_oop_mask();
duke@435 668 const int oop_bits = Universe::verify_oop_bits();
never@739 669 __ mov(rdx, rax);
never@739 670 __ andptr(rdx, oop_mask);
never@739 671 __ cmpptr(rdx, oop_bits);
duke@435 672 __ jcc(Assembler::notZero, error);
duke@435 673
coleenp@4037 674 // make sure klass is 'reasonable', which is not zero.
never@739 675 __ movptr(rax, Address(rax, oopDesc::klass_offset_in_bytes())); // get klass
never@739 676 __ testptr(rax, rax);
duke@435 677 __ jcc(Assembler::zero, error); // if klass is NULL it is broken
duke@435 678
duke@435 679 // return if everything seems ok
duke@435 680 __ bind(exit);
never@739 681 __ movptr(rax, Address(rsp, 5 * wordSize)); // get saved rax, back
never@739 682 __ pop(rdx); // restore rdx
never@739 683 __ popf(); // restore EFLAGS
duke@435 684 __ ret(3 * wordSize); // pop arguments
duke@435 685
duke@435 686 // handle errors
duke@435 687 __ bind(error);
never@739 688 __ movptr(rax, Address(rsp, 5 * wordSize)); // get saved rax, back
never@739 689 __ pop(rdx); // get saved rdx back
never@739 690 __ popf(); // get saved EFLAGS off stack -- will be ignored
never@739 691 __ pusha(); // push registers (eip = return address & msg are already pushed)
duke@435 692 BLOCK_COMMENT("call MacroAssembler::debug");
never@739 693 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug32)));
never@739 694 __ popa();
duke@435 695 __ ret(3 * wordSize); // pop arguments
duke@435 696 return start;
duke@435 697 }
duke@435 698
duke@435 699 //
duke@435 700 // Generate pre-barrier for array stores
duke@435 701 //
duke@435 702 // Input:
duke@435 703 // start - starting address
ysr@1280 704 // count - element count
iveresov@2606 705 void gen_write_ref_array_pre_barrier(Register start, Register count, bool uninitialized_target) {
duke@435 706 assert_different_registers(start, count);
duke@435 707 BarrierSet* bs = Universe::heap()->barrier_set();
duke@435 708 switch (bs->kind()) {
duke@435 709 case BarrierSet::G1SATBCT:
duke@435 710 case BarrierSet::G1SATBCTLogging:
iveresov@2606 711 // With G1, don't generate the call if we statically know that the target in uninitialized
iveresov@2606 712 if (!uninitialized_target) {
iveresov@2606 713 __ pusha(); // push registers
iveresov@2606 714 __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre),
iveresov@2606 715 start, count);
iveresov@2606 716 __ popa();
iveresov@2606 717 }
duke@435 718 break;
duke@435 719 case BarrierSet::CardTableModRef:
duke@435 720 case BarrierSet::CardTableExtension:
duke@435 721 case BarrierSet::ModRef:
duke@435 722 break;
duke@435 723 default :
duke@435 724 ShouldNotReachHere();
duke@435 725
duke@435 726 }
duke@435 727 }
duke@435 728
duke@435 729
duke@435 730 //
duke@435 731 // Generate a post-barrier for an array store
duke@435 732 //
duke@435 733 // start - starting address
duke@435 734 // count - element count
duke@435 735 //
duke@435 736 // The two input registers are overwritten.
duke@435 737 //
duke@435 738 void gen_write_ref_array_post_barrier(Register start, Register count) {
duke@435 739 BarrierSet* bs = Universe::heap()->barrier_set();
duke@435 740 assert_different_registers(start, count);
duke@435 741 switch (bs->kind()) {
duke@435 742 case BarrierSet::G1SATBCT:
duke@435 743 case BarrierSet::G1SATBCTLogging:
duke@435 744 {
never@739 745 __ pusha(); // push registers
apetrusenko@1627 746 __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post),
apetrusenko@1627 747 start, count);
never@739 748 __ popa();
duke@435 749 }
duke@435 750 break;
duke@435 751
duke@435 752 case BarrierSet::CardTableModRef:
duke@435 753 case BarrierSet::CardTableExtension:
duke@435 754 {
duke@435 755 CardTableModRefBS* ct = (CardTableModRefBS*)bs;
duke@435 756 assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
duke@435 757
duke@435 758 Label L_loop;
duke@435 759 const Register end = count; // elements count; end == start+count-1
duke@435 760 assert_different_registers(start, end);
duke@435 761
never@739 762 __ lea(end, Address(start, count, Address::times_ptr, -wordSize));
never@739 763 __ shrptr(start, CardTableModRefBS::card_shift);
never@739 764 __ shrptr(end, CardTableModRefBS::card_shift);
never@739 765 __ subptr(end, start); // end --> count
duke@435 766 __ BIND(L_loop);
never@684 767 intptr_t disp = (intptr_t) ct->byte_map_base;
never@684 768 Address cardtable(start, count, Address::times_1, disp);
never@684 769 __ movb(cardtable, 0);
duke@435 770 __ decrement(count);
duke@435 771 __ jcc(Assembler::greaterEqual, L_loop);
duke@435 772 }
duke@435 773 break;
duke@435 774 case BarrierSet::ModRef:
duke@435 775 break;
duke@435 776 default :
duke@435 777 ShouldNotReachHere();
duke@435 778
duke@435 779 }
duke@435 780 }
duke@435 781
kvn@840 782
kvn@840 783 // Copy 64 bytes chunks
kvn@840 784 //
kvn@840 785 // Inputs:
kvn@840 786 // from - source array address
kvn@840 787 // to_from - destination array address - from
kvn@840 788 // qword_count - 8-bytes element count, negative
kvn@840 789 //
kvn@840 790 void xmm_copy_forward(Register from, Register to_from, Register qword_count) {
kvn@840 791 assert( UseSSE >= 2, "supported cpu only" );
kvn@840 792 Label L_copy_64_bytes_loop, L_copy_64_bytes, L_copy_8_bytes, L_exit;
kvn@840 793 // Copy 64-byte chunks
kvn@840 794 __ jmpb(L_copy_64_bytes);
kvn@1800 795 __ align(OptoLoopAlignment);
kvn@840 796 __ BIND(L_copy_64_bytes_loop);
kvn@840 797
kvn@4411 798 if (UseUnalignedLoadStores) {
kvn@4411 799 if (UseAVX >= 2) {
kvn@4411 800 __ vmovdqu(xmm0, Address(from, 0));
kvn@4411 801 __ vmovdqu(Address(from, to_from, Address::times_1, 0), xmm0);
kvn@4411 802 __ vmovdqu(xmm1, Address(from, 32));
kvn@4411 803 __ vmovdqu(Address(from, to_from, Address::times_1, 32), xmm1);
kvn@4411 804 } else {
kvn@4411 805 __ movdqu(xmm0, Address(from, 0));
kvn@4411 806 __ movdqu(Address(from, to_from, Address::times_1, 0), xmm0);
kvn@4411 807 __ movdqu(xmm1, Address(from, 16));
kvn@4411 808 __ movdqu(Address(from, to_from, Address::times_1, 16), xmm1);
kvn@4411 809 __ movdqu(xmm2, Address(from, 32));
kvn@4411 810 __ movdqu(Address(from, to_from, Address::times_1, 32), xmm2);
kvn@4411 811 __ movdqu(xmm3, Address(from, 48));
kvn@4411 812 __ movdqu(Address(from, to_from, Address::times_1, 48), xmm3);
kvn@4411 813 }
kvn@840 814 } else {
kvn@840 815 __ movq(xmm0, Address(from, 0));
kvn@840 816 __ movq(Address(from, to_from, Address::times_1, 0), xmm0);
kvn@840 817 __ movq(xmm1, Address(from, 8));
kvn@840 818 __ movq(Address(from, to_from, Address::times_1, 8), xmm1);
kvn@840 819 __ movq(xmm2, Address(from, 16));
kvn@840 820 __ movq(Address(from, to_from, Address::times_1, 16), xmm2);
kvn@840 821 __ movq(xmm3, Address(from, 24));
kvn@840 822 __ movq(Address(from, to_from, Address::times_1, 24), xmm3);
kvn@840 823 __ movq(xmm4, Address(from, 32));
kvn@840 824 __ movq(Address(from, to_from, Address::times_1, 32), xmm4);
kvn@840 825 __ movq(xmm5, Address(from, 40));
kvn@840 826 __ movq(Address(from, to_from, Address::times_1, 40), xmm5);
kvn@840 827 __ movq(xmm6, Address(from, 48));
kvn@840 828 __ movq(Address(from, to_from, Address::times_1, 48), xmm6);
kvn@840 829 __ movq(xmm7, Address(from, 56));
kvn@840 830 __ movq(Address(from, to_from, Address::times_1, 56), xmm7);
kvn@840 831 }
kvn@840 832
kvn@840 833 __ addl(from, 64);
kvn@840 834 __ BIND(L_copy_64_bytes);
kvn@840 835 __ subl(qword_count, 8);
kvn@840 836 __ jcc(Assembler::greaterEqual, L_copy_64_bytes_loop);
kvn@4873 837
kvn@4873 838 if (UseUnalignedLoadStores && (UseAVX >= 2)) {
kvn@4873 839 // clean upper bits of YMM registers
kvn@7816 840 __ vpxor(xmm0, xmm0);
kvn@7816 841 __ vpxor(xmm1, xmm1);
kvn@4873 842 }
kvn@840 843 __ addl(qword_count, 8);
kvn@840 844 __ jccb(Assembler::zero, L_exit);
kvn@840 845 //
kvn@840 846 // length is too short, just copy qwords
kvn@840 847 //
kvn@840 848 __ BIND(L_copy_8_bytes);
kvn@840 849 __ movq(xmm0, Address(from, 0));
kvn@840 850 __ movq(Address(from, to_from, Address::times_1), xmm0);
kvn@840 851 __ addl(from, 8);
kvn@840 852 __ decrement(qword_count);
kvn@840 853 __ jcc(Assembler::greater, L_copy_8_bytes);
kvn@840 854 __ BIND(L_exit);
kvn@840 855 }
kvn@840 856
duke@435 857 // Copy 64 bytes chunks
duke@435 858 //
duke@435 859 // Inputs:
duke@435 860 // from - source array address
duke@435 861 // to_from - destination array address - from
duke@435 862 // qword_count - 8-bytes element count, negative
duke@435 863 //
duke@435 864 void mmx_copy_forward(Register from, Register to_from, Register qword_count) {
kvn@840 865 assert( VM_Version::supports_mmx(), "supported cpu only" );
duke@435 866 Label L_copy_64_bytes_loop, L_copy_64_bytes, L_copy_8_bytes, L_exit;
duke@435 867 // Copy 64-byte chunks
duke@435 868 __ jmpb(L_copy_64_bytes);
kvn@1800 869 __ align(OptoLoopAlignment);
duke@435 870 __ BIND(L_copy_64_bytes_loop);
duke@435 871 __ movq(mmx0, Address(from, 0));
duke@435 872 __ movq(mmx1, Address(from, 8));
duke@435 873 __ movq(mmx2, Address(from, 16));
duke@435 874 __ movq(Address(from, to_from, Address::times_1, 0), mmx0);
duke@435 875 __ movq(mmx3, Address(from, 24));
duke@435 876 __ movq(Address(from, to_from, Address::times_1, 8), mmx1);
duke@435 877 __ movq(mmx4, Address(from, 32));
duke@435 878 __ movq(Address(from, to_from, Address::times_1, 16), mmx2);
duke@435 879 __ movq(mmx5, Address(from, 40));
duke@435 880 __ movq(Address(from, to_from, Address::times_1, 24), mmx3);
duke@435 881 __ movq(mmx6, Address(from, 48));
duke@435 882 __ movq(Address(from, to_from, Address::times_1, 32), mmx4);
duke@435 883 __ movq(mmx7, Address(from, 56));
duke@435 884 __ movq(Address(from, to_from, Address::times_1, 40), mmx5);
duke@435 885 __ movq(Address(from, to_from, Address::times_1, 48), mmx6);
duke@435 886 __ movq(Address(from, to_from, Address::times_1, 56), mmx7);
never@739 887 __ addptr(from, 64);
duke@435 888 __ BIND(L_copy_64_bytes);
duke@435 889 __ subl(qword_count, 8);
duke@435 890 __ jcc(Assembler::greaterEqual, L_copy_64_bytes_loop);
duke@435 891 __ addl(qword_count, 8);
duke@435 892 __ jccb(Assembler::zero, L_exit);
duke@435 893 //
duke@435 894 // length is too short, just copy qwords
duke@435 895 //
duke@435 896 __ BIND(L_copy_8_bytes);
duke@435 897 __ movq(mmx0, Address(from, 0));
duke@435 898 __ movq(Address(from, to_from, Address::times_1), mmx0);
never@739 899 __ addptr(from, 8);
duke@435 900 __ decrement(qword_count);
duke@435 901 __ jcc(Assembler::greater, L_copy_8_bytes);
duke@435 902 __ BIND(L_exit);
duke@435 903 __ emms();
duke@435 904 }
duke@435 905
duke@435 906 address generate_disjoint_copy(BasicType t, bool aligned,
duke@435 907 Address::ScaleFactor sf,
iveresov@2606 908 address* entry, const char *name,
iveresov@2606 909 bool dest_uninitialized = false) {
duke@435 910 __ align(CodeEntryAlignment);
duke@435 911 StubCodeMark mark(this, "StubRoutines", name);
duke@435 912 address start = __ pc();
duke@435 913
duke@435 914 Label L_0_count, L_exit, L_skip_align1, L_skip_align2, L_copy_byte;
duke@435 915 Label L_copy_2_bytes, L_copy_4_bytes, L_copy_64_bytes;
duke@435 916
never@739 917 int shift = Address::times_ptr - sf;
duke@435 918
duke@435 919 const Register from = rsi; // source array address
duke@435 920 const Register to = rdi; // destination array address
duke@435 921 const Register count = rcx; // elements count
duke@435 922 const Register to_from = to; // (to - from)
duke@435 923 const Register saved_to = rdx; // saved destination array address
duke@435 924
duke@435 925 __ enter(); // required for proper stackwalking of RuntimeStub frame
never@739 926 __ push(rsi);
never@739 927 __ push(rdi);
never@739 928 __ movptr(from , Address(rsp, 12+ 4));
never@739 929 __ movptr(to , Address(rsp, 12+ 8));
duke@435 930 __ movl(count, Address(rsp, 12+ 12));
iveresov@2595 931
iveresov@2595 932 if (entry != NULL) {
iveresov@2595 933 *entry = __ pc(); // Entry point from conjoint arraycopy stub.
iveresov@2595 934 BLOCK_COMMENT("Entry:");
iveresov@2595 935 }
iveresov@2595 936
duke@435 937 if (t == T_OBJECT) {
duke@435 938 __ testl(count, count);
duke@435 939 __ jcc(Assembler::zero, L_0_count);
iveresov@2606 940 gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
never@739 941 __ mov(saved_to, to); // save 'to'
duke@435 942 }
duke@435 943
never@739 944 __ subptr(to, from); // to --> to_from
duke@435 945 __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
duke@435 946 __ jcc(Assembler::below, L_copy_4_bytes); // use unsigned cmp
kvn@840 947 if (!UseUnalignedLoadStores && !aligned && (t == T_BYTE || t == T_SHORT)) {
duke@435 948 // align source address at 4 bytes address boundary
duke@435 949 if (t == T_BYTE) {
duke@435 950 // One byte misalignment happens only for byte arrays
duke@435 951 __ testl(from, 1);
duke@435 952 __ jccb(Assembler::zero, L_skip_align1);
duke@435 953 __ movb(rax, Address(from, 0));
duke@435 954 __ movb(Address(from, to_from, Address::times_1, 0), rax);
duke@435 955 __ increment(from);
duke@435 956 __ decrement(count);
duke@435 957 __ BIND(L_skip_align1);
duke@435 958 }
duke@435 959 // Two bytes misalignment happens only for byte and short (char) arrays
duke@435 960 __ testl(from, 2);
duke@435 961 __ jccb(Assembler::zero, L_skip_align2);
duke@435 962 __ movw(rax, Address(from, 0));
duke@435 963 __ movw(Address(from, to_from, Address::times_1, 0), rax);
never@739 964 __ addptr(from, 2);
duke@435 965 __ subl(count, 1<<(shift-1));
duke@435 966 __ BIND(L_skip_align2);
duke@435 967 }
duke@435 968 if (!VM_Version::supports_mmx()) {
never@739 969 __ mov(rax, count); // save 'count'
never@739 970 __ shrl(count, shift); // bytes count
never@739 971 __ addptr(to_from, from);// restore 'to'
never@739 972 __ rep_mov();
never@739 973 __ subptr(to_from, from);// restore 'to_from'
never@739 974 __ mov(count, rax); // restore 'count'
duke@435 975 __ jmpb(L_copy_2_bytes); // all dwords were copied
duke@435 976 } else {
kvn@840 977 if (!UseUnalignedLoadStores) {
kvn@840 978 // align to 8 bytes, we know we are 4 byte aligned to start
kvn@840 979 __ testptr(from, 4);
kvn@840 980 __ jccb(Assembler::zero, L_copy_64_bytes);
kvn@840 981 __ movl(rax, Address(from, 0));
kvn@840 982 __ movl(Address(from, to_from, Address::times_1, 0), rax);
kvn@840 983 __ addptr(from, 4);
kvn@840 984 __ subl(count, 1<<shift);
kvn@840 985 }
duke@435 986 __ BIND(L_copy_64_bytes);
never@739 987 __ mov(rax, count);
duke@435 988 __ shrl(rax, shift+1); // 8 bytes chunk count
duke@435 989 //
duke@435 990 // Copy 8-byte chunks through MMX registers, 8 per iteration of the loop
duke@435 991 //
kvn@840 992 if (UseXMMForArrayCopy) {
kvn@840 993 xmm_copy_forward(from, to_from, rax);
kvn@840 994 } else {
kvn@840 995 mmx_copy_forward(from, to_from, rax);
kvn@840 996 }
duke@435 997 }
duke@435 998 // copy tailing dword
duke@435 999 __ BIND(L_copy_4_bytes);
duke@435 1000 __ testl(count, 1<<shift);
duke@435 1001 __ jccb(Assembler::zero, L_copy_2_bytes);
duke@435 1002 __ movl(rax, Address(from, 0));
duke@435 1003 __ movl(Address(from, to_from, Address::times_1, 0), rax);
duke@435 1004 if (t == T_BYTE || t == T_SHORT) {
never@739 1005 __ addptr(from, 4);
duke@435 1006 __ BIND(L_copy_2_bytes);
duke@435 1007 // copy tailing word
duke@435 1008 __ testl(count, 1<<(shift-1));
duke@435 1009 __ jccb(Assembler::zero, L_copy_byte);
duke@435 1010 __ movw(rax, Address(from, 0));
duke@435 1011 __ movw(Address(from, to_from, Address::times_1, 0), rax);
duke@435 1012 if (t == T_BYTE) {
never@739 1013 __ addptr(from, 2);
duke@435 1014 __ BIND(L_copy_byte);
duke@435 1015 // copy tailing byte
duke@435 1016 __ testl(count, 1);
duke@435 1017 __ jccb(Assembler::zero, L_exit);
duke@435 1018 __ movb(rax, Address(from, 0));
duke@435 1019 __ movb(Address(from, to_from, Address::times_1, 0), rax);
duke@435 1020 __ BIND(L_exit);
duke@435 1021 } else {
duke@435 1022 __ BIND(L_copy_byte);
duke@435 1023 }
duke@435 1024 } else {
duke@435 1025 __ BIND(L_copy_2_bytes);
duke@435 1026 }
duke@435 1027
duke@435 1028 if (t == T_OBJECT) {
duke@435 1029 __ movl(count, Address(rsp, 12+12)); // reread 'count'
never@739 1030 __ mov(to, saved_to); // restore 'to'
duke@435 1031 gen_write_ref_array_post_barrier(to, count);
duke@435 1032 __ BIND(L_0_count);
duke@435 1033 }
duke@435 1034 inc_copy_counter_np(t);
never@739 1035 __ pop(rdi);
never@739 1036 __ pop(rsi);
duke@435 1037 __ leave(); // required for proper stackwalking of RuntimeStub frame
never@739 1038 __ xorptr(rax, rax); // return 0
duke@435 1039 __ ret(0);
duke@435 1040 return start;
duke@435 1041 }
duke@435 1042
duke@435 1043
never@2118 1044 address generate_fill(BasicType t, bool aligned, const char *name) {
never@2118 1045 __ align(CodeEntryAlignment);
never@2118 1046 StubCodeMark mark(this, "StubRoutines", name);
never@2118 1047 address start = __ pc();
never@2118 1048
never@2118 1049 BLOCK_COMMENT("Entry:");
never@2118 1050
never@2118 1051 const Register to = rdi; // source array address
never@2118 1052 const Register value = rdx; // value
never@2118 1053 const Register count = rsi; // elements count
never@2118 1054
never@2118 1055 __ enter(); // required for proper stackwalking of RuntimeStub frame
never@2118 1056 __ push(rsi);
never@2118 1057 __ push(rdi);
never@2118 1058 __ movptr(to , Address(rsp, 12+ 4));
never@2118 1059 __ movl(value, Address(rsp, 12+ 8));
never@2118 1060 __ movl(count, Address(rsp, 12+ 12));
never@2118 1061
never@2118 1062 __ generate_fill(t, aligned, to, value, count, rax, xmm0);
never@2118 1063
never@2118 1064 __ pop(rdi);
never@2118 1065 __ pop(rsi);
never@2118 1066 __ leave(); // required for proper stackwalking of RuntimeStub frame
never@2118 1067 __ ret(0);
never@2118 1068 return start;
never@2118 1069 }
never@2118 1070
duke@435 1071 address generate_conjoint_copy(BasicType t, bool aligned,
duke@435 1072 Address::ScaleFactor sf,
duke@435 1073 address nooverlap_target,
iveresov@2606 1074 address* entry, const char *name,
iveresov@2606 1075 bool dest_uninitialized = false) {
duke@435 1076 __ align(CodeEntryAlignment);
duke@435 1077 StubCodeMark mark(this, "StubRoutines", name);
duke@435 1078 address start = __ pc();
duke@435 1079
duke@435 1080 Label L_0_count, L_exit, L_skip_align1, L_skip_align2, L_copy_byte;
duke@435 1081 Label L_copy_2_bytes, L_copy_4_bytes, L_copy_8_bytes, L_copy_8_bytes_loop;
duke@435 1082
never@739 1083 int shift = Address::times_ptr - sf;
duke@435 1084
duke@435 1085 const Register src = rax; // source array address
duke@435 1086 const Register dst = rdx; // destination array address
duke@435 1087 const Register from = rsi; // source array address
duke@435 1088 const Register to = rdi; // destination array address
duke@435 1089 const Register count = rcx; // elements count
duke@435 1090 const Register end = rax; // array end address
duke@435 1091
duke@435 1092 __ enter(); // required for proper stackwalking of RuntimeStub frame
never@739 1093 __ push(rsi);
never@739 1094 __ push(rdi);
never@739 1095 __ movptr(src , Address(rsp, 12+ 4)); // from
never@739 1096 __ movptr(dst , Address(rsp, 12+ 8)); // to
never@739 1097 __ movl2ptr(count, Address(rsp, 12+12)); // count
duke@435 1098
duke@435 1099 if (entry != NULL) {
duke@435 1100 *entry = __ pc(); // Entry point from generic arraycopy stub.
duke@435 1101 BLOCK_COMMENT("Entry:");
duke@435 1102 }
duke@435 1103
iveresov@2595 1104 // nooverlap_target expects arguments in rsi and rdi.
never@739 1105 __ mov(from, src);
never@739 1106 __ mov(to , dst);
duke@435 1107
iveresov@2595 1108 // arrays overlap test: dispatch to disjoint stub if necessary.
duke@435 1109 RuntimeAddress nooverlap(nooverlap_target);
never@739 1110 __ cmpptr(dst, src);
never@739 1111 __ lea(end, Address(src, count, sf, 0)); // src + count * elem_size
duke@435 1112 __ jump_cc(Assembler::belowEqual, nooverlap);
never@739 1113 __ cmpptr(dst, end);
duke@435 1114 __ jump_cc(Assembler::aboveEqual, nooverlap);
duke@435 1115
iveresov@2595 1116 if (t == T_OBJECT) {
iveresov@2595 1117 __ testl(count, count);
iveresov@2595 1118 __ jcc(Assembler::zero, L_0_count);
iveresov@2606 1119 gen_write_ref_array_pre_barrier(dst, count, dest_uninitialized);
iveresov@2595 1120 }
iveresov@2595 1121
duke@435 1122 // copy from high to low
duke@435 1123 __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
duke@435 1124 __ jcc(Assembler::below, L_copy_4_bytes); // use unsigned cmp
duke@435 1125 if (t == T_BYTE || t == T_SHORT) {
duke@435 1126 // Align the end of destination array at 4 bytes address boundary
never@739 1127 __ lea(end, Address(dst, count, sf, 0));
duke@435 1128 if (t == T_BYTE) {
duke@435 1129 // One byte misalignment happens only for byte arrays
duke@435 1130 __ testl(end, 1);
duke@435 1131 __ jccb(Assembler::zero, L_skip_align1);
duke@435 1132 __ decrement(count);
duke@435 1133 __ movb(rdx, Address(from, count, sf, 0));
duke@435 1134 __ movb(Address(to, count, sf, 0), rdx);
duke@435 1135 __ BIND(L_skip_align1);
duke@435 1136 }
duke@435 1137 // Two bytes misalignment happens only for byte and short (char) arrays
duke@435 1138 __ testl(end, 2);
duke@435 1139 __ jccb(Assembler::zero, L_skip_align2);
never@739 1140 __ subptr(count, 1<<(shift-1));
duke@435 1141 __ movw(rdx, Address(from, count, sf, 0));
duke@435 1142 __ movw(Address(to, count, sf, 0), rdx);
duke@435 1143 __ BIND(L_skip_align2);
duke@435 1144 __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
duke@435 1145 __ jcc(Assembler::below, L_copy_4_bytes);
duke@435 1146 }
duke@435 1147
duke@435 1148 if (!VM_Version::supports_mmx()) {
duke@435 1149 __ std();
never@739 1150 __ mov(rax, count); // Save 'count'
never@739 1151 __ mov(rdx, to); // Save 'to'
never@739 1152 __ lea(rsi, Address(from, count, sf, -4));
never@739 1153 __ lea(rdi, Address(to , count, sf, -4));
never@739 1154 __ shrptr(count, shift); // bytes count
never@739 1155 __ rep_mov();
duke@435 1156 __ cld();
never@739 1157 __ mov(count, rax); // restore 'count'
duke@435 1158 __ andl(count, (1<<shift)-1); // mask the number of rest elements
never@739 1159 __ movptr(from, Address(rsp, 12+4)); // reread 'from'
never@739 1160 __ mov(to, rdx); // restore 'to'
duke@435 1161 __ jmpb(L_copy_2_bytes); // all dword were copied
duke@435 1162 } else {
duke@435 1163 // Align to 8 bytes the end of array. It is aligned to 4 bytes already.
never@739 1164 __ testptr(end, 4);
duke@435 1165 __ jccb(Assembler::zero, L_copy_8_bytes);
duke@435 1166 __ subl(count, 1<<shift);
duke@435 1167 __ movl(rdx, Address(from, count, sf, 0));
duke@435 1168 __ movl(Address(to, count, sf, 0), rdx);
duke@435 1169 __ jmpb(L_copy_8_bytes);
duke@435 1170
kvn@1800 1171 __ align(OptoLoopAlignment);
duke@435 1172 // Move 8 bytes
duke@435 1173 __ BIND(L_copy_8_bytes_loop);
kvn@840 1174 if (UseXMMForArrayCopy) {
kvn@840 1175 __ movq(xmm0, Address(from, count, sf, 0));
kvn@840 1176 __ movq(Address(to, count, sf, 0), xmm0);
kvn@840 1177 } else {
kvn@840 1178 __ movq(mmx0, Address(from, count, sf, 0));
kvn@840 1179 __ movq(Address(to, count, sf, 0), mmx0);
kvn@840 1180 }
duke@435 1181 __ BIND(L_copy_8_bytes);
duke@435 1182 __ subl(count, 2<<shift);
duke@435 1183 __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
duke@435 1184 __ addl(count, 2<<shift);
kvn@840 1185 if (!UseXMMForArrayCopy) {
kvn@840 1186 __ emms();
kvn@840 1187 }
duke@435 1188 }
duke@435 1189 __ BIND(L_copy_4_bytes);
duke@435 1190 // copy prefix qword
duke@435 1191 __ testl(count, 1<<shift);
duke@435 1192 __ jccb(Assembler::zero, L_copy_2_bytes);
duke@435 1193 __ movl(rdx, Address(from, count, sf, -4));
duke@435 1194 __ movl(Address(to, count, sf, -4), rdx);
duke@435 1195
duke@435 1196 if (t == T_BYTE || t == T_SHORT) {
duke@435 1197 __ subl(count, (1<<shift));
duke@435 1198 __ BIND(L_copy_2_bytes);
duke@435 1199 // copy prefix dword
duke@435 1200 __ testl(count, 1<<(shift-1));
duke@435 1201 __ jccb(Assembler::zero, L_copy_byte);
duke@435 1202 __ movw(rdx, Address(from, count, sf, -2));
duke@435 1203 __ movw(Address(to, count, sf, -2), rdx);
duke@435 1204 if (t == T_BYTE) {
duke@435 1205 __ subl(count, 1<<(shift-1));
duke@435 1206 __ BIND(L_copy_byte);
duke@435 1207 // copy prefix byte
duke@435 1208 __ testl(count, 1);
duke@435 1209 __ jccb(Assembler::zero, L_exit);
duke@435 1210 __ movb(rdx, Address(from, 0));
duke@435 1211 __ movb(Address(to, 0), rdx);
duke@435 1212 __ BIND(L_exit);
duke@435 1213 } else {
duke@435 1214 __ BIND(L_copy_byte);
duke@435 1215 }
duke@435 1216 } else {
duke@435 1217 __ BIND(L_copy_2_bytes);
duke@435 1218 }
duke@435 1219 if (t == T_OBJECT) {
never@739 1220 __ movl2ptr(count, Address(rsp, 12+12)); // reread count
duke@435 1221 gen_write_ref_array_post_barrier(to, count);
duke@435 1222 __ BIND(L_0_count);
duke@435 1223 }
duke@435 1224 inc_copy_counter_np(t);
never@739 1225 __ pop(rdi);
never@739 1226 __ pop(rsi);
duke@435 1227 __ leave(); // required for proper stackwalking of RuntimeStub frame
never@739 1228 __ xorptr(rax, rax); // return 0
duke@435 1229 __ ret(0);
duke@435 1230 return start;
duke@435 1231 }
duke@435 1232
duke@435 1233
duke@435 1234 address generate_disjoint_long_copy(address* entry, const char *name) {
duke@435 1235 __ align(CodeEntryAlignment);
duke@435 1236 StubCodeMark mark(this, "StubRoutines", name);
duke@435 1237 address start = __ pc();
duke@435 1238
duke@435 1239 Label L_copy_8_bytes, L_copy_8_bytes_loop;
duke@435 1240 const Register from = rax; // source array address
duke@435 1241 const Register to = rdx; // destination array address
duke@435 1242 const Register count = rcx; // elements count
duke@435 1243 const Register to_from = rdx; // (to - from)
duke@435 1244
duke@435 1245 __ enter(); // required for proper stackwalking of RuntimeStub frame
never@739 1246 __ movptr(from , Address(rsp, 8+0)); // from
never@739 1247 __ movptr(to , Address(rsp, 8+4)); // to
never@739 1248 __ movl2ptr(count, Address(rsp, 8+8)); // count
duke@435 1249
duke@435 1250 *entry = __ pc(); // Entry point from conjoint arraycopy stub.
duke@435 1251 BLOCK_COMMENT("Entry:");
duke@435 1252
never@739 1253 __ subptr(to, from); // to --> to_from
duke@435 1254 if (VM_Version::supports_mmx()) {
kvn@840 1255 if (UseXMMForArrayCopy) {
kvn@840 1256 xmm_copy_forward(from, to_from, count);
kvn@840 1257 } else {
kvn@840 1258 mmx_copy_forward(from, to_from, count);
kvn@840 1259 }
duke@435 1260 } else {
duke@435 1261 __ jmpb(L_copy_8_bytes);
kvn@1800 1262 __ align(OptoLoopAlignment);
duke@435 1263 __ BIND(L_copy_8_bytes_loop);
duke@435 1264 __ fild_d(Address(from, 0));
duke@435 1265 __ fistp_d(Address(from, to_from, Address::times_1));
never@739 1266 __ addptr(from, 8);
duke@435 1267 __ BIND(L_copy_8_bytes);
duke@435 1268 __ decrement(count);
duke@435 1269 __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
duke@435 1270 }
duke@435 1271 inc_copy_counter_np(T_LONG);
duke@435 1272 __ leave(); // required for proper stackwalking of RuntimeStub frame
never@739 1273 __ xorptr(rax, rax); // return 0
duke@435 1274 __ ret(0);
duke@435 1275 return start;
duke@435 1276 }
duke@435 1277
duke@435 1278 address generate_conjoint_long_copy(address nooverlap_target,
duke@435 1279 address* entry, const char *name) {
duke@435 1280 __ align(CodeEntryAlignment);
duke@435 1281 StubCodeMark mark(this, "StubRoutines", name);
duke@435 1282 address start = __ pc();
duke@435 1283
duke@435 1284 Label L_copy_8_bytes, L_copy_8_bytes_loop;
duke@435 1285 const Register from = rax; // source array address
duke@435 1286 const Register to = rdx; // destination array address
duke@435 1287 const Register count = rcx; // elements count
duke@435 1288 const Register end_from = rax; // source array end address
duke@435 1289
duke@435 1290 __ enter(); // required for proper stackwalking of RuntimeStub frame
never@739 1291 __ movptr(from , Address(rsp, 8+0)); // from
never@739 1292 __ movptr(to , Address(rsp, 8+4)); // to
never@739 1293 __ movl2ptr(count, Address(rsp, 8+8)); // count
duke@435 1294
duke@435 1295 *entry = __ pc(); // Entry point from generic arraycopy stub.
duke@435 1296 BLOCK_COMMENT("Entry:");
duke@435 1297
duke@435 1298 // arrays overlap test
never@739 1299 __ cmpptr(to, from);
duke@435 1300 RuntimeAddress nooverlap(nooverlap_target);
duke@435 1301 __ jump_cc(Assembler::belowEqual, nooverlap);
never@739 1302 __ lea(end_from, Address(from, count, Address::times_8, 0));
never@739 1303 __ cmpptr(to, end_from);
never@739 1304 __ movptr(from, Address(rsp, 8)); // from
duke@435 1305 __ jump_cc(Assembler::aboveEqual, nooverlap);
duke@435 1306
duke@435 1307 __ jmpb(L_copy_8_bytes);
duke@435 1308
kvn@1800 1309 __ align(OptoLoopAlignment);
duke@435 1310 __ BIND(L_copy_8_bytes_loop);
duke@435 1311 if (VM_Version::supports_mmx()) {
kvn@840 1312 if (UseXMMForArrayCopy) {
kvn@840 1313 __ movq(xmm0, Address(from, count, Address::times_8));
kvn@840 1314 __ movq(Address(to, count, Address::times_8), xmm0);
kvn@840 1315 } else {
kvn@840 1316 __ movq(mmx0, Address(from, count, Address::times_8));
kvn@840 1317 __ movq(Address(to, count, Address::times_8), mmx0);
kvn@840 1318 }
duke@435 1319 } else {
duke@435 1320 __ fild_d(Address(from, count, Address::times_8));
duke@435 1321 __ fistp_d(Address(to, count, Address::times_8));
duke@435 1322 }
duke@435 1323 __ BIND(L_copy_8_bytes);
duke@435 1324 __ decrement(count);
duke@435 1325 __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
duke@435 1326
kvn@840 1327 if (VM_Version::supports_mmx() && !UseXMMForArrayCopy) {
duke@435 1328 __ emms();
duke@435 1329 }
duke@435 1330 inc_copy_counter_np(T_LONG);
duke@435 1331 __ leave(); // required for proper stackwalking of RuntimeStub frame
never@739 1332 __ xorptr(rax, rax); // return 0
duke@435 1333 __ ret(0);
duke@435 1334 return start;
duke@435 1335 }
duke@435 1336
duke@435 1337
duke@435 1338 // Helper for generating a dynamic type check.
duke@435 1339 // The sub_klass must be one of {rbx, rdx, rsi}.
duke@435 1340 // The temp is killed.
duke@435 1341 void generate_type_check(Register sub_klass,
duke@435 1342 Address& super_check_offset_addr,
duke@435 1343 Address& super_klass_addr,
duke@435 1344 Register temp,
jrose@1079 1345 Label* L_success, Label* L_failure) {
duke@435 1346 BLOCK_COMMENT("type_check:");
duke@435 1347
duke@435 1348 Label L_fallthrough;
jrose@1079 1349 #define LOCAL_JCC(assembler_con, label_ptr) \
jrose@1079 1350 if (label_ptr != NULL) __ jcc(assembler_con, *(label_ptr)); \
jrose@1079 1351 else __ jcc(assembler_con, L_fallthrough) /*omit semi*/
duke@435 1352
jrose@1079 1353 // The following is a strange variation of the fast path which requires
jrose@1079 1354 // one less register, because needed values are on the argument stack.
jrose@1079 1355 // __ check_klass_subtype_fast_path(sub_klass, *super_klass*, temp,
jrose@1079 1356 // L_success, L_failure, NULL);
duke@435 1357 assert_different_registers(sub_klass, temp);
duke@435 1358
stefank@3391 1359 int sc_offset = in_bytes(Klass::secondary_super_cache_offset());
duke@435 1360
duke@435 1361 // if the pointers are equal, we are done (e.g., String[] elements)
never@739 1362 __ cmpptr(sub_klass, super_klass_addr);
jrose@1079 1363 LOCAL_JCC(Assembler::equal, L_success);
duke@435 1364
duke@435 1365 // check the supertype display:
never@739 1366 __ movl2ptr(temp, super_check_offset_addr);
duke@435 1367 Address super_check_addr(sub_klass, temp, Address::times_1, 0);
never@739 1368 __ movptr(temp, super_check_addr); // load displayed supertype
never@739 1369 __ cmpptr(temp, super_klass_addr); // test the super type
jrose@1079 1370 LOCAL_JCC(Assembler::equal, L_success);
duke@435 1371
duke@435 1372 // if it was a primary super, we can just fail immediately
duke@435 1373 __ cmpl(super_check_offset_addr, sc_offset);
jrose@1079 1374 LOCAL_JCC(Assembler::notEqual, L_failure);
duke@435 1375
jrose@1079 1376 // The repne_scan instruction uses fixed registers, which will get spilled.
jrose@1079 1377 // We happen to know this works best when super_klass is in rax.
jrose@1079 1378 Register super_klass = temp;
jrose@1079 1379 __ movptr(super_klass, super_klass_addr);
jrose@1079 1380 __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg,
jrose@1079 1381 L_success, L_failure);
duke@435 1382
jrose@1079 1383 __ bind(L_fallthrough);
duke@435 1384
jrose@1079 1385 if (L_success == NULL) { BLOCK_COMMENT("L_success:"); }
jrose@1079 1386 if (L_failure == NULL) { BLOCK_COMMENT("L_failure:"); }
duke@435 1387
jrose@1079 1388 #undef LOCAL_JCC
duke@435 1389 }
duke@435 1390
duke@435 1391 //
duke@435 1392 // Generate checkcasting array copy stub
duke@435 1393 //
duke@435 1394 // Input:
duke@435 1395 // 4(rsp) - source array address
duke@435 1396 // 8(rsp) - destination array address
duke@435 1397 // 12(rsp) - element count, can be zero
duke@435 1398 // 16(rsp) - size_t ckoff (super_check_offset)
duke@435 1399 // 20(rsp) - oop ckval (super_klass)
duke@435 1400 //
duke@435 1401 // Output:
duke@435 1402 // rax, == 0 - success
duke@435 1403 // rax, == -1^K - failure, where K is partial transfer count
duke@435 1404 //
iveresov@2606 1405 address generate_checkcast_copy(const char *name, address* entry, bool dest_uninitialized = false) {
duke@435 1406 __ align(CodeEntryAlignment);
duke@435 1407 StubCodeMark mark(this, "StubRoutines", name);
duke@435 1408 address start = __ pc();
duke@435 1409
duke@435 1410 Label L_load_element, L_store_element, L_do_card_marks, L_done;
duke@435 1411
duke@435 1412 // register use:
duke@435 1413 // rax, rdx, rcx -- loop control (end_from, end_to, count)
duke@435 1414 // rdi, rsi -- element access (oop, klass)
duke@435 1415 // rbx, -- temp
duke@435 1416 const Register from = rax; // source array address
duke@435 1417 const Register to = rdx; // destination array address
duke@435 1418 const Register length = rcx; // elements count
duke@435 1419 const Register elem = rdi; // each oop copied
duke@435 1420 const Register elem_klass = rsi; // each elem._klass (sub_klass)
duke@435 1421 const Register temp = rbx; // lone remaining temp
duke@435 1422
duke@435 1423 __ enter(); // required for proper stackwalking of RuntimeStub frame
duke@435 1424
never@739 1425 __ push(rsi);
never@739 1426 __ push(rdi);
never@739 1427 __ push(rbx);
duke@435 1428
duke@435 1429 Address from_arg(rsp, 16+ 4); // from
duke@435 1430 Address to_arg(rsp, 16+ 8); // to
duke@435 1431 Address length_arg(rsp, 16+12); // elements count
duke@435 1432 Address ckoff_arg(rsp, 16+16); // super_check_offset
duke@435 1433 Address ckval_arg(rsp, 16+20); // super_klass
duke@435 1434
duke@435 1435 // Load up:
never@739 1436 __ movptr(from, from_arg);
never@739 1437 __ movptr(to, to_arg);
never@739 1438 __ movl2ptr(length, length_arg);
duke@435 1439
iveresov@2595 1440 if (entry != NULL) {
iveresov@2595 1441 *entry = __ pc(); // Entry point from generic arraycopy stub.
iveresov@2595 1442 BLOCK_COMMENT("Entry:");
iveresov@2595 1443 }
duke@435 1444
duke@435 1445 //---------------------------------------------------------------
duke@435 1446 // Assembler stub will be used for this call to arraycopy
duke@435 1447 // if the two arrays are subtypes of Object[] but the
duke@435 1448 // destination array type is not equal to or a supertype
duke@435 1449 // of the source type. Each element must be separately
duke@435 1450 // checked.
duke@435 1451
duke@435 1452 // Loop-invariant addresses. They are exclusive end pointers.
never@739 1453 Address end_from_addr(from, length, Address::times_ptr, 0);
never@739 1454 Address end_to_addr(to, length, Address::times_ptr, 0);
duke@435 1455
duke@435 1456 Register end_from = from; // re-use
duke@435 1457 Register end_to = to; // re-use
duke@435 1458 Register count = length; // re-use
duke@435 1459
duke@435 1460 // Loop-variant addresses. They assume post-incremented count < 0.
never@739 1461 Address from_element_addr(end_from, count, Address::times_ptr, 0);
never@739 1462 Address to_element_addr(end_to, count, Address::times_ptr, 0);
duke@435 1463 Address elem_klass_addr(elem, oopDesc::klass_offset_in_bytes());
duke@435 1464
duke@435 1465 // Copy from low to high addresses, indexed from the end of each array.
iveresov@2606 1466 gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
never@739 1467 __ lea(end_from, end_from_addr);
never@739 1468 __ lea(end_to, end_to_addr);
duke@435 1469 assert(length == count, ""); // else fix next line:
never@739 1470 __ negptr(count); // negate and test the length
duke@435 1471 __ jccb(Assembler::notZero, L_load_element);
duke@435 1472
duke@435 1473 // Empty array: Nothing to do.
never@739 1474 __ xorptr(rax, rax); // return 0 on (trivial) success
duke@435 1475 __ jmp(L_done);
duke@435 1476
duke@435 1477 // ======== begin loop ========
duke@435 1478 // (Loop is rotated; its entry is L_load_element.)
duke@435 1479 // Loop control:
duke@435 1480 // for (count = -count; count != 0; count++)
duke@435 1481 // Base pointers src, dst are biased by 8*count,to last element.
kvn@1800 1482 __ align(OptoLoopAlignment);
duke@435 1483
duke@435 1484 __ BIND(L_store_element);
never@739 1485 __ movptr(to_element_addr, elem); // store the oop
duke@435 1486 __ increment(count); // increment the count toward zero
duke@435 1487 __ jccb(Assembler::zero, L_do_card_marks);
duke@435 1488
duke@435 1489 // ======== loop entry is here ========
duke@435 1490 __ BIND(L_load_element);
never@739 1491 __ movptr(elem, from_element_addr); // load the oop
never@739 1492 __ testptr(elem, elem);
duke@435 1493 __ jccb(Assembler::zero, L_store_element);
duke@435 1494
duke@435 1495 // (Could do a trick here: Remember last successful non-null
duke@435 1496 // element stored and make a quick oop equality check on it.)
duke@435 1497
never@739 1498 __ movptr(elem_klass, elem_klass_addr); // query the object klass
duke@435 1499 generate_type_check(elem_klass, ckoff_arg, ckval_arg, temp,
duke@435 1500 &L_store_element, NULL);
kvn@5156 1501 // (On fall-through, we have failed the element type check.)
duke@435 1502 // ======== end loop ========
duke@435 1503
duke@435 1504 // It was a real error; we must depend on the caller to finish the job.
rasbold@454 1505 // Register "count" = -1 * number of *remaining* oops, length_arg = *total* oops.
rasbold@454 1506 // Emit GC store barriers for the oops we have copied (length_arg + count),
duke@435 1507 // and report their number to the caller.
kvn@5156 1508 assert_different_registers(to, count, rax);
kvn@5156 1509 Label L_post_barrier;
duke@435 1510 __ addl(count, length_arg); // transfers = (length - remaining)
never@739 1511 __ movl2ptr(rax, count); // save the value
kvn@5156 1512 __ notptr(rax); // report (-1^K) to caller (does not affect flags)
kvn@5156 1513 __ jccb(Assembler::notZero, L_post_barrier);
kvn@5156 1514 __ jmp(L_done); // K == 0, nothing was copied, skip post barrier
duke@435 1515
duke@435 1516 // Come here on success only.
duke@435 1517 __ BIND(L_do_card_marks);
kvn@5156 1518 __ xorptr(rax, rax); // return 0 on success
never@739 1519 __ movl2ptr(count, length_arg);
kvn@5156 1520
kvn@5156 1521 __ BIND(L_post_barrier);
kvn@5156 1522 __ movptr(to, to_arg); // reload
duke@435 1523 gen_write_ref_array_post_barrier(to, count);
duke@435 1524
duke@435 1525 // Common exit point (success or failure).
duke@435 1526 __ BIND(L_done);
never@739 1527 __ pop(rbx);
never@739 1528 __ pop(rdi);
never@739 1529 __ pop(rsi);
duke@435 1530 inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr);
duke@435 1531 __ leave(); // required for proper stackwalking of RuntimeStub frame
duke@435 1532 __ ret(0);
duke@435 1533
duke@435 1534 return start;
duke@435 1535 }
duke@435 1536
duke@435 1537 //
duke@435 1538 // Generate 'unsafe' array copy stub
duke@435 1539 // Though just as safe as the other stubs, it takes an unscaled
duke@435 1540 // size_t argument instead of an element count.
duke@435 1541 //
duke@435 1542 // Input:
duke@435 1543 // 4(rsp) - source array address
duke@435 1544 // 8(rsp) - destination array address
duke@435 1545 // 12(rsp) - byte count, can be zero
duke@435 1546 //
duke@435 1547 // Output:
duke@435 1548 // rax, == 0 - success
duke@435 1549 // rax, == -1 - need to call System.arraycopy
duke@435 1550 //
duke@435 1551 // Examines the alignment of the operands and dispatches
duke@435 1552 // to a long, int, short, or byte copy loop.
duke@435 1553 //
duke@435 1554 address generate_unsafe_copy(const char *name,
duke@435 1555 address byte_copy_entry,
duke@435 1556 address short_copy_entry,
duke@435 1557 address int_copy_entry,
duke@435 1558 address long_copy_entry) {
duke@435 1559
duke@435 1560 Label L_long_aligned, L_int_aligned, L_short_aligned;
duke@435 1561
duke@435 1562 __ align(CodeEntryAlignment);
duke@435 1563 StubCodeMark mark(this, "StubRoutines", name);
duke@435 1564 address start = __ pc();
duke@435 1565
duke@435 1566 const Register from = rax; // source array address
duke@435 1567 const Register to = rdx; // destination array address
duke@435 1568 const Register count = rcx; // elements count
duke@435 1569
duke@435 1570 __ enter(); // required for proper stackwalking of RuntimeStub frame
never@739 1571 __ push(rsi);
never@739 1572 __ push(rdi);
duke@435 1573 Address from_arg(rsp, 12+ 4); // from
duke@435 1574 Address to_arg(rsp, 12+ 8); // to
duke@435 1575 Address count_arg(rsp, 12+12); // byte count
duke@435 1576
duke@435 1577 // Load up:
never@739 1578 __ movptr(from , from_arg);
never@739 1579 __ movptr(to , to_arg);
never@739 1580 __ movl2ptr(count, count_arg);
duke@435 1581
duke@435 1582 // bump this on entry, not on exit:
duke@435 1583 inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
duke@435 1584
duke@435 1585 const Register bits = rsi;
never@739 1586 __ mov(bits, from);
never@739 1587 __ orptr(bits, to);
never@739 1588 __ orptr(bits, count);
duke@435 1589
duke@435 1590 __ testl(bits, BytesPerLong-1);
duke@435 1591 __ jccb(Assembler::zero, L_long_aligned);
duke@435 1592
duke@435 1593 __ testl(bits, BytesPerInt-1);
duke@435 1594 __ jccb(Assembler::zero, L_int_aligned);
duke@435 1595
duke@435 1596 __ testl(bits, BytesPerShort-1);
duke@435 1597 __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
duke@435 1598
duke@435 1599 __ BIND(L_short_aligned);
never@739 1600 __ shrptr(count, LogBytesPerShort); // size => short_count
duke@435 1601 __ movl(count_arg, count); // update 'count'
duke@435 1602 __ jump(RuntimeAddress(short_copy_entry));
duke@435 1603
duke@435 1604 __ BIND(L_int_aligned);
never@739 1605 __ shrptr(count, LogBytesPerInt); // size => int_count
duke@435 1606 __ movl(count_arg, count); // update 'count'
duke@435 1607 __ jump(RuntimeAddress(int_copy_entry));
duke@435 1608
duke@435 1609 __ BIND(L_long_aligned);
never@739 1610 __ shrptr(count, LogBytesPerLong); // size => qword_count
duke@435 1611 __ movl(count_arg, count); // update 'count'
never@739 1612 __ pop(rdi); // Do pops here since jlong_arraycopy stub does not do it.
never@739 1613 __ pop(rsi);
duke@435 1614 __ jump(RuntimeAddress(long_copy_entry));
duke@435 1615
duke@435 1616 return start;
duke@435 1617 }
duke@435 1618
duke@435 1619
duke@435 1620 // Perform range checks on the proposed arraycopy.
duke@435 1621 // Smashes src_pos and dst_pos. (Uses them up for temps.)
duke@435 1622 void arraycopy_range_checks(Register src,
duke@435 1623 Register src_pos,
duke@435 1624 Register dst,
duke@435 1625 Register dst_pos,
duke@435 1626 Address& length,
duke@435 1627 Label& L_failed) {
duke@435 1628 BLOCK_COMMENT("arraycopy_range_checks:");
duke@435 1629 const Register src_end = src_pos; // source array end position
duke@435 1630 const Register dst_end = dst_pos; // destination array end position
duke@435 1631 __ addl(src_end, length); // src_pos + length
duke@435 1632 __ addl(dst_end, length); // dst_pos + length
duke@435 1633
duke@435 1634 // if (src_pos + length > arrayOop(src)->length() ) FAIL;
duke@435 1635 __ cmpl(src_end, Address(src, arrayOopDesc::length_offset_in_bytes()));
duke@435 1636 __ jcc(Assembler::above, L_failed);
duke@435 1637
duke@435 1638 // if (dst_pos + length > arrayOop(dst)->length() ) FAIL;
duke@435 1639 __ cmpl(dst_end, Address(dst, arrayOopDesc::length_offset_in_bytes()));
duke@435 1640 __ jcc(Assembler::above, L_failed);
duke@435 1641
duke@435 1642 BLOCK_COMMENT("arraycopy_range_checks done");
duke@435 1643 }
duke@435 1644
duke@435 1645
duke@435 1646 //
duke@435 1647 // Generate generic array copy stubs
duke@435 1648 //
duke@435 1649 // Input:
duke@435 1650 // 4(rsp) - src oop
duke@435 1651 // 8(rsp) - src_pos
duke@435 1652 // 12(rsp) - dst oop
duke@435 1653 // 16(rsp) - dst_pos
duke@435 1654 // 20(rsp) - element count
duke@435 1655 //
duke@435 1656 // Output:
duke@435 1657 // rax, == 0 - success
duke@435 1658 // rax, == -1^K - failure, where K is partial transfer count
duke@435 1659 //
duke@435 1660 address generate_generic_copy(const char *name,
duke@435 1661 address entry_jbyte_arraycopy,
duke@435 1662 address entry_jshort_arraycopy,
duke@435 1663 address entry_jint_arraycopy,
duke@435 1664 address entry_oop_arraycopy,
duke@435 1665 address entry_jlong_arraycopy,
duke@435 1666 address entry_checkcast_arraycopy) {
duke@435 1667 Label L_failed, L_failed_0, L_objArray;
duke@435 1668
duke@435 1669 { int modulus = CodeEntryAlignment;
duke@435 1670 int target = modulus - 5; // 5 = sizeof jmp(L_failed)
duke@435 1671 int advance = target - (__ offset() % modulus);
duke@435 1672 if (advance < 0) advance += modulus;
duke@435 1673 if (advance > 0) __ nop(advance);
duke@435 1674 }
duke@435 1675 StubCodeMark mark(this, "StubRoutines", name);
duke@435 1676
duke@435 1677 // Short-hop target to L_failed. Makes for denser prologue code.
duke@435 1678 __ BIND(L_failed_0);
duke@435 1679 __ jmp(L_failed);
duke@435 1680 assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
duke@435 1681
duke@435 1682 __ align(CodeEntryAlignment);
duke@435 1683 address start = __ pc();
duke@435 1684
duke@435 1685 __ enter(); // required for proper stackwalking of RuntimeStub frame
never@739 1686 __ push(rsi);
never@739 1687 __ push(rdi);
duke@435 1688
duke@435 1689 // bump this on entry, not on exit:
duke@435 1690 inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
duke@435 1691
duke@435 1692 // Input values
duke@435 1693 Address SRC (rsp, 12+ 4);
duke@435 1694 Address SRC_POS (rsp, 12+ 8);
duke@435 1695 Address DST (rsp, 12+12);
duke@435 1696 Address DST_POS (rsp, 12+16);
duke@435 1697 Address LENGTH (rsp, 12+20);
duke@435 1698
duke@435 1699 //-----------------------------------------------------------------------
duke@435 1700 // Assembler stub will be used for this call to arraycopy
duke@435 1701 // if the following conditions are met:
duke@435 1702 //
duke@435 1703 // (1) src and dst must not be null.
duke@435 1704 // (2) src_pos must not be negative.
duke@435 1705 // (3) dst_pos must not be negative.
duke@435 1706 // (4) length must not be negative.
duke@435 1707 // (5) src klass and dst klass should be the same and not NULL.
duke@435 1708 // (6) src and dst should be arrays.
duke@435 1709 // (7) src_pos + length must not exceed length of src.
duke@435 1710 // (8) dst_pos + length must not exceed length of dst.
duke@435 1711 //
duke@435 1712
duke@435 1713 const Register src = rax; // source array oop
duke@435 1714 const Register src_pos = rsi;
duke@435 1715 const Register dst = rdx; // destination array oop
duke@435 1716 const Register dst_pos = rdi;
duke@435 1717 const Register length = rcx; // transfer count
duke@435 1718
duke@435 1719 // if (src == NULL) return -1;
never@739 1720 __ movptr(src, SRC); // src oop
never@739 1721 __ testptr(src, src);
duke@435 1722 __ jccb(Assembler::zero, L_failed_0);
duke@435 1723
duke@435 1724 // if (src_pos < 0) return -1;
never@739 1725 __ movl2ptr(src_pos, SRC_POS); // src_pos
duke@435 1726 __ testl(src_pos, src_pos);
duke@435 1727 __ jccb(Assembler::negative, L_failed_0);
duke@435 1728
duke@435 1729 // if (dst == NULL) return -1;
never@739 1730 __ movptr(dst, DST); // dst oop
never@739 1731 __ testptr(dst, dst);
duke@435 1732 __ jccb(Assembler::zero, L_failed_0);
duke@435 1733
duke@435 1734 // if (dst_pos < 0) return -1;
never@739 1735 __ movl2ptr(dst_pos, DST_POS); // dst_pos
duke@435 1736 __ testl(dst_pos, dst_pos);
duke@435 1737 __ jccb(Assembler::negative, L_failed_0);
duke@435 1738
duke@435 1739 // if (length < 0) return -1;
never@739 1740 __ movl2ptr(length, LENGTH); // length
duke@435 1741 __ testl(length, length);
duke@435 1742 __ jccb(Assembler::negative, L_failed_0);
duke@435 1743
duke@435 1744 // if (src->klass() == NULL) return -1;
duke@435 1745 Address src_klass_addr(src, oopDesc::klass_offset_in_bytes());
duke@435 1746 Address dst_klass_addr(dst, oopDesc::klass_offset_in_bytes());
duke@435 1747 const Register rcx_src_klass = rcx; // array klass
never@739 1748 __ movptr(rcx_src_klass, Address(src, oopDesc::klass_offset_in_bytes()));
duke@435 1749
duke@435 1750 #ifdef ASSERT
duke@435 1751 // assert(src->klass() != NULL);
duke@435 1752 BLOCK_COMMENT("assert klasses not null");
duke@435 1753 { Label L1, L2;
never@739 1754 __ testptr(rcx_src_klass, rcx_src_klass);
duke@435 1755 __ jccb(Assembler::notZero, L2); // it is broken if klass is NULL
duke@435 1756 __ bind(L1);
duke@435 1757 __ stop("broken null klass");
duke@435 1758 __ bind(L2);
never@739 1759 __ cmpptr(dst_klass_addr, (int32_t)NULL_WORD);
duke@435 1760 __ jccb(Assembler::equal, L1); // this would be broken also
duke@435 1761 BLOCK_COMMENT("assert done");
duke@435 1762 }
duke@435 1763 #endif //ASSERT
duke@435 1764
duke@435 1765 // Load layout helper (32-bits)
duke@435 1766 //
duke@435 1767 // |array_tag| | header_size | element_type | |log2_element_size|
duke@435 1768 // 32 30 24 16 8 2 0
duke@435 1769 //
duke@435 1770 // array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
duke@435 1771 //
duke@435 1772
stefank@3391 1773 int lh_offset = in_bytes(Klass::layout_helper_offset());
duke@435 1774 Address src_klass_lh_addr(rcx_src_klass, lh_offset);
duke@435 1775
duke@435 1776 // Handle objArrays completely differently...
duke@435 1777 jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
duke@435 1778 __ cmpl(src_klass_lh_addr, objArray_lh);
duke@435 1779 __ jcc(Assembler::equal, L_objArray);
duke@435 1780
duke@435 1781 // if (src->klass() != dst->klass()) return -1;
never@739 1782 __ cmpptr(rcx_src_klass, dst_klass_addr);
duke@435 1783 __ jccb(Assembler::notEqual, L_failed_0);
duke@435 1784
duke@435 1785 const Register rcx_lh = rcx; // layout helper
duke@435 1786 assert(rcx_lh == rcx_src_klass, "known alias");
duke@435 1787 __ movl(rcx_lh, src_klass_lh_addr);
duke@435 1788
duke@435 1789 // if (!src->is_Array()) return -1;
duke@435 1790 __ cmpl(rcx_lh, Klass::_lh_neutral_value);
duke@435 1791 __ jcc(Assembler::greaterEqual, L_failed_0); // signed cmp
duke@435 1792
duke@435 1793 // At this point, it is known to be a typeArray (array_tag 0x3).
duke@435 1794 #ifdef ASSERT
duke@435 1795 { Label L;
duke@435 1796 __ cmpl(rcx_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
duke@435 1797 __ jcc(Assembler::greaterEqual, L); // signed cmp
duke@435 1798 __ stop("must be a primitive array");
duke@435 1799 __ bind(L);
duke@435 1800 }
duke@435 1801 #endif
duke@435 1802
duke@435 1803 assert_different_registers(src, src_pos, dst, dst_pos, rcx_lh);
duke@435 1804 arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
duke@435 1805
coleenp@4142 1806 // TypeArrayKlass
duke@435 1807 //
duke@435 1808 // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
duke@435 1809 // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
duke@435 1810 //
duke@435 1811 const Register rsi_offset = rsi; // array offset
duke@435 1812 const Register src_array = src; // src array offset
duke@435 1813 const Register dst_array = dst; // dst array offset
duke@435 1814 const Register rdi_elsize = rdi; // log2 element size
duke@435 1815
never@739 1816 __ mov(rsi_offset, rcx_lh);
never@739 1817 __ shrptr(rsi_offset, Klass::_lh_header_size_shift);
never@739 1818 __ andptr(rsi_offset, Klass::_lh_header_size_mask); // array_offset
never@739 1819 __ addptr(src_array, rsi_offset); // src array offset
never@739 1820 __ addptr(dst_array, rsi_offset); // dst array offset
never@739 1821 __ andptr(rcx_lh, Klass::_lh_log2_element_size_mask); // log2 elsize
duke@435 1822
duke@435 1823 // next registers should be set before the jump to corresponding stub
duke@435 1824 const Register from = src; // source array address
duke@435 1825 const Register to = dst; // destination array address
duke@435 1826 const Register count = rcx; // elements count
duke@435 1827 // some of them should be duplicated on stack
duke@435 1828 #define FROM Address(rsp, 12+ 4)
duke@435 1829 #define TO Address(rsp, 12+ 8) // Not used now
duke@435 1830 #define COUNT Address(rsp, 12+12) // Only for oop arraycopy
duke@435 1831
duke@435 1832 BLOCK_COMMENT("scale indexes to element size");
never@739 1833 __ movl2ptr(rsi, SRC_POS); // src_pos
never@739 1834 __ shlptr(rsi); // src_pos << rcx (log2 elsize)
duke@435 1835 assert(src_array == from, "");
never@739 1836 __ addptr(from, rsi); // from = src_array + SRC_POS << log2 elsize
never@739 1837 __ movl2ptr(rdi, DST_POS); // dst_pos
never@739 1838 __ shlptr(rdi); // dst_pos << rcx (log2 elsize)
duke@435 1839 assert(dst_array == to, "");
never@739 1840 __ addptr(to, rdi); // to = dst_array + DST_POS << log2 elsize
never@739 1841 __ movptr(FROM, from); // src_addr
never@739 1842 __ mov(rdi_elsize, rcx_lh); // log2 elsize
never@739 1843 __ movl2ptr(count, LENGTH); // elements count
duke@435 1844
duke@435 1845 BLOCK_COMMENT("choose copy loop based on element size");
duke@435 1846 __ cmpl(rdi_elsize, 0);
duke@435 1847
duke@435 1848 __ jump_cc(Assembler::equal, RuntimeAddress(entry_jbyte_arraycopy));
duke@435 1849 __ cmpl(rdi_elsize, LogBytesPerShort);
duke@435 1850 __ jump_cc(Assembler::equal, RuntimeAddress(entry_jshort_arraycopy));
duke@435 1851 __ cmpl(rdi_elsize, LogBytesPerInt);
duke@435 1852 __ jump_cc(Assembler::equal, RuntimeAddress(entry_jint_arraycopy));
duke@435 1853 #ifdef ASSERT
duke@435 1854 __ cmpl(rdi_elsize, LogBytesPerLong);
duke@435 1855 __ jccb(Assembler::notEqual, L_failed);
duke@435 1856 #endif
never@739 1857 __ pop(rdi); // Do pops here since jlong_arraycopy stub does not do it.
never@739 1858 __ pop(rsi);
duke@435 1859 __ jump(RuntimeAddress(entry_jlong_arraycopy));
duke@435 1860
duke@435 1861 __ BIND(L_failed);
never@739 1862 __ xorptr(rax, rax);
never@739 1863 __ notptr(rax); // return -1
never@739 1864 __ pop(rdi);
never@739 1865 __ pop(rsi);
duke@435 1866 __ leave(); // required for proper stackwalking of RuntimeStub frame
duke@435 1867 __ ret(0);
duke@435 1868
coleenp@4142 1869 // ObjArrayKlass
duke@435 1870 __ BIND(L_objArray);
duke@435 1871 // live at this point: rcx_src_klass, src[_pos], dst[_pos]
duke@435 1872
duke@435 1873 Label L_plain_copy, L_checkcast_copy;
duke@435 1874 // test array classes for subtyping
never@739 1875 __ cmpptr(rcx_src_klass, dst_klass_addr); // usual case is exact equality
duke@435 1876 __ jccb(Assembler::notEqual, L_checkcast_copy);
duke@435 1877
duke@435 1878 // Identically typed arrays can be copied without element-wise checks.
duke@435 1879 assert_different_registers(src, src_pos, dst, dst_pos, rcx_src_klass);
duke@435 1880 arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
duke@435 1881
duke@435 1882 __ BIND(L_plain_copy);
never@739 1883 __ movl2ptr(count, LENGTH); // elements count
never@739 1884 __ movl2ptr(src_pos, SRC_POS); // reload src_pos
never@739 1885 __ lea(from, Address(src, src_pos, Address::times_ptr,
never@739 1886 arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
never@739 1887 __ movl2ptr(dst_pos, DST_POS); // reload dst_pos
never@739 1888 __ lea(to, Address(dst, dst_pos, Address::times_ptr,
never@739 1889 arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
never@739 1890 __ movptr(FROM, from); // src_addr
never@739 1891 __ movptr(TO, to); // dst_addr
duke@435 1892 __ movl(COUNT, count); // count
duke@435 1893 __ jump(RuntimeAddress(entry_oop_arraycopy));
duke@435 1894
duke@435 1895 __ BIND(L_checkcast_copy);
duke@435 1896 // live at this point: rcx_src_klass, dst[_pos], src[_pos]
duke@435 1897 {
duke@435 1898 // Handy offsets:
coleenp@4142 1899 int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
stefank@3391 1900 int sco_offset = in_bytes(Klass::super_check_offset_offset());
duke@435 1901
duke@435 1902 Register rsi_dst_klass = rsi;
duke@435 1903 Register rdi_temp = rdi;
duke@435 1904 assert(rsi_dst_klass == src_pos, "expected alias w/ src_pos");
duke@435 1905 assert(rdi_temp == dst_pos, "expected alias w/ dst_pos");
duke@435 1906 Address dst_klass_lh_addr(rsi_dst_klass, lh_offset);
duke@435 1907
duke@435 1908 // Before looking at dst.length, make sure dst is also an objArray.
never@739 1909 __ movptr(rsi_dst_klass, dst_klass_addr);
duke@435 1910 __ cmpl(dst_klass_lh_addr, objArray_lh);
duke@435 1911 __ jccb(Assembler::notEqual, L_failed);
duke@435 1912
duke@435 1913 // It is safe to examine both src.length and dst.length.
never@739 1914 __ movl2ptr(src_pos, SRC_POS); // reload rsi
duke@435 1915 arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
duke@435 1916 // (Now src_pos and dst_pos are killed, but not src and dst.)
duke@435 1917
duke@435 1918 // We'll need this temp (don't forget to pop it after the type check).
never@739 1919 __ push(rbx);
duke@435 1920 Register rbx_src_klass = rbx;
duke@435 1921
never@739 1922 __ mov(rbx_src_klass, rcx_src_klass); // spill away from rcx
never@739 1923 __ movptr(rsi_dst_klass, dst_klass_addr);
duke@435 1924 Address super_check_offset_addr(rsi_dst_klass, sco_offset);
duke@435 1925 Label L_fail_array_check;
duke@435 1926 generate_type_check(rbx_src_klass,
duke@435 1927 super_check_offset_addr, dst_klass_addr,
duke@435 1928 rdi_temp, NULL, &L_fail_array_check);
duke@435 1929 // (On fall-through, we have passed the array type check.)
never@739 1930 __ pop(rbx);
duke@435 1931 __ jmp(L_plain_copy);
duke@435 1932
duke@435 1933 __ BIND(L_fail_array_check);
duke@435 1934 // Reshuffle arguments so we can call checkcast_arraycopy:
duke@435 1935
duke@435 1936 // match initial saves for checkcast_arraycopy
never@739 1937 // push(rsi); // already done; see above
never@739 1938 // push(rdi); // already done; see above
never@739 1939 // push(rbx); // already done; see above
duke@435 1940
duke@435 1941 // Marshal outgoing arguments now, freeing registers.
duke@435 1942 Address from_arg(rsp, 16+ 4); // from
duke@435 1943 Address to_arg(rsp, 16+ 8); // to
duke@435 1944 Address length_arg(rsp, 16+12); // elements count
duke@435 1945 Address ckoff_arg(rsp, 16+16); // super_check_offset
duke@435 1946 Address ckval_arg(rsp, 16+20); // super_klass
duke@435 1947
duke@435 1948 Address SRC_POS_arg(rsp, 16+ 8);
duke@435 1949 Address DST_POS_arg(rsp, 16+16);
duke@435 1950 Address LENGTH_arg(rsp, 16+20);
duke@435 1951 // push rbx, changed the incoming offsets (why not just use rbp,??)
duke@435 1952 // assert(SRC_POS_arg.disp() == SRC_POS.disp() + 4, "");
duke@435 1953
never@739 1954 __ movptr(rbx, Address(rsi_dst_klass, ek_offset));
never@739 1955 __ movl2ptr(length, LENGTH_arg); // reload elements count
never@739 1956 __ movl2ptr(src_pos, SRC_POS_arg); // reload src_pos
never@739 1957 __ movl2ptr(dst_pos, DST_POS_arg); // reload dst_pos
duke@435 1958
never@739 1959 __ movptr(ckval_arg, rbx); // destination element type
duke@435 1960 __ movl(rbx, Address(rbx, sco_offset));
duke@435 1961 __ movl(ckoff_arg, rbx); // corresponding class check offset
duke@435 1962
duke@435 1963 __ movl(length_arg, length); // outgoing length argument
duke@435 1964
never@739 1965 __ lea(from, Address(src, src_pos, Address::times_ptr,
duke@435 1966 arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
never@739 1967 __ movptr(from_arg, from);
duke@435 1968
never@739 1969 __ lea(to, Address(dst, dst_pos, Address::times_ptr,
duke@435 1970 arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
never@739 1971 __ movptr(to_arg, to);
duke@435 1972 __ jump(RuntimeAddress(entry_checkcast_arraycopy));
duke@435 1973 }
duke@435 1974
duke@435 1975 return start;
duke@435 1976 }
duke@435 1977
duke@435 1978 void generate_arraycopy_stubs() {
duke@435 1979 address entry;
duke@435 1980 address entry_jbyte_arraycopy;
duke@435 1981 address entry_jshort_arraycopy;
duke@435 1982 address entry_jint_arraycopy;
duke@435 1983 address entry_oop_arraycopy;
duke@435 1984 address entry_jlong_arraycopy;
duke@435 1985 address entry_checkcast_arraycopy;
duke@435 1986
duke@435 1987 StubRoutines::_arrayof_jbyte_disjoint_arraycopy =
duke@435 1988 generate_disjoint_copy(T_BYTE, true, Address::times_1, &entry,
duke@435 1989 "arrayof_jbyte_disjoint_arraycopy");
duke@435 1990 StubRoutines::_arrayof_jbyte_arraycopy =
duke@435 1991 generate_conjoint_copy(T_BYTE, true, Address::times_1, entry,
duke@435 1992 NULL, "arrayof_jbyte_arraycopy");
duke@435 1993 StubRoutines::_jbyte_disjoint_arraycopy =
duke@435 1994 generate_disjoint_copy(T_BYTE, false, Address::times_1, &entry,
duke@435 1995 "jbyte_disjoint_arraycopy");
duke@435 1996 StubRoutines::_jbyte_arraycopy =
duke@435 1997 generate_conjoint_copy(T_BYTE, false, Address::times_1, entry,
duke@435 1998 &entry_jbyte_arraycopy, "jbyte_arraycopy");
duke@435 1999
duke@435 2000 StubRoutines::_arrayof_jshort_disjoint_arraycopy =
duke@435 2001 generate_disjoint_copy(T_SHORT, true, Address::times_2, &entry,
duke@435 2002 "arrayof_jshort_disjoint_arraycopy");
duke@435 2003 StubRoutines::_arrayof_jshort_arraycopy =
duke@435 2004 generate_conjoint_copy(T_SHORT, true, Address::times_2, entry,
duke@435 2005 NULL, "arrayof_jshort_arraycopy");
duke@435 2006 StubRoutines::_jshort_disjoint_arraycopy =
duke@435 2007 generate_disjoint_copy(T_SHORT, false, Address::times_2, &entry,
duke@435 2008 "jshort_disjoint_arraycopy");
duke@435 2009 StubRoutines::_jshort_arraycopy =
duke@435 2010 generate_conjoint_copy(T_SHORT, false, Address::times_2, entry,
duke@435 2011 &entry_jshort_arraycopy, "jshort_arraycopy");
duke@435 2012
duke@435 2013 // Next arrays are always aligned on 4 bytes at least.
duke@435 2014 StubRoutines::_jint_disjoint_arraycopy =
duke@435 2015 generate_disjoint_copy(T_INT, true, Address::times_4, &entry,
duke@435 2016 "jint_disjoint_arraycopy");
duke@435 2017 StubRoutines::_jint_arraycopy =
duke@435 2018 generate_conjoint_copy(T_INT, true, Address::times_4, entry,
duke@435 2019 &entry_jint_arraycopy, "jint_arraycopy");
duke@435 2020
duke@435 2021 StubRoutines::_oop_disjoint_arraycopy =
never@739 2022 generate_disjoint_copy(T_OBJECT, true, Address::times_ptr, &entry,
duke@435 2023 "oop_disjoint_arraycopy");
duke@435 2024 StubRoutines::_oop_arraycopy =
never@739 2025 generate_conjoint_copy(T_OBJECT, true, Address::times_ptr, entry,
duke@435 2026 &entry_oop_arraycopy, "oop_arraycopy");
duke@435 2027
iveresov@2606 2028 StubRoutines::_oop_disjoint_arraycopy_uninit =
iveresov@2606 2029 generate_disjoint_copy(T_OBJECT, true, Address::times_ptr, &entry,
iveresov@2606 2030 "oop_disjoint_arraycopy_uninit",
iveresov@2606 2031 /*dest_uninitialized*/true);
iveresov@2606 2032 StubRoutines::_oop_arraycopy_uninit =
iveresov@2606 2033 generate_conjoint_copy(T_OBJECT, true, Address::times_ptr, entry,
iveresov@2606 2034 NULL, "oop_arraycopy_uninit",
iveresov@2606 2035 /*dest_uninitialized*/true);
iveresov@2606 2036
duke@435 2037 StubRoutines::_jlong_disjoint_arraycopy =
duke@435 2038 generate_disjoint_long_copy(&entry, "jlong_disjoint_arraycopy");
duke@435 2039 StubRoutines::_jlong_arraycopy =
duke@435 2040 generate_conjoint_long_copy(entry, &entry_jlong_arraycopy,
duke@435 2041 "jlong_arraycopy");
duke@435 2042
never@2118 2043 StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
never@2118 2044 StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
never@2118 2045 StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
never@2118 2046 StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
never@2118 2047 StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
never@2118 2048 StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
never@2118 2049
iveresov@2606 2050 StubRoutines::_arrayof_jint_disjoint_arraycopy = StubRoutines::_jint_disjoint_arraycopy;
iveresov@2606 2051 StubRoutines::_arrayof_oop_disjoint_arraycopy = StubRoutines::_oop_disjoint_arraycopy;
iveresov@2606 2052 StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit = StubRoutines::_oop_disjoint_arraycopy_uninit;
iveresov@2606 2053 StubRoutines::_arrayof_jlong_disjoint_arraycopy = StubRoutines::_jlong_disjoint_arraycopy;
duke@435 2054
iveresov@2606 2055 StubRoutines::_arrayof_jint_arraycopy = StubRoutines::_jint_arraycopy;
iveresov@2606 2056 StubRoutines::_arrayof_oop_arraycopy = StubRoutines::_oop_arraycopy;
iveresov@2606 2057 StubRoutines::_arrayof_oop_arraycopy_uninit = StubRoutines::_oop_arraycopy_uninit;
iveresov@2606 2058 StubRoutines::_arrayof_jlong_arraycopy = StubRoutines::_jlong_arraycopy;
duke@435 2059
duke@435 2060 StubRoutines::_checkcast_arraycopy =
iveresov@2606 2061 generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy);
iveresov@2606 2062 StubRoutines::_checkcast_arraycopy_uninit =
iveresov@2606 2063 generate_checkcast_copy("checkcast_arraycopy_uninit", NULL, /*dest_uninitialized*/true);
duke@435 2064
duke@435 2065 StubRoutines::_unsafe_arraycopy =
duke@435 2066 generate_unsafe_copy("unsafe_arraycopy",
duke@435 2067 entry_jbyte_arraycopy,
duke@435 2068 entry_jshort_arraycopy,
duke@435 2069 entry_jint_arraycopy,
duke@435 2070 entry_jlong_arraycopy);
duke@435 2071
duke@435 2072 StubRoutines::_generic_arraycopy =
duke@435 2073 generate_generic_copy("generic_arraycopy",
duke@435 2074 entry_jbyte_arraycopy,
duke@435 2075 entry_jshort_arraycopy,
duke@435 2076 entry_jint_arraycopy,
duke@435 2077 entry_oop_arraycopy,
duke@435 2078 entry_jlong_arraycopy,
duke@435 2079 entry_checkcast_arraycopy);
duke@435 2080 }
duke@435 2081
never@1609 2082 void generate_math_stubs() {
never@1609 2083 {
never@1609 2084 StubCodeMark mark(this, "StubRoutines", "log");
never@1609 2085 StubRoutines::_intrinsic_log = (double (*)(double)) __ pc();
never@1609 2086
never@1609 2087 __ fld_d(Address(rsp, 4));
never@1609 2088 __ flog();
never@1609 2089 __ ret(0);
never@1609 2090 }
never@1609 2091 {
never@1609 2092 StubCodeMark mark(this, "StubRoutines", "log10");
never@1609 2093 StubRoutines::_intrinsic_log10 = (double (*)(double)) __ pc();
never@1609 2094
never@1609 2095 __ fld_d(Address(rsp, 4));
never@1609 2096 __ flog10();
never@1609 2097 __ ret(0);
never@1609 2098 }
never@1609 2099 {
never@1609 2100 StubCodeMark mark(this, "StubRoutines", "sin");
never@1609 2101 StubRoutines::_intrinsic_sin = (double (*)(double)) __ pc();
never@1609 2102
never@1609 2103 __ fld_d(Address(rsp, 4));
never@1609 2104 __ trigfunc('s');
never@1609 2105 __ ret(0);
never@1609 2106 }
never@1609 2107 {
never@1609 2108 StubCodeMark mark(this, "StubRoutines", "cos");
never@1609 2109 StubRoutines::_intrinsic_cos = (double (*)(double)) __ pc();
never@1609 2110
never@1609 2111 __ fld_d(Address(rsp, 4));
never@1609 2112 __ trigfunc('c');
never@1609 2113 __ ret(0);
never@1609 2114 }
never@1609 2115 {
never@1609 2116 StubCodeMark mark(this, "StubRoutines", "tan");
never@1609 2117 StubRoutines::_intrinsic_tan = (double (*)(double)) __ pc();
never@1609 2118
never@1609 2119 __ fld_d(Address(rsp, 4));
never@1609 2120 __ trigfunc('t');
never@1609 2121 __ ret(0);
never@1609 2122 }
roland@3787 2123 {
roland@3787 2124 StubCodeMark mark(this, "StubRoutines", "exp");
roland@3787 2125 StubRoutines::_intrinsic_exp = (double (*)(double)) __ pc();
never@1609 2126
roland@3787 2127 __ fld_d(Address(rsp, 4));
roland@3787 2128 __ exp_with_fallback(0);
roland@3787 2129 __ ret(0);
roland@3787 2130 }
roland@3787 2131 {
roland@3787 2132 StubCodeMark mark(this, "StubRoutines", "pow");
roland@3787 2133 StubRoutines::_intrinsic_pow = (double (*)(double,double)) __ pc();
roland@3787 2134
roland@3787 2135 __ fld_d(Address(rsp, 12));
roland@3787 2136 __ fld_d(Address(rsp, 4));
roland@3787 2137 __ pow_with_fallback(0);
roland@3787 2138 __ ret(0);
roland@3787 2139 }
never@1609 2140 }
never@1609 2141
kvn@4205 2142 // AES intrinsic stubs
kvn@4205 2143 enum {AESBlockSize = 16};
kvn@4205 2144
kvn@4205 2145 address generate_key_shuffle_mask() {
kvn@4205 2146 __ align(16);
kvn@4205 2147 StubCodeMark mark(this, "StubRoutines", "key_shuffle_mask");
kvn@4205 2148 address start = __ pc();
kvn@4205 2149 __ emit_data(0x00010203, relocInfo::none, 0 );
kvn@4205 2150 __ emit_data(0x04050607, relocInfo::none, 0 );
kvn@4205 2151 __ emit_data(0x08090a0b, relocInfo::none, 0 );
kvn@4205 2152 __ emit_data(0x0c0d0e0f, relocInfo::none, 0 );
kvn@4205 2153 return start;
kvn@4205 2154 }
kvn@4205 2155
kvn@4205 2156 // Utility routine for loading a 128-bit key word in little endian format
kvn@4205 2157 // can optionally specify that the shuffle mask is already in an xmmregister
kvn@4205 2158 void load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
kvn@4205 2159 __ movdqu(xmmdst, Address(key, offset));
kvn@4205 2160 if (xmm_shuf_mask != NULL) {
kvn@4205 2161 __ pshufb(xmmdst, xmm_shuf_mask);
kvn@4205 2162 } else {
kvn@4205 2163 __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
kvn@4205 2164 }
kvn@4205 2165 }
kvn@4205 2166
kvn@4205 2167 // aesenc using specified key+offset
kvn@4205 2168 // can optionally specify that the shuffle mask is already in an xmmregister
kvn@4205 2169 void aes_enc_key(XMMRegister xmmdst, XMMRegister xmmtmp, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
kvn@4205 2170 load_key(xmmtmp, key, offset, xmm_shuf_mask);
kvn@4205 2171 __ aesenc(xmmdst, xmmtmp);
kvn@4205 2172 }
kvn@4205 2173
kvn@4205 2174 // aesdec using specified key+offset
kvn@4205 2175 // can optionally specify that the shuffle mask is already in an xmmregister
kvn@4205 2176 void aes_dec_key(XMMRegister xmmdst, XMMRegister xmmtmp, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
kvn@4205 2177 load_key(xmmtmp, key, offset, xmm_shuf_mask);
kvn@4205 2178 __ aesdec(xmmdst, xmmtmp);
kvn@4205 2179 }
kvn@4205 2180
kvn@4205 2181
kvn@4205 2182 // Arguments:
kvn@4205 2183 //
kvn@4205 2184 // Inputs:
kvn@4205 2185 // c_rarg0 - source byte array address
kvn@4205 2186 // c_rarg1 - destination byte array address
kvn@4205 2187 // c_rarg2 - K (key) in little endian int array
kvn@4205 2188 //
kvn@4205 2189 address generate_aescrypt_encryptBlock() {
kvn@4363 2190 assert(UseAES, "need AES instructions and misaligned SSE support");
kvn@4205 2191 __ align(CodeEntryAlignment);
kvn@4205 2192 StubCodeMark mark(this, "StubRoutines", "aescrypt_encryptBlock");
kvn@4205 2193 Label L_doLast;
kvn@4205 2194 address start = __ pc();
kvn@4205 2195
kvn@4363 2196 const Register from = rdx; // source array address
kvn@4205 2197 const Register to = rdx; // destination array address
kvn@4205 2198 const Register key = rcx; // key array address
kvn@4205 2199 const Register keylen = rax;
kvn@4205 2200 const Address from_param(rbp, 8+0);
kvn@4205 2201 const Address to_param (rbp, 8+4);
kvn@4205 2202 const Address key_param (rbp, 8+8);
kvn@4205 2203
kvn@4205 2204 const XMMRegister xmm_result = xmm0;
kvn@4363 2205 const XMMRegister xmm_key_shuf_mask = xmm1;
kvn@4363 2206 const XMMRegister xmm_temp1 = xmm2;
kvn@4363 2207 const XMMRegister xmm_temp2 = xmm3;
kvn@4363 2208 const XMMRegister xmm_temp3 = xmm4;
kvn@4363 2209 const XMMRegister xmm_temp4 = xmm5;
kvn@4363 2210
kvn@4363 2211 __ enter(); // required for proper stackwalking of RuntimeStub frame
kvn@4363 2212 __ movptr(from, from_param);
kvn@4363 2213 __ movptr(key, key_param);
kvn@4363 2214
kvn@4363 2215 // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
kvn@4205 2216 __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
kvn@4205 2217
kvn@4205 2218 __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
kvn@4205 2219 __ movdqu(xmm_result, Address(from, 0)); // get 16 bytes of input
kvn@4363 2220 __ movptr(to, to_param);
kvn@4205 2221
kvn@4205 2222 // For encryption, the java expanded key ordering is just what we need
kvn@4205 2223
kvn@4363 2224 load_key(xmm_temp1, key, 0x00, xmm_key_shuf_mask);
kvn@4363 2225 __ pxor(xmm_result, xmm_temp1);
kvn@4363 2226
kvn@4363 2227 load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
kvn@4363 2228 load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
kvn@4363 2229 load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
kvn@4363 2230 load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
kvn@4363 2231
kvn@4363 2232 __ aesenc(xmm_result, xmm_temp1);
kvn@4363 2233 __ aesenc(xmm_result, xmm_temp2);
kvn@4363 2234 __ aesenc(xmm_result, xmm_temp3);
kvn@4363 2235 __ aesenc(xmm_result, xmm_temp4);
kvn@4363 2236
kvn@4363 2237 load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
kvn@4363 2238 load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
kvn@4363 2239 load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
kvn@4363 2240 load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
kvn@4363 2241
kvn@4363 2242 __ aesenc(xmm_result, xmm_temp1);
kvn@4363 2243 __ aesenc(xmm_result, xmm_temp2);
kvn@4363 2244 __ aesenc(xmm_result, xmm_temp3);
kvn@4363 2245 __ aesenc(xmm_result, xmm_temp4);
kvn@4363 2246
kvn@4363 2247 load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
kvn@4363 2248 load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
kvn@4363 2249
kvn@4363 2250 __ cmpl(keylen, 44);
kvn@4363 2251 __ jccb(Assembler::equal, L_doLast);
kvn@4363 2252
kvn@4363 2253 __ aesenc(xmm_result, xmm_temp1);
kvn@4363 2254 __ aesenc(xmm_result, xmm_temp2);
kvn@4363 2255
kvn@4363 2256 load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
kvn@4363 2257 load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
kvn@4363 2258
kvn@4363 2259 __ cmpl(keylen, 52);
kvn@4363 2260 __ jccb(Assembler::equal, L_doLast);
kvn@4363 2261
kvn@4363 2262 __ aesenc(xmm_result, xmm_temp1);
kvn@4363 2263 __ aesenc(xmm_result, xmm_temp2);
kvn@4363 2264
kvn@4363 2265 load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
kvn@4363 2266 load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
kvn@4205 2267
kvn@4205 2268 __ BIND(L_doLast);
kvn@4363 2269 __ aesenc(xmm_result, xmm_temp1);
kvn@4363 2270 __ aesenclast(xmm_result, xmm_temp2);
kvn@4205 2271 __ movdqu(Address(to, 0), xmm_result); // store the result
kvn@4205 2272 __ xorptr(rax, rax); // return 0
kvn@4205 2273 __ leave(); // required for proper stackwalking of RuntimeStub frame
kvn@4205 2274 __ ret(0);
kvn@4205 2275
kvn@4205 2276 return start;
kvn@4205 2277 }
kvn@4205 2278
kvn@4205 2279
kvn@4205 2280 // Arguments:
kvn@4205 2281 //
kvn@4205 2282 // Inputs:
kvn@4205 2283 // c_rarg0 - source byte array address
kvn@4205 2284 // c_rarg1 - destination byte array address
kvn@4205 2285 // c_rarg2 - K (key) in little endian int array
kvn@4205 2286 //
kvn@4205 2287 address generate_aescrypt_decryptBlock() {
kvn@4363 2288 assert(UseAES, "need AES instructions and misaligned SSE support");
kvn@4205 2289 __ align(CodeEntryAlignment);
kvn@4205 2290 StubCodeMark mark(this, "StubRoutines", "aescrypt_decryptBlock");
kvn@4205 2291 Label L_doLast;
kvn@4205 2292 address start = __ pc();
kvn@4205 2293
kvn@4363 2294 const Register from = rdx; // source array address
kvn@4205 2295 const Register to = rdx; // destination array address
kvn@4205 2296 const Register key = rcx; // key array address
kvn@4205 2297 const Register keylen = rax;
kvn@4205 2298 const Address from_param(rbp, 8+0);
kvn@4205 2299 const Address to_param (rbp, 8+4);
kvn@4205 2300 const Address key_param (rbp, 8+8);
kvn@4205 2301
kvn@4205 2302 const XMMRegister xmm_result = xmm0;
kvn@4363 2303 const XMMRegister xmm_key_shuf_mask = xmm1;
kvn@4363 2304 const XMMRegister xmm_temp1 = xmm2;
kvn@4363 2305 const XMMRegister xmm_temp2 = xmm3;
kvn@4363 2306 const XMMRegister xmm_temp3 = xmm4;
kvn@4363 2307 const XMMRegister xmm_temp4 = xmm5;
kvn@4205 2308
kvn@4205 2309 __ enter(); // required for proper stackwalking of RuntimeStub frame
kvn@4363 2310 __ movptr(from, from_param);
kvn@4363 2311 __ movptr(key, key_param);
kvn@4363 2312
kvn@4363 2313 // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
kvn@4205 2314 __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
kvn@4205 2315
kvn@4205 2316 __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
kvn@4205 2317 __ movdqu(xmm_result, Address(from, 0));
kvn@4363 2318 __ movptr(to, to_param);
kvn@4205 2319
kvn@4205 2320 // for decryption java expanded key ordering is rotated one position from what we want
kvn@4205 2321 // so we start from 0x10 here and hit 0x00 last
kvn@4205 2322 // we don't know if the key is aligned, hence not using load-execute form
kvn@4363 2323 load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
kvn@4363 2324 load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
kvn@4363 2325 load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
kvn@4363 2326 load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
kvn@4363 2327
kvn@4363 2328 __ pxor (xmm_result, xmm_temp1);
kvn@4363 2329 __ aesdec(xmm_result, xmm_temp2);
kvn@4363 2330 __ aesdec(xmm_result, xmm_temp3);
kvn@4363 2331 __ aesdec(xmm_result, xmm_temp4);
kvn@4363 2332
kvn@4363 2333 load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
kvn@4363 2334 load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
kvn@4363 2335 load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
kvn@4363 2336 load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
kvn@4363 2337
kvn@4363 2338 __ aesdec(xmm_result, xmm_temp1);
kvn@4363 2339 __ aesdec(xmm_result, xmm_temp2);
kvn@4363 2340 __ aesdec(xmm_result, xmm_temp3);
kvn@4363 2341 __ aesdec(xmm_result, xmm_temp4);
kvn@4363 2342
kvn@4363 2343 load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
kvn@4363 2344 load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
kvn@4363 2345 load_key(xmm_temp3, key, 0x00, xmm_key_shuf_mask);
kvn@4363 2346
kvn@4363 2347 __ cmpl(keylen, 44);
kvn@4363 2348 __ jccb(Assembler::equal, L_doLast);
kvn@4363 2349
kvn@4363 2350 __ aesdec(xmm_result, xmm_temp1);
kvn@4363 2351 __ aesdec(xmm_result, xmm_temp2);
kvn@4363 2352
kvn@4363 2353 load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
kvn@4363 2354 load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
kvn@4363 2355
kvn@4363 2356 __ cmpl(keylen, 52);
kvn@4363 2357 __ jccb(Assembler::equal, L_doLast);
kvn@4363 2358
kvn@4363 2359 __ aesdec(xmm_result, xmm_temp1);
kvn@4363 2360 __ aesdec(xmm_result, xmm_temp2);
kvn@4363 2361
kvn@4363 2362 load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
kvn@4363 2363 load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
kvn@4205 2364
kvn@4205 2365 __ BIND(L_doLast);
kvn@4363 2366 __ aesdec(xmm_result, xmm_temp1);
kvn@4363 2367 __ aesdec(xmm_result, xmm_temp2);
kvn@4363 2368
kvn@4205 2369 // for decryption the aesdeclast operation is always on key+0x00
kvn@4363 2370 __ aesdeclast(xmm_result, xmm_temp3);
kvn@4205 2371 __ movdqu(Address(to, 0), xmm_result); // store the result
kvn@4205 2372 __ xorptr(rax, rax); // return 0
kvn@4205 2373 __ leave(); // required for proper stackwalking of RuntimeStub frame
kvn@4205 2374 __ ret(0);
kvn@4205 2375
kvn@4205 2376 return start;
kvn@4205 2377 }
kvn@4205 2378
kvn@4205 2379 void handleSOERegisters(bool saving) {
kvn@4205 2380 const int saveFrameSizeInBytes = 4 * wordSize;
kvn@4205 2381 const Address saved_rbx (rbp, -3 * wordSize);
kvn@4205 2382 const Address saved_rsi (rbp, -2 * wordSize);
kvn@4205 2383 const Address saved_rdi (rbp, -1 * wordSize);
kvn@4205 2384
kvn@4205 2385 if (saving) {
kvn@4205 2386 __ subptr(rsp, saveFrameSizeInBytes);
kvn@4205 2387 __ movptr(saved_rsi, rsi);
kvn@4205 2388 __ movptr(saved_rdi, rdi);
kvn@4205 2389 __ movptr(saved_rbx, rbx);
kvn@4205 2390 } else {
kvn@4205 2391 // restoring
kvn@4205 2392 __ movptr(rsi, saved_rsi);
kvn@4205 2393 __ movptr(rdi, saved_rdi);
kvn@4205 2394 __ movptr(rbx, saved_rbx);
kvn@4205 2395 }
kvn@4205 2396 }
kvn@4205 2397
kvn@4205 2398 // Arguments:
kvn@4205 2399 //
kvn@4205 2400 // Inputs:
kvn@4205 2401 // c_rarg0 - source byte array address
kvn@4205 2402 // c_rarg1 - destination byte array address
kvn@4205 2403 // c_rarg2 - K (key) in little endian int array
kvn@4205 2404 // c_rarg3 - r vector byte array address
kvn@4205 2405 // c_rarg4 - input length
kvn@4205 2406 //
kvn@6312 2407 // Output:
kvn@6312 2408 // rax - input length
kvn@6312 2409 //
kvn@4205 2410 address generate_cipherBlockChaining_encryptAESCrypt() {
kvn@4363 2411 assert(UseAES, "need AES instructions and misaligned SSE support");
kvn@4205 2412 __ align(CodeEntryAlignment);
kvn@4205 2413 StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_encryptAESCrypt");
kvn@4205 2414 address start = __ pc();
kvn@4205 2415
kvn@4205 2416 Label L_exit, L_key_192_256, L_key_256, L_loopTop_128, L_loopTop_192, L_loopTop_256;
kvn@4205 2417 const Register from = rsi; // source array address
kvn@4205 2418 const Register to = rdx; // destination array address
kvn@4205 2419 const Register key = rcx; // key array address
kvn@4205 2420 const Register rvec = rdi; // r byte array initialized from initvector array address
kvn@4205 2421 // and left with the results of the last encryption block
kvn@4205 2422 const Register len_reg = rbx; // src len (must be multiple of blocksize 16)
kvn@4205 2423 const Register pos = rax;
kvn@4205 2424
kvn@4205 2425 // xmm register assignments for the loops below
kvn@4205 2426 const XMMRegister xmm_result = xmm0;
kvn@4205 2427 const XMMRegister xmm_temp = xmm1;
kvn@4205 2428 // first 6 keys preloaded into xmm2-xmm7
kvn@4205 2429 const int XMM_REG_NUM_KEY_FIRST = 2;
kvn@4205 2430 const int XMM_REG_NUM_KEY_LAST = 7;
kvn@4205 2431 const XMMRegister xmm_key0 = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
kvn@4205 2432
kvn@4205 2433 __ enter(); // required for proper stackwalking of RuntimeStub frame
kvn@4205 2434 handleSOERegisters(true /*saving*/);
kvn@4205 2435
kvn@4205 2436 // load registers from incoming parameters
kvn@4205 2437 const Address from_param(rbp, 8+0);
kvn@4205 2438 const Address to_param (rbp, 8+4);
kvn@4205 2439 const Address key_param (rbp, 8+8);
kvn@4205 2440 const Address rvec_param (rbp, 8+12);
kvn@4205 2441 const Address len_param (rbp, 8+16);
kvn@4205 2442 __ movptr(from , from_param);
kvn@4205 2443 __ movptr(to , to_param);
kvn@4205 2444 __ movptr(key , key_param);
kvn@4205 2445 __ movptr(rvec , rvec_param);
kvn@4205 2446 __ movptr(len_reg , len_param);
kvn@4205 2447
kvn@4205 2448 const XMMRegister xmm_key_shuf_mask = xmm_temp; // used temporarily to swap key bytes up front
kvn@4205 2449 __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
kvn@4205 2450 // load up xmm regs 2 thru 7 with keys 0-5
kvn@4205 2451 for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x00; rnum <= XMM_REG_NUM_KEY_LAST; rnum++) {
kvn@4205 2452 load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
kvn@4205 2453 offset += 0x10;
kvn@4205 2454 }
kvn@4205 2455
kvn@4205 2456 __ movdqu(xmm_result, Address(rvec, 0x00)); // initialize xmm_result with r vec
kvn@4205 2457
kvn@4205 2458 // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
kvn@4205 2459 __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
kvn@4205 2460 __ cmpl(rax, 44);
kvn@4205 2461 __ jcc(Assembler::notEqual, L_key_192_256);
kvn@4205 2462
kvn@4205 2463 // 128 bit code follows here
kvn@4363 2464 __ movl(pos, 0);
kvn@4205 2465 __ align(OptoLoopAlignment);
kvn@4205 2466 __ BIND(L_loopTop_128);
kvn@4205 2467 __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of input
kvn@4205 2468 __ pxor (xmm_result, xmm_temp); // xor with the current r vector
kvn@4205 2469
kvn@4205 2470 __ pxor (xmm_result, xmm_key0); // do the aes rounds
kvn@4205 2471 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST; rnum++) {
kvn@4205 2472 __ aesenc(xmm_result, as_XMMRegister(rnum));
kvn@4205 2473 }
kvn@4205 2474 for (int key_offset = 0x60; key_offset <= 0x90; key_offset += 0x10) {
kvn@4205 2475 aes_enc_key(xmm_result, xmm_temp, key, key_offset);
kvn@4205 2476 }
kvn@4205 2477 load_key(xmm_temp, key, 0xa0);
kvn@4205 2478 __ aesenclast(xmm_result, xmm_temp);
kvn@4205 2479
kvn@4205 2480 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output
kvn@4205 2481 // no need to store r to memory until we exit
kvn@4205 2482 __ addptr(pos, AESBlockSize);
kvn@4205 2483 __ subptr(len_reg, AESBlockSize);
kvn@4205 2484 __ jcc(Assembler::notEqual, L_loopTop_128);
kvn@4205 2485
kvn@4205 2486 __ BIND(L_exit);
kvn@4205 2487 __ movdqu(Address(rvec, 0), xmm_result); // final value of r stored in rvec of CipherBlockChaining object
kvn@4205 2488
kvn@4205 2489 handleSOERegisters(false /*restoring*/);
kvn@6312 2490 __ movptr(rax, len_param); // return length
kvn@4205 2491 __ leave(); // required for proper stackwalking of RuntimeStub frame
kvn@4205 2492 __ ret(0);
kvn@4205 2493
kvn@4363 2494 __ BIND(L_key_192_256);
kvn@4363 2495 // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
kvn@4205 2496 __ cmpl(rax, 52);
kvn@4205 2497 __ jcc(Assembler::notEqual, L_key_256);
kvn@4205 2498
kvn@4205 2499 // 192-bit code follows here (could be changed to use more xmm registers)
kvn@4363 2500 __ movl(pos, 0);
kvn@4363 2501 __ align(OptoLoopAlignment);
kvn@4363 2502 __ BIND(L_loopTop_192);
kvn@4205 2503 __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of input
kvn@4205 2504 __ pxor (xmm_result, xmm_temp); // xor with the current r vector
kvn@4205 2505
kvn@4205 2506 __ pxor (xmm_result, xmm_key0); // do the aes rounds
kvn@4205 2507 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST; rnum++) {
kvn@4205 2508 __ aesenc(xmm_result, as_XMMRegister(rnum));
kvn@4205 2509 }
kvn@4205 2510 for (int key_offset = 0x60; key_offset <= 0xb0; key_offset += 0x10) {
kvn@4205 2511 aes_enc_key(xmm_result, xmm_temp, key, key_offset);
kvn@4205 2512 }
kvn@4205 2513 load_key(xmm_temp, key, 0xc0);
kvn@4205 2514 __ aesenclast(xmm_result, xmm_temp);
kvn@4205 2515
kvn@4205 2516 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output
kvn@4205 2517 // no need to store r to memory until we exit
kvn@4205 2518 __ addptr(pos, AESBlockSize);
kvn@4205 2519 __ subptr(len_reg, AESBlockSize);
kvn@4205 2520 __ jcc(Assembler::notEqual, L_loopTop_192);
kvn@4205 2521 __ jmp(L_exit);
kvn@4205 2522
kvn@4363 2523 __ BIND(L_key_256);
kvn@4205 2524 // 256-bit code follows here (could be changed to use more xmm registers)
kvn@4363 2525 __ movl(pos, 0);
kvn@4363 2526 __ align(OptoLoopAlignment);
kvn@4363 2527 __ BIND(L_loopTop_256);
kvn@4205 2528 __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of input
kvn@4205 2529 __ pxor (xmm_result, xmm_temp); // xor with the current r vector
kvn@4205 2530
kvn@4205 2531 __ pxor (xmm_result, xmm_key0); // do the aes rounds
kvn@4205 2532 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST; rnum++) {
kvn@4205 2533 __ aesenc(xmm_result, as_XMMRegister(rnum));
kvn@4205 2534 }
kvn@4205 2535 for (int key_offset = 0x60; key_offset <= 0xd0; key_offset += 0x10) {
kvn@4205 2536 aes_enc_key(xmm_result, xmm_temp, key, key_offset);
kvn@4205 2537 }
kvn@4205 2538 load_key(xmm_temp, key, 0xe0);
kvn@4205 2539 __ aesenclast(xmm_result, xmm_temp);
kvn@4205 2540
kvn@4205 2541 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output
kvn@4205 2542 // no need to store r to memory until we exit
kvn@4205 2543 __ addptr(pos, AESBlockSize);
kvn@4205 2544 __ subptr(len_reg, AESBlockSize);
kvn@4205 2545 __ jcc(Assembler::notEqual, L_loopTop_256);
kvn@4205 2546 __ jmp(L_exit);
kvn@4205 2547
kvn@4205 2548 return start;
kvn@4205 2549 }
kvn@4205 2550
kvn@4205 2551
kvn@4205 2552 // CBC AES Decryption.
kvn@4205 2553 // In 32-bit stub, because of lack of registers we do not try to parallelize 4 blocks at a time.
kvn@4205 2554 //
kvn@4205 2555 // Arguments:
kvn@4205 2556 //
kvn@4205 2557 // Inputs:
kvn@4205 2558 // c_rarg0 - source byte array address
kvn@4205 2559 // c_rarg1 - destination byte array address
kvn@4205 2560 // c_rarg2 - K (key) in little endian int array
kvn@4205 2561 // c_rarg3 - r vector byte array address
kvn@4205 2562 // c_rarg4 - input length
kvn@4205 2563 //
kvn@6312 2564 // Output:
kvn@6312 2565 // rax - input length
kvn@6312 2566 //
kvn@4205 2567
kvn@4205 2568 address generate_cipherBlockChaining_decryptAESCrypt() {
kvn@4363 2569 assert(UseAES, "need AES instructions and misaligned SSE support");
kvn@4205 2570 __ align(CodeEntryAlignment);
kvn@4205 2571 StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt");
kvn@4205 2572 address start = __ pc();
kvn@4205 2573
kvn@4205 2574 Label L_exit, L_key_192_256, L_key_256;
kvn@4205 2575 Label L_singleBlock_loopTop_128;
kvn@4205 2576 Label L_singleBlock_loopTop_192, L_singleBlock_loopTop_256;
kvn@4205 2577 const Register from = rsi; // source array address
kvn@4205 2578 const Register to = rdx; // destination array address
kvn@4205 2579 const Register key = rcx; // key array address
kvn@4205 2580 const Register rvec = rdi; // r byte array initialized from initvector array address
kvn@4205 2581 // and left with the results of the last encryption block
kvn@4205 2582 const Register len_reg = rbx; // src len (must be multiple of blocksize 16)
kvn@4205 2583 const Register pos = rax;
kvn@4205 2584
kvn@4205 2585 // xmm register assignments for the loops below
kvn@4205 2586 const XMMRegister xmm_result = xmm0;
kvn@4205 2587 const XMMRegister xmm_temp = xmm1;
kvn@4205 2588 // first 6 keys preloaded into xmm2-xmm7
kvn@4205 2589 const int XMM_REG_NUM_KEY_FIRST = 2;
kvn@4205 2590 const int XMM_REG_NUM_KEY_LAST = 7;
kvn@4205 2591 const int FIRST_NON_REG_KEY_offset = 0x70;
kvn@4205 2592 const XMMRegister xmm_key_first = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
kvn@4205 2593
kvn@4205 2594 __ enter(); // required for proper stackwalking of RuntimeStub frame
kvn@4205 2595 handleSOERegisters(true /*saving*/);
kvn@4205 2596
kvn@4205 2597 // load registers from incoming parameters
kvn@4205 2598 const Address from_param(rbp, 8+0);
kvn@4205 2599 const Address to_param (rbp, 8+4);
kvn@4205 2600 const Address key_param (rbp, 8+8);
kvn@4205 2601 const Address rvec_param (rbp, 8+12);
kvn@4205 2602 const Address len_param (rbp, 8+16);
kvn@4205 2603 __ movptr(from , from_param);
kvn@4205 2604 __ movptr(to , to_param);
kvn@4205 2605 __ movptr(key , key_param);
kvn@4205 2606 __ movptr(rvec , rvec_param);
kvn@4205 2607 __ movptr(len_reg , len_param);
kvn@4205 2608
kvn@4205 2609 // the java expanded key ordering is rotated one position from what we want
kvn@4205 2610 // so we start from 0x10 here and hit 0x00 last
kvn@4205 2611 const XMMRegister xmm_key_shuf_mask = xmm1; // used temporarily to swap key bytes up front
kvn@4205 2612 __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
kvn@4205 2613 // load up xmm regs 2 thru 6 with first 5 keys
kvn@4205 2614 for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x10; rnum <= XMM_REG_NUM_KEY_LAST; rnum++) {
kvn@4205 2615 load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
kvn@4205 2616 offset += 0x10;
kvn@4205 2617 }
kvn@4205 2618
kvn@4205 2619 // inside here, use the rvec register to point to previous block cipher
kvn@4205 2620 // with which we xor at the end of each newly decrypted block
kvn@4205 2621 const Register prev_block_cipher_ptr = rvec;
kvn@4205 2622
kvn@4205 2623 // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
kvn@4205 2624 __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
kvn@4205 2625 __ cmpl(rax, 44);
kvn@4205 2626 __ jcc(Assembler::notEqual, L_key_192_256);
kvn@4205 2627
kvn@4205 2628
kvn@4205 2629 // 128-bit code follows here, parallelized
kvn@4363 2630 __ movl(pos, 0);
kvn@4363 2631 __ align(OptoLoopAlignment);
kvn@4363 2632 __ BIND(L_singleBlock_loopTop_128);
kvn@4205 2633 __ cmpptr(len_reg, 0); // any blocks left??
kvn@4205 2634 __ jcc(Assembler::equal, L_exit);
kvn@4205 2635 __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input
kvn@4205 2636 __ pxor (xmm_result, xmm_key_first); // do the aes dec rounds
kvn@4205 2637 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST; rnum++) {
kvn@4205 2638 __ aesdec(xmm_result, as_XMMRegister(rnum));
kvn@4205 2639 }
kvn@4205 2640 for (int key_offset = FIRST_NON_REG_KEY_offset; key_offset <= 0xa0; key_offset += 0x10) { // 128-bit runs up to key offset a0
kvn@4205 2641 aes_dec_key(xmm_result, xmm_temp, key, key_offset);
kvn@4205 2642 }
kvn@4205 2643 load_key(xmm_temp, key, 0x00); // final key is stored in java expanded array at offset 0
kvn@4205 2644 __ aesdeclast(xmm_result, xmm_temp);
kvn@4205 2645 __ movdqu(xmm_temp, Address(prev_block_cipher_ptr, 0x00));
kvn@4205 2646 __ pxor (xmm_result, xmm_temp); // xor with the current r vector
kvn@4205 2647 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output
kvn@4205 2648 // no need to store r to memory until we exit
kvn@4205 2649 __ lea(prev_block_cipher_ptr, Address(from, pos, Address::times_1, 0)); // set up new ptr
kvn@4205 2650 __ addptr(pos, AESBlockSize);
kvn@4205 2651 __ subptr(len_reg, AESBlockSize);
kvn@4205 2652 __ jmp(L_singleBlock_loopTop_128);
kvn@4205 2653
kvn@4205 2654
kvn@4205 2655 __ BIND(L_exit);
kvn@4205 2656 __ movdqu(xmm_temp, Address(prev_block_cipher_ptr, 0x00));
kvn@4205 2657 __ movptr(rvec , rvec_param); // restore this since used in loop
kvn@4205 2658 __ movdqu(Address(rvec, 0), xmm_temp); // final value of r stored in rvec of CipherBlockChaining object
kvn@4205 2659 handleSOERegisters(false /*restoring*/);
kvn@6312 2660 __ movptr(rax, len_param); // return length
kvn@4205 2661 __ leave(); // required for proper stackwalking of RuntimeStub frame
kvn@4205 2662 __ ret(0);
kvn@4205 2663
kvn@4205 2664
kvn@4205 2665 __ BIND(L_key_192_256);
kvn@4205 2666 // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
kvn@4205 2667 __ cmpl(rax, 52);
kvn@4205 2668 __ jcc(Assembler::notEqual, L_key_256);
kvn@4205 2669
kvn@4205 2670 // 192-bit code follows here (could be optimized to use parallelism)
kvn@4363 2671 __ movl(pos, 0);
kvn@4205 2672 __ align(OptoLoopAlignment);
kvn@4205 2673 __ BIND(L_singleBlock_loopTop_192);
kvn@4205 2674 __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input
kvn@4205 2675 __ pxor (xmm_result, xmm_key_first); // do the aes dec rounds
kvn@4205 2676 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST; rnum++) {
kvn@4205 2677 __ aesdec(xmm_result, as_XMMRegister(rnum));
kvn@4205 2678 }
kvn@4205 2679 for (int key_offset = FIRST_NON_REG_KEY_offset; key_offset <= 0xc0; key_offset += 0x10) { // 192-bit runs up to key offset c0
kvn@4205 2680 aes_dec_key(xmm_result, xmm_temp, key, key_offset);
kvn@4205 2681 }
kvn@4205 2682 load_key(xmm_temp, key, 0x00); // final key is stored in java expanded array at offset 0
kvn@4205 2683 __ aesdeclast(xmm_result, xmm_temp);
kvn@4205 2684 __ movdqu(xmm_temp, Address(prev_block_cipher_ptr, 0x00));
kvn@4205 2685 __ pxor (xmm_result, xmm_temp); // xor with the current r vector
kvn@4205 2686 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output
kvn@4205 2687 // no need to store r to memory until we exit
kvn@4205 2688 __ lea(prev_block_cipher_ptr, Address(from, pos, Address::times_1, 0)); // set up new ptr
kvn@4205 2689 __ addptr(pos, AESBlockSize);
kvn@4205 2690 __ subptr(len_reg, AESBlockSize);
kvn@4205 2691 __ jcc(Assembler::notEqual,L_singleBlock_loopTop_192);
kvn@4205 2692 __ jmp(L_exit);
kvn@4205 2693
kvn@4205 2694 __ BIND(L_key_256);
kvn@4205 2695 // 256-bit code follows here (could be optimized to use parallelism)
kvn@4363 2696 __ movl(pos, 0);
kvn@4205 2697 __ align(OptoLoopAlignment);
kvn@4205 2698 __ BIND(L_singleBlock_loopTop_256);
kvn@4205 2699 __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input
kvn@4205 2700 __ pxor (xmm_result, xmm_key_first); // do the aes dec rounds
kvn@4205 2701 for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST; rnum++) {
kvn@4205 2702 __ aesdec(xmm_result, as_XMMRegister(rnum));
kvn@4205 2703 }
kvn@4205 2704 for (int key_offset = FIRST_NON_REG_KEY_offset; key_offset <= 0xe0; key_offset += 0x10) { // 256-bit runs up to key offset e0
kvn@4205 2705 aes_dec_key(xmm_result, xmm_temp, key, key_offset);
kvn@4205 2706 }
kvn@4205 2707 load_key(xmm_temp, key, 0x00); // final key is stored in java expanded array at offset 0
kvn@4205 2708 __ aesdeclast(xmm_result, xmm_temp);
kvn@4205 2709 __ movdqu(xmm_temp, Address(prev_block_cipher_ptr, 0x00));
kvn@4205 2710 __ pxor (xmm_result, xmm_temp); // xor with the current r vector
kvn@4205 2711 __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output
kvn@4205 2712 // no need to store r to memory until we exit
kvn@4205 2713 __ lea(prev_block_cipher_ptr, Address(from, pos, Address::times_1, 0)); // set up new ptr
kvn@4205 2714 __ addptr(pos, AESBlockSize);
kvn@4205 2715 __ subptr(len_reg, AESBlockSize);
kvn@4205 2716 __ jcc(Assembler::notEqual,L_singleBlock_loopTop_256);
kvn@4205 2717 __ jmp(L_exit);
kvn@4205 2718
kvn@4205 2719 return start;
kvn@4205 2720 }
kvn@4205 2721
ascarpino@9788 2722 // byte swap x86 long
ascarpino@9788 2723 address generate_ghash_long_swap_mask() {
ascarpino@9788 2724 __ align(CodeEntryAlignment);
ascarpino@9788 2725 StubCodeMark mark(this, "StubRoutines", "ghash_long_swap_mask");
ascarpino@9788 2726 address start = __ pc();
ascarpino@9788 2727 __ emit_data(0x0b0a0908, relocInfo::none, 0);
ascarpino@9788 2728 __ emit_data(0x0f0e0d0c, relocInfo::none, 0);
ascarpino@9788 2729 __ emit_data(0x03020100, relocInfo::none, 0);
ascarpino@9788 2730 __ emit_data(0x07060504, relocInfo::none, 0);
ascarpino@9788 2731
ascarpino@9788 2732 return start;
ascarpino@9788 2733 }
ascarpino@9788 2734
ascarpino@9788 2735 // byte swap x86 byte array
ascarpino@9788 2736 address generate_ghash_byte_swap_mask() {
ascarpino@9788 2737 __ align(CodeEntryAlignment);
ascarpino@9788 2738 StubCodeMark mark(this, "StubRoutines", "ghash_byte_swap_mask");
ascarpino@9788 2739 address start = __ pc();
ascarpino@9788 2740 __ emit_data(0x0c0d0e0f, relocInfo::none, 0);
ascarpino@9788 2741 __ emit_data(0x08090a0b, relocInfo::none, 0);
ascarpino@9788 2742 __ emit_data(0x04050607, relocInfo::none, 0);
ascarpino@9788 2743 __ emit_data(0x00010203, relocInfo::none, 0);
ascarpino@9788 2744 return start;
ascarpino@9788 2745 }
ascarpino@9788 2746
ascarpino@9788 2747 /* Single and multi-block ghash operations */
ascarpino@9788 2748 address generate_ghash_processBlocks() {
ascarpino@9788 2749 assert(UseGHASHIntrinsics, "need GHASH intrinsics and CLMUL support");
ascarpino@9788 2750 __ align(CodeEntryAlignment);
ascarpino@9788 2751 Label L_ghash_loop, L_exit;
ascarpino@9788 2752 StubCodeMark mark(this, "StubRoutines", "ghash_processBlocks");
ascarpino@9788 2753 address start = __ pc();
ascarpino@9788 2754
ascarpino@9788 2755 const Register state = rdi;
ascarpino@9788 2756 const Register subkeyH = rsi;
ascarpino@9788 2757 const Register data = rdx;
ascarpino@9788 2758 const Register blocks = rcx;
ascarpino@9788 2759
ascarpino@9788 2760 const Address state_param(rbp, 8+0);
ascarpino@9788 2761 const Address subkeyH_param(rbp, 8+4);
ascarpino@9788 2762 const Address data_param(rbp, 8+8);
ascarpino@9788 2763 const Address blocks_param(rbp, 8+12);
ascarpino@9788 2764
ascarpino@9788 2765 const XMMRegister xmm_temp0 = xmm0;
ascarpino@9788 2766 const XMMRegister xmm_temp1 = xmm1;
ascarpino@9788 2767 const XMMRegister xmm_temp2 = xmm2;
ascarpino@9788 2768 const XMMRegister xmm_temp3 = xmm3;
ascarpino@9788 2769 const XMMRegister xmm_temp4 = xmm4;
ascarpino@9788 2770 const XMMRegister xmm_temp5 = xmm5;
ascarpino@9788 2771 const XMMRegister xmm_temp6 = xmm6;
ascarpino@9788 2772 const XMMRegister xmm_temp7 = xmm7;
ascarpino@9788 2773
ascarpino@9788 2774 __ enter();
ascarpino@9789 2775 handleSOERegisters(true); // Save registers
ascarpino@9788 2776
ascarpino@9788 2777 __ movptr(state, state_param);
ascarpino@9788 2778 __ movptr(subkeyH, subkeyH_param);
ascarpino@9788 2779 __ movptr(data, data_param);
ascarpino@9788 2780 __ movptr(blocks, blocks_param);
ascarpino@9788 2781
ascarpino@9788 2782 __ movdqu(xmm_temp0, Address(state, 0));
ascarpino@9788 2783 __ pshufb(xmm_temp0, ExternalAddress(StubRoutines::x86::ghash_long_swap_mask_addr()));
ascarpino@9788 2784
ascarpino@9788 2785 __ movdqu(xmm_temp1, Address(subkeyH, 0));
ascarpino@9788 2786 __ pshufb(xmm_temp1, ExternalAddress(StubRoutines::x86::ghash_long_swap_mask_addr()));
ascarpino@9788 2787
ascarpino@9788 2788 __ BIND(L_ghash_loop);
ascarpino@9788 2789 __ movdqu(xmm_temp2, Address(data, 0));
ascarpino@9788 2790 __ pshufb(xmm_temp2, ExternalAddress(StubRoutines::x86::ghash_byte_swap_mask_addr()));
ascarpino@9788 2791
ascarpino@9788 2792 __ pxor(xmm_temp0, xmm_temp2);
ascarpino@9788 2793
ascarpino@9788 2794 //
ascarpino@9788 2795 // Multiply with the hash key
ascarpino@9788 2796 //
ascarpino@9788 2797 __ movdqu(xmm_temp3, xmm_temp0);
ascarpino@9788 2798 __ pclmulqdq(xmm_temp3, xmm_temp1, 0); // xmm3 holds a0*b0
ascarpino@9788 2799 __ movdqu(xmm_temp4, xmm_temp0);
ascarpino@9788 2800 __ pclmulqdq(xmm_temp4, xmm_temp1, 16); // xmm4 holds a0*b1
ascarpino@9788 2801
ascarpino@9788 2802 __ movdqu(xmm_temp5, xmm_temp0);
ascarpino@9788 2803 __ pclmulqdq(xmm_temp5, xmm_temp1, 1); // xmm5 holds a1*b0
ascarpino@9788 2804 __ movdqu(xmm_temp6, xmm_temp0);
ascarpino@9788 2805 __ pclmulqdq(xmm_temp6, xmm_temp1, 17); // xmm6 holds a1*b1
ascarpino@9788 2806
ascarpino@9788 2807 __ pxor(xmm_temp4, xmm_temp5); // xmm4 holds a0*b1 + a1*b0
ascarpino@9788 2808
ascarpino@9788 2809 __ movdqu(xmm_temp5, xmm_temp4); // move the contents of xmm4 to xmm5
ascarpino@9788 2810 __ psrldq(xmm_temp4, 8); // shift by xmm4 64 bits to the right
ascarpino@9788 2811 __ pslldq(xmm_temp5, 8); // shift by xmm5 64 bits to the left
ascarpino@9788 2812 __ pxor(xmm_temp3, xmm_temp5);
ascarpino@9788 2813 __ pxor(xmm_temp6, xmm_temp4); // Register pair <xmm6:xmm3> holds the result
ascarpino@9788 2814 // of the carry-less multiplication of
ascarpino@9788 2815 // xmm0 by xmm1.
ascarpino@9788 2816
ascarpino@9788 2817 // We shift the result of the multiplication by one bit position
ascarpino@9788 2818 // to the left to cope for the fact that the bits are reversed.
ascarpino@9788 2819 __ movdqu(xmm_temp7, xmm_temp3);
ascarpino@9788 2820 __ movdqu(xmm_temp4, xmm_temp6);
ascarpino@9788 2821 __ pslld (xmm_temp3, 1);
ascarpino@9788 2822 __ pslld(xmm_temp6, 1);
ascarpino@9788 2823 __ psrld(xmm_temp7, 31);
ascarpino@9788 2824 __ psrld(xmm_temp4, 31);
ascarpino@9788 2825 __ movdqu(xmm_temp5, xmm_temp7);
ascarpino@9788 2826 __ pslldq(xmm_temp4, 4);
ascarpino@9788 2827 __ pslldq(xmm_temp7, 4);
ascarpino@9788 2828 __ psrldq(xmm_temp5, 12);
ascarpino@9788 2829 __ por(xmm_temp3, xmm_temp7);
ascarpino@9788 2830 __ por(xmm_temp6, xmm_temp4);
ascarpino@9788 2831 __ por(xmm_temp6, xmm_temp5);
ascarpino@9788 2832
ascarpino@9788 2833 //
ascarpino@9788 2834 // First phase of the reduction
ascarpino@9788 2835 //
ascarpino@9788 2836 // Move xmm3 into xmm4, xmm5, xmm7 in order to perform the shifts
ascarpino@9788 2837 // independently.
ascarpino@9788 2838 __ movdqu(xmm_temp7, xmm_temp3);
ascarpino@9788 2839 __ movdqu(xmm_temp4, xmm_temp3);
ascarpino@9788 2840 __ movdqu(xmm_temp5, xmm_temp3);
ascarpino@9788 2841 __ pslld(xmm_temp7, 31); // packed right shift shifting << 31
ascarpino@9788 2842 __ pslld(xmm_temp4, 30); // packed right shift shifting << 30
ascarpino@9788 2843 __ pslld(xmm_temp5, 25); // packed right shift shifting << 25
ascarpino@9788 2844 __ pxor(xmm_temp7, xmm_temp4); // xor the shifted versions
ascarpino@9788 2845 __ pxor(xmm_temp7, xmm_temp5);
ascarpino@9788 2846 __ movdqu(xmm_temp4, xmm_temp7);
ascarpino@9788 2847 __ pslldq(xmm_temp7, 12);
ascarpino@9788 2848 __ psrldq(xmm_temp4, 4);
ascarpino@9788 2849 __ pxor(xmm_temp3, xmm_temp7); // first phase of the reduction complete
ascarpino@9788 2850
ascarpino@9788 2851 //
ascarpino@9788 2852 // Second phase of the reduction
ascarpino@9788 2853 //
ascarpino@9788 2854 // Make 3 copies of xmm3 in xmm2, xmm5, xmm7 for doing these
ascarpino@9788 2855 // shift operations.
ascarpino@9788 2856 __ movdqu(xmm_temp2, xmm_temp3);
ascarpino@9788 2857 __ movdqu(xmm_temp7, xmm_temp3);
ascarpino@9788 2858 __ movdqu(xmm_temp5, xmm_temp3);
ascarpino@9788 2859 __ psrld(xmm_temp2, 1); // packed left shifting >> 1
ascarpino@9788 2860 __ psrld(xmm_temp7, 2); // packed left shifting >> 2
ascarpino@9788 2861 __ psrld(xmm_temp5, 7); // packed left shifting >> 7
ascarpino@9788 2862 __ pxor(xmm_temp2, xmm_temp7); // xor the shifted versions
ascarpino@9788 2863 __ pxor(xmm_temp2, xmm_temp5);
ascarpino@9788 2864 __ pxor(xmm_temp2, xmm_temp4);
ascarpino@9788 2865 __ pxor(xmm_temp3, xmm_temp2);
ascarpino@9788 2866 __ pxor(xmm_temp6, xmm_temp3); // the result is in xmm6
ascarpino@9788 2867
ascarpino@9788 2868 __ decrement(blocks);
ascarpino@9788 2869 __ jcc(Assembler::zero, L_exit);
ascarpino@9788 2870 __ movdqu(xmm_temp0, xmm_temp6);
ascarpino@9788 2871 __ addptr(data, 16);
ascarpino@9788 2872 __ jmp(L_ghash_loop);
ascarpino@9788 2873
ascarpino@9788 2874 __ BIND(L_exit);
ascarpino@9788 2875 // Byte swap 16-byte result
ascarpino@9788 2876 __ pshufb(xmm_temp6, ExternalAddress(StubRoutines::x86::ghash_long_swap_mask_addr()));
ascarpino@9788 2877 __ movdqu(Address(state, 0), xmm_temp6); // store the result
ascarpino@9788 2878
ascarpino@9789 2879 handleSOERegisters(false); // restore registers
ascarpino@9788 2880 __ leave();
ascarpino@9788 2881 __ ret(0);
ascarpino@9788 2882 return start;
ascarpino@9788 2883 }
ascarpino@9788 2884
drchase@5353 2885 /**
drchase@5353 2886 * Arguments:
drchase@5353 2887 *
drchase@5353 2888 * Inputs:
drchase@5353 2889 * rsp(4) - int crc
drchase@5353 2890 * rsp(8) - byte* buf
drchase@5353 2891 * rsp(12) - int length
drchase@5353 2892 *
drchase@5353 2893 * Ouput:
drchase@5353 2894 * rax - int crc result
drchase@5353 2895 */
drchase@5353 2896 address generate_updateBytesCRC32() {
drchase@5353 2897 assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions");
drchase@5353 2898
drchase@5353 2899 __ align(CodeEntryAlignment);
drchase@5353 2900 StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32");
drchase@5353 2901
drchase@5353 2902 address start = __ pc();
drchase@5353 2903
drchase@5353 2904 const Register crc = rdx; // crc
drchase@5353 2905 const Register buf = rsi; // source java byte array address
drchase@5353 2906 const Register len = rcx; // length
drchase@5353 2907 const Register table = rdi; // crc_table address (reuse register)
drchase@5353 2908 const Register tmp = rbx;
drchase@5353 2909 assert_different_registers(crc, buf, len, table, tmp, rax);
drchase@5353 2910
drchase@5353 2911 BLOCK_COMMENT("Entry:");
drchase@5353 2912 __ enter(); // required for proper stackwalking of RuntimeStub frame
drchase@5353 2913 __ push(rsi);
drchase@5353 2914 __ push(rdi);
drchase@5353 2915 __ push(rbx);
drchase@5353 2916
drchase@5353 2917 Address crc_arg(rbp, 8 + 0);
drchase@5353 2918 Address buf_arg(rbp, 8 + 4);
drchase@5353 2919 Address len_arg(rbp, 8 + 8);
drchase@5353 2920
drchase@5353 2921 // Load up:
drchase@5353 2922 __ movl(crc, crc_arg);
drchase@5353 2923 __ movptr(buf, buf_arg);
drchase@5353 2924 __ movl(len, len_arg);
drchase@5353 2925
drchase@5353 2926 __ kernel_crc32(crc, buf, len, table, tmp);
drchase@5353 2927
drchase@5353 2928 __ movl(rax, crc);
drchase@5353 2929 __ pop(rbx);
drchase@5353 2930 __ pop(rdi);
drchase@5353 2931 __ pop(rsi);
drchase@5353 2932 __ leave(); // required for proper stackwalking of RuntimeStub frame
drchase@5353 2933 __ ret(0);
drchase@5353 2934
drchase@5353 2935 return start;
drchase@5353 2936 }
drchase@5353 2937
goetz@5400 2938 // Safefetch stubs.
goetz@5400 2939 void generate_safefetch(const char* name, int size, address* entry,
goetz@5400 2940 address* fault_pc, address* continuation_pc) {
goetz@5400 2941 // safefetch signatures:
goetz@5400 2942 // int SafeFetch32(int* adr, int errValue);
goetz@5400 2943 // intptr_t SafeFetchN (intptr_t* adr, intptr_t errValue);
goetz@5400 2944
goetz@5400 2945 StubCodeMark mark(this, "StubRoutines", name);
goetz@5400 2946
goetz@5400 2947 // Entry point, pc or function descriptor.
goetz@5400 2948 *entry = __ pc();
goetz@5400 2949
goetz@5400 2950 __ movl(rax, Address(rsp, 0x8));
goetz@5400 2951 __ movl(rcx, Address(rsp, 0x4));
goetz@5400 2952 // Load *adr into eax, may fault.
goetz@5400 2953 *fault_pc = __ pc();
goetz@5400 2954 switch (size) {
goetz@5400 2955 case 4:
goetz@5400 2956 // int32_t
goetz@5400 2957 __ movl(rax, Address(rcx, 0));
goetz@5400 2958 break;
goetz@5400 2959 case 8:
goetz@5400 2960 // int64_t
goetz@5400 2961 Unimplemented();
goetz@5400 2962 break;
goetz@5400 2963 default:
goetz@5400 2964 ShouldNotReachHere();
goetz@5400 2965 }
goetz@5400 2966
goetz@5400 2967 // Return errValue or *adr.
goetz@5400 2968 *continuation_pc = __ pc();
goetz@5400 2969 __ ret(0);
goetz@5400 2970 }
kvn@4205 2971
duke@435 2972 public:
duke@435 2973 // Information about frame layout at time of blocking runtime call.
duke@435 2974 // Note that we only have to preserve callee-saved registers since
duke@435 2975 // the compilers are responsible for supplying a continuation point
duke@435 2976 // if they expect all registers to be preserved.
duke@435 2977 enum layout {
duke@435 2978 thread_off, // last_java_sp
never@2978 2979 arg1_off,
never@2978 2980 arg2_off,
duke@435 2981 rbp_off, // callee saved register
duke@435 2982 ret_pc,
duke@435 2983 framesize
duke@435 2984 };
duke@435 2985
duke@435 2986 private:
duke@435 2987
duke@435 2988 #undef __
duke@435 2989 #define __ masm->
duke@435 2990
duke@435 2991 //------------------------------------------------------------------------------------------------------------------------
duke@435 2992 // Continuation point for throwing of implicit exceptions that are not handled in
duke@435 2993 // the current activation. Fabricates an exception oop and initiates normal
duke@435 2994 // exception dispatching in this frame.
duke@435 2995 //
duke@435 2996 // Previously the compiler (c2) allowed for callee save registers on Java calls.
duke@435 2997 // This is no longer true after adapter frames were removed but could possibly
duke@435 2998 // be brought back in the future if the interpreter code was reworked and it
duke@435 2999 // was deemed worthwhile. The comment below was left to describe what must
duke@435 3000 // happen here if callee saves were resurrected. As it stands now this stub
duke@435 3001 // could actually be a vanilla BufferBlob and have now oopMap at all.
duke@435 3002 // Since it doesn't make much difference we've chosen to leave it the
duke@435 3003 // way it was in the callee save days and keep the comment.
duke@435 3004
duke@435 3005 // If we need to preserve callee-saved values we need a callee-saved oop map and
duke@435 3006 // therefore have to make these stubs into RuntimeStubs rather than BufferBlobs.
duke@435 3007 // If the compiler needs all registers to be preserved between the fault
duke@435 3008 // point and the exception handler then it must assume responsibility for that in
duke@435 3009 // AbstractCompiler::continuation_for_implicit_null_exception or
duke@435 3010 // continuation_for_implicit_division_by_zero_exception. All other implicit
duke@435 3011 // exceptions (e.g., NullPointerException or AbstractMethodError on entry) are
duke@435 3012 // either at call sites or otherwise assume that stack unwinding will be initiated,
duke@435 3013 // so caller saved registers were assumed volatile in the compiler.
duke@435 3014 address generate_throw_exception(const char* name, address runtime_entry,
never@3136 3015 Register arg1 = noreg, Register arg2 = noreg) {
duke@435 3016
duke@435 3017 int insts_size = 256;
duke@435 3018 int locs_size = 32;
duke@435 3019
duke@435 3020 CodeBuffer code(name, insts_size, locs_size);
duke@435 3021 OopMapSet* oop_maps = new OopMapSet();
duke@435 3022 MacroAssembler* masm = new MacroAssembler(&code);
duke@435 3023
duke@435 3024 address start = __ pc();
duke@435 3025
duke@435 3026 // This is an inlined and slightly modified version of call_VM
duke@435 3027 // which has the ability to fetch the return PC out of
duke@435 3028 // thread-local storage and also sets up last_Java_sp slightly
duke@435 3029 // differently than the real call_VM
duke@435 3030 Register java_thread = rbx;
duke@435 3031 __ get_thread(java_thread);
duke@435 3032
duke@435 3033 __ enter(); // required for proper stackwalking of RuntimeStub frame
duke@435 3034
duke@435 3035 // pc and rbp, already pushed
never@739 3036 __ subptr(rsp, (framesize-2) * wordSize); // prolog
duke@435 3037
duke@435 3038 // Frame is now completed as far as size and linkage.
duke@435 3039
duke@435 3040 int frame_complete = __ pc() - start;
duke@435 3041
duke@435 3042 // push java thread (becomes first argument of C function)
never@739 3043 __ movptr(Address(rsp, thread_off * wordSize), java_thread);
never@2978 3044 if (arg1 != noreg) {
never@2978 3045 __ movptr(Address(rsp, arg1_off * wordSize), arg1);
never@2978 3046 }
never@2978 3047 if (arg2 != noreg) {
never@2978 3048 assert(arg1 != noreg, "missing reg arg");
never@2978 3049 __ movptr(Address(rsp, arg2_off * wordSize), arg2);
never@2978 3050 }
duke@435 3051
duke@435 3052 // Set up last_Java_sp and last_Java_fp
duke@435 3053 __ set_last_Java_frame(java_thread, rsp, rbp, NULL);
duke@435 3054
duke@435 3055 // Call runtime
duke@435 3056 BLOCK_COMMENT("call runtime_entry");
duke@435 3057 __ call(RuntimeAddress(runtime_entry));
duke@435 3058 // Generate oop map
duke@435 3059 OopMap* map = new OopMap(framesize, 0);
duke@435 3060 oop_maps->add_gc_map(__ pc() - start, map);
duke@435 3061
duke@435 3062 // restore the thread (cannot use the pushed argument since arguments
duke@435 3063 // may be overwritten by C code generated by an optimizing compiler);
duke@435 3064 // however can use the register value directly if it is callee saved.
duke@435 3065 __ get_thread(java_thread);
duke@435 3066
kevinw@8877 3067 __ reset_last_Java_frame(java_thread, true);
duke@435 3068
duke@435 3069 __ leave(); // required for proper stackwalking of RuntimeStub frame
duke@435 3070
duke@435 3071 // check for pending exceptions
duke@435 3072 #ifdef ASSERT
duke@435 3073 Label L;
never@739 3074 __ cmpptr(Address(java_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
duke@435 3075 __ jcc(Assembler::notEqual, L);
duke@435 3076 __ should_not_reach_here();
duke@435 3077 __ bind(L);
duke@435 3078 #endif /* ASSERT */
duke@435 3079 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
duke@435 3080
duke@435 3081
duke@435 3082 RuntimeStub* stub = RuntimeStub::new_runtime_stub(name, &code, frame_complete, framesize, oop_maps, false);
duke@435 3083 return stub->entry_point();
duke@435 3084 }
duke@435 3085
duke@435 3086
duke@435 3087 void create_control_words() {
duke@435 3088 // Round to nearest, 53-bit mode, exceptions masked
duke@435 3089 StubRoutines::_fpu_cntrl_wrd_std = 0x027F;
duke@435 3090 // Round to zero, 53-bit mode, exception mased
duke@435 3091 StubRoutines::_fpu_cntrl_wrd_trunc = 0x0D7F;
duke@435 3092 // Round to nearest, 24-bit mode, exceptions masked
duke@435 3093 StubRoutines::_fpu_cntrl_wrd_24 = 0x007F;
duke@435 3094 // Round to nearest, 64-bit mode, exceptions masked
duke@435 3095 StubRoutines::_fpu_cntrl_wrd_64 = 0x037F;
duke@435 3096 // Round to nearest, 64-bit mode, exceptions masked
duke@435 3097 StubRoutines::_mxcsr_std = 0x1F80;
duke@435 3098 // Note: the following two constants are 80-bit values
duke@435 3099 // layout is critical for correct loading by FPU.
duke@435 3100 // Bias for strict fp multiply/divide
duke@435 3101 StubRoutines::_fpu_subnormal_bias1[0]= 0x00000000; // 2^(-15360) == 0x03ff 8000 0000 0000 0000
duke@435 3102 StubRoutines::_fpu_subnormal_bias1[1]= 0x80000000;
duke@435 3103 StubRoutines::_fpu_subnormal_bias1[2]= 0x03ff;
duke@435 3104 // Un-Bias for strict fp multiply/divide
duke@435 3105 StubRoutines::_fpu_subnormal_bias2[0]= 0x00000000; // 2^(+15360) == 0x7bff 8000 0000 0000 0000
duke@435 3106 StubRoutines::_fpu_subnormal_bias2[1]= 0x80000000;
duke@435 3107 StubRoutines::_fpu_subnormal_bias2[2]= 0x7bff;
duke@435 3108 }
duke@435 3109
duke@435 3110 //---------------------------------------------------------------------------
duke@435 3111 // Initialization
duke@435 3112
duke@435 3113 void generate_initial() {
duke@435 3114 // Generates all stubs and initializes the entry points
duke@435 3115
duke@435 3116 //------------------------------------------------------------------------------------------------------------------------
duke@435 3117 // entry points that exist in all platforms
duke@435 3118 // Note: This is code that could be shared among different platforms - however the benefit seems to be smaller than
duke@435 3119 // the disadvantage of having a much more complicated generator structure. See also comment in stubRoutines.hpp.
duke@435 3120 StubRoutines::_forward_exception_entry = generate_forward_exception();
duke@435 3121
duke@435 3122 StubRoutines::_call_stub_entry =
duke@435 3123 generate_call_stub(StubRoutines::_call_stub_return_address);
duke@435 3124 // is referenced by megamorphic call
duke@435 3125 StubRoutines::_catch_exception_entry = generate_catch_exception();
duke@435 3126
duke@435 3127 // These are currently used by Solaris/Intel
duke@435 3128 StubRoutines::_atomic_xchg_entry = generate_atomic_xchg();
duke@435 3129
duke@435 3130 StubRoutines::_handler_for_unsafe_access_entry =
duke@435 3131 generate_handler_for_unsafe_access();
duke@435 3132
duke@435 3133 // platform dependent
duke@435 3134 create_control_words();
duke@435 3135
never@739 3136 StubRoutines::x86::_verify_mxcsr_entry = generate_verify_mxcsr();
never@739 3137 StubRoutines::x86::_verify_fpu_cntrl_wrd_entry = generate_verify_fpu_cntrl_wrd();
duke@435 3138 StubRoutines::_d2i_wrapper = generate_d2i_wrapper(T_INT,
duke@435 3139 CAST_FROM_FN_PTR(address, SharedRuntime::d2i));
duke@435 3140 StubRoutines::_d2l_wrapper = generate_d2i_wrapper(T_LONG,
duke@435 3141 CAST_FROM_FN_PTR(address, SharedRuntime::d2l));
never@2978 3142
never@2978 3143 // Build this early so it's available for the interpreter
bdelsart@3372 3144 StubRoutines::_throw_StackOverflowError_entry = generate_throw_exception("StackOverflowError throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_StackOverflowError));
drchase@5353 3145
drchase@5353 3146 if (UseCRC32Intrinsics) {
drchase@5353 3147 // set table address before stub generation which use it
drchase@5353 3148 StubRoutines::_crc_table_adr = (address)StubRoutines::x86::_crc_table;
drchase@5353 3149 StubRoutines::_updateBytesCRC32 = generate_updateBytesCRC32();
drchase@5353 3150 }
duke@435 3151 }
duke@435 3152
duke@435 3153
duke@435 3154 void generate_all() {
duke@435 3155 // Generates all stubs and initializes the entry points
duke@435 3156
duke@435 3157 // These entry points require SharedInfo::stack0 to be set up in non-core builds
duke@435 3158 // and need to be relocatable, so they each fabricate a RuntimeStub internally.
never@3136 3159 StubRoutines::_throw_AbstractMethodError_entry = generate_throw_exception("AbstractMethodError throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_AbstractMethodError));
never@3136 3160 StubRoutines::_throw_IncompatibleClassChangeError_entry= generate_throw_exception("IncompatibleClassChangeError throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_IncompatibleClassChangeError));
never@3136 3161 StubRoutines::_throw_NullPointerException_at_call_entry= generate_throw_exception("NullPointerException at call throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_NullPointerException_at_call));
duke@435 3162
duke@435 3163 //------------------------------------------------------------------------------------------------------------------------
duke@435 3164 // entry points that are platform specific
duke@435 3165
duke@435 3166 // support for verify_oop (must happen after universe_init)
duke@435 3167 StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
duke@435 3168
duke@435 3169 // arraycopy stubs used by compilers
duke@435 3170 generate_arraycopy_stubs();
jrose@1145 3171
never@1609 3172 generate_math_stubs();
kvn@4205 3173
kvn@4205 3174 // don't bother generating these AES intrinsic stubs unless global flag is set
kvn@4205 3175 if (UseAESIntrinsics) {
kvn@4205 3176 StubRoutines::x86::_key_shuffle_mask_addr = generate_key_shuffle_mask(); // might be needed by the others
kvn@4205 3177
kvn@4205 3178 StubRoutines::_aescrypt_encryptBlock = generate_aescrypt_encryptBlock();
kvn@4205 3179 StubRoutines::_aescrypt_decryptBlock = generate_aescrypt_decryptBlock();
kvn@4205 3180 StubRoutines::_cipherBlockChaining_encryptAESCrypt = generate_cipherBlockChaining_encryptAESCrypt();
kvn@4205 3181 StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptAESCrypt();
kvn@4205 3182 }
goetz@5400 3183
ascarpino@9788 3184 // Generate GHASH intrinsics code
ascarpino@9788 3185 if (UseGHASHIntrinsics) {
ascarpino@9788 3186 StubRoutines::x86::_ghash_long_swap_mask_addr = generate_ghash_long_swap_mask();
ascarpino@9788 3187 StubRoutines::x86::_ghash_byte_swap_mask_addr = generate_ghash_byte_swap_mask();
ascarpino@9788 3188 StubRoutines::_ghash_processBlocks = generate_ghash_processBlocks();
ascarpino@9788 3189 }
ascarpino@9788 3190
goetz@5400 3191 // Safefetch stubs.
goetz@5400 3192 generate_safefetch("SafeFetch32", sizeof(int), &StubRoutines::_safefetch32_entry,
goetz@5400 3193 &StubRoutines::_safefetch32_fault_pc,
goetz@5400 3194 &StubRoutines::_safefetch32_continuation_pc);
goetz@5400 3195 StubRoutines::_safefetchN_entry = StubRoutines::_safefetch32_entry;
goetz@5400 3196 StubRoutines::_safefetchN_fault_pc = StubRoutines::_safefetch32_fault_pc;
goetz@5400 3197 StubRoutines::_safefetchN_continuation_pc = StubRoutines::_safefetch32_continuation_pc;
duke@435 3198 }
duke@435 3199
duke@435 3200
duke@435 3201 public:
duke@435 3202 StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
duke@435 3203 if (all) {
duke@435 3204 generate_all();
duke@435 3205 } else {
duke@435 3206 generate_initial();
duke@435 3207 }
duke@435 3208 }
duke@435 3209 }; // end class declaration
duke@435 3210
duke@435 3211
duke@435 3212 void StubGenerator_generate(CodeBuffer* code, bool all) {
duke@435 3213 StubGenerator g(code, all);
duke@435 3214 }

mercurial