blob: ced35941868218677ade3a6b7c9f957fa976e780 [file] [log] [blame]
Greg Claytond495c532011-05-17 03:37:42 +00001//===-- Memory.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Memory.h"
11// C Includes
Virgile Belloffeba252014-03-08 17:15:35 +000012#include <inttypes.h>
Greg Claytond495c532011-05-17 03:37:42 +000013// C++ Includes
14// Other libraries and framework includes
15// Project includes
Greg Clayton358cf1e2015-06-25 21:46:34 +000016#include "lldb/Core/RangeMap.h"
17#include "lldb/Core/State.h"
Greg Claytond495c532011-05-17 03:37:42 +000018#include "lldb/Target/Process.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000019#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000020#include "lldb/Utility/Log.h"
Greg Claytond495c532011-05-17 03:37:42 +000021
22using namespace lldb;
23using namespace lldb_private;
24
25//----------------------------------------------------------------------
26// MemoryCache constructor
27//----------------------------------------------------------------------
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000028MemoryCache::MemoryCache(Process &process)
Kate Stoneb9c1b512016-09-06 20:57:50 +000029 : m_mutex(), m_L1_cache(), m_L2_cache(), m_invalid_ranges(),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000030 m_process(process),
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 m_L2_cache_line_byte_size(process.GetMemoryCacheLineSize()) {}
Greg Claytond495c532011-05-17 03:37:42 +000032
33//----------------------------------------------------------------------
34// Destructor
35//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000036MemoryCache::~MemoryCache() {}
37
38void MemoryCache::Clear(bool clear_invalid_ranges) {
39 std::lock_guard<std::recursive_mutex> guard(m_mutex);
40 m_L1_cache.clear();
41 m_L2_cache.clear();
42 if (clear_invalid_ranges)
43 m_invalid_ranges.Clear();
44 m_L2_cache_line_byte_size = m_process.GetMemoryCacheLineSize();
Greg Claytond495c532011-05-17 03:37:42 +000045}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047void MemoryCache::AddL1CacheData(lldb::addr_t addr, const void *src,
48 size_t src_len) {
49 AddL1CacheData(
50 addr, DataBufferSP(new DataBufferHeap(DataBufferHeap(src, src_len))));
51}
52
53void MemoryCache::AddL1CacheData(lldb::addr_t addr,
54 const DataBufferSP &data_buffer_sp) {
55 std::lock_guard<std::recursive_mutex> guard(m_mutex);
56 m_L1_cache[addr] = data_buffer_sp;
57}
58
59void MemoryCache::Flush(addr_t addr, size_t size) {
60 if (size == 0)
61 return;
62
63 std::lock_guard<std::recursive_mutex> guard(m_mutex);
64
65 // Erase any blocks from the L1 cache that intersect with the flush range
66 if (!m_L1_cache.empty()) {
67 AddrRange flush_range(addr, size);
68 BlockMap::iterator pos = m_L1_cache.upper_bound(addr);
69 if (pos != m_L1_cache.begin()) {
70 --pos;
71 }
72 while (pos != m_L1_cache.end()) {
73 AddrRange chunk_range(pos->first, pos->second->GetByteSize());
74 if (!chunk_range.DoesIntersect(flush_range))
75 break;
76 pos = m_L1_cache.erase(pos);
77 }
78 }
79
80 if (!m_L2_cache.empty()) {
81 const uint32_t cache_line_byte_size = m_L2_cache_line_byte_size;
82 const addr_t end_addr = (addr + size - 1);
83 const addr_t first_cache_line_addr = addr - (addr % cache_line_byte_size);
84 const addr_t last_cache_line_addr =
85 end_addr - (end_addr % cache_line_byte_size);
86 // Watch for overflow where size will cause us to go off the end of the
87 // 64 bit address space
88 uint32_t num_cache_lines;
89 if (last_cache_line_addr >= first_cache_line_addr)
90 num_cache_lines = ((last_cache_line_addr - first_cache_line_addr) /
91 cache_line_byte_size) +
92 1;
93 else
94 num_cache_lines =
95 (UINT64_MAX - first_cache_line_addr + 1) / cache_line_byte_size;
96
97 uint32_t cache_idx = 0;
98 for (addr_t curr_addr = first_cache_line_addr; cache_idx < num_cache_lines;
99 curr_addr += cache_line_byte_size, ++cache_idx) {
100 BlockMap::iterator pos = m_L2_cache.find(curr_addr);
101 if (pos != m_L2_cache.end())
102 m_L2_cache.erase(pos);
103 }
104 }
105}
106
107void MemoryCache::AddInvalidRange(lldb::addr_t base_addr,
108 lldb::addr_t byte_size) {
109 if (byte_size > 0) {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000110 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 InvalidRanges::Entry range(base_addr, byte_size);
112 m_invalid_ranges.Append(range);
113 m_invalid_ranges.Sort();
114 }
Greg Clayton358cf1e2015-06-25 21:46:34 +0000115}
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117bool MemoryCache::RemoveInvalidRange(lldb::addr_t base_addr,
118 lldb::addr_t byte_size) {
119 if (byte_size > 0) {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000120 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 const uint32_t idx = m_invalid_ranges.FindEntryIndexThatContains(base_addr);
122 if (idx != UINT32_MAX) {
123 const InvalidRanges::Entry *entry = m_invalid_ranges.GetEntryAtIndex(idx);
124 if (entry->GetRangeBase() == base_addr &&
125 entry->GetByteSize() == byte_size)
126 return m_invalid_ranges.RemoveEntrtAtIndex(idx);
127 }
128 }
129 return false;
Greg Claytond495c532011-05-17 03:37:42 +0000130}
131
Zachary Turner97206d52017-05-12 04:51:55 +0000132size_t MemoryCache::Read(addr_t addr, void *dst, size_t dst_len,
133 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 size_t bytes_left = dst_len;
Greg Claytonde0e9d02012-04-13 20:37:20 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 // Check the L1 cache for a range that contain the entire memory read.
137 // If we find a range in the L1 cache that does, we use it. Else we fall
138 // back to reading memory in m_L2_cache_line_byte_size byte sized chunks.
139 // The L1 cache contains chunks of memory that are not required to be
140 // m_L2_cache_line_byte_size bytes in size, so we don't try anything
141 // tricky when reading from them (no partial reads from the L1 cache).
Greg Claytonde0e9d02012-04-13 20:37:20 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 std::lock_guard<std::recursive_mutex> guard(m_mutex);
144 if (!m_L1_cache.empty()) {
145 AddrRange read_range(addr, dst_len);
146 BlockMap::iterator pos = m_L1_cache.upper_bound(addr);
147 if (pos != m_L1_cache.begin()) {
148 --pos;
Greg Clayton358cf1e2015-06-25 21:46:34 +0000149 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 AddrRange chunk_range(pos->first, pos->second->GetByteSize());
151 if (chunk_range.Contains(read_range)) {
152 memcpy(dst, pos->second->GetBytes() + addr - chunk_range.GetRangeBase(),
153 dst_len);
154 return dst_len;
Greg Claytond495c532011-05-17 03:37:42 +0000155 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 }
157
158 // If this memory read request is larger than the cache line size, then
159 // we (1) try to read as much of it at once as possible, and (2) don't
160 // add the data to the memory cache. We don't want to split a big read
161 // up into more separate reads than necessary, and with a large memory read
162 // request, it is unlikely that the caller function will ask for the next
163 // 4 bytes after the large memory read - so there's little benefit to saving
164 // it in the cache.
165 if (dst && dst_len > m_L2_cache_line_byte_size) {
166 size_t bytes_read =
167 m_process.ReadMemoryFromInferior(addr, dst, dst_len, error);
168 // Add this non block sized range to the L1 cache if we actually read
169 // anything
170 if (bytes_read > 0)
171 AddL1CacheData(addr, dst, bytes_read);
172 return bytes_read;
173 }
174
175 if (dst && bytes_left > 0) {
176 const uint32_t cache_line_byte_size = m_L2_cache_line_byte_size;
177 uint8_t *dst_buf = (uint8_t *)dst;
178 addr_t curr_addr = addr - (addr % cache_line_byte_size);
179 addr_t cache_offset = addr - curr_addr;
180
181 while (bytes_left > 0) {
182 if (m_invalid_ranges.FindEntryThatContains(curr_addr)) {
183 error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64,
184 curr_addr);
185 return dst_len - bytes_left;
186 }
187
188 BlockMap::const_iterator pos = m_L2_cache.find(curr_addr);
189 BlockMap::const_iterator end = m_L2_cache.end();
190
191 if (pos != end) {
192 size_t curr_read_size = cache_line_byte_size - cache_offset;
193 if (curr_read_size > bytes_left)
194 curr_read_size = bytes_left;
195
196 memcpy(dst_buf + dst_len - bytes_left,
197 pos->second->GetBytes() + cache_offset, curr_read_size);
198
199 bytes_left -= curr_read_size;
200 curr_addr += curr_read_size + cache_offset;
201 cache_offset = 0;
202
203 if (bytes_left > 0) {
204 // Get sequential cache page hits
205 for (++pos; (pos != end) && (bytes_left > 0); ++pos) {
206 assert((curr_addr % cache_line_byte_size) == 0);
207
208 if (pos->first != curr_addr)
209 break;
210
211 curr_read_size = pos->second->GetByteSize();
212 if (curr_read_size > bytes_left)
213 curr_read_size = bytes_left;
214
215 memcpy(dst_buf + dst_len - bytes_left, pos->second->GetBytes(),
216 curr_read_size);
217
218 bytes_left -= curr_read_size;
219 curr_addr += curr_read_size;
220
221 // We have a cache page that succeeded to read some bytes
222 // but not an entire page. If this happens, we must cap
223 // off how much data we are able to read...
224 if (pos->second->GetByteSize() != cache_line_byte_size)
225 return dst_len - bytes_left;
226 }
227 }
228 }
229
230 // We need to read from the process
231
232 if (bytes_left > 0) {
233 assert((curr_addr % cache_line_byte_size) == 0);
234 std::unique_ptr<DataBufferHeap> data_buffer_heap_ap(
235 new DataBufferHeap(cache_line_byte_size, 0));
236 size_t process_bytes_read = m_process.ReadMemoryFromInferior(
237 curr_addr, data_buffer_heap_ap->GetBytes(),
238 data_buffer_heap_ap->GetByteSize(), error);
239 if (process_bytes_read == 0)
240 return dst_len - bytes_left;
241
242 if (process_bytes_read != cache_line_byte_size)
243 data_buffer_heap_ap->SetByteSize(process_bytes_read);
244 m_L2_cache[curr_addr] = DataBufferSP(data_buffer_heap_ap.release());
245 // We have read data and put it into the cache, continue through the
246 // loop again to get the data out of the cache...
247 }
248 }
249 }
250
251 return dst_len - bytes_left;
Greg Claytond495c532011-05-17 03:37:42 +0000252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254AllocatedBlock::AllocatedBlock(lldb::addr_t addr, uint32_t byte_size,
255 uint32_t permissions, uint32_t chunk_size)
Greg Claytonac7c2ef2017-02-09 17:56:55 +0000256 : m_range(addr, byte_size), m_permissions(permissions),
257 m_chunk_size(chunk_size)
Greg Claytond495c532011-05-17 03:37:42 +0000258{
Greg Claytonac7c2ef2017-02-09 17:56:55 +0000259 // The entire address range is free to start with.
260 m_free_blocks.Append(m_range);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 assert(byte_size > chunk_size);
Greg Claytond495c532011-05-17 03:37:42 +0000262}
263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264AllocatedBlock::~AllocatedBlock() {}
Greg Claytond495c532011-05-17 03:37:42 +0000265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266lldb::addr_t AllocatedBlock::ReserveBlock(uint32_t size) {
Greg Clayton98f9bcc2017-02-22 23:42:55 +0000267 // We must return something valid for zero bytes.
268 if (size == 0)
269 size = 1;
Pavel Labath3b7e1982017-02-05 00:44:54 +0000270 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Greg Claytonac7c2ef2017-02-09 17:56:55 +0000271
272 const size_t free_count = m_free_blocks.GetSize();
273 for (size_t i=0; i<free_count; ++i)
274 {
Greg Clayton21b4b2e2017-02-09 18:21:04 +0000275 auto &free_block = m_free_blocks.GetEntryRef(i);
Greg Claytonac7c2ef2017-02-09 17:56:55 +0000276 const lldb::addr_t range_size = free_block.GetByteSize();
277 if (range_size >= size)
278 {
279 // We found a free block that is big enough for our data. Figure out how
280 // many chunks we will need and calculate the resulting block size we will
281 // reserve.
Greg Clayton98f9bcc2017-02-22 23:42:55 +0000282 addr_t addr = free_block.GetRangeBase();
Greg Claytonac7c2ef2017-02-09 17:56:55 +0000283 size_t num_chunks = CalculateChunksNeededForSize(size);
284 lldb::addr_t block_size = num_chunks * m_chunk_size;
285 lldb::addr_t bytes_left = range_size - block_size;
286 if (bytes_left == 0)
287 {
288 // The newly allocated block will take all of the bytes in this
289 // available block, so we can just add it to the allocated ranges and
290 // remove the range from the free ranges.
291 m_reserved_blocks.Insert(free_block, false);
292 m_free_blocks.RemoveEntryAtIndex(i);
293 }
294 else
295 {
296 // Make the new allocated range and add it to the allocated ranges.
Greg Clayton21b4b2e2017-02-09 18:21:04 +0000297 Range<lldb::addr_t, uint32_t> reserved_block(free_block);
Greg Claytonac7c2ef2017-02-09 17:56:55 +0000298 reserved_block.SetByteSize(block_size);
299 // Insert the reserved range and don't combine it with other blocks
300 // in the reserved blocks list.
301 m_reserved_blocks.Insert(reserved_block, false);
302 // Adjust the free range in place since we won't change the sorted
303 // ordering of the m_free_blocks list.
304 free_block.SetRangeBase(reserved_block.GetRangeEnd());
305 free_block.SetByteSize(bytes_left);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 }
Greg Clayton98f9bcc2017-02-22 23:42:55 +0000307 LLDB_LOGV(log, "({0}) (size = {1} ({1:x})) => {2:x}", this, size, addr);
308 return addr;
Greg Claytond495c532011-05-17 03:37:42 +0000309 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 }
Jim Inghame7701fe2014-08-08 20:01:41 +0000311
Greg Clayton98f9bcc2017-02-22 23:42:55 +0000312 LLDB_LOGV(log, "({0}) (size = {1} ({1:x})) => {2:x}", this, size,
313 LLDB_INVALID_ADDRESS);
314 return LLDB_INVALID_ADDRESS;
Greg Claytond495c532011-05-17 03:37:42 +0000315}
316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317bool AllocatedBlock::FreeBlock(addr_t addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 bool success = false;
Greg Claytonac7c2ef2017-02-09 17:56:55 +0000319 auto entry_idx = m_reserved_blocks.FindEntryIndexThatContains(addr);
320 if (entry_idx != UINT32_MAX)
321 {
322 m_free_blocks.Insert(m_reserved_blocks.GetEntryRef(entry_idx), true);
323 m_reserved_blocks.RemoveEntryAtIndex(entry_idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 success = true;
325 }
Pavel Labath3b7e1982017-02-05 00:44:54 +0000326 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Greg Claytonac7c2ef2017-02-09 17:56:55 +0000327 LLDB_LOGV(log, "({0}) (addr = {1:x}) => {2}", this, addr, success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 return success;
Greg Claytond495c532011-05-17 03:37:42 +0000329}
330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331AllocatedMemoryCache::AllocatedMemoryCache(Process &process)
332 : m_process(process), m_mutex(), m_memory_map() {}
333
334AllocatedMemoryCache::~AllocatedMemoryCache() {}
335
336void AllocatedMemoryCache::Clear() {
337 std::lock_guard<std::recursive_mutex> guard(m_mutex);
338 if (m_process.IsAlive()) {
339 PermissionsToBlockMap::iterator pos, end = m_memory_map.end();
340 for (pos = m_memory_map.begin(); pos != end; ++pos)
341 m_process.DoDeallocateMemory(pos->second->GetBaseAddress());
342 }
343 m_memory_map.clear();
Greg Claytond495c532011-05-17 03:37:42 +0000344}
345
Greg Claytond495c532011-05-17 03:37:42 +0000346AllocatedMemoryCache::AllocatedBlockSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347AllocatedMemoryCache::AllocatePage(uint32_t byte_size, uint32_t permissions,
Zachary Turner97206d52017-05-12 04:51:55 +0000348 uint32_t chunk_size, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 AllocatedBlockSP block_sp;
350 const size_t page_size = 4096;
351 const size_t num_pages = (byte_size + page_size - 1) / page_size;
352 const size_t page_byte_size = num_pages * page_size;
Greg Claytond495c532011-05-17 03:37:42 +0000353
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 addr_t addr = m_process.DoAllocateMemory(page_byte_size, permissions, error);
Greg Claytond495c532011-05-17 03:37:42 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
357 if (log) {
358 log->Printf("Process::DoAllocateMemory (byte_size = 0x%8.8" PRIx32
359 ", permissions = %s) => 0x%16.16" PRIx64,
360 (uint32_t)page_byte_size, GetPermissionsAsCString(permissions),
361 (uint64_t)addr);
362 }
Greg Claytond495c532011-05-17 03:37:42 +0000363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 if (addr != LLDB_INVALID_ADDRESS) {
365 block_sp.reset(
366 new AllocatedBlock(addr, page_byte_size, permissions, chunk_size));
367 m_memory_map.insert(std::make_pair(permissions, block_sp));
368 }
369 return block_sp;
370}
371
372lldb::addr_t AllocatedMemoryCache::AllocateMemory(size_t byte_size,
373 uint32_t permissions,
Zachary Turner97206d52017-05-12 04:51:55 +0000374 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 std::lock_guard<std::recursive_mutex> guard(m_mutex);
376
377 addr_t addr = LLDB_INVALID_ADDRESS;
378 std::pair<PermissionsToBlockMap::iterator, PermissionsToBlockMap::iterator>
379 range = m_memory_map.equal_range(permissions);
380
381 for (PermissionsToBlockMap::iterator pos = range.first; pos != range.second;
382 ++pos) {
383 addr = (*pos).second->ReserveBlock(byte_size);
Greg Claytond495c532011-05-17 03:37:42 +0000384 if (addr != LLDB_INVALID_ADDRESS)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 break;
386 }
387
388 if (addr == LLDB_INVALID_ADDRESS) {
389 AllocatedBlockSP block_sp(AllocatePage(byte_size, permissions, 16, error));
390
391 if (block_sp)
392 addr = block_sp->ReserveBlock(byte_size);
393 }
394 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
395 if (log)
396 log->Printf(
397 "AllocatedMemoryCache::AllocateMemory (byte_size = 0x%8.8" PRIx32
398 ", permissions = %s) => 0x%16.16" PRIx64,
399 (uint32_t)byte_size, GetPermissionsAsCString(permissions),
400 (uint64_t)addr);
401 return addr;
Greg Claytond495c532011-05-17 03:37:42 +0000402}
403
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404bool AllocatedMemoryCache::DeallocateMemory(lldb::addr_t addr) {
405 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 PermissionsToBlockMap::iterator pos, end = m_memory_map.end();
408 bool success = false;
409 for (pos = m_memory_map.begin(); pos != end; ++pos) {
410 if (pos->second->Contains(addr)) {
411 success = pos->second->FreeBlock(addr);
412 break;
Greg Claytond495c532011-05-17 03:37:42 +0000413 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 }
415 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
416 if (log)
417 log->Printf("AllocatedMemoryCache::DeallocateMemory (addr = 0x%16.16" PRIx64
418 ") => %i",
419 (uint64_t)addr, success);
420 return success;
Greg Claytond495c532011-05-17 03:37:42 +0000421}