1 /*
   2  * Copyright (c) 2000, 2015, 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 #ifndef SHARE_VM_GC_SHARED_BARRIERSET_HPP
  26 #define SHARE_VM_GC_SHARED_BARRIERSET_HPP
  27 
  28 #include "memory/memRegion.hpp"
  29 #include "oops/oopsHierarchy.hpp"
  30 #include "asm/register.hpp"
  31 #include "utilities/fakeRttiSupport.hpp"
  32 
  33 // This class provides the interface between a barrier implementation and
  34 // the rest of the system.
  35 
  36 class MacroAssembler;
  37 
  38 class BarrierSet: public CHeapObj<mtGC> {
  39   friend class VMStructs;
  40 public:
  41   // Fake RTTI support.  For a derived class T to participate
  42   // - T must have a corresponding Name entry.
  43   // - GetName<T> must be specialized to return the corresponding Name
  44   //   entry.
  45   // - If T is a base class, the constructor must have a FakeRtti
  46   //   parameter and pass it up to its base class, with the tag set
  47   //   augmented with the corresponding Name entry.
  48   // - If T is a concrete class, the constructor must create a
  49   //   FakeRtti object whose tag set includes the corresponding Name
  50   //   entry, and pass it up to its base class.
  51 
  52   enum Name {                   // associated class
  53     ModRef,                     // ModRefBarrierSet
  54     CardTableModRef,            // CardTableModRefBS
  55     CardTableForRS,             // CardTableModRefBSForCTRS
  56     CardTableExtension,         // CardTableExtension
  57     G1SATBCT,                   // G1SATBCardTableModRefBS
  58     G1SATBCTLogging,            // G1SATBCardTableLoggingModRefBS
  59     ShenandoahBarrierSet        // ShenandoahBarrierSet
  60   };
  61 
  62 protected:
  63   typedef FakeRttiSupport<BarrierSet, Name> FakeRtti;
  64 
  65 private:
  66   FakeRtti _fake_rtti;
  67 
  68   // Metafunction mapping a class derived from BarrierSet to the
  69   // corresponding Name enum tag.
  70   template<typename T> struct GetName;
  71 
  72   // Downcast argument to a derived barrier set type.
  73   // The cast is checked in a debug build.
  74   // T must have a specialization for BarrierSet::GetName<T>.
  75   template<typename T> friend T* barrier_set_cast(BarrierSet* bs);
  76 
  77 public:
  78   // Note: This is not presently the Name corresponding to the
  79   // concrete class of this object.
  80   BarrierSet::Name kind() const { return _fake_rtti.concrete_tag(); }
  81 
  82   // Test whether this object is of the type corresponding to bsn.
  83   bool is_a(BarrierSet::Name bsn) const { return _fake_rtti.has_tag(bsn); }
  84 
  85   // End of fake RTTI support.
  86 
  87 public:
  88   enum Flags {
  89     None                = 0,
  90     TargetUninitialized = 1
  91   };
  92 
  93 protected:
  94   // Some barrier sets create tables whose elements correspond to parts of
  95   // the heap; the CardTableModRefBS is an example.  Such barrier sets will
  96   // normally reserve space for such tables, and commit parts of the table
  97   // "covering" parts of the heap that are committed. At most one covered
  98   // region per generation is needed.
  99   static const int _max_covered_regions = 2;
 100 
 101   BarrierSet(const FakeRtti& fake_rtti) : _fake_rtti(fake_rtti) { }
 102   ~BarrierSet() { }
 103 
 104 public:
 105 
 106   // These operations indicate what kind of barriers the BarrierSet has.
 107   virtual bool has_read_ref_barrier() = 0;
 108   virtual bool has_read_prim_barrier() = 0;
 109   virtual bool has_write_ref_barrier() = 0;
 110   virtual bool has_write_ref_pre_barrier() = 0;
 111   virtual bool has_write_prim_barrier() = 0;
 112 
 113   // These functions indicate whether a particular access of the given
 114   // kinds requires a barrier.
 115   virtual bool read_ref_needs_barrier(void* field) = 0;
 116   virtual bool read_prim_needs_barrier(HeapWord* field, size_t bytes) = 0;
 117   virtual bool write_prim_needs_barrier(HeapWord* field, size_t bytes,
 118                                         juint val1, juint val2) = 0;
 119 
 120   // The first four operations provide a direct implementation of the
 121   // barrier set.  An interpreter loop, for example, could call these
 122   // directly, as appropriate.
 123 
 124   // Invoke the barrier, if any, necessary when reading the given ref field.
 125   virtual void read_ref_field(void* field) = 0;
 126 
 127   // Invoke the barrier, if any, necessary when reading the given primitive
 128   // "field" of "bytes" bytes in "obj".
 129   virtual void read_prim_field(HeapWord* field, size_t bytes) = 0;
 130 
 131   // Invoke the barrier, if any, necessary when writing "new_val" into the
 132   // ref field at "offset" in "obj".
 133   // (For efficiency reasons, this operation is specialized for certain
 134   // barrier types.  Semantically, it should be thought of as a call to the
 135   // virtual "_work" function below, which must implement the barrier.)
 136   // First the pre-write versions...
 137   template <class T> inline void write_ref_field_pre(T* field, oop new_val);
 138 private:
 139   // Helper for write_ref_field_pre and friends, testing for specialized cases.
 140   bool devirtualize_reference_writes() const;
 141 
 142   // Keep this private so as to catch violations at build time.
 143   virtual void write_ref_field_pre_work(     void* field, oop new_val) { guarantee(false, "Not needed"); };
 144 protected:
 145   virtual void write_ref_field_pre_work(      oop* field, oop new_val) {};
 146   virtual void write_ref_field_pre_work(narrowOop* field, oop new_val) {};
 147 public:
 148 
 149   // ...then the post-write version.
 150   inline void write_ref_field(void* field, oop new_val, bool release = false);
 151 protected:
 152   virtual void write_ref_field_work(void* field, oop new_val, bool release) = 0;
 153 public:
 154 
 155   // Invoke the barrier, if any, necessary when writing the "bytes"-byte
 156   // value(s) "val1" (and "val2") into the primitive "field".
 157   virtual void write_prim_field(HeapWord* field, size_t bytes,
 158                                 juint val1, juint val2) = 0;
 159 
 160   // Operations on arrays, or general regions (e.g., for "clone") may be
 161   // optimized by some barriers.
 162 
 163   // The first six operations tell whether such an optimization exists for
 164   // the particular barrier.
 165   virtual bool has_read_ref_array_opt() = 0;
 166   virtual bool has_read_prim_array_opt() = 0;
 167   virtual bool has_write_ref_array_pre_opt() { return true; }
 168   virtual bool has_write_ref_array_opt() = 0;
 169   virtual bool has_write_prim_array_opt() = 0;
 170 
 171   virtual bool has_read_region_opt() = 0;
 172   virtual bool has_write_region_opt() = 0;
 173 
 174   // These operations should assert false unless the corresponding operation
 175   // above returns true.  Otherwise, they should perform an appropriate
 176   // barrier for an array whose elements are all in the given memory region.
 177   virtual void read_ref_array(MemRegion mr) = 0;
 178   virtual void read_prim_array(MemRegion mr) = 0;
 179 
 180   // Below length is the # array elements being written
 181   virtual void write_ref_array_pre(oop* dst, int length,
 182                                    bool dest_uninitialized = false) {}
 183   virtual void write_ref_array_pre(narrowOop* dst, int length,
 184                                    bool dest_uninitialized = false) {}
 185   // Below count is the # array elements being written, starting
 186   // at the address "start", which may not necessarily be HeapWord-aligned
 187   inline void write_ref_array(HeapWord* start, size_t count);
 188 
 189   // Static versions, suitable for calling from generated code;
 190   // count is # array elements being written, starting with "start",
 191   // which may not necessarily be HeapWord-aligned.
 192   static void static_write_ref_array_pre(HeapWord* start, size_t count);
 193   static void static_write_ref_array_post(HeapWord* start, size_t count);
 194 
 195 protected:
 196   virtual void write_ref_array_work(MemRegion mr) = 0;
 197 public:
 198   virtual void write_prim_array(MemRegion mr) = 0;
 199 
 200   virtual void read_region(MemRegion mr) = 0;
 201 
 202   // (For efficiency reasons, this operation is specialized for certain
 203   // barrier types.  Semantically, it should be thought of as a call to the
 204   // virtual "_work" function below, which must implement the barrier.)
 205   void write_region(MemRegion mr);
 206 protected:
 207   virtual void write_region_work(MemRegion mr) = 0;
 208 public:
 209   // Inform the BarrierSet that the the covered heap region that starts
 210   // with "base" has been changed to have the given size (possibly from 0,
 211   // for initialization.)
 212   virtual void resize_covered_region(MemRegion new_region) = 0;
 213 
 214   // If the barrier set imposes any alignment restrictions on boundaries
 215   // within the heap, this function tells whether they are met.
 216   virtual bool is_aligned(HeapWord* addr) = 0;
 217 
 218   // Print a description of the memory for the barrier set
 219   virtual void print_on(outputStream* st) const = 0;
 220 
 221   virtual oop read_barrier(oop src) {
 222     return src;
 223   }
 224   virtual oop write_barrier(oop src) {
 225     return src;
 226   }
 227   virtual oop resolve_and_update_oop(oop* p, oop obj) {
 228     return obj;
 229   }
 230   virtual oop resolve_and_update_oop(narrowOop* p, oop obj) {
 231     return obj;
 232   }
 233 #ifndef CC_INTERP
 234   virtual void interpreter_read_barrier(MacroAssembler* masm, Register dst) {
 235     // Default implementation does nothing.
 236   }
 237 
 238   virtual void interpreter_read_barrier_not_null(MacroAssembler* masm, Register dst) {
 239     // Default implementation does nothing.
 240   }
 241 
 242   virtual void interpreter_write_barrier(MacroAssembler* masm, Register dst) {
 243     // Default implementation does nothing.
 244   }
 245 #endif
 246 };
 247 
 248 template<typename T>
 249 inline T* barrier_set_cast(BarrierSet* bs) {
 250   assert(bs->is_a(BarrierSet::GetName<T>::value), "wrong type of barrier set");
 251   return static_cast<T*>(bs);
 252 }
 253 
 254 #endif // SHARE_VM_GC_SHARED_BARRIERSET_HPP