blob: c8d7f08972e240a22b9bb599d0d26025108ab1fa [file] [log] [blame]
Barry Hayes6e5cf602010-06-22 12:32:59 -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/*
18 * Needed for PROT_* definitions.
19 */
20#include <sys/mman.h>
21
22#include "Dalvik.h"
23#include "alloc/HeapSource.h"
24#include "alloc/Visit.h"
25
26/*
27 * Maintain a card table from the the write barrier. All writes of
28 * non-NULL values to heap addresses should go through an entry in
29 * WriteBarrier, and from there to here.
30 *
Barry Hayes8f921a72010-07-09 12:53:49 -070031 * The heap is divided into "cards" of GC_CARD_SIZE bytes, as
32 * determined by GC_CARD_SHIFT. The card table contains one byte of
33 * data per card, to be used by the GC. The value of the byte will be
34 * one of GC_CARD_CLEAN or GC_CARD_DIRTY.
Barry Hayes6e5cf602010-06-22 12:32:59 -070035 *
36 * After any store of a non-NULL object pointer into a heap object,
37 * code is obliged to mark the card dirty. The setters in
38 * ObjectInlines.h [such as dvmSetFieldObject] do this for you. The
39 * JIT and fast interpreters also contain code to mark cards as dirty.
40 *
41 * [TODO: Concurrent collection will have to expand on this, as it
42 * uses the card table as well.]
43 *
44 * The card table is used to support partial collection, which at the
45 * moment means "treat the zygote's heap as permanent, and only GC
46 * objects in the application heap". In order to do this efficiently,
47 * the GC need to find quickly references to objects in the
48 * application heap from the zygote heap. When an application creates
49 * an object and stores it into an object on the zygote heap, it will
50 * mark the corresponding card in the zygote heap as "dirty". When the
51 * GC does a partial collection, it can efficiently find all the
52 * cross-heap objects, since they are all on dirty cards. The GC also
53 * takes the opportunity to mark as "clean" any cards which are dirty,
54 * but no longer contain cross-heap pointers.
55 *
56 * The card table's base [the "biased card table"] gets set to a
57 * rather strange value. In order to keep the JIT from having to
58 * fabricate or load GC_DIRTY_CARD to store into the card table,
59 * biased base is within the mmap allocation at a point where it's low
60 * byte is equal to GC_DIRTY_CARD. See dvmCardTableStartup for details.
61 */
62
63/*
64 * Initializes the card table; must be called before any other
65 * dvmCardTable*() functions.
66 */
67bool dvmCardTableStartup(GcHeap *gcHeap, void *heapBase)
68{
69 size_t length;
70 void *allocBase;
71 u1 *biasedBase;
72
73 /* Set up the card table */
74 length = gDvm.heapSizeMax / GC_CARD_SIZE;
75 /* Allocate an extra 256 bytes to allow fixed low-byte of base */
76 allocBase = dvmAllocRegion(length + 0x100, PROT_READ | PROT_WRITE,
77 "dalvik-card-table");
78 if (allocBase == NULL) {
79 return false;
80 }
81 gcHeap->cardTableBase = allocBase;
82 gcHeap->cardTableLength = length;
83 /* All zeros is the correct initial value; all clean. */
84 assert(GC_CARD_CLEAN == 0);
85
86 biasedBase = (u1 *)((uintptr_t)allocBase -
87 ((uintptr_t)heapBase >> GC_CARD_SHIFT));
88 if (((uintptr_t)biasedBase & 0xff) != GC_CARD_DIRTY) {
89 int offset;
90 offset = GC_CARD_DIRTY - ((uintptr_t)biasedBase & 0xff);
91 biasedBase += offset + (offset < 0 ? 0x100 : 0);
92 }
93 assert(((uintptr_t)biasedBase & 0xff) == GC_CARD_DIRTY);
Barry Hayes4496ed92010-07-12 09:52:20 -070094 gDvm.biasedCardTableBase = biasedBase;
Barry Hayes6e5cf602010-06-22 12:32:59 -070095
96 return true;
97}
98
99/*
100 * Tears down the entire CardTable.
101 */
102void dvmCardTableShutdown()
103{
104 munmap(gDvm.gcHeap->cardTableBase, gDvm.gcHeap->cardTableLength);
105}
106
107/*
Barry Hayes4496ed92010-07-12 09:52:20 -0700108 * Returns true iff the address is within the bounds of the card table.
109 */
110bool dvmIsValidCard(const u1 *cardAddr)
111{
112 GcHeap *h = gDvm.gcHeap;
113 return cardAddr >= h->cardTableBase &&
114 cardAddr < &h->cardTableBase[h->cardTableLength];
115}
116
117/*
Barry Hayes8f921a72010-07-09 12:53:49 -0700118 * Returns the address of the relevent byte in the card table, given
Barry Hayes6e5cf602010-06-22 12:32:59 -0700119 * an address on the heap.
120 */
121u1 *dvmCardFromAddr(const void *addr)
122{
Barry Hayes4496ed92010-07-12 09:52:20 -0700123 u1 *biasedBase = gDvm.biasedCardTableBase;
124 u1 *cardAddr = biasedBase + ((uintptr_t)addr >> GC_CARD_SHIFT);
125 assert(dvmIsValidCard(cardAddr));
Barry Hayes6e5cf602010-06-22 12:32:59 -0700126 return cardAddr;
127}
128
Barry Hayes8f921a72010-07-09 12:53:49 -0700129/*
130 * Returns the first address in the heap which maps to this card.
131 */
132void *dvmAddrFromCard(const u1 *cardAddr)
133{
Barry Hayes4496ed92010-07-12 09:52:20 -0700134 assert(dvmIsValidCard(cardAddr));
135 uintptr_t offset = cardAddr - gDvm.biasedCardTableBase;
Barry Hayes8f921a72010-07-09 12:53:49 -0700136 return (void *)(offset << GC_CARD_SHIFT);
Barry Hayes6e5cf602010-06-22 12:32:59 -0700137}
138
139/*
140 * Dirties the card for the given address.
141 */
142void dvmMarkCard(const void *addr)
143{
144 u1 *cardAddr = dvmCardFromAddr(addr);
145 *cardAddr = GC_CARD_DIRTY;
146}
147
148/*
149 * Returns true iff all address within the Object are on unmarked cards.
150 */
151static bool objectIsClean(const Object *obj)
152{
153 assert(dvmIsValidObject(obj));
154 size_t size = dvmHeapSourceChunkSize(obj);
155 u1 *start = dvmCardFromAddr(obj);
156 u1 *end = dvmCardFromAddr((char *)obj + size-1);
157 u1 *index;
158
159 for (index = start; index <= end; index++) {
160 if (*index != GC_CARD_CLEAN) {
161 return false;
162 }
163 }
164 return true;
165}
166
167/*
168 * A Visitor callback in support of checkCleanObjects. "arg" is
169 * expected to be the immuneLimit.
170 */
Barry Hayes8f921a72010-07-09 12:53:49 -0700171static void crossGenCheckVisitor(void *ptr, void *arg)
Barry Hayes6e5cf602010-06-22 12:32:59 -0700172{
173 Object *ref = *(Object **)ptr;
174 Object *immuneLimit = (Object *)arg;
175
176 if (ref >= immuneLimit) {
177 LOGE("Clean obj contains threatened ref %p: %p", ptr, ref);
178 dvmAbort();
179 }
180}
181
182/*
183 * A HeapBitmap callback in support of checkCleanObjects.
184 */
Barry Hayes8f921a72010-07-09 12:53:49 -0700185static bool crossGenCheckCallback(size_t numPtrs, void **ptrs,
Barry Hayes6e5cf602010-06-22 12:32:59 -0700186 const void *finger, void *arg)
187{
188 size_t i;
189 for (i = 0; i < numPtrs; i++) {
190 Object *obj = ptrs[i];
191 if (objectIsClean(obj)) {
192 dvmVisitObject(crossGenCheckVisitor, obj, arg);
193 }
194 }
195
196 return true;
197}
198
199/*
200 * dvmAbort if a clean, immune Object in the bitmap contains a pointer
201 * to a threatened Object.
202 */
203void dvmVerifyCardTable(HeapBitmap *bitmap, const char *immuneLimit)
204{
205 dvmHeapBitmapWalk(bitmap, crossGenCheckCallback, (void *)immuneLimit);
206}
207