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 "base/logging.h"
20 #include "card_table-inl.h"
21 #include "gc/heap.h"
22 #include "gc/space/space.h"
23 #include "heap_bitmap.h"
24 #include "runtime.h"
25 #include "utils.h"
26
27 namespace art {
28 namespace gc {
29 namespace accounting {
30
31 /*
32 * Maintain a card table from the write barrier. All writes of
33 * non-NULL values to heap addresses should go through an entry in
34 * WriteBarrier, and from there to here.
35 *
36 * The heap is divided into "cards" of GC_CARD_SIZE bytes, as
37 * determined by GC_CARD_SHIFT. The card table contains one byte of
38 * data per card, to be used by the GC. The value of the byte will be
39 * one of GC_CARD_CLEAN or GC_CARD_DIRTY.
40 *
41 * After any store of a non-NULL object pointer into a heap object,
42 * code is obliged to mark the card dirty. The setters in
43 * object.h [such as SetFieldObject] do this for you. The
44 * compiler also contains code to mark cards as dirty.
45 *
46 * The card table's base [the "biased card table"] gets set to a
47 * rather strange value. In order to keep the JIT from having to
48 * fabricate or load GC_DIRTY_CARD to store into the card table,
49 * biased base is within the mmap allocation at a point where its low
50 * byte is equal to GC_DIRTY_CARD. See CardTable::Create for details.
51 */
52
Create(const byte * heap_begin,size_t heap_capacity)53 CardTable* CardTable::Create(const byte* heap_begin, size_t heap_capacity) {
54 /* Set up the card table */
55 size_t capacity = heap_capacity / kCardSize;
56 /* Allocate an extra 256 bytes to allow fixed low-byte of base */
57 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous("card table", NULL,
58 capacity + 256, PROT_READ | PROT_WRITE));
59 CHECK(mem_map.get() != NULL) << "couldn't allocate card table";
60 // All zeros is the correct initial value; all clean. Anonymous mmaps are initialized to zero, we
61 // don't clear the card table to avoid unnecessary pages being allocated
62 COMPILE_ASSERT(kCardClean == 0, card_clean_must_be_0);
63
64 byte* cardtable_begin = mem_map->Begin();
65 CHECK(cardtable_begin != NULL);
66
67 // We allocated up to a bytes worth of extra space to allow biased_begin's byte value to equal
68 // GC_CARD_DIRTY, compute a offset value to make this the case
69 size_t offset = 0;
70 byte* biased_begin = reinterpret_cast<byte*>(reinterpret_cast<uintptr_t>(cardtable_begin) -
71 (reinterpret_cast<uintptr_t>(heap_begin) >> kCardShift));
72 if (((uintptr_t)biased_begin & 0xff) != kCardDirty) {
73 int delta = kCardDirty - (reinterpret_cast<int>(biased_begin) & 0xff);
74 offset = delta + (delta < 0 ? 0x100 : 0);
75 biased_begin += offset;
76 }
77 CHECK_EQ(reinterpret_cast<int>(biased_begin) & 0xff, kCardDirty);
78
79 return new CardTable(mem_map.release(), biased_begin, offset);
80 }
81
CardTable(MemMap * mem_map,byte * biased_begin,size_t offset)82 CardTable::CardTable(MemMap* mem_map, byte* biased_begin, size_t offset)
83 : mem_map_(mem_map), biased_begin_(biased_begin), offset_(offset) {
84 byte* __attribute__((unused)) begin = mem_map_->Begin() + offset_;
85 byte* __attribute__((unused)) end = mem_map_->End();
86 }
87
ClearSpaceCards(space::ContinuousSpace * space)88 void CardTable::ClearSpaceCards(space::ContinuousSpace* space) {
89 // TODO: clear just the range of the table that has been modified
90 byte* card_start = CardFromAddr(space->Begin());
91 byte* card_end = CardFromAddr(space->End()); // Make sure to round up.
92 memset(reinterpret_cast<void*>(card_start), kCardClean, card_end - card_start);
93 }
94
ClearCardTable()95 void CardTable::ClearCardTable() {
96 // TODO: clear just the range of the table that has been modified
97 memset(mem_map_->Begin(), kCardClean, mem_map_->Size());
98 }
99
AddrIsInCardTable(const void * addr) const100 bool CardTable::AddrIsInCardTable(const void* addr) const {
101 return IsValidCard(biased_begin_ + ((uintptr_t)addr >> kCardShift));
102 }
103
CheckAddrIsInCardTable(const byte * addr) const104 void CardTable::CheckAddrIsInCardTable(const byte* addr) const {
105 byte* card_addr = biased_begin_ + ((uintptr_t)addr >> kCardShift);
106 byte* begin = mem_map_->Begin() + offset_;
107 byte* end = mem_map_->End();
108 CHECK(AddrIsInCardTable(addr))
109 << "Card table " << this
110 << " begin: " << reinterpret_cast<void*>(begin)
111 << " end: " << reinterpret_cast<void*>(end)
112 << " card_addr: " << reinterpret_cast<void*>(card_addr)
113 << " heap begin: " << AddrFromCard(begin)
114 << " heap end: " << AddrFromCard(end)
115 << " addr: " << reinterpret_cast<const void*>(addr);
116 }
117
VerifyCardTable()118 void CardTable::VerifyCardTable() {
119 UNIMPLEMENTED(WARNING) << "Card table verification";
120 }
121
122 } // namespace accounting
123 } // namespace gc
124 } // namespace art
125