| 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 | |
| 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 Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 31 | * 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 Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 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 | * |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 41 | * The card table's base [the "biased card table"] gets set to a |
| 42 | * rather strange value. In order to keep the JIT from having to |
| 43 | * fabricate or load GC_DIRTY_CARD to store into the card table, |
| 44 | * biased base is within the mmap allocation at a point where it's low |
| 45 | * byte is equal to GC_DIRTY_CARD. See dvmCardTableStartup for details. |
| 46 | */ |
| 47 | |
| 48 | /* |
| 49 | * Initializes the card table; must be called before any other |
| 50 | * dvmCardTable*() functions. |
| 51 | */ |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 52 | bool dvmCardTableStartup(void) |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 53 | { |
| 54 | size_t length; |
| 55 | void *allocBase; |
| 56 | u1 *biasedBase; |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 57 | GcHeap *gcHeap = gDvm.gcHeap; |
| 58 | void *heapBase = dvmHeapSourceGetBase(); |
| 59 | assert(gcHeap != NULL); |
| 60 | assert(heapBase != NULL); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 61 | |
| 62 | /* Set up the card table */ |
| 63 | length = gDvm.heapSizeMax / GC_CARD_SIZE; |
| 64 | /* Allocate an extra 256 bytes to allow fixed low-byte of base */ |
| 65 | allocBase = dvmAllocRegion(length + 0x100, PROT_READ | PROT_WRITE, |
| 66 | "dalvik-card-table"); |
| 67 | if (allocBase == NULL) { |
| 68 | return false; |
| 69 | } |
| 70 | gcHeap->cardTableBase = allocBase; |
| 71 | gcHeap->cardTableLength = length; |
| 72 | /* All zeros is the correct initial value; all clean. */ |
| 73 | assert(GC_CARD_CLEAN == 0); |
| 74 | |
| 75 | biasedBase = (u1 *)((uintptr_t)allocBase - |
| 76 | ((uintptr_t)heapBase >> GC_CARD_SHIFT)); |
| 77 | if (((uintptr_t)biasedBase & 0xff) != GC_CARD_DIRTY) { |
| 78 | int offset; |
| 79 | offset = GC_CARD_DIRTY - ((uintptr_t)biasedBase & 0xff); |
| 80 | biasedBase += offset + (offset < 0 ? 0x100 : 0); |
| 81 | } |
| 82 | assert(((uintptr_t)biasedBase & 0xff) == GC_CARD_DIRTY); |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 83 | gDvm.biasedCardTableBase = biasedBase; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Tears down the entire CardTable. |
| 90 | */ |
| 91 | void dvmCardTableShutdown() |
| 92 | { |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 93 | gDvm.biasedCardTableBase = NULL; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 94 | munmap(gDvm.gcHeap->cardTableBase, gDvm.gcHeap->cardTableLength); |
| 95 | } |
| 96 | |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame^] | 97 | void dvmClearCardTable(void) |
| 98 | { |
| 99 | assert(gDvm.gcHeap->cardTableBase != NULL); |
| 100 | memset(gDvm.gcHeap->cardTableBase, GC_CARD_CLEAN, gDvm.gcHeap->cardTableLength); |
| 101 | } |
| 102 | |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 103 | /* |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 104 | * Returns true iff the address is within the bounds of the card table. |
| 105 | */ |
| 106 | bool dvmIsValidCard(const u1 *cardAddr) |
| 107 | { |
| 108 | GcHeap *h = gDvm.gcHeap; |
| 109 | return cardAddr >= h->cardTableBase && |
| 110 | cardAddr < &h->cardTableBase[h->cardTableLength]; |
| 111 | } |
| 112 | |
| 113 | /* |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 114 | * Returns the address of the relevent byte in the card table, given |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 115 | * an address on the heap. |
| 116 | */ |
| 117 | u1 *dvmCardFromAddr(const void *addr) |
| 118 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 119 | u1 *biasedBase = gDvm.biasedCardTableBase; |
| 120 | u1 *cardAddr = biasedBase + ((uintptr_t)addr >> GC_CARD_SHIFT); |
| 121 | assert(dvmIsValidCard(cardAddr)); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 122 | return cardAddr; |
| 123 | } |
| 124 | |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 125 | /* |
| 126 | * Returns the first address in the heap which maps to this card. |
| 127 | */ |
| 128 | void *dvmAddrFromCard(const u1 *cardAddr) |
| 129 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 130 | assert(dvmIsValidCard(cardAddr)); |
| 131 | uintptr_t offset = cardAddr - gDvm.biasedCardTableBase; |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 132 | return (void *)(offset << GC_CARD_SHIFT); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Dirties the card for the given address. |
| 137 | */ |
| 138 | void dvmMarkCard(const void *addr) |
| 139 | { |
| 140 | u1 *cardAddr = dvmCardFromAddr(addr); |
| 141 | *cardAddr = GC_CARD_DIRTY; |
| 142 | } |