]> git.saurik.com Git - apple/xnu.git/blame_incremental - libsyscall/mach/mach_vm.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / libsyscall / mach / mach_vm.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*
30 * Make sure we don't accidentally include the external definitions of
31 * the routines we're interposing on below.
32 */
33#define _vm_map_user_
34#define _mach_vm_user_
35#include <mach/mach.h>
36#include <mach/mach_traps.h>
37#undef _vm_map_user_
38#include <mach/vm_map_internal.h>
39#undef _mach_vm_user_
40#include <mach/mach_vm_internal.h>
41
42#include "stack_logging_internal.h"
43
44malloc_logger_t *__syscall_logger = NULL; // This may get set by Libc's malloc stack logging initialization code.
45
46kern_return_t
47mach_vm_allocate(
48 mach_port_name_t target,
49 mach_vm_address_t *address,
50 mach_vm_size_t size,
51 int flags)
52{
53 kern_return_t rv;
54
55 rv = _kernelrpc_mach_vm_allocate_trap(target, address, size, flags);
56
57 if (rv == MACH_SEND_INVALID_DEST) {
58 rv = _kernelrpc_mach_vm_allocate(target, address, size, flags);
59 }
60
61 int userTagFlags = flags & VM_FLAGS_ALIAS_MASK;
62 if (__syscall_logger && rv == KERN_SUCCESS && (userTagFlags != VM_MAKE_TAG(VM_MEMORY_STACK))) {
63 __syscall_logger(stack_logging_type_vm_allocate | userTagFlags, (uintptr_t)target, (uintptr_t)size, 0, (uintptr_t)*address, 0);
64 }
65
66 return rv;
67}
68
69kern_return_t
70mach_vm_deallocate(
71 mach_port_name_t target,
72 mach_vm_address_t address,
73 mach_vm_size_t size)
74{
75 kern_return_t rv;
76
77 if (__syscall_logger) {
78 __syscall_logger(stack_logging_type_vm_deallocate, (uintptr_t)target, (uintptr_t)address, (uintptr_t)size, 0, 0);
79 }
80
81 rv = _kernelrpc_mach_vm_deallocate_trap(target, address, size);
82
83 if (rv == MACH_SEND_INVALID_DEST) {
84 rv = _kernelrpc_mach_vm_deallocate(target, address, size);
85 }
86
87 return rv;
88}
89
90kern_return_t
91mach_vm_protect(
92 mach_port_name_t task,
93 mach_vm_address_t address,
94 mach_vm_size_t size,
95 boolean_t set_maximum,
96 vm_prot_t new_protection)
97{
98 kern_return_t rv;
99
100 rv = _kernelrpc_mach_vm_protect_trap(task, address, size, set_maximum,
101 new_protection);
102
103 if (rv == MACH_SEND_INVALID_DEST) {
104 rv = _kernelrpc_mach_vm_protect(task, address, size,
105 set_maximum, new_protection);
106 }
107
108 return rv;
109}
110
111kern_return_t
112vm_allocate(
113 mach_port_name_t task,
114 vm_address_t *address,
115 vm_size_t size,
116 int flags)
117{
118 kern_return_t rv;
119 mach_vm_address_t mach_addr;
120
121 mach_addr = (mach_vm_address_t)*address;
122 rv = mach_vm_allocate(task, &mach_addr, size, flags);
123#if defined(__LP64__)
124 *address = mach_addr;
125#else
126 *address = (vm_address_t)(mach_addr & ((vm_address_t)-1));
127#endif
128
129 return rv;
130}
131
132kern_return_t
133vm_deallocate(
134 mach_port_name_t task,
135 vm_address_t address,
136 vm_size_t size)
137{
138 kern_return_t rv;
139
140 rv = mach_vm_deallocate(task, address, size);
141
142 return rv;
143}
144
145kern_return_t
146vm_protect(
147 mach_port_name_t task,
148 vm_address_t address,
149 vm_size_t size,
150 boolean_t set_maximum,
151 vm_prot_t new_protection)
152{
153 kern_return_t rv;
154
155 rv = mach_vm_protect(task, address, size, set_maximum, new_protection);
156
157 return rv;
158}
159
160kern_return_t
161mach_vm_map(
162 mach_port_name_t target,
163 mach_vm_address_t *address,
164 mach_vm_size_t size,
165 mach_vm_offset_t mask,
166 int flags,
167 mem_entry_name_port_t object,
168 memory_object_offset_t offset,
169 boolean_t copy,
170 vm_prot_t cur_protection,
171 vm_prot_t max_protection,
172 vm_inherit_t inheritance)
173{
174 kern_return_t rv = MACH_SEND_INVALID_DEST;
175
176 if (object == MEMORY_OBJECT_NULL && max_protection == VM_PROT_ALL &&
177 inheritance == VM_INHERIT_DEFAULT) {
178 rv = _kernelrpc_mach_vm_map_trap(target, address, size, mask, flags,
179 cur_protection);
180 }
181
182 if (rv == MACH_SEND_INVALID_DEST) {
183 rv = _kernelrpc_mach_vm_map(target, address, size, mask, flags, object,
184 offset, copy, cur_protection, max_protection, inheritance);
185 }
186
187 int userTagFlags = flags & VM_FLAGS_ALIAS_MASK;
188 if (__syscall_logger && rv == KERN_SUCCESS && (userTagFlags != VM_MAKE_TAG(VM_MEMORY_STACK))) {
189 int eventTypeFlags = stack_logging_type_vm_allocate | stack_logging_type_mapped_file_or_shared_mem;
190 __syscall_logger(eventTypeFlags | userTagFlags, (uintptr_t)target, (uintptr_t)size, 0, (uintptr_t)*address, 0);
191 }
192
193 return rv;
194}
195
196kern_return_t
197mach_vm_remap(
198 mach_port_name_t target,
199 mach_vm_address_t *address,
200 mach_vm_size_t size,
201 mach_vm_offset_t mask,
202 int flags,
203 mach_port_name_t src_task,
204 mach_vm_address_t src_address,
205 boolean_t copy,
206 vm_prot_t *cur_protection,
207 vm_prot_t *max_protection,
208 vm_inherit_t inheritance)
209{
210 kern_return_t rv;
211
212 rv = _kernelrpc_mach_vm_remap(target, address, size, mask, flags,
213 src_task, src_address, copy, cur_protection, max_protection,
214 inheritance);
215
216 if (__syscall_logger && rv == KERN_SUCCESS) {
217 int eventTypeFlags = stack_logging_type_vm_allocate | stack_logging_type_mapped_file_or_shared_mem;
218 int userTagFlags = flags & VM_FLAGS_ALIAS_MASK;
219 __syscall_logger(eventTypeFlags | userTagFlags, (uintptr_t)target, (uintptr_t)size, 0, (uintptr_t)*address, 0);
220 }
221
222 return rv;
223}
224
225kern_return_t
226mach_vm_remap_new(
227 mach_port_name_t target,
228 mach_vm_address_t *address,
229 mach_vm_size_t size,
230 mach_vm_offset_t mask,
231 int flags,
232 mach_port_name_t src_task,
233 mach_vm_address_t src_address,
234 boolean_t copy,
235 vm_prot_t *cur_protection,
236 vm_prot_t *max_protection,
237 vm_inherit_t inheritance)
238{
239 kern_return_t rv;
240
241 /* {max,cur}_protection is inout */
242 rv = _kernelrpc_mach_vm_remap_new(target, address, size, mask, flags,
243 src_task, src_address, copy, cur_protection, max_protection,
244 inheritance);
245
246 if (__syscall_logger && rv == KERN_SUCCESS) {
247 int eventTypeFlags = stack_logging_type_vm_allocate | stack_logging_type_mapped_file_or_shared_mem;
248 int userTagFlags = flags & VM_FLAGS_ALIAS_MASK;
249 __syscall_logger(eventTypeFlags | userTagFlags, (uintptr_t)target, (uintptr_t)size, 0, (uintptr_t)*address, 0);
250 }
251
252 return rv;
253}
254
255kern_return_t
256mach_vm_read(
257 mach_port_name_t target,
258 mach_vm_address_t address,
259 mach_vm_size_t size,
260 vm_offset_t *data,
261 mach_msg_type_number_t *dataCnt)
262{
263 kern_return_t rv;
264
265 rv = _kernelrpc_mach_vm_read(target, address, size, data, dataCnt);
266
267 if (__syscall_logger && rv == KERN_SUCCESS) {
268 int eventTypeFlags = stack_logging_type_vm_allocate | stack_logging_type_mapped_file_or_shared_mem;
269 // The target argument is the remote task from which data is being read,
270 // so pass mach_task_self() as the destination task receiving the allocation.
271 __syscall_logger(eventTypeFlags, (uintptr_t)mach_task_self(), (uintptr_t)*dataCnt, 0, *data, 0);
272 }
273
274 return rv;
275}
276
277kern_return_t
278vm_map(
279 mach_port_name_t target,
280 vm_address_t *address,
281 vm_size_t size,
282 vm_offset_t mask,
283 int flags,
284 mem_entry_name_port_t object,
285 vm_offset_t offset,
286 boolean_t copy,
287 vm_prot_t cur_protection,
288 vm_prot_t max_protection,
289 vm_inherit_t inheritance)
290{
291 kern_return_t rv;
292
293 rv = _kernelrpc_vm_map(target, address, size, mask, flags, object,
294 offset, copy, cur_protection, max_protection, inheritance);
295
296 if (__syscall_logger && rv == KERN_SUCCESS) {
297 int eventTypeFlags = stack_logging_type_vm_allocate | stack_logging_type_mapped_file_or_shared_mem;
298 int userTagFlags = flags & VM_FLAGS_ALIAS_MASK;
299 __syscall_logger(eventTypeFlags | userTagFlags, (uintptr_t)target, (uintptr_t)size, 0, (uintptr_t)*address, 0);
300 }
301
302 return rv;
303}
304
305kern_return_t
306vm_remap(
307 mach_port_name_t target,
308 vm_address_t *address,
309 vm_size_t size,
310 vm_offset_t mask,
311 int flags,
312 mach_port_name_t src_task,
313 vm_address_t src_address,
314 boolean_t copy,
315 vm_prot_t *cur_protection,
316 vm_prot_t *max_protection,
317 vm_inherit_t inheritance)
318{
319 kern_return_t rv;
320
321 rv = _kernelrpc_vm_remap(target, address, size, mask, flags,
322 src_task, src_address, copy, cur_protection, max_protection,
323 inheritance);
324
325 if (__syscall_logger) {
326 int eventTypeFlags = stack_logging_type_vm_allocate | stack_logging_type_mapped_file_or_shared_mem;
327 int userTagFlags = flags & VM_FLAGS_ALIAS_MASK;
328 __syscall_logger(eventTypeFlags | userTagFlags, (uintptr_t)target, (uintptr_t)size, 0, (uintptr_t)*address, 0);
329 }
330
331 return rv;
332}
333
334kern_return_t
335vm_remap_new(
336 mach_port_name_t target,
337 vm_address_t *address,
338 vm_size_t size,
339 vm_offset_t mask,
340 int flags,
341 mach_port_name_t src_task,
342 vm_address_t src_address,
343 boolean_t copy,
344 vm_prot_t *cur_protection,
345 vm_prot_t *max_protection,
346 vm_inherit_t inheritance)
347{
348 kern_return_t rv;
349
350 /* {max,cur}_protection is inout */
351 rv = _kernelrpc_vm_remap_new(target, address, size, mask, flags,
352 src_task, src_address, copy, cur_protection, max_protection,
353 inheritance);
354
355 if (__syscall_logger) {
356 int eventTypeFlags = stack_logging_type_vm_allocate | stack_logging_type_mapped_file_or_shared_mem;
357 int userTagFlags = flags & VM_FLAGS_ALIAS_MASK;
358 __syscall_logger(eventTypeFlags | userTagFlags, (uintptr_t)target, (uintptr_t)size, 0, (uintptr_t)*address, 0);
359 }
360
361 return rv;
362}
363
364kern_return_t
365vm_read(
366 mach_port_name_t target,
367 vm_address_t address,
368 vm_size_t size,
369 vm_offset_t *data,
370 mach_msg_type_number_t *dataCnt)
371{
372 kern_return_t rv;
373
374 rv = _kernelrpc_vm_read(target, address, size, data, dataCnt);
375
376 if (__syscall_logger && rv == KERN_SUCCESS) {
377 int eventTypeFlags = stack_logging_type_vm_allocate | stack_logging_type_mapped_file_or_shared_mem;
378 // The target argument is the remote task from which data is being read,
379 // so pass mach_task_self() as the destination task receiving the allocation.
380 __syscall_logger(eventTypeFlags, (uintptr_t)mach_task_self(), (uintptr_t)*dataCnt, 0, *data, 0);
381 }
382
383 return rv;
384}
385
386kern_return_t
387mach_vm_purgable_control(
388 mach_port_name_t target,
389 mach_vm_offset_t address,
390 vm_purgable_t control,
391 int *state)
392{
393 kern_return_t rv;
394
395 rv = _kernelrpc_mach_vm_purgable_control_trap(target, address, control, state);
396
397 if (rv == MACH_SEND_INVALID_DEST) {
398 rv = _kernelrpc_mach_vm_purgable_control(target, address, control, state);
399 }
400
401 return rv;
402}
403
404kern_return_t
405vm_purgable_control(
406 mach_port_name_t task,
407 vm_offset_t address,
408 vm_purgable_t control,
409 int *state)
410{
411 return mach_vm_purgable_control(task,
412 (mach_vm_offset_t) address,
413 control,
414 state);
415}