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