1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jit_code_cache.h"
#include <sstream>
#include "art_method-inl.h"
#include "mem_map.h"
#include "oat_file-inl.h"
namespace art {
namespace jit {
static constexpr int kProtAll = PROT_READ | PROT_WRITE | PROT_EXEC;
static constexpr int kProtData = PROT_READ | PROT_WRITE;
static constexpr int kProtCode = PROT_READ | PROT_EXEC;
#define CHECKED_MPROTECT(memory, size, prot) \
do { \
int rc = mprotect(memory, size, prot); \
if (UNLIKELY(rc != 0)) { \
errno = rc; \
PLOG(FATAL) << "Failed to mprotect jit code cache"; \
} \
} while (false) \
JitCodeCache* JitCodeCache::Create(size_t capacity, std::string* error_msg) {
CHECK_GT(capacity, 0U);
CHECK_LT(capacity, kMaxCapacity);
std::string error_str;
// Map name specific for android_os_Debug.cpp accounting.
MemMap* data_map = MemMap::MapAnonymous(
"data-code-cache", nullptr, capacity, kProtAll, false, false, &error_str);
if (data_map == nullptr) {
std::ostringstream oss;
oss << "Failed to create read write execute cache: " << error_str << " size=" << capacity;
*error_msg = oss.str();
return nullptr;
}
// Data cache is 1 / 4 of the map.
// TODO: Make this variable?
size_t data_size = RoundUp(data_map->Size() / 4, kPageSize);
size_t code_size = data_map->Size() - data_size;
uint8_t* divider = data_map->Begin() + data_size;
// We need to have 32 bit offsets from method headers in code cache which point to things
// in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work.
MemMap* code_map = data_map->RemapAtEnd(divider, "jit-code-cache", kProtAll, &error_str);
if (code_map == nullptr) {
std::ostringstream oss;
oss << "Failed to create read write execute cache: " << error_str << " size=" << capacity;
*error_msg = oss.str();
return nullptr;
}
DCHECK_EQ(code_map->Size(), code_size);
DCHECK_EQ(code_map->Begin(), divider);
return new JitCodeCache(code_map, data_map);
}
JitCodeCache::JitCodeCache(MemMap* code_map, MemMap* data_map)
: lock_("Jit code cache", kJitCodeCacheLock),
code_map_(code_map),
data_map_(data_map),
num_methods_(0) {
VLOG(jit) << "Created jit code cache: data size="
<< PrettySize(data_map_->Size())
<< ", code size="
<< PrettySize(code_map_->Size());
code_mspace_ = create_mspace_with_base(code_map_->Begin(), code_map_->Size(), false /*locked*/);
data_mspace_ = create_mspace_with_base(data_map_->Begin(), data_map_->Size(), false /*locked*/);
if (code_mspace_ == nullptr || data_mspace_ == nullptr) {
PLOG(FATAL) << "create_mspace_with_base failed";
}
// Prevent morecore requests from the mspace.
mspace_set_footprint_limit(code_mspace_, code_map_->Size());
mspace_set_footprint_limit(data_mspace_, data_map_->Size());
CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode);
CHECKED_MPROTECT(data_map_->Begin(), data_map_->Size(), kProtData);
}
bool JitCodeCache::ContainsMethod(ArtMethod* method) const {
return ContainsCodePtr(method->GetEntryPointFromQuickCompiledCode());
}
bool JitCodeCache::ContainsCodePtr(const void* ptr) const {
return code_map_->Begin() <= ptr && ptr < code_map_->End();
}
class ScopedCodeCacheWrite {
public:
explicit ScopedCodeCacheWrite(MemMap* code_map) : code_map_(code_map) {
CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtAll);
}
~ScopedCodeCacheWrite() {
CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode);
}
private:
MemMap* const code_map_;
DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite);
};
uint8_t* JitCodeCache::CommitCode(Thread* self,
const uint8_t* mapping_table,
const uint8_t* vmap_table,
const uint8_t* gc_map,
size_t frame_size_in_bytes,
size_t core_spill_mask,
size_t fp_spill_mask,
const uint8_t* code,
size_t code_size) {
size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
// Ensure the header ends up at expected instruction alignment.
size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
size_t total_size = header_size + code_size;
OatQuickMethodHeader* method_header = nullptr;
uint8_t* code_ptr = nullptr;
MutexLock mu(self, lock_);
{
ScopedCodeCacheWrite scc(code_map_.get());
uint8_t* result = reinterpret_cast<uint8_t*>(
mspace_memalign(code_mspace_, alignment, total_size));
if (result == nullptr) {
return nullptr;
}
code_ptr = result + header_size;
DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(code_ptr), alignment);
std::copy(code, code + code_size, code_ptr);
method_header = reinterpret_cast<OatQuickMethodHeader*>(code_ptr) - 1;
new (method_header) OatQuickMethodHeader(
(mapping_table == nullptr) ? 0 : code_ptr - mapping_table,
(vmap_table == nullptr) ? 0 : code_ptr - vmap_table,
(gc_map == nullptr) ? 0 : code_ptr - gc_map,
frame_size_in_bytes,
core_spill_mask,
fp_spill_mask,
code_size);
}
__builtin___clear_cache(reinterpret_cast<char*>(code_ptr),
reinterpret_cast<char*>(code_ptr + code_size));
++num_methods_; // TODO: This is hacky but works since each method has exactly one code region.
return reinterpret_cast<uint8_t*>(method_header);
}
size_t JitCodeCache::CodeCacheSize() {
MutexLock mu(Thread::Current(), lock_);
size_t bytes_allocated = 0;
mspace_inspect_all(code_mspace_, DlmallocBytesAllocatedCallback, &bytes_allocated);
return bytes_allocated;
}
size_t JitCodeCache::DataCacheSize() {
MutexLock mu(Thread::Current(), lock_);
size_t bytes_allocated = 0;
mspace_inspect_all(data_mspace_, DlmallocBytesAllocatedCallback, &bytes_allocated);
return bytes_allocated;
}
uint8_t* JitCodeCache::ReserveData(Thread* self, size_t size) {
size = RoundUp(size, sizeof(void*));
MutexLock mu(self, lock_);
return reinterpret_cast<uint8_t*>(mspace_malloc(data_mspace_, size));
}
uint8_t* JitCodeCache::AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end) {
uint8_t* result = ReserveData(self, end - begin);
if (result == nullptr) {
return nullptr; // Out of space in the data cache.
}
std::copy(begin, end, result);
return result;
}
const void* JitCodeCache::GetCodeFor(ArtMethod* method) {
const void* code = method->GetEntryPointFromQuickCompiledCode();
if (ContainsCodePtr(code)) {
return code;
}
MutexLock mu(Thread::Current(), lock_);
auto it = method_code_map_.find(method);
if (it != method_code_map_.end()) {
return it->second;
}
return nullptr;
}
void JitCodeCache::SaveCompiledCode(ArtMethod* method, const void* old_code_ptr) {
DCHECK_EQ(method->GetEntryPointFromQuickCompiledCode(), old_code_ptr);
DCHECK(ContainsCodePtr(old_code_ptr)) << PrettyMethod(method) << " old_code_ptr="
<< old_code_ptr;
MutexLock mu(Thread::Current(), lock_);
auto it = method_code_map_.find(method);
if (it != method_code_map_.end()) {
return;
}
method_code_map_.Put(method, old_code_ptr);
}
} // namespace jit
} // namespace art
|