1 /*
   2  * Copyright (c) 2016, 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 "c1/c1_LIRAssembler.hpp"
  28 #include "c1/c1_MacroAssembler.hpp"
  29 #include "gc/g1/c1G1BSCodeGen.hpp"
  30 #include "gc/g1/g1CardTable.hpp"
  31 #include "gc/g1/g1BarrierSet.hpp"
  32 #include "gc/g1/g1BSCodeGen.hpp"
  33 #include "gc/g1/heapRegion.hpp"
  34 #include "gc/shared/collectedHeap.hpp"
  35 #include "runtime/thread.hpp"
  36 #include "interpreter/interp_masm.hpp"
  37 
  38 #define __ masm->
  39 
  40 void G1BSCodeGen::gen_write_ref_array_pre_barrier(MacroAssembler* masm, DecoratorSet decorators, Register addr, Register count) {
  41   bool dest_uninitialized = (decorators & DEST_NOT_INITIALIZED) != 0;
  42 
  43   if (!dest_uninitialized) {
  44      __ pusha();                      // push registers
  45      if (count == c_rarg0) {
  46        if (addr == c_rarg1) {
  47          // exactly backwards!!
  48          __ xchgptr(c_rarg1, c_rarg0);
  49        } else {
  50          __ movptr(c_rarg1, count);
  51          __ movptr(c_rarg0, addr);
  52        }
  53      } else {
  54        __ movptr(c_rarg0, addr);
  55        __ movptr(c_rarg1, count);
  56      }
  57      __ call_VM_leaf(CAST_FROM_FN_PTR(address, ModRefBarrierSet::static_write_ref_array_pre), 2);
  58      __ popa();
  59   }
  60 }
  61 
  62 void G1BSCodeGen::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
  63                           Register dst, Address src, Register tmp1, Register tmp2) {
  64   bool on_oop = type == T_OBJECT || type == T_ARRAY;
  65   bool on_weak = (decorators & GC_ACCESS_ON_WEAK) != 0;
  66   bool on_phantom = (decorators & GC_ACCESS_ON_PHANTOM) != 0;
  67   bool on_reference = on_weak || on_phantom;
  68   ModRefBSCodeGen::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
  69   if (on_oop && on_reference) {
  70     const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
  71     NOT_LP64(__ get_thread(thread));
  72 
  73     // Generate the G1 pre-barrier code to log the value of
  74     // the referent field or jniHandle in an SATB buffer.
  75     g1_write_barrier_pre(masm /* masm */,
  76                          noreg /* obj */,
  77                          dst /* pre_val */,
  78                          thread /* thread */,
  79                          tmp1 /* tmp */,
  80                          true /* tosca_live */,
  81                          true /* expand_call */);
  82   }
  83 }
  84 
  85 void G1BSCodeGen::g1_write_barrier_pre(MacroAssembler* masm,
  86                                        Register obj,
  87                                        Register pre_val,
  88                                        Register thread,
  89                                        Register tmp,
  90                                        bool tosca_live,
  91                                        bool expand_call) {
  92   // If expand_call is true then we expand the call_VM_leaf macro
  93   // directly to skip generating the check by
  94   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
  95 
  96 #ifdef _LP64
  97   assert(thread == r15_thread, "must be");
  98 #endif // _LP64
  99 
 100   Label done;
 101   Label runtime;
 102 
 103   assert(pre_val != noreg, "check this code");
 104 
 105   if (obj != noreg) {
 106     assert_different_registers(obj, pre_val, tmp);
 107     assert(pre_val != rax, "check this code");
 108   }
 109 
 110   Address in_progress(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
 111                                        SATBMarkQueue::byte_offset_of_active()));
 112   Address index(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
 113                                        SATBMarkQueue::byte_offset_of_index()));
 114   Address buffer(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
 115                                        SATBMarkQueue::byte_offset_of_buf()));
 116 
 117 
 118   // Is marking active?
 119   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 120     __ cmpl(in_progress, 0);
 121   } else {
 122     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 123     __ cmpb(in_progress, 0);
 124   }
 125   __ jcc(Assembler::equal, done);
 126 
 127   // Do we need to load the previous value?
 128   if (obj != noreg) {
 129     __ load_heap_oop(pre_val, Address(obj, 0));
 130   }
 131 
 132   // Is the previous value null?
 133   __ cmpptr(pre_val, (int32_t) NULL_WORD);
 134   __ jcc(Assembler::equal, done);
 135 
 136   // Can we store original value in the thread's buffer?
 137   // Is index == 0?
 138   // (The index field is typed as size_t.)
 139 
 140   __ movptr(tmp, index);                   // tmp := *index_adr
 141   __ cmpptr(tmp, 0);                       // tmp == 0?
 142   __ jcc(Assembler::equal, runtime);       // If yes, goto runtime
 143 
 144   __ subptr(tmp, wordSize);                // tmp := tmp - wordSize
 145   __ movptr(index, tmp);                   // *index_adr := tmp
 146   __ addptr(tmp, buffer);                  // tmp := tmp + *buffer_adr
 147 
 148   // Record the previous value
 149   __ movptr(Address(tmp, 0), pre_val);
 150   __ jmp(done);
 151 
 152   __ bind(runtime);
 153   // save the live input values
 154   if(tosca_live) __ push(rax);
 155 
 156   if (obj != noreg && obj != rax)
 157     __ push(obj);
 158 
 159   if (pre_val != rax)
 160     __ push(pre_val);
 161 
 162   // Calling the runtime using the regular call_VM_leaf mechanism generates
 163   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
 164   // that checks that the *(ebp+frame::interpreter_frame_last_sp) == NULL.
 165   //
 166   // If we care generating the pre-barrier without a frame (e.g. in the
 167   // intrinsified Reference.get() routine) then ebp might be pointing to
 168   // the caller frame and so this check will most likely fail at runtime.
 169   //
 170   // Expanding the call directly bypasses the generation of the check.
 171   // So when we do not have have a full interpreter frame on the stack
 172   // expand_call should be passed true.
 173 
 174   NOT_LP64( __ push(thread); )
 175 
 176   if (expand_call) {
 177     LP64_ONLY( assert(pre_val != c_rarg1, "smashed arg"); )
 178     if (c_rarg1 != thread) {
 179       __ mov(c_rarg1, thread);
 180     }
 181     if (c_rarg0 != pre_val) {
 182       __ mov(c_rarg0, pre_val);
 183     }
 184     __ MacroAssembler::call_VM_leaf_base(CAST_FROM_FN_PTR(address, G1BarrierSet::g1_wb_pre), 2);
 185   } else {
 186     __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::g1_wb_pre), pre_val, thread);
 187   }
 188 
 189   NOT_LP64( __ pop(thread); )
 190 
 191   // save the live input values
 192   if (pre_val != rax)
 193     __ pop(pre_val);
 194 
 195   if (obj != noreg && obj != rax)
 196     __ pop(obj);
 197 
 198   if(tosca_live) __ pop(rax);
 199 
 200   __ bind(done);
 201 }
 202 
 203 void G1BSCodeGen::g1_write_barrier_post(MacroAssembler* masm,
 204                                         Register store_addr,
 205                                         Register new_val,
 206                                         Register thread,
 207                                         Register tmp,
 208                                         Register tmp2) {
 209 #ifdef _LP64
 210   assert(thread == r15_thread, "must be");
 211 #endif // _LP64
 212 
 213   Address queue_index(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
 214                                        DirtyCardQueue::byte_offset_of_index()));
 215   Address buffer(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
 216                                        DirtyCardQueue::byte_offset_of_buf()));
 217 
 218   CardTableModRefBS* ct =
 219     barrier_set_cast<CardTableModRefBS>(Universe::heap()->barrier_set());
 220   assert(sizeof(*ct->card_table()->byte_map_base()) == sizeof(jbyte), "adjust this code");
 221 
 222   Label done;
 223   Label runtime;
 224 
 225   // Does store cross heap regions?
 226 
 227   __ movptr(tmp, store_addr);
 228   __ xorptr(tmp, new_val);
 229   __ shrptr(tmp, HeapRegion::LogOfHRGrainBytes);
 230   __ jcc(Assembler::equal, done);
 231 
 232   // crosses regions, storing NULL?
 233 
 234   __ cmpptr(new_val, (int32_t) NULL_WORD);
 235   __ jcc(Assembler::equal, done);
 236 
 237   // storing region crossing non-NULL, is card already dirty?
 238 
 239   const Register card_addr = tmp;
 240   const Register cardtable = tmp2;
 241 
 242   __ movptr(card_addr, store_addr);
 243   __ shrptr(card_addr, CardTable::card_shift);
 244   // Do not use ExternalAddress to load 'byte_map_base', since 'byte_map_base' is NOT
 245   // a valid address and therefore is not properly handled by the relocation code.
 246   __ movptr(cardtable, (intptr_t)ct->card_table()->byte_map_base());
 247   __ addptr(card_addr, cardtable);
 248 
 249   __ cmpb(Address(card_addr, 0), (int)G1CardTable::g1_young_card_val());
 250   __ jcc(Assembler::equal, done);
 251 
 252   __ membar(Assembler::Membar_mask_bits(Assembler::StoreLoad));
 253   __ cmpb(Address(card_addr, 0), (int)G1CardTable::dirty_card_val());
 254   __ jcc(Assembler::equal, done);
 255 
 256 
 257   // storing a region crossing, non-NULL oop, card is clean.
 258   // dirty card and log.
 259 
 260   __ movb(Address(card_addr, 0), (int)G1CardTable::dirty_card_val());
 261 
 262   __ cmpl(queue_index, 0);
 263   __ jcc(Assembler::equal, runtime);
 264   __ subl(queue_index, wordSize);
 265   __ movptr(tmp2, buffer);
 266 #ifdef _LP64
 267   __ movslq(rscratch1, queue_index);
 268   __ addq(tmp2, rscratch1);
 269   __ movq(Address(tmp2, 0), card_addr);
 270 #else
 271   __ addl(tmp2, queue_index);
 272   __ movl(Address(tmp2, 0), card_addr);
 273 #endif
 274   __ jmp(done);
 275 
 276   __ bind(runtime);
 277   // save the live input values
 278   __ push(store_addr);
 279   __ push(new_val);
 280 #ifdef _LP64
 281   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::g1_wb_post), card_addr, r15_thread);
 282 #else
 283   __ push(thread);
 284   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::g1_wb_post), card_addr, thread);
 285   __ pop(thread);
 286 #endif
 287   __ pop(new_val);
 288   __ pop(store_addr);
 289 
 290   __ bind(done);
 291 }
 292 
 293 void G1BSCodeGen::oop_store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 294                                Address dst, Register val, Register tmp1, Register tmp2) {
 295   Register tmp3 = LP64_ONLY(r8) NOT_LP64(rsi);
 296   Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
 297   // flatten object address if needed
 298   // We do it regardless of precise because we need the registers
 299   if (dst.index() == noreg && dst.disp() == 0) {
 300     if (dst.base() != tmp1) {
 301       __ movptr(tmp1, dst.base());
 302     }
 303   } else {
 304     __ lea(tmp1, dst);
 305   }
 306 
 307   NOT_LP64(__ get_thread(rcx));
 308   NOT_LP64(__ save_bcp());
 309 
 310   g1_write_barrier_pre(masm /*masm*/,
 311                        tmp1 /* obj */,
 312                        tmp2 /* pre_val */,
 313                        rthread /* thread */,
 314                        tmp3  /* tmp */,
 315                        val != noreg /* tosca_live */,
 316                        false /* expand_call */);
 317   if (val == noreg) {
 318     __ store_heap_oop_null(Address(tmp1, 0));
 319   } else {
 320     // G1 barrier needs uncompressed oop for region cross check.
 321     Register new_val = val;
 322     if (UseCompressedOops) {
 323       new_val = tmp2;
 324       __ movptr(new_val, val);
 325     }
 326     __ store_heap_oop(Address(tmp1, 0), val);
 327     g1_write_barrier_post(masm /*masm*/,
 328                           tmp1 /* store_adr */,
 329                           new_val /* new_val */,
 330                           rthread /* thread */,
 331                           tmp3 /* tmp */,
 332                           tmp2 /* tmp2 */);
 333   }
 334   NOT_LP64( __ restore_bcp());
 335 }
 336 
 337 void G1BSCodeGen::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators, Register addr, Register count, Register tmp) {
 338   __ pusha();             // push registers (overkill)
 339   if (c_rarg0 == count) { // On win64 c_rarg0 == rcx
 340     assert_different_registers(c_rarg1, addr);
 341     __ mov(c_rarg1, count);
 342     __ mov(c_rarg0, addr);
 343   } else {
 344     assert_different_registers(c_rarg0, count);
 345     __ mov(c_rarg0, addr);
 346     __ mov(c_rarg1, count);
 347   }
 348   __ call_VM_leaf(CAST_FROM_FN_PTR(address, ModRefBarrierSet::static_write_ref_array_post), 2);
 349   __ popa();
 350 }
 351 
 352 #undef __
 353 #define __ ce->masm()->
 354 
 355 void G1BSCodeGen::gen_g1_pre_barrier_stub(LIR_Assembler* ce, G1PreBarrierStub* stub) {
 356   C1G1BSCodeGen* code_gen = (C1G1BSCodeGen*)Universe::heap()->barrier_set()->c1_code_gen();
 357   // At this point we know that marking is in progress.
 358   // If do_load() is true then we have to emit the
 359   // load of the previous value; otherwise it has already
 360   // been loaded into _pre_val.
 361 
 362   __ bind(stub->_entry);
 363   assert(stub->pre_val()->is_register(), "Precondition.");
 364 
 365   Register pre_val_reg = stub->pre_val()->as_register();
 366 
 367   if (stub->do_load()) {
 368     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/);
 369   }
 370 
 371   __ cmpptr(pre_val_reg, (int32_t)NULL_WORD);
 372   __ jcc(Assembler::equal, stub->_continuation);
 373   ce->store_parameter(stub->pre_val()->as_register(), 0);
 374   __ call(RuntimeAddress(code_gen->pre_barrier_c1_runtime_code_blob()->code_begin()));
 375   __ jmp(stub->_continuation);
 376 
 377 }
 378 
 379 void G1BSCodeGen::gen_g1_post_barrier_stub(LIR_Assembler* ce, G1PostBarrierStub* stub) {
 380   C1G1BSCodeGen* code_gen = (C1G1BSCodeGen*)Universe::heap()->barrier_set()->c1_code_gen();
 381   __ bind(stub->_entry);
 382   assert(stub->addr()->is_register(), "Precondition.");
 383   assert(stub->new_val()->is_register(), "Precondition.");
 384   Register new_val_reg = stub->new_val()->as_register();
 385   __ cmpptr(new_val_reg, (int32_t) NULL_WORD);
 386   __ jcc(Assembler::equal, stub->_continuation);
 387   ce->store_parameter(stub->addr()->as_pointer_register(), 0);
 388   __ call(RuntimeAddress(code_gen->post_barrier_c1_runtime_code_blob()->code_begin()));
 389   __ jmp(stub->_continuation);
 390 }
 391 
 392 #undef __
 393 
 394 #define __ sasm->
 395 
 396 void G1BSCodeGen::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
 397   __ prologue("g1_pre_barrier", false);
 398   // arg0 : previous value of memory
 399 
 400   BarrierSet* bs = Universe::heap()->barrier_set();
 401   __ push(rax);
 402   __ push(rdx);
 403 
 404   const Register pre_val = rax;
 405   const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
 406   const Register tmp = rdx;
 407 
 408   NOT_LP64(__ get_thread(thread);)
 409 
 410   Address queue_active(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
 411                                         SATBMarkQueue::byte_offset_of_active()));
 412   Address queue_index(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
 413                                        SATBMarkQueue::byte_offset_of_index()));
 414   Address buffer(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
 415                                   SATBMarkQueue::byte_offset_of_buf()));
 416 
 417   Label done;
 418   Label runtime;
 419 
 420   // Is marking still active?
 421   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 422     __ cmpl(queue_active, 0);
 423   } else {
 424     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 425     __ cmpb(queue_active, 0);
 426   }
 427   __ jcc(Assembler::equal, done);
 428 
 429   // Can we store original value in the thread's buffer?
 430 
 431   __ movptr(tmp, queue_index);
 432   __ testptr(tmp, tmp);
 433   __ jcc(Assembler::zero, runtime);
 434   __ subptr(tmp, wordSize);
 435   __ movptr(queue_index, tmp);
 436   __ addptr(tmp, buffer);
 437 
 438   // prev_val (rax)
 439   __ load_parameter(0, pre_val);
 440   __ movptr(Address(tmp, 0), pre_val);
 441   __ jmp(done);
 442 
 443   __ bind(runtime);
 444 
 445   __ save_live_registers_no_oop_map(3, true);
 446 
 447   // load the pre-value
 448   __ load_parameter(0, rcx);
 449   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::g1_wb_pre), rcx, thread);
 450 
 451   __ restore_live_registers(true);
 452 
 453   __ bind(done);
 454 
 455   __ pop(rdx);
 456   __ pop(rax);
 457 
 458   __ epilogue();
 459 }
 460 
 461 void G1BSCodeGen::generate_c1_post_barrier_runtime_stub(StubAssembler* sasm) {
 462   __ prologue("g1_post_barrier", false);
 463 
 464   // arg0: store_address
 465   Address store_addr(rbp, 2*BytesPerWord);
 466 
 467   CardTableModRefBS* ct =
 468     barrier_set_cast<CardTableModRefBS>(Universe::heap()->barrier_set());
 469   assert(sizeof(*ct->card_table()->byte_map_base()) == sizeof(jbyte), "adjust this code");
 470 
 471   Label done;
 472   Label enqueued;
 473   Label runtime;
 474 
 475   // At this point we know new_value is non-NULL and the new_value crosses regions.
 476   // Must check to see if card is already dirty
 477 
 478   const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
 479 
 480   Address queue_index(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
 481                                        DirtyCardQueue::byte_offset_of_index()));
 482   Address buffer(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
 483                                   DirtyCardQueue::byte_offset_of_buf()));
 484 
 485   __ push(rax);
 486   __ push(rcx);
 487 
 488   const Register cardtable = rax;
 489   const Register card_addr = rcx;
 490 
 491   __ load_parameter(0, card_addr);
 492   __ shrptr(card_addr, CardTable::card_shift);
 493   // Do not use ExternalAddress to load 'byte_map_base', since 'byte_map_base' is NOT
 494   // a valid address and therefore is not properly handled by the relocation code.
 495   __ movptr(cardtable, (intptr_t)ct->card_table()->byte_map_base());
 496   __ addptr(card_addr, cardtable);
 497 
 498   NOT_LP64(__ get_thread(thread);)
 499 
 500   __ cmpb(Address(card_addr, 0), (int)G1CardTable::g1_young_card_val());
 501   __ jcc(Assembler::equal, done);
 502 
 503   __ membar(Assembler::Membar_mask_bits(Assembler::StoreLoad));
 504   __ cmpb(Address(card_addr, 0), (int)G1CardTable::dirty_card_val());
 505   __ jcc(Assembler::equal, done);
 506 
 507   // storing region crossing non-NULL, card is clean.
 508   // dirty card and log.
 509 
 510   __ movb(Address(card_addr, 0), (int)G1CardTable::dirty_card_val());
 511 
 512   const Register tmp = rdx;
 513   __ push(rdx);
 514 
 515   __ movptr(tmp, queue_index);
 516   __ testptr(tmp, tmp);
 517   __ jcc(Assembler::zero, runtime);
 518   __ subptr(tmp, wordSize);
 519   __ movptr(queue_index, tmp);
 520   __ addptr(tmp, buffer);
 521   __ movptr(Address(tmp, 0), card_addr);
 522   __ jmp(enqueued);
 523 
 524   __ bind(runtime);
 525 
 526   __ save_live_registers_no_oop_map(3, true);
 527 
 528   __ call_VM_leaf(CAST_FROM_FN_PTR(address, G1BarrierSet::g1_wb_post), card_addr, thread);
 529 
 530   __ restore_live_registers(true);
 531 
 532   __ bind(enqueued);
 533   __ pop(rdx);
 534 
 535   __ bind(done);
 536   __ pop(rcx);
 537   __ pop(rax);
 538 
 539   __ epilogue();
 540 }
 541 
 542 #undef __