1 /*
   2  * Copyright (c) 2013, 2015, Red Hat, Inc. and/or its affiliates.
   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 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
  26 
  27 #include "gc/shenandoah/shenandoahHeap.hpp"
  28 #include "gc/shared/barrierSet.hpp"
  29 
  30 class ShenandoahBarrierSet: public BarrierSet {
  31 private:
  32 
  33   static inline oop get_shenandoah_forwardee_helper(oop p) {
  34     assert(UseShenandoahGC, "must only be called when Shenandoah is used.");
  35     assert(Universe::heap()->is_in(p), "We shouldn't be calling this on objects not in the heap");
  36     oop forwardee;
  37 #ifdef ASSERT
  38     if (ShenandoahVerifyReadsToFromSpace) {
  39       ShenandoahHeap* heap = (ShenandoahHeap *) Universe::heap();
  40       ShenandoahHeapRegion* region = heap->heap_region_containing(p);
  41       {
  42         region->memProtectionOff();
  43         forwardee = oop( *((HeapWord**) ((HeapWord*) p) - 1));
  44         region->memProtectionOn();
  45       }
  46     } else {
  47       forwardee = oop( *((HeapWord**) ((HeapWord*) p) - 1));
  48     }
  49 #else
  50     forwardee = oop( *((HeapWord**) ((HeapWord*) p) - 1));
  51 #endif
  52     return forwardee;
  53   }
  54 
  55 public:
  56 
  57   ShenandoahBarrierSet();
  58 
  59   void print_on(outputStream* st) const;
  60 
  61   bool is_a(BarrierSet::Name bsn);
  62 
  63   bool has_read_prim_array_opt();
  64   bool has_read_prim_barrier();
  65   bool has_read_ref_array_opt();
  66   bool has_read_ref_barrier();
  67   bool has_read_region_opt();
  68   bool has_write_prim_array_opt();
  69   bool has_write_prim_barrier();
  70   bool has_write_ref_array_opt();
  71   bool has_write_ref_barrier();
  72   bool has_write_ref_pre_barrier();
  73   bool has_write_region_opt();
  74   bool is_aligned(HeapWord* hw);
  75   void read_prim_array(MemRegion mr);
  76   void read_prim_field(HeapWord* hw, size_t s);
  77   bool read_prim_needs_barrier(HeapWord* hw, size_t s);
  78   void read_ref_array(MemRegion mr);
  79 
  80   void read_ref_field(void* v);
  81 
  82   bool read_ref_needs_barrier(void* v);
  83   void read_region(MemRegion mr);
  84   void resize_covered_region(MemRegion mr);
  85   void write_prim_array(MemRegion mr);
  86   void write_prim_field(HeapWord* hw, size_t s , juint x, juint y);
  87   bool write_prim_needs_barrier(HeapWord* hw, size_t s, juint x, juint y);
  88   void write_ref_array_work(MemRegion mr);
  89 
  90   template <class T> void
  91   write_ref_array_pre_work(T* dst, int count);
  92 
  93   void write_ref_array_pre(oop* dst, int count, bool dest_uninitialized);
  94 
  95   void write_ref_array_pre(narrowOop* dst, int count, bool dest_uninitialized);
  96 
  97 
  98   template <class T> static void write_ref_field_pre_static(T* field, oop newVal);
  99 
 100   // We export this to make it available in cases where the static
 101   // type of the barrier set is known.  Note that it is non-virtual.
 102   template <class T> inline void inline_write_ref_field_pre(T* field, oop newVal);
 103 
 104   // These are the more general virtual versions.
 105   void write_ref_field_pre_work(oop* field, oop new_val);
 106   void write_ref_field_pre_work(narrowOop* field, oop new_val);
 107   void write_ref_field_pre_work(void* field, oop new_val);
 108 
 109   void write_ref_field_work(void* v, oop o, bool release = false);
 110   void write_region_work(MemRegion mr);
 111 
 112   virtual oop read_barrier(oop src);
 113 
 114   virtual oop resolve_and_update_oop(oop* p, oop obj) {
 115     return resolve_and_update_oop_static(p, obj);
 116   }
 117   virtual oop resolve_and_update_oop(narrowOop* p, oop obj) {
 118     Unimplemented();
 119     return NULL;
 120   }
 121 
 122   template <class T>
 123   static inline oop resolve_and_update_oop_static(T p, oop obj) {
 124     oop forw = ShenandoahBarrierSet::resolve_oop_static_not_null(obj);
 125     if (forw != obj) {
 126       obj = forw;
 127       oopDesc::encode_store_heap_oop_not_null(p, obj);
 128     }
 129     return obj;
 130   }
 131 
 132   static inline oop resolve_oop_static_not_null(oop p) {
 133     assert(p != NULL, "Must be NULL checked");
 134 
 135     oop result = get_shenandoah_forwardee_helper(p);
 136 
 137     if (result != NULL) {
 138 #ifdef ASSERT
 139       if (result != p) {
 140         oop second_forwarding = get_shenandoah_forwardee_helper(result);
 141 
 142         // We should never be forwarded more than once.
 143         if (result != second_forwarding) {
 144           ShenandoahHeap* sh = (ShenandoahHeap*) Universe::heap();
 145           tty->print("first reference "PTR_FORMAT" is in heap region:\n", p2i((HeapWord*) p));
 146           sh->heap_region_containing(p)->print();
 147           tty->print("first_forwarding "PTR_FORMAT" is in heap region:\n", p2i((HeapWord*) result));
 148           sh->heap_region_containing(result)->print();
 149           tty->print("final reference "PTR_FORMAT" is in heap region:\n", p2i((HeapWord*) second_forwarding));
 150           sh->heap_region_containing(second_forwarding)->print();
 151           assert(get_shenandoah_forwardee_helper(result) == result, "Only one fowarding per customer");
 152         }
 153       }
 154 #endif
 155       if (! ShenandoahVerifyReadsToFromSpace) {
 156         // is_oop() would trigger a SEGFAULT when we're checking from-space-access.
 157         assert(ShenandoahHeap::heap()->is_in(result) && result->is_oop(), "resolved oop must be a valid oop in the heap");
 158       }
 159     }
 160     return result;
 161   }
 162 
 163   static inline oop resolve_oop_static(oop p) {
 164     if (((HeapWord*) p) != NULL) {
 165       return resolve_oop_static_not_null(p);
 166     } else {
 167       return p;
 168     }
 169   }
 170 
 171   static inline oop resolve_oop_static_no_check(oop p) {
 172     if (((HeapWord*) p) != NULL) {
 173       return get_shenandoah_forwardee_helper(p);
 174     } else {
 175       return p;
 176     }
 177   }
 178 
 179 
 180   oop resolve_and_maybe_copy_oopHelper(oop src);
 181   oop resolve_and_maybe_copy_oop_work(oop src);
 182   oop resolve_and_maybe_copy_oop_work2(oop src);
 183   virtual oop write_barrier(oop src);
 184 
 185   static oopDesc* write_barrier_c2(oopDesc* src);
 186   static oopDesc* write_barrier_interp(oopDesc* src);
 187   static oopDesc* write_barrier_c1(JavaThread* thread, oopDesc* src);
 188 
 189 private:
 190   bool need_update_refs_barrier();
 191 
 192 #ifndef CC_INTERP
 193 public:
 194   virtual void interpreter_read_barrier(MacroAssembler* masm, Register dst);
 195   virtual void interpreter_read_barrier_not_null(MacroAssembler* masm, Register dst);
 196   void interpreter_write_barrier(MacroAssembler* masm, Register dst);
 197 
 198 private:
 199   void compile_resolve_oop_runtime(MacroAssembler* masm, Register dst);
 200 
 201 #endif
 202 };
 203 
 204 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP