blob: bd8de9aa7acdf3eeedddd9dbca9ebdcf6bdc7f4d [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 *
Barry Hayes6e5cf602010-06-22 12:32:59 -070041 * 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 Hayesb874ab92010-07-14 08:13:18 -070052bool dvmCardTableStartup(void)
Barry Hayes6e5cf602010-06-22 12:32:59 -070053{
54 size_t length;
55 void *allocBase;
56 u1 *biasedBase;
Barry Hayesb874ab92010-07-14 08:13:18 -070057 GcHeap *gcHeap = gDvm.gcHeap;
58 void *heapBase = dvmHeapSourceGetBase();
59 assert(gcHeap != NULL);
60 assert(heapBase != NULL);
Barry Hayes6e5cf602010-06-22 12:32:59 -070061
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 Hayes4496ed92010-07-12 09:52:20 -070083 gDvm.biasedCardTableBase = biasedBase;
Barry Hayes6e5cf602010-06-22 12:32:59 -070084
85 return true;
86}
87
88/*
89 * Tears down the entire CardTable.
90 */
91void dvmCardTableShutdown()
92{
Barry Hayesb874ab92010-07-14 08:13:18 -070093 gDvm.biasedCardTableBase = NULL;
Barry Hayes6e5cf602010-06-22 12:32:59 -070094 munmap(gDvm.gcHeap->cardTableBase, gDvm.gcHeap->cardTableLength);
95}
96
Carl Shapiro106c5fd2010-07-28 14:12:27 -070097void dvmClearCardTable(void)
98{
99 assert(gDvm.gcHeap->cardTableBase != NULL);
100 memset(gDvm.gcHeap->cardTableBase, GC_CARD_CLEAN, gDvm.gcHeap->cardTableLength);
101}
102
Barry Hayes6e5cf602010-06-22 12:32:59 -0700103/*
Barry Hayes4496ed92010-07-12 09:52:20 -0700104 * Returns true iff the address is within the bounds of the card table.
105 */
106bool 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 Hayes8f921a72010-07-09 12:53:49 -0700114 * Returns the address of the relevent byte in the card table, given
Barry Hayes6e5cf602010-06-22 12:32:59 -0700115 * an address on the heap.
116 */
117u1 *dvmCardFromAddr(const void *addr)
118{
Barry Hayes4496ed92010-07-12 09:52:20 -0700119 u1 *biasedBase = gDvm.biasedCardTableBase;
120 u1 *cardAddr = biasedBase + ((uintptr_t)addr >> GC_CARD_SHIFT);
121 assert(dvmIsValidCard(cardAddr));
Barry Hayes6e5cf602010-06-22 12:32:59 -0700122 return cardAddr;
123}
124
Barry Hayes8f921a72010-07-09 12:53:49 -0700125/*
126 * Returns the first address in the heap which maps to this card.
127 */
128void *dvmAddrFromCard(const u1 *cardAddr)
129{
Barry Hayes4496ed92010-07-12 09:52:20 -0700130 assert(dvmIsValidCard(cardAddr));
131 uintptr_t offset = cardAddr - gDvm.biasedCardTableBase;
Barry Hayes8f921a72010-07-09 12:53:49 -0700132 return (void *)(offset << GC_CARD_SHIFT);
Barry Hayes6e5cf602010-06-22 12:32:59 -0700133}
134
135/*
136 * Dirties the card for the given address.
137 */
138void dvmMarkCard(const void *addr)
139{
140 u1 *cardAddr = dvmCardFromAddr(addr);
141 *cardAddr = GC_CARD_DIRTY;
142}