1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. 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 "asm/macroAssembler.inline.hpp"
  27 #include "gc/shared/barrierSet.hpp"
  28 #include "gc/shared/cardTable.hpp"
  29 #include "gc/shared/cardTableBarrierSet.hpp"
  30 #include "gc/shared/cardTableBarrierSetAssembler.hpp"
  31 #include "interpreter/interp_masm.hpp"
  32 
  33 #define __ masm->
  34 
  35 
  36 void CardTableBarrierSetAssembler::store_check(MacroAssembler* masm, Register obj, Address dst) {
  37 
  38   BarrierSet* bs = BarrierSet::barrier_set();
  39   assert(bs->kind() == BarrierSet::CardTableBarrierSet, "Wrong barrier set kind");
  40 
  41   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
  42   CardTable* ct = ctbs->card_table();
  43   assert(sizeof(*ct->byte_map_base()) == sizeof(jbyte), "adjust this code");
  44 
  45   __ lsr(obj, obj, CardTable::card_shift);
  46 
  47   assert(CardTable::dirty_card_val() == 0, "must be");
  48 
  49   __ load_byte_map_base(rscratch1);
  50 
  51   if (UseCondCardMark) {
  52     Label L_already_dirty;
  53     __ membar(Assembler::StoreLoad);
  54     __ ldrb(rscratch2,  Address(obj, rscratch1));
  55     __ cbz(rscratch2, L_already_dirty);
  56     __ strb(zr, Address(obj, rscratch1));
  57     __ bind(L_already_dirty);
  58   } else {
  59     if (UseConcMarkSweepGC && CMSPrecleaningEnabled) {
  60       __ membar(Assembler::StoreStore);
  61     }
  62     __ strb(zr, Address(obj, rscratch1));
  63   }
  64 }
  65 
  66 void CardTableBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
  67                                                                     Register start, Register end, Register scratch, RegSet saved_regs) {
  68   BarrierSet* bs = BarrierSet::barrier_set();
  69   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
  70   CardTable* ct = ctbs->card_table();
  71   assert(sizeof(*ct->byte_map_base()) == sizeof(jbyte), "adjust this code");
  72 
  73   Label L_loop;
  74 
  75   __ lsr(start, start, CardTable::card_shift);
  76   __ lsr(end, end, CardTable::card_shift);
  77   __ sub(end, end, start); // number of bytes to copy
  78 
  79   const Register count = end; // 'end' register contains bytes count now
  80   __ load_byte_map_base(scratch);
  81   __ add(start, start, scratch);
  82   if (UseConcMarkSweepGC) {
  83     __ membar(__ StoreStore);
  84   }
  85   __ bind(L_loop);
  86   __ strb(zr, Address(start, count));
  87   __ subs(count, count, 1);
  88   __ br(Assembler::GE, L_loop);
  89 }
  90 
  91 void CardTableBarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
  92                                                 Address dst, Register val, Register tmp1, Register tmp2) {
  93   bool on_array = (decorators & IN_HEAP_ARRAY) != 0;
  94   bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
  95   bool precise = on_array || on_anonymous;
  96   if (val == noreg) {
  97     __ store_heap_oop_null(dst);
  98   } else {
  99     __ store_heap_oop(dst, val);
 100     // flatten object address if needed
 101     if (!precise || (dst.index() == noreg && dst.offset() == 0)) {
 102       store_check(masm, dst.base(), dst);
 103     } else {
 104       __ lea(r3, dst);
 105       store_check(masm, r3, dst);
 106     }
 107   }
 108 }