1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/shenandoah/brooksPointer.hpp"
  26 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  27 #include "gc/shenandoah/shenandoahConnectionMatrix.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.hpp"
  29 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  30 #include "gc/shenandoah/shenandoahRuntime.hpp"
  31 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "interpreter/interp_masm.hpp"
  34 #include "runtime/sharedRuntime.hpp"
  35 #include "runtime/thread.hpp"
  36 #include "utilities/macros.hpp"
  37 #ifdef COMPILER1
  38 #include "c1/c1_LIRAssembler.hpp"
  39 #include "c1/c1_MacroAssembler.hpp"
  40 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  41 #endif
  42 
  43 #define __ masm->
  44 
  45 address ShenandoahBarrierSetAssembler::_shenandoah_wb = NULL;
  46 address ShenandoahBarrierSetAssembler::_shenandoah_wb_C = NULL;
  47 
  48 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
  49                                                        Register src, Register dst, Register count) {
  50 
  51   bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
  52   bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
  53   bool obj_int = type == T_OBJECT LP64_ONLY(&& UseCompressedOops);
  54   bool dest_uninitialized = (decorators & AS_DEST_NOT_INITIALIZED) != 0;
  55 
  56   if (type == T_OBJECT || type == T_ARRAY) {
  57 #ifdef _LP64
  58     if (!checkcast && !obj_int) {
  59       // Save count for barrier
  60       __ movptr(r11, count);
  61     } else if (disjoint && obj_int) {
  62       // Save dst in r11 in the disjoint case
  63       __ movq(r11, dst);
  64     }
  65 #else
  66     if (disjoint) {
  67       __ mov(rdx, dst);          // save 'to'
  68     }
  69 #endif
  70 
  71     if (!dest_uninitialized) {
  72       Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
  73 #ifndef _LP64
  74       __ push(thread);
  75       __ get_thread(thread);
  76 #endif
  77 
  78       Label filtered;
  79       Address in_progress(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()));
  80       // Is marking active?
  81       if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
  82         __ cmpl(in_progress, 0);
  83       } else {
  84         assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
  85         __ cmpb(in_progress, 0);
  86       }
  87 
  88       NOT_LP64(__ pop(thread);)
  89 
  90         __ jcc(Assembler::equal, filtered);
  91 
  92       __ pusha();                      // push registers
  93 #ifdef _LP64
  94       if (count == c_rarg0) {
  95         if (dst == c_rarg1) {
  96           // exactly backwards!!
  97           __ xchgptr(c_rarg1, c_rarg0);
  98         } else {
  99           __ movptr(c_rarg1, count);
 100           __ movptr(c_rarg0, dst);
 101         }
 102       } else {
 103         __ movptr(c_rarg0, dst);
 104         __ movptr(c_rarg1, count);
 105       }
 106       if (UseCompressedOops) {
 107         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_narrow_oop_entry), 2);
 108       } else {
 109         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_oop_entry), 2);
 110       }
 111 #else
 112       __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_oop_entry),
 113                       dst, count);
 114 #endif
 115       __ popa();
 116       __ bind(filtered);
 117     }
 118   }
 119 
 120 }
 121 
 122 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 123                                                        Register src, Register dst, Register count) {
 124   bool checkcast = (decorators & ARRAYCOPY_CHECKCAST) != 0;
 125   bool disjoint = (decorators & ARRAYCOPY_DISJOINT) != 0;
 126   bool obj_int = type == T_OBJECT LP64_ONLY(&& UseCompressedOops);
 127   Register tmp = rax;
 128 
 129   if (type == T_OBJECT || type == T_ARRAY) {
 130 #ifdef _LP64
 131     if (!checkcast && !obj_int) {
 132       // Save count for barrier
 133       count = r11;
 134     } else if (disjoint && obj_int) {
 135       // Use the saved dst in the disjoint case
 136       dst = r11;
 137     } else if (checkcast) {
 138       tmp = rscratch1;
 139     }
 140 #else
 141     if (disjoint) {
 142       __ mov(dst, rdx); // restore 'to'
 143     }
 144 #endif
 145 
 146     __ pusha();             // push registers (overkill)
 147 #ifdef _LP64
 148     if (c_rarg0 == count) { // On win64 c_rarg0 == rcx
 149       assert_different_registers(c_rarg1, dst);
 150       __ mov(c_rarg1, count);
 151       __ mov(c_rarg0, dst);
 152     } else {
 153       assert_different_registers(c_rarg0, count);
 154       __ mov(c_rarg0, dst);
 155       __ mov(c_rarg1, count);
 156     }
 157     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_post_entry), 2);
 158 #else
 159     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_post_entry),
 160                     dst, count);
 161 #endif
 162     __ popa();
 163   }
 164 }
 165 
 166 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm,
 167                                                                  Register obj,
 168                                                                  Register pre_val,
 169                                                                  Register thread,
 170                                                                  Register tmp,
 171                                                                  bool tosca_live,
 172                                                                  bool expand_call) {
 173 
 174   if (ShenandoahSATBBarrier) {
 175     satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, tosca_live, expand_call);
 176   }
 177 }
 178 
 179 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm,
 180                                                            Register obj,
 181                                                            Register pre_val,
 182                                                            Register thread,
 183                                                            Register tmp,
 184                                                            bool tosca_live,
 185                                                            bool expand_call) {
 186   // If expand_call is true then we expand the call_VM_leaf macro
 187   // directly to skip generating the check by
 188   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
 189 
 190 #ifdef _LP64
 191   assert(thread == r15_thread, "must be");
 192 #endif // _LP64
 193 
 194   Label done;
 195   Label runtime;
 196 
 197   assert(pre_val != noreg, "check this code");
 198 
 199   if (obj != noreg) {
 200     assert_different_registers(obj, pre_val, tmp);
 201     assert(pre_val != rax, "check this code");
 202   }
 203 
 204   Address in_progress(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()));
 205   Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 206   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 207 
 208   Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 209   __ testb(gc_state, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL);
 210   __ jcc(Assembler::zero, done);
 211 
 212   // Do we need to load the previous value?
 213   if (obj != noreg) {
 214     __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
 215   }
 216 
 217   // Is the previous value null?
 218   __ cmpptr(pre_val, (int32_t) NULL_WORD);
 219   __ jcc(Assembler::equal, done);
 220 
 221   // Can we store original value in the thread's buffer?
 222   // Is index == 0?
 223   // (The index field is typed as size_t.)
 224 
 225   __ movptr(tmp, index);                   // tmp := *index_adr
 226   __ cmpptr(tmp, 0);                       // tmp == 0?
 227   __ jcc(Assembler::equal, runtime);       // If yes, goto runtime
 228 
 229   __ subptr(tmp, wordSize);                // tmp := tmp - wordSize
 230   __ movptr(index, tmp);                   // *index_adr := tmp
 231   __ addptr(tmp, buffer);                  // tmp := tmp + *buffer_adr
 232 
 233   // Record the previous value
 234   __ movptr(Address(tmp, 0), pre_val);
 235   __ jmp(done);
 236 
 237   __ bind(runtime);
 238   // save the live input values
 239   if(tosca_live) __ push(rax);
 240 
 241   if (obj != noreg && obj != rax)
 242     __ push(obj);
 243 
 244   if (pre_val != rax)
 245     __ push(pre_val);
 246 
 247   // Calling the runtime using the regular call_VM_leaf mechanism generates
 248   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
 249   // that checks that the *(ebp+frame::interpreter_frame_last_sp) == NULL.
 250   //
 251   // If we care generating the pre-barrier without a frame (e.g. in the
 252   // intrinsified Reference.get() routine) then ebp might be pointing to
 253   // the caller frame and so this check will most likely fail at runtime.
 254   //
 255   // Expanding the call directly bypasses the generation of the check.
 256   // So when we do not have have a full interpreter frame on the stack
 257   // expand_call should be passed true.
 258 
 259   NOT_LP64( __ push(thread); )
 260 
 261   if (expand_call) {
 262     LP64_ONLY( assert(pre_val != c_rarg1, "smashed arg"); )
 263 #ifdef _LP64
 264     if (c_rarg1 != thread) {
 265       __ mov(c_rarg1, thread);
 266     }
 267     if (c_rarg0 != pre_val) {
 268       __ mov(c_rarg0, pre_val);
 269     }
 270 #else
 271     __ push(thread);
 272     __ push(pre_val);
 273 #endif
 274     __ MacroAssembler::call_VM_leaf_base(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), 2);
 275   } else {
 276     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 277   }
 278 
 279   NOT_LP64( __ pop(thread); )
 280 
 281   // save the live input values
 282   if (pre_val != rax)
 283     __ pop(pre_val);
 284 
 285   if (obj != noreg && obj != rax)
 286     __ pop(obj);
 287 
 288   if(tosca_live) __ pop(rax);
 289 
 290   __ bind(done);
 291 }
 292 
 293 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_post(MacroAssembler* masm,
 294                                                                   Register store_addr,
 295                                                                   Register new_val,
 296                                                                   Register thread,
 297                                                                   Register tmp,
 298                                                                   Register tmp2) {
 299   assert(UseShenandoahGC, "why else should we be here?");
 300 
 301   if (! UseShenandoahMatrix) {
 302     // No need for that barrier if not using matrix.
 303     return;
 304   }
 305 
 306   Label done;
 307   __ testptr(new_val, new_val);
 308   __ jcc(Assembler::zero, done);
 309   ShenandoahConnectionMatrix* matrix = ShenandoahHeap::heap()->connection_matrix();
 310   address matrix_addr = matrix->matrix_addr();
 311   __ movptr(rscratch1, (intptr_t) ShenandoahHeap::heap()->base());
 312   // Compute to-region index
 313   __ movptr(tmp, new_val);
 314   __ subptr(tmp, rscratch1);
 315   __ shrptr(tmp, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 316   // Compute from-region index
 317   __ movptr(tmp2, store_addr);
 318   __ subptr(tmp2, rscratch1);
 319   __ shrptr(tmp2, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 320   // Compute matrix index
 321   __ imulptr(tmp, tmp, matrix->stride_jint());
 322   __ addptr(tmp, tmp2);
 323   // Address is _matrix[to * stride + from]
 324   __ movptr(rscratch1, (intptr_t) matrix_addr);
 325   // Test if the element is already set.
 326   __ cmpb(Address(rscratch1, tmp, Address::times_1), 0);
 327   __ jcc(Assembler::notEqual, done);
 328   // Store true, if not yet set.
 329   __ movb(Address(rscratch1, tmp, Address::times_1), 1);
 330   __ bind(done);
 331 }
 332 
 333 void ShenandoahBarrierSetAssembler::read_barrier(MacroAssembler* masm, Register dst) {
 334   if (ShenandoahReadBarrier) {
 335     read_barrier_impl(masm, dst);
 336   }
 337 }
 338 
 339 void ShenandoahBarrierSetAssembler::read_barrier_impl(MacroAssembler* masm, Register dst) {
 340   assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier), "should be enabled");
 341   Label is_null;
 342   __ testptr(dst, dst);
 343   __ jcc(Assembler::zero, is_null);
 344   read_barrier_not_null_impl(masm, dst);
 345   __ bind(is_null);
 346 }
 347 
 348 void ShenandoahBarrierSetAssembler::read_barrier_not_null(MacroAssembler* masm, Register dst) {
 349   if (ShenandoahReadBarrier) {
 350     read_barrier_not_null_impl(masm, dst);
 351   }
 352 }
 353 
 354 void ShenandoahBarrierSetAssembler::read_barrier_not_null_impl(MacroAssembler* masm, Register dst) {
 355   assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier), "should be enabled");
 356   __ movptr(dst, Address(dst, BrooksPointer::byte_offset()));
 357 }
 358 
 359 
 360 void ShenandoahBarrierSetAssembler::write_barrier(MacroAssembler* masm, Register dst) {
 361   if (ShenandoahWriteBarrier) {
 362     write_barrier_impl(masm, dst);
 363   }
 364 }
 365 
 366 void ShenandoahBarrierSetAssembler::write_barrier_impl(MacroAssembler* masm, Register dst) {
 367   assert(UseShenandoahGC && (ShenandoahWriteBarrier || ShenandoahStoreValEnqueueBarrier), "should be enabled");
 368 #ifdef _LP64
 369   assert(dst != rscratch1, "different regs");
 370 
 371   Label done;
 372 
 373   Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 374   __ testb(gc_state, ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL);
 375 
 376   // Now check if evacuation is in progress.
 377   read_barrier_not_null(masm, dst);
 378 
 379   __ jcc(Assembler::zero, done);
 380   __ push(rscratch1);
 381   __ push(rscratch2);
 382 
 383   __ movptr(rscratch1, dst);
 384   __ shrptr(rscratch1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 385   __ movptr(rscratch2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
 386   __ movbool(rscratch2, Address(rscratch2, rscratch1, Address::times_1));
 387   __ testb(rscratch2, 0x1);
 388 
 389   __ pop(rscratch2);
 390   __ pop(rscratch1);
 391 
 392   __ jcc(Assembler::zero, done);
 393 
 394   __ push(rscratch1);
 395 
 396   // Save possibly live regs.
 397   if (dst != rax) {
 398     __ push(rax);
 399   }
 400   if (dst != rbx) {
 401     __ push(rbx);
 402   }
 403   if (dst != rcx) {
 404     __ push(rcx);
 405   }
 406   if (dst != rdx) {
 407     __ push(rdx);
 408   }
 409   if (dst != c_rarg1) {
 410     __ push(c_rarg1);
 411   }
 412 
 413   __ subptr(rsp, 2 * Interpreter::stackElementSize);
 414   __ movdbl(Address(rsp, 0), xmm0);
 415 
 416   // Call into runtime
 417   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_IRT), dst);
 418   __ mov(rscratch1, rax);
 419 
 420   // Restore possibly live regs.
 421   __ movdbl(xmm0, Address(rsp, 0));
 422   __ addptr(rsp, 2 * Interpreter::stackElementSize);
 423 
 424   if (dst != c_rarg1) {
 425     __ pop(c_rarg1);
 426   }
 427   if (dst != rdx) {
 428     __ pop(rdx);
 429   }
 430   if (dst != rcx) {
 431     __ pop(rcx);
 432   }
 433   if (dst != rbx) {
 434     __ pop(rbx);
 435   }
 436   if (dst != rax) {
 437     __ pop(rax);
 438   }
 439 
 440   // Move result into dst reg.
 441   __ mov(dst, rscratch1);
 442 
 443   __ pop(rscratch1);
 444 
 445   __ bind(done);
 446 #else
 447   Unimplemented();
 448 #endif
 449 }
 450 
 451 void ShenandoahBarrierSetAssembler::storeval_barrier(MacroAssembler* masm, Register dst, Register tmp) {
 452   if (ShenandoahStoreValReadBarrier || ShenandoahStoreValEnqueueBarrier) {
 453     storeval_barrier_impl(masm, dst, tmp);
 454   }
 455 }
 456 
 457 void ShenandoahBarrierSetAssembler::storeval_barrier_impl(MacroAssembler* masm, Register dst, Register tmp) {
 458   assert(UseShenandoahGC && (ShenandoahStoreValReadBarrier || ShenandoahStoreValEnqueueBarrier), "should be enabled");
 459 
 460   if (dst == noreg) return;
 461 
 462 #ifdef _LP64
 463   if (ShenandoahStoreValEnqueueBarrier) {
 464     Label is_null;
 465     __ testptr(dst, dst);
 466     __ jcc(Assembler::zero, is_null);
 467     write_barrier_impl(masm, dst);
 468     __ bind(is_null);
 469 
 470     // The set of registers to be saved+restored is the same as in the write-barrier above.
 471     // Those are the commonly used registers in the interpreter.
 472     __ pusha();
 473     // __ push_callee_saved_registers();
 474     __ subptr(rsp, 2 * Interpreter::stackElementSize);
 475     __ movdbl(Address(rsp, 0), xmm0);
 476 
 477     satb_write_barrier_pre(masm, noreg, dst, r15_thread, tmp, true, false);
 478     __ movdbl(xmm0, Address(rsp, 0));
 479     __ addptr(rsp, 2 * Interpreter::stackElementSize);
 480     //__ pop_callee_saved_registers();
 481     __ popa();
 482   }
 483   if (ShenandoahStoreValReadBarrier) {
 484     read_barrier_impl(masm, dst);
 485   }
 486 #else
 487   Unimplemented();
 488 #endif
 489 }
 490 
 491 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 492              Register dst, Address src, Register tmp1, Register tmp_thread) {
 493   bool on_oop = type == T_OBJECT || type == T_ARRAY;
 494   bool in_heap = (decorators & IN_HEAP) != 0;
 495   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
 496   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
 497   bool on_reference = on_weak || on_phantom;
 498   if (in_heap) {
 499     read_barrier_not_null(masm, src.base());
 500   }
 501   BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
 502   if (ShenandoahKeepAliveBarrier && on_oop && on_reference) {
 503     const Register thread = NOT_LP64(tmp_thread) LP64_ONLY(r15_thread);
 504     NOT_LP64(__ get_thread(thread));
 505 
 506     // Generate the SATB pre-barrier code to log the value of
 507     // the referent field in an SATB buffer.
 508     shenandoah_write_barrier_pre(masm /* masm */,
 509                                  noreg /* obj */,
 510                                  dst /* pre_val */,
 511                                  thread /* thread */,
 512                                  tmp1 /* tmp */,
 513                                  true /* tosca_live */,
 514                                  true /* expand_call */);
 515   }
 516 }
 517 
 518 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 519               Address dst, Register val, Register tmp1, Register tmp2) {
 520 
 521   bool in_heap = (decorators & IN_HEAP) != 0;
 522   bool in_concurrent_root = (decorators & IN_CONCURRENT_ROOT) != 0;
 523   if (in_heap) {
 524     write_barrier(masm, dst.base());
 525   }
 526   if (type == T_OBJECT || type == T_ARRAY) {
 527     bool needs_pre_barrier = in_heap || in_concurrent_root;
 528     bool needs_post_barrier = val != noreg && in_heap && UseShenandoahMatrix;
 529 
 530     Register tmp3 = LP64_ONLY(r8) NOT_LP64(rsi);
 531     Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
 532     // flatten object address if needed
 533     // We do it regardless of precise because we need the registers
 534     if (dst.index() == noreg && dst.disp() == 0) {
 535       if (dst.base() != tmp1) {
 536         __ movptr(tmp1, dst.base());
 537       }
 538     } else {
 539       __ lea(tmp1, dst);
 540     }
 541 
 542 #ifndef _LP64
 543     InterpreterMacroAssembler *imasm = static_cast<InterpreterMacroAssembler*>(masm);
 544 #endif
 545 
 546     NOT_LP64(__ get_thread(rcx));
 547     NOT_LP64(imasm->save_bcp());
 548 
 549     if (needs_pre_barrier) {
 550       shenandoah_write_barrier_pre(masm /*masm*/,
 551                                    tmp1 /* obj */,
 552                                    tmp2 /* pre_val */,
 553                                    rthread /* thread */,
 554                                    tmp3  /* tmp */,
 555                                    val != noreg /* tosca_live */,
 556                                    false /* expand_call */);
 557     }
 558     if (val == noreg) {
 559       BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg);
 560     } else {
 561       storeval_barrier(masm, val, tmp3);
 562       Register new_val = val;
 563       if (needs_post_barrier) {
 564         if (UseCompressedOops) {
 565           new_val = tmp2;
 566           __ movptr(new_val, val);
 567         }
 568       }
 569       BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp1, 0), val, noreg, noreg);
 570 
 571       if (needs_post_barrier) {
 572         shenandoah_write_barrier_post(masm /*masm*/,
 573                                       tmp1 /* store_adr */,
 574                                       new_val /* new_val */,
 575                                       rthread /* thread */,
 576                                       tmp3 /* tmp */,
 577                                       tmp2 /* tmp2 */);
 578       }
 579     }
 580     NOT_LP64(imasm->restore_bcp());
 581 
 582   } else {
 583     BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2);
 584   }
 585 }
 586 
 587 void ShenandoahBarrierSetAssembler::obj_equals(MacroAssembler* masm, DecoratorSet decorators, Register op1, Register op2) {
 588   __ cmpptr(op1, op2);
 589   if (ShenandoahAcmpBarrier) {
 590     Label done;
 591     __ jccb(Assembler::equal, done);
 592     read_barrier(masm, op1);
 593     read_barrier(masm, op2);
 594     __ cmpptr(op1, op2);
 595     __ bind(done);
 596   }
 597 }
 598 
 599 void ShenandoahBarrierSetAssembler::obj_equals_addr(MacroAssembler* masm, DecoratorSet decorators, Register src1, Address src2) {
 600   __ cmpptr(src1, src2);
 601   if (ShenandoahAcmpBarrier) {
 602     Label done;
 603     __ jccb(Assembler::equal, done);
 604     __ movptr(rscratch2, src2);
 605     read_barrier(masm, src1);
 606     read_barrier(masm, rscratch2);
 607     __ cmpptr(src1, rscratch2);
 608     __ bind(done);
 609   }
 610 }
 611 
 612 void ShenandoahBarrierSetAssembler::resolve_for_read(MacroAssembler* masm, DecoratorSet decorators, Register obj) {
 613   bool oop_not_null = (decorators & OOP_NOT_NULL) != 0;
 614   if (oop_not_null) {
 615     read_barrier_not_null(masm, obj);
 616   } else {
 617     read_barrier(masm, obj);
 618   }
 619 }
 620 
 621 void ShenandoahBarrierSetAssembler::resolve_for_write(MacroAssembler* masm, DecoratorSet decorators, Register obj) {
 622   write_barrier(masm, obj);
 623 }
 624 
 625 // Special Shenandoah CAS implementation that handles false negatives
 626 // due to concurrent evacuation.
 627 #ifndef _LP64
 628 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, DecoratorSet decorators,
 629                                                 Register res, Address addr, Register oldval, Register newval,
 630                                                 bool exchange, bool encode, Register tmp1, Register tmp2) {
 631   // Shenandoah has no 32-bit version for this.
 632   Unimplemented();
 633 }
 634 #else
 635 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, DecoratorSet decorators,
 636                                                 Register res, Address addr, Register oldval, Register newval,
 637                                                 bool exchange, bool encode, Register tmp1, Register tmp2) {
 638 
 639   if (!ShenandoahCASBarrier) {
 640     BarrierSetAssembler::cmpxchg_oop(masm, decorators, res, addr, oldval, newval, exchange, encode, tmp1, tmp2);
 641     return;
 642   }
 643 
 644   assert(ShenandoahCASBarrier, "Should only be used when CAS barrier is enabled");
 645   assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
 646 
 647   Label retry, done;
 648 
 649   // Apply storeval barrier to newval.
 650   if (encode) {
 651     if (newval == c_rarg1 && ShenandoahStoreValEnqueueBarrier) {
 652       __ mov(tmp2, newval);
 653       storeval_barrier(masm, tmp2, tmp1);
 654     } else {
 655       storeval_barrier(masm, newval, tmp1);
 656     }
 657   }
 658 
 659   if (UseCompressedOops) {
 660     if (encode) {
 661       __ encode_heap_oop(oldval);
 662       __ mov(rscratch1, newval);
 663       __ encode_heap_oop(rscratch1);
 664       newval = rscratch1;
 665     }
 666   }
 667 
 668   // Remember oldval for retry logic below
 669   if (UseCompressedOops) {
 670     __ movl(tmp1, oldval);
 671   } else {
 672     __ movptr(tmp1, oldval);
 673   }
 674 
 675   // Step 1. Try to CAS with given arguments. If successful, then we are done,
 676   // and can safely return.
 677   if (os::is_MP()) __ lock();
 678   if (UseCompressedOops) {
 679     __ cmpxchgl(newval, addr);
 680   } else {
 681     __ cmpxchgptr(newval, addr);
 682   }
 683   __ jcc(Assembler::equal, done, true);
 684 
 685   // Step 2. CAS had failed. This may be a false negative.
 686   //
 687   // The trouble comes when we compare the to-space pointer with the from-space
 688   // pointer to the same object. To resolve this, it will suffice to read both
 689   // oldval and the value from memory through the read barriers -- this will give
 690   // both to-space pointers. If they mismatch, then it was a legitimate failure.
 691   //
 692   if (UseCompressedOops) {
 693     __ decode_heap_oop(tmp1);
 694   }
 695   __ resolve_for_read(0, tmp1);
 696 
 697   if (UseCompressedOops) {
 698     __ movl(tmp2, oldval);
 699     __ decode_heap_oop(tmp2);
 700   } else {
 701     __ movptr(tmp2, oldval);
 702   }
 703   __ resolve_for_read(0, tmp2);
 704 
 705   __ cmpptr(tmp1, tmp2);
 706   __ jcc(Assembler::notEqual, done, true);
 707 
 708   // Step 3. Try to CAS again with resolved to-space pointers.
 709   //
 710   // Corner case: it may happen that somebody stored the from-space pointer
 711   // to memory while we were preparing for retry. Therefore, we can fail again
 712   // on retry, and so need to do this in loop, always re-reading the failure
 713   // witness through the read barrier.
 714   __ bind(retry);
 715   if (os::is_MP()) __ lock();
 716   if (UseCompressedOops) {
 717     __ cmpxchgl(newval, addr);
 718   } else {
 719     __ cmpxchgptr(newval, addr);
 720   }
 721   __ jcc(Assembler::equal, done, true);
 722 
 723   if (UseCompressedOops) {
 724     __ movl(tmp2, oldval);
 725     __ decode_heap_oop(tmp2);
 726   } else {
 727     __ movptr(tmp2, oldval);
 728   }
 729   __ resolve_for_read(0, tmp2);
 730 
 731   __ cmpptr(tmp1, tmp2);
 732   __ jcc(Assembler::equal, retry, true);
 733 
 734   // Step 4. If we need a boolean result out of CAS, check the flag again,
 735   // and promote the result. Note that we handle the flag from both the CAS
 736   // itself and from the retry loop.
 737   __ bind(done);
 738   if (!exchange) {
 739     assert(res != NULL, "need result register");
 740     __ setb(Assembler::equal, res);
 741     __ movzbl(res, res);
 742   }
 743 }
 744 #endif // LP64
 745 
 746 void ShenandoahBarrierSetAssembler::xchg_oop(MacroAssembler* masm, DecoratorSet decorators,
 747                                              Register obj, Address addr, Register tmp) {
 748   storeval_barrier(masm, obj, tmp);
 749   BarrierSetAssembler::xchg_oop(masm, decorators, obj, addr, tmp);
 750 }
 751 
 752 #ifdef COMPILER1
 753 
 754 #undef __
 755 #define __ ce->masm()->
 756 
 757 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
 758   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
 759   // At this point we know that marking is in progress.
 760   // If do_load() is true then we have to emit the
 761   // load of the previous value; otherwise it has already
 762   // been loaded into _pre_val.
 763 
 764   __ bind(*stub->entry());
 765   assert(stub->pre_val()->is_register(), "Precondition.");
 766 
 767   Register pre_val_reg = stub->pre_val()->as_register();
 768 
 769   if (stub->do_load()) {
 770     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/);
 771   }
 772 
 773   __ cmpptr(pre_val_reg, (int32_t)NULL_WORD);
 774   __ jcc(Assembler::equal, *stub->continuation());
 775   ce->store_parameter(stub->pre_val()->as_register(), 0);
 776   __ call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
 777   __ jmp(*stub->continuation());
 778 
 779 }
 780 
 781 #undef __
 782 
 783 #define __ sasm->
 784 
 785 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
 786   __ prologue("shenandoah_pre_barrier", false);
 787   // arg0 : previous value of memory
 788 
 789   __ push(rax);
 790   __ push(rdx);
 791 
 792   const Register pre_val = rax;
 793   const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
 794   const Register tmp = rdx;
 795 
 796   NOT_LP64(__ get_thread(thread);)
 797 
 798   Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 799   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 800 
 801   Label done;
 802   Label runtime;
 803 
 804   // Is SATB still active?
 805   Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 806   __ testb(gc_state, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL);
 807   __ jcc(Assembler::zero, done);
 808 
 809   // Can we store original value in the thread's buffer?
 810 
 811   __ movptr(tmp, queue_index);
 812   __ testptr(tmp, tmp);
 813   __ jcc(Assembler::zero, runtime);
 814   __ subptr(tmp, wordSize);
 815   __ movptr(queue_index, tmp);
 816   __ addptr(tmp, buffer);
 817 
 818   // prev_val (rax)
 819   __ load_parameter(0, pre_val);
 820   __ movptr(Address(tmp, 0), pre_val);
 821   __ jmp(done);
 822 
 823   __ bind(runtime);
 824 
 825   __ save_live_registers_no_oop_map(true);
 826 
 827   // load the pre-value
 828   __ load_parameter(0, rcx);
 829   __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), rcx, thread);
 830 
 831   __ restore_live_registers(true);
 832 
 833   __ bind(done);
 834 
 835   __ pop(rdx);
 836   __ pop(rax);
 837 
 838   __ epilogue();
 839 }
 840 
 841 #undef __
 842 
 843 #endif // COMPILER1
 844 
 845 address ShenandoahBarrierSetAssembler::shenandoah_wb() {
 846   assert(_shenandoah_wb != NULL, "need write barrier stub");
 847   return _shenandoah_wb;
 848 }
 849 
 850 address ShenandoahBarrierSetAssembler::shenandoah_wb_C() {
 851   assert(_shenandoah_wb_C != NULL, "need write barrier stub");
 852   return _shenandoah_wb_C;
 853 }
 854 
 855 #define __ cgen->assembler()->
 856 
 857 address ShenandoahBarrierSetAssembler::generate_shenandoah_wb(StubCodeGenerator* cgen, bool c_abi, bool do_cset_test) {
 858   __ align(CodeEntryAlignment);
 859   StubCodeMark mark(cgen, "StubRoutines", "shenandoah_wb");
 860   address start = __ pc();
 861 
 862   Label not_done;
 863 
 864   // We use RDI, which also serves as argument register for slow call.
 865   // RAX always holds the src object ptr, except after the slow call and
 866   // the cmpxchg, then it holds the result.
 867   // R8 and RCX are used as temporary registers.
 868   if (!c_abi) {
 869     __ push(rdi);
 870     __ push(r8);
 871   }
 872 
 873   // Check for object beeing in the collection set.
 874   // TODO: Can we use only 1 register here?
 875   // The source object arrives here in rax.
 876   // live: rax
 877   // live: rdi
 878   if (!c_abi) {
 879     __ mov(rdi, rax);
 880   } else {
 881     if (rax != c_rarg0) {
 882       __ mov(rax, c_rarg0);
 883     }
 884   }
 885   if (do_cset_test) {
 886     __ shrptr(rdi, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 887     // live: r8
 888     __ movptr(r8, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
 889     __ movbool(r8, Address(r8, rdi, Address::times_1));
 890     // unlive: rdi
 891     __ testbool(r8);
 892     // unlive: r8
 893     __ jccb(Assembler::notZero, not_done);
 894 
 895     if (!c_abi) {
 896       __ pop(r8);
 897       __ pop(rdi);
 898     }
 899     __ ret(0);
 900 
 901     __ bind(not_done);
 902   }
 903 
 904   if (!c_abi) {
 905     __ push(rcx);
 906   }
 907 
 908   if (!c_abi) {
 909     __ push(rdx);
 910     __ push(rdi);
 911     __ push(rsi);
 912     __ push(r8);
 913     __ push(r9);
 914     __ push(r10);
 915     __ push(r11);
 916     __ push(r12);
 917     __ push(r13);
 918     __ push(r14);
 919     __ push(r15);
 920   }
 921   __ save_vector_registers();
 922   __ movptr(rdi, rax);
 923   __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_JRT), rdi);
 924   __ restore_vector_registers();
 925   if (!c_abi) {
 926     __ pop(r15);
 927     __ pop(r14);
 928     __ pop(r13);
 929     __ pop(r12);
 930     __ pop(r11);
 931     __ pop(r10);
 932     __ pop(r9);
 933     __ pop(r8);
 934     __ pop(rsi);
 935     __ pop(rdi);
 936     __ pop(rdx);
 937 
 938     __ pop(rcx);
 939     __ pop(r8);
 940     __ pop(rdi);
 941   }
 942   __ ret(0);
 943 
 944   return start;
 945 }
 946 
 947 #undef __
 948 
 949 void ShenandoahBarrierSetAssembler::barrier_stubs_init() {
 950   if (ShenandoahWriteBarrier || ShenandoahStoreValEnqueueBarrier) {
 951     int stub_code_size = 1536;
 952     ResourceMark rm;
 953     BufferBlob* bb = BufferBlob::create("shenandoah_barrier_stubs", stub_code_size);
 954     CodeBuffer buf(bb);
 955     StubCodeGenerator cgen(&buf);
 956     _shenandoah_wb = generate_shenandoah_wb(&cgen, false, true);
 957     _shenandoah_wb_C = generate_shenandoah_wb(&cgen, true, !ShenandoahWriteBarrierCsetTestInIR);
 958   }
 959 }