blob: 2b039c7b16cb19997819570b161e8502bd6bcc14 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- MachVMMemory.cpp ----------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Created by Greg Clayton on 6/26/07.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MachVMMemory.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "DNBLog.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000015#include "MachVMRegion.h"
16#include <dlfcn.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include <mach/mach_vm.h>
Han Ming Ongab3b8b22012-11-17 00:21:04 +000018#include <mach/shared_region.h>
Han Ming Ong8594ae82012-11-27 19:21:03 +000019#include <sys/sysctl.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
Han Ming Ong41423692018-02-28 22:18:45 +000021#if defined(WITH_FBS) || defined(WITH_BKS)
22extern "C" {
Davide Italiano2b371fb2019-10-07 20:35:22 +000023#import <System/sys/kern_memorystatus.h>
Han Ming Ong41423692018-02-28 22:18:45 +000024}
25#endif
26
Bruce Mitchenerdcad05b2015-09-01 23:45:14 +000027static const vm_size_t kInvalidPageSize = ~0;
28
Kate Stoneb9c1b512016-09-06 20:57:50 +000029MachVMMemory::MachVMMemory() : m_page_size(kInvalidPageSize), m_err(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031MachVMMemory::~MachVMMemory() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Kate Stoneb9c1b512016-09-06 20:57:50 +000033nub_size_t MachVMMemory::PageSize(task_t task) {
34 if (m_page_size == kInvalidPageSize) {
35#if defined(TASK_VM_INFO) && TASK_VM_INFO >= 22
36 if (task != TASK_NULL) {
37 kern_return_t kr;
38 mach_msg_type_number_t info_count = TASK_VM_INFO_COUNT;
39 task_vm_info_data_t vm_info;
40 kr = task_info(task, TASK_VM_INFO, (task_info_t)&vm_info, &info_count);
41 if (kr == KERN_SUCCESS) {
42 DNBLogThreadedIf(
43 LOG_TASK,
44 "MachVMMemory::PageSize task_info returned page size of 0x%x",
45 (int)vm_info.page_size);
46 m_page_size = vm_info.page_size;
47 return m_page_size;
48 } else {
49 DNBLogThreadedIf(LOG_TASK, "MachVMMemory::PageSize task_info call "
50 "failed to get page size, TASK_VM_INFO %d, "
51 "TASK_VM_INFO_COUNT %d, kern return %d",
52 TASK_VM_INFO, TASK_VM_INFO_COUNT, kr);
53 }
54 }
Jason Molendabecd6392013-04-06 07:16:15 +000055#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 m_err = ::host_page_size(::mach_host_self(), &m_page_size);
57 if (m_err.Fail())
58 m_page_size = 0;
59 }
60 return m_page_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063nub_size_t MachVMMemory::MaxBytesLeftInPage(task_t task, nub_addr_t addr,
64 nub_size_t count) {
65 const nub_size_t page_size = PageSize(task);
66 if (page_size > 0) {
67 nub_size_t page_offset = (addr % page_size);
68 nub_size_t bytes_left_in_page = page_size - page_offset;
69 if (count > bytes_left_in_page)
70 count = bytes_left_in_page;
71 }
72 return count;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075nub_bool_t MachVMMemory::GetMemoryRegionInfo(task_t task, nub_addr_t address,
76 DNBRegionInfo *region_info) {
77 MachVMRegion vmRegion(task);
Jason Molenda1f3966b2011-11-08 04:28:12 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 if (vmRegion.GetRegionForAddress(address)) {
80 region_info->addr = vmRegion.StartAddress();
81 region_info->size = vmRegion.GetByteSize();
82 region_info->permissions = vmRegion.GetDNBPermissions();
83 } else {
84 region_info->addr = address;
85 region_info->size = 0;
86 if (vmRegion.GetError().Success()) {
87 // vmRegion.GetRegionForAddress() return false, indicating that "address"
88 // wasn't in a valid region, but the "vmRegion" info was successfully
89 // read from the task which means the info describes the next valid
90 // region from which we can infer the size of this invalid region
91 mach_vm_address_t start_addr = vmRegion.StartAddress();
92 if (address < start_addr)
93 region_info->size = start_addr - address;
Greg Clayton46fb5582011-11-18 07:03:08 +000094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 // If we can't get any info about the size from the next region it means
96 // we asked about an address that was past all mappings, so the size
97 // of this region will take up all remaining address space.
98 if (region_info->size == 0)
99 region_info->size = INVALID_NUB_ADDRESS - region_info->addr;
Greg Claytonfc5dd292011-12-12 18:51:14 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 // Not readable, writeable or executable
102 region_info->permissions = 0;
103 }
104 return true;
Jason Molenda1f3966b2011-11-08 04:28:12 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107static uint64_t GetPhysicalMemory() {
108 // This doesn't change often at all. No need to poll each time.
109 static uint64_t physical_memory = 0;
110 static bool calculated = false;
111 if (calculated)
Han Ming Ong8594ae82012-11-27 19:21:03 +0000112 return physical_memory;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113
114 size_t len = sizeof(physical_memory);
115 sysctlbyname("hw.memsize", &physical_memory, &len, NULL, 0);
116
117 calculated = true;
118 return physical_memory;
Han Ming Ong8594ae82012-11-27 19:21:03 +0000119}
120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121nub_bool_t MachVMMemory::GetMemoryProfile(
122 DNBProfileDataScanType scanType, task_t task, struct task_basic_info ti,
123 cpu_type_t cputype, nub_process_t pid, vm_statistics64_data_t &vminfo,
Han Ming Ong41423692018-02-28 22:18:45 +0000124 uint64_t &physical_memory, uint64_t &anonymous,
125 uint64_t &phys_footprint, uint64_t &memory_cap)
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000126{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 if (scanType & eProfileHostMemory)
128 physical_memory = GetPhysicalMemory();
129
130 if (scanType & eProfileMemory) {
131 static mach_port_t localHost = mach_host_self();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
133 host_statistics64(localHost, HOST_VM_INFO64, (host_info64_t)&vminfo,
134 &count);
Han Ming Ong017edbd2018-02-01 21:46:40 +0000135
136 kern_return_t kr;
137 mach_msg_type_number_t info_count;
138 task_vm_info_data_t vm_info;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139
Han Ming Ong017edbd2018-02-01 21:46:40 +0000140 info_count = TASK_VM_INFO_COUNT;
141 kr = task_info(task, TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &info_count);
142 if (kr == KERN_SUCCESS) {
143 if (scanType & eProfileMemoryAnonymous) {
144 anonymous = vm_info.internal + vm_info.compressed - vm_info.purgeable_volatile_pmap;
145 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146
Han Ming Ong017edbd2018-02-01 21:46:40 +0000147 phys_footprint = vm_info.phys_footprint;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 }
149 }
150
Han Ming Ong41423692018-02-28 22:18:45 +0000151#if defined(WITH_FBS) || defined(WITH_BKS)
152 if (scanType & eProfileMemoryCap) {
153 memorystatus_memlimit_properties_t memlimit_properties;
154 memset(&memlimit_properties, 0, sizeof(memlimit_properties));
155 if (memorystatus_control(MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES, pid, 0, &memlimit_properties, sizeof(memlimit_properties)) == 0) {
156 memory_cap = memlimit_properties.memlimit_active;
157 }
158 }
159#endif
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 return true;
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164nub_size_t MachVMMemory::Read(task_t task, nub_addr_t address, void *data,
165 nub_size_t data_count) {
166 if (data == NULL || data_count == 0)
167 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 nub_size_t total_bytes_read = 0;
170 nub_addr_t curr_addr = address;
171 uint8_t *curr_data = (uint8_t *)data;
172 while (total_bytes_read < data_count) {
173 mach_vm_size_t curr_size =
174 MaxBytesLeftInPage(task, curr_addr, data_count - total_bytes_read);
175 mach_msg_type_number_t curr_bytes_read = 0;
176 vm_offset_t vm_memory = 0;
177 m_err = ::mach_vm_read(task, curr_addr, curr_size, &vm_memory,
178 &curr_bytes_read);
179
180 if (DNBLogCheckLogBit(LOG_MEMORY))
181 m_err.LogThreaded("::mach_vm_read ( task = 0x%4.4x, addr = 0x%8.8llx, "
182 "size = %llu, data => %8.8p, dataCnt => %i )",
183 task, (uint64_t)curr_addr, (uint64_t)curr_size,
184 vm_memory, curr_bytes_read);
185
186 if (m_err.Success()) {
187 if (curr_bytes_read != curr_size) {
Jim Ingham329617a2012-03-09 21:09:42 +0000188 if (DNBLogCheckLogBit(LOG_MEMORY))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 m_err.LogThreaded(
190 "::mach_vm_read ( task = 0x%4.4x, addr = 0x%8.8llx, size = %llu, "
191 "data => %8.8p, dataCnt=>%i ) only read %u of %llu bytes",
192 task, (uint64_t)curr_addr, (uint64_t)curr_size, vm_memory,
193 curr_bytes_read, curr_bytes_read, (uint64_t)curr_size);
194 }
195 ::memcpy(curr_data, (void *)vm_memory, curr_bytes_read);
196 ::vm_deallocate(mach_task_self(), vm_memory, curr_bytes_read);
197 total_bytes_read += curr_bytes_read;
198 curr_addr += curr_bytes_read;
199 curr_data += curr_bytes_read;
200 } else {
201 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 }
204 return total_bytes_read;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205}
206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207nub_size_t MachVMMemory::Write(task_t task, nub_addr_t address,
208 const void *data, nub_size_t data_count) {
209 MachVMRegion vmRegion(task);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 nub_size_t total_bytes_written = 0;
212 nub_addr_t curr_addr = address;
213 const uint8_t *curr_data = (const uint8_t *)data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 while (total_bytes_written < data_count) {
216 if (vmRegion.GetRegionForAddress(curr_addr)) {
217 mach_vm_size_t curr_data_count = data_count - total_bytes_written;
218 mach_vm_size_t region_bytes_left = vmRegion.BytesRemaining(curr_addr);
219 if (region_bytes_left == 0) {
220 break;
221 }
222 if (curr_data_count > region_bytes_left)
223 curr_data_count = region_bytes_left;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 if (vmRegion.SetProtections(curr_addr, curr_data_count,
226 VM_PROT_READ | VM_PROT_WRITE)) {
227 nub_size_t bytes_written =
228 WriteRegion(task, curr_addr, curr_data, curr_data_count);
229 if (bytes_written <= 0) {
Zachary Turner97206d52017-05-12 04:51:55 +0000230 // Status should have already be posted by WriteRegion...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 break;
232 } else {
233 total_bytes_written += bytes_written;
234 curr_addr += bytes_written;
235 curr_data += bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 } else {
238 DNBLogThreadedIf(
239 LOG_MEMORY_PROTECTIONS, "Failed to set read/write protections on "
240 "region for address: [0x%8.8llx-0x%8.8llx)",
241 (uint64_t)curr_addr, (uint64_t)(curr_addr + curr_data_count));
242 break;
243 }
244 } else {
245 DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS,
246 "Failed to get region for address: 0x%8.8llx",
247 (uint64_t)address);
248 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 return total_bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253}
254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255nub_size_t MachVMMemory::WriteRegion(task_t task, const nub_addr_t address,
256 const void *data,
257 const nub_size_t data_count) {
258 if (data == NULL || data_count == 0)
259 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 nub_size_t total_bytes_written = 0;
262 nub_addr_t curr_addr = address;
263 const uint8_t *curr_data = (const uint8_t *)data;
264 while (total_bytes_written < data_count) {
265 mach_msg_type_number_t curr_data_count =
266 static_cast<mach_msg_type_number_t>(MaxBytesLeftInPage(
267 task, curr_addr, data_count - total_bytes_written));
268 m_err =
269 ::mach_vm_write(task, curr_addr, (pointer_t)curr_data, curr_data_count);
270 if (DNBLogCheckLogBit(LOG_MEMORY) || m_err.Fail())
271 m_err.LogThreaded("::mach_vm_write ( task = 0x%4.4x, addr = 0x%8.8llx, "
272 "data = %8.8p, dataCnt = %u )",
273 task, (uint64_t)curr_addr, curr_data, curr_data_count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275#if !defined(__i386__) && !defined(__x86_64__)
276 vm_machine_attribute_val_t mattr_value = MATTR_VAL_CACHE_FLUSH;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 m_err = ::vm_machine_attribute(task, curr_addr, curr_data_count,
279 MATTR_CACHE, &mattr_value);
280 if (DNBLogCheckLogBit(LOG_MEMORY) || m_err.Fail())
281 m_err.LogThreaded("::vm_machine_attribute ( task = 0x%4.4x, addr = "
282 "0x%8.8llx, size = %u, attr = MATTR_CACHE, mattr_value "
283 "=> MATTR_VAL_CACHE_FLUSH )",
284 task, (uint64_t)curr_addr, curr_data_count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285#endif
286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 if (m_err.Success()) {
288 total_bytes_written += curr_data_count;
289 curr_addr += curr_data_count;
290 curr_data += curr_data_count;
291 } else {
292 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 }
295 return total_bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296}