1 /*
   2  * Copyright (c) 2001, 2018, 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 "gc/g1/g1CardTable.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/shared/memset_with_concurrent_readers.hpp"
  29 #include "logging/log.hpp"
  30 #include "runtime/atomic.hpp"
  31 #include "runtime/orderAccess.hpp"
  32 
  33 bool G1CardTable::mark_card_deferred(size_t card_index) {
  34   CardValue val = _byte_map[card_index];
  35   // It's already processed
  36   if ((val & (clean_card_mask_val() | deferred_card_val())) == deferred_card_val()) {
  37     return false;
  38   }
  39 
  40   // Cached bit can be installed either on a clean card or on a claimed card.
  41   CardValue new_val = val;
  42   if (val == clean_card_val()) {
  43     new_val = deferred_card_val();
  44   } else {
  45     if (val & claimed_card_val()) {
  46       new_val = val | deferred_card_val();
  47     }
  48   }
  49   if (new_val != val) {
  50     Atomic::cmpxchg(new_val, &_byte_map[card_index], val);
  51   }
  52   return true;
  53 }
  54 
  55 void G1CardTable::g1_mark_as_young(const MemRegion& mr) {
  56   CardValue *const first = byte_for(mr.start());
  57   CardValue *const last = byte_after(mr.last());
  58 
  59   memset_with_concurrent_readers(first, g1_young_gen, last - first);
  60 }
  61 
  62 #ifndef PRODUCT
  63 void G1CardTable::verify_g1_young_region(MemRegion mr) {
  64   // G1FastWriteBarrier does not use the value G1CardTable::g1_young_gen
  65   // for cards. Cards for a young region can be dirty or clean, and they are
  66   // never scanned. So there is nothing to verify.
  67   if (!G1FastWriteBarrier) {
  68     verify_region(mr, g1_young_gen,  true);
  69   }
  70 }
  71 
  72 void G1CardTable::verfiy_claimed_dirty_region(MemRegion mr) {
  73   // _ct->print_content_for_mr(mr, tty);
  74   // All cards should be either "claimed" or "claimed and deferred".
  75   const CardValue claimed_val = dirty_card_val() | claimed_card_val();
  76   const CardValue deferred_val = claimed_val | deferred_card_val();
  77   CardValue* start = byte_for(mr.start());
  78   CardValue* end = byte_for(mr.last());
  79   bool failures = false;
  80   for (CardValue* curr = start; curr <= end; ++curr) {
  81     CardValue curr_val = *curr;
  82     bool failed = (curr_val != claimed_val && curr_val != deferred_val);
  83     if (failed) {
  84       if (!failures) {
  85         log_error(gc, verify)("== CT verification failed: [" INTPTR_FORMAT "," INTPTR_FORMAT "]", p2i(start), p2i(end));
  86         log_error(gc, verify)("==   expecting value: %d or %d", claimed_val, deferred_val);
  87         failures = true;
  88       }
  89       log_error(gc, verify)("==   card " PTR_FORMAT " [" PTR_FORMAT "," PTR_FORMAT "], val: %d",
  90                             p2i(curr), p2i(addr_for(curr)),
  91                             p2i((HeapWord*) (((size_t) addr_for(curr)) + card_size)),
  92                             (int) curr_val);
  93     }
  94   }
  95   guarantee(!failures, "there should be no failure");
  96 }
  97 #endif
  98 
  99 void G1CardTableChangedListener::on_commit(uint start_idx, size_t num_regions, bool zero_filled) {
 100   // Default value for a clean card on the card table is -1. So we cannot take advantage of the zero_filled parameter.
 101   MemRegion mr(G1CollectedHeap::heap()->bottom_addr_for_region(start_idx), num_regions * HeapRegion::GrainWords);
 102   _card_table->clear(mr);
 103 }
 104 
 105 void G1CardTable::initialize(G1RegionToSpaceMapper* mapper) {
 106   mapper->set_mapping_changed_listener(&_listener);
 107 
 108   _byte_map_size = mapper->reserved().byte_size();
 109 
 110   _guard_index = cards_required(_whole_heap.word_size()) - 1;
 111   _last_valid_index = _guard_index - 1;
 112 
 113   HeapWord* low_bound  = _whole_heap.start();
 114   HeapWord* high_bound = _whole_heap.end();
 115 
 116   _cur_covered_regions = 1;
 117   _covered[0] = _whole_heap;
 118 
 119   _byte_map = (CardValue*) mapper->reserved().start();
 120   _byte_map_base = _byte_map - (uintptr_t(low_bound) >> card_shift);
 121   assert(byte_for(low_bound) == &_byte_map[0], "Checking start of map");
 122   assert(byte_for(high_bound-1) <= &_byte_map[_last_valid_index], "Checking end of map");
 123 
 124   log_trace(gc, barrier)("G1CardTable::G1CardTable: ");
 125   log_trace(gc, barrier)("    &_byte_map[0]: " INTPTR_FORMAT "  &_byte_map[_last_valid_index]: " INTPTR_FORMAT,
 126                          p2i(&_byte_map[0]), p2i(&_byte_map[_last_valid_index]));
 127   log_trace(gc, barrier)("    _byte_map_base: " INTPTR_FORMAT,  p2i(_byte_map_base));
 128 }
 129 
 130 bool G1CardTable::is_in_young(oop obj) const {
 131   if (G1FastWriteBarrier) {
 132     return G1CollectedHeap::heap()->heap_region_containing(obj)->is_young();
 133   } else {
 134     volatile CardValue* p = byte_for(obj);
 135     return *p == G1CardTable::g1_young_card_val();
 136   }
 137 }