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