blob: 9455955661e01ef83f005f93606c014d8590fe36 [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 *
31 * The heap is divided into "cards" of 512 bytes, as determined by
32 * GC_CARD_SHIFT. The card table contains one byte of data per card,
33 * to be used by the GC. The value of the byte will be one of
34 * GC_CARD_CLEAN or GC_CARD_DIRTY.
35 *
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);
94 gcHeap->biasedCardTableBase = biasedBase;
95
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/*
108 * Returns The address of the relevent byte in the card table, given
109 * an address on the heap.
110 */
111u1 *dvmCardFromAddr(const void *addr)
112{
113 u1 *cardAddr = gDvm.gcHeap->biasedCardTableBase +
114 ((uintptr_t)addr >> GC_CARD_SHIFT);
115 assert(cardAddr >= gDvm.gcHeap->cardTableBase);
116 assert(cardAddr <
117 &gDvm.gcHeap->cardTableBase[gDvm.gcHeap->cardTableLength]);
118 return cardAddr;
119}
120
121void *dvmAddrFromCard(const u1 *cardAddr) {
122 assert(cardAddr >= gDvm.gcHeap->cardTableBase);
123 assert(cardAddr <
124 &gDvm.gcHeap->cardTableBase[gDvm.gcHeap->cardTableLength]);
125 void *addr = (void *)((cardAddr - gDvm.gcHeap->biasedCardTableBase) << GC_CARD_SHIFT);
126 return addr;
127}
128
129/*
130 * Dirties the card for the given address.
131 */
132void dvmMarkCard(const void *addr)
133{
134 u1 *cardAddr = dvmCardFromAddr(addr);
135 *cardAddr = GC_CARD_DIRTY;
136}
137
138/*
139 * Returns true iff all address within the Object are on unmarked cards.
140 */
141static bool objectIsClean(const Object *obj)
142{
143 assert(dvmIsValidObject(obj));
144 size_t size = dvmHeapSourceChunkSize(obj);
145 u1 *start = dvmCardFromAddr(obj);
146 u1 *end = dvmCardFromAddr((char *)obj + size-1);
147 u1 *index;
148
149 for (index = start; index <= end; index++) {
150 if (*index != GC_CARD_CLEAN) {
151 return false;
152 }
153 }
154 return true;
155}
156
157/*
158 * A Visitor callback in support of checkCleanObjects. "arg" is
159 * expected to be the immuneLimit.
160 */
161static void
162crossGenCheckVisitor(void *ptr, void *arg)
163{
164 Object *ref = *(Object **)ptr;
165 Object *immuneLimit = (Object *)arg;
166
167 if (ref >= immuneLimit) {
168 LOGE("Clean obj contains threatened ref %p: %p", ptr, ref);
169 dvmAbort();
170 }
171}
172
173/*
174 * A HeapBitmap callback in support of checkCleanObjects.
175 */
176static bool
177crossGenCheckCallback(size_t numPtrs, void **ptrs,
178 const void *finger, void *arg)
179{
180 size_t i;
181 for (i = 0; i < numPtrs; i++) {
182 Object *obj = ptrs[i];
183 if (objectIsClean(obj)) {
184 dvmVisitObject(crossGenCheckVisitor, obj, arg);
185 }
186 }
187
188 return true;
189}
190
191/*
192 * dvmAbort if a clean, immune Object in the bitmap contains a pointer
193 * to a threatened Object.
194 */
195void dvmVerifyCardTable(HeapBitmap *bitmap, const char *immuneLimit)
196{
197 dvmHeapBitmapWalk(bitmap, crossGenCheckCallback, (void *)immuneLimit);
198}
199