1 //===-- MachVMMemory.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 // Created by Greg Clayton on 6/26/07. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MachVMMemory.h" 15 #include "DNBLog.h" 16 #include "MachVMRegion.h" 17 #include <dlfcn.h> 18 #include <mach/mach_vm.h> 19 #include <mach/shared_region.h> 20 #include <sys/sysctl.h> 21 22 #if defined(WITH_FBS) || defined(WITH_BKS) 23 extern "C" { 24 #import <sys/kern_memorystatus.h> 25 } 26 #endif 27 28 static const vm_size_t kInvalidPageSize = ~0; 29 30 MachVMMemory::MachVMMemory() : m_page_size(kInvalidPageSize), m_err(0) {} 31 32 MachVMMemory::~MachVMMemory() {} 33 34 nub_size_t MachVMMemory::PageSize(task_t task) { 35 if (m_page_size == kInvalidPageSize) { 36 #if defined(TASK_VM_INFO) && TASK_VM_INFO >= 22 37 if (task != TASK_NULL) { 38 kern_return_t kr; 39 mach_msg_type_number_t info_count = TASK_VM_INFO_COUNT; 40 task_vm_info_data_t vm_info; 41 kr = task_info(task, TASK_VM_INFO, (task_info_t)&vm_info, &info_count); 42 if (kr == KERN_SUCCESS) { 43 DNBLogThreadedIf( 44 LOG_TASK, 45 "MachVMMemory::PageSize task_info returned page size of 0x%x", 46 (int)vm_info.page_size); 47 m_page_size = vm_info.page_size; 48 return m_page_size; 49 } else { 50 DNBLogThreadedIf(LOG_TASK, "MachVMMemory::PageSize task_info call " 51 "failed to get page size, TASK_VM_INFO %d, " 52 "TASK_VM_INFO_COUNT %d, kern return %d", 53 TASK_VM_INFO, TASK_VM_INFO_COUNT, kr); 54 } 55 } 56 #endif 57 m_err = ::host_page_size(::mach_host_self(), &m_page_size); 58 if (m_err.Fail()) 59 m_page_size = 0; 60 } 61 return m_page_size; 62 } 63 64 nub_size_t MachVMMemory::MaxBytesLeftInPage(task_t task, nub_addr_t addr, 65 nub_size_t count) { 66 const nub_size_t page_size = PageSize(task); 67 if (page_size > 0) { 68 nub_size_t page_offset = (addr % page_size); 69 nub_size_t bytes_left_in_page = page_size - page_offset; 70 if (count > bytes_left_in_page) 71 count = bytes_left_in_page; 72 } 73 return count; 74 } 75 76 nub_bool_t MachVMMemory::GetMemoryRegionInfo(task_t task, nub_addr_t address, 77 DNBRegionInfo *region_info) { 78 MachVMRegion vmRegion(task); 79 80 if (vmRegion.GetRegionForAddress(address)) { 81 region_info->addr = vmRegion.StartAddress(); 82 region_info->size = vmRegion.GetByteSize(); 83 region_info->permissions = vmRegion.GetDNBPermissions(); 84 } else { 85 region_info->addr = address; 86 region_info->size = 0; 87 if (vmRegion.GetError().Success()) { 88 // vmRegion.GetRegionForAddress() return false, indicating that "address" 89 // wasn't in a valid region, but the "vmRegion" info was successfully 90 // read from the task which means the info describes the next valid 91 // region from which we can infer the size of this invalid region 92 mach_vm_address_t start_addr = vmRegion.StartAddress(); 93 if (address < start_addr) 94 region_info->size = start_addr - address; 95 } 96 // If we can't get any info about the size from the next region it means 97 // we asked about an address that was past all mappings, so the size 98 // of this region will take up all remaining address space. 99 if (region_info->size == 0) 100 region_info->size = INVALID_NUB_ADDRESS - region_info->addr; 101 102 // Not readable, writeable or executable 103 region_info->permissions = 0; 104 } 105 return true; 106 } 107 108 static uint64_t GetPhysicalMemory() { 109 // This doesn't change often at all. No need to poll each time. 110 static uint64_t physical_memory = 0; 111 static bool calculated = false; 112 if (calculated) 113 return physical_memory; 114 115 size_t len = sizeof(physical_memory); 116 sysctlbyname("hw.memsize", &physical_memory, &len, NULL, 0); 117 118 calculated = true; 119 return physical_memory; 120 } 121 122 nub_bool_t MachVMMemory::GetMemoryProfile( 123 DNBProfileDataScanType scanType, task_t task, struct task_basic_info ti, 124 cpu_type_t cputype, nub_process_t pid, vm_statistics64_data_t &vminfo, 125 uint64_t &physical_memory, uint64_t &anonymous, 126 uint64_t &phys_footprint, uint64_t &memory_cap) 127 { 128 if (scanType & eProfileHostMemory) 129 physical_memory = GetPhysicalMemory(); 130 131 if (scanType & eProfileMemory) { 132 static mach_port_t localHost = mach_host_self(); 133 mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; 134 host_statistics64(localHost, HOST_VM_INFO64, (host_info64_t)&vminfo, 135 &count); 136 137 kern_return_t kr; 138 mach_msg_type_number_t info_count; 139 task_vm_info_data_t vm_info; 140 141 info_count = TASK_VM_INFO_COUNT; 142 kr = task_info(task, TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &info_count); 143 if (kr == KERN_SUCCESS) { 144 if (scanType & eProfileMemoryAnonymous) { 145 anonymous = vm_info.internal + vm_info.compressed - vm_info.purgeable_volatile_pmap; 146 } 147 148 phys_footprint = vm_info.phys_footprint; 149 } 150 } 151 152 #if defined(WITH_FBS) || defined(WITH_BKS) 153 if (scanType & eProfileMemoryCap) { 154 memorystatus_memlimit_properties_t memlimit_properties; 155 memset(&memlimit_properties, 0, sizeof(memlimit_properties)); 156 if (memorystatus_control(MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES, pid, 0, &memlimit_properties, sizeof(memlimit_properties)) == 0) { 157 memory_cap = memlimit_properties.memlimit_active; 158 } 159 } 160 #endif 161 162 return true; 163 } 164 165 nub_size_t MachVMMemory::Read(task_t task, nub_addr_t address, void *data, 166 nub_size_t data_count) { 167 if (data == NULL || data_count == 0) 168 return 0; 169 170 nub_size_t total_bytes_read = 0; 171 nub_addr_t curr_addr = address; 172 uint8_t *curr_data = (uint8_t *)data; 173 while (total_bytes_read < data_count) { 174 mach_vm_size_t curr_size = 175 MaxBytesLeftInPage(task, curr_addr, data_count - total_bytes_read); 176 mach_msg_type_number_t curr_bytes_read = 0; 177 vm_offset_t vm_memory = 0; 178 m_err = ::mach_vm_read(task, curr_addr, curr_size, &vm_memory, 179 &curr_bytes_read); 180 181 if (DNBLogCheckLogBit(LOG_MEMORY)) 182 m_err.LogThreaded("::mach_vm_read ( task = 0x%4.4x, addr = 0x%8.8llx, " 183 "size = %llu, data => %8.8p, dataCnt => %i )", 184 task, (uint64_t)curr_addr, (uint64_t)curr_size, 185 vm_memory, curr_bytes_read); 186 187 if (m_err.Success()) { 188 if (curr_bytes_read != curr_size) { 189 if (DNBLogCheckLogBit(LOG_MEMORY)) 190 m_err.LogThreaded( 191 "::mach_vm_read ( task = 0x%4.4x, addr = 0x%8.8llx, size = %llu, " 192 "data => %8.8p, dataCnt=>%i ) only read %u of %llu bytes", 193 task, (uint64_t)curr_addr, (uint64_t)curr_size, vm_memory, 194 curr_bytes_read, curr_bytes_read, (uint64_t)curr_size); 195 } 196 ::memcpy(curr_data, (void *)vm_memory, curr_bytes_read); 197 ::vm_deallocate(mach_task_self(), vm_memory, curr_bytes_read); 198 total_bytes_read += curr_bytes_read; 199 curr_addr += curr_bytes_read; 200 curr_data += curr_bytes_read; 201 } else { 202 break; 203 } 204 } 205 return total_bytes_read; 206 } 207 208 nub_size_t MachVMMemory::Write(task_t task, nub_addr_t address, 209 const void *data, nub_size_t data_count) { 210 MachVMRegion vmRegion(task); 211 212 nub_size_t total_bytes_written = 0; 213 nub_addr_t curr_addr = address; 214 const uint8_t *curr_data = (const uint8_t *)data; 215 216 while (total_bytes_written < data_count) { 217 if (vmRegion.GetRegionForAddress(curr_addr)) { 218 mach_vm_size_t curr_data_count = data_count - total_bytes_written; 219 mach_vm_size_t region_bytes_left = vmRegion.BytesRemaining(curr_addr); 220 if (region_bytes_left == 0) { 221 break; 222 } 223 if (curr_data_count > region_bytes_left) 224 curr_data_count = region_bytes_left; 225 226 if (vmRegion.SetProtections(curr_addr, curr_data_count, 227 VM_PROT_READ | VM_PROT_WRITE)) { 228 nub_size_t bytes_written = 229 WriteRegion(task, curr_addr, curr_data, curr_data_count); 230 if (bytes_written <= 0) { 231 // Status should have already be posted by WriteRegion... 232 break; 233 } else { 234 total_bytes_written += bytes_written; 235 curr_addr += bytes_written; 236 curr_data += bytes_written; 237 } 238 } else { 239 DNBLogThreadedIf( 240 LOG_MEMORY_PROTECTIONS, "Failed to set read/write protections on " 241 "region for address: [0x%8.8llx-0x%8.8llx)", 242 (uint64_t)curr_addr, (uint64_t)(curr_addr + curr_data_count)); 243 break; 244 } 245 } else { 246 DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS, 247 "Failed to get region for address: 0x%8.8llx", 248 (uint64_t)address); 249 break; 250 } 251 } 252 253 return total_bytes_written; 254 } 255 256 nub_size_t MachVMMemory::WriteRegion(task_t task, const nub_addr_t address, 257 const void *data, 258 const nub_size_t data_count) { 259 if (data == NULL || data_count == 0) 260 return 0; 261 262 nub_size_t total_bytes_written = 0; 263 nub_addr_t curr_addr = address; 264 const uint8_t *curr_data = (const uint8_t *)data; 265 while (total_bytes_written < data_count) { 266 mach_msg_type_number_t curr_data_count = 267 static_cast<mach_msg_type_number_t>(MaxBytesLeftInPage( 268 task, curr_addr, data_count - total_bytes_written)); 269 m_err = 270 ::mach_vm_write(task, curr_addr, (pointer_t)curr_data, curr_data_count); 271 if (DNBLogCheckLogBit(LOG_MEMORY) || m_err.Fail()) 272 m_err.LogThreaded("::mach_vm_write ( task = 0x%4.4x, addr = 0x%8.8llx, " 273 "data = %8.8p, dataCnt = %u )", 274 task, (uint64_t)curr_addr, curr_data, curr_data_count); 275 276 #if !defined(__i386__) && !defined(__x86_64__) 277 vm_machine_attribute_val_t mattr_value = MATTR_VAL_CACHE_FLUSH; 278 279 m_err = ::vm_machine_attribute(task, curr_addr, curr_data_count, 280 MATTR_CACHE, &mattr_value); 281 if (DNBLogCheckLogBit(LOG_MEMORY) || m_err.Fail()) 282 m_err.LogThreaded("::vm_machine_attribute ( task = 0x%4.4x, addr = " 283 "0x%8.8llx, size = %u, attr = MATTR_CACHE, mattr_value " 284 "=> MATTR_VAL_CACHE_FLUSH )", 285 task, (uint64_t)curr_addr, curr_data_count); 286 #endif 287 288 if (m_err.Success()) { 289 total_bytes_written += curr_data_count; 290 curr_addr += curr_data_count; 291 curr_data += curr_data_count; 292 } else { 293 break; 294 } 295 } 296 return total_bytes_written; 297 } 298