1 //===-- MachVMMemory.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  Created by Greg Clayton on 6/26/07.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "MachVMMemory.h"
14 #include "DNBLog.h"
15 #include "MachVMRegion.h"
16 #include <dlfcn.h>
17 #include <mach/mach_vm.h>
18 #include <mach/shared_region.h>
19 #include <sys/sysctl.h>
20 
21 #if defined(WITH_FBS) || defined(WITH_BKS)
22 extern "C" {
23 #import <System/sys/kern_memorystatus.h>
24 }
25 #endif
26 
27 static const vm_size_t kInvalidPageSize = ~0;
28 
29 MachVMMemory::MachVMMemory() : m_page_size(kInvalidPageSize), m_err(0) {}
30 
31 MachVMMemory::~MachVMMemory() {}
32 
33 nub_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     }
55 #endif
56     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;
61 }
62 
63 nub_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;
73 }
74 
75 nub_bool_t MachVMMemory::GetMemoryRegionInfo(task_t task, nub_addr_t address,
76                                              DNBRegionInfo *region_info) {
77   MachVMRegion vmRegion(task);
78 
79   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;
94     }
95     // 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;
100 
101     // Not readable, writeable or executable
102     region_info->permissions = 0;
103   }
104   return true;
105 }
106 
107 static 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)
112     return physical_memory;
113 
114   size_t len = sizeof(physical_memory);
115   sysctlbyname("hw.memsize", &physical_memory, &len, NULL, 0);
116 
117   calculated = true;
118   return physical_memory;
119 }
120 
121 nub_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,
124     uint64_t &physical_memory, uint64_t &anonymous,
125     uint64_t &phys_footprint, uint64_t &memory_cap)
126 {
127   if (scanType & eProfileHostMemory)
128     physical_memory = GetPhysicalMemory();
129 
130   if (scanType & eProfileMemory) {
131     static mach_port_t localHost = mach_host_self();
132     mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
133     host_statistics64(localHost, HOST_VM_INFO64, (host_info64_t)&vminfo,
134                       &count);
135 
136     kern_return_t kr;
137     mach_msg_type_number_t info_count;
138     task_vm_info_data_t vm_info;
139 
140     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       }
146 
147       phys_footprint = vm_info.phys_footprint;
148     }
149   }
150 
151 #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 
161   return true;
162 }
163 
164 nub_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;
168 
169   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) {
188         if (DNBLogCheckLogBit(LOG_MEMORY))
189           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;
202     }
203   }
204   return total_bytes_read;
205 }
206 
207 nub_size_t MachVMMemory::Write(task_t task, nub_addr_t address,
208                                const void *data, nub_size_t data_count) {
209   MachVMRegion vmRegion(task);
210 
211   nub_size_t total_bytes_written = 0;
212   nub_addr_t curr_addr = address;
213   const uint8_t *curr_data = (const uint8_t *)data;
214 
215   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;
224 
225       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) {
230           // Status should have already be posted by WriteRegion...
231           break;
232         } else {
233           total_bytes_written += bytes_written;
234           curr_addr += bytes_written;
235           curr_data += bytes_written;
236         }
237       } 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;
249     }
250   }
251 
252   return total_bytes_written;
253 }
254 
255 nub_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;
260 
261   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);
274 
275 #if !defined(__i386__) && !defined(__x86_64__)
276     vm_machine_attribute_val_t mattr_value = MATTR_VAL_CACHE_FLUSH;
277 
278     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);
285 #endif
286 
287     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;
293     }
294   }
295   return total_bytes_written;
296 }
297