blob: 386b27cfbd75a9f85be54a82a3ae048a56dc5a2f [file] [log] [blame]
Ian Rogers5d76c432011-10-31 21:42:49 -07001/*
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> /* for PROT_* */
20
21#include "heap.h"
22#include "heap_bitmap.h"
23#include "logging.h"
Elliott Hughesf0605a02012-01-13 20:12:02 -080024#include "utils.h"
Ian Rogers5d76c432011-10-31 21:42:49 -070025
26namespace art {
27/*
28 * Maintain a card table from the write barrier. All writes of
29 * non-NULL values to heap addresses should go through an entry in
30 * WriteBarrier, and from there to here.
31 *
32 * The heap is divided into "cards" of GC_CARD_SIZE bytes, as
33 * determined by GC_CARD_SHIFT. The card table contains one byte of
34 * data per card, to be used by the GC. The value of the byte will be
35 * one of GC_CARD_CLEAN or GC_CARD_DIRTY.
36 *
37 * After any store of a non-NULL object pointer into a heap object,
38 * code is obliged to mark the card dirty. The setters in
39 * object.h [such as SetFieldObject] do this for you. The
40 * compiler also contains code to mark cards as dirty.
41 *
42 * The card table's base [the "biased card table"] gets set to a
43 * rather strange value. In order to keep the JIT from having to
44 * fabricate or load GC_DIRTY_CARD to store into the card table,
45 * biased base is within the mmap allocation at a point where its low
Ian Rogers30fab402012-01-23 15:43:46 -080046 * byte is equal to GC_DIRTY_CARD. See CardTable::Create for details.
Ian Rogers5d76c432011-10-31 21:42:49 -070047 */
48
Ian Rogers30fab402012-01-23 15:43:46 -080049CardTable* CardTable::Create(const byte* heap_begin, size_t heap_capacity) {
Ian Rogers5d76c432011-10-31 21:42:49 -070050 /* Set up the card table */
Ian Rogers30fab402012-01-23 15:43:46 -080051 size_t capacity = heap_capacity / GC_CARD_SIZE;
Ian Rogers5d76c432011-10-31 21:42:49 -070052 /* Allocate an extra 256 bytes to allow fixed low-byte of base */
Ian Rogers30fab402012-01-23 15:43:46 -080053 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous("dalvik-card-table", NULL,
54 capacity + 256, PROT_READ | PROT_WRITE));
55 if (mem_map.get() == NULL) {
Elliott Hughesf0605a02012-01-13 20:12:02 -080056 std::string maps;
57 ReadFileToString("/proc/self/maps", &maps);
58 LOG(FATAL) << "couldn't allocate card table\n" << maps;
Ian Rogers5d76c432011-10-31 21:42:49 -070059 }
Ian Rogers30fab402012-01-23 15:43:46 -080060 // 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
Ian Rogers5d76c432011-10-31 21:42:49 -070062 CHECK_EQ(GC_CARD_CLEAN, 0);
Ian Rogers30fab402012-01-23 15:43:46 -080063
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 = (byte *)((uintptr_t)cardtable_begin -((uintptr_t)heap_begin >> GC_CARD_SHIFT));
71 if (((uintptr_t)biased_begin & 0xff) != GC_CARD_DIRTY) {
72 int delta = GC_CARD_DIRTY - (reinterpret_cast<int>(biased_begin) & 0xff);
73 offset = delta + (delta < 0 ? 0x100 : 0);
74 biased_begin += offset;
Ian Rogers5d76c432011-10-31 21:42:49 -070075 }
Ian Rogers30fab402012-01-23 15:43:46 -080076 CHECK_EQ(reinterpret_cast<int>(biased_begin) & 0xff, GC_CARD_DIRTY);
77
78 return new CardTable(mem_map.release(), biased_begin, offset);
Ian Rogers5d76c432011-10-31 21:42:49 -070079}
80
81void CardTable::ClearCardTable() {
Ian Rogers30fab402012-01-23 15:43:46 -080082 // TODO: clear just the range of the table that has been modified
83 memset(mem_map_->Begin(), GC_CARD_CLEAN, mem_map_->Size());
Ian Rogers5d76c432011-10-31 21:42:49 -070084}
85
Ian Rogers30fab402012-01-23 15:43:46 -080086void CardTable::CheckAddrIsInCardTable(const byte* addr) const {
87 byte* cardAddr = biased_begin_ + ((uintptr_t)addr >> GC_CARD_SHIFT);
88 if (!IsValidCard(cardAddr)) {
89 byte* begin = mem_map_->Begin() + offset_;
90 byte* end = mem_map_->End();
91 LOG(FATAL) << "Cardtable - begin: " << reinterpret_cast<void*>(begin)
92 << " end: " << reinterpret_cast<void*>(end)
93 << " addr: " << reinterpret_cast<const void*>(addr)
94 << " cardAddr: " << reinterpret_cast<void*>(cardAddr);
95 }
Ian Rogers5d76c432011-10-31 21:42:49 -070096}
97
Ian Rogers30fab402012-01-23 15:43:46 -080098void CardTable::Scan(byte* heap_begin, byte* heap_end, Callback* visitor, void* arg) const {
99 byte* card_cur = CardFromAddr(heap_begin);
100 byte* card_end = CardFromAddr(heap_end);
101 while (card_cur < card_end) {
102 while (card_cur < card_end && *card_cur == GC_CARD_CLEAN) {
103 card_cur++;
Ian Rogers5d76c432011-10-31 21:42:49 -0700104 }
Ian Rogers30fab402012-01-23 15:43:46 -0800105 byte* run_start = card_cur;
Ian Rogers5d76c432011-10-31 21:42:49 -0700106 size_t run = 0;
Ian Rogers30fab402012-01-23 15:43:46 -0800107 while (card_cur < card_end && *card_cur == GC_CARD_DIRTY) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700108 run++;
Ian Rogers30fab402012-01-23 15:43:46 -0800109 card_cur++;
Ian Rogers5d76c432011-10-31 21:42:49 -0700110 }
111 if (run > 0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800112 byte* run_end = &card_cur[run];
Ian Rogers5d76c432011-10-31 21:42:49 -0700113 Heap::GetLiveBits()->VisitRange(reinterpret_cast<uintptr_t>(AddrFromCard(run_start)),
114 reinterpret_cast<uintptr_t>(AddrFromCard(run_end)),
115 visitor, arg);
116 }
117 }
118}
119
Ian Rogers5d76c432011-10-31 21:42:49 -0700120void CardTable::VerifyCardTable() {
121 UNIMPLEMENTED(WARNING) << "Card table verification";
122}
123
124} // namespace art