1 /*
   2  * Copyright (c) 2017, 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_CARDTABLE_HPP
  26 #define SHARE_VM_GC_SHARED_CARDTABLE_HPP
  27 
  28 #include "memory/memRegion.hpp"
  29 
  30 class CardTable: public CHeapObj<mtGC> {
  31   friend class VMStructs;
  32 protected:
  33   // The declaration order of these const fields is important; see the
  34   // constructor before changing.
  35   const bool      _scanned_concurrently;
  36   const MemRegion _whole_heap;       // the region covered by the card table
  37   size_t          _guard_index;      // index of very last element in the card
  38                                      // table; it is set to a guard value
  39                                      // (last_card) and should never be modified
  40   size_t          _last_valid_index; // index of the last valid element
  41   const size_t    _page_size;        // page size used when mapping _byte_map
  42   size_t          _byte_map_size;    // in bytes
  43   jbyte*          _byte_map;         // the card marking array
  44   jbyte*          _byte_map_base;
  45 
  46   int _cur_covered_regions;
  47   // The covered regions should be in address order.
  48   MemRegion* _covered;
  49   // The committed regions correspond one-to-one to the covered regions.
  50   // They represent the card-table memory that has been committed to service
  51   // the corresponding covered region.  It may be that committed region for
  52   // one covered region corresponds to a larger region because of page-size
  53   // roundings.  Thus, a committed region for one covered region may
  54   // actually extend onto the card-table space for the next covered region.
  55   MemRegion* _committed;
  56 
  57   // The last card is a guard card, and we commit the page for it so
  58   // we can use the card for verification purposes. We make sure we never
  59   // uncommit the MemRegion for that page.
  60   MemRegion _guard_region;
  61 
  62   inline size_t compute_byte_map_size();
  63 
  64   // Finds and return the index of the region, if any, to which the given
  65   // region would be contiguous.  If none exists, assign a new region and
  66   // returns its index.  Requires that no more than the maximum number of
  67   // covered regions defined in the constructor are ever in use.
  68   int find_covering_region_by_base(HeapWord* base);
  69 
  70   // Same as above, but finds the region containing the given address
  71   // instead of starting at a given base address.
  72   int find_covering_region_containing(HeapWord* addr);
  73 
  74   // Returns the leftmost end of a committed region corresponding to a
  75   // covered region before covered region "ind", or else "NULL" if "ind" is
  76   // the first covered region.
  77   HeapWord* largest_prev_committed_end(int ind) const;
  78 
  79   // Returns the part of the region mr that doesn't intersect with
  80   // any committed region other than self.  Used to prevent uncommitting
  81   // regions that are also committed by other regions.  Also protects
  82   // against uncommitting the guard region.
  83   MemRegion committed_unique_to_self(int self, MemRegion mr) const;
  84 
  85   // Some barrier sets create tables whose elements correspond to parts of
  86   // the heap; the CardTableModRefBS is an example.  Such barrier sets will
  87   // normally reserve space for such tables, and commit parts of the table
  88   // "covering" parts of the heap that are committed. At most one covered
  89   // region per generation is needed.
  90   static const int _max_covered_regions = 2;
  91 
  92   enum CardValues {
  93     clean_card                  = -1,
  94     // The mask contains zeros in places for all other values.
  95     clean_card_mask             = clean_card - 31,
  96 
  97     dirty_card                  =  0,
  98     precleaned_card             =  1,
  99     claimed_card                =  2,
 100     deferred_card               =  4,
 101     last_card                   =  8,
 102     CT_MR_BS_last_reserved      = 16
 103   };
 104 
 105   // a word's worth (row) of clean card values
 106   static const intptr_t clean_card_row = (intptr_t)(-1);
 107 
 108 public:
 109   CardTable(MemRegion whole_heap, bool conc_scan);
 110   virtual ~CardTable();
 111   virtual void initialize();
 112 
 113   // The kinds of precision a CardTableModRefBS may offer.
 114   enum PrecisionStyle {
 115     Precise,
 116     ObjHeadPreciseArray
 117   };
 118 
 119   // Tells what style of precision this card table offers.
 120   PrecisionStyle precision() {
 121     return ObjHeadPreciseArray; // Only one supported for now.
 122   }
 123 
 124   // *** Barrier set functions.
 125 
 126   // Initialization utilities; covered_words is the size of the covered region
 127   // in, um, words.
 128   inline size_t cards_required(size_t covered_words) {
 129     // Add one for a guard card, used to detect errors.
 130     const size_t words = align_size_up(covered_words, card_size_in_words);
 131     return words / card_size_in_words + 1;
 132   }
 133 
 134   // Dirty the bytes corresponding to "mr" (not all of which must be
 135   // covered.)
 136   void dirty_MemRegion(MemRegion mr);
 137 
 138   // Clear (to clean_card) the bytes entirely contained within "mr" (not
 139   // all of which must be covered.)
 140   void clear_MemRegion(MemRegion mr);
 141 
 142   // Return true if "p" is at the start of a card.
 143   bool is_card_aligned(HeapWord* p) {
 144     jbyte* pcard = byte_for(p);
 145     return (addr_for(pcard) == p);
 146   }
 147 
 148   // Mapping from address to card marking array entry
 149   jbyte* byte_for(const void* p) const {
 150     assert(_whole_heap.contains(p),
 151            "Attempt to access p = " PTR_FORMAT " out of bounds of "
 152            " card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",
 153            p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end()));
 154     jbyte* result = &_byte_map_base[uintptr_t(p) >> card_shift];
 155     assert(result >= _byte_map && result < _byte_map + _byte_map_size,
 156            "out of bounds accessor for card marking array");
 157     return result;
 158   }
 159 
 160   // The card table byte one after the card marking array
 161   // entry for argument address. Typically used for higher bounds
 162   // for loops iterating through the card table.
 163   jbyte* byte_after(const void* p) const {
 164     return byte_for(p) + 1;
 165   }
 166 
 167   virtual void invalidate(MemRegion mr);
 168   void clear(MemRegion mr);
 169   void dirty(MemRegion mr);
 170 
 171   // Provide read-only access to the card table array.
 172   const jbyte* byte_for_const(const void* p) const {
 173     return byte_for(p);
 174   }
 175   const jbyte* byte_after_const(const void* p) const {
 176     return byte_after(p);
 177   }
 178 
 179   // Mapping from card marking array entry to address of first word
 180   HeapWord* addr_for(const jbyte* p) const {
 181     assert(p >= _byte_map && p < _byte_map + _byte_map_size,
 182            "out of bounds access to card marking array");
 183     size_t delta = pointer_delta(p, _byte_map_base, sizeof(jbyte));
 184     HeapWord* result = (HeapWord*) (delta << card_shift);
 185     assert(_whole_heap.contains(result),
 186            "Returning result = " PTR_FORMAT " out of bounds of "
 187            " card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",
 188            p2i(result), p2i(_whole_heap.start()), p2i(_whole_heap.end()));
 189     return result;
 190   }
 191 
 192   // Mapping from address to card marking array index.
 193   size_t index_for(void* p) {
 194     assert(_whole_heap.contains(p),
 195            "Attempt to access p = " PTR_FORMAT " out of bounds of "
 196            " card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",
 197            p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end()));
 198     return byte_for(p) - _byte_map;
 199   }
 200 
 201   const jbyte* byte_for_index(const size_t card_index) const {
 202     return _byte_map + card_index;
 203   }
 204 
 205   // Resize one of the regions covered by the remembered set.
 206   virtual void resize_covered_region(MemRegion new_region);
 207 
 208   // *** Card-table-RemSet-specific things.
 209 
 210   static uintx ct_max_alignment_constraint();
 211 
 212   // Apply closure "cl" to the dirty cards containing some part of
 213   // MemRegion "mr".
 214   void dirty_card_iterate(MemRegion mr, MemRegionClosure* cl);
 215 
 216   // Return the MemRegion corresponding to the first maximal run
 217   // of dirty cards lying completely within MemRegion mr.
 218   // If reset is "true", then sets those card table entries to the given
 219   // value.
 220   MemRegion dirty_card_range_after_reset(MemRegion mr, bool reset,
 221                                          int reset_val);
 222   void verify();
 223 
 224   void verify_guard();
 225 
 226   // val_equals -> it will check that all cards covered by mr equal val
 227   // !val_equals -> it will check that all cards covered by mr do not equal val
 228   void verify_region(MemRegion mr, jbyte val, bool val_equals) PRODUCT_RETURN;
 229   void verify_not_dirty_region(MemRegion mr) PRODUCT_RETURN;
 230   void verify_dirty_region(MemRegion mr) PRODUCT_RETURN;
 231 
 232   // Print a description of the memory for the card table
 233   virtual void print_on(outputStream* st) const;
 234 
 235   // Constants
 236   enum SomePublicConstants {
 237     card_shift                  = 9,
 238     card_size                   = 1 << card_shift,
 239     card_size_in_words          = card_size / sizeof(HeapWord)
 240   };
 241 
 242   static jbyte clean_card_val()          { return clean_card; }
 243   static jbyte clean_card_mask_val()     { return clean_card_mask; }
 244   static jbyte dirty_card_val()          { return dirty_card; }
 245   static jbyte claimed_card_val()        { return claimed_card; }
 246   static jbyte precleaned_card_val()     { return precleaned_card; }
 247   static jbyte deferred_card_val()       { return deferred_card; }
 248   static intptr_t clean_card_row_val()   { return clean_card_row; }
 249 
 250   // Card marking array base (adjusted for heap low boundary)
 251   // This would be the 0th element of _byte_map, if the heap started at 0x0.
 252   // But since the heap starts at some higher address, this points to somewhere
 253   // before the beginning of the actual _byte_map.
 254   jbyte* byte_map_base() const { return _byte_map_base; }
 255   bool scanned_concurrently() const { return _scanned_concurrently; }
 256 
 257   virtual bool is_in_young(void* addr) const = 0;
 258 };
 259 
 260 #endif // SHARE_VM_GC_SHARED_CARDTABLE_HPP