]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_map.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / osfmk / vm / vm_map.c
1 /*
2 * Copyright (c) 2000-2012 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,1987 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 Software.Distribution@CS.CMU.EDU
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_map.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 * Date: 1985
62 *
63 * Virtual memory mapping module.
64 */
65
66 #include <task_swapper.h>
67 #include <mach_assert.h>
68
69 #include <vm/vm_options.h>
70
71 #include <libkern/OSAtomic.h>
72
73 #include <mach/kern_return.h>
74 #include <mach/port.h>
75 #include <mach/vm_attributes.h>
76 #include <mach/vm_param.h>
77 #include <mach/vm_behavior.h>
78 #include <mach/vm_statistics.h>
79 #include <mach/memory_object.h>
80 #include <mach/mach_vm.h>
81 #include <machine/cpu_capabilities.h>
82 #include <mach/sdt.h>
83
84 #include <kern/assert.h>
85 #include <kern/counters.h>
86 #include <kern/kalloc.h>
87 #include <kern/zalloc.h>
88
89 #include <vm/cpm.h>
90 #include <vm/vm_compressor_pager.h>
91 #include <vm/vm_init.h>
92 #include <vm/vm_fault.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_pageout.h>
97 #include <vm/vm_kern.h>
98 #include <ipc/ipc_port.h>
99 #include <kern/sched_prim.h>
100 #include <kern/misc_protos.h>
101 #include <kern/xpr.h>
102
103 #include <mach/vm_map_server.h>
104 #include <mach/mach_host_server.h>
105 #include <vm/vm_protos.h>
106 #include <vm/vm_purgeable_internal.h>
107
108 #include <vm/vm_protos.h>
109 #include <vm/vm_shared_region.h>
110 #include <vm/vm_map_store.h>
111
112
113 extern u_int32_t random(void); /* from <libkern/libkern.h> */
114 /* Internal prototypes
115 */
116
117 static void vm_map_simplify_range(
118 vm_map_t map,
119 vm_map_offset_t start,
120 vm_map_offset_t end); /* forward */
121
122 static boolean_t vm_map_range_check(
123 vm_map_t map,
124 vm_map_offset_t start,
125 vm_map_offset_t end,
126 vm_map_entry_t *entry);
127
128 static vm_map_entry_t _vm_map_entry_create(
129 struct vm_map_header *map_header, boolean_t map_locked);
130
131 static void _vm_map_entry_dispose(
132 struct vm_map_header *map_header,
133 vm_map_entry_t entry);
134
135 static void vm_map_pmap_enter(
136 vm_map_t map,
137 vm_map_offset_t addr,
138 vm_map_offset_t end_addr,
139 vm_object_t object,
140 vm_object_offset_t offset,
141 vm_prot_t protection);
142
143 static void _vm_map_clip_end(
144 struct vm_map_header *map_header,
145 vm_map_entry_t entry,
146 vm_map_offset_t end);
147
148 static void _vm_map_clip_start(
149 struct vm_map_header *map_header,
150 vm_map_entry_t entry,
151 vm_map_offset_t start);
152
153 static void vm_map_entry_delete(
154 vm_map_t map,
155 vm_map_entry_t entry);
156
157 static kern_return_t vm_map_delete(
158 vm_map_t map,
159 vm_map_offset_t start,
160 vm_map_offset_t end,
161 int flags,
162 vm_map_t zap_map);
163
164 static kern_return_t vm_map_copy_overwrite_unaligned(
165 vm_map_t dst_map,
166 vm_map_entry_t entry,
167 vm_map_copy_t copy,
168 vm_map_address_t start,
169 boolean_t discard_on_success);
170
171 static kern_return_t vm_map_copy_overwrite_aligned(
172 vm_map_t dst_map,
173 vm_map_entry_t tmp_entry,
174 vm_map_copy_t copy,
175 vm_map_offset_t start,
176 pmap_t pmap);
177
178 static kern_return_t vm_map_copyin_kernel_buffer(
179 vm_map_t src_map,
180 vm_map_address_t src_addr,
181 vm_map_size_t len,
182 boolean_t src_destroy,
183 vm_map_copy_t *copy_result); /* OUT */
184
185 static kern_return_t vm_map_copyout_kernel_buffer(
186 vm_map_t map,
187 vm_map_address_t *addr, /* IN/OUT */
188 vm_map_copy_t copy,
189 boolean_t overwrite,
190 boolean_t consume_on_success);
191
192 static void vm_map_fork_share(
193 vm_map_t old_map,
194 vm_map_entry_t old_entry,
195 vm_map_t new_map);
196
197 static boolean_t vm_map_fork_copy(
198 vm_map_t old_map,
199 vm_map_entry_t *old_entry_p,
200 vm_map_t new_map);
201
202 void vm_map_region_top_walk(
203 vm_map_entry_t entry,
204 vm_region_top_info_t top);
205
206 void vm_map_region_walk(
207 vm_map_t map,
208 vm_map_offset_t va,
209 vm_map_entry_t entry,
210 vm_object_offset_t offset,
211 vm_object_size_t range,
212 vm_region_extended_info_t extended,
213 boolean_t look_for_pages,
214 mach_msg_type_number_t count);
215
216 static kern_return_t vm_map_wire_nested(
217 vm_map_t map,
218 vm_map_offset_t start,
219 vm_map_offset_t end,
220 vm_prot_t caller_prot,
221 boolean_t user_wire,
222 pmap_t map_pmap,
223 vm_map_offset_t pmap_addr,
224 ppnum_t *physpage_p);
225
226 static kern_return_t vm_map_unwire_nested(
227 vm_map_t map,
228 vm_map_offset_t start,
229 vm_map_offset_t end,
230 boolean_t user_wire,
231 pmap_t map_pmap,
232 vm_map_offset_t pmap_addr);
233
234 static kern_return_t vm_map_overwrite_submap_recurse(
235 vm_map_t dst_map,
236 vm_map_offset_t dst_addr,
237 vm_map_size_t dst_size);
238
239 static kern_return_t vm_map_copy_overwrite_nested(
240 vm_map_t dst_map,
241 vm_map_offset_t dst_addr,
242 vm_map_copy_t copy,
243 boolean_t interruptible,
244 pmap_t pmap,
245 boolean_t discard_on_success);
246
247 static kern_return_t vm_map_remap_extract(
248 vm_map_t map,
249 vm_map_offset_t addr,
250 vm_map_size_t size,
251 boolean_t copy,
252 struct vm_map_header *map_header,
253 vm_prot_t *cur_protection,
254 vm_prot_t *max_protection,
255 vm_inherit_t inheritance,
256 boolean_t pageable);
257
258 static kern_return_t vm_map_remap_range_allocate(
259 vm_map_t map,
260 vm_map_address_t *address,
261 vm_map_size_t size,
262 vm_map_offset_t mask,
263 int flags,
264 vm_map_entry_t *map_entry);
265
266 static void vm_map_region_look_for_page(
267 vm_map_t map,
268 vm_map_offset_t va,
269 vm_object_t object,
270 vm_object_offset_t offset,
271 int max_refcnt,
272 int depth,
273 vm_region_extended_info_t extended,
274 mach_msg_type_number_t count);
275
276 static int vm_map_region_count_obj_refs(
277 vm_map_entry_t entry,
278 vm_object_t object);
279
280
281 static kern_return_t vm_map_willneed(
282 vm_map_t map,
283 vm_map_offset_t start,
284 vm_map_offset_t end);
285
286 static kern_return_t vm_map_reuse_pages(
287 vm_map_t map,
288 vm_map_offset_t start,
289 vm_map_offset_t end);
290
291 static kern_return_t vm_map_reusable_pages(
292 vm_map_t map,
293 vm_map_offset_t start,
294 vm_map_offset_t end);
295
296 static kern_return_t vm_map_can_reuse(
297 vm_map_t map,
298 vm_map_offset_t start,
299 vm_map_offset_t end);
300
301 #if MACH_ASSERT
302 static kern_return_t vm_map_pageout(
303 vm_map_t map,
304 vm_map_offset_t start,
305 vm_map_offset_t end);
306 #endif /* MACH_ASSERT */
307
308 /*
309 * Macros to copy a vm_map_entry. We must be careful to correctly
310 * manage the wired page count. vm_map_entry_copy() creates a new
311 * map entry to the same memory - the wired count in the new entry
312 * must be set to zero. vm_map_entry_copy_full() creates a new
313 * entry that is identical to the old entry. This preserves the
314 * wire count; it's used for map splitting and zone changing in
315 * vm_map_copyout.
316 */
317
318 #define vm_map_entry_copy(NEW,OLD) \
319 MACRO_BEGIN \
320 boolean_t _vmec_reserved = (NEW)->from_reserved_zone; \
321 *(NEW) = *(OLD); \
322 (NEW)->is_shared = FALSE; \
323 (NEW)->needs_wakeup = FALSE; \
324 (NEW)->in_transition = FALSE; \
325 (NEW)->wired_count = 0; \
326 (NEW)->user_wired_count = 0; \
327 (NEW)->permanent = FALSE; \
328 (NEW)->used_for_jit = FALSE; \
329 (NEW)->from_reserved_zone = _vmec_reserved; \
330 (NEW)->iokit_acct = FALSE; \
331 (NEW)->vme_resilient_codesign = FALSE; \
332 (NEW)->vme_resilient_media = FALSE; \
333 MACRO_END
334
335 #define vm_map_entry_copy_full(NEW,OLD) \
336 MACRO_BEGIN \
337 boolean_t _vmecf_reserved = (NEW)->from_reserved_zone; \
338 (*(NEW) = *(OLD)); \
339 (NEW)->from_reserved_zone = _vmecf_reserved; \
340 MACRO_END
341
342 /*
343 * Decide if we want to allow processes to execute from their data or stack areas.
344 * override_nx() returns true if we do. Data/stack execution can be enabled independently
345 * for 32 and 64 bit processes. Set the VM_ABI_32 or VM_ABI_64 flags in allow_data_exec
346 * or allow_stack_exec to enable data execution for that type of data area for that particular
347 * ABI (or both by or'ing the flags together). These are initialized in the architecture
348 * specific pmap files since the default behavior varies according to architecture. The
349 * main reason it varies is because of the need to provide binary compatibility with old
350 * applications that were written before these restrictions came into being. In the old
351 * days, an app could execute anything it could read, but this has slowly been tightened
352 * up over time. The default behavior is:
353 *
354 * 32-bit PPC apps may execute from both stack and data areas
355 * 32-bit Intel apps may exeucte from data areas but not stack
356 * 64-bit PPC/Intel apps may not execute from either data or stack
357 *
358 * An application on any architecture may override these defaults by explicitly
359 * adding PROT_EXEC permission to the page in question with the mprotect(2)
360 * system call. This code here just determines what happens when an app tries to
361 * execute from a page that lacks execute permission.
362 *
363 * Note that allow_data_exec or allow_stack_exec may also be modified by sysctl to change the
364 * default behavior for both 32 and 64 bit apps on a system-wide basis. Furthermore,
365 * a Mach-O header flag bit (MH_NO_HEAP_EXECUTION) can be used to forcibly disallow
366 * execution from data areas for a particular binary even if the arch normally permits it. As
367 * a final wrinkle, a posix_spawn attribute flag can be used to negate this opt-in header bit
368 * to support some complicated use cases, notably browsers with out-of-process plugins that
369 * are not all NX-safe.
370 */
371
372 extern int allow_data_exec, allow_stack_exec;
373
374 int
375 override_nx(vm_map_t map, uint32_t user_tag) /* map unused on arm */
376 {
377 int current_abi;
378
379 if (map->pmap == kernel_pmap) return FALSE;
380
381 /*
382 * Determine if the app is running in 32 or 64 bit mode.
383 */
384
385 if (vm_map_is_64bit(map))
386 current_abi = VM_ABI_64;
387 else
388 current_abi = VM_ABI_32;
389
390 /*
391 * Determine if we should allow the execution based on whether it's a
392 * stack or data area and the current architecture.
393 */
394
395 if (user_tag == VM_MEMORY_STACK)
396 return allow_stack_exec & current_abi;
397
398 return (allow_data_exec & current_abi) && (map->map_disallow_data_exec == FALSE);
399 }
400
401
402 /*
403 * Virtual memory maps provide for the mapping, protection,
404 * and sharing of virtual memory objects. In addition,
405 * this module provides for an efficient virtual copy of
406 * memory from one map to another.
407 *
408 * Synchronization is required prior to most operations.
409 *
410 * Maps consist of an ordered doubly-linked list of simple
411 * entries; a single hint is used to speed up lookups.
412 *
413 * Sharing maps have been deleted from this version of Mach.
414 * All shared objects are now mapped directly into the respective
415 * maps. This requires a change in the copy on write strategy;
416 * the asymmetric (delayed) strategy is used for shared temporary
417 * objects instead of the symmetric (shadow) strategy. All maps
418 * are now "top level" maps (either task map, kernel map or submap
419 * of the kernel map).
420 *
421 * Since portions of maps are specified by start/end addreses,
422 * which may not align with existing map entries, all
423 * routines merely "clip" entries to these start/end values.
424 * [That is, an entry is split into two, bordering at a
425 * start or end value.] Note that these clippings may not
426 * always be necessary (as the two resulting entries are then
427 * not changed); however, the clipping is done for convenience.
428 * No attempt is currently made to "glue back together" two
429 * abutting entries.
430 *
431 * The symmetric (shadow) copy strategy implements virtual copy
432 * by copying VM object references from one map to
433 * another, and then marking both regions as copy-on-write.
434 * It is important to note that only one writeable reference
435 * to a VM object region exists in any map when this strategy
436 * is used -- this means that shadow object creation can be
437 * delayed until a write operation occurs. The symmetric (delayed)
438 * strategy allows multiple maps to have writeable references to
439 * the same region of a vm object, and hence cannot delay creating
440 * its copy objects. See vm_object_copy_quickly() in vm_object.c.
441 * Copying of permanent objects is completely different; see
442 * vm_object_copy_strategically() in vm_object.c.
443 */
444
445 static zone_t vm_map_zone; /* zone for vm_map structures */
446 static zone_t vm_map_entry_zone; /* zone for vm_map_entry structures */
447 static zone_t vm_map_entry_reserved_zone; /* zone with reserve for non-blocking
448 * allocations */
449 static zone_t vm_map_copy_zone; /* zone for vm_map_copy structures */
450 zone_t vm_map_holes_zone; /* zone for vm map holes (vm_map_links) structures */
451
452
453 /*
454 * Placeholder object for submap operations. This object is dropped
455 * into the range by a call to vm_map_find, and removed when
456 * vm_map_submap creates the submap.
457 */
458
459 vm_object_t vm_submap_object;
460
461 static void *map_data;
462 static vm_size_t map_data_size;
463 static void *kentry_data;
464 static vm_size_t kentry_data_size;
465 static void *map_holes_data;
466 static vm_size_t map_holes_data_size;
467
468 #define NO_COALESCE_LIMIT ((1024 * 128) - 1)
469
470 /* Skip acquiring locks if we're in the midst of a kernel core dump */
471 unsigned int not_in_kdp = 1;
472
473 unsigned int vm_map_set_cache_attr_count = 0;
474
475 kern_return_t
476 vm_map_set_cache_attr(
477 vm_map_t map,
478 vm_map_offset_t va)
479 {
480 vm_map_entry_t map_entry;
481 vm_object_t object;
482 kern_return_t kr = KERN_SUCCESS;
483
484 vm_map_lock_read(map);
485
486 if (!vm_map_lookup_entry(map, va, &map_entry) ||
487 map_entry->is_sub_map) {
488 /*
489 * that memory is not properly mapped
490 */
491 kr = KERN_INVALID_ARGUMENT;
492 goto done;
493 }
494 object = VME_OBJECT(map_entry);
495
496 if (object == VM_OBJECT_NULL) {
497 /*
498 * there should be a VM object here at this point
499 */
500 kr = KERN_INVALID_ARGUMENT;
501 goto done;
502 }
503 vm_object_lock(object);
504 object->set_cache_attr = TRUE;
505 vm_object_unlock(object);
506
507 vm_map_set_cache_attr_count++;
508 done:
509 vm_map_unlock_read(map);
510
511 return kr;
512 }
513
514
515 #if CONFIG_CODE_DECRYPTION
516 /*
517 * vm_map_apple_protected:
518 * This remaps the requested part of the object with an object backed by
519 * the decrypting pager.
520 * crypt_info contains entry points and session data for the crypt module.
521 * The crypt_info block will be copied by vm_map_apple_protected. The data structures
522 * referenced in crypt_info must remain valid until crypt_info->crypt_end() is called.
523 */
524 kern_return_t
525 vm_map_apple_protected(
526 vm_map_t map,
527 vm_map_offset_t start,
528 vm_map_offset_t end,
529 vm_object_offset_t crypto_backing_offset,
530 struct pager_crypt_info *crypt_info)
531 {
532 boolean_t map_locked;
533 kern_return_t kr;
534 vm_map_entry_t map_entry;
535 struct vm_map_entry tmp_entry;
536 memory_object_t unprotected_mem_obj;
537 vm_object_t protected_object;
538 vm_map_offset_t map_addr;
539 vm_map_offset_t start_aligned, end_aligned;
540 vm_object_offset_t crypto_start, crypto_end;
541 int vm_flags;
542
543 map_locked = FALSE;
544 unprotected_mem_obj = MEMORY_OBJECT_NULL;
545
546 start_aligned = vm_map_trunc_page(start, PAGE_MASK_64);
547 end_aligned = vm_map_round_page(end, PAGE_MASK_64);
548 start_aligned = vm_map_trunc_page(start_aligned, VM_MAP_PAGE_MASK(map));
549 end_aligned = vm_map_round_page(end_aligned, VM_MAP_PAGE_MASK(map));
550
551 assert(start_aligned == start);
552 assert(end_aligned == end);
553
554 map_addr = start_aligned;
555 for (map_addr = start_aligned;
556 map_addr < end;
557 map_addr = tmp_entry.vme_end) {
558 vm_map_lock(map);
559 map_locked = TRUE;
560
561 /* lookup the protected VM object */
562 if (!vm_map_lookup_entry(map,
563 map_addr,
564 &map_entry) ||
565 map_entry->is_sub_map ||
566 VME_OBJECT(map_entry) == VM_OBJECT_NULL ||
567 !(map_entry->protection & VM_PROT_EXECUTE)) {
568 /* that memory is not properly mapped */
569 kr = KERN_INVALID_ARGUMENT;
570 goto done;
571 }
572
573 /* get the protected object to be decrypted */
574 protected_object = VME_OBJECT(map_entry);
575 if (protected_object == VM_OBJECT_NULL) {
576 /* there should be a VM object here at this point */
577 kr = KERN_INVALID_ARGUMENT;
578 goto done;
579 }
580 /* ensure protected object stays alive while map is unlocked */
581 vm_object_reference(protected_object);
582
583 /* limit the map entry to the area we want to cover */
584 vm_map_clip_start(map, map_entry, start_aligned);
585 vm_map_clip_end(map, map_entry, end_aligned);
586
587 tmp_entry = *map_entry;
588 map_entry = VM_MAP_ENTRY_NULL; /* not valid after unlocking map */
589 vm_map_unlock(map);
590 map_locked = FALSE;
591
592 /*
593 * This map entry might be only partially encrypted
594 * (if not fully "page-aligned").
595 */
596 crypto_start = 0;
597 crypto_end = tmp_entry.vme_end - tmp_entry.vme_start;
598 if (tmp_entry.vme_start < start) {
599 if (tmp_entry.vme_start != start_aligned) {
600 kr = KERN_INVALID_ADDRESS;
601 }
602 crypto_start += (start - tmp_entry.vme_start);
603 }
604 if (tmp_entry.vme_end > end) {
605 if (tmp_entry.vme_end != end_aligned) {
606 kr = KERN_INVALID_ADDRESS;
607 }
608 crypto_end -= (tmp_entry.vme_end - end);
609 }
610
611 /*
612 * This "extra backing offset" is needed to get the decryption
613 * routine to use the right key. It adjusts for the possibly
614 * relative offset of an interposed "4K" pager...
615 */
616 if (crypto_backing_offset == (vm_object_offset_t) -1) {
617 crypto_backing_offset = VME_OFFSET(&tmp_entry);
618 }
619
620 /*
621 * Lookup (and create if necessary) the protected memory object
622 * matching that VM object.
623 * If successful, this also grabs a reference on the memory object,
624 * to guarantee that it doesn't go away before we get a chance to map
625 * it.
626 */
627 unprotected_mem_obj = apple_protect_pager_setup(
628 protected_object,
629 VME_OFFSET(&tmp_entry),
630 crypto_backing_offset,
631 crypt_info,
632 crypto_start,
633 crypto_end);
634
635 /* release extra ref on protected object */
636 vm_object_deallocate(protected_object);
637
638 if (unprotected_mem_obj == NULL) {
639 kr = KERN_FAILURE;
640 goto done;
641 }
642
643 vm_flags = VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE;
644
645 /* map this memory object in place of the current one */
646 map_addr = tmp_entry.vme_start;
647 kr = vm_map_enter_mem_object(map,
648 &map_addr,
649 (tmp_entry.vme_end -
650 tmp_entry.vme_start),
651 (mach_vm_offset_t) 0,
652 vm_flags,
653 (ipc_port_t) unprotected_mem_obj,
654 0,
655 TRUE,
656 tmp_entry.protection,
657 tmp_entry.max_protection,
658 tmp_entry.inheritance);
659 assert(kr == KERN_SUCCESS);
660 assert(map_addr == tmp_entry.vme_start);
661
662 #if VM_MAP_DEBUG_APPLE_PROTECT
663 printf("APPLE_PROTECT: map %p [0x%llx:0x%llx] pager %p: "
664 "backing:[object:%p,offset:0x%llx,"
665 "crypto_backing_offset:0x%llx,"
666 "crypto_start:0x%llx,crypto_end:0x%llx]\n",
667 map,
668 (uint64_t) map_addr,
669 (uint64_t) (map_addr + (tmp_entry.vme_end -
670 tmp_entry.vme_start)),
671 unprotected_mem_obj,
672 protected_object,
673 VME_OFFSET(&tmp_entry),
674 crypto_backing_offset,
675 crypto_start,
676 crypto_end);
677 #endif /* VM_MAP_DEBUG_APPLE_PROTECT */
678
679 /*
680 * Release the reference obtained by
681 * apple_protect_pager_setup().
682 * The mapping (if it succeeded) is now holding a reference on
683 * the memory object.
684 */
685 memory_object_deallocate(unprotected_mem_obj);
686 unprotected_mem_obj = MEMORY_OBJECT_NULL;
687
688 /* continue with next map entry */
689 crypto_backing_offset += (tmp_entry.vme_end -
690 tmp_entry.vme_start);
691 crypto_backing_offset -= crypto_start;
692 }
693 kr = KERN_SUCCESS;
694
695 done:
696 if (map_locked) {
697 vm_map_unlock(map);
698 }
699 return kr;
700 }
701 #endif /* CONFIG_CODE_DECRYPTION */
702
703
704 lck_grp_t vm_map_lck_grp;
705 lck_grp_attr_t vm_map_lck_grp_attr;
706 lck_attr_t vm_map_lck_attr;
707 lck_attr_t vm_map_lck_rw_attr;
708
709
710 /*
711 * vm_map_init:
712 *
713 * Initialize the vm_map module. Must be called before
714 * any other vm_map routines.
715 *
716 * Map and entry structures are allocated from zones -- we must
717 * initialize those zones.
718 *
719 * There are three zones of interest:
720 *
721 * vm_map_zone: used to allocate maps.
722 * vm_map_entry_zone: used to allocate map entries.
723 * vm_map_entry_reserved_zone: fallback zone for kernel map entries
724 *
725 * The kernel allocates map entries from a special zone that is initially
726 * "crammed" with memory. It would be difficult (perhaps impossible) for
727 * the kernel to allocate more memory to a entry zone when it became
728 * empty since the very act of allocating memory implies the creation
729 * of a new entry.
730 */
731 void
732 vm_map_init(
733 void)
734 {
735 vm_size_t entry_zone_alloc_size;
736 const char *mez_name = "VM map entries";
737
738 vm_map_zone = zinit((vm_map_size_t) sizeof(struct _vm_map), 40*1024,
739 PAGE_SIZE, "maps");
740 zone_change(vm_map_zone, Z_NOENCRYPT, TRUE);
741 #if defined(__LP64__)
742 entry_zone_alloc_size = PAGE_SIZE * 5;
743 #else
744 entry_zone_alloc_size = PAGE_SIZE * 6;
745 #endif
746 vm_map_entry_zone = zinit((vm_map_size_t) sizeof(struct vm_map_entry),
747 1024*1024, entry_zone_alloc_size,
748 mez_name);
749 zone_change(vm_map_entry_zone, Z_NOENCRYPT, TRUE);
750 zone_change(vm_map_entry_zone, Z_NOCALLOUT, TRUE);
751 zone_change(vm_map_entry_zone, Z_GZALLOC_EXEMPT, TRUE);
752
753 vm_map_entry_reserved_zone = zinit((vm_map_size_t) sizeof(struct vm_map_entry),
754 kentry_data_size * 64, kentry_data_size,
755 "Reserved VM map entries");
756 zone_change(vm_map_entry_reserved_zone, Z_NOENCRYPT, TRUE);
757
758 vm_map_copy_zone = zinit((vm_map_size_t) sizeof(struct vm_map_copy),
759 16*1024, PAGE_SIZE, "VM map copies");
760 zone_change(vm_map_copy_zone, Z_NOENCRYPT, TRUE);
761
762 vm_map_holes_zone = zinit((vm_map_size_t) sizeof(struct vm_map_links),
763 16*1024, PAGE_SIZE, "VM map holes");
764 zone_change(vm_map_holes_zone, Z_NOENCRYPT, TRUE);
765
766 /*
767 * Cram the map and kentry zones with initial data.
768 * Set reserved_zone non-collectible to aid zone_gc().
769 */
770 zone_change(vm_map_zone, Z_COLLECT, FALSE);
771
772 zone_change(vm_map_entry_reserved_zone, Z_COLLECT, FALSE);
773 zone_change(vm_map_entry_reserved_zone, Z_EXPAND, FALSE);
774 zone_change(vm_map_entry_reserved_zone, Z_FOREIGN, TRUE);
775 zone_change(vm_map_entry_reserved_zone, Z_NOCALLOUT, TRUE);
776 zone_change(vm_map_entry_reserved_zone, Z_CALLERACCT, FALSE); /* don't charge caller */
777 zone_change(vm_map_copy_zone, Z_CALLERACCT, FALSE); /* don't charge caller */
778 zone_change(vm_map_entry_reserved_zone, Z_GZALLOC_EXEMPT, TRUE);
779
780 zone_change(vm_map_holes_zone, Z_COLLECT, TRUE);
781 zone_change(vm_map_holes_zone, Z_EXPAND, TRUE);
782 zone_change(vm_map_holes_zone, Z_FOREIGN, TRUE);
783 zone_change(vm_map_holes_zone, Z_NOCALLOUT, TRUE);
784 zone_change(vm_map_holes_zone, Z_CALLERACCT, TRUE);
785 zone_change(vm_map_holes_zone, Z_GZALLOC_EXEMPT, TRUE);
786
787 /*
788 * Add the stolen memory to zones, adjust zone size and stolen counts.
789 */
790 zcram(vm_map_zone, (vm_offset_t)map_data, map_data_size);
791 zcram(vm_map_entry_reserved_zone, (vm_offset_t)kentry_data, kentry_data_size);
792 zcram(vm_map_holes_zone, (vm_offset_t)map_holes_data, map_holes_data_size);
793 VM_PAGE_MOVE_STOLEN(atop_64(map_data_size) + atop_64(kentry_data_size) + atop_64(map_holes_data_size));
794
795 lck_grp_attr_setdefault(&vm_map_lck_grp_attr);
796 lck_grp_init(&vm_map_lck_grp, "vm_map", &vm_map_lck_grp_attr);
797 lck_attr_setdefault(&vm_map_lck_attr);
798
799 lck_attr_setdefault(&vm_map_lck_rw_attr);
800 lck_attr_cleardebug(&vm_map_lck_rw_attr);
801
802 #if CONFIG_FREEZE
803 default_freezer_init();
804 #endif /* CONFIG_FREEZE */
805 }
806
807 void
808 vm_map_steal_memory(
809 void)
810 {
811 uint32_t kentry_initial_pages;
812
813 map_data_size = round_page(10 * sizeof(struct _vm_map));
814 map_data = pmap_steal_memory(map_data_size);
815
816 /*
817 * kentry_initial_pages corresponds to the number of kernel map entries
818 * required during bootstrap until the asynchronous replenishment
819 * scheme is activated and/or entries are available from the general
820 * map entry pool.
821 */
822 #if defined(__LP64__)
823 kentry_initial_pages = 10;
824 #else
825 kentry_initial_pages = 6;
826 #endif
827
828 #if CONFIG_GZALLOC
829 /* If using the guard allocator, reserve more memory for the kernel
830 * reserved map entry pool.
831 */
832 if (gzalloc_enabled())
833 kentry_initial_pages *= 1024;
834 #endif
835
836 kentry_data_size = kentry_initial_pages * PAGE_SIZE;
837 kentry_data = pmap_steal_memory(kentry_data_size);
838
839 map_holes_data_size = kentry_data_size;
840 map_holes_data = pmap_steal_memory(map_holes_data_size);
841 }
842
843 void
844 vm_kernel_reserved_entry_init(void) {
845 zone_prio_refill_configure(vm_map_entry_reserved_zone, (6*PAGE_SIZE)/sizeof(struct vm_map_entry));
846 zone_prio_refill_configure(vm_map_holes_zone, (6*PAGE_SIZE)/sizeof(struct vm_map_links));
847 }
848
849 void
850 vm_map_disable_hole_optimization(vm_map_t map)
851 {
852 vm_map_entry_t head_entry, hole_entry, next_hole_entry;
853
854 if (map->holelistenabled) {
855
856 head_entry = hole_entry = (vm_map_entry_t) map->holes_list;
857
858 while (hole_entry != NULL) {
859
860 next_hole_entry = hole_entry->vme_next;
861
862 hole_entry->vme_next = NULL;
863 hole_entry->vme_prev = NULL;
864 zfree(vm_map_holes_zone, hole_entry);
865
866 if (next_hole_entry == head_entry) {
867 hole_entry = NULL;
868 } else {
869 hole_entry = next_hole_entry;
870 }
871 }
872
873 map->holes_list = NULL;
874 map->holelistenabled = FALSE;
875
876 map->first_free = vm_map_first_entry(map);
877 SAVE_HINT_HOLE_WRITE(map, NULL);
878 }
879 }
880
881 boolean_t
882 vm_kernel_map_is_kernel(vm_map_t map) {
883 return (map->pmap == kernel_pmap);
884 }
885
886 /*
887 * vm_map_create:
888 *
889 * Creates and returns a new empty VM map with
890 * the given physical map structure, and having
891 * the given lower and upper address bounds.
892 */
893
894 boolean_t vm_map_supports_hole_optimization = TRUE;
895
896 vm_map_t
897 vm_map_create(
898 pmap_t pmap,
899 vm_map_offset_t min,
900 vm_map_offset_t max,
901 boolean_t pageable)
902 {
903 static int color_seed = 0;
904 register vm_map_t result;
905 struct vm_map_links *hole_entry = NULL;
906
907 result = (vm_map_t) zalloc(vm_map_zone);
908 if (result == VM_MAP_NULL)
909 panic("vm_map_create");
910
911 vm_map_first_entry(result) = vm_map_to_entry(result);
912 vm_map_last_entry(result) = vm_map_to_entry(result);
913 result->hdr.nentries = 0;
914 result->hdr.entries_pageable = pageable;
915
916 vm_map_store_init( &(result->hdr) );
917
918 result->hdr.page_shift = PAGE_SHIFT;
919
920 result->size = 0;
921 result->user_wire_limit = MACH_VM_MAX_ADDRESS; /* default limit is unlimited */
922 result->user_wire_size = 0;
923 result->ref_count = 1;
924 #if TASK_SWAPPER
925 result->res_count = 1;
926 result->sw_state = MAP_SW_IN;
927 #endif /* TASK_SWAPPER */
928 result->pmap = pmap;
929 result->min_offset = min;
930 result->max_offset = max;
931 result->wiring_required = FALSE;
932 result->no_zero_fill = FALSE;
933 result->mapped_in_other_pmaps = FALSE;
934 result->wait_for_space = FALSE;
935 result->switch_protect = FALSE;
936 result->disable_vmentry_reuse = FALSE;
937 result->map_disallow_data_exec = FALSE;
938 result->highest_entry_end = 0;
939 result->first_free = vm_map_to_entry(result);
940 result->hint = vm_map_to_entry(result);
941 result->color_rr = (color_seed++) & vm_color_mask;
942 result->jit_entry_exists = FALSE;
943
944 if (vm_map_supports_hole_optimization && pmap != kernel_pmap) {
945 hole_entry = zalloc(vm_map_holes_zone);
946
947 hole_entry->start = min;
948 hole_entry->end = (max > (vm_map_offset_t)MACH_VM_MAX_ADDRESS) ? max : (vm_map_offset_t)MACH_VM_MAX_ADDRESS;
949 result->holes_list = result->hole_hint = hole_entry;
950 hole_entry->prev = hole_entry->next = (vm_map_entry_t) hole_entry;
951 result->holelistenabled = TRUE;
952
953 } else {
954
955 result->holelistenabled = FALSE;
956 }
957
958 #if CONFIG_FREEZE
959 result->default_freezer_handle = NULL;
960 #endif
961 vm_map_lock_init(result);
962 lck_mtx_init_ext(&result->s_lock, &result->s_lock_ext, &vm_map_lck_grp, &vm_map_lck_attr);
963
964 return(result);
965 }
966
967 /*
968 * vm_map_entry_create: [ internal use only ]
969 *
970 * Allocates a VM map entry for insertion in the
971 * given map (or map copy). No fields are filled.
972 */
973 #define vm_map_entry_create(map, map_locked) _vm_map_entry_create(&(map)->hdr, map_locked)
974
975 #define vm_map_copy_entry_create(copy, map_locked) \
976 _vm_map_entry_create(&(copy)->cpy_hdr, map_locked)
977 unsigned reserved_zalloc_count, nonreserved_zalloc_count;
978
979 static vm_map_entry_t
980 _vm_map_entry_create(
981 struct vm_map_header *map_header, boolean_t __unused map_locked)
982 {
983 zone_t zone;
984 vm_map_entry_t entry;
985
986 zone = vm_map_entry_zone;
987
988 assert(map_header->entries_pageable ? !map_locked : TRUE);
989
990 if (map_header->entries_pageable) {
991 entry = (vm_map_entry_t) zalloc(zone);
992 }
993 else {
994 entry = (vm_map_entry_t) zalloc_canblock(zone, FALSE);
995
996 if (entry == VM_MAP_ENTRY_NULL) {
997 zone = vm_map_entry_reserved_zone;
998 entry = (vm_map_entry_t) zalloc(zone);
999 OSAddAtomic(1, &reserved_zalloc_count);
1000 } else
1001 OSAddAtomic(1, &nonreserved_zalloc_count);
1002 }
1003
1004 if (entry == VM_MAP_ENTRY_NULL)
1005 panic("vm_map_entry_create");
1006 entry->from_reserved_zone = (zone == vm_map_entry_reserved_zone);
1007
1008 vm_map_store_update( (vm_map_t) NULL, entry, VM_MAP_ENTRY_CREATE);
1009 #if MAP_ENTRY_CREATION_DEBUG
1010 entry->vme_creation_maphdr = map_header;
1011 fastbacktrace(&entry->vme_creation_bt[0],
1012 (sizeof(entry->vme_creation_bt)/sizeof(uintptr_t)));
1013 #endif
1014 return(entry);
1015 }
1016
1017 /*
1018 * vm_map_entry_dispose: [ internal use only ]
1019 *
1020 * Inverse of vm_map_entry_create.
1021 *
1022 * write map lock held so no need to
1023 * do anything special to insure correctness
1024 * of the stores
1025 */
1026 #define vm_map_entry_dispose(map, entry) \
1027 _vm_map_entry_dispose(&(map)->hdr, (entry))
1028
1029 #define vm_map_copy_entry_dispose(map, entry) \
1030 _vm_map_entry_dispose(&(copy)->cpy_hdr, (entry))
1031
1032 static void
1033 _vm_map_entry_dispose(
1034 register struct vm_map_header *map_header,
1035 register vm_map_entry_t entry)
1036 {
1037 register zone_t zone;
1038
1039 if (map_header->entries_pageable || !(entry->from_reserved_zone))
1040 zone = vm_map_entry_zone;
1041 else
1042 zone = vm_map_entry_reserved_zone;
1043
1044 if (!map_header->entries_pageable) {
1045 if (zone == vm_map_entry_zone)
1046 OSAddAtomic(-1, &nonreserved_zalloc_count);
1047 else
1048 OSAddAtomic(-1, &reserved_zalloc_count);
1049 }
1050
1051 zfree(zone, entry);
1052 }
1053
1054 #if MACH_ASSERT
1055 static boolean_t first_free_check = FALSE;
1056 boolean_t
1057 first_free_is_valid(
1058 vm_map_t map)
1059 {
1060 if (!first_free_check)
1061 return TRUE;
1062
1063 return( first_free_is_valid_store( map ));
1064 }
1065 #endif /* MACH_ASSERT */
1066
1067
1068 #define vm_map_copy_entry_link(copy, after_where, entry) \
1069 _vm_map_store_entry_link(&(copy)->cpy_hdr, after_where, (entry))
1070
1071 #define vm_map_copy_entry_unlink(copy, entry) \
1072 _vm_map_store_entry_unlink(&(copy)->cpy_hdr, (entry))
1073
1074 #if MACH_ASSERT && TASK_SWAPPER
1075 /*
1076 * vm_map_res_reference:
1077 *
1078 * Adds another valid residence count to the given map.
1079 *
1080 * Map is locked so this function can be called from
1081 * vm_map_swapin.
1082 *
1083 */
1084 void vm_map_res_reference(register vm_map_t map)
1085 {
1086 /* assert map is locked */
1087 assert(map->res_count >= 0);
1088 assert(map->ref_count >= map->res_count);
1089 if (map->res_count == 0) {
1090 lck_mtx_unlock(&map->s_lock);
1091 vm_map_lock(map);
1092 vm_map_swapin(map);
1093 lck_mtx_lock(&map->s_lock);
1094 ++map->res_count;
1095 vm_map_unlock(map);
1096 } else
1097 ++map->res_count;
1098 }
1099
1100 /*
1101 * vm_map_reference_swap:
1102 *
1103 * Adds valid reference and residence counts to the given map.
1104 *
1105 * The map may not be in memory (i.e. zero residence count).
1106 *
1107 */
1108 void vm_map_reference_swap(register vm_map_t map)
1109 {
1110 assert(map != VM_MAP_NULL);
1111 lck_mtx_lock(&map->s_lock);
1112 assert(map->res_count >= 0);
1113 assert(map->ref_count >= map->res_count);
1114 map->ref_count++;
1115 vm_map_res_reference(map);
1116 lck_mtx_unlock(&map->s_lock);
1117 }
1118
1119 /*
1120 * vm_map_res_deallocate:
1121 *
1122 * Decrement residence count on a map; possibly causing swapout.
1123 *
1124 * The map must be in memory (i.e. non-zero residence count).
1125 *
1126 * The map is locked, so this function is callable from vm_map_deallocate.
1127 *
1128 */
1129 void vm_map_res_deallocate(register vm_map_t map)
1130 {
1131 assert(map->res_count > 0);
1132 if (--map->res_count == 0) {
1133 lck_mtx_unlock(&map->s_lock);
1134 vm_map_lock(map);
1135 vm_map_swapout(map);
1136 vm_map_unlock(map);
1137 lck_mtx_lock(&map->s_lock);
1138 }
1139 assert(map->ref_count >= map->res_count);
1140 }
1141 #endif /* MACH_ASSERT && TASK_SWAPPER */
1142
1143 /*
1144 * vm_map_destroy:
1145 *
1146 * Actually destroy a map.
1147 */
1148 void
1149 vm_map_destroy(
1150 vm_map_t map,
1151 int flags)
1152 {
1153 vm_map_lock(map);
1154
1155 /* final cleanup: no need to unnest shared region */
1156 flags |= VM_MAP_REMOVE_NO_UNNESTING;
1157
1158 /* clean up regular map entries */
1159 (void) vm_map_delete(map, map->min_offset, map->max_offset,
1160 flags, VM_MAP_NULL);
1161 /* clean up leftover special mappings (commpage, etc...) */
1162 (void) vm_map_delete(map, 0x0, 0xFFFFFFFFFFFFF000ULL,
1163 flags, VM_MAP_NULL);
1164
1165 #if CONFIG_FREEZE
1166 if (map->default_freezer_handle) {
1167 default_freezer_handle_deallocate(map->default_freezer_handle);
1168 map->default_freezer_handle = NULL;
1169 }
1170 #endif
1171 vm_map_disable_hole_optimization(map);
1172 vm_map_unlock(map);
1173
1174 assert(map->hdr.nentries == 0);
1175
1176 if(map->pmap)
1177 pmap_destroy(map->pmap);
1178
1179 zfree(vm_map_zone, map);
1180 }
1181
1182 #if TASK_SWAPPER
1183 /*
1184 * vm_map_swapin/vm_map_swapout
1185 *
1186 * Swap a map in and out, either referencing or releasing its resources.
1187 * These functions are internal use only; however, they must be exported
1188 * because they may be called from macros, which are exported.
1189 *
1190 * In the case of swapout, there could be races on the residence count,
1191 * so if the residence count is up, we return, assuming that a
1192 * vm_map_deallocate() call in the near future will bring us back.
1193 *
1194 * Locking:
1195 * -- We use the map write lock for synchronization among races.
1196 * -- The map write lock, and not the simple s_lock, protects the
1197 * swap state of the map.
1198 * -- If a map entry is a share map, then we hold both locks, in
1199 * hierarchical order.
1200 *
1201 * Synchronization Notes:
1202 * 1) If a vm_map_swapin() call happens while swapout in progress, it
1203 * will block on the map lock and proceed when swapout is through.
1204 * 2) A vm_map_reference() call at this time is illegal, and will
1205 * cause a panic. vm_map_reference() is only allowed on resident
1206 * maps, since it refuses to block.
1207 * 3) A vm_map_swapin() call during a swapin will block, and
1208 * proceeed when the first swapin is done, turning into a nop.
1209 * This is the reason the res_count is not incremented until
1210 * after the swapin is complete.
1211 * 4) There is a timing hole after the checks of the res_count, before
1212 * the map lock is taken, during which a swapin may get the lock
1213 * before a swapout about to happen. If this happens, the swapin
1214 * will detect the state and increment the reference count, causing
1215 * the swapout to be a nop, thereby delaying it until a later
1216 * vm_map_deallocate. If the swapout gets the lock first, then
1217 * the swapin will simply block until the swapout is done, and
1218 * then proceed.
1219 *
1220 * Because vm_map_swapin() is potentially an expensive operation, it
1221 * should be used with caution.
1222 *
1223 * Invariants:
1224 * 1) A map with a residence count of zero is either swapped, or
1225 * being swapped.
1226 * 2) A map with a non-zero residence count is either resident,
1227 * or being swapped in.
1228 */
1229
1230 int vm_map_swap_enable = 1;
1231
1232 void vm_map_swapin (vm_map_t map)
1233 {
1234 register vm_map_entry_t entry;
1235
1236 if (!vm_map_swap_enable) /* debug */
1237 return;
1238
1239 /*
1240 * Map is locked
1241 * First deal with various races.
1242 */
1243 if (map->sw_state == MAP_SW_IN)
1244 /*
1245 * we raced with swapout and won. Returning will incr.
1246 * the res_count, turning the swapout into a nop.
1247 */
1248 return;
1249
1250 /*
1251 * The residence count must be zero. If we raced with another
1252 * swapin, the state would have been IN; if we raced with a
1253 * swapout (after another competing swapin), we must have lost
1254 * the race to get here (see above comment), in which case
1255 * res_count is still 0.
1256 */
1257 assert(map->res_count == 0);
1258
1259 /*
1260 * There are no intermediate states of a map going out or
1261 * coming in, since the map is locked during the transition.
1262 */
1263 assert(map->sw_state == MAP_SW_OUT);
1264
1265 /*
1266 * We now operate upon each map entry. If the entry is a sub-
1267 * or share-map, we call vm_map_res_reference upon it.
1268 * If the entry is an object, we call vm_object_res_reference
1269 * (this may iterate through the shadow chain).
1270 * Note that we hold the map locked the entire time,
1271 * even if we get back here via a recursive call in
1272 * vm_map_res_reference.
1273 */
1274 entry = vm_map_first_entry(map);
1275
1276 while (entry != vm_map_to_entry(map)) {
1277 if (VME_OBJECT(entry) != VM_OBJECT_NULL) {
1278 if (entry->is_sub_map) {
1279 vm_map_t lmap = VME_SUBMAP(entry);
1280 lck_mtx_lock(&lmap->s_lock);
1281 vm_map_res_reference(lmap);
1282 lck_mtx_unlock(&lmap->s_lock);
1283 } else {
1284 vm_object_t object = VME_OBEJCT(entry);
1285 vm_object_lock(object);
1286 /*
1287 * This call may iterate through the
1288 * shadow chain.
1289 */
1290 vm_object_res_reference(object);
1291 vm_object_unlock(object);
1292 }
1293 }
1294 entry = entry->vme_next;
1295 }
1296 assert(map->sw_state == MAP_SW_OUT);
1297 map->sw_state = MAP_SW_IN;
1298 }
1299
1300 void vm_map_swapout(vm_map_t map)
1301 {
1302 register vm_map_entry_t entry;
1303
1304 /*
1305 * Map is locked
1306 * First deal with various races.
1307 * If we raced with a swapin and lost, the residence count
1308 * will have been incremented to 1, and we simply return.
1309 */
1310 lck_mtx_lock(&map->s_lock);
1311 if (map->res_count != 0) {
1312 lck_mtx_unlock(&map->s_lock);
1313 return;
1314 }
1315 lck_mtx_unlock(&map->s_lock);
1316
1317 /*
1318 * There are no intermediate states of a map going out or
1319 * coming in, since the map is locked during the transition.
1320 */
1321 assert(map->sw_state == MAP_SW_IN);
1322
1323 if (!vm_map_swap_enable)
1324 return;
1325
1326 /*
1327 * We now operate upon each map entry. If the entry is a sub-
1328 * or share-map, we call vm_map_res_deallocate upon it.
1329 * If the entry is an object, we call vm_object_res_deallocate
1330 * (this may iterate through the shadow chain).
1331 * Note that we hold the map locked the entire time,
1332 * even if we get back here via a recursive call in
1333 * vm_map_res_deallocate.
1334 */
1335 entry = vm_map_first_entry(map);
1336
1337 while (entry != vm_map_to_entry(map)) {
1338 if (VME_OBJECT(entry) != VM_OBJECT_NULL) {
1339 if (entry->is_sub_map) {
1340 vm_map_t lmap = VME_SUBMAP(entry);
1341 lck_mtx_lock(&lmap->s_lock);
1342 vm_map_res_deallocate(lmap);
1343 lck_mtx_unlock(&lmap->s_lock);
1344 } else {
1345 vm_object_t object = VME_OBJECT(entry);
1346 vm_object_lock(object);
1347 /*
1348 * This call may take a long time,
1349 * since it could actively push
1350 * out pages (if we implement it
1351 * that way).
1352 */
1353 vm_object_res_deallocate(object);
1354 vm_object_unlock(object);
1355 }
1356 }
1357 entry = entry->vme_next;
1358 }
1359 assert(map->sw_state == MAP_SW_IN);
1360 map->sw_state = MAP_SW_OUT;
1361 }
1362
1363 #endif /* TASK_SWAPPER */
1364
1365 /*
1366 * vm_map_lookup_entry: [ internal use only ]
1367 *
1368 * Calls into the vm map store layer to find the map
1369 * entry containing (or immediately preceding) the
1370 * specified address in the given map; the entry is returned
1371 * in the "entry" parameter. The boolean
1372 * result indicates whether the address is
1373 * actually contained in the map.
1374 */
1375 boolean_t
1376 vm_map_lookup_entry(
1377 register vm_map_t map,
1378 register vm_map_offset_t address,
1379 vm_map_entry_t *entry) /* OUT */
1380 {
1381 return ( vm_map_store_lookup_entry( map, address, entry ));
1382 }
1383
1384 /*
1385 * Routine: vm_map_find_space
1386 * Purpose:
1387 * Allocate a range in the specified virtual address map,
1388 * returning the entry allocated for that range.
1389 * Used by kmem_alloc, etc.
1390 *
1391 * The map must be NOT be locked. It will be returned locked
1392 * on KERN_SUCCESS, unlocked on failure.
1393 *
1394 * If an entry is allocated, the object/offset fields
1395 * are initialized to zero.
1396 */
1397 kern_return_t
1398 vm_map_find_space(
1399 register vm_map_t map,
1400 vm_map_offset_t *address, /* OUT */
1401 vm_map_size_t size,
1402 vm_map_offset_t mask,
1403 int flags,
1404 vm_map_entry_t *o_entry) /* OUT */
1405 {
1406 vm_map_entry_t entry, new_entry;
1407 register vm_map_offset_t start;
1408 register vm_map_offset_t end;
1409 vm_map_entry_t hole_entry;
1410
1411 if (size == 0) {
1412 *address = 0;
1413 return KERN_INVALID_ARGUMENT;
1414 }
1415
1416 if (flags & VM_FLAGS_GUARD_AFTER) {
1417 /* account for the back guard page in the size */
1418 size += VM_MAP_PAGE_SIZE(map);
1419 }
1420
1421 new_entry = vm_map_entry_create(map, FALSE);
1422
1423 /*
1424 * Look for the first possible address; if there's already
1425 * something at this address, we have to start after it.
1426 */
1427
1428 vm_map_lock(map);
1429
1430 if( map->disable_vmentry_reuse == TRUE) {
1431 VM_MAP_HIGHEST_ENTRY(map, entry, start);
1432 } else {
1433 if (map->holelistenabled) {
1434 hole_entry = (vm_map_entry_t)map->holes_list;
1435
1436 if (hole_entry == NULL) {
1437 /*
1438 * No more space in the map?
1439 */
1440 vm_map_entry_dispose(map, new_entry);
1441 vm_map_unlock(map);
1442 return(KERN_NO_SPACE);
1443 }
1444
1445 entry = hole_entry;
1446 start = entry->vme_start;
1447 } else {
1448 assert(first_free_is_valid(map));
1449 if ((entry = map->first_free) == vm_map_to_entry(map))
1450 start = map->min_offset;
1451 else
1452 start = entry->vme_end;
1453 }
1454 }
1455
1456 /*
1457 * In any case, the "entry" always precedes
1458 * the proposed new region throughout the loop:
1459 */
1460
1461 while (TRUE) {
1462 register vm_map_entry_t next;
1463
1464 /*
1465 * Find the end of the proposed new region.
1466 * Be sure we didn't go beyond the end, or
1467 * wrap around the address.
1468 */
1469
1470 if (flags & VM_FLAGS_GUARD_BEFORE) {
1471 /* reserve space for the front guard page */
1472 start += VM_MAP_PAGE_SIZE(map);
1473 }
1474 end = ((start + mask) & ~mask);
1475
1476 if (end < start) {
1477 vm_map_entry_dispose(map, new_entry);
1478 vm_map_unlock(map);
1479 return(KERN_NO_SPACE);
1480 }
1481 start = end;
1482 end += size;
1483
1484 if ((end > map->max_offset) || (end < start)) {
1485 vm_map_entry_dispose(map, new_entry);
1486 vm_map_unlock(map);
1487 return(KERN_NO_SPACE);
1488 }
1489
1490 next = entry->vme_next;
1491
1492 if (map->holelistenabled) {
1493 if (entry->vme_end >= end)
1494 break;
1495 } else {
1496 /*
1497 * If there are no more entries, we must win.
1498 *
1499 * OR
1500 *
1501 * If there is another entry, it must be
1502 * after the end of the potential new region.
1503 */
1504
1505 if (next == vm_map_to_entry(map))
1506 break;
1507
1508 if (next->vme_start >= end)
1509 break;
1510 }
1511
1512 /*
1513 * Didn't fit -- move to the next entry.
1514 */
1515
1516 entry = next;
1517
1518 if (map->holelistenabled) {
1519 if (entry == (vm_map_entry_t) map->holes_list) {
1520 /*
1521 * Wrapped around
1522 */
1523 vm_map_entry_dispose(map, new_entry);
1524 vm_map_unlock(map);
1525 return(KERN_NO_SPACE);
1526 }
1527 start = entry->vme_start;
1528 } else {
1529 start = entry->vme_end;
1530 }
1531 }
1532
1533 if (map->holelistenabled) {
1534 if (vm_map_lookup_entry(map, entry->vme_start, &entry)) {
1535 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", entry, (unsigned long long)entry->vme_start);
1536 }
1537 }
1538
1539 /*
1540 * At this point,
1541 * "start" and "end" should define the endpoints of the
1542 * available new range, and
1543 * "entry" should refer to the region before the new
1544 * range, and
1545 *
1546 * the map should be locked.
1547 */
1548
1549 if (flags & VM_FLAGS_GUARD_BEFORE) {
1550 /* go back for the front guard page */
1551 start -= VM_MAP_PAGE_SIZE(map);
1552 }
1553 *address = start;
1554
1555 assert(start < end);
1556 new_entry->vme_start = start;
1557 new_entry->vme_end = end;
1558 assert(page_aligned(new_entry->vme_start));
1559 assert(page_aligned(new_entry->vme_end));
1560 assert(VM_MAP_PAGE_ALIGNED(new_entry->vme_start,
1561 VM_MAP_PAGE_MASK(map)));
1562 assert(VM_MAP_PAGE_ALIGNED(new_entry->vme_end,
1563 VM_MAP_PAGE_MASK(map)));
1564
1565 new_entry->is_shared = FALSE;
1566 new_entry->is_sub_map = FALSE;
1567 new_entry->use_pmap = TRUE;
1568 VME_OBJECT_SET(new_entry, VM_OBJECT_NULL);
1569 VME_OFFSET_SET(new_entry, (vm_object_offset_t) 0);
1570
1571 new_entry->needs_copy = FALSE;
1572
1573 new_entry->inheritance = VM_INHERIT_DEFAULT;
1574 new_entry->protection = VM_PROT_DEFAULT;
1575 new_entry->max_protection = VM_PROT_ALL;
1576 new_entry->behavior = VM_BEHAVIOR_DEFAULT;
1577 new_entry->wired_count = 0;
1578 new_entry->user_wired_count = 0;
1579
1580 new_entry->in_transition = FALSE;
1581 new_entry->needs_wakeup = FALSE;
1582 new_entry->no_cache = FALSE;
1583 new_entry->permanent = FALSE;
1584 new_entry->superpage_size = FALSE;
1585 if (VM_MAP_PAGE_SHIFT(map) != PAGE_SHIFT) {
1586 new_entry->map_aligned = TRUE;
1587 } else {
1588 new_entry->map_aligned = FALSE;
1589 }
1590
1591 new_entry->used_for_jit = FALSE;
1592 new_entry->zero_wired_pages = FALSE;
1593 new_entry->iokit_acct = FALSE;
1594 new_entry->vme_resilient_codesign = FALSE;
1595 new_entry->vme_resilient_media = FALSE;
1596
1597 int alias;
1598 VM_GET_FLAGS_ALIAS(flags, alias);
1599 VME_ALIAS_SET(new_entry, alias);
1600
1601 /*
1602 * Insert the new entry into the list
1603 */
1604
1605 vm_map_store_entry_link(map, entry, new_entry);
1606
1607 map->size += size;
1608
1609 /*
1610 * Update the lookup hint
1611 */
1612 SAVE_HINT_MAP_WRITE(map, new_entry);
1613
1614 *o_entry = new_entry;
1615 return(KERN_SUCCESS);
1616 }
1617
1618 int vm_map_pmap_enter_print = FALSE;
1619 int vm_map_pmap_enter_enable = FALSE;
1620
1621 /*
1622 * Routine: vm_map_pmap_enter [internal only]
1623 *
1624 * Description:
1625 * Force pages from the specified object to be entered into
1626 * the pmap at the specified address if they are present.
1627 * As soon as a page not found in the object the scan ends.
1628 *
1629 * Returns:
1630 * Nothing.
1631 *
1632 * In/out conditions:
1633 * The source map should not be locked on entry.
1634 */
1635 __unused static void
1636 vm_map_pmap_enter(
1637 vm_map_t map,
1638 register vm_map_offset_t addr,
1639 register vm_map_offset_t end_addr,
1640 register vm_object_t object,
1641 vm_object_offset_t offset,
1642 vm_prot_t protection)
1643 {
1644 int type_of_fault;
1645 kern_return_t kr;
1646
1647 if(map->pmap == 0)
1648 return;
1649
1650 while (addr < end_addr) {
1651 register vm_page_t m;
1652
1653
1654 /*
1655 * TODO:
1656 * From vm_map_enter(), we come into this function without the map
1657 * lock held or the object lock held.
1658 * We haven't taken a reference on the object either.
1659 * We should do a proper lookup on the map to make sure
1660 * that things are sane before we go locking objects that
1661 * could have been deallocated from under us.
1662 */
1663
1664 vm_object_lock(object);
1665
1666 m = vm_page_lookup(object, offset);
1667 /*
1668 * ENCRYPTED SWAP:
1669 * The user should never see encrypted data, so do not
1670 * enter an encrypted page in the page table.
1671 */
1672 if (m == VM_PAGE_NULL || m->busy || m->encrypted ||
1673 m->fictitious ||
1674 (m->unusual && ( m->error || m->restart || m->absent))) {
1675 vm_object_unlock(object);
1676 return;
1677 }
1678
1679 if (vm_map_pmap_enter_print) {
1680 printf("vm_map_pmap_enter:");
1681 printf("map: %p, addr: %llx, object: %p, offset: %llx\n",
1682 map, (unsigned long long)addr, object, (unsigned long long)offset);
1683 }
1684 type_of_fault = DBG_CACHE_HIT_FAULT;
1685 kr = vm_fault_enter(m, map->pmap, addr, protection, protection,
1686 VM_PAGE_WIRED(m), FALSE, FALSE, FALSE,
1687 0, /* XXX need user tag / alias? */
1688 0, /* alternate accounting? */
1689 NULL,
1690 &type_of_fault);
1691
1692 vm_object_unlock(object);
1693
1694 offset += PAGE_SIZE_64;
1695 addr += PAGE_SIZE;
1696 }
1697 }
1698
1699 boolean_t vm_map_pmap_is_empty(
1700 vm_map_t map,
1701 vm_map_offset_t start,
1702 vm_map_offset_t end);
1703 boolean_t vm_map_pmap_is_empty(
1704 vm_map_t map,
1705 vm_map_offset_t start,
1706 vm_map_offset_t end)
1707 {
1708 #ifdef MACHINE_PMAP_IS_EMPTY
1709 return pmap_is_empty(map->pmap, start, end);
1710 #else /* MACHINE_PMAP_IS_EMPTY */
1711 vm_map_offset_t offset;
1712 ppnum_t phys_page;
1713
1714 if (map->pmap == NULL) {
1715 return TRUE;
1716 }
1717
1718 for (offset = start;
1719 offset < end;
1720 offset += PAGE_SIZE) {
1721 phys_page = pmap_find_phys(map->pmap, offset);
1722 if (phys_page) {
1723 kprintf("vm_map_pmap_is_empty(%p,0x%llx,0x%llx): "
1724 "page %d at 0x%llx\n",
1725 map, (long long)start, (long long)end,
1726 phys_page, (long long)offset);
1727 return FALSE;
1728 }
1729 }
1730 return TRUE;
1731 #endif /* MACHINE_PMAP_IS_EMPTY */
1732 }
1733
1734 #define MAX_TRIES_TO_GET_RANDOM_ADDRESS 1000
1735 kern_return_t
1736 vm_map_random_address_for_size(
1737 vm_map_t map,
1738 vm_map_offset_t *address,
1739 vm_map_size_t size)
1740 {
1741 kern_return_t kr = KERN_SUCCESS;
1742 int tries = 0;
1743 vm_map_offset_t random_addr = 0;
1744 vm_map_offset_t hole_end;
1745
1746 vm_map_entry_t next_entry = VM_MAP_ENTRY_NULL;
1747 vm_map_entry_t prev_entry = VM_MAP_ENTRY_NULL;
1748 vm_map_size_t vm_hole_size = 0;
1749 vm_map_size_t addr_space_size;
1750
1751 addr_space_size = vm_map_max(map) - vm_map_min(map);
1752
1753 assert(page_aligned(size));
1754
1755 while (tries < MAX_TRIES_TO_GET_RANDOM_ADDRESS) {
1756 random_addr = ((vm_map_offset_t)random()) << PAGE_SHIFT;
1757 random_addr = vm_map_trunc_page(
1758 vm_map_min(map) +(random_addr % addr_space_size),
1759 VM_MAP_PAGE_MASK(map));
1760
1761 if (vm_map_lookup_entry(map, random_addr, &prev_entry) == FALSE) {
1762 if (prev_entry == vm_map_to_entry(map)) {
1763 next_entry = vm_map_first_entry(map);
1764 } else {
1765 next_entry = prev_entry->vme_next;
1766 }
1767 if (next_entry == vm_map_to_entry(map)) {
1768 hole_end = vm_map_max(map);
1769 } else {
1770 hole_end = next_entry->vme_start;
1771 }
1772 vm_hole_size = hole_end - random_addr;
1773 if (vm_hole_size >= size) {
1774 *address = random_addr;
1775 break;
1776 }
1777 }
1778 tries++;
1779 }
1780
1781 if (tries == MAX_TRIES_TO_GET_RANDOM_ADDRESS) {
1782 kr = KERN_NO_SPACE;
1783 }
1784 return kr;
1785 }
1786
1787 /*
1788 * Routine: vm_map_enter
1789 *
1790 * Description:
1791 * Allocate a range in the specified virtual address map.
1792 * The resulting range will refer to memory defined by
1793 * the given memory object and offset into that object.
1794 *
1795 * Arguments are as defined in the vm_map call.
1796 */
1797 int _map_enter_debug = 0;
1798 static unsigned int vm_map_enter_restore_successes = 0;
1799 static unsigned int vm_map_enter_restore_failures = 0;
1800 kern_return_t
1801 vm_map_enter(
1802 vm_map_t map,
1803 vm_map_offset_t *address, /* IN/OUT */
1804 vm_map_size_t size,
1805 vm_map_offset_t mask,
1806 int flags,
1807 vm_object_t object,
1808 vm_object_offset_t offset,
1809 boolean_t needs_copy,
1810 vm_prot_t cur_protection,
1811 vm_prot_t max_protection,
1812 vm_inherit_t inheritance)
1813 {
1814 vm_map_entry_t entry, new_entry;
1815 vm_map_offset_t start, tmp_start, tmp_offset;
1816 vm_map_offset_t end, tmp_end;
1817 vm_map_offset_t tmp2_start, tmp2_end;
1818 vm_map_offset_t step;
1819 kern_return_t result = KERN_SUCCESS;
1820 vm_map_t zap_old_map = VM_MAP_NULL;
1821 vm_map_t zap_new_map = VM_MAP_NULL;
1822 boolean_t map_locked = FALSE;
1823 boolean_t pmap_empty = TRUE;
1824 boolean_t new_mapping_established = FALSE;
1825 boolean_t keep_map_locked = ((flags & VM_FLAGS_KEEP_MAP_LOCKED) != 0);
1826 boolean_t anywhere = ((flags & VM_FLAGS_ANYWHERE) != 0);
1827 boolean_t purgable = ((flags & VM_FLAGS_PURGABLE) != 0);
1828 boolean_t overwrite = ((flags & VM_FLAGS_OVERWRITE) != 0);
1829 boolean_t no_cache = ((flags & VM_FLAGS_NO_CACHE) != 0);
1830 boolean_t is_submap = ((flags & VM_FLAGS_SUBMAP) != 0);
1831 boolean_t permanent = ((flags & VM_FLAGS_PERMANENT) != 0);
1832 boolean_t entry_for_jit = ((flags & VM_FLAGS_MAP_JIT) != 0);
1833 boolean_t iokit_acct = ((flags & VM_FLAGS_IOKIT_ACCT) != 0);
1834 boolean_t resilient_codesign = ((flags & VM_FLAGS_RESILIENT_CODESIGN) != 0);
1835 boolean_t resilient_media = ((flags & VM_FLAGS_RESILIENT_MEDIA) != 0);
1836 unsigned int superpage_size = ((flags & VM_FLAGS_SUPERPAGE_MASK) >> VM_FLAGS_SUPERPAGE_SHIFT);
1837 vm_tag_t alias, user_alias;
1838 vm_map_offset_t effective_min_offset, effective_max_offset;
1839 kern_return_t kr;
1840 boolean_t clear_map_aligned = FALSE;
1841 vm_map_entry_t hole_entry;
1842
1843 if (superpage_size) {
1844 switch (superpage_size) {
1845 /*
1846 * Note that the current implementation only supports
1847 * a single size for superpages, SUPERPAGE_SIZE, per
1848 * architecture. As soon as more sizes are supposed
1849 * to be supported, SUPERPAGE_SIZE has to be replaced
1850 * with a lookup of the size depending on superpage_size.
1851 */
1852 #ifdef __x86_64__
1853 case SUPERPAGE_SIZE_ANY:
1854 /* handle it like 2 MB and round up to page size */
1855 size = (size + 2*1024*1024 - 1) & ~(2*1024*1024 - 1);
1856 case SUPERPAGE_SIZE_2MB:
1857 break;
1858 #endif
1859 default:
1860 return KERN_INVALID_ARGUMENT;
1861 }
1862 mask = SUPERPAGE_SIZE-1;
1863 if (size & (SUPERPAGE_SIZE-1))
1864 return KERN_INVALID_ARGUMENT;
1865 inheritance = VM_INHERIT_NONE; /* fork() children won't inherit superpages */
1866 }
1867
1868
1869
1870 if (resilient_codesign || resilient_media) {
1871 if ((cur_protection & (VM_PROT_WRITE | VM_PROT_EXECUTE)) ||
1872 (max_protection & (VM_PROT_WRITE | VM_PROT_EXECUTE))) {
1873 return KERN_PROTECTION_FAILURE;
1874 }
1875 }
1876
1877 if (is_submap) {
1878 if (purgable) {
1879 /* submaps can not be purgeable */
1880 return KERN_INVALID_ARGUMENT;
1881 }
1882 if (object == VM_OBJECT_NULL) {
1883 /* submaps can not be created lazily */
1884 return KERN_INVALID_ARGUMENT;
1885 }
1886 }
1887 if (flags & VM_FLAGS_ALREADY) {
1888 /*
1889 * VM_FLAGS_ALREADY says that it's OK if the same mapping
1890 * is already present. For it to be meaningul, the requested
1891 * mapping has to be at a fixed address (!VM_FLAGS_ANYWHERE) and
1892 * we shouldn't try and remove what was mapped there first
1893 * (!VM_FLAGS_OVERWRITE).
1894 */
1895 if ((flags & VM_FLAGS_ANYWHERE) ||
1896 (flags & VM_FLAGS_OVERWRITE)) {
1897 return KERN_INVALID_ARGUMENT;
1898 }
1899 }
1900
1901 effective_min_offset = map->min_offset;
1902
1903 if (flags & VM_FLAGS_BEYOND_MAX) {
1904 /*
1905 * Allow an insertion beyond the map's max offset.
1906 */
1907 if (vm_map_is_64bit(map))
1908 effective_max_offset = 0xFFFFFFFFFFFFF000ULL;
1909 else
1910 effective_max_offset = 0x00000000FFFFF000ULL;
1911 } else {
1912 effective_max_offset = map->max_offset;
1913 }
1914
1915 if (size == 0 ||
1916 (offset & PAGE_MASK_64) != 0) {
1917 *address = 0;
1918 return KERN_INVALID_ARGUMENT;
1919 }
1920
1921 VM_GET_FLAGS_ALIAS(flags, alias);
1922 if (map->pmap == kernel_pmap) {
1923 user_alias = VM_KERN_MEMORY_NONE;
1924 } else {
1925 user_alias = alias;
1926 }
1927
1928 #define RETURN(value) { result = value; goto BailOut; }
1929
1930 assert(page_aligned(*address));
1931 assert(page_aligned(size));
1932
1933 if (!VM_MAP_PAGE_ALIGNED(size, VM_MAP_PAGE_MASK(map))) {
1934 /*
1935 * In most cases, the caller rounds the size up to the
1936 * map's page size.
1937 * If we get a size that is explicitly not map-aligned here,
1938 * we'll have to respect the caller's wish and mark the
1939 * mapping as "not map-aligned" to avoid tripping the
1940 * map alignment checks later.
1941 */
1942 clear_map_aligned = TRUE;
1943 }
1944 if (!anywhere &&
1945 !VM_MAP_PAGE_ALIGNED(*address, VM_MAP_PAGE_MASK(map))) {
1946 /*
1947 * We've been asked to map at a fixed address and that
1948 * address is not aligned to the map's specific alignment.
1949 * The caller should know what it's doing (i.e. most likely
1950 * mapping some fragmented copy map, transferring memory from
1951 * a VM map with a different alignment), so clear map_aligned
1952 * for this new VM map entry and proceed.
1953 */
1954 clear_map_aligned = TRUE;
1955 }
1956
1957 /*
1958 * Only zero-fill objects are allowed to be purgable.
1959 * LP64todo - limit purgable objects to 32-bits for now
1960 */
1961 if (purgable &&
1962 (offset != 0 ||
1963 (object != VM_OBJECT_NULL &&
1964 (object->vo_size != size ||
1965 object->purgable == VM_PURGABLE_DENY))
1966 || size > ANON_MAX_SIZE)) /* LP64todo: remove when dp capable */
1967 return KERN_INVALID_ARGUMENT;
1968
1969 if (!anywhere && overwrite) {
1970 /*
1971 * Create a temporary VM map to hold the old mappings in the
1972 * affected area while we create the new one.
1973 * This avoids releasing the VM map lock in
1974 * vm_map_entry_delete() and allows atomicity
1975 * when we want to replace some mappings with a new one.
1976 * It also allows us to restore the old VM mappings if the
1977 * new mapping fails.
1978 */
1979 zap_old_map = vm_map_create(PMAP_NULL,
1980 *address,
1981 *address + size,
1982 map->hdr.entries_pageable);
1983 vm_map_set_page_shift(zap_old_map, VM_MAP_PAGE_SHIFT(map));
1984 vm_map_disable_hole_optimization(zap_old_map);
1985 }
1986
1987 StartAgain: ;
1988
1989 start = *address;
1990
1991 if (anywhere) {
1992 vm_map_lock(map);
1993 map_locked = TRUE;
1994
1995 if (entry_for_jit) {
1996 if (map->jit_entry_exists) {
1997 result = KERN_INVALID_ARGUMENT;
1998 goto BailOut;
1999 }
2000 /*
2001 * Get a random start address.
2002 */
2003 result = vm_map_random_address_for_size(map, address, size);
2004 if (result != KERN_SUCCESS) {
2005 goto BailOut;
2006 }
2007 start = *address;
2008 }
2009
2010
2011 /*
2012 * Calculate the first possible address.
2013 */
2014
2015 if (start < effective_min_offset)
2016 start = effective_min_offset;
2017 if (start > effective_max_offset)
2018 RETURN(KERN_NO_SPACE);
2019
2020 /*
2021 * Look for the first possible address;
2022 * if there's already something at this
2023 * address, we have to start after it.
2024 */
2025
2026 if( map->disable_vmentry_reuse == TRUE) {
2027 VM_MAP_HIGHEST_ENTRY(map, entry, start);
2028 } else {
2029
2030 if (map->holelistenabled) {
2031 hole_entry = (vm_map_entry_t)map->holes_list;
2032
2033 if (hole_entry == NULL) {
2034 /*
2035 * No more space in the map?
2036 */
2037 result = KERN_NO_SPACE;
2038 goto BailOut;
2039 } else {
2040
2041 boolean_t found_hole = FALSE;
2042
2043 do {
2044 if (hole_entry->vme_start >= start) {
2045 start = hole_entry->vme_start;
2046 found_hole = TRUE;
2047 break;
2048 }
2049
2050 if (hole_entry->vme_end > start) {
2051 found_hole = TRUE;
2052 break;
2053 }
2054 hole_entry = hole_entry->vme_next;
2055
2056 } while (hole_entry != (vm_map_entry_t) map->holes_list);
2057
2058 if (found_hole == FALSE) {
2059 result = KERN_NO_SPACE;
2060 goto BailOut;
2061 }
2062
2063 entry = hole_entry;
2064
2065 if (start == 0)
2066 start += PAGE_SIZE_64;
2067 }
2068 } else {
2069 assert(first_free_is_valid(map));
2070
2071 entry = map->first_free;
2072
2073 if (entry == vm_map_to_entry(map)) {
2074 entry = NULL;
2075 } else {
2076 if (entry->vme_next == vm_map_to_entry(map)){
2077 /*
2078 * Hole at the end of the map.
2079 */
2080 entry = NULL;
2081 } else {
2082 if (start < (entry->vme_next)->vme_start ) {
2083 start = entry->vme_end;
2084 start = vm_map_round_page(start,
2085 VM_MAP_PAGE_MASK(map));
2086 } else {
2087 /*
2088 * Need to do a lookup.
2089 */
2090 entry = NULL;
2091 }
2092 }
2093 }
2094
2095 if (entry == NULL) {
2096 vm_map_entry_t tmp_entry;
2097 if (vm_map_lookup_entry(map, start, &tmp_entry)) {
2098 assert(!entry_for_jit);
2099 start = tmp_entry->vme_end;
2100 start = vm_map_round_page(start,
2101 VM_MAP_PAGE_MASK(map));
2102 }
2103 entry = tmp_entry;
2104 }
2105 }
2106 }
2107
2108 /*
2109 * In any case, the "entry" always precedes
2110 * the proposed new region throughout the
2111 * loop:
2112 */
2113
2114 while (TRUE) {
2115 register vm_map_entry_t next;
2116
2117 /*
2118 * Find the end of the proposed new region.
2119 * Be sure we didn't go beyond the end, or
2120 * wrap around the address.
2121 */
2122
2123 end = ((start + mask) & ~mask);
2124 end = vm_map_round_page(end,
2125 VM_MAP_PAGE_MASK(map));
2126 if (end < start)
2127 RETURN(KERN_NO_SPACE);
2128 start = end;
2129 assert(VM_MAP_PAGE_ALIGNED(start,
2130 VM_MAP_PAGE_MASK(map)));
2131 end += size;
2132
2133 if ((end > effective_max_offset) || (end < start)) {
2134 if (map->wait_for_space) {
2135 assert(!keep_map_locked);
2136 if (size <= (effective_max_offset -
2137 effective_min_offset)) {
2138 assert_wait((event_t)map,
2139 THREAD_ABORTSAFE);
2140 vm_map_unlock(map);
2141 map_locked = FALSE;
2142 thread_block(THREAD_CONTINUE_NULL);
2143 goto StartAgain;
2144 }
2145 }
2146 RETURN(KERN_NO_SPACE);
2147 }
2148
2149 next = entry->vme_next;
2150
2151 if (map->holelistenabled) {
2152 if (entry->vme_end >= end)
2153 break;
2154 } else {
2155 /*
2156 * If there are no more entries, we must win.
2157 *
2158 * OR
2159 *
2160 * If there is another entry, it must be
2161 * after the end of the potential new region.
2162 */
2163
2164 if (next == vm_map_to_entry(map))
2165 break;
2166
2167 if (next->vme_start >= end)
2168 break;
2169 }
2170
2171 /*
2172 * Didn't fit -- move to the next entry.
2173 */
2174
2175 entry = next;
2176
2177 if (map->holelistenabled) {
2178 if (entry == (vm_map_entry_t) map->holes_list) {
2179 /*
2180 * Wrapped around
2181 */
2182 result = KERN_NO_SPACE;
2183 goto BailOut;
2184 }
2185 start = entry->vme_start;
2186 } else {
2187 start = entry->vme_end;
2188 }
2189
2190 start = vm_map_round_page(start,
2191 VM_MAP_PAGE_MASK(map));
2192 }
2193
2194 if (map->holelistenabled) {
2195 if (vm_map_lookup_entry(map, entry->vme_start, &entry)) {
2196 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", entry, (unsigned long long)entry->vme_start);
2197 }
2198 }
2199
2200 *address = start;
2201 assert(VM_MAP_PAGE_ALIGNED(*address,
2202 VM_MAP_PAGE_MASK(map)));
2203 } else {
2204 /*
2205 * Verify that:
2206 * the address doesn't itself violate
2207 * the mask requirement.
2208 */
2209
2210 vm_map_lock(map);
2211 map_locked = TRUE;
2212 if ((start & mask) != 0)
2213 RETURN(KERN_NO_SPACE);
2214
2215 /*
2216 * ... the address is within bounds
2217 */
2218
2219 end = start + size;
2220
2221 if ((start < effective_min_offset) ||
2222 (end > effective_max_offset) ||
2223 (start >= end)) {
2224 RETURN(KERN_INVALID_ADDRESS);
2225 }
2226
2227 if (overwrite && zap_old_map != VM_MAP_NULL) {
2228 /*
2229 * Fixed mapping and "overwrite" flag: attempt to
2230 * remove all existing mappings in the specified
2231 * address range, saving them in our "zap_old_map".
2232 */
2233 (void) vm_map_delete(map, start, end,
2234 (VM_MAP_REMOVE_SAVE_ENTRIES |
2235 VM_MAP_REMOVE_NO_MAP_ALIGN),
2236 zap_old_map);
2237 }
2238
2239 /*
2240 * ... the starting address isn't allocated
2241 */
2242
2243 if (vm_map_lookup_entry(map, start, &entry)) {
2244 if (! (flags & VM_FLAGS_ALREADY)) {
2245 RETURN(KERN_NO_SPACE);
2246 }
2247 /*
2248 * Check if what's already there is what we want.
2249 */
2250 tmp_start = start;
2251 tmp_offset = offset;
2252 if (entry->vme_start < start) {
2253 tmp_start -= start - entry->vme_start;
2254 tmp_offset -= start - entry->vme_start;
2255
2256 }
2257 for (; entry->vme_start < end;
2258 entry = entry->vme_next) {
2259 /*
2260 * Check if the mapping's attributes
2261 * match the existing map entry.
2262 */
2263 if (entry == vm_map_to_entry(map) ||
2264 entry->vme_start != tmp_start ||
2265 entry->is_sub_map != is_submap ||
2266 VME_OFFSET(entry) != tmp_offset ||
2267 entry->needs_copy != needs_copy ||
2268 entry->protection != cur_protection ||
2269 entry->max_protection != max_protection ||
2270 entry->inheritance != inheritance ||
2271 entry->iokit_acct != iokit_acct ||
2272 VME_ALIAS(entry) != alias) {
2273 /* not the same mapping ! */
2274 RETURN(KERN_NO_SPACE);
2275 }
2276 /*
2277 * Check if the same object is being mapped.
2278 */
2279 if (is_submap) {
2280 if (VME_SUBMAP(entry) !=
2281 (vm_map_t) object) {
2282 /* not the same submap */
2283 RETURN(KERN_NO_SPACE);
2284 }
2285 } else {
2286 if (VME_OBJECT(entry) != object) {
2287 /* not the same VM object... */
2288 vm_object_t obj2;
2289
2290 obj2 = VME_OBJECT(entry);
2291 if ((obj2 == VM_OBJECT_NULL ||
2292 obj2->internal) &&
2293 (object == VM_OBJECT_NULL ||
2294 object->internal)) {
2295 /*
2296 * ... but both are
2297 * anonymous memory,
2298 * so equivalent.
2299 */
2300 } else {
2301 RETURN(KERN_NO_SPACE);
2302 }
2303 }
2304 }
2305
2306 tmp_offset += entry->vme_end - entry->vme_start;
2307 tmp_start += entry->vme_end - entry->vme_start;
2308 if (entry->vme_end >= end) {
2309 /* reached the end of our mapping */
2310 break;
2311 }
2312 }
2313 /* it all matches: let's use what's already there ! */
2314 RETURN(KERN_MEMORY_PRESENT);
2315 }
2316
2317 /*
2318 * ... the next region doesn't overlap the
2319 * end point.
2320 */
2321
2322 if ((entry->vme_next != vm_map_to_entry(map)) &&
2323 (entry->vme_next->vme_start < end))
2324 RETURN(KERN_NO_SPACE);
2325 }
2326
2327 /*
2328 * At this point,
2329 * "start" and "end" should define the endpoints of the
2330 * available new range, and
2331 * "entry" should refer to the region before the new
2332 * range, and
2333 *
2334 * the map should be locked.
2335 */
2336
2337 /*
2338 * See whether we can avoid creating a new entry (and object) by
2339 * extending one of our neighbors. [So far, we only attempt to
2340 * extend from below.] Note that we can never extend/join
2341 * purgable objects because they need to remain distinct
2342 * entities in order to implement their "volatile object"
2343 * semantics.
2344 */
2345
2346 if (purgable || entry_for_jit) {
2347 if (object == VM_OBJECT_NULL) {
2348
2349 object = vm_object_allocate(size);
2350 object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
2351 object->true_share = TRUE;
2352 if (purgable) {
2353 task_t owner;
2354 object->purgable = VM_PURGABLE_NONVOLATILE;
2355 if (map->pmap == kernel_pmap) {
2356 /*
2357 * Purgeable mappings made in a kernel
2358 * map are "owned" by the kernel itself
2359 * rather than the current user task
2360 * because they're likely to be used by
2361 * more than this user task (see
2362 * execargs_purgeable_allocate(), for
2363 * example).
2364 */
2365 owner = kernel_task;
2366 } else {
2367 owner = current_task();
2368 }
2369 assert(object->vo_purgeable_owner == NULL);
2370 assert(object->resident_page_count == 0);
2371 assert(object->wired_page_count == 0);
2372 vm_object_lock(object);
2373 vm_purgeable_nonvolatile_enqueue(object, owner);
2374 vm_object_unlock(object);
2375 }
2376 offset = (vm_object_offset_t)0;
2377 }
2378 } else if ((is_submap == FALSE) &&
2379 (object == VM_OBJECT_NULL) &&
2380 (entry != vm_map_to_entry(map)) &&
2381 (entry->vme_end == start) &&
2382 (!entry->is_shared) &&
2383 (!entry->is_sub_map) &&
2384 (!entry->in_transition) &&
2385 (!entry->needs_wakeup) &&
2386 (entry->behavior == VM_BEHAVIOR_DEFAULT) &&
2387 (entry->protection == cur_protection) &&
2388 (entry->max_protection == max_protection) &&
2389 (entry->inheritance == inheritance) &&
2390 ((user_alias == VM_MEMORY_REALLOC) ||
2391 (VME_ALIAS(entry) == alias)) &&
2392 (entry->no_cache == no_cache) &&
2393 (entry->permanent == permanent) &&
2394 (!entry->superpage_size && !superpage_size) &&
2395 /*
2396 * No coalescing if not map-aligned, to avoid propagating
2397 * that condition any further than needed:
2398 */
2399 (!entry->map_aligned || !clear_map_aligned) &&
2400 (!entry->zero_wired_pages) &&
2401 (!entry->used_for_jit && !entry_for_jit) &&
2402 (entry->iokit_acct == iokit_acct) &&
2403 (!entry->vme_resilient_codesign) &&
2404 (!entry->vme_resilient_media) &&
2405
2406 ((entry->vme_end - entry->vme_start) + size <=
2407 (user_alias == VM_MEMORY_REALLOC ?
2408 ANON_CHUNK_SIZE :
2409 NO_COALESCE_LIMIT)) &&
2410
2411 (entry->wired_count == 0)) { /* implies user_wired_count == 0 */
2412 if (vm_object_coalesce(VME_OBJECT(entry),
2413 VM_OBJECT_NULL,
2414 VME_OFFSET(entry),
2415 (vm_object_offset_t) 0,
2416 (vm_map_size_t)(entry->vme_end - entry->vme_start),
2417 (vm_map_size_t)(end - entry->vme_end))) {
2418
2419 /*
2420 * Coalesced the two objects - can extend
2421 * the previous map entry to include the
2422 * new range.
2423 */
2424 map->size += (end - entry->vme_end);
2425 assert(entry->vme_start < end);
2426 assert(VM_MAP_PAGE_ALIGNED(end,
2427 VM_MAP_PAGE_MASK(map)));
2428 if (__improbable(vm_debug_events))
2429 DTRACE_VM5(map_entry_extend, vm_map_t, map, vm_map_entry_t, entry, vm_address_t, entry->vme_start, vm_address_t, entry->vme_end, vm_address_t, end);
2430 entry->vme_end = end;
2431 if (map->holelistenabled) {
2432 vm_map_store_update_first_free(map, entry, TRUE);
2433 } else {
2434 vm_map_store_update_first_free(map, map->first_free, TRUE);
2435 }
2436 new_mapping_established = TRUE;
2437 RETURN(KERN_SUCCESS);
2438 }
2439 }
2440
2441 step = superpage_size ? SUPERPAGE_SIZE : (end - start);
2442 new_entry = NULL;
2443
2444 for (tmp2_start = start; tmp2_start<end; tmp2_start += step) {
2445 tmp2_end = tmp2_start + step;
2446 /*
2447 * Create a new entry
2448 * LP64todo - for now, we can only allocate 4GB internal objects
2449 * because the default pager can't page bigger ones. Remove this
2450 * when it can.
2451 *
2452 * XXX FBDP
2453 * The reserved "page zero" in each process's address space can
2454 * be arbitrarily large. Splitting it into separate 4GB objects and
2455 * therefore different VM map entries serves no purpose and just
2456 * slows down operations on the VM map, so let's not split the
2457 * allocation into 4GB chunks if the max protection is NONE. That
2458 * memory should never be accessible, so it will never get to the
2459 * default pager.
2460 */
2461 tmp_start = tmp2_start;
2462 if (object == VM_OBJECT_NULL &&
2463 size > (vm_map_size_t)ANON_CHUNK_SIZE &&
2464 max_protection != VM_PROT_NONE &&
2465 superpage_size == 0)
2466 tmp_end = tmp_start + (vm_map_size_t)ANON_CHUNK_SIZE;
2467 else
2468 tmp_end = tmp2_end;
2469 do {
2470 new_entry = vm_map_entry_insert(map, entry, tmp_start, tmp_end,
2471 object, offset, needs_copy,
2472 FALSE, FALSE,
2473 cur_protection, max_protection,
2474 VM_BEHAVIOR_DEFAULT,
2475 (entry_for_jit)? VM_INHERIT_NONE: inheritance,
2476 0, no_cache,
2477 permanent,
2478 superpage_size,
2479 clear_map_aligned,
2480 is_submap);
2481
2482 assert((object != kernel_object) || (VM_KERN_MEMORY_NONE != alias));
2483 VME_ALIAS_SET(new_entry, alias);
2484
2485 if (entry_for_jit){
2486 if (!(map->jit_entry_exists)){
2487 new_entry->used_for_jit = TRUE;
2488 map->jit_entry_exists = TRUE;
2489 }
2490 }
2491
2492 if (resilient_codesign &&
2493 ! ((cur_protection | max_protection) &
2494 (VM_PROT_WRITE | VM_PROT_EXECUTE))) {
2495 new_entry->vme_resilient_codesign = TRUE;
2496 }
2497
2498 if (resilient_media &&
2499 ! ((cur_protection | max_protection) &
2500 (VM_PROT_WRITE | VM_PROT_EXECUTE))) {
2501 new_entry->vme_resilient_media = TRUE;
2502 }
2503
2504 assert(!new_entry->iokit_acct);
2505 if (!is_submap &&
2506 object != VM_OBJECT_NULL &&
2507 object->purgable != VM_PURGABLE_DENY) {
2508 assert(new_entry->use_pmap);
2509 assert(!new_entry->iokit_acct);
2510 /*
2511 * Turn off pmap accounting since
2512 * purgeable objects have their
2513 * own ledgers.
2514 */
2515 new_entry->use_pmap = FALSE;
2516 } else if (!is_submap &&
2517 iokit_acct) {
2518 /* alternate accounting */
2519 assert(!new_entry->iokit_acct);
2520 assert(new_entry->use_pmap);
2521 new_entry->iokit_acct = TRUE;
2522 new_entry->use_pmap = FALSE;
2523 vm_map_iokit_mapped_region(
2524 map,
2525 (new_entry->vme_end -
2526 new_entry->vme_start));
2527 } else if (!is_submap) {
2528 assert(!new_entry->iokit_acct);
2529 assert(new_entry->use_pmap);
2530 }
2531
2532 if (is_submap) {
2533 vm_map_t submap;
2534 boolean_t submap_is_64bit;
2535 boolean_t use_pmap;
2536
2537 assert(new_entry->is_sub_map);
2538 assert(!new_entry->use_pmap);
2539 assert(!new_entry->iokit_acct);
2540 submap = (vm_map_t) object;
2541 submap_is_64bit = vm_map_is_64bit(submap);
2542 use_pmap = (user_alias == VM_MEMORY_SHARED_PMAP);
2543 #ifndef NO_NESTED_PMAP
2544 if (use_pmap && submap->pmap == NULL) {
2545 ledger_t ledger = map->pmap->ledger;
2546 /* we need a sub pmap to nest... */
2547 submap->pmap = pmap_create(ledger, 0,
2548 submap_is_64bit);
2549 if (submap->pmap == NULL) {
2550 /* let's proceed without nesting... */
2551 }
2552 }
2553 if (use_pmap && submap->pmap != NULL) {
2554 kr = pmap_nest(map->pmap,
2555 submap->pmap,
2556 tmp_start,
2557 tmp_start,
2558 tmp_end - tmp_start);
2559 if (kr != KERN_SUCCESS) {
2560 printf("vm_map_enter: "
2561 "pmap_nest(0x%llx,0x%llx) "
2562 "error 0x%x\n",
2563 (long long)tmp_start,
2564 (long long)tmp_end,
2565 kr);
2566 } else {
2567 /* we're now nested ! */
2568 new_entry->use_pmap = TRUE;
2569 pmap_empty = FALSE;
2570 }
2571 }
2572 #endif /* NO_NESTED_PMAP */
2573 }
2574 entry = new_entry;
2575
2576 if (superpage_size) {
2577 vm_page_t pages, m;
2578 vm_object_t sp_object;
2579
2580 VME_OFFSET_SET(entry, 0);
2581
2582 /* allocate one superpage */
2583 kr = cpm_allocate(SUPERPAGE_SIZE, &pages, 0, SUPERPAGE_NBASEPAGES-1, TRUE, 0);
2584 if (kr != KERN_SUCCESS) {
2585 /* deallocate whole range... */
2586 new_mapping_established = TRUE;
2587 /* ... but only up to "tmp_end" */
2588 size -= end - tmp_end;
2589 RETURN(kr);
2590 }
2591
2592 /* create one vm_object per superpage */
2593 sp_object = vm_object_allocate((vm_map_size_t)(entry->vme_end - entry->vme_start));
2594 sp_object->phys_contiguous = TRUE;
2595 sp_object->vo_shadow_offset = (vm_object_offset_t)pages->phys_page*PAGE_SIZE;
2596 VME_OBJECT_SET(entry, sp_object);
2597 assert(entry->use_pmap);
2598
2599 /* enter the base pages into the object */
2600 vm_object_lock(sp_object);
2601 for (offset = 0; offset < SUPERPAGE_SIZE; offset += PAGE_SIZE) {
2602 m = pages;
2603 pmap_zero_page(m->phys_page);
2604 pages = NEXT_PAGE(m);
2605 *(NEXT_PAGE_PTR(m)) = VM_PAGE_NULL;
2606 vm_page_insert_wired(m, sp_object, offset, VM_KERN_MEMORY_OSFMK);
2607 }
2608 vm_object_unlock(sp_object);
2609 }
2610 } while (tmp_end != tmp2_end &&
2611 (tmp_start = tmp_end) &&
2612 (tmp_end = (tmp2_end - tmp_end > (vm_map_size_t)ANON_CHUNK_SIZE) ?
2613 tmp_end + (vm_map_size_t)ANON_CHUNK_SIZE : tmp2_end));
2614 }
2615
2616 new_mapping_established = TRUE;
2617
2618 BailOut:
2619 assert(map_locked == TRUE);
2620
2621 if (result == KERN_SUCCESS) {
2622 vm_prot_t pager_prot;
2623 memory_object_t pager;
2624
2625 #if DEBUG
2626 if (pmap_empty &&
2627 !(flags & VM_FLAGS_NO_PMAP_CHECK)) {
2628 assert(vm_map_pmap_is_empty(map,
2629 *address,
2630 *address+size));
2631 }
2632 #endif /* DEBUG */
2633
2634 /*
2635 * For "named" VM objects, let the pager know that the
2636 * memory object is being mapped. Some pagers need to keep
2637 * track of this, to know when they can reclaim the memory
2638 * object, for example.
2639 * VM calls memory_object_map() for each mapping (specifying
2640 * the protection of each mapping) and calls
2641 * memory_object_last_unmap() when all the mappings are gone.
2642 */
2643 pager_prot = max_protection;
2644 if (needs_copy) {
2645 /*
2646 * Copy-On-Write mapping: won't modify
2647 * the memory object.
2648 */
2649 pager_prot &= ~VM_PROT_WRITE;
2650 }
2651 if (!is_submap &&
2652 object != VM_OBJECT_NULL &&
2653 object->named &&
2654 object->pager != MEMORY_OBJECT_NULL) {
2655 vm_object_lock(object);
2656 pager = object->pager;
2657 if (object->named &&
2658 pager != MEMORY_OBJECT_NULL) {
2659 assert(object->pager_ready);
2660 vm_object_mapping_wait(object, THREAD_UNINT);
2661 vm_object_mapping_begin(object);
2662 vm_object_unlock(object);
2663
2664 kr = memory_object_map(pager, pager_prot);
2665 assert(kr == KERN_SUCCESS);
2666
2667 vm_object_lock(object);
2668 vm_object_mapping_end(object);
2669 }
2670 vm_object_unlock(object);
2671 }
2672 }
2673
2674 assert(map_locked == TRUE);
2675
2676 if (!keep_map_locked) {
2677 vm_map_unlock(map);
2678 map_locked = FALSE;
2679 }
2680
2681 /*
2682 * We can't hold the map lock if we enter this block.
2683 */
2684
2685 if (result == KERN_SUCCESS) {
2686
2687 /* Wire down the new entry if the user
2688 * requested all new map entries be wired.
2689 */
2690 if ((map->wiring_required)||(superpage_size)) {
2691 assert(!keep_map_locked);
2692 pmap_empty = FALSE; /* pmap won't be empty */
2693 kr = vm_map_wire(map, start, end,
2694 new_entry->protection | VM_PROT_MEMORY_TAG_MAKE(VM_KERN_MEMORY_MLOCK),
2695 TRUE);
2696 result = kr;
2697 }
2698
2699 }
2700
2701 if (result != KERN_SUCCESS) {
2702 if (new_mapping_established) {
2703 /*
2704 * We have to get rid of the new mappings since we
2705 * won't make them available to the user.
2706 * Try and do that atomically, to minimize the risk
2707 * that someone else create new mappings that range.
2708 */
2709 zap_new_map = vm_map_create(PMAP_NULL,
2710 *address,
2711 *address + size,
2712 map->hdr.entries_pageable);
2713 vm_map_set_page_shift(zap_new_map,
2714 VM_MAP_PAGE_SHIFT(map));
2715 vm_map_disable_hole_optimization(zap_new_map);
2716
2717 if (!map_locked) {
2718 vm_map_lock(map);
2719 map_locked = TRUE;
2720 }
2721 (void) vm_map_delete(map, *address, *address+size,
2722 (VM_MAP_REMOVE_SAVE_ENTRIES |
2723 VM_MAP_REMOVE_NO_MAP_ALIGN),
2724 zap_new_map);
2725 }
2726 if (zap_old_map != VM_MAP_NULL &&
2727 zap_old_map->hdr.nentries != 0) {
2728 vm_map_entry_t entry1, entry2;
2729
2730 /*
2731 * The new mapping failed. Attempt to restore
2732 * the old mappings, saved in the "zap_old_map".
2733 */
2734 if (!map_locked) {
2735 vm_map_lock(map);
2736 map_locked = TRUE;
2737 }
2738
2739 /* first check if the coast is still clear */
2740 start = vm_map_first_entry(zap_old_map)->vme_start;
2741 end = vm_map_last_entry(zap_old_map)->vme_end;
2742 if (vm_map_lookup_entry(map, start, &entry1) ||
2743 vm_map_lookup_entry(map, end, &entry2) ||
2744 entry1 != entry2) {
2745 /*
2746 * Part of that range has already been
2747 * re-mapped: we can't restore the old
2748 * mappings...
2749 */
2750 vm_map_enter_restore_failures++;
2751 } else {
2752 /*
2753 * Transfer the saved map entries from
2754 * "zap_old_map" to the original "map",
2755 * inserting them all after "entry1".
2756 */
2757 for (entry2 = vm_map_first_entry(zap_old_map);
2758 entry2 != vm_map_to_entry(zap_old_map);
2759 entry2 = vm_map_first_entry(zap_old_map)) {
2760 vm_map_size_t entry_size;
2761
2762 entry_size = (entry2->vme_end -
2763 entry2->vme_start);
2764 vm_map_store_entry_unlink(zap_old_map,
2765 entry2);
2766 zap_old_map->size -= entry_size;
2767 vm_map_store_entry_link(map, entry1, entry2);
2768 map->size += entry_size;
2769 entry1 = entry2;
2770 }
2771 if (map->wiring_required) {
2772 /*
2773 * XXX TODO: we should rewire the
2774 * old pages here...
2775 */
2776 }
2777 vm_map_enter_restore_successes++;
2778 }
2779 }
2780 }
2781
2782 /*
2783 * The caller is responsible for releasing the lock if it requested to
2784 * keep the map locked.
2785 */
2786 if (map_locked && !keep_map_locked) {
2787 vm_map_unlock(map);
2788 }
2789
2790 /*
2791 * Get rid of the "zap_maps" and all the map entries that
2792 * they may still contain.
2793 */
2794 if (zap_old_map != VM_MAP_NULL) {
2795 vm_map_destroy(zap_old_map, VM_MAP_REMOVE_NO_PMAP_CLEANUP);
2796 zap_old_map = VM_MAP_NULL;
2797 }
2798 if (zap_new_map != VM_MAP_NULL) {
2799 vm_map_destroy(zap_new_map, VM_MAP_REMOVE_NO_PMAP_CLEANUP);
2800 zap_new_map = VM_MAP_NULL;
2801 }
2802
2803 return result;
2804
2805 #undef RETURN
2806 }
2807
2808
2809 /*
2810 * Counters for the prefault optimization.
2811 */
2812 int64_t vm_prefault_nb_pages = 0;
2813 int64_t vm_prefault_nb_bailout = 0;
2814
2815 static kern_return_t
2816 vm_map_enter_mem_object_helper(
2817 vm_map_t target_map,
2818 vm_map_offset_t *address,
2819 vm_map_size_t initial_size,
2820 vm_map_offset_t mask,
2821 int flags,
2822 ipc_port_t port,
2823 vm_object_offset_t offset,
2824 boolean_t copy,
2825 vm_prot_t cur_protection,
2826 vm_prot_t max_protection,
2827 vm_inherit_t inheritance,
2828 upl_page_list_ptr_t page_list,
2829 unsigned int page_list_count)
2830 {
2831 vm_map_address_t map_addr;
2832 vm_map_size_t map_size;
2833 vm_object_t object;
2834 vm_object_size_t size;
2835 kern_return_t result;
2836 boolean_t mask_cur_protection, mask_max_protection;
2837 boolean_t try_prefault = (page_list_count != 0);
2838 vm_map_offset_t offset_in_mapping = 0;
2839
2840 mask_cur_protection = cur_protection & VM_PROT_IS_MASK;
2841 mask_max_protection = max_protection & VM_PROT_IS_MASK;
2842 cur_protection &= ~VM_PROT_IS_MASK;
2843 max_protection &= ~VM_PROT_IS_MASK;
2844
2845 /*
2846 * Check arguments for validity
2847 */
2848 if ((target_map == VM_MAP_NULL) ||
2849 (cur_protection & ~VM_PROT_ALL) ||
2850 (max_protection & ~VM_PROT_ALL) ||
2851 (inheritance > VM_INHERIT_LAST_VALID) ||
2852 (try_prefault && (copy || !page_list)) ||
2853 initial_size == 0) {
2854 return KERN_INVALID_ARGUMENT;
2855 }
2856
2857 {
2858 map_addr = vm_map_trunc_page(*address,
2859 VM_MAP_PAGE_MASK(target_map));
2860 map_size = vm_map_round_page(initial_size,
2861 VM_MAP_PAGE_MASK(target_map));
2862 }
2863 size = vm_object_round_page(initial_size);
2864
2865 /*
2866 * Find the vm object (if any) corresponding to this port.
2867 */
2868 if (!IP_VALID(port)) {
2869 object = VM_OBJECT_NULL;
2870 offset = 0;
2871 copy = FALSE;
2872 } else if (ip_kotype(port) == IKOT_NAMED_ENTRY) {
2873 vm_named_entry_t named_entry;
2874
2875 named_entry = (vm_named_entry_t) port->ip_kobject;
2876
2877 if (flags & (VM_FLAGS_RETURN_DATA_ADDR |
2878 VM_FLAGS_RETURN_4K_DATA_ADDR)) {
2879 offset += named_entry->data_offset;
2880 }
2881
2882 /* a few checks to make sure user is obeying rules */
2883 if (size == 0) {
2884 if (offset >= named_entry->size)
2885 return KERN_INVALID_RIGHT;
2886 size = named_entry->size - offset;
2887 }
2888 if (mask_max_protection) {
2889 max_protection &= named_entry->protection;
2890 }
2891 if (mask_cur_protection) {
2892 cur_protection &= named_entry->protection;
2893 }
2894 if ((named_entry->protection & max_protection) !=
2895 max_protection)
2896 return KERN_INVALID_RIGHT;
2897 if ((named_entry->protection & cur_protection) !=
2898 cur_protection)
2899 return KERN_INVALID_RIGHT;
2900 if (offset + size < offset) {
2901 /* overflow */
2902 return KERN_INVALID_ARGUMENT;
2903 }
2904 if (named_entry->size < (offset + initial_size)) {
2905 return KERN_INVALID_ARGUMENT;
2906 }
2907
2908 if (named_entry->is_copy) {
2909 /* for a vm_map_copy, we can only map it whole */
2910 if ((size != named_entry->size) &&
2911 (vm_map_round_page(size,
2912 VM_MAP_PAGE_MASK(target_map)) ==
2913 named_entry->size)) {
2914 /* XXX FBDP use the rounded size... */
2915 size = vm_map_round_page(
2916 size,
2917 VM_MAP_PAGE_MASK(target_map));
2918 }
2919
2920 if (!(flags & VM_FLAGS_ANYWHERE) &&
2921 (offset != 0 ||
2922 size != named_entry->size)) {
2923 /*
2924 * XXX for a mapping at a "fixed" address,
2925 * we can't trim after mapping the whole
2926 * memory entry, so reject a request for a
2927 * partial mapping.
2928 */
2929 return KERN_INVALID_ARGUMENT;
2930 }
2931 }
2932
2933 /* the callers parameter offset is defined to be the */
2934 /* offset from beginning of named entry offset in object */
2935 offset = offset + named_entry->offset;
2936
2937 if (! VM_MAP_PAGE_ALIGNED(size,
2938 VM_MAP_PAGE_MASK(target_map))) {
2939 /*
2940 * Let's not map more than requested;
2941 * vm_map_enter() will handle this "not map-aligned"
2942 * case.
2943 */
2944 map_size = size;
2945 }
2946
2947 named_entry_lock(named_entry);
2948 if (named_entry->is_sub_map) {
2949 vm_map_t submap;
2950
2951 if (flags & (VM_FLAGS_RETURN_DATA_ADDR |
2952 VM_FLAGS_RETURN_4K_DATA_ADDR)) {
2953 panic("VM_FLAGS_RETURN_DATA_ADDR not expected for submap.");
2954 }
2955
2956 submap = named_entry->backing.map;
2957 vm_map_lock(submap);
2958 vm_map_reference(submap);
2959 vm_map_unlock(submap);
2960 named_entry_unlock(named_entry);
2961
2962 result = vm_map_enter(target_map,
2963 &map_addr,
2964 map_size,
2965 mask,
2966 flags | VM_FLAGS_SUBMAP,
2967 (vm_object_t) submap,
2968 offset,
2969 copy,
2970 cur_protection,
2971 max_protection,
2972 inheritance);
2973 if (result != KERN_SUCCESS) {
2974 vm_map_deallocate(submap);
2975 } else {
2976 /*
2977 * No need to lock "submap" just to check its
2978 * "mapped" flag: that flag is never reset
2979 * once it's been set and if we race, we'll
2980 * just end up setting it twice, which is OK.
2981 */
2982 if (submap->mapped_in_other_pmaps == FALSE &&
2983 vm_map_pmap(submap) != PMAP_NULL &&
2984 vm_map_pmap(submap) !=
2985 vm_map_pmap(target_map)) {
2986 /*
2987 * This submap is being mapped in a map
2988 * that uses a different pmap.
2989 * Set its "mapped_in_other_pmaps" flag
2990 * to indicate that we now need to
2991 * remove mappings from all pmaps rather
2992 * than just the submap's pmap.
2993 */
2994 vm_map_lock(submap);
2995 submap->mapped_in_other_pmaps = TRUE;
2996 vm_map_unlock(submap);
2997 }
2998 *address = map_addr;
2999 }
3000 return result;
3001
3002 } else if (named_entry->is_pager) {
3003 unsigned int access;
3004 vm_prot_t protections;
3005 unsigned int wimg_mode;
3006
3007 protections = named_entry->protection & VM_PROT_ALL;
3008 access = GET_MAP_MEM(named_entry->protection);
3009
3010 if (flags & (VM_FLAGS_RETURN_DATA_ADDR|
3011 VM_FLAGS_RETURN_4K_DATA_ADDR)) {
3012 panic("VM_FLAGS_RETURN_DATA_ADDR not expected for submap.");
3013 }
3014
3015 object = vm_object_enter(named_entry->backing.pager,
3016 named_entry->size,
3017 named_entry->internal,
3018 FALSE,
3019 FALSE);
3020 if (object == VM_OBJECT_NULL) {
3021 named_entry_unlock(named_entry);
3022 return KERN_INVALID_OBJECT;
3023 }
3024
3025 /* JMM - drop reference on pager here */
3026
3027 /* create an extra ref for the named entry */
3028 vm_object_lock(object);
3029 vm_object_reference_locked(object);
3030 named_entry->backing.object = object;
3031 named_entry->is_pager = FALSE;
3032 named_entry_unlock(named_entry);
3033
3034 wimg_mode = object->wimg_bits;
3035
3036 if (access == MAP_MEM_IO) {
3037 wimg_mode = VM_WIMG_IO;
3038 } else if (access == MAP_MEM_COPYBACK) {
3039 wimg_mode = VM_WIMG_USE_DEFAULT;
3040 } else if (access == MAP_MEM_INNERWBACK) {
3041 wimg_mode = VM_WIMG_INNERWBACK;
3042 } else if (access == MAP_MEM_WTHRU) {
3043 wimg_mode = VM_WIMG_WTHRU;
3044 } else if (access == MAP_MEM_WCOMB) {
3045 wimg_mode = VM_WIMG_WCOMB;
3046 }
3047
3048 /* wait for object (if any) to be ready */
3049 if (!named_entry->internal) {
3050 while (!object->pager_ready) {
3051 vm_object_wait(
3052 object,
3053 VM_OBJECT_EVENT_PAGER_READY,
3054 THREAD_UNINT);
3055 vm_object_lock(object);
3056 }
3057 }
3058
3059 if (object->wimg_bits != wimg_mode)
3060 vm_object_change_wimg_mode(object, wimg_mode);
3061
3062 #if VM_OBJECT_TRACKING_OP_TRUESHARE
3063 if (!object->true_share &&
3064 vm_object_tracking_inited) {
3065 void *bt[VM_OBJECT_TRACKING_BTDEPTH];
3066 int num = 0;
3067
3068 num = OSBacktrace(bt,
3069 VM_OBJECT_TRACKING_BTDEPTH);
3070 btlog_add_entry(vm_object_tracking_btlog,
3071 object,
3072 VM_OBJECT_TRACKING_OP_TRUESHARE,
3073 bt,
3074 num);
3075 }
3076 #endif /* VM_OBJECT_TRACKING_OP_TRUESHARE */
3077
3078 object->true_share = TRUE;
3079
3080 if (object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC)
3081 object->copy_strategy = MEMORY_OBJECT_COPY_DELAY;
3082 vm_object_unlock(object);
3083
3084 } else if (named_entry->is_copy) {
3085 kern_return_t kr;
3086 vm_map_copy_t copy_map;
3087 vm_map_entry_t copy_entry;
3088 vm_map_offset_t copy_addr;
3089
3090 if (flags & ~(VM_FLAGS_FIXED |
3091 VM_FLAGS_ANYWHERE |
3092 VM_FLAGS_OVERWRITE |
3093 VM_FLAGS_RETURN_4K_DATA_ADDR |
3094 VM_FLAGS_RETURN_DATA_ADDR)) {
3095 named_entry_unlock(named_entry);
3096 return KERN_INVALID_ARGUMENT;
3097 }
3098
3099 if (flags & (VM_FLAGS_RETURN_DATA_ADDR |
3100 VM_FLAGS_RETURN_4K_DATA_ADDR)) {
3101 offset_in_mapping = offset - vm_object_trunc_page(offset);
3102 if (flags & VM_FLAGS_RETURN_4K_DATA_ADDR)
3103 offset_in_mapping &= ~((signed)(0xFFF));
3104 offset = vm_object_trunc_page(offset);
3105 map_size = vm_object_round_page(offset + offset_in_mapping + initial_size) - offset;
3106 }
3107
3108 copy_map = named_entry->backing.copy;
3109 assert(copy_map->type == VM_MAP_COPY_ENTRY_LIST);
3110 if (copy_map->type != VM_MAP_COPY_ENTRY_LIST) {
3111 /* unsupported type; should not happen */
3112 printf("vm_map_enter_mem_object: "
3113 "memory_entry->backing.copy "
3114 "unsupported type 0x%x\n",
3115 copy_map->type);
3116 named_entry_unlock(named_entry);
3117 return KERN_INVALID_ARGUMENT;
3118 }
3119
3120 /* reserve a contiguous range */
3121 kr = vm_map_enter(target_map,
3122 &map_addr,
3123 /* map whole mem entry, trim later: */
3124 named_entry->size,
3125 mask,
3126 flags & (VM_FLAGS_ANYWHERE |
3127 VM_FLAGS_OVERWRITE |
3128 VM_FLAGS_RETURN_4K_DATA_ADDR |
3129 VM_FLAGS_RETURN_DATA_ADDR),
3130 VM_OBJECT_NULL,
3131 0,
3132 FALSE, /* copy */
3133 cur_protection,
3134 max_protection,
3135 inheritance);
3136 if (kr != KERN_SUCCESS) {
3137 named_entry_unlock(named_entry);
3138 return kr;
3139 }
3140
3141 copy_addr = map_addr;
3142
3143 for (copy_entry = vm_map_copy_first_entry(copy_map);
3144 copy_entry != vm_map_copy_to_entry(copy_map);
3145 copy_entry = copy_entry->vme_next) {
3146 int remap_flags = 0;
3147 vm_map_t copy_submap;
3148 vm_object_t copy_object;
3149 vm_map_size_t copy_size;
3150 vm_object_offset_t copy_offset;
3151
3152 copy_offset = VME_OFFSET(copy_entry);
3153 copy_size = (copy_entry->vme_end -
3154 copy_entry->vme_start);
3155
3156 /* sanity check */
3157 if ((copy_addr + copy_size) >
3158 (map_addr +
3159 named_entry->size /* XXX full size */ )) {
3160 /* over-mapping too much !? */
3161 kr = KERN_INVALID_ARGUMENT;
3162 /* abort */
3163 break;
3164 }
3165
3166 /* take a reference on the object */
3167 if (copy_entry->is_sub_map) {
3168 remap_flags |= VM_FLAGS_SUBMAP;
3169 copy_submap = VME_SUBMAP(copy_entry);
3170 vm_map_lock(copy_submap);
3171 vm_map_reference(copy_submap);
3172 vm_map_unlock(copy_submap);
3173 copy_object = (vm_object_t) copy_submap;
3174 } else {
3175 copy_object = VME_OBJECT(copy_entry);
3176 vm_object_reference(copy_object);
3177 }
3178
3179 /* over-map the object into destination */
3180 remap_flags |= flags;
3181 remap_flags |= VM_FLAGS_FIXED;
3182 remap_flags |= VM_FLAGS_OVERWRITE;
3183 remap_flags &= ~VM_FLAGS_ANYWHERE;
3184 kr = vm_map_enter(target_map,
3185 &copy_addr,
3186 copy_size,
3187 (vm_map_offset_t) 0,
3188 remap_flags,
3189 copy_object,
3190 copy_offset,
3191 copy,
3192 cur_protection,
3193 max_protection,
3194 inheritance);
3195 if (kr != KERN_SUCCESS) {
3196 if (copy_entry->is_sub_map) {
3197 vm_map_deallocate(copy_submap);
3198 } else {
3199 vm_object_deallocate(copy_object);
3200 }
3201 /* abort */
3202 break;
3203 }
3204
3205 /* next mapping */
3206 copy_addr += copy_size;
3207 }
3208
3209 if (kr == KERN_SUCCESS) {
3210 if (flags & (VM_FLAGS_RETURN_DATA_ADDR |
3211 VM_FLAGS_RETURN_4K_DATA_ADDR)) {
3212 *address = map_addr + offset_in_mapping;
3213 } else {
3214 *address = map_addr;
3215 }
3216
3217 if (offset) {
3218 /*
3219 * Trim in front, from 0 to "offset".
3220 */
3221 vm_map_remove(target_map,
3222 map_addr,
3223 map_addr + offset,
3224 0);
3225 *address += offset;
3226 }
3227 if (offset + map_size < named_entry->size) {
3228 /*
3229 * Trim in back, from
3230 * "offset + map_size" to
3231 * "named_entry->size".
3232 */
3233 vm_map_remove(target_map,
3234 (map_addr +
3235 offset + map_size),
3236 (map_addr +
3237 named_entry->size),
3238 0);
3239 }
3240 }
3241 named_entry_unlock(named_entry);
3242
3243 if (kr != KERN_SUCCESS) {
3244 if (! (flags & VM_FLAGS_OVERWRITE)) {
3245 /* deallocate the contiguous range */
3246 (void) vm_deallocate(target_map,
3247 map_addr,
3248 map_size);
3249 }
3250 }
3251
3252 return kr;
3253
3254 } else {
3255 /* This is the case where we are going to map */
3256 /* an already mapped object. If the object is */
3257 /* not ready it is internal. An external */
3258 /* object cannot be mapped until it is ready */
3259 /* we can therefore avoid the ready check */
3260 /* in this case. */
3261 if (flags & (VM_FLAGS_RETURN_DATA_ADDR |
3262 VM_FLAGS_RETURN_4K_DATA_ADDR)) {
3263 offset_in_mapping = offset - vm_object_trunc_page(offset);
3264 if (flags & VM_FLAGS_RETURN_4K_DATA_ADDR)
3265 offset_in_mapping &= ~((signed)(0xFFF));
3266 offset = vm_object_trunc_page(offset);
3267 map_size = vm_object_round_page(offset + offset_in_mapping + initial_size) - offset;
3268 }
3269
3270 object = named_entry->backing.object;
3271 assert(object != VM_OBJECT_NULL);
3272 named_entry_unlock(named_entry);
3273 vm_object_reference(object);
3274 }
3275 } else if (ip_kotype(port) == IKOT_MEMORY_OBJECT) {
3276 /*
3277 * JMM - This is temporary until we unify named entries
3278 * and raw memory objects.
3279 *
3280 * Detected fake ip_kotype for a memory object. In
3281 * this case, the port isn't really a port at all, but
3282 * instead is just a raw memory object.
3283 */
3284 if (flags & (VM_FLAGS_RETURN_DATA_ADDR |
3285 VM_FLAGS_RETURN_4K_DATA_ADDR)) {
3286 panic("VM_FLAGS_RETURN_DATA_ADDR not expected for raw memory object.");
3287 }
3288
3289 object = vm_object_enter((memory_object_t)port,
3290 size, FALSE, FALSE, FALSE);
3291 if (object == VM_OBJECT_NULL)
3292 return KERN_INVALID_OBJECT;
3293
3294 /* wait for object (if any) to be ready */
3295 if (object != VM_OBJECT_NULL) {
3296 if (object == kernel_object) {
3297 printf("Warning: Attempt to map kernel object"
3298 " by a non-private kernel entity\n");
3299 return KERN_INVALID_OBJECT;
3300 }
3301 if (!object->pager_ready) {
3302 vm_object_lock(object);
3303
3304 while (!object->pager_ready) {
3305 vm_object_wait(object,
3306 VM_OBJECT_EVENT_PAGER_READY,
3307 THREAD_UNINT);
3308 vm_object_lock(object);
3309 }
3310 vm_object_unlock(object);
3311 }
3312 }
3313 } else {
3314 return KERN_INVALID_OBJECT;
3315 }
3316
3317 if (object != VM_OBJECT_NULL &&
3318 object->named &&
3319 object->pager != MEMORY_OBJECT_NULL &&
3320 object->copy_strategy != MEMORY_OBJECT_COPY_NONE) {
3321 memory_object_t pager;
3322 vm_prot_t pager_prot;
3323 kern_return_t kr;
3324
3325 /*
3326 * For "named" VM objects, let the pager know that the
3327 * memory object is being mapped. Some pagers need to keep
3328 * track of this, to know when they can reclaim the memory
3329 * object, for example.
3330 * VM calls memory_object_map() for each mapping (specifying
3331 * the protection of each mapping) and calls
3332 * memory_object_last_unmap() when all the mappings are gone.
3333 */
3334 pager_prot = max_protection;
3335 if (copy) {
3336 /*
3337 * Copy-On-Write mapping: won't modify the
3338 * memory object.
3339 */
3340 pager_prot &= ~VM_PROT_WRITE;
3341 }
3342 vm_object_lock(object);
3343 pager = object->pager;
3344 if (object->named &&
3345 pager != MEMORY_OBJECT_NULL &&
3346 object->copy_strategy != MEMORY_OBJECT_COPY_NONE) {
3347 assert(object->pager_ready);
3348 vm_object_mapping_wait(object, THREAD_UNINT);
3349 vm_object_mapping_begin(object);
3350 vm_object_unlock(object);
3351
3352 kr = memory_object_map(pager, pager_prot);
3353 assert(kr == KERN_SUCCESS);
3354
3355 vm_object_lock(object);
3356 vm_object_mapping_end(object);
3357 }
3358 vm_object_unlock(object);
3359 }
3360
3361 /*
3362 * Perform the copy if requested
3363 */
3364
3365 if (copy) {
3366 vm_object_t new_object;
3367 vm_object_offset_t new_offset;
3368
3369 result = vm_object_copy_strategically(object, offset,
3370 map_size,
3371 &new_object, &new_offset,
3372 &copy);
3373
3374
3375 if (result == KERN_MEMORY_RESTART_COPY) {
3376 boolean_t success;
3377 boolean_t src_needs_copy;
3378
3379 /*
3380 * XXX
3381 * We currently ignore src_needs_copy.
3382 * This really is the issue of how to make
3383 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
3384 * non-kernel users to use. Solution forthcoming.
3385 * In the meantime, since we don't allow non-kernel
3386 * memory managers to specify symmetric copy,
3387 * we won't run into problems here.
3388 */
3389 new_object = object;
3390 new_offset = offset;
3391 success = vm_object_copy_quickly(&new_object,
3392 new_offset,
3393 map_size,
3394 &src_needs_copy,
3395 &copy);
3396 assert(success);
3397 result = KERN_SUCCESS;
3398 }
3399 /*
3400 * Throw away the reference to the
3401 * original object, as it won't be mapped.
3402 */
3403
3404 vm_object_deallocate(object);
3405
3406 if (result != KERN_SUCCESS) {
3407 return result;
3408 }
3409
3410 object = new_object;
3411 offset = new_offset;
3412 }
3413
3414 /*
3415 * If users want to try to prefault pages, the mapping and prefault
3416 * needs to be atomic.
3417 */
3418 if (try_prefault)
3419 flags |= VM_FLAGS_KEEP_MAP_LOCKED;
3420
3421 {
3422 result = vm_map_enter(target_map,
3423 &map_addr, map_size,
3424 (vm_map_offset_t)mask,
3425 flags,
3426 object, offset,
3427 copy,
3428 cur_protection, max_protection,
3429 inheritance);
3430 }
3431 if (result != KERN_SUCCESS)
3432 vm_object_deallocate(object);
3433
3434 /*
3435 * Try to prefault, and do not forget to release the vm map lock.
3436 */
3437 if (result == KERN_SUCCESS && try_prefault) {
3438 mach_vm_address_t va = map_addr;
3439 kern_return_t kr = KERN_SUCCESS;
3440 unsigned int i = 0;
3441
3442 for (i = 0; i < page_list_count; ++i) {
3443 if (UPL_VALID_PAGE(page_list, i)) {
3444 /*
3445 * If this function call failed, we should stop
3446 * trying to optimize, other calls are likely
3447 * going to fail too.
3448 *
3449 * We are not gonna report an error for such
3450 * failure though. That's an optimization, not
3451 * something critical.
3452 */
3453 kr = pmap_enter_options(target_map->pmap,
3454 va, UPL_PHYS_PAGE(page_list, i),
3455 cur_protection, VM_PROT_NONE,
3456 0, TRUE, PMAP_OPTIONS_NOWAIT, NULL);
3457 if (kr != KERN_SUCCESS) {
3458 OSIncrementAtomic64(&vm_prefault_nb_bailout);
3459 break;
3460 }
3461 OSIncrementAtomic64(&vm_prefault_nb_pages);
3462 }
3463
3464 /* Next virtual address */
3465 va += PAGE_SIZE;
3466 }
3467 vm_map_unlock(target_map);
3468 }
3469
3470 if (flags & (VM_FLAGS_RETURN_DATA_ADDR |
3471 VM_FLAGS_RETURN_4K_DATA_ADDR)) {
3472 *address = map_addr + offset_in_mapping;
3473 } else {
3474 *address = map_addr;
3475 }
3476 return result;
3477 }
3478
3479 kern_return_t
3480 vm_map_enter_mem_object(
3481 vm_map_t target_map,
3482 vm_map_offset_t *address,
3483 vm_map_size_t initial_size,
3484 vm_map_offset_t mask,
3485 int flags,
3486 ipc_port_t port,
3487 vm_object_offset_t offset,
3488 boolean_t copy,
3489 vm_prot_t cur_protection,
3490 vm_prot_t max_protection,
3491 vm_inherit_t inheritance)
3492 {
3493 return vm_map_enter_mem_object_helper(target_map, address, initial_size, mask, flags,
3494 port, offset, copy, cur_protection, max_protection,
3495 inheritance, NULL, 0);
3496 }
3497
3498 kern_return_t
3499 vm_map_enter_mem_object_prefault(
3500 vm_map_t target_map,
3501 vm_map_offset_t *address,
3502 vm_map_size_t initial_size,
3503 vm_map_offset_t mask,
3504 int flags,
3505 ipc_port_t port,
3506 vm_object_offset_t offset,
3507 vm_prot_t cur_protection,
3508 vm_prot_t max_protection,
3509 upl_page_list_ptr_t page_list,
3510 unsigned int page_list_count)
3511 {
3512 return vm_map_enter_mem_object_helper(target_map, address, initial_size, mask, flags,
3513 port, offset, FALSE, cur_protection, max_protection,
3514 VM_INHERIT_DEFAULT, page_list, page_list_count);
3515 }
3516
3517
3518 kern_return_t
3519 vm_map_enter_mem_object_control(
3520 vm_map_t target_map,
3521 vm_map_offset_t *address,
3522 vm_map_size_t initial_size,
3523 vm_map_offset_t mask,
3524 int flags,
3525 memory_object_control_t control,
3526 vm_object_offset_t offset,
3527 boolean_t copy,
3528 vm_prot_t cur_protection,
3529 vm_prot_t max_protection,
3530 vm_inherit_t inheritance)
3531 {
3532 vm_map_address_t map_addr;
3533 vm_map_size_t map_size;
3534 vm_object_t object;
3535 vm_object_size_t size;
3536 kern_return_t result;
3537 memory_object_t pager;
3538 vm_prot_t pager_prot;
3539 kern_return_t kr;
3540
3541 /*
3542 * Check arguments for validity
3543 */
3544 if ((target_map == VM_MAP_NULL) ||
3545 (cur_protection & ~VM_PROT_ALL) ||
3546 (max_protection & ~VM_PROT_ALL) ||
3547 (inheritance > VM_INHERIT_LAST_VALID) ||
3548 initial_size == 0) {
3549 return KERN_INVALID_ARGUMENT;
3550 }
3551
3552 {
3553 map_addr = vm_map_trunc_page(*address,
3554 VM_MAP_PAGE_MASK(target_map));
3555 map_size = vm_map_round_page(initial_size,
3556 VM_MAP_PAGE_MASK(target_map));
3557 }
3558 size = vm_object_round_page(initial_size);
3559
3560 object = memory_object_control_to_vm_object(control);
3561
3562 if (object == VM_OBJECT_NULL)
3563 return KERN_INVALID_OBJECT;
3564
3565 if (object == kernel_object) {
3566 printf("Warning: Attempt to map kernel object"
3567 " by a non-private kernel entity\n");
3568 return KERN_INVALID_OBJECT;
3569 }
3570
3571 vm_object_lock(object);
3572 object->ref_count++;
3573 vm_object_res_reference(object);
3574
3575 /*
3576 * For "named" VM objects, let the pager know that the
3577 * memory object is being mapped. Some pagers need to keep
3578 * track of this, to know when they can reclaim the memory
3579 * object, for example.
3580 * VM calls memory_object_map() for each mapping (specifying
3581 * the protection of each mapping) and calls
3582 * memory_object_last_unmap() when all the mappings are gone.
3583 */
3584 pager_prot = max_protection;
3585 if (copy) {
3586 pager_prot &= ~VM_PROT_WRITE;
3587 }
3588 pager = object->pager;
3589 if (object->named &&
3590 pager != MEMORY_OBJECT_NULL &&
3591 object->copy_strategy != MEMORY_OBJECT_COPY_NONE) {
3592 assert(object->pager_ready);
3593 vm_object_mapping_wait(object, THREAD_UNINT);
3594 vm_object_mapping_begin(object);
3595 vm_object_unlock(object);
3596
3597 kr = memory_object_map(pager, pager_prot);
3598 assert(kr == KERN_SUCCESS);
3599
3600 vm_object_lock(object);
3601 vm_object_mapping_end(object);
3602 }
3603 vm_object_unlock(object);
3604
3605 /*
3606 * Perform the copy if requested
3607 */
3608
3609 if (copy) {
3610 vm_object_t new_object;
3611 vm_object_offset_t new_offset;
3612
3613 result = vm_object_copy_strategically(object, offset, size,
3614 &new_object, &new_offset,
3615 &copy);
3616
3617
3618 if (result == KERN_MEMORY_RESTART_COPY) {
3619 boolean_t success;
3620 boolean_t src_needs_copy;
3621
3622 /*
3623 * XXX
3624 * We currently ignore src_needs_copy.
3625 * This really is the issue of how to make
3626 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
3627 * non-kernel users to use. Solution forthcoming.
3628 * In the meantime, since we don't allow non-kernel
3629 * memory managers to specify symmetric copy,
3630 * we won't run into problems here.
3631 */
3632 new_object = object;
3633 new_offset = offset;
3634 success = vm_object_copy_quickly(&new_object,
3635 new_offset, size,
3636 &src_needs_copy,
3637 &copy);
3638 assert(success);
3639 result = KERN_SUCCESS;
3640 }
3641 /*
3642 * Throw away the reference to the
3643 * original object, as it won't be mapped.
3644 */
3645
3646 vm_object_deallocate(object);
3647
3648 if (result != KERN_SUCCESS) {
3649 return result;
3650 }
3651
3652 object = new_object;
3653 offset = new_offset;
3654 }
3655
3656 {
3657 result = vm_map_enter(target_map,
3658 &map_addr, map_size,
3659 (vm_map_offset_t)mask,
3660 flags,
3661 object, offset,
3662 copy,
3663 cur_protection, max_protection,
3664 inheritance);
3665 }
3666 if (result != KERN_SUCCESS)
3667 vm_object_deallocate(object);
3668 *address = map_addr;
3669
3670 return result;
3671 }
3672
3673
3674 #if VM_CPM
3675
3676 #ifdef MACH_ASSERT
3677 extern pmap_paddr_t avail_start, avail_end;
3678 #endif
3679
3680 /*
3681 * Allocate memory in the specified map, with the caveat that
3682 * the memory is physically contiguous. This call may fail
3683 * if the system can't find sufficient contiguous memory.
3684 * This call may cause or lead to heart-stopping amounts of
3685 * paging activity.
3686 *
3687 * Memory obtained from this call should be freed in the
3688 * normal way, viz., via vm_deallocate.
3689 */
3690 kern_return_t
3691 vm_map_enter_cpm(
3692 vm_map_t map,
3693 vm_map_offset_t *addr,
3694 vm_map_size_t size,
3695 int flags)
3696 {
3697 vm_object_t cpm_obj;
3698 pmap_t pmap;
3699 vm_page_t m, pages;
3700 kern_return_t kr;
3701 vm_map_offset_t va, start, end, offset;
3702 #if MACH_ASSERT
3703 vm_map_offset_t prev_addr = 0;
3704 #endif /* MACH_ASSERT */
3705
3706 boolean_t anywhere = ((VM_FLAGS_ANYWHERE & flags) != 0);
3707 vm_tag_t tag;
3708
3709 VM_GET_FLAGS_ALIAS(flags, tag);
3710
3711 if (size == 0) {
3712 *addr = 0;
3713 return KERN_SUCCESS;
3714 }
3715 if (anywhere)
3716 *addr = vm_map_min(map);
3717 else
3718 *addr = vm_map_trunc_page(*addr,
3719 VM_MAP_PAGE_MASK(map));
3720 size = vm_map_round_page(size,
3721 VM_MAP_PAGE_MASK(map));
3722
3723 /*
3724 * LP64todo - cpm_allocate should probably allow
3725 * allocations of >4GB, but not with the current
3726 * algorithm, so just cast down the size for now.
3727 */
3728 if (size > VM_MAX_ADDRESS)
3729 return KERN_RESOURCE_SHORTAGE;
3730 if ((kr = cpm_allocate(CAST_DOWN(vm_size_t, size),
3731 &pages, 0, 0, TRUE, flags)) != KERN_SUCCESS)
3732 return kr;
3733
3734 cpm_obj = vm_object_allocate((vm_object_size_t)size);
3735 assert(cpm_obj != VM_OBJECT_NULL);
3736 assert(cpm_obj->internal);
3737 assert(cpm_obj->vo_size == (vm_object_size_t)size);
3738 assert(cpm_obj->can_persist == FALSE);
3739 assert(cpm_obj->pager_created == FALSE);
3740 assert(cpm_obj->pageout == FALSE);
3741 assert(cpm_obj->shadow == VM_OBJECT_NULL);
3742
3743 /*
3744 * Insert pages into object.
3745 */
3746
3747 vm_object_lock(cpm_obj);
3748 for (offset = 0; offset < size; offset += PAGE_SIZE) {
3749 m = pages;
3750 pages = NEXT_PAGE(m);
3751 *(NEXT_PAGE_PTR(m)) = VM_PAGE_NULL;
3752
3753 assert(!m->gobbled);
3754 assert(!m->wanted);
3755 assert(!m->pageout);
3756 assert(!m->tabled);
3757 assert(VM_PAGE_WIRED(m));
3758 /*
3759 * ENCRYPTED SWAP:
3760 * "m" is not supposed to be pageable, so it
3761 * should not be encrypted. It wouldn't be safe
3762 * to enter it in a new VM object while encrypted.
3763 */
3764 ASSERT_PAGE_DECRYPTED(m);
3765 assert(m->busy);
3766 assert(m->phys_page>=(avail_start>>PAGE_SHIFT) && m->phys_page<=(avail_end>>PAGE_SHIFT));
3767
3768 m->busy = FALSE;
3769 vm_page_insert(m, cpm_obj, offset);
3770 }
3771 assert(cpm_obj->resident_page_count == size / PAGE_SIZE);
3772 vm_object_unlock(cpm_obj);
3773
3774 /*
3775 * Hang onto a reference on the object in case a
3776 * multi-threaded application for some reason decides
3777 * to deallocate the portion of the address space into
3778 * which we will insert this object.
3779 *
3780 * Unfortunately, we must insert the object now before
3781 * we can talk to the pmap module about which addresses
3782 * must be wired down. Hence, the race with a multi-
3783 * threaded app.
3784 */
3785 vm_object_reference(cpm_obj);
3786
3787 /*
3788 * Insert object into map.
3789 */
3790
3791 kr = vm_map_enter(
3792 map,
3793 addr,
3794 size,
3795 (vm_map_offset_t)0,
3796 flags,
3797 cpm_obj,
3798 (vm_object_offset_t)0,
3799 FALSE,
3800 VM_PROT_ALL,
3801 VM_PROT_ALL,
3802 VM_INHERIT_DEFAULT);
3803
3804 if (kr != KERN_SUCCESS) {
3805 /*
3806 * A CPM object doesn't have can_persist set,
3807 * so all we have to do is deallocate it to
3808 * free up these pages.
3809 */
3810 assert(cpm_obj->pager_created == FALSE);
3811 assert(cpm_obj->can_persist == FALSE);
3812 assert(cpm_obj->pageout == FALSE);
3813 assert(cpm_obj->shadow == VM_OBJECT_NULL);
3814 vm_object_deallocate(cpm_obj); /* kill acquired ref */
3815 vm_object_deallocate(cpm_obj); /* kill creation ref */
3816 }
3817
3818 /*
3819 * Inform the physical mapping system that the
3820 * range of addresses may not fault, so that
3821 * page tables and such can be locked down as well.
3822 */
3823 start = *addr;
3824 end = start + size;
3825 pmap = vm_map_pmap(map);
3826 pmap_pageable(pmap, start, end, FALSE);
3827
3828 /*
3829 * Enter each page into the pmap, to avoid faults.
3830 * Note that this loop could be coded more efficiently,
3831 * if the need arose, rather than looking up each page
3832 * again.
3833 */
3834 for (offset = 0, va = start; offset < size;
3835 va += PAGE_SIZE, offset += PAGE_SIZE) {
3836 int type_of_fault;
3837
3838 vm_object_lock(cpm_obj);
3839 m = vm_page_lookup(cpm_obj, (vm_object_offset_t)offset);
3840 assert(m != VM_PAGE_NULL);
3841
3842 vm_page_zero_fill(m);
3843
3844 type_of_fault = DBG_ZERO_FILL_FAULT;
3845
3846 vm_fault_enter(m, pmap, va, VM_PROT_ALL, VM_PROT_WRITE,
3847 VM_PAGE_WIRED(m), FALSE, FALSE, FALSE, 0, NULL,
3848 &type_of_fault);
3849
3850 vm_object_unlock(cpm_obj);
3851 }
3852
3853 #if MACH_ASSERT
3854 /*
3855 * Verify ordering in address space.
3856 */
3857 for (offset = 0; offset < size; offset += PAGE_SIZE) {
3858 vm_object_lock(cpm_obj);
3859 m = vm_page_lookup(cpm_obj, (vm_object_offset_t)offset);
3860 vm_object_unlock(cpm_obj);
3861 if (m == VM_PAGE_NULL)
3862 panic("vm_allocate_cpm: obj %p off 0x%llx no page",
3863 cpm_obj, (uint64_t)offset);
3864 assert(m->tabled);
3865 assert(!m->busy);
3866 assert(!m->wanted);
3867 assert(!m->fictitious);
3868 assert(!m->private);
3869 assert(!m->absent);
3870 assert(!m->error);
3871 assert(!m->cleaning);
3872 assert(!m->laundry);
3873 assert(!m->precious);
3874 assert(!m->clustered);
3875 if (offset != 0) {
3876 if (m->phys_page != prev_addr + 1) {
3877 printf("start 0x%llx end 0x%llx va 0x%llx\n",
3878 (uint64_t)start, (uint64_t)end, (uint64_t)va);
3879 printf("obj %p off 0x%llx\n", cpm_obj, (uint64_t)offset);
3880 printf("m %p prev_address 0x%llx\n", m, (uint64_t)prev_addr);
3881 panic("vm_allocate_cpm: pages not contig!");
3882 }
3883 }
3884 prev_addr = m->phys_page;
3885 }
3886 #endif /* MACH_ASSERT */
3887
3888 vm_object_deallocate(cpm_obj); /* kill extra ref */
3889
3890 return kr;
3891 }
3892
3893
3894 #else /* VM_CPM */
3895
3896 /*
3897 * Interface is defined in all cases, but unless the kernel
3898 * is built explicitly for this option, the interface does
3899 * nothing.
3900 */
3901
3902 kern_return_t
3903 vm_map_enter_cpm(
3904 __unused vm_map_t map,
3905 __unused vm_map_offset_t *addr,
3906 __unused vm_map_size_t size,
3907 __unused int flags)
3908 {
3909 return KERN_FAILURE;
3910 }
3911 #endif /* VM_CPM */
3912
3913 /* Not used without nested pmaps */
3914 #ifndef NO_NESTED_PMAP
3915 /*
3916 * Clip and unnest a portion of a nested submap mapping.
3917 */
3918
3919
3920 static void
3921 vm_map_clip_unnest(
3922 vm_map_t map,
3923 vm_map_entry_t entry,
3924 vm_map_offset_t start_unnest,
3925 vm_map_offset_t end_unnest)
3926 {
3927 vm_map_offset_t old_start_unnest = start_unnest;
3928 vm_map_offset_t old_end_unnest = end_unnest;
3929
3930 assert(entry->is_sub_map);
3931 assert(VME_SUBMAP(entry) != NULL);
3932 assert(entry->use_pmap);
3933
3934 /*
3935 * Query the platform for the optimal unnest range.
3936 * DRK: There's some duplication of effort here, since
3937 * callers may have adjusted the range to some extent. This
3938 * routine was introduced to support 1GiB subtree nesting
3939 * for x86 platforms, which can also nest on 2MiB boundaries
3940 * depending on size/alignment.
3941 */
3942 if (pmap_adjust_unnest_parameters(map->pmap, &start_unnest, &end_unnest)) {
3943 log_unnest_badness(map, old_start_unnest, old_end_unnest);
3944 }
3945
3946 if (entry->vme_start > start_unnest ||
3947 entry->vme_end < end_unnest) {
3948 panic("vm_map_clip_unnest(0x%llx,0x%llx): "
3949 "bad nested entry: start=0x%llx end=0x%llx\n",
3950 (long long)start_unnest, (long long)end_unnest,
3951 (long long)entry->vme_start, (long long)entry->vme_end);
3952 }
3953
3954 if (start_unnest > entry->vme_start) {
3955 _vm_map_clip_start(&map->hdr,
3956 entry,
3957 start_unnest);
3958 if (map->holelistenabled) {
3959 vm_map_store_update_first_free(map, NULL, FALSE);
3960 } else {
3961 vm_map_store_update_first_free(map, map->first_free, FALSE);
3962 }
3963 }
3964 if (entry->vme_end > end_unnest) {
3965 _vm_map_clip_end(&map->hdr,
3966 entry,
3967 end_unnest);
3968 if (map->holelistenabled) {
3969 vm_map_store_update_first_free(map, NULL, FALSE);
3970 } else {
3971 vm_map_store_update_first_free(map, map->first_free, FALSE);
3972 }
3973 }
3974
3975 pmap_unnest(map->pmap,
3976 entry->vme_start,
3977 entry->vme_end - entry->vme_start);
3978 if ((map->mapped_in_other_pmaps) && (map->ref_count)) {
3979 /* clean up parent map/maps */
3980 vm_map_submap_pmap_clean(
3981 map, entry->vme_start,
3982 entry->vme_end,
3983 VME_SUBMAP(entry),
3984 VME_OFFSET(entry));
3985 }
3986 entry->use_pmap = FALSE;
3987 if ((map->pmap != kernel_pmap) &&
3988 (VME_ALIAS(entry) == VM_MEMORY_SHARED_PMAP)) {
3989 VME_ALIAS_SET(entry, VM_MEMORY_UNSHARED_PMAP);
3990 }
3991 }
3992 #endif /* NO_NESTED_PMAP */
3993
3994 /*
3995 * vm_map_clip_start: [ internal use only ]
3996 *
3997 * Asserts that the given entry begins at or after
3998 * the specified address; if necessary,
3999 * it splits the entry into two.
4000 */
4001 void
4002 vm_map_clip_start(
4003 vm_map_t map,
4004 vm_map_entry_t entry,
4005 vm_map_offset_t startaddr)
4006 {
4007 #ifndef NO_NESTED_PMAP
4008 if (entry->is_sub_map &&
4009 entry->use_pmap &&
4010 startaddr >= entry->vme_start) {
4011 vm_map_offset_t start_unnest, end_unnest;
4012
4013 /*
4014 * Make sure "startaddr" is no longer in a nested range
4015 * before we clip. Unnest only the minimum range the platform
4016 * can handle.
4017 * vm_map_clip_unnest may perform additional adjustments to
4018 * the unnest range.
4019 */
4020 start_unnest = startaddr & ~(pmap_nesting_size_min - 1);
4021 end_unnest = start_unnest + pmap_nesting_size_min;
4022 vm_map_clip_unnest(map, entry, start_unnest, end_unnest);
4023 }
4024 #endif /* NO_NESTED_PMAP */
4025 if (startaddr > entry->vme_start) {
4026 if (VME_OBJECT(entry) &&
4027 !entry->is_sub_map &&
4028 VME_OBJECT(entry)->phys_contiguous) {
4029 pmap_remove(map->pmap,
4030 (addr64_t)(entry->vme_start),
4031 (addr64_t)(entry->vme_end));
4032 }
4033 _vm_map_clip_start(&map->hdr, entry, startaddr);
4034 if (map->holelistenabled) {
4035 vm_map_store_update_first_free(map, NULL, FALSE);
4036 } else {
4037 vm_map_store_update_first_free(map, map->first_free, FALSE);
4038 }
4039 }
4040 }
4041
4042
4043 #define vm_map_copy_clip_start(copy, entry, startaddr) \
4044 MACRO_BEGIN \
4045 if ((startaddr) > (entry)->vme_start) \
4046 _vm_map_clip_start(&(copy)->cpy_hdr,(entry),(startaddr)); \
4047 MACRO_END
4048
4049 /*
4050 * This routine is called only when it is known that
4051 * the entry must be split.
4052 */
4053 static void
4054 _vm_map_clip_start(
4055 register struct vm_map_header *map_header,
4056 register vm_map_entry_t entry,
4057 register vm_map_offset_t start)
4058 {
4059 register vm_map_entry_t new_entry;
4060
4061 /*
4062 * Split off the front portion --
4063 * note that we must insert the new
4064 * entry BEFORE this one, so that
4065 * this entry has the specified starting
4066 * address.
4067 */
4068
4069 if (entry->map_aligned) {
4070 assert(VM_MAP_PAGE_ALIGNED(start,
4071 VM_MAP_HDR_PAGE_MASK(map_header)));
4072 }
4073
4074 new_entry = _vm_map_entry_create(map_header, !map_header->entries_pageable);
4075 vm_map_entry_copy_full(new_entry, entry);
4076
4077 new_entry->vme_end = start;
4078 assert(new_entry->vme_start < new_entry->vme_end);
4079 VME_OFFSET_SET(entry, VME_OFFSET(entry) + (start - entry->vme_start));
4080 assert(start < entry->vme_end);
4081 entry->vme_start = start;
4082
4083 _vm_map_store_entry_link(map_header, entry->vme_prev, new_entry);
4084
4085 if (entry->is_sub_map)
4086 vm_map_reference(VME_SUBMAP(new_entry));
4087 else
4088 vm_object_reference(VME_OBJECT(new_entry));
4089 }
4090
4091
4092 /*
4093 * vm_map_clip_end: [ internal use only ]
4094 *
4095 * Asserts that the given entry ends at or before
4096 * the specified address; if necessary,
4097 * it splits the entry into two.
4098 */
4099 void
4100 vm_map_clip_end(
4101 vm_map_t map,
4102 vm_map_entry_t entry,
4103 vm_map_offset_t endaddr)
4104 {
4105 if (endaddr > entry->vme_end) {
4106 /*
4107 * Within the scope of this clipping, limit "endaddr" to
4108 * the end of this map entry...
4109 */
4110 endaddr = entry->vme_end;
4111 }
4112 #ifndef NO_NESTED_PMAP
4113 if (entry->is_sub_map && entry->use_pmap) {
4114 vm_map_offset_t start_unnest, end_unnest;
4115
4116 /*
4117 * Make sure the range between the start of this entry and
4118 * the new "endaddr" is no longer nested before we clip.
4119 * Unnest only the minimum range the platform can handle.
4120 * vm_map_clip_unnest may perform additional adjustments to
4121 * the unnest range.
4122 */
4123 start_unnest = entry->vme_start;
4124 end_unnest =
4125 (endaddr + pmap_nesting_size_min - 1) &
4126 ~(pmap_nesting_size_min - 1);
4127 vm_map_clip_unnest(map, entry, start_unnest, end_unnest);
4128 }
4129 #endif /* NO_NESTED_PMAP */
4130 if (endaddr < entry->vme_end) {
4131 if (VME_OBJECT(entry) &&
4132 !entry->is_sub_map &&
4133 VME_OBJECT(entry)->phys_contiguous) {
4134 pmap_remove(map->pmap,
4135 (addr64_t)(entry->vme_start),
4136 (addr64_t)(entry->vme_end));
4137 }
4138 _vm_map_clip_end(&map->hdr, entry, endaddr);
4139 if (map->holelistenabled) {
4140 vm_map_store_update_first_free(map, NULL, FALSE);
4141 } else {
4142 vm_map_store_update_first_free(map, map->first_free, FALSE);
4143 }
4144 }
4145 }
4146
4147
4148 #define vm_map_copy_clip_end(copy, entry, endaddr) \
4149 MACRO_BEGIN \
4150 if ((endaddr) < (entry)->vme_end) \
4151 _vm_map_clip_end(&(copy)->cpy_hdr,(entry),(endaddr)); \
4152 MACRO_END
4153
4154 /*
4155 * This routine is called only when it is known that
4156 * the entry must be split.
4157 */
4158 static void
4159 _vm_map_clip_end(
4160 register struct vm_map_header *map_header,
4161 register vm_map_entry_t entry,
4162 register vm_map_offset_t end)
4163 {
4164 register vm_map_entry_t new_entry;
4165
4166 /*
4167 * Create a new entry and insert it
4168 * AFTER the specified entry
4169 */
4170
4171 if (entry->map_aligned) {
4172 assert(VM_MAP_PAGE_ALIGNED(end,
4173 VM_MAP_HDR_PAGE_MASK(map_header)));
4174 }
4175
4176 new_entry = _vm_map_entry_create(map_header, !map_header->entries_pageable);
4177 vm_map_entry_copy_full(new_entry, entry);
4178
4179 assert(entry->vme_start < end);
4180 new_entry->vme_start = entry->vme_end = end;
4181 VME_OFFSET_SET(new_entry,
4182 VME_OFFSET(new_entry) + (end - entry->vme_start));
4183 assert(new_entry->vme_start < new_entry->vme_end);
4184
4185 _vm_map_store_entry_link(map_header, entry, new_entry);
4186
4187 if (entry->is_sub_map)
4188 vm_map_reference(VME_SUBMAP(new_entry));
4189 else
4190 vm_object_reference(VME_OBJECT(new_entry));
4191 }
4192
4193
4194 /*
4195 * VM_MAP_RANGE_CHECK: [ internal use only ]
4196 *
4197 * Asserts that the starting and ending region
4198 * addresses fall within the valid range of the map.
4199 */
4200 #define VM_MAP_RANGE_CHECK(map, start, end) \
4201 MACRO_BEGIN \
4202 if (start < vm_map_min(map)) \
4203 start = vm_map_min(map); \
4204 if (end > vm_map_max(map)) \
4205 end = vm_map_max(map); \
4206 if (start > end) \
4207 start = end; \
4208 MACRO_END
4209
4210 /*
4211 * vm_map_range_check: [ internal use only ]
4212 *
4213 * Check that the region defined by the specified start and
4214 * end addresses are wholly contained within a single map
4215 * entry or set of adjacent map entries of the spacified map,
4216 * i.e. the specified region contains no unmapped space.
4217 * If any or all of the region is unmapped, FALSE is returned.
4218 * Otherwise, TRUE is returned and if the output argument 'entry'
4219 * is not NULL it points to the map entry containing the start
4220 * of the region.
4221 *
4222 * The map is locked for reading on entry and is left locked.
4223 */
4224 static boolean_t
4225 vm_map_range_check(
4226 register vm_map_t map,
4227 register vm_map_offset_t start,
4228 register vm_map_offset_t end,
4229 vm_map_entry_t *entry)
4230 {
4231 vm_map_entry_t cur;
4232 register vm_map_offset_t prev;
4233
4234 /*
4235 * Basic sanity checks first
4236 */
4237 if (start < vm_map_min(map) || end > vm_map_max(map) || start > end)
4238 return (FALSE);
4239
4240 /*
4241 * Check first if the region starts within a valid
4242 * mapping for the map.
4243 */
4244 if (!vm_map_lookup_entry(map, start, &cur))
4245 return (FALSE);
4246
4247 /*
4248 * Optimize for the case that the region is contained
4249 * in a single map entry.
4250 */
4251 if (entry != (vm_map_entry_t *) NULL)
4252 *entry = cur;
4253 if (end <= cur->vme_end)
4254 return (TRUE);
4255
4256 /*
4257 * If the region is not wholly contained within a
4258 * single entry, walk the entries looking for holes.
4259 */
4260 prev = cur->vme_end;
4261 cur = cur->vme_next;
4262 while ((cur != vm_map_to_entry(map)) && (prev == cur->vme_start)) {
4263 if (end <= cur->vme_end)
4264 return (TRUE);
4265 prev = cur->vme_end;
4266 cur = cur->vme_next;
4267 }
4268 return (FALSE);
4269 }
4270
4271 /*
4272 * vm_map_submap: [ kernel use only ]
4273 *
4274 * Mark the given range as handled by a subordinate map.
4275 *
4276 * This range must have been created with vm_map_find using
4277 * the vm_submap_object, and no other operations may have been
4278 * performed on this range prior to calling vm_map_submap.
4279 *
4280 * Only a limited number of operations can be performed
4281 * within this rage after calling vm_map_submap:
4282 * vm_fault
4283 * [Don't try vm_map_copyin!]
4284 *
4285 * To remove a submapping, one must first remove the
4286 * range from the superior map, and then destroy the
4287 * submap (if desired). [Better yet, don't try it.]
4288 */
4289 kern_return_t
4290 vm_map_submap(
4291 vm_map_t map,
4292 vm_map_offset_t start,
4293 vm_map_offset_t end,
4294 vm_map_t submap,
4295 vm_map_offset_t offset,
4296 #ifdef NO_NESTED_PMAP
4297 __unused
4298 #endif /* NO_NESTED_PMAP */
4299 boolean_t use_pmap)
4300 {
4301 vm_map_entry_t entry;
4302 register kern_return_t result = KERN_INVALID_ARGUMENT;
4303 register vm_object_t object;
4304
4305 vm_map_lock(map);
4306
4307 if (! vm_map_lookup_entry(map, start, &entry)) {
4308 entry = entry->vme_next;
4309 }
4310
4311 if (entry == vm_map_to_entry(map) ||
4312 entry->is_sub_map) {
4313 vm_map_unlock(map);
4314 return KERN_INVALID_ARGUMENT;
4315 }
4316
4317 vm_map_clip_start(map, entry, start);
4318 vm_map_clip_end(map, entry, end);
4319
4320 if ((entry->vme_start == start) && (entry->vme_end == end) &&
4321 (!entry->is_sub_map) &&
4322 ((object = VME_OBJECT(entry)) == vm_submap_object) &&
4323 (object->resident_page_count == 0) &&
4324 (object->copy == VM_OBJECT_NULL) &&
4325 (object->shadow == VM_OBJECT_NULL) &&
4326 (!object->pager_created)) {
4327 VME_OFFSET_SET(entry, (vm_object_offset_t)offset);
4328 VME_OBJECT_SET(entry, VM_OBJECT_NULL);
4329 vm_object_deallocate(object);
4330 entry->is_sub_map = TRUE;
4331 entry->use_pmap = FALSE;
4332 VME_SUBMAP_SET(entry, submap);
4333 vm_map_reference(submap);
4334 if (submap->mapped_in_other_pmaps == FALSE &&
4335 vm_map_pmap(submap) != PMAP_NULL &&
4336 vm_map_pmap(submap) != vm_map_pmap(map)) {
4337 /*
4338 * This submap is being mapped in a map
4339 * that uses a different pmap.
4340 * Set its "mapped_in_other_pmaps" flag
4341 * to indicate that we now need to
4342 * remove mappings from all pmaps rather
4343 * than just the submap's pmap.
4344 */
4345 submap->mapped_in_other_pmaps = TRUE;
4346 }
4347
4348 #ifndef NO_NESTED_PMAP
4349 if (use_pmap) {
4350 /* nest if platform code will allow */
4351 if(submap->pmap == NULL) {
4352 ledger_t ledger = map->pmap->ledger;
4353 submap->pmap = pmap_create(ledger,
4354 (vm_map_size_t) 0, FALSE);
4355 if(submap->pmap == PMAP_NULL) {
4356 vm_map_unlock(map);
4357 return(KERN_NO_SPACE);
4358 }
4359 }
4360 result = pmap_nest(map->pmap,
4361 (VME_SUBMAP(entry))->pmap,
4362 (addr64_t)start,
4363 (addr64_t)start,
4364 (uint64_t)(end - start));
4365 if(result)
4366 panic("vm_map_submap: pmap_nest failed, rc = %08X\n", result);
4367 entry->use_pmap = TRUE;
4368 }
4369 #else /* NO_NESTED_PMAP */
4370 pmap_remove(map->pmap, (addr64_t)start, (addr64_t)end);
4371 #endif /* NO_NESTED_PMAP */
4372 result = KERN_SUCCESS;
4373 }
4374 vm_map_unlock(map);
4375
4376 return(result);
4377 }
4378
4379 /*
4380 * vm_map_protect:
4381 *
4382 * Sets the protection of the specified address
4383 * region in the target map. If "set_max" is
4384 * specified, the maximum protection is to be set;
4385 * otherwise, only the current protection is affected.
4386 */
4387 kern_return_t
4388 vm_map_protect(
4389 register vm_map_t map,
4390 register vm_map_offset_t start,
4391 register vm_map_offset_t end,
4392 register vm_prot_t new_prot,
4393 register boolean_t set_max)
4394 {
4395 register vm_map_entry_t current;
4396 register vm_map_offset_t prev;
4397 vm_map_entry_t entry;
4398 vm_prot_t new_max;
4399
4400 XPR(XPR_VM_MAP,
4401 "vm_map_protect, 0x%X start 0x%X end 0x%X, new 0x%X %d",
4402 map, start, end, new_prot, set_max);
4403
4404 vm_map_lock(map);
4405
4406 /* LP64todo - remove this check when vm_map_commpage64()
4407 * no longer has to stuff in a map_entry for the commpage
4408 * above the map's max_offset.
4409 */
4410 if (start >= map->max_offset) {
4411 vm_map_unlock(map);
4412 return(KERN_INVALID_ADDRESS);
4413 }
4414
4415 while(1) {
4416 /*
4417 * Lookup the entry. If it doesn't start in a valid
4418 * entry, return an error.
4419 */
4420 if (! vm_map_lookup_entry(map, start, &entry)) {
4421 vm_map_unlock(map);
4422 return(KERN_INVALID_ADDRESS);
4423 }
4424
4425 if (entry->superpage_size && (start & (SUPERPAGE_SIZE-1))) { /* extend request to whole entry */
4426 start = SUPERPAGE_ROUND_DOWN(start);
4427 continue;
4428 }
4429 break;
4430 }
4431 if (entry->superpage_size)
4432 end = SUPERPAGE_ROUND_UP(end);
4433
4434 /*
4435 * Make a first pass to check for protection and address
4436 * violations.
4437 */
4438
4439 current = entry;
4440 prev = current->vme_start;
4441 while ((current != vm_map_to_entry(map)) &&
4442 (current->vme_start < end)) {
4443
4444 /*
4445 * If there is a hole, return an error.
4446 */
4447 if (current->vme_start != prev) {
4448 vm_map_unlock(map);
4449 return(KERN_INVALID_ADDRESS);
4450 }
4451
4452 new_max = current->max_protection;
4453 if(new_prot & VM_PROT_COPY) {
4454 new_max |= VM_PROT_WRITE;
4455 if ((new_prot & (new_max | VM_PROT_COPY)) != new_prot) {
4456 vm_map_unlock(map);
4457 return(KERN_PROTECTION_FAILURE);
4458 }
4459 } else {
4460 if ((new_prot & new_max) != new_prot) {
4461 vm_map_unlock(map);
4462 return(KERN_PROTECTION_FAILURE);
4463 }
4464 }
4465
4466
4467 prev = current->vme_end;
4468 current = current->vme_next;
4469 }
4470 if (end > prev) {
4471 vm_map_unlock(map);
4472 return(KERN_INVALID_ADDRESS);
4473 }
4474
4475 /*
4476 * Go back and fix up protections.
4477 * Clip to start here if the range starts within
4478 * the entry.
4479 */
4480
4481 current = entry;
4482 if (current != vm_map_to_entry(map)) {
4483 /* clip and unnest if necessary */
4484 vm_map_clip_start(map, current, start);
4485 }
4486
4487 while ((current != vm_map_to_entry(map)) &&
4488 (current->vme_start < end)) {
4489
4490 vm_prot_t old_prot;
4491
4492 vm_map_clip_end(map, current, end);
4493
4494 if (current->is_sub_map) {
4495 /* clipping did unnest if needed */
4496 assert(!current->use_pmap);
4497 }
4498
4499 old_prot = current->protection;
4500
4501 if(new_prot & VM_PROT_COPY) {
4502 /* caller is asking specifically to copy the */
4503 /* mapped data, this implies that max protection */
4504 /* will include write. Caller must be prepared */
4505 /* for loss of shared memory communication in the */
4506 /* target area after taking this step */
4507
4508 if (current->is_sub_map == FALSE &&
4509 VME_OBJECT(current) == VM_OBJECT_NULL) {
4510 VME_OBJECT_SET(current,
4511 vm_object_allocate(
4512 (vm_map_size_t)
4513 (current->vme_end -
4514 current->vme_start)));
4515 VME_OFFSET_SET(current, 0);
4516 assert(current->use_pmap);
4517 }
4518 assert(current->wired_count == 0);
4519 current->needs_copy = TRUE;
4520 current->max_protection |= VM_PROT_WRITE;
4521 }
4522
4523 if (set_max)
4524 current->protection =
4525 (current->max_protection =
4526 new_prot & ~VM_PROT_COPY) &
4527 old_prot;
4528 else
4529 current->protection = new_prot & ~VM_PROT_COPY;
4530
4531 /*
4532 * Update physical map if necessary.
4533 * If the request is to turn off write protection,
4534 * we won't do it for real (in pmap). This is because
4535 * it would cause copy-on-write to fail. We've already
4536 * set, the new protection in the map, so if a
4537 * write-protect fault occurred, it will be fixed up
4538 * properly, COW or not.
4539 */
4540 if (current->protection != old_prot) {
4541 /* Look one level in we support nested pmaps */
4542 /* from mapped submaps which are direct entries */
4543 /* in our map */
4544
4545 vm_prot_t prot;
4546
4547 prot = current->protection & ~VM_PROT_WRITE;
4548
4549 if (override_nx(map, VME_ALIAS(current)) && prot)
4550 prot |= VM_PROT_EXECUTE;
4551
4552 if (current->is_sub_map && current->use_pmap) {
4553 pmap_protect(VME_SUBMAP(current)->pmap,
4554 current->vme_start,
4555 current->vme_end,
4556 prot);
4557 } else {
4558 pmap_protect(map->pmap,
4559 current->vme_start,
4560 current->vme_end,
4561 prot);
4562 }
4563 }
4564 current = current->vme_next;
4565 }
4566
4567 current = entry;
4568 while ((current != vm_map_to_entry(map)) &&
4569 (current->vme_start <= end)) {
4570 vm_map_simplify_entry(map, current);
4571 current = current->vme_next;
4572 }
4573
4574 vm_map_unlock(map);
4575 return(KERN_SUCCESS);
4576 }
4577
4578 /*
4579 * vm_map_inherit:
4580 *
4581 * Sets the inheritance of the specified address
4582 * range in the target map. Inheritance
4583 * affects how the map will be shared with
4584 * child maps at the time of vm_map_fork.
4585 */
4586 kern_return_t
4587 vm_map_inherit(
4588 register vm_map_t map,
4589 register vm_map_offset_t start,
4590 register vm_map_offset_t end,
4591 register vm_inherit_t new_inheritance)
4592 {
4593 register vm_map_entry_t entry;
4594 vm_map_entry_t temp_entry;
4595
4596 vm_map_lock(map);
4597
4598 VM_MAP_RANGE_CHECK(map, start, end);
4599
4600 if (vm_map_lookup_entry(map, start, &temp_entry)) {
4601 entry = temp_entry;
4602 }
4603 else {
4604 temp_entry = temp_entry->vme_next;
4605 entry = temp_entry;
4606 }
4607
4608 /* first check entire range for submaps which can't support the */
4609 /* given inheritance. */
4610 while ((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
4611 if(entry->is_sub_map) {
4612 if(new_inheritance == VM_INHERIT_COPY) {
4613 vm_map_unlock(map);
4614 return(KERN_INVALID_ARGUMENT);
4615 }
4616 }
4617
4618 entry = entry->vme_next;
4619 }
4620
4621 entry = temp_entry;
4622 if (entry != vm_map_to_entry(map)) {
4623 /* clip and unnest if necessary */
4624 vm_map_clip_start(map, entry, start);
4625 }
4626
4627 while ((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
4628 vm_map_clip_end(map, entry, end);
4629 if (entry->is_sub_map) {
4630 /* clip did unnest if needed */
4631 assert(!entry->use_pmap);
4632 }
4633
4634 entry->inheritance = new_inheritance;
4635
4636 entry = entry->vme_next;
4637 }
4638
4639 vm_map_unlock(map);
4640 return(KERN_SUCCESS);
4641 }
4642
4643 /*
4644 * Update the accounting for the amount of wired memory in this map. If the user has
4645 * exceeded the defined limits, then we fail. Wiring on behalf of the kernel never fails.
4646 */
4647
4648 static kern_return_t
4649 add_wire_counts(
4650 vm_map_t map,
4651 vm_map_entry_t entry,
4652 boolean_t user_wire)
4653 {
4654 vm_map_size_t size;
4655
4656 if (user_wire) {
4657 unsigned int total_wire_count = vm_page_wire_count + vm_lopage_free_count;
4658
4659 /*
4660 * We're wiring memory at the request of the user. Check if this is the first time the user is wiring
4661 * this map entry.
4662 */
4663
4664 if (entry->user_wired_count == 0) {
4665 size = entry->vme_end - entry->vme_start;
4666
4667 /*
4668 * Since this is the first time the user is wiring this map entry, check to see if we're
4669 * exceeding the user wire limits. There is a per map limit which is the smaller of either
4670 * the process's rlimit or the global vm_user_wire_limit which caps this value. There is also
4671 * a system-wide limit on the amount of memory all users can wire. If the user is over either
4672 * limit, then we fail.
4673 */
4674
4675 if(size + map->user_wire_size > MIN(map->user_wire_limit, vm_user_wire_limit) ||
4676 size + ptoa_64(total_wire_count) > vm_global_user_wire_limit ||
4677 size + ptoa_64(total_wire_count) > max_mem - vm_global_no_user_wire_amount)
4678 return KERN_RESOURCE_SHORTAGE;
4679
4680 /*
4681 * The first time the user wires an entry, we also increment the wired_count and add this to
4682 * the total that has been wired in the map.
4683 */
4684
4685 if (entry->wired_count >= MAX_WIRE_COUNT)
4686 return KERN_FAILURE;
4687
4688 entry->wired_count++;
4689 map->user_wire_size += size;
4690 }
4691
4692 if (entry->user_wired_count >= MAX_WIRE_COUNT)
4693 return KERN_FAILURE;
4694
4695 entry->user_wired_count++;
4696
4697 } else {
4698
4699 /*
4700 * The kernel's wiring the memory. Just bump the count and continue.
4701 */
4702
4703 if (entry->wired_count >= MAX_WIRE_COUNT)
4704 panic("vm_map_wire: too many wirings");
4705
4706 entry->wired_count++;
4707 }
4708
4709 return KERN_SUCCESS;
4710 }
4711
4712 /*
4713 * Update the memory wiring accounting now that the given map entry is being unwired.
4714 */
4715
4716 static void
4717 subtract_wire_counts(
4718 vm_map_t map,
4719 vm_map_entry_t entry,
4720 boolean_t user_wire)
4721 {
4722
4723 if (user_wire) {
4724
4725 /*
4726 * We're unwiring memory at the request of the user. See if we're removing the last user wire reference.
4727 */
4728
4729 if (entry->user_wired_count == 1) {
4730
4731 /*
4732 * We're removing the last user wire reference. Decrement the wired_count and the total
4733 * user wired memory for this map.
4734 */
4735
4736 assert(entry->wired_count >= 1);
4737 entry->wired_count--;
4738 map->user_wire_size -= entry->vme_end - entry->vme_start;
4739 }
4740
4741 assert(entry->user_wired_count >= 1);
4742 entry->user_wired_count--;
4743
4744 } else {
4745
4746 /*
4747 * The kernel is unwiring the memory. Just update the count.
4748 */
4749
4750 assert(entry->wired_count >= 1);
4751 entry->wired_count--;
4752 }
4753 }
4754
4755 /*
4756 * vm_map_wire:
4757 *
4758 * Sets the pageability of the specified address range in the
4759 * target map as wired. Regions specified as not pageable require
4760 * locked-down physical memory and physical page maps. The
4761 * access_type variable indicates types of accesses that must not
4762 * generate page faults. This is checked against protection of
4763 * memory being locked-down.
4764 *
4765 * The map must not be locked, but a reference must remain to the
4766 * map throughout the call.
4767 */
4768 static kern_return_t
4769 vm_map_wire_nested(
4770 register vm_map_t map,
4771 register vm_map_offset_t start,
4772 register vm_map_offset_t end,
4773 register vm_prot_t caller_prot,
4774 boolean_t user_wire,
4775 pmap_t map_pmap,
4776 vm_map_offset_t pmap_addr,
4777 ppnum_t *physpage_p)
4778 {
4779 register vm_map_entry_t entry;
4780 register vm_prot_t access_type;
4781 struct vm_map_entry *first_entry, tmp_entry;
4782 vm_map_t real_map;
4783 register vm_map_offset_t s,e;
4784 kern_return_t rc;
4785 boolean_t need_wakeup;
4786 boolean_t main_map = FALSE;
4787 wait_interrupt_t interruptible_state;
4788 thread_t cur_thread;
4789 unsigned int last_timestamp;
4790 vm_map_size_t size;
4791 boolean_t wire_and_extract;
4792
4793 access_type = (caller_prot & VM_PROT_ALL);
4794
4795 wire_and_extract = FALSE;
4796 if (physpage_p != NULL) {
4797 /*
4798 * The caller wants the physical page number of the
4799 * wired page. We return only one physical page number
4800 * so this works for only one page at a time.
4801 */
4802 if ((end - start) != PAGE_SIZE) {
4803 return KERN_INVALID_ARGUMENT;
4804 }
4805 wire_and_extract = TRUE;
4806 *physpage_p = 0;
4807 }
4808
4809 vm_map_lock(map);
4810 if(map_pmap == NULL)
4811 main_map = TRUE;
4812 last_timestamp = map->timestamp;
4813
4814 VM_MAP_RANGE_CHECK(map, start, end);
4815 assert(page_aligned(start));
4816 assert(page_aligned(end));
4817 assert(VM_MAP_PAGE_ALIGNED(start, VM_MAP_PAGE_MASK(map)));
4818 assert(VM_MAP_PAGE_ALIGNED(end, VM_MAP_PAGE_MASK(map)));
4819 if (start == end) {
4820 /* We wired what the caller asked for, zero pages */
4821 vm_map_unlock(map);
4822 return KERN_SUCCESS;
4823 }
4824
4825 need_wakeup = FALSE;
4826 cur_thread = current_thread();
4827
4828 s = start;
4829 rc = KERN_SUCCESS;
4830
4831 if (vm_map_lookup_entry(map, s, &first_entry)) {
4832 entry = first_entry;
4833 /*
4834 * vm_map_clip_start will be done later.
4835 * We don't want to unnest any nested submaps here !
4836 */
4837 } else {
4838 /* Start address is not in map */
4839 rc = KERN_INVALID_ADDRESS;
4840 goto done;
4841 }
4842
4843 while ((entry != vm_map_to_entry(map)) && (s < end)) {
4844 /*
4845 * At this point, we have wired from "start" to "s".
4846 * We still need to wire from "s" to "end".
4847 *
4848 * "entry" hasn't been clipped, so it could start before "s"
4849 * and/or end after "end".
4850 */
4851
4852 /* "e" is how far we want to wire in this entry */
4853 e = entry->vme_end;
4854 if (e > end)
4855 e = end;
4856
4857 /*
4858 * If another thread is wiring/unwiring this entry then
4859 * block after informing other thread to wake us up.
4860 */
4861 if (entry->in_transition) {
4862 wait_result_t wait_result;
4863
4864 /*
4865 * We have not clipped the entry. Make sure that
4866 * the start address is in range so that the lookup
4867 * below will succeed.
4868 * "s" is the current starting point: we've already
4869 * wired from "start" to "s" and we still have
4870 * to wire from "s" to "end".
4871 */
4872
4873 entry->needs_wakeup = TRUE;
4874
4875 /*
4876 * wake up anybody waiting on entries that we have
4877 * already wired.
4878 */
4879 if (need_wakeup) {
4880 vm_map_entry_wakeup(map);
4881 need_wakeup = FALSE;
4882 }
4883 /*
4884 * User wiring is interruptible
4885 */
4886 wait_result = vm_map_entry_wait(map,
4887 (user_wire) ? THREAD_ABORTSAFE :
4888 THREAD_UNINT);
4889 if (user_wire && wait_result == THREAD_INTERRUPTED) {
4890 /*
4891 * undo the wirings we have done so far
4892 * We do not clear the needs_wakeup flag,
4893 * because we cannot tell if we were the
4894 * only one waiting.
4895 */
4896 rc = KERN_FAILURE;
4897 goto done;
4898 }
4899
4900 /*
4901 * Cannot avoid a lookup here. reset timestamp.
4902 */
4903 last_timestamp = map->timestamp;
4904
4905 /*
4906 * The entry could have been clipped, look it up again.
4907 * Worse that can happen is, it may not exist anymore.
4908 */
4909 if (!vm_map_lookup_entry(map, s, &first_entry)) {
4910 /*
4911 * User: undo everything upto the previous
4912 * entry. let vm_map_unwire worry about
4913 * checking the validity of the range.
4914 */
4915 rc = KERN_FAILURE;
4916 goto done;
4917 }
4918 entry = first_entry;
4919 continue;
4920 }
4921
4922 if (entry->is_sub_map) {
4923 vm_map_offset_t sub_start;
4924 vm_map_offset_t sub_end;
4925 vm_map_offset_t local_start;
4926 vm_map_offset_t local_end;
4927 pmap_t pmap;
4928
4929 if (wire_and_extract) {
4930 /*
4931 * Wiring would result in copy-on-write
4932 * which would not be compatible with
4933 * the sharing we have with the original
4934 * provider of this memory.
4935 */
4936 rc = KERN_INVALID_ARGUMENT;
4937 goto done;
4938 }
4939
4940 vm_map_clip_start(map, entry, s);
4941 vm_map_clip_end(map, entry, end);
4942
4943 sub_start = VME_OFFSET(entry);
4944 sub_end = entry->vme_end;
4945 sub_end += VME_OFFSET(entry) - entry->vme_start;
4946
4947 local_end = entry->vme_end;
4948 if(map_pmap == NULL) {
4949 vm_object_t object;
4950 vm_object_offset_t offset;
4951 vm_prot_t prot;
4952 boolean_t wired;
4953 vm_map_entry_t local_entry;
4954 vm_map_version_t version;
4955 vm_map_t lookup_map;
4956
4957 if(entry->use_pmap) {
4958 pmap = VME_SUBMAP(entry)->pmap;
4959 /* ppc implementation requires that */
4960 /* submaps pmap address ranges line */
4961 /* up with parent map */
4962 #ifdef notdef
4963 pmap_addr = sub_start;
4964 #endif
4965 pmap_addr = s;
4966 } else {
4967 pmap = map->pmap;
4968 pmap_addr = s;
4969 }
4970
4971 if (entry->wired_count) {
4972 if ((rc = add_wire_counts(map, entry, user_wire)) != KERN_SUCCESS)
4973 goto done;
4974
4975 /*
4976 * The map was not unlocked:
4977 * no need to goto re-lookup.
4978 * Just go directly to next entry.
4979 */
4980 entry = entry->vme_next;
4981 s = entry->vme_start;
4982 continue;
4983
4984 }
4985
4986 /* call vm_map_lookup_locked to */
4987 /* cause any needs copy to be */
4988 /* evaluated */
4989 local_start = entry->vme_start;
4990 lookup_map = map;
4991 vm_map_lock_write_to_read(map);
4992 if(vm_map_lookup_locked(
4993 &lookup_map, local_start,
4994 access_type,
4995 OBJECT_LOCK_EXCLUSIVE,
4996 &version, &object,
4997 &offset, &prot, &wired,
4998 NULL,
4999 &real_map)) {
5000
5001 vm_map_unlock_read(lookup_map);
5002 vm_map_unwire(map, start,
5003 s, user_wire);
5004 return(KERN_FAILURE);
5005 }
5006 vm_object_unlock(object);
5007 if(real_map != lookup_map)
5008 vm_map_unlock(real_map);
5009 vm_map_unlock_read(lookup_map);
5010 vm_map_lock(map);
5011
5012 /* we unlocked, so must re-lookup */
5013 if (!vm_map_lookup_entry(map,
5014 local_start,
5015 &local_entry)) {
5016 rc = KERN_FAILURE;
5017 goto done;
5018 }
5019
5020 /*
5021 * entry could have been "simplified",
5022 * so re-clip
5023 */
5024 entry = local_entry;
5025 assert(s == local_start);
5026 vm_map_clip_start(map, entry, s);
5027 vm_map_clip_end(map, entry, end);
5028 /* re-compute "e" */
5029 e = entry->vme_end;
5030 if (e > end)
5031 e = end;
5032
5033 /* did we have a change of type? */
5034 if (!entry->is_sub_map) {
5035 last_timestamp = map->timestamp;
5036 continue;
5037 }
5038 } else {
5039 local_start = entry->vme_start;
5040 pmap = map_pmap;
5041 }
5042
5043 if ((rc = add_wire_counts(map, entry, user_wire)) != KERN_SUCCESS)
5044 goto done;
5045
5046 entry->in_transition = TRUE;
5047
5048 vm_map_unlock(map);
5049 rc = vm_map_wire_nested(VME_SUBMAP(entry),
5050 sub_start, sub_end,
5051 caller_prot,
5052 user_wire, pmap, pmap_addr,
5053 NULL);
5054 vm_map_lock(map);
5055
5056 /*
5057 * Find the entry again. It could have been clipped
5058 * after we unlocked the map.
5059 */
5060 if (!vm_map_lookup_entry(map, local_start,
5061 &first_entry))
5062 panic("vm_map_wire: re-lookup failed");
5063 entry = first_entry;
5064
5065 assert(local_start == s);
5066 /* re-compute "e" */
5067 e = entry->vme_end;
5068 if (e > end)
5069 e = end;
5070
5071 last_timestamp = map->timestamp;
5072 while ((entry != vm_map_to_entry(map)) &&
5073 (entry->vme_start < e)) {
5074 assert(entry->in_transition);
5075 entry->in_transition = FALSE;
5076 if (entry->needs_wakeup) {
5077 entry->needs_wakeup = FALSE;
5078 need_wakeup = TRUE;
5079 }
5080 if (rc != KERN_SUCCESS) {/* from vm_*_wire */
5081 subtract_wire_counts(map, entry, user_wire);
5082 }
5083 entry = entry->vme_next;
5084 }
5085 if (rc != KERN_SUCCESS) { /* from vm_*_wire */
5086 goto done;
5087 }
5088
5089 /* no need to relookup again */
5090 s = entry->vme_start;
5091 continue;
5092 }
5093
5094 /*
5095 * If this entry is already wired then increment
5096 * the appropriate wire reference count.
5097 */
5098 if (entry->wired_count) {
5099
5100 if ((entry->protection & access_type) != access_type) {
5101 /* found a protection problem */
5102
5103 /*
5104 * XXX FBDP
5105 * We should always return an error
5106 * in this case but since we didn't
5107 * enforce it before, let's do
5108 * it only for the new "wire_and_extract"
5109 * code path for now...
5110 */
5111 if (wire_and_extract) {
5112 rc = KERN_PROTECTION_FAILURE;
5113 goto done;
5114 }
5115 }
5116
5117 /*
5118 * entry is already wired down, get our reference
5119 * after clipping to our range.
5120 */
5121 vm_map_clip_start(map, entry, s);
5122 vm_map_clip_end(map, entry, end);
5123
5124 if ((rc = add_wire_counts(map, entry, user_wire)) != KERN_SUCCESS)
5125 goto done;
5126
5127 if (wire_and_extract) {
5128 vm_object_t object;
5129 vm_object_offset_t offset;
5130 vm_page_t m;
5131
5132 /*
5133 * We don't have to "wire" the page again
5134 * bit we still have to "extract" its
5135 * physical page number, after some sanity
5136 * checks.
5137 */
5138 assert((entry->vme_end - entry->vme_start)
5139 == PAGE_SIZE);
5140 assert(!entry->needs_copy);
5141 assert(!entry->is_sub_map);
5142 assert(VME_OBJECT(entry));
5143 if (((entry->vme_end - entry->vme_start)
5144 != PAGE_SIZE) ||
5145 entry->needs_copy ||
5146 entry->is_sub_map ||
5147 VME_OBJECT(entry) == VM_OBJECT_NULL) {
5148 rc = KERN_INVALID_ARGUMENT;
5149 goto done;
5150 }
5151
5152 object = VME_OBJECT(entry);
5153 offset = VME_OFFSET(entry);
5154 /* need exclusive lock to update m->dirty */
5155 if (entry->protection & VM_PROT_WRITE) {
5156 vm_object_lock(object);
5157 } else {
5158 vm_object_lock_shared(object);
5159 }
5160 m = vm_page_lookup(object, offset);
5161 assert(m != VM_PAGE_NULL);
5162 assert(m->wire_count);
5163 if (m != VM_PAGE_NULL && m->wire_count) {
5164 *physpage_p = m->phys_page;
5165 if (entry->protection & VM_PROT_WRITE) {
5166 vm_object_lock_assert_exclusive(
5167 m->object);
5168 m->dirty = TRUE;
5169 }
5170 } else {
5171 /* not already wired !? */
5172 *physpage_p = 0;
5173 }
5174 vm_object_unlock(object);
5175 }
5176
5177 /* map was not unlocked: no need to relookup */
5178 entry = entry->vme_next;
5179 s = entry->vme_start;
5180 continue;
5181 }
5182
5183 /*
5184 * Unwired entry or wire request transmitted via submap
5185 */
5186
5187
5188 /*
5189 * Perform actions of vm_map_lookup that need the write
5190 * lock on the map: create a shadow object for a
5191 * copy-on-write region, or an object for a zero-fill
5192 * region.
5193 */
5194 size = entry->vme_end - entry->vme_start;
5195 /*
5196 * If wiring a copy-on-write page, we need to copy it now
5197 * even if we're only (currently) requesting read access.
5198 * This is aggressive, but once it's wired we can't move it.
5199 */
5200 if (entry->needs_copy) {
5201 if (wire_and_extract) {
5202 /*
5203 * We're supposed to share with the original
5204 * provider so should not be "needs_copy"
5205 */
5206 rc = KERN_INVALID_ARGUMENT;
5207 goto done;
5208 }
5209
5210 VME_OBJECT_SHADOW(entry, size);
5211 entry->needs_copy = FALSE;
5212 } else if (VME_OBJECT(entry) == VM_OBJECT_NULL) {
5213 if (wire_and_extract) {
5214 /*
5215 * We're supposed to share with the original
5216 * provider so should already have an object.
5217 */
5218 rc = KERN_INVALID_ARGUMENT;
5219 goto done;
5220 }
5221 VME_OBJECT_SET(entry, vm_object_allocate(size));
5222 VME_OFFSET_SET(entry, (vm_object_offset_t)0);
5223 assert(entry->use_pmap);
5224 }
5225
5226 vm_map_clip_start(map, entry, s);
5227 vm_map_clip_end(map, entry, end);
5228
5229 /* re-compute "e" */
5230 e = entry->vme_end;
5231 if (e > end)
5232 e = end;
5233
5234 /*
5235 * Check for holes and protection mismatch.
5236 * Holes: Next entry should be contiguous unless this
5237 * is the end of the region.
5238 * Protection: Access requested must be allowed, unless
5239 * wiring is by protection class
5240 */
5241 if ((entry->vme_end < end) &&
5242 ((entry->vme_next == vm_map_to_entry(map)) ||
5243 (entry->vme_next->vme_start > entry->vme_end))) {
5244 /* found a hole */
5245 rc = KERN_INVALID_ADDRESS;
5246 goto done;
5247 }
5248 if ((entry->protection & access_type) != access_type) {
5249 /* found a protection problem */
5250 rc = KERN_PROTECTION_FAILURE;
5251 goto done;
5252 }
5253
5254 assert(entry->wired_count == 0 && entry->user_wired_count == 0);
5255
5256 if ((rc = add_wire_counts(map, entry, user_wire)) != KERN_SUCCESS)
5257 goto done;
5258
5259 entry->in_transition = TRUE;
5260
5261 /*
5262 * This entry might get split once we unlock the map.
5263 * In vm_fault_wire(), we need the current range as
5264 * defined by this entry. In order for this to work
5265 * along with a simultaneous clip operation, we make a
5266 * temporary copy of this entry and use that for the
5267 * wiring. Note that the underlying objects do not
5268 * change during a clip.
5269 */
5270 tmp_entry = *entry;
5271
5272 /*
5273 * The in_transition state guarentees that the entry
5274 * (or entries for this range, if split occured) will be
5275 * there when the map lock is acquired for the second time.
5276 */
5277 vm_map_unlock(map);
5278
5279 if (!user_wire && cur_thread != THREAD_NULL)
5280 interruptible_state = thread_interrupt_level(THREAD_UNINT);
5281 else
5282 interruptible_state = THREAD_UNINT;
5283
5284 if(map_pmap)
5285 rc = vm_fault_wire(map,
5286 &tmp_entry, caller_prot, map_pmap, pmap_addr,
5287 physpage_p);
5288 else
5289 rc = vm_fault_wire(map,
5290 &tmp_entry, caller_prot, map->pmap,
5291 tmp_entry.vme_start,
5292 physpage_p);
5293
5294 if (!user_wire && cur_thread != THREAD_NULL)
5295 thread_interrupt_level(interruptible_state);
5296
5297 vm_map_lock(map);
5298
5299 if (last_timestamp+1 != map->timestamp) {
5300 /*
5301 * Find the entry again. It could have been clipped
5302 * after we unlocked the map.
5303 */
5304 if (!vm_map_lookup_entry(map, tmp_entry.vme_start,
5305 &first_entry))
5306 panic("vm_map_wire: re-lookup failed");
5307
5308 entry = first_entry;
5309 }
5310
5311 last_timestamp = map->timestamp;
5312
5313 while ((entry != vm_map_to_entry(map)) &&
5314 (entry->vme_start < tmp_entry.vme_end)) {
5315 assert(entry->in_transition);
5316 entry->in_transition = FALSE;
5317 if (entry->needs_wakeup) {
5318 entry->needs_wakeup = FALSE;
5319 need_wakeup = TRUE;
5320 }
5321 if (rc != KERN_SUCCESS) { /* from vm_*_wire */
5322 subtract_wire_counts(map, entry, user_wire);
5323 }
5324 entry = entry->vme_next;
5325 }
5326
5327 if (rc != KERN_SUCCESS) { /* from vm_*_wire */
5328 goto done;
5329 }
5330
5331 s = entry->vme_start;
5332 } /* end while loop through map entries */
5333
5334 done:
5335 if (rc == KERN_SUCCESS) {
5336 /* repair any damage we may have made to the VM map */
5337 vm_map_simplify_range(map, start, end);
5338 }
5339
5340 vm_map_unlock(map);
5341
5342 /*
5343 * wake up anybody waiting on entries we wired.
5344 */
5345 if (need_wakeup)
5346 vm_map_entry_wakeup(map);
5347
5348 if (rc != KERN_SUCCESS) {
5349 /* undo what has been wired so far */
5350 vm_map_unwire(map, start, s, user_wire);
5351 if (physpage_p) {
5352 *physpage_p = 0;
5353 }
5354 }
5355
5356 return rc;
5357
5358 }
5359
5360 kern_return_t
5361 vm_map_wire_external(
5362 register vm_map_t map,
5363 register vm_map_offset_t start,
5364 register vm_map_offset_t end,
5365 register vm_prot_t caller_prot,
5366 boolean_t user_wire)
5367 {
5368 kern_return_t kret;
5369
5370 caller_prot &= ~VM_PROT_MEMORY_TAG_MASK;
5371 caller_prot |= VM_PROT_MEMORY_TAG_MAKE(vm_tag_bt());
5372 kret = vm_map_wire_nested(map, start, end, caller_prot,
5373 user_wire, (pmap_t)NULL, 0, NULL);
5374 return kret;
5375 }
5376
5377 kern_return_t
5378 vm_map_wire(
5379 register vm_map_t map,
5380 register vm_map_offset_t start,
5381 register vm_map_offset_t end,
5382 register vm_prot_t caller_prot,
5383 boolean_t user_wire)
5384 {
5385 kern_return_t kret;
5386
5387 kret = vm_map_wire_nested(map, start, end, caller_prot,
5388 user_wire, (pmap_t)NULL, 0, NULL);
5389 return kret;
5390 }
5391
5392 kern_return_t
5393 vm_map_wire_and_extract_external(
5394 vm_map_t map,
5395 vm_map_offset_t start,
5396 vm_prot_t caller_prot,
5397 boolean_t user_wire,
5398 ppnum_t *physpage_p)
5399 {
5400 kern_return_t kret;
5401
5402 caller_prot &= ~VM_PROT_MEMORY_TAG_MASK;
5403 caller_prot |= VM_PROT_MEMORY_TAG_MAKE(vm_tag_bt());
5404 kret = vm_map_wire_nested(map,
5405 start,
5406 start+VM_MAP_PAGE_SIZE(map),
5407 caller_prot,
5408 user_wire,
5409 (pmap_t)NULL,
5410 0,
5411 physpage_p);
5412 if (kret != KERN_SUCCESS &&
5413 physpage_p != NULL) {
5414 *physpage_p = 0;
5415 }
5416 return kret;
5417 }
5418
5419 kern_return_t
5420 vm_map_wire_and_extract(
5421 vm_map_t map,
5422 vm_map_offset_t start,
5423 vm_prot_t caller_prot,
5424 boolean_t user_wire,
5425 ppnum_t *physpage_p)
5426 {
5427 kern_return_t kret;
5428
5429 kret = vm_map_wire_nested(map,
5430 start,
5431 start+VM_MAP_PAGE_SIZE(map),
5432 caller_prot,
5433 user_wire,
5434 (pmap_t)NULL,
5435 0,
5436 physpage_p);
5437 if (kret != KERN_SUCCESS &&
5438 physpage_p != NULL) {
5439 *physpage_p = 0;
5440 }
5441 return kret;
5442 }
5443
5444 /*
5445 * vm_map_unwire:
5446 *
5447 * Sets the pageability of the specified address range in the target
5448 * as pageable. Regions specified must have been wired previously.
5449 *
5450 * The map must not be locked, but a reference must remain to the map
5451 * throughout the call.
5452 *
5453 * Kernel will panic on failures. User unwire ignores holes and
5454 * unwired and intransition entries to avoid losing memory by leaving
5455 * it unwired.
5456 */
5457 static kern_return_t
5458 vm_map_unwire_nested(
5459 register vm_map_t map,
5460 register vm_map_offset_t start,
5461 register vm_map_offset_t end,
5462 boolean_t user_wire,
5463 pmap_t map_pmap,
5464 vm_map_offset_t pmap_addr)
5465 {
5466 register vm_map_entry_t entry;
5467 struct vm_map_entry *first_entry, tmp_entry;
5468 boolean_t need_wakeup;
5469 boolean_t main_map = FALSE;
5470 unsigned int last_timestamp;
5471
5472 vm_map_lock(map);
5473 if(map_pmap == NULL)
5474 main_map = TRUE;
5475 last_timestamp = map->timestamp;
5476
5477 VM_MAP_RANGE_CHECK(map, start, end);
5478 assert(page_aligned(start));
5479 assert(page_aligned(end));
5480 assert(VM_MAP_PAGE_ALIGNED(start, VM_MAP_PAGE_MASK(map)));
5481 assert(VM_MAP_PAGE_ALIGNED(end, VM_MAP_PAGE_MASK(map)));
5482
5483 if (start == end) {
5484 /* We unwired what the caller asked for: zero pages */
5485 vm_map_unlock(map);
5486 return KERN_SUCCESS;
5487 }
5488
5489 if (vm_map_lookup_entry(map, start, &first_entry)) {
5490 entry = first_entry;
5491 /*
5492 * vm_map_clip_start will be done later.
5493 * We don't want to unnest any nested sub maps here !
5494 */
5495 }
5496 else {
5497 if (!user_wire) {
5498 panic("vm_map_unwire: start not found");
5499 }
5500 /* Start address is not in map. */
5501 vm_map_unlock(map);
5502 return(KERN_INVALID_ADDRESS);
5503 }
5504
5505 if (entry->superpage_size) {
5506 /* superpages are always wired */
5507 vm_map_unlock(map);
5508 return KERN_INVALID_ADDRESS;
5509 }
5510
5511 need_wakeup = FALSE;
5512 while ((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
5513 if (entry->in_transition) {
5514 /*
5515 * 1)
5516 * Another thread is wiring down this entry. Note
5517 * that if it is not for the other thread we would
5518 * be unwiring an unwired entry. This is not
5519 * permitted. If we wait, we will be unwiring memory
5520 * we did not wire.
5521 *
5522 * 2)
5523 * Another thread is unwiring this entry. We did not
5524 * have a reference to it, because if we did, this
5525 * entry will not be getting unwired now.
5526 */
5527 if (!user_wire) {
5528 /*
5529 * XXX FBDP
5530 * This could happen: there could be some
5531 * overlapping vslock/vsunlock operations
5532 * going on.
5533 * We should probably just wait and retry,
5534 * but then we have to be careful that this
5535 * entry could get "simplified" after
5536 * "in_transition" gets unset and before
5537 * we re-lookup the entry, so we would
5538 * have to re-clip the entry to avoid
5539 * re-unwiring what we have already unwired...
5540 * See vm_map_wire_nested().
5541 *
5542 * Or we could just ignore "in_transition"
5543 * here and proceed to decement the wired
5544 * count(s) on this entry. That should be fine
5545 * as long as "wired_count" doesn't drop all
5546 * the way to 0 (and we should panic if THAT
5547 * happens).
5548 */
5549 panic("vm_map_unwire: in_transition entry");
5550 }
5551
5552 entry = entry->vme_next;
5553 continue;
5554 }
5555
5556 if (entry->is_sub_map) {
5557 vm_map_offset_t sub_start;
5558 vm_map_offset_t sub_end;
5559 vm_map_offset_t local_end;
5560 pmap_t pmap;
5561
5562 vm_map_clip_start(map, entry, start);
5563 vm_map_clip_end(map, entry, end);
5564
5565 sub_start = VME_OFFSET(entry);
5566 sub_end = entry->vme_end - entry->vme_start;
5567 sub_end += VME_OFFSET(entry);
5568 local_end = entry->vme_end;
5569 if(map_pmap == NULL) {
5570 if(entry->use_pmap) {
5571 pmap = VME_SUBMAP(entry)->pmap;
5572 pmap_addr = sub_start;
5573 } else {
5574 pmap = map->pmap;
5575 pmap_addr = start;
5576 }
5577 if (entry->wired_count == 0 ||
5578 (user_wire && entry->user_wired_count == 0)) {
5579 if (!user_wire)
5580 panic("vm_map_unwire: entry is unwired");
5581 entry = entry->vme_next;
5582 continue;
5583 }
5584
5585 /*
5586 * Check for holes
5587 * Holes: Next entry should be contiguous unless
5588 * this is the end of the region.
5589 */
5590 if (((entry->vme_end < end) &&
5591 ((entry->vme_next == vm_map_to_entry(map)) ||
5592 (entry->vme_next->vme_start
5593 > entry->vme_end)))) {
5594 if (!user_wire)
5595 panic("vm_map_unwire: non-contiguous region");
5596 /*
5597 entry = entry->vme_next;
5598 continue;
5599 */
5600 }
5601
5602 subtract_wire_counts(map, entry, user_wire);
5603
5604 if (entry->wired_count != 0) {
5605 entry = entry->vme_next;
5606 continue;
5607 }
5608
5609 entry->in_transition = TRUE;
5610 tmp_entry = *entry;/* see comment in vm_map_wire() */
5611
5612 /*
5613 * We can unlock the map now. The in_transition state
5614 * guarantees existance of the entry.
5615 */
5616 vm_map_unlock(map);
5617 vm_map_unwire_nested(VME_SUBMAP(entry),
5618 sub_start, sub_end, user_wire, pmap, pmap_addr);
5619 vm_map_lock(map);
5620
5621 if (last_timestamp+1 != map->timestamp) {
5622 /*
5623 * Find the entry again. It could have been
5624 * clipped or deleted after we unlocked the map.
5625 */
5626 if (!vm_map_lookup_entry(map,
5627 tmp_entry.vme_start,
5628 &first_entry)) {
5629 if (!user_wire)
5630 panic("vm_map_unwire: re-lookup failed");
5631 entry = first_entry->vme_next;
5632 } else
5633 entry = first_entry;
5634 }
5635 last_timestamp = map->timestamp;
5636
5637 /*
5638 * clear transition bit for all constituent entries
5639 * that were in the original entry (saved in
5640 * tmp_entry). Also check for waiters.
5641 */
5642 while ((entry != vm_map_to_entry(map)) &&
5643 (entry->vme_start < tmp_entry.vme_end)) {
5644 assert(entry->in_transition);
5645 entry->in_transition = FALSE;
5646 if (entry->needs_wakeup) {
5647 entry->needs_wakeup = FALSE;
5648 need_wakeup = TRUE;
5649 }
5650 entry = entry->vme_next;
5651 }
5652 continue;
5653 } else {
5654 vm_map_unlock(map);
5655 vm_map_unwire_nested(VME_SUBMAP(entry),
5656 sub_start, sub_end, user_wire, map_pmap,
5657 pmap_addr);
5658 vm_map_lock(map);
5659
5660 if (last_timestamp+1 != map->timestamp) {
5661 /*
5662 * Find the entry again. It could have been
5663 * clipped or deleted after we unlocked the map.
5664 */
5665 if (!vm_map_lookup_entry(map,
5666 tmp_entry.vme_start,
5667 &first_entry)) {
5668 if (!user_wire)
5669 panic("vm_map_unwire: re-lookup failed");
5670 entry = first_entry->vme_next;
5671 } else
5672 entry = first_entry;
5673 }
5674 last_timestamp = map->timestamp;
5675 }
5676 }
5677
5678
5679 if ((entry->wired_count == 0) ||
5680 (user_wire && entry->user_wired_count == 0)) {
5681 if (!user_wire)
5682 panic("vm_map_unwire: entry is unwired");
5683
5684 entry = entry->vme_next;
5685 continue;
5686 }
5687
5688 assert(entry->wired_count > 0 &&
5689 (!user_wire || entry->user_wired_count > 0));
5690
5691 vm_map_clip_start(map, entry, start);
5692 vm_map_clip_end(map, entry, end);
5693
5694 /*
5695 * Check for holes
5696 * Holes: Next entry should be contiguous unless
5697 * this is the end of the region.
5698 */
5699 if (((entry->vme_end < end) &&
5700 ((entry->vme_next == vm_map_to_entry(map)) ||
5701 (entry->vme_next->vme_start > entry->vme_end)))) {
5702
5703 if (!user_wire)
5704 panic("vm_map_unwire: non-contiguous region");
5705 entry = entry->vme_next;
5706 continue;
5707 }
5708
5709 subtract_wire_counts(map, entry, user_wire);
5710
5711 if (entry->wired_count != 0) {
5712 entry = entry->vme_next;
5713 continue;
5714 }
5715
5716 if(entry->zero_wired_pages) {
5717 entry->zero_wired_pages = FALSE;
5718 }
5719
5720 entry->in_transition = TRUE;
5721 tmp_entry = *entry; /* see comment in vm_map_wire() */
5722
5723 /*
5724 * We can unlock the map now. The in_transition state
5725 * guarantees existance of the entry.
5726 */
5727 vm_map_unlock(map);
5728 if(map_pmap) {
5729 vm_fault_unwire(map,
5730 &tmp_entry, FALSE, map_pmap, pmap_addr);
5731 } else {
5732 vm_fault_unwire(map,
5733 &tmp_entry, FALSE, map->pmap,
5734 tmp_entry.vme_start);
5735 }
5736 vm_map_lock(map);
5737
5738 if (last_timestamp+1 != map->timestamp) {
5739 /*
5740 * Find the entry again. It could have been clipped
5741 * or deleted after we unlocked the map.
5742 */
5743 if (!vm_map_lookup_entry(map, tmp_entry.vme_start,
5744 &first_entry)) {
5745 if (!user_wire)
5746 panic("vm_map_unwire: re-lookup failed");
5747 entry = first_entry->vme_next;
5748 } else
5749 entry = first_entry;
5750 }
5751 last_timestamp = map->timestamp;
5752
5753 /*
5754 * clear transition bit for all constituent entries that
5755 * were in the original entry (saved in tmp_entry). Also
5756 * check for waiters.
5757 */
5758 while ((entry != vm_map_to_entry(map)) &&
5759 (entry->vme_start < tmp_entry.vme_end)) {
5760 assert(entry->in_transition);
5761 entry->in_transition = FALSE;
5762 if (entry->needs_wakeup) {
5763 entry->needs_wakeup = FALSE;
5764 need_wakeup = TRUE;
5765 }
5766 entry = entry->vme_next;
5767 }
5768 }
5769
5770 /*
5771 * We might have fragmented the address space when we wired this
5772 * range of addresses. Attempt to re-coalesce these VM map entries
5773 * with their neighbors now that they're no longer wired.
5774 * Under some circumstances, address space fragmentation can
5775 * prevent VM object shadow chain collapsing, which can cause
5776 * swap space leaks.
5777 */
5778 vm_map_simplify_range(map, start, end);
5779
5780 vm_map_unlock(map);
5781 /*
5782 * wake up anybody waiting on entries that we have unwired.
5783 */
5784 if (need_wakeup)
5785 vm_map_entry_wakeup(map);
5786 return(KERN_SUCCESS);
5787
5788 }
5789
5790 kern_return_t
5791 vm_map_unwire(
5792 register vm_map_t map,
5793 register vm_map_offset_t start,
5794 register vm_map_offset_t end,
5795 boolean_t user_wire)
5796 {
5797 return vm_map_unwire_nested(map, start, end,
5798 user_wire, (pmap_t)NULL, 0);
5799 }
5800
5801
5802 /*
5803 * vm_map_entry_delete: [ internal use only ]
5804 *
5805 * Deallocate the given entry from the target map.
5806 */
5807 static void
5808 vm_map_entry_delete(
5809 register vm_map_t map,
5810 register vm_map_entry_t entry)
5811 {
5812 register vm_map_offset_t s, e;
5813 register vm_object_t object;
5814 register vm_map_t submap;
5815
5816 s = entry->vme_start;
5817 e = entry->vme_end;
5818 assert(page_aligned(s));
5819 assert(page_aligned(e));
5820 if (entry->map_aligned == TRUE) {
5821 assert(VM_MAP_PAGE_ALIGNED(s, VM_MAP_PAGE_MASK(map)));
5822 assert(VM_MAP_PAGE_ALIGNED(e, VM_MAP_PAGE_MASK(map)));
5823 }
5824 assert(entry->wired_count == 0);
5825 assert(entry->user_wired_count == 0);
5826 assert(!entry->permanent);
5827
5828 if (entry->is_sub_map) {
5829 object = NULL;
5830 submap = VME_SUBMAP(entry);
5831 } else {
5832 submap = NULL;
5833 object = VME_OBJECT(entry);
5834 }
5835
5836 vm_map_store_entry_unlink(map, entry);
5837 map->size -= e - s;
5838
5839 vm_map_entry_dispose(map, entry);
5840
5841 vm_map_unlock(map);
5842 /*
5843 * Deallocate the object only after removing all
5844 * pmap entries pointing to its pages.
5845 */
5846 if (submap)
5847 vm_map_deallocate(submap);
5848 else
5849 vm_object_deallocate(object);
5850
5851 }
5852
5853 void
5854 vm_map_submap_pmap_clean(
5855 vm_map_t map,
5856 vm_map_offset_t start,
5857 vm_map_offset_t end,
5858 vm_map_t sub_map,
5859 vm_map_offset_t offset)
5860 {
5861 vm_map_offset_t submap_start;
5862 vm_map_offset_t submap_end;
5863 vm_map_size_t remove_size;
5864 vm_map_entry_t entry;
5865
5866 submap_end = offset + (end - start);
5867 submap_start = offset;
5868
5869 vm_map_lock_read(sub_map);
5870 if(vm_map_lookup_entry(sub_map, offset, &entry)) {
5871
5872 remove_size = (entry->vme_end - entry->vme_start);
5873 if(offset > entry->vme_start)
5874 remove_size -= offset - entry->vme_start;
5875
5876
5877 if(submap_end < entry->vme_end) {
5878 remove_size -=
5879 entry->vme_end - submap_end;
5880 }
5881 if(entry->is_sub_map) {
5882 vm_map_submap_pmap_clean(
5883 sub_map,
5884 start,
5885 start + remove_size,
5886 VME_SUBMAP(entry),
5887 VME_OFFSET(entry));
5888 } else {
5889
5890 if((map->mapped_in_other_pmaps) && (map->ref_count)
5891 && (VME_OBJECT(entry) != NULL)) {
5892 vm_object_pmap_protect_options(
5893 VME_OBJECT(entry),
5894 (VME_OFFSET(entry) +
5895 offset -
5896 entry->vme_start),
5897 remove_size,
5898 PMAP_NULL,
5899 entry->vme_start,
5900 VM_PROT_NONE,
5901 PMAP_OPTIONS_REMOVE);
5902 } else {
5903 pmap_remove(map->pmap,
5904 (addr64_t)start,
5905 (addr64_t)(start + remove_size));
5906 }
5907 }
5908 }
5909
5910 entry = entry->vme_next;
5911
5912 while((entry != vm_map_to_entry(sub_map))
5913 && (entry->vme_start < submap_end)) {
5914 remove_size = (entry->vme_end - entry->vme_start);
5915 if(submap_end < entry->vme_end) {
5916 remove_size -= entry->vme_end - submap_end;
5917 }
5918 if(entry->is_sub_map) {
5919 vm_map_submap_pmap_clean(
5920 sub_map,
5921 (start + entry->vme_start) - offset,
5922 ((start + entry->vme_start) - offset) + remove_size,
5923 VME_SUBMAP(entry),
5924 VME_OFFSET(entry));
5925 } else {
5926 if((map->mapped_in_other_pmaps) && (map->ref_count)
5927 && (VME_OBJECT(entry) != NULL)) {
5928 vm_object_pmap_protect_options(
5929 VME_OBJECT(entry),
5930 VME_OFFSET(entry),
5931 remove_size,
5932 PMAP_NULL,
5933 entry->vme_start,
5934 VM_PROT_NONE,
5935 PMAP_OPTIONS_REMOVE);
5936 } else {
5937 pmap_remove(map->pmap,
5938 (addr64_t)((start + entry->vme_start)
5939 - offset),
5940 (addr64_t)(((start + entry->vme_start)
5941 - offset) + remove_size));
5942 }
5943 }
5944 entry = entry->vme_next;
5945 }
5946 vm_map_unlock_read(sub_map);
5947 return;
5948 }
5949
5950 /*
5951 * vm_map_delete: [ internal use only ]
5952 *
5953 * Deallocates the given address range from the target map.
5954 * Removes all user wirings. Unwires one kernel wiring if
5955 * VM_MAP_REMOVE_KUNWIRE is set. Waits for kernel wirings to go
5956 * away if VM_MAP_REMOVE_WAIT_FOR_KWIRE is set. Sleeps
5957 * interruptibly if VM_MAP_REMOVE_INTERRUPTIBLE is set.
5958 *
5959 * This routine is called with map locked and leaves map locked.
5960 */
5961 static kern_return_t
5962 vm_map_delete(
5963 vm_map_t map,
5964 vm_map_offset_t start,
5965 vm_map_offset_t end,
5966 int flags,
5967 vm_map_t zap_map)
5968 {
5969 vm_map_entry_t entry, next;
5970 struct vm_map_entry *first_entry, tmp_entry;
5971 register vm_map_offset_t s;
5972 register vm_object_t object;
5973 boolean_t need_wakeup;
5974 unsigned int last_timestamp = ~0; /* unlikely value */
5975 int interruptible;
5976
5977 interruptible = (flags & VM_MAP_REMOVE_INTERRUPTIBLE) ?
5978 THREAD_ABORTSAFE : THREAD_UNINT;
5979
5980 /*
5981 * All our DMA I/O operations in IOKit are currently done by
5982 * wiring through the map entries of the task requesting the I/O.
5983 * Because of this, we must always wait for kernel wirings
5984 * to go away on the entries before deleting them.
5985 *
5986 * Any caller who wants to actually remove a kernel wiring
5987 * should explicitly set the VM_MAP_REMOVE_KUNWIRE flag to
5988 * properly remove one wiring instead of blasting through
5989 * them all.
5990 */
5991 flags |= VM_MAP_REMOVE_WAIT_FOR_KWIRE;
5992
5993 while(1) {
5994 /*
5995 * Find the start of the region, and clip it
5996 */
5997 if (vm_map_lookup_entry(map, start, &first_entry)) {
5998 entry = first_entry;
5999 if (map == kalloc_map &&
6000 (entry->vme_start != start ||
6001 entry->vme_end != end)) {
6002 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6003 "mismatched entry %p [0x%llx:0x%llx]\n",
6004 map,
6005 (uint64_t)start,
6006 (uint64_t)end,
6007 entry,
6008 (uint64_t)entry->vme_start,
6009 (uint64_t)entry->vme_end);
6010 }
6011 if (entry->superpage_size && (start & ~SUPERPAGE_MASK)) { /* extend request to whole entry */ start = SUPERPAGE_ROUND_DOWN(start);
6012 start = SUPERPAGE_ROUND_DOWN(start);
6013 continue;
6014 }
6015 if (start == entry->vme_start) {
6016 /*
6017 * No need to clip. We don't want to cause
6018 * any unnecessary unnesting in this case...
6019 */
6020 } else {
6021 if ((flags & VM_MAP_REMOVE_NO_MAP_ALIGN) &&
6022 entry->map_aligned &&
6023 !VM_MAP_PAGE_ALIGNED(
6024 start,
6025 VM_MAP_PAGE_MASK(map))) {
6026 /*
6027 * The entry will no longer be
6028 * map-aligned after clipping
6029 * and the caller said it's OK.
6030 */
6031 entry->map_aligned = FALSE;
6032 }
6033 if (map == kalloc_map) {
6034 panic("vm_map_delete(%p,0x%llx,0x%llx):"
6035 " clipping %p at 0x%llx\n",
6036 map,
6037 (uint64_t)start,
6038 (uint64_t)end,
6039 entry,
6040 (uint64_t)start);
6041 }
6042 vm_map_clip_start(map, entry, start);
6043 }
6044
6045 /*
6046 * Fix the lookup hint now, rather than each
6047 * time through the loop.
6048 */
6049 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
6050 } else {
6051 if (map->pmap == kernel_pmap &&
6052 map->ref_count != 0) {
6053 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6054 "no map entry at 0x%llx\n",
6055 map,
6056 (uint64_t)start,
6057 (uint64_t)end,
6058 (uint64_t)start);
6059 }
6060 entry = first_entry->vme_next;
6061 }
6062 break;
6063 }
6064 if (entry->superpage_size)
6065 end = SUPERPAGE_ROUND_UP(end);
6066
6067 need_wakeup = FALSE;
6068 /*
6069 * Step through all entries in this region
6070 */
6071 s = entry->vme_start;
6072 while ((entry != vm_map_to_entry(map)) && (s < end)) {
6073 /*
6074 * At this point, we have deleted all the memory entries
6075 * between "start" and "s". We still need to delete
6076 * all memory entries between "s" and "end".
6077 * While we were blocked and the map was unlocked, some
6078 * new memory entries could have been re-allocated between
6079 * "start" and "s" and we don't want to mess with those.
6080 * Some of those entries could even have been re-assembled
6081 * with an entry after "s" (in vm_map_simplify_entry()), so
6082 * we may have to vm_map_clip_start() again.
6083 */
6084
6085 if (entry->vme_start >= s) {
6086 /*
6087 * This entry starts on or after "s"
6088 * so no need to clip its start.
6089 */
6090 } else {
6091 /*
6092 * This entry has been re-assembled by a
6093 * vm_map_simplify_entry(). We need to
6094 * re-clip its start.
6095 */
6096 if ((flags & VM_MAP_REMOVE_NO_MAP_ALIGN) &&
6097 entry->map_aligned &&
6098 !VM_MAP_PAGE_ALIGNED(s,
6099 VM_MAP_PAGE_MASK(map))) {
6100 /*
6101 * The entry will no longer be map-aligned
6102 * after clipping and the caller said it's OK.
6103 */
6104 entry->map_aligned = FALSE;
6105 }
6106 if (map == kalloc_map) {
6107 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6108 "clipping %p at 0x%llx\n",
6109 map,
6110 (uint64_t)start,
6111 (uint64_t)end,
6112 entry,
6113 (uint64_t)s);
6114 }
6115 vm_map_clip_start(map, entry, s);
6116 }
6117 if (entry->vme_end <= end) {
6118 /*
6119 * This entry is going away completely, so no need
6120 * to clip and possibly cause an unnecessary unnesting.
6121 */
6122 } else {
6123 if ((flags & VM_MAP_REMOVE_NO_MAP_ALIGN) &&
6124 entry->map_aligned &&
6125 !VM_MAP_PAGE_ALIGNED(end,
6126 VM_MAP_PAGE_MASK(map))) {
6127 /*
6128 * The entry will no longer be map-aligned
6129 * after clipping and the caller said it's OK.
6130 */
6131 entry->map_aligned = FALSE;
6132 }
6133 if (map == kalloc_map) {
6134 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6135 "clipping %p at 0x%llx\n",
6136 map,
6137 (uint64_t)start,
6138 (uint64_t)end,
6139 entry,
6140 (uint64_t)end);
6141 }
6142 vm_map_clip_end(map, entry, end);
6143 }
6144
6145 if (entry->permanent) {
6146 panic("attempt to remove permanent VM map entry "
6147 "%p [0x%llx:0x%llx]\n",
6148 entry, (uint64_t) s, (uint64_t) end);
6149 }
6150
6151
6152 if (entry->in_transition) {
6153 wait_result_t wait_result;
6154
6155 /*
6156 * Another thread is wiring/unwiring this entry.
6157 * Let the other thread know we are waiting.
6158 */
6159 assert(s == entry->vme_start);
6160 entry->needs_wakeup = TRUE;
6161
6162 /*
6163 * wake up anybody waiting on entries that we have
6164 * already unwired/deleted.
6165 */
6166 if (need_wakeup) {
6167 vm_map_entry_wakeup(map);
6168 need_wakeup = FALSE;
6169 }
6170
6171 wait_result = vm_map_entry_wait(map, interruptible);
6172
6173 if (interruptible &&
6174 wait_result == THREAD_INTERRUPTED) {
6175 /*
6176 * We do not clear the needs_wakeup flag,
6177 * since we cannot tell if we were the only one.
6178 */
6179 return KERN_ABORTED;
6180 }
6181
6182 /*
6183 * The entry could have been clipped or it
6184 * may not exist anymore. Look it up again.
6185 */
6186 if (!vm_map_lookup_entry(map, s, &first_entry)) {
6187 /*
6188 * User: use the next entry
6189 */
6190 entry = first_entry->vme_next;
6191 s = entry->vme_start;
6192 } else {
6193 entry = first_entry;
6194 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
6195 }
6196 last_timestamp = map->timestamp;
6197 continue;
6198 } /* end in_transition */
6199
6200 if (entry->wired_count) {
6201 boolean_t user_wire;
6202
6203 user_wire = entry->user_wired_count > 0;
6204
6205 /*
6206 * Remove a kernel wiring if requested
6207 */
6208 if (flags & VM_MAP_REMOVE_KUNWIRE) {
6209 entry->wired_count--;
6210 }
6211
6212 /*
6213 * Remove all user wirings for proper accounting
6214 */
6215 if (entry->user_wired_count > 0) {
6216 while (entry->user_wired_count)
6217 subtract_wire_counts(map, entry, user_wire);
6218 }
6219
6220 if (entry->wired_count != 0) {
6221 assert(map != kernel_map);
6222 /*
6223 * Cannot continue. Typical case is when
6224 * a user thread has physical io pending on
6225 * on this page. Either wait for the
6226 * kernel wiring to go away or return an
6227 * error.
6228 */
6229 if (flags & VM_MAP_REMOVE_WAIT_FOR_KWIRE) {
6230 wait_result_t wait_result;
6231
6232 assert(s == entry->vme_start);
6233 entry->needs_wakeup = TRUE;
6234 wait_result = vm_map_entry_wait(map,
6235 interruptible);
6236
6237 if (interruptible &&
6238 wait_result == THREAD_INTERRUPTED) {
6239 /*
6240 * We do not clear the
6241 * needs_wakeup flag, since we
6242 * cannot tell if we were the
6243 * only one.
6244 */
6245 return KERN_ABORTED;
6246 }
6247
6248 /*
6249 * The entry could have been clipped or
6250 * it may not exist anymore. Look it
6251 * up again.
6252 */
6253 if (!vm_map_lookup_entry(map, s,
6254 &first_entry)) {
6255 assert(map != kernel_map);
6256 /*
6257 * User: use the next entry
6258 */
6259 entry = first_entry->vme_next;
6260 s = entry->vme_start;
6261 } else {
6262 entry = first_entry;
6263 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
6264 }
6265 last_timestamp = map->timestamp;
6266 continue;
6267 }
6268 else {
6269 return KERN_FAILURE;
6270 }
6271 }
6272
6273 entry->in_transition = TRUE;
6274 /*
6275 * copy current entry. see comment in vm_map_wire()
6276 */
6277 tmp_entry = *entry;
6278 assert(s == entry->vme_start);
6279
6280 /*
6281 * We can unlock the map now. The in_transition
6282 * state guarentees existance of the entry.
6283 */
6284 vm_map_unlock(map);
6285
6286 if (tmp_entry.is_sub_map) {
6287 vm_map_t sub_map;
6288 vm_map_offset_t sub_start, sub_end;
6289 pmap_t pmap;
6290 vm_map_offset_t pmap_addr;
6291
6292
6293 sub_map = VME_SUBMAP(&tmp_entry);
6294 sub_start = VME_OFFSET(&tmp_entry);
6295 sub_end = sub_start + (tmp_entry.vme_end -
6296 tmp_entry.vme_start);
6297 if (tmp_entry.use_pmap) {
6298 pmap = sub_map->pmap;
6299 pmap_addr = tmp_entry.vme_start;
6300 } else {
6301 pmap = map->pmap;
6302 pmap_addr = tmp_entry.vme_start;
6303 }
6304 (void) vm_map_unwire_nested(sub_map,
6305 sub_start, sub_end,
6306 user_wire,
6307 pmap, pmap_addr);
6308 } else {
6309
6310 if (VME_OBJECT(&tmp_entry) == kernel_object) {
6311 pmap_protect_options(
6312 map->pmap,
6313 tmp_entry.vme_start,
6314 tmp_entry.vme_end,
6315 VM_PROT_NONE,
6316 PMAP_OPTIONS_REMOVE,
6317 NULL);
6318 }
6319 vm_fault_unwire(map, &tmp_entry,
6320 VME_OBJECT(&tmp_entry) == kernel_object,
6321 map->pmap, tmp_entry.vme_start);
6322 }
6323
6324 vm_map_lock(map);
6325
6326 if (last_timestamp+1 != map->timestamp) {
6327 /*
6328 * Find the entry again. It could have
6329 * been clipped after we unlocked the map.
6330 */
6331 if (!vm_map_lookup_entry(map, s, &first_entry)){
6332 assert((map != kernel_map) &&
6333 (!entry->is_sub_map));
6334 first_entry = first_entry->vme_next;
6335 s = first_entry->vme_start;
6336 } else {
6337 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
6338 }
6339 } else {
6340 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
6341 first_entry = entry;
6342 }
6343
6344 last_timestamp = map->timestamp;
6345
6346 entry = first_entry;
6347 while ((entry != vm_map_to_entry(map)) &&
6348 (entry->vme_start < tmp_entry.vme_end)) {
6349 assert(entry->in_transition);
6350 entry->in_transition = FALSE;
6351 if (entry->needs_wakeup) {
6352 entry->needs_wakeup = FALSE;
6353 need_wakeup = TRUE;
6354 }
6355 entry = entry->vme_next;
6356 }
6357 /*
6358 * We have unwired the entry(s). Go back and
6359 * delete them.
6360 */
6361 entry = first_entry;
6362 continue;
6363 }
6364
6365 /* entry is unwired */
6366 assert(entry->wired_count == 0);
6367 assert(entry->user_wired_count == 0);
6368
6369 assert(s == entry->vme_start);
6370
6371 if (flags & VM_MAP_REMOVE_NO_PMAP_CLEANUP) {
6372 /*
6373 * XXX with the VM_MAP_REMOVE_SAVE_ENTRIES flag to
6374 * vm_map_delete(), some map entries might have been
6375 * transferred to a "zap_map", which doesn't have a
6376 * pmap. The original pmap has already been flushed
6377 * in the vm_map_delete() call targeting the original
6378 * map, but when we get to destroying the "zap_map",
6379 * we don't have any pmap to flush, so let's just skip
6380 * all this.
6381 */
6382 } else if (entry->is_sub_map) {
6383 if (entry->use_pmap) {
6384 #ifndef NO_NESTED_PMAP
6385 int pmap_flags;
6386
6387 if (flags & VM_MAP_REMOVE_NO_UNNESTING) {
6388 /*
6389 * This is the final cleanup of the
6390 * address space being terminated.
6391 * No new mappings are expected and
6392 * we don't really need to unnest the
6393 * shared region (and lose the "global"
6394 * pmap mappings, if applicable).
6395 *
6396 * Tell the pmap layer that we're
6397 * "clean" wrt nesting.
6398 */
6399 pmap_flags = PMAP_UNNEST_CLEAN;
6400 } else {
6401 /*
6402 * We're unmapping part of the nested
6403 * shared region, so we can't keep the
6404 * nested pmap.
6405 */
6406 pmap_flags = 0;
6407 }
6408 pmap_unnest_options(
6409 map->pmap,
6410 (addr64_t)entry->vme_start,
6411 entry->vme_end - entry->vme_start,
6412 pmap_flags);
6413 #endif /* NO_NESTED_PMAP */
6414 if ((map->mapped_in_other_pmaps) && (map->ref_count)) {
6415 /* clean up parent map/maps */
6416 vm_map_submap_pmap_clean(
6417 map, entry->vme_start,
6418 entry->vme_end,
6419 VME_SUBMAP(entry),
6420 VME_OFFSET(entry));
6421 }
6422 } else {
6423 vm_map_submap_pmap_clean(
6424 map, entry->vme_start, entry->vme_end,
6425 VME_SUBMAP(entry),
6426 VME_OFFSET(entry));
6427 }
6428 } else if (VME_OBJECT(entry) != kernel_object &&
6429 VME_OBJECT(entry) != compressor_object) {
6430 object = VME_OBJECT(entry);
6431 if ((map->mapped_in_other_pmaps) && (map->ref_count)) {
6432 vm_object_pmap_protect_options(
6433 object, VME_OFFSET(entry),
6434 entry->vme_end - entry->vme_start,
6435 PMAP_NULL,
6436 entry->vme_start,
6437 VM_PROT_NONE,
6438 PMAP_OPTIONS_REMOVE);
6439 } else if ((VME_OBJECT(entry) != VM_OBJECT_NULL) ||
6440 (map->pmap == kernel_pmap)) {
6441 /* Remove translations associated
6442 * with this range unless the entry
6443 * does not have an object, or
6444 * it's the kernel map or a descendant
6445 * since the platform could potentially
6446 * create "backdoor" mappings invisible
6447 * to the VM. It is expected that
6448 * objectless, non-kernel ranges
6449 * do not have such VM invisible
6450 * translations.
6451 */
6452 pmap_remove_options(map->pmap,
6453 (addr64_t)entry->vme_start,
6454 (addr64_t)entry->vme_end,
6455 PMAP_OPTIONS_REMOVE);
6456 }
6457 }
6458
6459 if (entry->iokit_acct) {
6460 /* alternate accounting */
6461 vm_map_iokit_unmapped_region(map,
6462 (entry->vme_end -
6463 entry->vme_start));
6464 entry->iokit_acct = FALSE;
6465 }
6466
6467 /*
6468 * All pmap mappings for this map entry must have been
6469 * cleared by now.
6470 */
6471 #if DEBUG
6472 assert(vm_map_pmap_is_empty(map,
6473 entry->vme_start,
6474 entry->vme_end));
6475 #endif /* DEBUG */
6476
6477 next = entry->vme_next;
6478
6479 if (map->pmap == kernel_pmap &&
6480 map->ref_count != 0 &&
6481 entry->vme_end < end &&
6482 (next == vm_map_to_entry(map) ||
6483 next->vme_start != entry->vme_end)) {
6484 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6485 "hole after %p at 0x%llx\n",
6486 map,
6487 (uint64_t)start,
6488 (uint64_t)end,
6489 entry,
6490 (uint64_t)entry->vme_end);
6491 }
6492
6493 s = next->vme_start;
6494 last_timestamp = map->timestamp;
6495
6496 if ((flags & VM_MAP_REMOVE_SAVE_ENTRIES) &&
6497 zap_map != VM_MAP_NULL) {
6498 vm_map_size_t entry_size;
6499 /*
6500 * The caller wants to save the affected VM map entries
6501 * into the "zap_map". The caller will take care of
6502 * these entries.
6503 */
6504 /* unlink the entry from "map" ... */
6505 vm_map_store_entry_unlink(map, entry);
6506 /* ... and add it to the end of the "zap_map" */
6507 vm_map_store_entry_link(zap_map,
6508 vm_map_last_entry(zap_map),
6509 entry);
6510 entry_size = entry->vme_end - entry->vme_start;
6511 map->size -= entry_size;
6512 zap_map->size += entry_size;
6513 /* we didn't unlock the map, so no timestamp increase */
6514 last_timestamp--;
6515 } else {
6516 vm_map_entry_delete(map, entry);
6517 /* vm_map_entry_delete unlocks the map */
6518 vm_map_lock(map);
6519 }
6520
6521 entry = next;
6522
6523 if(entry == vm_map_to_entry(map)) {
6524 break;
6525 }
6526 if (last_timestamp+1 != map->timestamp) {
6527 /*
6528 * we are responsible for deleting everything
6529 * from the give space, if someone has interfered
6530 * we pick up where we left off, back fills should
6531 * be all right for anyone except map_delete and
6532 * we have to assume that the task has been fully
6533 * disabled before we get here
6534 */
6535 if (!vm_map_lookup_entry(map, s, &entry)){
6536 entry = entry->vme_next;
6537 s = entry->vme_start;
6538 } else {
6539 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
6540 }
6541 /*
6542 * others can not only allocate behind us, we can
6543 * also see coalesce while we don't have the map lock
6544 */
6545 if(entry == vm_map_to_entry(map)) {
6546 break;
6547 }
6548 }
6549 last_timestamp = map->timestamp;
6550 }
6551
6552 if (map->wait_for_space)
6553 thread_wakeup((event_t) map);
6554 /*
6555 * wake up anybody waiting on entries that we have already deleted.
6556 */
6557 if (need_wakeup)
6558 vm_map_entry_wakeup(map);
6559
6560 return KERN_SUCCESS;
6561 }
6562
6563 /*
6564 * vm_map_remove:
6565 *
6566 * Remove the given address range from the target map.
6567 * This is the exported form of vm_map_delete.
6568 */
6569 kern_return_t
6570 vm_map_remove(
6571 register vm_map_t map,
6572 register vm_map_offset_t start,
6573 register vm_map_offset_t end,
6574 register boolean_t flags)
6575 {
6576 register kern_return_t result;
6577
6578 vm_map_lock(map);
6579 VM_MAP_RANGE_CHECK(map, start, end);
6580 /*
6581 * For the zone_map, the kernel controls the allocation/freeing of memory.
6582 * Any free to the zone_map should be within the bounds of the map and
6583 * should free up memory. If the VM_MAP_RANGE_CHECK() silently converts a
6584 * free to the zone_map into a no-op, there is a problem and we should
6585 * panic.
6586 */
6587 if ((map == zone_map) && (start == end))
6588 panic("Nothing being freed to the zone_map. start = end = %p\n", (void *)start);
6589 result = vm_map_delete(map, start, end, flags, VM_MAP_NULL);
6590 vm_map_unlock(map);
6591
6592 return(result);
6593 }
6594
6595
6596 /*
6597 * Routine: vm_map_copy_discard
6598 *
6599 * Description:
6600 * Dispose of a map copy object (returned by
6601 * vm_map_copyin).
6602 */
6603 void
6604 vm_map_copy_discard(
6605 vm_map_copy_t copy)
6606 {
6607 if (copy == VM_MAP_COPY_NULL)
6608 return;
6609
6610 switch (copy->type) {
6611 case VM_MAP_COPY_ENTRY_LIST:
6612 while (vm_map_copy_first_entry(copy) !=
6613 vm_map_copy_to_entry(copy)) {
6614 vm_map_entry_t entry = vm_map_copy_first_entry(copy);
6615
6616 vm_map_copy_entry_unlink(copy, entry);
6617 if (entry->is_sub_map) {
6618 vm_map_deallocate(VME_SUBMAP(entry));
6619 } else {
6620 vm_object_deallocate(VME_OBJECT(entry));
6621 }
6622 vm_map_copy_entry_dispose(copy, entry);
6623 }
6624 break;
6625 case VM_MAP_COPY_OBJECT:
6626 vm_object_deallocate(copy->cpy_object);
6627 break;
6628 case VM_MAP_COPY_KERNEL_BUFFER:
6629
6630 /*
6631 * The vm_map_copy_t and possibly the data buffer were
6632 * allocated by a single call to kalloc(), i.e. the
6633 * vm_map_copy_t was not allocated out of the zone.
6634 */
6635 if (copy->size > msg_ool_size_small || copy->offset)
6636 panic("Invalid vm_map_copy_t sz:%lld, ofst:%lld",
6637 (long long)copy->size, (long long)copy->offset);
6638 kfree(copy, copy->size + cpy_kdata_hdr_sz);
6639 return;
6640 }
6641 zfree(vm_map_copy_zone, copy);
6642 }
6643
6644 /*
6645 * Routine: vm_map_copy_copy
6646 *
6647 * Description:
6648 * Move the information in a map copy object to
6649 * a new map copy object, leaving the old one
6650 * empty.
6651 *
6652 * This is used by kernel routines that need
6653 * to look at out-of-line data (in copyin form)
6654 * before deciding whether to return SUCCESS.
6655 * If the routine returns FAILURE, the original
6656 * copy object will be deallocated; therefore,
6657 * these routines must make a copy of the copy
6658 * object and leave the original empty so that
6659 * deallocation will not fail.
6660 */
6661 vm_map_copy_t
6662 vm_map_copy_copy(
6663 vm_map_copy_t copy)
6664 {
6665 vm_map_copy_t new_copy;
6666
6667 if (copy == VM_MAP_COPY_NULL)
6668 return VM_MAP_COPY_NULL;
6669
6670 /*
6671 * Allocate a new copy object, and copy the information
6672 * from the old one into it.
6673 */
6674
6675 new_copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
6676 new_copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
6677 *new_copy = *copy;
6678
6679 if (copy->type == VM_MAP_COPY_ENTRY_LIST) {
6680 /*
6681 * The links in the entry chain must be
6682 * changed to point to the new copy object.
6683 */
6684 vm_map_copy_first_entry(copy)->vme_prev
6685 = vm_map_copy_to_entry(new_copy);
6686 vm_map_copy_last_entry(copy)->vme_next
6687 = vm_map_copy_to_entry(new_copy);
6688 }
6689
6690 /*
6691 * Change the old copy object into one that contains
6692 * nothing to be deallocated.
6693 */
6694 copy->type = VM_MAP_COPY_OBJECT;
6695 copy->cpy_object = VM_OBJECT_NULL;
6696
6697 /*
6698 * Return the new object.
6699 */
6700 return new_copy;
6701 }
6702
6703 static kern_return_t
6704 vm_map_overwrite_submap_recurse(
6705 vm_map_t dst_map,
6706 vm_map_offset_t dst_addr,
6707 vm_map_size_t dst_size)
6708 {
6709 vm_map_offset_t dst_end;
6710 vm_map_entry_t tmp_entry;
6711 vm_map_entry_t entry;
6712 kern_return_t result;
6713 boolean_t encountered_sub_map = FALSE;
6714
6715
6716
6717 /*
6718 * Verify that the destination is all writeable
6719 * initially. We have to trunc the destination
6720 * address and round the copy size or we'll end up
6721 * splitting entries in strange ways.
6722 */
6723
6724 dst_end = vm_map_round_page(dst_addr + dst_size,
6725 VM_MAP_PAGE_MASK(dst_map));
6726 vm_map_lock(dst_map);
6727
6728 start_pass_1:
6729 if (!vm_map_lookup_entry(dst_map, dst_addr, &tmp_entry)) {
6730 vm_map_unlock(dst_map);
6731 return(KERN_INVALID_ADDRESS);
6732 }
6733
6734 vm_map_clip_start(dst_map,
6735 tmp_entry,
6736 vm_map_trunc_page(dst_addr,
6737 VM_MAP_PAGE_MASK(dst_map)));
6738 if (tmp_entry->is_sub_map) {
6739 /* clipping did unnest if needed */
6740 assert(!tmp_entry->use_pmap);
6741 }
6742
6743 for (entry = tmp_entry;;) {
6744 vm_map_entry_t next;
6745
6746 next = entry->vme_next;
6747 while(entry->is_sub_map) {
6748 vm_map_offset_t sub_start;
6749 vm_map_offset_t sub_end;
6750 vm_map_offset_t local_end;
6751
6752 if (entry->in_transition) {
6753 /*
6754 * Say that we are waiting, and wait for entry.
6755 */
6756 entry->needs_wakeup = TRUE;
6757 vm_map_entry_wait(dst_map, THREAD_UNINT);
6758
6759 goto start_pass_1;
6760 }
6761
6762 encountered_sub_map = TRUE;
6763 sub_start = VME_OFFSET(entry);
6764
6765 if(entry->vme_end < dst_end)
6766 sub_end = entry->vme_end;
6767 else
6768 sub_end = dst_end;
6769 sub_end -= entry->vme_start;
6770 sub_end += VME_OFFSET(entry);
6771 local_end = entry->vme_end;
6772 vm_map_unlock(dst_map);
6773
6774 result = vm_map_overwrite_submap_recurse(
6775 VME_SUBMAP(entry),
6776 sub_start,
6777 sub_end - sub_start);
6778
6779 if(result != KERN_SUCCESS)
6780 return result;
6781 if (dst_end <= entry->vme_end)
6782 return KERN_SUCCESS;
6783 vm_map_lock(dst_map);
6784 if(!vm_map_lookup_entry(dst_map, local_end,
6785 &tmp_entry)) {
6786 vm_map_unlock(dst_map);
6787 return(KERN_INVALID_ADDRESS);
6788 }
6789 entry = tmp_entry;
6790 next = entry->vme_next;
6791 }
6792
6793 if ( ! (entry->protection & VM_PROT_WRITE)) {
6794 vm_map_unlock(dst_map);
6795 return(KERN_PROTECTION_FAILURE);
6796 }
6797
6798 /*
6799 * If the entry is in transition, we must wait
6800 * for it to exit that state. Anything could happen
6801 * when we unlock the map, so start over.
6802 */
6803 if (entry->in_transition) {
6804
6805 /*
6806 * Say that we are waiting, and wait for entry.
6807 */
6808 entry->needs_wakeup = TRUE;
6809 vm_map_entry_wait(dst_map, THREAD_UNINT);
6810
6811 goto start_pass_1;
6812 }
6813
6814 /*
6815 * our range is contained completely within this map entry
6816 */
6817 if (dst_end <= entry->vme_end) {
6818 vm_map_unlock(dst_map);
6819 return KERN_SUCCESS;
6820 }
6821 /*
6822 * check that range specified is contiguous region
6823 */
6824 if ((next == vm_map_to_entry(dst_map)) ||
6825 (next->vme_start != entry->vme_end)) {
6826 vm_map_unlock(dst_map);
6827 return(KERN_INVALID_ADDRESS);
6828 }
6829
6830 /*
6831 * Check for permanent objects in the destination.
6832 */
6833 if ((VME_OBJECT(entry) != VM_OBJECT_NULL) &&
6834 ((!VME_OBJECT(entry)->internal) ||
6835 (VME_OBJECT(entry)->true_share))) {
6836 if(encountered_sub_map) {
6837 vm_map_unlock(dst_map);
6838 return(KERN_FAILURE);
6839 }
6840 }
6841
6842
6843 entry = next;
6844 }/* for */
6845 vm_map_unlock(dst_map);
6846 return(KERN_SUCCESS);
6847 }
6848
6849 /*
6850 * Routine: vm_map_copy_overwrite
6851 *
6852 * Description:
6853 * Copy the memory described by the map copy
6854 * object (copy; returned by vm_map_copyin) onto
6855 * the specified destination region (dst_map, dst_addr).
6856 * The destination must be writeable.
6857 *
6858 * Unlike vm_map_copyout, this routine actually
6859 * writes over previously-mapped memory. If the
6860 * previous mapping was to a permanent (user-supplied)
6861 * memory object, it is preserved.
6862 *
6863 * The attributes (protection and inheritance) of the
6864 * destination region are preserved.
6865 *
6866 * If successful, consumes the copy object.
6867 * Otherwise, the caller is responsible for it.
6868 *
6869 * Implementation notes:
6870 * To overwrite aligned temporary virtual memory, it is
6871 * sufficient to remove the previous mapping and insert
6872 * the new copy. This replacement is done either on
6873 * the whole region (if no permanent virtual memory
6874 * objects are embedded in the destination region) or
6875 * in individual map entries.
6876 *
6877 * To overwrite permanent virtual memory , it is necessary
6878 * to copy each page, as the external memory management
6879 * interface currently does not provide any optimizations.
6880 *
6881 * Unaligned memory also has to be copied. It is possible
6882 * to use 'vm_trickery' to copy the aligned data. This is
6883 * not done but not hard to implement.
6884 *
6885 * Once a page of permanent memory has been overwritten,
6886 * it is impossible to interrupt this function; otherwise,
6887 * the call would be neither atomic nor location-independent.
6888 * The kernel-state portion of a user thread must be
6889 * interruptible.
6890 *
6891 * It may be expensive to forward all requests that might
6892 * overwrite permanent memory (vm_write, vm_copy) to
6893 * uninterruptible kernel threads. This routine may be
6894 * called by interruptible threads; however, success is
6895 * not guaranteed -- if the request cannot be performed
6896 * atomically and interruptibly, an error indication is
6897 * returned.
6898 */
6899
6900 static kern_return_t
6901 vm_map_copy_overwrite_nested(
6902 vm_map_t dst_map,
6903 vm_map_address_t dst_addr,
6904 vm_map_copy_t copy,
6905 boolean_t interruptible,
6906 pmap_t pmap,
6907 boolean_t discard_on_success)
6908 {
6909 vm_map_offset_t dst_end;
6910 vm_map_entry_t tmp_entry;
6911 vm_map_entry_t entry;
6912 kern_return_t kr;
6913 boolean_t aligned = TRUE;
6914 boolean_t contains_permanent_objects = FALSE;
6915 boolean_t encountered_sub_map = FALSE;
6916 vm_map_offset_t base_addr;
6917 vm_map_size_t copy_size;
6918 vm_map_size_t total_size;
6919
6920
6921 /*
6922 * Check for null copy object.
6923 */
6924
6925 if (copy == VM_MAP_COPY_NULL)
6926 return(KERN_SUCCESS);
6927
6928 /*
6929 * Check for special kernel buffer allocated
6930 * by new_ipc_kmsg_copyin.
6931 */
6932
6933 if (copy->type == VM_MAP_COPY_KERNEL_BUFFER) {
6934 return(vm_map_copyout_kernel_buffer(
6935 dst_map, &dst_addr,
6936 copy, TRUE, discard_on_success));
6937 }
6938
6939 /*
6940 * Only works for entry lists at the moment. Will
6941 * support page lists later.
6942 */
6943
6944 assert(copy->type == VM_MAP_COPY_ENTRY_LIST);
6945
6946 if (copy->size == 0) {
6947 if (discard_on_success)
6948 vm_map_copy_discard(copy);
6949 return(KERN_SUCCESS);
6950 }
6951
6952 /*
6953 * Verify that the destination is all writeable
6954 * initially. We have to trunc the destination
6955 * address and round the copy size or we'll end up
6956 * splitting entries in strange ways.
6957 */
6958
6959 if (!VM_MAP_PAGE_ALIGNED(copy->size,
6960 VM_MAP_PAGE_MASK(dst_map)) ||
6961 !VM_MAP_PAGE_ALIGNED(copy->offset,
6962 VM_MAP_PAGE_MASK(dst_map)) ||
6963 !VM_MAP_PAGE_ALIGNED(dst_addr,
6964 VM_MAP_PAGE_MASK(dst_map)))
6965 {
6966 aligned = FALSE;
6967 dst_end = vm_map_round_page(dst_addr + copy->size,
6968 VM_MAP_PAGE_MASK(dst_map));
6969 } else {
6970 dst_end = dst_addr + copy->size;
6971 }
6972
6973 vm_map_lock(dst_map);
6974
6975 /* LP64todo - remove this check when vm_map_commpage64()
6976 * no longer has to stuff in a map_entry for the commpage
6977 * above the map's max_offset.
6978 */
6979 if (dst_addr >= dst_map->max_offset) {
6980 vm_map_unlock(dst_map);
6981 return(KERN_INVALID_ADDRESS);
6982 }
6983
6984 start_pass_1:
6985 if (!vm_map_lookup_entry(dst_map, dst_addr, &tmp_entry)) {
6986 vm_map_unlock(dst_map);
6987 return(KERN_INVALID_ADDRESS);
6988 }
6989 vm_map_clip_start(dst_map,
6990 tmp_entry,
6991 vm_map_trunc_page(dst_addr,
6992 VM_MAP_PAGE_MASK(dst_map)));
6993 for (entry = tmp_entry;;) {
6994 vm_map_entry_t next = entry->vme_next;
6995
6996 while(entry->is_sub_map) {
6997 vm_map_offset_t sub_start;
6998 vm_map_offset_t sub_end;
6999 vm_map_offset_t local_end;
7000
7001 if (entry->in_transition) {
7002
7003 /*
7004 * Say that we are waiting, and wait for entry.
7005 */
7006 entry->needs_wakeup = TRUE;
7007 vm_map_entry_wait(dst_map, THREAD_UNINT);
7008
7009 goto start_pass_1;
7010 }
7011
7012 local_end = entry->vme_end;
7013 if (!(entry->needs_copy)) {
7014 /* if needs_copy we are a COW submap */
7015 /* in such a case we just replace so */
7016 /* there is no need for the follow- */
7017 /* ing check. */
7018 encountered_sub_map = TRUE;
7019 sub_start = VME_OFFSET(entry);
7020
7021 if(entry->vme_end < dst_end)
7022 sub_end = entry->vme_end;
7023 else
7024 sub_end = dst_end;
7025 sub_end -= entry->vme_start;
7026 sub_end += VME_OFFSET(entry);
7027 vm_map_unlock(dst_map);
7028
7029 kr = vm_map_overwrite_submap_recurse(
7030 VME_SUBMAP(entry),
7031 sub_start,
7032 sub_end - sub_start);
7033 if(kr != KERN_SUCCESS)
7034 return kr;
7035 vm_map_lock(dst_map);
7036 }
7037
7038 if (dst_end <= entry->vme_end)
7039 goto start_overwrite;
7040 if(!vm_map_lookup_entry(dst_map, local_end,
7041 &entry)) {
7042 vm_map_unlock(dst_map);
7043 return(KERN_INVALID_ADDRESS);
7044 }
7045 next = entry->vme_next;
7046 }
7047
7048 if ( ! (entry->protection & VM_PROT_WRITE)) {
7049 vm_map_unlock(dst_map);
7050 return(KERN_PROTECTION_FAILURE);
7051 }
7052
7053 /*
7054 * If the entry is in transition, we must wait
7055 * for it to exit that state. Anything could happen
7056 * when we unlock the map, so start over.
7057 */
7058 if (entry->in_transition) {
7059
7060 /*
7061 * Say that we are waiting, and wait for entry.
7062 */
7063 entry->needs_wakeup = TRUE;
7064 vm_map_entry_wait(dst_map, THREAD_UNINT);
7065
7066 goto start_pass_1;
7067 }
7068
7069 /*
7070 * our range is contained completely within this map entry
7071 */
7072 if (dst_end <= entry->vme_end)
7073 break;
7074 /*
7075 * check that range specified is contiguous region
7076 */
7077 if ((next == vm_map_to_entry(dst_map)) ||
7078 (next->vme_start != entry->vme_end)) {
7079 vm_map_unlock(dst_map);
7080 return(KERN_INVALID_ADDRESS);
7081 }
7082
7083
7084 /*
7085 * Check for permanent objects in the destination.
7086 */
7087 if ((VME_OBJECT(entry) != VM_OBJECT_NULL) &&
7088 ((!VME_OBJECT(entry)->internal) ||
7089 (VME_OBJECT(entry)->true_share))) {
7090 contains_permanent_objects = TRUE;
7091 }
7092
7093 entry = next;
7094 }/* for */
7095
7096 start_overwrite:
7097 /*
7098 * If there are permanent objects in the destination, then
7099 * the copy cannot be interrupted.
7100 */
7101
7102 if (interruptible && contains_permanent_objects) {
7103 vm_map_unlock(dst_map);
7104 return(KERN_FAILURE); /* XXX */
7105 }
7106
7107 /*
7108 *
7109 * Make a second pass, overwriting the data
7110 * At the beginning of each loop iteration,
7111 * the next entry to be overwritten is "tmp_entry"
7112 * (initially, the value returned from the lookup above),
7113 * and the starting address expected in that entry
7114 * is "start".
7115 */
7116
7117 total_size = copy->size;
7118 if(encountered_sub_map) {
7119 copy_size = 0;
7120 /* re-calculate tmp_entry since we've had the map */
7121 /* unlocked */
7122 if (!vm_map_lookup_entry( dst_map, dst_addr, &tmp_entry)) {
7123 vm_map_unlock(dst_map);
7124 return(KERN_INVALID_ADDRESS);
7125 }
7126 } else {
7127 copy_size = copy->size;
7128 }
7129
7130 base_addr = dst_addr;
7131 while(TRUE) {
7132 /* deconstruct the copy object and do in parts */
7133 /* only in sub_map, interruptable case */
7134 vm_map_entry_t copy_entry;
7135 vm_map_entry_t previous_prev = VM_MAP_ENTRY_NULL;
7136 vm_map_entry_t next_copy = VM_MAP_ENTRY_NULL;
7137 int nentries;
7138 int remaining_entries = 0;
7139 vm_map_offset_t new_offset = 0;
7140
7141 for (entry = tmp_entry; copy_size == 0;) {
7142 vm_map_entry_t next;
7143
7144 next = entry->vme_next;
7145
7146 /* tmp_entry and base address are moved along */
7147 /* each time we encounter a sub-map. Otherwise */
7148 /* entry can outpase tmp_entry, and the copy_size */
7149 /* may reflect the distance between them */
7150 /* if the current entry is found to be in transition */
7151 /* we will start over at the beginning or the last */
7152 /* encounter of a submap as dictated by base_addr */
7153 /* we will zero copy_size accordingly. */
7154 if (entry->in_transition) {
7155 /*
7156 * Say that we are waiting, and wait for entry.
7157 */
7158 entry->needs_wakeup = TRUE;
7159 vm_map_entry_wait(dst_map, THREAD_UNINT);
7160
7161 if(!vm_map_lookup_entry(dst_map, base_addr,
7162 &tmp_entry)) {
7163 vm_map_unlock(dst_map);
7164 return(KERN_INVALID_ADDRESS);
7165 }
7166 copy_size = 0;
7167 entry = tmp_entry;
7168 continue;
7169 }
7170 if(entry->is_sub_map) {
7171 vm_map_offset_t sub_start;
7172 vm_map_offset_t sub_end;
7173 vm_map_offset_t local_end;
7174
7175 if (entry->needs_copy) {
7176 /* if this is a COW submap */
7177 /* just back the range with a */
7178 /* anonymous entry */
7179 if(entry->vme_end < dst_end)
7180 sub_end = entry->vme_end;
7181 else
7182 sub_end = dst_end;
7183 if(entry->vme_start < base_addr)
7184 sub_start = base_addr;
7185 else
7186 sub_start = entry->vme_start;
7187 vm_map_clip_end(
7188 dst_map, entry, sub_end);
7189 vm_map_clip_start(
7190 dst_map, entry, sub_start);
7191 assert(!entry->use_pmap);
7192 entry->is_sub_map = FALSE;
7193 vm_map_deallocate(
7194 VME_SUBMAP(entry));
7195 VME_SUBMAP_SET(entry, NULL);
7196 entry->is_shared = FALSE;
7197 entry->needs_copy = FALSE;
7198 VME_OFFSET_SET(entry, 0);
7199 /*
7200 * XXX FBDP
7201 * We should propagate the protections
7202 * of the submap entry here instead
7203 * of forcing them to VM_PROT_ALL...
7204 * Or better yet, we should inherit
7205 * the protection of the copy_entry.
7206 */
7207 entry->protection = VM_PROT_ALL;
7208 entry->max_protection = VM_PROT_ALL;
7209 entry->wired_count = 0;
7210 entry->user_wired_count = 0;
7211 if(entry->inheritance
7212 == VM_INHERIT_SHARE)
7213 entry->inheritance = VM_INHERIT_COPY;
7214 continue;
7215 }
7216 /* first take care of any non-sub_map */
7217 /* entries to send */
7218 if(base_addr < entry->vme_start) {
7219 /* stuff to send */
7220 copy_size =
7221 entry->vme_start - base_addr;
7222 break;
7223 }
7224 sub_start = VME_OFFSET(entry);
7225
7226 if(entry->vme_end < dst_end)
7227 sub_end = entry->vme_end;
7228 else
7229 sub_end = dst_end;
7230 sub_end -= entry->vme_start;
7231 sub_end += VME_OFFSET(entry);
7232 local_end = entry->vme_end;
7233 vm_map_unlock(dst_map);
7234 copy_size = sub_end - sub_start;
7235
7236 /* adjust the copy object */
7237 if (total_size > copy_size) {
7238 vm_map_size_t local_size = 0;
7239 vm_map_size_t entry_size;
7240
7241 nentries = 1;
7242 new_offset = copy->offset;
7243 copy_entry = vm_map_copy_first_entry(copy);
7244 while(copy_entry !=
7245 vm_map_copy_to_entry(copy)){
7246 entry_size = copy_entry->vme_end -
7247 copy_entry->vme_start;
7248 if((local_size < copy_size) &&
7249 ((local_size + entry_size)
7250 >= copy_size)) {
7251 vm_map_copy_clip_end(copy,
7252 copy_entry,
7253 copy_entry->vme_start +
7254 (copy_size - local_size));
7255 entry_size = copy_entry->vme_end -
7256 copy_entry->vme_start;
7257 local_size += entry_size;
7258 new_offset += entry_size;
7259 }
7260 if(local_size >= copy_size) {
7261 next_copy = copy_entry->vme_next;
7262 copy_entry->vme_next =
7263 vm_map_copy_to_entry(copy);
7264 previous_prev =
7265 copy->cpy_hdr.links.prev;
7266 copy->cpy_hdr.links.prev = copy_entry;
7267 copy->size = copy_size;
7268 remaining_entries =
7269 copy->cpy_hdr.nentries;
7270 remaining_entries -= nentries;
7271 copy->cpy_hdr.nentries = nentries;
7272 break;
7273 } else {
7274 local_size += entry_size;
7275 new_offset += entry_size;
7276 nentries++;
7277 }
7278 copy_entry = copy_entry->vme_next;
7279 }
7280 }
7281
7282 if((entry->use_pmap) && (pmap == NULL)) {
7283 kr = vm_map_copy_overwrite_nested(
7284 VME_SUBMAP(entry),
7285 sub_start,
7286 copy,
7287 interruptible,
7288 VME_SUBMAP(entry)->pmap,
7289 TRUE);
7290 } else if (pmap != NULL) {
7291 kr = vm_map_copy_overwrite_nested(
7292 VME_SUBMAP(entry),
7293 sub_start,
7294 copy,
7295 interruptible, pmap,
7296 TRUE);
7297 } else {
7298 kr = vm_map_copy_overwrite_nested(
7299 VME_SUBMAP(entry),
7300 sub_start,
7301 copy,
7302 interruptible,
7303 dst_map->pmap,
7304 TRUE);
7305 }
7306 if(kr != KERN_SUCCESS) {
7307 if(next_copy != NULL) {
7308 copy->cpy_hdr.nentries +=
7309 remaining_entries;
7310 copy->cpy_hdr.links.prev->vme_next =
7311 next_copy;
7312 copy->cpy_hdr.links.prev
7313 = previous_prev;
7314 copy->size = total_size;
7315 }
7316 return kr;
7317 }
7318 if (dst_end <= local_end) {
7319 return(KERN_SUCCESS);
7320 }
7321 /* otherwise copy no longer exists, it was */
7322 /* destroyed after successful copy_overwrite */
7323 copy = (vm_map_copy_t)
7324 zalloc(vm_map_copy_zone);
7325 copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
7326 vm_map_copy_first_entry(copy) =
7327 vm_map_copy_last_entry(copy) =
7328 vm_map_copy_to_entry(copy);
7329 copy->type = VM_MAP_COPY_ENTRY_LIST;
7330 copy->offset = new_offset;
7331
7332 /*
7333 * XXX FBDP
7334 * this does not seem to deal with
7335 * the VM map store (R&B tree)
7336 */
7337
7338 total_size -= copy_size;
7339 copy_size = 0;
7340 /* put back remainder of copy in container */
7341 if(next_copy != NULL) {
7342 copy->cpy_hdr.nentries = remaining_entries;
7343 copy->cpy_hdr.links.next = next_copy;
7344 copy->cpy_hdr.links.prev = previous_prev;
7345 copy->size = total_size;
7346 next_copy->vme_prev =
7347 vm_map_copy_to_entry(copy);
7348 next_copy = NULL;
7349 }
7350 base_addr = local_end;
7351 vm_map_lock(dst_map);
7352 if(!vm_map_lookup_entry(dst_map,
7353 local_end, &tmp_entry)) {
7354 vm_map_unlock(dst_map);
7355 return(KERN_INVALID_ADDRESS);
7356 }
7357 entry = tmp_entry;
7358 continue;
7359 }
7360 if (dst_end <= entry->vme_end) {
7361 copy_size = dst_end - base_addr;
7362 break;
7363 }
7364
7365 if ((next == vm_map_to_entry(dst_map)) ||
7366 (next->vme_start != entry->vme_end)) {
7367 vm_map_unlock(dst_map);
7368 return(KERN_INVALID_ADDRESS);
7369 }
7370
7371 entry = next;
7372 }/* for */
7373
7374 next_copy = NULL;
7375 nentries = 1;
7376
7377 /* adjust the copy object */
7378 if (total_size > copy_size) {
7379 vm_map_size_t local_size = 0;
7380 vm_map_size_t entry_size;
7381
7382 new_offset = copy->offset;
7383 copy_entry = vm_map_copy_first_entry(copy);
7384 while(copy_entry != vm_map_copy_to_entry(copy)) {
7385 entry_size = copy_entry->vme_end -
7386 copy_entry->vme_start;
7387 if((local_size < copy_size) &&
7388 ((local_size + entry_size)
7389 >= copy_size)) {
7390 vm_map_copy_clip_end(copy, copy_entry,
7391 copy_entry->vme_start +
7392 (copy_size - local_size));
7393 entry_size = copy_entry->vme_end -
7394 copy_entry->vme_start;
7395 local_size += entry_size;
7396 new_offset += entry_size;
7397 }
7398 if(local_size >= copy_size) {
7399 next_copy = copy_entry->vme_next;
7400 copy_entry->vme_next =
7401 vm_map_copy_to_entry(copy);
7402 previous_prev =
7403 copy->cpy_hdr.links.prev;
7404 copy->cpy_hdr.links.prev = copy_entry;
7405 copy->size = copy_size;
7406 remaining_entries =
7407 copy->cpy_hdr.nentries;
7408 remaining_entries -= nentries;
7409 copy->cpy_hdr.nentries = nentries;
7410 break;
7411 } else {
7412 local_size += entry_size;
7413 new_offset += entry_size;
7414 nentries++;
7415 }
7416 copy_entry = copy_entry->vme_next;
7417 }
7418 }
7419
7420 if (aligned) {
7421 pmap_t local_pmap;
7422
7423 if(pmap)
7424 local_pmap = pmap;
7425 else
7426 local_pmap = dst_map->pmap;
7427
7428 if ((kr = vm_map_copy_overwrite_aligned(
7429 dst_map, tmp_entry, copy,
7430 base_addr, local_pmap)) != KERN_SUCCESS) {
7431 if(next_copy != NULL) {
7432 copy->cpy_hdr.nentries +=
7433 remaining_entries;
7434 copy->cpy_hdr.links.prev->vme_next =
7435 next_copy;
7436 copy->cpy_hdr.links.prev =
7437 previous_prev;
7438 copy->size += copy_size;
7439 }
7440 return kr;
7441 }
7442 vm_map_unlock(dst_map);
7443 } else {
7444 /*
7445 * Performance gain:
7446 *
7447 * if the copy and dst address are misaligned but the same
7448 * offset within the page we can copy_not_aligned the
7449 * misaligned parts and copy aligned the rest. If they are
7450 * aligned but len is unaligned we simply need to copy
7451 * the end bit unaligned. We'll need to split the misaligned
7452 * bits of the region in this case !
7453 */
7454 /* ALWAYS UNLOCKS THE dst_map MAP */
7455 kr = vm_map_copy_overwrite_unaligned(
7456 dst_map,
7457 tmp_entry,
7458 copy,
7459 base_addr,
7460 discard_on_success);
7461 if (kr != KERN_SUCCESS) {
7462 if(next_copy != NULL) {
7463 copy->cpy_hdr.nentries +=
7464 remaining_entries;
7465 copy->cpy_hdr.links.prev->vme_next =
7466 next_copy;
7467 copy->cpy_hdr.links.prev =
7468 previous_prev;
7469 copy->size += copy_size;
7470 }
7471 return kr;
7472 }
7473 }
7474 total_size -= copy_size;
7475 if(total_size == 0)
7476 break;
7477 base_addr += copy_size;
7478 copy_size = 0;
7479 copy->offset = new_offset;
7480 if(next_copy != NULL) {
7481 copy->cpy_hdr.nentries = remaining_entries;
7482 copy->cpy_hdr.links.next = next_copy;
7483 copy->cpy_hdr.links.prev = previous_prev;
7484 next_copy->vme_prev = vm_map_copy_to_entry(copy);
7485 copy->size = total_size;
7486 }
7487 vm_map_lock(dst_map);
7488 while(TRUE) {
7489 if (!vm_map_lookup_entry(dst_map,
7490 base_addr, &tmp_entry)) {
7491 vm_map_unlock(dst_map);
7492 return(KERN_INVALID_ADDRESS);
7493 }
7494 if (tmp_entry->in_transition) {
7495 entry->needs_wakeup = TRUE;
7496 vm_map_entry_wait(dst_map, THREAD_UNINT);
7497 } else {
7498 break;
7499 }
7500 }
7501 vm_map_clip_start(dst_map,
7502 tmp_entry,
7503 vm_map_trunc_page(base_addr,
7504 VM_MAP_PAGE_MASK(dst_map)));
7505
7506 entry = tmp_entry;
7507 } /* while */
7508
7509 /*
7510 * Throw away the vm_map_copy object
7511 */
7512 if (discard_on_success)
7513 vm_map_copy_discard(copy);
7514
7515 return(KERN_SUCCESS);
7516 }/* vm_map_copy_overwrite */
7517
7518 kern_return_t
7519 vm_map_copy_overwrite(
7520 vm_map_t dst_map,
7521 vm_map_offset_t dst_addr,
7522 vm_map_copy_t copy,
7523 boolean_t interruptible)
7524 {
7525 vm_map_size_t head_size, tail_size;
7526 vm_map_copy_t head_copy, tail_copy;
7527 vm_map_offset_t head_addr, tail_addr;
7528 vm_map_entry_t entry;
7529 kern_return_t kr;
7530
7531 head_size = 0;
7532 tail_size = 0;
7533 head_copy = NULL;
7534 tail_copy = NULL;
7535 head_addr = 0;
7536 tail_addr = 0;
7537
7538 if (interruptible ||
7539 copy == VM_MAP_COPY_NULL ||
7540 copy->type != VM_MAP_COPY_ENTRY_LIST) {
7541 /*
7542 * We can't split the "copy" map if we're interruptible
7543 * or if we don't have a "copy" map...
7544 */
7545 blunt_copy:
7546 return vm_map_copy_overwrite_nested(dst_map,
7547 dst_addr,
7548 copy,
7549 interruptible,
7550 (pmap_t) NULL,
7551 TRUE);
7552 }
7553
7554 if (copy->size < 3 * PAGE_SIZE) {
7555 /*
7556 * Too small to bother with optimizing...
7557 */
7558 goto blunt_copy;
7559 }
7560
7561 if ((dst_addr & VM_MAP_PAGE_MASK(dst_map)) !=
7562 (copy->offset & VM_MAP_PAGE_MASK(dst_map))) {
7563 /*
7564 * Incompatible mis-alignment of source and destination...
7565 */
7566 goto blunt_copy;
7567 }
7568
7569 /*
7570 * Proper alignment or identical mis-alignment at the beginning.
7571 * Let's try and do a small unaligned copy first (if needed)
7572 * and then an aligned copy for the rest.
7573 */
7574 if (!page_aligned(dst_addr)) {
7575 head_addr = dst_addr;
7576 head_size = (VM_MAP_PAGE_SIZE(dst_map) -
7577 (copy->offset & VM_MAP_PAGE_MASK(dst_map)));
7578 }
7579 if (!page_aligned(copy->offset + copy->size)) {
7580 /*
7581 * Mis-alignment at the end.
7582 * Do an aligned copy up to the last page and
7583 * then an unaligned copy for the remaining bytes.
7584 */
7585 tail_size = ((copy->offset + copy->size) &
7586 VM_MAP_PAGE_MASK(dst_map));
7587 tail_addr = dst_addr + copy->size - tail_size;
7588 }
7589
7590 if (head_size + tail_size == copy->size) {
7591 /*
7592 * It's all unaligned, no optimization possible...
7593 */
7594 goto blunt_copy;
7595 }
7596
7597 /*
7598 * Can't optimize if there are any submaps in the
7599 * destination due to the way we free the "copy" map
7600 * progressively in vm_map_copy_overwrite_nested()
7601 * in that case.
7602 */
7603 vm_map_lock_read(dst_map);
7604 if (! vm_map_lookup_entry(dst_map, dst_addr, &entry)) {
7605 vm_map_unlock_read(dst_map);
7606 goto blunt_copy;
7607 }
7608 for (;
7609 (entry != vm_map_copy_to_entry(copy) &&
7610 entry->vme_start < dst_addr + copy->size);
7611 entry = entry->vme_next) {
7612 if (entry->is_sub_map) {
7613 vm_map_unlock_read(dst_map);
7614 goto blunt_copy;
7615 }
7616 }
7617 vm_map_unlock_read(dst_map);
7618
7619 if (head_size) {
7620 /*
7621 * Unaligned copy of the first "head_size" bytes, to reach
7622 * a page boundary.
7623 */
7624
7625 /*
7626 * Extract "head_copy" out of "copy".
7627 */
7628 head_copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
7629 head_copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
7630 vm_map_copy_first_entry(head_copy) =
7631 vm_map_copy_to_entry(head_copy);
7632 vm_map_copy_last_entry(head_copy) =
7633 vm_map_copy_to_entry(head_copy);
7634 head_copy->type = VM_MAP_COPY_ENTRY_LIST;
7635 head_copy->cpy_hdr.nentries = 0;
7636 head_copy->cpy_hdr.entries_pageable =
7637 copy->cpy_hdr.entries_pageable;
7638 vm_map_store_init(&head_copy->cpy_hdr);
7639
7640 head_copy->offset = copy->offset;
7641 head_copy->size = head_size;
7642
7643 copy->offset += head_size;
7644 copy->size -= head_size;
7645
7646 entry = vm_map_copy_first_entry(copy);
7647 vm_map_copy_clip_end(copy, entry, copy->offset);
7648 vm_map_copy_entry_unlink(copy, entry);
7649 vm_map_copy_entry_link(head_copy,
7650 vm_map_copy_to_entry(head_copy),
7651 entry);
7652
7653 /*
7654 * Do the unaligned copy.
7655 */
7656 kr = vm_map_copy_overwrite_nested(dst_map,
7657 head_addr,
7658 head_copy,
7659 interruptible,
7660 (pmap_t) NULL,
7661 FALSE);
7662 if (kr != KERN_SUCCESS)
7663 goto done;
7664 }
7665
7666 if (tail_size) {
7667 /*
7668 * Extract "tail_copy" out of "copy".
7669 */
7670 tail_copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
7671 tail_copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
7672 vm_map_copy_first_entry(tail_copy) =
7673 vm_map_copy_to_entry(tail_copy);
7674 vm_map_copy_last_entry(tail_copy) =
7675 vm_map_copy_to_entry(tail_copy);
7676 tail_copy->type = VM_MAP_COPY_ENTRY_LIST;
7677 tail_copy->cpy_hdr.nentries = 0;
7678 tail_copy->cpy_hdr.entries_pageable =
7679 copy->cpy_hdr.entries_pageable;
7680 vm_map_store_init(&tail_copy->cpy_hdr);
7681
7682 tail_copy->offset = copy->offset + copy->size - tail_size;
7683 tail_copy->size = tail_size;
7684
7685 copy->size -= tail_size;
7686
7687 entry = vm_map_copy_last_entry(copy);
7688 vm_map_copy_clip_start(copy, entry, tail_copy->offset);
7689 entry = vm_map_copy_last_entry(copy);
7690 vm_map_copy_entry_unlink(copy, entry);
7691 vm_map_copy_entry_link(tail_copy,
7692 vm_map_copy_last_entry(tail_copy),
7693 entry);
7694 }
7695
7696 /*
7697 * Copy most (or possibly all) of the data.
7698 */
7699 kr = vm_map_copy_overwrite_nested(dst_map,
7700 dst_addr + head_size,
7701 copy,
7702 interruptible,
7703 (pmap_t) NULL,
7704 FALSE);
7705 if (kr != KERN_SUCCESS) {
7706 goto done;
7707 }
7708
7709 if (tail_size) {
7710 kr = vm_map_copy_overwrite_nested(dst_map,
7711 tail_addr,
7712 tail_copy,
7713 interruptible,
7714 (pmap_t) NULL,
7715 FALSE);
7716 }
7717
7718 done:
7719 assert(copy->type == VM_MAP_COPY_ENTRY_LIST);
7720 if (kr == KERN_SUCCESS) {
7721 /*
7722 * Discard all the copy maps.
7723 */
7724 if (head_copy) {
7725 vm_map_copy_discard(head_copy);
7726 head_copy = NULL;
7727 }
7728 vm_map_copy_discard(copy);
7729 if (tail_copy) {
7730 vm_map_copy_discard(tail_copy);
7731 tail_copy = NULL;
7732 }
7733 } else {
7734 /*
7735 * Re-assemble the original copy map.
7736 */
7737 if (head_copy) {
7738 entry = vm_map_copy_first_entry(head_copy);
7739 vm_map_copy_entry_unlink(head_copy, entry);
7740 vm_map_copy_entry_link(copy,
7741 vm_map_copy_to_entry(copy),
7742 entry);
7743 copy->offset -= head_size;
7744 copy->size += head_size;
7745 vm_map_copy_discard(head_copy);
7746 head_copy = NULL;
7747 }
7748 if (tail_copy) {
7749 entry = vm_map_copy_last_entry(tail_copy);
7750 vm_map_copy_entry_unlink(tail_copy, entry);
7751 vm_map_copy_entry_link(copy,
7752 vm_map_copy_last_entry(copy),
7753 entry);
7754 copy->size += tail_size;
7755 vm_map_copy_discard(tail_copy);
7756 tail_copy = NULL;
7757 }
7758 }
7759 return kr;
7760 }
7761
7762
7763 /*
7764 * Routine: vm_map_copy_overwrite_unaligned [internal use only]
7765 *
7766 * Decription:
7767 * Physically copy unaligned data
7768 *
7769 * Implementation:
7770 * Unaligned parts of pages have to be physically copied. We use
7771 * a modified form of vm_fault_copy (which understands none-aligned
7772 * page offsets and sizes) to do the copy. We attempt to copy as
7773 * much memory in one go as possibly, however vm_fault_copy copies
7774 * within 1 memory object so we have to find the smaller of "amount left"
7775 * "source object data size" and "target object data size". With
7776 * unaligned data we don't need to split regions, therefore the source
7777 * (copy) object should be one map entry, the target range may be split
7778 * over multiple map entries however. In any event we are pessimistic
7779 * about these assumptions.
7780 *
7781 * Assumptions:
7782 * dst_map is locked on entry and is return locked on success,
7783 * unlocked on error.
7784 */
7785
7786 static kern_return_t
7787 vm_map_copy_overwrite_unaligned(
7788 vm_map_t dst_map,
7789 vm_map_entry_t entry,
7790 vm_map_copy_t copy,
7791 vm_map_offset_t start,
7792 boolean_t discard_on_success)
7793 {
7794 vm_map_entry_t copy_entry;
7795 vm_map_entry_t copy_entry_next;
7796 vm_map_version_t version;
7797 vm_object_t dst_object;
7798 vm_object_offset_t dst_offset;
7799 vm_object_offset_t src_offset;
7800 vm_object_offset_t entry_offset;
7801 vm_map_offset_t entry_end;
7802 vm_map_size_t src_size,
7803 dst_size,
7804 copy_size,
7805 amount_left;
7806 kern_return_t kr = KERN_SUCCESS;
7807
7808
7809 copy_entry = vm_map_copy_first_entry(copy);
7810
7811 vm_map_lock_write_to_read(dst_map);
7812
7813 src_offset = copy->offset - vm_object_trunc_page(copy->offset);
7814 amount_left = copy->size;
7815 /*
7816 * unaligned so we never clipped this entry, we need the offset into
7817 * the vm_object not just the data.
7818 */
7819 while (amount_left > 0) {
7820
7821 if (entry == vm_map_to_entry(dst_map)) {
7822 vm_map_unlock_read(dst_map);
7823 return KERN_INVALID_ADDRESS;
7824 }
7825
7826 /* "start" must be within the current map entry */
7827 assert ((start>=entry->vme_start) && (start<entry->vme_end));
7828
7829 dst_offset = start - entry->vme_start;
7830
7831 dst_size = entry->vme_end - start;
7832
7833 src_size = copy_entry->vme_end -
7834 (copy_entry->vme_start + src_offset);
7835
7836 if (dst_size < src_size) {
7837 /*
7838 * we can only copy dst_size bytes before
7839 * we have to get the next destination entry
7840 */
7841 copy_size = dst_size;
7842 } else {
7843 /*
7844 * we can only copy src_size bytes before
7845 * we have to get the next source copy entry
7846 */
7847 copy_size = src_size;
7848 }
7849
7850 if (copy_size > amount_left) {
7851 copy_size = amount_left;
7852 }
7853 /*
7854 * Entry needs copy, create a shadow shadow object for
7855 * Copy on write region.
7856 */
7857 if (entry->needs_copy &&
7858 ((entry->protection & VM_PROT_WRITE) != 0))
7859 {
7860 if (vm_map_lock_read_to_write(dst_map)) {
7861 vm_map_lock_read(dst_map);
7862 goto RetryLookup;
7863 }
7864 VME_OBJECT_SHADOW(entry,
7865 (vm_map_size_t)(entry->vme_end
7866 - entry->vme_start));
7867 entry->needs_copy = FALSE;
7868 vm_map_lock_write_to_read(dst_map);
7869 }
7870 dst_object = VME_OBJECT(entry);
7871 /*
7872 * unlike with the virtual (aligned) copy we're going
7873 * to fault on it therefore we need a target object.
7874 */
7875 if (dst_object == VM_OBJECT_NULL) {
7876 if (vm_map_lock_read_to_write(dst_map)) {
7877 vm_map_lock_read(dst_map);
7878 goto RetryLookup;
7879 }
7880 dst_object = vm_object_allocate((vm_map_size_t)
7881 entry->vme_end - entry->vme_start);
7882 VME_OBJECT(entry) = dst_object;
7883 VME_OFFSET_SET(entry, 0);
7884 assert(entry->use_pmap);
7885 vm_map_lock_write_to_read(dst_map);
7886 }
7887 /*
7888 * Take an object reference and unlock map. The "entry" may
7889 * disappear or change when the map is unlocked.
7890 */
7891 vm_object_reference(dst_object);
7892 version.main_timestamp = dst_map->timestamp;
7893 entry_offset = VME_OFFSET(entry);
7894 entry_end = entry->vme_end;
7895 vm_map_unlock_read(dst_map);
7896 /*
7897 * Copy as much as possible in one pass
7898 */
7899 kr = vm_fault_copy(
7900 VME_OBJECT(copy_entry),
7901 VME_OFFSET(copy_entry) + src_offset,
7902 &copy_size,
7903 dst_object,
7904 entry_offset + dst_offset,
7905 dst_map,
7906 &version,
7907 THREAD_UNINT );
7908
7909 start += copy_size;
7910 src_offset += copy_size;
7911 amount_left -= copy_size;
7912 /*
7913 * Release the object reference
7914 */
7915 vm_object_deallocate(dst_object);
7916 /*
7917 * If a hard error occurred, return it now
7918 */
7919 if (kr != KERN_SUCCESS)
7920 return kr;
7921
7922 if ((copy_entry->vme_start + src_offset) == copy_entry->vme_end
7923 || amount_left == 0)
7924 {
7925 /*
7926 * all done with this copy entry, dispose.
7927 */
7928 copy_entry_next = copy_entry->vme_next;
7929
7930 if (discard_on_success) {
7931 vm_map_copy_entry_unlink(copy, copy_entry);
7932 assert(!copy_entry->is_sub_map);
7933 vm_object_deallocate(VME_OBJECT(copy_entry));
7934 vm_map_copy_entry_dispose(copy, copy_entry);
7935 }
7936
7937 if (copy_entry_next == vm_map_copy_to_entry(copy) &&
7938 amount_left) {
7939 /*
7940 * not finished copying but run out of source
7941 */
7942 return KERN_INVALID_ADDRESS;
7943 }
7944
7945 copy_entry = copy_entry_next;
7946
7947 src_offset = 0;
7948 }
7949
7950 if (amount_left == 0)
7951 return KERN_SUCCESS;
7952
7953 vm_map_lock_read(dst_map);
7954 if (version.main_timestamp == dst_map->timestamp) {
7955 if (start == entry_end) {
7956 /*
7957 * destination region is split. Use the version
7958 * information to avoid a lookup in the normal
7959 * case.
7960 */
7961 entry = entry->vme_next;
7962 /*
7963 * should be contiguous. Fail if we encounter
7964 * a hole in the destination.
7965 */
7966 if (start != entry->vme_start) {
7967 vm_map_unlock_read(dst_map);
7968 return KERN_INVALID_ADDRESS ;
7969 }
7970 }
7971 } else {
7972 /*
7973 * Map version check failed.
7974 * we must lookup the entry because somebody
7975 * might have changed the map behind our backs.
7976 */
7977 RetryLookup:
7978 if (!vm_map_lookup_entry(dst_map, start, &entry))
7979 {
7980 vm_map_unlock_read(dst_map);
7981 return KERN_INVALID_ADDRESS ;
7982 }
7983 }
7984 }/* while */
7985
7986 return KERN_SUCCESS;
7987 }/* vm_map_copy_overwrite_unaligned */
7988
7989 /*
7990 * Routine: vm_map_copy_overwrite_aligned [internal use only]
7991 *
7992 * Description:
7993 * Does all the vm_trickery possible for whole pages.
7994 *
7995 * Implementation:
7996 *
7997 * If there are no permanent objects in the destination,
7998 * and the source and destination map entry zones match,
7999 * and the destination map entry is not shared,
8000 * then the map entries can be deleted and replaced
8001 * with those from the copy. The following code is the
8002 * basic idea of what to do, but there are lots of annoying
8003 * little details about getting protection and inheritance
8004 * right. Should add protection, inheritance, and sharing checks
8005 * to the above pass and make sure that no wiring is involved.
8006 */
8007
8008 int vm_map_copy_overwrite_aligned_src_not_internal = 0;
8009 int vm_map_copy_overwrite_aligned_src_not_symmetric = 0;
8010 int vm_map_copy_overwrite_aligned_src_large = 0;
8011
8012 static kern_return_t
8013 vm_map_copy_overwrite_aligned(
8014 vm_map_t dst_map,
8015 vm_map_entry_t tmp_entry,
8016 vm_map_copy_t copy,
8017 vm_map_offset_t start,
8018 __unused pmap_t pmap)
8019 {
8020 vm_object_t object;
8021 vm_map_entry_t copy_entry;
8022 vm_map_size_t copy_size;
8023 vm_map_size_t size;
8024 vm_map_entry_t entry;
8025
8026 while ((copy_entry = vm_map_copy_first_entry(copy))
8027 != vm_map_copy_to_entry(copy))
8028 {
8029 copy_size = (copy_entry->vme_end - copy_entry->vme_start);
8030
8031 entry = tmp_entry;
8032 if (entry->is_sub_map) {
8033 /* unnested when clipped earlier */
8034 assert(!entry->use_pmap);
8035 }
8036 if (entry == vm_map_to_entry(dst_map)) {
8037 vm_map_unlock(dst_map);
8038 return KERN_INVALID_ADDRESS;
8039 }
8040 size = (entry->vme_end - entry->vme_start);
8041 /*
8042 * Make sure that no holes popped up in the
8043 * address map, and that the protection is
8044 * still valid, in case the map was unlocked
8045 * earlier.
8046 */
8047
8048 if ((entry->vme_start != start) || ((entry->is_sub_map)
8049 && !entry->needs_copy)) {
8050 vm_map_unlock(dst_map);
8051 return(KERN_INVALID_ADDRESS);
8052 }
8053 assert(entry != vm_map_to_entry(dst_map));
8054
8055 /*
8056 * Check protection again
8057 */
8058
8059 if ( ! (entry->protection & VM_PROT_WRITE)) {
8060 vm_map_unlock(dst_map);
8061 return(KERN_PROTECTION_FAILURE);
8062 }
8063
8064 /*
8065 * Adjust to source size first
8066 */
8067
8068 if (copy_size < size) {
8069 if (entry->map_aligned &&
8070 !VM_MAP_PAGE_ALIGNED(entry->vme_start + copy_size,
8071 VM_MAP_PAGE_MASK(dst_map))) {
8072 /* no longer map-aligned */
8073 entry->map_aligned = FALSE;
8074 }
8075 vm_map_clip_end(dst_map, entry, entry->vme_start + copy_size);
8076 size = copy_size;
8077 }
8078
8079 /*
8080 * Adjust to destination size
8081 */
8082
8083 if (size < copy_size) {
8084 vm_map_copy_clip_end(copy, copy_entry,
8085 copy_entry->vme_start + size);
8086 copy_size = size;
8087 }
8088
8089 assert((entry->vme_end - entry->vme_start) == size);
8090 assert((tmp_entry->vme_end - tmp_entry->vme_start) == size);
8091 assert((copy_entry->vme_end - copy_entry->vme_start) == size);
8092
8093 /*
8094 * If the destination contains temporary unshared memory,
8095 * we can perform the copy by throwing it away and
8096 * installing the source data.
8097 */
8098
8099 object = VME_OBJECT(entry);
8100 if ((!entry->is_shared &&
8101 ((object == VM_OBJECT_NULL) ||
8102 (object->internal && !object->true_share))) ||
8103 entry->needs_copy) {
8104 vm_object_t old_object = VME_OBJECT(entry);
8105 vm_object_offset_t old_offset = VME_OFFSET(entry);
8106 vm_object_offset_t offset;
8107
8108 /*
8109 * Ensure that the source and destination aren't
8110 * identical
8111 */
8112 if (old_object == VME_OBJECT(copy_entry) &&
8113 old_offset == VME_OFFSET(copy_entry)) {
8114 vm_map_copy_entry_unlink(copy, copy_entry);
8115 vm_map_copy_entry_dispose(copy, copy_entry);
8116
8117 if (old_object != VM_OBJECT_NULL)
8118 vm_object_deallocate(old_object);
8119
8120 start = tmp_entry->vme_end;
8121 tmp_entry = tmp_entry->vme_next;
8122 continue;
8123 }
8124
8125 #define __TRADEOFF1_OBJ_SIZE (64 * 1024 * 1024) /* 64 MB */
8126 #define __TRADEOFF1_COPY_SIZE (128 * 1024) /* 128 KB */
8127 if (VME_OBJECT(copy_entry) != VM_OBJECT_NULL &&
8128 VME_OBJECT(copy_entry)->vo_size >= __TRADEOFF1_OBJ_SIZE &&
8129 copy_size <= __TRADEOFF1_COPY_SIZE) {
8130 /*
8131 * Virtual vs. Physical copy tradeoff #1.
8132 *
8133 * Copying only a few pages out of a large
8134 * object: do a physical copy instead of
8135 * a virtual copy, to avoid possibly keeping
8136 * the entire large object alive because of
8137 * those few copy-on-write pages.
8138 */
8139 vm_map_copy_overwrite_aligned_src_large++;
8140 goto slow_copy;
8141 }
8142
8143 if ((dst_map->pmap != kernel_pmap) &&
8144 (VME_ALIAS(entry) >= VM_MEMORY_MALLOC) &&
8145 (VME_ALIAS(entry) <= VM_MEMORY_MALLOC_LARGE_REUSED)) {
8146 vm_object_t new_object, new_shadow;
8147
8148 /*
8149 * We're about to map something over a mapping
8150 * established by malloc()...
8151 */
8152 new_object = VME_OBJECT(copy_entry);
8153 if (new_object != VM_OBJECT_NULL) {
8154 vm_object_lock_shared(new_object);
8155 }
8156 while (new_object != VM_OBJECT_NULL &&
8157 !new_object->true_share &&
8158 new_object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC &&
8159 new_object->internal) {
8160 new_shadow = new_object->shadow;
8161 if (new_shadow == VM_OBJECT_NULL) {
8162 break;
8163 }
8164 vm_object_lock_shared(new_shadow);
8165 vm_object_unlock(new_object);
8166 new_object = new_shadow;
8167 }
8168 if (new_object != VM_OBJECT_NULL) {
8169 if (!new_object->internal) {
8170 /*
8171 * The new mapping is backed
8172 * by an external object. We
8173 * don't want malloc'ed memory
8174 * to be replaced with such a
8175 * non-anonymous mapping, so
8176 * let's go off the optimized
8177 * path...
8178 */
8179 vm_map_copy_overwrite_aligned_src_not_internal++;
8180 vm_object_unlock(new_object);
8181 goto slow_copy;
8182 }
8183 if (new_object->true_share ||
8184 new_object->copy_strategy != MEMORY_OBJECT_COPY_SYMMETRIC) {
8185 /*
8186 * Same if there's a "true_share"
8187 * object in the shadow chain, or
8188 * an object with a non-default
8189 * (SYMMETRIC) copy strategy.
8190 */
8191 vm_map_copy_overwrite_aligned_src_not_symmetric++;
8192 vm_object_unlock(new_object);
8193 goto slow_copy;
8194 }
8195 vm_object_unlock(new_object);
8196 }
8197 /*
8198 * The new mapping is still backed by
8199 * anonymous (internal) memory, so it's
8200 * OK to substitute it for the original
8201 * malloc() mapping.
8202 */
8203 }
8204
8205 if (old_object != VM_OBJECT_NULL) {
8206 if(entry->is_sub_map) {
8207 if(entry->use_pmap) {
8208 #ifndef NO_NESTED_PMAP
8209 pmap_unnest(dst_map->pmap,
8210 (addr64_t)entry->vme_start,
8211 entry->vme_end - entry->vme_start);
8212 #endif /* NO_NESTED_PMAP */
8213 if(dst_map->mapped_in_other_pmaps) {
8214 /* clean up parent */
8215 /* map/maps */
8216 vm_map_submap_pmap_clean(
8217 dst_map, entry->vme_start,
8218 entry->vme_end,
8219 VME_SUBMAP(entry),
8220 VME_OFFSET(entry));
8221 }
8222 } else {
8223 vm_map_submap_pmap_clean(
8224 dst_map, entry->vme_start,
8225 entry->vme_end,
8226 VME_SUBMAP(entry),
8227 VME_OFFSET(entry));
8228 }
8229 vm_map_deallocate(VME_SUBMAP(entry));
8230 } else {
8231 if(dst_map->mapped_in_other_pmaps) {
8232 vm_object_pmap_protect_options(
8233 VME_OBJECT(entry),
8234 VME_OFFSET(entry),
8235 entry->vme_end
8236 - entry->vme_start,
8237 PMAP_NULL,
8238 entry->vme_start,
8239 VM_PROT_NONE,
8240 PMAP_OPTIONS_REMOVE);
8241 } else {
8242 pmap_remove_options(
8243 dst_map->pmap,
8244 (addr64_t)(entry->vme_start),
8245 (addr64_t)(entry->vme_end),
8246 PMAP_OPTIONS_REMOVE);
8247 }
8248 vm_object_deallocate(old_object);
8249 }
8250 }
8251
8252 entry->is_sub_map = FALSE;
8253 VME_OBJECT_SET(entry, VME_OBJECT(copy_entry));
8254 object = VME_OBJECT(entry);
8255 entry->needs_copy = copy_entry->needs_copy;
8256 entry->wired_count = 0;
8257 entry->user_wired_count = 0;
8258 offset = VME_OFFSET(copy_entry);
8259 VME_OFFSET_SET(entry, offset);
8260
8261 vm_map_copy_entry_unlink(copy, copy_entry);
8262 vm_map_copy_entry_dispose(copy, copy_entry);
8263
8264 /*
8265 * we could try to push pages into the pmap at this point, BUT
8266 * this optimization only saved on average 2 us per page if ALL
8267 * the pages in the source were currently mapped
8268 * and ALL the pages in the dest were touched, if there were fewer
8269 * than 2/3 of the pages touched, this optimization actually cost more cycles
8270 * it also puts a lot of pressure on the pmap layer w/r to mapping structures
8271 */
8272
8273 /*
8274 * Set up for the next iteration. The map
8275 * has not been unlocked, so the next
8276 * address should be at the end of this
8277 * entry, and the next map entry should be
8278 * the one following it.
8279 */
8280
8281 start = tmp_entry->vme_end;
8282 tmp_entry = tmp_entry->vme_next;
8283 } else {
8284 vm_map_version_t version;
8285 vm_object_t dst_object;
8286 vm_object_offset_t dst_offset;
8287 kern_return_t r;
8288
8289 slow_copy:
8290 if (entry->needs_copy) {
8291 VME_OBJECT_SHADOW(entry,
8292 (entry->vme_end -
8293 entry->vme_start));
8294 entry->needs_copy = FALSE;
8295 }
8296
8297 dst_object = VME_OBJECT(entry);
8298 dst_offset = VME_OFFSET(entry);
8299
8300 /*
8301 * Take an object reference, and record
8302 * the map version information so that the
8303 * map can be safely unlocked.
8304 */
8305
8306 if (dst_object == VM_OBJECT_NULL) {
8307 /*
8308 * We would usually have just taken the
8309 * optimized path above if the destination
8310 * object has not been allocated yet. But we
8311 * now disable that optimization if the copy
8312 * entry's object is not backed by anonymous
8313 * memory to avoid replacing malloc'ed
8314 * (i.e. re-usable) anonymous memory with a
8315 * not-so-anonymous mapping.
8316 * So we have to handle this case here and
8317 * allocate a new VM object for this map entry.
8318 */
8319 dst_object = vm_object_allocate(
8320 entry->vme_end - entry->vme_start);
8321 dst_offset = 0;
8322 VME_OBJECT_SET(entry, dst_object);
8323 VME_OFFSET_SET(entry, dst_offset);
8324 assert(entry->use_pmap);
8325
8326 }
8327
8328 vm_object_reference(dst_object);
8329
8330 /* account for unlock bumping up timestamp */
8331 version.main_timestamp = dst_map->timestamp + 1;
8332
8333 vm_map_unlock(dst_map);
8334
8335 /*
8336 * Copy as much as possible in one pass
8337 */
8338
8339 copy_size = size;
8340 r = vm_fault_copy(
8341 VME_OBJECT(copy_entry),
8342 VME_OFFSET(copy_entry),
8343 &copy_size,
8344 dst_object,
8345 dst_offset,
8346 dst_map,
8347 &version,
8348 THREAD_UNINT );
8349
8350 /*
8351 * Release the object reference
8352 */
8353
8354 vm_object_deallocate(dst_object);
8355
8356 /*
8357 * If a hard error occurred, return it now
8358 */
8359
8360 if (r != KERN_SUCCESS)
8361 return(r);
8362
8363 if (copy_size != 0) {
8364 /*
8365 * Dispose of the copied region
8366 */
8367
8368 vm_map_copy_clip_end(copy, copy_entry,
8369 copy_entry->vme_start + copy_size);
8370 vm_map_copy_entry_unlink(copy, copy_entry);
8371 vm_object_deallocate(VME_OBJECT(copy_entry));
8372 vm_map_copy_entry_dispose(copy, copy_entry);
8373 }
8374
8375 /*
8376 * Pick up in the destination map where we left off.
8377 *
8378 * Use the version information to avoid a lookup
8379 * in the normal case.
8380 */
8381
8382 start += copy_size;
8383 vm_map_lock(dst_map);
8384 if (version.main_timestamp == dst_map->timestamp &&
8385 copy_size != 0) {
8386 /* We can safely use saved tmp_entry value */
8387
8388 if (tmp_entry->map_aligned &&
8389 !VM_MAP_PAGE_ALIGNED(
8390 start,
8391 VM_MAP_PAGE_MASK(dst_map))) {
8392 /* no longer map-aligned */
8393 tmp_entry->map_aligned = FALSE;
8394 }
8395 vm_map_clip_end(dst_map, tmp_entry, start);
8396 tmp_entry = tmp_entry->vme_next;
8397 } else {
8398 /* Must do lookup of tmp_entry */
8399
8400 if (!vm_map_lookup_entry(dst_map, start, &tmp_entry)) {
8401 vm_map_unlock(dst_map);
8402 return(KERN_INVALID_ADDRESS);
8403 }
8404 if (tmp_entry->map_aligned &&
8405 !VM_MAP_PAGE_ALIGNED(
8406 start,
8407 VM_MAP_PAGE_MASK(dst_map))) {
8408 /* no longer map-aligned */
8409 tmp_entry->map_aligned = FALSE;
8410 }
8411 vm_map_clip_start(dst_map, tmp_entry, start);
8412 }
8413 }
8414 }/* while */
8415
8416 return(KERN_SUCCESS);
8417 }/* vm_map_copy_overwrite_aligned */
8418
8419 /*
8420 * Routine: vm_map_copyin_kernel_buffer [internal use only]
8421 *
8422 * Description:
8423 * Copy in data to a kernel buffer from space in the
8424 * source map. The original space may be optionally
8425 * deallocated.
8426 *
8427 * If successful, returns a new copy object.
8428 */
8429 static kern_return_t
8430 vm_map_copyin_kernel_buffer(
8431 vm_map_t src_map,
8432 vm_map_offset_t src_addr,
8433 vm_map_size_t len,
8434 boolean_t src_destroy,
8435 vm_map_copy_t *copy_result)
8436 {
8437 kern_return_t kr;
8438 vm_map_copy_t copy;
8439 vm_size_t kalloc_size;
8440
8441 if (len > msg_ool_size_small)
8442 return KERN_INVALID_ARGUMENT;
8443
8444 kalloc_size = (vm_size_t)(cpy_kdata_hdr_sz + len);
8445
8446 copy = (vm_map_copy_t)kalloc(kalloc_size);
8447 if (copy == VM_MAP_COPY_NULL)
8448 return KERN_RESOURCE_SHORTAGE;
8449 copy->type = VM_MAP_COPY_KERNEL_BUFFER;
8450 copy->size = len;
8451 copy->offset = 0;
8452
8453 kr = copyinmap(src_map, src_addr, copy->cpy_kdata, (vm_size_t)len);
8454 if (kr != KERN_SUCCESS) {
8455 kfree(copy, kalloc_size);
8456 return kr;
8457 }
8458 if (src_destroy) {
8459 (void) vm_map_remove(
8460 src_map,
8461 vm_map_trunc_page(src_addr,
8462 VM_MAP_PAGE_MASK(src_map)),
8463 vm_map_round_page(src_addr + len,
8464 VM_MAP_PAGE_MASK(src_map)),
8465 (VM_MAP_REMOVE_INTERRUPTIBLE |
8466 VM_MAP_REMOVE_WAIT_FOR_KWIRE |
8467 (src_map == kernel_map) ? VM_MAP_REMOVE_KUNWIRE : 0));
8468 }
8469 *copy_result = copy;
8470 return KERN_SUCCESS;
8471 }
8472
8473 /*
8474 * Routine: vm_map_copyout_kernel_buffer [internal use only]
8475 *
8476 * Description:
8477 * Copy out data from a kernel buffer into space in the
8478 * destination map. The space may be otpionally dynamically
8479 * allocated.
8480 *
8481 * If successful, consumes the copy object.
8482 * Otherwise, the caller is responsible for it.
8483 */
8484 static int vm_map_copyout_kernel_buffer_failures = 0;
8485 static kern_return_t
8486 vm_map_copyout_kernel_buffer(
8487 vm_map_t map,
8488 vm_map_address_t *addr, /* IN/OUT */
8489 vm_map_copy_t copy,
8490 boolean_t overwrite,
8491 boolean_t consume_on_success)
8492 {
8493 kern_return_t kr = KERN_SUCCESS;
8494 thread_t thread = current_thread();
8495
8496 /*
8497 * check for corrupted vm_map_copy structure
8498 */
8499 if (copy->size > msg_ool_size_small || copy->offset)
8500 panic("Invalid vm_map_copy_t sz:%lld, ofst:%lld",
8501 (long long)copy->size, (long long)copy->offset);
8502
8503 if (!overwrite) {
8504
8505 /*
8506 * Allocate space in the target map for the data
8507 */
8508 *addr = 0;
8509 kr = vm_map_enter(map,
8510 addr,
8511 vm_map_round_page(copy->size,
8512 VM_MAP_PAGE_MASK(map)),
8513 (vm_map_offset_t) 0,
8514 VM_FLAGS_ANYWHERE,
8515 VM_OBJECT_NULL,
8516 (vm_object_offset_t) 0,
8517 FALSE,
8518 VM_PROT_DEFAULT,
8519 VM_PROT_ALL,
8520 VM_INHERIT_DEFAULT);
8521 if (kr != KERN_SUCCESS)
8522 return kr;
8523 }
8524
8525 /*
8526 * Copyout the data from the kernel buffer to the target map.
8527 */
8528 if (thread->map == map) {
8529
8530 /*
8531 * If the target map is the current map, just do
8532 * the copy.
8533 */
8534 assert((vm_size_t) copy->size == copy->size);
8535 if (copyout(copy->cpy_kdata, *addr, (vm_size_t) copy->size)) {
8536 kr = KERN_INVALID_ADDRESS;
8537 }
8538 }
8539 else {
8540 vm_map_t oldmap;
8541
8542 /*
8543 * If the target map is another map, assume the
8544 * target's address space identity for the duration
8545 * of the copy.
8546 */
8547 vm_map_reference(map);
8548 oldmap = vm_map_switch(map);
8549
8550 assert((vm_size_t) copy->size == copy->size);
8551 if (copyout(copy->cpy_kdata, *addr, (vm_size_t) copy->size)) {
8552 vm_map_copyout_kernel_buffer_failures++;
8553 kr = KERN_INVALID_ADDRESS;
8554 }
8555
8556 (void) vm_map_switch(oldmap);
8557 vm_map_deallocate(map);
8558 }
8559
8560 if (kr != KERN_SUCCESS) {
8561 /* the copy failed, clean up */
8562 if (!overwrite) {
8563 /*
8564 * Deallocate the space we allocated in the target map.
8565 */
8566 (void) vm_map_remove(
8567 map,
8568 vm_map_trunc_page(*addr,
8569 VM_MAP_PAGE_MASK(map)),
8570 vm_map_round_page((*addr +
8571 vm_map_round_page(copy->size,
8572 VM_MAP_PAGE_MASK(map))),
8573 VM_MAP_PAGE_MASK(map)),
8574 VM_MAP_NO_FLAGS);
8575 *addr = 0;
8576 }
8577 } else {
8578 /* copy was successful, dicard the copy structure */
8579 if (consume_on_success) {
8580 kfree(copy, copy->size + cpy_kdata_hdr_sz);
8581 }
8582 }
8583
8584 return kr;
8585 }
8586
8587 /*
8588 * Macro: vm_map_copy_insert
8589 *
8590 * Description:
8591 * Link a copy chain ("copy") into a map at the
8592 * specified location (after "where").
8593 * Side effects:
8594 * The copy chain is destroyed.
8595 * Warning:
8596 * The arguments are evaluated multiple times.
8597 */
8598 #define vm_map_copy_insert(map, where, copy) \
8599 MACRO_BEGIN \
8600 vm_map_store_copy_insert(map, where, copy); \
8601 zfree(vm_map_copy_zone, copy); \
8602 MACRO_END
8603
8604 void
8605 vm_map_copy_remap(
8606 vm_map_t map,
8607 vm_map_entry_t where,
8608 vm_map_copy_t copy,
8609 vm_map_offset_t adjustment,
8610 vm_prot_t cur_prot,
8611 vm_prot_t max_prot,
8612 vm_inherit_t inheritance)
8613 {
8614 vm_map_entry_t copy_entry, new_entry;
8615
8616 for (copy_entry = vm_map_copy_first_entry(copy);
8617 copy_entry != vm_map_copy_to_entry(copy);
8618 copy_entry = copy_entry->vme_next) {
8619 /* get a new VM map entry for the map */
8620 new_entry = vm_map_entry_create(map,
8621 !map->hdr.entries_pageable);
8622 /* copy the "copy entry" to the new entry */
8623 vm_map_entry_copy(new_entry, copy_entry);
8624 /* adjust "start" and "end" */
8625 new_entry->vme_start += adjustment;
8626 new_entry->vme_end += adjustment;
8627 /* clear some attributes */
8628 new_entry->inheritance = inheritance;
8629 new_entry->protection = cur_prot;
8630 new_entry->max_protection = max_prot;
8631 new_entry->behavior = VM_BEHAVIOR_DEFAULT;
8632 /* take an extra reference on the entry's "object" */
8633 if (new_entry->is_sub_map) {
8634 assert(!new_entry->use_pmap); /* not nested */
8635 vm_map_lock(VME_SUBMAP(new_entry));
8636 vm_map_reference(VME_SUBMAP(new_entry));
8637 vm_map_unlock(VME_SUBMAP(new_entry));
8638 } else {
8639 vm_object_reference(VME_OBJECT(new_entry));
8640 }
8641 /* insert the new entry in the map */
8642 vm_map_store_entry_link(map, where, new_entry);
8643 /* continue inserting the "copy entries" after the new entry */
8644 where = new_entry;
8645 }
8646 }
8647
8648 /*
8649 * Routine: vm_map_copyout
8650 *
8651 * Description:
8652 * Copy out a copy chain ("copy") into newly-allocated
8653 * space in the destination map.
8654 *
8655 * If successful, consumes the copy object.
8656 * Otherwise, the caller is responsible for it.
8657 */
8658
8659 kern_return_t
8660 vm_map_copyout(
8661 vm_map_t dst_map,
8662 vm_map_address_t *dst_addr, /* OUT */
8663 vm_map_copy_t copy)
8664 {
8665 return vm_map_copyout_internal(dst_map, dst_addr, copy,
8666 TRUE, /* consume_on_success */
8667 VM_PROT_DEFAULT,
8668 VM_PROT_ALL,
8669 VM_INHERIT_DEFAULT);
8670 }
8671
8672 kern_return_t
8673 vm_map_copyout_internal(
8674 vm_map_t dst_map,
8675 vm_map_address_t *dst_addr, /* OUT */
8676 vm_map_copy_t copy,
8677 boolean_t consume_on_success,
8678 vm_prot_t cur_protection,
8679 vm_prot_t max_protection,
8680 vm_inherit_t inheritance)
8681 {
8682 vm_map_size_t size;
8683 vm_map_size_t adjustment;
8684 vm_map_offset_t start;
8685 vm_object_offset_t vm_copy_start;
8686 vm_map_entry_t last;
8687 vm_map_entry_t entry;
8688 vm_map_entry_t hole_entry;
8689
8690 /*
8691 * Check for null copy object.
8692 */
8693
8694 if (copy == VM_MAP_COPY_NULL) {
8695 *dst_addr = 0;
8696 return(KERN_SUCCESS);
8697 }
8698
8699 /*
8700 * Check for special copy object, created
8701 * by vm_map_copyin_object.
8702 */
8703
8704 if (copy->type == VM_MAP_COPY_OBJECT) {
8705 vm_object_t object = copy->cpy_object;
8706 kern_return_t kr;
8707 vm_object_offset_t offset;
8708
8709 offset = vm_object_trunc_page(copy->offset);
8710 size = vm_map_round_page((copy->size +
8711 (vm_map_size_t)(copy->offset -
8712 offset)),
8713 VM_MAP_PAGE_MASK(dst_map));
8714 *dst_addr = 0;
8715 kr = vm_map_enter(dst_map, dst_addr, size,
8716 (vm_map_offset_t) 0, VM_FLAGS_ANYWHERE,
8717 object, offset, FALSE,
8718 VM_PROT_DEFAULT, VM_PROT_ALL,
8719 VM_INHERIT_DEFAULT);
8720 if (kr != KERN_SUCCESS)
8721 return(kr);
8722 /* Account for non-pagealigned copy object */
8723 *dst_addr += (vm_map_offset_t)(copy->offset - offset);
8724 if (consume_on_success)
8725 zfree(vm_map_copy_zone, copy);
8726 return(KERN_SUCCESS);
8727 }
8728
8729 /*
8730 * Check for special kernel buffer allocated
8731 * by new_ipc_kmsg_copyin.
8732 */
8733
8734 if (copy->type == VM_MAP_COPY_KERNEL_BUFFER) {
8735 return vm_map_copyout_kernel_buffer(dst_map, dst_addr,
8736 copy, FALSE,
8737 consume_on_success);
8738 }
8739
8740
8741 /*
8742 * Find space for the data
8743 */
8744
8745 vm_copy_start = vm_map_trunc_page((vm_map_size_t)copy->offset,
8746 VM_MAP_COPY_PAGE_MASK(copy));
8747 size = vm_map_round_page((vm_map_size_t)copy->offset + copy->size,
8748 VM_MAP_COPY_PAGE_MASK(copy))
8749 - vm_copy_start;
8750
8751
8752 StartAgain: ;
8753
8754 vm_map_lock(dst_map);
8755 if( dst_map->disable_vmentry_reuse == TRUE) {
8756 VM_MAP_HIGHEST_ENTRY(dst_map, entry, start);
8757 last = entry;
8758 } else {
8759 if (dst_map->holelistenabled) {
8760 hole_entry = (vm_map_entry_t)dst_map->holes_list;
8761
8762 if (hole_entry == NULL) {
8763 /*
8764 * No more space in the map?
8765 */
8766 vm_map_unlock(dst_map);
8767 return(KERN_NO_SPACE);
8768 }
8769
8770 last = hole_entry;
8771 start = last->vme_start;
8772 } else {
8773 assert(first_free_is_valid(dst_map));
8774 start = ((last = dst_map->first_free) == vm_map_to_entry(dst_map)) ?
8775 vm_map_min(dst_map) : last->vme_end;
8776 }
8777 start = vm_map_round_page(start,
8778 VM_MAP_PAGE_MASK(dst_map));
8779 }
8780
8781 while (TRUE) {
8782 vm_map_entry_t next = last->vme_next;
8783 vm_map_offset_t end = start + size;
8784
8785 if ((end > dst_map->max_offset) || (end < start)) {
8786 if (dst_map->wait_for_space) {
8787 if (size <= (dst_map->max_offset - dst_map->min_offset)) {
8788 assert_wait((event_t) dst_map,
8789 THREAD_INTERRUPTIBLE);
8790 vm_map_unlock(dst_map);
8791 thread_block(THREAD_CONTINUE_NULL);
8792 goto StartAgain;
8793 }
8794 }
8795 vm_map_unlock(dst_map);
8796 return(KERN_NO_SPACE);
8797 }
8798
8799 if (dst_map->holelistenabled) {
8800 if (last->vme_end >= end)
8801 break;
8802 } else {
8803 /*
8804 * If there are no more entries, we must win.
8805 *
8806 * OR
8807 *
8808 * If there is another entry, it must be
8809 * after the end of the potential new region.
8810 */
8811
8812 if (next == vm_map_to_entry(dst_map))
8813 break;
8814
8815 if (next->vme_start >= end)
8816 break;
8817 }
8818
8819 last = next;
8820
8821 if (dst_map->holelistenabled) {
8822 if (last == (vm_map_entry_t) dst_map->holes_list) {
8823 /*
8824 * Wrapped around
8825 */
8826 vm_map_unlock(dst_map);
8827 return(KERN_NO_SPACE);
8828 }
8829 start = last->vme_start;
8830 } else {
8831 start = last->vme_end;
8832 }
8833 start = vm_map_round_page(start,
8834 VM_MAP_PAGE_MASK(dst_map));
8835 }
8836
8837 if (dst_map->holelistenabled) {
8838 if (vm_map_lookup_entry(dst_map, last->vme_start, &last)) {
8839 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", last, (unsigned long long)last->vme_start);
8840 }
8841 }
8842
8843
8844 adjustment = start - vm_copy_start;
8845 if (! consume_on_success) {
8846 /*
8847 * We're not allowed to consume "copy", so we'll have to
8848 * copy its map entries into the destination map below.
8849 * No need to re-allocate map entries from the correct
8850 * (pageable or not) zone, since we'll get new map entries
8851 * during the transfer.
8852 * We'll also adjust the map entries's "start" and "end"
8853 * during the transfer, to keep "copy"'s entries consistent
8854 * with its "offset".
8855 */
8856 goto after_adjustments;
8857 }
8858
8859 /*
8860 * Since we're going to just drop the map
8861 * entries from the copy into the destination
8862 * map, they must come from the same pool.
8863 */
8864
8865 if (copy->cpy_hdr.entries_pageable != dst_map->hdr.entries_pageable) {
8866 /*
8867 * Mismatches occur when dealing with the default
8868 * pager.
8869 */
8870 zone_t old_zone;
8871 vm_map_entry_t next, new;
8872
8873 /*
8874 * Find the zone that the copies were allocated from
8875 */
8876
8877 entry = vm_map_copy_first_entry(copy);
8878
8879 /*
8880 * Reinitialize the copy so that vm_map_copy_entry_link
8881 * will work.
8882 */
8883 vm_map_store_copy_reset(copy, entry);
8884 copy->cpy_hdr.entries_pageable = dst_map->hdr.entries_pageable;
8885
8886 /*
8887 * Copy each entry.
8888 */
8889 while (entry != vm_map_copy_to_entry(copy)) {
8890 new = vm_map_copy_entry_create(copy, !copy->cpy_hdr.entries_pageable);
8891 vm_map_entry_copy_full(new, entry);
8892 assert(!new->iokit_acct);
8893 if (new->is_sub_map) {
8894 /* clr address space specifics */
8895 new->use_pmap = FALSE;
8896 }
8897 vm_map_copy_entry_link(copy,
8898 vm_map_copy_last_entry(copy),
8899 new);
8900 next = entry->vme_next;
8901 old_zone = entry->from_reserved_zone ? vm_map_entry_reserved_zone : vm_map_entry_zone;
8902 zfree(old_zone, entry);
8903 entry = next;
8904 }
8905 }
8906
8907 /*
8908 * Adjust the addresses in the copy chain, and
8909 * reset the region attributes.
8910 */
8911
8912 for (entry = vm_map_copy_first_entry(copy);
8913 entry != vm_map_copy_to_entry(copy);
8914 entry = entry->vme_next) {
8915 if (VM_MAP_PAGE_SHIFT(dst_map) == PAGE_SHIFT) {
8916 /*
8917 * We're injecting this copy entry into a map that
8918 * has the standard page alignment, so clear
8919 * "map_aligned" (which might have been inherited
8920 * from the original map entry).
8921 */
8922 entry->map_aligned = FALSE;
8923 }
8924
8925 entry->vme_start += adjustment;
8926 entry->vme_end += adjustment;
8927
8928 if (entry->map_aligned) {
8929 assert(VM_MAP_PAGE_ALIGNED(entry->vme_start,
8930 VM_MAP_PAGE_MASK(dst_map)));
8931 assert(VM_MAP_PAGE_ALIGNED(entry->vme_end,
8932 VM_MAP_PAGE_MASK(dst_map)));
8933 }
8934
8935 entry->inheritance = VM_INHERIT_DEFAULT;
8936 entry->protection = VM_PROT_DEFAULT;
8937 entry->max_protection = VM_PROT_ALL;
8938 entry->behavior = VM_BEHAVIOR_DEFAULT;
8939
8940 /*
8941 * If the entry is now wired,
8942 * map the pages into the destination map.
8943 */
8944 if (entry->wired_count != 0) {
8945 register vm_map_offset_t va;
8946 vm_object_offset_t offset;
8947 register vm_object_t object;
8948 vm_prot_t prot;
8949 int type_of_fault;
8950
8951 object = VME_OBJECT(entry);
8952 offset = VME_OFFSET(entry);
8953 va = entry->vme_start;
8954
8955 pmap_pageable(dst_map->pmap,
8956 entry->vme_start,
8957 entry->vme_end,
8958 TRUE);
8959
8960 while (va < entry->vme_end) {
8961 register vm_page_t m;
8962
8963 /*
8964 * Look up the page in the object.
8965 * Assert that the page will be found in the
8966 * top object:
8967 * either
8968 * the object was newly created by
8969 * vm_object_copy_slowly, and has
8970 * copies of all of the pages from
8971 * the source object
8972 * or
8973 * the object was moved from the old
8974 * map entry; because the old map
8975 * entry was wired, all of the pages
8976 * were in the top-level object.
8977 * (XXX not true if we wire pages for
8978 * reading)
8979 */
8980 vm_object_lock(object);
8981
8982 m = vm_page_lookup(object, offset);
8983 if (m == VM_PAGE_NULL || !VM_PAGE_WIRED(m) ||
8984 m->absent)
8985 panic("vm_map_copyout: wiring %p", m);
8986
8987 /*
8988 * ENCRYPTED SWAP:
8989 * The page is assumed to be wired here, so it
8990 * shouldn't be encrypted. Otherwise, we
8991 * couldn't enter it in the page table, since
8992 * we don't want the user to see the encrypted
8993 * data.
8994 */
8995 ASSERT_PAGE_DECRYPTED(m);
8996
8997 prot = entry->protection;
8998
8999 if (override_nx(dst_map, VME_ALIAS(entry)) &&
9000 prot)
9001 prot |= VM_PROT_EXECUTE;
9002
9003 type_of_fault = DBG_CACHE_HIT_FAULT;
9004
9005 vm_fault_enter(m, dst_map->pmap, va, prot, prot,
9006 VM_PAGE_WIRED(m), FALSE, FALSE,
9007 FALSE, VME_ALIAS(entry),
9008 ((entry->iokit_acct ||
9009 (!entry->is_sub_map &&
9010 !entry->use_pmap))
9011 ? PMAP_OPTIONS_ALT_ACCT
9012 : 0),
9013 NULL, &type_of_fault);
9014
9015 vm_object_unlock(object);
9016
9017 offset += PAGE_SIZE_64;
9018 va += PAGE_SIZE;
9019 }
9020 }
9021 }
9022
9023 after_adjustments:
9024
9025 /*
9026 * Correct the page alignment for the result
9027 */
9028
9029 *dst_addr = start + (copy->offset - vm_copy_start);
9030
9031 /*
9032 * Update the hints and the map size
9033 */
9034
9035 if (consume_on_success) {
9036 SAVE_HINT_MAP_WRITE(dst_map, vm_map_copy_last_entry(copy));
9037 } else {
9038 SAVE_HINT_MAP_WRITE(dst_map, last);
9039 }
9040
9041 dst_map->size += size;
9042
9043 /*
9044 * Link in the copy
9045 */
9046
9047 if (consume_on_success) {
9048 vm_map_copy_insert(dst_map, last, copy);
9049 } else {
9050 vm_map_copy_remap(dst_map, last, copy, adjustment,
9051 cur_protection, max_protection,
9052 inheritance);
9053 }
9054
9055 vm_map_unlock(dst_map);
9056
9057 /*
9058 * XXX If wiring_required, call vm_map_pageable
9059 */
9060
9061 return(KERN_SUCCESS);
9062 }
9063
9064 /*
9065 * Routine: vm_map_copyin
9066 *
9067 * Description:
9068 * see vm_map_copyin_common. Exported via Unsupported.exports.
9069 *
9070 */
9071
9072 #undef vm_map_copyin
9073
9074 kern_return_t
9075 vm_map_copyin(
9076 vm_map_t src_map,
9077 vm_map_address_t src_addr,
9078 vm_map_size_t len,
9079 boolean_t src_destroy,
9080 vm_map_copy_t *copy_result) /* OUT */
9081 {
9082 return(vm_map_copyin_common(src_map, src_addr, len, src_destroy,
9083 FALSE, copy_result, FALSE));
9084 }
9085
9086 /*
9087 * Routine: vm_map_copyin_common
9088 *
9089 * Description:
9090 * Copy the specified region (src_addr, len) from the
9091 * source address space (src_map), possibly removing
9092 * the region from the source address space (src_destroy).
9093 *
9094 * Returns:
9095 * A vm_map_copy_t object (copy_result), suitable for
9096 * insertion into another address space (using vm_map_copyout),
9097 * copying over another address space region (using
9098 * vm_map_copy_overwrite). If the copy is unused, it
9099 * should be destroyed (using vm_map_copy_discard).
9100 *
9101 * In/out conditions:
9102 * The source map should not be locked on entry.
9103 */
9104
9105 typedef struct submap_map {
9106 vm_map_t parent_map;
9107 vm_map_offset_t base_start;
9108 vm_map_offset_t base_end;
9109 vm_map_size_t base_len;
9110 struct submap_map *next;
9111 } submap_map_t;
9112
9113 kern_return_t
9114 vm_map_copyin_common(
9115 vm_map_t src_map,
9116 vm_map_address_t src_addr,
9117 vm_map_size_t len,
9118 boolean_t src_destroy,
9119 __unused boolean_t src_volatile,
9120 vm_map_copy_t *copy_result, /* OUT */
9121 boolean_t use_maxprot)
9122 {
9123 vm_map_entry_t tmp_entry; /* Result of last map lookup --
9124 * in multi-level lookup, this
9125 * entry contains the actual
9126 * vm_object/offset.
9127 */
9128 register
9129 vm_map_entry_t new_entry = VM_MAP_ENTRY_NULL; /* Map entry for copy */
9130
9131 vm_map_offset_t src_start; /* Start of current entry --
9132 * where copy is taking place now
9133 */
9134 vm_map_offset_t src_end; /* End of entire region to be
9135 * copied */
9136 vm_map_offset_t src_base;
9137 vm_map_t base_map = src_map;
9138 boolean_t map_share=FALSE;
9139 submap_map_t *parent_maps = NULL;
9140
9141 register
9142 vm_map_copy_t copy; /* Resulting copy */
9143 vm_map_address_t copy_addr;
9144 vm_map_size_t copy_size;
9145
9146 /*
9147 * Check for copies of zero bytes.
9148 */
9149
9150 if (len == 0) {
9151 *copy_result = VM_MAP_COPY_NULL;
9152 return(KERN_SUCCESS);
9153 }
9154
9155 /*
9156 * Check that the end address doesn't overflow
9157 */
9158 src_end = src_addr + len;
9159 if (src_end < src_addr)
9160 return KERN_INVALID_ADDRESS;
9161
9162 /*
9163 * If the copy is sufficiently small, use a kernel buffer instead
9164 * of making a virtual copy. The theory being that the cost of
9165 * setting up VM (and taking C-O-W faults) dominates the copy costs
9166 * for small regions.
9167 */
9168 if ((len < msg_ool_size_small) && !use_maxprot)
9169 return vm_map_copyin_kernel_buffer(src_map, src_addr, len,
9170 src_destroy, copy_result);
9171
9172 /*
9173 * Compute (page aligned) start and end of region
9174 */
9175 src_start = vm_map_trunc_page(src_addr,
9176 VM_MAP_PAGE_MASK(src_map));
9177 src_end = vm_map_round_page(src_end,
9178 VM_MAP_PAGE_MASK(src_map));
9179
9180 XPR(XPR_VM_MAP, "vm_map_copyin_common map 0x%x addr 0x%x len 0x%x dest %d\n", src_map, src_addr, len, src_destroy, 0);
9181
9182 /*
9183 * Allocate a header element for the list.
9184 *
9185 * Use the start and end in the header to
9186 * remember the endpoints prior to rounding.
9187 */
9188
9189 copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
9190 copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
9191 vm_map_copy_first_entry(copy) =
9192 vm_map_copy_last_entry(copy) = vm_map_copy_to_entry(copy);
9193 copy->type = VM_MAP_COPY_ENTRY_LIST;
9194 copy->cpy_hdr.nentries = 0;
9195 copy->cpy_hdr.entries_pageable = TRUE;
9196 #if 00
9197 copy->cpy_hdr.page_shift = src_map->hdr.page_shift;
9198 #else
9199 /*
9200 * The copy entries can be broken down for a variety of reasons,
9201 * so we can't guarantee that they will remain map-aligned...
9202 * Will need to adjust the first copy_entry's "vme_start" and
9203 * the last copy_entry's "vme_end" to be rounded to PAGE_MASK
9204 * rather than the original map's alignment.
9205 */
9206 copy->cpy_hdr.page_shift = PAGE_SHIFT;
9207 #endif
9208
9209 vm_map_store_init( &(copy->cpy_hdr) );
9210
9211 copy->offset = src_addr;
9212 copy->size = len;
9213
9214 new_entry = vm_map_copy_entry_create(copy, !copy->cpy_hdr.entries_pageable);
9215
9216 #define RETURN(x) \
9217 MACRO_BEGIN \
9218 vm_map_unlock(src_map); \
9219 if(src_map != base_map) \
9220 vm_map_deallocate(src_map); \
9221 if (new_entry != VM_MAP_ENTRY_NULL) \
9222 vm_map_copy_entry_dispose(copy,new_entry); \
9223 vm_map_copy_discard(copy); \
9224 { \
9225 submap_map_t *_ptr; \
9226 \
9227 for(_ptr = parent_maps; _ptr != NULL; _ptr = parent_maps) { \
9228 parent_maps=parent_maps->next; \
9229 if (_ptr->parent_map != base_map) \
9230 vm_map_deallocate(_ptr->parent_map); \
9231 kfree(_ptr, sizeof(submap_map_t)); \
9232 } \
9233 } \
9234 MACRO_RETURN(x); \
9235 MACRO_END
9236
9237 /*
9238 * Find the beginning of the region.
9239 */
9240
9241 vm_map_lock(src_map);
9242
9243 /*
9244 * Lookup the original "src_addr" rather than the truncated
9245 * "src_start", in case "src_start" falls in a non-map-aligned
9246 * map entry *before* the map entry that contains "src_addr"...
9247 */
9248 if (!vm_map_lookup_entry(src_map, src_addr, &tmp_entry))
9249 RETURN(KERN_INVALID_ADDRESS);
9250 if(!tmp_entry->is_sub_map) {
9251 /*
9252 * ... but clip to the map-rounded "src_start" rather than
9253 * "src_addr" to preserve map-alignment. We'll adjust the
9254 * first copy entry at the end, if needed.
9255 */
9256 vm_map_clip_start(src_map, tmp_entry, src_start);
9257 }
9258 if (src_start < tmp_entry->vme_start) {
9259 /*
9260 * Move "src_start" up to the start of the
9261 * first map entry to copy.
9262 */
9263 src_start = tmp_entry->vme_start;
9264 }
9265 /* set for later submap fix-up */
9266 copy_addr = src_start;
9267
9268 /*
9269 * Go through entries until we get to the end.
9270 */
9271
9272 while (TRUE) {
9273 register
9274 vm_map_entry_t src_entry = tmp_entry; /* Top-level entry */
9275 vm_map_size_t src_size; /* Size of source
9276 * map entry (in both
9277 * maps)
9278 */
9279
9280 register
9281 vm_object_t src_object; /* Object to copy */
9282 vm_object_offset_t src_offset;
9283
9284 boolean_t src_needs_copy; /* Should source map
9285 * be made read-only
9286 * for copy-on-write?
9287 */
9288
9289 boolean_t new_entry_needs_copy; /* Will new entry be COW? */
9290
9291 boolean_t was_wired; /* Was source wired? */
9292 vm_map_version_t version; /* Version before locks
9293 * dropped to make copy
9294 */
9295 kern_return_t result; /* Return value from
9296 * copy_strategically.
9297 */
9298 while(tmp_entry->is_sub_map) {
9299 vm_map_size_t submap_len;
9300 submap_map_t *ptr;
9301
9302 ptr = (submap_map_t *)kalloc(sizeof(submap_map_t));
9303 ptr->next = parent_maps;
9304 parent_maps = ptr;
9305 ptr->parent_map = src_map;
9306 ptr->base_start = src_start;
9307 ptr->base_end = src_end;
9308 submap_len = tmp_entry->vme_end - src_start;
9309 if(submap_len > (src_end-src_start))
9310 submap_len = src_end-src_start;
9311 ptr->base_len = submap_len;
9312
9313 src_start -= tmp_entry->vme_start;
9314 src_start += VME_OFFSET(tmp_entry);
9315 src_end = src_start + submap_len;
9316 src_map = VME_SUBMAP(tmp_entry);
9317 vm_map_lock(src_map);
9318 /* keep an outstanding reference for all maps in */
9319 /* the parents tree except the base map */
9320 vm_map_reference(src_map);
9321 vm_map_unlock(ptr->parent_map);
9322 if (!vm_map_lookup_entry(
9323 src_map, src_start, &tmp_entry))
9324 RETURN(KERN_INVALID_ADDRESS);
9325 map_share = TRUE;
9326 if(!tmp_entry->is_sub_map)
9327 vm_map_clip_start(src_map, tmp_entry, src_start);
9328 src_entry = tmp_entry;
9329 }
9330 /* we are now in the lowest level submap... */
9331
9332 if ((VME_OBJECT(tmp_entry) != VM_OBJECT_NULL) &&
9333 (VME_OBJECT(tmp_entry)->phys_contiguous)) {
9334 /* This is not, supported for now.In future */
9335 /* we will need to detect the phys_contig */
9336 /* condition and then upgrade copy_slowly */
9337 /* to do physical copy from the device mem */
9338 /* based object. We can piggy-back off of */
9339 /* the was wired boolean to set-up the */
9340 /* proper handling */
9341 RETURN(KERN_PROTECTION_FAILURE);
9342 }
9343 /*
9344 * Create a new address map entry to hold the result.
9345 * Fill in the fields from the appropriate source entries.
9346 * We must unlock the source map to do this if we need
9347 * to allocate a map entry.
9348 */
9349 if (new_entry == VM_MAP_ENTRY_NULL) {
9350 version.main_timestamp = src_map->timestamp;
9351 vm_map_unlock(src_map);
9352
9353 new_entry = vm_map_copy_entry_create(copy, !copy->cpy_hdr.entries_pageable);
9354
9355 vm_map_lock(src_map);
9356 if ((version.main_timestamp + 1) != src_map->timestamp) {
9357 if (!vm_map_lookup_entry(src_map, src_start,
9358 &tmp_entry)) {
9359 RETURN(KERN_INVALID_ADDRESS);
9360 }
9361 if (!tmp_entry->is_sub_map)
9362 vm_map_clip_start(src_map, tmp_entry, src_start);
9363 continue; /* restart w/ new tmp_entry */
9364 }
9365 }
9366
9367 /*
9368 * Verify that the region can be read.
9369 */
9370 if (((src_entry->protection & VM_PROT_READ) == VM_PROT_NONE &&
9371 !use_maxprot) ||
9372 (src_entry->max_protection & VM_PROT_READ) == 0)
9373 RETURN(KERN_PROTECTION_FAILURE);
9374
9375 /*
9376 * Clip against the endpoints of the entire region.
9377 */
9378
9379 vm_map_clip_end(src_map, src_entry, src_end);
9380
9381 src_size = src_entry->vme_end - src_start;
9382 src_object = VME_OBJECT(src_entry);
9383 src_offset = VME_OFFSET(src_entry);
9384 was_wired = (src_entry->wired_count != 0);
9385
9386 vm_map_entry_copy(new_entry, src_entry);
9387 if (new_entry->is_sub_map) {
9388 /* clr address space specifics */
9389 new_entry->use_pmap = FALSE;
9390 }
9391
9392 /*
9393 * Attempt non-blocking copy-on-write optimizations.
9394 */
9395
9396 if (src_destroy &&
9397 (src_object == VM_OBJECT_NULL ||
9398 (src_object->internal && !src_object->true_share
9399 && !map_share))) {
9400 /*
9401 * If we are destroying the source, and the object
9402 * is internal, we can move the object reference
9403 * from the source to the copy. The copy is
9404 * copy-on-write only if the source is.
9405 * We make another reference to the object, because
9406 * destroying the source entry will deallocate it.
9407 */
9408 vm_object_reference(src_object);
9409
9410 /*
9411 * Copy is always unwired. vm_map_copy_entry
9412 * set its wired count to zero.
9413 */
9414
9415 goto CopySuccessful;
9416 }
9417
9418
9419 RestartCopy:
9420 XPR(XPR_VM_MAP, "vm_map_copyin_common src_obj 0x%x ent 0x%x obj 0x%x was_wired %d\n",
9421 src_object, new_entry, VME_OBJECT(new_entry),
9422 was_wired, 0);
9423 if ((src_object == VM_OBJECT_NULL ||
9424 (!was_wired && !map_share && !tmp_entry->is_shared)) &&
9425 vm_object_copy_quickly(
9426 &VME_OBJECT(new_entry),
9427 src_offset,
9428 src_size,
9429 &src_needs_copy,
9430 &new_entry_needs_copy)) {
9431
9432 new_entry->needs_copy = new_entry_needs_copy;
9433
9434 /*
9435 * Handle copy-on-write obligations
9436 */
9437
9438 if (src_needs_copy && !tmp_entry->needs_copy) {
9439 vm_prot_t prot;
9440
9441 prot = src_entry->protection & ~VM_PROT_WRITE;
9442
9443 if (override_nx(src_map, VME_ALIAS(src_entry))
9444 && prot)
9445 prot |= VM_PROT_EXECUTE;
9446
9447 vm_object_pmap_protect(
9448 src_object,
9449 src_offset,
9450 src_size,
9451 (src_entry->is_shared ?
9452 PMAP_NULL
9453 : src_map->pmap),
9454 src_entry->vme_start,
9455 prot);
9456
9457 assert(tmp_entry->wired_count == 0);
9458 tmp_entry->needs_copy = TRUE;
9459 }
9460
9461 /*
9462 * The map has never been unlocked, so it's safe
9463 * to move to the next entry rather than doing
9464 * another lookup.
9465 */
9466
9467 goto CopySuccessful;
9468 }
9469
9470 /*
9471 * Take an object reference, so that we may
9472 * release the map lock(s).
9473 */
9474
9475 assert(src_object != VM_OBJECT_NULL);
9476 vm_object_reference(src_object);
9477
9478 /*
9479 * Record the timestamp for later verification.
9480 * Unlock the map.
9481 */
9482
9483 version.main_timestamp = src_map->timestamp;
9484 vm_map_unlock(src_map); /* Increments timestamp once! */
9485
9486 /*
9487 * Perform the copy
9488 */
9489
9490 if (was_wired) {
9491 CopySlowly:
9492 vm_object_lock(src_object);
9493 result = vm_object_copy_slowly(
9494 src_object,
9495 src_offset,
9496 src_size,
9497 THREAD_UNINT,
9498 &VME_OBJECT(new_entry));
9499 VME_OFFSET_SET(new_entry, 0);
9500 new_entry->needs_copy = FALSE;
9501
9502 }
9503 else if (src_object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC &&
9504 (tmp_entry->is_shared || map_share)) {
9505 vm_object_t new_object;
9506
9507 vm_object_lock_shared(src_object);
9508 new_object = vm_object_copy_delayed(
9509 src_object,
9510 src_offset,
9511 src_size,
9512 TRUE);
9513 if (new_object == VM_OBJECT_NULL)
9514 goto CopySlowly;
9515
9516 VME_OBJECT_SET(new_entry, new_object);
9517 assert(new_entry->wired_count == 0);
9518 new_entry->needs_copy = TRUE;
9519 assert(!new_entry->iokit_acct);
9520 assert(new_object->purgable == VM_PURGABLE_DENY);
9521 new_entry->use_pmap = TRUE;
9522 result = KERN_SUCCESS;
9523
9524 } else {
9525 vm_object_offset_t new_offset;
9526 new_offset = VME_OFFSET(new_entry);
9527 result = vm_object_copy_strategically(src_object,
9528 src_offset,
9529 src_size,
9530 &VME_OBJECT(new_entry),
9531 &new_offset,
9532 &new_entry_needs_copy);
9533 if (new_offset != VME_OFFSET(new_entry)) {
9534 VME_OFFSET_SET(new_entry, new_offset);
9535 }
9536
9537 new_entry->needs_copy = new_entry_needs_copy;
9538 }
9539
9540 if (result != KERN_SUCCESS &&
9541 result != KERN_MEMORY_RESTART_COPY) {
9542 vm_map_lock(src_map);
9543 RETURN(result);
9544 }
9545
9546 /*
9547 * Throw away the extra reference
9548 */
9549
9550 vm_object_deallocate(src_object);
9551
9552 /*
9553 * Verify that the map has not substantially
9554 * changed while the copy was being made.
9555 */
9556
9557 vm_map_lock(src_map);
9558
9559 if ((version.main_timestamp + 1) == src_map->timestamp)
9560 goto VerificationSuccessful;
9561
9562 /*
9563 * Simple version comparison failed.
9564 *
9565 * Retry the lookup and verify that the
9566 * same object/offset are still present.
9567 *
9568 * [Note: a memory manager that colludes with
9569 * the calling task can detect that we have
9570 * cheated. While the map was unlocked, the
9571 * mapping could have been changed and restored.]
9572 */
9573
9574 if (!vm_map_lookup_entry(src_map, src_start, &tmp_entry)) {
9575 if (result != KERN_MEMORY_RESTART_COPY) {
9576 vm_object_deallocate(VME_OBJECT(new_entry));
9577 VME_OBJECT_SET(new_entry, VM_OBJECT_NULL);
9578 assert(!new_entry->iokit_acct);
9579 new_entry->use_pmap = TRUE;
9580 }
9581 RETURN(KERN_INVALID_ADDRESS);
9582 }
9583
9584 src_entry = tmp_entry;
9585 vm_map_clip_start(src_map, src_entry, src_start);
9586
9587 if ((((src_entry->protection & VM_PROT_READ) == VM_PROT_NONE) &&
9588 !use_maxprot) ||
9589 ((src_entry->max_protection & VM_PROT_READ) == 0))
9590 goto VerificationFailed;
9591
9592 if (src_entry->vme_end < new_entry->vme_end) {
9593 assert(VM_MAP_PAGE_ALIGNED(src_entry->vme_end,
9594 VM_MAP_COPY_PAGE_MASK(copy)));
9595 new_entry->vme_end = src_entry->vme_end;
9596 src_size = new_entry->vme_end - src_start;
9597 }
9598
9599 if ((VME_OBJECT(src_entry) != src_object) ||
9600 (VME_OFFSET(src_entry) != src_offset) ) {
9601
9602 /*
9603 * Verification failed.
9604 *
9605 * Start over with this top-level entry.
9606 */
9607
9608 VerificationFailed: ;
9609
9610 vm_object_deallocate(VME_OBJECT(new_entry));
9611 tmp_entry = src_entry;
9612 continue;
9613 }
9614
9615 /*
9616 * Verification succeeded.
9617 */
9618
9619 VerificationSuccessful: ;
9620
9621 if (result == KERN_MEMORY_RESTART_COPY)
9622 goto RestartCopy;
9623
9624 /*
9625 * Copy succeeded.
9626 */
9627
9628 CopySuccessful: ;
9629
9630 /*
9631 * Link in the new copy entry.
9632 */
9633
9634 vm_map_copy_entry_link(copy, vm_map_copy_last_entry(copy),
9635 new_entry);
9636
9637 /*
9638 * Determine whether the entire region
9639 * has been copied.
9640 */
9641 src_base = src_start;
9642 src_start = new_entry->vme_end;
9643 new_entry = VM_MAP_ENTRY_NULL;
9644 while ((src_start >= src_end) && (src_end != 0)) {
9645 submap_map_t *ptr;
9646
9647 if (src_map == base_map) {
9648 /* back to the top */
9649 break;
9650 }
9651
9652 ptr = parent_maps;
9653 assert(ptr != NULL);
9654 parent_maps = parent_maps->next;
9655
9656 /* fix up the damage we did in that submap */
9657 vm_map_simplify_range(src_map,
9658 src_base,
9659 src_end);
9660
9661 vm_map_unlock(src_map);
9662 vm_map_deallocate(src_map);
9663 vm_map_lock(ptr->parent_map);
9664 src_map = ptr->parent_map;
9665 src_base = ptr->base_start;
9666 src_start = ptr->base_start + ptr->base_len;
9667 src_end = ptr->base_end;
9668 if (!vm_map_lookup_entry(src_map,
9669 src_start,
9670 &tmp_entry) &&
9671 (src_end > src_start)) {
9672 RETURN(KERN_INVALID_ADDRESS);
9673 }
9674 kfree(ptr, sizeof(submap_map_t));
9675 if (parent_maps == NULL)
9676 map_share = FALSE;
9677 src_entry = tmp_entry->vme_prev;
9678 }
9679
9680 if ((VM_MAP_PAGE_SHIFT(src_map) != PAGE_SHIFT) &&
9681 (src_start >= src_addr + len) &&
9682 (src_addr + len != 0)) {
9683 /*
9684 * Stop copying now, even though we haven't reached
9685 * "src_end". We'll adjust the end of the last copy
9686 * entry at the end, if needed.
9687 *
9688 * If src_map's aligment is different from the
9689 * system's page-alignment, there could be
9690 * extra non-map-aligned map entries between
9691 * the original (non-rounded) "src_addr + len"
9692 * and the rounded "src_end".
9693 * We do not want to copy those map entries since
9694 * they're not part of the copied range.
9695 */
9696 break;
9697 }
9698
9699 if ((src_start >= src_end) && (src_end != 0))
9700 break;
9701
9702 /*
9703 * Verify that there are no gaps in the region
9704 */
9705
9706 tmp_entry = src_entry->vme_next;
9707 if ((tmp_entry->vme_start != src_start) ||
9708 (tmp_entry == vm_map_to_entry(src_map))) {
9709 RETURN(KERN_INVALID_ADDRESS);
9710 }
9711 }
9712
9713 /*
9714 * If the source should be destroyed, do it now, since the
9715 * copy was successful.
9716 */
9717 if (src_destroy) {
9718 (void) vm_map_delete(
9719 src_map,
9720 vm_map_trunc_page(src_addr,
9721 VM_MAP_PAGE_MASK(src_map)),
9722 src_end,
9723 ((src_map == kernel_map) ?
9724 VM_MAP_REMOVE_KUNWIRE :
9725 VM_MAP_NO_FLAGS),
9726 VM_MAP_NULL);
9727 } else {
9728 /* fix up the damage we did in the base map */
9729 vm_map_simplify_range(
9730 src_map,
9731 vm_map_trunc_page(src_addr,
9732 VM_MAP_PAGE_MASK(src_map)),
9733 vm_map_round_page(src_end,
9734 VM_MAP_PAGE_MASK(src_map)));
9735 }
9736
9737 vm_map_unlock(src_map);
9738
9739 if (VM_MAP_PAGE_SHIFT(src_map) != PAGE_SHIFT) {
9740 vm_map_offset_t original_start, original_offset, original_end;
9741
9742 assert(VM_MAP_COPY_PAGE_MASK(copy) == PAGE_MASK);
9743
9744 /* adjust alignment of first copy_entry's "vme_start" */
9745 tmp_entry = vm_map_copy_first_entry(copy);
9746 if (tmp_entry != vm_map_copy_to_entry(copy)) {
9747 vm_map_offset_t adjustment;
9748
9749 original_start = tmp_entry->vme_start;
9750 original_offset = VME_OFFSET(tmp_entry);
9751
9752 /* map-align the start of the first copy entry... */
9753 adjustment = (tmp_entry->vme_start -
9754 vm_map_trunc_page(
9755 tmp_entry->vme_start,
9756 VM_MAP_PAGE_MASK(src_map)));
9757 tmp_entry->vme_start -= adjustment;
9758 VME_OFFSET_SET(tmp_entry,
9759 VME_OFFSET(tmp_entry) - adjustment);
9760 copy_addr -= adjustment;
9761 assert(tmp_entry->vme_start < tmp_entry->vme_end);
9762 /* ... adjust for mis-aligned start of copy range */
9763 adjustment =
9764 (vm_map_trunc_page(copy->offset,
9765 PAGE_MASK) -
9766 vm_map_trunc_page(copy->offset,
9767 VM_MAP_PAGE_MASK(src_map)));
9768 if (adjustment) {
9769 assert(page_aligned(adjustment));
9770 assert(adjustment < VM_MAP_PAGE_SIZE(src_map));
9771 tmp_entry->vme_start += adjustment;
9772 VME_OFFSET_SET(tmp_entry,
9773 (VME_OFFSET(tmp_entry) +
9774 adjustment));
9775 copy_addr += adjustment;
9776 assert(tmp_entry->vme_start < tmp_entry->vme_end);
9777 }
9778
9779 /*
9780 * Assert that the adjustments haven't exposed
9781 * more than was originally copied...
9782 */
9783 assert(tmp_entry->vme_start >= original_start);
9784 assert(VME_OFFSET(tmp_entry) >= original_offset);
9785 /*
9786 * ... and that it did not adjust outside of a
9787 * a single 16K page.
9788 */
9789 assert(vm_map_trunc_page(tmp_entry->vme_start,
9790 VM_MAP_PAGE_MASK(src_map)) ==
9791 vm_map_trunc_page(original_start,
9792 VM_MAP_PAGE_MASK(src_map)));
9793 }
9794
9795 /* adjust alignment of last copy_entry's "vme_end" */
9796 tmp_entry = vm_map_copy_last_entry(copy);
9797 if (tmp_entry != vm_map_copy_to_entry(copy)) {
9798 vm_map_offset_t adjustment;
9799
9800 original_end = tmp_entry->vme_end;
9801
9802 /* map-align the end of the last copy entry... */
9803 tmp_entry->vme_end =
9804 vm_map_round_page(tmp_entry->vme_end,
9805 VM_MAP_PAGE_MASK(src_map));
9806 /* ... adjust for mis-aligned end of copy range */
9807 adjustment =
9808 (vm_map_round_page((copy->offset +
9809 copy->size),
9810 VM_MAP_PAGE_MASK(src_map)) -
9811 vm_map_round_page((copy->offset +
9812 copy->size),
9813 PAGE_MASK));
9814 if (adjustment) {
9815 assert(page_aligned(adjustment));
9816 assert(adjustment < VM_MAP_PAGE_SIZE(src_map));
9817 tmp_entry->vme_end -= adjustment;
9818 assert(tmp_entry->vme_start < tmp_entry->vme_end);
9819 }
9820
9821 /*
9822 * Assert that the adjustments haven't exposed
9823 * more than was originally copied...
9824 */
9825 assert(tmp_entry->vme_end <= original_end);
9826 /*
9827 * ... and that it did not adjust outside of a
9828 * a single 16K page.
9829 */
9830 assert(vm_map_round_page(tmp_entry->vme_end,
9831 VM_MAP_PAGE_MASK(src_map)) ==
9832 vm_map_round_page(original_end,
9833 VM_MAP_PAGE_MASK(src_map)));
9834 }
9835 }
9836
9837 /* Fix-up start and end points in copy. This is necessary */
9838 /* when the various entries in the copy object were picked */
9839 /* up from different sub-maps */
9840
9841 tmp_entry = vm_map_copy_first_entry(copy);
9842 copy_size = 0; /* compute actual size */
9843 while (tmp_entry != vm_map_copy_to_entry(copy)) {
9844 assert(VM_MAP_PAGE_ALIGNED(
9845 copy_addr + (tmp_entry->vme_end -
9846 tmp_entry->vme_start),
9847 VM_MAP_COPY_PAGE_MASK(copy)));
9848 assert(VM_MAP_PAGE_ALIGNED(
9849 copy_addr,
9850 VM_MAP_COPY_PAGE_MASK(copy)));
9851
9852 /*
9853 * The copy_entries will be injected directly into the
9854 * destination map and might not be "map aligned" there...
9855 */
9856 tmp_entry->map_aligned = FALSE;
9857
9858 tmp_entry->vme_end = copy_addr +
9859 (tmp_entry->vme_end - tmp_entry->vme_start);
9860 tmp_entry->vme_start = copy_addr;
9861 assert(tmp_entry->vme_start < tmp_entry->vme_end);
9862 copy_addr += tmp_entry->vme_end - tmp_entry->vme_start;
9863 copy_size += tmp_entry->vme_end - tmp_entry->vme_start;
9864 tmp_entry = (struct vm_map_entry *)tmp_entry->vme_next;
9865 }
9866
9867 if (VM_MAP_PAGE_SHIFT(src_map) != PAGE_SHIFT &&
9868 copy_size < copy->size) {
9869 /*
9870 * The actual size of the VM map copy is smaller than what
9871 * was requested by the caller. This must be because some
9872 * PAGE_SIZE-sized pages are missing at the end of the last
9873 * VM_MAP_PAGE_SIZE(src_map)-sized chunk of the range.
9874 * The caller might not have been aware of those missing
9875 * pages and might not want to be aware of it, which is
9876 * fine as long as they don't try to access (and crash on)
9877 * those missing pages.
9878 * Let's adjust the size of the "copy", to avoid failing
9879 * in vm_map_copyout() or vm_map_copy_overwrite().
9880 */
9881 assert(vm_map_round_page(copy_size,
9882 VM_MAP_PAGE_MASK(src_map)) ==
9883 vm_map_round_page(copy->size,
9884 VM_MAP_PAGE_MASK(src_map)));
9885 copy->size = copy_size;
9886 }
9887
9888 *copy_result = copy;
9889 return(KERN_SUCCESS);
9890
9891 #undef RETURN
9892 }
9893
9894 kern_return_t
9895 vm_map_copy_extract(
9896 vm_map_t src_map,
9897 vm_map_address_t src_addr,
9898 vm_map_size_t len,
9899 vm_map_copy_t *copy_result, /* OUT */
9900 vm_prot_t *cur_prot, /* OUT */
9901 vm_prot_t *max_prot)
9902 {
9903 vm_map_offset_t src_start, src_end;
9904 vm_map_copy_t copy;
9905 kern_return_t kr;
9906
9907 /*
9908 * Check for copies of zero bytes.
9909 */
9910
9911 if (len == 0) {
9912 *copy_result = VM_MAP_COPY_NULL;
9913 return(KERN_SUCCESS);
9914 }
9915
9916 /*
9917 * Check that the end address doesn't overflow
9918 */
9919 src_end = src_addr + len;
9920 if (src_end < src_addr)
9921 return KERN_INVALID_ADDRESS;
9922
9923 /*
9924 * Compute (page aligned) start and end of region
9925 */
9926 src_start = vm_map_trunc_page(src_addr, PAGE_MASK);
9927 src_end = vm_map_round_page(src_end, PAGE_MASK);
9928
9929 /*
9930 * Allocate a header element for the list.
9931 *
9932 * Use the start and end in the header to
9933 * remember the endpoints prior to rounding.
9934 */
9935
9936 copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
9937 copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
9938 vm_map_copy_first_entry(copy) =
9939 vm_map_copy_last_entry(copy) = vm_map_copy_to_entry(copy);
9940 copy->type = VM_MAP_COPY_ENTRY_LIST;
9941 copy->cpy_hdr.nentries = 0;
9942 copy->cpy_hdr.entries_pageable = TRUE;
9943
9944 vm_map_store_init(&copy->cpy_hdr);
9945
9946 copy->offset = 0;
9947 copy->size = len;
9948
9949 kr = vm_map_remap_extract(src_map,
9950 src_addr,
9951 len,
9952 FALSE, /* copy */
9953 &copy->cpy_hdr,
9954 cur_prot,
9955 max_prot,
9956 VM_INHERIT_SHARE,
9957 TRUE); /* pageable */
9958 if (kr != KERN_SUCCESS) {
9959 vm_map_copy_discard(copy);
9960 return kr;
9961 }
9962
9963 *copy_result = copy;
9964 return KERN_SUCCESS;
9965 }
9966
9967 /*
9968 * vm_map_copyin_object:
9969 *
9970 * Create a copy object from an object.
9971 * Our caller donates an object reference.
9972 */
9973
9974 kern_return_t
9975 vm_map_copyin_object(
9976 vm_object_t object,
9977 vm_object_offset_t offset, /* offset of region in object */
9978 vm_object_size_t size, /* size of region in object */
9979 vm_map_copy_t *copy_result) /* OUT */
9980 {
9981 vm_map_copy_t copy; /* Resulting copy */
9982
9983 /*
9984 * We drop the object into a special copy object
9985 * that contains the object directly.
9986 */
9987
9988 copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
9989 copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
9990 copy->type = VM_MAP_COPY_OBJECT;
9991 copy->cpy_object = object;
9992 copy->offset = offset;
9993 copy->size = size;
9994
9995 *copy_result = copy;
9996 return(KERN_SUCCESS);
9997 }
9998
9999 static void
10000 vm_map_fork_share(
10001 vm_map_t old_map,
10002 vm_map_entry_t old_entry,
10003 vm_map_t new_map)
10004 {
10005 vm_object_t object;
10006 vm_map_entry_t new_entry;
10007
10008 /*
10009 * New sharing code. New map entry
10010 * references original object. Internal
10011 * objects use asynchronous copy algorithm for
10012 * future copies. First make sure we have
10013 * the right object. If we need a shadow,
10014 * or someone else already has one, then
10015 * make a new shadow and share it.
10016 */
10017
10018 object = VME_OBJECT(old_entry);
10019 if (old_entry->is_sub_map) {
10020 assert(old_entry->wired_count == 0);
10021 #ifndef NO_NESTED_PMAP
10022 if(old_entry->use_pmap) {
10023 kern_return_t result;
10024
10025 result = pmap_nest(new_map->pmap,
10026 (VME_SUBMAP(old_entry))->pmap,
10027 (addr64_t)old_entry->vme_start,
10028 (addr64_t)old_entry->vme_start,
10029 (uint64_t)(old_entry->vme_end - old_entry->vme_start));
10030 if(result)
10031 panic("vm_map_fork_share: pmap_nest failed!");
10032 }
10033 #endif /* NO_NESTED_PMAP */
10034 } else if (object == VM_OBJECT_NULL) {
10035 object = vm_object_allocate((vm_map_size_t)(old_entry->vme_end -
10036 old_entry->vme_start));
10037 VME_OFFSET_SET(old_entry, 0);
10038 VME_OBJECT_SET(old_entry, object);
10039 old_entry->use_pmap = TRUE;
10040 assert(!old_entry->needs_copy);
10041 } else if (object->copy_strategy !=
10042 MEMORY_OBJECT_COPY_SYMMETRIC) {
10043
10044 /*
10045 * We are already using an asymmetric
10046 * copy, and therefore we already have
10047 * the right object.
10048 */
10049
10050 assert(! old_entry->needs_copy);
10051 }
10052 else if (old_entry->needs_copy || /* case 1 */
10053 object->shadowed || /* case 2 */
10054 (!object->true_share && /* case 3 */
10055 !old_entry->is_shared &&
10056 (object->vo_size >
10057 (vm_map_size_t)(old_entry->vme_end -
10058 old_entry->vme_start)))) {
10059
10060 /*
10061 * We need to create a shadow.
10062 * There are three cases here.
10063 * In the first case, we need to
10064 * complete a deferred symmetrical
10065 * copy that we participated in.
10066 * In the second and third cases,
10067 * we need to create the shadow so
10068 * that changes that we make to the
10069 * object do not interfere with
10070 * any symmetrical copies which
10071 * have occured (case 2) or which
10072 * might occur (case 3).
10073 *
10074 * The first case is when we had
10075 * deferred shadow object creation
10076 * via the entry->needs_copy mechanism.
10077 * This mechanism only works when
10078 * only one entry points to the source
10079 * object, and we are about to create
10080 * a second entry pointing to the
10081 * same object. The problem is that
10082 * there is no way of mapping from
10083 * an object to the entries pointing
10084 * to it. (Deferred shadow creation
10085 * works with one entry because occurs
10086 * at fault time, and we walk from the
10087 * entry to the object when handling
10088 * the fault.)
10089 *
10090 * The second case is when the object
10091 * to be shared has already been copied
10092 * with a symmetric copy, but we point
10093 * directly to the object without
10094 * needs_copy set in our entry. (This
10095 * can happen because different ranges
10096 * of an object can be pointed to by
10097 * different entries. In particular,
10098 * a single entry pointing to an object
10099 * can be split by a call to vm_inherit,
10100 * which, combined with task_create, can
10101 * result in the different entries
10102 * having different needs_copy values.)
10103 * The shadowed flag in the object allows
10104 * us to detect this case. The problem
10105 * with this case is that if this object
10106 * has or will have shadows, then we
10107 * must not perform an asymmetric copy
10108 * of this object, since such a copy
10109 * allows the object to be changed, which
10110 * will break the previous symmetrical
10111 * copies (which rely upon the object
10112 * not changing). In a sense, the shadowed
10113 * flag says "don't change this object".
10114 * We fix this by creating a shadow
10115 * object for this object, and sharing
10116 * that. This works because we are free
10117 * to change the shadow object (and thus
10118 * to use an asymmetric copy strategy);
10119 * this is also semantically correct,
10120 * since this object is temporary, and
10121 * therefore a copy of the object is
10122 * as good as the object itself. (This
10123 * is not true for permanent objects,
10124 * since the pager needs to see changes,
10125 * which won't happen if the changes
10126 * are made to a copy.)
10127 *
10128 * The third case is when the object
10129 * to be shared has parts sticking
10130 * outside of the entry we're working
10131 * with, and thus may in the future
10132 * be subject to a symmetrical copy.
10133 * (This is a preemptive version of
10134 * case 2.)
10135 */
10136 VME_OBJECT_SHADOW(old_entry,
10137 (vm_map_size_t) (old_entry->vme_end -
10138 old_entry->vme_start));
10139
10140 /*
10141 * If we're making a shadow for other than
10142 * copy on write reasons, then we have
10143 * to remove write permission.
10144 */
10145
10146 if (!old_entry->needs_copy &&
10147 (old_entry->protection & VM_PROT_WRITE)) {
10148 vm_prot_t prot;
10149
10150 prot = old_entry->protection & ~VM_PROT_WRITE;
10151
10152 if (override_nx(old_map, VME_ALIAS(old_entry)) && prot)
10153 prot |= VM_PROT_EXECUTE;
10154
10155 if (old_map->mapped_in_other_pmaps) {
10156 vm_object_pmap_protect(
10157 VME_OBJECT(old_entry),
10158 VME_OFFSET(old_entry),
10159 (old_entry->vme_end -
10160 old_entry->vme_start),
10161 PMAP_NULL,
10162 old_entry->vme_start,
10163 prot);
10164 } else {
10165 pmap_protect(old_map->pmap,
10166 old_entry->vme_start,
10167 old_entry->vme_end,
10168 prot);
10169 }
10170 }
10171
10172 old_entry->needs_copy = FALSE;
10173 object = VME_OBJECT(old_entry);
10174 }
10175
10176
10177 /*
10178 * If object was using a symmetric copy strategy,
10179 * change its copy strategy to the default
10180 * asymmetric copy strategy, which is copy_delay
10181 * in the non-norma case and copy_call in the
10182 * norma case. Bump the reference count for the
10183 * new entry.
10184 */
10185
10186 if(old_entry->is_sub_map) {
10187 vm_map_lock(VME_SUBMAP(old_entry));
10188 vm_map_reference(VME_SUBMAP(old_entry));
10189 vm_map_unlock(VME_SUBMAP(old_entry));
10190 } else {
10191 vm_object_lock(object);
10192 vm_object_reference_locked(object);
10193 if (object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC) {
10194 object->copy_strategy = MEMORY_OBJECT_COPY_DELAY;
10195 }
10196 vm_object_unlock(object);
10197 }
10198
10199 /*
10200 * Clone the entry, using object ref from above.
10201 * Mark both entries as shared.
10202 */
10203
10204 new_entry = vm_map_entry_create(new_map, FALSE); /* Never the kernel
10205 * map or descendants */
10206 vm_map_entry_copy(new_entry, old_entry);
10207 old_entry->is_shared = TRUE;
10208 new_entry->is_shared = TRUE;
10209
10210 /*
10211 * Insert the entry into the new map -- we
10212 * know we're inserting at the end of the new
10213 * map.
10214 */
10215
10216 vm_map_store_entry_link(new_map, vm_map_last_entry(new_map), new_entry);
10217
10218 /*
10219 * Update the physical map
10220 */
10221
10222 if (old_entry->is_sub_map) {
10223 /* Bill Angell pmap support goes here */
10224 } else {
10225 pmap_copy(new_map->pmap, old_map->pmap, new_entry->vme_start,
10226 old_entry->vme_end - old_entry->vme_start,
10227 old_entry->vme_start);
10228 }
10229 }
10230
10231 static boolean_t
10232 vm_map_fork_copy(
10233 vm_map_t old_map,
10234 vm_map_entry_t *old_entry_p,
10235 vm_map_t new_map)
10236 {
10237 vm_map_entry_t old_entry = *old_entry_p;
10238 vm_map_size_t entry_size = old_entry->vme_end - old_entry->vme_start;
10239 vm_map_offset_t start = old_entry->vme_start;
10240 vm_map_copy_t copy;
10241 vm_map_entry_t last = vm_map_last_entry(new_map);
10242
10243 vm_map_unlock(old_map);
10244 /*
10245 * Use maxprot version of copyin because we
10246 * care about whether this memory can ever
10247 * be accessed, not just whether it's accessible
10248 * right now.
10249 */
10250 if (vm_map_copyin_maxprot(old_map, start, entry_size, FALSE, &copy)
10251 != KERN_SUCCESS) {
10252 /*
10253 * The map might have changed while it
10254 * was unlocked, check it again. Skip
10255 * any blank space or permanently
10256 * unreadable region.
10257 */
10258 vm_map_lock(old_map);
10259 if (!vm_map_lookup_entry(old_map, start, &last) ||
10260 (last->max_protection & VM_PROT_READ) == VM_PROT_NONE) {
10261 last = last->vme_next;
10262 }
10263 *old_entry_p = last;
10264
10265 /*
10266 * XXX For some error returns, want to
10267 * XXX skip to the next element. Note
10268 * that INVALID_ADDRESS and
10269 * PROTECTION_FAILURE are handled above.
10270 */
10271
10272 return FALSE;
10273 }
10274
10275 /*
10276 * Insert the copy into the new map
10277 */
10278
10279 vm_map_copy_insert(new_map, last, copy);
10280
10281 /*
10282 * Pick up the traversal at the end of
10283 * the copied region.
10284 */
10285
10286 vm_map_lock(old_map);
10287 start += entry_size;
10288 if (! vm_map_lookup_entry(old_map, start, &last)) {
10289 last = last->vme_next;
10290 } else {
10291 if (last->vme_start == start) {
10292 /*
10293 * No need to clip here and we don't
10294 * want to cause any unnecessary
10295 * unnesting...
10296 */
10297 } else {
10298 vm_map_clip_start(old_map, last, start);
10299 }
10300 }
10301 *old_entry_p = last;
10302
10303 return TRUE;
10304 }
10305
10306 /*
10307 * vm_map_fork:
10308 *
10309 * Create and return a new map based on the old
10310 * map, according to the inheritance values on the
10311 * regions in that map.
10312 *
10313 * The source map must not be locked.
10314 */
10315 vm_map_t
10316 vm_map_fork(
10317 ledger_t ledger,
10318 vm_map_t old_map)
10319 {
10320 pmap_t new_pmap;
10321 vm_map_t new_map;
10322 vm_map_entry_t old_entry;
10323 vm_map_size_t new_size = 0, entry_size;
10324 vm_map_entry_t new_entry;
10325 boolean_t src_needs_copy;
10326 boolean_t new_entry_needs_copy;
10327 boolean_t pmap_is64bit;
10328
10329 pmap_is64bit =
10330 #if defined(__i386__) || defined(__x86_64__)
10331 old_map->pmap->pm_task_map != TASK_MAP_32BIT;
10332 #else
10333 #error Unknown architecture.
10334 #endif
10335
10336 new_pmap = pmap_create(ledger, (vm_map_size_t) 0, pmap_is64bit);
10337
10338 vm_map_reference_swap(old_map);
10339 vm_map_lock(old_map);
10340
10341 new_map = vm_map_create(new_pmap,
10342 old_map->min_offset,
10343 old_map->max_offset,
10344 old_map->hdr.entries_pageable);
10345 /* inherit the parent map's page size */
10346 vm_map_set_page_shift(new_map, VM_MAP_PAGE_SHIFT(old_map));
10347 for (
10348 old_entry = vm_map_first_entry(old_map);
10349 old_entry != vm_map_to_entry(old_map);
10350 ) {
10351
10352 entry_size = old_entry->vme_end - old_entry->vme_start;
10353
10354 switch (old_entry->inheritance) {
10355 case VM_INHERIT_NONE:
10356 break;
10357
10358 case VM_INHERIT_SHARE:
10359 vm_map_fork_share(old_map, old_entry, new_map);
10360 new_size += entry_size;
10361 break;
10362
10363 case VM_INHERIT_COPY:
10364
10365 /*
10366 * Inline the copy_quickly case;
10367 * upon failure, fall back on call
10368 * to vm_map_fork_copy.
10369 */
10370
10371 if(old_entry->is_sub_map)
10372 break;
10373 if ((old_entry->wired_count != 0) ||
10374 ((VME_OBJECT(old_entry) != NULL) &&
10375 (VME_OBJECT(old_entry)->true_share))) {
10376 goto slow_vm_map_fork_copy;
10377 }
10378
10379 new_entry = vm_map_entry_create(new_map, FALSE); /* never the kernel map or descendants */
10380 vm_map_entry_copy(new_entry, old_entry);
10381 if (new_entry->is_sub_map) {
10382 /* clear address space specifics */
10383 new_entry->use_pmap = FALSE;
10384 }
10385
10386 if (! vm_object_copy_quickly(
10387 &VME_OBJECT(new_entry),
10388 VME_OFFSET(old_entry),
10389 (old_entry->vme_end -
10390 old_entry->vme_start),
10391 &src_needs_copy,
10392 &new_entry_needs_copy)) {
10393 vm_map_entry_dispose(new_map, new_entry);
10394 goto slow_vm_map_fork_copy;
10395 }
10396
10397 /*
10398 * Handle copy-on-write obligations
10399 */
10400
10401 if (src_needs_copy && !old_entry->needs_copy) {
10402 vm_prot_t prot;
10403
10404 prot = old_entry->protection & ~VM_PROT_WRITE;
10405
10406 if (override_nx(old_map, VME_ALIAS(old_entry))
10407 && prot)
10408 prot |= VM_PROT_EXECUTE;
10409
10410 vm_object_pmap_protect(
10411 VME_OBJECT(old_entry),
10412 VME_OFFSET(old_entry),
10413 (old_entry->vme_end -
10414 old_entry->vme_start),
10415 ((old_entry->is_shared
10416 || old_map->mapped_in_other_pmaps)
10417 ? PMAP_NULL :
10418 old_map->pmap),
10419 old_entry->vme_start,
10420 prot);
10421
10422 assert(old_entry->wired_count == 0);
10423 old_entry->needs_copy = TRUE;
10424 }
10425 new_entry->needs_copy = new_entry_needs_copy;
10426
10427 /*
10428 * Insert the entry at the end
10429 * of the map.
10430 */
10431
10432 vm_map_store_entry_link(new_map, vm_map_last_entry(new_map),
10433 new_entry);
10434 new_size += entry_size;
10435 break;
10436
10437 slow_vm_map_fork_copy:
10438 if (vm_map_fork_copy(old_map, &old_entry, new_map)) {
10439 new_size += entry_size;
10440 }
10441 continue;
10442 }
10443 old_entry = old_entry->vme_next;
10444 }
10445
10446
10447 new_map->size = new_size;
10448 vm_map_unlock(old_map);
10449 vm_map_deallocate(old_map);
10450
10451 return(new_map);
10452 }
10453
10454 /*
10455 * vm_map_exec:
10456 *
10457 * Setup the "new_map" with the proper execution environment according
10458 * to the type of executable (platform, 64bit, chroot environment).
10459 * Map the comm page and shared region, etc...
10460 */
10461 kern_return_t
10462 vm_map_exec(
10463 vm_map_t new_map,
10464 task_t task,
10465 void *fsroot,
10466 cpu_type_t cpu)
10467 {
10468 SHARED_REGION_TRACE_DEBUG(
10469 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): ->\n",
10470 (void *)VM_KERNEL_ADDRPERM(current_task()),
10471 (void *)VM_KERNEL_ADDRPERM(new_map),
10472 (void *)VM_KERNEL_ADDRPERM(task),
10473 (void *)VM_KERNEL_ADDRPERM(fsroot),
10474 cpu));
10475 (void) vm_commpage_enter(new_map, task);
10476 (void) vm_shared_region_enter(new_map, task, fsroot, cpu);
10477 SHARED_REGION_TRACE_DEBUG(
10478 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): <-\n",
10479 (void *)VM_KERNEL_ADDRPERM(current_task()),
10480 (void *)VM_KERNEL_ADDRPERM(new_map),
10481 (void *)VM_KERNEL_ADDRPERM(task),
10482 (void *)VM_KERNEL_ADDRPERM(fsroot),
10483 cpu));
10484 return KERN_SUCCESS;
10485 }
10486
10487 /*
10488 * vm_map_lookup_locked:
10489 *
10490 * Finds the VM object, offset, and
10491 * protection for a given virtual address in the
10492 * specified map, assuming a page fault of the
10493 * type specified.
10494 *
10495 * Returns the (object, offset, protection) for
10496 * this address, whether it is wired down, and whether
10497 * this map has the only reference to the data in question.
10498 * In order to later verify this lookup, a "version"
10499 * is returned.
10500 *
10501 * The map MUST be locked by the caller and WILL be
10502 * locked on exit. In order to guarantee the
10503 * existence of the returned object, it is returned
10504 * locked.
10505 *
10506 * If a lookup is requested with "write protection"
10507 * specified, the map may be changed to perform virtual
10508 * copying operations, although the data referenced will
10509 * remain the same.
10510 */
10511 kern_return_t
10512 vm_map_lookup_locked(
10513 vm_map_t *var_map, /* IN/OUT */
10514 vm_map_offset_t vaddr,
10515 vm_prot_t fault_type,
10516 int object_lock_type,
10517 vm_map_version_t *out_version, /* OUT */
10518 vm_object_t *object, /* OUT */
10519 vm_object_offset_t *offset, /* OUT */
10520 vm_prot_t *out_prot, /* OUT */
10521 boolean_t *wired, /* OUT */
10522 vm_object_fault_info_t fault_info, /* OUT */
10523 vm_map_t *real_map)
10524 {
10525 vm_map_entry_t entry;
10526 register vm_map_t map = *var_map;
10527 vm_map_t old_map = *var_map;
10528 vm_map_t cow_sub_map_parent = VM_MAP_NULL;
10529 vm_map_offset_t cow_parent_vaddr = 0;
10530 vm_map_offset_t old_start = 0;
10531 vm_map_offset_t old_end = 0;
10532 register vm_prot_t prot;
10533 boolean_t mask_protections;
10534 boolean_t force_copy;
10535 vm_prot_t original_fault_type;
10536
10537 /*
10538 * VM_PROT_MASK means that the caller wants us to use "fault_type"
10539 * as a mask against the mapping's actual protections, not as an
10540 * absolute value.
10541 */
10542 mask_protections = (fault_type & VM_PROT_IS_MASK) ? TRUE : FALSE;
10543 force_copy = (fault_type & VM_PROT_COPY) ? TRUE : FALSE;
10544 fault_type &= VM_PROT_ALL;
10545 original_fault_type = fault_type;
10546
10547 *real_map = map;
10548
10549 RetryLookup:
10550 fault_type = original_fault_type;
10551
10552 /*
10553 * If the map has an interesting hint, try it before calling
10554 * full blown lookup routine.
10555 */
10556 entry = map->hint;
10557
10558 if ((entry == vm_map_to_entry(map)) ||
10559 (vaddr < entry->vme_start) || (vaddr >= entry->vme_end)) {
10560 vm_map_entry_t tmp_entry;
10561
10562 /*
10563 * Entry was either not a valid hint, or the vaddr
10564 * was not contained in the entry, so do a full lookup.
10565 */
10566 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
10567 if((cow_sub_map_parent) && (cow_sub_map_parent != map))
10568 vm_map_unlock(cow_sub_map_parent);
10569 if((*real_map != map)
10570 && (*real_map != cow_sub_map_parent))
10571 vm_map_unlock(*real_map);
10572 return KERN_INVALID_ADDRESS;
10573 }
10574
10575 entry = tmp_entry;
10576 }
10577 if(map == old_map) {
10578 old_start = entry->vme_start;
10579 old_end = entry->vme_end;
10580 }
10581
10582 /*
10583 * Handle submaps. Drop lock on upper map, submap is
10584 * returned locked.
10585 */
10586
10587 submap_recurse:
10588 if (entry->is_sub_map) {
10589 vm_map_offset_t local_vaddr;
10590 vm_map_offset_t end_delta;
10591 vm_map_offset_t start_delta;
10592 vm_map_entry_t submap_entry;
10593 boolean_t mapped_needs_copy=FALSE;
10594
10595 local_vaddr = vaddr;
10596
10597 if ((entry->use_pmap && !(fault_type & VM_PROT_WRITE))) {
10598 /* if real_map equals map we unlock below */
10599 if ((*real_map != map) &&
10600 (*real_map != cow_sub_map_parent))
10601 vm_map_unlock(*real_map);
10602 *real_map = VME_SUBMAP(entry);
10603 }
10604
10605 if(entry->needs_copy && (fault_type & VM_PROT_WRITE)) {
10606 if (!mapped_needs_copy) {
10607 if (vm_map_lock_read_to_write(map)) {
10608 vm_map_lock_read(map);
10609 *real_map = map;
10610 goto RetryLookup;
10611 }
10612 vm_map_lock_read(VME_SUBMAP(entry));
10613 *var_map = VME_SUBMAP(entry);
10614 cow_sub_map_parent = map;
10615 /* reset base to map before cow object */
10616 /* this is the map which will accept */
10617 /* the new cow object */
10618 old_start = entry->vme_start;
10619 old_end = entry->vme_end;
10620 cow_parent_vaddr = vaddr;
10621 mapped_needs_copy = TRUE;
10622 } else {
10623 vm_map_lock_read(VME_SUBMAP(entry));
10624 *var_map = VME_SUBMAP(entry);
10625 if((cow_sub_map_parent != map) &&
10626 (*real_map != map))
10627 vm_map_unlock(map);
10628 }
10629 } else {
10630 vm_map_lock_read(VME_SUBMAP(entry));
10631 *var_map = VME_SUBMAP(entry);
10632 /* leave map locked if it is a target */
10633 /* cow sub_map above otherwise, just */
10634 /* follow the maps down to the object */
10635 /* here we unlock knowing we are not */
10636 /* revisiting the map. */
10637 if((*real_map != map) && (map != cow_sub_map_parent))
10638 vm_map_unlock_read(map);
10639 }
10640
10641 map = *var_map;
10642
10643 /* calculate the offset in the submap for vaddr */
10644 local_vaddr = (local_vaddr - entry->vme_start) + VME_OFFSET(entry);
10645
10646 RetrySubMap:
10647 if(!vm_map_lookup_entry(map, local_vaddr, &submap_entry)) {
10648 if((cow_sub_map_parent) && (cow_sub_map_parent != map)){
10649 vm_map_unlock(cow_sub_map_parent);
10650 }
10651 if((*real_map != map)
10652 && (*real_map != cow_sub_map_parent)) {
10653 vm_map_unlock(*real_map);
10654 }
10655 *real_map = map;
10656 return KERN_INVALID_ADDRESS;
10657 }
10658
10659 /* find the attenuated shadow of the underlying object */
10660 /* on our target map */
10661
10662 /* in english the submap object may extend beyond the */
10663 /* region mapped by the entry or, may only fill a portion */
10664 /* of it. For our purposes, we only care if the object */
10665 /* doesn't fill. In this case the area which will */
10666 /* ultimately be clipped in the top map will only need */
10667 /* to be as big as the portion of the underlying entry */
10668 /* which is mapped */
10669 start_delta = submap_entry->vme_start > VME_OFFSET(entry) ?
10670 submap_entry->vme_start - VME_OFFSET(entry) : 0;
10671
10672 end_delta =
10673 (VME_OFFSET(entry) + start_delta + (old_end - old_start)) <=
10674 submap_entry->vme_end ?
10675 0 : (VME_OFFSET(entry) +
10676 (old_end - old_start))
10677 - submap_entry->vme_end;
10678
10679 old_start += start_delta;
10680 old_end -= end_delta;
10681
10682 if(submap_entry->is_sub_map) {
10683 entry = submap_entry;
10684 vaddr = local_vaddr;
10685 goto submap_recurse;
10686 }
10687
10688 if(((fault_type & VM_PROT_WRITE) && cow_sub_map_parent)) {
10689
10690 vm_object_t sub_object, copy_object;
10691 vm_object_offset_t copy_offset;
10692 vm_map_offset_t local_start;
10693 vm_map_offset_t local_end;
10694 boolean_t copied_slowly = FALSE;
10695
10696 if (vm_map_lock_read_to_write(map)) {
10697 vm_map_lock_read(map);
10698 old_start -= start_delta;
10699 old_end += end_delta;
10700 goto RetrySubMap;
10701 }
10702
10703
10704 sub_object = VME_OBJECT(submap_entry);
10705 if (sub_object == VM_OBJECT_NULL) {
10706 sub_object =
10707 vm_object_allocate(
10708 (vm_map_size_t)
10709 (submap_entry->vme_end -
10710 submap_entry->vme_start));
10711 VME_OBJECT_SET(submap_entry, sub_object);
10712 VME_OFFSET_SET(submap_entry, 0);
10713 }
10714 local_start = local_vaddr -
10715 (cow_parent_vaddr - old_start);
10716 local_end = local_vaddr +
10717 (old_end - cow_parent_vaddr);
10718 vm_map_clip_start(map, submap_entry, local_start);
10719 vm_map_clip_end(map, submap_entry, local_end);
10720 if (submap_entry->is_sub_map) {
10721 /* unnesting was done when clipping */
10722 assert(!submap_entry->use_pmap);
10723 }
10724
10725 /* This is the COW case, lets connect */
10726 /* an entry in our space to the underlying */
10727 /* object in the submap, bypassing the */
10728 /* submap. */
10729
10730
10731 if(submap_entry->wired_count != 0 ||
10732 (sub_object->copy_strategy ==
10733 MEMORY_OBJECT_COPY_NONE)) {
10734 vm_object_lock(sub_object);
10735 vm_object_copy_slowly(sub_object,
10736 VME_OFFSET(submap_entry),
10737 (submap_entry->vme_end -
10738 submap_entry->vme_start),
10739 FALSE,
10740 &copy_object);
10741 copied_slowly = TRUE;
10742 } else {
10743
10744 /* set up shadow object */
10745 copy_object = sub_object;
10746 vm_object_reference(copy_object);
10747 sub_object->shadowed = TRUE;
10748 assert(submap_entry->wired_count == 0);
10749 submap_entry->needs_copy = TRUE;
10750
10751 prot = submap_entry->protection & ~VM_PROT_WRITE;
10752
10753 if (override_nx(old_map,
10754 VME_ALIAS(submap_entry))
10755 && prot)
10756 prot |= VM_PROT_EXECUTE;
10757
10758 vm_object_pmap_protect(
10759 sub_object,
10760 VME_OFFSET(submap_entry),
10761 submap_entry->vme_end -
10762 submap_entry->vme_start,
10763 (submap_entry->is_shared
10764 || map->mapped_in_other_pmaps) ?
10765 PMAP_NULL : map->pmap,
10766 submap_entry->vme_start,
10767 prot);
10768 }
10769
10770 /*
10771 * Adjust the fault offset to the submap entry.
10772 */
10773 copy_offset = (local_vaddr -
10774 submap_entry->vme_start +
10775 VME_OFFSET(submap_entry));
10776
10777 /* This works diffently than the */
10778 /* normal submap case. We go back */
10779 /* to the parent of the cow map and*/
10780 /* clip out the target portion of */
10781 /* the sub_map, substituting the */
10782 /* new copy object, */
10783
10784 vm_map_unlock(map);
10785 local_start = old_start;
10786 local_end = old_end;
10787 map = cow_sub_map_parent;
10788 *var_map = cow_sub_map_parent;
10789 vaddr = cow_parent_vaddr;
10790 cow_sub_map_parent = NULL;
10791
10792 if(!vm_map_lookup_entry(map,
10793 vaddr, &entry)) {
10794 vm_object_deallocate(
10795 copy_object);
10796 vm_map_lock_write_to_read(map);
10797 return KERN_INVALID_ADDRESS;
10798 }
10799
10800 /* clip out the portion of space */
10801 /* mapped by the sub map which */
10802 /* corresponds to the underlying */
10803 /* object */
10804
10805 /*
10806 * Clip (and unnest) the smallest nested chunk
10807 * possible around the faulting address...
10808 */
10809 local_start = vaddr & ~(pmap_nesting_size_min - 1);
10810 local_end = local_start + pmap_nesting_size_min;
10811 /*
10812 * ... but don't go beyond the "old_start" to "old_end"
10813 * range, to avoid spanning over another VM region
10814 * with a possibly different VM object and/or offset.
10815 */
10816 if (local_start < old_start) {
10817 local_start = old_start;
10818 }
10819 if (local_end > old_end) {
10820 local_end = old_end;
10821 }
10822 /*
10823 * Adjust copy_offset to the start of the range.
10824 */
10825 copy_offset -= (vaddr - local_start);
10826
10827 vm_map_clip_start(map, entry, local_start);
10828 vm_map_clip_end(map, entry, local_end);
10829 if (entry->is_sub_map) {
10830 /* unnesting was done when clipping */
10831 assert(!entry->use_pmap);
10832 }
10833
10834 /* substitute copy object for */
10835 /* shared map entry */
10836 vm_map_deallocate(VME_SUBMAP(entry));
10837 assert(!entry->iokit_acct);
10838 entry->is_sub_map = FALSE;
10839 entry->use_pmap = TRUE;
10840 VME_OBJECT_SET(entry, copy_object);
10841
10842 /* propagate the submap entry's protections */
10843 entry->protection |= submap_entry->protection;
10844 entry->max_protection |= submap_entry->max_protection;
10845
10846 if(copied_slowly) {
10847 VME_OFFSET_SET(entry, local_start - old_start);
10848 entry->needs_copy = FALSE;
10849 entry->is_shared = FALSE;
10850 } else {
10851 VME_OFFSET_SET(entry, copy_offset);
10852 assert(entry->wired_count == 0);
10853 entry->needs_copy = TRUE;
10854 if(entry->inheritance == VM_INHERIT_SHARE)
10855 entry->inheritance = VM_INHERIT_COPY;
10856 if (map != old_map)
10857 entry->is_shared = TRUE;
10858 }
10859 if(entry->inheritance == VM_INHERIT_SHARE)
10860 entry->inheritance = VM_INHERIT_COPY;
10861
10862 vm_map_lock_write_to_read(map);
10863 } else {
10864 if((cow_sub_map_parent)
10865 && (cow_sub_map_parent != *real_map)
10866 && (cow_sub_map_parent != map)) {
10867 vm_map_unlock(cow_sub_map_parent);
10868 }
10869 entry = submap_entry;
10870 vaddr = local_vaddr;
10871 }
10872 }
10873
10874 /*
10875 * Check whether this task is allowed to have
10876 * this page.
10877 */
10878
10879 prot = entry->protection;
10880
10881 if (override_nx(old_map, VME_ALIAS(entry)) && prot) {
10882 /*
10883 * HACK -- if not a stack, then allow execution
10884 */
10885 prot |= VM_PROT_EXECUTE;
10886 }
10887
10888 if (mask_protections) {
10889 fault_type &= prot;
10890 if (fault_type == VM_PROT_NONE) {
10891 goto protection_failure;
10892 }
10893 }
10894 if ((fault_type & (prot)) != fault_type) {
10895 protection_failure:
10896 if (*real_map != map) {
10897 vm_map_unlock(*real_map);
10898 }
10899 *real_map = map;
10900
10901 if ((fault_type & VM_PROT_EXECUTE) && prot)
10902 log_stack_execution_failure((addr64_t)vaddr, prot);
10903
10904 DTRACE_VM2(prot_fault, int, 1, (uint64_t *), NULL);
10905 return KERN_PROTECTION_FAILURE;
10906 }
10907
10908 /*
10909 * If this page is not pageable, we have to get
10910 * it for all possible accesses.
10911 */
10912
10913 *wired = (entry->wired_count != 0);
10914 if (*wired)
10915 fault_type = prot;
10916
10917 /*
10918 * If the entry was copy-on-write, we either ...
10919 */
10920
10921 if (entry->needs_copy) {
10922 /*
10923 * If we want to write the page, we may as well
10924 * handle that now since we've got the map locked.
10925 *
10926 * If we don't need to write the page, we just
10927 * demote the permissions allowed.
10928 */
10929
10930 if ((fault_type & VM_PROT_WRITE) || *wired || force_copy) {
10931 /*
10932 * Make a new object, and place it in the
10933 * object chain. Note that no new references
10934 * have appeared -- one just moved from the
10935 * map to the new object.
10936 */
10937
10938 if (vm_map_lock_read_to_write(map)) {
10939 vm_map_lock_read(map);
10940 goto RetryLookup;
10941 }
10942 VME_OBJECT_SHADOW(entry,
10943 (vm_map_size_t) (entry->vme_end -
10944 entry->vme_start));
10945
10946 VME_OBJECT(entry)->shadowed = TRUE;
10947 entry->needs_copy = FALSE;
10948 vm_map_lock_write_to_read(map);
10949 }
10950 else {
10951 /*
10952 * We're attempting to read a copy-on-write
10953 * page -- don't allow writes.
10954 */
10955
10956 prot &= (~VM_PROT_WRITE);
10957 }
10958 }
10959
10960 /*
10961 * Create an object if necessary.
10962 */
10963 if (VME_OBJECT(entry) == VM_OBJECT_NULL) {
10964
10965 if (vm_map_lock_read_to_write(map)) {
10966 vm_map_lock_read(map);
10967 goto RetryLookup;
10968 }
10969
10970 VME_OBJECT_SET(entry,
10971 vm_object_allocate(
10972 (vm_map_size_t)(entry->vme_end -
10973 entry->vme_start)));
10974 VME_OFFSET_SET(entry, 0);
10975 vm_map_lock_write_to_read(map);
10976 }
10977
10978 /*
10979 * Return the object/offset from this entry. If the entry
10980 * was copy-on-write or empty, it has been fixed up. Also
10981 * return the protection.
10982 */
10983
10984 *offset = (vaddr - entry->vme_start) + VME_OFFSET(entry);
10985 *object = VME_OBJECT(entry);
10986 *out_prot = prot;
10987
10988 if (fault_info) {
10989 fault_info->interruptible = THREAD_UNINT; /* for now... */
10990 /* ... the caller will change "interruptible" if needed */
10991 fault_info->cluster_size = 0;
10992 fault_info->user_tag = VME_ALIAS(entry);
10993 fault_info->pmap_options = 0;
10994 if (entry->iokit_acct ||
10995 (!entry->is_sub_map && !entry->use_pmap)) {
10996 fault_info->pmap_options |= PMAP_OPTIONS_ALT_ACCT;
10997 }
10998 fault_info->behavior = entry->behavior;
10999 fault_info->lo_offset = VME_OFFSET(entry);
11000 fault_info->hi_offset =
11001 (entry->vme_end - entry->vme_start) + VME_OFFSET(entry);
11002 fault_info->no_cache = entry->no_cache;
11003 fault_info->stealth = FALSE;
11004 fault_info->io_sync = FALSE;
11005 if (entry->used_for_jit ||
11006 entry->vme_resilient_codesign) {
11007 fault_info->cs_bypass = TRUE;
11008 } else {
11009 fault_info->cs_bypass = FALSE;
11010 }
11011 fault_info->mark_zf_absent = FALSE;
11012 fault_info->batch_pmap_op = FALSE;
11013 }
11014
11015 /*
11016 * Lock the object to prevent it from disappearing
11017 */
11018 if (object_lock_type == OBJECT_LOCK_EXCLUSIVE)
11019 vm_object_lock(*object);
11020 else
11021 vm_object_lock_shared(*object);
11022
11023 /*
11024 * Save the version number
11025 */
11026
11027 out_version->main_timestamp = map->timestamp;
11028
11029 return KERN_SUCCESS;
11030 }
11031
11032
11033 /*
11034 * vm_map_verify:
11035 *
11036 * Verifies that the map in question has not changed
11037 * since the given version. If successful, the map
11038 * will not change until vm_map_verify_done() is called.
11039 */
11040 boolean_t
11041 vm_map_verify(
11042 register vm_map_t map,
11043 register vm_map_version_t *version) /* REF */
11044 {
11045 boolean_t result;
11046
11047 vm_map_lock_read(map);
11048 result = (map->timestamp == version->main_timestamp);
11049
11050 if (!result)
11051 vm_map_unlock_read(map);
11052
11053 return(result);
11054 }
11055
11056 /*
11057 * vm_map_verify_done:
11058 *
11059 * Releases locks acquired by a vm_map_verify.
11060 *
11061 * This is now a macro in vm/vm_map.h. It does a
11062 * vm_map_unlock_read on the map.
11063 */
11064
11065
11066 /*
11067 * TEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARY
11068 * Goes away after regular vm_region_recurse function migrates to
11069 * 64 bits
11070 * vm_region_recurse: A form of vm_region which follows the
11071 * submaps in a target map
11072 *
11073 */
11074
11075 kern_return_t
11076 vm_map_region_recurse_64(
11077 vm_map_t map,
11078 vm_map_offset_t *address, /* IN/OUT */
11079 vm_map_size_t *size, /* OUT */
11080 natural_t *nesting_depth, /* IN/OUT */
11081 vm_region_submap_info_64_t submap_info, /* IN/OUT */
11082 mach_msg_type_number_t *count) /* IN/OUT */
11083 {
11084 mach_msg_type_number_t original_count;
11085 vm_region_extended_info_data_t extended;
11086 vm_map_entry_t tmp_entry;
11087 vm_map_offset_t user_address;
11088 unsigned int user_max_depth;
11089
11090 /*
11091 * "curr_entry" is the VM map entry preceding or including the
11092 * address we're looking for.
11093 * "curr_map" is the map or sub-map containing "curr_entry".
11094 * "curr_address" is the equivalent of the top map's "user_address"
11095 * in the current map.
11096 * "curr_offset" is the cumulated offset of "curr_map" in the
11097 * target task's address space.
11098 * "curr_depth" is the depth of "curr_map" in the chain of
11099 * sub-maps.
11100 *
11101 * "curr_max_below" and "curr_max_above" limit the range (around
11102 * "curr_address") we should take into account in the current (sub)map.
11103 * They limit the range to what's visible through the map entries
11104 * we've traversed from the top map to the current map.
11105
11106 */
11107 vm_map_entry_t curr_entry;
11108 vm_map_address_t curr_address;
11109 vm_map_offset_t curr_offset;
11110 vm_map_t curr_map;
11111 unsigned int curr_depth;
11112 vm_map_offset_t curr_max_below, curr_max_above;
11113 vm_map_offset_t curr_skip;
11114
11115 /*
11116 * "next_" is the same as "curr_" but for the VM region immediately
11117 * after the address we're looking for. We need to keep track of this
11118 * too because we want to return info about that region if the
11119 * address we're looking for is not mapped.
11120 */
11121 vm_map_entry_t next_entry;
11122 vm_map_offset_t next_offset;
11123 vm_map_offset_t next_address;
11124 vm_map_t next_map;
11125 unsigned int next_depth;
11126 vm_map_offset_t next_max_below, next_max_above;
11127 vm_map_offset_t next_skip;
11128
11129 boolean_t look_for_pages;
11130 vm_region_submap_short_info_64_t short_info;
11131
11132 if (map == VM_MAP_NULL) {
11133 /* no address space to work on */
11134 return KERN_INVALID_ARGUMENT;
11135 }
11136
11137
11138 if (*count < VM_REGION_SUBMAP_SHORT_INFO_COUNT_64) {
11139 /*
11140 * "info" structure is not big enough and
11141 * would overflow
11142 */
11143 return KERN_INVALID_ARGUMENT;
11144 }
11145
11146 original_count = *count;
11147
11148 if (original_count < VM_REGION_SUBMAP_INFO_V0_COUNT_64) {
11149 *count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
11150 look_for_pages = FALSE;
11151 short_info = (vm_region_submap_short_info_64_t) submap_info;
11152 submap_info = NULL;
11153 } else {
11154 look_for_pages = TRUE;
11155 *count = VM_REGION_SUBMAP_INFO_V0_COUNT_64;
11156 short_info = NULL;
11157
11158 if (original_count >= VM_REGION_SUBMAP_INFO_V1_COUNT_64) {
11159 *count = VM_REGION_SUBMAP_INFO_V1_COUNT_64;
11160 }
11161 }
11162
11163 user_address = *address;
11164 user_max_depth = *nesting_depth;
11165
11166 if (not_in_kdp) {
11167 vm_map_lock_read(map);
11168 }
11169
11170 recurse_again:
11171 curr_entry = NULL;
11172 curr_map = map;
11173 curr_address = user_address;
11174 curr_offset = 0;
11175 curr_skip = 0;
11176 curr_depth = 0;
11177 curr_max_above = ((vm_map_offset_t) -1) - curr_address;
11178 curr_max_below = curr_address;
11179
11180 next_entry = NULL;
11181 next_map = NULL;
11182 next_address = 0;
11183 next_offset = 0;
11184 next_skip = 0;
11185 next_depth = 0;
11186 next_max_above = (vm_map_offset_t) -1;
11187 next_max_below = (vm_map_offset_t) -1;
11188
11189 for (;;) {
11190 if (vm_map_lookup_entry(curr_map,
11191 curr_address,
11192 &tmp_entry)) {
11193 /* tmp_entry contains the address we're looking for */
11194 curr_entry = tmp_entry;
11195 } else {
11196 vm_map_offset_t skip;
11197 /*
11198 * The address is not mapped. "tmp_entry" is the
11199 * map entry preceding the address. We want the next
11200 * one, if it exists.
11201 */
11202 curr_entry = tmp_entry->vme_next;
11203
11204 if (curr_entry == vm_map_to_entry(curr_map) ||
11205 (curr_entry->vme_start >=
11206 curr_address + curr_max_above)) {
11207 /* no next entry at this level: stop looking */
11208 if (not_in_kdp) {
11209 vm_map_unlock_read(curr_map);
11210 }
11211 curr_entry = NULL;
11212 curr_map = NULL;
11213 curr_skip = 0;
11214 curr_offset = 0;
11215 curr_depth = 0;
11216 curr_max_above = 0;
11217 curr_max_below = 0;
11218 break;
11219 }
11220
11221 /* adjust current address and offset */
11222 skip = curr_entry->vme_start - curr_address;
11223 curr_address = curr_entry->vme_start;
11224 curr_skip += skip;
11225 curr_offset += skip;
11226 curr_max_above -= skip;
11227 curr_max_below = 0;
11228 }
11229
11230 /*
11231 * Is the next entry at this level closer to the address (or
11232 * deeper in the submap chain) than the one we had
11233 * so far ?
11234 */
11235 tmp_entry = curr_entry->vme_next;
11236 if (tmp_entry == vm_map_to_entry(curr_map)) {
11237 /* no next entry at this level */
11238 } else if (tmp_entry->vme_start >=
11239 curr_address + curr_max_above) {
11240 /*
11241 * tmp_entry is beyond the scope of what we mapped of
11242 * this submap in the upper level: ignore it.
11243 */
11244 } else if ((next_entry == NULL) ||
11245 (tmp_entry->vme_start + curr_offset <=
11246 next_entry->vme_start + next_offset)) {
11247 /*
11248 * We didn't have a "next_entry" or this one is
11249 * closer to the address we're looking for:
11250 * use this "tmp_entry" as the new "next_entry".
11251 */
11252 if (next_entry != NULL) {
11253 /* unlock the last "next_map" */
11254 if (next_map != curr_map && not_in_kdp) {
11255 vm_map_unlock_read(next_map);
11256 }
11257 }
11258 next_entry = tmp_entry;
11259 next_map = curr_map;
11260 next_depth = curr_depth;
11261 next_address = next_entry->vme_start;
11262 next_skip = curr_skip;
11263 next_skip += (next_address - curr_address);
11264 next_offset = curr_offset;
11265 next_offset += (next_address - curr_address);
11266 next_max_above = MIN(next_max_above, curr_max_above);
11267 next_max_above = MIN(next_max_above,
11268 next_entry->vme_end - next_address);
11269 next_max_below = MIN(next_max_below, curr_max_below);
11270 next_max_below = MIN(next_max_below,
11271 next_address - next_entry->vme_start);
11272 }
11273
11274 /*
11275 * "curr_max_{above,below}" allow us to keep track of the
11276 * portion of the submap that is actually mapped at this level:
11277 * the rest of that submap is irrelevant to us, since it's not
11278 * mapped here.
11279 * The relevant portion of the map starts at
11280 * "VME_OFFSET(curr_entry)" up to the size of "curr_entry".
11281 */
11282 curr_max_above = MIN(curr_max_above,
11283 curr_entry->vme_end - curr_address);
11284 curr_max_below = MIN(curr_max_below,
11285 curr_address - curr_entry->vme_start);
11286
11287 if (!curr_entry->is_sub_map ||
11288 curr_depth >= user_max_depth) {
11289 /*
11290 * We hit a leaf map or we reached the maximum depth
11291 * we could, so stop looking. Keep the current map
11292 * locked.
11293 */
11294 break;
11295 }
11296
11297 /*
11298 * Get down to the next submap level.
11299 */
11300
11301 /*
11302 * Lock the next level and unlock the current level,
11303 * unless we need to keep it locked to access the "next_entry"
11304 * later.
11305 */
11306 if (not_in_kdp) {
11307 vm_map_lock_read(VME_SUBMAP(curr_entry));
11308 }
11309 if (curr_map == next_map) {
11310 /* keep "next_map" locked in case we need it */
11311 } else {
11312 /* release this map */
11313 if (not_in_kdp)
11314 vm_map_unlock_read(curr_map);
11315 }
11316
11317 /*
11318 * Adjust the offset. "curr_entry" maps the submap
11319 * at relative address "curr_entry->vme_start" in the
11320 * curr_map but skips the first "VME_OFFSET(curr_entry)"
11321 * bytes of the submap.
11322 * "curr_offset" always represents the offset of a virtual
11323 * address in the curr_map relative to the absolute address
11324 * space (i.e. the top-level VM map).
11325 */
11326 curr_offset +=
11327 (VME_OFFSET(curr_entry) - curr_entry->vme_start);
11328 curr_address = user_address + curr_offset;
11329 /* switch to the submap */
11330 curr_map = VME_SUBMAP(curr_entry);
11331 curr_depth++;
11332 curr_entry = NULL;
11333 }
11334
11335 if (curr_entry == NULL) {
11336 /* no VM region contains the address... */
11337 if (next_entry == NULL) {
11338 /* ... and no VM region follows it either */
11339 return KERN_INVALID_ADDRESS;
11340 }
11341 /* ... gather info about the next VM region */
11342 curr_entry = next_entry;
11343 curr_map = next_map; /* still locked ... */
11344 curr_address = next_address;
11345 curr_skip = next_skip;
11346 curr_offset = next_offset;
11347 curr_depth = next_depth;
11348 curr_max_above = next_max_above;
11349 curr_max_below = next_max_below;
11350 } else {
11351 /* we won't need "next_entry" after all */
11352 if (next_entry != NULL) {
11353 /* release "next_map" */
11354 if (next_map != curr_map && not_in_kdp) {
11355 vm_map_unlock_read(next_map);
11356 }
11357 }
11358 }
11359 next_entry = NULL;
11360 next_map = NULL;
11361 next_offset = 0;
11362 next_skip = 0;
11363 next_depth = 0;
11364 next_max_below = -1;
11365 next_max_above = -1;
11366
11367 if (curr_entry->is_sub_map &&
11368 curr_depth < user_max_depth) {
11369 /*
11370 * We're not as deep as we could be: we must have
11371 * gone back up after not finding anything mapped
11372 * below the original top-level map entry's.
11373 * Let's move "curr_address" forward and recurse again.
11374 */
11375 user_address = curr_address;
11376 goto recurse_again;
11377 }
11378
11379 *nesting_depth = curr_depth;
11380 *size = curr_max_above + curr_max_below;
11381 *address = user_address + curr_skip - curr_max_below;
11382
11383 // LP64todo: all the current tools are 32bit, obviously never worked for 64b
11384 // so probably should be a real 32b ID vs. ptr.
11385 // Current users just check for equality
11386 #define INFO_MAKE_OBJECT_ID(p) ((uint32_t)(uintptr_t)VM_KERNEL_ADDRPERM(p))
11387
11388 if (look_for_pages) {
11389 submap_info->user_tag = VME_ALIAS(curr_entry);
11390 submap_info->offset = VME_OFFSET(curr_entry);
11391 submap_info->protection = curr_entry->protection;
11392 submap_info->inheritance = curr_entry->inheritance;
11393 submap_info->max_protection = curr_entry->max_protection;
11394 submap_info->behavior = curr_entry->behavior;
11395 submap_info->user_wired_count = curr_entry->user_wired_count;
11396 submap_info->is_submap = curr_entry->is_sub_map;
11397 submap_info->object_id = INFO_MAKE_OBJECT_ID(VME_OBJECT(curr_entry));
11398 } else {
11399 short_info->user_tag = VME_ALIAS(curr_entry);
11400 short_info->offset = VME_OFFSET(curr_entry);
11401 short_info->protection = curr_entry->protection;
11402 short_info->inheritance = curr_entry->inheritance;
11403 short_info->max_protection = curr_entry->max_protection;
11404 short_info->behavior = curr_entry->behavior;
11405 short_info->user_wired_count = curr_entry->user_wired_count;
11406 short_info->is_submap = curr_entry->is_sub_map;
11407 short_info->object_id = INFO_MAKE_OBJECT_ID(VME_OBJECT(curr_entry));
11408 }
11409
11410 extended.pages_resident = 0;
11411 extended.pages_swapped_out = 0;
11412 extended.pages_shared_now_private = 0;
11413 extended.pages_dirtied = 0;
11414 extended.pages_reusable = 0;
11415 extended.external_pager = 0;
11416 extended.shadow_depth = 0;
11417 extended.share_mode = SM_EMPTY;
11418 extended.ref_count = 0;
11419
11420 if (not_in_kdp) {
11421 if (!curr_entry->is_sub_map) {
11422 vm_map_offset_t range_start, range_end;
11423 range_start = MAX((curr_address - curr_max_below),
11424 curr_entry->vme_start);
11425 range_end = MIN((curr_address + curr_max_above),
11426 curr_entry->vme_end);
11427 vm_map_region_walk(curr_map,
11428 range_start,
11429 curr_entry,
11430 (VME_OFFSET(curr_entry) +
11431 (range_start -
11432 curr_entry->vme_start)),
11433 range_end - range_start,
11434 &extended,
11435 look_for_pages, VM_REGION_EXTENDED_INFO_COUNT);
11436 if (extended.external_pager &&
11437 extended.ref_count == 2 &&
11438 extended.share_mode == SM_SHARED) {
11439 extended.share_mode = SM_PRIVATE;
11440 }
11441 } else {
11442 if (curr_entry->use_pmap) {
11443 extended.share_mode = SM_TRUESHARED;
11444 } else {
11445 extended.share_mode = SM_PRIVATE;
11446 }
11447 extended.ref_count = VME_SUBMAP(curr_entry)->ref_count;
11448 }
11449 }
11450
11451 if (look_for_pages) {
11452 submap_info->pages_resident = extended.pages_resident;
11453 submap_info->pages_swapped_out = extended.pages_swapped_out;
11454 submap_info->pages_shared_now_private =
11455 extended.pages_shared_now_private;
11456 submap_info->pages_dirtied = extended.pages_dirtied;
11457 submap_info->external_pager = extended.external_pager;
11458 submap_info->shadow_depth = extended.shadow_depth;
11459 submap_info->share_mode = extended.share_mode;
11460 submap_info->ref_count = extended.ref_count;
11461
11462 if (original_count >= VM_REGION_SUBMAP_INFO_V1_COUNT_64) {
11463 submap_info->pages_reusable = extended.pages_reusable;
11464 }
11465 } else {
11466 short_info->external_pager = extended.external_pager;
11467 short_info->shadow_depth = extended.shadow_depth;
11468 short_info->share_mode = extended.share_mode;
11469 short_info->ref_count = extended.ref_count;
11470 }
11471
11472 if (not_in_kdp) {
11473 vm_map_unlock_read(curr_map);
11474 }
11475
11476 return KERN_SUCCESS;
11477 }
11478
11479 /*
11480 * vm_region:
11481 *
11482 * User call to obtain information about a region in
11483 * a task's address map. Currently, only one flavor is
11484 * supported.
11485 *
11486 * XXX The reserved and behavior fields cannot be filled
11487 * in until the vm merge from the IK is completed, and
11488 * vm_reserve is implemented.
11489 */
11490
11491 kern_return_t
11492 vm_map_region(
11493 vm_map_t map,
11494 vm_map_offset_t *address, /* IN/OUT */
11495 vm_map_size_t *size, /* OUT */
11496 vm_region_flavor_t flavor, /* IN */
11497 vm_region_info_t info, /* OUT */
11498 mach_msg_type_number_t *count, /* IN/OUT */
11499 mach_port_t *object_name) /* OUT */
11500 {
11501 vm_map_entry_t tmp_entry;
11502 vm_map_entry_t entry;
11503 vm_map_offset_t start;
11504
11505 if (map == VM_MAP_NULL)
11506 return(KERN_INVALID_ARGUMENT);
11507
11508 switch (flavor) {
11509
11510 case VM_REGION_BASIC_INFO:
11511 /* legacy for old 32-bit objects info */
11512 {
11513 vm_region_basic_info_t basic;
11514
11515 if (*count < VM_REGION_BASIC_INFO_COUNT)
11516 return(KERN_INVALID_ARGUMENT);
11517
11518 basic = (vm_region_basic_info_t) info;
11519 *count = VM_REGION_BASIC_INFO_COUNT;
11520
11521 vm_map_lock_read(map);
11522
11523 start = *address;
11524 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
11525 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
11526 vm_map_unlock_read(map);
11527 return(KERN_INVALID_ADDRESS);
11528 }
11529 } else {
11530 entry = tmp_entry;
11531 }
11532
11533 start = entry->vme_start;
11534
11535 basic->offset = (uint32_t)VME_OFFSET(entry);
11536 basic->protection = entry->protection;
11537 basic->inheritance = entry->inheritance;
11538 basic->max_protection = entry->max_protection;
11539 basic->behavior = entry->behavior;
11540 basic->user_wired_count = entry->user_wired_count;
11541 basic->reserved = entry->is_sub_map;
11542 *address = start;
11543 *size = (entry->vme_end - start);
11544
11545 if (object_name) *object_name = IP_NULL;
11546 if (entry->is_sub_map) {
11547 basic->shared = FALSE;
11548 } else {
11549 basic->shared = entry->is_shared;
11550 }
11551
11552 vm_map_unlock_read(map);
11553 return(KERN_SUCCESS);
11554 }
11555
11556 case VM_REGION_BASIC_INFO_64:
11557 {
11558 vm_region_basic_info_64_t basic;
11559
11560 if (*count < VM_REGION_BASIC_INFO_COUNT_64)
11561 return(KERN_INVALID_ARGUMENT);
11562
11563 basic = (vm_region_basic_info_64_t) info;
11564 *count = VM_REGION_BASIC_INFO_COUNT_64;
11565
11566 vm_map_lock_read(map);
11567
11568 start = *address;
11569 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
11570 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
11571 vm_map_unlock_read(map);
11572 return(KERN_INVALID_ADDRESS);
11573 }
11574 } else {
11575 entry = tmp_entry;
11576 }
11577
11578 start = entry->vme_start;
11579
11580 basic->offset = VME_OFFSET(entry);
11581 basic->protection = entry->protection;
11582 basic->inheritance = entry->inheritance;
11583 basic->max_protection = entry->max_protection;
11584 basic->behavior = entry->behavior;
11585 basic->user_wired_count = entry->user_wired_count;
11586 basic->reserved = entry->is_sub_map;
11587 *address = start;
11588 *size = (entry->vme_end - start);
11589
11590 if (object_name) *object_name = IP_NULL;
11591 if (entry->is_sub_map) {
11592 basic->shared = FALSE;
11593 } else {
11594 basic->shared = entry->is_shared;
11595 }
11596
11597 vm_map_unlock_read(map);
11598 return(KERN_SUCCESS);
11599 }
11600 case VM_REGION_EXTENDED_INFO:
11601 if (*count < VM_REGION_EXTENDED_INFO_COUNT)
11602 return(KERN_INVALID_ARGUMENT);
11603 /*fallthru*/
11604 case VM_REGION_EXTENDED_INFO__legacy:
11605 if (*count < VM_REGION_EXTENDED_INFO_COUNT__legacy)
11606 return KERN_INVALID_ARGUMENT;
11607
11608 {
11609 vm_region_extended_info_t extended;
11610 mach_msg_type_number_t original_count;
11611
11612 extended = (vm_region_extended_info_t) info;
11613
11614 vm_map_lock_read(map);
11615
11616 start = *address;
11617 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
11618 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
11619 vm_map_unlock_read(map);
11620 return(KERN_INVALID_ADDRESS);
11621 }
11622 } else {
11623 entry = tmp_entry;
11624 }
11625 start = entry->vme_start;
11626
11627 extended->protection = entry->protection;
11628 extended->user_tag = VME_ALIAS(entry);
11629 extended->pages_resident = 0;
11630 extended->pages_swapped_out = 0;
11631 extended->pages_shared_now_private = 0;
11632 extended->pages_dirtied = 0;
11633 extended->external_pager = 0;
11634 extended->shadow_depth = 0;
11635
11636 original_count = *count;
11637 if (flavor == VM_REGION_EXTENDED_INFO__legacy) {
11638 *count = VM_REGION_EXTENDED_INFO_COUNT__legacy;
11639 } else {
11640 extended->pages_reusable = 0;
11641 *count = VM_REGION_EXTENDED_INFO_COUNT;
11642 }
11643
11644 vm_map_region_walk(map, start, entry, VME_OFFSET(entry), entry->vme_end - start, extended, TRUE, *count);
11645
11646 if (extended->external_pager && extended->ref_count == 2 && extended->share_mode == SM_SHARED)
11647 extended->share_mode = SM_PRIVATE;
11648
11649 if (object_name)
11650 *object_name = IP_NULL;
11651 *address = start;
11652 *size = (entry->vme_end - start);
11653
11654 vm_map_unlock_read(map);
11655 return(KERN_SUCCESS);
11656 }
11657 case VM_REGION_TOP_INFO:
11658 {
11659 vm_region_top_info_t top;
11660
11661 if (*count < VM_REGION_TOP_INFO_COUNT)
11662 return(KERN_INVALID_ARGUMENT);
11663
11664 top = (vm_region_top_info_t) info;
11665 *count = VM_REGION_TOP_INFO_COUNT;
11666
11667 vm_map_lock_read(map);
11668
11669 start = *address;
11670 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
11671 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
11672 vm_map_unlock_read(map);
11673 return(KERN_INVALID_ADDRESS);
11674 }
11675 } else {
11676 entry = tmp_entry;
11677
11678 }
11679 start = entry->vme_start;
11680
11681 top->private_pages_resident = 0;
11682 top->shared_pages_resident = 0;
11683
11684 vm_map_region_top_walk(entry, top);
11685
11686 if (object_name)
11687 *object_name = IP_NULL;
11688 *address = start;
11689 *size = (entry->vme_end - start);
11690
11691 vm_map_unlock_read(map);
11692 return(KERN_SUCCESS);
11693 }
11694 default:
11695 return(KERN_INVALID_ARGUMENT);
11696 }
11697 }
11698
11699 #define OBJ_RESIDENT_COUNT(obj, entry_size) \
11700 MIN((entry_size), \
11701 ((obj)->all_reusable ? \
11702 (obj)->wired_page_count : \
11703 (obj)->resident_page_count - (obj)->reusable_page_count))
11704
11705 void
11706 vm_map_region_top_walk(
11707 vm_map_entry_t entry,
11708 vm_region_top_info_t top)
11709 {
11710
11711 if (VME_OBJECT(entry) == 0 || entry->is_sub_map) {
11712 top->share_mode = SM_EMPTY;
11713 top->ref_count = 0;
11714 top->obj_id = 0;
11715 return;
11716 }
11717
11718 {
11719 struct vm_object *obj, *tmp_obj;
11720 int ref_count;
11721 uint32_t entry_size;
11722
11723 entry_size = (uint32_t) ((entry->vme_end - entry->vme_start) / PAGE_SIZE_64);
11724
11725 obj = VME_OBJECT(entry);
11726
11727 vm_object_lock(obj);
11728
11729 if ((ref_count = obj->ref_count) > 1 && obj->paging_in_progress)
11730 ref_count--;
11731
11732 assert(obj->reusable_page_count <= obj->resident_page_count);
11733 if (obj->shadow) {
11734 if (ref_count == 1)
11735 top->private_pages_resident =
11736 OBJ_RESIDENT_COUNT(obj, entry_size);
11737 else
11738 top->shared_pages_resident =
11739 OBJ_RESIDENT_COUNT(obj, entry_size);
11740 top->ref_count = ref_count;
11741 top->share_mode = SM_COW;
11742
11743 while ((tmp_obj = obj->shadow)) {
11744 vm_object_lock(tmp_obj);
11745 vm_object_unlock(obj);
11746 obj = tmp_obj;
11747
11748 if ((ref_count = obj->ref_count) > 1 && obj->paging_in_progress)
11749 ref_count--;
11750
11751 assert(obj->reusable_page_count <= obj->resident_page_count);
11752 top->shared_pages_resident +=
11753 OBJ_RESIDENT_COUNT(obj, entry_size);
11754 top->ref_count += ref_count - 1;
11755 }
11756 } else {
11757 if (entry->superpage_size) {
11758 top->share_mode = SM_LARGE_PAGE;
11759 top->shared_pages_resident = 0;
11760 top->private_pages_resident = entry_size;
11761 } else if (entry->needs_copy) {
11762 top->share_mode = SM_COW;
11763 top->shared_pages_resident =
11764 OBJ_RESIDENT_COUNT(obj, entry_size);
11765 } else {
11766 if (ref_count == 1 ||
11767 (ref_count == 2 && !(obj->pager_trusted) && !(obj->internal))) {
11768 top->share_mode = SM_PRIVATE;
11769 top->private_pages_resident =
11770 OBJ_RESIDENT_COUNT(obj,
11771 entry_size);
11772 } else {
11773 top->share_mode = SM_SHARED;
11774 top->shared_pages_resident =
11775 OBJ_RESIDENT_COUNT(obj,
11776 entry_size);
11777 }
11778 }
11779 top->ref_count = ref_count;
11780 }
11781 /* XXX K64: obj_id will be truncated */
11782 top->obj_id = (unsigned int) (uintptr_t)VM_KERNEL_ADDRPERM(obj);
11783
11784 vm_object_unlock(obj);
11785 }
11786 }
11787
11788 void
11789 vm_map_region_walk(
11790 vm_map_t map,
11791 vm_map_offset_t va,
11792 vm_map_entry_t entry,
11793 vm_object_offset_t offset,
11794 vm_object_size_t range,
11795 vm_region_extended_info_t extended,
11796 boolean_t look_for_pages,
11797 mach_msg_type_number_t count)
11798 {
11799 register struct vm_object *obj, *tmp_obj;
11800 register vm_map_offset_t last_offset;
11801 register int i;
11802 register int ref_count;
11803 struct vm_object *shadow_object;
11804 int shadow_depth;
11805
11806 if ((VME_OBJECT(entry) == 0) ||
11807 (entry->is_sub_map) ||
11808 (VME_OBJECT(entry)->phys_contiguous &&
11809 !entry->superpage_size)) {
11810 extended->share_mode = SM_EMPTY;
11811 extended->ref_count = 0;
11812 return;
11813 }
11814
11815 if (entry->superpage_size) {
11816 extended->shadow_depth = 0;
11817 extended->share_mode = SM_LARGE_PAGE;
11818 extended->ref_count = 1;
11819 extended->external_pager = 0;
11820 extended->pages_resident = (unsigned int)(range >> PAGE_SHIFT);
11821 extended->shadow_depth = 0;
11822 return;
11823 }
11824
11825 {
11826 obj = VME_OBJECT(entry);
11827
11828 vm_object_lock(obj);
11829
11830 if ((ref_count = obj->ref_count) > 1 && obj->paging_in_progress)
11831 ref_count--;
11832
11833 if (look_for_pages) {
11834 for (last_offset = offset + range;
11835 offset < last_offset;
11836 offset += PAGE_SIZE_64, va += PAGE_SIZE) {
11837 vm_map_region_look_for_page(map, va, obj,
11838 offset, ref_count,
11839 0, extended, count);
11840 }
11841 } else {
11842 shadow_object = obj->shadow;
11843 shadow_depth = 0;
11844
11845 if ( !(obj->pager_trusted) && !(obj->internal))
11846 extended->external_pager = 1;
11847
11848 if (shadow_object != VM_OBJECT_NULL) {
11849 vm_object_lock(shadow_object);
11850 for (;
11851 shadow_object != VM_OBJECT_NULL;
11852 shadow_depth++) {
11853 vm_object_t next_shadow;
11854
11855 if ( !(shadow_object->pager_trusted) &&
11856 !(shadow_object->internal))
11857 extended->external_pager = 1;
11858
11859 next_shadow = shadow_object->shadow;
11860 if (next_shadow) {
11861 vm_object_lock(next_shadow);
11862 }
11863 vm_object_unlock(shadow_object);
11864 shadow_object = next_shadow;
11865 }
11866 }
11867 extended->shadow_depth = shadow_depth;
11868 }
11869
11870 if (extended->shadow_depth || entry->needs_copy)
11871 extended->share_mode = SM_COW;
11872 else {
11873 if (ref_count == 1)
11874 extended->share_mode = SM_PRIVATE;
11875 else {
11876 if (obj->true_share)
11877 extended->share_mode = SM_TRUESHARED;
11878 else
11879 extended->share_mode = SM_SHARED;
11880 }
11881 }
11882 extended->ref_count = ref_count - extended->shadow_depth;
11883
11884 for (i = 0; i < extended->shadow_depth; i++) {
11885 if ((tmp_obj = obj->shadow) == 0)
11886 break;
11887 vm_object_lock(tmp_obj);
11888 vm_object_unlock(obj);
11889
11890 if ((ref_count = tmp_obj->ref_count) > 1 && tmp_obj->paging_in_progress)
11891 ref_count--;
11892
11893 extended->ref_count += ref_count;
11894 obj = tmp_obj;
11895 }
11896 vm_object_unlock(obj);
11897
11898 if (extended->share_mode == SM_SHARED) {
11899 register vm_map_entry_t cur;
11900 register vm_map_entry_t last;
11901 int my_refs;
11902
11903 obj = VME_OBJECT(entry);
11904 last = vm_map_to_entry(map);
11905 my_refs = 0;
11906
11907 if ((ref_count = obj->ref_count) > 1 && obj->paging_in_progress)
11908 ref_count--;
11909 for (cur = vm_map_first_entry(map); cur != last; cur = cur->vme_next)
11910 my_refs += vm_map_region_count_obj_refs(cur, obj);
11911
11912 if (my_refs == ref_count)
11913 extended->share_mode = SM_PRIVATE_ALIASED;
11914 else if (my_refs > 1)
11915 extended->share_mode = SM_SHARED_ALIASED;
11916 }
11917 }
11918 }
11919
11920
11921 /* object is locked on entry and locked on return */
11922
11923
11924 static void
11925 vm_map_region_look_for_page(
11926 __unused vm_map_t map,
11927 __unused vm_map_offset_t va,
11928 vm_object_t object,
11929 vm_object_offset_t offset,
11930 int max_refcnt,
11931 int depth,
11932 vm_region_extended_info_t extended,
11933 mach_msg_type_number_t count)
11934 {
11935 register vm_page_t p;
11936 register vm_object_t shadow;
11937 register int ref_count;
11938 vm_object_t caller_object;
11939 kern_return_t kr;
11940 shadow = object->shadow;
11941 caller_object = object;
11942
11943
11944 while (TRUE) {
11945
11946 if ( !(object->pager_trusted) && !(object->internal))
11947 extended->external_pager = 1;
11948
11949 if ((p = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
11950 if (shadow && (max_refcnt == 1))
11951 extended->pages_shared_now_private++;
11952
11953 if (!p->fictitious &&
11954 (p->dirty || pmap_is_modified(p->phys_page)))
11955 extended->pages_dirtied++;
11956 else if (count >= VM_REGION_EXTENDED_INFO_COUNT) {
11957 if (p->reusable || p->object->all_reusable) {
11958 extended->pages_reusable++;
11959 }
11960 }
11961
11962 extended->pages_resident++;
11963
11964 if(object != caller_object)
11965 vm_object_unlock(object);
11966
11967 return;
11968 }
11969 #if MACH_PAGEMAP
11970 if (object->existence_map) {
11971 if (vm_external_state_get(object->existence_map, offset) == VM_EXTERNAL_STATE_EXISTS) {
11972
11973 extended->pages_swapped_out++;
11974
11975 if(object != caller_object)
11976 vm_object_unlock(object);
11977
11978 return;
11979 }
11980 } else
11981 #endif /* MACH_PAGEMAP */
11982 if (object->internal &&
11983 object->alive &&
11984 !object->terminating &&
11985 object->pager_ready) {
11986
11987 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
11988 if (VM_COMPRESSOR_PAGER_STATE_GET(object,
11989 offset)
11990 == VM_EXTERNAL_STATE_EXISTS) {
11991 /* the pager has that page */
11992 extended->pages_swapped_out++;
11993 if (object != caller_object)
11994 vm_object_unlock(object);
11995 return;
11996 }
11997 } else {
11998 memory_object_t pager;
11999
12000 vm_object_paging_begin(object);
12001 pager = object->pager;
12002 vm_object_unlock(object);
12003
12004 kr = memory_object_data_request(
12005 pager,
12006 offset + object->paging_offset,
12007 0, /* just poke the pager */
12008 VM_PROT_READ,
12009 NULL);
12010
12011 vm_object_lock(object);
12012 vm_object_paging_end(object);
12013
12014 if (kr == KERN_SUCCESS) {
12015 /* the pager has that page */
12016 extended->pages_swapped_out++;
12017 if (object != caller_object)
12018 vm_object_unlock(object);
12019 return;
12020 }
12021 }
12022 }
12023
12024 if (shadow) {
12025 vm_object_lock(shadow);
12026
12027 if ((ref_count = shadow->ref_count) > 1 && shadow->paging_in_progress)
12028 ref_count--;
12029
12030 if (++depth > extended->shadow_depth)
12031 extended->shadow_depth = depth;
12032
12033 if (ref_count > max_refcnt)
12034 max_refcnt = ref_count;
12035
12036 if(object != caller_object)
12037 vm_object_unlock(object);
12038
12039 offset = offset + object->vo_shadow_offset;
12040 object = shadow;
12041 shadow = object->shadow;
12042 continue;
12043 }
12044 if(object != caller_object)
12045 vm_object_unlock(object);
12046 break;
12047 }
12048 }
12049
12050 static int
12051 vm_map_region_count_obj_refs(
12052 vm_map_entry_t entry,
12053 vm_object_t object)
12054 {
12055 register int ref_count;
12056 register vm_object_t chk_obj;
12057 register vm_object_t tmp_obj;
12058
12059 if (VME_OBJECT(entry) == 0)
12060 return(0);
12061
12062 if (entry->is_sub_map)
12063 return(0);
12064 else {
12065 ref_count = 0;
12066
12067 chk_obj = VME_OBJECT(entry);
12068 vm_object_lock(chk_obj);
12069
12070 while (chk_obj) {
12071 if (chk_obj == object)
12072 ref_count++;
12073 tmp_obj = chk_obj->shadow;
12074 if (tmp_obj)
12075 vm_object_lock(tmp_obj);
12076 vm_object_unlock(chk_obj);
12077
12078 chk_obj = tmp_obj;
12079 }
12080 }
12081 return(ref_count);
12082 }
12083
12084
12085 /*
12086 * Routine: vm_map_simplify
12087 *
12088 * Description:
12089 * Attempt to simplify the map representation in
12090 * the vicinity of the given starting address.
12091 * Note:
12092 * This routine is intended primarily to keep the
12093 * kernel maps more compact -- they generally don't
12094 * benefit from the "expand a map entry" technology
12095 * at allocation time because the adjacent entry
12096 * is often wired down.
12097 */
12098 void
12099 vm_map_simplify_entry(
12100 vm_map_t map,
12101 vm_map_entry_t this_entry)
12102 {
12103 vm_map_entry_t prev_entry;
12104
12105 counter(c_vm_map_simplify_entry_called++);
12106
12107 prev_entry = this_entry->vme_prev;
12108
12109 if ((this_entry != vm_map_to_entry(map)) &&
12110 (prev_entry != vm_map_to_entry(map)) &&
12111
12112 (prev_entry->vme_end == this_entry->vme_start) &&
12113
12114 (prev_entry->is_sub_map == this_entry->is_sub_map) &&
12115 (VME_OBJECT(prev_entry) == VME_OBJECT(this_entry)) &&
12116 ((VME_OFFSET(prev_entry) + (prev_entry->vme_end -
12117 prev_entry->vme_start))
12118 == VME_OFFSET(this_entry)) &&
12119
12120 (prev_entry->behavior == this_entry->behavior) &&
12121 (prev_entry->needs_copy == this_entry->needs_copy) &&
12122 (prev_entry->protection == this_entry->protection) &&
12123 (prev_entry->max_protection == this_entry->max_protection) &&
12124 (prev_entry->inheritance == this_entry->inheritance) &&
12125 (prev_entry->use_pmap == this_entry->use_pmap) &&
12126 (VME_ALIAS(prev_entry) == VME_ALIAS(this_entry)) &&
12127 (prev_entry->no_cache == this_entry->no_cache) &&
12128 (prev_entry->permanent == this_entry->permanent) &&
12129 (prev_entry->map_aligned == this_entry->map_aligned) &&
12130 (prev_entry->zero_wired_pages == this_entry->zero_wired_pages) &&
12131 (prev_entry->used_for_jit == this_entry->used_for_jit) &&
12132 /* from_reserved_zone: OK if that field doesn't match */
12133 (prev_entry->iokit_acct == this_entry->iokit_acct) &&
12134 (prev_entry->vme_resilient_codesign ==
12135 this_entry->vme_resilient_codesign) &&
12136 (prev_entry->vme_resilient_media ==
12137 this_entry->vme_resilient_media) &&
12138
12139 (prev_entry->wired_count == this_entry->wired_count) &&
12140 (prev_entry->user_wired_count == this_entry->user_wired_count) &&
12141
12142 (prev_entry->in_transition == FALSE) &&
12143 (this_entry->in_transition == FALSE) &&
12144 (prev_entry->needs_wakeup == FALSE) &&
12145 (this_entry->needs_wakeup == FALSE) &&
12146 (prev_entry->is_shared == FALSE) &&
12147 (this_entry->is_shared == FALSE) &&
12148 (prev_entry->superpage_size == FALSE) &&
12149 (this_entry->superpage_size == FALSE)
12150 ) {
12151 vm_map_store_entry_unlink(map, prev_entry);
12152 assert(prev_entry->vme_start < this_entry->vme_end);
12153 if (prev_entry->map_aligned)
12154 assert(VM_MAP_PAGE_ALIGNED(prev_entry->vme_start,
12155 VM_MAP_PAGE_MASK(map)));
12156 this_entry->vme_start = prev_entry->vme_start;
12157 VME_OFFSET_SET(this_entry, VME_OFFSET(prev_entry));
12158
12159 if (map->holelistenabled) {
12160 vm_map_store_update_first_free(map, this_entry, TRUE);
12161 }
12162
12163 if (prev_entry->is_sub_map) {
12164 vm_map_deallocate(VME_SUBMAP(prev_entry));
12165 } else {
12166 vm_object_deallocate(VME_OBJECT(prev_entry));
12167 }
12168 vm_map_entry_dispose(map, prev_entry);
12169 SAVE_HINT_MAP_WRITE(map, this_entry);
12170 counter(c_vm_map_simplified++);
12171 }
12172 }
12173
12174 void
12175 vm_map_simplify(
12176 vm_map_t map,
12177 vm_map_offset_t start)
12178 {
12179 vm_map_entry_t this_entry;
12180
12181 vm_map_lock(map);
12182 if (vm_map_lookup_entry(map, start, &this_entry)) {
12183 vm_map_simplify_entry(map, this_entry);
12184 vm_map_simplify_entry(map, this_entry->vme_next);
12185 }
12186 counter(c_vm_map_simplify_called++);
12187 vm_map_unlock(map);
12188 }
12189
12190 static void
12191 vm_map_simplify_range(
12192 vm_map_t map,
12193 vm_map_offset_t start,
12194 vm_map_offset_t end)
12195 {
12196 vm_map_entry_t entry;
12197
12198 /*
12199 * The map should be locked (for "write") by the caller.
12200 */
12201
12202 if (start >= end) {
12203 /* invalid address range */
12204 return;
12205 }
12206
12207 start = vm_map_trunc_page(start,
12208 VM_MAP_PAGE_MASK(map));
12209 end = vm_map_round_page(end,
12210 VM_MAP_PAGE_MASK(map));
12211
12212 if (!vm_map_lookup_entry(map, start, &entry)) {
12213 /* "start" is not mapped and "entry" ends before "start" */
12214 if (entry == vm_map_to_entry(map)) {
12215 /* start with first entry in the map */
12216 entry = vm_map_first_entry(map);
12217 } else {
12218 /* start with next entry */
12219 entry = entry->vme_next;
12220 }
12221 }
12222
12223 while (entry != vm_map_to_entry(map) &&
12224 entry->vme_start <= end) {
12225 /* try and coalesce "entry" with its previous entry */
12226 vm_map_simplify_entry(map, entry);
12227 entry = entry->vme_next;
12228 }
12229 }
12230
12231
12232 /*
12233 * Routine: vm_map_machine_attribute
12234 * Purpose:
12235 * Provide machine-specific attributes to mappings,
12236 * such as cachability etc. for machines that provide
12237 * them. NUMA architectures and machines with big/strange
12238 * caches will use this.
12239 * Note:
12240 * Responsibilities for locking and checking are handled here,
12241 * everything else in the pmap module. If any non-volatile
12242 * information must be kept, the pmap module should handle
12243 * it itself. [This assumes that attributes do not
12244 * need to be inherited, which seems ok to me]
12245 */
12246 kern_return_t
12247 vm_map_machine_attribute(
12248 vm_map_t map,
12249 vm_map_offset_t start,
12250 vm_map_offset_t end,
12251 vm_machine_attribute_t attribute,
12252 vm_machine_attribute_val_t* value) /* IN/OUT */
12253 {
12254 kern_return_t ret;
12255 vm_map_size_t sync_size;
12256 vm_map_entry_t entry;
12257
12258 if (start < vm_map_min(map) || end > vm_map_max(map))
12259 return KERN_INVALID_ADDRESS;
12260
12261 /* Figure how much memory we need to flush (in page increments) */
12262 sync_size = end - start;
12263
12264 vm_map_lock(map);
12265
12266 if (attribute != MATTR_CACHE) {
12267 /* If we don't have to find physical addresses, we */
12268 /* don't have to do an explicit traversal here. */
12269 ret = pmap_attribute(map->pmap, start, end-start,
12270 attribute, value);
12271 vm_map_unlock(map);
12272 return ret;
12273 }
12274
12275 ret = KERN_SUCCESS; /* Assume it all worked */
12276
12277 while(sync_size) {
12278 if (vm_map_lookup_entry(map, start, &entry)) {
12279 vm_map_size_t sub_size;
12280 if((entry->vme_end - start) > sync_size) {
12281 sub_size = sync_size;
12282 sync_size = 0;
12283 } else {
12284 sub_size = entry->vme_end - start;
12285 sync_size -= sub_size;
12286 }
12287 if(entry->is_sub_map) {
12288 vm_map_offset_t sub_start;
12289 vm_map_offset_t sub_end;
12290
12291 sub_start = (start - entry->vme_start)
12292 + VME_OFFSET(entry);
12293 sub_end = sub_start + sub_size;
12294 vm_map_machine_attribute(
12295 VME_SUBMAP(entry),
12296 sub_start,
12297 sub_end,
12298 attribute, value);
12299 } else {
12300 if (VME_OBJECT(entry)) {
12301 vm_page_t m;
12302 vm_object_t object;
12303 vm_object_t base_object;
12304 vm_object_t last_object;
12305 vm_object_offset_t offset;
12306 vm_object_offset_t base_offset;
12307 vm_map_size_t range;
12308 range = sub_size;
12309 offset = (start - entry->vme_start)
12310 + VME_OFFSET(entry);
12311 base_offset = offset;
12312 object = VME_OBJECT(entry);
12313 base_object = object;
12314 last_object = NULL;
12315
12316 vm_object_lock(object);
12317
12318 while (range) {
12319 m = vm_page_lookup(
12320 object, offset);
12321
12322 if (m && !m->fictitious) {
12323 ret =
12324 pmap_attribute_cache_sync(
12325 m->phys_page,
12326 PAGE_SIZE,
12327 attribute, value);
12328
12329 } else if (object->shadow) {
12330 offset = offset + object->vo_shadow_offset;
12331 last_object = object;
12332 object = object->shadow;
12333 vm_object_lock(last_object->shadow);
12334 vm_object_unlock(last_object);
12335 continue;
12336 }
12337 range -= PAGE_SIZE;
12338
12339 if (base_object != object) {
12340 vm_object_unlock(object);
12341 vm_object_lock(base_object);
12342 object = base_object;
12343 }
12344 /* Bump to the next page */
12345 base_offset += PAGE_SIZE;
12346 offset = base_offset;
12347 }
12348 vm_object_unlock(object);
12349 }
12350 }
12351 start += sub_size;
12352 } else {
12353 vm_map_unlock(map);
12354 return KERN_FAILURE;
12355 }
12356
12357 }
12358
12359 vm_map_unlock(map);
12360
12361 return ret;
12362 }
12363
12364 /*
12365 * vm_map_behavior_set:
12366 *
12367 * Sets the paging reference behavior of the specified address
12368 * range in the target map. Paging reference behavior affects
12369 * how pagein operations resulting from faults on the map will be
12370 * clustered.
12371 */
12372 kern_return_t
12373 vm_map_behavior_set(
12374 vm_map_t map,
12375 vm_map_offset_t start,
12376 vm_map_offset_t end,
12377 vm_behavior_t new_behavior)
12378 {
12379 register vm_map_entry_t entry;
12380 vm_map_entry_t temp_entry;
12381
12382 XPR(XPR_VM_MAP,
12383 "vm_map_behavior_set, 0x%X start 0x%X end 0x%X behavior %d",
12384 map, start, end, new_behavior, 0);
12385
12386 if (start > end ||
12387 start < vm_map_min(map) ||
12388 end > vm_map_max(map)) {
12389 return KERN_NO_SPACE;
12390 }
12391
12392 switch (new_behavior) {
12393
12394 /*
12395 * This first block of behaviors all set a persistent state on the specified
12396 * memory range. All we have to do here is to record the desired behavior
12397 * in the vm_map_entry_t's.
12398 */
12399
12400 case VM_BEHAVIOR_DEFAULT:
12401 case VM_BEHAVIOR_RANDOM:
12402 case VM_BEHAVIOR_SEQUENTIAL:
12403 case VM_BEHAVIOR_RSEQNTL:
12404 case VM_BEHAVIOR_ZERO_WIRED_PAGES:
12405 vm_map_lock(map);
12406
12407 /*
12408 * The entire address range must be valid for the map.
12409 * Note that vm_map_range_check() does a
12410 * vm_map_lookup_entry() internally and returns the
12411 * entry containing the start of the address range if
12412 * the entire range is valid.
12413 */
12414 if (vm_map_range_check(map, start, end, &temp_entry)) {
12415 entry = temp_entry;
12416 vm_map_clip_start(map, entry, start);
12417 }
12418 else {
12419 vm_map_unlock(map);
12420 return(KERN_INVALID_ADDRESS);
12421 }
12422
12423 while ((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
12424 vm_map_clip_end(map, entry, end);
12425 if (entry->is_sub_map) {
12426 assert(!entry->use_pmap);
12427 }
12428
12429 if( new_behavior == VM_BEHAVIOR_ZERO_WIRED_PAGES ) {
12430 entry->zero_wired_pages = TRUE;
12431 } else {
12432 entry->behavior = new_behavior;
12433 }
12434 entry = entry->vme_next;
12435 }
12436
12437 vm_map_unlock(map);
12438 break;
12439
12440 /*
12441 * The rest of these are different from the above in that they cause
12442 * an immediate action to take place as opposed to setting a behavior that
12443 * affects future actions.
12444 */
12445
12446 case VM_BEHAVIOR_WILLNEED:
12447 return vm_map_willneed(map, start, end);
12448
12449 case VM_BEHAVIOR_DONTNEED:
12450 return vm_map_msync(map, start, end - start, VM_SYNC_DEACTIVATE | VM_SYNC_CONTIGUOUS);
12451
12452 case VM_BEHAVIOR_FREE:
12453 return vm_map_msync(map, start, end - start, VM_SYNC_KILLPAGES | VM_SYNC_CONTIGUOUS);
12454
12455 case VM_BEHAVIOR_REUSABLE:
12456 return vm_map_reusable_pages(map, start, end);
12457
12458 case VM_BEHAVIOR_REUSE:
12459 return vm_map_reuse_pages(map, start, end);
12460
12461 case VM_BEHAVIOR_CAN_REUSE:
12462 return vm_map_can_reuse(map, start, end);
12463
12464 #if MACH_ASSERT
12465 case VM_BEHAVIOR_PAGEOUT:
12466 return vm_map_pageout(map, start, end);
12467 #endif /* MACH_ASSERT */
12468
12469 default:
12470 return(KERN_INVALID_ARGUMENT);
12471 }
12472
12473 return(KERN_SUCCESS);
12474 }
12475
12476
12477 /*
12478 * Internals for madvise(MADV_WILLNEED) system call.
12479 *
12480 * The present implementation is to do a read-ahead if the mapping corresponds
12481 * to a mapped regular file. If it's an anonymous mapping, then we do nothing
12482 * and basically ignore the "advice" (which we are always free to do).
12483 */
12484
12485
12486 static kern_return_t
12487 vm_map_willneed(
12488 vm_map_t map,
12489 vm_map_offset_t start,
12490 vm_map_offset_t end
12491 )
12492 {
12493 vm_map_entry_t entry;
12494 vm_object_t object;
12495 memory_object_t pager;
12496 struct vm_object_fault_info fault_info;
12497 kern_return_t kr;
12498 vm_object_size_t len;
12499 vm_object_offset_t offset;
12500
12501 /*
12502 * Fill in static values in fault_info. Several fields get ignored by the code
12503 * we call, but we'll fill them in anyway since uninitialized fields are bad
12504 * when it comes to future backwards compatibility.
12505 */
12506
12507 fault_info.interruptible = THREAD_UNINT; /* ignored value */
12508 fault_info.behavior = VM_BEHAVIOR_SEQUENTIAL;
12509 fault_info.no_cache = FALSE; /* ignored value */
12510 fault_info.stealth = TRUE;
12511 fault_info.io_sync = FALSE;
12512 fault_info.cs_bypass = FALSE;
12513 fault_info.mark_zf_absent = FALSE;
12514 fault_info.batch_pmap_op = FALSE;
12515
12516 /*
12517 * The MADV_WILLNEED operation doesn't require any changes to the
12518 * vm_map_entry_t's, so the read lock is sufficient.
12519 */
12520
12521 vm_map_lock_read(map);
12522
12523 /*
12524 * The madvise semantics require that the address range be fully
12525 * allocated with no holes. Otherwise, we're required to return
12526 * an error.
12527 */
12528
12529 if (! vm_map_range_check(map, start, end, &entry)) {
12530 vm_map_unlock_read(map);
12531 return KERN_INVALID_ADDRESS;
12532 }
12533
12534 /*
12535 * Examine each vm_map_entry_t in the range.
12536 */
12537 for (; entry != vm_map_to_entry(map) && start < end; ) {
12538
12539 /*
12540 * The first time through, the start address could be anywhere
12541 * within the vm_map_entry we found. So adjust the offset to
12542 * correspond. After that, the offset will always be zero to
12543 * correspond to the beginning of the current vm_map_entry.
12544 */
12545 offset = (start - entry->vme_start) + VME_OFFSET(entry);
12546
12547 /*
12548 * Set the length so we don't go beyond the end of the
12549 * map_entry or beyond the end of the range we were given.
12550 * This range could span also multiple map entries all of which
12551 * map different files, so make sure we only do the right amount
12552 * of I/O for each object. Note that it's possible for there
12553 * to be multiple map entries all referring to the same object
12554 * but with different page permissions, but it's not worth
12555 * trying to optimize that case.
12556 */
12557 len = MIN(entry->vme_end - start, end - start);
12558
12559 if ((vm_size_t) len != len) {
12560 /* 32-bit overflow */
12561 len = (vm_size_t) (0 - PAGE_SIZE);
12562 }
12563 fault_info.cluster_size = (vm_size_t) len;
12564 fault_info.lo_offset = offset;
12565 fault_info.hi_offset = offset + len;
12566 fault_info.user_tag = VME_ALIAS(entry);
12567 fault_info.pmap_options = 0;
12568 if (entry->iokit_acct ||
12569 (!entry->is_sub_map && !entry->use_pmap)) {
12570 fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT;
12571 }
12572
12573 /*
12574 * If there's no read permission to this mapping, then just
12575 * skip it.
12576 */
12577 if ((entry->protection & VM_PROT_READ) == 0) {
12578 entry = entry->vme_next;
12579 start = entry->vme_start;
12580 continue;
12581 }
12582
12583 /*
12584 * Find the file object backing this map entry. If there is
12585 * none, then we simply ignore the "will need" advice for this
12586 * entry and go on to the next one.
12587 */
12588 if ((object = find_vnode_object(entry)) == VM_OBJECT_NULL) {
12589 entry = entry->vme_next;
12590 start = entry->vme_start;
12591 continue;
12592 }
12593
12594 /*
12595 * The data_request() could take a long time, so let's
12596 * release the map lock to avoid blocking other threads.
12597 */
12598 vm_map_unlock_read(map);
12599
12600 vm_object_paging_begin(object);
12601 pager = object->pager;
12602 vm_object_unlock(object);
12603
12604 /*
12605 * Get the data from the object asynchronously.
12606 *
12607 * Note that memory_object_data_request() places limits on the
12608 * amount of I/O it will do. Regardless of the len we
12609 * specified, it won't do more than MAX_UPL_TRANSFER_BYTES and it
12610 * silently truncates the len to that size. This isn't
12611 * necessarily bad since madvise shouldn't really be used to
12612 * page in unlimited amounts of data. Other Unix variants
12613 * limit the willneed case as well. If this turns out to be an
12614 * issue for developers, then we can always adjust the policy
12615 * here and still be backwards compatible since this is all
12616 * just "advice".
12617 */
12618 kr = memory_object_data_request(
12619 pager,
12620 offset + object->paging_offset,
12621 0, /* ignored */
12622 VM_PROT_READ,
12623 (memory_object_fault_info_t)&fault_info);
12624
12625 vm_object_lock(object);
12626 vm_object_paging_end(object);
12627 vm_object_unlock(object);
12628
12629 /*
12630 * If we couldn't do the I/O for some reason, just give up on
12631 * the madvise. We still return success to the user since
12632 * madvise isn't supposed to fail when the advice can't be
12633 * taken.
12634 */
12635 if (kr != KERN_SUCCESS) {
12636 return KERN_SUCCESS;
12637 }
12638
12639 start += len;
12640 if (start >= end) {
12641 /* done */
12642 return KERN_SUCCESS;
12643 }
12644
12645 /* look up next entry */
12646 vm_map_lock_read(map);
12647 if (! vm_map_lookup_entry(map, start, &entry)) {
12648 /*
12649 * There's a new hole in the address range.
12650 */
12651 vm_map_unlock_read(map);
12652 return KERN_INVALID_ADDRESS;
12653 }
12654 }
12655
12656 vm_map_unlock_read(map);
12657 return KERN_SUCCESS;
12658 }
12659
12660 static boolean_t
12661 vm_map_entry_is_reusable(
12662 vm_map_entry_t entry)
12663 {
12664 /* Only user map entries */
12665
12666 vm_object_t object;
12667
12668 switch (VME_ALIAS(entry)) {
12669 case VM_MEMORY_MALLOC:
12670 case VM_MEMORY_MALLOC_SMALL:
12671 case VM_MEMORY_MALLOC_LARGE:
12672 case VM_MEMORY_REALLOC:
12673 case VM_MEMORY_MALLOC_TINY:
12674 case VM_MEMORY_MALLOC_LARGE_REUSABLE:
12675 case VM_MEMORY_MALLOC_LARGE_REUSED:
12676 /*
12677 * This is a malloc() memory region: check if it's still
12678 * in its original state and can be re-used for more
12679 * malloc() allocations.
12680 */
12681 break;
12682 default:
12683 /*
12684 * Not a malloc() memory region: let the caller decide if
12685 * it's re-usable.
12686 */
12687 return TRUE;
12688 }
12689
12690 if (entry->is_shared ||
12691 entry->is_sub_map ||
12692 entry->in_transition ||
12693 entry->protection != VM_PROT_DEFAULT ||
12694 entry->max_protection != VM_PROT_ALL ||
12695 entry->inheritance != VM_INHERIT_DEFAULT ||
12696 entry->no_cache ||
12697 entry->permanent ||
12698 entry->superpage_size != FALSE ||
12699 entry->zero_wired_pages ||
12700 entry->wired_count != 0 ||
12701 entry->user_wired_count != 0) {
12702 return FALSE;
12703 }
12704
12705 object = VME_OBJECT(entry);
12706 if (object == VM_OBJECT_NULL) {
12707 return TRUE;
12708 }
12709 if (
12710 #if 0
12711 /*
12712 * Let's proceed even if the VM object is potentially
12713 * shared.
12714 * We check for this later when processing the actual
12715 * VM pages, so the contents will be safe if shared.
12716 *
12717 * But we can still mark this memory region as "reusable" to
12718 * acknowledge that the caller did let us know that the memory
12719 * could be re-used and should not be penalized for holding
12720 * on to it. This allows its "resident size" to not include
12721 * the reusable range.
12722 */
12723 object->ref_count == 1 &&
12724 #endif
12725 object->wired_page_count == 0 &&
12726 object->copy == VM_OBJECT_NULL &&
12727 object->shadow == VM_OBJECT_NULL &&
12728 object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC &&
12729 object->internal &&
12730 !object->true_share &&
12731 object->wimg_bits == VM_WIMG_USE_DEFAULT &&
12732 !object->code_signed) {
12733 return TRUE;
12734 }
12735 return FALSE;
12736
12737
12738 }
12739
12740 static kern_return_t
12741 vm_map_reuse_pages(
12742 vm_map_t map,
12743 vm_map_offset_t start,
12744 vm_map_offset_t end)
12745 {
12746 vm_map_entry_t entry;
12747 vm_object_t object;
12748 vm_object_offset_t start_offset, end_offset;
12749
12750 /*
12751 * The MADV_REUSE operation doesn't require any changes to the
12752 * vm_map_entry_t's, so the read lock is sufficient.
12753 */
12754
12755 vm_map_lock_read(map);
12756 assert(map->pmap != kernel_pmap); /* protect alias access */
12757
12758 /*
12759 * The madvise semantics require that the address range be fully
12760 * allocated with no holes. Otherwise, we're required to return
12761 * an error.
12762 */
12763
12764 if (!vm_map_range_check(map, start, end, &entry)) {
12765 vm_map_unlock_read(map);
12766 vm_page_stats_reusable.reuse_pages_failure++;
12767 return KERN_INVALID_ADDRESS;
12768 }
12769
12770 /*
12771 * Examine each vm_map_entry_t in the range.
12772 */
12773 for (; entry != vm_map_to_entry(map) && entry->vme_start < end;
12774 entry = entry->vme_next) {
12775 /*
12776 * Sanity check on the VM map entry.
12777 */
12778 if (! vm_map_entry_is_reusable(entry)) {
12779 vm_map_unlock_read(map);
12780 vm_page_stats_reusable.reuse_pages_failure++;
12781 return KERN_INVALID_ADDRESS;
12782 }
12783
12784 /*
12785 * The first time through, the start address could be anywhere
12786 * within the vm_map_entry we found. So adjust the offset to
12787 * correspond.
12788 */
12789 if (entry->vme_start < start) {
12790 start_offset = start - entry->vme_start;
12791 } else {
12792 start_offset = 0;
12793 }
12794 end_offset = MIN(end, entry->vme_end) - entry->vme_start;
12795 start_offset += VME_OFFSET(entry);
12796 end_offset += VME_OFFSET(entry);
12797
12798 object = VME_OBJECT(entry);
12799 if (object != VM_OBJECT_NULL) {
12800 vm_object_lock(object);
12801 vm_object_reuse_pages(object, start_offset, end_offset,
12802 TRUE);
12803 vm_object_unlock(object);
12804 }
12805
12806 if (VME_ALIAS(entry) == VM_MEMORY_MALLOC_LARGE_REUSABLE) {
12807 /*
12808 * XXX
12809 * We do not hold the VM map exclusively here.
12810 * The "alias" field is not that critical, so it's
12811 * safe to update it here, as long as it is the only
12812 * one that can be modified while holding the VM map
12813 * "shared".
12814 */
12815 VME_ALIAS_SET(entry, VM_MEMORY_MALLOC_LARGE_REUSED);
12816 }
12817 }
12818
12819 vm_map_unlock_read(map);
12820 vm_page_stats_reusable.reuse_pages_success++;
12821 return KERN_SUCCESS;
12822 }
12823
12824
12825 static kern_return_t
12826 vm_map_reusable_pages(
12827 vm_map_t map,
12828 vm_map_offset_t start,
12829 vm_map_offset_t end)
12830 {
12831 vm_map_entry_t entry;
12832 vm_object_t object;
12833 vm_object_offset_t start_offset, end_offset;
12834 vm_map_offset_t pmap_offset;
12835
12836 /*
12837 * The MADV_REUSABLE operation doesn't require any changes to the
12838 * vm_map_entry_t's, so the read lock is sufficient.
12839 */
12840
12841 vm_map_lock_read(map);
12842 assert(map->pmap != kernel_pmap); /* protect alias access */
12843
12844 /*
12845 * The madvise semantics require that the address range be fully
12846 * allocated with no holes. Otherwise, we're required to return
12847 * an error.
12848 */
12849
12850 if (!vm_map_range_check(map, start, end, &entry)) {
12851 vm_map_unlock_read(map);
12852 vm_page_stats_reusable.reusable_pages_failure++;
12853 return KERN_INVALID_ADDRESS;
12854 }
12855
12856 /*
12857 * Examine each vm_map_entry_t in the range.
12858 */
12859 for (; entry != vm_map_to_entry(map) && entry->vme_start < end;
12860 entry = entry->vme_next) {
12861 int kill_pages = 0;
12862
12863 /*
12864 * Sanity check on the VM map entry.
12865 */
12866 if (! vm_map_entry_is_reusable(entry)) {
12867 vm_map_unlock_read(map);
12868 vm_page_stats_reusable.reusable_pages_failure++;
12869 return KERN_INVALID_ADDRESS;
12870 }
12871
12872 /*
12873 * The first time through, the start address could be anywhere
12874 * within the vm_map_entry we found. So adjust the offset to
12875 * correspond.
12876 */
12877 if (entry->vme_start < start) {
12878 start_offset = start - entry->vme_start;
12879 pmap_offset = start;
12880 } else {
12881 start_offset = 0;
12882 pmap_offset = entry->vme_start;
12883 }
12884 end_offset = MIN(end, entry->vme_end) - entry->vme_start;
12885 start_offset += VME_OFFSET(entry);
12886 end_offset += VME_OFFSET(entry);
12887
12888 object = VME_OBJECT(entry);
12889 if (object == VM_OBJECT_NULL)
12890 continue;
12891
12892
12893 vm_object_lock(object);
12894 if (object->ref_count == 1 &&
12895 !object->shadow &&
12896 /*
12897 * "iokit_acct" entries are billed for their virtual size
12898 * (rather than for their resident pages only), so they
12899 * wouldn't benefit from making pages reusable, and it
12900 * would be hard to keep track of pages that are both
12901 * "iokit_acct" and "reusable" in the pmap stats and ledgers.
12902 */
12903 !(entry->iokit_acct ||
12904 (!entry->is_sub_map && !entry->use_pmap)))
12905 kill_pages = 1;
12906 else
12907 kill_pages = -1;
12908 if (kill_pages != -1) {
12909 vm_object_deactivate_pages(object,
12910 start_offset,
12911 end_offset - start_offset,
12912 kill_pages,
12913 TRUE /*reusable_pages*/,
12914 map->pmap,
12915 pmap_offset);
12916 } else {
12917 vm_page_stats_reusable.reusable_pages_shared++;
12918 }
12919 vm_object_unlock(object);
12920
12921 if (VME_ALIAS(entry) == VM_MEMORY_MALLOC_LARGE ||
12922 VME_ALIAS(entry) == VM_MEMORY_MALLOC_LARGE_REUSED) {
12923 /*
12924 * XXX
12925 * We do not hold the VM map exclusively here.
12926 * The "alias" field is not that critical, so it's
12927 * safe to update it here, as long as it is the only
12928 * one that can be modified while holding the VM map
12929 * "shared".
12930 */
12931 VME_ALIAS_SET(entry, VM_MEMORY_MALLOC_LARGE_REUSABLE);
12932 }
12933 }
12934
12935 vm_map_unlock_read(map);
12936 vm_page_stats_reusable.reusable_pages_success++;
12937 return KERN_SUCCESS;
12938 }
12939
12940
12941 static kern_return_t
12942 vm_map_can_reuse(
12943 vm_map_t map,
12944 vm_map_offset_t start,
12945 vm_map_offset_t end)
12946 {
12947 vm_map_entry_t entry;
12948
12949 /*
12950 * The MADV_REUSABLE operation doesn't require any changes to the
12951 * vm_map_entry_t's, so the read lock is sufficient.
12952 */
12953
12954 vm_map_lock_read(map);
12955 assert(map->pmap != kernel_pmap); /* protect alias access */
12956
12957 /*
12958 * The madvise semantics require that the address range be fully
12959 * allocated with no holes. Otherwise, we're required to return
12960 * an error.
12961 */
12962
12963 if (!vm_map_range_check(map, start, end, &entry)) {
12964 vm_map_unlock_read(map);
12965 vm_page_stats_reusable.can_reuse_failure++;
12966 return KERN_INVALID_ADDRESS;
12967 }
12968
12969 /*
12970 * Examine each vm_map_entry_t in the range.
12971 */
12972 for (; entry != vm_map_to_entry(map) && entry->vme_start < end;
12973 entry = entry->vme_next) {
12974 /*
12975 * Sanity check on the VM map entry.
12976 */
12977 if (! vm_map_entry_is_reusable(entry)) {
12978 vm_map_unlock_read(map);
12979 vm_page_stats_reusable.can_reuse_failure++;
12980 return KERN_INVALID_ADDRESS;
12981 }
12982 }
12983
12984 vm_map_unlock_read(map);
12985 vm_page_stats_reusable.can_reuse_success++;
12986 return KERN_SUCCESS;
12987 }
12988
12989
12990 #if MACH_ASSERT
12991 static kern_return_t
12992 vm_map_pageout(
12993 vm_map_t map,
12994 vm_map_offset_t start,
12995 vm_map_offset_t end)
12996 {
12997 vm_map_entry_t entry;
12998
12999 /*
13000 * The MADV_PAGEOUT operation doesn't require any changes to the
13001 * vm_map_entry_t's, so the read lock is sufficient.
13002 */
13003
13004 vm_map_lock_read(map);
13005
13006 /*
13007 * The madvise semantics require that the address range be fully
13008 * allocated with no holes. Otherwise, we're required to return
13009 * an error.
13010 */
13011
13012 if (!vm_map_range_check(map, start, end, &entry)) {
13013 vm_map_unlock_read(map);
13014 return KERN_INVALID_ADDRESS;
13015 }
13016
13017 /*
13018 * Examine each vm_map_entry_t in the range.
13019 */
13020 for (; entry != vm_map_to_entry(map) && entry->vme_start < end;
13021 entry = entry->vme_next) {
13022 vm_object_t object;
13023
13024 /*
13025 * Sanity check on the VM map entry.
13026 */
13027 if (entry->is_sub_map) {
13028 vm_map_t submap;
13029 vm_map_offset_t submap_start;
13030 vm_map_offset_t submap_end;
13031 vm_map_entry_t submap_entry;
13032
13033 submap = VME_SUBMAP(entry);
13034 submap_start = VME_OFFSET(entry);
13035 submap_end = submap_start + (entry->vme_end -
13036 entry->vme_start);
13037
13038 vm_map_lock_read(submap);
13039
13040 if (! vm_map_range_check(submap,
13041 submap_start,
13042 submap_end,
13043 &submap_entry)) {
13044 vm_map_unlock_read(submap);
13045 vm_map_unlock_read(map);
13046 return KERN_INVALID_ADDRESS;
13047 }
13048
13049 object = VME_OBJECT(submap_entry);
13050 if (submap_entry->is_sub_map ||
13051 object == VM_OBJECT_NULL ||
13052 !object->internal) {
13053 vm_map_unlock_read(submap);
13054 continue;
13055 }
13056
13057 vm_object_pageout(object);
13058
13059 vm_map_unlock_read(submap);
13060 submap = VM_MAP_NULL;
13061 submap_entry = VM_MAP_ENTRY_NULL;
13062 continue;
13063 }
13064
13065 object = VME_OBJECT(entry);
13066 if (entry->is_sub_map ||
13067 object == VM_OBJECT_NULL ||
13068 !object->internal) {
13069 continue;
13070 }
13071
13072 vm_object_pageout(object);
13073 }
13074
13075 vm_map_unlock_read(map);
13076 return KERN_SUCCESS;
13077 }
13078 #endif /* MACH_ASSERT */
13079
13080
13081 /*
13082 * Routine: vm_map_entry_insert
13083 *
13084 * Descritpion: This routine inserts a new vm_entry in a locked map.
13085 */
13086 vm_map_entry_t
13087 vm_map_entry_insert(
13088 vm_map_t map,
13089 vm_map_entry_t insp_entry,
13090 vm_map_offset_t start,
13091 vm_map_offset_t end,
13092 vm_object_t object,
13093 vm_object_offset_t offset,
13094 boolean_t needs_copy,
13095 boolean_t is_shared,
13096 boolean_t in_transition,
13097 vm_prot_t cur_protection,
13098 vm_prot_t max_protection,
13099 vm_behavior_t behavior,
13100 vm_inherit_t inheritance,
13101 unsigned wired_count,
13102 boolean_t no_cache,
13103 boolean_t permanent,
13104 unsigned int superpage_size,
13105 boolean_t clear_map_aligned,
13106 boolean_t is_submap)
13107 {
13108 vm_map_entry_t new_entry;
13109
13110 assert(insp_entry != (vm_map_entry_t)0);
13111
13112 new_entry = vm_map_entry_create(map, !map->hdr.entries_pageable);
13113
13114 if (VM_MAP_PAGE_SHIFT(map) != PAGE_SHIFT) {
13115 new_entry->map_aligned = TRUE;
13116 } else {
13117 new_entry->map_aligned = FALSE;
13118 }
13119 if (clear_map_aligned &&
13120 (! VM_MAP_PAGE_ALIGNED(start, VM_MAP_PAGE_MASK(map)) ||
13121 ! VM_MAP_PAGE_ALIGNED(end, VM_MAP_PAGE_MASK(map)))) {
13122 new_entry->map_aligned = FALSE;
13123 }
13124
13125 new_entry->vme_start = start;
13126 new_entry->vme_end = end;
13127 assert(page_aligned(new_entry->vme_start));
13128 assert(page_aligned(new_entry->vme_end));
13129 if (new_entry->map_aligned) {
13130 assert(VM_MAP_PAGE_ALIGNED(new_entry->vme_start,
13131 VM_MAP_PAGE_MASK(map)));
13132 assert(VM_MAP_PAGE_ALIGNED(new_entry->vme_end,
13133 VM_MAP_PAGE_MASK(map)));
13134 }
13135 assert(new_entry->vme_start < new_entry->vme_end);
13136
13137 VME_OBJECT_SET(new_entry, object);
13138 VME_OFFSET_SET(new_entry, offset);
13139 new_entry->is_shared = is_shared;
13140 new_entry->is_sub_map = is_submap;
13141 new_entry->needs_copy = needs_copy;
13142 new_entry->in_transition = in_transition;
13143 new_entry->needs_wakeup = FALSE;
13144 new_entry->inheritance = inheritance;
13145 new_entry->protection = cur_protection;
13146 new_entry->max_protection = max_protection;
13147 new_entry->behavior = behavior;
13148 new_entry->wired_count = wired_count;
13149 new_entry->user_wired_count = 0;
13150 if (is_submap) {
13151 /*
13152 * submap: "use_pmap" means "nested".
13153 * default: false.
13154 */
13155 new_entry->use_pmap = FALSE;
13156 } else {
13157 /*
13158 * object: "use_pmap" means "use pmap accounting" for footprint.
13159 * default: true.
13160 */
13161 new_entry->use_pmap = TRUE;
13162 }
13163 VME_ALIAS_SET(new_entry, 0);
13164 new_entry->zero_wired_pages = FALSE;
13165 new_entry->no_cache = no_cache;
13166 new_entry->permanent = permanent;
13167 if (superpage_size)
13168 new_entry->superpage_size = TRUE;
13169 else
13170 new_entry->superpage_size = FALSE;
13171 new_entry->used_for_jit = FALSE;
13172 new_entry->iokit_acct = FALSE;
13173 new_entry->vme_resilient_codesign = FALSE;
13174 new_entry->vme_resilient_media = FALSE;
13175
13176 /*
13177 * Insert the new entry into the list.
13178 */
13179
13180 vm_map_store_entry_link(map, insp_entry, new_entry);
13181 map->size += end - start;
13182
13183 /*
13184 * Update the free space hint and the lookup hint.
13185 */
13186
13187 SAVE_HINT_MAP_WRITE(map, new_entry);
13188 return new_entry;
13189 }
13190
13191 /*
13192 * Routine: vm_map_remap_extract
13193 *
13194 * Descritpion: This routine returns a vm_entry list from a map.
13195 */
13196 static kern_return_t
13197 vm_map_remap_extract(
13198 vm_map_t map,
13199 vm_map_offset_t addr,
13200 vm_map_size_t size,
13201 boolean_t copy,
13202 struct vm_map_header *map_header,
13203 vm_prot_t *cur_protection,
13204 vm_prot_t *max_protection,
13205 /* What, no behavior? */
13206 vm_inherit_t inheritance,
13207 boolean_t pageable)
13208 {
13209 kern_return_t result;
13210 vm_map_size_t mapped_size;
13211 vm_map_size_t tmp_size;
13212 vm_map_entry_t src_entry; /* result of last map lookup */
13213 vm_map_entry_t new_entry;
13214 vm_object_offset_t offset;
13215 vm_map_offset_t map_address;
13216 vm_map_offset_t src_start; /* start of entry to map */
13217 vm_map_offset_t src_end; /* end of region to be mapped */
13218 vm_object_t object;
13219 vm_map_version_t version;
13220 boolean_t src_needs_copy;
13221 boolean_t new_entry_needs_copy;
13222
13223 assert(map != VM_MAP_NULL);
13224 assert(size != 0);
13225 assert(size == vm_map_round_page(size, PAGE_MASK));
13226 assert(inheritance == VM_INHERIT_NONE ||
13227 inheritance == VM_INHERIT_COPY ||
13228 inheritance == VM_INHERIT_SHARE);
13229
13230 /*
13231 * Compute start and end of region.
13232 */
13233 src_start = vm_map_trunc_page(addr, PAGE_MASK);
13234 src_end = vm_map_round_page(src_start + size, PAGE_MASK);
13235
13236
13237 /*
13238 * Initialize map_header.
13239 */
13240 map_header->links.next = (struct vm_map_entry *)&map_header->links;
13241 map_header->links.prev = (struct vm_map_entry *)&map_header->links;
13242 map_header->nentries = 0;
13243 map_header->entries_pageable = pageable;
13244 map_header->page_shift = PAGE_SHIFT;
13245
13246 vm_map_store_init( map_header );
13247
13248 *cur_protection = VM_PROT_ALL;
13249 *max_protection = VM_PROT_ALL;
13250
13251 map_address = 0;
13252 mapped_size = 0;
13253 result = KERN_SUCCESS;
13254
13255 /*
13256 * The specified source virtual space might correspond to
13257 * multiple map entries, need to loop on them.
13258 */
13259 vm_map_lock(map);
13260 while (mapped_size != size) {
13261 vm_map_size_t entry_size;
13262
13263 /*
13264 * Find the beginning of the region.
13265 */
13266 if (! vm_map_lookup_entry(map, src_start, &src_entry)) {
13267 result = KERN_INVALID_ADDRESS;
13268 break;
13269 }
13270
13271 if (src_start < src_entry->vme_start ||
13272 (mapped_size && src_start != src_entry->vme_start)) {
13273 result = KERN_INVALID_ADDRESS;
13274 break;
13275 }
13276
13277 tmp_size = size - mapped_size;
13278 if (src_end > src_entry->vme_end)
13279 tmp_size -= (src_end - src_entry->vme_end);
13280
13281 entry_size = (vm_map_size_t)(src_entry->vme_end -
13282 src_entry->vme_start);
13283
13284 if(src_entry->is_sub_map) {
13285 vm_map_reference(VME_SUBMAP(src_entry));
13286 object = VM_OBJECT_NULL;
13287 } else {
13288 object = VME_OBJECT(src_entry);
13289 if (src_entry->iokit_acct) {
13290 /*
13291 * This entry uses "IOKit accounting".
13292 */
13293 } else if (object != VM_OBJECT_NULL &&
13294 object->purgable != VM_PURGABLE_DENY) {
13295 /*
13296 * Purgeable objects have their own accounting:
13297 * no pmap accounting for them.
13298 */
13299 assert(!src_entry->use_pmap);
13300 } else {
13301 /*
13302 * Not IOKit or purgeable:
13303 * must be accounted by pmap stats.
13304 */
13305 assert(src_entry->use_pmap);
13306 }
13307
13308 if (object == VM_OBJECT_NULL) {
13309 object = vm_object_allocate(entry_size);
13310 VME_OFFSET_SET(src_entry, 0);
13311 VME_OBJECT_SET(src_entry, object);
13312 } else if (object->copy_strategy !=
13313 MEMORY_OBJECT_COPY_SYMMETRIC) {
13314 /*
13315 * We are already using an asymmetric
13316 * copy, and therefore we already have
13317 * the right object.
13318 */
13319 assert(!src_entry->needs_copy);
13320 } else if (src_entry->needs_copy || object->shadowed ||
13321 (object->internal && !object->true_share &&
13322 !src_entry->is_shared &&
13323 object->vo_size > entry_size)) {
13324
13325 VME_OBJECT_SHADOW(src_entry, entry_size);
13326
13327 if (!src_entry->needs_copy &&
13328 (src_entry->protection & VM_PROT_WRITE)) {
13329 vm_prot_t prot;
13330
13331 prot = src_entry->protection & ~VM_PROT_WRITE;
13332
13333 if (override_nx(map,
13334 VME_ALIAS(src_entry))
13335 && prot)
13336 prot |= VM_PROT_EXECUTE;
13337
13338 if(map->mapped_in_other_pmaps) {
13339 vm_object_pmap_protect(
13340 VME_OBJECT(src_entry),
13341 VME_OFFSET(src_entry),
13342 entry_size,
13343 PMAP_NULL,
13344 src_entry->vme_start,
13345 prot);
13346 } else {
13347 pmap_protect(vm_map_pmap(map),
13348 src_entry->vme_start,
13349 src_entry->vme_end,
13350 prot);
13351 }
13352 }
13353
13354 object = VME_OBJECT(src_entry);
13355 src_entry->needs_copy = FALSE;
13356 }
13357
13358
13359 vm_object_lock(object);
13360 vm_object_reference_locked(object); /* object ref. for new entry */
13361 if (object->copy_strategy ==
13362 MEMORY_OBJECT_COPY_SYMMETRIC) {
13363 object->copy_strategy =
13364 MEMORY_OBJECT_COPY_DELAY;
13365 }
13366 vm_object_unlock(object);
13367 }
13368
13369 offset = (VME_OFFSET(src_entry) +
13370 (src_start - src_entry->vme_start));
13371
13372 new_entry = _vm_map_entry_create(map_header, !map_header->entries_pageable);
13373 vm_map_entry_copy(new_entry, src_entry);
13374 if (new_entry->is_sub_map) {
13375 /* clr address space specifics */
13376 new_entry->use_pmap = FALSE;
13377 }
13378
13379 new_entry->map_aligned = FALSE;
13380
13381 new_entry->vme_start = map_address;
13382 new_entry->vme_end = map_address + tmp_size;
13383 assert(new_entry->vme_start < new_entry->vme_end);
13384 new_entry->inheritance = inheritance;
13385 VME_OFFSET_SET(new_entry, offset);
13386
13387 /*
13388 * The new region has to be copied now if required.
13389 */
13390 RestartCopy:
13391 if (!copy) {
13392 /*
13393 * Cannot allow an entry describing a JIT
13394 * region to be shared across address spaces.
13395 */
13396 if (src_entry->used_for_jit == TRUE) {
13397 result = KERN_INVALID_ARGUMENT;
13398 break;
13399 }
13400 src_entry->is_shared = TRUE;
13401 new_entry->is_shared = TRUE;
13402 if (!(new_entry->is_sub_map))
13403 new_entry->needs_copy = FALSE;
13404
13405 } else if (src_entry->is_sub_map) {
13406 /* make this a COW sub_map if not already */
13407 assert(new_entry->wired_count == 0);
13408 new_entry->needs_copy = TRUE;
13409 object = VM_OBJECT_NULL;
13410 } else if (src_entry->wired_count == 0 &&
13411 vm_object_copy_quickly(&VME_OBJECT(new_entry),
13412 VME_OFFSET(new_entry),
13413 (new_entry->vme_end -
13414 new_entry->vme_start),
13415 &src_needs_copy,
13416 &new_entry_needs_copy)) {
13417
13418 new_entry->needs_copy = new_entry_needs_copy;
13419 new_entry->is_shared = FALSE;
13420
13421 /*
13422 * Handle copy_on_write semantics.
13423 */
13424 if (src_needs_copy && !src_entry->needs_copy) {
13425 vm_prot_t prot;
13426
13427 prot = src_entry->protection & ~VM_PROT_WRITE;
13428
13429 if (override_nx(map,
13430 VME_ALIAS(src_entry))
13431 && prot)
13432 prot |= VM_PROT_EXECUTE;
13433
13434 vm_object_pmap_protect(object,
13435 offset,
13436 entry_size,
13437 ((src_entry->is_shared
13438 || map->mapped_in_other_pmaps) ?
13439 PMAP_NULL : map->pmap),
13440 src_entry->vme_start,
13441 prot);
13442
13443 assert(src_entry->wired_count == 0);
13444 src_entry->needs_copy = TRUE;
13445 }
13446 /*
13447 * Throw away the old object reference of the new entry.
13448 */
13449 vm_object_deallocate(object);
13450
13451 } else {
13452 new_entry->is_shared = FALSE;
13453
13454 /*
13455 * The map can be safely unlocked since we
13456 * already hold a reference on the object.
13457 *
13458 * Record the timestamp of the map for later
13459 * verification, and unlock the map.
13460 */
13461 version.main_timestamp = map->timestamp;
13462 vm_map_unlock(map); /* Increments timestamp once! */
13463
13464 /*
13465 * Perform the copy.
13466 */
13467 if (src_entry->wired_count > 0) {
13468 vm_object_lock(object);
13469 result = vm_object_copy_slowly(
13470 object,
13471 offset,
13472 entry_size,
13473 THREAD_UNINT,
13474 &VME_OBJECT(new_entry));
13475
13476 VME_OFFSET_SET(new_entry, 0);
13477 new_entry->needs_copy = FALSE;
13478 } else {
13479 vm_object_offset_t new_offset;
13480
13481 new_offset = VME_OFFSET(new_entry);
13482 result = vm_object_copy_strategically(
13483 object,
13484 offset,
13485 entry_size,
13486 &VME_OBJECT(new_entry),
13487 &new_offset,
13488 &new_entry_needs_copy);
13489 if (new_offset != VME_OFFSET(new_entry)) {
13490 VME_OFFSET_SET(new_entry, new_offset);
13491 }
13492
13493 new_entry->needs_copy = new_entry_needs_copy;
13494 }
13495
13496 /*
13497 * Throw away the old object reference of the new entry.
13498 */
13499 vm_object_deallocate(object);
13500
13501 if (result != KERN_SUCCESS &&
13502 result != KERN_MEMORY_RESTART_COPY) {
13503 _vm_map_entry_dispose(map_header, new_entry);
13504 break;
13505 }
13506
13507 /*
13508 * Verify that the map has not substantially
13509 * changed while the copy was being made.
13510 */
13511
13512 vm_map_lock(map);
13513 if (version.main_timestamp + 1 != map->timestamp) {
13514 /*
13515 * Simple version comparison failed.
13516 *
13517 * Retry the lookup and verify that the
13518 * same object/offset are still present.
13519 */
13520 vm_object_deallocate(VME_OBJECT(new_entry));
13521 _vm_map_entry_dispose(map_header, new_entry);
13522 if (result == KERN_MEMORY_RESTART_COPY)
13523 result = KERN_SUCCESS;
13524 continue;
13525 }
13526
13527 if (result == KERN_MEMORY_RESTART_COPY) {
13528 vm_object_reference(object);
13529 goto RestartCopy;
13530 }
13531 }
13532
13533 _vm_map_store_entry_link(map_header,
13534 map_header->links.prev, new_entry);
13535
13536 /*Protections for submap mapping are irrelevant here*/
13537 if( !src_entry->is_sub_map ) {
13538 *cur_protection &= src_entry->protection;
13539 *max_protection &= src_entry->max_protection;
13540 }
13541 map_address += tmp_size;
13542 mapped_size += tmp_size;
13543 src_start += tmp_size;
13544
13545 } /* end while */
13546
13547 vm_map_unlock(map);
13548 if (result != KERN_SUCCESS) {
13549 /*
13550 * Free all allocated elements.
13551 */
13552 for (src_entry = map_header->links.next;
13553 src_entry != (struct vm_map_entry *)&map_header->links;
13554 src_entry = new_entry) {
13555 new_entry = src_entry->vme_next;
13556 _vm_map_store_entry_unlink(map_header, src_entry);
13557 if (src_entry->is_sub_map) {
13558 vm_map_deallocate(VME_SUBMAP(src_entry));
13559 } else {
13560 vm_object_deallocate(VME_OBJECT(src_entry));
13561 }
13562 _vm_map_entry_dispose(map_header, src_entry);
13563 }
13564 }
13565 return result;
13566 }
13567
13568 /*
13569 * Routine: vm_remap
13570 *
13571 * Map portion of a task's address space.
13572 * Mapped region must not overlap more than
13573 * one vm memory object. Protections and
13574 * inheritance attributes remain the same
13575 * as in the original task and are out parameters.
13576 * Source and Target task can be identical
13577 * Other attributes are identical as for vm_map()
13578 */
13579 kern_return_t
13580 vm_map_remap(
13581 vm_map_t target_map,
13582 vm_map_address_t *address,
13583 vm_map_size_t size,
13584 vm_map_offset_t mask,
13585 int flags,
13586 vm_map_t src_map,
13587 vm_map_offset_t memory_address,
13588 boolean_t copy,
13589 vm_prot_t *cur_protection,
13590 vm_prot_t *max_protection,
13591 vm_inherit_t inheritance)
13592 {
13593 kern_return_t result;
13594 vm_map_entry_t entry;
13595 vm_map_entry_t insp_entry = VM_MAP_ENTRY_NULL;
13596 vm_map_entry_t new_entry;
13597 struct vm_map_header map_header;
13598 vm_map_offset_t offset_in_mapping;
13599
13600 if (target_map == VM_MAP_NULL)
13601 return KERN_INVALID_ARGUMENT;
13602
13603 switch (inheritance) {
13604 case VM_INHERIT_NONE:
13605 case VM_INHERIT_COPY:
13606 case VM_INHERIT_SHARE:
13607 if (size != 0 && src_map != VM_MAP_NULL)
13608 break;
13609 /*FALL THRU*/
13610 default:
13611 return KERN_INVALID_ARGUMENT;
13612 }
13613
13614 /*
13615 * If the user is requesting that we return the address of the
13616 * first byte of the data (rather than the base of the page),
13617 * then we use different rounding semantics: specifically,
13618 * we assume that (memory_address, size) describes a region
13619 * all of whose pages we must cover, rather than a base to be truncated
13620 * down and a size to be added to that base. So we figure out
13621 * the highest page that the requested region includes and make
13622 * sure that the size will cover it.
13623 *
13624 * The key example we're worried about it is of the form:
13625 *
13626 * memory_address = 0x1ff0, size = 0x20
13627 *
13628 * With the old semantics, we round down the memory_address to 0x1000
13629 * and round up the size to 0x1000, resulting in our covering *only*
13630 * page 0x1000. With the new semantics, we'd realize that the region covers
13631 * 0x1ff0-0x2010, and compute a size of 0x2000. Thus, we cover both page
13632 * 0x1000 and page 0x2000 in the region we remap.
13633 */
13634 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
13635 offset_in_mapping = memory_address - vm_map_trunc_page(memory_address, PAGE_MASK);
13636 size = vm_map_round_page(memory_address + size - vm_map_trunc_page(memory_address, PAGE_MASK), PAGE_MASK);
13637 } else {
13638 size = vm_map_round_page(size, PAGE_MASK);
13639 }
13640
13641 result = vm_map_remap_extract(src_map, memory_address,
13642 size, copy, &map_header,
13643 cur_protection,
13644 max_protection,
13645 inheritance,
13646 target_map->hdr.entries_pageable);
13647
13648 if (result != KERN_SUCCESS) {
13649 return result;
13650 }
13651
13652 /*
13653 * Allocate/check a range of free virtual address
13654 * space for the target
13655 */
13656 *address = vm_map_trunc_page(*address,
13657 VM_MAP_PAGE_MASK(target_map));
13658 vm_map_lock(target_map);
13659 result = vm_map_remap_range_allocate(target_map, address, size,
13660 mask, flags, &insp_entry);
13661
13662 for (entry = map_header.links.next;
13663 entry != (struct vm_map_entry *)&map_header.links;
13664 entry = new_entry) {
13665 new_entry = entry->vme_next;
13666 _vm_map_store_entry_unlink(&map_header, entry);
13667 if (result == KERN_SUCCESS) {
13668 if (flags & VM_FLAGS_RESILIENT_CODESIGN) {
13669 /* no codesigning -> read-only access */
13670 assert(!entry->used_for_jit);
13671 entry->max_protection = VM_PROT_READ;
13672 entry->protection = VM_PROT_READ;
13673 entry->vme_resilient_codesign = TRUE;
13674 }
13675 entry->vme_start += *address;
13676 entry->vme_end += *address;
13677 assert(!entry->map_aligned);
13678 vm_map_store_entry_link(target_map, insp_entry, entry);
13679 insp_entry = entry;
13680 } else {
13681 if (!entry->is_sub_map) {
13682 vm_object_deallocate(VME_OBJECT(entry));
13683 } else {
13684 vm_map_deallocate(VME_SUBMAP(entry));
13685 }
13686 _vm_map_entry_dispose(&map_header, entry);
13687 }
13688 }
13689
13690 if (flags & VM_FLAGS_RESILIENT_CODESIGN) {
13691 *cur_protection = VM_PROT_READ;
13692 *max_protection = VM_PROT_READ;
13693 }
13694
13695 if( target_map->disable_vmentry_reuse == TRUE) {
13696 if( target_map->highest_entry_end < insp_entry->vme_end ){
13697 target_map->highest_entry_end = insp_entry->vme_end;
13698 }
13699 }
13700
13701 if (result == KERN_SUCCESS) {
13702 target_map->size += size;
13703 SAVE_HINT_MAP_WRITE(target_map, insp_entry);
13704 }
13705 vm_map_unlock(target_map);
13706
13707 if (result == KERN_SUCCESS && target_map->wiring_required)
13708 result = vm_map_wire(target_map, *address,
13709 *address + size, *cur_protection | VM_PROT_MEMORY_TAG_MAKE(VM_KERN_MEMORY_MLOCK),
13710 TRUE);
13711
13712 /*
13713 * If requested, return the address of the data pointed to by the
13714 * request, rather than the base of the resulting page.
13715 */
13716 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
13717 *address += offset_in_mapping;
13718 }
13719
13720 return result;
13721 }
13722
13723 /*
13724 * Routine: vm_map_remap_range_allocate
13725 *
13726 * Description:
13727 * Allocate a range in the specified virtual address map.
13728 * returns the address and the map entry just before the allocated
13729 * range
13730 *
13731 * Map must be locked.
13732 */
13733
13734 static kern_return_t
13735 vm_map_remap_range_allocate(
13736 vm_map_t map,
13737 vm_map_address_t *address, /* IN/OUT */
13738 vm_map_size_t size,
13739 vm_map_offset_t mask,
13740 int flags,
13741 vm_map_entry_t *map_entry) /* OUT */
13742 {
13743 vm_map_entry_t entry;
13744 vm_map_offset_t start;
13745 vm_map_offset_t end;
13746 kern_return_t kr;
13747 vm_map_entry_t hole_entry;
13748
13749 StartAgain: ;
13750
13751 start = *address;
13752
13753 if (flags & VM_FLAGS_ANYWHERE)
13754 {
13755 /*
13756 * Calculate the first possible address.
13757 */
13758
13759 if (start < map->min_offset)
13760 start = map->min_offset;
13761 if (start > map->max_offset)
13762 return(KERN_NO_SPACE);
13763
13764 /*
13765 * Look for the first possible address;
13766 * if there's already something at this
13767 * address, we have to start after it.
13768 */
13769
13770 if( map->disable_vmentry_reuse == TRUE) {
13771 VM_MAP_HIGHEST_ENTRY(map, entry, start);
13772 } else {
13773
13774 if (map->holelistenabled) {
13775 hole_entry = (vm_map_entry_t)map->holes_list;
13776
13777 if (hole_entry == NULL) {
13778 /*
13779 * No more space in the map?
13780 */
13781 return(KERN_NO_SPACE);
13782 } else {
13783
13784 boolean_t found_hole = FALSE;
13785
13786 do {
13787 if (hole_entry->vme_start >= start) {
13788 start = hole_entry->vme_start;
13789 found_hole = TRUE;
13790 break;
13791 }
13792
13793 if (hole_entry->vme_end > start) {
13794 found_hole = TRUE;
13795 break;
13796 }
13797 hole_entry = hole_entry->vme_next;
13798
13799 } while (hole_entry != (vm_map_entry_t) map->holes_list);
13800
13801 if (found_hole == FALSE) {
13802 return (KERN_NO_SPACE);
13803 }
13804
13805 entry = hole_entry;
13806 }
13807 } else {
13808 assert(first_free_is_valid(map));
13809 if (start == map->min_offset) {
13810 if ((entry = map->first_free) != vm_map_to_entry(map))
13811 start = entry->vme_end;
13812 } else {
13813 vm_map_entry_t tmp_entry;
13814 if (vm_map_lookup_entry(map, start, &tmp_entry))
13815 start = tmp_entry->vme_end;
13816 entry = tmp_entry;
13817 }
13818 }
13819 start = vm_map_round_page(start,
13820 VM_MAP_PAGE_MASK(map));
13821 }
13822
13823 /*
13824 * In any case, the "entry" always precedes
13825 * the proposed new region throughout the
13826 * loop:
13827 */
13828
13829 while (TRUE) {
13830 register vm_map_entry_t next;
13831
13832 /*
13833 * Find the end of the proposed new region.
13834 * Be sure we didn't go beyond the end, or
13835 * wrap around the address.
13836 */
13837
13838 end = ((start + mask) & ~mask);
13839 end = vm_map_round_page(end,
13840 VM_MAP_PAGE_MASK(map));
13841 if (end < start)
13842 return(KERN_NO_SPACE);
13843 start = end;
13844 end += size;
13845
13846 if ((end > map->max_offset) || (end < start)) {
13847 if (map->wait_for_space) {
13848 if (size <= (map->max_offset -
13849 map->min_offset)) {
13850 assert_wait((event_t) map, THREAD_INTERRUPTIBLE);
13851 vm_map_unlock(map);
13852 thread_block(THREAD_CONTINUE_NULL);
13853 vm_map_lock(map);
13854 goto StartAgain;
13855 }
13856 }
13857
13858 return(KERN_NO_SPACE);
13859 }
13860
13861 next = entry->vme_next;
13862
13863 if (map->holelistenabled) {
13864 if (entry->vme_end >= end)
13865 break;
13866 } else {
13867 /*
13868 * If there are no more entries, we must win.
13869 *
13870 * OR
13871 *
13872 * If there is another entry, it must be
13873 * after the end of the potential new region.
13874 */
13875
13876 if (next == vm_map_to_entry(map))
13877 break;
13878
13879 if (next->vme_start >= end)
13880 break;
13881 }
13882
13883 /*
13884 * Didn't fit -- move to the next entry.
13885 */
13886
13887 entry = next;
13888
13889 if (map->holelistenabled) {
13890 if (entry == (vm_map_entry_t) map->holes_list) {
13891 /*
13892 * Wrapped around
13893 */
13894 return(KERN_NO_SPACE);
13895 }
13896 start = entry->vme_start;
13897 } else {
13898 start = entry->vme_end;
13899 }
13900 }
13901
13902 if (map->holelistenabled) {
13903
13904 if (vm_map_lookup_entry(map, entry->vme_start, &entry)) {
13905 panic("Found an existing entry (%p) instead of potential hole at address: 0x%llx.\n", entry, (unsigned long long)entry->vme_start);
13906 }
13907 }
13908
13909 *address = start;
13910
13911 } else {
13912 vm_map_entry_t temp_entry;
13913
13914 /*
13915 * Verify that:
13916 * the address doesn't itself violate
13917 * the mask requirement.
13918 */
13919
13920 if ((start & mask) != 0)
13921 return(KERN_NO_SPACE);
13922
13923
13924 /*
13925 * ... the address is within bounds
13926 */
13927
13928 end = start + size;
13929
13930 if ((start < map->min_offset) ||
13931 (end > map->max_offset) ||
13932 (start >= end)) {
13933 return(KERN_INVALID_ADDRESS);
13934 }
13935
13936 /*
13937 * If we're asked to overwrite whatever was mapped in that
13938 * range, first deallocate that range.
13939 */
13940 if (flags & VM_FLAGS_OVERWRITE) {
13941 vm_map_t zap_map;
13942
13943 /*
13944 * We use a "zap_map" to avoid having to unlock
13945 * the "map" in vm_map_delete(), which would compromise
13946 * the atomicity of the "deallocate" and then "remap"
13947 * combination.
13948 */
13949 zap_map = vm_map_create(PMAP_NULL,
13950 start,
13951 end,
13952 map->hdr.entries_pageable);
13953 if (zap_map == VM_MAP_NULL) {
13954 return KERN_RESOURCE_SHORTAGE;
13955 }
13956 vm_map_set_page_shift(zap_map, VM_MAP_PAGE_SHIFT(map));
13957 vm_map_disable_hole_optimization(zap_map);
13958
13959 kr = vm_map_delete(map, start, end,
13960 (VM_MAP_REMOVE_SAVE_ENTRIES |
13961 VM_MAP_REMOVE_NO_MAP_ALIGN),
13962 zap_map);
13963 if (kr == KERN_SUCCESS) {
13964 vm_map_destroy(zap_map,
13965 VM_MAP_REMOVE_NO_PMAP_CLEANUP);
13966 zap_map = VM_MAP_NULL;
13967 }
13968 }
13969
13970 /*
13971 * ... the starting address isn't allocated
13972 */
13973
13974 if (vm_map_lookup_entry(map, start, &temp_entry))
13975 return(KERN_NO_SPACE);
13976
13977 entry = temp_entry;
13978
13979 /*
13980 * ... the next region doesn't overlap the
13981 * end point.
13982 */
13983
13984 if ((entry->vme_next != vm_map_to_entry(map)) &&
13985 (entry->vme_next->vme_start < end))
13986 return(KERN_NO_SPACE);
13987 }
13988 *map_entry = entry;
13989 return(KERN_SUCCESS);
13990 }
13991
13992 /*
13993 * vm_map_switch:
13994 *
13995 * Set the address map for the current thread to the specified map
13996 */
13997
13998 vm_map_t
13999 vm_map_switch(
14000 vm_map_t map)
14001 {
14002 int mycpu;
14003 thread_t thread = current_thread();
14004 vm_map_t oldmap = thread->map;
14005
14006 mp_disable_preemption();
14007 mycpu = cpu_number();
14008
14009 /*
14010 * Deactivate the current map and activate the requested map
14011 */
14012 PMAP_SWITCH_USER(thread, map, mycpu);
14013
14014 mp_enable_preemption();
14015 return(oldmap);
14016 }
14017
14018
14019 /*
14020 * Routine: vm_map_write_user
14021 *
14022 * Description:
14023 * Copy out data from a kernel space into space in the
14024 * destination map. The space must already exist in the
14025 * destination map.
14026 * NOTE: This routine should only be called by threads
14027 * which can block on a page fault. i.e. kernel mode user
14028 * threads.
14029 *
14030 */
14031 kern_return_t
14032 vm_map_write_user(
14033 vm_map_t map,
14034 void *src_p,
14035 vm_map_address_t dst_addr,
14036 vm_size_t size)
14037 {
14038 kern_return_t kr = KERN_SUCCESS;
14039
14040 if(current_map() == map) {
14041 if (copyout(src_p, dst_addr, size)) {
14042 kr = KERN_INVALID_ADDRESS;
14043 }
14044 } else {
14045 vm_map_t oldmap;
14046
14047 /* take on the identity of the target map while doing */
14048 /* the transfer */
14049
14050 vm_map_reference(map);
14051 oldmap = vm_map_switch(map);
14052 if (copyout(src_p, dst_addr, size)) {
14053 kr = KERN_INVALID_ADDRESS;
14054 }
14055 vm_map_switch(oldmap);
14056 vm_map_deallocate(map);
14057 }
14058 return kr;
14059 }
14060
14061 /*
14062 * Routine: vm_map_read_user
14063 *
14064 * Description:
14065 * Copy in data from a user space source map into the
14066 * kernel map. The space must already exist in the
14067 * kernel map.
14068 * NOTE: This routine should only be called by threads
14069 * which can block on a page fault. i.e. kernel mode user
14070 * threads.
14071 *
14072 */
14073 kern_return_t
14074 vm_map_read_user(
14075 vm_map_t map,
14076 vm_map_address_t src_addr,
14077 void *dst_p,
14078 vm_size_t size)
14079 {
14080 kern_return_t kr = KERN_SUCCESS;
14081
14082 if(current_map() == map) {
14083 if (copyin(src_addr, dst_p, size)) {
14084 kr = KERN_INVALID_ADDRESS;
14085 }
14086 } else {
14087 vm_map_t oldmap;
14088
14089 /* take on the identity of the target map while doing */
14090 /* the transfer */
14091
14092 vm_map_reference(map);
14093 oldmap = vm_map_switch(map);
14094 if (copyin(src_addr, dst_p, size)) {
14095 kr = KERN_INVALID_ADDRESS;
14096 }
14097 vm_map_switch(oldmap);
14098 vm_map_deallocate(map);
14099 }
14100 return kr;
14101 }
14102
14103
14104 /*
14105 * vm_map_check_protection:
14106 *
14107 * Assert that the target map allows the specified
14108 * privilege on the entire address region given.
14109 * The entire region must be allocated.
14110 */
14111 boolean_t
14112 vm_map_check_protection(vm_map_t map, vm_map_offset_t start,
14113 vm_map_offset_t end, vm_prot_t protection)
14114 {
14115 vm_map_entry_t entry;
14116 vm_map_entry_t tmp_entry;
14117
14118 vm_map_lock(map);
14119
14120 if (start < vm_map_min(map) || end > vm_map_max(map) || start > end)
14121 {
14122 vm_map_unlock(map);
14123 return (FALSE);
14124 }
14125
14126 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
14127 vm_map_unlock(map);
14128 return(FALSE);
14129 }
14130
14131 entry = tmp_entry;
14132
14133 while (start < end) {
14134 if (entry == vm_map_to_entry(map)) {
14135 vm_map_unlock(map);
14136 return(FALSE);
14137 }
14138
14139 /*
14140 * No holes allowed!
14141 */
14142
14143 if (start < entry->vme_start) {
14144 vm_map_unlock(map);
14145 return(FALSE);
14146 }
14147
14148 /*
14149 * Check protection associated with entry.
14150 */
14151
14152 if ((entry->protection & protection) != protection) {
14153 vm_map_unlock(map);
14154 return(FALSE);
14155 }
14156
14157 /* go to next entry */
14158
14159 start = entry->vme_end;
14160 entry = entry->vme_next;
14161 }
14162 vm_map_unlock(map);
14163 return(TRUE);
14164 }
14165
14166 kern_return_t
14167 vm_map_purgable_control(
14168 vm_map_t map,
14169 vm_map_offset_t address,
14170 vm_purgable_t control,
14171 int *state)
14172 {
14173 vm_map_entry_t entry;
14174 vm_object_t object;
14175 kern_return_t kr;
14176 boolean_t was_nonvolatile;
14177
14178 /*
14179 * Vet all the input parameters and current type and state of the
14180 * underlaying object. Return with an error if anything is amiss.
14181 */
14182 if (map == VM_MAP_NULL)
14183 return(KERN_INVALID_ARGUMENT);
14184
14185 if (control != VM_PURGABLE_SET_STATE &&
14186 control != VM_PURGABLE_GET_STATE &&
14187 control != VM_PURGABLE_PURGE_ALL)
14188 return(KERN_INVALID_ARGUMENT);
14189
14190 if (control == VM_PURGABLE_PURGE_ALL) {
14191 vm_purgeable_object_purge_all();
14192 return KERN_SUCCESS;
14193 }
14194
14195 if (control == VM_PURGABLE_SET_STATE &&
14196 (((*state & ~(VM_PURGABLE_ALL_MASKS)) != 0) ||
14197 ((*state & VM_PURGABLE_STATE_MASK) > VM_PURGABLE_STATE_MASK)))
14198 return(KERN_INVALID_ARGUMENT);
14199
14200 vm_map_lock_read(map);
14201
14202 if (!vm_map_lookup_entry(map, address, &entry) || entry->is_sub_map) {
14203
14204 /*
14205 * Must pass a valid non-submap address.
14206 */
14207 vm_map_unlock_read(map);
14208 return(KERN_INVALID_ADDRESS);
14209 }
14210
14211 if ((entry->protection & VM_PROT_WRITE) == 0) {
14212 /*
14213 * Can't apply purgable controls to something you can't write.
14214 */
14215 vm_map_unlock_read(map);
14216 return(KERN_PROTECTION_FAILURE);
14217 }
14218
14219 object = VME_OBJECT(entry);
14220 if (object == VM_OBJECT_NULL ||
14221 object->purgable == VM_PURGABLE_DENY) {
14222 /*
14223 * Object must already be present and be purgeable.
14224 */
14225 vm_map_unlock_read(map);
14226 return KERN_INVALID_ARGUMENT;
14227 }
14228
14229 vm_object_lock(object);
14230
14231 #if 00
14232 if (VME_OFFSET(entry) != 0 ||
14233 entry->vme_end - entry->vme_start != object->vo_size) {
14234 /*
14235 * Can only apply purgable controls to the whole (existing)
14236 * object at once.
14237 */
14238 vm_map_unlock_read(map);
14239 vm_object_unlock(object);
14240 return KERN_INVALID_ARGUMENT;
14241 }
14242 #endif
14243
14244 assert(!entry->is_sub_map);
14245 assert(!entry->use_pmap); /* purgeable has its own accounting */
14246
14247 vm_map_unlock_read(map);
14248
14249 was_nonvolatile = (object->purgable == VM_PURGABLE_NONVOLATILE);
14250
14251 kr = vm_object_purgable_control(object, control, state);
14252
14253 if (was_nonvolatile &&
14254 object->purgable != VM_PURGABLE_NONVOLATILE &&
14255 map->pmap == kernel_pmap) {
14256 #if DEBUG
14257 object->vo_purgeable_volatilizer = kernel_task;
14258 #endif /* DEBUG */
14259 }
14260
14261 vm_object_unlock(object);
14262
14263 return kr;
14264 }
14265
14266 kern_return_t
14267 vm_map_page_query_internal(
14268 vm_map_t target_map,
14269 vm_map_offset_t offset,
14270 int *disposition,
14271 int *ref_count)
14272 {
14273 kern_return_t kr;
14274 vm_page_info_basic_data_t info;
14275 mach_msg_type_number_t count;
14276
14277 count = VM_PAGE_INFO_BASIC_COUNT;
14278 kr = vm_map_page_info(target_map,
14279 offset,
14280 VM_PAGE_INFO_BASIC,
14281 (vm_page_info_t) &info,
14282 &count);
14283 if (kr == KERN_SUCCESS) {
14284 *disposition = info.disposition;
14285 *ref_count = info.ref_count;
14286 } else {
14287 *disposition = 0;
14288 *ref_count = 0;
14289 }
14290
14291 return kr;
14292 }
14293
14294 kern_return_t
14295 vm_map_page_info(
14296 vm_map_t map,
14297 vm_map_offset_t offset,
14298 vm_page_info_flavor_t flavor,
14299 vm_page_info_t info,
14300 mach_msg_type_number_t *count)
14301 {
14302 vm_map_entry_t map_entry;
14303 vm_object_t object;
14304 vm_page_t m;
14305 kern_return_t kr;
14306 kern_return_t retval = KERN_SUCCESS;
14307 boolean_t top_object;
14308 int disposition;
14309 int ref_count;
14310 vm_page_info_basic_t basic_info;
14311 int depth;
14312 vm_map_offset_t offset_in_page;
14313
14314 switch (flavor) {
14315 case VM_PAGE_INFO_BASIC:
14316 if (*count != VM_PAGE_INFO_BASIC_COUNT) {
14317 /*
14318 * The "vm_page_info_basic_data" structure was not
14319 * properly padded, so allow the size to be off by
14320 * one to maintain backwards binary compatibility...
14321 */
14322 if (*count != VM_PAGE_INFO_BASIC_COUNT - 1)
14323 return KERN_INVALID_ARGUMENT;
14324 }
14325 break;
14326 default:
14327 return KERN_INVALID_ARGUMENT;
14328 }
14329
14330 disposition = 0;
14331 ref_count = 0;
14332 top_object = TRUE;
14333 depth = 0;
14334
14335 retval = KERN_SUCCESS;
14336 offset_in_page = offset & PAGE_MASK;
14337 offset = vm_map_trunc_page(offset, PAGE_MASK);
14338
14339 vm_map_lock_read(map);
14340
14341 /*
14342 * First, find the map entry covering "offset", going down
14343 * submaps if necessary.
14344 */
14345 for (;;) {
14346 if (!vm_map_lookup_entry(map, offset, &map_entry)) {
14347 vm_map_unlock_read(map);
14348 return KERN_INVALID_ADDRESS;
14349 }
14350 /* compute offset from this map entry's start */
14351 offset -= map_entry->vme_start;
14352 /* compute offset into this map entry's object (or submap) */
14353 offset += VME_OFFSET(map_entry);
14354
14355 if (map_entry->is_sub_map) {
14356 vm_map_t sub_map;
14357
14358 sub_map = VME_SUBMAP(map_entry);
14359 vm_map_lock_read(sub_map);
14360 vm_map_unlock_read(map);
14361
14362 map = sub_map;
14363
14364 ref_count = MAX(ref_count, map->ref_count);
14365 continue;
14366 }
14367 break;
14368 }
14369
14370 object = VME_OBJECT(map_entry);
14371 if (object == VM_OBJECT_NULL) {
14372 /* no object -> no page */
14373 vm_map_unlock_read(map);
14374 goto done;
14375 }
14376
14377 vm_object_lock(object);
14378 vm_map_unlock_read(map);
14379
14380 /*
14381 * Go down the VM object shadow chain until we find the page
14382 * we're looking for.
14383 */
14384 for (;;) {
14385 ref_count = MAX(ref_count, object->ref_count);
14386
14387 m = vm_page_lookup(object, offset);
14388
14389 if (m != VM_PAGE_NULL) {
14390 disposition |= VM_PAGE_QUERY_PAGE_PRESENT;
14391 break;
14392 } else {
14393 #if MACH_PAGEMAP
14394 if (object->existence_map) {
14395 if (vm_external_state_get(object->existence_map,
14396 offset) ==
14397 VM_EXTERNAL_STATE_EXISTS) {
14398 /*
14399 * this page has been paged out
14400 */
14401 disposition |= VM_PAGE_QUERY_PAGE_PAGED_OUT;
14402 break;
14403 }
14404 } else
14405 #endif
14406 if (object->internal &&
14407 object->alive &&
14408 !object->terminating &&
14409 object->pager_ready) {
14410
14411 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
14412 if (VM_COMPRESSOR_PAGER_STATE_GET(
14413 object,
14414 offset)
14415 == VM_EXTERNAL_STATE_EXISTS) {
14416 /* the pager has that page */
14417 disposition |= VM_PAGE_QUERY_PAGE_PAGED_OUT;
14418 break;
14419 }
14420 } else {
14421 memory_object_t pager;
14422
14423 vm_object_paging_begin(object);
14424 pager = object->pager;
14425 vm_object_unlock(object);
14426
14427 /*
14428 * Ask the default pager if
14429 * it has this page.
14430 */
14431 kr = memory_object_data_request(
14432 pager,
14433 offset + object->paging_offset,
14434 0, /* just poke the pager */
14435 VM_PROT_READ,
14436 NULL);
14437
14438 vm_object_lock(object);
14439 vm_object_paging_end(object);
14440
14441 if (kr == KERN_SUCCESS) {
14442 /* the default pager has it */
14443 disposition |= VM_PAGE_QUERY_PAGE_PAGED_OUT;
14444 break;
14445 }
14446 }
14447 }
14448
14449 if (object->shadow != VM_OBJECT_NULL) {
14450 vm_object_t shadow;
14451
14452 offset += object->vo_shadow_offset;
14453 shadow = object->shadow;
14454
14455 vm_object_lock(shadow);
14456 vm_object_unlock(object);
14457
14458 object = shadow;
14459 top_object = FALSE;
14460 depth++;
14461 } else {
14462 // if (!object->internal)
14463 // break;
14464 // retval = KERN_FAILURE;
14465 // goto done_with_object;
14466 break;
14467 }
14468 }
14469 }
14470 /* The ref_count is not strictly accurate, it measures the number */
14471 /* of entities holding a ref on the object, they may not be mapping */
14472 /* the object or may not be mapping the section holding the */
14473 /* target page but its still a ball park number and though an over- */
14474 /* count, it picks up the copy-on-write cases */
14475
14476 /* We could also get a picture of page sharing from pmap_attributes */
14477 /* but this would under count as only faulted-in mappings would */
14478 /* show up. */
14479
14480 if (top_object == TRUE && object->shadow)
14481 disposition |= VM_PAGE_QUERY_PAGE_COPIED;
14482
14483 if (! object->internal)
14484 disposition |= VM_PAGE_QUERY_PAGE_EXTERNAL;
14485
14486 if (m == VM_PAGE_NULL)
14487 goto done_with_object;
14488
14489 if (m->fictitious) {
14490 disposition |= VM_PAGE_QUERY_PAGE_FICTITIOUS;
14491 goto done_with_object;
14492 }
14493 if (m->dirty || pmap_is_modified(m->phys_page))
14494 disposition |= VM_PAGE_QUERY_PAGE_DIRTY;
14495
14496 if (m->reference || pmap_is_referenced(m->phys_page))
14497 disposition |= VM_PAGE_QUERY_PAGE_REF;
14498
14499 if (m->speculative)
14500 disposition |= VM_PAGE_QUERY_PAGE_SPECULATIVE;
14501
14502 if (m->cs_validated)
14503 disposition |= VM_PAGE_QUERY_PAGE_CS_VALIDATED;
14504 if (m->cs_tainted)
14505 disposition |= VM_PAGE_QUERY_PAGE_CS_TAINTED;
14506 if (m->cs_nx)
14507 disposition |= VM_PAGE_QUERY_PAGE_CS_NX;
14508
14509 done_with_object:
14510 vm_object_unlock(object);
14511 done:
14512
14513 switch (flavor) {
14514 case VM_PAGE_INFO_BASIC:
14515 basic_info = (vm_page_info_basic_t) info;
14516 basic_info->disposition = disposition;
14517 basic_info->ref_count = ref_count;
14518 basic_info->object_id = (vm_object_id_t) (uintptr_t)
14519 VM_KERNEL_ADDRPERM(object);
14520 basic_info->offset =
14521 (memory_object_offset_t) offset + offset_in_page;
14522 basic_info->depth = depth;
14523 break;
14524 }
14525
14526 return retval;
14527 }
14528
14529 /*
14530 * vm_map_msync
14531 *
14532 * Synchronises the memory range specified with its backing store
14533 * image by either flushing or cleaning the contents to the appropriate
14534 * memory manager engaging in a memory object synchronize dialog with
14535 * the manager. The client doesn't return until the manager issues
14536 * m_o_s_completed message. MIG Magically converts user task parameter
14537 * to the task's address map.
14538 *
14539 * interpretation of sync_flags
14540 * VM_SYNC_INVALIDATE - discard pages, only return precious
14541 * pages to manager.
14542 *
14543 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
14544 * - discard pages, write dirty or precious
14545 * pages back to memory manager.
14546 *
14547 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
14548 * - write dirty or precious pages back to
14549 * the memory manager.
14550 *
14551 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
14552 * is a hole in the region, and we would
14553 * have returned KERN_SUCCESS, return
14554 * KERN_INVALID_ADDRESS instead.
14555 *
14556 * NOTE
14557 * The memory object attributes have not yet been implemented, this
14558 * function will have to deal with the invalidate attribute
14559 *
14560 * RETURNS
14561 * KERN_INVALID_TASK Bad task parameter
14562 * KERN_INVALID_ARGUMENT both sync and async were specified.
14563 * KERN_SUCCESS The usual.
14564 * KERN_INVALID_ADDRESS There was a hole in the region.
14565 */
14566
14567 kern_return_t
14568 vm_map_msync(
14569 vm_map_t map,
14570 vm_map_address_t address,
14571 vm_map_size_t size,
14572 vm_sync_t sync_flags)
14573 {
14574 msync_req_t msr;
14575 msync_req_t new_msr;
14576 queue_chain_t req_q; /* queue of requests for this msync */
14577 vm_map_entry_t entry;
14578 vm_map_size_t amount_left;
14579 vm_object_offset_t offset;
14580 boolean_t do_sync_req;
14581 boolean_t had_hole = FALSE;
14582 memory_object_t pager;
14583 vm_map_offset_t pmap_offset;
14584
14585 if ((sync_flags & VM_SYNC_ASYNCHRONOUS) &&
14586 (sync_flags & VM_SYNC_SYNCHRONOUS))
14587 return(KERN_INVALID_ARGUMENT);
14588
14589 /*
14590 * align address and size on page boundaries
14591 */
14592 size = (vm_map_round_page(address + size,
14593 VM_MAP_PAGE_MASK(map)) -
14594 vm_map_trunc_page(address,
14595 VM_MAP_PAGE_MASK(map)));
14596 address = vm_map_trunc_page(address,
14597 VM_MAP_PAGE_MASK(map));
14598
14599 if (map == VM_MAP_NULL)
14600 return(KERN_INVALID_TASK);
14601
14602 if (size == 0)
14603 return(KERN_SUCCESS);
14604
14605 queue_init(&req_q);
14606 amount_left = size;
14607
14608 while (amount_left > 0) {
14609 vm_object_size_t flush_size;
14610 vm_object_t object;
14611
14612 vm_map_lock(map);
14613 if (!vm_map_lookup_entry(map,
14614 address,
14615 &entry)) {
14616
14617 vm_map_size_t skip;
14618
14619 /*
14620 * hole in the address map.
14621 */
14622 had_hole = TRUE;
14623
14624 /*
14625 * Check for empty map.
14626 */
14627 if (entry == vm_map_to_entry(map) &&
14628 entry->vme_next == entry) {
14629 vm_map_unlock(map);
14630 break;
14631 }
14632 /*
14633 * Check that we don't wrap and that
14634 * we have at least one real map entry.
14635 */
14636 if ((map->hdr.nentries == 0) ||
14637 (entry->vme_next->vme_start < address)) {
14638 vm_map_unlock(map);
14639 break;
14640 }
14641 /*
14642 * Move up to the next entry if needed
14643 */
14644 skip = (entry->vme_next->vme_start - address);
14645 if (skip >= amount_left)
14646 amount_left = 0;
14647 else
14648 amount_left -= skip;
14649 address = entry->vme_next->vme_start;
14650 vm_map_unlock(map);
14651 continue;
14652 }
14653
14654 offset = address - entry->vme_start;
14655 pmap_offset = address;
14656
14657 /*
14658 * do we have more to flush than is contained in this
14659 * entry ?
14660 */
14661 if (amount_left + entry->vme_start + offset > entry->vme_end) {
14662 flush_size = entry->vme_end -
14663 (entry->vme_start + offset);
14664 } else {
14665 flush_size = amount_left;
14666 }
14667 amount_left -= flush_size;
14668 address += flush_size;
14669
14670 if (entry->is_sub_map == TRUE) {
14671 vm_map_t local_map;
14672 vm_map_offset_t local_offset;
14673
14674 local_map = VME_SUBMAP(entry);
14675 local_offset = VME_OFFSET(entry);
14676 vm_map_unlock(map);
14677 if (vm_map_msync(
14678 local_map,
14679 local_offset,
14680 flush_size,
14681 sync_flags) == KERN_INVALID_ADDRESS) {
14682 had_hole = TRUE;
14683 }
14684 continue;
14685 }
14686 object = VME_OBJECT(entry);
14687
14688 /*
14689 * We can't sync this object if the object has not been
14690 * created yet
14691 */
14692 if (object == VM_OBJECT_NULL) {
14693 vm_map_unlock(map);
14694 continue;
14695 }
14696 offset += VME_OFFSET(entry);
14697
14698 vm_object_lock(object);
14699
14700 if (sync_flags & (VM_SYNC_KILLPAGES | VM_SYNC_DEACTIVATE)) {
14701 int kill_pages = 0;
14702 boolean_t reusable_pages = FALSE;
14703
14704 if (sync_flags & VM_SYNC_KILLPAGES) {
14705 if (object->ref_count == 1 && !object->shadow)
14706 kill_pages = 1;
14707 else
14708 kill_pages = -1;
14709 }
14710 if (kill_pages != -1)
14711 vm_object_deactivate_pages(
14712 object,
14713 offset,
14714 (vm_object_size_t) flush_size,
14715 kill_pages,
14716 reusable_pages,
14717 map->pmap,
14718 pmap_offset);
14719 vm_object_unlock(object);
14720 vm_map_unlock(map);
14721 continue;
14722 }
14723 /*
14724 * We can't sync this object if there isn't a pager.
14725 * Don't bother to sync internal objects, since there can't
14726 * be any "permanent" storage for these objects anyway.
14727 */
14728 if ((object->pager == MEMORY_OBJECT_NULL) ||
14729 (object->internal) || (object->private)) {
14730 vm_object_unlock(object);
14731 vm_map_unlock(map);
14732 continue;
14733 }
14734 /*
14735 * keep reference on the object until syncing is done
14736 */
14737 vm_object_reference_locked(object);
14738 vm_object_unlock(object);
14739
14740 vm_map_unlock(map);
14741
14742 do_sync_req = vm_object_sync(object,
14743 offset,
14744 flush_size,
14745 sync_flags & VM_SYNC_INVALIDATE,
14746 ((sync_flags & VM_SYNC_SYNCHRONOUS) ||
14747 (sync_flags & VM_SYNC_ASYNCHRONOUS)),
14748 sync_flags & VM_SYNC_SYNCHRONOUS);
14749 /*
14750 * only send a m_o_s if we returned pages or if the entry
14751 * is writable (ie dirty pages may have already been sent back)
14752 */
14753 if (!do_sync_req) {
14754 if ((sync_flags & VM_SYNC_INVALIDATE) && object->resident_page_count == 0) {
14755 /*
14756 * clear out the clustering and read-ahead hints
14757 */
14758 vm_object_lock(object);
14759
14760 object->pages_created = 0;
14761 object->pages_used = 0;
14762 object->sequential = 0;
14763 object->last_alloc = 0;
14764
14765 vm_object_unlock(object);
14766 }
14767 vm_object_deallocate(object);
14768 continue;
14769 }
14770 msync_req_alloc(new_msr);
14771
14772 vm_object_lock(object);
14773 offset += object->paging_offset;
14774
14775 new_msr->offset = offset;
14776 new_msr->length = flush_size;
14777 new_msr->object = object;
14778 new_msr->flag = VM_MSYNC_SYNCHRONIZING;
14779 re_iterate:
14780
14781 /*
14782 * We can't sync this object if there isn't a pager. The
14783 * pager can disappear anytime we're not holding the object
14784 * lock. So this has to be checked anytime we goto re_iterate.
14785 */
14786
14787 pager = object->pager;
14788
14789 if (pager == MEMORY_OBJECT_NULL) {
14790 vm_object_unlock(object);
14791 vm_object_deallocate(object);
14792 msync_req_free(new_msr);
14793 new_msr = NULL;
14794 continue;
14795 }
14796
14797 queue_iterate(&object->msr_q, msr, msync_req_t, msr_q) {
14798 /*
14799 * need to check for overlapping entry, if found, wait
14800 * on overlapping msr to be done, then reiterate
14801 */
14802 msr_lock(msr);
14803 if (msr->flag == VM_MSYNC_SYNCHRONIZING &&
14804 ((offset >= msr->offset &&
14805 offset < (msr->offset + msr->length)) ||
14806 (msr->offset >= offset &&
14807 msr->offset < (offset + flush_size))))
14808 {
14809 assert_wait((event_t) msr,THREAD_INTERRUPTIBLE);
14810 msr_unlock(msr);
14811 vm_object_unlock(object);
14812 thread_block(THREAD_CONTINUE_NULL);
14813 vm_object_lock(object);
14814 goto re_iterate;
14815 }
14816 msr_unlock(msr);
14817 }/* queue_iterate */
14818
14819 queue_enter(&object->msr_q, new_msr, msync_req_t, msr_q);
14820
14821 vm_object_paging_begin(object);
14822 vm_object_unlock(object);
14823
14824 queue_enter(&req_q, new_msr, msync_req_t, req_q);
14825
14826 (void) memory_object_synchronize(
14827 pager,
14828 offset,
14829 flush_size,
14830 sync_flags & ~VM_SYNC_CONTIGUOUS);
14831
14832 vm_object_lock(object);
14833 vm_object_paging_end(object);
14834 vm_object_unlock(object);
14835 }/* while */
14836
14837 /*
14838 * wait for memory_object_sychronize_completed messages from pager(s)
14839 */
14840
14841 while (!queue_empty(&req_q)) {
14842 msr = (msync_req_t)queue_first(&req_q);
14843 msr_lock(msr);
14844 while(msr->flag != VM_MSYNC_DONE) {
14845 assert_wait((event_t) msr, THREAD_INTERRUPTIBLE);
14846 msr_unlock(msr);
14847 thread_block(THREAD_CONTINUE_NULL);
14848 msr_lock(msr);
14849 }/* while */
14850 queue_remove(&req_q, msr, msync_req_t, req_q);
14851 msr_unlock(msr);
14852 vm_object_deallocate(msr->object);
14853 msync_req_free(msr);
14854 }/* queue_iterate */
14855
14856 /* for proper msync() behaviour */
14857 if (had_hole == TRUE && (sync_flags & VM_SYNC_CONTIGUOUS))
14858 return(KERN_INVALID_ADDRESS);
14859
14860 return(KERN_SUCCESS);
14861 }/* vm_msync */
14862
14863 /*
14864 * Routine: convert_port_entry_to_map
14865 * Purpose:
14866 * Convert from a port specifying an entry or a task
14867 * to a map. Doesn't consume the port ref; produces a map ref,
14868 * which may be null. Unlike convert_port_to_map, the
14869 * port may be task or a named entry backed.
14870 * Conditions:
14871 * Nothing locked.
14872 */
14873
14874
14875 vm_map_t
14876 convert_port_entry_to_map(
14877 ipc_port_t port)
14878 {
14879 vm_map_t map;
14880 vm_named_entry_t named_entry;
14881 uint32_t try_failed_count = 0;
14882
14883 if(IP_VALID(port) && (ip_kotype(port) == IKOT_NAMED_ENTRY)) {
14884 while(TRUE) {
14885 ip_lock(port);
14886 if(ip_active(port) && (ip_kotype(port)
14887 == IKOT_NAMED_ENTRY)) {
14888 named_entry =
14889 (vm_named_entry_t)port->ip_kobject;
14890 if (!(lck_mtx_try_lock(&(named_entry)->Lock))) {
14891 ip_unlock(port);
14892
14893 try_failed_count++;
14894 mutex_pause(try_failed_count);
14895 continue;
14896 }
14897 named_entry->ref_count++;
14898 lck_mtx_unlock(&(named_entry)->Lock);
14899 ip_unlock(port);
14900 if ((named_entry->is_sub_map) &&
14901 (named_entry->protection
14902 & VM_PROT_WRITE)) {
14903 map = named_entry->backing.map;
14904 } else {
14905 mach_destroy_memory_entry(port);
14906 return VM_MAP_NULL;
14907 }
14908 vm_map_reference_swap(map);
14909 mach_destroy_memory_entry(port);
14910 break;
14911 }
14912 else
14913 return VM_MAP_NULL;
14914 }
14915 }
14916 else
14917 map = convert_port_to_map(port);
14918
14919 return map;
14920 }
14921
14922 /*
14923 * Routine: convert_port_entry_to_object
14924 * Purpose:
14925 * Convert from a port specifying a named entry to an
14926 * object. Doesn't consume the port ref; produces a map ref,
14927 * which may be null.
14928 * Conditions:
14929 * Nothing locked.
14930 */
14931
14932
14933 vm_object_t
14934 convert_port_entry_to_object(
14935 ipc_port_t port)
14936 {
14937 vm_object_t object = VM_OBJECT_NULL;
14938 vm_named_entry_t named_entry;
14939 uint32_t try_failed_count = 0;
14940
14941 if (IP_VALID(port) &&
14942 (ip_kotype(port) == IKOT_NAMED_ENTRY)) {
14943 try_again:
14944 ip_lock(port);
14945 if (ip_active(port) &&
14946 (ip_kotype(port) == IKOT_NAMED_ENTRY)) {
14947 named_entry = (vm_named_entry_t)port->ip_kobject;
14948 if (!(lck_mtx_try_lock(&(named_entry)->Lock))) {
14949 ip_unlock(port);
14950 try_failed_count++;
14951 mutex_pause(try_failed_count);
14952 goto try_again;
14953 }
14954 named_entry->ref_count++;
14955 lck_mtx_unlock(&(named_entry)->Lock);
14956 ip_unlock(port);
14957 if (!(named_entry->is_sub_map) &&
14958 !(named_entry->is_pager) &&
14959 !(named_entry->is_copy) &&
14960 (named_entry->protection & VM_PROT_WRITE)) {
14961 object = named_entry->backing.object;
14962 vm_object_reference(object);
14963 }
14964 mach_destroy_memory_entry(port);
14965 }
14966 }
14967
14968 return object;
14969 }
14970
14971 /*
14972 * Export routines to other components for the things we access locally through
14973 * macros.
14974 */
14975 #undef current_map
14976 vm_map_t
14977 current_map(void)
14978 {
14979 return (current_map_fast());
14980 }
14981
14982 /*
14983 * vm_map_reference:
14984 *
14985 * Most code internal to the osfmk will go through a
14986 * macro defining this. This is always here for the
14987 * use of other kernel components.
14988 */
14989 #undef vm_map_reference
14990 void
14991 vm_map_reference(
14992 register vm_map_t map)
14993 {
14994 if (map == VM_MAP_NULL)
14995 return;
14996
14997 lck_mtx_lock(&map->s_lock);
14998 #if TASK_SWAPPER
14999 assert(map->res_count > 0);
15000 assert(map->ref_count >= map->res_count);
15001 map->res_count++;
15002 #endif
15003 map->ref_count++;
15004 lck_mtx_unlock(&map->s_lock);
15005 }
15006
15007 /*
15008 * vm_map_deallocate:
15009 *
15010 * Removes a reference from the specified map,
15011 * destroying it if no references remain.
15012 * The map should not be locked.
15013 */
15014 void
15015 vm_map_deallocate(
15016 register vm_map_t map)
15017 {
15018 unsigned int ref;
15019
15020 if (map == VM_MAP_NULL)
15021 return;
15022
15023 lck_mtx_lock(&map->s_lock);
15024 ref = --map->ref_count;
15025 if (ref > 0) {
15026 vm_map_res_deallocate(map);
15027 lck_mtx_unlock(&map->s_lock);
15028 return;
15029 }
15030 assert(map->ref_count == 0);
15031 lck_mtx_unlock(&map->s_lock);
15032
15033 #if TASK_SWAPPER
15034 /*
15035 * The map residence count isn't decremented here because
15036 * the vm_map_delete below will traverse the entire map,
15037 * deleting entries, and the residence counts on objects
15038 * and sharing maps will go away then.
15039 */
15040 #endif
15041
15042 vm_map_destroy(map, VM_MAP_NO_FLAGS);
15043 }
15044
15045
15046 void
15047 vm_map_disable_NX(vm_map_t map)
15048 {
15049 if (map == NULL)
15050 return;
15051 if (map->pmap == NULL)
15052 return;
15053
15054 pmap_disable_NX(map->pmap);
15055 }
15056
15057 void
15058 vm_map_disallow_data_exec(vm_map_t map)
15059 {
15060 if (map == NULL)
15061 return;
15062
15063 map->map_disallow_data_exec = TRUE;
15064 }
15065
15066 /* XXX Consider making these constants (VM_MAX_ADDRESS and MACH_VM_MAX_ADDRESS)
15067 * more descriptive.
15068 */
15069 void
15070 vm_map_set_32bit(vm_map_t map)
15071 {
15072 map->max_offset = (vm_map_offset_t)VM_MAX_ADDRESS;
15073 }
15074
15075
15076 void
15077 vm_map_set_64bit(vm_map_t map)
15078 {
15079 map->max_offset = (vm_map_offset_t)MACH_VM_MAX_ADDRESS;
15080 }
15081
15082 vm_map_offset_t
15083 vm_compute_max_offset(boolean_t is64)
15084 {
15085 return (is64 ? (vm_map_offset_t)MACH_VM_MAX_ADDRESS : (vm_map_offset_t)VM_MAX_ADDRESS);
15086 }
15087
15088 uint64_t
15089 vm_map_get_max_aslr_slide_pages(vm_map_t map)
15090 {
15091 return (1 << (vm_map_is_64bit(map) ? 16 : 8));
15092 }
15093
15094 boolean_t
15095 vm_map_is_64bit(
15096 vm_map_t map)
15097 {
15098 return map->max_offset > ((vm_map_offset_t)VM_MAX_ADDRESS);
15099 }
15100
15101 boolean_t
15102 vm_map_has_hard_pagezero(
15103 vm_map_t map,
15104 vm_map_offset_t pagezero_size)
15105 {
15106 /*
15107 * XXX FBDP
15108 * We should lock the VM map (for read) here but we can get away
15109 * with it for now because there can't really be any race condition:
15110 * the VM map's min_offset is changed only when the VM map is created
15111 * and when the zero page is established (when the binary gets loaded),
15112 * and this routine gets called only when the task terminates and the
15113 * VM map is being torn down, and when a new map is created via
15114 * load_machfile()/execve().
15115 */
15116 return (map->min_offset >= pagezero_size);
15117 }
15118
15119 /*
15120 * Raise a VM map's maximun offset.
15121 */
15122 kern_return_t
15123 vm_map_raise_max_offset(
15124 vm_map_t map,
15125 vm_map_offset_t new_max_offset)
15126 {
15127 kern_return_t ret;
15128
15129 vm_map_lock(map);
15130 ret = KERN_INVALID_ADDRESS;
15131
15132 if (new_max_offset >= map->max_offset) {
15133 if (!vm_map_is_64bit(map)) {
15134 if (new_max_offset <= (vm_map_offset_t)VM_MAX_ADDRESS) {
15135 map->max_offset = new_max_offset;
15136 ret = KERN_SUCCESS;
15137 }
15138 } else {
15139 if (new_max_offset <= (vm_map_offset_t)MACH_VM_MAX_ADDRESS) {
15140 map->max_offset = new_max_offset;
15141 ret = KERN_SUCCESS;
15142 }
15143 }
15144 }
15145
15146 vm_map_unlock(map);
15147 return ret;
15148 }
15149
15150
15151 /*
15152 * Raise a VM map's minimum offset.
15153 * To strictly enforce "page zero" reservation.
15154 */
15155 kern_return_t
15156 vm_map_raise_min_offset(
15157 vm_map_t map,
15158 vm_map_offset_t new_min_offset)
15159 {
15160 vm_map_entry_t first_entry;
15161
15162 new_min_offset = vm_map_round_page(new_min_offset,
15163 VM_MAP_PAGE_MASK(map));
15164
15165 vm_map_lock(map);
15166
15167 if (new_min_offset < map->min_offset) {
15168 /*
15169 * Can't move min_offset backwards, as that would expose
15170 * a part of the address space that was previously, and for
15171 * possibly good reasons, inaccessible.
15172 */
15173 vm_map_unlock(map);
15174 return KERN_INVALID_ADDRESS;
15175 }
15176 if (new_min_offset >= map->max_offset) {
15177 /* can't go beyond the end of the address space */
15178 vm_map_unlock(map);
15179 return KERN_INVALID_ADDRESS;
15180 }
15181
15182 first_entry = vm_map_first_entry(map);
15183 if (first_entry != vm_map_to_entry(map) &&
15184 first_entry->vme_start < new_min_offset) {
15185 /*
15186 * Some memory was already allocated below the new
15187 * minimun offset. It's too late to change it now...
15188 */
15189 vm_map_unlock(map);
15190 return KERN_NO_SPACE;
15191 }
15192
15193 map->min_offset = new_min_offset;
15194
15195 assert(map->holes_list);
15196 map->holes_list->start = new_min_offset;
15197 assert(new_min_offset < map->holes_list->end);
15198
15199 vm_map_unlock(map);
15200
15201 return KERN_SUCCESS;
15202 }
15203
15204 /*
15205 * Set the limit on the maximum amount of user wired memory allowed for this map.
15206 * This is basically a copy of the MEMLOCK rlimit value maintained by the BSD side of
15207 * the kernel. The limits are checked in the mach VM side, so we keep a copy so we
15208 * don't have to reach over to the BSD data structures.
15209 */
15210
15211 void
15212 vm_map_set_user_wire_limit(vm_map_t map,
15213 vm_size_t limit)
15214 {
15215 map->user_wire_limit = limit;
15216 }
15217
15218
15219 void vm_map_switch_protect(vm_map_t map,
15220 boolean_t val)
15221 {
15222 vm_map_lock(map);
15223 map->switch_protect=val;
15224 vm_map_unlock(map);
15225 }
15226
15227 /*
15228 * IOKit has mapped a region into this map; adjust the pmap's ledgers appropriately.
15229 * phys_footprint is a composite limit consisting of iokit + physmem, so we need to
15230 * bump both counters.
15231 */
15232 void
15233 vm_map_iokit_mapped_region(vm_map_t map, vm_size_t bytes)
15234 {
15235 pmap_t pmap = vm_map_pmap(map);
15236
15237 ledger_credit(pmap->ledger, task_ledgers.iokit_mapped, bytes);
15238 ledger_credit(pmap->ledger, task_ledgers.phys_footprint, bytes);
15239 }
15240
15241 void
15242 vm_map_iokit_unmapped_region(vm_map_t map, vm_size_t bytes)
15243 {
15244 pmap_t pmap = vm_map_pmap(map);
15245
15246 ledger_debit(pmap->ledger, task_ledgers.iokit_mapped, bytes);
15247 ledger_debit(pmap->ledger, task_ledgers.phys_footprint, bytes);
15248 }
15249
15250 /* Add (generate) code signature for memory range */
15251 #if CONFIG_DYNAMIC_CODE_SIGNING
15252 kern_return_t vm_map_sign(vm_map_t map,
15253 vm_map_offset_t start,
15254 vm_map_offset_t end)
15255 {
15256 vm_map_entry_t entry;
15257 vm_page_t m;
15258 vm_object_t object;
15259
15260 /*
15261 * Vet all the input parameters and current type and state of the
15262 * underlaying object. Return with an error if anything is amiss.
15263 */
15264 if (map == VM_MAP_NULL)
15265 return(KERN_INVALID_ARGUMENT);
15266
15267 vm_map_lock_read(map);
15268
15269 if (!vm_map_lookup_entry(map, start, &entry) || entry->is_sub_map) {
15270 /*
15271 * Must pass a valid non-submap address.
15272 */
15273 vm_map_unlock_read(map);
15274 return(KERN_INVALID_ADDRESS);
15275 }
15276
15277 if((entry->vme_start > start) || (entry->vme_end < end)) {
15278 /*
15279 * Map entry doesn't cover the requested range. Not handling
15280 * this situation currently.
15281 */
15282 vm_map_unlock_read(map);
15283 return(KERN_INVALID_ARGUMENT);
15284 }
15285
15286 object = VME_OBJECT(entry);
15287 if (object == VM_OBJECT_NULL) {
15288 /*
15289 * Object must already be present or we can't sign.
15290 */
15291 vm_map_unlock_read(map);
15292 return KERN_INVALID_ARGUMENT;
15293 }
15294
15295 vm_object_lock(object);
15296 vm_map_unlock_read(map);
15297
15298 while(start < end) {
15299 uint32_t refmod;
15300
15301 m = vm_page_lookup(object,
15302 start - entry->vme_start + VME_OFFSET(entry));
15303 if (m==VM_PAGE_NULL) {
15304 /* shoud we try to fault a page here? we can probably
15305 * demand it exists and is locked for this request */
15306 vm_object_unlock(object);
15307 return KERN_FAILURE;
15308 }
15309 /* deal with special page status */
15310 if (m->busy ||
15311 (m->unusual && (m->error || m->restart || m->private || m->absent))) {
15312 vm_object_unlock(object);
15313 return KERN_FAILURE;
15314 }
15315
15316 /* Page is OK... now "validate" it */
15317 /* This is the place where we'll call out to create a code
15318 * directory, later */
15319 m->cs_validated = TRUE;
15320
15321 /* The page is now "clean" for codesigning purposes. That means
15322 * we don't consider it as modified (wpmapped) anymore. But
15323 * we'll disconnect the page so we note any future modification
15324 * attempts. */
15325 m->wpmapped = FALSE;
15326 refmod = pmap_disconnect(m->phys_page);
15327
15328 /* Pull the dirty status from the pmap, since we cleared the
15329 * wpmapped bit */
15330 if ((refmod & VM_MEM_MODIFIED) && !m->dirty) {
15331 SET_PAGE_DIRTY(m, FALSE);
15332 }
15333
15334 /* On to the next page */
15335 start += PAGE_SIZE;
15336 }
15337 vm_object_unlock(object);
15338
15339 return KERN_SUCCESS;
15340 }
15341 #endif
15342
15343 kern_return_t vm_map_partial_reap(vm_map_t map, unsigned int *reclaimed_resident, unsigned int *reclaimed_compressed)
15344 {
15345 vm_map_entry_t entry = VM_MAP_ENTRY_NULL;
15346 vm_map_entry_t next_entry;
15347 kern_return_t kr = KERN_SUCCESS;
15348 vm_map_t zap_map;
15349
15350 vm_map_lock(map);
15351
15352 /*
15353 * We use a "zap_map" to avoid having to unlock
15354 * the "map" in vm_map_delete().
15355 */
15356 zap_map = vm_map_create(PMAP_NULL,
15357 map->min_offset,
15358 map->max_offset,
15359 map->hdr.entries_pageable);
15360
15361 if (zap_map == VM_MAP_NULL) {
15362 return KERN_RESOURCE_SHORTAGE;
15363 }
15364
15365 vm_map_set_page_shift(zap_map,
15366 VM_MAP_PAGE_SHIFT(map));
15367 vm_map_disable_hole_optimization(zap_map);
15368
15369 for (entry = vm_map_first_entry(map);
15370 entry != vm_map_to_entry(map);
15371 entry = next_entry) {
15372 next_entry = entry->vme_next;
15373
15374 if (VME_OBJECT(entry) &&
15375 !entry->is_sub_map &&
15376 (VME_OBJECT(entry)->internal == TRUE) &&
15377 (VME_OBJECT(entry)->ref_count == 1)) {
15378
15379 *reclaimed_resident += VME_OBJECT(entry)->resident_page_count;
15380 *reclaimed_compressed += vm_compressor_pager_get_count(VME_OBJECT(entry)->pager);
15381
15382 (void)vm_map_delete(map,
15383 entry->vme_start,
15384 entry->vme_end,
15385 VM_MAP_REMOVE_SAVE_ENTRIES,
15386 zap_map);
15387 }
15388 }
15389
15390 vm_map_unlock(map);
15391
15392 /*
15393 * Get rid of the "zap_maps" and all the map entries that
15394 * they may still contain.
15395 */
15396 if (zap_map != VM_MAP_NULL) {
15397 vm_map_destroy(zap_map, VM_MAP_REMOVE_NO_PMAP_CLEANUP);
15398 zap_map = VM_MAP_NULL;
15399 }
15400
15401 return kr;
15402 }
15403
15404 #if CONFIG_FREEZE
15405
15406 kern_return_t vm_map_freeze_walk(
15407 vm_map_t map,
15408 unsigned int *purgeable_count,
15409 unsigned int *wired_count,
15410 unsigned int *clean_count,
15411 unsigned int *dirty_count,
15412 unsigned int dirty_budget,
15413 boolean_t *has_shared)
15414 {
15415 vm_map_entry_t entry;
15416
15417 vm_map_lock_read(map);
15418
15419 *purgeable_count = *wired_count = *clean_count = *dirty_count = 0;
15420 *has_shared = FALSE;
15421
15422 for (entry = vm_map_first_entry(map);
15423 entry != vm_map_to_entry(map);
15424 entry = entry->vme_next) {
15425 unsigned int purgeable, clean, dirty, wired;
15426 boolean_t shared;
15427
15428 if ((VME_OBJECT(entry) == 0) ||
15429 (entry->is_sub_map) ||
15430 (VME_OBJECT(entry)->phys_contiguous)) {
15431 continue;
15432 }
15433
15434 default_freezer_pack(&purgeable, &wired, &clean, &dirty, dirty_budget, &shared, VME_OBJECT(entry), NULL);
15435
15436 *purgeable_count += purgeable;
15437 *wired_count += wired;
15438 *clean_count += clean;
15439 *dirty_count += dirty;
15440
15441 if (shared) {
15442 *has_shared = TRUE;
15443 }
15444
15445 /* Adjust pageout budget and finish up if reached */
15446 if (dirty_budget) {
15447 dirty_budget -= dirty;
15448 if (dirty_budget == 0) {
15449 break;
15450 }
15451 }
15452 }
15453
15454 vm_map_unlock_read(map);
15455
15456 return KERN_SUCCESS;
15457 }
15458
15459 int c_freezer_swapout_count;
15460 int c_freezer_compression_count = 0;
15461 AbsoluteTime c_freezer_last_yield_ts = 0;
15462
15463 kern_return_t vm_map_freeze(
15464 vm_map_t map,
15465 unsigned int *purgeable_count,
15466 unsigned int *wired_count,
15467 unsigned int *clean_count,
15468 unsigned int *dirty_count,
15469 unsigned int dirty_budget,
15470 boolean_t *has_shared)
15471 {
15472 vm_map_entry_t entry2 = VM_MAP_ENTRY_NULL;
15473 kern_return_t kr = KERN_SUCCESS;
15474 boolean_t default_freezer_active = TRUE;
15475
15476 *purgeable_count = *wired_count = *clean_count = *dirty_count = 0;
15477 *has_shared = FALSE;
15478
15479 /*
15480 * We need the exclusive lock here so that we can
15481 * block any page faults or lookups while we are
15482 * in the middle of freezing this vm map.
15483 */
15484 vm_map_lock(map);
15485
15486 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
15487 default_freezer_active = FALSE;
15488
15489 if (vm_compressor_low_on_space() || vm_swap_low_on_space()) {
15490 kr = KERN_NO_SPACE;
15491 goto done;
15492 }
15493 }
15494 assert(default_freezer_active == FALSE);
15495
15496 if (default_freezer_active) {
15497 if (map->default_freezer_handle == NULL) {
15498 map->default_freezer_handle = default_freezer_handle_allocate();
15499 }
15500
15501 if ((kr = default_freezer_handle_init(map->default_freezer_handle)) != KERN_SUCCESS) {
15502 /*
15503 * Can happen if default_freezer_handle passed in is NULL
15504 * Or, a table has already been allocated and associated
15505 * with this handle, i.e. the map is already frozen.
15506 */
15507 goto done;
15508 }
15509 }
15510 c_freezer_compression_count = 0;
15511 clock_get_uptime(&c_freezer_last_yield_ts);
15512
15513 for (entry2 = vm_map_first_entry(map);
15514 entry2 != vm_map_to_entry(map);
15515 entry2 = entry2->vme_next) {
15516
15517 vm_object_t src_object = VME_OBJECT(entry2);
15518
15519 if (VME_OBJECT(entry2) &&
15520 !entry2->is_sub_map &&
15521 !VME_OBJECT(entry2)->phys_contiguous) {
15522 /* If eligible, scan the entry, moving eligible pages over to our parent object */
15523 if (default_freezer_active) {
15524 unsigned int purgeable, clean, dirty, wired;
15525 boolean_t shared;
15526
15527 default_freezer_pack(&purgeable, &wired, &clean, &dirty, dirty_budget, &shared,
15528 src_object, map->default_freezer_handle);
15529
15530 *purgeable_count += purgeable;
15531 *wired_count += wired;
15532 *clean_count += clean;
15533 *dirty_count += dirty;
15534
15535 /* Adjust pageout budget and finish up if reached */
15536 if (dirty_budget) {
15537 dirty_budget -= dirty;
15538 if (dirty_budget == 0) {
15539 break;
15540 }
15541 }
15542
15543 if (shared) {
15544 *has_shared = TRUE;
15545 }
15546 } else {
15547 if (VME_OBJECT(entry2)->internal == TRUE) {
15548
15549 if (DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED) {
15550 /*
15551 * Pages belonging to this object could be swapped to disk.
15552 * Make sure it's not a shared object because we could end
15553 * up just bringing it back in again.
15554 */
15555 if (VME_OBJECT(entry2)->ref_count > 1) {
15556 continue;
15557 }
15558 }
15559 vm_object_compressed_freezer_pageout(VME_OBJECT(entry2));
15560 }
15561
15562 if (vm_compressor_low_on_space() || vm_swap_low_on_space()) {
15563 kr = KERN_NO_SPACE;
15564 break;
15565 }
15566 }
15567 }
15568 }
15569
15570 if (default_freezer_active) {
15571 /* Finally, throw out the pages to swap */
15572 default_freezer_pageout(map->default_freezer_handle);
15573 }
15574
15575 done:
15576 vm_map_unlock(map);
15577
15578 if (!default_freezer_active) {
15579 vm_object_compressed_freezer_done();
15580 }
15581 if (DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED) {
15582 /*
15583 * reset the counter tracking the # of swapped c_segs
15584 * because we are now done with this freeze session and task.
15585 */
15586 c_freezer_swapout_count = 0;
15587 }
15588 return kr;
15589 }
15590
15591 kern_return_t
15592 vm_map_thaw(
15593 vm_map_t map)
15594 {
15595 kern_return_t kr = KERN_SUCCESS;
15596
15597 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
15598 /*
15599 * We will on-demand thaw in the presence of the compressed pager.
15600 */
15601 return kr;
15602 }
15603
15604 vm_map_lock(map);
15605
15606 if (map->default_freezer_handle == NULL) {
15607 /*
15608 * This map is not in a frozen state.
15609 */
15610 kr = KERN_FAILURE;
15611 goto out;
15612 }
15613
15614 kr = default_freezer_unpack(map->default_freezer_handle);
15615 out:
15616 vm_map_unlock(map);
15617
15618 return kr;
15619 }
15620 #endif
15621
15622 /*
15623 * vm_map_entry_should_cow_for_true_share:
15624 *
15625 * Determines if the map entry should be clipped and setup for copy-on-write
15626 * to avoid applying "true_share" to a large VM object when only a subset is
15627 * targeted.
15628 *
15629 * For now, we target only the map entries created for the Objective C
15630 * Garbage Collector, which initially have the following properties:
15631 * - alias == VM_MEMORY_MALLOC
15632 * - wired_count == 0
15633 * - !needs_copy
15634 * and a VM object with:
15635 * - internal
15636 * - copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC
15637 * - !true_share
15638 * - vo_size == ANON_CHUNK_SIZE
15639 *
15640 * Only non-kernel map entries.
15641 */
15642 boolean_t
15643 vm_map_entry_should_cow_for_true_share(
15644 vm_map_entry_t entry)
15645 {
15646 vm_object_t object;
15647
15648 if (entry->is_sub_map) {
15649 /* entry does not point at a VM object */
15650 return FALSE;
15651 }
15652
15653 if (entry->needs_copy) {
15654 /* already set for copy_on_write: done! */
15655 return FALSE;
15656 }
15657
15658 if (VME_ALIAS(entry) != VM_MEMORY_MALLOC &&
15659 VME_ALIAS(entry) != VM_MEMORY_MALLOC_SMALL) {
15660 /* not a malloc heap or Obj-C Garbage Collector heap */
15661 return FALSE;
15662 }
15663
15664 if (entry->wired_count) {
15665 /* wired: can't change the map entry... */
15666 vm_counters.should_cow_but_wired++;
15667 return FALSE;
15668 }
15669
15670 object = VME_OBJECT(entry);
15671
15672 if (object == VM_OBJECT_NULL) {
15673 /* no object yet... */
15674 return FALSE;
15675 }
15676
15677 if (!object->internal) {
15678 /* not an internal object */
15679 return FALSE;
15680 }
15681
15682 if (object->copy_strategy != MEMORY_OBJECT_COPY_SYMMETRIC) {
15683 /* not the default copy strategy */
15684 return FALSE;
15685 }
15686
15687 if (object->true_share) {
15688 /* already true_share: too late to avoid it */
15689 return FALSE;
15690 }
15691
15692 if (VME_ALIAS(entry) == VM_MEMORY_MALLOC &&
15693 object->vo_size != ANON_CHUNK_SIZE) {
15694 /* ... not an object created for the ObjC Garbage Collector */
15695 return FALSE;
15696 }
15697
15698 if (VME_ALIAS(entry) == VM_MEMORY_MALLOC_SMALL &&
15699 object->vo_size != 2048 * 4096) {
15700 /* ... not a "MALLOC_SMALL" heap */
15701 return FALSE;
15702 }
15703
15704 /*
15705 * All the criteria match: we have a large object being targeted for "true_share".
15706 * To limit the adverse side-effects linked with "true_share", tell the caller to
15707 * try and avoid setting up the entire object for "true_share" by clipping the
15708 * targeted range and setting it up for copy-on-write.
15709 */
15710 return TRUE;
15711 }
15712
15713 vm_map_offset_t
15714 vm_map_round_page_mask(
15715 vm_map_offset_t offset,
15716 vm_map_offset_t mask)
15717 {
15718 return VM_MAP_ROUND_PAGE(offset, mask);
15719 }
15720
15721 vm_map_offset_t
15722 vm_map_trunc_page_mask(
15723 vm_map_offset_t offset,
15724 vm_map_offset_t mask)
15725 {
15726 return VM_MAP_TRUNC_PAGE(offset, mask);
15727 }
15728
15729 boolean_t
15730 vm_map_page_aligned(
15731 vm_map_offset_t offset,
15732 vm_map_offset_t mask)
15733 {
15734 return ((offset) & mask) == 0;
15735 }
15736
15737 int
15738 vm_map_page_shift(
15739 vm_map_t map)
15740 {
15741 return VM_MAP_PAGE_SHIFT(map);
15742 }
15743
15744 int
15745 vm_map_page_size(
15746 vm_map_t map)
15747 {
15748 return VM_MAP_PAGE_SIZE(map);
15749 }
15750
15751 vm_map_offset_t
15752 vm_map_page_mask(
15753 vm_map_t map)
15754 {
15755 return VM_MAP_PAGE_MASK(map);
15756 }
15757
15758 kern_return_t
15759 vm_map_set_page_shift(
15760 vm_map_t map,
15761 int pageshift)
15762 {
15763 if (map->hdr.nentries != 0) {
15764 /* too late to change page size */
15765 return KERN_FAILURE;
15766 }
15767
15768 map->hdr.page_shift = pageshift;
15769
15770 return KERN_SUCCESS;
15771 }
15772
15773 int
15774 vm_map_purge(
15775 vm_map_t map)
15776 {
15777 int num_object_purged;
15778 vm_map_entry_t entry;
15779 vm_map_offset_t next_address;
15780 vm_object_t object;
15781 int state;
15782 kern_return_t kr;
15783
15784 num_object_purged = 0;
15785
15786 vm_map_lock_read(map);
15787 entry = vm_map_first_entry(map);
15788 while (entry != vm_map_to_entry(map)) {
15789 if (entry->is_sub_map) {
15790 goto next;
15791 }
15792 if (! (entry->protection & VM_PROT_WRITE)) {
15793 goto next;
15794 }
15795 object = VME_OBJECT(entry);
15796 if (object == VM_OBJECT_NULL) {
15797 goto next;
15798 }
15799 if (object->purgable != VM_PURGABLE_VOLATILE) {
15800 goto next;
15801 }
15802
15803 vm_object_lock(object);
15804 #if 00
15805 if (VME_OFFSET(entry) != 0 ||
15806 (entry->vme_end - entry->vme_start) != object->vo_size) {
15807 vm_object_unlock(object);
15808 goto next;
15809 }
15810 #endif
15811 next_address = entry->vme_end;
15812 vm_map_unlock_read(map);
15813 state = VM_PURGABLE_EMPTY;
15814 kr = vm_object_purgable_control(object,
15815 VM_PURGABLE_SET_STATE,
15816 &state);
15817 if (kr == KERN_SUCCESS) {
15818 num_object_purged++;
15819 }
15820 vm_object_unlock(object);
15821
15822 vm_map_lock_read(map);
15823 if (vm_map_lookup_entry(map, next_address, &entry)) {
15824 continue;
15825 }
15826 next:
15827 entry = entry->vme_next;
15828 }
15829 vm_map_unlock_read(map);
15830
15831 return num_object_purged;
15832 }
15833
15834 kern_return_t
15835 vm_map_query_volatile(
15836 vm_map_t map,
15837 mach_vm_size_t *volatile_virtual_size_p,
15838 mach_vm_size_t *volatile_resident_size_p,
15839 mach_vm_size_t *volatile_compressed_size_p,
15840 mach_vm_size_t *volatile_pmap_size_p,
15841 mach_vm_size_t *volatile_compressed_pmap_size_p)
15842 {
15843 mach_vm_size_t volatile_virtual_size;
15844 mach_vm_size_t volatile_resident_count;
15845 mach_vm_size_t volatile_compressed_count;
15846 mach_vm_size_t volatile_pmap_count;
15847 mach_vm_size_t volatile_compressed_pmap_count;
15848 mach_vm_size_t resident_count;
15849 unsigned int compressed_count;
15850 vm_map_entry_t entry;
15851 vm_object_t object;
15852
15853 /* map should be locked by caller */
15854
15855 volatile_virtual_size = 0;
15856 volatile_resident_count = 0;
15857 volatile_compressed_count = 0;
15858 volatile_pmap_count = 0;
15859 volatile_compressed_pmap_count = 0;
15860
15861 for (entry = vm_map_first_entry(map);
15862 entry != vm_map_to_entry(map);
15863 entry = entry->vme_next) {
15864 if (entry->is_sub_map) {
15865 continue;
15866 }
15867 if (! (entry->protection & VM_PROT_WRITE)) {
15868 continue;
15869 }
15870 object = VME_OBJECT(entry);
15871 if (object == VM_OBJECT_NULL) {
15872 continue;
15873 }
15874 if (object->purgable != VM_PURGABLE_VOLATILE &&
15875 object->purgable != VM_PURGABLE_EMPTY) {
15876 continue;
15877 }
15878 if (VME_OFFSET(entry)) {
15879 /*
15880 * If the map entry has been split and the object now
15881 * appears several times in the VM map, we don't want
15882 * to count the object's resident_page_count more than
15883 * once. We count it only for the first one, starting
15884 * at offset 0 and ignore the other VM map entries.
15885 */
15886 continue;
15887 }
15888 resident_count = object->resident_page_count;
15889 if ((VME_OFFSET(entry) / PAGE_SIZE) >= resident_count) {
15890 resident_count = 0;
15891 } else {
15892 resident_count -= (VME_OFFSET(entry) / PAGE_SIZE);
15893 }
15894
15895 volatile_virtual_size += entry->vme_end - entry->vme_start;
15896 volatile_resident_count += resident_count;
15897 if (object->pager) {
15898 volatile_compressed_count +=
15899 vm_compressor_pager_get_count(object->pager);
15900 }
15901 compressed_count = 0;
15902 volatile_pmap_count += pmap_query_resident(map->pmap,
15903 entry->vme_start,
15904 entry->vme_end,
15905 &compressed_count);
15906 volatile_compressed_pmap_count += compressed_count;
15907 }
15908
15909 /* map is still locked on return */
15910
15911 *volatile_virtual_size_p = volatile_virtual_size;
15912 *volatile_resident_size_p = volatile_resident_count * PAGE_SIZE;
15913 *volatile_compressed_size_p = volatile_compressed_count * PAGE_SIZE;
15914 *volatile_pmap_size_p = volatile_pmap_count * PAGE_SIZE;
15915 *volatile_compressed_pmap_size_p = volatile_compressed_pmap_count * PAGE_SIZE;
15916
15917 return KERN_SUCCESS;
15918 }
15919
15920 void
15921 vm_map_sizes(vm_map_t map,
15922 vm_map_size_t * psize,
15923 vm_map_size_t * pfree,
15924 vm_map_size_t * plargest_free)
15925 {
15926 vm_map_entry_t entry;
15927 vm_map_offset_t prev;
15928 vm_map_size_t free, total_free, largest_free;
15929 boolean_t end;
15930
15931 total_free = largest_free = 0;
15932
15933 vm_map_lock_read(map);
15934 if (psize) *psize = map->max_offset - map->min_offset;
15935
15936 prev = map->min_offset;
15937 for (entry = vm_map_first_entry(map);; entry = entry->vme_next)
15938 {
15939 end = (entry == vm_map_to_entry(map));
15940
15941 if (end) free = entry->vme_end - prev;
15942 else free = entry->vme_start - prev;
15943
15944 total_free += free;
15945 if (free > largest_free) largest_free = free;
15946
15947 if (end) break;
15948 prev = entry->vme_end;
15949 }
15950 vm_map_unlock_read(map);
15951 if (pfree) *pfree = total_free;
15952 if (plargest_free) *plargest_free = largest_free;
15953 }
15954
15955 #if VM_SCAN_FOR_SHADOW_CHAIN
15956 int vm_map_shadow_max(vm_map_t map);
15957 int vm_map_shadow_max(
15958 vm_map_t map)
15959 {
15960 int shadows, shadows_max;
15961 vm_map_entry_t entry;
15962 vm_object_t object, next_object;
15963
15964 if (map == NULL)
15965 return 0;
15966
15967 shadows_max = 0;
15968
15969 vm_map_lock_read(map);
15970
15971 for (entry = vm_map_first_entry(map);
15972 entry != vm_map_to_entry(map);
15973 entry = entry->vme_next) {
15974 if (entry->is_sub_map) {
15975 continue;
15976 }
15977 object = VME_OBJECT(entry);
15978 if (object == NULL) {
15979 continue;
15980 }
15981 vm_object_lock_shared(object);
15982 for (shadows = 0;
15983 object->shadow != NULL;
15984 shadows++, object = next_object) {
15985 next_object = object->shadow;
15986 vm_object_lock_shared(next_object);
15987 vm_object_unlock(object);
15988 }
15989 vm_object_unlock(object);
15990 if (shadows > shadows_max) {
15991 shadows_max = shadows;
15992 }
15993 }
15994
15995 vm_map_unlock_read(map);
15996
15997 return shadows_max;
15998 }
15999 #endif /* VM_SCAN_FOR_SHADOW_CHAIN */