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