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 <vm_cpm.h>
91 #include <mach/boolean.h>
92 #include <mach/kern_return.h>
93 #include <mach/mach_types.h> /* to get vm_address_t */
94 #include <mach/memory_object.h>
95 #include <mach/std_types.h> /* to get pointer_t */
96 #include <mach/upl.h>
97 #include <mach/vm_attributes.h>
98 #include <mach/vm_param.h>
99 #include <mach/vm_statistics.h>
100 #include <mach/mach_syscalls.h>
101 #include <mach/sdt.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.h>
115 #include <vm/vm_page.h>
116 #include <vm/memory_object.h>
117 #include <vm/vm_pageout.h>
118 #include <vm/vm_protos.h>
119 #include <vm/vm_purgeable_internal.h>
120 #if CONFIG_DEFERRED_RECLAIM
121 #include <vm/vm_reclaim_internal.h>
122 #endif /* CONFIG_DEFERRED_RECLAIM */
123 #include <vm/vm_init.h>
124
125 #include <san/kasan.h>
126
127 #include <libkern/OSDebug.h>
128 #include <IOKit/IOBSD.h>
129
130 #if VM_CPM
131 #include <vm/cpm.h>
132 #endif /* VM_CPM */
133
134 static void mach_memory_entry_no_senders(ipc_port_t, mach_port_mscount_t);
135
136 __attribute__((always_inline))
137 int
vm_map_kernel_flags_vmflags(vm_map_kernel_flags_t vmk_flags)138 vm_map_kernel_flags_vmflags(vm_map_kernel_flags_t vmk_flags)
139 {
140 int flags = vmk_flags.__vm_flags & VM_FLAGS_ANY_MASK;
141
142 /* in vmk flags the meaning of fixed/anywhere is inverted */
143 return flags ^ (VM_FLAGS_FIXED | VM_FLAGS_ANYWHERE);
144 }
145
146 __attribute__((always_inline, overloadable))
147 void
vm_map_kernel_flags_set_vmflags(vm_map_kernel_flags_t * vmk_flags,int vm_flags,vm_tag_t vm_tag)148 vm_map_kernel_flags_set_vmflags(
149 vm_map_kernel_flags_t *vmk_flags,
150 int vm_flags,
151 vm_tag_t vm_tag)
152 {
153 vm_flags ^= (VM_FLAGS_FIXED | VM_FLAGS_ANYWHERE);
154 vmk_flags->__vm_flags &= ~VM_FLAGS_ANY_MASK;
155 vmk_flags->__vm_flags |= (vm_flags & VM_FLAGS_ANY_MASK);
156 vmk_flags->vm_tag = vm_tag;
157 }
158
159 __attribute__((always_inline, overloadable))
160 void
vm_map_kernel_flags_set_vmflags(vm_map_kernel_flags_t * vmk_flags,int vm_flags_and_tag)161 vm_map_kernel_flags_set_vmflags(
162 vm_map_kernel_flags_t *vmk_flags,
163 int vm_flags_and_tag)
164 {
165 vm_flags_and_tag ^= (VM_FLAGS_FIXED | VM_FLAGS_ANYWHERE);
166 vmk_flags->__vm_flags &= ~VM_FLAGS_ANY_MASK;
167 vmk_flags->__vm_flags |= (vm_flags_and_tag & VM_FLAGS_ANY_MASK);
168 VM_GET_FLAGS_ALIAS(vm_flags_and_tag, vmk_flags->vm_tag);
169 }
170
171 __attribute__((always_inline))
172 void
vm_map_kernel_flags_and_vmflags(vm_map_kernel_flags_t * vmk_flags,int vm_flags_mask)173 vm_map_kernel_flags_and_vmflags(
174 vm_map_kernel_flags_t *vmk_flags,
175 int vm_flags_mask)
176 {
177 /* this function doesn't handle the inverted FIXED/ANYWHERE */
178 assert(vm_flags_mask & VM_FLAGS_ANYWHERE);
179 vmk_flags->__vm_flags &= vm_flags_mask;
180 }
181
182 bool
vm_map_kernel_flags_check_vmflags(vm_map_kernel_flags_t vmk_flags,int vm_flags_mask)183 vm_map_kernel_flags_check_vmflags(
184 vm_map_kernel_flags_t vmk_flags,
185 int vm_flags_mask)
186 {
187 int vmflags = vmk_flags.__vm_flags & VM_FLAGS_ANY_MASK;
188
189 /* Note: up to 16 still has good calling conventions */
190 static_assert(sizeof(vm_map_kernel_flags_t) == 8);
191
192 #if DEBUG || DEVELOPMENT
193 /*
194 * All of this compiles to nothing if all checks pass.
195 */
196 #define check(field, value) ({ \
197 vm_map_kernel_flags_t fl = VM_MAP_KERNEL_FLAGS_NONE; \
198 fl.__vm_flags = (value); \
199 fl.field = 0; \
200 assert(fl.__vm_flags == 0); \
201 })
202
203 /* bits 0-7 */
204 check(vmf_fixed, VM_FLAGS_ANYWHERE); // kind of a lie this is inverted
205 check(vmf_purgeable, VM_FLAGS_PURGABLE);
206 check(vmf_4gb_chunk, VM_FLAGS_4GB_CHUNK);
207 check(vmf_random_addr, VM_FLAGS_RANDOM_ADDR);
208 check(vmf_no_cache, VM_FLAGS_NO_CACHE);
209 check(vmf_resilient_codesign, VM_FLAGS_RESILIENT_CODESIGN);
210 check(vmf_resilient_media, VM_FLAGS_RESILIENT_MEDIA);
211 check(vmf_permanent, VM_FLAGS_PERMANENT);
212
213 /* bits 8-15 */
214 check(vmf_tpro, VM_FLAGS_TPRO);
215 check(vmf_overwrite, VM_FLAGS_OVERWRITE);
216
217 /* bits 16-23 */
218 check(vmf_superpage_size, VM_FLAGS_SUPERPAGE_MASK);
219 check(vmf_return_data_addr, VM_FLAGS_RETURN_DATA_ADDR);
220 check(vmf_return_4k_data_addr, VM_FLAGS_RETURN_4K_DATA_ADDR);
221
222 {
223 vm_map_kernel_flags_t fl = VM_MAP_KERNEL_FLAGS_NONE;
224
225 /* check user tags will never clip */
226 fl.vm_tag = VM_MEMORY_COUNT - 1;
227 assert(fl.vm_tag == VM_MEMORY_COUNT - 1);
228
229 /* check kernel tags will never clip */
230 fl.vm_tag = VM_MAX_TAG_VALUE - 1;
231 assert(fl.vm_tag == VM_MAX_TAG_VALUE - 1);
232 }
233
234
235 #undef check
236 #endif /* DEBUG || DEVELOPMENT */
237
238 return (vmflags & ~vm_flags_mask) == 0;
239 }
240
241 kern_return_t
242 vm_purgable_control(
243 vm_map_t map,
244 vm_offset_t address,
245 vm_purgable_t control,
246 int *state);
247
248 kern_return_t
249 mach_vm_purgable_control(
250 vm_map_t map,
251 mach_vm_offset_t address,
252 vm_purgable_t control,
253 int *state);
254
255 kern_return_t
256 mach_memory_entry_ownership(
257 ipc_port_t entry_port,
258 task_t owner,
259 int ledger_tag,
260 int ledger_flags);
261
262 IPC_KOBJECT_DEFINE(IKOT_NAMED_ENTRY,
263 .iko_op_stable = true,
264 .iko_op_no_senders = mach_memory_entry_no_senders);
265
266 /*
267 * mach_vm_allocate allocates "zero fill" memory in the specfied
268 * map.
269 */
270 kern_return_t
mach_vm_allocate_external(vm_map_t map,mach_vm_offset_t * addr,mach_vm_size_t size,int flags)271 mach_vm_allocate_external(
272 vm_map_t map,
273 mach_vm_offset_t *addr,
274 mach_vm_size_t size,
275 int flags)
276 {
277 vm_tag_t tag;
278
279 VM_GET_FLAGS_ALIAS(flags, tag);
280 return mach_vm_allocate_kernel(map, addr, size, flags, tag);
281 }
282
283 kern_return_t
mach_vm_allocate_kernel(vm_map_t map,mach_vm_offset_t * addr,mach_vm_size_t size,int flags,vm_tag_t tag)284 mach_vm_allocate_kernel(
285 vm_map_t map,
286 mach_vm_offset_t *addr,
287 mach_vm_size_t size,
288 int flags,
289 vm_tag_t tag)
290 {
291 vm_map_offset_t map_addr;
292 vm_map_size_t map_size;
293 kern_return_t result;
294 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
295
296 /* filter out any kernel-only flags */
297 if (flags & ~VM_FLAGS_USER_ALLOCATE) {
298 return KERN_INVALID_ARGUMENT;
299 }
300
301 vm_map_kernel_flags_set_vmflags(&vmk_flags, flags, tag);
302
303 if (map == VM_MAP_NULL) {
304 return KERN_INVALID_ARGUMENT;
305 }
306 if (size == 0) {
307 *addr = 0;
308 return KERN_SUCCESS;
309 }
310
311 if (vmk_flags.vmf_fixed) {
312 map_addr = vm_map_trunc_page(*addr, VM_MAP_PAGE_MASK(map));
313 } else {
314 map_addr = 0;
315 }
316 map_size = vm_map_round_page(size,
317 VM_MAP_PAGE_MASK(map));
318 if (map_size == 0) {
319 return KERN_INVALID_ARGUMENT;
320 }
321
322 vm_map_kernel_flags_update_range_id(&vmk_flags, map);
323
324 result = vm_map_enter(
325 map,
326 &map_addr,
327 map_size,
328 (vm_map_offset_t)0,
329 vmk_flags,
330 VM_OBJECT_NULL,
331 (vm_object_offset_t)0,
332 FALSE,
333 VM_PROT_DEFAULT,
334 VM_PROT_ALL,
335 VM_INHERIT_DEFAULT);
336
337 #if KASAN
338 if (result == KERN_SUCCESS && map->pmap == kernel_pmap) {
339 kasan_notify_address(map_addr, map_size);
340 }
341 #endif
342
343 *addr = map_addr;
344 return result;
345 }
346
347 /*
348 * vm_allocate
349 * Legacy routine that allocates "zero fill" memory in the specfied
350 * map (which is limited to the same size as the kernel).
351 */
352 kern_return_t
vm_allocate_external(vm_map_t map,vm_offset_t * addr,vm_size_t size,int flags)353 vm_allocate_external(
354 vm_map_t map,
355 vm_offset_t *addr,
356 vm_size_t size,
357 int flags)
358 {
359 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
360 vm_map_offset_t map_addr;
361 vm_map_size_t map_size;
362 kern_return_t result;
363
364 /* filter out any kernel-only flags */
365 if (flags & ~VM_FLAGS_USER_ALLOCATE) {
366 return KERN_INVALID_ARGUMENT;
367 }
368
369 vm_map_kernel_flags_set_vmflags(&vmk_flags, flags);
370
371 if (map == VM_MAP_NULL) {
372 return KERN_INVALID_ARGUMENT;
373 }
374 if (size == 0) {
375 *addr = 0;
376 return KERN_SUCCESS;
377 }
378
379 if (vmk_flags.vmf_fixed) {
380 map_addr = vm_map_trunc_page(*addr, VM_MAP_PAGE_MASK(map));
381 } else {
382 map_addr = 0;
383 }
384 map_size = vm_map_round_page(size,
385 VM_MAP_PAGE_MASK(map));
386 if (map_size == 0) {
387 return KERN_INVALID_ARGUMENT;
388 }
389
390 vm_map_kernel_flags_update_range_id(&vmk_flags, map);
391
392 result = vm_map_enter(
393 map,
394 &map_addr,
395 map_size,
396 (vm_map_offset_t)0,
397 vmk_flags,
398 VM_OBJECT_NULL,
399 (vm_object_offset_t)0,
400 FALSE,
401 VM_PROT_DEFAULT,
402 VM_PROT_ALL,
403 VM_INHERIT_DEFAULT);
404
405 #if KASAN
406 if (result == KERN_SUCCESS && map->pmap == kernel_pmap) {
407 kasan_notify_address(map_addr, map_size);
408 }
409 #endif
410
411 *addr = CAST_DOWN(vm_offset_t, map_addr);
412 return result;
413 }
414
415 /*
416 * mach_vm_deallocate -
417 * deallocates the specified range of addresses in the
418 * specified address map.
419 */
420 kern_return_t
mach_vm_deallocate(vm_map_t map,mach_vm_offset_t start,mach_vm_size_t size)421 mach_vm_deallocate(
422 vm_map_t map,
423 mach_vm_offset_t start,
424 mach_vm_size_t size)
425 {
426 if ((map == VM_MAP_NULL) || (start + size < start)) {
427 return KERN_INVALID_ARGUMENT;
428 }
429
430 if (size == (mach_vm_offset_t) 0) {
431 return KERN_SUCCESS;
432 }
433
434 return vm_map_remove_guard(map,
435 vm_map_trunc_page(start,
436 VM_MAP_PAGE_MASK(map)),
437 vm_map_round_page(start + size,
438 VM_MAP_PAGE_MASK(map)),
439 VM_MAP_REMOVE_NO_FLAGS,
440 KMEM_GUARD_NONE).kmr_return;
441 }
442
443 /*
444 * vm_deallocate -
445 * deallocates the specified range of addresses in the
446 * specified address map (limited to addresses the same
447 * size as the kernel).
448 */
449 kern_return_t
vm_deallocate(vm_map_t map,vm_offset_t start,vm_size_t size)450 vm_deallocate(
451 vm_map_t map,
452 vm_offset_t start,
453 vm_size_t size)
454 {
455 if ((map == VM_MAP_NULL) || (start + size < start)) {
456 return KERN_INVALID_ARGUMENT;
457 }
458
459 if (size == (vm_offset_t) 0) {
460 return KERN_SUCCESS;
461 }
462
463 return vm_map_remove_guard(map,
464 vm_map_trunc_page(start,
465 VM_MAP_PAGE_MASK(map)),
466 vm_map_round_page(start + size,
467 VM_MAP_PAGE_MASK(map)),
468 VM_MAP_REMOVE_NO_FLAGS,
469 KMEM_GUARD_NONE).kmr_return;
470 }
471
472 /*
473 * mach_vm_inherit -
474 * Sets the inheritance of the specified range in the
475 * specified map.
476 */
477 kern_return_t
mach_vm_inherit(vm_map_t map,mach_vm_offset_t start,mach_vm_size_t size,vm_inherit_t new_inheritance)478 mach_vm_inherit(
479 vm_map_t map,
480 mach_vm_offset_t start,
481 mach_vm_size_t size,
482 vm_inherit_t new_inheritance)
483 {
484 if ((map == VM_MAP_NULL) || (start + size < start) ||
485 (new_inheritance > VM_INHERIT_LAST_VALID)) {
486 return KERN_INVALID_ARGUMENT;
487 }
488
489 if (size == 0) {
490 return KERN_SUCCESS;
491 }
492
493 return vm_map_inherit(map,
494 vm_map_trunc_page(start,
495 VM_MAP_PAGE_MASK(map)),
496 vm_map_round_page(start + size,
497 VM_MAP_PAGE_MASK(map)),
498 new_inheritance);
499 }
500
501 /*
502 * vm_inherit -
503 * Sets the inheritance of the specified range in the
504 * specified map (range limited to addresses
505 */
506 kern_return_t
vm_inherit(vm_map_t map,vm_offset_t start,vm_size_t size,vm_inherit_t new_inheritance)507 vm_inherit(
508 vm_map_t map,
509 vm_offset_t start,
510 vm_size_t size,
511 vm_inherit_t new_inheritance)
512 {
513 if ((map == VM_MAP_NULL) || (start + size < start) ||
514 (new_inheritance > VM_INHERIT_LAST_VALID)) {
515 return KERN_INVALID_ARGUMENT;
516 }
517
518 if (size == 0) {
519 return KERN_SUCCESS;
520 }
521
522 return vm_map_inherit(map,
523 vm_map_trunc_page(start,
524 VM_MAP_PAGE_MASK(map)),
525 vm_map_round_page(start + size,
526 VM_MAP_PAGE_MASK(map)),
527 new_inheritance);
528 }
529
530 /*
531 * mach_vm_protect -
532 * Sets the protection of the specified range in the
533 * specified map.
534 */
535
536 kern_return_t
mach_vm_protect(vm_map_t map,mach_vm_offset_t start,mach_vm_size_t size,boolean_t set_maximum,vm_prot_t new_protection)537 mach_vm_protect(
538 vm_map_t map,
539 mach_vm_offset_t start,
540 mach_vm_size_t size,
541 boolean_t set_maximum,
542 vm_prot_t new_protection)
543 {
544 if ((map == VM_MAP_NULL) || (start + size < start) ||
545 (new_protection & ~(VM_PROT_ALL | VM_PROT_COPY))) {
546 return KERN_INVALID_ARGUMENT;
547 }
548
549 if (size == 0) {
550 return KERN_SUCCESS;
551 }
552
553 return vm_map_protect(map,
554 vm_map_trunc_page(start,
555 VM_MAP_PAGE_MASK(map)),
556 vm_map_round_page(start + size,
557 VM_MAP_PAGE_MASK(map)),
558 new_protection,
559 set_maximum);
560 }
561
562 /*
563 * vm_protect -
564 * Sets the protection of the specified range in the
565 * specified map. Addressability of the range limited
566 * to the same size as the kernel.
567 */
568
569 kern_return_t
vm_protect(vm_map_t map,vm_offset_t start,vm_size_t size,boolean_t set_maximum,vm_prot_t new_protection)570 vm_protect(
571 vm_map_t map,
572 vm_offset_t start,
573 vm_size_t size,
574 boolean_t set_maximum,
575 vm_prot_t new_protection)
576 {
577 if ((map == VM_MAP_NULL) || (start + size < start) ||
578 (new_protection & ~VM_VALID_VMPROTECT_FLAGS)
579 #if defined(__x86_64__)
580 || ((new_protection & VM_PROT_UEXEC) && !pmap_supported_feature(map->pmap, PMAP_FEAT_UEXEC))
581 #endif
582 ) {
583 return KERN_INVALID_ARGUMENT;
584 }
585
586 if (size == 0) {
587 return KERN_SUCCESS;
588 }
589
590 return vm_map_protect(map,
591 vm_map_trunc_page(start,
592 VM_MAP_PAGE_MASK(map)),
593 vm_map_round_page(start + size,
594 VM_MAP_PAGE_MASK(map)),
595 new_protection,
596 set_maximum);
597 }
598
599 /*
600 * mach_vm_machine_attributes -
601 * Handle machine-specific attributes for a mapping, such
602 * as cachability, migrability, etc.
603 */
604 kern_return_t
mach_vm_machine_attribute(vm_map_t map,mach_vm_address_t addr,mach_vm_size_t size,vm_machine_attribute_t attribute,vm_machine_attribute_val_t * value)605 mach_vm_machine_attribute(
606 vm_map_t map,
607 mach_vm_address_t addr,
608 mach_vm_size_t size,
609 vm_machine_attribute_t attribute,
610 vm_machine_attribute_val_t* value) /* IN/OUT */
611 {
612 if ((map == VM_MAP_NULL) || (addr + size < addr)) {
613 return KERN_INVALID_ARGUMENT;
614 }
615
616 if (size == 0) {
617 return KERN_SUCCESS;
618 }
619
620 return vm_map_machine_attribute(
621 map,
622 vm_map_trunc_page(addr,
623 VM_MAP_PAGE_MASK(map)),
624 vm_map_round_page(addr + size,
625 VM_MAP_PAGE_MASK(map)),
626 attribute,
627 value);
628 }
629
630 /*
631 * vm_machine_attribute -
632 * Handle machine-specific attributes for a mapping, such
633 * as cachability, migrability, etc. Limited addressability
634 * (same range limits as for the native kernel map).
635 */
636 kern_return_t
vm_machine_attribute(vm_map_t map,vm_address_t addr,vm_size_t size,vm_machine_attribute_t attribute,vm_machine_attribute_val_t * value)637 vm_machine_attribute(
638 vm_map_t map,
639 vm_address_t addr,
640 vm_size_t size,
641 vm_machine_attribute_t attribute,
642 vm_machine_attribute_val_t* value) /* IN/OUT */
643 {
644 if ((map == VM_MAP_NULL) || (addr + size < addr)) {
645 return KERN_INVALID_ARGUMENT;
646 }
647
648 if (size == 0) {
649 return KERN_SUCCESS;
650 }
651
652 return vm_map_machine_attribute(
653 map,
654 vm_map_trunc_page(addr,
655 VM_MAP_PAGE_MASK(map)),
656 vm_map_round_page(addr + size,
657 VM_MAP_PAGE_MASK(map)),
658 attribute,
659 value);
660 }
661
662 /*
663 * mach_vm_read -
664 * Read/copy a range from one address space and return it to the caller.
665 *
666 * It is assumed that the address for the returned memory is selected by
667 * the IPC implementation as part of receiving the reply to this call.
668 * If IPC isn't used, the caller must deal with the vm_map_copy_t object
669 * that gets returned.
670 *
671 * JMM - because of mach_msg_type_number_t, this call is limited to a
672 * single 4GB region at this time.
673 *
674 */
675 kern_return_t
mach_vm_read(vm_map_t map,mach_vm_address_t addr,mach_vm_size_t size,pointer_t * data,mach_msg_type_number_t * data_size)676 mach_vm_read(
677 vm_map_t map,
678 mach_vm_address_t addr,
679 mach_vm_size_t size,
680 pointer_t *data,
681 mach_msg_type_number_t *data_size)
682 {
683 kern_return_t error;
684 vm_map_copy_t ipc_address;
685
686 if (map == VM_MAP_NULL) {
687 return KERN_INVALID_ARGUMENT;
688 }
689
690 if ((mach_msg_type_number_t) size != size) {
691 return KERN_INVALID_ARGUMENT;
692 }
693
694 error = vm_map_copyin(map,
695 (vm_map_address_t)addr,
696 (vm_map_size_t)size,
697 FALSE, /* src_destroy */
698 &ipc_address);
699
700 if (KERN_SUCCESS == error) {
701 *data = (pointer_t) ipc_address;
702 *data_size = (mach_msg_type_number_t) size;
703 assert(*data_size == size);
704 }
705 return error;
706 }
707
708 /*
709 * vm_read -
710 * Read/copy a range from one address space and return it to the caller.
711 * Limited addressability (same range limits as for the native kernel map).
712 *
713 * It is assumed that the address for the returned memory is selected by
714 * the IPC implementation as part of receiving the reply to this call.
715 * If IPC isn't used, the caller must deal with the vm_map_copy_t object
716 * that gets returned.
717 */
718 kern_return_t
vm_read(vm_map_t map,vm_address_t addr,vm_size_t size,pointer_t * data,mach_msg_type_number_t * data_size)719 vm_read(
720 vm_map_t map,
721 vm_address_t addr,
722 vm_size_t size,
723 pointer_t *data,
724 mach_msg_type_number_t *data_size)
725 {
726 kern_return_t error;
727 vm_map_copy_t ipc_address;
728
729 if (map == VM_MAP_NULL) {
730 return KERN_INVALID_ARGUMENT;
731 }
732
733 mach_msg_type_number_t dsize;
734 if (os_convert_overflow(size, &dsize)) {
735 /*
736 * The kernel could handle a 64-bit "size" value, but
737 * it could not return the size of the data in "*data_size"
738 * without overflowing.
739 * Let's reject this "size" as invalid.
740 */
741 return KERN_INVALID_ARGUMENT;
742 }
743
744 error = vm_map_copyin(map,
745 (vm_map_address_t)addr,
746 (vm_map_size_t)size,
747 FALSE, /* src_destroy */
748 &ipc_address);
749
750 if (KERN_SUCCESS == error) {
751 *data = (pointer_t) ipc_address;
752 *data_size = dsize;
753 assert(*data_size == size);
754 }
755 return error;
756 }
757
758 /*
759 * mach_vm_read_list -
760 * Read/copy a list of address ranges from specified map.
761 *
762 * MIG does not know how to deal with a returned array of
763 * vm_map_copy_t structures, so we have to do the copyout
764 * manually here.
765 */
766 kern_return_t
mach_vm_read_list(vm_map_t map,mach_vm_read_entry_t data_list,natural_t count)767 mach_vm_read_list(
768 vm_map_t map,
769 mach_vm_read_entry_t data_list,
770 natural_t count)
771 {
772 mach_msg_type_number_t i;
773 kern_return_t error;
774 vm_map_copy_t copy;
775
776 if (map == VM_MAP_NULL ||
777 count > VM_MAP_ENTRY_MAX) {
778 return KERN_INVALID_ARGUMENT;
779 }
780
781 error = KERN_SUCCESS;
782 for (i = 0; i < count; i++) {
783 vm_map_address_t map_addr;
784 vm_map_size_t map_size;
785
786 map_addr = (vm_map_address_t)(data_list[i].address);
787 map_size = (vm_map_size_t)(data_list[i].size);
788
789 if (map_size != 0) {
790 error = vm_map_copyin(map,
791 map_addr,
792 map_size,
793 FALSE, /* src_destroy */
794 ©);
795 if (KERN_SUCCESS == error) {
796 error = vm_map_copyout(
797 current_task()->map,
798 &map_addr,
799 copy);
800 if (KERN_SUCCESS == error) {
801 data_list[i].address = map_addr;
802 continue;
803 }
804 vm_map_copy_discard(copy);
805 }
806 }
807 data_list[i].address = (mach_vm_address_t)0;
808 data_list[i].size = (mach_vm_size_t)0;
809 }
810 return error;
811 }
812
813 /*
814 * vm_read_list -
815 * Read/copy a list of address ranges from specified map.
816 *
817 * MIG does not know how to deal with a returned array of
818 * vm_map_copy_t structures, so we have to do the copyout
819 * manually here.
820 *
821 * The source and destination ranges are limited to those
822 * that can be described with a vm_address_t (i.e. same
823 * size map as the kernel).
824 *
825 * JMM - If the result of the copyout is an address range
826 * that cannot be described with a vm_address_t (i.e. the
827 * caller had a larger address space but used this call
828 * anyway), it will result in a truncated address being
829 * returned (and a likely confused caller).
830 */
831
832 kern_return_t
vm_read_list(vm_map_t map,vm_read_entry_t data_list,natural_t count)833 vm_read_list(
834 vm_map_t map,
835 vm_read_entry_t data_list,
836 natural_t count)
837 {
838 mach_msg_type_number_t i;
839 kern_return_t error;
840 vm_map_copy_t copy;
841
842 if (map == VM_MAP_NULL ||
843 count > VM_MAP_ENTRY_MAX) {
844 return KERN_INVALID_ARGUMENT;
845 }
846
847 error = KERN_SUCCESS;
848 for (i = 0; i < count; i++) {
849 vm_map_address_t map_addr;
850 vm_map_size_t map_size;
851
852 map_addr = (vm_map_address_t)(data_list[i].address);
853 map_size = (vm_map_size_t)(data_list[i].size);
854
855 if (map_size != 0) {
856 error = vm_map_copyin(map,
857 map_addr,
858 map_size,
859 FALSE, /* src_destroy */
860 ©);
861 if (KERN_SUCCESS == error) {
862 error = vm_map_copyout(current_task()->map,
863 &map_addr,
864 copy);
865 if (KERN_SUCCESS == error) {
866 data_list[i].address =
867 CAST_DOWN(vm_offset_t, map_addr);
868 continue;
869 }
870 vm_map_copy_discard(copy);
871 }
872 }
873 data_list[i].address = (mach_vm_address_t)0;
874 data_list[i].size = (mach_vm_size_t)0;
875 }
876 return error;
877 }
878
879 /*
880 * mach_vm_read_overwrite -
881 * Overwrite a range of the current map with data from the specified
882 * map/address range.
883 *
884 * In making an assumption that the current thread is local, it is
885 * no longer cluster-safe without a fully supportive local proxy
886 * thread/task (but we don't support cluster's anymore so this is moot).
887 */
888
889 kern_return_t
mach_vm_read_overwrite(vm_map_t map,mach_vm_address_t address,mach_vm_size_t size,mach_vm_address_t data,mach_vm_size_t * data_size)890 mach_vm_read_overwrite(
891 vm_map_t map,
892 mach_vm_address_t address,
893 mach_vm_size_t size,
894 mach_vm_address_t data,
895 mach_vm_size_t *data_size)
896 {
897 kern_return_t error;
898 vm_map_copy_t copy;
899
900 if (map == VM_MAP_NULL) {
901 return KERN_INVALID_ARGUMENT;
902 }
903
904 error = vm_map_copyin(map, (vm_map_address_t)address,
905 (vm_map_size_t)size, FALSE, ©);
906
907 if (KERN_SUCCESS == error) {
908 if (copy) {
909 assertf(copy->size == (vm_map_size_t) size, "Req size: 0x%llx, Copy size: 0x%llx\n", (uint64_t) size, (uint64_t) copy->size);
910 }
911
912 error = vm_map_copy_overwrite(current_thread()->map,
913 (vm_map_address_t)data,
914 copy, (vm_map_size_t) size, FALSE);
915 if (KERN_SUCCESS == error) {
916 *data_size = size;
917 return error;
918 }
919 vm_map_copy_discard(copy);
920 }
921 return error;
922 }
923
924 /*
925 * vm_read_overwrite -
926 * Overwrite a range of the current map with data from the specified
927 * map/address range.
928 *
929 * This routine adds the additional limitation that the source and
930 * destination ranges must be describable with vm_address_t values
931 * (i.e. the same size address spaces as the kernel, or at least the
932 * the ranges are in that first portion of the respective address
933 * spaces).
934 */
935
936 kern_return_t
vm_read_overwrite(vm_map_t map,vm_address_t address,vm_size_t size,vm_address_t data,vm_size_t * data_size)937 vm_read_overwrite(
938 vm_map_t map,
939 vm_address_t address,
940 vm_size_t size,
941 vm_address_t data,
942 vm_size_t *data_size)
943 {
944 kern_return_t error;
945 vm_map_copy_t copy;
946
947 if (map == VM_MAP_NULL) {
948 return KERN_INVALID_ARGUMENT;
949 }
950
951 error = vm_map_copyin(map, (vm_map_address_t)address,
952 (vm_map_size_t)size, FALSE, ©);
953
954 if (KERN_SUCCESS == error) {
955 if (copy) {
956 assertf(copy->size == (vm_map_size_t) size, "Req size: 0x%llx, Copy size: 0x%llx\n", (uint64_t) size, (uint64_t) copy->size);
957 }
958
959 error = vm_map_copy_overwrite(current_thread()->map,
960 (vm_map_address_t)data,
961 copy, (vm_map_size_t) size, FALSE);
962 if (KERN_SUCCESS == error) {
963 *data_size = size;
964 return error;
965 }
966 vm_map_copy_discard(copy);
967 }
968 return error;
969 }
970
971
972 /*
973 * mach_vm_write -
974 * Overwrite the specified address range with the data provided
975 * (from the current map).
976 */
977 kern_return_t
mach_vm_write(vm_map_t map,mach_vm_address_t address,pointer_t data,mach_msg_type_number_t size)978 mach_vm_write(
979 vm_map_t map,
980 mach_vm_address_t address,
981 pointer_t data,
982 mach_msg_type_number_t size)
983 {
984 if (map == VM_MAP_NULL) {
985 return KERN_INVALID_ARGUMENT;
986 }
987
988 return vm_map_copy_overwrite(map, (vm_map_address_t)address,
989 (vm_map_copy_t) data, size, FALSE /* interruptible XXX */);
990 }
991
992 /*
993 * vm_write -
994 * Overwrite the specified address range with the data provided
995 * (from the current map).
996 *
997 * The addressability of the range of addresses to overwrite is
998 * limited bu the use of a vm_address_t (same size as kernel map).
999 * Either the target map is also small, or the range is in the
1000 * low addresses within it.
1001 */
1002 kern_return_t
vm_write(vm_map_t map,vm_address_t address,pointer_t data,mach_msg_type_number_t size)1003 vm_write(
1004 vm_map_t map,
1005 vm_address_t address,
1006 pointer_t data,
1007 mach_msg_type_number_t size)
1008 {
1009 if (map == VM_MAP_NULL) {
1010 return KERN_INVALID_ARGUMENT;
1011 }
1012
1013 return vm_map_copy_overwrite(map, (vm_map_address_t)address,
1014 (vm_map_copy_t) data, size, FALSE /* interruptible XXX */);
1015 }
1016
1017 /*
1018 * mach_vm_copy -
1019 * Overwrite one range of the specified map with the contents of
1020 * another range within that same map (i.e. both address ranges
1021 * are "over there").
1022 */
1023 kern_return_t
mach_vm_copy(vm_map_t map,mach_vm_address_t source_address,mach_vm_size_t size,mach_vm_address_t dest_address)1024 mach_vm_copy(
1025 vm_map_t map,
1026 mach_vm_address_t source_address,
1027 mach_vm_size_t size,
1028 mach_vm_address_t dest_address)
1029 {
1030 vm_map_copy_t copy;
1031 kern_return_t kr;
1032
1033 if (map == VM_MAP_NULL) {
1034 return KERN_INVALID_ARGUMENT;
1035 }
1036
1037 kr = vm_map_copyin(map, (vm_map_address_t)source_address,
1038 (vm_map_size_t)size, FALSE, ©);
1039
1040 if (KERN_SUCCESS == kr) {
1041 if (copy) {
1042 assertf(copy->size == (vm_map_size_t) size, "Req size: 0x%llx, Copy size: 0x%llx\n", (uint64_t) size, (uint64_t) copy->size);
1043 }
1044
1045 kr = vm_map_copy_overwrite(map,
1046 (vm_map_address_t)dest_address,
1047 copy, (vm_map_size_t) size, FALSE /* interruptible XXX */);
1048
1049 if (KERN_SUCCESS != kr) {
1050 vm_map_copy_discard(copy);
1051 }
1052 }
1053 return kr;
1054 }
1055
1056 kern_return_t
vm_copy(vm_map_t map,vm_address_t source_address,vm_size_t size,vm_address_t dest_address)1057 vm_copy(
1058 vm_map_t map,
1059 vm_address_t source_address,
1060 vm_size_t size,
1061 vm_address_t dest_address)
1062 {
1063 vm_map_copy_t copy;
1064 kern_return_t kr;
1065
1066 if (map == VM_MAP_NULL) {
1067 return KERN_INVALID_ARGUMENT;
1068 }
1069
1070 kr = vm_map_copyin(map, (vm_map_address_t)source_address,
1071 (vm_map_size_t)size, FALSE, ©);
1072
1073 if (KERN_SUCCESS == kr) {
1074 if (copy) {
1075 assertf(copy->size == (vm_map_size_t) size, "Req size: 0x%llx, Copy size: 0x%llx\n", (uint64_t) size, (uint64_t) copy->size);
1076 }
1077
1078 kr = vm_map_copy_overwrite(map,
1079 (vm_map_address_t)dest_address,
1080 copy, (vm_map_size_t) size, FALSE /* interruptible XXX */);
1081
1082 if (KERN_SUCCESS != kr) {
1083 vm_map_copy_discard(copy);
1084 }
1085 }
1086 return kr;
1087 }
1088
1089 /*
1090 * mach_vm_map -
1091 * Map some range of an object into an address space.
1092 *
1093 * The object can be one of several types of objects:
1094 * NULL - anonymous memory
1095 * a named entry - a range within another address space
1096 * or a range within a memory object
1097 * a whole memory object
1098 *
1099 */
1100 kern_return_t
mach_vm_map_external(vm_map_t target_map,mach_vm_offset_t * address,mach_vm_size_t initial_size,mach_vm_offset_t mask,int flags,ipc_port_t port,vm_object_offset_t offset,boolean_t copy,vm_prot_t cur_protection,vm_prot_t max_protection,vm_inherit_t inheritance)1101 mach_vm_map_external(
1102 vm_map_t target_map,
1103 mach_vm_offset_t *address,
1104 mach_vm_size_t initial_size,
1105 mach_vm_offset_t mask,
1106 int flags,
1107 ipc_port_t port,
1108 vm_object_offset_t offset,
1109 boolean_t copy,
1110 vm_prot_t cur_protection,
1111 vm_prot_t max_protection,
1112 vm_inherit_t inheritance)
1113 {
1114 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
1115
1116 /* filter out any kernel-only flags */
1117 if (flags & ~VM_FLAGS_USER_MAP) {
1118 return KERN_INVALID_ARGUMENT;
1119 }
1120
1121 vm_map_kernel_flags_set_vmflags(&vmk_flags, flags);
1122 /* range_id is set by mach_vm_map_kernel */
1123 return mach_vm_map_kernel(target_map, address, initial_size, mask,
1124 vmk_flags, port, offset, copy,
1125 cur_protection, max_protection,
1126 inheritance);
1127 }
1128
1129 kern_return_t
mach_vm_map_kernel(vm_map_t target_map,mach_vm_offset_t * address,mach_vm_size_t initial_size,mach_vm_offset_t mask,vm_map_kernel_flags_t vmk_flags,ipc_port_t port,vm_object_offset_t offset,boolean_t copy,vm_prot_t cur_protection,vm_prot_t max_protection,vm_inherit_t inheritance)1130 mach_vm_map_kernel(
1131 vm_map_t target_map,
1132 mach_vm_offset_t *address,
1133 mach_vm_size_t initial_size,
1134 mach_vm_offset_t mask,
1135 vm_map_kernel_flags_t vmk_flags,
1136 ipc_port_t port,
1137 vm_object_offset_t offset,
1138 boolean_t copy,
1139 vm_prot_t cur_protection,
1140 vm_prot_t max_protection,
1141 vm_inherit_t inheritance)
1142 {
1143 kern_return_t kr;
1144 vm_map_offset_t vmmaddr;
1145
1146 vmmaddr = (vm_map_offset_t) *address;
1147
1148 /* filter out any kernel-only flags */
1149 if (!vm_map_kernel_flags_check_vmflags(vmk_flags, VM_FLAGS_USER_MAP)) {
1150 return KERN_INVALID_ARGUMENT;
1151 }
1152
1153 /* range_id is set by vm_map_enter_mem_object */
1154 kr = vm_map_enter_mem_object(target_map,
1155 &vmmaddr,
1156 initial_size,
1157 mask,
1158 vmk_flags,
1159 port,
1160 offset,
1161 copy,
1162 cur_protection,
1163 max_protection,
1164 inheritance);
1165
1166 #if KASAN
1167 if (kr == KERN_SUCCESS && target_map->pmap == kernel_pmap) {
1168 kasan_notify_address(vmmaddr, initial_size);
1169 }
1170 #endif
1171
1172 *address = vmmaddr;
1173 return kr;
1174 }
1175
1176
1177 /* legacy interface */
1178 __attribute__((always_inline))
1179 kern_return_t
vm_map_64_external(vm_map_t target_map,vm_offset_t * address,vm_size_t size,vm_offset_t mask,int flags,ipc_port_t port,vm_object_offset_t offset,boolean_t copy,vm_prot_t cur_protection,vm_prot_t max_protection,vm_inherit_t inheritance)1180 vm_map_64_external(
1181 vm_map_t target_map,
1182 vm_offset_t *address,
1183 vm_size_t size,
1184 vm_offset_t mask,
1185 int flags,
1186 ipc_port_t port,
1187 vm_object_offset_t offset,
1188 boolean_t copy,
1189 vm_prot_t cur_protection,
1190 vm_prot_t max_protection,
1191 vm_inherit_t inheritance)
1192 {
1193 static_assert(sizeof(vm_offset_t) == sizeof(mach_vm_offset_t));
1194
1195 return mach_vm_map_external(target_map, (mach_vm_offset_t *)address,
1196 size, mask, flags, port, offset, copy,
1197 cur_protection, max_protection, inheritance);
1198 }
1199
1200 /* temporary, until world build */
1201 __attribute__((always_inline))
1202 kern_return_t
vm_map_external(vm_map_t target_map,vm_offset_t * address,vm_size_t size,vm_offset_t mask,int flags,ipc_port_t port,vm_offset_t offset,boolean_t copy,vm_prot_t cur_protection,vm_prot_t max_protection,vm_inherit_t inheritance)1203 vm_map_external(
1204 vm_map_t target_map,
1205 vm_offset_t *address,
1206 vm_size_t size,
1207 vm_offset_t mask,
1208 int flags,
1209 ipc_port_t port,
1210 vm_offset_t offset,
1211 boolean_t copy,
1212 vm_prot_t cur_protection,
1213 vm_prot_t max_protection,
1214 vm_inherit_t inheritance)
1215 {
1216 static_assert(sizeof(vm_offset_t) == sizeof(mach_vm_offset_t));
1217
1218 return mach_vm_map_external(target_map, (mach_vm_offset_t *)address,
1219 size, mask, flags, port, offset, copy,
1220 cur_protection, max_protection, inheritance);
1221 }
1222
1223 /*
1224 * mach_vm_remap_new -
1225 * Behaves like mach_vm_remap, except that VM_FLAGS_RETURN_DATA_ADDR is always set
1226 * and {cur,max}_protection are in/out.
1227 */
1228 kern_return_t
mach_vm_remap_new_external(vm_map_t target_map,mach_vm_offset_t * address,mach_vm_size_t size,mach_vm_offset_t mask,int flags,mach_port_t src_tport,mach_vm_offset_t memory_address,boolean_t copy,vm_prot_t * cur_protection,vm_prot_t * max_protection,vm_inherit_t inheritance)1229 mach_vm_remap_new_external(
1230 vm_map_t target_map,
1231 mach_vm_offset_t *address,
1232 mach_vm_size_t size,
1233 mach_vm_offset_t mask,
1234 int flags,
1235 mach_port_t src_tport,
1236 mach_vm_offset_t memory_address,
1237 boolean_t copy,
1238 vm_prot_t *cur_protection, /* IN/OUT */
1239 vm_prot_t *max_protection, /* IN/OUT */
1240 vm_inherit_t inheritance)
1241 {
1242 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
1243 vm_map_t src_map;
1244 kern_return_t kr;
1245
1246 /* filter out any kernel-only flags */
1247 if (flags & ~VM_FLAGS_USER_REMAP) {
1248 return KERN_INVALID_ARGUMENT;
1249 }
1250
1251 vm_map_kernel_flags_set_vmflags(&vmk_flags,
1252 flags | VM_FLAGS_RETURN_DATA_ADDR);
1253
1254 if (target_map == VM_MAP_NULL) {
1255 return KERN_INVALID_ARGUMENT;
1256 }
1257
1258 if ((*cur_protection & ~VM_PROT_ALL) ||
1259 (*max_protection & ~VM_PROT_ALL) ||
1260 (*cur_protection & *max_protection) != *cur_protection) {
1261 return KERN_INVALID_ARGUMENT;
1262 }
1263 if ((*max_protection & (VM_PROT_WRITE | VM_PROT_EXECUTE)) ==
1264 (VM_PROT_WRITE | VM_PROT_EXECUTE)) {
1265 /*
1266 * XXX FBDP TODO
1267 * enforce target's "wx" policies
1268 */
1269 return KERN_PROTECTION_FAILURE;
1270 }
1271
1272 if (copy || *max_protection == VM_PROT_READ || *max_protection == VM_PROT_NONE) {
1273 src_map = convert_port_to_map_read(src_tport);
1274 } else {
1275 src_map = convert_port_to_map(src_tport);
1276 }
1277
1278 if (src_map == VM_MAP_NULL) {
1279 return KERN_INVALID_ARGUMENT;
1280 }
1281
1282 static_assert(sizeof(mach_vm_offset_t) == sizeof(vm_map_address_t));
1283
1284 /* range_id is set by vm_map_remap */
1285 kr = vm_map_remap(target_map,
1286 address,
1287 size,
1288 mask,
1289 vmk_flags,
1290 src_map,
1291 memory_address,
1292 copy,
1293 cur_protection, /* IN/OUT */
1294 max_protection, /* IN/OUT */
1295 inheritance);
1296
1297 vm_map_deallocate(src_map);
1298
1299 if (kr == KERN_SUCCESS) {
1300 ipc_port_release_send(src_tport); /* consume on success */
1301 }
1302 return kr;
1303 }
1304
1305 /*
1306 * mach_vm_remap -
1307 * Remap a range of memory from one task into another,
1308 * to another address range within the same task, or
1309 * over top of itself (with altered permissions and/or
1310 * as an in-place copy of itself).
1311 */
1312 kern_return_t
mach_vm_remap_external(vm_map_t target_map,mach_vm_offset_t * address,mach_vm_size_t size,mach_vm_offset_t mask,int flags,vm_map_t src_map,mach_vm_offset_t memory_address,boolean_t copy,vm_prot_t * cur_protection,vm_prot_t * max_protection,vm_inherit_t inheritance)1313 mach_vm_remap_external(
1314 vm_map_t target_map,
1315 mach_vm_offset_t *address,
1316 mach_vm_size_t size,
1317 mach_vm_offset_t mask,
1318 int flags,
1319 vm_map_t src_map,
1320 mach_vm_offset_t memory_address,
1321 boolean_t copy,
1322 vm_prot_t *cur_protection, /* OUT */
1323 vm_prot_t *max_protection, /* OUT */
1324 vm_inherit_t inheritance)
1325 {
1326 vm_tag_t tag;
1327 VM_GET_FLAGS_ALIAS(flags, tag);
1328
1329 return mach_vm_remap_kernel(target_map, address, size, mask, flags, tag, src_map, memory_address,
1330 copy, cur_protection, max_protection, inheritance);
1331 }
1332
1333 static kern_return_t
mach_vm_remap_kernel_helper(vm_map_t target_map,mach_vm_offset_t * address,mach_vm_size_t size,mach_vm_offset_t mask,int flags,vm_tag_t tag,vm_map_t src_map,mach_vm_offset_t memory_address,boolean_t copy,vm_prot_t * cur_protection,vm_prot_t * max_protection,vm_inherit_t inheritance)1334 mach_vm_remap_kernel_helper(
1335 vm_map_t target_map,
1336 mach_vm_offset_t *address,
1337 mach_vm_size_t size,
1338 mach_vm_offset_t mask,
1339 int flags,
1340 vm_tag_t tag,
1341 vm_map_t src_map,
1342 mach_vm_offset_t memory_address,
1343 boolean_t copy,
1344 vm_prot_t *cur_protection, /* IN/OUT */
1345 vm_prot_t *max_protection, /* IN/OUT */
1346 vm_inherit_t inheritance)
1347 {
1348 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
1349 kern_return_t kr;
1350
1351 if (VM_MAP_NULL == target_map || VM_MAP_NULL == src_map) {
1352 return KERN_INVALID_ARGUMENT;
1353 }
1354
1355 /* filter out any kernel-only flags */
1356 if (flags & ~VM_FLAGS_USER_REMAP) {
1357 return KERN_INVALID_ARGUMENT;
1358 }
1359
1360 vm_map_kernel_flags_set_vmflags(&vmk_flags,
1361 flags | VM_FLAGS_RETURN_DATA_ADDR, tag);
1362
1363 static_assert(sizeof(mach_vm_offset_t) == sizeof(vm_map_address_t));
1364
1365 /* range_id is set by vm_map_remap */
1366 kr = vm_map_remap(target_map,
1367 address,
1368 size,
1369 mask,
1370 vmk_flags,
1371 src_map,
1372 memory_address,
1373 copy,
1374 cur_protection, /* IN/OUT */
1375 max_protection, /* IN/OUT */
1376 inheritance);
1377
1378 #if KASAN
1379 if (kr == KERN_SUCCESS && target_map->pmap == kernel_pmap) {
1380 kasan_notify_address(*address, size);
1381 }
1382 #endif
1383 return kr;
1384 }
1385
1386 kern_return_t
mach_vm_remap_kernel(vm_map_t target_map,mach_vm_offset_t * address,mach_vm_size_t size,mach_vm_offset_t mask,int flags,vm_tag_t tag,vm_map_t src_map,mach_vm_offset_t memory_address,boolean_t copy,vm_prot_t * cur_protection,vm_prot_t * max_protection,vm_inherit_t inheritance)1387 mach_vm_remap_kernel(
1388 vm_map_t target_map,
1389 mach_vm_offset_t *address,
1390 mach_vm_size_t size,
1391 mach_vm_offset_t mask,
1392 int flags,
1393 vm_tag_t tag,
1394 vm_map_t src_map,
1395 mach_vm_offset_t memory_address,
1396 boolean_t copy,
1397 vm_prot_t *cur_protection, /* OUT */
1398 vm_prot_t *max_protection, /* OUT */
1399 vm_inherit_t inheritance)
1400 {
1401 *cur_protection = VM_PROT_NONE;
1402 *max_protection = VM_PROT_NONE;
1403
1404 return mach_vm_remap_kernel_helper(target_map,
1405 address,
1406 size,
1407 mask,
1408 flags,
1409 tag,
1410 src_map,
1411 memory_address,
1412 copy,
1413 cur_protection,
1414 max_protection,
1415 inheritance);
1416 }
1417
1418 kern_return_t
mach_vm_remap_new_kernel(vm_map_t target_map,mach_vm_offset_t * address,mach_vm_size_t size,mach_vm_offset_t mask,int flags,vm_tag_t tag,vm_map_t src_map,mach_vm_offset_t memory_address,boolean_t copy,vm_prot_t * cur_protection,vm_prot_t * max_protection,vm_inherit_t inheritance)1419 mach_vm_remap_new_kernel(
1420 vm_map_t target_map,
1421 mach_vm_offset_t *address,
1422 mach_vm_size_t size,
1423 mach_vm_offset_t mask,
1424 int flags,
1425 vm_tag_t tag,
1426 vm_map_t src_map,
1427 mach_vm_offset_t memory_address,
1428 boolean_t copy,
1429 vm_prot_t *cur_protection, /* IN/OUT */
1430 vm_prot_t *max_protection, /* IN/OUT */
1431 vm_inherit_t inheritance)
1432 {
1433 if ((*cur_protection & ~VM_PROT_ALL) ||
1434 (*max_protection & ~VM_PROT_ALL) ||
1435 (*cur_protection & *max_protection) != *cur_protection) {
1436 return KERN_INVALID_ARGUMENT;
1437 }
1438
1439 flags |= VM_FLAGS_RETURN_DATA_ADDR;
1440
1441 return mach_vm_remap_kernel_helper(target_map,
1442 address,
1443 size,
1444 mask,
1445 flags,
1446 tag,
1447 src_map,
1448 memory_address,
1449 copy,
1450 cur_protection,
1451 max_protection,
1452 inheritance);
1453 }
1454
1455 /*
1456 * vm_remap_new -
1457 * Behaves like vm_remap, except that VM_FLAGS_RETURN_DATA_ADDR is always set
1458 * and {cur,max}_protection are in/out.
1459 */
1460 kern_return_t
vm_remap_new_external(vm_map_t target_map,vm_offset_t * address,vm_size_t size,vm_offset_t mask,int flags,mach_port_t src_tport,vm_offset_t memory_address,boolean_t copy,vm_prot_t * cur_protection,vm_prot_t * max_protection,vm_inherit_t inheritance)1461 vm_remap_new_external(
1462 vm_map_t target_map,
1463 vm_offset_t *address,
1464 vm_size_t size,
1465 vm_offset_t mask,
1466 int flags,
1467 mach_port_t src_tport,
1468 vm_offset_t memory_address,
1469 boolean_t copy,
1470 vm_prot_t *cur_protection, /* IN/OUT */
1471 vm_prot_t *max_protection, /* IN/OUT */
1472 vm_inherit_t inheritance)
1473 {
1474 static_assert(sizeof(vm_map_offset_t) == sizeof(vm_offset_t));
1475
1476 return mach_vm_remap_new_external(target_map,
1477 (vm_map_offset_t *)address,
1478 size,
1479 mask,
1480 flags,
1481 src_tport,
1482 memory_address,
1483 copy,
1484 cur_protection, /* IN/OUT */
1485 max_protection, /* IN/OUT */
1486 inheritance);
1487 }
1488
1489 /*
1490 * vm_remap -
1491 * Remap a range of memory from one task into another,
1492 * to another address range within the same task, or
1493 * over top of itself (with altered permissions and/or
1494 * as an in-place copy of itself).
1495 *
1496 * The addressability of the source and target address
1497 * range is limited by the size of vm_address_t (in the
1498 * kernel context).
1499 */
1500 kern_return_t
vm_remap_external(vm_map_t target_map,vm_offset_t * address,vm_size_t size,vm_offset_t mask,int flags,vm_map_t src_map,vm_offset_t memory_address,boolean_t copy,vm_prot_t * cur_protection,vm_prot_t * max_protection,vm_inherit_t inheritance)1501 vm_remap_external(
1502 vm_map_t target_map,
1503 vm_offset_t *address,
1504 vm_size_t size,
1505 vm_offset_t mask,
1506 int flags,
1507 vm_map_t src_map,
1508 vm_offset_t memory_address,
1509 boolean_t copy,
1510 vm_prot_t *cur_protection, /* OUT */
1511 vm_prot_t *max_protection, /* OUT */
1512 vm_inherit_t inheritance)
1513 {
1514 static_assert(sizeof(vm_offset_t) == sizeof(mach_vm_offset_t));
1515
1516 return mach_vm_remap_external(target_map, (mach_vm_offset_t *)address,
1517 size, mask, flags, src_map, memory_address, copy,
1518 cur_protection, max_protection, inheritance);
1519 }
1520
1521 /*
1522 * NOTE: these routine (and this file) will no longer require mach_host_server.h
1523 * when mach_vm_wire and vm_wire are changed to use ledgers.
1524 */
1525 #include <mach/mach_host_server.h>
1526 /*
1527 * mach_vm_wire
1528 * Specify that the range of the virtual address space
1529 * of the target task must not cause page faults for
1530 * the indicated accesses.
1531 *
1532 * [ To unwire the pages, specify VM_PROT_NONE. ]
1533 */
1534 kern_return_t
mach_vm_wire_external(host_priv_t host_priv,vm_map_t map,mach_vm_offset_t start,mach_vm_size_t size,vm_prot_t access)1535 mach_vm_wire_external(
1536 host_priv_t host_priv,
1537 vm_map_t map,
1538 mach_vm_offset_t start,
1539 mach_vm_size_t size,
1540 vm_prot_t access)
1541 {
1542 if (host_priv == HOST_PRIV_NULL) {
1543 return KERN_INVALID_HOST;
1544 }
1545
1546 return mach_vm_wire_kernel(map, start, size, access, VM_KERN_MEMORY_MLOCK);
1547 }
1548
1549 kern_return_t
mach_vm_wire_kernel(vm_map_t map,mach_vm_offset_t start,mach_vm_size_t size,vm_prot_t access,vm_tag_t tag)1550 mach_vm_wire_kernel(
1551 vm_map_t map,
1552 mach_vm_offset_t start,
1553 mach_vm_size_t size,
1554 vm_prot_t access,
1555 vm_tag_t tag)
1556 {
1557 kern_return_t rc;
1558
1559 if (map == VM_MAP_NULL) {
1560 return KERN_INVALID_TASK;
1561 }
1562
1563 if (access & ~VM_PROT_ALL || (start + size < start)) {
1564 return KERN_INVALID_ARGUMENT;
1565 }
1566
1567 if (access != VM_PROT_NONE) {
1568 rc = vm_map_wire_kernel(map,
1569 vm_map_trunc_page(start,
1570 VM_MAP_PAGE_MASK(map)),
1571 vm_map_round_page(start + size,
1572 VM_MAP_PAGE_MASK(map)),
1573 access, tag,
1574 TRUE);
1575 } else {
1576 rc = vm_map_unwire(map,
1577 vm_map_trunc_page(start,
1578 VM_MAP_PAGE_MASK(map)),
1579 vm_map_round_page(start + size,
1580 VM_MAP_PAGE_MASK(map)),
1581 TRUE);
1582 }
1583 return rc;
1584 }
1585
1586 /*
1587 * vm_wire -
1588 * Specify that the range of the virtual address space
1589 * of the target task must not cause page faults for
1590 * the indicated accesses.
1591 *
1592 * [ To unwire the pages, specify VM_PROT_NONE. ]
1593 */
1594 kern_return_t
vm_wire(host_priv_t host_priv,vm_map_t map,vm_offset_t start,vm_size_t size,vm_prot_t access)1595 vm_wire(
1596 host_priv_t host_priv,
1597 vm_map_t map,
1598 vm_offset_t start,
1599 vm_size_t size,
1600 vm_prot_t access)
1601 {
1602 kern_return_t rc;
1603
1604 if (host_priv == HOST_PRIV_NULL) {
1605 return KERN_INVALID_HOST;
1606 }
1607
1608 if (map == VM_MAP_NULL) {
1609 return KERN_INVALID_TASK;
1610 }
1611
1612 if ((access & ~VM_PROT_ALL) || (start + size < start)) {
1613 return KERN_INVALID_ARGUMENT;
1614 }
1615
1616 if (size == 0) {
1617 rc = KERN_SUCCESS;
1618 } else if (access != VM_PROT_NONE) {
1619 rc = vm_map_wire_kernel(map,
1620 vm_map_trunc_page(start,
1621 VM_MAP_PAGE_MASK(map)),
1622 vm_map_round_page(start + size,
1623 VM_MAP_PAGE_MASK(map)),
1624 access, VM_KERN_MEMORY_OSFMK,
1625 TRUE);
1626 } else {
1627 rc = vm_map_unwire(map,
1628 vm_map_trunc_page(start,
1629 VM_MAP_PAGE_MASK(map)),
1630 vm_map_round_page(start + size,
1631 VM_MAP_PAGE_MASK(map)),
1632 TRUE);
1633 }
1634 return rc;
1635 }
1636
1637 /*
1638 * vm_msync
1639 *
1640 * Synchronises the memory range specified with its backing store
1641 * image by either flushing or cleaning the contents to the appropriate
1642 * memory manager.
1643 *
1644 * interpretation of sync_flags
1645 * VM_SYNC_INVALIDATE - discard pages, only return precious
1646 * pages to manager.
1647 *
1648 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
1649 * - discard pages, write dirty or precious
1650 * pages back to memory manager.
1651 *
1652 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
1653 * - write dirty or precious pages back to
1654 * the memory manager.
1655 *
1656 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
1657 * is a hole in the region, and we would
1658 * have returned KERN_SUCCESS, return
1659 * KERN_INVALID_ADDRESS instead.
1660 *
1661 * RETURNS
1662 * KERN_INVALID_TASK Bad task parameter
1663 * KERN_INVALID_ARGUMENT both sync and async were specified.
1664 * KERN_SUCCESS The usual.
1665 * KERN_INVALID_ADDRESS There was a hole in the region.
1666 */
1667
1668 kern_return_t
mach_vm_msync(vm_map_t map,mach_vm_address_t address,mach_vm_size_t size,vm_sync_t sync_flags)1669 mach_vm_msync(
1670 vm_map_t map,
1671 mach_vm_address_t address,
1672 mach_vm_size_t size,
1673 vm_sync_t sync_flags)
1674 {
1675 if (map == VM_MAP_NULL) {
1676 return KERN_INVALID_TASK;
1677 }
1678
1679 return vm_map_msync(map, (vm_map_address_t)address,
1680 (vm_map_size_t)size, sync_flags);
1681 }
1682
1683 /*
1684 * vm_msync
1685 *
1686 * Synchronises the memory range specified with its backing store
1687 * image by either flushing or cleaning the contents to the appropriate
1688 * memory manager.
1689 *
1690 * interpretation of sync_flags
1691 * VM_SYNC_INVALIDATE - discard pages, only return precious
1692 * pages to manager.
1693 *
1694 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
1695 * - discard pages, write dirty or precious
1696 * pages back to memory manager.
1697 *
1698 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
1699 * - write dirty or precious pages back to
1700 * the memory manager.
1701 *
1702 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
1703 * is a hole in the region, and we would
1704 * have returned KERN_SUCCESS, return
1705 * KERN_INVALID_ADDRESS instead.
1706 *
1707 * The addressability of the range is limited to that which can
1708 * be described by a vm_address_t.
1709 *
1710 * RETURNS
1711 * KERN_INVALID_TASK Bad task parameter
1712 * KERN_INVALID_ARGUMENT both sync and async were specified.
1713 * KERN_SUCCESS The usual.
1714 * KERN_INVALID_ADDRESS There was a hole in the region.
1715 */
1716
1717 kern_return_t
vm_msync(vm_map_t map,vm_address_t address,vm_size_t size,vm_sync_t sync_flags)1718 vm_msync(
1719 vm_map_t map,
1720 vm_address_t address,
1721 vm_size_t size,
1722 vm_sync_t sync_flags)
1723 {
1724 if (map == VM_MAP_NULL) {
1725 return KERN_INVALID_TASK;
1726 }
1727
1728 return vm_map_msync(map, (vm_map_address_t)address,
1729 (vm_map_size_t)size, sync_flags);
1730 }
1731
1732
1733 int
vm_toggle_entry_reuse(int toggle,int * old_value)1734 vm_toggle_entry_reuse(int toggle, int *old_value)
1735 {
1736 vm_map_t map = current_map();
1737
1738 assert(!map->is_nested_map);
1739 if (toggle == VM_TOGGLE_GETVALUE && old_value != NULL) {
1740 *old_value = map->disable_vmentry_reuse;
1741 } else if (toggle == VM_TOGGLE_SET) {
1742 vm_map_entry_t map_to_entry;
1743
1744 vm_map_lock(map);
1745 vm_map_disable_hole_optimization(map);
1746 map->disable_vmentry_reuse = TRUE;
1747 __IGNORE_WCASTALIGN(map_to_entry = vm_map_to_entry(map));
1748 if (map->first_free == map_to_entry) {
1749 map->highest_entry_end = vm_map_min(map);
1750 } else {
1751 map->highest_entry_end = map->first_free->vme_end;
1752 }
1753 vm_map_unlock(map);
1754 } else if (toggle == VM_TOGGLE_CLEAR) {
1755 vm_map_lock(map);
1756 map->disable_vmentry_reuse = FALSE;
1757 vm_map_unlock(map);
1758 } else {
1759 return KERN_INVALID_ARGUMENT;
1760 }
1761
1762 return KERN_SUCCESS;
1763 }
1764
1765 /*
1766 * mach_vm_behavior_set
1767 *
1768 * Sets the paging behavior attribute for the specified range
1769 * in the specified map.
1770 *
1771 * This routine will fail with KERN_INVALID_ADDRESS if any address
1772 * in [start,start+size) is not a valid allocated memory region.
1773 */
1774 kern_return_t
mach_vm_behavior_set(vm_map_t map,mach_vm_offset_t start,mach_vm_size_t size,vm_behavior_t new_behavior)1775 mach_vm_behavior_set(
1776 vm_map_t map,
1777 mach_vm_offset_t start,
1778 mach_vm_size_t size,
1779 vm_behavior_t new_behavior)
1780 {
1781 vm_map_offset_t align_mask;
1782
1783 if ((map == VM_MAP_NULL) || (start + size < start)) {
1784 return KERN_INVALID_ARGUMENT;
1785 }
1786
1787 if (size == 0) {
1788 return KERN_SUCCESS;
1789 }
1790
1791 switch (new_behavior) {
1792 case VM_BEHAVIOR_REUSABLE:
1793 case VM_BEHAVIOR_REUSE:
1794 case VM_BEHAVIOR_CAN_REUSE:
1795 /*
1796 * Align to the hardware page size, to allow
1797 * malloc() to maximize the amount of re-usability,
1798 * even on systems with larger software page size.
1799 */
1800 align_mask = PAGE_MASK;
1801 break;
1802 default:
1803 align_mask = VM_MAP_PAGE_MASK(map);
1804 break;
1805 }
1806
1807 return vm_map_behavior_set(map,
1808 vm_map_trunc_page(start, align_mask),
1809 vm_map_round_page(start + size, align_mask),
1810 new_behavior);
1811 }
1812
1813 /*
1814 * vm_behavior_set
1815 *
1816 * Sets the paging behavior attribute for the specified range
1817 * in the specified map.
1818 *
1819 * This routine will fail with KERN_INVALID_ADDRESS if any address
1820 * in [start,start+size) is not a valid allocated memory region.
1821 *
1822 * This routine is potentially limited in addressibility by the
1823 * use of vm_offset_t (if the map provided is larger than the
1824 * kernel's).
1825 */
1826 kern_return_t
vm_behavior_set(vm_map_t map,vm_offset_t start,vm_size_t size,vm_behavior_t new_behavior)1827 vm_behavior_set(
1828 vm_map_t map,
1829 vm_offset_t start,
1830 vm_size_t size,
1831 vm_behavior_t new_behavior)
1832 {
1833 if (start + size < start) {
1834 return KERN_INVALID_ARGUMENT;
1835 }
1836
1837 return mach_vm_behavior_set(map,
1838 (mach_vm_offset_t) start,
1839 (mach_vm_size_t) size,
1840 new_behavior);
1841 }
1842
1843 /*
1844 * mach_vm_region:
1845 *
1846 * User call to obtain information about a region in
1847 * a task's address map. Currently, only one flavor is
1848 * supported.
1849 *
1850 * XXX The reserved and behavior fields cannot be filled
1851 * in until the vm merge from the IK is completed, and
1852 * vm_reserve is implemented.
1853 *
1854 * XXX Dependency: syscall_vm_region() also supports only one flavor.
1855 */
1856
1857 kern_return_t
mach_vm_region(vm_map_t map,mach_vm_offset_t * address,mach_vm_size_t * size,vm_region_flavor_t flavor,vm_region_info_t info,mach_msg_type_number_t * count,mach_port_t * object_name)1858 mach_vm_region(
1859 vm_map_t map,
1860 mach_vm_offset_t *address, /* IN/OUT */
1861 mach_vm_size_t *size, /* OUT */
1862 vm_region_flavor_t flavor, /* IN */
1863 vm_region_info_t info, /* OUT */
1864 mach_msg_type_number_t *count, /* IN/OUT */
1865 mach_port_t *object_name) /* OUT */
1866 {
1867 vm_map_offset_t map_addr;
1868 vm_map_size_t map_size;
1869 kern_return_t kr;
1870
1871 if (VM_MAP_NULL == map) {
1872 return KERN_INVALID_ARGUMENT;
1873 }
1874
1875 map_addr = (vm_map_offset_t)*address;
1876 map_size = (vm_map_size_t)*size;
1877
1878 /* legacy conversion */
1879 if (VM_REGION_BASIC_INFO == flavor) {
1880 flavor = VM_REGION_BASIC_INFO_64;
1881 }
1882
1883 kr = vm_map_region(map,
1884 &map_addr, &map_size,
1885 flavor, info, count,
1886 object_name);
1887
1888 *address = map_addr;
1889 *size = map_size;
1890 return kr;
1891 }
1892
1893 /*
1894 * vm_region_64 and vm_region:
1895 *
1896 * User call to obtain information about a region in
1897 * a task's address map. Currently, only one flavor is
1898 * supported.
1899 *
1900 * XXX The reserved and behavior fields cannot be filled
1901 * in until the vm merge from the IK is completed, and
1902 * vm_reserve is implemented.
1903 *
1904 * XXX Dependency: syscall_vm_region() also supports only one flavor.
1905 */
1906
1907 kern_return_t
vm_region_64(vm_map_t map,vm_offset_t * address,vm_size_t * size,vm_region_flavor_t flavor,vm_region_info_t info,mach_msg_type_number_t * count,mach_port_t * object_name)1908 vm_region_64(
1909 vm_map_t map,
1910 vm_offset_t *address, /* IN/OUT */
1911 vm_size_t *size, /* OUT */
1912 vm_region_flavor_t flavor, /* IN */
1913 vm_region_info_t info, /* OUT */
1914 mach_msg_type_number_t *count, /* IN/OUT */
1915 mach_port_t *object_name) /* OUT */
1916 {
1917 vm_map_offset_t map_addr;
1918 vm_map_size_t map_size;
1919 kern_return_t kr;
1920
1921 if (VM_MAP_NULL == map) {
1922 return KERN_INVALID_ARGUMENT;
1923 }
1924
1925 map_addr = (vm_map_offset_t)*address;
1926 map_size = (vm_map_size_t)*size;
1927
1928 /* legacy conversion */
1929 if (VM_REGION_BASIC_INFO == flavor) {
1930 flavor = VM_REGION_BASIC_INFO_64;
1931 }
1932
1933 kr = vm_map_region(map,
1934 &map_addr, &map_size,
1935 flavor, info, count,
1936 object_name);
1937
1938 *address = CAST_DOWN(vm_offset_t, map_addr);
1939 *size = CAST_DOWN(vm_size_t, map_size);
1940
1941 if (KERN_SUCCESS == kr && map_addr + map_size > VM_MAX_ADDRESS) {
1942 return KERN_INVALID_ADDRESS;
1943 }
1944 return kr;
1945 }
1946
1947 kern_return_t
vm_region(vm_map_t map,vm_address_t * address,vm_size_t * size,vm_region_flavor_t flavor,vm_region_info_t info,mach_msg_type_number_t * count,mach_port_t * object_name)1948 vm_region(
1949 vm_map_t map,
1950 vm_address_t *address, /* IN/OUT */
1951 vm_size_t *size, /* OUT */
1952 vm_region_flavor_t flavor, /* IN */
1953 vm_region_info_t info, /* OUT */
1954 mach_msg_type_number_t *count, /* IN/OUT */
1955 mach_port_t *object_name) /* OUT */
1956 {
1957 vm_map_address_t map_addr;
1958 vm_map_size_t map_size;
1959 kern_return_t kr;
1960
1961 if (VM_MAP_NULL == map) {
1962 return KERN_INVALID_ARGUMENT;
1963 }
1964
1965 map_addr = (vm_map_address_t)*address;
1966 map_size = (vm_map_size_t)*size;
1967
1968 kr = vm_map_region(map,
1969 &map_addr, &map_size,
1970 flavor, info, count,
1971 object_name);
1972
1973 *address = CAST_DOWN(vm_address_t, map_addr);
1974 *size = CAST_DOWN(vm_size_t, map_size);
1975
1976 if (KERN_SUCCESS == kr && map_addr + map_size > VM_MAX_ADDRESS) {
1977 return KERN_INVALID_ADDRESS;
1978 }
1979 return kr;
1980 }
1981
1982 /*
1983 * vm_region_recurse: A form of vm_region which follows the
1984 * submaps in a target map
1985 *
1986 */
1987 kern_return_t
mach_vm_region_recurse(vm_map_t map,mach_vm_address_t * address,mach_vm_size_t * size,uint32_t * depth,vm_region_recurse_info_t info,mach_msg_type_number_t * infoCnt)1988 mach_vm_region_recurse(
1989 vm_map_t map,
1990 mach_vm_address_t *address,
1991 mach_vm_size_t *size,
1992 uint32_t *depth,
1993 vm_region_recurse_info_t info,
1994 mach_msg_type_number_t *infoCnt)
1995 {
1996 vm_map_address_t map_addr;
1997 vm_map_size_t map_size;
1998 kern_return_t kr;
1999
2000 if (VM_MAP_NULL == map) {
2001 return KERN_INVALID_ARGUMENT;
2002 }
2003
2004 map_addr = (vm_map_address_t)*address;
2005 map_size = (vm_map_size_t)*size;
2006
2007 kr = vm_map_region_recurse_64(
2008 map,
2009 &map_addr,
2010 &map_size,
2011 depth,
2012 (vm_region_submap_info_64_t)info,
2013 infoCnt);
2014
2015 *address = map_addr;
2016 *size = map_size;
2017 return kr;
2018 }
2019
2020 /*
2021 * vm_region_recurse: A form of vm_region which follows the
2022 * submaps in a target map
2023 *
2024 */
2025 kern_return_t
vm_region_recurse_64(vm_map_t map,vm_address_t * address,vm_size_t * size,uint32_t * depth,vm_region_recurse_info_64_t info,mach_msg_type_number_t * infoCnt)2026 vm_region_recurse_64(
2027 vm_map_t map,
2028 vm_address_t *address,
2029 vm_size_t *size,
2030 uint32_t *depth,
2031 vm_region_recurse_info_64_t info,
2032 mach_msg_type_number_t *infoCnt)
2033 {
2034 vm_map_address_t map_addr;
2035 vm_map_size_t map_size;
2036 kern_return_t kr;
2037
2038 if (VM_MAP_NULL == map) {
2039 return KERN_INVALID_ARGUMENT;
2040 }
2041
2042 map_addr = (vm_map_address_t)*address;
2043 map_size = (vm_map_size_t)*size;
2044
2045 kr = vm_map_region_recurse_64(
2046 map,
2047 &map_addr,
2048 &map_size,
2049 depth,
2050 (vm_region_submap_info_64_t)info,
2051 infoCnt);
2052
2053 *address = CAST_DOWN(vm_address_t, map_addr);
2054 *size = CAST_DOWN(vm_size_t, map_size);
2055
2056 if (KERN_SUCCESS == kr && map_addr + map_size > VM_MAX_ADDRESS) {
2057 return KERN_INVALID_ADDRESS;
2058 }
2059 return kr;
2060 }
2061
2062 kern_return_t
vm_region_recurse(vm_map_t map,vm_offset_t * address,vm_size_t * size,natural_t * depth,vm_region_recurse_info_t info32,mach_msg_type_number_t * infoCnt)2063 vm_region_recurse(
2064 vm_map_t map,
2065 vm_offset_t *address, /* IN/OUT */
2066 vm_size_t *size, /* OUT */
2067 natural_t *depth, /* IN/OUT */
2068 vm_region_recurse_info_t info32, /* IN/OUT */
2069 mach_msg_type_number_t *infoCnt) /* IN/OUT */
2070 {
2071 vm_region_submap_info_data_64_t info64;
2072 vm_region_submap_info_t info;
2073 vm_map_address_t map_addr;
2074 vm_map_size_t map_size;
2075 kern_return_t kr;
2076
2077 if (VM_MAP_NULL == map || *infoCnt < VM_REGION_SUBMAP_INFO_COUNT) {
2078 return KERN_INVALID_ARGUMENT;
2079 }
2080
2081
2082 map_addr = (vm_map_address_t)*address;
2083 map_size = (vm_map_size_t)*size;
2084 info = (vm_region_submap_info_t)info32;
2085 *infoCnt = VM_REGION_SUBMAP_INFO_COUNT_64;
2086
2087 kr = vm_map_region_recurse_64(map, &map_addr, &map_size,
2088 depth, &info64, infoCnt);
2089
2090 info->protection = info64.protection;
2091 info->max_protection = info64.max_protection;
2092 info->inheritance = info64.inheritance;
2093 info->offset = (uint32_t)info64.offset; /* trouble-maker */
2094 info->user_tag = info64.user_tag;
2095 info->pages_resident = info64.pages_resident;
2096 info->pages_shared_now_private = info64.pages_shared_now_private;
2097 info->pages_swapped_out = info64.pages_swapped_out;
2098 info->pages_dirtied = info64.pages_dirtied;
2099 info->ref_count = info64.ref_count;
2100 info->shadow_depth = info64.shadow_depth;
2101 info->external_pager = info64.external_pager;
2102 info->share_mode = info64.share_mode;
2103 info->is_submap = info64.is_submap;
2104 info->behavior = info64.behavior;
2105 info->object_id = info64.object_id;
2106 info->user_wired_count = info64.user_wired_count;
2107
2108 *address = CAST_DOWN(vm_address_t, map_addr);
2109 *size = CAST_DOWN(vm_size_t, map_size);
2110 *infoCnt = VM_REGION_SUBMAP_INFO_COUNT;
2111
2112 if (KERN_SUCCESS == kr && map_addr + map_size > VM_MAX_ADDRESS) {
2113 return KERN_INVALID_ADDRESS;
2114 }
2115 return kr;
2116 }
2117
2118 kern_return_t
mach_vm_purgable_control(vm_map_t map,mach_vm_offset_t address,vm_purgable_t control,int * state)2119 mach_vm_purgable_control(
2120 vm_map_t map,
2121 mach_vm_offset_t address,
2122 vm_purgable_t control,
2123 int *state)
2124 {
2125 if (VM_MAP_NULL == map) {
2126 return KERN_INVALID_ARGUMENT;
2127 }
2128
2129 if (control == VM_PURGABLE_SET_STATE_FROM_KERNEL) {
2130 /* not allowed from user-space */
2131 return KERN_INVALID_ARGUMENT;
2132 }
2133
2134 return vm_map_purgable_control(map,
2135 vm_map_trunc_page(address, VM_MAP_PAGE_MASK(map)),
2136 control,
2137 state);
2138 }
2139
2140 kern_return_t
mach_vm_purgable_control_external(mach_port_t target_tport,mach_vm_offset_t address,vm_purgable_t control,int * state)2141 mach_vm_purgable_control_external(
2142 mach_port_t target_tport,
2143 mach_vm_offset_t address,
2144 vm_purgable_t control,
2145 int *state)
2146 {
2147 vm_map_t map;
2148 kern_return_t kr;
2149
2150 if (control == VM_PURGABLE_GET_STATE) {
2151 map = convert_port_to_map_read(target_tport);
2152 } else {
2153 map = convert_port_to_map(target_tport);
2154 }
2155
2156 kr = mach_vm_purgable_control(map, address, control, state);
2157 vm_map_deallocate(map);
2158
2159 return kr;
2160 }
2161
2162 kern_return_t
vm_purgable_control(vm_map_t map,vm_offset_t address,vm_purgable_t control,int * state)2163 vm_purgable_control(
2164 vm_map_t map,
2165 vm_offset_t address,
2166 vm_purgable_t control,
2167 int *state)
2168 {
2169 if (VM_MAP_NULL == map) {
2170 return KERN_INVALID_ARGUMENT;
2171 }
2172
2173 if (control == VM_PURGABLE_SET_STATE_FROM_KERNEL) {
2174 /* not allowed from user-space */
2175 return KERN_INVALID_ARGUMENT;
2176 }
2177
2178 return vm_map_purgable_control(map,
2179 vm_map_trunc_page(address, VM_MAP_PAGE_MASK(map)),
2180 control,
2181 state);
2182 }
2183
2184 kern_return_t
vm_purgable_control_external(mach_port_t target_tport,vm_offset_t address,vm_purgable_t control,int * state)2185 vm_purgable_control_external(
2186 mach_port_t target_tport,
2187 vm_offset_t address,
2188 vm_purgable_t control,
2189 int *state)
2190 {
2191 vm_map_t map;
2192 kern_return_t kr;
2193
2194 if (control == VM_PURGABLE_GET_STATE) {
2195 map = convert_port_to_map_read(target_tport);
2196 } else {
2197 map = convert_port_to_map(target_tport);
2198 }
2199
2200 kr = vm_purgable_control(map, address, control, state);
2201 vm_map_deallocate(map);
2202
2203 return kr;
2204 }
2205
2206
2207 /*
2208 * Ordinarily, the right to allocate CPM is restricted
2209 * to privileged applications (those that can gain access
2210 * to the host priv port). Set this variable to zero if
2211 * you want to let any application allocate CPM.
2212 */
2213 unsigned int vm_allocate_cpm_privileged = 0;
2214
2215 /*
2216 * Allocate memory in the specified map, with the caveat that
2217 * the memory is physically contiguous. This call may fail
2218 * if the system can't find sufficient contiguous memory.
2219 * This call may cause or lead to heart-stopping amounts of
2220 * paging activity.
2221 *
2222 * Memory obtained from this call should be freed in the
2223 * normal way, viz., via vm_deallocate.
2224 */
2225 kern_return_t
vm_allocate_cpm(host_priv_t host_priv,vm_map_t map,vm_address_t * addr,vm_size_t size,int flags)2226 vm_allocate_cpm(
2227 host_priv_t host_priv,
2228 vm_map_t map,
2229 vm_address_t *addr,
2230 vm_size_t size,
2231 int flags)
2232 {
2233 vm_map_address_t map_addr;
2234 vm_map_size_t map_size;
2235 kern_return_t kr;
2236 vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
2237
2238 if (vm_allocate_cpm_privileged && HOST_PRIV_NULL == host_priv) {
2239 return KERN_INVALID_HOST;
2240 }
2241
2242 if (VM_MAP_NULL == map) {
2243 return KERN_INVALID_ARGUMENT;
2244 }
2245
2246 map_addr = (vm_map_address_t)*addr;
2247 map_size = (vm_map_size_t)size;
2248
2249 vm_map_kernel_flags_set_vmflags(&vmk_flags, flags);
2250 vm_map_kernel_flags_update_range_id(&vmk_flags, map);
2251
2252 kr = vm_map_enter_cpm(map, &map_addr, map_size, vmk_flags);
2253
2254 *addr = CAST_DOWN(vm_address_t, map_addr);
2255 return kr;
2256 }
2257
2258
2259 kern_return_t
mach_vm_page_query(vm_map_t map,mach_vm_offset_t offset,int * disposition,int * ref_count)2260 mach_vm_page_query(
2261 vm_map_t map,
2262 mach_vm_offset_t offset,
2263 int *disposition,
2264 int *ref_count)
2265 {
2266 if (VM_MAP_NULL == map) {
2267 return KERN_INVALID_ARGUMENT;
2268 }
2269
2270 return vm_map_page_query_internal(
2271 map,
2272 vm_map_trunc_page(offset, PAGE_MASK),
2273 disposition, ref_count);
2274 }
2275
2276 kern_return_t
vm_map_page_query(vm_map_t map,vm_offset_t offset,int * disposition,int * ref_count)2277 vm_map_page_query(
2278 vm_map_t map,
2279 vm_offset_t offset,
2280 int *disposition,
2281 int *ref_count)
2282 {
2283 if (VM_MAP_NULL == map) {
2284 return KERN_INVALID_ARGUMENT;
2285 }
2286
2287 return vm_map_page_query_internal(
2288 map,
2289 vm_map_trunc_page(offset, PAGE_MASK),
2290 disposition, ref_count);
2291 }
2292
2293 kern_return_t
mach_vm_page_range_query(vm_map_t map,mach_vm_offset_t address,mach_vm_size_t size,mach_vm_address_t dispositions_addr,mach_vm_size_t * dispositions_count)2294 mach_vm_page_range_query(
2295 vm_map_t map,
2296 mach_vm_offset_t address,
2297 mach_vm_size_t size,
2298 mach_vm_address_t dispositions_addr,
2299 mach_vm_size_t *dispositions_count)
2300 {
2301 kern_return_t kr = KERN_SUCCESS;
2302 int num_pages = 0, i = 0;
2303 mach_vm_size_t curr_sz = 0, copy_sz = 0;
2304 mach_vm_size_t disp_buf_req_size = 0, disp_buf_total_size = 0;
2305 mach_msg_type_number_t count = 0;
2306
2307 void *info = NULL;
2308 void *local_disp = NULL;
2309 vm_map_size_t info_size = 0, local_disp_size = 0;
2310 mach_vm_offset_t start = 0, end = 0;
2311 int effective_page_shift, effective_page_size, effective_page_mask;
2312
2313 if (map == VM_MAP_NULL || dispositions_count == NULL) {
2314 return KERN_INVALID_ARGUMENT;
2315 }
2316
2317 effective_page_shift = vm_self_region_page_shift_safely(map);
2318 if (effective_page_shift == -1) {
2319 return KERN_INVALID_ARGUMENT;
2320 }
2321 effective_page_size = (1 << effective_page_shift);
2322 effective_page_mask = effective_page_size - 1;
2323
2324 if (os_mul_overflow(*dispositions_count, sizeof(int), &disp_buf_req_size)) {
2325 return KERN_INVALID_ARGUMENT;
2326 }
2327
2328 start = vm_map_trunc_page(address, effective_page_mask);
2329 end = vm_map_round_page(address + size, effective_page_mask);
2330
2331 if (end < start) {
2332 return KERN_INVALID_ARGUMENT;
2333 }
2334
2335 if ((end - start) < size) {
2336 /*
2337 * Aligned size is less than unaligned size.
2338 */
2339 return KERN_INVALID_ARGUMENT;
2340 }
2341
2342 if (disp_buf_req_size == 0 || (end == start)) {
2343 return KERN_SUCCESS;
2344 }
2345
2346 /*
2347 * For large requests, we will go through them
2348 * MAX_PAGE_RANGE_QUERY chunk at a time.
2349 */
2350
2351 curr_sz = MIN(end - start, MAX_PAGE_RANGE_QUERY);
2352 num_pages = (int) (curr_sz >> effective_page_shift);
2353
2354 info_size = num_pages * sizeof(vm_page_info_basic_data_t);
2355 info = kalloc_data(info_size, Z_WAITOK);
2356
2357 local_disp_size = num_pages * sizeof(int);
2358 local_disp = kalloc_data(local_disp_size, Z_WAITOK);
2359
2360 if (info == NULL || local_disp == NULL) {
2361 kr = KERN_RESOURCE_SHORTAGE;
2362 goto out;
2363 }
2364
2365 while (size) {
2366 count = VM_PAGE_INFO_BASIC_COUNT;
2367 kr = vm_map_page_range_info_internal(
2368 map,
2369 start,
2370 vm_map_round_page(start + curr_sz, effective_page_mask),
2371 effective_page_shift,
2372 VM_PAGE_INFO_BASIC,
2373 (vm_page_info_t) info,
2374 &count);
2375
2376 assert(kr == KERN_SUCCESS);
2377
2378 for (i = 0; i < num_pages; i++) {
2379 ((int*)local_disp)[i] = ((vm_page_info_basic_t)info)[i].disposition;
2380 }
2381
2382 copy_sz = MIN(disp_buf_req_size, num_pages * sizeof(int) /* an int per page */);
2383 kr = copyout(local_disp, (mach_vm_address_t)dispositions_addr, copy_sz);
2384
2385 start += curr_sz;
2386 disp_buf_req_size -= copy_sz;
2387 disp_buf_total_size += copy_sz;
2388
2389 if (kr != 0) {
2390 break;
2391 }
2392
2393 if ((disp_buf_req_size == 0) || (curr_sz >= size)) {
2394 /*
2395 * We might have inspected the full range OR
2396 * more than it esp. if the user passed in
2397 * non-page aligned start/size and/or if we
2398 * descended into a submap. We are done here.
2399 */
2400
2401 size = 0;
2402 } else {
2403 dispositions_addr += copy_sz;
2404
2405 size -= curr_sz;
2406
2407 curr_sz = MIN(vm_map_round_page(size, effective_page_mask), MAX_PAGE_RANGE_QUERY);
2408 num_pages = (int)(curr_sz >> effective_page_shift);
2409 }
2410 }
2411
2412 *dispositions_count = disp_buf_total_size / sizeof(int);
2413
2414 out:
2415 kfree_data(local_disp, local_disp_size);
2416 kfree_data(info, info_size);
2417 return kr;
2418 }
2419
2420 kern_return_t
mach_vm_page_info(vm_map_t map,mach_vm_address_t address,vm_page_info_flavor_t flavor,vm_page_info_t info,mach_msg_type_number_t * count)2421 mach_vm_page_info(
2422 vm_map_t map,
2423 mach_vm_address_t address,
2424 vm_page_info_flavor_t flavor,
2425 vm_page_info_t info,
2426 mach_msg_type_number_t *count)
2427 {
2428 kern_return_t kr;
2429
2430 if (map == VM_MAP_NULL) {
2431 return KERN_INVALID_ARGUMENT;
2432 }
2433
2434 kr = vm_map_page_info(map, address, flavor, info, count);
2435 return kr;
2436 }
2437
2438 /* map a (whole) upl into an address space */
2439 kern_return_t
vm_upl_map(vm_map_t map,upl_t upl,vm_address_t * dst_addr)2440 vm_upl_map(
2441 vm_map_t map,
2442 upl_t upl,
2443 vm_address_t *dst_addr)
2444 {
2445 vm_map_offset_t map_addr;
2446 kern_return_t kr;
2447
2448 if (VM_MAP_NULL == map) {
2449 return KERN_INVALID_ARGUMENT;
2450 }
2451
2452 kr = vm_map_enter_upl(map, upl, &map_addr);
2453 *dst_addr = CAST_DOWN(vm_address_t, map_addr);
2454 return kr;
2455 }
2456
2457 kern_return_t
vm_upl_unmap(vm_map_t map,upl_t upl)2458 vm_upl_unmap(
2459 vm_map_t map,
2460 upl_t upl)
2461 {
2462 if (VM_MAP_NULL == map) {
2463 return KERN_INVALID_ARGUMENT;
2464 }
2465
2466 return vm_map_remove_upl(map, upl);
2467 }
2468
2469 /* map a part of a upl into an address space with requested protection. */
2470 kern_return_t
vm_upl_map_range(vm_map_t map,upl_t upl,vm_offset_t offset_to_map,vm_size_t size_to_map,vm_prot_t prot_to_map,vm_address_t * dst_addr)2471 vm_upl_map_range(
2472 vm_map_t map,
2473 upl_t upl,
2474 vm_offset_t offset_to_map,
2475 vm_size_t size_to_map,
2476 vm_prot_t prot_to_map,
2477 vm_address_t *dst_addr)
2478 {
2479 vm_map_offset_t map_addr, aligned_offset_to_map, adjusted_offset;
2480 kern_return_t kr;
2481
2482 if (VM_MAP_NULL == map) {
2483 return KERN_INVALID_ARGUMENT;
2484 }
2485 aligned_offset_to_map = VM_MAP_TRUNC_PAGE(offset_to_map, VM_MAP_PAGE_MASK(map));
2486 adjusted_offset = offset_to_map - aligned_offset_to_map;
2487 size_to_map = VM_MAP_ROUND_PAGE(size_to_map + adjusted_offset, VM_MAP_PAGE_MASK(map));
2488
2489 kr = vm_map_enter_upl_range(map, upl, aligned_offset_to_map, size_to_map, prot_to_map, &map_addr);
2490 *dst_addr = CAST_DOWN(vm_address_t, (map_addr + adjusted_offset));
2491 return kr;
2492 }
2493
2494 /* unmap a part of a upl that was mapped in the address space. */
2495 kern_return_t
vm_upl_unmap_range(vm_map_t map,upl_t upl,vm_offset_t offset_to_unmap,vm_size_t size_to_unmap)2496 vm_upl_unmap_range(
2497 vm_map_t map,
2498 upl_t upl,
2499 vm_offset_t offset_to_unmap,
2500 vm_size_t size_to_unmap)
2501 {
2502 vm_map_offset_t aligned_offset_to_unmap, page_offset;
2503
2504 if (VM_MAP_NULL == map) {
2505 return KERN_INVALID_ARGUMENT;
2506 }
2507
2508 aligned_offset_to_unmap = VM_MAP_TRUNC_PAGE(offset_to_unmap, VM_MAP_PAGE_MASK(map));
2509 page_offset = offset_to_unmap - aligned_offset_to_unmap;
2510 size_to_unmap = VM_MAP_ROUND_PAGE(size_to_unmap + page_offset, VM_MAP_PAGE_MASK(map));
2511
2512 return vm_map_remove_upl_range(map, upl, aligned_offset_to_unmap, size_to_unmap);
2513 }
2514
2515 /* Retrieve a upl for an object underlying an address range in a map */
2516
2517 kern_return_t
vm_map_get_upl(vm_map_t map,vm_map_offset_t map_offset,upl_size_t * upl_size,upl_t * upl,upl_page_info_array_t page_list,unsigned int * count,upl_control_flags_t * flags,vm_tag_t tag,int force_data_sync)2518 vm_map_get_upl(
2519 vm_map_t map,
2520 vm_map_offset_t map_offset,
2521 upl_size_t *upl_size,
2522 upl_t *upl,
2523 upl_page_info_array_t page_list,
2524 unsigned int *count,
2525 upl_control_flags_t *flags,
2526 vm_tag_t tag,
2527 int force_data_sync)
2528 {
2529 upl_control_flags_t map_flags;
2530 kern_return_t kr;
2531
2532 if (VM_MAP_NULL == map) {
2533 return KERN_INVALID_ARGUMENT;
2534 }
2535
2536 map_flags = *flags & ~UPL_NOZEROFILL;
2537 if (force_data_sync) {
2538 map_flags |= UPL_FORCE_DATA_SYNC;
2539 }
2540
2541 kr = vm_map_create_upl(map,
2542 map_offset,
2543 upl_size,
2544 upl,
2545 page_list,
2546 count,
2547 &map_flags,
2548 tag);
2549
2550 *flags = (map_flags & ~UPL_FORCE_DATA_SYNC);
2551 return kr;
2552 }
2553
2554 /*
2555 * mach_make_memory_entry_64
2556 *
2557 * Think of it as a two-stage vm_remap() operation. First
2558 * you get a handle. Second, you get map that handle in
2559 * somewhere else. Rather than doing it all at once (and
2560 * without needing access to the other whole map).
2561 */
2562 kern_return_t
mach_make_memory_entry_64(vm_map_t target_map,memory_object_size_t * size,memory_object_offset_t offset,vm_prot_t permission,ipc_port_t * object_handle,ipc_port_t parent_handle)2563 mach_make_memory_entry_64(
2564 vm_map_t target_map,
2565 memory_object_size_t *size,
2566 memory_object_offset_t offset,
2567 vm_prot_t permission,
2568 ipc_port_t *object_handle,
2569 ipc_port_t parent_handle)
2570 {
2571 vm_named_entry_kernel_flags_t vmne_kflags;
2572
2573 if ((permission & MAP_MEM_FLAGS_MASK) & ~MAP_MEM_FLAGS_USER) {
2574 /*
2575 * Unknown flag: reject for forward compatibility.
2576 */
2577 return KERN_INVALID_VALUE;
2578 }
2579
2580 vmne_kflags = VM_NAMED_ENTRY_KERNEL_FLAGS_NONE;
2581 if (permission & MAP_MEM_LEDGER_TAGGED) {
2582 vmne_kflags.vmnekf_ledger_tag = VM_LEDGER_TAG_DEFAULT;
2583 }
2584 return mach_make_memory_entry_internal(target_map,
2585 size,
2586 offset,
2587 permission,
2588 vmne_kflags,
2589 object_handle,
2590 parent_handle);
2591 }
2592
2593 kern_return_t
mach_make_memory_entry_internal(vm_map_t target_map,memory_object_size_t * size,memory_object_offset_t offset,vm_prot_t permission,vm_named_entry_kernel_flags_t vmne_kflags,ipc_port_t * object_handle,ipc_port_t parent_handle)2594 mach_make_memory_entry_internal(
2595 vm_map_t target_map,
2596 memory_object_size_t *size,
2597 memory_object_offset_t offset,
2598 vm_prot_t permission,
2599 vm_named_entry_kernel_flags_t vmne_kflags,
2600 ipc_port_t *object_handle,
2601 ipc_port_t parent_handle)
2602 {
2603 vm_named_entry_t parent_entry;
2604 vm_named_entry_t user_entry;
2605 kern_return_t kr = KERN_SUCCESS;
2606 vm_object_t object;
2607 vm_map_size_t map_size;
2608 vm_map_offset_t map_start, map_end;
2609
2610 /*
2611 * Stash the offset in the page for use by vm_map_enter_mem_object()
2612 * in the VM_FLAGS_RETURN_DATA_ADDR/MAP_MEM_USE_DATA_ADDR case.
2613 */
2614 vm_object_offset_t offset_in_page;
2615
2616 unsigned int access;
2617 vm_prot_t protections;
2618 vm_prot_t original_protections, mask_protections;
2619 unsigned int wimg_mode;
2620 boolean_t use_data_addr;
2621 boolean_t use_4K_compat;
2622
2623 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x\n", target_map, offset, *size, permission);
2624
2625 user_entry = NULL;
2626
2627 if ((permission & MAP_MEM_FLAGS_MASK) & ~MAP_MEM_FLAGS_ALL) {
2628 /*
2629 * Unknown flag: reject for forward compatibility.
2630 */
2631 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_VALUE);
2632 return KERN_INVALID_VALUE;
2633 }
2634
2635 parent_entry = mach_memory_entry_from_port(parent_handle);
2636
2637 if (parent_entry && parent_entry->is_copy) {
2638 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_ARGUMENT);
2639 return KERN_INVALID_ARGUMENT;
2640 }
2641
2642 if (target_map == NULL || target_map->pmap == kernel_pmap) {
2643 offset = pgz_decode(offset, *size);
2644 }
2645
2646 original_protections = permission & VM_PROT_ALL;
2647 protections = original_protections;
2648 mask_protections = permission & VM_PROT_IS_MASK;
2649 access = GET_MAP_MEM(permission);
2650 use_data_addr = ((permission & MAP_MEM_USE_DATA_ADDR) != 0);
2651 use_4K_compat = ((permission & MAP_MEM_4K_DATA_ADDR) != 0);
2652
2653 user_entry = NULL;
2654
2655 map_start = vm_map_trunc_page(offset, VM_MAP_PAGE_MASK(target_map));
2656
2657 if (permission & MAP_MEM_ONLY) {
2658 boolean_t parent_is_object;
2659
2660 map_end = vm_map_round_page(offset + *size, VM_MAP_PAGE_MASK(target_map));
2661 map_size = map_end - map_start;
2662
2663 if (use_data_addr || use_4K_compat || parent_entry == NULL) {
2664 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_ARGUMENT);
2665 return KERN_INVALID_ARGUMENT;
2666 }
2667
2668 parent_is_object = parent_entry->is_object;
2669 if (!parent_is_object) {
2670 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_ARGUMENT);
2671 return KERN_INVALID_ARGUMENT;
2672 }
2673 object = vm_named_entry_to_vm_object(parent_entry);
2674 if (parent_is_object && object != VM_OBJECT_NULL) {
2675 wimg_mode = object->wimg_bits;
2676 } else {
2677 wimg_mode = VM_WIMG_USE_DEFAULT;
2678 }
2679 if ((access != parent_entry->access) &&
2680 !(parent_entry->protection & VM_PROT_WRITE)) {
2681 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_RIGHT);
2682 return KERN_INVALID_RIGHT;
2683 }
2684 vm_prot_to_wimg(access, &wimg_mode);
2685 if (access != MAP_MEM_NOOP) {
2686 parent_entry->access = access;
2687 }
2688 if (parent_is_object && object &&
2689 (access != MAP_MEM_NOOP) &&
2690 (!(object->nophyscache))) {
2691 if (object->wimg_bits != wimg_mode) {
2692 vm_object_lock(object);
2693 vm_object_change_wimg_mode(object, wimg_mode);
2694 vm_object_unlock(object);
2695 }
2696 }
2697 if (object_handle) {
2698 *object_handle = IP_NULL;
2699 }
2700 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_SUCCESS);
2701 return KERN_SUCCESS;
2702 } else if (permission & MAP_MEM_NAMED_CREATE) {
2703 int ledger_flags = 0;
2704 task_t owner;
2705 bool fully_owned = false;
2706
2707 map_end = vm_map_round_page(offset + *size, VM_MAP_PAGE_MASK(target_map));
2708 map_size = map_end - map_start;
2709
2710 if (use_data_addr || use_4K_compat) {
2711 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_ARGUMENT);
2712 return KERN_INVALID_ARGUMENT;
2713 }
2714
2715 if (map_size == 0) {
2716 *size = 0;
2717 *object_handle = IPC_PORT_NULL;
2718 return KERN_SUCCESS;
2719 }
2720
2721 /*
2722 * Force the creation of the VM object now.
2723 */
2724 #if __LP64__
2725 if (map_size > ANON_MAX_SIZE) {
2726 kr = KERN_FAILURE;
2727 goto make_mem_done;
2728 }
2729 #endif /* __LP64__ */
2730
2731 object = vm_object_allocate(map_size);
2732 assert(object != VM_OBJECT_NULL);
2733
2734 /*
2735 * XXX
2736 * We use this path when we want to make sure that
2737 * nobody messes with the object (coalesce, for
2738 * example) before we map it.
2739 * We might want to use these objects for transposition via
2740 * vm_object_transpose() too, so we don't want any copy or
2741 * shadow objects either...
2742 */
2743 object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
2744 object->true_share = TRUE;
2745
2746 owner = current_task();
2747 if ((permission & MAP_MEM_PURGABLE) ||
2748 vmne_kflags.vmnekf_ledger_tag) {
2749 assert(object->vo_owner == NULL);
2750 assert(object->resident_page_count == 0);
2751 assert(object->wired_page_count == 0);
2752 assert(owner != TASK_NULL);
2753 if (vmne_kflags.vmnekf_ledger_no_footprint) {
2754 ledger_flags |= VM_LEDGER_FLAG_NO_FOOTPRINT;
2755 object->vo_no_footprint = TRUE;
2756 }
2757 if (permission & MAP_MEM_PURGABLE) {
2758 if (!(permission & VM_PROT_WRITE)) {
2759 /* if we can't write, we can't purge */
2760 vm_object_deallocate(object);
2761 kr = KERN_INVALID_ARGUMENT;
2762 goto make_mem_done;
2763 }
2764 object->purgable = VM_PURGABLE_NONVOLATILE;
2765 if (permission & MAP_MEM_PURGABLE_KERNEL_ONLY) {
2766 object->purgeable_only_by_kernel = TRUE;
2767 }
2768 #if __arm64__
2769 if (owner->task_legacy_footprint) {
2770 /*
2771 * For ios11, we failed to account for
2772 * this memory. Keep doing that for
2773 * legacy apps (built before ios12),
2774 * for backwards compatibility's sake...
2775 */
2776 owner = kernel_task;
2777 }
2778 #endif /* __arm64__ */
2779 vm_object_lock(object);
2780 vm_purgeable_nonvolatile_enqueue(object, owner);
2781 vm_object_unlock(object);
2782 /* all memory in this named entry is "owned" */
2783 fully_owned = true;
2784 }
2785 }
2786
2787 if (vmne_kflags.vmnekf_ledger_tag) {
2788 /*
2789 * Bill this object to the current task's
2790 * ledgers for the given tag.
2791 */
2792 if (vmne_kflags.vmnekf_ledger_no_footprint) {
2793 ledger_flags |= VM_LEDGER_FLAG_NO_FOOTPRINT;
2794 }
2795 vm_object_lock(object);
2796 object->vo_ledger_tag = vmne_kflags.vmnekf_ledger_tag;
2797 kr = vm_object_ownership_change(
2798 object,
2799 vmne_kflags.vmnekf_ledger_tag,
2800 owner, /* new owner */
2801 ledger_flags,
2802 FALSE); /* task_objq locked? */
2803 vm_object_unlock(object);
2804 if (kr != KERN_SUCCESS) {
2805 vm_object_deallocate(object);
2806 goto make_mem_done;
2807 }
2808 /* all memory in this named entry is "owned" */
2809 fully_owned = true;
2810 }
2811
2812 #if CONFIG_SECLUDED_MEMORY
2813 if (secluded_for_iokit && /* global boot-arg */
2814 ((permission & MAP_MEM_GRAB_SECLUDED))) {
2815 object->can_grab_secluded = TRUE;
2816 assert(!object->eligible_for_secluded);
2817 }
2818 #endif /* CONFIG_SECLUDED_MEMORY */
2819
2820 /*
2821 * The VM object is brand new and nobody else knows about it,
2822 * so we don't need to lock it.
2823 */
2824
2825 wimg_mode = object->wimg_bits;
2826 vm_prot_to_wimg(access, &wimg_mode);
2827 if (access != MAP_MEM_NOOP) {
2828 object->wimg_bits = wimg_mode;
2829 }
2830
2831 /* the object has no pages, so no WIMG bits to update here */
2832
2833 user_entry = mach_memory_entry_allocate(object_handle);
2834 vm_named_entry_associate_vm_object(
2835 user_entry,
2836 object,
2837 0,
2838 map_size,
2839 (protections & VM_PROT_ALL));
2840 user_entry->internal = TRUE;
2841 user_entry->is_sub_map = FALSE;
2842 user_entry->offset = 0;
2843 user_entry->data_offset = 0;
2844 user_entry->protection = protections;
2845 user_entry->access = access;
2846 user_entry->size = map_size;
2847 user_entry->is_fully_owned = fully_owned;
2848
2849 /* user_object pager and internal fields are not used */
2850 /* when the object field is filled in. */
2851
2852 *size = CAST_DOWN(vm_size_t, (user_entry->size -
2853 user_entry->data_offset));
2854 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_SUCCESS);
2855 return KERN_SUCCESS;
2856 }
2857
2858 if (permission & MAP_MEM_VM_COPY) {
2859 vm_map_copy_t copy;
2860
2861 if (target_map == VM_MAP_NULL) {
2862 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_TASK);
2863 return KERN_INVALID_TASK;
2864 }
2865
2866 map_end = vm_map_round_page(offset + *size, VM_MAP_PAGE_MASK(target_map));
2867 map_size = map_end - map_start;
2868 if (map_size == 0) {
2869 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_ARGUMENT);
2870 return KERN_INVALID_ARGUMENT;
2871 }
2872
2873 if (use_data_addr || use_4K_compat) {
2874 offset_in_page = offset - map_start;
2875 if (use_4K_compat) {
2876 offset_in_page &= ~((signed)(0xFFF));
2877 }
2878 } else {
2879 offset_in_page = 0;
2880 }
2881
2882 kr = vm_map_copyin_internal(target_map,
2883 map_start,
2884 map_size,
2885 VM_MAP_COPYIN_ENTRY_LIST,
2886 ©);
2887 if (kr != KERN_SUCCESS) {
2888 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, kr);
2889 return kr;
2890 }
2891 assert(copy != VM_MAP_COPY_NULL);
2892
2893 user_entry = mach_memory_entry_allocate(object_handle);
2894 user_entry->backing.copy = copy;
2895 user_entry->internal = FALSE;
2896 user_entry->is_sub_map = FALSE;
2897 user_entry->is_copy = TRUE;
2898 user_entry->offset = 0;
2899 user_entry->protection = protections;
2900 user_entry->size = map_size;
2901 user_entry->data_offset = offset_in_page;
2902
2903 /* is all memory in this named entry "owned"? */
2904 vm_map_entry_t entry;
2905 user_entry->is_fully_owned = TRUE;
2906 for (entry = vm_map_copy_first_entry(copy);
2907 entry != vm_map_copy_to_entry(copy);
2908 entry = entry->vme_next) {
2909 if (entry->is_sub_map ||
2910 VME_OBJECT(entry) == VM_OBJECT_NULL ||
2911 VM_OBJECT_OWNER(VME_OBJECT(entry)) == TASK_NULL) {
2912 /* this memory is not "owned" */
2913 user_entry->is_fully_owned = FALSE;
2914 break;
2915 }
2916 }
2917
2918 *size = CAST_DOWN(vm_size_t, (user_entry->size -
2919 user_entry->data_offset));
2920 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_SUCCESS);
2921 return KERN_SUCCESS;
2922 }
2923
2924 if ((permission & MAP_MEM_VM_SHARE)
2925 || parent_entry == NULL
2926 || (permission & MAP_MEM_NAMED_REUSE)) {
2927 vm_map_copy_t copy;
2928 vm_prot_t cur_prot, max_prot;
2929 vm_map_kernel_flags_t vmk_flags;
2930 vm_map_entry_t parent_copy_entry;
2931
2932 if (target_map == VM_MAP_NULL) {
2933 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_TASK);
2934 return KERN_INVALID_TASK;
2935 }
2936
2937 map_end = vm_map_round_page(offset + *size, VM_MAP_PAGE_MASK(target_map));
2938 vmk_flags = VM_MAP_KERNEL_FLAGS_NONE;
2939 vmk_flags.vmkf_range_id = KMEM_RANGE_ID_DATA;
2940 parent_copy_entry = VM_MAP_ENTRY_NULL;
2941 if (!(permission & MAP_MEM_VM_SHARE)) {
2942 vm_map_t tmp_map, real_map;
2943 vm_map_version_t version;
2944 vm_object_t tmp_object;
2945 vm_object_offset_t obj_off;
2946 vm_prot_t prot;
2947 boolean_t wired;
2948 bool contended;
2949
2950 /* resolve any pending submap copy-on-write... */
2951 if (protections & VM_PROT_WRITE) {
2952 tmp_map = target_map;
2953 vm_map_lock_read(tmp_map);
2954 kr = vm_map_lookup_and_lock_object(&tmp_map,
2955 map_start,
2956 protections | mask_protections,
2957 OBJECT_LOCK_EXCLUSIVE,
2958 &version,
2959 &tmp_object,
2960 &obj_off,
2961 &prot,
2962 &wired,
2963 NULL, /* fault_info */
2964 &real_map,
2965 &contended);
2966 if (kr != KERN_SUCCESS) {
2967 vm_map_unlock_read(tmp_map);
2968 } else {
2969 vm_object_unlock(tmp_object);
2970 vm_map_unlock_read(tmp_map);
2971 if (real_map != tmp_map) {
2972 vm_map_unlock_read(real_map);
2973 }
2974 }
2975 }
2976 /* ... and carry on */
2977
2978 /* stop extracting if VM object changes */
2979 vmk_flags.vmkf_copy_single_object = TRUE;
2980 if ((permission & MAP_MEM_NAMED_REUSE) &&
2981 parent_entry != NULL &&
2982 parent_entry->is_object) {
2983 vm_map_copy_t parent_copy;
2984 parent_copy = parent_entry->backing.copy;
2985 /*
2986 * Assert that the vm_map_copy is coming from the right
2987 * zone and hasn't been forged
2988 */
2989 vm_map_copy_require(parent_copy);
2990 assert(parent_copy->cpy_hdr.nentries == 1);
2991 parent_copy_entry = vm_map_copy_first_entry(parent_copy);
2992 assert(!parent_copy_entry->is_sub_map);
2993 }
2994 }
2995
2996 map_size = map_end - map_start;
2997 if (map_size == 0) {
2998 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_INVALID_ARGUMENT);
2999 return KERN_INVALID_ARGUMENT;
3000 }
3001
3002 if (use_data_addr || use_4K_compat) {
3003 offset_in_page = offset - map_start;
3004 if (use_4K_compat) {
3005 offset_in_page &= ~((signed)(0xFFF));
3006 }
3007 } else {
3008 offset_in_page = 0;
3009 }
3010
3011 if (mask_protections) {
3012 /*
3013 * caller is asking for whichever proctections are
3014 * available: no required protections.
3015 */
3016 cur_prot = VM_PROT_NONE;
3017 max_prot = VM_PROT_NONE;
3018 } else {
3019 /*
3020 * Caller wants a memory entry with "protections".
3021 * Make sure we extract only memory that matches that.
3022 */
3023 cur_prot = protections;
3024 max_prot = protections;
3025 }
3026 if (target_map->pmap == kernel_pmap) {
3027 /*
3028 * Get "reserved" map entries to avoid deadlocking
3029 * on the kernel map or a kernel submap if we
3030 * run out of VM map entries and need to refill that
3031 * zone.
3032 */
3033 vmk_flags.vmkf_copy_pageable = FALSE;
3034 } else {
3035 vmk_flags.vmkf_copy_pageable = TRUE;
3036 }
3037 vmk_flags.vmkf_copy_same_map = FALSE;
3038 assert(map_size != 0);
3039 kr = vm_map_copy_extract(target_map,
3040 map_start,
3041 map_size,
3042 FALSE, /* copy */
3043 ©,
3044 &cur_prot,
3045 &max_prot,
3046 VM_INHERIT_SHARE,
3047 vmk_flags);
3048 if (kr != KERN_SUCCESS) {
3049 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, kr);
3050 if (VM_MAP_PAGE_SHIFT(target_map) < PAGE_SHIFT) {
3051 // panic("DEBUG4K %s:%d kr 0x%x", __FUNCTION__, __LINE__, kr);
3052 }
3053 return kr;
3054 }
3055 assert(copy != VM_MAP_COPY_NULL);
3056
3057 if (mask_protections) {
3058 /*
3059 * We just want as much of "original_protections"
3060 * as we can get out of the actual "cur_prot".
3061 */
3062 protections &= cur_prot;
3063 if (protections == VM_PROT_NONE) {
3064 /* no access at all: fail */
3065 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_PROTECTION_FAILURE);
3066 if (VM_MAP_PAGE_SHIFT(target_map) < PAGE_SHIFT) {
3067 // panic("DEBUG4K %s:%d kr 0x%x", __FUNCTION__, __LINE__, kr);
3068 }
3069 vm_map_copy_discard(copy);
3070 return KERN_PROTECTION_FAILURE;
3071 }
3072 } else {
3073 /*
3074 * We want exactly "original_protections"
3075 * out of "cur_prot".
3076 */
3077 assert((cur_prot & protections) == protections);
3078 assert((max_prot & protections) == protections);
3079 /* XXX FBDP TODO: no longer needed? */
3080 if ((cur_prot & protections) != protections) {
3081 if (VM_MAP_PAGE_SHIFT(target_map) < PAGE_SHIFT) {
3082 // panic("DEBUG4K %s:%d kr 0x%x", __FUNCTION__, __LINE__, KERN_PROTECTION_FAILURE);
3083 }
3084 vm_map_copy_discard(copy);
3085 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_PROTECTION_FAILURE);
3086 return KERN_PROTECTION_FAILURE;
3087 }
3088 }
3089
3090 if (!(permission & MAP_MEM_VM_SHARE)) {
3091 vm_map_entry_t copy_entry;
3092
3093 /* limit size to what's actually covered by "copy" */
3094 assert(copy->cpy_hdr.nentries == 1);
3095 copy_entry = vm_map_copy_first_entry(copy);
3096 map_size = copy_entry->vme_end - copy_entry->vme_start;
3097
3098 if ((permission & MAP_MEM_NAMED_REUSE) &&
3099 parent_copy_entry != VM_MAP_ENTRY_NULL &&
3100 VME_OBJECT(copy_entry) == VME_OBJECT(parent_copy_entry) &&
3101 VME_OFFSET(copy_entry) == VME_OFFSET(parent_copy_entry) &&
3102 parent_entry->offset == 0 &&
3103 parent_entry->size == map_size &&
3104 (parent_entry->data_offset == offset_in_page)) {
3105 /* we have a match: re-use "parent_entry" */
3106
3107 /* release our new "copy" */
3108 vm_map_copy_discard(copy);
3109 /* get extra send right on handle */
3110 parent_handle = ipc_port_copy_send_any(parent_handle);
3111
3112 *size = CAST_DOWN(vm_size_t,
3113 (parent_entry->size -
3114 parent_entry->data_offset));
3115 *object_handle = parent_handle;
3116 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_SUCCESS);
3117 return KERN_SUCCESS;
3118 }
3119
3120 /* no match: we need to create a new entry */
3121 object = VME_OBJECT(copy_entry);
3122 vm_object_lock(object);
3123 wimg_mode = object->wimg_bits;
3124 if (!(object->nophyscache)) {
3125 vm_prot_to_wimg(access, &wimg_mode);
3126 }
3127 if (object->wimg_bits != wimg_mode) {
3128 vm_object_change_wimg_mode(object, wimg_mode);
3129 }
3130 vm_object_unlock(object);
3131 }
3132
3133 user_entry = mach_memory_entry_allocate(object_handle);
3134 user_entry->backing.copy = copy;
3135 user_entry->is_sub_map = FALSE;
3136 user_entry->is_object = FALSE;
3137 user_entry->internal = FALSE;
3138 user_entry->protection = protections;
3139 user_entry->size = map_size;
3140 user_entry->data_offset = offset_in_page;
3141
3142 if (permission & MAP_MEM_VM_SHARE) {
3143 vm_map_entry_t copy_entry;
3144
3145 user_entry->is_copy = TRUE;
3146 user_entry->offset = 0;
3147
3148 /* is all memory in this named entry "owned"? */
3149 user_entry->is_fully_owned = TRUE;
3150 for (copy_entry = vm_map_copy_first_entry(copy);
3151 copy_entry != vm_map_copy_to_entry(copy);
3152 copy_entry = copy_entry->vme_next) {
3153 if (copy_entry->is_sub_map ||
3154 VM_OBJECT_OWNER(VME_OBJECT(copy_entry)) == TASK_NULL) {
3155 /* this memory is not "owned" */
3156 user_entry->is_fully_owned = FALSE;
3157 break;
3158 }
3159 }
3160 } else {
3161 user_entry->is_object = TRUE;
3162 user_entry->internal = object->internal;
3163 user_entry->offset = VME_OFFSET(vm_map_copy_first_entry(copy));
3164 user_entry->access = GET_MAP_MEM(permission);
3165 /* is all memory in this named entry "owned"? */
3166 if (VM_OBJECT_OWNER(vm_named_entry_to_vm_object(user_entry)) != TASK_NULL) {
3167 user_entry->is_fully_owned = TRUE;
3168 }
3169 }
3170
3171 *size = CAST_DOWN(vm_size_t, (user_entry->size -
3172 user_entry->data_offset));
3173 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_SUCCESS);
3174 return KERN_SUCCESS;
3175 }
3176
3177 /* The new object will be based on an existing named object */
3178 if (parent_entry == NULL) {
3179 kr = KERN_INVALID_ARGUMENT;
3180 goto make_mem_done;
3181 }
3182
3183 if (parent_entry->is_copy) {
3184 panic("parent_entry %p is_copy not supported", parent_entry);
3185 kr = KERN_INVALID_ARGUMENT;
3186 goto make_mem_done;
3187 }
3188
3189 if (use_data_addr || use_4K_compat) {
3190 /*
3191 * submaps and pagers should only be accessible from within
3192 * the kernel, which shouldn't use the data address flag, so can fail here.
3193 */
3194 if (parent_entry->is_sub_map) {
3195 panic("Shouldn't be using data address with a parent entry that is a submap.");
3196 }
3197 /*
3198 * Account for offset to data in parent entry and
3199 * compute our own offset to data.
3200 */
3201 if ((offset + *size + parent_entry->data_offset) > parent_entry->size) {
3202 kr = KERN_INVALID_ARGUMENT;
3203 goto make_mem_done;
3204 }
3205
3206 map_start = vm_map_trunc_page(offset + parent_entry->data_offset, PAGE_MASK);
3207 offset_in_page = (offset + parent_entry->data_offset) - map_start;
3208 if (use_4K_compat) {
3209 offset_in_page &= ~((signed)(0xFFF));
3210 }
3211 map_end = vm_map_round_page(offset + parent_entry->data_offset + *size, PAGE_MASK);
3212 map_size = map_end - map_start;
3213 } else {
3214 map_end = vm_map_round_page(offset + *size, PAGE_MASK);
3215 map_size = map_end - map_start;
3216 offset_in_page = 0;
3217
3218 if ((offset + map_size) > parent_entry->size) {
3219 kr = KERN_INVALID_ARGUMENT;
3220 goto make_mem_done;
3221 }
3222 }
3223
3224 if (mask_protections) {
3225 /*
3226 * The caller asked us to use the "protections" as
3227 * a mask, so restrict "protections" to what this
3228 * mapping actually allows.
3229 */
3230 protections &= parent_entry->protection;
3231 }
3232 if ((protections & parent_entry->protection) != protections) {
3233 kr = KERN_PROTECTION_FAILURE;
3234 goto make_mem_done;
3235 }
3236
3237 user_entry = mach_memory_entry_allocate(object_handle);
3238 user_entry->size = map_size;
3239 user_entry->offset = parent_entry->offset + map_start;
3240 user_entry->data_offset = offset_in_page;
3241 user_entry->is_sub_map = parent_entry->is_sub_map;
3242 user_entry->is_copy = parent_entry->is_copy;
3243 user_entry->internal = parent_entry->internal;
3244 user_entry->protection = protections;
3245
3246 if (access != MAP_MEM_NOOP) {
3247 user_entry->access = access;
3248 }
3249
3250 if (parent_entry->is_sub_map) {
3251 vm_map_t map = parent_entry->backing.map;
3252 vm_map_reference(map);
3253 user_entry->backing.map = map;
3254 } else {
3255 object = vm_named_entry_to_vm_object(parent_entry);
3256 assert(object != VM_OBJECT_NULL);
3257 assert(object->copy_strategy != MEMORY_OBJECT_COPY_SYMMETRIC);
3258 vm_named_entry_associate_vm_object(
3259 user_entry,
3260 object,
3261 user_entry->offset,
3262 user_entry->size,
3263 (user_entry->protection & VM_PROT_ALL));
3264 assert(user_entry->is_object);
3265 /* we now point to this object, hold on */
3266 vm_object_lock(object);
3267 vm_object_reference_locked(object);
3268 #if VM_OBJECT_TRACKING_OP_TRUESHARE
3269 if (!object->true_share &&
3270 vm_object_tracking_btlog) {
3271 btlog_record(vm_object_tracking_btlog, object,
3272 VM_OBJECT_TRACKING_OP_TRUESHARE,
3273 btref_get(__builtin_frame_address(0), 0));
3274 }
3275 #endif /* VM_OBJECT_TRACKING_OP_TRUESHARE */
3276
3277 object->true_share = TRUE;
3278 if (object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC) {
3279 object->copy_strategy = MEMORY_OBJECT_COPY_DELAY;
3280 }
3281 vm_object_unlock(object);
3282 }
3283 *size = CAST_DOWN(vm_size_t, (user_entry->size -
3284 user_entry->data_offset));
3285 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, KERN_SUCCESS);
3286 return KERN_SUCCESS;
3287
3288 make_mem_done:
3289 DEBUG4K_MEMENTRY("map %p offset 0x%llx size 0x%llx prot 0x%x -> entry %p kr 0x%x\n", target_map, offset, *size, permission, user_entry, kr);
3290 return kr;
3291 }
3292
3293 kern_return_t
_mach_make_memory_entry(vm_map_t target_map,memory_object_size_t * size,memory_object_offset_t offset,vm_prot_t permission,ipc_port_t * object_handle,ipc_port_t parent_entry)3294 _mach_make_memory_entry(
3295 vm_map_t target_map,
3296 memory_object_size_t *size,
3297 memory_object_offset_t offset,
3298 vm_prot_t permission,
3299 ipc_port_t *object_handle,
3300 ipc_port_t parent_entry)
3301 {
3302 memory_object_size_t mo_size;
3303 kern_return_t kr;
3304
3305 mo_size = (memory_object_size_t)*size;
3306 kr = mach_make_memory_entry_64(target_map, &mo_size,
3307 (memory_object_offset_t)offset, permission, object_handle,
3308 parent_entry);
3309 *size = mo_size;
3310 return kr;
3311 }
3312
3313 kern_return_t
mach_make_memory_entry(vm_map_t target_map,vm_size_t * size,vm_offset_t offset,vm_prot_t permission,ipc_port_t * object_handle,ipc_port_t parent_entry)3314 mach_make_memory_entry(
3315 vm_map_t target_map,
3316 vm_size_t *size,
3317 vm_offset_t offset,
3318 vm_prot_t permission,
3319 ipc_port_t *object_handle,
3320 ipc_port_t parent_entry)
3321 {
3322 memory_object_size_t mo_size;
3323 kern_return_t kr;
3324
3325 mo_size = (memory_object_size_t)*size;
3326 kr = mach_make_memory_entry_64(target_map, &mo_size,
3327 (memory_object_offset_t)offset, permission, object_handle,
3328 parent_entry);
3329 *size = CAST_DOWN(vm_size_t, mo_size);
3330 return kr;
3331 }
3332
3333 /*
3334 * task_wire
3335 *
3336 * Set or clear the map's wiring_required flag. This flag, if set,
3337 * will cause all future virtual memory allocation to allocate
3338 * user wired memory. Unwiring pages wired down as a result of
3339 * this routine is done with the vm_wire interface.
3340 */
3341 kern_return_t
task_wire(vm_map_t map,boolean_t must_wire)3342 task_wire(
3343 vm_map_t map,
3344 boolean_t must_wire)
3345 {
3346 if (map == VM_MAP_NULL) {
3347 return KERN_INVALID_ARGUMENT;
3348 }
3349
3350 vm_map_lock(map);
3351 map->wiring_required = (must_wire == TRUE);
3352 vm_map_unlock(map);
3353
3354 return KERN_SUCCESS;
3355 }
3356
3357 kern_return_t
vm_map_exec_lockdown(vm_map_t map)3358 vm_map_exec_lockdown(
3359 vm_map_t map)
3360 {
3361 if (map == VM_MAP_NULL) {
3362 return KERN_INVALID_ARGUMENT;
3363 }
3364
3365 vm_map_lock(map);
3366 map->map_disallow_new_exec = TRUE;
3367 vm_map_unlock(map);
3368
3369 return KERN_SUCCESS;
3370 }
3371
3372 __private_extern__ vm_named_entry_t
mach_memory_entry_allocate(ipc_port_t * user_handle_p)3373 mach_memory_entry_allocate(ipc_port_t *user_handle_p)
3374 {
3375 vm_named_entry_t user_entry;
3376
3377 user_entry = kalloc_type(struct vm_named_entry,
3378 Z_WAITOK | Z_ZERO | Z_NOFAIL);
3379 named_entry_lock_init(user_entry);
3380
3381 *user_handle_p = ipc_kobject_alloc_port((ipc_kobject_t)user_entry,
3382 IKOT_NAMED_ENTRY,
3383 IPC_KOBJECT_ALLOC_MAKE_SEND | IPC_KOBJECT_ALLOC_NSREQUEST);
3384
3385 #if VM_NAMED_ENTRY_DEBUG
3386 /* backtrace at allocation time, for debugging only */
3387 user_entry->named_entry_bt = btref_get(__builtin_frame_address(0), 0);
3388 #endif /* VM_NAMED_ENTRY_DEBUG */
3389 return user_entry;
3390 }
3391
3392 /*
3393 * mach_memory_object_memory_entry_64
3394 *
3395 * Create a named entry backed by the provided pager.
3396 *
3397 */
3398 kern_return_t
mach_memory_object_memory_entry_64(host_t host,boolean_t internal,vm_object_offset_t size,vm_prot_t permission,memory_object_t pager,ipc_port_t * entry_handle)3399 mach_memory_object_memory_entry_64(
3400 host_t host,
3401 boolean_t internal,
3402 vm_object_offset_t size,
3403 vm_prot_t permission,
3404 memory_object_t pager,
3405 ipc_port_t *entry_handle)
3406 {
3407 vm_named_entry_t user_entry;
3408 ipc_port_t user_handle;
3409 vm_object_t object;
3410
3411 if (host == HOST_NULL) {
3412 return KERN_INVALID_HOST;
3413 }
3414
3415 size = vm_object_round_page(size);
3416
3417 if (pager == MEMORY_OBJECT_NULL && internal) {
3418 object = vm_object_allocate(size);
3419 if (object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC) {
3420 object->copy_strategy = MEMORY_OBJECT_COPY_DELAY;
3421 }
3422 } else {
3423 object = memory_object_to_vm_object(pager);
3424 if (object != VM_OBJECT_NULL) {
3425 vm_object_reference(object);
3426 }
3427 }
3428 if (object == VM_OBJECT_NULL) {
3429 return KERN_INVALID_ARGUMENT;
3430 }
3431
3432 user_entry = mach_memory_entry_allocate(&user_handle);
3433 user_entry->size = size;
3434 user_entry->offset = 0;
3435 user_entry->protection = permission & VM_PROT_ALL;
3436 user_entry->access = GET_MAP_MEM(permission);
3437 user_entry->is_sub_map = FALSE;
3438
3439 vm_named_entry_associate_vm_object(user_entry, object, 0, size,
3440 (user_entry->protection & VM_PROT_ALL));
3441 user_entry->internal = object->internal;
3442 assert(object->internal == internal);
3443 if (VM_OBJECT_OWNER(object) != TASK_NULL) {
3444 /* all memory in this entry is "owned" */
3445 user_entry->is_fully_owned = TRUE;
3446 }
3447
3448 *entry_handle = user_handle;
3449 return KERN_SUCCESS;
3450 }
3451
3452 kern_return_t
mach_memory_object_memory_entry(host_t host,boolean_t internal,vm_size_t size,vm_prot_t permission,memory_object_t pager,ipc_port_t * entry_handle)3453 mach_memory_object_memory_entry(
3454 host_t host,
3455 boolean_t internal,
3456 vm_size_t size,
3457 vm_prot_t permission,
3458 memory_object_t pager,
3459 ipc_port_t *entry_handle)
3460 {
3461 return mach_memory_object_memory_entry_64( host, internal,
3462 (vm_object_offset_t)size, permission, pager, entry_handle);
3463 }
3464
3465
3466 kern_return_t
mach_memory_entry_purgable_control(ipc_port_t entry_port,vm_purgable_t control,int * state)3467 mach_memory_entry_purgable_control(
3468 ipc_port_t entry_port,
3469 vm_purgable_t control,
3470 int *state)
3471 {
3472 if (control == VM_PURGABLE_SET_STATE_FROM_KERNEL) {
3473 /* not allowed from user-space */
3474 return KERN_INVALID_ARGUMENT;
3475 }
3476
3477 return memory_entry_purgeable_control_internal(entry_port, control, state);
3478 }
3479
3480 kern_return_t
memory_entry_purgeable_control_internal(ipc_port_t entry_port,vm_purgable_t control,int * state)3481 memory_entry_purgeable_control_internal(
3482 ipc_port_t entry_port,
3483 vm_purgable_t control,
3484 int *state)
3485 {
3486 kern_return_t kr;
3487 vm_named_entry_t mem_entry;
3488 vm_object_t object;
3489
3490 mem_entry = mach_memory_entry_from_port(entry_port);
3491 if (mem_entry == NULL) {
3492 return KERN_INVALID_ARGUMENT;
3493 }
3494
3495 if (control != VM_PURGABLE_SET_STATE &&
3496 control != VM_PURGABLE_GET_STATE &&
3497 control != VM_PURGABLE_SET_STATE_FROM_KERNEL) {
3498 return KERN_INVALID_ARGUMENT;
3499 }
3500
3501 if ((control == VM_PURGABLE_SET_STATE ||
3502 control == VM_PURGABLE_SET_STATE_FROM_KERNEL) &&
3503 (((*state & ~(VM_PURGABLE_ALL_MASKS)) != 0) ||
3504 ((*state & VM_PURGABLE_STATE_MASK) > VM_PURGABLE_STATE_MASK))) {
3505 return KERN_INVALID_ARGUMENT;
3506 }
3507
3508 named_entry_lock(mem_entry);
3509
3510 if (mem_entry->is_sub_map ||
3511 mem_entry->is_copy) {
3512 named_entry_unlock(mem_entry);
3513 return KERN_INVALID_ARGUMENT;
3514 }
3515
3516 assert(mem_entry->is_object);
3517 object = vm_named_entry_to_vm_object(mem_entry);
3518 if (object == VM_OBJECT_NULL) {
3519 named_entry_unlock(mem_entry);
3520 return KERN_INVALID_ARGUMENT;
3521 }
3522
3523 vm_object_lock(object);
3524
3525 /* check that named entry covers entire object ? */
3526 if (mem_entry->offset != 0 || object->vo_size != mem_entry->size) {
3527 vm_object_unlock(object);
3528 named_entry_unlock(mem_entry);
3529 return KERN_INVALID_ARGUMENT;
3530 }
3531
3532 named_entry_unlock(mem_entry);
3533
3534 kr = vm_object_purgable_control(object, control, state);
3535
3536 vm_object_unlock(object);
3537
3538 return kr;
3539 }
3540
3541 kern_return_t
mach_memory_entry_access_tracking(ipc_port_t entry_port,int * access_tracking,uint32_t * access_tracking_reads,uint32_t * access_tracking_writes)3542 mach_memory_entry_access_tracking(
3543 ipc_port_t entry_port,
3544 int *access_tracking,
3545 uint32_t *access_tracking_reads,
3546 uint32_t *access_tracking_writes)
3547 {
3548 return memory_entry_access_tracking_internal(entry_port,
3549 access_tracking,
3550 access_tracking_reads,
3551 access_tracking_writes);
3552 }
3553
3554 kern_return_t
memory_entry_access_tracking_internal(ipc_port_t entry_port,int * access_tracking,uint32_t * access_tracking_reads,uint32_t * access_tracking_writes)3555 memory_entry_access_tracking_internal(
3556 ipc_port_t entry_port,
3557 int *access_tracking,
3558 uint32_t *access_tracking_reads,
3559 uint32_t *access_tracking_writes)
3560 {
3561 vm_named_entry_t mem_entry;
3562 vm_object_t object;
3563 kern_return_t kr;
3564
3565 mem_entry = mach_memory_entry_from_port(entry_port);
3566 if (mem_entry == NULL) {
3567 return KERN_INVALID_ARGUMENT;
3568 }
3569
3570 named_entry_lock(mem_entry);
3571
3572 if (mem_entry->is_sub_map ||
3573 mem_entry->is_copy) {
3574 named_entry_unlock(mem_entry);
3575 return KERN_INVALID_ARGUMENT;
3576 }
3577
3578 assert(mem_entry->is_object);
3579 object = vm_named_entry_to_vm_object(mem_entry);
3580 if (object == VM_OBJECT_NULL) {
3581 named_entry_unlock(mem_entry);
3582 return KERN_INVALID_ARGUMENT;
3583 }
3584
3585 #if VM_OBJECT_ACCESS_TRACKING
3586 vm_object_access_tracking(object,
3587 access_tracking,
3588 access_tracking_reads,
3589 access_tracking_writes);
3590 kr = KERN_SUCCESS;
3591 #else /* VM_OBJECT_ACCESS_TRACKING */
3592 (void) access_tracking;
3593 (void) access_tracking_reads;
3594 (void) access_tracking_writes;
3595 kr = KERN_NOT_SUPPORTED;
3596 #endif /* VM_OBJECT_ACCESS_TRACKING */
3597
3598 named_entry_unlock(mem_entry);
3599
3600 return kr;
3601 }
3602
3603 #if DEVELOPMENT || DEBUG
3604 /* For dtrace probe in mach_memory_entry_ownership */
3605 extern int proc_selfpid(void);
3606 extern char *proc_name_address(void *p);
3607 #endif /* DEVELOPMENT || DEBUG */
3608
3609 /* Kernel call only, MIG uses *_from_user() below */
3610 kern_return_t
mach_memory_entry_ownership(ipc_port_t entry_port,task_t owner,int ledger_tag,int ledger_flags)3611 mach_memory_entry_ownership(
3612 ipc_port_t entry_port,
3613 task_t owner,
3614 int ledger_tag,
3615 int ledger_flags)
3616 {
3617 task_t cur_task;
3618 kern_return_t kr;
3619 vm_named_entry_t mem_entry;
3620 vm_object_t object;
3621 #if DEVELOPMENT || DEBUG
3622 int to_panic = 0;
3623 static bool init_bootarg = false;
3624 #endif
3625
3626 cur_task = current_task();
3627 if (cur_task != kernel_task &&
3628 ((owner != cur_task && owner != TASK_NULL) ||
3629 (ledger_flags & VM_LEDGER_FLAG_NO_FOOTPRINT) ||
3630 (ledger_flags & VM_LEDGER_FLAG_NO_FOOTPRINT_FOR_DEBUG) ||
3631 ledger_tag == VM_LEDGER_TAG_NETWORK)) {
3632 /*
3633 * An entitlement is required to:
3634 * + tranfer memory ownership to someone else,
3635 * + request that the memory not count against the footprint,
3636 * + tag as "network" (since that implies "no footprint")
3637 *
3638 * Exception: task with task_no_footprint_for_debug == 1 on internal build
3639 */
3640 if (!cur_task->task_can_transfer_memory_ownership &&
3641 IOCurrentTaskHasEntitlement("com.apple.private.memory.ownership_transfer")) {
3642 cur_task->task_can_transfer_memory_ownership = TRUE;
3643 }
3644 if (!cur_task->task_can_transfer_memory_ownership) {
3645 #if DEVELOPMENT || DEBUG
3646 if ((ledger_tag == VM_LEDGER_TAG_DEFAULT) &&
3647 (ledger_flags & VM_LEDGER_FLAG_NO_FOOTPRINT_FOR_DEBUG) &&
3648 cur_task->task_no_footprint_for_debug) {
3649 /*
3650 * Allow performance tools running on internal builds to hide memory usage from phys_footprint even
3651 * WITHOUT an entitlement. This can be enabled by per task sysctl vm.task_no_footprint_for_debug=1
3652 * with the ledger tag VM_LEDGER_TAG_DEFAULT and flag VM_LEDGER_FLAG_NO_FOOTPRINT_FOR_DEBUG.
3653 *
3654 * If the boot-arg "panic_on_no_footprint_for_debug" is set, the kernel will
3655 * panic here in order to detect any abuse of this feature, which is intended solely for
3656 * memory debugging purpose.
3657 */
3658 if (!init_bootarg) {
3659 PE_parse_boot_argn("panic_on_no_footprint_for_debug", &to_panic, sizeof(to_panic));
3660 init_bootarg = true;
3661 }
3662 if (to_panic) {
3663 panic("%s: panic_on_no_footprint_for_debug is triggered by pid %d procname %s", __func__, proc_selfpid(), get_bsdtask_info(cur_task)? proc_name_address(get_bsdtask_info(cur_task)) : "?");
3664 }
3665
3666 /*
3667 * Flushing out user space processes using this interface:
3668 * $ dtrace -n 'task_no_footprint_for_debug {printf("%d[%s]\n", pid, execname); stack(); ustack();}'
3669 */
3670 DTRACE_VM(task_no_footprint_for_debug);
3671 } else
3672 #endif /* DEVELOPMENT || DEBUG */
3673 return KERN_NO_ACCESS;
3674 }
3675
3676 if (ledger_flags & VM_LEDGER_FLAG_NO_FOOTPRINT_FOR_DEBUG) {
3677 /*
3678 * We've made it past the checks above, so we either
3679 * have the entitlement or the sysctl.
3680 * Convert to VM_LEDGER_FLAG_NO_FOOTPRINT.
3681 */
3682 ledger_flags &= ~VM_LEDGER_FLAG_NO_FOOTPRINT_FOR_DEBUG;
3683 ledger_flags |= VM_LEDGER_FLAG_NO_FOOTPRINT;
3684 }
3685 }
3686
3687 if (ledger_flags & ~VM_LEDGER_FLAGS) {
3688 return KERN_INVALID_ARGUMENT;
3689 }
3690 if (ledger_tag == VM_LEDGER_TAG_UNCHANGED) {
3691 /* leave "ledger_tag" unchanged */
3692 } else if (ledger_tag < 0 ||
3693 ledger_tag > VM_LEDGER_TAG_MAX) {
3694 return KERN_INVALID_ARGUMENT;
3695 }
3696 if (owner == TASK_NULL) {
3697 /* leave "owner" unchanged */
3698 owner = VM_OBJECT_OWNER_UNCHANGED;
3699 }
3700
3701 mem_entry = mach_memory_entry_from_port(entry_port);
3702 if (mem_entry == NULL) {
3703 return KERN_INVALID_ARGUMENT;
3704 }
3705
3706 named_entry_lock(mem_entry);
3707
3708 if (mem_entry->is_sub_map ||
3709 !mem_entry->is_fully_owned) {
3710 named_entry_unlock(mem_entry);
3711 return KERN_INVALID_ARGUMENT;
3712 }
3713
3714 if (mem_entry->is_object) {
3715 object = vm_named_entry_to_vm_object(mem_entry);
3716 if (object == VM_OBJECT_NULL) {
3717 named_entry_unlock(mem_entry);
3718 return KERN_INVALID_ARGUMENT;
3719 }
3720 vm_object_lock(object);
3721 /* check that named entry covers entire object ? */
3722 if (mem_entry->offset != 0 || object->vo_size != mem_entry->size) {
3723 vm_object_unlock(object);
3724 named_entry_unlock(mem_entry);
3725 return KERN_INVALID_ARGUMENT;
3726 }
3727 named_entry_unlock(mem_entry);
3728 kr = vm_object_ownership_change(object,
3729 ledger_tag,
3730 owner,
3731 ledger_flags,
3732 FALSE); /* task_objq_locked */
3733 vm_object_unlock(object);
3734 } else if (mem_entry->is_copy) {
3735 vm_map_copy_t copy;
3736 vm_map_entry_t entry;
3737
3738 copy = mem_entry->backing.copy;
3739 named_entry_unlock(mem_entry);
3740 for (entry = vm_map_copy_first_entry(copy);
3741 entry != vm_map_copy_to_entry(copy);
3742 entry = entry->vme_next) {
3743 object = VME_OBJECT(entry);
3744 if (entry->is_sub_map ||
3745 object == VM_OBJECT_NULL) {
3746 kr = KERN_INVALID_ARGUMENT;
3747 break;
3748 }
3749 vm_object_lock(object);
3750 if (VME_OFFSET(entry) != 0 ||
3751 entry->vme_end - entry->vme_start != object->vo_size) {
3752 vm_object_unlock(object);
3753 kr = KERN_INVALID_ARGUMENT;
3754 break;
3755 }
3756 kr = vm_object_ownership_change(object,
3757 ledger_tag,
3758 owner,
3759 ledger_flags,
3760 FALSE); /* task_objq_locked */
3761 vm_object_unlock(object);
3762 if (kr != KERN_SUCCESS) {
3763 kr = KERN_INVALID_ARGUMENT;
3764 break;
3765 }
3766 }
3767 } else {
3768 named_entry_unlock(mem_entry);
3769 return KERN_INVALID_ARGUMENT;
3770 }
3771
3772 return kr;
3773 }
3774
3775 /* MIG call from userspace */
3776 kern_return_t
mach_memory_entry_ownership_from_user(ipc_port_t entry_port,mach_port_t owner_port,int ledger_tag,int ledger_flags)3777 mach_memory_entry_ownership_from_user(
3778 ipc_port_t entry_port,
3779 mach_port_t owner_port,
3780 int ledger_tag,
3781 int ledger_flags)
3782 {
3783 task_t owner = TASK_NULL;
3784 kern_return_t kr;
3785
3786 if (IP_VALID(owner_port)) {
3787 if (ip_kotype(owner_port) == IKOT_TASK_ID_TOKEN) {
3788 task_id_token_t token = convert_port_to_task_id_token(owner_port);
3789 (void)task_identity_token_get_task_grp(token, &owner, TASK_GRP_MIG);
3790 task_id_token_release(token);
3791 /* token ref released */
3792 } else {
3793 owner = convert_port_to_task_mig(owner_port);
3794 }
3795 }
3796 /* hold task ref on owner (Nullable) */
3797
3798 if (owner && task_is_a_corpse(owner)) {
3799 /* identity token can represent a corpse, disallow it */
3800 task_deallocate_mig(owner);
3801 owner = TASK_NULL;
3802 }
3803
3804 /* mach_memory_entry_ownership() will handle TASK_NULL owner */
3805 kr = mach_memory_entry_ownership(entry_port, owner, /* Nullable */
3806 ledger_tag, ledger_flags);
3807
3808 if (owner) {
3809 task_deallocate_mig(owner);
3810 }
3811
3812 if (kr == KERN_SUCCESS) {
3813 /* MIG rule, consume port right on success */
3814 ipc_port_release_send(owner_port);
3815 }
3816 return kr;
3817 }
3818
3819 kern_return_t
mach_memory_entry_get_page_counts(ipc_port_t entry_port,unsigned int * resident_page_count,unsigned int * dirty_page_count)3820 mach_memory_entry_get_page_counts(
3821 ipc_port_t entry_port,
3822 unsigned int *resident_page_count,
3823 unsigned int *dirty_page_count)
3824 {
3825 kern_return_t kr;
3826 vm_named_entry_t mem_entry;
3827 vm_object_t object;
3828 vm_object_offset_t offset;
3829 vm_object_size_t size;
3830
3831 mem_entry = mach_memory_entry_from_port(entry_port);
3832 if (mem_entry == NULL) {
3833 return KERN_INVALID_ARGUMENT;
3834 }
3835
3836 named_entry_lock(mem_entry);
3837
3838 if (mem_entry->is_sub_map ||
3839 mem_entry->is_copy) {
3840 named_entry_unlock(mem_entry);
3841 return KERN_INVALID_ARGUMENT;
3842 }
3843
3844 assert(mem_entry->is_object);
3845 object = vm_named_entry_to_vm_object(mem_entry);
3846 if (object == VM_OBJECT_NULL) {
3847 named_entry_unlock(mem_entry);
3848 return KERN_INVALID_ARGUMENT;
3849 }
3850
3851 vm_object_lock(object);
3852
3853 offset = mem_entry->offset;
3854 size = mem_entry->size;
3855 size = vm_object_round_page(offset + size) - vm_object_trunc_page(offset);
3856 offset = vm_object_trunc_page(offset);
3857
3858 named_entry_unlock(mem_entry);
3859
3860 kr = vm_object_get_page_counts(object, offset, size, resident_page_count, dirty_page_count);
3861
3862 vm_object_unlock(object);
3863
3864 return kr;
3865 }
3866
3867 kern_return_t
mach_memory_entry_phys_page_offset(ipc_port_t entry_port,vm_object_offset_t * offset_p)3868 mach_memory_entry_phys_page_offset(
3869 ipc_port_t entry_port,
3870 vm_object_offset_t *offset_p)
3871 {
3872 vm_named_entry_t mem_entry;
3873 vm_object_t object;
3874 vm_object_offset_t offset;
3875 vm_object_offset_t data_offset;
3876
3877 mem_entry = mach_memory_entry_from_port(entry_port);
3878 if (mem_entry == NULL) {
3879 return KERN_INVALID_ARGUMENT;
3880 }
3881
3882 named_entry_lock(mem_entry);
3883
3884 if (mem_entry->is_sub_map ||
3885 mem_entry->is_copy) {
3886 named_entry_unlock(mem_entry);
3887 return KERN_INVALID_ARGUMENT;
3888 }
3889
3890 assert(mem_entry->is_object);
3891 object = vm_named_entry_to_vm_object(mem_entry);
3892 if (object == VM_OBJECT_NULL) {
3893 named_entry_unlock(mem_entry);
3894 return KERN_INVALID_ARGUMENT;
3895 }
3896
3897 offset = mem_entry->offset;
3898 data_offset = mem_entry->data_offset;
3899
3900 named_entry_unlock(mem_entry);
3901
3902 *offset_p = offset - vm_object_trunc_page(offset) + data_offset;
3903 return KERN_SUCCESS;
3904 }
3905
3906 kern_return_t
mach_memory_entry_map_size(ipc_port_t entry_port,vm_map_t map,memory_object_offset_t offset,memory_object_offset_t size,mach_vm_size_t * map_size)3907 mach_memory_entry_map_size(
3908 ipc_port_t entry_port,
3909 vm_map_t map,
3910 memory_object_offset_t offset,
3911 memory_object_offset_t size,
3912 mach_vm_size_t *map_size)
3913 {
3914 vm_named_entry_t mem_entry;
3915 vm_object_t object;
3916 vm_object_offset_t object_offset_start, object_offset_end;
3917 vm_map_copy_t copy_map, target_copy_map;
3918 vm_map_offset_t overmap_start, overmap_end, trimmed_start;
3919 kern_return_t kr;
3920
3921 mem_entry = mach_memory_entry_from_port(entry_port);
3922 if (mem_entry == NULL) {
3923 return KERN_INVALID_ARGUMENT;
3924 }
3925
3926 named_entry_lock(mem_entry);
3927
3928 if (mem_entry->is_sub_map) {
3929 named_entry_unlock(mem_entry);
3930 return KERN_INVALID_ARGUMENT;
3931 }
3932
3933 if (mem_entry->is_object) {
3934 object = vm_named_entry_to_vm_object(mem_entry);
3935 if (object == VM_OBJECT_NULL) {
3936 named_entry_unlock(mem_entry);
3937 return KERN_INVALID_ARGUMENT;
3938 }
3939
3940 object_offset_start = mem_entry->offset;
3941 object_offset_start += mem_entry->data_offset;
3942 object_offset_start += offset;
3943 object_offset_end = object_offset_start + size;
3944 object_offset_start = vm_map_trunc_page(object_offset_start,
3945 VM_MAP_PAGE_MASK(map));
3946 object_offset_end = vm_map_round_page(object_offset_end,
3947 VM_MAP_PAGE_MASK(map));
3948
3949 named_entry_unlock(mem_entry);
3950
3951 *map_size = object_offset_end - object_offset_start;
3952 return KERN_SUCCESS;
3953 }
3954
3955 if (!mem_entry->is_copy) {
3956 panic("unsupported type of mem_entry %p", mem_entry);
3957 }
3958
3959 assert(mem_entry->is_copy);
3960 if (VM_MAP_COPY_PAGE_MASK(mem_entry->backing.copy) == VM_MAP_PAGE_MASK(map)) {
3961 *map_size = vm_map_round_page(mem_entry->offset + mem_entry->data_offset + offset + size, VM_MAP_PAGE_MASK(map)) - vm_map_trunc_page(mem_entry->offset + mem_entry->data_offset + offset, VM_MAP_PAGE_MASK(map));
3962 DEBUG4K_SHARE("map %p (%d) mem_entry %p offset 0x%llx + 0x%llx + 0x%llx size 0x%llx -> map_size 0x%llx\n", map, VM_MAP_PAGE_MASK(map), mem_entry, mem_entry->offset, mem_entry->data_offset, offset, size, *map_size);
3963 named_entry_unlock(mem_entry);
3964 return KERN_SUCCESS;
3965 }
3966
3967 DEBUG4K_SHARE("mem_entry %p copy %p (%d) map %p (%d) offset 0x%llx size 0x%llx\n", mem_entry, mem_entry->backing.copy, VM_MAP_COPY_PAGE_SHIFT(mem_entry->backing.copy), map, VM_MAP_PAGE_SHIFT(map), offset, size);
3968 copy_map = mem_entry->backing.copy;
3969 target_copy_map = VM_MAP_COPY_NULL;
3970 DEBUG4K_ADJUST("adjusting...\n");
3971 kr = vm_map_copy_adjust_to_target(copy_map,
3972 mem_entry->data_offset + offset,
3973 size,
3974 map,
3975 FALSE,
3976 &target_copy_map,
3977 &overmap_start,
3978 &overmap_end,
3979 &trimmed_start);
3980 if (kr == KERN_SUCCESS) {
3981 if (target_copy_map->size != copy_map->size) {
3982 DEBUG4K_ADJUST("copy %p (%d) map %p (%d) offset 0x%llx size 0x%llx overmap_start 0x%llx overmap_end 0x%llx trimmed_start 0x%llx map_size 0x%llx -> 0x%llx\n", copy_map, VM_MAP_COPY_PAGE_SHIFT(copy_map), map, VM_MAP_PAGE_SHIFT(map), (uint64_t)offset, (uint64_t)size, (uint64_t)overmap_start, (uint64_t)overmap_end, (uint64_t)trimmed_start, (uint64_t)copy_map->size, (uint64_t)target_copy_map->size);
3983 }
3984 *map_size = target_copy_map->size;
3985 if (target_copy_map != copy_map) {
3986 vm_map_copy_discard(target_copy_map);
3987 }
3988 target_copy_map = VM_MAP_COPY_NULL;
3989 }
3990 named_entry_unlock(mem_entry);
3991 return kr;
3992 }
3993
3994 /*
3995 * mach_memory_entry_port_release:
3996 *
3997 * Release a send right on a named entry port. This is the correct
3998 * way to destroy a named entry. When the last right on the port is
3999 * released, mach_memory_entry_no_senders() willl be called.
4000 */
4001 void
mach_memory_entry_port_release(ipc_port_t port)4002 mach_memory_entry_port_release(
4003 ipc_port_t port)
4004 {
4005 assert(ip_kotype(port) == IKOT_NAMED_ENTRY);
4006 ipc_port_release_send(port);
4007 }
4008
4009 vm_named_entry_t
mach_memory_entry_from_port(ipc_port_t port)4010 mach_memory_entry_from_port(ipc_port_t port)
4011 {
4012 if (IP_VALID(port)) {
4013 return ipc_kobject_get_stable(port, IKOT_NAMED_ENTRY);
4014 }
4015 return NULL;
4016 }
4017
4018 /*
4019 * mach_memory_entry_no_senders:
4020 *
4021 * Destroys the memory entry associated with a mach port.
4022 * Memory entries have the exact same lifetime as their owning port.
4023 *
4024 * Releasing a memory entry is done by calling
4025 * mach_memory_entry_port_release() on its owning port.
4026 */
4027 static void
mach_memory_entry_no_senders(ipc_port_t port,mach_port_mscount_t mscount)4028 mach_memory_entry_no_senders(ipc_port_t port, mach_port_mscount_t mscount)
4029 {
4030 vm_named_entry_t named_entry;
4031
4032 named_entry = ipc_kobject_dealloc_port(port, mscount, IKOT_NAMED_ENTRY);
4033
4034 if (named_entry->is_sub_map) {
4035 vm_map_deallocate(named_entry->backing.map);
4036 } else if (named_entry->is_copy) {
4037 vm_map_copy_discard(named_entry->backing.copy);
4038 } else if (named_entry->is_object) {
4039 assert(named_entry->backing.copy->cpy_hdr.nentries == 1);
4040 vm_map_copy_discard(named_entry->backing.copy);
4041 } else {
4042 assert(named_entry->backing.copy == VM_MAP_COPY_NULL);
4043 }
4044
4045 #if VM_NAMED_ENTRY_DEBUG
4046 btref_put(named_entry->named_entry_bt);
4047 #endif /* VM_NAMED_ENTRY_DEBUG */
4048
4049 named_entry_lock_destroy(named_entry);
4050 kfree_type(struct vm_named_entry, named_entry);
4051 }
4052
4053 /* Allow manipulation of individual page state. This is actually part of */
4054 /* the UPL regimen but takes place on the memory entry rather than on a UPL */
4055
4056 kern_return_t
mach_memory_entry_page_op(ipc_port_t entry_port,vm_object_offset_t offset,int ops,ppnum_t * phys_entry,int * flags)4057 mach_memory_entry_page_op(
4058 ipc_port_t entry_port,
4059 vm_object_offset_t offset,
4060 int ops,
4061 ppnum_t *phys_entry,
4062 int *flags)
4063 {
4064 vm_named_entry_t mem_entry;
4065 vm_object_t object;
4066 kern_return_t kr;
4067
4068 mem_entry = mach_memory_entry_from_port(entry_port);
4069 if (mem_entry == NULL) {
4070 return KERN_INVALID_ARGUMENT;
4071 }
4072
4073 named_entry_lock(mem_entry);
4074
4075 if (mem_entry->is_sub_map ||
4076 mem_entry->is_copy) {
4077 named_entry_unlock(mem_entry);
4078 return KERN_INVALID_ARGUMENT;
4079 }
4080
4081 assert(mem_entry->is_object);
4082 object = vm_named_entry_to_vm_object(mem_entry);
4083 if (object == VM_OBJECT_NULL) {
4084 named_entry_unlock(mem_entry);
4085 return KERN_INVALID_ARGUMENT;
4086 }
4087
4088 vm_object_reference(object);
4089 named_entry_unlock(mem_entry);
4090
4091 kr = vm_object_page_op(object, offset, ops, phys_entry, flags);
4092
4093 vm_object_deallocate(object);
4094
4095 return kr;
4096 }
4097
4098 /*
4099 * mach_memory_entry_range_op offers performance enhancement over
4100 * mach_memory_entry_page_op for page_op functions which do not require page
4101 * level state to be returned from the call. Page_op was created to provide
4102 * a low-cost alternative to page manipulation via UPLs when only a single
4103 * page was involved. The range_op call establishes the ability in the _op
4104 * family of functions to work on multiple pages where the lack of page level
4105 * state handling allows the caller to avoid the overhead of the upl structures.
4106 */
4107
4108 kern_return_t
mach_memory_entry_range_op(ipc_port_t entry_port,vm_object_offset_t offset_beg,vm_object_offset_t offset_end,int ops,int * range)4109 mach_memory_entry_range_op(
4110 ipc_port_t entry_port,
4111 vm_object_offset_t offset_beg,
4112 vm_object_offset_t offset_end,
4113 int ops,
4114 int *range)
4115 {
4116 vm_named_entry_t mem_entry;
4117 vm_object_t object;
4118 kern_return_t kr;
4119
4120 mem_entry = mach_memory_entry_from_port(entry_port);
4121 if (mem_entry == NULL) {
4122 return KERN_INVALID_ARGUMENT;
4123 }
4124
4125 named_entry_lock(mem_entry);
4126
4127 if (mem_entry->is_sub_map ||
4128 mem_entry->is_copy) {
4129 named_entry_unlock(mem_entry);
4130 return KERN_INVALID_ARGUMENT;
4131 }
4132
4133 assert(mem_entry->is_object);
4134 object = vm_named_entry_to_vm_object(mem_entry);
4135 if (object == VM_OBJECT_NULL) {
4136 named_entry_unlock(mem_entry);
4137 return KERN_INVALID_ARGUMENT;
4138 }
4139
4140 vm_object_reference(object);
4141 named_entry_unlock(mem_entry);
4142
4143 kr = vm_object_range_op(object,
4144 offset_beg,
4145 offset_end,
4146 ops,
4147 (uint32_t *) range);
4148
4149 vm_object_deallocate(object);
4150
4151 return kr;
4152 }
4153
4154 /* ******* Temporary Internal calls to UPL for BSD ***** */
4155
4156 extern int kernel_upl_map(
4157 vm_map_t map,
4158 upl_t upl,
4159 vm_offset_t *dst_addr);
4160
4161 extern int kernel_upl_unmap(
4162 vm_map_t map,
4163 upl_t upl);
4164
4165 extern int kernel_upl_commit(
4166 upl_t upl,
4167 upl_page_info_t *pl,
4168 mach_msg_type_number_t count);
4169
4170 extern int kernel_upl_commit_range(
4171 upl_t upl,
4172 upl_offset_t offset,
4173 upl_size_t size,
4174 int flags,
4175 upl_page_info_array_t pl,
4176 mach_msg_type_number_t count);
4177
4178 extern int kernel_upl_abort(
4179 upl_t upl,
4180 int abort_type);
4181
4182 extern int kernel_upl_abort_range(
4183 upl_t upl,
4184 upl_offset_t offset,
4185 upl_size_t size,
4186 int abort_flags);
4187
4188
4189 kern_return_t
kernel_upl_map(vm_map_t map,upl_t upl,vm_offset_t * dst_addr)4190 kernel_upl_map(
4191 vm_map_t map,
4192 upl_t upl,
4193 vm_offset_t *dst_addr)
4194 {
4195 return vm_upl_map(map, upl, dst_addr);
4196 }
4197
4198
4199 kern_return_t
kernel_upl_unmap(vm_map_t map,upl_t upl)4200 kernel_upl_unmap(
4201 vm_map_t map,
4202 upl_t upl)
4203 {
4204 return vm_upl_unmap(map, upl);
4205 }
4206
4207 kern_return_t
kernel_upl_commit(upl_t upl,upl_page_info_t * pl,mach_msg_type_number_t count)4208 kernel_upl_commit(
4209 upl_t upl,
4210 upl_page_info_t *pl,
4211 mach_msg_type_number_t count)
4212 {
4213 kern_return_t kr;
4214
4215 kr = upl_commit(upl, pl, count);
4216 upl_deallocate(upl);
4217 return kr;
4218 }
4219
4220
4221 kern_return_t
kernel_upl_commit_range(upl_t upl,upl_offset_t offset,upl_size_t size,int flags,upl_page_info_array_t pl,mach_msg_type_number_t count)4222 kernel_upl_commit_range(
4223 upl_t upl,
4224 upl_offset_t offset,
4225 upl_size_t size,
4226 int flags,
4227 upl_page_info_array_t pl,
4228 mach_msg_type_number_t count)
4229 {
4230 boolean_t finished = FALSE;
4231 kern_return_t kr;
4232
4233 if (flags & UPL_COMMIT_FREE_ON_EMPTY) {
4234 flags |= UPL_COMMIT_NOTIFY_EMPTY;
4235 }
4236
4237 if (flags & UPL_COMMIT_KERNEL_ONLY_FLAGS) {
4238 return KERN_INVALID_ARGUMENT;
4239 }
4240
4241 kr = upl_commit_range(upl, offset, size, flags, pl, count, &finished);
4242
4243 if ((flags & UPL_COMMIT_NOTIFY_EMPTY) && finished) {
4244 upl_deallocate(upl);
4245 }
4246
4247 return kr;
4248 }
4249
4250 kern_return_t
kernel_upl_abort_range(upl_t upl,upl_offset_t offset,upl_size_t size,int abort_flags)4251 kernel_upl_abort_range(
4252 upl_t upl,
4253 upl_offset_t offset,
4254 upl_size_t size,
4255 int abort_flags)
4256 {
4257 kern_return_t kr;
4258 boolean_t finished = FALSE;
4259
4260 if (abort_flags & UPL_COMMIT_FREE_ON_EMPTY) {
4261 abort_flags |= UPL_COMMIT_NOTIFY_EMPTY;
4262 }
4263
4264 kr = upl_abort_range(upl, offset, size, abort_flags, &finished);
4265
4266 if ((abort_flags & UPL_COMMIT_FREE_ON_EMPTY) && finished) {
4267 upl_deallocate(upl);
4268 }
4269
4270 return kr;
4271 }
4272
4273 kern_return_t
kernel_upl_abort(upl_t upl,int abort_type)4274 kernel_upl_abort(
4275 upl_t upl,
4276 int abort_type)
4277 {
4278 kern_return_t kr;
4279
4280 kr = upl_abort(upl, abort_type);
4281 upl_deallocate(upl);
4282 return kr;
4283 }
4284
4285 /*
4286 * Now a kernel-private interface (for BootCache
4287 * use only). Need a cleaner way to create an
4288 * empty vm_map() and return a handle to it.
4289 */
4290
4291 kern_return_t
vm_region_object_create(vm_map_t target_map,vm_size_t size,ipc_port_t * object_handle)4292 vm_region_object_create(
4293 vm_map_t target_map,
4294 vm_size_t size,
4295 ipc_port_t *object_handle)
4296 {
4297 vm_named_entry_t user_entry;
4298 vm_map_t new_map;
4299
4300 user_entry = mach_memory_entry_allocate(object_handle);
4301
4302 /* Create a named object based on a submap of specified size */
4303
4304 new_map = vm_map_create_options(PMAP_NULL, VM_MAP_MIN_ADDRESS,
4305 vm_map_round_page(size, VM_MAP_PAGE_MASK(target_map)),
4306 VM_MAP_CREATE_PAGEABLE);
4307 vm_map_set_page_shift(new_map, VM_MAP_PAGE_SHIFT(target_map));
4308
4309 user_entry->backing.map = new_map;
4310 user_entry->internal = TRUE;
4311 user_entry->is_sub_map = TRUE;
4312 user_entry->offset = 0;
4313 user_entry->protection = VM_PROT_ALL;
4314 user_entry->size = size;
4315
4316 return KERN_SUCCESS;
4317 }
4318
4319 ppnum_t vm_map_get_phys_page( /* forward */
4320 vm_map_t map,
4321 vm_offset_t offset);
4322
4323 ppnum_t
vm_map_get_phys_page(vm_map_t map,vm_offset_t addr)4324 vm_map_get_phys_page(
4325 vm_map_t map,
4326 vm_offset_t addr)
4327 {
4328 vm_object_offset_t offset;
4329 vm_object_t object;
4330 vm_map_offset_t map_offset;
4331 vm_map_entry_t entry;
4332 ppnum_t phys_page = 0;
4333
4334 map_offset = vm_map_trunc_page(addr, PAGE_MASK);
4335
4336 vm_map_lock(map);
4337 while (vm_map_lookup_entry(map, map_offset, &entry)) {
4338 if (entry->is_sub_map) {
4339 vm_map_t old_map;
4340 vm_map_lock(VME_SUBMAP(entry));
4341 old_map = map;
4342 map = VME_SUBMAP(entry);
4343 map_offset = (VME_OFFSET(entry) +
4344 (map_offset - entry->vme_start));
4345 vm_map_unlock(old_map);
4346 continue;
4347 }
4348 if (VME_OBJECT(entry) == VM_OBJECT_NULL) {
4349 vm_map_unlock(map);
4350 return (ppnum_t) 0;
4351 }
4352 if (VME_OBJECT(entry)->phys_contiguous) {
4353 /* These are not standard pageable memory mappings */
4354 /* If they are not present in the object they will */
4355 /* have to be picked up from the pager through the */
4356 /* fault mechanism. */
4357 if (VME_OBJECT(entry)->vo_shadow_offset == 0) {
4358 /* need to call vm_fault */
4359 vm_map_unlock(map);
4360 vm_fault(map, map_offset, VM_PROT_NONE,
4361 FALSE /* change_wiring */, VM_KERN_MEMORY_NONE,
4362 THREAD_UNINT, NULL, 0);
4363 vm_map_lock(map);
4364 continue;
4365 }
4366 offset = (VME_OFFSET(entry) +
4367 (map_offset - entry->vme_start));
4368 phys_page = (ppnum_t)
4369 ((VME_OBJECT(entry)->vo_shadow_offset
4370 + offset) >> PAGE_SHIFT);
4371 break;
4372 }
4373 offset = (VME_OFFSET(entry) + (map_offset - entry->vme_start));
4374 object = VME_OBJECT(entry);
4375 vm_object_lock(object);
4376 while (TRUE) {
4377 vm_page_t dst_page = vm_page_lookup(object, offset);
4378 if (dst_page == VM_PAGE_NULL) {
4379 if (object->shadow) {
4380 vm_object_t old_object;
4381 vm_object_lock(object->shadow);
4382 old_object = object;
4383 offset = offset + object->vo_shadow_offset;
4384 object = object->shadow;
4385 vm_object_unlock(old_object);
4386 } else {
4387 vm_object_unlock(object);
4388 break;
4389 }
4390 } else {
4391 phys_page = (ppnum_t)(VM_PAGE_GET_PHYS_PAGE(dst_page));
4392 vm_object_unlock(object);
4393 break;
4394 }
4395 }
4396 break;
4397 }
4398
4399 vm_map_unlock(map);
4400 return phys_page;
4401 }
4402
4403 kern_return_t
mach_vm_deferred_reclamation_buffer_init(task_t task,mach_vm_offset_t address,mach_vm_size_t size,mach_vm_address_t indices)4404 mach_vm_deferred_reclamation_buffer_init(
4405 task_t task,
4406 mach_vm_offset_t address,
4407 mach_vm_size_t size,
4408 mach_vm_address_t indices)
4409 {
4410 #if CONFIG_DEFERRED_RECLAIM
4411 return vm_deferred_reclamation_buffer_init_internal(task, address, size, indices);
4412 #else
4413 (void) task;
4414 (void) address;
4415 (void) size;
4416 (void) indices;
4417 return KERN_NOT_SUPPORTED;
4418 #endif /* CONFIG_DEFERRED_RECLAIM */
4419 }
4420
4421 kern_return_t
mach_vm_deferred_reclamation_buffer_synchronize(task_t task,mach_vm_size_t num_entries_to_reclaim)4422 mach_vm_deferred_reclamation_buffer_synchronize(
4423 task_t task,
4424 mach_vm_size_t num_entries_to_reclaim)
4425 {
4426 #if CONFIG_DEFERRED_RECLAIM
4427 return vm_deferred_reclamation_buffer_synchronize_internal(task, num_entries_to_reclaim);
4428 #else
4429 (void) task;
4430 (void) num_entries_to_reclaim;
4431 return KERN_NOT_SUPPORTED;
4432 #endif /* CONFIG_DEFERRED_RECLAIM */
4433 }
4434
4435 kern_return_t
mach_vm_deferred_reclamation_buffer_update_reclaimable_bytes(task_t task,mach_vm_size_t reclaimable_bytes)4436 mach_vm_deferred_reclamation_buffer_update_reclaimable_bytes(task_t task, mach_vm_size_t reclaimable_bytes)
4437 {
4438 #if CONFIG_DEFERRED_RECLAIM
4439 return vm_deferred_reclamation_buffer_update_reclaimable_bytes_internal(task, reclaimable_bytes);
4440 #else
4441 (void) task;
4442 (void) reclaimable_bytes;
4443 return KERN_NOT_SUPPORTED;
4444 #endif /* CONFIG_DEFERRED_RECLAIM */
4445 }
4446
4447 #if 0
4448 kern_return_t kernel_object_iopl_request( /* forward */
4449 vm_named_entry_t named_entry,
4450 memory_object_offset_t offset,
4451 upl_size_t *upl_size,
4452 upl_t *upl_ptr,
4453 upl_page_info_array_t user_page_list,
4454 unsigned int *page_list_count,
4455 int *flags);
4456
4457 kern_return_t
4458 kernel_object_iopl_request(
4459 vm_named_entry_t named_entry,
4460 memory_object_offset_t offset,
4461 upl_size_t *upl_size,
4462 upl_t *upl_ptr,
4463 upl_page_info_array_t user_page_list,
4464 unsigned int *page_list_count,
4465 int *flags)
4466 {
4467 vm_object_t object;
4468 kern_return_t ret;
4469
4470 int caller_flags;
4471
4472 caller_flags = *flags;
4473
4474 if (caller_flags & ~UPL_VALID_FLAGS) {
4475 /*
4476 * For forward compatibility's sake,
4477 * reject any unknown flag.
4478 */
4479 return KERN_INVALID_VALUE;
4480 }
4481
4482 /* a few checks to make sure user is obeying rules */
4483 if (*upl_size == 0) {
4484 if (offset >= named_entry->size) {
4485 return KERN_INVALID_RIGHT;
4486 }
4487 *upl_size = (upl_size_t) (named_entry->size - offset);
4488 if (*upl_size != named_entry->size - offset) {
4489 return KERN_INVALID_ARGUMENT;
4490 }
4491 }
4492 if (caller_flags & UPL_COPYOUT_FROM) {
4493 if ((named_entry->protection & VM_PROT_READ)
4494 != VM_PROT_READ) {
4495 return KERN_INVALID_RIGHT;
4496 }
4497 } else {
4498 if ((named_entry->protection &
4499 (VM_PROT_READ | VM_PROT_WRITE))
4500 != (VM_PROT_READ | VM_PROT_WRITE)) {
4501 return KERN_INVALID_RIGHT;
4502 }
4503 }
4504 if (named_entry->size < (offset + *upl_size)) {
4505 return KERN_INVALID_ARGUMENT;
4506 }
4507
4508 /* the callers parameter offset is defined to be the */
4509 /* offset from beginning of named entry offset in object */
4510 offset = offset + named_entry->offset;
4511
4512 if (named_entry->is_sub_map ||
4513 named_entry->is_copy) {
4514 return KERN_INVALID_ARGUMENT;
4515 }
4516
4517 named_entry_lock(named_entry);
4518
4519 /* This is the case where we are going to operate */
4520 /* on an already known object. If the object is */
4521 /* not ready it is internal. An external */
4522 /* object cannot be mapped until it is ready */
4523 /* we can therefore avoid the ready check */
4524 /* in this case. */
4525 assert(named_entry->is_object);
4526 object = vm_named_entry_to_vm_object(named_entry);
4527 vm_object_reference(object);
4528 named_entry_unlock(named_entry);
4529
4530 if (!object->private) {
4531 if (*upl_size > MAX_UPL_TRANSFER_BYTES) {
4532 *upl_size = MAX_UPL_TRANSFER_BYTES;
4533 }
4534 if (object->phys_contiguous) {
4535 *flags = UPL_PHYS_CONTIG;
4536 } else {
4537 *flags = 0;
4538 }
4539 } else {
4540 *flags = UPL_DEV_MEMORY | UPL_PHYS_CONTIG;
4541 }
4542
4543 ret = vm_object_iopl_request(object,
4544 offset,
4545 *upl_size,
4546 upl_ptr,
4547 user_page_list,
4548 page_list_count,
4549 (upl_control_flags_t)(unsigned int)caller_flags);
4550 vm_object_deallocate(object);
4551 return ret;
4552 }
4553 #endif
4554
4555 /*
4556 * These symbols are looked up at runtime by vmware, VirtualBox,
4557 * despite not being exported in the symbol sets.
4558 */
4559
4560 #if defined(__x86_64__)
4561
4562 kern_return_t
4563 mach_vm_map(
4564 vm_map_t target_map,
4565 mach_vm_offset_t *address,
4566 mach_vm_size_t initial_size,
4567 mach_vm_offset_t mask,
4568 int flags,
4569 ipc_port_t port,
4570 vm_object_offset_t offset,
4571 boolean_t copy,
4572 vm_prot_t cur_protection,
4573 vm_prot_t max_protection,
4574 vm_inherit_t inheritance);
4575
4576 kern_return_t
4577 mach_vm_remap(
4578 vm_map_t target_map,
4579 mach_vm_offset_t *address,
4580 mach_vm_size_t size,
4581 mach_vm_offset_t mask,
4582 int flags,
4583 vm_map_t src_map,
4584 mach_vm_offset_t memory_address,
4585 boolean_t copy,
4586 vm_prot_t *cur_protection,
4587 vm_prot_t *max_protection,
4588 vm_inherit_t inheritance);
4589
4590 kern_return_t
mach_vm_map(vm_map_t target_map,mach_vm_offset_t * address,mach_vm_size_t initial_size,mach_vm_offset_t mask,int flags,ipc_port_t port,vm_object_offset_t offset,boolean_t copy,vm_prot_t cur_protection,vm_prot_t max_protection,vm_inherit_t inheritance)4591 mach_vm_map(
4592 vm_map_t target_map,
4593 mach_vm_offset_t *address,
4594 mach_vm_size_t initial_size,
4595 mach_vm_offset_t mask,
4596 int flags,
4597 ipc_port_t port,
4598 vm_object_offset_t offset,
4599 boolean_t copy,
4600 vm_prot_t cur_protection,
4601 vm_prot_t max_protection,
4602 vm_inherit_t inheritance)
4603 {
4604 return mach_vm_map_external(target_map, address, initial_size, mask, flags, port,
4605 offset, copy, cur_protection, max_protection, inheritance);
4606 }
4607
4608 kern_return_t
mach_vm_remap(vm_map_t target_map,mach_vm_offset_t * address,mach_vm_size_t size,mach_vm_offset_t mask,int flags,vm_map_t src_map,mach_vm_offset_t memory_address,boolean_t copy,vm_prot_t * cur_protection,vm_prot_t * max_protection,vm_inherit_t inheritance)4609 mach_vm_remap(
4610 vm_map_t target_map,
4611 mach_vm_offset_t *address,
4612 mach_vm_size_t size,
4613 mach_vm_offset_t mask,
4614 int flags,
4615 vm_map_t src_map,
4616 mach_vm_offset_t memory_address,
4617 boolean_t copy,
4618 vm_prot_t *cur_protection, /* OUT */
4619 vm_prot_t *max_protection, /* OUT */
4620 vm_inherit_t inheritance)
4621 {
4622 return mach_vm_remap_external(target_map, address, size, mask, flags, src_map, memory_address,
4623 copy, cur_protection, max_protection, inheritance);
4624 }
4625
4626 kern_return_t
4627 vm_map(
4628 vm_map_t target_map,
4629 vm_offset_t *address,
4630 vm_size_t size,
4631 vm_offset_t mask,
4632 int flags,
4633 ipc_port_t port,
4634 vm_offset_t offset,
4635 boolean_t copy,
4636 vm_prot_t cur_protection,
4637 vm_prot_t max_protection,
4638 vm_inherit_t inheritance);
4639
4640 kern_return_t
vm_map(vm_map_t target_map,vm_offset_t * address,vm_size_t size,vm_offset_t mask,int flags,ipc_port_t port,vm_offset_t offset,boolean_t copy,vm_prot_t cur_protection,vm_prot_t max_protection,vm_inherit_t inheritance)4641 vm_map(
4642 vm_map_t target_map,
4643 vm_offset_t *address,
4644 vm_size_t size,
4645 vm_offset_t mask,
4646 int flags,
4647 ipc_port_t port,
4648 vm_offset_t offset,
4649 boolean_t copy,
4650 vm_prot_t cur_protection,
4651 vm_prot_t max_protection,
4652 vm_inherit_t inheritance)
4653 {
4654 static_assert(sizeof(vm_offset_t) == sizeof(mach_vm_offset_t));
4655
4656 return mach_vm_map(target_map, (mach_vm_offset_t *)address,
4657 size, mask, flags, port, offset, copy,
4658 cur_protection, max_protection, inheritance);
4659 }
4660
4661 #endif /* __x86_64__ */
4662