Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -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 | #include "card_table.h" |
| 18 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 20 | #include "card_table-inl.h" |
| 21 | #include "gc/heap.h" |
| 22 | #include "gc/space/space.h" |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 23 | #include "heap_bitmap.h" |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 24 | #include "runtime.h" |
Elliott Hughes | f0605a0 | 2012-01-13 20:12:02 -0800 | [diff] [blame] | 25 | #include "utils.h" |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 28 | namespace gc { |
| 29 | namespace accounting { |
| 30 | |
Mathieu Chartier | 2c26501 | 2014-08-05 18:15:56 -0700 | [diff] [blame] | 31 | constexpr size_t CardTable::kCardShift; |
| 32 | constexpr size_t CardTable::kCardSize; |
| 33 | constexpr uint8_t CardTable::kCardClean; |
| 34 | constexpr uint8_t CardTable::kCardDirty; |
| 35 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Maintain a card table from the write barrier. All writes of |
| 38 | * non-NULL values to heap addresses should go through an entry in |
| 39 | * WriteBarrier, and from there to here. |
| 40 | * |
| 41 | * The heap is divided into "cards" of GC_CARD_SIZE bytes, as |
| 42 | * determined by GC_CARD_SHIFT. The card table contains one byte of |
| 43 | * data per card, to be used by the GC. The value of the byte will be |
| 44 | * one of GC_CARD_CLEAN or GC_CARD_DIRTY. |
| 45 | * |
| 46 | * After any store of a non-NULL object pointer into a heap object, |
| 47 | * code is obliged to mark the card dirty. The setters in |
| 48 | * object.h [such as SetFieldObject] do this for you. The |
| 49 | * compiler also contains code to mark cards as dirty. |
| 50 | * |
| 51 | * The card table's base [the "biased card table"] gets set to a |
| 52 | * rather strange value. In order to keep the JIT from having to |
| 53 | * fabricate or load GC_DIRTY_CARD to store into the card table, |
| 54 | * biased base is within the mmap allocation at a point where its low |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 55 | * byte is equal to GC_DIRTY_CARD. See CardTable::Create for details. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 56 | */ |
| 57 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 58 | CardTable* CardTable::Create(const uint8_t* heap_begin, size_t heap_capacity) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 59 | /* Set up the card table */ |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 60 | size_t capacity = heap_capacity / kCardSize; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 61 | /* Allocate an extra 256 bytes to allow fixed low-byte of base */ |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 62 | std::string error_msg; |
Mathieu Chartier | 2c26501 | 2014-08-05 18:15:56 -0700 | [diff] [blame] | 63 | std::unique_ptr<MemMap> mem_map( |
| 64 | MemMap::MapAnonymous("card table", nullptr, capacity + 256, PROT_READ | PROT_WRITE, |
| 65 | false, &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 66 | CHECK(mem_map.get() != NULL) << "couldn't allocate card table: " << error_msg; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 67 | // All zeros is the correct initial value; all clean. Anonymous mmaps are initialized to zero, we |
| 68 | // don't clear the card table to avoid unnecessary pages being allocated |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 69 | static_assert(kCardClean == 0, "kCardClean must be 0"); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 70 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 71 | uint8_t* cardtable_begin = mem_map->Begin(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 72 | CHECK(cardtable_begin != NULL); |
| 73 | |
| 74 | // We allocated up to a bytes worth of extra space to allow biased_begin's byte value to equal |
Mathieu Chartier | 2c26501 | 2014-08-05 18:15:56 -0700 | [diff] [blame] | 75 | // kCardDirty, compute a offset value to make this the case |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 76 | size_t offset = 0; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 77 | uint8_t* biased_begin = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(cardtable_begin) - |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 78 | (reinterpret_cast<uintptr_t>(heap_begin) >> kCardShift)); |
Mathieu Chartier | 2c26501 | 2014-08-05 18:15:56 -0700 | [diff] [blame] | 79 | uintptr_t biased_byte = reinterpret_cast<uintptr_t>(biased_begin) & 0xff; |
| 80 | if (biased_byte != kCardDirty) { |
| 81 | int delta = kCardDirty - biased_byte; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 82 | offset = delta + (delta < 0 ? 0x100 : 0); |
| 83 | biased_begin += offset; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 84 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 85 | CHECK_EQ(reinterpret_cast<uintptr_t>(biased_begin) & 0xff, kCardDirty); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 86 | return new CardTable(mem_map.release(), biased_begin, offset); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 89 | CardTable::CardTable(MemMap* mem_map, uint8_t* biased_begin, size_t offset) |
Elliott Hughes | a168c83 | 2012-06-12 15:34:20 -0700 | [diff] [blame] | 90 | : mem_map_(mem_map), biased_begin_(biased_begin), offset_(offset) { |
Elliott Hughes | a168c83 | 2012-06-12 15:34:20 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 93 | void CardTable::ClearSpaceCards(space::ContinuousSpace* space) { |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 94 | // TODO: clear just the range of the table that has been modified |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 95 | uint8_t* card_start = CardFromAddr(space->Begin()); |
| 96 | uint8_t* card_end = CardFromAddr(space->End()); // Make sure to round up. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 97 | memset(reinterpret_cast<void*>(card_start), kCardClean, card_end - card_start); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 100 | void CardTable::ClearCardTable() { |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 101 | static_assert(kCardClean == 0, "kCardClean must be 0"); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 102 | mem_map_->MadviseDontNeedAndZero(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 105 | bool CardTable::AddrIsInCardTable(const void* addr) const { |
| 106 | return IsValidCard(biased_begin_ + ((uintptr_t)addr >> kCardShift)); |
| 107 | } |
| 108 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 109 | void CardTable::CheckAddrIsInCardTable(const uint8_t* addr) const { |
| 110 | uint8_t* card_addr = biased_begin_ + ((uintptr_t)addr >> kCardShift); |
| 111 | uint8_t* begin = mem_map_->Begin() + offset_; |
| 112 | uint8_t* end = mem_map_->End(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 113 | CHECK(AddrIsInCardTable(addr)) |
| 114 | << "Card table " << this |
| 115 | << " begin: " << reinterpret_cast<void*>(begin) |
| 116 | << " end: " << reinterpret_cast<void*>(end) |
| 117 | << " card_addr: " << reinterpret_cast<void*>(card_addr) |
| 118 | << " heap begin: " << AddrFromCard(begin) |
| 119 | << " heap end: " << AddrFromCard(end) |
| 120 | << " addr: " << reinterpret_cast<const void*>(addr); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 123 | void CardTable::VerifyCardTable() { |
| 124 | UNIMPLEMENTED(WARNING) << "Card table verification"; |
| 125 | } |
| 126 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 127 | } // namespace accounting |
| 128 | } // namespace gc |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 129 | } // namespace art |