1 /*
2 * Copyright (c) 2000-2021 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 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or [email protected]
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: vm/vm_user.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 *
62 * User-exported virtual memory functions.
63 */
64
65 /*
66 * There are three implementations of the "XXX_allocate" functionality in
67 * the kernel: mach_vm_allocate (for any task on the platform), vm_allocate
68 * (for a task with the same address space size, especially the current task),
69 * and vm32_vm_allocate (for the specific case of a 32-bit task). vm_allocate
70 * in the kernel should only be used on the kernel_task. vm32_vm_allocate only
71 * makes sense on platforms where a user task can either be 32 or 64, or the kernel
72 * task can be 32 or 64. mach_vm_allocate makes sense everywhere, and is preferred
73 * for new code.
74 *
75 * The entrypoints into the kernel are more complex. All platforms support a
76 * mach_vm_allocate-style API (subsystem 4800) which operates with the largest
77 * size types for the platform. On platforms that only support U32/K32,
78 * subsystem 4800 is all you need. On platforms that support both U32 and U64,
79 * subsystem 3800 is used disambiguate the size of parameters, and they will
80 * always be 32-bit and call into the vm32_vm_allocate APIs. On non-U32/K32 platforms,
81 * the MIG glue should never call into vm_allocate directly, because the calling
82 * task and kernel_task are unlikely to use the same size parameters
83 *
84 * New VM call implementations should be added here and to mach_vm.defs
85 * (subsystem 4800), and use mach_vm_* "wide" types.
86 */
87
88 #include <debug.h>
89
90 #include <mach/boolean.h>
91 #include <mach/kern_return.h>
92 #include <mach/mach_types.h> /* to get vm_address_t */
93 #include <mach/memory_object.h>
94 #include <mach/std_types.h> /* to get pointer_t */
95 #include <mach/upl.h>
96 #include <mach/vm_attributes.h>
97 #include <mach/vm_param.h>
98 #include <mach/vm_statistics.h>
99 #include <mach/mach_syscalls.h>
100 #include <mach/sdt.h>
101 #include <mach/memory_entry.h>
102
103 #include <mach/host_priv_server.h>
104 #include <mach/mach_vm_server.h>
105 #include <mach/memory_entry_server.h>
106 #include <mach/vm_map_server.h>
107
108 #include <kern/host.h>
109 #include <kern/kalloc.h>
110 #include <kern/task.h>
111 #include <kern/misc_protos.h>
112 #include <vm/vm_fault.h>
113 #include <vm/vm_map_internal.h>
114 #include <vm/vm_object_xnu.h>
115 #include <vm/vm_kern.h>
116 #include <vm/vm_page_internal.h>
117 #include <vm/memory_object_internal.h>
118 #include <vm/vm_pageout_internal.h>
119 #include <vm/vm_protos.h>
120 #include <vm/vm_purgeable_internal.h>
121 #include <vm/vm_memory_entry_xnu.h>
122 #include <vm/vm_kern_internal.h>
123 #include <vm/vm_iokit.h>
124 #include <vm/vm_sanitize_internal.h>
125 #if CONFIG_DEFERRED_RECLAIM
126 #include <vm/vm_reclaim_internal.h>
127 #endif /* CONFIG_DEFERRED_RECLAIM */
128 #include <vm/vm_init_xnu.h>
129
130 #include <san/kasan.h>
131
132 #include <libkern/OSDebug.h>
133 #include <IOKit/IOBSD.h>
134 #include <sys/kdebug_triage.h>
135
136 /*
137 * mach_vm_allocate allocates "zero fill" memory in the specfied
138 * map.
139 */
140 kern_return_t
mach_vm_allocate_external(vm_map_t map,mach_vm_offset_ut * addr,mach_vm_size_ut size,int flags)141 mach_vm_allocate_external(
142 vm_map_t map,
143 mach_vm_offset_ut *addr,
144 mach_vm_size_ut size,
145 int flags)
146 {
147 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
148
149 /* filter out any kernel-only flags */
150 if (flags & ~VM_FLAGS_USER_ALLOCATE) {
151 ktriage_record(thread_tid(current_thread()),
152 KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM,
153 KDBG_TRIAGE_RESERVED,
154 KDBG_TRIAGE_VM_ALLOCATE_KERNEL_BADFLAGS_ERROR),
155 KERN_INVALID_ARGUMENT /* arg */);
156 return KERN_INVALID_ARGUMENT;
157 }
158
159 vm_map_kernel_flags_set_vmflags(&vmk_flags, flags);
160
161 return mach_vm_allocate_kernel(map, addr, size, vmk_flags);
162 }
163
164 /*
165 * vm_allocate
166 * Legacy routine that allocates "zero fill" memory in the specfied
167 * map (which is limited to the same size as the kernel).
168 */
169 kern_return_t
vm_allocate_external(vm_map_t map,vm_offset_ut * addr,vm_size_ut size,int flags)170 vm_allocate_external(
171 vm_map_t map,
172 vm_offset_ut *addr,
173 vm_size_ut size,
174 int flags)
175 {
176 return mach_vm_allocate_external(map, addr, size, flags);
177 }
178
179 static __attribute__((always_inline, warn_unused_result))
180 kern_return_t
mach_vm_deallocate_sanitize(vm_map_t map,mach_vm_offset_ut start_u,mach_vm_size_ut size_u,mach_vm_offset_t * start,mach_vm_offset_t * end,mach_vm_size_t * size)181 mach_vm_deallocate_sanitize(
182 vm_map_t map,
183 mach_vm_offset_ut start_u,
184 mach_vm_size_ut size_u,
185 mach_vm_offset_t *start,
186 mach_vm_offset_t *end,
187 mach_vm_size_t *size)
188 {
189 return vm_sanitize_addr_size(start_u, size_u,
190 VM_SANITIZE_CALLER_VM_DEALLOCATE, map,
191 VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS, start,
192 end, size);
193 }
194
195 /*
196 * mach_vm_deallocate -
197 * deallocates the specified range of addresses in the
198 * specified address map.
199 */
200 kern_return_t
mach_vm_deallocate(vm_map_t map,mach_vm_offset_ut start_u,mach_vm_size_ut size_u)201 mach_vm_deallocate(
202 vm_map_t map,
203 mach_vm_offset_ut start_u,
204 mach_vm_size_ut size_u)
205 {
206 mach_vm_offset_t start, end;
207 mach_vm_size_t size;
208 kern_return_t kr;
209
210 if (map == VM_MAP_NULL) {
211 return KERN_INVALID_ARGUMENT;
212 }
213
214 kr = mach_vm_deallocate_sanitize(map,
215 start_u,
216 size_u,
217 &start,
218 &end,
219 &size);
220 if (__improbable(kr != KERN_SUCCESS)) {
221 return vm_sanitize_get_kr(kr);
222 }
223
224 return vm_map_remove_guard(map, start, end,
225 VM_MAP_REMOVE_NO_FLAGS,
226 KMEM_GUARD_NONE).kmr_return;
227 }
228
229 /*
230 * vm_deallocate -
231 * deallocates the specified range of addresses in the
232 * specified address map (limited to addresses the same
233 * size as the kernel).
234 */
235 kern_return_t
vm_deallocate(vm_map_t map,vm_offset_ut start,vm_size_ut size)236 vm_deallocate(
237 vm_map_t map,
238 vm_offset_ut start,
239 vm_size_ut size)
240 {
241 return mach_vm_deallocate(map, start, size);
242 }
243
244 /*
245 * mach_vm_inherit -
246 * Sets the inheritance of the specified range in the
247 * specified map.
248 */
249 kern_return_t
mach_vm_inherit(vm_map_t map,mach_vm_offset_ut start_u,mach_vm_size_ut size_u,vm_inherit_ut new_inheritance_u)250 mach_vm_inherit(
251 vm_map_t map,
252 mach_vm_offset_ut start_u,
253 mach_vm_size_ut size_u,
254 vm_inherit_ut new_inheritance_u)
255 {
256 if (map == VM_MAP_NULL) {
257 return KERN_INVALID_ARGUMENT;
258 }
259
260 if (VM_SANITIZE_UNSAFE_IS_ZERO(size_u)) {
261 return KERN_SUCCESS;
262 }
263
264 return vm_map_inherit(map,
265 start_u,
266 vm_sanitize_compute_ut_end(start_u, size_u),
267 new_inheritance_u);
268 }
269
270 /*
271 * vm_inherit -
272 * Sets the inheritance of the specified range in the
273 * specified map (range limited to addresses
274 */
275 kern_return_t
vm_inherit(vm_map_t map,vm_offset_ut start_u,vm_size_ut size_u,vm_inherit_ut new_inheritance_u)276 vm_inherit(
277 vm_map_t map,
278 vm_offset_ut start_u,
279 vm_size_ut size_u,
280 vm_inherit_ut new_inheritance_u)
281 {
282 return mach_vm_inherit(map, start_u, size_u, new_inheritance_u);
283 }
284
285 /*
286 * mach_vm_protect -
287 * Sets the protection of the specified range in the
288 * specified map.
289 */
290
291 kern_return_t
mach_vm_protect(vm_map_t map,mach_vm_address_ut start_u,mach_vm_size_ut size_u,boolean_t set_maximum,vm_prot_ut new_protection_u)292 mach_vm_protect(
293 vm_map_t map,
294 mach_vm_address_ut start_u,
295 mach_vm_size_ut size_u,
296 boolean_t set_maximum,
297 vm_prot_ut new_protection_u)
298 {
299 if (map == VM_MAP_NULL) {
300 return KERN_INVALID_ARGUMENT;
301 }
302
303 if (VM_SANITIZE_UNSAFE_IS_ZERO(size_u)) {
304 return KERN_SUCCESS;
305 }
306
307 return vm_map_protect(map,
308 start_u,
309 vm_sanitize_compute_ut_end(start_u, size_u),
310 set_maximum,
311 new_protection_u);
312 }
313
314 /*
315 * vm_protect -
316 * Sets the protection of the specified range in the
317 * specified map. Addressability of the range limited
318 * to the same size as the kernel.
319 */
320
321 kern_return_t
vm_protect(vm_map_t map,vm_offset_ut start_u,vm_size_ut size_u,boolean_t set_maximum,vm_prot_ut new_protection_u)322 vm_protect(
323 vm_map_t map,
324 vm_offset_ut start_u,
325 vm_size_ut size_u,
326 boolean_t set_maximum,
327 vm_prot_ut new_protection_u)
328 {
329 return mach_vm_protect(map, start_u, size_u, set_maximum, new_protection_u);
330 }
331
332 /*
333 * mach_vm_machine_attributes -
334 * Handle machine-specific attributes for a mapping, such
335 * as cachability, migrability, etc.
336 */
337 kern_return_t
mach_vm_machine_attribute(vm_map_t map,mach_vm_address_ut addr_u,mach_vm_size_ut size_u,vm_machine_attribute_t attribute,vm_machine_attribute_val_t * value)338 mach_vm_machine_attribute(
339 vm_map_t map,
340 mach_vm_address_ut addr_u,
341 mach_vm_size_ut size_u,
342 vm_machine_attribute_t attribute,
343 vm_machine_attribute_val_t *value) /* IN/OUT */
344 {
345 if (map == VM_MAP_NULL) {
346 return KERN_INVALID_ARGUMENT;
347 }
348
349 if (VM_SANITIZE_UNSAFE_IS_ZERO(size_u)) {
350 return KERN_SUCCESS;
351 }
352
353 return vm_map_machine_attribute(map,
354 addr_u,
355 vm_sanitize_compute_ut_end(addr_u, size_u),
356 attribute,
357 value);
358 }
359
360 /*
361 * vm_machine_attribute -
362 * Handle machine-specific attributes for a mapping, such
363 * as cachability, migrability, etc. Limited addressability
364 * (same range limits as for the native kernel map).
365 */
366 kern_return_t
vm_machine_attribute(vm_map_t map,vm_address_ut addr_u,vm_size_ut size_u,vm_machine_attribute_t attribute,vm_machine_attribute_val_t * value)367 vm_machine_attribute(
368 vm_map_t map,
369 vm_address_ut addr_u,
370 vm_size_ut size_u,
371 vm_machine_attribute_t attribute,
372 vm_machine_attribute_val_t *value) /* IN/OUT */
373 {
374 return mach_vm_machine_attribute(map, addr_u, size_u, attribute, value);
375 }
376
377 /*
378 * mach_vm_read -
379 * Read/copy a range from one address space and return it to the caller.
380 *
381 * It is assumed that the address for the returned memory is selected by
382 * the IPC implementation as part of receiving the reply to this call.
383 * If IPC isn't used, the caller must deal with the vm_map_copy_t object
384 * that gets returned.
385 *
386 * JMM - because of mach_msg_type_number_t, this call is limited to a
387 * single 4GB region at this time.
388 *
389 */
390 kern_return_t
mach_vm_read(vm_map_t map,mach_vm_address_ut addr,mach_vm_size_ut size,pointer_ut * data,mach_msg_type_number_t * data_size)391 mach_vm_read(
392 vm_map_t map,
393 mach_vm_address_ut addr,
394 mach_vm_size_ut size,
395 pointer_ut *data,
396 mach_msg_type_number_t *data_size)
397 {
398 kern_return_t error;
399 vm_map_copy_t ipc_address;
400
401 if (map == VM_MAP_NULL) {
402 return KERN_INVALID_ARGUMENT;
403 }
404
405 /*
406 * mach_msg_type_number_t is a signed int,
407 * make sure we do not overflow it.
408 */
409 if (!VM_SANITIZE_UNSAFE_FITS(size, mach_msg_type_number_t)) {
410 return KERN_INVALID_ARGUMENT;
411 }
412
413 error = vm_map_copyin(map, addr, size, FALSE, &ipc_address);
414
415 if (KERN_SUCCESS == error) {
416 VM_SANITIZE_UT_SET(*data, (pointer_t) ipc_address);
417 /* On success we know size was validated by vm_map_copyin. */
418 *data_size =
419 (mach_msg_type_number_t)VM_SANITIZE_UNSAFE_UNWRAP(size);
420 }
421 return error;
422 }
423
424 /*
425 * vm_read -
426 * Read/copy a range from one address space and return it to the caller.
427 * Limited addressability (same range limits as for the native kernel map).
428 *
429 * It is assumed that the address for the returned memory is selected by
430 * the IPC implementation as part of receiving the reply to this call.
431 * If IPC isn't used, the caller must deal with the vm_map_copy_t object
432 * that gets returned.
433 */
434 kern_return_t
vm_read(vm_map_t map,vm_address_ut addr,vm_size_ut size,pointer_ut * data,mach_msg_type_number_t * data_size)435 vm_read(
436 vm_map_t map,
437 vm_address_ut addr,
438 vm_size_ut size,
439 pointer_ut *data,
440 mach_msg_type_number_t *data_size)
441 {
442 return mach_vm_read(map, addr, size, data, data_size);
443 }
444
445 /*
446 * mach_vm_read_list -
447 * Read/copy a list of address ranges from specified map.
448 *
449 * MIG does not know how to deal with a returned array of
450 * vm_map_copy_t structures, so we have to do the copyout
451 * manually here.
452 */
453 kern_return_t
mach_vm_read_list(vm_map_t map,mach_vm_read_entry_t data_list,natural_t count)454 mach_vm_read_list(
455 vm_map_t map,
456 mach_vm_read_entry_t data_list,
457 natural_t count)
458 {
459 mach_msg_type_number_t i;
460 kern_return_t error;
461 vm_map_copy_t copy;
462
463 if (map == VM_MAP_NULL ||
464 count > VM_MAP_ENTRY_MAX) {
465 return KERN_INVALID_ARGUMENT;
466 }
467
468 error = KERN_SUCCESS;
469 for (i = 0; i < count; i++) {
470 vm_map_address_t map_addr;
471 vm_map_size_t map_size;
472
473 map_addr = (vm_map_address_t)(data_list[i].address);
474 map_size = (vm_map_size_t)(data_list[i].size);
475
476 if (map_size != 0) {
477 error = vm_map_copyin(map,
478 map_addr,
479 map_size,
480 FALSE, /* src_destroy */
481 ©);
482 if (KERN_SUCCESS == error) {
483 error = vm_map_copyout(
484 current_task()->map,
485 &map_addr,
486 copy);
487 if (KERN_SUCCESS == error) {
488 data_list[i].address = map_addr;
489 continue;
490 }
491 vm_map_copy_discard(copy);
492 }
493 }
494 data_list[i].address = (mach_vm_address_t)0;
495 data_list[i].size = (mach_vm_size_t)0;
496 }
497 return error;
498 }
499
500 /*
501 * vm_read_list -
502 * Read/copy a list of address ranges from specified map.
503 *
504 * MIG does not know how to deal with a returned array of
505 * vm_map_copy_t structures, so we have to do the copyout
506 * manually here.
507 *
508 * The source and destination ranges are limited to those
509 * that can be described with a vm_address_t (i.e. same
510 * size map as the kernel).
511 *
512 * JMM - If the result of the copyout is an address range
513 * that cannot be described with a vm_address_t (i.e. the
514 * caller had a larger address space but used this call
515 * anyway), it will result in a truncated address being
516 * returned (and a likely confused caller).
517 */
518
519 kern_return_t
vm_read_list(vm_map_t map,vm_read_entry_t data_list,natural_t count)520 vm_read_list(
521 vm_map_t map,
522 vm_read_entry_t data_list,
523 natural_t count)
524 {
525 mach_msg_type_number_t i;
526 kern_return_t error;
527 vm_map_copy_t copy;
528
529 if (map == VM_MAP_NULL ||
530 count > VM_MAP_ENTRY_MAX) {
531 return KERN_INVALID_ARGUMENT;
532 }
533
534 error = KERN_SUCCESS;
535 for (i = 0; i < count; i++) {
536 vm_map_address_t map_addr;
537 vm_map_size_t map_size;
538
539 map_addr = (vm_map_address_t)(data_list[i].address);
540 map_size = (vm_map_size_t)(data_list[i].size);
541
542 if (map_size != 0) {
543 error = vm_map_copyin(map,
544 map_addr,
545 map_size,
546 FALSE, /* src_destroy */
547 ©);
548 if (KERN_SUCCESS == error) {
549 error = vm_map_copyout(current_task()->map,
550 &map_addr,
551 copy);
552 if (KERN_SUCCESS == error) {
553 data_list[i].address =
554 CAST_DOWN(vm_offset_t, map_addr);
555 continue;
556 }
557 vm_map_copy_discard(copy);
558 }
559 }
560 data_list[i].address = (mach_vm_address_t)0;
561 data_list[i].size = (mach_vm_size_t)0;
562 }
563 return error;
564 }
565
566 /*
567 * mach_vm_read_overwrite -
568 * Overwrite a range of the current map with data from the specified
569 * map/address range.
570 *
571 * In making an assumption that the current thread is local, it is
572 * no longer cluster-safe without a fully supportive local proxy
573 * thread/task (but we don't support cluster's anymore so this is moot).
574 */
575
576 kern_return_t
mach_vm_read_overwrite(vm_map_t map,mach_vm_address_ut address,mach_vm_size_ut size,mach_vm_address_ut data,mach_vm_size_ut * data_size)577 mach_vm_read_overwrite(
578 vm_map_t map,
579 mach_vm_address_ut address,
580 mach_vm_size_ut size,
581 mach_vm_address_ut data,
582 mach_vm_size_ut *data_size)
583 {
584 kern_return_t error;
585 vm_map_copy_t copy;
586
587 if (map == VM_MAP_NULL) {
588 return KERN_INVALID_ARGUMENT;
589 }
590
591 error = vm_map_copyin(map, address, size, FALSE, ©);
592
593 if (KERN_SUCCESS == error) {
594 if (copy) {
595 assert(VM_SANITIZE_UNSAFE_IS_EQUAL(size, copy->size));
596 }
597
598 error = vm_map_copy_overwrite(current_thread()->map,
599 data, copy, size, FALSE);
600 if (KERN_SUCCESS == error) {
601 *data_size = size;
602 return error;
603 }
604 vm_map_copy_discard(copy);
605 }
606 return error;
607 }
608
609 /*
610 * vm_read_overwrite -
611 * Overwrite a range of the current map with data from the specified
612 * map/address range.
613 *
614 * This routine adds the additional limitation that the source and
615 * destination ranges must be describable with vm_address_t values
616 * (i.e. the same size address spaces as the kernel, or at least the
617 * the ranges are in that first portion of the respective address
618 * spaces).
619 */
620
621 kern_return_t
vm_read_overwrite(vm_map_t map,vm_address_ut address,vm_size_ut size,vm_address_ut data,vm_size_ut * data_size)622 vm_read_overwrite(
623 vm_map_t map,
624 vm_address_ut address,
625 vm_size_ut size,
626 vm_address_ut data,
627 vm_size_ut *data_size)
628 {
629 return mach_vm_read_overwrite(map, address, size, data, data_size);
630 }
631
632
633 /*
634 * mach_vm_write -
635 * Overwrite the specified address range with the data provided
636 * (from the current map).
637 */
638 kern_return_t
mach_vm_write(vm_map_t map,mach_vm_address_ut address,pointer_ut data_u,mach_msg_type_number_t size)639 mach_vm_write(
640 vm_map_t map,
641 mach_vm_address_ut address,
642 pointer_ut data_u,
643 mach_msg_type_number_t size)
644 {
645 if (map == VM_MAP_NULL) {
646 return KERN_INVALID_ARGUMENT;
647 }
648
649 /*
650 * data is created by the kernel's MIG server from a userspace buffer,
651 * so it is safe to unwrap.
652 */
653 vm_map_copy_t data = (vm_map_copy_t) VM_SANITIZE_UNSAFE_UNWRAP(data_u);
654
655 return vm_map_copy_overwrite(map, address,
656 data, size, FALSE /* interruptible XXX */);
657 }
658
659 /*
660 * vm_write -
661 * Overwrite the specified address range with the data provided
662 * (from the current map).
663 *
664 * The addressability of the range of addresses to overwrite is
665 * limited bu the use of a vm_address_t (same size as kernel map).
666 * Either the target map is also small, or the range is in the
667 * low addresses within it.
668 */
669 kern_return_t
vm_write(vm_map_t map,vm_address_ut address,pointer_ut data,mach_msg_type_number_t size)670 vm_write(
671 vm_map_t map,
672 vm_address_ut address,
673 pointer_ut data,
674 mach_msg_type_number_t size)
675 {
676 return mach_vm_write(map, address, data, size);
677 }
678
679 /*
680 * mach_vm_copy -
681 * Overwrite one range of the specified map with the contents of
682 * another range within that same map (i.e. both address ranges
683 * are "over there").
684 */
685 kern_return_t
mach_vm_copy(vm_map_t map,mach_vm_address_ut source_address,mach_vm_size_ut size,mach_vm_address_ut dest_address)686 mach_vm_copy(
687 vm_map_t map,
688 mach_vm_address_ut source_address,
689 mach_vm_size_ut size,
690 mach_vm_address_ut dest_address)
691 {
692 vm_map_copy_t copy;
693 kern_return_t kr;
694
695 if (map == VM_MAP_NULL) {
696 return KERN_INVALID_ARGUMENT;
697 }
698
699 kr = vm_map_copyin(map, source_address, size, FALSE, ©);
700
701 if (KERN_SUCCESS == kr) {
702 if (copy) {
703 assert(VM_SANITIZE_UNSAFE_IS_EQUAL(size, copy->size));
704 }
705
706 kr = vm_map_copy_overwrite(map, dest_address,
707 copy, size, FALSE /* interruptible XXX */);
708
709 if (KERN_SUCCESS != kr) {
710 vm_map_copy_discard(copy);
711 }
712 }
713 return kr;
714 }
715
716 kern_return_t
vm_copy(vm_map_t map,vm_address_ut source_address,vm_size_ut size,vm_address_ut dest_address)717 vm_copy(
718 vm_map_t map,
719 vm_address_ut source_address,
720 vm_size_ut size,
721 vm_address_ut dest_address)
722 {
723 return mach_vm_copy(map, source_address, size, dest_address);
724 }
725
726 /*
727 * mach_vm_map -
728 * Map some range of an object into an address space.
729 *
730 * The object can be one of several types of objects:
731 * NULL - anonymous memory
732 * a named entry - a range within another address space
733 * or a range within a memory object
734 * a whole memory object
735 *
736 */
737 kern_return_t
mach_vm_map_external(vm_map_t target_map,mach_vm_offset_ut * address,mach_vm_size_ut initial_size,mach_vm_offset_ut mask,int flags,ipc_port_t port,memory_object_offset_ut offset,boolean_t copy,vm_prot_ut cur_protection,vm_prot_ut max_protection,vm_inherit_ut inheritance)738 mach_vm_map_external(
739 vm_map_t target_map,
740 mach_vm_offset_ut *address,
741 mach_vm_size_ut initial_size,
742 mach_vm_offset_ut mask,
743 int flags,
744 ipc_port_t port,
745 memory_object_offset_ut offset,
746 boolean_t copy,
747 vm_prot_ut cur_protection,
748 vm_prot_ut max_protection,
749 vm_inherit_ut inheritance)
750 {
751 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
752
753 /* filter out any kernel-only flags */
754 if (flags & ~VM_FLAGS_USER_MAP) {
755 return KERN_INVALID_ARGUMENT;
756 }
757
758 vm_map_kernel_flags_set_vmflags(&vmk_flags, flags);
759 /* range_id is set by mach_vm_map_kernel */
760 return mach_vm_map_kernel(target_map, address, initial_size, mask,
761 vmk_flags, port, offset, copy,
762 cur_protection, max_protection,
763 inheritance);
764 }
765
766 /* legacy interface */
767 __attribute__((always_inline))
768 kern_return_t
vm_map_64_external(vm_map_t target_map,vm_offset_ut * address,vm_size_ut size,vm_offset_ut mask,int flags,ipc_port_t port,memory_object_offset_ut offset,boolean_t copy,vm_prot_ut cur_protection,vm_prot_ut max_protection,vm_inherit_ut inheritance)769 vm_map_64_external(
770 vm_map_t target_map,
771 vm_offset_ut *address,
772 vm_size_ut size,
773 vm_offset_ut mask,
774 int flags,
775 ipc_port_t port,
776 memory_object_offset_ut offset,
777 boolean_t copy,
778 vm_prot_ut cur_protection,
779 vm_prot_ut max_protection,
780 vm_inherit_ut inheritance)
781 {
782 return mach_vm_map_external(target_map, address,
783 size, mask, flags, port, offset, copy,
784 cur_protection, max_protection, inheritance);
785 }
786
787 /* temporary, until world build */
788 __attribute__((always_inline))
789 kern_return_t
vm_map_external(vm_map_t target_map,vm_offset_ut * address,vm_size_ut size,vm_offset_ut mask,int flags,ipc_port_t port,vm_offset_ut offset,boolean_t copy,vm_prot_ut cur_protection,vm_prot_ut max_protection,vm_inherit_ut inheritance)790 vm_map_external(
791 vm_map_t target_map,
792 vm_offset_ut *address,
793 vm_size_ut size,
794 vm_offset_ut mask,
795 int flags,
796 ipc_port_t port,
797 vm_offset_ut offset,
798 boolean_t copy,
799 vm_prot_ut cur_protection,
800 vm_prot_ut max_protection,
801 vm_inherit_ut inheritance)
802 {
803 return mach_vm_map_external(target_map, address,
804 size, mask, flags, port, offset, copy,
805 cur_protection, max_protection, inheritance);
806 }
807
808 static __attribute__((always_inline, warn_unused_result))
809 kern_return_t
mach_vm_remap_new_external_sanitize(vm_map_t target_map,vm_prot_ut cur_protection_u,vm_prot_ut max_protection_u,vm_prot_t * cur_protection,vm_prot_t * max_protection)810 mach_vm_remap_new_external_sanitize(
811 vm_map_t target_map,
812 vm_prot_ut cur_protection_u,
813 vm_prot_ut max_protection_u,
814 vm_prot_t *cur_protection,
815 vm_prot_t *max_protection)
816 {
817 return vm_sanitize_cur_and_max_prots(cur_protection_u, max_protection_u,
818 VM_SANITIZE_CALLER_VM_MAP_REMAP, target_map,
819 cur_protection, max_protection);
820 }
821
822 /*
823 * mach_vm_remap_new -
824 * Behaves like mach_vm_remap, except that VM_FLAGS_RETURN_DATA_ADDR is always set
825 * and {cur,max}_protection are in/out.
826 */
827 kern_return_t
mach_vm_remap_new_external(vm_map_t target_map,mach_vm_offset_ut * address,mach_vm_size_ut size,mach_vm_offset_ut mask,int flags,mach_port_t src_tport,mach_vm_offset_ut memory_address,boolean_t copy,vm_prot_ut * cur_protection_u,vm_prot_ut * max_protection_u,vm_inherit_ut inheritance)828 mach_vm_remap_new_external(
829 vm_map_t target_map,
830 mach_vm_offset_ut *address,
831 mach_vm_size_ut size,
832 mach_vm_offset_ut mask,
833 int flags,
834 mach_port_t src_tport,
835 mach_vm_offset_ut memory_address,
836 boolean_t copy,
837 vm_prot_ut *cur_protection_u, /* IN/OUT */
838 vm_prot_ut *max_protection_u, /* IN/OUT */
839 vm_inherit_ut inheritance)
840 {
841 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
842 vm_map_t src_map;
843 vm_prot_t cur_protection, max_protection;
844 kern_return_t kr;
845
846 if (target_map == VM_MAP_NULL) {
847 return KERN_INVALID_ARGUMENT;
848 }
849
850 /* filter out any kernel-only flags */
851 if (flags & ~VM_FLAGS_USER_REMAP) {
852 return KERN_INVALID_ARGUMENT;
853 }
854
855 vm_map_kernel_flags_set_vmflags(&vmk_flags,
856 flags | VM_FLAGS_RETURN_DATA_ADDR);
857
858 /*
859 * We don't need cur_protection here, but sanitizing it before
860 * enforcing W^X below matches historical error codes better.
861 */
862 kr = mach_vm_remap_new_external_sanitize(target_map,
863 *cur_protection_u,
864 *max_protection_u,
865 &cur_protection,
866 &max_protection);
867 if (__improbable(kr != KERN_SUCCESS)) {
868 return vm_sanitize_get_kr(kr);
869 }
870
871 if ((max_protection & (VM_PROT_WRITE | VM_PROT_EXECUTE)) ==
872 (VM_PROT_WRITE | VM_PROT_EXECUTE)) {
873 /*
874 * XXX FBDP TODO
875 * enforce target's "wx" policies
876 */
877 return KERN_PROTECTION_FAILURE;
878 }
879
880 if (copy || max_protection == VM_PROT_READ || max_protection == VM_PROT_NONE) {
881 src_map = convert_port_to_map_read(src_tport);
882 } else {
883 src_map = convert_port_to_map(src_tport);
884 }
885
886 /* range_id is set by vm_map_remap */
887 kr = vm_map_remap(target_map,
888 address,
889 size,
890 mask,
891 vmk_flags,
892 src_map,
893 memory_address,
894 copy,
895 cur_protection_u, /* IN/OUT */
896 max_protection_u, /* IN/OUT */
897 inheritance);
898
899 vm_map_deallocate(src_map);
900
901 if (kr == KERN_SUCCESS) {
902 ipc_port_release_send(src_tport); /* consume on success */
903 }
904 return kr;
905 }
906
907 /*
908 * mach_vm_remap -
909 * Remap a range of memory from one task into another,
910 * to another address range within the same task, or
911 * over top of itself (with altered permissions and/or
912 * as an in-place copy of itself).
913 */
914 kern_return_t
mach_vm_remap_external(vm_map_t target_map,mach_vm_offset_ut * address,mach_vm_size_ut size,mach_vm_offset_ut mask,int flags,vm_map_t src_map,mach_vm_offset_ut memory_address,boolean_t copy,vm_prot_ut * cur_protection,vm_prot_ut * max_protection,vm_inherit_ut inheritance)915 mach_vm_remap_external(
916 vm_map_t target_map,
917 mach_vm_offset_ut *address,
918 mach_vm_size_ut size,
919 mach_vm_offset_ut mask,
920 int flags,
921 vm_map_t src_map,
922 mach_vm_offset_ut memory_address,
923 boolean_t copy,
924 vm_prot_ut *cur_protection, /* OUT */
925 vm_prot_ut *max_protection, /* OUT */
926 vm_inherit_ut inheritance)
927 {
928 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
929
930 /* filter out any kernel-only flags */
931 if (flags & ~VM_FLAGS_USER_REMAP) {
932 return KERN_INVALID_ARGUMENT;
933 }
934
935 vm_map_kernel_flags_set_vmflags(&vmk_flags, flags);
936
937 *cur_protection = vm_sanitize_wrap_prot(VM_PROT_NONE);
938 *max_protection = vm_sanitize_wrap_prot(VM_PROT_NONE);
939 vmk_flags.vmkf_remap_legacy_mode = true;
940
941 /* range_id is set by vm_map_remap */
942 return vm_map_remap(target_map,
943 address,
944 size,
945 mask,
946 vmk_flags,
947 src_map,
948 memory_address,
949 copy,
950 cur_protection,
951 max_protection,
952 inheritance);
953 }
954
955 /*
956 * vm_remap_new -
957 * Behaves like vm_remap, except that VM_FLAGS_RETURN_DATA_ADDR is always set
958 * and {cur,max}_protection are in/out.
959 */
960 kern_return_t
vm_remap_new_external(vm_map_t target_map,vm_offset_ut * address,vm_size_ut size,vm_offset_ut mask,int flags,mach_port_t src_tport,vm_offset_ut memory_address,boolean_t copy,vm_prot_ut * cur_protection,vm_prot_ut * max_protection,vm_inherit_ut inheritance)961 vm_remap_new_external(
962 vm_map_t target_map,
963 vm_offset_ut *address,
964 vm_size_ut size,
965 vm_offset_ut mask,
966 int flags,
967 mach_port_t src_tport,
968 vm_offset_ut memory_address,
969 boolean_t copy,
970 vm_prot_ut *cur_protection, /* IN/OUT */
971 vm_prot_ut *max_protection, /* IN/OUT */
972 vm_inherit_ut inheritance)
973 {
974 return mach_vm_remap_new_external(target_map,
975 address,
976 size,
977 mask,
978 flags,
979 src_tport,
980 memory_address,
981 copy,
982 cur_protection, /* IN/OUT */
983 max_protection, /* IN/OUT */
984 inheritance);
985 }
986
987 /*
988 * vm_remap -
989 * Remap a range of memory from one task into another,
990 * to another address range within the same task, or
991 * over top of itself (with altered permissions and/or
992 * as an in-place copy of itself).
993 *
994 * The addressability of the source and target address
995 * range is limited by the size of vm_address_t (in the
996 * kernel context).
997 */
998 kern_return_t
vm_remap_external(vm_map_t target_map,vm_offset_ut * address,vm_size_ut size,vm_offset_ut mask,int flags,vm_map_t src_map,vm_offset_ut memory_address,boolean_t copy,vm_prot_ut * cur_protection,vm_prot_ut * max_protection,vm_inherit_ut inheritance)999 vm_remap_external(
1000 vm_map_t target_map,
1001 vm_offset_ut *address,
1002 vm_size_ut size,
1003 vm_offset_ut mask,
1004 int flags,
1005 vm_map_t src_map,
1006 vm_offset_ut memory_address,
1007 boolean_t copy,
1008 vm_prot_ut *cur_protection, /* OUT */
1009 vm_prot_ut *max_protection, /* OUT */
1010 vm_inherit_ut inheritance)
1011 {
1012 return mach_vm_remap_external(target_map, address,
1013 size, mask, flags, src_map, memory_address, copy,
1014 cur_protection, max_protection, inheritance);
1015 }
1016
1017 /*
1018 * NOTE: these routine (and this file) will no longer require mach_host_server.h
1019 * when mach_vm_wire and vm_wire are changed to use ledgers.
1020 */
1021 #include <mach/mach_host_server.h>
1022 /*
1023 * mach_vm_wire
1024 * Specify that the range of the virtual address space
1025 * of the target task must not cause page faults for
1026 * the indicated accesses.
1027 *
1028 * [ To unwire the pages, specify VM_PROT_NONE. ]
1029 */
1030 kern_return_t
mach_vm_wire_external(host_priv_t host_priv,vm_map_t map,mach_vm_address_ut start,mach_vm_size_ut size,vm_prot_ut access)1031 mach_vm_wire_external(
1032 host_priv_t host_priv,
1033 vm_map_t map,
1034 mach_vm_address_ut start,
1035 mach_vm_size_ut size,
1036 vm_prot_ut access)
1037 {
1038 kern_return_t rc;
1039 mach_vm_offset_ut end;
1040
1041 if (host_priv == HOST_PRIV_NULL) {
1042 return KERN_INVALID_HOST;
1043 }
1044
1045 if (map == VM_MAP_NULL) {
1046 return KERN_INVALID_TASK;
1047 }
1048
1049 end = vm_sanitize_compute_ut_end(start, size);
1050 if (VM_SANITIZE_UNSAFE_IS_ZERO(access)) {
1051 rc = vm_map_unwire_impl(map, start, end, true,
1052 VM_SANITIZE_CALLER_VM_UNWIRE_USER);
1053 } else {
1054 rc = vm_map_wire_impl(map, start, end, access,
1055 VM_KERN_MEMORY_MLOCK, true, NULL, VM_SANITIZE_CALLER_VM_WIRE_USER);
1056 }
1057
1058 return rc;
1059 }
1060
1061 /*
1062 * vm_wire -
1063 * Specify that the range of the virtual address space
1064 * of the target task must not cause page faults for
1065 * the indicated accesses.
1066 *
1067 * [ To unwire the pages, specify VM_PROT_NONE. ]
1068 */
1069 kern_return_t
vm_wire(host_priv_t host_priv,vm_map_t map,vm_offset_ut start,vm_size_ut size,vm_prot_ut access)1070 vm_wire(
1071 host_priv_t host_priv,
1072 vm_map_t map,
1073 vm_offset_ut start,
1074 vm_size_ut size,
1075 vm_prot_ut access)
1076 {
1077 return mach_vm_wire_external(host_priv, map, start, size, access);
1078 }
1079
1080 /*
1081 * vm_msync
1082 *
1083 * Synchronises the memory range specified with its backing store
1084 * image by either flushing or cleaning the contents to the appropriate
1085 * memory manager.
1086 *
1087 * interpretation of sync_flags
1088 * VM_SYNC_INVALIDATE - discard pages, only return precious
1089 * pages to manager.
1090 *
1091 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
1092 * - discard pages, write dirty or precious
1093 * pages back to memory manager.
1094 *
1095 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
1096 * - write dirty or precious pages back to
1097 * the memory manager.
1098 *
1099 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
1100 * is a hole in the region, and we would
1101 * have returned KERN_SUCCESS, return
1102 * KERN_INVALID_ADDRESS instead.
1103 *
1104 * RETURNS
1105 * KERN_INVALID_TASK Bad task parameter
1106 * KERN_INVALID_ARGUMENT both sync and async were specified.
1107 * KERN_SUCCESS The usual.
1108 * KERN_INVALID_ADDRESS There was a hole in the region.
1109 */
1110
1111 kern_return_t
mach_vm_msync(vm_map_t map,mach_vm_address_ut address_u,mach_vm_size_ut size_u,vm_sync_t sync_flags)1112 mach_vm_msync(
1113 vm_map_t map,
1114 mach_vm_address_ut address_u,
1115 mach_vm_size_ut size_u,
1116 vm_sync_t sync_flags)
1117 {
1118 if (map == VM_MAP_NULL) {
1119 return KERN_INVALID_TASK;
1120 }
1121
1122 if (VM_SANITIZE_UNSAFE_IS_ZERO(size_u)) {
1123 return KERN_SUCCESS;
1124 }
1125
1126 return vm_map_msync(map, address_u, size_u, sync_flags);
1127 }
1128
1129 /*
1130 * vm_msync
1131 *
1132 * Synchronises the memory range specified with its backing store
1133 * image by either flushing or cleaning the contents to the appropriate
1134 * memory manager.
1135 *
1136 * interpretation of sync_flags
1137 * VM_SYNC_INVALIDATE - discard pages, only return precious
1138 * pages to manager.
1139 *
1140 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
1141 * - discard pages, write dirty or precious
1142 * pages back to memory manager.
1143 *
1144 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
1145 * - write dirty or precious pages back to
1146 * the memory manager.
1147 *
1148 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
1149 * is a hole in the region, and we would
1150 * have returned KERN_SUCCESS, return
1151 * KERN_INVALID_ADDRESS instead.
1152 *
1153 * The addressability of the range is limited to that which can
1154 * be described by a vm_address_t.
1155 *
1156 * RETURNS
1157 * KERN_INVALID_TASK Bad task parameter
1158 * KERN_INVALID_ARGUMENT both sync and async were specified.
1159 * KERN_SUCCESS The usual.
1160 * KERN_INVALID_ADDRESS There was a hole in the region.
1161 */
1162
1163 kern_return_t
vm_msync(vm_map_t map,vm_address_ut address_u,vm_size_ut size_u,vm_sync_t sync_flags)1164 vm_msync(
1165 vm_map_t map,
1166 vm_address_ut address_u,
1167 vm_size_ut size_u,
1168 vm_sync_t sync_flags)
1169 {
1170 return mach_vm_msync(map, address_u, size_u, sync_flags);
1171 }
1172
1173
1174 int
vm_toggle_entry_reuse(int toggle,int * old_value)1175 vm_toggle_entry_reuse(int toggle, int *old_value)
1176 {
1177 vm_map_t map = current_map();
1178
1179 assert(!map->is_nested_map);
1180 if (toggle == VM_TOGGLE_GETVALUE && old_value != NULL) {
1181 *old_value = map->disable_vmentry_reuse;
1182 } else if (toggle == VM_TOGGLE_SET) {
1183 vm_map_entry_t map_to_entry;
1184
1185 vm_map_lock(map);
1186 vm_map_disable_hole_optimization(map);
1187 map->disable_vmentry_reuse = TRUE;
1188 __IGNORE_WCASTALIGN(map_to_entry = vm_map_to_entry(map));
1189 if (map->first_free == map_to_entry) {
1190 map->highest_entry_end = vm_map_min(map);
1191 } else {
1192 map->highest_entry_end = map->first_free->vme_end;
1193 }
1194 vm_map_unlock(map);
1195 } else if (toggle == VM_TOGGLE_CLEAR) {
1196 vm_map_lock(map);
1197 map->disable_vmentry_reuse = FALSE;
1198 vm_map_unlock(map);
1199 } else {
1200 return KERN_INVALID_ARGUMENT;
1201 }
1202
1203 return KERN_SUCCESS;
1204 }
1205
1206
1207 static __attribute__((always_inline, warn_unused_result))
1208 kern_return_t
mach_vm_behavior_set_sanitize(vm_map_t map,mach_vm_offset_ut start_u,mach_vm_size_ut size_u,vm_behavior_ut new_behavior_u,mach_vm_offset_t * start,mach_vm_offset_t * end,mach_vm_size_t * size,vm_behavior_t * new_behavior)1209 mach_vm_behavior_set_sanitize(
1210 vm_map_t map,
1211 mach_vm_offset_ut start_u,
1212 mach_vm_size_ut size_u,
1213 vm_behavior_ut new_behavior_u,
1214 mach_vm_offset_t *start,
1215 mach_vm_offset_t *end,
1216 mach_vm_size_t *size,
1217 vm_behavior_t *new_behavior)
1218 {
1219 mach_vm_offset_t align_mask;
1220 kern_return_t kr;
1221
1222 kr = vm_sanitize_behavior(new_behavior_u, VM_SANITIZE_CALLER_VM_BEHAVIOR_SET, new_behavior);
1223 if (__improbable(kr != KERN_SUCCESS)) {
1224 return kr;
1225 }
1226
1227 /* Choose alignment of addr/size based on the behavior being set. */
1228 switch (*new_behavior) {
1229 case VM_BEHAVIOR_REUSABLE:
1230 case VM_BEHAVIOR_REUSE:
1231 case VM_BEHAVIOR_CAN_REUSE:
1232 case VM_BEHAVIOR_ZERO:
1233 /*
1234 * Align to the hardware page size, to allow
1235 * malloc() to maximize the amount of re-usability,
1236 * even on systems with larger software page size.
1237 */
1238 align_mask = PAGE_MASK;
1239 break;
1240 default:
1241 align_mask = VM_MAP_PAGE_MASK(map);
1242 break;
1243 }
1244
1245 kr = vm_sanitize_addr_size(start_u, size_u, VM_SANITIZE_CALLER_VM_BEHAVIOR_SET,
1246 align_mask, map,
1247 VM_SANITIZE_FLAGS_SIZE_ZERO_SUCCEEDS,
1248 start, end, size);
1249 if (__improbable(kr != KERN_SUCCESS)) {
1250 return kr;
1251 }
1252
1253 return KERN_SUCCESS;
1254 }
1255
1256 /*
1257 * mach_vm_behavior_set
1258 *
1259 * Sets the paging behavior attribute for the specified range
1260 * in the specified map.
1261 *
1262 * This routine will fail with KERN_INVALID_ADDRESS if any address
1263 * in [start,start+size) is not a valid allocated memory region.
1264 */
1265 kern_return_t
mach_vm_behavior_set(vm_map_t map,mach_vm_offset_ut start_u,mach_vm_size_ut size_u,vm_behavior_ut new_behavior_u)1266 mach_vm_behavior_set(
1267 vm_map_t map,
1268 mach_vm_offset_ut start_u,
1269 mach_vm_size_ut size_u,
1270 vm_behavior_ut new_behavior_u)
1271 {
1272 kern_return_t kr;
1273 mach_vm_offset_t start, end;
1274 mach_vm_size_t size;
1275 vm_behavior_t new_behavior;
1276
1277 if (map == VM_MAP_NULL) {
1278 return KERN_INVALID_ARGUMENT;
1279 }
1280
1281 kr = mach_vm_behavior_set_sanitize(map,
1282 start_u, size_u, new_behavior_u,
1283 &start, &end, &size, &new_behavior);
1284 if (__improbable(kr != KERN_SUCCESS)) {
1285 return vm_sanitize_get_kr(kr);
1286 }
1287
1288 return vm_map_behavior_set(map,
1289 start,
1290 end,
1291 new_behavior);
1292 }
1293
1294 /*
1295 * vm_behavior_set
1296 *
1297 * Sets the paging behavior attribute for the specified range
1298 * in the specified map.
1299 *
1300 * This routine will fail with KERN_INVALID_ADDRESS if any address
1301 * in [start,start+size) is not a valid allocated memory region.
1302 *
1303 * This routine is potentially limited in addressibility by the
1304 * use of vm_offset_t (if the map provided is larger than the
1305 * kernel's).
1306 */
1307 kern_return_t
vm_behavior_set(vm_map_t map,vm_offset_ut start,vm_size_ut size,vm_behavior_ut new_behavior)1308 vm_behavior_set(
1309 vm_map_t map,
1310 vm_offset_ut start,
1311 vm_size_ut size,
1312 vm_behavior_ut new_behavior)
1313 {
1314 return mach_vm_behavior_set(map,
1315 start,
1316 size,
1317 new_behavior);
1318 }
1319
1320 /*
1321 * mach_vm_region:
1322 *
1323 * User call to obtain information about a region in
1324 * a task's address map. Currently, only one flavor is
1325 * supported.
1326 *
1327 * XXX The reserved and behavior fields cannot be filled
1328 * in until the vm merge from the IK is completed, and
1329 * vm_reserve is implemented.
1330 *
1331 * XXX Dependency: syscall_vm_region() also supports only one flavor.
1332 */
1333
1334 kern_return_t
mach_vm_region(vm_map_t map,mach_vm_offset_ut * address_u,mach_vm_size_ut * size_u,vm_region_flavor_t flavor,vm_region_info_t info,mach_msg_type_number_t * count,mach_port_t * object_name)1335 mach_vm_region(
1336 vm_map_t map,
1337 mach_vm_offset_ut *address_u, /* IN/OUT */
1338 mach_vm_size_ut *size_u, /* OUT */
1339 vm_region_flavor_t flavor, /* IN */
1340 vm_region_info_t info, /* OUT */
1341 mach_msg_type_number_t *count, /* IN/OUT */
1342 mach_port_t *object_name) /* OUT */
1343 {
1344 if (VM_MAP_NULL == map) {
1345 return KERN_INVALID_ARGUMENT;
1346 }
1347
1348 /* legacy conversion */
1349 if (VM_REGION_BASIC_INFO == flavor) {
1350 flavor = VM_REGION_BASIC_INFO_64;
1351 }
1352
1353 return vm_map_region(map, address_u, size_u, flavor, info, count,
1354 object_name);
1355 }
1356
1357 static inline kern_return_t
vm_region_get_kern_return(kern_return_t kr,vm_offset_ut addr_u,vm_size_ut size_u)1358 vm_region_get_kern_return(
1359 kern_return_t kr,
1360 vm_offset_ut addr_u,
1361 vm_size_ut size_u)
1362 {
1363 vm_offset_ut end_u = vm_sanitize_compute_ut_end(addr_u, size_u);
1364
1365 if (KERN_SUCCESS == kr && VM_SANITIZE_UNSAFE_UNWRAP(end_u) > VM_MAX_ADDRESS) {
1366 return KERN_INVALID_ADDRESS;
1367 }
1368 return kr;
1369 }
1370
1371 /*
1372 * vm_region_64 and vm_region:
1373 *
1374 * User call to obtain information about a region in
1375 * a task's address map. Currently, only one flavor is
1376 * supported.
1377 *
1378 * XXX The reserved and behavior fields cannot be filled
1379 * in until the vm merge from the IK is completed, and
1380 * vm_reserve is implemented.
1381 *
1382 * XXX Dependency: syscall_vm_region() also supports only one flavor.
1383 */
1384
1385 kern_return_t
vm_region_64(vm_map_t map,vm_offset_ut * address_u,vm_size_ut * size_u,vm_region_flavor_t flavor,vm_region_info_t info,mach_msg_type_number_t * count,mach_port_t * object_name)1386 vm_region_64(
1387 vm_map_t map,
1388 vm_offset_ut *address_u, /* IN/OUT */
1389 vm_size_ut *size_u, /* OUT */
1390 vm_region_flavor_t flavor, /* IN */
1391 vm_region_info_t info, /* OUT */
1392 mach_msg_type_number_t *count, /* IN/OUT */
1393 mach_port_t *object_name) /* OUT */
1394 {
1395 kern_return_t kr;
1396
1397 kr = mach_vm_region(map, address_u, size_u, flavor, info, count,
1398 object_name);
1399
1400 return vm_region_get_kern_return(kr, *address_u, *size_u);
1401 }
1402
1403 kern_return_t
vm_region(vm_map_t map,vm_address_ut * address_u,vm_size_ut * size_u,vm_region_flavor_t flavor,vm_region_info_t info,mach_msg_type_number_t * count,mach_port_t * object_name)1404 vm_region(
1405 vm_map_t map,
1406 vm_address_ut *address_u, /* IN/OUT */
1407 vm_size_ut *size_u, /* OUT */
1408 vm_region_flavor_t flavor, /* IN */
1409 vm_region_info_t info, /* OUT */
1410 mach_msg_type_number_t *count, /* IN/OUT */
1411 mach_port_t *object_name) /* OUT */
1412 {
1413 kern_return_t kr;
1414
1415 if (VM_MAP_NULL == map) {
1416 return KERN_INVALID_ARGUMENT;
1417 }
1418
1419 kr = vm_map_region(map, address_u, size_u, flavor, info, count,
1420 object_name);
1421
1422 return vm_region_get_kern_return(kr, *address_u, *size_u);
1423 }
1424
1425 /*
1426 * vm_region_recurse: A form of vm_region which follows the
1427 * submaps in a target map
1428 *
1429 */
1430 kern_return_t
mach_vm_region_recurse(vm_map_t map,mach_vm_address_ut * address_u,mach_vm_size_ut * size_u,uint32_t * depth,vm_region_recurse_info_t info,mach_msg_type_number_t * infoCnt)1431 mach_vm_region_recurse(
1432 vm_map_t map,
1433 mach_vm_address_ut *address_u,
1434 mach_vm_size_ut *size_u,
1435 uint32_t *depth,
1436 vm_region_recurse_info_t info,
1437 mach_msg_type_number_t *infoCnt)
1438 {
1439 if (VM_MAP_NULL == map) {
1440 return KERN_INVALID_ARGUMENT;
1441 }
1442
1443 return vm_map_region_recurse_64(map, address_u, size_u, depth,
1444 (vm_region_submap_info_64_t)info, infoCnt);
1445 }
1446
1447 /*
1448 * vm_region_recurse: A form of vm_region which follows the
1449 * submaps in a target map
1450 *
1451 */
1452 kern_return_t
vm_region_recurse_64(vm_map_t map,vm_address_ut * address_u,vm_size_ut * size_u,uint32_t * depth,vm_region_recurse_info_64_t info,mach_msg_type_number_t * infoCnt)1453 vm_region_recurse_64(
1454 vm_map_t map,
1455 vm_address_ut *address_u,
1456 vm_size_ut *size_u,
1457 uint32_t *depth,
1458 vm_region_recurse_info_64_t info,
1459 mach_msg_type_number_t *infoCnt)
1460 {
1461 kern_return_t kr;
1462
1463 kr = mach_vm_region_recurse(map, address_u, size_u, depth,
1464 (vm_region_recurse_info_t)info, infoCnt);
1465
1466 return vm_region_get_kern_return(kr, *address_u, *size_u);
1467 }
1468
1469 kern_return_t
vm_region_recurse(vm_map_t map,vm_offset_ut * address_u,vm_size_ut * size_u,natural_t * depth,vm_region_recurse_info_t info32,mach_msg_type_number_t * infoCnt)1470 vm_region_recurse(
1471 vm_map_t map,
1472 vm_offset_ut *address_u, /* IN/OUT */
1473 vm_size_ut *size_u, /* OUT */
1474 natural_t *depth, /* IN/OUT */
1475 vm_region_recurse_info_t info32, /* IN/OUT */
1476 mach_msg_type_number_t *infoCnt) /* IN/OUT */
1477 {
1478 vm_region_submap_info_data_64_t info64;
1479 vm_region_submap_info_t info;
1480 kern_return_t kr;
1481
1482 if (VM_MAP_NULL == map || *infoCnt < VM_REGION_SUBMAP_INFO_COUNT) {
1483 return KERN_INVALID_ARGUMENT;
1484 }
1485
1486 info = (vm_region_submap_info_t)info32;
1487 *infoCnt = VM_REGION_SUBMAP_INFO_COUNT_64;
1488
1489 kr = vm_map_region_recurse_64(map, address_u, size_u,
1490 depth, &info64, infoCnt);
1491
1492 info->protection = info64.protection;
1493 info->max_protection = info64.max_protection;
1494 info->inheritance = info64.inheritance;
1495 info->offset = (uint32_t)info64.offset; /* trouble-maker */
1496 info->user_tag = info64.user_tag;
1497 info->pages_resident = info64.pages_resident;
1498 info->pages_shared_now_private = info64.pages_shared_now_private;
1499 info->pages_swapped_out = info64.pages_swapped_out;
1500 info->pages_dirtied = info64.pages_dirtied;
1501 info->ref_count = info64.ref_count;
1502 info->shadow_depth = info64.shadow_depth;
1503 info->external_pager = info64.external_pager;
1504 info->share_mode = info64.share_mode;
1505 info->is_submap = info64.is_submap;
1506 info->behavior = info64.behavior;
1507 info->object_id = info64.object_id;
1508 info->user_wired_count = info64.user_wired_count;
1509
1510 *infoCnt = VM_REGION_SUBMAP_INFO_COUNT;
1511
1512 return vm_region_get_kern_return(kr, *address_u, *size_u);
1513 }
1514
1515 kern_return_t
mach_vm_purgable_control(vm_map_t map,mach_vm_offset_ut address_u,vm_purgable_t control,int * state)1516 mach_vm_purgable_control(
1517 vm_map_t map,
1518 mach_vm_offset_ut address_u,
1519 vm_purgable_t control,
1520 int *state)
1521 {
1522 if (VM_MAP_NULL == map) {
1523 return KERN_INVALID_ARGUMENT;
1524 }
1525
1526 switch (control) {
1527 case VM_PURGABLE_SET_STATE:
1528 case VM_PURGABLE_GET_STATE:
1529 case VM_PURGABLE_PURGE_ALL:
1530 break;
1531 case VM_PURGABLE_SET_STATE_FROM_KERNEL:
1532 default:
1533 /* not allowed from user-space */
1534 return KERN_INVALID_ARGUMENT;
1535 }
1536
1537 return vm_map_purgable_control(map, address_u, control, state);
1538 }
1539
1540 kern_return_t
mach_vm_purgable_control_external(mach_port_t target_tport,mach_vm_offset_ut address_u,vm_purgable_t control,int * state)1541 mach_vm_purgable_control_external(
1542 mach_port_t target_tport,
1543 mach_vm_offset_ut address_u,
1544 vm_purgable_t control,
1545 int *state)
1546 {
1547 vm_map_t map;
1548 kern_return_t kr;
1549
1550 if (control == VM_PURGABLE_GET_STATE) {
1551 map = convert_port_to_map_read(target_tport);
1552 } else {
1553 map = convert_port_to_map(target_tport);
1554 }
1555
1556 kr = mach_vm_purgable_control(map, address_u, control, state);
1557 vm_map_deallocate(map);
1558
1559 return kr;
1560 }
1561
1562 kern_return_t
vm_purgable_control_external(mach_port_t target_tport,vm_offset_ut address,vm_purgable_t control,int * state)1563 vm_purgable_control_external(
1564 mach_port_t target_tport,
1565 vm_offset_ut address,
1566 vm_purgable_t control,
1567 int *state)
1568 {
1569 return mach_vm_purgable_control_external(target_tport, address, control, state);
1570 }
1571
1572
1573 kern_return_t
mach_vm_page_query(vm_map_t map,mach_vm_offset_ut offset_u,int * disposition,int * ref_count)1574 mach_vm_page_query(
1575 vm_map_t map,
1576 mach_vm_offset_ut offset_u,
1577 int *disposition,
1578 int *ref_count)
1579 {
1580 kern_return_t kr;
1581 vm_page_info_basic_data_t info;
1582 mach_msg_type_number_t count;
1583
1584 if (VM_MAP_NULL == map) {
1585 return KERN_INVALID_ARGUMENT;
1586 }
1587
1588 count = VM_PAGE_INFO_BASIC_COUNT;
1589 kr = vm_map_page_info(map, offset_u, VM_PAGE_INFO_BASIC,
1590 (vm_page_info_t) &info, &count);
1591 if (kr == KERN_SUCCESS) {
1592 *disposition = info.disposition;
1593 *ref_count = info.ref_count;
1594 } else {
1595 *disposition = 0;
1596 *ref_count = 0;
1597 }
1598
1599 return kr;
1600 }
1601
1602 kern_return_t
vm_map_page_query(vm_map_t map,vm_offset_ut offset,int * disposition,int * ref_count)1603 vm_map_page_query(
1604 vm_map_t map,
1605 vm_offset_ut offset,
1606 int *disposition,
1607 int *ref_count)
1608 {
1609 return mach_vm_page_query(map, offset, disposition, ref_count);
1610 }
1611
1612 static __attribute__((always_inline, warn_unused_result))
1613 kern_return_t
mach_vm_page_range_query_sanitize(mach_vm_offset_ut address_u,mach_vm_size_ut size_u,int effective_page_mask,mach_vm_address_ut dispositions_addr_u,mach_vm_size_ut dispositions_count_u,mach_vm_offset_t * start,mach_vm_size_t * size,mach_vm_address_t * dispositions_addr,mach_vm_size_t * disp_buf_req_size)1614 mach_vm_page_range_query_sanitize(
1615 mach_vm_offset_ut address_u,
1616 mach_vm_size_ut size_u,
1617 int effective_page_mask,
1618 mach_vm_address_ut dispositions_addr_u,
1619 mach_vm_size_ut dispositions_count_u,
1620 mach_vm_offset_t *start,
1621 mach_vm_size_t *size,
1622 mach_vm_address_t *dispositions_addr,
1623 mach_vm_size_t *disp_buf_req_size)
1624 {
1625 mach_vm_offset_t end;
1626 mach_vm_size_t dispositions_count;
1627 mach_vm_address_t discard;
1628
1629 /*
1630 * There are no alignment requirements on
1631 * dispositions_addr_u/dispositions_count_u, those are derived into
1632 * inputs into copyout. So it is safe to unwrap them. We do want to
1633 * check that the range starting at dispositions_addr_u and ending
1634 * after dispositions_count_u integers is sound (i.e., doesn't wrap
1635 * around due to integer overflow).
1636 */
1637 *dispositions_addr = VM_SANITIZE_UNSAFE_UNWRAP(dispositions_addr_u);
1638 dispositions_count = VM_SANITIZE_UNSAFE_UNWRAP(dispositions_count_u);
1639 if (
1640 os_mul_overflow(
1641 dispositions_count,
1642 sizeof(int),
1643 disp_buf_req_size) ||
1644 os_add_overflow(
1645 *dispositions_addr,
1646 *disp_buf_req_size,
1647 &discard)) {
1648 return KERN_INVALID_ARGUMENT;
1649 }
1650
1651 return vm_sanitize_addr_size(address_u, size_u,
1652 VM_SANITIZE_CALLER_VM_MAP_PAGE_RANGE_QUERY,
1653 effective_page_mask,
1654 VM_SANITIZE_FLAGS_SIZE_ZERO_FALLTHROUGH, start,
1655 &end, size);
1656 }
1657
1658 kern_return_t
mach_vm_page_range_query(vm_map_t map,mach_vm_offset_ut address_u,mach_vm_size_ut size_u,mach_vm_address_ut dispositions_addr_u,mach_vm_size_ut * dispositions_count_u)1659 mach_vm_page_range_query(
1660 vm_map_t map,
1661 mach_vm_offset_ut address_u,
1662 mach_vm_size_ut size_u,
1663 mach_vm_address_ut dispositions_addr_u,
1664 mach_vm_size_ut *dispositions_count_u)
1665 {
1666 kern_return_t kr;
1667 int num_pages = 0, i = 0;
1668 mach_vm_size_t curr_sz = 0, copy_sz = 0;
1669 mach_vm_size_t disp_buf_req_size = 0, disp_buf_total_size = 0;
1670 mach_msg_type_number_t count = 0;
1671 mach_vm_address_t dispositions_addr;
1672
1673 void *info = NULL;
1674 void *local_disp = NULL;
1675 vm_map_size_t info_size = 0, local_disp_size = 0;
1676 mach_vm_offset_t start = 0;
1677 vm_map_size_t size;
1678 int effective_page_shift, effective_page_size, effective_page_mask;
1679
1680 if (map == VM_MAP_NULL || dispositions_count_u == NULL) {
1681 return KERN_INVALID_ARGUMENT;
1682 }
1683
1684 effective_page_shift = vm_self_region_page_shift_safely(map);
1685 if (effective_page_shift == -1) {
1686 return KERN_INVALID_ARGUMENT;
1687 }
1688 effective_page_size = (1 << effective_page_shift);
1689 effective_page_mask = effective_page_size - 1;
1690
1691 kr = mach_vm_page_range_query_sanitize(address_u,
1692 size_u,
1693 effective_page_mask,
1694 dispositions_addr_u,
1695 *dispositions_count_u,
1696 &start,
1697 &size,
1698 &dispositions_addr,
1699 &disp_buf_req_size);
1700 if (__improbable(kr != KERN_SUCCESS)) {
1701 return vm_sanitize_get_kr(kr);
1702 }
1703
1704 if (disp_buf_req_size == 0 || size == 0) {
1705 return KERN_SUCCESS;
1706 }
1707
1708 /*
1709 * For large requests, we will go through them
1710 * MAX_PAGE_RANGE_QUERY chunk at a time.
1711 */
1712
1713 curr_sz = MIN(size, MAX_PAGE_RANGE_QUERY);
1714 num_pages = (int) (curr_sz >> effective_page_shift);
1715
1716 info_size = num_pages * sizeof(vm_page_info_basic_data_t);
1717 info = kalloc_data(info_size, Z_WAITOK);
1718
1719 local_disp_size = num_pages * sizeof(int);
1720 local_disp = kalloc_data(local_disp_size, Z_WAITOK);
1721
1722 if (info == NULL || local_disp == NULL) {
1723 kr = KERN_RESOURCE_SHORTAGE;
1724 goto out;
1725 }
1726
1727 while (size) {
1728 count = VM_PAGE_INFO_BASIC_COUNT;
1729 kr = vm_map_page_range_info_internal(
1730 map,
1731 start,
1732 vm_map_round_page(start + curr_sz, effective_page_mask),
1733 effective_page_shift,
1734 VM_PAGE_INFO_BASIC,
1735 (vm_page_info_t) info,
1736 &count);
1737
1738 assert(kr == KERN_SUCCESS);
1739
1740 for (i = 0; i < num_pages; i++) {
1741 ((int*)local_disp)[i] = ((vm_page_info_basic_t)info)[i].disposition;
1742 }
1743
1744 copy_sz = MIN(disp_buf_req_size, num_pages * sizeof(int) /* an int per page */);
1745 kr = copyout(local_disp, (mach_vm_address_t)dispositions_addr, copy_sz);
1746
1747 start += curr_sz;
1748 disp_buf_req_size -= copy_sz;
1749 disp_buf_total_size += copy_sz;
1750
1751 if (kr != 0) {
1752 break;
1753 }
1754
1755 if ((disp_buf_req_size == 0) || (curr_sz >= size)) {
1756 /*
1757 * We might have inspected the full range OR
1758 * more than it esp. if the user passed in
1759 * non-page aligned start/size and/or if we
1760 * descended into a submap. We are done here.
1761 */
1762
1763 size = 0;
1764 } else {
1765 dispositions_addr += copy_sz;
1766
1767 size -= curr_sz;
1768
1769 curr_sz = MIN(vm_map_round_page(size, effective_page_mask), MAX_PAGE_RANGE_QUERY);
1770 num_pages = (int)(curr_sz >> effective_page_shift);
1771 }
1772 }
1773
1774 VM_SANITIZE_UT_SET(
1775 *dispositions_count_u,
1776 disp_buf_total_size / sizeof(int));
1777
1778 out:
1779 kfree_data(local_disp, local_disp_size);
1780 kfree_data(info, info_size);
1781 return kr;
1782 }
1783
1784 kern_return_t
mach_vm_page_info(vm_map_t map,mach_vm_address_ut address,vm_page_info_flavor_t flavor,vm_page_info_t info,mach_msg_type_number_t * count)1785 mach_vm_page_info(
1786 vm_map_t map,
1787 mach_vm_address_ut address,
1788 vm_page_info_flavor_t flavor,
1789 vm_page_info_t info,
1790 mach_msg_type_number_t *count)
1791 {
1792 kern_return_t kr;
1793
1794 if (map == VM_MAP_NULL) {
1795 return KERN_INVALID_ARGUMENT;
1796 }
1797
1798 kr = vm_map_page_info(map, address, flavor, info, count);
1799 return kr;
1800 }
1801
1802 /*
1803 * task_wire
1804 *
1805 * Set or clear the map's wiring_required flag. This flag, if set,
1806 * will cause all future virtual memory allocation to allocate
1807 * user wired memory. Unwiring pages wired down as a result of
1808 * this routine is done with the vm_wire interface.
1809 */
1810 kern_return_t
task_wire(vm_map_t map,boolean_t must_wire __unused)1811 task_wire(
1812 vm_map_t map,
1813 boolean_t must_wire __unused)
1814 {
1815 if (map == VM_MAP_NULL) {
1816 return KERN_INVALID_ARGUMENT;
1817 }
1818
1819 return KERN_NOT_SUPPORTED;
1820 }
1821
1822 kern_return_t
vm_map_exec_lockdown(vm_map_t map)1823 vm_map_exec_lockdown(
1824 vm_map_t map)
1825 {
1826 if (map == VM_MAP_NULL) {
1827 return KERN_INVALID_ARGUMENT;
1828 }
1829
1830 vm_map_lock(map);
1831 map->map_disallow_new_exec = TRUE;
1832 vm_map_unlock(map);
1833
1834 return KERN_SUCCESS;
1835 }
1836
1837 #if XNU_PLATFORM_MacOSX
1838 /*
1839 * Now a kernel-private interface (for BootCache
1840 * use only). Need a cleaner way to create an
1841 * empty vm_map() and return a handle to it.
1842 */
1843
1844 kern_return_t
vm_region_object_create(vm_map_t target_map,vm_size_t size,ipc_port_t * object_handle)1845 vm_region_object_create(
1846 vm_map_t target_map,
1847 vm_size_t size,
1848 ipc_port_t *object_handle)
1849 {
1850 vm_named_entry_t user_entry;
1851 vm_map_t new_map;
1852
1853 user_entry = mach_memory_entry_allocate(object_handle);
1854
1855 /* Create a named object based on a submap of specified size */
1856
1857 new_map = vm_map_create_options(PMAP_NULL, VM_MAP_MIN_ADDRESS,
1858 vm_map_round_page(size, VM_MAP_PAGE_MASK(target_map)),
1859 VM_MAP_CREATE_PAGEABLE);
1860 vm_map_set_page_shift(new_map, VM_MAP_PAGE_SHIFT(target_map));
1861
1862 user_entry->backing.map = new_map;
1863 user_entry->internal = TRUE;
1864 user_entry->is_sub_map = TRUE;
1865 user_entry->offset = 0;
1866 user_entry->protection = VM_PROT_ALL;
1867 user_entry->size = size;
1868
1869 return KERN_SUCCESS;
1870 }
1871 #endif /* XNU_PLATFORM_MacOSX */
1872
1873 kern_return_t
mach_vm_deferred_reclamation_buffer_init(task_t task,mach_vm_offset_ut * address,mach_vm_size_ut size)1874 mach_vm_deferred_reclamation_buffer_init(
1875 task_t task,
1876 mach_vm_offset_ut *address,
1877 mach_vm_size_ut size)
1878 {
1879 #if CONFIG_DEFERRED_RECLAIM
1880 return vm_deferred_reclamation_buffer_init_internal(task, address, size);
1881 #else
1882 (void) task;
1883 (void) address;
1884 (void) size;
1885 (void) indices;
1886 return KERN_NOT_SUPPORTED;
1887 #endif /* CONFIG_DEFERRED_RECLAIM */
1888 }
1889
1890 kern_return_t
mach_vm_deferred_reclamation_buffer_synchronize(task_t task,mach_vm_size_ut num_entries_to_reclaim_u)1891 mach_vm_deferred_reclamation_buffer_synchronize(
1892 task_t task,
1893 mach_vm_size_ut num_entries_to_reclaim_u)
1894 {
1895 #if CONFIG_DEFERRED_RECLAIM
1896 /*
1897 * This unwrapping is safe as num_entries_to_reclaim is not to be
1898 * interpreted as the size of range of addresses.
1899 */
1900 mach_vm_size_t num_entries_to_reclaim =
1901 VM_SANITIZE_UNSAFE_UNWRAP(num_entries_to_reclaim_u);
1902 return vm_deferred_reclamation_buffer_synchronize_internal(task, num_entries_to_reclaim);
1903 #else
1904 (void) task;
1905 (void) num_entries_to_reclaim;
1906 return KERN_NOT_SUPPORTED;
1907 #endif /* CONFIG_DEFERRED_RECLAIM */
1908 }
1909
1910 kern_return_t
mach_vm_deferred_reclamation_buffer_update_reclaimable_bytes(task_t task,mach_vm_size_ut reclaimable_bytes_u)1911 mach_vm_deferred_reclamation_buffer_update_reclaimable_bytes(
1912 task_t task,
1913 mach_vm_size_ut reclaimable_bytes_u)
1914 {
1915 #if CONFIG_DEFERRED_RECLAIM
1916 /*
1917 * This unwrapping is safe as reclaimable_bytes is not to be
1918 * interpreted as the size of range of addresses.
1919 */
1920 mach_vm_size_t reclaimable_bytes =
1921 VM_SANITIZE_UNSAFE_UNWRAP(reclaimable_bytes_u);
1922 return vm_deferred_reclamation_buffer_update_reclaimable_bytes_internal(task, reclaimable_bytes);
1923 #else
1924 (void) task;
1925 (void) reclaimable_bytes;
1926 return KERN_NOT_SUPPORTED;
1927 #endif /* CONFIG_DEFERRED_RECLAIM */
1928 }
1929
1930 #if CONFIG_MAP_RANGES
1931
1932 extern void qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *));
1933
1934 static int
vm_map_user_range_cmp(const void * e1,const void * e2)1935 vm_map_user_range_cmp(const void *e1, const void *e2)
1936 {
1937 const struct vm_map_user_range *r1 = e1;
1938 const struct vm_map_user_range *r2 = e2;
1939
1940 if (r1->vmur_min_address != r2->vmur_min_address) {
1941 return r1->vmur_min_address < r2->vmur_min_address ? -1 : 1;
1942 }
1943
1944 return 0;
1945 }
1946
1947 static int
mach_vm_range_recipe_v1_cmp(const void * e1,const void * e2)1948 mach_vm_range_recipe_v1_cmp(const void *e1, const void *e2)
1949 {
1950 const mach_vm_range_recipe_v1_t *r1 = e1;
1951 const mach_vm_range_recipe_v1_t *r2 = e2;
1952
1953 if (r1->range.min_address != r2->range.min_address) {
1954 return r1->range.min_address < r2->range.min_address ? -1 : 1;
1955 }
1956
1957 return 0;
1958 }
1959
1960 static inline __result_use_check kern_return_t
mach_vm_range_create_v1_sanitize(vm_map_t map,mach_vm_range_recipe_v1_ut * recipe_u,uint32_t count,mach_vm_range_recipe_v1_t ** recipe_p)1961 mach_vm_range_create_v1_sanitize(
1962 vm_map_t map,
1963 mach_vm_range_recipe_v1_ut *recipe_u,
1964 uint32_t count,
1965 mach_vm_range_recipe_v1_t **recipe_p)
1966 {
1967 kern_return_t kr;
1968
1969 for (size_t i = 0; i < count; i++) {
1970 vm_map_offset_t start, end;
1971 vm_map_size_t size;
1972 mach_vm_range_ut * range_u = &recipe_u[i].range_u;
1973 kr = vm_sanitize_addr_end(
1974 range_u->min_address_u,
1975 range_u->max_address_u,
1976 VM_SANITIZE_CALLER_MACH_VM_RANGE_CREATE,
1977 map,
1978 VM_SANITIZE_FLAGS_SIZE_ZERO_FAILS
1979 | VM_SANITIZE_FLAGS_CHECK_ALIGNED_START
1980 | VM_SANITIZE_FLAGS_CHECK_ALIGNED_SIZE,
1981 &start, &end, &size); // Ignore return values
1982 if (__improbable(kr != KERN_SUCCESS)) {
1983 return kr;
1984 }
1985 }
1986 /*
1987 * Sanitization only checked properties of recipe_u.
1988 * We can now see it through the lens of the safe type.
1989 * The cast is undefined behavior, but of the kind VM sanitization
1990 * relies on anyway, so we don't expect this to cause issues.
1991 */
1992 *recipe_p = (mach_vm_range_recipe_v1_t *)recipe_u;
1993
1994 return KERN_SUCCESS;
1995 }
1996
1997 /*!
1998 * @function mach_vm_range_create_v1()
1999 *
2000 * @brief
2001 * Handle the backend for mach_vm_range_create() for the
2002 * MACH_VM_RANGE_FLAVOR_V1 flavor.
2003 *
2004 * @description
2005 * This call allows to create "ranges" in the map of a task
2006 * that have special semantics/policies around placement of
2007 * new allocations (in the vm_map_locate_space() sense).
2008 *
2009 * @returns
2010 * - KERN_SUCCESS on success
2011 * - KERN_INVALID_ARGUMENT for incorrect arguments
2012 * - KERN_NO_SPACE if the maximum amount of ranges would be exceeded
2013 * - KERN_MEMORY_PRESENT if any of the requested ranges
2014 * overlaps with existing ranges or allocations in the map.
2015 */
2016 static kern_return_t
mach_vm_range_create_v1(vm_map_t map,mach_vm_range_recipe_v1_ut * recipe_u,uint32_t new_count)2017 mach_vm_range_create_v1(
2018 vm_map_t map,
2019 mach_vm_range_recipe_v1_ut *recipe_u,
2020 uint32_t new_count)
2021 {
2022 mach_vm_range_recipe_v1_t *recipe;
2023 vm_map_user_range_t table;
2024 kern_return_t kr = KERN_SUCCESS;
2025 uint16_t count;
2026
2027 struct mach_vm_range void1 = {
2028 .min_address = map->default_range.max_address,
2029 .max_address = map->data_range.min_address,
2030 };
2031 struct mach_vm_range void2 = {
2032 .min_address = map->data_range.max_address,
2033 #if XNU_TARGET_OS_IOS && EXTENDED_USER_VA_SUPPORT
2034 .max_address = MACH_VM_JUMBO_ADDRESS,
2035 #else /* !XNU_TARGET_OS_IOS || !EXTENDED_USER_VA_SUPPORT */
2036 .max_address = vm_map_max(map),
2037 #endif /* XNU_TARGET_OS_IOS && EXTENDED_USER_VA_SUPPORT */
2038 };
2039
2040 kr = mach_vm_range_create_v1_sanitize(map, recipe_u, new_count, &recipe);
2041 if (__improbable(kr != KERN_SUCCESS)) {
2042 return vm_sanitize_get_kr(kr);
2043 }
2044
2045 qsort(recipe, new_count, sizeof(mach_vm_range_recipe_v1_t),
2046 mach_vm_range_recipe_v1_cmp);
2047
2048 /*
2049 * Step 1: Validate that the recipes have no intersections.
2050 */
2051
2052 for (size_t i = 0; i < new_count; i++) {
2053 mach_vm_range_t r = &recipe[i].range;
2054 mach_vm_size_t s;
2055
2056 if (recipe[i].flags) {
2057 return KERN_INVALID_ARGUMENT;
2058 }
2059
2060 static_assert(UMEM_RANGE_ID_FIXED == MACH_VM_RANGE_FIXED);
2061 switch (recipe[i].range_tag) {
2062 case MACH_VM_RANGE_FIXED:
2063 break;
2064 default:
2065 return KERN_INVALID_ARGUMENT;
2066 }
2067
2068 s = mach_vm_range_size(r);
2069 if (!mach_vm_range_contains(&void1, r->min_address, s) &&
2070 !mach_vm_range_contains(&void2, r->min_address, s)) {
2071 return KERN_INVALID_ARGUMENT;
2072 }
2073
2074 if (i > 0 && recipe[i - 1].range.max_address >
2075 recipe[i].range.min_address) {
2076 return KERN_INVALID_ARGUMENT;
2077 }
2078 }
2079
2080 vm_map_lock(map);
2081
2082 table = map->extra_ranges;
2083 count = map->extra_ranges_count;
2084
2085 if (count + new_count > VM_MAP_EXTRA_RANGES_MAX) {
2086 kr = KERN_NO_SPACE;
2087 goto out_unlock;
2088 }
2089
2090 /*
2091 * Step 2: Check that there is no intersection with existing ranges.
2092 */
2093
2094 for (size_t i = 0, j = 0; i < new_count && j < count;) {
2095 mach_vm_range_t r1 = &recipe[i].range;
2096 vm_map_user_range_t r2 = &table[j];
2097
2098 if (r1->max_address <= r2->vmur_min_address) {
2099 i++;
2100 } else if (r2->vmur_max_address <= r1->min_address) {
2101 j++;
2102 } else {
2103 kr = KERN_MEMORY_PRESENT;
2104 goto out_unlock;
2105 }
2106 }
2107
2108 /*
2109 * Step 3: commit the new ranges.
2110 */
2111
2112 static_assert(VM_MAP_EXTRA_RANGES_MAX * sizeof(struct vm_map_user_range) <=
2113 KALLOC_SAFE_ALLOC_SIZE);
2114
2115 table = krealloc_data(table,
2116 count * sizeof(struct vm_map_user_range),
2117 (count + new_count) * sizeof(struct vm_map_user_range),
2118 Z_ZERO | Z_WAITOK | Z_NOFAIL);
2119
2120 for (size_t i = 0; i < new_count; i++) {
2121 static_assert(MACH_VM_MAX_ADDRESS < (1ull << 56));
2122
2123 table[count + i] = (struct vm_map_user_range){
2124 .vmur_min_address = recipe[i].range.min_address,
2125 .vmur_max_address = recipe[i].range.max_address,
2126 .vmur_range_id = (vm_map_range_id_t)recipe[i].range_tag,
2127 };
2128 }
2129
2130 qsort(table, count + new_count,
2131 sizeof(struct vm_map_user_range), vm_map_user_range_cmp);
2132
2133 map->extra_ranges_count += new_count;
2134 map->extra_ranges = table;
2135
2136 out_unlock:
2137 vm_map_unlock(map);
2138
2139 if (kr == KERN_SUCCESS) {
2140 for (size_t i = 0; i < new_count; i++) {
2141 vm_map_kernel_flags_t vmk_flags = {
2142 .vmf_fixed = true,
2143 .vmf_overwrite = true,
2144 .vmkf_overwrite_immutable = true,
2145 .vm_tag = recipe[i].vm_tag,
2146 };
2147 __assert_only kern_return_t kr2;
2148
2149 kr2 = vm_map_enter(map, &recipe[i].range.min_address,
2150 mach_vm_range_size(&recipe[i].range),
2151 0, vmk_flags, VM_OBJECT_NULL, 0, FALSE,
2152 VM_PROT_NONE, VM_PROT_ALL,
2153 VM_INHERIT_DEFAULT);
2154 assert(kr2 == KERN_SUCCESS);
2155 }
2156 }
2157 return kr;
2158 }
2159
2160 kern_return_t
mach_vm_range_create(vm_map_t map,mach_vm_range_flavor_t flavor,mach_vm_range_recipes_raw_t recipe,natural_t size)2161 mach_vm_range_create(
2162 vm_map_t map,
2163 mach_vm_range_flavor_t flavor,
2164 mach_vm_range_recipes_raw_t recipe,
2165 natural_t size)
2166 {
2167 if (map != current_map()) {
2168 return KERN_INVALID_ARGUMENT;
2169 }
2170
2171 if (!map->uses_user_ranges) {
2172 return KERN_NOT_SUPPORTED;
2173 }
2174
2175 if (size == 0) {
2176 return KERN_SUCCESS;
2177 }
2178
2179 if (flavor == MACH_VM_RANGE_FLAVOR_V1) {
2180 mach_vm_range_recipe_v1_ut *array;
2181
2182 if (size % sizeof(mach_vm_range_recipe_v1_ut)) {
2183 return KERN_INVALID_ARGUMENT;
2184 }
2185
2186 size /= sizeof(mach_vm_range_recipe_v1_ut);
2187 if (size > VM_MAP_EXTRA_RANGES_MAX) {
2188 return KERN_NO_SPACE;
2189 }
2190
2191 array = (mach_vm_range_recipe_v1_ut *)recipe;
2192 return mach_vm_range_create_v1(map, array, size);
2193 }
2194
2195 return KERN_INVALID_ARGUMENT;
2196 }
2197
2198 #else /* !CONFIG_MAP_RANGES */
2199
2200 kern_return_t
mach_vm_range_create(vm_map_t map,mach_vm_range_flavor_t flavor,mach_vm_range_recipes_raw_t recipe,natural_t size)2201 mach_vm_range_create(
2202 vm_map_t map,
2203 mach_vm_range_flavor_t flavor,
2204 mach_vm_range_recipes_raw_t recipe,
2205 natural_t size)
2206 {
2207 #pragma unused(map, flavor, recipe, size)
2208 return KERN_NOT_SUPPORTED;
2209 }
2210
2211 #endif /* !CONFIG_MAP_RANGES */
2212
2213 /*
2214 * These symbols are looked up at runtime by vmware, VirtualBox,
2215 * despite not being exported in the symbol sets.
2216 */
2217
2218 #if defined(__x86_64__)
2219
2220 extern typeof(mach_vm_remap_external) mach_vm_remap;
2221 extern typeof(mach_vm_map_external) mach_vm_map;
2222 extern typeof(vm_map_external) vm_map;
2223
2224 kern_return_t
mach_vm_map(vm_map_t target_map,mach_vm_offset_ut * address,mach_vm_size_ut initial_size,mach_vm_offset_ut mask,int flags,ipc_port_t port,memory_object_offset_ut offset,boolean_t copy,vm_prot_ut cur_protection,vm_prot_ut max_protection,vm_inherit_ut inheritance)2225 mach_vm_map(
2226 vm_map_t target_map,
2227 mach_vm_offset_ut *address,
2228 mach_vm_size_ut initial_size,
2229 mach_vm_offset_ut mask,
2230 int flags,
2231 ipc_port_t port,
2232 memory_object_offset_ut offset,
2233 boolean_t copy,
2234 vm_prot_ut cur_protection,
2235 vm_prot_ut max_protection,
2236 vm_inherit_ut inheritance)
2237 {
2238 return mach_vm_map_external(target_map, address, initial_size, mask, flags, port,
2239 offset, copy, cur_protection, max_protection, inheritance);
2240 }
2241
2242 kern_return_t
mach_vm_remap(vm_map_t target_map,mach_vm_offset_ut * address,mach_vm_size_ut size,mach_vm_offset_ut mask,int flags,vm_map_t src_map,mach_vm_offset_ut memory_address,boolean_t copy,vm_prot_ut * cur_protection,vm_prot_ut * max_protection,vm_inherit_ut inheritance)2243 mach_vm_remap(
2244 vm_map_t target_map,
2245 mach_vm_offset_ut *address,
2246 mach_vm_size_ut size,
2247 mach_vm_offset_ut mask,
2248 int flags,
2249 vm_map_t src_map,
2250 mach_vm_offset_ut memory_address,
2251 boolean_t copy,
2252 vm_prot_ut *cur_protection, /* OUT */
2253 vm_prot_ut *max_protection, /* OUT */
2254 vm_inherit_ut inheritance)
2255 {
2256 return mach_vm_remap_external(target_map, address, size, mask, flags, src_map, memory_address,
2257 copy, cur_protection, max_protection, inheritance);
2258 }
2259
2260 kern_return_t
vm_map(vm_map_t target_map,vm_offset_ut * address,vm_size_ut size,vm_offset_ut mask,int flags,ipc_port_t port,vm_offset_ut offset,boolean_t copy,vm_prot_ut cur_protection,vm_prot_ut max_protection,vm_inherit_ut inheritance)2261 vm_map(
2262 vm_map_t target_map,
2263 vm_offset_ut *address,
2264 vm_size_ut size,
2265 vm_offset_ut mask,
2266 int flags,
2267 ipc_port_t port,
2268 vm_offset_ut offset,
2269 boolean_t copy,
2270 vm_prot_ut cur_protection,
2271 vm_prot_ut max_protection,
2272 vm_inherit_ut inheritance)
2273 {
2274 return mach_vm_map(target_map, address,
2275 size, mask, flags, port, offset, copy,
2276 cur_protection, max_protection, inheritance);
2277 }
2278
2279 #endif /* __x86_64__ */
2280