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 "MachVMRegion.h"
16 #include "DNBLog.h"
17 #include <mach/mach_vm.h>
18 
19 MachVMMemory::MachVMMemory() :
20     m_page_size    (kInvalidPageSize),
21     m_err        (0)
22 {
23 }
24 
25 MachVMMemory::~MachVMMemory()
26 {
27 }
28 
29 nub_size_t
30 MachVMMemory::PageSize()
31 {
32     if (m_page_size == kInvalidPageSize)
33     {
34         m_err = ::host_page_size( ::mach_host_self(), &m_page_size);
35         if (m_err.Fail())
36             m_page_size = 0;
37     }
38     return m_page_size;
39 }
40 
41 nub_size_t
42 MachVMMemory::MaxBytesLeftInPage(nub_addr_t addr, nub_size_t count)
43 {
44     const nub_size_t page_size = PageSize();
45     if (page_size > 0)
46     {
47         nub_size_t page_offset = (addr % page_size);
48         nub_size_t bytes_left_in_page = page_size - page_offset;
49         if (count > bytes_left_in_page)
50             count = bytes_left_in_page;
51     }
52     return count;
53 }
54 
55 nub_bool_t
56 MachVMMemory::GetMemoryRegionInfo(task_t task, nub_addr_t address, DNBRegionInfo *region_info)
57 {
58     MachVMRegion vmRegion(task);
59 
60     if (vmRegion.GetRegionForAddress(address))
61     {
62         region_info->addr = vmRegion.StartAddress();
63         region_info->size = vmRegion.GetByteSize();
64         region_info->permissions = vmRegion.GetDNBPermissions();
65     }
66     else
67     {
68         region_info->addr = address;
69         region_info->size = 0;
70         if (vmRegion.GetError().Success())
71         {
72             // vmRegion.GetRegionForAddress() return false, indicating that "address"
73             // wasn't in a valid region, but the "vmRegion" info was successfully
74             // read from the task which means the info describes the next valid
75             // region from which we can infer the size of this invalid region
76             mach_vm_address_t start_addr = vmRegion.StartAddress();
77             if (address < start_addr)
78                 region_info->size = start_addr - address;
79         }
80         // If we can't get any infor about the size from the next region, just fill
81         // 1 in as the byte size
82         if (region_info->size == 0)
83             region_info->size = 1;
84 
85         // Not readable, writeable or executable
86         region_info->permissions = 0;
87     }
88     return true;
89 }
90 
91 nub_size_t
92 MachVMMemory::Read(task_t task, nub_addr_t address, void *data, nub_size_t data_count)
93 {
94     if (data == NULL || data_count == 0)
95         return 0;
96 
97     nub_size_t total_bytes_read = 0;
98     nub_addr_t curr_addr = address;
99     uint8_t *curr_data = (uint8_t*)data;
100     while (total_bytes_read < data_count)
101     {
102         mach_vm_size_t curr_size = MaxBytesLeftInPage(curr_addr, data_count - total_bytes_read);
103         mach_msg_type_number_t curr_bytes_read = 0;
104         vm_offset_t vm_memory = NULL;
105         m_err = ::mach_vm_read (task, curr_addr, curr_size, &vm_memory, &curr_bytes_read);
106 
107         // We end up being asked to read memory at 0x0 a lot without that being a real error, so that ends up just
108         // causing a lot of useless log spam.  Only complain on failing reads if the address is not 0x0.
109         if (DNBLogCheckLogBit(LOG_MEMORY) || (m_err.Fail() && curr_addr != 0))
110             m_err.LogThreaded("::mach_vm_read ( task = 0x%4.4x, addr = 0x%8.8llx, size = %llu, data => %8.8p, dataCnt => %i )", task, (uint64_t)curr_addr, (uint64_t)curr_size, vm_memory, curr_bytes_read);
111 
112         if (m_err.Success())
113         {
114             if (curr_bytes_read != curr_size)
115             {
116                 if (DNBLogCheckLogBit(LOG_MEMORY))
117                     m_err.LogThreaded("::mach_vm_read ( task = 0x%4.4x, addr = 0x%8.8llx, size = %llu, data => %8.8p, dataCnt=>%i ) only read %u of %llu bytes", task, (uint64_t)curr_addr, (uint64_t)curr_size, vm_memory, curr_bytes_read, curr_bytes_read, (uint64_t)curr_size);
118             }
119             ::memcpy (curr_data, (void *)vm_memory, curr_bytes_read);
120             ::vm_deallocate (mach_task_self (), vm_memory, curr_bytes_read);
121             total_bytes_read += curr_bytes_read;
122             curr_addr += curr_bytes_read;
123             curr_data += curr_bytes_read;
124         }
125         else
126         {
127             break;
128         }
129     }
130     return total_bytes_read;
131 }
132 
133 
134 nub_size_t
135 MachVMMemory::Write(task_t task, nub_addr_t address, const void *data, nub_size_t data_count)
136 {
137     MachVMRegion vmRegion(task);
138 
139     nub_size_t total_bytes_written = 0;
140     nub_addr_t curr_addr = address;
141     const uint8_t *curr_data = (const uint8_t*)data;
142 
143 
144     while (total_bytes_written < data_count)
145     {
146         if (vmRegion.GetRegionForAddress(curr_addr))
147         {
148             mach_vm_size_t curr_data_count = data_count - total_bytes_written;
149             mach_vm_size_t region_bytes_left = vmRegion.BytesRemaining(curr_addr);
150             if (region_bytes_left == 0)
151             {
152                 break;
153             }
154             if (curr_data_count > region_bytes_left)
155                 curr_data_count = region_bytes_left;
156 
157             if (vmRegion.SetProtections(curr_addr, curr_data_count, VM_PROT_READ | VM_PROT_WRITE))
158             {
159                 nub_size_t bytes_written = WriteRegion(task, curr_addr, curr_data, curr_data_count);
160                 if (bytes_written <= 0)
161                 {
162                     // Error should have already be posted by WriteRegion...
163                     break;
164                 }
165                 else
166                 {
167                     total_bytes_written += bytes_written;
168                     curr_addr += bytes_written;
169                     curr_data += bytes_written;
170                 }
171             }
172             else
173             {
174                 DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS, "Failed to set read/write protections on region for address: [0x%8.8llx-0x%8.8llx)", (uint64_t)curr_addr, (uint64_t)(curr_addr + curr_data_count));
175                 break;
176             }
177         }
178         else
179         {
180             DNBLogThreadedIf(LOG_MEMORY_PROTECTIONS, "Failed to get region for address: 0x%8.8llx", (uint64_t)address);
181             break;
182         }
183     }
184 
185     return total_bytes_written;
186 }
187 
188 
189 nub_size_t
190 MachVMMemory::WriteRegion(task_t task, const nub_addr_t address, const void *data, const nub_size_t data_count)
191 {
192     if (data == NULL || data_count == 0)
193         return 0;
194 
195     nub_size_t total_bytes_written = 0;
196     nub_addr_t curr_addr = address;
197     const uint8_t *curr_data = (const uint8_t*)data;
198     while (total_bytes_written < data_count)
199     {
200         mach_msg_type_number_t curr_data_count = MaxBytesLeftInPage(curr_addr, data_count - total_bytes_written);
201         m_err = ::mach_vm_write (task, curr_addr, (pointer_t) curr_data, curr_data_count);
202         if (DNBLogCheckLogBit(LOG_MEMORY) || m_err.Fail())
203             m_err.LogThreaded("::mach_vm_write ( task = 0x%4.4x, addr = 0x%8.8llx, data = %8.8p, dataCnt = %u )", task, (uint64_t)curr_addr, curr_data, curr_data_count);
204 
205 #if !defined (__i386__) && !defined (__x86_64__)
206         vm_machine_attribute_val_t mattr_value = MATTR_VAL_CACHE_FLUSH;
207 
208         m_err = ::vm_machine_attribute (task, curr_addr, curr_data_count, MATTR_CACHE, &mattr_value);
209         if (DNBLogCheckLogBit(LOG_MEMORY) || m_err.Fail())
210             m_err.LogThreaded("::vm_machine_attribute ( task = 0x%4.4x, addr = 0x%8.8llx, size = %u, attr = MATTR_CACHE, mattr_value => MATTR_VAL_CACHE_FLUSH )", task, (uint64_t)curr_addr, curr_data_count);
211 #endif
212 
213         if (m_err.Success())
214         {
215             total_bytes_written += curr_data_count;
216             curr_addr += curr_data_count;
217             curr_data += curr_data_count;
218         }
219         else
220         {
221             break;
222         }
223     }
224     return total_bytes_written;
225 }
226