1 /*
   2  * Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "c1/c1_IR.hpp"
  27 #include "gc/shared/satbMarkQueue.hpp"
  28 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  29 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  30 #include "gc/shenandoah/shenandoahHeap.hpp"
  31 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  32 #include "gc/shenandoah/shenandoahRuntime.hpp"
  33 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  34 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  35 
  36 #ifdef ASSERT
  37 #define __ gen->lir(__FILE__, __LINE__)->
  38 #else
  39 #define __ gen->lir()->
  40 #endif
  41 
  42 void ShenandoahPreBarrierStub::emit_code(LIR_Assembler* ce) {
  43   ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
  44   bs->gen_pre_barrier_stub(ce, this);
  45 }
  46 
  47 void ShenandoahLoadReferenceBarrierStub::emit_code(LIR_Assembler* ce) {
  48   ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
  49   bs->gen_load_reference_barrier_stub(ce, this);
  50 }
  51 
  52 ShenandoahBarrierSetC1::ShenandoahBarrierSetC1() :
  53   _pre_barrier_c1_runtime_code_blob(NULL),
  54   _load_reference_barrier_rt_code_blob(NULL) {}
  55 
  56 void ShenandoahBarrierSetC1::pre_barrier(LIRGenerator* gen, CodeEmitInfo* info, DecoratorSet decorators, LIR_Opr addr_opr, LIR_Opr pre_val) {
  57   // First we test whether marking is in progress.
  58   BasicType flag_type;
  59   bool patch = (decorators & C1_NEEDS_PATCHING) != 0;
  60   bool do_load = pre_val == LIR_OprFact::illegalOpr;
  61   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
  62     flag_type = T_INT;
  63   } else {
  64     guarantee(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1,
  65               "Assumption");
  66     // Use unsigned type T_BOOLEAN here rather than signed T_BYTE since some platforms, eg. ARM,
  67     // need to use unsigned instructions to use the large offset to load the satb_mark_queue.
  68     flag_type = T_BOOLEAN;
  69   }
  70   LIR_Opr thrd = gen->getThreadPointer();
  71   LIR_Address* mark_active_flag_addr =
  72     new LIR_Address(thrd,
  73                     in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()),
  74                     flag_type);
  75   // Read the marking-in-progress flag.
  76   LIR_Opr flag_val = gen->new_register(T_INT);
  77   __ load(mark_active_flag_addr, flag_val);
  78   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
  79 
  80   LIR_PatchCode pre_val_patch_code = lir_patch_none;
  81 
  82   CodeStub* slow;
  83 
  84   if (do_load) {
  85     assert(pre_val == LIR_OprFact::illegalOpr, "sanity");
  86     assert(addr_opr != LIR_OprFact::illegalOpr, "sanity");
  87 
  88     if (patch)
  89       pre_val_patch_code = lir_patch_normal;
  90 
  91     pre_val = gen->new_register(T_OBJECT);
  92 
  93     if (!addr_opr->is_address()) {
  94       assert(addr_opr->is_register(), "must be");
  95       addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
  96     }
  97     slow = new ShenandoahPreBarrierStub(addr_opr, pre_val, pre_val_patch_code, info ? new CodeEmitInfo(info) : NULL);
  98   } else {
  99     assert(addr_opr == LIR_OprFact::illegalOpr, "sanity");
 100     assert(pre_val->is_register(), "must be");
 101     assert(pre_val->type() == T_OBJECT, "must be an object");
 102 
 103     slow = new ShenandoahPreBarrierStub(pre_val);
 104   }
 105 
 106   __ branch(lir_cond_notEqual, T_INT, slow);
 107   __ branch_destination(slow->continuation());
 108 }
 109 
 110 LIR_Opr ShenandoahBarrierSetC1::load_reference_barrier(LIRGenerator* gen, LIR_Opr obj, LIR_Opr addr, bool is_native) {
 111   if (ShenandoahLoadRefBarrier) {
 112     return load_reference_barrier_impl(gen, obj, addr, is_native);
 113   } else {
 114     return obj;
 115   }
 116 }
 117 
 118 LIR_Opr ShenandoahBarrierSetC1::load_reference_barrier_impl(LIRGenerator* gen, LIR_Opr obj, LIR_Opr addr, bool is_native) {
 119   assert(ShenandoahLoadRefBarrier, "Should be enabled");
 120 
 121   obj = ensure_in_register(gen, obj, T_OBJECT);
 122   assert(obj->is_register(), "must be a register at this point");
 123   addr = ensure_in_register(gen, addr, T_ADDRESS);
 124   assert(addr->is_register(), "must be a register at this point");
 125   LIR_Opr result = gen->result_register_for(obj->value_type());
 126   __ move(obj, result);
 127   LIR_Opr tmp1 = gen->new_register(T_ADDRESS);
 128   LIR_Opr tmp2 = gen->new_register(T_ADDRESS);
 129 
 130   LIR_Opr thrd = gen->getThreadPointer();
 131   LIR_Address* active_flag_addr =
 132     new LIR_Address(thrd,
 133                     in_bytes(ShenandoahThreadLocalData::gc_state_offset()),
 134                     T_BYTE);
 135   // Read and check the gc-state-flag.
 136   LIR_Opr flag_val = gen->new_register(T_INT);
 137   __ load(active_flag_addr, flag_val);
 138   LIR_Opr mask = LIR_OprFact::intConst(ShenandoahHeap::HAS_FORWARDED |
 139                                        ShenandoahHeap::EVACUATION |
 140                                        ShenandoahHeap::TRAVERSAL);
 141   LIR_Opr mask_reg = gen->new_register(T_INT);
 142   __ move(mask, mask_reg);
 143 
 144   if (TwoOperandLIRForm) {
 145     __ logical_and(flag_val, mask_reg, flag_val);
 146   } else {
 147     LIR_Opr masked_flag = gen->new_register(T_INT);
 148     __ logical_and(flag_val, mask_reg, masked_flag);
 149     flag_val = masked_flag;
 150   }
 151   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
 152 
 153   CodeStub* slow = new ShenandoahLoadReferenceBarrierStub(obj, addr, result, tmp1, tmp2, is_native);
 154   __ branch(lir_cond_notEqual, T_INT, slow);
 155   __ branch_destination(slow->continuation());
 156 
 157   return result;
 158 }
 159 
 160 LIR_Opr ShenandoahBarrierSetC1::ensure_in_register(LIRGenerator* gen, LIR_Opr obj, BasicType type) {
 161   if (!obj->is_register()) {
 162     LIR_Opr obj_reg;
 163     if (obj->is_constant()) {
 164       obj_reg = gen->new_register(type);
 165       __ move(obj, obj_reg);
 166     } else {
 167       obj_reg = gen->new_pointer_register();
 168       __ leal(obj, obj_reg);
 169     }
 170     obj = obj_reg;
 171   }
 172   return obj;
 173 }
 174 
 175 LIR_Opr ShenandoahBarrierSetC1::storeval_barrier(LIRGenerator* gen, LIR_Opr obj, CodeEmitInfo* info, DecoratorSet decorators) {
 176   if (ShenandoahStoreValEnqueueBarrier) {
 177     obj = ensure_in_register(gen, obj, T_OBJECT);
 178     pre_barrier(gen, info, decorators, LIR_OprFact::illegalOpr, obj);
 179   }
 180   return obj;
 181 }
 182 
 183 void ShenandoahBarrierSetC1::store_at_resolved(LIRAccess& access, LIR_Opr value) {
 184   if (access.is_oop()) {
 185     if (ShenandoahSATBBarrier) {
 186       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), access.resolved_addr(), LIR_OprFact::illegalOpr /* pre_val */);
 187     }
 188     value = storeval_barrier(access.gen(), value, access.access_emit_info(), access.decorators());
 189   }
 190   BarrierSetC1::store_at_resolved(access, value);
 191 }
 192 
 193 LIR_Opr ShenandoahBarrierSetC1::resolve_address(LIRAccess& access, bool resolve_in_register) {
 194   // We must resolve in register when patching. This is to avoid
 195   // having a patch area in the load barrier stub, since the call
 196   // into the runtime to patch will not have the proper oop map.
 197   const bool patch_before_barrier = access.is_oop() && (access.decorators() & C1_NEEDS_PATCHING) != 0;
 198   return BarrierSetC1::resolve_address(access, resolve_in_register || patch_before_barrier);
 199 }
 200 
 201 void ShenandoahBarrierSetC1::load_at_resolved(LIRAccess& access, LIR_Opr result) {
 202   // 1: non-reference load, no additional barrier is needed
 203   if (!access.is_oop()) {
 204     BarrierSetC1::load_at_resolved(access, result);
 205     return;
 206   }
 207 
 208   LIRGenerator* gen = access.gen();
 209   DecoratorSet decorators = access.decorators();
 210   BasicType type = access.type();
 211 
 212   // 2: load a reference from src location and apply LRB if ShenandoahLoadRefBarrier is set
 213   if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
 214     LIR_Opr tmp = gen->new_register(T_OBJECT);
 215     BarrierSetC1::load_at_resolved(access, tmp);
 216     bool is_native = ShenandoahBarrierSet::use_load_reference_barrier_native(decorators, type);
 217     tmp = load_reference_barrier(gen, tmp, access.resolved_addr(), is_native);
 218     __ move(tmp, result);
 219   } else {
 220     BarrierSetC1::load_at_resolved(access, result);
 221   }
 222 
 223   // 3: apply keep-alive barrier if ShenandoahSATBBarrier is set
 224   if (ShenandoahSATBBarrier) {
 225     bool is_weak = (decorators & ON_WEAK_OOP_REF) != 0;
 226     bool is_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
 227     bool is_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 228     bool is_traversal_mode = ShenandoahHeap::heap()->is_traversal_mode();
 229     bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0 || is_traversal_mode;
 230 
 231     if ((is_weak || is_phantom || is_anonymous) && keep_alive) {
 232       // Register the value in the referent field with the pre-barrier
 233       LabelObj *Lcont_anonymous;
 234       if (is_anonymous) {
 235         Lcont_anonymous = new LabelObj();
 236         generate_referent_check(access, Lcont_anonymous);
 237       }
 238       pre_barrier(gen, access.access_emit_info(), decorators, LIR_OprFact::illegalOpr /* addr_opr */,
 239                   result /* pre_val */);
 240       if (is_anonymous) {
 241         __ branch_destination(Lcont_anonymous->label());
 242       }
 243     }
 244  }
 245 }
 246 
 247 class C1ShenandoahPreBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
 248   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 249     ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
 250     bs->generate_c1_pre_barrier_runtime_stub(sasm);
 251     return NULL;
 252   }
 253 };
 254 
 255 class C1ShenandoahLoadReferenceBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
 256 private:
 257   const bool _is_native;
 258 
 259 public:
 260   C1ShenandoahLoadReferenceBarrierCodeGenClosure(bool is_native) : _is_native(is_native) {}
 261 
 262   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 263     ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
 264     bs->generate_c1_load_reference_barrier_runtime_stub(sasm, _is_native);
 265     return NULL;
 266   }
 267 };
 268 
 269 void ShenandoahBarrierSetC1::generate_c1_runtime_stubs(BufferBlob* buffer_blob) {
 270   C1ShenandoahPreBarrierCodeGenClosure pre_code_gen_cl;
 271   _pre_barrier_c1_runtime_code_blob = Runtime1::generate_blob(buffer_blob, -1,
 272                                                               "shenandoah_pre_barrier_slow",
 273                                                               false, &pre_code_gen_cl);
 274   if (ShenandoahLoadRefBarrier) {
 275     C1ShenandoahLoadReferenceBarrierCodeGenClosure lrb_code_gen_cl(false);
 276     _load_reference_barrier_rt_code_blob = Runtime1::generate_blob(buffer_blob, -1,
 277                                                                   "shenandoah_load_reference_barrier_slow",
 278                                                                   false, &lrb_code_gen_cl);
 279 
 280     C1ShenandoahLoadReferenceBarrierCodeGenClosure lrb_native_code_gen_cl(true);
 281     _load_reference_barrier_native_rt_code_blob = Runtime1::generate_blob(buffer_blob, -1,
 282                                                                    "shenandoah_load_reference_barrier_native_slow",
 283                                                                    false, &lrb_native_code_gen_cl);
 284   }
 285 }