1 //===-- Memory.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/Memory.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/DataBufferHeap.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/RangeMap.h"
19 #include "lldb/Core/State.h"
20 #include "lldb/Target/Process.h"
23 using namespace lldb_private;
25 //----------------------------------------------------------------------
26 // MemoryCache constructor
27 //----------------------------------------------------------------------
28 MemoryCache::MemoryCache(Process &process)
34 m_L2_cache_line_byte_size(process.GetMemoryCacheLineSize())
38 //----------------------------------------------------------------------
40 //----------------------------------------------------------------------
41 MemoryCache::~MemoryCache()
46 MemoryCache::Clear(bool clear_invalid_ranges)
48 std::lock_guard<std::recursive_mutex> guard(m_mutex);
51 if (clear_invalid_ranges)
52 m_invalid_ranges.Clear();
53 m_L2_cache_line_byte_size = m_process.GetMemoryCacheLineSize();
57 MemoryCache::AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len)
59 AddL1CacheData(addr,DataBufferSP (new DataBufferHeap(DataBufferHeap(src, src_len))));
63 MemoryCache::AddL1CacheData(lldb::addr_t addr, const DataBufferSP &data_buffer_sp)
65 std::lock_guard<std::recursive_mutex> guard(m_mutex);
66 m_L1_cache[addr] = data_buffer_sp;
70 MemoryCache::Flush (addr_t addr, size_t size)
75 std::lock_guard<std::recursive_mutex> guard(m_mutex);
77 // Erase any blocks from the L1 cache that intersect with the flush range
78 if (!m_L1_cache.empty())
80 AddrRange flush_range(addr, size);
81 BlockMap::iterator pos = m_L1_cache.upper_bound(addr);
82 if (pos != m_L1_cache.begin())
86 while (pos != m_L1_cache.end())
88 AddrRange chunk_range(pos->first, pos->second->GetByteSize());
89 if (!chunk_range.DoesIntersect(flush_range))
91 pos = m_L1_cache.erase(pos);
95 if (!m_L2_cache.empty())
97 const uint32_t cache_line_byte_size = m_L2_cache_line_byte_size;
98 const addr_t end_addr = (addr + size - 1);
99 const addr_t first_cache_line_addr = addr - (addr % cache_line_byte_size);
100 const addr_t last_cache_line_addr = end_addr - (end_addr % cache_line_byte_size);
101 // Watch for overflow where size will cause us to go off the end of the
102 // 64 bit address space
103 uint32_t num_cache_lines;
104 if (last_cache_line_addr >= first_cache_line_addr)
105 num_cache_lines = ((last_cache_line_addr - first_cache_line_addr)/cache_line_byte_size) + 1;
107 num_cache_lines = (UINT64_MAX - first_cache_line_addr + 1)/cache_line_byte_size;
109 uint32_t cache_idx = 0;
110 for (addr_t curr_addr = first_cache_line_addr;
111 cache_idx < num_cache_lines;
112 curr_addr += cache_line_byte_size, ++cache_idx)
114 BlockMap::iterator pos = m_L2_cache.find (curr_addr);
115 if (pos != m_L2_cache.end())
116 m_L2_cache.erase(pos);
122 MemoryCache::AddInvalidRange (lldb::addr_t base_addr, lldb::addr_t byte_size)
126 std::lock_guard<std::recursive_mutex> guard(m_mutex);
127 InvalidRanges::Entry range (base_addr, byte_size);
128 m_invalid_ranges.Append(range);
129 m_invalid_ranges.Sort();
134 MemoryCache::RemoveInvalidRange (lldb::addr_t base_addr, lldb::addr_t byte_size)
138 std::lock_guard<std::recursive_mutex> guard(m_mutex);
139 const uint32_t idx = m_invalid_ranges.FindEntryIndexThatContains(base_addr);
140 if (idx != UINT32_MAX)
142 const InvalidRanges::Entry *entry = m_invalid_ranges.GetEntryAtIndex (idx);
143 if (entry->GetRangeBase() == base_addr && entry->GetByteSize() == byte_size)
144 return m_invalid_ranges.RemoveEntrtAtIndex (idx);
153 MemoryCache::Read (addr_t addr,
158 size_t bytes_left = dst_len;
160 // Check the L1 cache for a range that contain the entire memory read.
161 // If we find a range in the L1 cache that does, we use it. Else we fall
162 // back to reading memory in m_L2_cache_line_byte_size byte sized chunks.
163 // The L1 cache contains chunks of memory that are not required to be
164 // m_L2_cache_line_byte_size bytes in size, so we don't try anything
165 // tricky when reading from them (no partial reads from the L1 cache).
167 std::lock_guard<std::recursive_mutex> guard(m_mutex);
168 if (!m_L1_cache.empty())
170 AddrRange read_range(addr, dst_len);
171 BlockMap::iterator pos = m_L1_cache.upper_bound(addr);
172 if (pos != m_L1_cache.begin ())
176 AddrRange chunk_range(pos->first, pos->second->GetByteSize());
177 if (chunk_range.Contains(read_range))
179 memcpy(dst, pos->second->GetBytes() + addr - chunk_range.GetRangeBase(), dst_len);
185 // If this memory read request is larger than the cache line size, then
186 // we (1) try to read as much of it at once as possible, and (2) don't
187 // add the data to the memory cache. We don't want to split a big read
188 // up into more separate reads than necessary, and with a large memory read
189 // request, it is unlikely that the caller function will ask for the next
190 // 4 bytes after the large memory read - so there's little benefit to saving
192 if (dst && dst_len > m_L2_cache_line_byte_size)
194 size_t bytes_read = m_process.ReadMemoryFromInferior (addr, dst, dst_len, error);
195 // Add this non block sized range to the L1 cache if we actually read anything
197 AddL1CacheData(addr, dst, bytes_read);
201 if (dst && bytes_left > 0)
203 const uint32_t cache_line_byte_size = m_L2_cache_line_byte_size;
204 uint8_t *dst_buf = (uint8_t *)dst;
205 addr_t curr_addr = addr - (addr % cache_line_byte_size);
206 addr_t cache_offset = addr - curr_addr;
208 while (bytes_left > 0)
210 if (m_invalid_ranges.FindEntryThatContains(curr_addr))
212 error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, curr_addr);
213 return dst_len - bytes_left;
216 BlockMap::const_iterator pos = m_L2_cache.find (curr_addr);
217 BlockMap::const_iterator end = m_L2_cache.end ();
221 size_t curr_read_size = cache_line_byte_size - cache_offset;
222 if (curr_read_size > bytes_left)
223 curr_read_size = bytes_left;
225 memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes() + cache_offset, curr_read_size);
227 bytes_left -= curr_read_size;
228 curr_addr += curr_read_size + cache_offset;
233 // Get sequential cache page hits
234 for (++pos; (pos != end) && (bytes_left > 0); ++pos)
236 assert ((curr_addr % cache_line_byte_size) == 0);
238 if (pos->first != curr_addr)
241 curr_read_size = pos->second->GetByteSize();
242 if (curr_read_size > bytes_left)
243 curr_read_size = bytes_left;
245 memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes(), curr_read_size);
247 bytes_left -= curr_read_size;
248 curr_addr += curr_read_size;
250 // We have a cache page that succeeded to read some bytes
251 // but not an entire page. If this happens, we must cap
252 // off how much data we are able to read...
253 if (pos->second->GetByteSize() != cache_line_byte_size)
254 return dst_len - bytes_left;
259 // We need to read from the process
263 assert ((curr_addr % cache_line_byte_size) == 0);
264 std::unique_ptr<DataBufferHeap> data_buffer_heap_ap(new DataBufferHeap (cache_line_byte_size, 0));
265 size_t process_bytes_read = m_process.ReadMemoryFromInferior (curr_addr,
266 data_buffer_heap_ap->GetBytes(),
267 data_buffer_heap_ap->GetByteSize(),
269 if (process_bytes_read == 0)
270 return dst_len - bytes_left;
272 if (process_bytes_read != cache_line_byte_size)
273 data_buffer_heap_ap->SetByteSize (process_bytes_read);
274 m_L2_cache[curr_addr] = DataBufferSP (data_buffer_heap_ap.release());
275 // We have read data and put it into the cache, continue through the
276 // loop again to get the data out of the cache...
281 return dst_len - bytes_left;
286 AllocatedBlock::AllocatedBlock (lldb::addr_t addr,
288 uint32_t permissions,
289 uint32_t chunk_size) :
291 m_byte_size (byte_size),
292 m_permissions (permissions),
293 m_chunk_size (chunk_size),
294 m_offset_to_chunk_size ()
295 // m_allocated (byte_size / chunk_size)
297 assert (byte_size > chunk_size);
300 AllocatedBlock::~AllocatedBlock ()
305 AllocatedBlock::ReserveBlock (uint32_t size)
307 addr_t addr = LLDB_INVALID_ADDRESS;
308 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
309 if (size <= m_byte_size)
311 const uint32_t needed_chunks = CalculateChunksNeededForSize (size);
313 if (m_offset_to_chunk_size.empty())
315 m_offset_to_chunk_size[0] = needed_chunks;
317 log->Printf("[1] AllocatedBlock::ReserveBlock(%p) (size = %u (0x%x)) => offset = 0x%x, %u %u bit chunks", (void *)this,
318 size, size, 0, needed_chunks, m_chunk_size);
323 uint32_t last_offset = 0;
324 OffsetToChunkSize::const_iterator pos = m_offset_to_chunk_size.begin();
325 OffsetToChunkSize::const_iterator end = m_offset_to_chunk_size.end();
328 if (pos->first > last_offset)
330 const uint32_t bytes_available = pos->first - last_offset;
331 const uint32_t num_chunks = CalculateChunksNeededForSize (bytes_available);
332 if (num_chunks >= needed_chunks)
334 m_offset_to_chunk_size[last_offset] = needed_chunks;
336 log->Printf("[2] AllocatedBlock::ReserveBlock(%p) (size = %u (0x%x)) => offset = 0x%x, %u %u bit chunks - "
338 (void *)this, size, size, last_offset, needed_chunks, m_chunk_size, m_offset_to_chunk_size.size());
339 addr = m_addr + last_offset;
344 last_offset = pos->first + pos->second * m_chunk_size;
349 const uint32_t chunks_left = CalculateChunksNeededForSize (m_byte_size - last_offset);
350 if (chunks_left >= needed_chunks)
352 m_offset_to_chunk_size[last_offset] = needed_chunks;
354 log->Printf("[3] AllocatedBlock::ReserveBlock(%p) (size = %u (0x%x)) => offset = 0x%x, %u %u bit chunks - "
356 (void *)this, size, size, last_offset, needed_chunks, m_chunk_size, m_offset_to_chunk_size.size());
357 addr = m_addr + last_offset;
363 // const uint32_t total_chunks = m_allocated.size ();
364 // uint32_t unallocated_idx = 0;
365 // uint32_t allocated_idx = m_allocated.find_first();
366 // uint32_t first_chunk_idx = UINT32_MAX;
367 // uint32_t num_chunks;
370 // if (allocated_idx == UINT32_MAX)
372 // // No more bits are set starting from unallocated_idx, so we
373 // // either have enough chunks for the request, or we don't.
374 // // Eiter way we break out of the while loop...
375 // num_chunks = total_chunks - unallocated_idx;
376 // if (needed_chunks <= num_chunks)
377 // first_chunk_idx = unallocated_idx;
380 // else if (allocated_idx > unallocated_idx)
382 // // We have some allocated chunks, check if there are enough
383 // // free chunks to satisfy the request?
384 // num_chunks = allocated_idx - unallocated_idx;
385 // if (needed_chunks <= num_chunks)
387 // // Yep, we have enough!
388 // first_chunk_idx = unallocated_idx;
393 // while (unallocated_idx < total_chunks)
395 // if (m_allocated[unallocated_idx])
396 // ++unallocated_idx;
401 // if (unallocated_idx >= total_chunks)
404 // allocated_idx = m_allocated.find_next(unallocated_idx);
407 // if (first_chunk_idx != UINT32_MAX)
409 // const uint32_t end_bit_idx = unallocated_idx + needed_chunks;
410 // for (uint32_t idx = first_chunk_idx; idx < end_bit_idx; ++idx)
411 // m_allocated.set(idx);
412 // return m_addr + m_chunk_size * first_chunk_idx;
417 log->Printf("AllocatedBlock::ReserveBlock(%p) (size = %u (0x%x)) => 0x%16.16" PRIx64, (void *)this, size, size, (uint64_t)addr);
422 AllocatedBlock::FreeBlock (addr_t addr)
424 uint32_t offset = addr - m_addr;
425 OffsetToChunkSize::iterator pos = m_offset_to_chunk_size.find (offset);
426 bool success = false;
427 if (pos != m_offset_to_chunk_size.end())
429 m_offset_to_chunk_size.erase (pos);
432 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_VERBOSE));
434 log->Printf("AllocatedBlock::FreeBlock(%p) (addr = 0x%16.16" PRIx64 ") => %i, num_chunks: %lu", (void *)this, (uint64_t)addr,
435 success, m_offset_to_chunk_size.size());
439 AllocatedMemoryCache::AllocatedMemoryCache(Process &process) : m_process(process), m_mutex(), m_memory_map()
443 AllocatedMemoryCache::~AllocatedMemoryCache ()
449 AllocatedMemoryCache::Clear()
451 std::lock_guard<std::recursive_mutex> guard(m_mutex);
452 if (m_process.IsAlive())
454 PermissionsToBlockMap::iterator pos, end = m_memory_map.end();
455 for (pos = m_memory_map.begin(); pos != end; ++pos)
456 m_process.DoDeallocateMemory(pos->second->GetBaseAddress());
458 m_memory_map.clear();
462 AllocatedMemoryCache::AllocatedBlockSP
463 AllocatedMemoryCache::AllocatePage (uint32_t byte_size,
464 uint32_t permissions,
468 AllocatedBlockSP block_sp;
469 const size_t page_size = 4096;
470 const size_t num_pages = (byte_size + page_size - 1) / page_size;
471 const size_t page_byte_size = num_pages * page_size;
473 addr_t addr = m_process.DoAllocateMemory(page_byte_size, permissions, error);
475 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
478 log->Printf ("Process::DoAllocateMemory (byte_size = 0x%8.8" PRIx32 ", permissions = %s) => 0x%16.16" PRIx64,
479 (uint32_t)page_byte_size,
480 GetPermissionsAsCString(permissions),
484 if (addr != LLDB_INVALID_ADDRESS)
486 block_sp.reset (new AllocatedBlock (addr, page_byte_size, permissions, chunk_size));
487 m_memory_map.insert (std::make_pair (permissions, block_sp));
493 AllocatedMemoryCache::AllocateMemory (size_t byte_size,
494 uint32_t permissions,
497 std::lock_guard<std::recursive_mutex> guard(m_mutex);
499 addr_t addr = LLDB_INVALID_ADDRESS;
500 std::pair<PermissionsToBlockMap::iterator, PermissionsToBlockMap::iterator> range = m_memory_map.equal_range (permissions);
502 for (PermissionsToBlockMap::iterator pos = range.first; pos != range.second; ++pos)
504 addr = (*pos).second->ReserveBlock (byte_size);
505 if (addr != LLDB_INVALID_ADDRESS)
509 if (addr == LLDB_INVALID_ADDRESS)
511 AllocatedBlockSP block_sp (AllocatePage (byte_size, permissions, 16, error));
514 addr = block_sp->ReserveBlock (byte_size);
516 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
518 log->Printf ("AllocatedMemoryCache::AllocateMemory (byte_size = 0x%8.8" PRIx32 ", permissions = %s) => 0x%16.16" PRIx64, (uint32_t)byte_size, GetPermissionsAsCString(permissions), (uint64_t)addr);
523 AllocatedMemoryCache::DeallocateMemory (lldb::addr_t addr)
525 std::lock_guard<std::recursive_mutex> guard(m_mutex);
527 PermissionsToBlockMap::iterator pos, end = m_memory_map.end();
528 bool success = false;
529 for (pos = m_memory_map.begin(); pos != end; ++pos)
531 if (pos->second->Contains (addr))
533 success = pos->second->FreeBlock (addr);
537 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
539 log->Printf("AllocatedMemoryCache::DeallocateMemory (addr = 0x%16.16" PRIx64 ") => %i", (uint64_t)addr, success);