1 /*
2  * Copyright (C) 2014 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 #ifndef ART_RUNTIME_GC_ACCOUNTING_READ_BARRIER_TABLE_H_
18 #define ART_RUNTIME_GC_ACCOUNTING_READ_BARRIER_TABLE_H_
19 
20 #include <sys/mman.h>  // For the PROT_* and MAP_* constants.
21 
22 #include "base/bit_utils.h"
23 #include "base/locks.h"
24 #include "base/mem_map.h"
25 #include "gc/space/space.h"
26 #include "runtime_globals.h"
27 
28 namespace art {
29 namespace gc {
30 namespace accounting {
31 
32 // Used to decide whether to take the read barrier fast/slow paths for
33 // kUseTableLookupReadBarrier. If an entry is set, take the read
34 // barrier slow path. There's an entry per region.
35 class ReadBarrierTable {
36  public:
ReadBarrierTable()37   ReadBarrierTable() {
38     size_t capacity = static_cast<size_t>(kHeapCapacity / kRegionSize);
39     DCHECK_EQ(kHeapCapacity / kRegionSize,
40               static_cast<uint64_t>(static_cast<size_t>(kHeapCapacity / kRegionSize)));
41     std::string error_msg;
42     mem_map_ = MemMap::MapAnonymous("read barrier table",
43                                     capacity,
44                                     PROT_READ | PROT_WRITE,
45                                     /*low_4gb=*/ false,
46                                     &error_msg);
47     CHECK(mem_map_.IsValid() && mem_map_.Begin() != nullptr)
48         << "couldn't allocate read barrier table: " << error_msg;
49   }
ClearForSpace(space::ContinuousSpace * space)50   void ClearForSpace(space::ContinuousSpace* space) {
51     uint8_t* entry_start = EntryFromAddr(space->Begin());
52     uint8_t* entry_end = EntryFromAddr(space->Limit());
53     memset(reinterpret_cast<void*>(entry_start), 0, entry_end - entry_start);
54   }
Clear(uint8_t * start_addr,uint8_t * end_addr)55   void Clear(uint8_t* start_addr, uint8_t* end_addr) {
56     DCHECK(IsValidHeapAddr(start_addr)) << start_addr;
57     DCHECK(IsValidHeapAddr(end_addr)) << end_addr;
58     DCHECK_ALIGNED(start_addr, kRegionSize);
59     DCHECK_ALIGNED(end_addr, kRegionSize);
60     uint8_t* entry_start = EntryFromAddr(start_addr);
61     uint8_t* entry_end = EntryFromAddr(end_addr);
62     memset(reinterpret_cast<void*>(entry_start), 0, entry_end - entry_start);
63   }
IsSet(const void * heap_addr)64   bool IsSet(const void* heap_addr) const {
65     DCHECK(IsValidHeapAddr(heap_addr)) << heap_addr;
66     uint8_t entry_value = *EntryFromAddr(heap_addr);
67     DCHECK(entry_value == 0 || entry_value == kSetEntryValue);
68     return entry_value == kSetEntryValue;
69   }
ClearAll()70   void ClearAll() {
71     mem_map_.MadviseDontNeedAndZero();
72   }
SetAll()73   void SetAll() {
74     memset(mem_map_.Begin(), kSetEntryValue, mem_map_.Size());
75   }
IsAllCleared()76   bool IsAllCleared() const {
77     for (uint32_t* p = reinterpret_cast<uint32_t*>(mem_map_.Begin());
78          p < reinterpret_cast<uint32_t*>(mem_map_.End()); ++p) {
79       if (*p != 0) {
80         return false;
81       }
82     }
83     return true;
84   }
85 
86   // This should match RegionSpace::kRegionSize. static_assert'ed in concurrent_copying.h.
87   static constexpr size_t kRegionSize = 256 * KB;
88 
89  private:
90   static constexpr uint64_t kHeapCapacity = 4ULL * GB;  // low 4gb.
91   static constexpr uint8_t kSetEntryValue = 0x01;
92 
EntryFromAddr(const void * heap_addr)93   uint8_t* EntryFromAddr(const void* heap_addr) const {
94     DCHECK(IsValidHeapAddr(heap_addr)) << heap_addr;
95     uint8_t* entry_addr = mem_map_.Begin() + reinterpret_cast<uintptr_t>(heap_addr) / kRegionSize;
96     DCHECK(IsValidEntry(entry_addr)) << "heap_addr: " << heap_addr
97                                      << " entry_addr: " << reinterpret_cast<void*>(entry_addr);
98     return entry_addr;
99   }
100 
IsValidHeapAddr(const void * heap_addr)101   bool IsValidHeapAddr(const void* heap_addr) const {
102 #ifdef __LP64__
103     return reinterpret_cast<uint64_t>(heap_addr) < kHeapCapacity;
104 #else
105     UNUSED(heap_addr);
106     return true;
107 #endif
108   }
109 
IsValidEntry(const uint8_t * entry_addr)110   bool IsValidEntry(const uint8_t* entry_addr) const {
111     uint8_t* begin = mem_map_.Begin();
112     uint8_t* end = mem_map_.End();
113     return entry_addr >= begin && entry_addr < end;
114   }
115 
116   MemMap mem_map_;
117 };
118 
119 }  // namespace accounting
120 }  // namespace gc
121 }  // namespace art
122 
123 #endif  // ART_RUNTIME_GC_ACCOUNTING_READ_BARRIER_TABLE_H_
124