| 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) { |
| Carl Shapiro | 5d81aa3 | 2010-08-20 11:04:25 -0700 | [diff] [blame] | 75 | int offset = GC_CARD_DIRTY - ((uintptr_t)biasedBase & 0xff); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 76 | biasedBase += offset + (offset < 0 ? 0x100 : 0); |
| 77 | } |
| 78 | assert(((uintptr_t)biasedBase & 0xff) == GC_CARD_DIRTY); |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 79 | gDvm.biasedCardTableBase = biasedBase; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Tears down the entire CardTable. |
| 86 | */ |
| 87 | void dvmCardTableShutdown() |
| 88 | { |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 89 | gDvm.biasedCardTableBase = NULL; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 90 | munmap(gDvm.gcHeap->cardTableBase, gDvm.gcHeap->cardTableLength); |
| 91 | } |
| 92 | |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 93 | void dvmClearCardTable(void) |
| 94 | { |
| 95 | assert(gDvm.gcHeap->cardTableBase != NULL); |
| 96 | memset(gDvm.gcHeap->cardTableBase, GC_CARD_CLEAN, gDvm.gcHeap->cardTableLength); |
| 97 | } |
| 98 | |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 99 | /* |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 100 | * Returns true iff the address is within the bounds of the card table. |
| 101 | */ |
| 102 | bool dvmIsValidCard(const u1 *cardAddr) |
| 103 | { |
| 104 | GcHeap *h = gDvm.gcHeap; |
| 105 | return cardAddr >= h->cardTableBase && |
| 106 | cardAddr < &h->cardTableBase[h->cardTableLength]; |
| 107 | } |
| 108 | |
| 109 | /* |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 110 | * Returns the address of the relevent byte in the card table, given |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 111 | * an address on the heap. |
| 112 | */ |
| 113 | u1 *dvmCardFromAddr(const void *addr) |
| 114 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 115 | u1 *biasedBase = gDvm.biasedCardTableBase; |
| 116 | u1 *cardAddr = biasedBase + ((uintptr_t)addr >> GC_CARD_SHIFT); |
| 117 | assert(dvmIsValidCard(cardAddr)); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 118 | return cardAddr; |
| 119 | } |
| 120 | |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 121 | /* |
| 122 | * Returns the first address in the heap which maps to this card. |
| 123 | */ |
| 124 | void *dvmAddrFromCard(const u1 *cardAddr) |
| 125 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 126 | assert(dvmIsValidCard(cardAddr)); |
| 127 | uintptr_t offset = cardAddr - gDvm.biasedCardTableBase; |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 128 | return (void *)(offset << GC_CARD_SHIFT); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Dirties the card for the given address. |
| 133 | */ |
| 134 | void dvmMarkCard(const void *addr) |
| 135 | { |
| 136 | u1 *cardAddr = dvmCardFromAddr(addr); |
| 137 | *cardAddr = GC_CARD_DIRTY; |
| 138 | } |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Handles the complexity of object arrays for isObjectDirty. Array |
| 142 | * objects are exactly marked so all spanned cards are examined. |
| 143 | */ |
| 144 | static bool isObjectArrayDirty(const Object *obj) |
| 145 | { |
| 146 | u1 *ptr, *limit; |
| 147 | size_t size; |
| 148 | |
| 149 | assert(obj != NULL); |
| 150 | assert(dvmIsValidObject(obj)); |
| 151 | assert(IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISOBJECTARRAY)); |
| 152 | size = dvmArrayObjectSize((const ArrayObject *)obj); |
| 153 | ptr = dvmCardFromAddr(obj); |
| 154 | limit = dvmCardFromAddr((u1 *)obj + size - 1) + 1; |
| 155 | assert(ptr != limit); |
| 156 | for (; ptr != limit; ++ptr) { |
| 157 | if (*ptr == GC_CARD_DIRTY) { |
| 158 | return true; |
| 159 | } |
| 160 | } |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Returns true if the object is on a dirty card. |
| 166 | */ |
| 167 | static bool isObjectDirty(const Object *obj) |
| 168 | { |
| 169 | assert(obj != NULL); |
| 170 | assert(dvmIsValidObject(obj)); |
| 171 | if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISOBJECTARRAY)) { |
| 172 | return isObjectArrayDirty(obj); |
| 173 | } else { |
| 174 | u1 *card = dvmCardFromAddr(obj); |
| 175 | return *card == GC_CARD_DIRTY; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Context structure for verifying the card table. |
| 181 | */ |
| 182 | typedef struct { |
| 183 | HeapBitmap *markBits; |
| 184 | size_t whiteRefs; |
| 185 | } WhiteReferenceCounter; |
| 186 | |
| 187 | /* |
| 188 | * Visitor that counts white referents. |
| 189 | */ |
| 190 | static void countWhiteReferenceVisitor(void *addr, void *arg) |
| 191 | { |
| 192 | WhiteReferenceCounter *ctx; |
| 193 | Object *obj; |
| 194 | |
| 195 | assert(addr != NULL); |
| 196 | assert(arg != NULL); |
| 197 | obj = *(Object **)addr; |
| 198 | assert(dvmIsValidObject(obj)); |
| 199 | ctx = arg; |
| 200 | if (obj == NULL || dvmHeapBitmapIsObjectBitSet(ctx->markBits, obj)) { |
| 201 | return; |
| 202 | } |
| 203 | ctx->whiteRefs += 1; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Returns true if the given object is a reference object and the |
| 208 | * just the referent is unmarked. |
| 209 | */ |
| 210 | static bool isReferentUnmarked(const Object *obj, |
| 211 | const WhiteReferenceCounter* ctx) |
| 212 | { |
| 213 | assert(obj != NULL); |
| 214 | assert(obj->clazz != NULL); |
| 215 | assert(ctx != NULL); |
| 216 | if (ctx->whiteRefs != 1) { |
| 217 | return false; |
| 218 | } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)) { |
| 219 | size_t offset = gDvm.offJavaLangRefReference_referent; |
| 220 | const Object *referent = dvmGetFieldObject(obj, offset); |
| 221 | return !dvmHeapBitmapIsObjectBitSet(ctx->markBits, referent); |
| 222 | } else { |
| 223 | return false; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Returns true if the given object is a string and has been interned |
| 229 | * by the user. |
| 230 | */ |
| 231 | static bool isWeakInternedString(const Object *obj) |
| 232 | { |
| 233 | assert(obj != NULL); |
| 234 | if (obj->clazz == gDvm.classJavaLangString) { |
| 235 | return dvmIsWeakInternedString((StringObject *)obj); |
| 236 | } else { |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Callback applied to marked objects. If the object is found to be |
| 243 | * gray a message is written to the log. By virtue of where the card |
| 244 | * table verification occurs weak references have yet to be blackened |
| 245 | * and so their containing objects are permitted to be gray. |
| 246 | */ |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame^] | 247 | static void verifyCardTableCallback(void *ptr, void *arg) |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 248 | { |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame^] | 249 | Object *obj = ptr; |
| 250 | WhiteReferenceCounter ctx = { arg, 0 }; |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 251 | |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame^] | 252 | dvmVisitObject(countWhiteReferenceVisitor, obj, &ctx); |
| 253 | if (ctx.whiteRefs == 0) { |
| 254 | return; |
| 255 | } else if (isObjectDirty(obj)) { |
| 256 | return; |
| 257 | } else if (isReferentUnmarked(obj, &ctx)) { |
| 258 | return; |
| 259 | } else if (isWeakInternedString(obj)) { |
| 260 | return; |
| 261 | } else { |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 262 | LOGE("Verify failed, object %p is gray", obj); |
| 263 | dvmDumpObject(obj); |
| 264 | dvmAbort(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Verifies that gray objects are on a dirty card. |
| 270 | */ |
| 271 | void dvmVerifyCardTable(void) |
| 272 | { |
| 273 | HeapBitmap *markBits = gDvm.gcHeap->markContext.bitmap; |
| 274 | dvmHeapBitmapWalk(markBits, verifyCardTableCallback, markBits); |
| 275 | } |