blob: 5a1e9b5ef7d58ff95007237dc80b6e3a65b63092 [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
Elliott Hughesa168c832012-06-12 15:34:20 -070019#include <dynamic_annotations.h>
Ian Rogers5d76c432011-10-31 21:42:49 -070020
21#include "heap.h"
22#include "heap_bitmap.h"
23#include "logging.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080024#include "runtime.h"
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070025#include "space.h"
Elliott Hughesf0605a02012-01-13 20:12:02 -080026#include "utils.h"
Ian Rogers5d76c432011-10-31 21:42:49 -070027
28namespace art {
29/*
30 * Maintain a card table from the write barrier. All writes of
31 * non-NULL values to heap addresses should go through an entry in
32 * WriteBarrier, and from there to here.
33 *
34 * The heap is divided into "cards" of GC_CARD_SIZE bytes, as
35 * determined by GC_CARD_SHIFT. The card table contains one byte of
36 * data per card, to be used by the GC. The value of the byte will be
37 * one of GC_CARD_CLEAN or GC_CARD_DIRTY.
38 *
39 * After any store of a non-NULL object pointer into a heap object,
40 * code is obliged to mark the card dirty. The setters in
41 * object.h [such as SetFieldObject] do this for you. The
42 * compiler also contains code to mark cards as dirty.
43 *
44 * The card table's base [the "biased card table"] gets set to a
45 * rather strange value. In order to keep the JIT from having to
46 * fabricate or load GC_DIRTY_CARD to store into the card table,
47 * biased base is within the mmap allocation at a point where its low
Ian Rogers30fab402012-01-23 15:43:46 -080048 * byte is equal to GC_DIRTY_CARD. See CardTable::Create for details.
Ian Rogers5d76c432011-10-31 21:42:49 -070049 */
50
Ian Rogers30fab402012-01-23 15:43:46 -080051CardTable* CardTable::Create(const byte* heap_begin, size_t heap_capacity) {
Ian Rogers5d76c432011-10-31 21:42:49 -070052 /* Set up the card table */
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070053 size_t capacity = heap_capacity / kCardSize;
Ian Rogers5d76c432011-10-31 21:42:49 -070054 /* Allocate an extra 256 bytes to allow fixed low-byte of base */
Ian Rogers30fab402012-01-23 15:43:46 -080055 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous("dalvik-card-table", NULL,
56 capacity + 256, PROT_READ | PROT_WRITE));
57 if (mem_map.get() == NULL) {
Elliott Hughesf0605a02012-01-13 20:12:02 -080058 std::string maps;
59 ReadFileToString("/proc/self/maps", &maps);
60 LOG(FATAL) << "couldn't allocate card table\n" << maps;
Ian Rogers5d76c432011-10-31 21:42:49 -070061 }
Ian Rogers30fab402012-01-23 15:43:46 -080062 // All zeros is the correct initial value; all clean. Anonymous mmaps are initialized to zero, we
63 // don't clear the card table to avoid unnecessary pages being allocated
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070064 COMPILE_ASSERT(kCardClean == 0, card_clean_must_be_0);
Ian Rogers30fab402012-01-23 15:43:46 -080065
66 byte* cardtable_begin = mem_map->Begin();
67 CHECK(cardtable_begin != NULL);
68
69 // We allocated up to a bytes worth of extra space to allow biased_begin's byte value to equal
70 // GC_CARD_DIRTY, compute a offset value to make this the case
71 size_t offset = 0;
Elliott Hughes398f64b2012-03-26 18:05:48 -070072 byte* biased_begin = reinterpret_cast<byte*>(reinterpret_cast<uintptr_t>(cardtable_begin) -
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070073 (reinterpret_cast<uintptr_t>(heap_begin) >> kCardShift));
74 if (((uintptr_t)biased_begin & 0xff) != kCardDirty) {
75 int delta = kCardDirty - (reinterpret_cast<int>(biased_begin) & 0xff);
Ian Rogers30fab402012-01-23 15:43:46 -080076 offset = delta + (delta < 0 ? 0x100 : 0);
77 biased_begin += offset;
Ian Rogers5d76c432011-10-31 21:42:49 -070078 }
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070079 CHECK_EQ(reinterpret_cast<int>(biased_begin) & 0xff, kCardDirty);
Ian Rogers30fab402012-01-23 15:43:46 -080080
81 return new CardTable(mem_map.release(), biased_begin, offset);
Ian Rogers5d76c432011-10-31 21:42:49 -070082}
83
Elliott Hughesa168c832012-06-12 15:34:20 -070084CardTable::CardTable(MemMap* mem_map, byte* biased_begin, size_t offset)
85 : mem_map_(mem_map), biased_begin_(biased_begin), offset_(offset) {
86 byte* __attribute__((unused)) begin = mem_map_->Begin() + offset_;
87 byte* __attribute__((unused)) end = mem_map_->End();
88 ANNOTATE_BENIGN_RACE_SIZED(begin, (end - begin), "writes to GC card table");
89}
90
Mathieu Chartier2fde5332012-09-14 14:51:54 -070091void CardTable::ClearSpaceCards(ContinuousSpace* space) {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070092 // TODO: clear just the range of the table that has been modified
Mathieu Chartiercc236d72012-07-20 10:29:05 -070093 byte* card_start = CardFromAddr(space->Begin());
94 byte* card_end = CardFromAddr(space->End()); // Make sure to round up.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070095 memset(reinterpret_cast<void*>(card_start), kCardClean, card_end - card_start);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070096}
97
Ian Rogers5d76c432011-10-31 21:42:49 -070098void CardTable::ClearCardTable() {
Ian Rogers30fab402012-01-23 15:43:46 -080099 // TODO: clear just the range of the table that has been modified
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700100 memset(mem_map_->Begin(), kCardClean, mem_map_->Size());
Ian Rogers5d76c432011-10-31 21:42:49 -0700101}
102
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700103void CardTable::PreClearCards(ContinuousSpace* space, std::vector<byte*>& out_cards) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700104 byte* card_end = CardFromAddr(space->End());
105 for (byte* card_cur = CardFromAddr(space->Begin()); card_cur < card_end; ++card_cur) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700106 if (*card_cur == kCardDirty) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700107 out_cards.push_back(card_cur);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700108 *card_cur = kCardClean;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700109 }
110 }
111}
112
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700113void CardTable::GetDirtyCards(ContinuousSpace* space, std::vector<byte*>& out_cards) const {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700114 byte* card_end = CardFromAddr(space->End());
115 for (byte* card_cur = CardFromAddr(space->Begin());card_cur < card_end; ++card_cur) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700116 if (*card_cur == kCardDirty) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700117 out_cards.push_back(card_cur);
118 }
119 }
120}
121
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700122bool CardTable::AddrIsInCardTable(const void* addr) const {
123 return IsValidCard(biased_begin_ + ((uintptr_t)addr >> kCardShift));
124}
125
Ian Rogers30fab402012-01-23 15:43:46 -0800126void CardTable::CheckAddrIsInCardTable(const byte* addr) const {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700127 byte* card_addr = biased_begin_ + ((uintptr_t)addr >> kCardShift);
128 byte* begin = mem_map_->Begin() + offset_;
129 byte* end = mem_map_->End();
130 CHECK(AddrIsInCardTable(addr))
131 << "Card table " << this
132 << " begin: " << reinterpret_cast<void*>(begin)
133 << " end: " << reinterpret_cast<void*>(end)
134 << " card_addr: " << reinterpret_cast<void*>(card_addr)
135 << " heap begin: " << AddrFromCard(begin)
136 << " heap end: " << AddrFromCard(end)
137 << " addr: " << reinterpret_cast<const void*>(addr);
Ian Rogers5d76c432011-10-31 21:42:49 -0700138}
139
Ian Rogers5d76c432011-10-31 21:42:49 -0700140void CardTable::VerifyCardTable() {
141 UNIMPLEMENTED(WARNING) << "Card table verification";
142}
143
144} // namespace art