Home | History | Annotate | Download | only in accounting
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "card_table.h"
     18 
     19 #include <sys/mman.h>
     20 
     21 #include "base/mem_map.h"
     22 #include "base/systrace.h"
     23 #include "base/utils.h"
     24 #include "card_table-inl.h"
     25 #include "gc/heap.h"
     26 #include "gc/space/space.h"
     27 #include "heap_bitmap.h"
     28 #include "runtime.h"
     29 
     30 namespace art {
     31 namespace gc {
     32 namespace accounting {
     33 
     34 constexpr size_t CardTable::kCardShift;
     35 constexpr size_t CardTable::kCardSize;
     36 constexpr uint8_t CardTable::kCardClean;
     37 constexpr uint8_t CardTable::kCardDirty;
     38 
     39 /*
     40  * Maintain a card table from the write barrier. All writes of
     41  * non-null values to heap addresses should go through an entry in
     42  * WriteBarrier, and from there to here.
     43  *
     44  * The heap is divided into "cards" of `kCardSize` bytes, as
     45  * determined by `kCardShift`. The card table contains one byte of
     46  * data per card, to be used by the GC. The value of the byte will be
     47  * one of `kCardClean` or `kCardDirty`.
     48  *
     49  * After any store of a non-null object pointer into a heap object,
     50  * code is obliged to mark the card dirty. The setters in
     51  * object.h [such as SetFieldObject] do this for you. The
     52  * compiler also contains code to mark cards as dirty.
     53  *
     54  * The card table's base [the "biased card table"] gets set to a
     55  * rather strange value.  In order to keep the JIT from having to
     56  * fabricate or load `kCardDirty` to store into the card table,
     57  * biased base is within the mmap allocation at a point where its low
     58  * byte is equal to `kCardDirty`. See CardTable::Create for details.
     59  */
     60 
     61 CardTable* CardTable::Create(const uint8_t* heap_begin, size_t heap_capacity) {
     62   ScopedTrace trace(__PRETTY_FUNCTION__);
     63   /* Set up the card table */
     64   size_t capacity = heap_capacity / kCardSize;
     65   /* Allocate an extra 256 bytes to allow fixed low-byte of base */
     66   std::string error_msg;
     67   MemMap mem_map = MemMap::MapAnonymous("card table",
     68                                         capacity + 256,
     69                                         PROT_READ | PROT_WRITE,
     70                                         /*low_4gb=*/ false,
     71                                         &error_msg);
     72   CHECK(mem_map.IsValid()) << "couldn't allocate card table: " << error_msg;
     73   // All zeros is the correct initial value; all clean. Anonymous mmaps are initialized to zero, we
     74   // don't clear the card table to avoid unnecessary pages being allocated
     75   static_assert(kCardClean == 0, "kCardClean must be 0");
     76 
     77   uint8_t* cardtable_begin = mem_map.Begin();
     78   CHECK(cardtable_begin != nullptr);
     79 
     80   // We allocated up to a bytes worth of extra space to allow `biased_begin`'s byte value to equal
     81   // `kCardDirty`, compute a offset value to make this the case
     82   size_t offset = 0;
     83   uint8_t* biased_begin = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(cardtable_begin) -
     84       (reinterpret_cast<uintptr_t>(heap_begin) >> kCardShift));
     85   uintptr_t biased_byte = reinterpret_cast<uintptr_t>(biased_begin) & 0xff;
     86   if (biased_byte != kCardDirty) {
     87     int delta = kCardDirty - biased_byte;
     88     offset = delta + (delta < 0 ? 0x100 : 0);
     89     biased_begin += offset;
     90   }
     91   CHECK_EQ(reinterpret_cast<uintptr_t>(biased_begin) & 0xff, kCardDirty);
     92   return new CardTable(std::move(mem_map), biased_begin, offset);
     93 }
     94 
     95 CardTable::CardTable(MemMap&& mem_map, uint8_t* biased_begin, size_t offset)
     96     : mem_map_(std::move(mem_map)), biased_begin_(biased_begin), offset_(offset) {
     97 }
     98 
     99 CardTable::~CardTable() {
    100   // Destroys MemMap via std::unique_ptr<>.
    101 }
    102 
    103 void CardTable::ClearCardTable() {
    104   static_assert(kCardClean == 0, "kCardClean must be 0");
    105   mem_map_.MadviseDontNeedAndZero();
    106 }
    107 
    108 void CardTable::ClearCardRange(uint8_t* start, uint8_t* end) {
    109   CHECK_ALIGNED(reinterpret_cast<uintptr_t>(start), kCardSize);
    110   CHECK_ALIGNED(reinterpret_cast<uintptr_t>(end), kCardSize);
    111   static_assert(kCardClean == 0, "kCardClean must be 0");
    112   uint8_t* start_card = CardFromAddr(start);
    113   uint8_t* end_card = CardFromAddr(end);
    114   ZeroAndReleasePages(start_card, end_card - start_card);
    115 }
    116 
    117 bool CardTable::AddrIsInCardTable(const void* addr) const {
    118   return IsValidCard(biased_begin_ + ((uintptr_t)addr >> kCardShift));
    119 }
    120 
    121 void CardTable::CheckAddrIsInCardTable(const uint8_t* addr) const {
    122   uint8_t* card_addr = biased_begin_ + ((uintptr_t)addr >> kCardShift);
    123   uint8_t* begin = mem_map_.Begin() + offset_;
    124   uint8_t* end = mem_map_.End();
    125   CHECK(AddrIsInCardTable(addr))
    126       << "Card table " << this
    127       << " begin: " << reinterpret_cast<void*>(begin)
    128       << " end: " << reinterpret_cast<void*>(end)
    129       << " card_addr: " << reinterpret_cast<void*>(card_addr)
    130       << " heap begin: " << AddrFromCard(begin)
    131       << " heap end: " << AddrFromCard(end)
    132       << " addr: " << reinterpret_cast<const void*>(addr);
    133 }
    134 
    135 void CardTable::VerifyCardTable() {
    136   UNIMPLEMENTED(WARNING) << "Card table verification";
    137 }
    138 
    139 }  // namespace accounting
    140 }  // namespace gc
    141 }  // namespace art
    142