1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shared/cardTable.hpp"
  30 #include "gc/shared/cardTableBarrierSet.hpp"
  31 #include "gc/shared/cardTableBarrierSetAssembler.hpp"
  32 #include "interpreter/interp_masm.hpp"
  33 
  34 #define __ masm->
  35 
  36 #ifdef PRODUCT
  37 #define BLOCK_COMMENT(str) /* nothing */
  38 #else
  39 #define BLOCK_COMMENT(str) __ block_comment(str)
  40 #endif
  41 
  42 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  43 
  44 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
  45 
  46 void CardTableBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators, Register addr, Register count,
  47                                                                     bool do_return) {
  48   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(BarrierSet::barrier_set());
  49   CardTable* ct = ctbs->card_table();
  50   assert(sizeof(*ct->byte_map_base()) == sizeof(jbyte), "adjust this code");
  51 
  52   NearLabel doXC, done;
  53   assert_different_registers(Z_R0, Z_R1, addr, count);
  54 
  55   // Nothing to do if count <= 0.
  56   if (!do_return) {
  57     __ compare64_and_branch(count, (intptr_t) 0, Assembler::bcondNotHigh, done);
  58   } else {
  59     __ z_ltgr(count, count);
  60     __ z_bcr(Assembler::bcondNotPositive, Z_R14);
  61   }
  62 
  63   // Note: We can't combine the shifts. We could lose a carry
  64   // from calculating the array end address.
  65   // count = (count-1)*BytesPerHeapOop + addr
  66   // Count holds addr of last oop in array then.
  67   __ z_sllg(count, count, LogBytesPerHeapOop);
  68   __ add2reg_with_index(count, -BytesPerHeapOop, count, addr);
  69 
  70   // Get base address of card table.
  71   __ load_const_optimized(Z_R1, (address)ct->byte_map_base());
  72 
  73   // count = (count>>shift) - (addr>>shift)
  74   __ z_srlg(addr,  addr,  CardTable::card_shift);
  75   __ z_srlg(count, count, CardTable::card_shift);
  76 
  77   // Prefetch first elements of card table for update.
  78   if (VM_Version::has_Prefetch()) {
  79     __ z_pfd(0x02, 0, addr, Z_R1);
  80   }
  81 
  82   // Special case: clear just one byte.
  83   __ clear_reg(Z_R0, true, false);  // Used for doOneByte.
  84   __ z_sgr(count, addr);            // Count = n-1 now, CC used for brc below.
  85   __ z_stc(Z_R0, 0, addr, Z_R1);    // Must preserve CC from z_sgr.
  86   if (!do_return) {
  87     __ z_brz(done);
  88   } else {
  89     __ z_bcr(Assembler::bcondZero, Z_R14);
  90   }
  91 
  92   __ z_cghi(count, 255);
  93   __ z_brnh(doXC);
  94 
  95   // MVCLE: clear a long area.
  96   // Start addr of card table range = base + addr.
  97   // # bytes in    card table range = (count + 1)
  98   __ add2reg_with_index(Z_R0, 0, Z_R1, addr);
  99   __ add2reg(Z_R1, 1, count);
 100 
 101   // dirty hack:
 102   // There are just two callers. Both pass
 103   // count in Z_ARG3 = Z_R4
 104   // addr  in Z_ARG2 = Z_R3
 105   // ==> use Z_ARG2 as src len reg = 0
 106   //         Z_ARG1 as src addr (ignored)
 107   assert(count == Z_ARG3, "count: unexpected register number");
 108   assert(addr  == Z_ARG2, "addr:  unexpected register number");
 109   __ clear_reg(Z_ARG2, true, false);
 110 
 111   __ MacroAssembler::move_long_ext(Z_R0, Z_ARG1, 0);
 112 
 113   if (!do_return) {
 114     __ z_bru(done);
 115   } else {
 116     __ z_bcr(Assembler::bcondAlways, Z_R14);
 117   }
 118 
 119   // XC: clear a short area.
 120   Label XC_template; // Instr template, never exec directly!
 121   __ bind(XC_template);
 122   __ z_xc(0, 0, addr, 0, addr);
 123 
 124   __ bind(doXC);
 125   // start addr of card table range = base + addr
 126   // end   addr of card table range = base + addr + count
 127   __ add2reg_with_index(addr, 0, Z_R1, addr);
 128 
 129   if (VM_Version::has_ExecuteExtensions()) {
 130     __ z_exrl(count, XC_template);   // Execute XC with var. len.
 131   } else {
 132     __ z_larl(Z_R1, XC_template);
 133     __ z_ex(count, 0, Z_R0, Z_R1);   // Execute XC with var. len.
 134   }
 135   if (do_return) {
 136     __ z_br(Z_R14);
 137   }
 138 
 139   __ bind(done);
 140 }
 141 
 142 void CardTableBarrierSetAssembler::store_check(MacroAssembler* masm, Register store_addr, Register tmp) {
 143   // Does a store check for the oop in register obj. The content of
 144   // register obj is destroyed afterwards.
 145   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(BarrierSet::barrier_set());
 146   CardTable* ct = ctbs->card_table();
 147   assert(sizeof(*ct->byte_map_base()) == sizeof(jbyte), "adjust this code");
 148 
 149   assert_different_registers(store_addr, tmp);
 150 
 151   __ z_srlg(store_addr, store_addr, CardTable::card_shift);
 152   __ load_absolute_address(tmp, (address)ct->byte_map_base());
 153   __ z_agr(store_addr, tmp);
 154   __ z_mvi(0, store_addr, CardTable::dirty_card_val());
 155 }
 156 
 157 void CardTableBarrierSetAssembler::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 158                                                 const Address& dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
 159   bool on_array = (decorators & IN_HEAP_ARRAY) != 0;
 160   bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 161   bool precise = on_array || on_anonymous;
 162 
 163   BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
 164 
 165   // No need for post barrier if storing NULL
 166   if (val != noreg) {
 167     const Register base = dst.base(),
 168                    idx  = dst.index();
 169     const intptr_t disp = dst.disp();
 170     if (precise && (disp != 0 || idx != noreg)) {
 171       __ add2reg_with_index(base, disp, idx, base);
 172     }
 173     store_check(masm, base, tmp1);
 174   }
 175 }