| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame^] | 17 | #include <sys/mman.h> /* for PROT_* */ |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 18 | |
| 19 | #include "Dalvik.h" |
| 20 | #include "alloc/HeapSource.h" |
| 21 | #include "alloc/Visit.h" |
| 22 | |
| 23 | /* |
| 24 | * Maintain a card table from the the write barrier. All writes of |
| 25 | * non-NULL values to heap addresses should go through an entry in |
| 26 | * WriteBarrier, and from there to here. |
| 27 | * |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 28 | * The heap is divided into "cards" of GC_CARD_SIZE bytes, as |
| 29 | * determined by GC_CARD_SHIFT. The card table contains one byte of |
| 30 | * data per card, to be used by the GC. The value of the byte will be |
| 31 | * one of GC_CARD_CLEAN or GC_CARD_DIRTY. |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 32 | * |
| 33 | * After any store of a non-NULL object pointer into a heap object, |
| 34 | * code is obliged to mark the card dirty. The setters in |
| 35 | * ObjectInlines.h [such as dvmSetFieldObject] do this for you. The |
| 36 | * JIT and fast interpreters also contain code to mark cards as dirty. |
| 37 | * |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 38 | * The card table's base [the "biased card table"] gets set to a |
| 39 | * rather strange value. In order to keep the JIT from having to |
| 40 | * fabricate or load GC_DIRTY_CARD to store into the card table, |
| 41 | * biased base is within the mmap allocation at a point where it's low |
| 42 | * byte is equal to GC_DIRTY_CARD. See dvmCardTableStartup for details. |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | * Initializes the card table; must be called before any other |
| 47 | * dvmCardTable*() functions. |
| 48 | */ |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 49 | bool dvmCardTableStartup(void) |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 50 | { |
| 51 | size_t length; |
| 52 | void *allocBase; |
| 53 | u1 *biasedBase; |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 54 | GcHeap *gcHeap = gDvm.gcHeap; |
| 55 | void *heapBase = dvmHeapSourceGetBase(); |
| 56 | assert(gcHeap != NULL); |
| 57 | assert(heapBase != NULL); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 58 | |
| 59 | /* Set up the card table */ |
| 60 | length = gDvm.heapSizeMax / GC_CARD_SIZE; |
| 61 | /* Allocate an extra 256 bytes to allow fixed low-byte of base */ |
| 62 | allocBase = dvmAllocRegion(length + 0x100, PROT_READ | PROT_WRITE, |
| 63 | "dalvik-card-table"); |
| 64 | if (allocBase == NULL) { |
| 65 | return false; |
| 66 | } |
| 67 | gcHeap->cardTableBase = allocBase; |
| 68 | gcHeap->cardTableLength = length; |
| 69 | /* All zeros is the correct initial value; all clean. */ |
| 70 | assert(GC_CARD_CLEAN == 0); |
| 71 | |
| 72 | biasedBase = (u1 *)((uintptr_t)allocBase - |
| 73 | ((uintptr_t)heapBase >> GC_CARD_SHIFT)); |
| 74 | if (((uintptr_t)biasedBase & 0xff) != GC_CARD_DIRTY) { |
| 75 | int offset; |
| 76 | offset = GC_CARD_DIRTY - ((uintptr_t)biasedBase & 0xff); |
| 77 | biasedBase += offset + (offset < 0 ? 0x100 : 0); |
| 78 | } |
| 79 | assert(((uintptr_t)biasedBase & 0xff) == GC_CARD_DIRTY); |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 80 | gDvm.biasedCardTableBase = biasedBase; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Tears down the entire CardTable. |
| 87 | */ |
| 88 | void dvmCardTableShutdown() |
| 89 | { |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 90 | gDvm.biasedCardTableBase = NULL; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 91 | munmap(gDvm.gcHeap->cardTableBase, gDvm.gcHeap->cardTableLength); |
| 92 | } |
| 93 | |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 94 | void dvmClearCardTable(void) |
| 95 | { |
| 96 | assert(gDvm.gcHeap->cardTableBase != NULL); |
| 97 | memset(gDvm.gcHeap->cardTableBase, GC_CARD_CLEAN, gDvm.gcHeap->cardTableLength); |
| 98 | } |
| 99 | |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 100 | /* |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 101 | * Returns true iff the address is within the bounds of the card table. |
| 102 | */ |
| 103 | bool dvmIsValidCard(const u1 *cardAddr) |
| 104 | { |
| 105 | GcHeap *h = gDvm.gcHeap; |
| 106 | return cardAddr >= h->cardTableBase && |
| 107 | cardAddr < &h->cardTableBase[h->cardTableLength]; |
| 108 | } |
| 109 | |
| 110 | /* |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 111 | * Returns the address of the relevent byte in the card table, given |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 112 | * an address on the heap. |
| 113 | */ |
| 114 | u1 *dvmCardFromAddr(const void *addr) |
| 115 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 116 | u1 *biasedBase = gDvm.biasedCardTableBase; |
| 117 | u1 *cardAddr = biasedBase + ((uintptr_t)addr >> GC_CARD_SHIFT); |
| 118 | assert(dvmIsValidCard(cardAddr)); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 119 | return cardAddr; |
| 120 | } |
| 121 | |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Returns the first address in the heap which maps to this card. |
| 124 | */ |
| 125 | void *dvmAddrFromCard(const u1 *cardAddr) |
| 126 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 127 | assert(dvmIsValidCard(cardAddr)); |
| 128 | uintptr_t offset = cardAddr - gDvm.biasedCardTableBase; |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 129 | return (void *)(offset << GC_CARD_SHIFT); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Dirties the card for the given address. |
| 134 | */ |
| 135 | void dvmMarkCard(const void *addr) |
| 136 | { |
| 137 | u1 *cardAddr = dvmCardFromAddr(addr); |
| 138 | *cardAddr = GC_CARD_DIRTY; |
| 139 | } |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame^] | 140 | |
| 141 | /* |
| 142 | * Handles the complexity of object arrays for isObjectDirty. Array |
| 143 | * objects are exactly marked so all spanned cards are examined. |
| 144 | */ |
| 145 | static bool isObjectArrayDirty(const Object *obj) |
| 146 | { |
| 147 | u1 *ptr, *limit; |
| 148 | size_t size; |
| 149 | |
| 150 | assert(obj != NULL); |
| 151 | assert(dvmIsValidObject(obj)); |
| 152 | assert(IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISOBJECTARRAY)); |
| 153 | size = dvmArrayObjectSize((const ArrayObject *)obj); |
| 154 | ptr = dvmCardFromAddr(obj); |
| 155 | limit = dvmCardFromAddr((u1 *)obj + size - 1) + 1; |
| 156 | assert(ptr != limit); |
| 157 | for (; ptr != limit; ++ptr) { |
| 158 | if (*ptr == GC_CARD_DIRTY) { |
| 159 | return true; |
| 160 | } |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Returns true if the object is on a dirty card. |
| 167 | */ |
| 168 | static bool isObjectDirty(const Object *obj) |
| 169 | { |
| 170 | assert(obj != NULL); |
| 171 | assert(dvmIsValidObject(obj)); |
| 172 | if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISOBJECTARRAY)) { |
| 173 | return isObjectArrayDirty(obj); |
| 174 | } else { |
| 175 | u1 *card = dvmCardFromAddr(obj); |
| 176 | return *card == GC_CARD_DIRTY; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Context structure for verifying the card table. |
| 182 | */ |
| 183 | typedef struct { |
| 184 | HeapBitmap *markBits; |
| 185 | size_t whiteRefs; |
| 186 | } WhiteReferenceCounter; |
| 187 | |
| 188 | /* |
| 189 | * Visitor that counts white referents. |
| 190 | */ |
| 191 | static void countWhiteReferenceVisitor(void *addr, void *arg) |
| 192 | { |
| 193 | WhiteReferenceCounter *ctx; |
| 194 | Object *obj; |
| 195 | |
| 196 | assert(addr != NULL); |
| 197 | assert(arg != NULL); |
| 198 | obj = *(Object **)addr; |
| 199 | assert(dvmIsValidObject(obj)); |
| 200 | ctx = arg; |
| 201 | if (obj == NULL || dvmHeapBitmapIsObjectBitSet(ctx->markBits, obj)) { |
| 202 | return; |
| 203 | } |
| 204 | ctx->whiteRefs += 1; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Returns true if the given object is a reference object and the |
| 209 | * just the referent is unmarked. |
| 210 | */ |
| 211 | static bool isReferentUnmarked(const Object *obj, |
| 212 | const WhiteReferenceCounter* ctx) |
| 213 | { |
| 214 | assert(obj != NULL); |
| 215 | assert(obj->clazz != NULL); |
| 216 | assert(ctx != NULL); |
| 217 | if (ctx->whiteRefs != 1) { |
| 218 | return false; |
| 219 | } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)) { |
| 220 | size_t offset = gDvm.offJavaLangRefReference_referent; |
| 221 | const Object *referent = dvmGetFieldObject(obj, offset); |
| 222 | return !dvmHeapBitmapIsObjectBitSet(ctx->markBits, referent); |
| 223 | } else { |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Returns true if the given object is a string and has been interned |
| 230 | * by the user. |
| 231 | */ |
| 232 | static bool isWeakInternedString(const Object *obj) |
| 233 | { |
| 234 | assert(obj != NULL); |
| 235 | if (obj->clazz == gDvm.classJavaLangString) { |
| 236 | return dvmIsWeakInternedString((StringObject *)obj); |
| 237 | } else { |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Callback applied to marked objects. If the object is found to be |
| 244 | * gray a message is written to the log. By virtue of where the card |
| 245 | * table verification occurs weak references have yet to be blackened |
| 246 | * and so their containing objects are permitted to be gray. |
| 247 | */ |
| 248 | static void verifyCardTableCallback(size_t numPtrs, void **ptrs, |
| 249 | const void *finger, void *arg) |
| 250 | { |
| 251 | size_t i; |
| 252 | |
| 253 | for (i = 0; i < numPtrs; ++i) { |
| 254 | Object *obj = ptrs[i]; |
| 255 | WhiteReferenceCounter ctx = { arg, 0 }; |
| 256 | dvmVisitObject(countWhiteReferenceVisitor, obj, &ctx); |
| 257 | if (ctx.whiteRefs == 0) { |
| 258 | continue; |
| 259 | } else if (isObjectDirty(obj)) { |
| 260 | continue; |
| 261 | } else if (isReferentUnmarked(obj, &ctx)) { |
| 262 | continue; |
| 263 | } else if (isWeakInternedString(obj)) { |
| 264 | continue; |
| 265 | } |
| 266 | LOGE("Verify failed, object %p is gray", obj); |
| 267 | dvmDumpObject(obj); |
| 268 | dvmAbort(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Verifies that gray objects are on a dirty card. |
| 274 | */ |
| 275 | void dvmVerifyCardTable(void) |
| 276 | { |
| 277 | HeapBitmap *markBits = gDvm.gcHeap->markContext.bitmap; |
| 278 | dvmHeapBitmapWalk(markBits, verifyCardTableCallback, markBits); |
| 279 | } |