blob: 49562a7f0b66cd55da6aac1b493c8b7b5533529c [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);
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/*
Barry Hayes8f921a72010-07-09 12:53:49 -0700108 * Returns the address of the relevent byte in the card table, given
Barry Hayes6e5cf602010-06-22 12:32:59 -0700109 * an address on the heap.
110 */
111u1 *dvmCardFromAddr(const void *addr)
112{
Barry Hayes8f921a72010-07-09 12:53:49 -0700113 GcHeap *h = gDvm.gcHeap;
114 u1 *cardAddr = h->biasedCardTableBase + ((uintptr_t)addr >> GC_CARD_SHIFT);
115 assert(cardAddr >= h->cardTableBase);
116 assert(cardAddr < &h->cardTableBase[h->cardTableLength]);
Barry Hayes6e5cf602010-06-22 12:32:59 -0700117 return cardAddr;
118}
119
Barry Hayes8f921a72010-07-09 12:53:49 -0700120/*
121 * Returns the first address in the heap which maps to this card.
122 */
123void *dvmAddrFromCard(const u1 *cardAddr)
124{
125 GcHeap *h = gDvm.gcHeap;
126 assert(cardAddr >= h->cardTableBase);
127 assert(cardAddr < &h->cardTableBase[h->cardTableLength]);
128 uintptr_t offset = cardAddr - h->biasedCardTableBase;
129 return (void *)(offset << GC_CARD_SHIFT);
Barry Hayes6e5cf602010-06-22 12:32:59 -0700130}
131
132/*
133 * Dirties the card for the given address.
134 */
135void dvmMarkCard(const void *addr)
136{
137 u1 *cardAddr = dvmCardFromAddr(addr);
138 *cardAddr = GC_CARD_DIRTY;
139}
140
141/*
142 * Returns true iff all address within the Object are on unmarked cards.
143 */
144static bool objectIsClean(const Object *obj)
145{
146 assert(dvmIsValidObject(obj));
147 size_t size = dvmHeapSourceChunkSize(obj);
148 u1 *start = dvmCardFromAddr(obj);
149 u1 *end = dvmCardFromAddr((char *)obj + size-1);
150 u1 *index;
151
152 for (index = start; index <= end; index++) {
153 if (*index != GC_CARD_CLEAN) {
154 return false;
155 }
156 }
157 return true;
158}
159
160/*
161 * A Visitor callback in support of checkCleanObjects. "arg" is
162 * expected to be the immuneLimit.
163 */
Barry Hayes8f921a72010-07-09 12:53:49 -0700164static void crossGenCheckVisitor(void *ptr, void *arg)
Barry Hayes6e5cf602010-06-22 12:32:59 -0700165{
166 Object *ref = *(Object **)ptr;
167 Object *immuneLimit = (Object *)arg;
168
169 if (ref >= immuneLimit) {
170 LOGE("Clean obj contains threatened ref %p: %p", ptr, ref);
171 dvmAbort();
172 }
173}
174
175/*
176 * A HeapBitmap callback in support of checkCleanObjects.
177 */
Barry Hayes8f921a72010-07-09 12:53:49 -0700178static bool crossGenCheckCallback(size_t numPtrs, void **ptrs,
Barry Hayes6e5cf602010-06-22 12:32:59 -0700179 const void *finger, void *arg)
180{
181 size_t i;
182 for (i = 0; i < numPtrs; i++) {
183 Object *obj = ptrs[i];
184 if (objectIsClean(obj)) {
185 dvmVisitObject(crossGenCheckVisitor, obj, arg);
186 }
187 }
188
189 return true;
190}
191
192/*
193 * dvmAbort if a clean, immune Object in the bitmap contains a pointer
194 * to a threatened Object.
195 */
196void dvmVerifyCardTable(HeapBitmap *bitmap, const char *immuneLimit)
197{
198 dvmHeapBitmapWalk(bitmap, crossGenCheckCallback, (void *)immuneLimit);
199}
200