]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/vm/vm_map.c
xnu-2782.40.9.tar.gz
[apple/xnu.git] / osfmk / vm / vm_map.c
... / ...
CommitLineData
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
112extern u_int32_t random(void); /* from <libkern/libkern.h> */
113/* Internal prototypes
114 */
115
116static void vm_map_simplify_range(
117 vm_map_t map,
118 vm_map_offset_t start,
119 vm_map_offset_t end); /* forward */
120
121static boolean_t vm_map_range_check(
122 vm_map_t map,
123 vm_map_offset_t start,
124 vm_map_offset_t end,
125 vm_map_entry_t *entry);
126
127static vm_map_entry_t _vm_map_entry_create(
128 struct vm_map_header *map_header, boolean_t map_locked);
129
130static void _vm_map_entry_dispose(
131 struct vm_map_header *map_header,
132 vm_map_entry_t entry);
133
134static void vm_map_pmap_enter(
135 vm_map_t map,
136 vm_map_offset_t addr,
137 vm_map_offset_t end_addr,
138 vm_object_t object,
139 vm_object_offset_t offset,
140 vm_prot_t protection);
141
142static void _vm_map_clip_end(
143 struct vm_map_header *map_header,
144 vm_map_entry_t entry,
145 vm_map_offset_t end);
146
147static void _vm_map_clip_start(
148 struct vm_map_header *map_header,
149 vm_map_entry_t entry,
150 vm_map_offset_t start);
151
152static void vm_map_entry_delete(
153 vm_map_t map,
154 vm_map_entry_t entry);
155
156static kern_return_t vm_map_delete(
157 vm_map_t map,
158 vm_map_offset_t start,
159 vm_map_offset_t end,
160 int flags,
161 vm_map_t zap_map);
162
163static kern_return_t vm_map_copy_overwrite_unaligned(
164 vm_map_t dst_map,
165 vm_map_entry_t entry,
166 vm_map_copy_t copy,
167 vm_map_address_t start,
168 boolean_t discard_on_success);
169
170static kern_return_t vm_map_copy_overwrite_aligned(
171 vm_map_t dst_map,
172 vm_map_entry_t tmp_entry,
173 vm_map_copy_t copy,
174 vm_map_offset_t start,
175 pmap_t pmap);
176
177static kern_return_t vm_map_copyin_kernel_buffer(
178 vm_map_t src_map,
179 vm_map_address_t src_addr,
180 vm_map_size_t len,
181 boolean_t src_destroy,
182 vm_map_copy_t *copy_result); /* OUT */
183
184static kern_return_t vm_map_copyout_kernel_buffer(
185 vm_map_t map,
186 vm_map_address_t *addr, /* IN/OUT */
187 vm_map_copy_t copy,
188 boolean_t overwrite,
189 boolean_t consume_on_success);
190
191static void vm_map_fork_share(
192 vm_map_t old_map,
193 vm_map_entry_t old_entry,
194 vm_map_t new_map);
195
196static boolean_t vm_map_fork_copy(
197 vm_map_t old_map,
198 vm_map_entry_t *old_entry_p,
199 vm_map_t new_map);
200
201void vm_map_region_top_walk(
202 vm_map_entry_t entry,
203 vm_region_top_info_t top);
204
205void vm_map_region_walk(
206 vm_map_t map,
207 vm_map_offset_t va,
208 vm_map_entry_t entry,
209 vm_object_offset_t offset,
210 vm_object_size_t range,
211 vm_region_extended_info_t extended,
212 boolean_t look_for_pages,
213 mach_msg_type_number_t count);
214
215static kern_return_t vm_map_wire_nested(
216 vm_map_t map,
217 vm_map_offset_t start,
218 vm_map_offset_t end,
219 vm_prot_t access_type,
220 boolean_t user_wire,
221 pmap_t map_pmap,
222 vm_map_offset_t pmap_addr,
223 ppnum_t *physpage_p);
224
225static kern_return_t vm_map_unwire_nested(
226 vm_map_t map,
227 vm_map_offset_t start,
228 vm_map_offset_t end,
229 boolean_t user_wire,
230 pmap_t map_pmap,
231 vm_map_offset_t pmap_addr);
232
233static kern_return_t vm_map_overwrite_submap_recurse(
234 vm_map_t dst_map,
235 vm_map_offset_t dst_addr,
236 vm_map_size_t dst_size);
237
238static kern_return_t vm_map_copy_overwrite_nested(
239 vm_map_t dst_map,
240 vm_map_offset_t dst_addr,
241 vm_map_copy_t copy,
242 boolean_t interruptible,
243 pmap_t pmap,
244 boolean_t discard_on_success);
245
246static kern_return_t vm_map_remap_extract(
247 vm_map_t map,
248 vm_map_offset_t addr,
249 vm_map_size_t size,
250 boolean_t copy,
251 struct vm_map_header *map_header,
252 vm_prot_t *cur_protection,
253 vm_prot_t *max_protection,
254 vm_inherit_t inheritance,
255 boolean_t pageable);
256
257static kern_return_t vm_map_remap_range_allocate(
258 vm_map_t map,
259 vm_map_address_t *address,
260 vm_map_size_t size,
261 vm_map_offset_t mask,
262 int flags,
263 vm_map_entry_t *map_entry);
264
265static void vm_map_region_look_for_page(
266 vm_map_t map,
267 vm_map_offset_t va,
268 vm_object_t object,
269 vm_object_offset_t offset,
270 int max_refcnt,
271 int depth,
272 vm_region_extended_info_t extended,
273 mach_msg_type_number_t count);
274
275static int vm_map_region_count_obj_refs(
276 vm_map_entry_t entry,
277 vm_object_t object);
278
279
280static kern_return_t vm_map_willneed(
281 vm_map_t map,
282 vm_map_offset_t start,
283 vm_map_offset_t end);
284
285static kern_return_t vm_map_reuse_pages(
286 vm_map_t map,
287 vm_map_offset_t start,
288 vm_map_offset_t end);
289
290static kern_return_t vm_map_reusable_pages(
291 vm_map_t map,
292 vm_map_offset_t start,
293 vm_map_offset_t end);
294
295static kern_return_t vm_map_can_reuse(
296 vm_map_t map,
297 vm_map_offset_t start,
298 vm_map_offset_t end);
299
300
301/*
302 * Macros to copy a vm_map_entry. We must be careful to correctly
303 * manage the wired page count. vm_map_entry_copy() creates a new
304 * map entry to the same memory - the wired count in the new entry
305 * must be set to zero. vm_map_entry_copy_full() creates a new
306 * entry that is identical to the old entry. This preserves the
307 * wire count; it's used for map splitting and zone changing in
308 * vm_map_copyout.
309 */
310
311#define vm_map_entry_copy(NEW,OLD) \
312MACRO_BEGIN \
313boolean_t _vmec_reserved = (NEW)->from_reserved_zone; \
314 *(NEW) = *(OLD); \
315 (NEW)->is_shared = FALSE; \
316 (NEW)->needs_wakeup = FALSE; \
317 (NEW)->in_transition = FALSE; \
318 (NEW)->wired_count = 0; \
319 (NEW)->user_wired_count = 0; \
320 (NEW)->permanent = FALSE; \
321 (NEW)->used_for_jit = FALSE; \
322 (NEW)->from_reserved_zone = _vmec_reserved; \
323 (NEW)->iokit_acct = FALSE; \
324MACRO_END
325
326#define vm_map_entry_copy_full(NEW,OLD) \
327MACRO_BEGIN \
328boolean_t _vmecf_reserved = (NEW)->from_reserved_zone; \
329(*(NEW) = *(OLD)); \
330(NEW)->from_reserved_zone = _vmecf_reserved; \
331MACRO_END
332
333/*
334 * Decide if we want to allow processes to execute from their data or stack areas.
335 * override_nx() returns true if we do. Data/stack execution can be enabled independently
336 * for 32 and 64 bit processes. Set the VM_ABI_32 or VM_ABI_64 flags in allow_data_exec
337 * or allow_stack_exec to enable data execution for that type of data area for that particular
338 * ABI (or both by or'ing the flags together). These are initialized in the architecture
339 * specific pmap files since the default behavior varies according to architecture. The
340 * main reason it varies is because of the need to provide binary compatibility with old
341 * applications that were written before these restrictions came into being. In the old
342 * days, an app could execute anything it could read, but this has slowly been tightened
343 * up over time. The default behavior is:
344 *
345 * 32-bit PPC apps may execute from both stack and data areas
346 * 32-bit Intel apps may exeucte from data areas but not stack
347 * 64-bit PPC/Intel apps may not execute from either data or stack
348 *
349 * An application on any architecture may override these defaults by explicitly
350 * adding PROT_EXEC permission to the page in question with the mprotect(2)
351 * system call. This code here just determines what happens when an app tries to
352 * execute from a page that lacks execute permission.
353 *
354 * Note that allow_data_exec or allow_stack_exec may also be modified by sysctl to change the
355 * default behavior for both 32 and 64 bit apps on a system-wide basis. Furthermore,
356 * a Mach-O header flag bit (MH_NO_HEAP_EXECUTION) can be used to forcibly disallow
357 * execution from data areas for a particular binary even if the arch normally permits it. As
358 * a final wrinkle, a posix_spawn attribute flag can be used to negate this opt-in header bit
359 * to support some complicated use cases, notably browsers with out-of-process plugins that
360 * are not all NX-safe.
361 */
362
363extern int allow_data_exec, allow_stack_exec;
364
365int
366override_nx(vm_map_t map, uint32_t user_tag) /* map unused on arm */
367{
368 int current_abi;
369
370 /*
371 * Determine if the app is running in 32 or 64 bit mode.
372 */
373
374 if (vm_map_is_64bit(map))
375 current_abi = VM_ABI_64;
376 else
377 current_abi = VM_ABI_32;
378
379 /*
380 * Determine if we should allow the execution based on whether it's a
381 * stack or data area and the current architecture.
382 */
383
384 if (user_tag == VM_MEMORY_STACK)
385 return allow_stack_exec & current_abi;
386
387 return (allow_data_exec & current_abi) && (map->map_disallow_data_exec == FALSE);
388}
389
390
391/*
392 * Virtual memory maps provide for the mapping, protection,
393 * and sharing of virtual memory objects. In addition,
394 * this module provides for an efficient virtual copy of
395 * memory from one map to another.
396 *
397 * Synchronization is required prior to most operations.
398 *
399 * Maps consist of an ordered doubly-linked list of simple
400 * entries; a single hint is used to speed up lookups.
401 *
402 * Sharing maps have been deleted from this version of Mach.
403 * All shared objects are now mapped directly into the respective
404 * maps. This requires a change in the copy on write strategy;
405 * the asymmetric (delayed) strategy is used for shared temporary
406 * objects instead of the symmetric (shadow) strategy. All maps
407 * are now "top level" maps (either task map, kernel map or submap
408 * of the kernel map).
409 *
410 * Since portions of maps are specified by start/end addreses,
411 * which may not align with existing map entries, all
412 * routines merely "clip" entries to these start/end values.
413 * [That is, an entry is split into two, bordering at a
414 * start or end value.] Note that these clippings may not
415 * always be necessary (as the two resulting entries are then
416 * not changed); however, the clipping is done for convenience.
417 * No attempt is currently made to "glue back together" two
418 * abutting entries.
419 *
420 * The symmetric (shadow) copy strategy implements virtual copy
421 * by copying VM object references from one map to
422 * another, and then marking both regions as copy-on-write.
423 * It is important to note that only one writeable reference
424 * to a VM object region exists in any map when this strategy
425 * is used -- this means that shadow object creation can be
426 * delayed until a write operation occurs. The symmetric (delayed)
427 * strategy allows multiple maps to have writeable references to
428 * the same region of a vm object, and hence cannot delay creating
429 * its copy objects. See vm_object_copy_quickly() in vm_object.c.
430 * Copying of permanent objects is completely different; see
431 * vm_object_copy_strategically() in vm_object.c.
432 */
433
434static zone_t vm_map_zone; /* zone for vm_map structures */
435static zone_t vm_map_entry_zone; /* zone for vm_map_entry structures */
436static zone_t vm_map_entry_reserved_zone; /* zone with reserve for non-blocking
437 * allocations */
438static zone_t vm_map_copy_zone; /* zone for vm_map_copy structures */
439
440
441/*
442 * Placeholder object for submap operations. This object is dropped
443 * into the range by a call to vm_map_find, and removed when
444 * vm_map_submap creates the submap.
445 */
446
447vm_object_t vm_submap_object;
448
449static void *map_data;
450static vm_size_t map_data_size;
451static void *kentry_data;
452static vm_size_t kentry_data_size;
453
454#define NO_COALESCE_LIMIT ((1024 * 128) - 1)
455
456/* Skip acquiring locks if we're in the midst of a kernel core dump */
457unsigned int not_in_kdp = 1;
458
459unsigned int vm_map_set_cache_attr_count = 0;
460
461kern_return_t
462vm_map_set_cache_attr(
463 vm_map_t map,
464 vm_map_offset_t va)
465{
466 vm_map_entry_t map_entry;
467 vm_object_t object;
468 kern_return_t kr = KERN_SUCCESS;
469
470 vm_map_lock_read(map);
471
472 if (!vm_map_lookup_entry(map, va, &map_entry) ||
473 map_entry->is_sub_map) {
474 /*
475 * that memory is not properly mapped
476 */
477 kr = KERN_INVALID_ARGUMENT;
478 goto done;
479 }
480 object = map_entry->object.vm_object;
481
482 if (object == VM_OBJECT_NULL) {
483 /*
484 * there should be a VM object here at this point
485 */
486 kr = KERN_INVALID_ARGUMENT;
487 goto done;
488 }
489 vm_object_lock(object);
490 object->set_cache_attr = TRUE;
491 vm_object_unlock(object);
492
493 vm_map_set_cache_attr_count++;
494done:
495 vm_map_unlock_read(map);
496
497 return kr;
498}
499
500
501#if CONFIG_CODE_DECRYPTION
502/*
503 * vm_map_apple_protected:
504 * This remaps the requested part of the object with an object backed by
505 * the decrypting pager.
506 * crypt_info contains entry points and session data for the crypt module.
507 * The crypt_info block will be copied by vm_map_apple_protected. The data structures
508 * referenced in crypt_info must remain valid until crypt_info->crypt_end() is called.
509 */
510kern_return_t
511vm_map_apple_protected(
512 vm_map_t map,
513 vm_map_offset_t start,
514 vm_map_offset_t end,
515 struct pager_crypt_info *crypt_info)
516{
517 boolean_t map_locked;
518 kern_return_t kr;
519 vm_map_entry_t map_entry;
520 memory_object_t protected_mem_obj;
521 vm_object_t protected_object;
522 vm_map_offset_t map_addr;
523
524 vm_map_lock_read(map);
525 map_locked = TRUE;
526
527 /* lookup the protected VM object */
528 if (!vm_map_lookup_entry(map,
529 start,
530 &map_entry) ||
531 map_entry->vme_end < end ||
532 map_entry->is_sub_map ||
533 !(map_entry->protection & VM_PROT_EXECUTE)) {
534 /* that memory is not properly mapped */
535 kr = KERN_INVALID_ARGUMENT;
536 goto done;
537 }
538 protected_object = map_entry->object.vm_object;
539 if (protected_object == VM_OBJECT_NULL) {
540 /* there should be a VM object here at this point */
541 kr = KERN_INVALID_ARGUMENT;
542 goto done;
543 }
544
545 /* make sure protected object stays alive while map is unlocked */
546 vm_object_reference(protected_object);
547
548 vm_map_unlock_read(map);
549 map_locked = FALSE;
550
551 /*
552 * Lookup (and create if necessary) the protected memory object
553 * matching that VM object.
554 * If successful, this also grabs a reference on the memory object,
555 * to guarantee that it doesn't go away before we get a chance to map
556 * it.
557 */
558 protected_mem_obj = apple_protect_pager_setup(protected_object, crypt_info);
559
560 /* release extra ref on protected object */
561 vm_object_deallocate(protected_object);
562
563 if (protected_mem_obj == NULL) {
564 kr = KERN_FAILURE;
565 goto done;
566 }
567
568 /* map this memory object in place of the current one */
569 map_addr = start;
570 kr = vm_map_enter_mem_object(map,
571 &map_addr,
572 end - start,
573 (mach_vm_offset_t) 0,
574 VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
575 (ipc_port_t) protected_mem_obj,
576 (map_entry->offset +
577 (start - map_entry->vme_start)),
578 TRUE,
579 map_entry->protection,
580 map_entry->max_protection,
581 map_entry->inheritance);
582 assert(map_addr == start);
583 /*
584 * Release the reference obtained by apple_protect_pager_setup().
585 * The mapping (if it succeeded) is now holding a reference on the
586 * memory object.
587 */
588 memory_object_deallocate(protected_mem_obj);
589
590done:
591 if (map_locked) {
592 vm_map_unlock_read(map);
593 }
594 return kr;
595}
596#endif /* CONFIG_CODE_DECRYPTION */
597
598
599lck_grp_t vm_map_lck_grp;
600lck_grp_attr_t vm_map_lck_grp_attr;
601lck_attr_t vm_map_lck_attr;
602lck_attr_t vm_map_lck_rw_attr;
603
604
605/*
606 * vm_map_init:
607 *
608 * Initialize the vm_map module. Must be called before
609 * any other vm_map routines.
610 *
611 * Map and entry structures are allocated from zones -- we must
612 * initialize those zones.
613 *
614 * There are three zones of interest:
615 *
616 * vm_map_zone: used to allocate maps.
617 * vm_map_entry_zone: used to allocate map entries.
618 * vm_map_entry_reserved_zone: fallback zone for kernel map entries
619 *
620 * The kernel allocates map entries from a special zone that is initially
621 * "crammed" with memory. It would be difficult (perhaps impossible) for
622 * the kernel to allocate more memory to a entry zone when it became
623 * empty since the very act of allocating memory implies the creation
624 * of a new entry.
625 */
626void
627vm_map_init(
628 void)
629{
630 vm_size_t entry_zone_alloc_size;
631 const char *mez_name = "VM map entries";
632
633 vm_map_zone = zinit((vm_map_size_t) sizeof(struct _vm_map), 40*1024,
634 PAGE_SIZE, "maps");
635 zone_change(vm_map_zone, Z_NOENCRYPT, TRUE);
636#if defined(__LP64__)
637 entry_zone_alloc_size = PAGE_SIZE * 5;
638#else
639 entry_zone_alloc_size = PAGE_SIZE * 6;
640#endif
641 vm_map_entry_zone = zinit((vm_map_size_t) sizeof(struct vm_map_entry),
642 1024*1024, entry_zone_alloc_size,
643 mez_name);
644 zone_change(vm_map_entry_zone, Z_NOENCRYPT, TRUE);
645 zone_change(vm_map_entry_zone, Z_NOCALLOUT, TRUE);
646 zone_change(vm_map_entry_zone, Z_GZALLOC_EXEMPT, TRUE);
647
648 vm_map_entry_reserved_zone = zinit((vm_map_size_t) sizeof(struct vm_map_entry),
649 kentry_data_size * 64, kentry_data_size,
650 "Reserved VM map entries");
651 zone_change(vm_map_entry_reserved_zone, Z_NOENCRYPT, TRUE);
652
653 vm_map_copy_zone = zinit((vm_map_size_t) sizeof(struct vm_map_copy),
654 16*1024, PAGE_SIZE, "VM map copies");
655 zone_change(vm_map_copy_zone, Z_NOENCRYPT, TRUE);
656
657 /*
658 * Cram the map and kentry zones with initial data.
659 * Set reserved_zone non-collectible to aid zone_gc().
660 */
661 zone_change(vm_map_zone, Z_COLLECT, FALSE);
662
663 zone_change(vm_map_entry_reserved_zone, Z_COLLECT, FALSE);
664 zone_change(vm_map_entry_reserved_zone, Z_EXPAND, FALSE);
665 zone_change(vm_map_entry_reserved_zone, Z_FOREIGN, TRUE);
666 zone_change(vm_map_entry_reserved_zone, Z_NOCALLOUT, TRUE);
667 zone_change(vm_map_entry_reserved_zone, Z_CALLERACCT, FALSE); /* don't charge caller */
668 zone_change(vm_map_copy_zone, Z_CALLERACCT, FALSE); /* don't charge caller */
669 zone_change(vm_map_entry_reserved_zone, Z_GZALLOC_EXEMPT, TRUE);
670
671 zcram(vm_map_zone, (vm_offset_t)map_data, map_data_size);
672 zcram(vm_map_entry_reserved_zone, (vm_offset_t)kentry_data, kentry_data_size);
673
674 lck_grp_attr_setdefault(&vm_map_lck_grp_attr);
675 lck_grp_init(&vm_map_lck_grp, "vm_map", &vm_map_lck_grp_attr);
676 lck_attr_setdefault(&vm_map_lck_attr);
677
678 lck_attr_setdefault(&vm_map_lck_rw_attr);
679 lck_attr_cleardebug(&vm_map_lck_rw_attr);
680
681#if CONFIG_FREEZE
682 default_freezer_init();
683#endif /* CONFIG_FREEZE */
684}
685
686void
687vm_map_steal_memory(
688 void)
689{
690 uint32_t kentry_initial_pages;
691
692 map_data_size = round_page(10 * sizeof(struct _vm_map));
693 map_data = pmap_steal_memory(map_data_size);
694
695 /*
696 * kentry_initial_pages corresponds to the number of kernel map entries
697 * required during bootstrap until the asynchronous replenishment
698 * scheme is activated and/or entries are available from the general
699 * map entry pool.
700 */
701#if defined(__LP64__)
702 kentry_initial_pages = 10;
703#else
704 kentry_initial_pages = 6;
705#endif
706
707#if CONFIG_GZALLOC
708 /* If using the guard allocator, reserve more memory for the kernel
709 * reserved map entry pool.
710 */
711 if (gzalloc_enabled())
712 kentry_initial_pages *= 1024;
713#endif
714
715 kentry_data_size = kentry_initial_pages * PAGE_SIZE;
716 kentry_data = pmap_steal_memory(kentry_data_size);
717}
718
719void vm_kernel_reserved_entry_init(void) {
720 zone_prio_refill_configure(vm_map_entry_reserved_zone, (6*PAGE_SIZE)/sizeof(struct vm_map_entry));
721}
722
723/*
724 * vm_map_create:
725 *
726 * Creates and returns a new empty VM map with
727 * the given physical map structure, and having
728 * the given lower and upper address bounds.
729 */
730vm_map_t
731vm_map_create(
732 pmap_t pmap,
733 vm_map_offset_t min,
734 vm_map_offset_t max,
735 boolean_t pageable)
736{
737 static int color_seed = 0;
738 register vm_map_t result;
739
740 result = (vm_map_t) zalloc(vm_map_zone);
741 if (result == VM_MAP_NULL)
742 panic("vm_map_create");
743
744 vm_map_first_entry(result) = vm_map_to_entry(result);
745 vm_map_last_entry(result) = vm_map_to_entry(result);
746 result->hdr.nentries = 0;
747 result->hdr.entries_pageable = pageable;
748
749 vm_map_store_init( &(result->hdr) );
750
751 result->hdr.page_shift = PAGE_SHIFT;
752
753 result->size = 0;
754 result->user_wire_limit = MACH_VM_MAX_ADDRESS; /* default limit is unlimited */
755 result->user_wire_size = 0;
756 result->ref_count = 1;
757#if TASK_SWAPPER
758 result->res_count = 1;
759 result->sw_state = MAP_SW_IN;
760#endif /* TASK_SWAPPER */
761 result->pmap = pmap;
762 result->min_offset = min;
763 result->max_offset = max;
764 result->wiring_required = FALSE;
765 result->no_zero_fill = FALSE;
766 result->mapped_in_other_pmaps = FALSE;
767 result->wait_for_space = FALSE;
768 result->switch_protect = FALSE;
769 result->disable_vmentry_reuse = FALSE;
770 result->map_disallow_data_exec = FALSE;
771 result->highest_entry_end = 0;
772 result->first_free = vm_map_to_entry(result);
773 result->hint = vm_map_to_entry(result);
774 result->color_rr = (color_seed++) & vm_color_mask;
775 result->jit_entry_exists = FALSE;
776#if CONFIG_FREEZE
777 result->default_freezer_handle = NULL;
778#endif
779 vm_map_lock_init(result);
780 lck_mtx_init_ext(&result->s_lock, &result->s_lock_ext, &vm_map_lck_grp, &vm_map_lck_attr);
781
782 return(result);
783}
784
785/*
786 * vm_map_entry_create: [ internal use only ]
787 *
788 * Allocates a VM map entry for insertion in the
789 * given map (or map copy). No fields are filled.
790 */
791#define vm_map_entry_create(map, map_locked) _vm_map_entry_create(&(map)->hdr, map_locked)
792
793#define vm_map_copy_entry_create(copy, map_locked) \
794 _vm_map_entry_create(&(copy)->cpy_hdr, map_locked)
795unsigned reserved_zalloc_count, nonreserved_zalloc_count;
796
797static vm_map_entry_t
798_vm_map_entry_create(
799 struct vm_map_header *map_header, boolean_t __unused map_locked)
800{
801 zone_t zone;
802 vm_map_entry_t entry;
803
804 zone = vm_map_entry_zone;
805
806 assert(map_header->entries_pageable ? !map_locked : TRUE);
807
808 if (map_header->entries_pageable) {
809 entry = (vm_map_entry_t) zalloc(zone);
810 }
811 else {
812 entry = (vm_map_entry_t) zalloc_canblock(zone, FALSE);
813
814 if (entry == VM_MAP_ENTRY_NULL) {
815 zone = vm_map_entry_reserved_zone;
816 entry = (vm_map_entry_t) zalloc(zone);
817 OSAddAtomic(1, &reserved_zalloc_count);
818 } else
819 OSAddAtomic(1, &nonreserved_zalloc_count);
820 }
821
822 if (entry == VM_MAP_ENTRY_NULL)
823 panic("vm_map_entry_create");
824 entry->from_reserved_zone = (zone == vm_map_entry_reserved_zone);
825
826 vm_map_store_update( (vm_map_t) NULL, entry, VM_MAP_ENTRY_CREATE);
827#if MAP_ENTRY_CREATION_DEBUG
828 entry->vme_creation_maphdr = map_header;
829 fastbacktrace(&entry->vme_creation_bt[0],
830 (sizeof(entry->vme_creation_bt)/sizeof(uintptr_t)));
831#endif
832 return(entry);
833}
834
835/*
836 * vm_map_entry_dispose: [ internal use only ]
837 *
838 * Inverse of vm_map_entry_create.
839 *
840 * write map lock held so no need to
841 * do anything special to insure correctness
842 * of the stores
843 */
844#define vm_map_entry_dispose(map, entry) \
845 _vm_map_entry_dispose(&(map)->hdr, (entry))
846
847#define vm_map_copy_entry_dispose(map, entry) \
848 _vm_map_entry_dispose(&(copy)->cpy_hdr, (entry))
849
850static void
851_vm_map_entry_dispose(
852 register struct vm_map_header *map_header,
853 register vm_map_entry_t entry)
854{
855 register zone_t zone;
856
857 if (map_header->entries_pageable || !(entry->from_reserved_zone))
858 zone = vm_map_entry_zone;
859 else
860 zone = vm_map_entry_reserved_zone;
861
862 if (!map_header->entries_pageable) {
863 if (zone == vm_map_entry_zone)
864 OSAddAtomic(-1, &nonreserved_zalloc_count);
865 else
866 OSAddAtomic(-1, &reserved_zalloc_count);
867 }
868
869 zfree(zone, entry);
870}
871
872#if MACH_ASSERT
873static boolean_t first_free_check = FALSE;
874boolean_t
875first_free_is_valid(
876 vm_map_t map)
877{
878 if (!first_free_check)
879 return TRUE;
880
881 return( first_free_is_valid_store( map ));
882}
883#endif /* MACH_ASSERT */
884
885
886#define vm_map_copy_entry_link(copy, after_where, entry) \
887 _vm_map_store_entry_link(&(copy)->cpy_hdr, after_where, (entry))
888
889#define vm_map_copy_entry_unlink(copy, entry) \
890 _vm_map_store_entry_unlink(&(copy)->cpy_hdr, (entry))
891
892#if MACH_ASSERT && TASK_SWAPPER
893/*
894 * vm_map_res_reference:
895 *
896 * Adds another valid residence count to the given map.
897 *
898 * Map is locked so this function can be called from
899 * vm_map_swapin.
900 *
901 */
902void vm_map_res_reference(register vm_map_t map)
903{
904 /* assert map is locked */
905 assert(map->res_count >= 0);
906 assert(map->ref_count >= map->res_count);
907 if (map->res_count == 0) {
908 lck_mtx_unlock(&map->s_lock);
909 vm_map_lock(map);
910 vm_map_swapin(map);
911 lck_mtx_lock(&map->s_lock);
912 ++map->res_count;
913 vm_map_unlock(map);
914 } else
915 ++map->res_count;
916}
917
918/*
919 * vm_map_reference_swap:
920 *
921 * Adds valid reference and residence counts to the given map.
922 *
923 * The map may not be in memory (i.e. zero residence count).
924 *
925 */
926void vm_map_reference_swap(register vm_map_t map)
927{
928 assert(map != VM_MAP_NULL);
929 lck_mtx_lock(&map->s_lock);
930 assert(map->res_count >= 0);
931 assert(map->ref_count >= map->res_count);
932 map->ref_count++;
933 vm_map_res_reference(map);
934 lck_mtx_unlock(&map->s_lock);
935}
936
937/*
938 * vm_map_res_deallocate:
939 *
940 * Decrement residence count on a map; possibly causing swapout.
941 *
942 * The map must be in memory (i.e. non-zero residence count).
943 *
944 * The map is locked, so this function is callable from vm_map_deallocate.
945 *
946 */
947void vm_map_res_deallocate(register vm_map_t map)
948{
949 assert(map->res_count > 0);
950 if (--map->res_count == 0) {
951 lck_mtx_unlock(&map->s_lock);
952 vm_map_lock(map);
953 vm_map_swapout(map);
954 vm_map_unlock(map);
955 lck_mtx_lock(&map->s_lock);
956 }
957 assert(map->ref_count >= map->res_count);
958}
959#endif /* MACH_ASSERT && TASK_SWAPPER */
960
961/*
962 * vm_map_destroy:
963 *
964 * Actually destroy a map.
965 */
966void
967vm_map_destroy(
968 vm_map_t map,
969 int flags)
970{
971 vm_map_lock(map);
972
973 /* clean up regular map entries */
974 (void) vm_map_delete(map, map->min_offset, map->max_offset,
975 flags, VM_MAP_NULL);
976 /* clean up leftover special mappings (commpage, etc...) */
977 (void) vm_map_delete(map, 0x0, 0xFFFFFFFFFFFFF000ULL,
978 flags, VM_MAP_NULL);
979
980#if CONFIG_FREEZE
981 if (map->default_freezer_handle) {
982 default_freezer_handle_deallocate(map->default_freezer_handle);
983 map->default_freezer_handle = NULL;
984 }
985#endif
986 vm_map_unlock(map);
987
988 assert(map->hdr.nentries == 0);
989
990 if(map->pmap)
991 pmap_destroy(map->pmap);
992
993 zfree(vm_map_zone, map);
994}
995
996#if TASK_SWAPPER
997/*
998 * vm_map_swapin/vm_map_swapout
999 *
1000 * Swap a map in and out, either referencing or releasing its resources.
1001 * These functions are internal use only; however, they must be exported
1002 * because they may be called from macros, which are exported.
1003 *
1004 * In the case of swapout, there could be races on the residence count,
1005 * so if the residence count is up, we return, assuming that a
1006 * vm_map_deallocate() call in the near future will bring us back.
1007 *
1008 * Locking:
1009 * -- We use the map write lock for synchronization among races.
1010 * -- The map write lock, and not the simple s_lock, protects the
1011 * swap state of the map.
1012 * -- If a map entry is a share map, then we hold both locks, in
1013 * hierarchical order.
1014 *
1015 * Synchronization Notes:
1016 * 1) If a vm_map_swapin() call happens while swapout in progress, it
1017 * will block on the map lock and proceed when swapout is through.
1018 * 2) A vm_map_reference() call at this time is illegal, and will
1019 * cause a panic. vm_map_reference() is only allowed on resident
1020 * maps, since it refuses to block.
1021 * 3) A vm_map_swapin() call during a swapin will block, and
1022 * proceeed when the first swapin is done, turning into a nop.
1023 * This is the reason the res_count is not incremented until
1024 * after the swapin is complete.
1025 * 4) There is a timing hole after the checks of the res_count, before
1026 * the map lock is taken, during which a swapin may get the lock
1027 * before a swapout about to happen. If this happens, the swapin
1028 * will detect the state and increment the reference count, causing
1029 * the swapout to be a nop, thereby delaying it until a later
1030 * vm_map_deallocate. If the swapout gets the lock first, then
1031 * the swapin will simply block until the swapout is done, and
1032 * then proceed.
1033 *
1034 * Because vm_map_swapin() is potentially an expensive operation, it
1035 * should be used with caution.
1036 *
1037 * Invariants:
1038 * 1) A map with a residence count of zero is either swapped, or
1039 * being swapped.
1040 * 2) A map with a non-zero residence count is either resident,
1041 * or being swapped in.
1042 */
1043
1044int vm_map_swap_enable = 1;
1045
1046void vm_map_swapin (vm_map_t map)
1047{
1048 register vm_map_entry_t entry;
1049
1050 if (!vm_map_swap_enable) /* debug */
1051 return;
1052
1053 /*
1054 * Map is locked
1055 * First deal with various races.
1056 */
1057 if (map->sw_state == MAP_SW_IN)
1058 /*
1059 * we raced with swapout and won. Returning will incr.
1060 * the res_count, turning the swapout into a nop.
1061 */
1062 return;
1063
1064 /*
1065 * The residence count must be zero. If we raced with another
1066 * swapin, the state would have been IN; if we raced with a
1067 * swapout (after another competing swapin), we must have lost
1068 * the race to get here (see above comment), in which case
1069 * res_count is still 0.
1070 */
1071 assert(map->res_count == 0);
1072
1073 /*
1074 * There are no intermediate states of a map going out or
1075 * coming in, since the map is locked during the transition.
1076 */
1077 assert(map->sw_state == MAP_SW_OUT);
1078
1079 /*
1080 * We now operate upon each map entry. If the entry is a sub-
1081 * or share-map, we call vm_map_res_reference upon it.
1082 * If the entry is an object, we call vm_object_res_reference
1083 * (this may iterate through the shadow chain).
1084 * Note that we hold the map locked the entire time,
1085 * even if we get back here via a recursive call in
1086 * vm_map_res_reference.
1087 */
1088 entry = vm_map_first_entry(map);
1089
1090 while (entry != vm_map_to_entry(map)) {
1091 if (entry->object.vm_object != VM_OBJECT_NULL) {
1092 if (entry->is_sub_map) {
1093 vm_map_t lmap = entry->object.sub_map;
1094 lck_mtx_lock(&lmap->s_lock);
1095 vm_map_res_reference(lmap);
1096 lck_mtx_unlock(&lmap->s_lock);
1097 } else {
1098 vm_object_t object = entry->object.vm_object;
1099 vm_object_lock(object);
1100 /*
1101 * This call may iterate through the
1102 * shadow chain.
1103 */
1104 vm_object_res_reference(object);
1105 vm_object_unlock(object);
1106 }
1107 }
1108 entry = entry->vme_next;
1109 }
1110 assert(map->sw_state == MAP_SW_OUT);
1111 map->sw_state = MAP_SW_IN;
1112}
1113
1114void vm_map_swapout(vm_map_t map)
1115{
1116 register vm_map_entry_t entry;
1117
1118 /*
1119 * Map is locked
1120 * First deal with various races.
1121 * If we raced with a swapin and lost, the residence count
1122 * will have been incremented to 1, and we simply return.
1123 */
1124 lck_mtx_lock(&map->s_lock);
1125 if (map->res_count != 0) {
1126 lck_mtx_unlock(&map->s_lock);
1127 return;
1128 }
1129 lck_mtx_unlock(&map->s_lock);
1130
1131 /*
1132 * There are no intermediate states of a map going out or
1133 * coming in, since the map is locked during the transition.
1134 */
1135 assert(map->sw_state == MAP_SW_IN);
1136
1137 if (!vm_map_swap_enable)
1138 return;
1139
1140 /*
1141 * We now operate upon each map entry. If the entry is a sub-
1142 * or share-map, we call vm_map_res_deallocate upon it.
1143 * If the entry is an object, we call vm_object_res_deallocate
1144 * (this may iterate through the shadow chain).
1145 * Note that we hold the map locked the entire time,
1146 * even if we get back here via a recursive call in
1147 * vm_map_res_deallocate.
1148 */
1149 entry = vm_map_first_entry(map);
1150
1151 while (entry != vm_map_to_entry(map)) {
1152 if (entry->object.vm_object != VM_OBJECT_NULL) {
1153 if (entry->is_sub_map) {
1154 vm_map_t lmap = entry->object.sub_map;
1155 lck_mtx_lock(&lmap->s_lock);
1156 vm_map_res_deallocate(lmap);
1157 lck_mtx_unlock(&lmap->s_lock);
1158 } else {
1159 vm_object_t object = entry->object.vm_object;
1160 vm_object_lock(object);
1161 /*
1162 * This call may take a long time,
1163 * since it could actively push
1164 * out pages (if we implement it
1165 * that way).
1166 */
1167 vm_object_res_deallocate(object);
1168 vm_object_unlock(object);
1169 }
1170 }
1171 entry = entry->vme_next;
1172 }
1173 assert(map->sw_state == MAP_SW_IN);
1174 map->sw_state = MAP_SW_OUT;
1175}
1176
1177#endif /* TASK_SWAPPER */
1178
1179/*
1180 * vm_map_lookup_entry: [ internal use only ]
1181 *
1182 * Calls into the vm map store layer to find the map
1183 * entry containing (or immediately preceding) the
1184 * specified address in the given map; the entry is returned
1185 * in the "entry" parameter. The boolean
1186 * result indicates whether the address is
1187 * actually contained in the map.
1188 */
1189boolean_t
1190vm_map_lookup_entry(
1191 register vm_map_t map,
1192 register vm_map_offset_t address,
1193 vm_map_entry_t *entry) /* OUT */
1194{
1195 return ( vm_map_store_lookup_entry( map, address, entry ));
1196}
1197
1198/*
1199 * Routine: vm_map_find_space
1200 * Purpose:
1201 * Allocate a range in the specified virtual address map,
1202 * returning the entry allocated for that range.
1203 * Used by kmem_alloc, etc.
1204 *
1205 * The map must be NOT be locked. It will be returned locked
1206 * on KERN_SUCCESS, unlocked on failure.
1207 *
1208 * If an entry is allocated, the object/offset fields
1209 * are initialized to zero.
1210 */
1211kern_return_t
1212vm_map_find_space(
1213 register vm_map_t map,
1214 vm_map_offset_t *address, /* OUT */
1215 vm_map_size_t size,
1216 vm_map_offset_t mask,
1217 int flags,
1218 vm_map_entry_t *o_entry) /* OUT */
1219{
1220 register vm_map_entry_t entry, new_entry;
1221 register vm_map_offset_t start;
1222 register vm_map_offset_t end;
1223
1224 if (size == 0) {
1225 *address = 0;
1226 return KERN_INVALID_ARGUMENT;
1227 }
1228
1229 if (flags & VM_FLAGS_GUARD_AFTER) {
1230 /* account for the back guard page in the size */
1231 size += VM_MAP_PAGE_SIZE(map);
1232 }
1233
1234 new_entry = vm_map_entry_create(map, FALSE);
1235
1236 /*
1237 * Look for the first possible address; if there's already
1238 * something at this address, we have to start after it.
1239 */
1240
1241 vm_map_lock(map);
1242
1243 if( map->disable_vmentry_reuse == TRUE) {
1244 VM_MAP_HIGHEST_ENTRY(map, entry, start);
1245 } else {
1246 assert(first_free_is_valid(map));
1247 if ((entry = map->first_free) == vm_map_to_entry(map))
1248 start = map->min_offset;
1249 else
1250 start = entry->vme_end;
1251 }
1252
1253 /*
1254 * In any case, the "entry" always precedes
1255 * the proposed new region throughout the loop:
1256 */
1257
1258 while (TRUE) {
1259 register vm_map_entry_t next;
1260
1261 /*
1262 * Find the end of the proposed new region.
1263 * Be sure we didn't go beyond the end, or
1264 * wrap around the address.
1265 */
1266
1267 if (flags & VM_FLAGS_GUARD_BEFORE) {
1268 /* reserve space for the front guard page */
1269 start += VM_MAP_PAGE_SIZE(map);
1270 }
1271 end = ((start + mask) & ~mask);
1272
1273 if (end < start) {
1274 vm_map_entry_dispose(map, new_entry);
1275 vm_map_unlock(map);
1276 return(KERN_NO_SPACE);
1277 }
1278 start = end;
1279 end += size;
1280
1281 if ((end > map->max_offset) || (end < start)) {
1282 vm_map_entry_dispose(map, new_entry);
1283 vm_map_unlock(map);
1284 return(KERN_NO_SPACE);
1285 }
1286
1287 /*
1288 * If there are no more entries, we must win.
1289 */
1290
1291 next = entry->vme_next;
1292 if (next == vm_map_to_entry(map))
1293 break;
1294
1295 /*
1296 * If there is another entry, it must be
1297 * after the end of the potential new region.
1298 */
1299
1300 if (next->vme_start >= end)
1301 break;
1302
1303 /*
1304 * Didn't fit -- move to the next entry.
1305 */
1306
1307 entry = next;
1308 start = entry->vme_end;
1309 }
1310
1311 /*
1312 * At this point,
1313 * "start" and "end" should define the endpoints of the
1314 * available new range, and
1315 * "entry" should refer to the region before the new
1316 * range, and
1317 *
1318 * the map should be locked.
1319 */
1320
1321 if (flags & VM_FLAGS_GUARD_BEFORE) {
1322 /* go back for the front guard page */
1323 start -= VM_MAP_PAGE_SIZE(map);
1324 }
1325 *address = start;
1326
1327 assert(start < end);
1328 new_entry->vme_start = start;
1329 new_entry->vme_end = end;
1330 assert(page_aligned(new_entry->vme_start));
1331 assert(page_aligned(new_entry->vme_end));
1332 assert(VM_MAP_PAGE_ALIGNED(new_entry->vme_start,
1333 VM_MAP_PAGE_MASK(map)));
1334 assert(VM_MAP_PAGE_ALIGNED(new_entry->vme_end,
1335 VM_MAP_PAGE_MASK(map)));
1336
1337 new_entry->is_shared = FALSE;
1338 new_entry->is_sub_map = FALSE;
1339 new_entry->use_pmap = TRUE;
1340 new_entry->object.vm_object = VM_OBJECT_NULL;
1341 new_entry->offset = (vm_object_offset_t) 0;
1342
1343 new_entry->needs_copy = FALSE;
1344
1345 new_entry->inheritance = VM_INHERIT_DEFAULT;
1346 new_entry->protection = VM_PROT_DEFAULT;
1347 new_entry->max_protection = VM_PROT_ALL;
1348 new_entry->behavior = VM_BEHAVIOR_DEFAULT;
1349 new_entry->wired_count = 0;
1350 new_entry->user_wired_count = 0;
1351
1352 new_entry->in_transition = FALSE;
1353 new_entry->needs_wakeup = FALSE;
1354 new_entry->no_cache = FALSE;
1355 new_entry->permanent = FALSE;
1356 new_entry->superpage_size = FALSE;
1357 if (VM_MAP_PAGE_SHIFT(map) != PAGE_SHIFT) {
1358 new_entry->map_aligned = TRUE;
1359 } else {
1360 new_entry->map_aligned = FALSE;
1361 }
1362
1363 new_entry->used_for_jit = 0;
1364
1365 new_entry->alias = 0;
1366 new_entry->zero_wired_pages = FALSE;
1367 new_entry->iokit_acct = FALSE;
1368
1369 VM_GET_FLAGS_ALIAS(flags, new_entry->alias);
1370
1371 /*
1372 * Insert the new entry into the list
1373 */
1374
1375 vm_map_store_entry_link(map, entry, new_entry);
1376
1377 map->size += size;
1378
1379 /*
1380 * Update the lookup hint
1381 */
1382 SAVE_HINT_MAP_WRITE(map, new_entry);
1383
1384 *o_entry = new_entry;
1385 return(KERN_SUCCESS);
1386}
1387
1388int vm_map_pmap_enter_print = FALSE;
1389int vm_map_pmap_enter_enable = FALSE;
1390
1391/*
1392 * Routine: vm_map_pmap_enter [internal only]
1393 *
1394 * Description:
1395 * Force pages from the specified object to be entered into
1396 * the pmap at the specified address if they are present.
1397 * As soon as a page not found in the object the scan ends.
1398 *
1399 * Returns:
1400 * Nothing.
1401 *
1402 * In/out conditions:
1403 * The source map should not be locked on entry.
1404 */
1405__unused static void
1406vm_map_pmap_enter(
1407 vm_map_t map,
1408 register vm_map_offset_t addr,
1409 register vm_map_offset_t end_addr,
1410 register vm_object_t object,
1411 vm_object_offset_t offset,
1412 vm_prot_t protection)
1413{
1414 int type_of_fault;
1415 kern_return_t kr;
1416
1417 if(map->pmap == 0)
1418 return;
1419
1420 while (addr < end_addr) {
1421 register vm_page_t m;
1422
1423
1424 /*
1425 * TODO:
1426 * From vm_map_enter(), we come into this function without the map
1427 * lock held or the object lock held.
1428 * We haven't taken a reference on the object either.
1429 * We should do a proper lookup on the map to make sure
1430 * that things are sane before we go locking objects that
1431 * could have been deallocated from under us.
1432 */
1433
1434 vm_object_lock(object);
1435
1436 m = vm_page_lookup(object, offset);
1437 /*
1438 * ENCRYPTED SWAP:
1439 * The user should never see encrypted data, so do not
1440 * enter an encrypted page in the page table.
1441 */
1442 if (m == VM_PAGE_NULL || m->busy || m->encrypted ||
1443 m->fictitious ||
1444 (m->unusual && ( m->error || m->restart || m->absent))) {
1445 vm_object_unlock(object);
1446 return;
1447 }
1448
1449 if (vm_map_pmap_enter_print) {
1450 printf("vm_map_pmap_enter:");
1451 printf("map: %p, addr: %llx, object: %p, offset: %llx\n",
1452 map, (unsigned long long)addr, object, (unsigned long long)offset);
1453 }
1454 type_of_fault = DBG_CACHE_HIT_FAULT;
1455 kr = vm_fault_enter(m, map->pmap, addr, protection, protection,
1456 VM_PAGE_WIRED(m), FALSE, FALSE, FALSE,
1457 0, /* XXX need user tag / alias? */
1458 0, /* alternate accounting? */
1459 NULL,
1460 &type_of_fault);
1461
1462 vm_object_unlock(object);
1463
1464 offset += PAGE_SIZE_64;
1465 addr += PAGE_SIZE;
1466 }
1467}
1468
1469boolean_t vm_map_pmap_is_empty(
1470 vm_map_t map,
1471 vm_map_offset_t start,
1472 vm_map_offset_t end);
1473boolean_t vm_map_pmap_is_empty(
1474 vm_map_t map,
1475 vm_map_offset_t start,
1476 vm_map_offset_t end)
1477{
1478#ifdef MACHINE_PMAP_IS_EMPTY
1479 return pmap_is_empty(map->pmap, start, end);
1480#else /* MACHINE_PMAP_IS_EMPTY */
1481 vm_map_offset_t offset;
1482 ppnum_t phys_page;
1483
1484 if (map->pmap == NULL) {
1485 return TRUE;
1486 }
1487
1488 for (offset = start;
1489 offset < end;
1490 offset += PAGE_SIZE) {
1491 phys_page = pmap_find_phys(map->pmap, offset);
1492 if (phys_page) {
1493 kprintf("vm_map_pmap_is_empty(%p,0x%llx,0x%llx): "
1494 "page %d at 0x%llx\n",
1495 map, (long long)start, (long long)end,
1496 phys_page, (long long)offset);
1497 return FALSE;
1498 }
1499 }
1500 return TRUE;
1501#endif /* MACHINE_PMAP_IS_EMPTY */
1502}
1503
1504#define MAX_TRIES_TO_GET_RANDOM_ADDRESS 1000
1505kern_return_t
1506vm_map_random_address_for_size(
1507 vm_map_t map,
1508 vm_map_offset_t *address,
1509 vm_map_size_t size)
1510{
1511 kern_return_t kr = KERN_SUCCESS;
1512 int tries = 0;
1513 vm_map_offset_t random_addr = 0;
1514 vm_map_offset_t hole_end;
1515
1516 vm_map_entry_t next_entry = VM_MAP_ENTRY_NULL;
1517 vm_map_entry_t prev_entry = VM_MAP_ENTRY_NULL;
1518 vm_map_size_t vm_hole_size = 0;
1519 vm_map_size_t addr_space_size;
1520
1521 addr_space_size = vm_map_max(map) - vm_map_min(map);
1522
1523 assert(page_aligned(size));
1524
1525 while (tries < MAX_TRIES_TO_GET_RANDOM_ADDRESS) {
1526 random_addr = ((vm_map_offset_t)random()) << PAGE_SHIFT;
1527 random_addr = vm_map_trunc_page(
1528 vm_map_min(map) +(random_addr % addr_space_size),
1529 VM_MAP_PAGE_MASK(map));
1530
1531 if (vm_map_lookup_entry(map, random_addr, &prev_entry) == FALSE) {
1532 if (prev_entry == vm_map_to_entry(map)) {
1533 next_entry = vm_map_first_entry(map);
1534 } else {
1535 next_entry = prev_entry->vme_next;
1536 }
1537 if (next_entry == vm_map_to_entry(map)) {
1538 hole_end = vm_map_max(map);
1539 } else {
1540 hole_end = next_entry->vme_start;
1541 }
1542 vm_hole_size = hole_end - random_addr;
1543 if (vm_hole_size >= size) {
1544 *address = random_addr;
1545 break;
1546 }
1547 }
1548 tries++;
1549 }
1550
1551 if (tries == MAX_TRIES_TO_GET_RANDOM_ADDRESS) {
1552 kr = KERN_NO_SPACE;
1553 }
1554 return kr;
1555}
1556
1557/*
1558 * Routine: vm_map_enter
1559 *
1560 * Description:
1561 * Allocate a range in the specified virtual address map.
1562 * The resulting range will refer to memory defined by
1563 * the given memory object and offset into that object.
1564 *
1565 * Arguments are as defined in the vm_map call.
1566 */
1567int _map_enter_debug = 0;
1568static unsigned int vm_map_enter_restore_successes = 0;
1569static unsigned int vm_map_enter_restore_failures = 0;
1570kern_return_t
1571vm_map_enter(
1572 vm_map_t map,
1573 vm_map_offset_t *address, /* IN/OUT */
1574 vm_map_size_t size,
1575 vm_map_offset_t mask,
1576 int flags,
1577 vm_object_t object,
1578 vm_object_offset_t offset,
1579 boolean_t needs_copy,
1580 vm_prot_t cur_protection,
1581 vm_prot_t max_protection,
1582 vm_inherit_t inheritance)
1583{
1584 vm_map_entry_t entry, new_entry;
1585 vm_map_offset_t start, tmp_start, tmp_offset;
1586 vm_map_offset_t end, tmp_end;
1587 vm_map_offset_t tmp2_start, tmp2_end;
1588 vm_map_offset_t step;
1589 kern_return_t result = KERN_SUCCESS;
1590 vm_map_t zap_old_map = VM_MAP_NULL;
1591 vm_map_t zap_new_map = VM_MAP_NULL;
1592 boolean_t map_locked = FALSE;
1593 boolean_t pmap_empty = TRUE;
1594 boolean_t new_mapping_established = FALSE;
1595 boolean_t keep_map_locked = ((flags & VM_FLAGS_KEEP_MAP_LOCKED) != 0);
1596 boolean_t anywhere = ((flags & VM_FLAGS_ANYWHERE) != 0);
1597 boolean_t purgable = ((flags & VM_FLAGS_PURGABLE) != 0);
1598 boolean_t overwrite = ((flags & VM_FLAGS_OVERWRITE) != 0);
1599 boolean_t no_cache = ((flags & VM_FLAGS_NO_CACHE) != 0);
1600 boolean_t is_submap = ((flags & VM_FLAGS_SUBMAP) != 0);
1601 boolean_t permanent = ((flags & VM_FLAGS_PERMANENT) != 0);
1602 boolean_t entry_for_jit = ((flags & VM_FLAGS_MAP_JIT) != 0);
1603 boolean_t iokit_acct = ((flags & VM_FLAGS_IOKIT_ACCT) != 0);
1604 unsigned int superpage_size = ((flags & VM_FLAGS_SUPERPAGE_MASK) >> VM_FLAGS_SUPERPAGE_SHIFT);
1605 char alias;
1606 vm_map_offset_t effective_min_offset, effective_max_offset;
1607 kern_return_t kr;
1608 boolean_t clear_map_aligned = FALSE;
1609
1610 if (superpage_size) {
1611 switch (superpage_size) {
1612 /*
1613 * Note that the current implementation only supports
1614 * a single size for superpages, SUPERPAGE_SIZE, per
1615 * architecture. As soon as more sizes are supposed
1616 * to be supported, SUPERPAGE_SIZE has to be replaced
1617 * with a lookup of the size depending on superpage_size.
1618 */
1619#ifdef __x86_64__
1620 case SUPERPAGE_SIZE_ANY:
1621 /* handle it like 2 MB and round up to page size */
1622 size = (size + 2*1024*1024 - 1) & ~(2*1024*1024 - 1);
1623 case SUPERPAGE_SIZE_2MB:
1624 break;
1625#endif
1626 default:
1627 return KERN_INVALID_ARGUMENT;
1628 }
1629 mask = SUPERPAGE_SIZE-1;
1630 if (size & (SUPERPAGE_SIZE-1))
1631 return KERN_INVALID_ARGUMENT;
1632 inheritance = VM_INHERIT_NONE; /* fork() children won't inherit superpages */
1633 }
1634
1635
1636
1637 if (is_submap) {
1638 if (purgable) {
1639 /* submaps can not be purgeable */
1640 return KERN_INVALID_ARGUMENT;
1641 }
1642 if (object == VM_OBJECT_NULL) {
1643 /* submaps can not be created lazily */
1644 return KERN_INVALID_ARGUMENT;
1645 }
1646 }
1647 if (flags & VM_FLAGS_ALREADY) {
1648 /*
1649 * VM_FLAGS_ALREADY says that it's OK if the same mapping
1650 * is already present. For it to be meaningul, the requested
1651 * mapping has to be at a fixed address (!VM_FLAGS_ANYWHERE) and
1652 * we shouldn't try and remove what was mapped there first
1653 * (!VM_FLAGS_OVERWRITE).
1654 */
1655 if ((flags & VM_FLAGS_ANYWHERE) ||
1656 (flags & VM_FLAGS_OVERWRITE)) {
1657 return KERN_INVALID_ARGUMENT;
1658 }
1659 }
1660
1661 effective_min_offset = map->min_offset;
1662
1663 if (flags & VM_FLAGS_BEYOND_MAX) {
1664 /*
1665 * Allow an insertion beyond the map's max offset.
1666 */
1667 if (vm_map_is_64bit(map))
1668 effective_max_offset = 0xFFFFFFFFFFFFF000ULL;
1669 else
1670 effective_max_offset = 0x00000000FFFFF000ULL;
1671 } else {
1672 effective_max_offset = map->max_offset;
1673 }
1674
1675 if (size == 0 ||
1676 (offset & PAGE_MASK_64) != 0) {
1677 *address = 0;
1678 return KERN_INVALID_ARGUMENT;
1679 }
1680
1681 VM_GET_FLAGS_ALIAS(flags, alias);
1682
1683#define RETURN(value) { result = value; goto BailOut; }
1684
1685 assert(page_aligned(*address));
1686 assert(page_aligned(size));
1687
1688 if (!VM_MAP_PAGE_ALIGNED(size, VM_MAP_PAGE_MASK(map))) {
1689 /*
1690 * In most cases, the caller rounds the size up to the
1691 * map's page size.
1692 * If we get a size that is explicitly not map-aligned here,
1693 * we'll have to respect the caller's wish and mark the
1694 * mapping as "not map-aligned" to avoid tripping the
1695 * map alignment checks later.
1696 */
1697 clear_map_aligned = TRUE;
1698 }
1699 if (!anywhere &&
1700 !VM_MAP_PAGE_ALIGNED(*address, VM_MAP_PAGE_MASK(map))) {
1701 /*
1702 * We've been asked to map at a fixed address and that
1703 * address is not aligned to the map's specific alignment.
1704 * The caller should know what it's doing (i.e. most likely
1705 * mapping some fragmented copy map, transferring memory from
1706 * a VM map with a different alignment), so clear map_aligned
1707 * for this new VM map entry and proceed.
1708 */
1709 clear_map_aligned = TRUE;
1710 }
1711
1712 /*
1713 * Only zero-fill objects are allowed to be purgable.
1714 * LP64todo - limit purgable objects to 32-bits for now
1715 */
1716 if (purgable &&
1717 (offset != 0 ||
1718 (object != VM_OBJECT_NULL &&
1719 (object->vo_size != size ||
1720 object->purgable == VM_PURGABLE_DENY))
1721 || size > ANON_MAX_SIZE)) /* LP64todo: remove when dp capable */
1722 return KERN_INVALID_ARGUMENT;
1723
1724 if (!anywhere && overwrite) {
1725 /*
1726 * Create a temporary VM map to hold the old mappings in the
1727 * affected area while we create the new one.
1728 * This avoids releasing the VM map lock in
1729 * vm_map_entry_delete() and allows atomicity
1730 * when we want to replace some mappings with a new one.
1731 * It also allows us to restore the old VM mappings if the
1732 * new mapping fails.
1733 */
1734 zap_old_map = vm_map_create(PMAP_NULL,
1735 *address,
1736 *address + size,
1737 map->hdr.entries_pageable);
1738 vm_map_set_page_shift(zap_old_map, VM_MAP_PAGE_SHIFT(map));
1739 }
1740
1741StartAgain: ;
1742
1743 start = *address;
1744
1745 if (anywhere) {
1746 vm_map_lock(map);
1747 map_locked = TRUE;
1748
1749 if (entry_for_jit) {
1750 if (map->jit_entry_exists) {
1751 result = KERN_INVALID_ARGUMENT;
1752 goto BailOut;
1753 }
1754 /*
1755 * Get a random start address.
1756 */
1757 result = vm_map_random_address_for_size(map, address, size);
1758 if (result != KERN_SUCCESS) {
1759 goto BailOut;
1760 }
1761 start = *address;
1762 }
1763
1764
1765 /*
1766 * Calculate the first possible address.
1767 */
1768
1769 if (start < effective_min_offset)
1770 start = effective_min_offset;
1771 if (start > effective_max_offset)
1772 RETURN(KERN_NO_SPACE);
1773
1774 /*
1775 * Look for the first possible address;
1776 * if there's already something at this
1777 * address, we have to start after it.
1778 */
1779
1780 if( map->disable_vmentry_reuse == TRUE) {
1781 VM_MAP_HIGHEST_ENTRY(map, entry, start);
1782 } else {
1783 assert(first_free_is_valid(map));
1784
1785 entry = map->first_free;
1786
1787 if (entry == vm_map_to_entry(map)) {
1788 entry = NULL;
1789 } else {
1790 if (entry->vme_next == vm_map_to_entry(map)){
1791 /*
1792 * Hole at the end of the map.
1793 */
1794 entry = NULL;
1795 } else {
1796 if (start < (entry->vme_next)->vme_start ) {
1797 start = entry->vme_end;
1798 start = vm_map_round_page(start,
1799 VM_MAP_PAGE_MASK(map));
1800 } else {
1801 /*
1802 * Need to do a lookup.
1803 */
1804 entry = NULL;
1805 }
1806 }
1807 }
1808
1809 if (entry == NULL) {
1810 vm_map_entry_t tmp_entry;
1811 if (vm_map_lookup_entry(map, start, &tmp_entry)) {
1812 assert(!entry_for_jit);
1813 start = tmp_entry->vme_end;
1814 start = vm_map_round_page(start,
1815 VM_MAP_PAGE_MASK(map));
1816 }
1817 entry = tmp_entry;
1818 }
1819 }
1820
1821 /*
1822 * In any case, the "entry" always precedes
1823 * the proposed new region throughout the
1824 * loop:
1825 */
1826
1827 while (TRUE) {
1828 register vm_map_entry_t next;
1829
1830 /*
1831 * Find the end of the proposed new region.
1832 * Be sure we didn't go beyond the end, or
1833 * wrap around the address.
1834 */
1835
1836 end = ((start + mask) & ~mask);
1837 end = vm_map_round_page(end,
1838 VM_MAP_PAGE_MASK(map));
1839 if (end < start)
1840 RETURN(KERN_NO_SPACE);
1841 start = end;
1842 assert(VM_MAP_PAGE_ALIGNED(start,
1843 VM_MAP_PAGE_MASK(map)));
1844 end += size;
1845
1846 if ((end > effective_max_offset) || (end < start)) {
1847 if (map->wait_for_space) {
1848 assert(!keep_map_locked);
1849 if (size <= (effective_max_offset -
1850 effective_min_offset)) {
1851 assert_wait((event_t)map,
1852 THREAD_ABORTSAFE);
1853 vm_map_unlock(map);
1854 map_locked = FALSE;
1855 thread_block(THREAD_CONTINUE_NULL);
1856 goto StartAgain;
1857 }
1858 }
1859 RETURN(KERN_NO_SPACE);
1860 }
1861
1862 /*
1863 * If there are no more entries, we must win.
1864 */
1865
1866 next = entry->vme_next;
1867 if (next == vm_map_to_entry(map))
1868 break;
1869
1870 /*
1871 * If there is another entry, it must be
1872 * after the end of the potential new region.
1873 */
1874
1875 if (next->vme_start >= end)
1876 break;
1877
1878 /*
1879 * Didn't fit -- move to the next entry.
1880 */
1881
1882 entry = next;
1883 start = entry->vme_end;
1884 start = vm_map_round_page(start,
1885 VM_MAP_PAGE_MASK(map));
1886 }
1887 *address = start;
1888 assert(VM_MAP_PAGE_ALIGNED(*address,
1889 VM_MAP_PAGE_MASK(map)));
1890 } else {
1891 /*
1892 * Verify that:
1893 * the address doesn't itself violate
1894 * the mask requirement.
1895 */
1896
1897 vm_map_lock(map);
1898 map_locked = TRUE;
1899 if ((start & mask) != 0)
1900 RETURN(KERN_NO_SPACE);
1901
1902 /*
1903 * ... the address is within bounds
1904 */
1905
1906 end = start + size;
1907
1908 if ((start < effective_min_offset) ||
1909 (end > effective_max_offset) ||
1910 (start >= end)) {
1911 RETURN(KERN_INVALID_ADDRESS);
1912 }
1913
1914 if (overwrite && zap_old_map != VM_MAP_NULL) {
1915 /*
1916 * Fixed mapping and "overwrite" flag: attempt to
1917 * remove all existing mappings in the specified
1918 * address range, saving them in our "zap_old_map".
1919 */
1920 (void) vm_map_delete(map, start, end,
1921 (VM_MAP_REMOVE_SAVE_ENTRIES |
1922 VM_MAP_REMOVE_NO_MAP_ALIGN),
1923 zap_old_map);
1924 }
1925
1926 /*
1927 * ... the starting address isn't allocated
1928 */
1929
1930 if (vm_map_lookup_entry(map, start, &entry)) {
1931 if (! (flags & VM_FLAGS_ALREADY)) {
1932 RETURN(KERN_NO_SPACE);
1933 }
1934 /*
1935 * Check if what's already there is what we want.
1936 */
1937 tmp_start = start;
1938 tmp_offset = offset;
1939 if (entry->vme_start < start) {
1940 tmp_start -= start - entry->vme_start;
1941 tmp_offset -= start - entry->vme_start;
1942
1943 }
1944 for (; entry->vme_start < end;
1945 entry = entry->vme_next) {
1946 /*
1947 * Check if the mapping's attributes
1948 * match the existing map entry.
1949 */
1950 if (entry == vm_map_to_entry(map) ||
1951 entry->vme_start != tmp_start ||
1952 entry->is_sub_map != is_submap ||
1953 entry->offset != tmp_offset ||
1954 entry->needs_copy != needs_copy ||
1955 entry->protection != cur_protection ||
1956 entry->max_protection != max_protection ||
1957 entry->inheritance != inheritance ||
1958 entry->iokit_acct != iokit_acct ||
1959 entry->alias != alias) {
1960 /* not the same mapping ! */
1961 RETURN(KERN_NO_SPACE);
1962 }
1963 /*
1964 * Check if the same object is being mapped.
1965 */
1966 if (is_submap) {
1967 if (entry->object.sub_map !=
1968 (vm_map_t) object) {
1969 /* not the same submap */
1970 RETURN(KERN_NO_SPACE);
1971 }
1972 } else {
1973 if (entry->object.vm_object != object) {
1974 /* not the same VM object... */
1975 vm_object_t obj2;
1976
1977 obj2 = entry->object.vm_object;
1978 if ((obj2 == VM_OBJECT_NULL ||
1979 obj2->internal) &&
1980 (object == VM_OBJECT_NULL ||
1981 object->internal)) {
1982 /*
1983 * ... but both are
1984 * anonymous memory,
1985 * so equivalent.
1986 */
1987 } else {
1988 RETURN(KERN_NO_SPACE);
1989 }
1990 }
1991 }
1992
1993 tmp_offset += entry->vme_end - entry->vme_start;
1994 tmp_start += entry->vme_end - entry->vme_start;
1995 if (entry->vme_end >= end) {
1996 /* reached the end of our mapping */
1997 break;
1998 }
1999 }
2000 /* it all matches: let's use what's already there ! */
2001 RETURN(KERN_MEMORY_PRESENT);
2002 }
2003
2004 /*
2005 * ... the next region doesn't overlap the
2006 * end point.
2007 */
2008
2009 if ((entry->vme_next != vm_map_to_entry(map)) &&
2010 (entry->vme_next->vme_start < end))
2011 RETURN(KERN_NO_SPACE);
2012 }
2013
2014 /*
2015 * At this point,
2016 * "start" and "end" should define the endpoints of the
2017 * available new range, and
2018 * "entry" should refer to the region before the new
2019 * range, and
2020 *
2021 * the map should be locked.
2022 */
2023
2024 /*
2025 * See whether we can avoid creating a new entry (and object) by
2026 * extending one of our neighbors. [So far, we only attempt to
2027 * extend from below.] Note that we can never extend/join
2028 * purgable objects because they need to remain distinct
2029 * entities in order to implement their "volatile object"
2030 * semantics.
2031 */
2032
2033 if (purgable || entry_for_jit) {
2034 if (object == VM_OBJECT_NULL) {
2035 object = vm_object_allocate(size);
2036 object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
2037 object->true_share = TRUE;
2038 if (purgable) {
2039 task_t owner;
2040 object->purgable = VM_PURGABLE_NONVOLATILE;
2041 if (map->pmap == kernel_pmap) {
2042 /*
2043 * Purgeable mappings made in a kernel
2044 * map are "owned" by the kernel itself
2045 * rather than the current user task
2046 * because they're likely to be used by
2047 * more than this user task (see
2048 * execargs_purgeable_allocate(), for
2049 * example).
2050 */
2051 owner = kernel_task;
2052 } else {
2053 owner = current_task();
2054 }
2055 assert(object->vo_purgeable_owner == NULL);
2056 assert(object->resident_page_count == 0);
2057 assert(object->wired_page_count == 0);
2058 vm_object_lock(object);
2059 vm_purgeable_nonvolatile_enqueue(object, owner);
2060 vm_object_unlock(object);
2061 }
2062 offset = (vm_object_offset_t)0;
2063 }
2064 } else if ((is_submap == FALSE) &&
2065 (object == VM_OBJECT_NULL) &&
2066 (entry != vm_map_to_entry(map)) &&
2067 (entry->vme_end == start) &&
2068 (!entry->is_shared) &&
2069 (!entry->is_sub_map) &&
2070 (!entry->in_transition) &&
2071 (!entry->needs_wakeup) &&
2072 (entry->behavior == VM_BEHAVIOR_DEFAULT) &&
2073 (entry->protection == cur_protection) &&
2074 (entry->max_protection == max_protection) &&
2075 (entry->inheritance == inheritance) &&
2076 ((alias == VM_MEMORY_REALLOC) || (entry->alias == alias)) &&
2077 (entry->no_cache == no_cache) &&
2078 (entry->permanent == permanent) &&
2079 (!entry->superpage_size && !superpage_size) &&
2080 /*
2081 * No coalescing if not map-aligned, to avoid propagating
2082 * that condition any further than needed:
2083 */
2084 (!entry->map_aligned || !clear_map_aligned) &&
2085 (!entry->zero_wired_pages) &&
2086 (!entry->used_for_jit && !entry_for_jit) &&
2087 (entry->iokit_acct == iokit_acct) &&
2088
2089 ((entry->vme_end - entry->vme_start) + size <=
2090 (alias == VM_MEMORY_REALLOC ?
2091 ANON_CHUNK_SIZE :
2092 NO_COALESCE_LIMIT)) &&
2093
2094 (entry->wired_count == 0)) { /* implies user_wired_count == 0 */
2095 if (vm_object_coalesce(entry->object.vm_object,
2096 VM_OBJECT_NULL,
2097 entry->offset,
2098 (vm_object_offset_t) 0,
2099 (vm_map_size_t)(entry->vme_end - entry->vme_start),
2100 (vm_map_size_t)(end - entry->vme_end))) {
2101
2102 /*
2103 * Coalesced the two objects - can extend
2104 * the previous map entry to include the
2105 * new range.
2106 */
2107 map->size += (end - entry->vme_end);
2108 assert(entry->vme_start < end);
2109 assert(VM_MAP_PAGE_ALIGNED(end,
2110 VM_MAP_PAGE_MASK(map)));
2111 entry->vme_end = end;
2112 vm_map_store_update_first_free(map, map->first_free);
2113 new_mapping_established = TRUE;
2114 RETURN(KERN_SUCCESS);
2115 }
2116 }
2117
2118 step = superpage_size ? SUPERPAGE_SIZE : (end - start);
2119 new_entry = NULL;
2120
2121 for (tmp2_start = start; tmp2_start<end; tmp2_start += step) {
2122 tmp2_end = tmp2_start + step;
2123 /*
2124 * Create a new entry
2125 * LP64todo - for now, we can only allocate 4GB internal objects
2126 * because the default pager can't page bigger ones. Remove this
2127 * when it can.
2128 *
2129 * XXX FBDP
2130 * The reserved "page zero" in each process's address space can
2131 * be arbitrarily large. Splitting it into separate 4GB objects and
2132 * therefore different VM map entries serves no purpose and just
2133 * slows down operations on the VM map, so let's not split the
2134 * allocation into 4GB chunks if the max protection is NONE. That
2135 * memory should never be accessible, so it will never get to the
2136 * default pager.
2137 */
2138 tmp_start = tmp2_start;
2139 if (object == VM_OBJECT_NULL &&
2140 size > (vm_map_size_t)ANON_CHUNK_SIZE &&
2141 max_protection != VM_PROT_NONE &&
2142 superpage_size == 0)
2143 tmp_end = tmp_start + (vm_map_size_t)ANON_CHUNK_SIZE;
2144 else
2145 tmp_end = tmp2_end;
2146 do {
2147 new_entry = vm_map_entry_insert(map, entry, tmp_start, tmp_end,
2148 object, offset, needs_copy,
2149 FALSE, FALSE,
2150 cur_protection, max_protection,
2151 VM_BEHAVIOR_DEFAULT,
2152 (entry_for_jit)? VM_INHERIT_NONE: inheritance,
2153 0, no_cache,
2154 permanent,
2155 superpage_size,
2156 clear_map_aligned,
2157 is_submap);
2158 new_entry->alias = alias;
2159 if (entry_for_jit){
2160 if (!(map->jit_entry_exists)){
2161 new_entry->used_for_jit = TRUE;
2162 map->jit_entry_exists = TRUE;
2163 }
2164 }
2165
2166 assert(!new_entry->iokit_acct);
2167 if (!is_submap &&
2168 object != VM_OBJECT_NULL &&
2169 object->purgable != VM_PURGABLE_DENY) {
2170 assert(new_entry->use_pmap);
2171 assert(!new_entry->iokit_acct);
2172 /*
2173 * Turn off pmap accounting since
2174 * purgeable objects have their
2175 * own ledgers.
2176 */
2177 new_entry->use_pmap = FALSE;
2178 } else if (!is_submap &&
2179 iokit_acct) {
2180 /* alternate accounting */
2181 assert(!new_entry->iokit_acct);
2182 assert(new_entry->use_pmap);
2183 new_entry->iokit_acct = TRUE;
2184 new_entry->use_pmap = FALSE;
2185 vm_map_iokit_mapped_region(
2186 map,
2187 (new_entry->vme_end -
2188 new_entry->vme_start));
2189 } else if (!is_submap) {
2190 assert(!new_entry->iokit_acct);
2191 assert(new_entry->use_pmap);
2192 }
2193
2194 if (is_submap) {
2195 vm_map_t submap;
2196 boolean_t submap_is_64bit;
2197 boolean_t use_pmap;
2198
2199 assert(new_entry->is_sub_map);
2200 assert(!new_entry->use_pmap);
2201 assert(!new_entry->iokit_acct);
2202 submap = (vm_map_t) object;
2203 submap_is_64bit = vm_map_is_64bit(submap);
2204 use_pmap = (alias == VM_MEMORY_SHARED_PMAP);
2205#ifndef NO_NESTED_PMAP
2206 if (use_pmap && submap->pmap == NULL) {
2207 ledger_t ledger = map->pmap->ledger;
2208 /* we need a sub pmap to nest... */
2209 submap->pmap = pmap_create(ledger, 0,
2210 submap_is_64bit);
2211 if (submap->pmap == NULL) {
2212 /* let's proceed without nesting... */
2213 }
2214 }
2215 if (use_pmap && submap->pmap != NULL) {
2216 kr = pmap_nest(map->pmap,
2217 submap->pmap,
2218 tmp_start,
2219 tmp_start,
2220 tmp_end - tmp_start);
2221 if (kr != KERN_SUCCESS) {
2222 printf("vm_map_enter: "
2223 "pmap_nest(0x%llx,0x%llx) "
2224 "error 0x%x\n",
2225 (long long)tmp_start,
2226 (long long)tmp_end,
2227 kr);
2228 } else {
2229 /* we're now nested ! */
2230 new_entry->use_pmap = TRUE;
2231 pmap_empty = FALSE;
2232 }
2233 }
2234#endif /* NO_NESTED_PMAP */
2235 }
2236 entry = new_entry;
2237
2238 if (superpage_size) {
2239 vm_page_t pages, m;
2240 vm_object_t sp_object;
2241
2242 entry->offset = 0;
2243
2244 /* allocate one superpage */
2245 kr = cpm_allocate(SUPERPAGE_SIZE, &pages, 0, SUPERPAGE_NBASEPAGES-1, TRUE, 0);
2246 if (kr != KERN_SUCCESS) {
2247 new_mapping_established = TRUE; /* will cause deallocation of whole range */
2248 RETURN(kr);
2249 }
2250
2251 /* create one vm_object per superpage */
2252 sp_object = vm_object_allocate((vm_map_size_t)(entry->vme_end - entry->vme_start));
2253 sp_object->phys_contiguous = TRUE;
2254 sp_object->vo_shadow_offset = (vm_object_offset_t)pages->phys_page*PAGE_SIZE;
2255 entry->object.vm_object = sp_object;
2256 assert(entry->use_pmap);
2257
2258 /* enter the base pages into the object */
2259 vm_object_lock(sp_object);
2260 for (offset = 0; offset < SUPERPAGE_SIZE; offset += PAGE_SIZE) {
2261 m = pages;
2262 pmap_zero_page(m->phys_page);
2263 pages = NEXT_PAGE(m);
2264 *(NEXT_PAGE_PTR(m)) = VM_PAGE_NULL;
2265 vm_page_insert(m, sp_object, offset);
2266 }
2267 vm_object_unlock(sp_object);
2268 }
2269 } while (tmp_end != tmp2_end &&
2270 (tmp_start = tmp_end) &&
2271 (tmp_end = (tmp2_end - tmp_end > (vm_map_size_t)ANON_CHUNK_SIZE) ?
2272 tmp_end + (vm_map_size_t)ANON_CHUNK_SIZE : tmp2_end));
2273 }
2274
2275 new_mapping_established = TRUE;
2276
2277BailOut:
2278 assert(map_locked == TRUE);
2279
2280 if (result == KERN_SUCCESS) {
2281 vm_prot_t pager_prot;
2282 memory_object_t pager;
2283
2284#if DEBUG
2285 if (pmap_empty &&
2286 !(flags & VM_FLAGS_NO_PMAP_CHECK)) {
2287 assert(vm_map_pmap_is_empty(map,
2288 *address,
2289 *address+size));
2290 }
2291#endif /* DEBUG */
2292
2293 /*
2294 * For "named" VM objects, let the pager know that the
2295 * memory object is being mapped. Some pagers need to keep
2296 * track of this, to know when they can reclaim the memory
2297 * object, for example.
2298 * VM calls memory_object_map() for each mapping (specifying
2299 * the protection of each mapping) and calls
2300 * memory_object_last_unmap() when all the mappings are gone.
2301 */
2302 pager_prot = max_protection;
2303 if (needs_copy) {
2304 /*
2305 * Copy-On-Write mapping: won't modify
2306 * the memory object.
2307 */
2308 pager_prot &= ~VM_PROT_WRITE;
2309 }
2310 if (!is_submap &&
2311 object != VM_OBJECT_NULL &&
2312 object->named &&
2313 object->pager != MEMORY_OBJECT_NULL) {
2314 vm_object_lock(object);
2315 pager = object->pager;
2316 if (object->named &&
2317 pager != MEMORY_OBJECT_NULL) {
2318 assert(object->pager_ready);
2319 vm_object_mapping_wait(object, THREAD_UNINT);
2320 vm_object_mapping_begin(object);
2321 vm_object_unlock(object);
2322
2323 kr = memory_object_map(pager, pager_prot);
2324 assert(kr == KERN_SUCCESS);
2325
2326 vm_object_lock(object);
2327 vm_object_mapping_end(object);
2328 }
2329 vm_object_unlock(object);
2330 }
2331 }
2332
2333 assert(map_locked == TRUE);
2334
2335 if (!keep_map_locked) {
2336 vm_map_unlock(map);
2337 map_locked = FALSE;
2338 }
2339
2340 /*
2341 * We can't hold the map lock if we enter this block.
2342 */
2343
2344 if (result == KERN_SUCCESS) {
2345
2346 /* Wire down the new entry if the user
2347 * requested all new map entries be wired.
2348 */
2349 if ((map->wiring_required)||(superpage_size)) {
2350 assert(!keep_map_locked);
2351 pmap_empty = FALSE; /* pmap won't be empty */
2352 kr = vm_map_wire(map, start, end,
2353 new_entry->protection, TRUE);
2354 result = kr;
2355 }
2356
2357 }
2358
2359 if (result != KERN_SUCCESS) {
2360 if (new_mapping_established) {
2361 /*
2362 * We have to get rid of the new mappings since we
2363 * won't make them available to the user.
2364 * Try and do that atomically, to minimize the risk
2365 * that someone else create new mappings that range.
2366 */
2367 zap_new_map = vm_map_create(PMAP_NULL,
2368 *address,
2369 *address + size,
2370 map->hdr.entries_pageable);
2371 vm_map_set_page_shift(zap_new_map,
2372 VM_MAP_PAGE_SHIFT(map));
2373 if (!map_locked) {
2374 vm_map_lock(map);
2375 map_locked = TRUE;
2376 }
2377 (void) vm_map_delete(map, *address, *address+size,
2378 (VM_MAP_REMOVE_SAVE_ENTRIES |
2379 VM_MAP_REMOVE_NO_MAP_ALIGN),
2380 zap_new_map);
2381 }
2382 if (zap_old_map != VM_MAP_NULL &&
2383 zap_old_map->hdr.nentries != 0) {
2384 vm_map_entry_t entry1, entry2;
2385
2386 /*
2387 * The new mapping failed. Attempt to restore
2388 * the old mappings, saved in the "zap_old_map".
2389 */
2390 if (!map_locked) {
2391 vm_map_lock(map);
2392 map_locked = TRUE;
2393 }
2394
2395 /* first check if the coast is still clear */
2396 start = vm_map_first_entry(zap_old_map)->vme_start;
2397 end = vm_map_last_entry(zap_old_map)->vme_end;
2398 if (vm_map_lookup_entry(map, start, &entry1) ||
2399 vm_map_lookup_entry(map, end, &entry2) ||
2400 entry1 != entry2) {
2401 /*
2402 * Part of that range has already been
2403 * re-mapped: we can't restore the old
2404 * mappings...
2405 */
2406 vm_map_enter_restore_failures++;
2407 } else {
2408 /*
2409 * Transfer the saved map entries from
2410 * "zap_old_map" to the original "map",
2411 * inserting them all after "entry1".
2412 */
2413 for (entry2 = vm_map_first_entry(zap_old_map);
2414 entry2 != vm_map_to_entry(zap_old_map);
2415 entry2 = vm_map_first_entry(zap_old_map)) {
2416 vm_map_size_t entry_size;
2417
2418 entry_size = (entry2->vme_end -
2419 entry2->vme_start);
2420 vm_map_store_entry_unlink(zap_old_map,
2421 entry2);
2422 zap_old_map->size -= entry_size;
2423 vm_map_store_entry_link(map, entry1, entry2);
2424 map->size += entry_size;
2425 entry1 = entry2;
2426 }
2427 if (map->wiring_required) {
2428 /*
2429 * XXX TODO: we should rewire the
2430 * old pages here...
2431 */
2432 }
2433 vm_map_enter_restore_successes++;
2434 }
2435 }
2436 }
2437
2438 /*
2439 * The caller is responsible for releasing the lock if it requested to
2440 * keep the map locked.
2441 */
2442 if (map_locked && !keep_map_locked) {
2443 vm_map_unlock(map);
2444 }
2445
2446 /*
2447 * Get rid of the "zap_maps" and all the map entries that
2448 * they may still contain.
2449 */
2450 if (zap_old_map != VM_MAP_NULL) {
2451 vm_map_destroy(zap_old_map, VM_MAP_REMOVE_NO_PMAP_CLEANUP);
2452 zap_old_map = VM_MAP_NULL;
2453 }
2454 if (zap_new_map != VM_MAP_NULL) {
2455 vm_map_destroy(zap_new_map, VM_MAP_REMOVE_NO_PMAP_CLEANUP);
2456 zap_new_map = VM_MAP_NULL;
2457 }
2458
2459 return result;
2460
2461#undef RETURN
2462}
2463
2464/*
2465 * Counters for the prefault optimization.
2466 */
2467int64_t vm_prefault_nb_pages = 0;
2468int64_t vm_prefault_nb_bailout = 0;
2469
2470static kern_return_t
2471vm_map_enter_mem_object_helper(
2472 vm_map_t target_map,
2473 vm_map_offset_t *address,
2474 vm_map_size_t initial_size,
2475 vm_map_offset_t mask,
2476 int flags,
2477 ipc_port_t port,
2478 vm_object_offset_t offset,
2479 boolean_t copy,
2480 vm_prot_t cur_protection,
2481 vm_prot_t max_protection,
2482 vm_inherit_t inheritance,
2483 upl_page_list_ptr_t page_list,
2484 unsigned int page_list_count)
2485{
2486 vm_map_address_t map_addr;
2487 vm_map_size_t map_size;
2488 vm_object_t object;
2489 vm_object_size_t size;
2490 kern_return_t result;
2491 boolean_t mask_cur_protection, mask_max_protection;
2492 boolean_t try_prefault = (page_list_count != 0);
2493 vm_map_offset_t offset_in_mapping;
2494
2495 mask_cur_protection = cur_protection & VM_PROT_IS_MASK;
2496 mask_max_protection = max_protection & VM_PROT_IS_MASK;
2497 cur_protection &= ~VM_PROT_IS_MASK;
2498 max_protection &= ~VM_PROT_IS_MASK;
2499
2500 /*
2501 * Check arguments for validity
2502 */
2503 if ((target_map == VM_MAP_NULL) ||
2504 (cur_protection & ~VM_PROT_ALL) ||
2505 (max_protection & ~VM_PROT_ALL) ||
2506 (inheritance > VM_INHERIT_LAST_VALID) ||
2507 (try_prefault && (copy || !page_list)) ||
2508 initial_size == 0)
2509 return KERN_INVALID_ARGUMENT;
2510
2511 map_addr = vm_map_trunc_page(*address,
2512 VM_MAP_PAGE_MASK(target_map));
2513 map_size = vm_map_round_page(initial_size,
2514 VM_MAP_PAGE_MASK(target_map));
2515 size = vm_object_round_page(initial_size);
2516
2517 /*
2518 * Find the vm object (if any) corresponding to this port.
2519 */
2520 if (!IP_VALID(port)) {
2521 object = VM_OBJECT_NULL;
2522 offset = 0;
2523 copy = FALSE;
2524 } else if (ip_kotype(port) == IKOT_NAMED_ENTRY) {
2525 vm_named_entry_t named_entry;
2526
2527 named_entry = (vm_named_entry_t) port->ip_kobject;
2528
2529 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
2530 offset += named_entry->data_offset;
2531 }
2532
2533 /* a few checks to make sure user is obeying rules */
2534 if (size == 0) {
2535 if (offset >= named_entry->size)
2536 return KERN_INVALID_RIGHT;
2537 size = named_entry->size - offset;
2538 }
2539 if (mask_max_protection) {
2540 max_protection &= named_entry->protection;
2541 }
2542 if (mask_cur_protection) {
2543 cur_protection &= named_entry->protection;
2544 }
2545 if ((named_entry->protection & max_protection) !=
2546 max_protection)
2547 return KERN_INVALID_RIGHT;
2548 if ((named_entry->protection & cur_protection) !=
2549 cur_protection)
2550 return KERN_INVALID_RIGHT;
2551 if (offset + size < offset) {
2552 /* overflow */
2553 return KERN_INVALID_ARGUMENT;
2554 }
2555 if (named_entry->size < (offset + size))
2556 return KERN_INVALID_ARGUMENT;
2557
2558 if (named_entry->is_copy) {
2559 /* for a vm_map_copy, we can only map it whole */
2560 if ((size != named_entry->size) &&
2561 (vm_map_round_page(size,
2562 VM_MAP_PAGE_MASK(target_map)) ==
2563 named_entry->size)) {
2564 /* XXX FBDP use the rounded size... */
2565 size = vm_map_round_page(
2566 size,
2567 VM_MAP_PAGE_MASK(target_map));
2568 }
2569
2570 if (!(flags & VM_FLAGS_ANYWHERE) &&
2571 (offset != 0 ||
2572 size != named_entry->size)) {
2573 /*
2574 * XXX for a mapping at a "fixed" address,
2575 * we can't trim after mapping the whole
2576 * memory entry, so reject a request for a
2577 * partial mapping.
2578 */
2579 return KERN_INVALID_ARGUMENT;
2580 }
2581 }
2582
2583 /* the callers parameter offset is defined to be the */
2584 /* offset from beginning of named entry offset in object */
2585 offset = offset + named_entry->offset;
2586
2587 if (! VM_MAP_PAGE_ALIGNED(size,
2588 VM_MAP_PAGE_MASK(target_map))) {
2589 /*
2590 * Let's not map more than requested;
2591 * vm_map_enter() will handle this "not map-aligned"
2592 * case.
2593 */
2594 map_size = size;
2595 }
2596
2597 named_entry_lock(named_entry);
2598 if (named_entry->is_sub_map) {
2599 vm_map_t submap;
2600
2601 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
2602 panic("VM_FLAGS_RETURN_DATA_ADDR not expected for submap.");
2603 }
2604
2605 submap = named_entry->backing.map;
2606 vm_map_lock(submap);
2607 vm_map_reference(submap);
2608 vm_map_unlock(submap);
2609 named_entry_unlock(named_entry);
2610
2611 result = vm_map_enter(target_map,
2612 &map_addr,
2613 map_size,
2614 mask,
2615 flags | VM_FLAGS_SUBMAP,
2616 (vm_object_t) submap,
2617 offset,
2618 copy,
2619 cur_protection,
2620 max_protection,
2621 inheritance);
2622 if (result != KERN_SUCCESS) {
2623 vm_map_deallocate(submap);
2624 } else {
2625 /*
2626 * No need to lock "submap" just to check its
2627 * "mapped" flag: that flag is never reset
2628 * once it's been set and if we race, we'll
2629 * just end up setting it twice, which is OK.
2630 */
2631 if (submap->mapped_in_other_pmaps == FALSE &&
2632 vm_map_pmap(submap) != PMAP_NULL &&
2633 vm_map_pmap(submap) !=
2634 vm_map_pmap(target_map)) {
2635 /*
2636 * This submap is being mapped in a map
2637 * that uses a different pmap.
2638 * Set its "mapped_in_other_pmaps" flag
2639 * to indicate that we now need to
2640 * remove mappings from all pmaps rather
2641 * than just the submap's pmap.
2642 */
2643 vm_map_lock(submap);
2644 submap->mapped_in_other_pmaps = TRUE;
2645 vm_map_unlock(submap);
2646 }
2647 *address = map_addr;
2648 }
2649 return result;
2650
2651 } else if (named_entry->is_pager) {
2652 unsigned int access;
2653 vm_prot_t protections;
2654 unsigned int wimg_mode;
2655
2656 protections = named_entry->protection & VM_PROT_ALL;
2657 access = GET_MAP_MEM(named_entry->protection);
2658
2659 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
2660 panic("VM_FLAGS_RETURN_DATA_ADDR not expected for submap.");
2661 }
2662
2663 object = vm_object_enter(named_entry->backing.pager,
2664 named_entry->size,
2665 named_entry->internal,
2666 FALSE,
2667 FALSE);
2668 if (object == VM_OBJECT_NULL) {
2669 named_entry_unlock(named_entry);
2670 return KERN_INVALID_OBJECT;
2671 }
2672
2673 /* JMM - drop reference on pager here */
2674
2675 /* create an extra ref for the named entry */
2676 vm_object_lock(object);
2677 vm_object_reference_locked(object);
2678 named_entry->backing.object = object;
2679 named_entry->is_pager = FALSE;
2680 named_entry_unlock(named_entry);
2681
2682 wimg_mode = object->wimg_bits;
2683
2684 if (access == MAP_MEM_IO) {
2685 wimg_mode = VM_WIMG_IO;
2686 } else if (access == MAP_MEM_COPYBACK) {
2687 wimg_mode = VM_WIMG_USE_DEFAULT;
2688 } else if (access == MAP_MEM_INNERWBACK) {
2689 wimg_mode = VM_WIMG_INNERWBACK;
2690 } else if (access == MAP_MEM_WTHRU) {
2691 wimg_mode = VM_WIMG_WTHRU;
2692 } else if (access == MAP_MEM_WCOMB) {
2693 wimg_mode = VM_WIMG_WCOMB;
2694 }
2695
2696 /* wait for object (if any) to be ready */
2697 if (!named_entry->internal) {
2698 while (!object->pager_ready) {
2699 vm_object_wait(
2700 object,
2701 VM_OBJECT_EVENT_PAGER_READY,
2702 THREAD_UNINT);
2703 vm_object_lock(object);
2704 }
2705 }
2706
2707 if (object->wimg_bits != wimg_mode)
2708 vm_object_change_wimg_mode(object, wimg_mode);
2709
2710#if VM_OBJECT_TRACKING_OP_TRUESHARE
2711 if (!object->true_share &&
2712 vm_object_tracking_inited) {
2713 void *bt[VM_OBJECT_TRACKING_BTDEPTH];
2714 int num = 0;
2715
2716 num = OSBacktrace(bt,
2717 VM_OBJECT_TRACKING_BTDEPTH);
2718 btlog_add_entry(vm_object_tracking_btlog,
2719 object,
2720 VM_OBJECT_TRACKING_OP_TRUESHARE,
2721 bt,
2722 num);
2723 }
2724#endif /* VM_OBJECT_TRACKING_OP_TRUESHARE */
2725
2726 object->true_share = TRUE;
2727
2728 if (object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC)
2729 object->copy_strategy = MEMORY_OBJECT_COPY_DELAY;
2730 vm_object_unlock(object);
2731
2732 } else if (named_entry->is_copy) {
2733 kern_return_t kr;
2734 vm_map_copy_t copy_map;
2735 vm_map_entry_t copy_entry;
2736 vm_map_offset_t copy_addr;
2737
2738 if (flags & ~(VM_FLAGS_FIXED |
2739 VM_FLAGS_ANYWHERE |
2740 VM_FLAGS_OVERWRITE |
2741 VM_FLAGS_RETURN_DATA_ADDR)) {
2742 named_entry_unlock(named_entry);
2743 return KERN_INVALID_ARGUMENT;
2744 }
2745
2746 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
2747 offset_in_mapping = offset - vm_object_trunc_page(offset);
2748 offset = vm_object_trunc_page(offset);
2749 map_size = vm_object_round_page(offset + offset_in_mapping + initial_size) - offset;
2750 }
2751
2752 copy_map = named_entry->backing.copy;
2753 assert(copy_map->type == VM_MAP_COPY_ENTRY_LIST);
2754 if (copy_map->type != VM_MAP_COPY_ENTRY_LIST) {
2755 /* unsupported type; should not happen */
2756 printf("vm_map_enter_mem_object: "
2757 "memory_entry->backing.copy "
2758 "unsupported type 0x%x\n",
2759 copy_map->type);
2760 named_entry_unlock(named_entry);
2761 return KERN_INVALID_ARGUMENT;
2762 }
2763
2764 /* reserve a contiguous range */
2765 kr = vm_map_enter(target_map,
2766 &map_addr,
2767 /* map whole mem entry, trim later: */
2768 named_entry->size,
2769 mask,
2770 flags & (VM_FLAGS_ANYWHERE |
2771 VM_FLAGS_OVERWRITE |
2772 VM_FLAGS_RETURN_DATA_ADDR),
2773 VM_OBJECT_NULL,
2774 0,
2775 FALSE, /* copy */
2776 cur_protection,
2777 max_protection,
2778 inheritance);
2779 if (kr != KERN_SUCCESS) {
2780 named_entry_unlock(named_entry);
2781 return kr;
2782 }
2783
2784 copy_addr = map_addr;
2785
2786 for (copy_entry = vm_map_copy_first_entry(copy_map);
2787 copy_entry != vm_map_copy_to_entry(copy_map);
2788 copy_entry = copy_entry->vme_next) {
2789 int remap_flags = 0;
2790 vm_map_t copy_submap;
2791 vm_object_t copy_object;
2792 vm_map_size_t copy_size;
2793 vm_object_offset_t copy_offset;
2794
2795 copy_offset = copy_entry->offset;
2796 copy_size = (copy_entry->vme_end -
2797 copy_entry->vme_start);
2798
2799 /* sanity check */
2800 if ((copy_addr + copy_size) >
2801 (map_addr +
2802 named_entry->size /* XXX full size */ )) {
2803 /* over-mapping too much !? */
2804 kr = KERN_INVALID_ARGUMENT;
2805 /* abort */
2806 break;
2807 }
2808
2809 /* take a reference on the object */
2810 if (copy_entry->is_sub_map) {
2811 remap_flags |= VM_FLAGS_SUBMAP;
2812 copy_submap =
2813 copy_entry->object.sub_map;
2814 vm_map_lock(copy_submap);
2815 vm_map_reference(copy_submap);
2816 vm_map_unlock(copy_submap);
2817 copy_object = (vm_object_t) copy_submap;
2818 } else {
2819 copy_object =
2820 copy_entry->object.vm_object;
2821 vm_object_reference(copy_object);
2822 }
2823
2824 /* over-map the object into destination */
2825 remap_flags |= flags;
2826 remap_flags |= VM_FLAGS_FIXED;
2827 remap_flags |= VM_FLAGS_OVERWRITE;
2828 remap_flags &= ~VM_FLAGS_ANYWHERE;
2829 kr = vm_map_enter(target_map,
2830 &copy_addr,
2831 copy_size,
2832 (vm_map_offset_t) 0,
2833 remap_flags,
2834 copy_object,
2835 copy_offset,
2836 copy,
2837 cur_protection,
2838 max_protection,
2839 inheritance);
2840 if (kr != KERN_SUCCESS) {
2841 if (copy_entry->is_sub_map) {
2842 vm_map_deallocate(copy_submap);
2843 } else {
2844 vm_object_deallocate(copy_object);
2845 }
2846 /* abort */
2847 break;
2848 }
2849
2850 /* next mapping */
2851 copy_addr += copy_size;
2852 }
2853
2854 if (kr == KERN_SUCCESS) {
2855 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
2856 *address = map_addr + offset_in_mapping;
2857 } else {
2858 *address = map_addr;
2859 }
2860
2861 if (offset) {
2862 /*
2863 * Trim in front, from 0 to "offset".
2864 */
2865 vm_map_remove(target_map,
2866 map_addr,
2867 map_addr + offset,
2868 0);
2869 *address += offset;
2870 }
2871 if (offset + map_size < named_entry->size) {
2872 /*
2873 * Trim in back, from
2874 * "offset + map_size" to
2875 * "named_entry->size".
2876 */
2877 vm_map_remove(target_map,
2878 (map_addr +
2879 offset + map_size),
2880 (map_addr +
2881 named_entry->size),
2882 0);
2883 }
2884 }
2885 named_entry_unlock(named_entry);
2886
2887 if (kr != KERN_SUCCESS) {
2888 if (! (flags & VM_FLAGS_OVERWRITE)) {
2889 /* deallocate the contiguous range */
2890 (void) vm_deallocate(target_map,
2891 map_addr,
2892 map_size);
2893 }
2894 }
2895
2896 return kr;
2897
2898 } else {
2899 /* This is the case where we are going to map */
2900 /* an already mapped object. If the object is */
2901 /* not ready it is internal. An external */
2902 /* object cannot be mapped until it is ready */
2903 /* we can therefore avoid the ready check */
2904 /* in this case. */
2905 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
2906 offset_in_mapping = offset - vm_object_trunc_page(offset);
2907 offset = vm_object_trunc_page(offset);
2908 map_size = vm_object_round_page(offset + offset_in_mapping + initial_size) - offset;
2909 }
2910
2911 object = named_entry->backing.object;
2912 assert(object != VM_OBJECT_NULL);
2913 named_entry_unlock(named_entry);
2914 vm_object_reference(object);
2915 }
2916 } else if (ip_kotype(port) == IKOT_MEMORY_OBJECT) {
2917 /*
2918 * JMM - This is temporary until we unify named entries
2919 * and raw memory objects.
2920 *
2921 * Detected fake ip_kotype for a memory object. In
2922 * this case, the port isn't really a port at all, but
2923 * instead is just a raw memory object.
2924 */
2925 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
2926 panic("VM_FLAGS_RETURN_DATA_ADDR not expected for raw memory object.");
2927 }
2928
2929 object = vm_object_enter((memory_object_t)port,
2930 size, FALSE, FALSE, FALSE);
2931 if (object == VM_OBJECT_NULL)
2932 return KERN_INVALID_OBJECT;
2933
2934 /* wait for object (if any) to be ready */
2935 if (object != VM_OBJECT_NULL) {
2936 if (object == kernel_object) {
2937 printf("Warning: Attempt to map kernel object"
2938 " by a non-private kernel entity\n");
2939 return KERN_INVALID_OBJECT;
2940 }
2941 if (!object->pager_ready) {
2942 vm_object_lock(object);
2943
2944 while (!object->pager_ready) {
2945 vm_object_wait(object,
2946 VM_OBJECT_EVENT_PAGER_READY,
2947 THREAD_UNINT);
2948 vm_object_lock(object);
2949 }
2950 vm_object_unlock(object);
2951 }
2952 }
2953 } else {
2954 return KERN_INVALID_OBJECT;
2955 }
2956
2957 if (object != VM_OBJECT_NULL &&
2958 object->named &&
2959 object->pager != MEMORY_OBJECT_NULL &&
2960 object->copy_strategy != MEMORY_OBJECT_COPY_NONE) {
2961 memory_object_t pager;
2962 vm_prot_t pager_prot;
2963 kern_return_t kr;
2964
2965 /*
2966 * For "named" VM objects, let the pager know that the
2967 * memory object is being mapped. Some pagers need to keep
2968 * track of this, to know when they can reclaim the memory
2969 * object, for example.
2970 * VM calls memory_object_map() for each mapping (specifying
2971 * the protection of each mapping) and calls
2972 * memory_object_last_unmap() when all the mappings are gone.
2973 */
2974 pager_prot = max_protection;
2975 if (copy) {
2976 /*
2977 * Copy-On-Write mapping: won't modify the
2978 * memory object.
2979 */
2980 pager_prot &= ~VM_PROT_WRITE;
2981 }
2982 vm_object_lock(object);
2983 pager = object->pager;
2984 if (object->named &&
2985 pager != MEMORY_OBJECT_NULL &&
2986 object->copy_strategy != MEMORY_OBJECT_COPY_NONE) {
2987 assert(object->pager_ready);
2988 vm_object_mapping_wait(object, THREAD_UNINT);
2989 vm_object_mapping_begin(object);
2990 vm_object_unlock(object);
2991
2992 kr = memory_object_map(pager, pager_prot);
2993 assert(kr == KERN_SUCCESS);
2994
2995 vm_object_lock(object);
2996 vm_object_mapping_end(object);
2997 }
2998 vm_object_unlock(object);
2999 }
3000
3001 /*
3002 * Perform the copy if requested
3003 */
3004
3005 if (copy) {
3006 vm_object_t new_object;
3007 vm_object_offset_t new_offset;
3008
3009 result = vm_object_copy_strategically(object, offset, size,
3010 &new_object, &new_offset,
3011 &copy);
3012
3013
3014 if (result == KERN_MEMORY_RESTART_COPY) {
3015 boolean_t success;
3016 boolean_t src_needs_copy;
3017
3018 /*
3019 * XXX
3020 * We currently ignore src_needs_copy.
3021 * This really is the issue of how to make
3022 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
3023 * non-kernel users to use. Solution forthcoming.
3024 * In the meantime, since we don't allow non-kernel
3025 * memory managers to specify symmetric copy,
3026 * we won't run into problems here.
3027 */
3028 new_object = object;
3029 new_offset = offset;
3030 success = vm_object_copy_quickly(&new_object,
3031 new_offset, size,
3032 &src_needs_copy,
3033 &copy);
3034 assert(success);
3035 result = KERN_SUCCESS;
3036 }
3037 /*
3038 * Throw away the reference to the
3039 * original object, as it won't be mapped.
3040 */
3041
3042 vm_object_deallocate(object);
3043
3044 if (result != KERN_SUCCESS)
3045 return result;
3046
3047 object = new_object;
3048 offset = new_offset;
3049 }
3050
3051 /*
3052 * If users want to try to prefault pages, the mapping and prefault
3053 * needs to be atomic.
3054 */
3055 if (try_prefault)
3056 flags |= VM_FLAGS_KEEP_MAP_LOCKED;
3057 result = vm_map_enter(target_map,
3058 &map_addr, map_size,
3059 (vm_map_offset_t)mask,
3060 flags,
3061 object, offset,
3062 copy,
3063 cur_protection, max_protection, inheritance);
3064 if (result != KERN_SUCCESS)
3065 vm_object_deallocate(object);
3066
3067 /*
3068 * Try to prefault, and do not forget to release the vm map lock.
3069 */
3070 if (result == KERN_SUCCESS && try_prefault) {
3071 mach_vm_address_t va = map_addr;
3072 kern_return_t kr = KERN_SUCCESS;
3073 unsigned int i = 0;
3074
3075 for (i = 0; i < page_list_count; ++i) {
3076 if (UPL_VALID_PAGE(page_list, i)) {
3077 /*
3078 * If this function call failed, we should stop
3079 * trying to optimize, other calls are likely
3080 * going to fail too.
3081 *
3082 * We are not gonna report an error for such
3083 * failure though. That's an optimization, not
3084 * something critical.
3085 */
3086 kr = pmap_enter_options(target_map->pmap,
3087 va, UPL_PHYS_PAGE(page_list, i),
3088 cur_protection, VM_PROT_NONE,
3089 0, TRUE, PMAP_OPTIONS_NOWAIT, NULL);
3090 if (kr != KERN_SUCCESS) {
3091 OSIncrementAtomic64(&vm_prefault_nb_bailout);
3092 goto BailOut;
3093 }
3094 OSIncrementAtomic64(&vm_prefault_nb_pages);
3095 }
3096
3097 /* Next virtual address */
3098 va += PAGE_SIZE;
3099 }
3100BailOut:
3101 vm_map_unlock(target_map);
3102 }
3103
3104 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
3105 *address = map_addr + offset_in_mapping;
3106 } else {
3107 *address = map_addr;
3108 }
3109 return result;
3110}
3111
3112kern_return_t
3113vm_map_enter_mem_object(
3114 vm_map_t target_map,
3115 vm_map_offset_t *address,
3116 vm_map_size_t initial_size,
3117 vm_map_offset_t mask,
3118 int flags,
3119 ipc_port_t port,
3120 vm_object_offset_t offset,
3121 boolean_t copy,
3122 vm_prot_t cur_protection,
3123 vm_prot_t max_protection,
3124 vm_inherit_t inheritance)
3125{
3126 return vm_map_enter_mem_object_helper(target_map, address, initial_size, mask, flags,
3127 port, offset, copy, cur_protection, max_protection,
3128 inheritance, NULL, 0);
3129}
3130
3131kern_return_t
3132vm_map_enter_mem_object_prefault(
3133 vm_map_t target_map,
3134 vm_map_offset_t *address,
3135 vm_map_size_t initial_size,
3136 vm_map_offset_t mask,
3137 int flags,
3138 ipc_port_t port,
3139 vm_object_offset_t offset,
3140 vm_prot_t cur_protection,
3141 vm_prot_t max_protection,
3142 upl_page_list_ptr_t page_list,
3143 unsigned int page_list_count)
3144{
3145 return vm_map_enter_mem_object_helper(target_map, address, initial_size, mask, flags,
3146 port, offset, FALSE, cur_protection, max_protection,
3147 VM_INHERIT_DEFAULT, page_list, page_list_count);
3148}
3149
3150
3151kern_return_t
3152vm_map_enter_mem_object_control(
3153 vm_map_t target_map,
3154 vm_map_offset_t *address,
3155 vm_map_size_t initial_size,
3156 vm_map_offset_t mask,
3157 int flags,
3158 memory_object_control_t control,
3159 vm_object_offset_t offset,
3160 boolean_t copy,
3161 vm_prot_t cur_protection,
3162 vm_prot_t max_protection,
3163 vm_inherit_t inheritance)
3164{
3165 vm_map_address_t map_addr;
3166 vm_map_size_t map_size;
3167 vm_object_t object;
3168 vm_object_size_t size;
3169 kern_return_t result;
3170 memory_object_t pager;
3171 vm_prot_t pager_prot;
3172 kern_return_t kr;
3173
3174 /*
3175 * Check arguments for validity
3176 */
3177 if ((target_map == VM_MAP_NULL) ||
3178 (cur_protection & ~VM_PROT_ALL) ||
3179 (max_protection & ~VM_PROT_ALL) ||
3180 (inheritance > VM_INHERIT_LAST_VALID) ||
3181 initial_size == 0)
3182 return KERN_INVALID_ARGUMENT;
3183
3184 map_addr = vm_map_trunc_page(*address,
3185 VM_MAP_PAGE_MASK(target_map));
3186 map_size = vm_map_round_page(initial_size,
3187 VM_MAP_PAGE_MASK(target_map));
3188 size = vm_object_round_page(initial_size);
3189
3190 object = memory_object_control_to_vm_object(control);
3191
3192 if (object == VM_OBJECT_NULL)
3193 return KERN_INVALID_OBJECT;
3194
3195 if (object == kernel_object) {
3196 printf("Warning: Attempt to map kernel object"
3197 " by a non-private kernel entity\n");
3198 return KERN_INVALID_OBJECT;
3199 }
3200
3201 vm_object_lock(object);
3202 object->ref_count++;
3203 vm_object_res_reference(object);
3204
3205 /*
3206 * For "named" VM objects, let the pager know that the
3207 * memory object is being mapped. Some pagers need to keep
3208 * track of this, to know when they can reclaim the memory
3209 * object, for example.
3210 * VM calls memory_object_map() for each mapping (specifying
3211 * the protection of each mapping) and calls
3212 * memory_object_last_unmap() when all the mappings are gone.
3213 */
3214 pager_prot = max_protection;
3215 if (copy) {
3216 pager_prot &= ~VM_PROT_WRITE;
3217 }
3218 pager = object->pager;
3219 if (object->named &&
3220 pager != MEMORY_OBJECT_NULL &&
3221 object->copy_strategy != MEMORY_OBJECT_COPY_NONE) {
3222 assert(object->pager_ready);
3223 vm_object_mapping_wait(object, THREAD_UNINT);
3224 vm_object_mapping_begin(object);
3225 vm_object_unlock(object);
3226
3227 kr = memory_object_map(pager, pager_prot);
3228 assert(kr == KERN_SUCCESS);
3229
3230 vm_object_lock(object);
3231 vm_object_mapping_end(object);
3232 }
3233 vm_object_unlock(object);
3234
3235 /*
3236 * Perform the copy if requested
3237 */
3238
3239 if (copy) {
3240 vm_object_t new_object;
3241 vm_object_offset_t new_offset;
3242
3243 result = vm_object_copy_strategically(object, offset, size,
3244 &new_object, &new_offset,
3245 &copy);
3246
3247
3248 if (result == KERN_MEMORY_RESTART_COPY) {
3249 boolean_t success;
3250 boolean_t src_needs_copy;
3251
3252 /*
3253 * XXX
3254 * We currently ignore src_needs_copy.
3255 * This really is the issue of how to make
3256 * MEMORY_OBJECT_COPY_SYMMETRIC safe for
3257 * non-kernel users to use. Solution forthcoming.
3258 * In the meantime, since we don't allow non-kernel
3259 * memory managers to specify symmetric copy,
3260 * we won't run into problems here.
3261 */
3262 new_object = object;
3263 new_offset = offset;
3264 success = vm_object_copy_quickly(&new_object,
3265 new_offset, size,
3266 &src_needs_copy,
3267 &copy);
3268 assert(success);
3269 result = KERN_SUCCESS;
3270 }
3271 /*
3272 * Throw away the reference to the
3273 * original object, as it won't be mapped.
3274 */
3275
3276 vm_object_deallocate(object);
3277
3278 if (result != KERN_SUCCESS)
3279 return result;
3280
3281 object = new_object;
3282 offset = new_offset;
3283 }
3284
3285 result = vm_map_enter(target_map,
3286 &map_addr, map_size,
3287 (vm_map_offset_t)mask,
3288 flags,
3289 object, offset,
3290 copy,
3291 cur_protection, max_protection, inheritance);
3292 if (result != KERN_SUCCESS)
3293 vm_object_deallocate(object);
3294 *address = map_addr;
3295
3296 return result;
3297}
3298
3299
3300#if VM_CPM
3301
3302#ifdef MACH_ASSERT
3303extern pmap_paddr_t avail_start, avail_end;
3304#endif
3305
3306/*
3307 * Allocate memory in the specified map, with the caveat that
3308 * the memory is physically contiguous. This call may fail
3309 * if the system can't find sufficient contiguous memory.
3310 * This call may cause or lead to heart-stopping amounts of
3311 * paging activity.
3312 *
3313 * Memory obtained from this call should be freed in the
3314 * normal way, viz., via vm_deallocate.
3315 */
3316kern_return_t
3317vm_map_enter_cpm(
3318 vm_map_t map,
3319 vm_map_offset_t *addr,
3320 vm_map_size_t size,
3321 int flags)
3322{
3323 vm_object_t cpm_obj;
3324 pmap_t pmap;
3325 vm_page_t m, pages;
3326 kern_return_t kr;
3327 vm_map_offset_t va, start, end, offset;
3328#if MACH_ASSERT
3329 vm_map_offset_t prev_addr = 0;
3330#endif /* MACH_ASSERT */
3331
3332 boolean_t anywhere = ((VM_FLAGS_ANYWHERE & flags) != 0);
3333
3334 if (size == 0) {
3335 *addr = 0;
3336 return KERN_SUCCESS;
3337 }
3338 if (anywhere)
3339 *addr = vm_map_min(map);
3340 else
3341 *addr = vm_map_trunc_page(*addr,
3342 VM_MAP_PAGE_MASK(map));
3343 size = vm_map_round_page(size,
3344 VM_MAP_PAGE_MASK(map));
3345
3346 /*
3347 * LP64todo - cpm_allocate should probably allow
3348 * allocations of >4GB, but not with the current
3349 * algorithm, so just cast down the size for now.
3350 */
3351 if (size > VM_MAX_ADDRESS)
3352 return KERN_RESOURCE_SHORTAGE;
3353 if ((kr = cpm_allocate(CAST_DOWN(vm_size_t, size),
3354 &pages, 0, 0, TRUE, flags)) != KERN_SUCCESS)
3355 return kr;
3356
3357 cpm_obj = vm_object_allocate((vm_object_size_t)size);
3358 assert(cpm_obj != VM_OBJECT_NULL);
3359 assert(cpm_obj->internal);
3360 assert(cpm_obj->vo_size == (vm_object_size_t)size);
3361 assert(cpm_obj->can_persist == FALSE);
3362 assert(cpm_obj->pager_created == FALSE);
3363 assert(cpm_obj->pageout == FALSE);
3364 assert(cpm_obj->shadow == VM_OBJECT_NULL);
3365
3366 /*
3367 * Insert pages into object.
3368 */
3369
3370 vm_object_lock(cpm_obj);
3371 for (offset = 0; offset < size; offset += PAGE_SIZE) {
3372 m = pages;
3373 pages = NEXT_PAGE(m);
3374 *(NEXT_PAGE_PTR(m)) = VM_PAGE_NULL;
3375
3376 assert(!m->gobbled);
3377 assert(!m->wanted);
3378 assert(!m->pageout);
3379 assert(!m->tabled);
3380 assert(VM_PAGE_WIRED(m));
3381 /*
3382 * ENCRYPTED SWAP:
3383 * "m" is not supposed to be pageable, so it
3384 * should not be encrypted. It wouldn't be safe
3385 * to enter it in a new VM object while encrypted.
3386 */
3387 ASSERT_PAGE_DECRYPTED(m);
3388 assert(m->busy);
3389 assert(m->phys_page>=(avail_start>>PAGE_SHIFT) && m->phys_page<=(avail_end>>PAGE_SHIFT));
3390
3391 m->busy = FALSE;
3392 vm_page_insert(m, cpm_obj, offset);
3393 }
3394 assert(cpm_obj->resident_page_count == size / PAGE_SIZE);
3395 vm_object_unlock(cpm_obj);
3396
3397 /*
3398 * Hang onto a reference on the object in case a
3399 * multi-threaded application for some reason decides
3400 * to deallocate the portion of the address space into
3401 * which we will insert this object.
3402 *
3403 * Unfortunately, we must insert the object now before
3404 * we can talk to the pmap module about which addresses
3405 * must be wired down. Hence, the race with a multi-
3406 * threaded app.
3407 */
3408 vm_object_reference(cpm_obj);
3409
3410 /*
3411 * Insert object into map.
3412 */
3413
3414 kr = vm_map_enter(
3415 map,
3416 addr,
3417 size,
3418 (vm_map_offset_t)0,
3419 flags,
3420 cpm_obj,
3421 (vm_object_offset_t)0,
3422 FALSE,
3423 VM_PROT_ALL,
3424 VM_PROT_ALL,
3425 VM_INHERIT_DEFAULT);
3426
3427 if (kr != KERN_SUCCESS) {
3428 /*
3429 * A CPM object doesn't have can_persist set,
3430 * so all we have to do is deallocate it to
3431 * free up these pages.
3432 */
3433 assert(cpm_obj->pager_created == FALSE);
3434 assert(cpm_obj->can_persist == FALSE);
3435 assert(cpm_obj->pageout == FALSE);
3436 assert(cpm_obj->shadow == VM_OBJECT_NULL);
3437 vm_object_deallocate(cpm_obj); /* kill acquired ref */
3438 vm_object_deallocate(cpm_obj); /* kill creation ref */
3439 }
3440
3441 /*
3442 * Inform the physical mapping system that the
3443 * range of addresses may not fault, so that
3444 * page tables and such can be locked down as well.
3445 */
3446 start = *addr;
3447 end = start + size;
3448 pmap = vm_map_pmap(map);
3449 pmap_pageable(pmap, start, end, FALSE);
3450
3451 /*
3452 * Enter each page into the pmap, to avoid faults.
3453 * Note that this loop could be coded more efficiently,
3454 * if the need arose, rather than looking up each page
3455 * again.
3456 */
3457 for (offset = 0, va = start; offset < size;
3458 va += PAGE_SIZE, offset += PAGE_SIZE) {
3459 int type_of_fault;
3460
3461 vm_object_lock(cpm_obj);
3462 m = vm_page_lookup(cpm_obj, (vm_object_offset_t)offset);
3463 assert(m != VM_PAGE_NULL);
3464
3465 vm_page_zero_fill(m);
3466
3467 type_of_fault = DBG_ZERO_FILL_FAULT;
3468
3469 vm_fault_enter(m, pmap, va, VM_PROT_ALL, VM_PROT_WRITE,
3470 VM_PAGE_WIRED(m), FALSE, FALSE, FALSE, 0, NULL,
3471 &type_of_fault);
3472
3473 vm_object_unlock(cpm_obj);
3474 }
3475
3476#if MACH_ASSERT
3477 /*
3478 * Verify ordering in address space.
3479 */
3480 for (offset = 0; offset < size; offset += PAGE_SIZE) {
3481 vm_object_lock(cpm_obj);
3482 m = vm_page_lookup(cpm_obj, (vm_object_offset_t)offset);
3483 vm_object_unlock(cpm_obj);
3484 if (m == VM_PAGE_NULL)
3485 panic("vm_allocate_cpm: obj %p off 0x%llx no page",
3486 cpm_obj, (uint64_t)offset);
3487 assert(m->tabled);
3488 assert(!m->busy);
3489 assert(!m->wanted);
3490 assert(!m->fictitious);
3491 assert(!m->private);
3492 assert(!m->absent);
3493 assert(!m->error);
3494 assert(!m->cleaning);
3495 assert(!m->laundry);
3496 assert(!m->precious);
3497 assert(!m->clustered);
3498 if (offset != 0) {
3499 if (m->phys_page != prev_addr + 1) {
3500 printf("start 0x%llx end 0x%llx va 0x%llx\n",
3501 (uint64_t)start, (uint64_t)end, (uint64_t)va);
3502 printf("obj %p off 0x%llx\n", cpm_obj, (uint64_t)offset);
3503 printf("m %p prev_address 0x%llx\n", m, (uint64_t)prev_addr);
3504 panic("vm_allocate_cpm: pages not contig!");
3505 }
3506 }
3507 prev_addr = m->phys_page;
3508 }
3509#endif /* MACH_ASSERT */
3510
3511 vm_object_deallocate(cpm_obj); /* kill extra ref */
3512
3513 return kr;
3514}
3515
3516
3517#else /* VM_CPM */
3518
3519/*
3520 * Interface is defined in all cases, but unless the kernel
3521 * is built explicitly for this option, the interface does
3522 * nothing.
3523 */
3524
3525kern_return_t
3526vm_map_enter_cpm(
3527 __unused vm_map_t map,
3528 __unused vm_map_offset_t *addr,
3529 __unused vm_map_size_t size,
3530 __unused int flags)
3531{
3532 return KERN_FAILURE;
3533}
3534#endif /* VM_CPM */
3535
3536/* Not used without nested pmaps */
3537#ifndef NO_NESTED_PMAP
3538/*
3539 * Clip and unnest a portion of a nested submap mapping.
3540 */
3541
3542
3543static void
3544vm_map_clip_unnest(
3545 vm_map_t map,
3546 vm_map_entry_t entry,
3547 vm_map_offset_t start_unnest,
3548 vm_map_offset_t end_unnest)
3549{
3550 vm_map_offset_t old_start_unnest = start_unnest;
3551 vm_map_offset_t old_end_unnest = end_unnest;
3552
3553 assert(entry->is_sub_map);
3554 assert(entry->object.sub_map != NULL);
3555 assert(entry->use_pmap);
3556
3557 /*
3558 * Query the platform for the optimal unnest range.
3559 * DRK: There's some duplication of effort here, since
3560 * callers may have adjusted the range to some extent. This
3561 * routine was introduced to support 1GiB subtree nesting
3562 * for x86 platforms, which can also nest on 2MiB boundaries
3563 * depending on size/alignment.
3564 */
3565 if (pmap_adjust_unnest_parameters(map->pmap, &start_unnest, &end_unnest)) {
3566 log_unnest_badness(map, old_start_unnest, old_end_unnest);
3567 }
3568
3569 if (entry->vme_start > start_unnest ||
3570 entry->vme_end < end_unnest) {
3571 panic("vm_map_clip_unnest(0x%llx,0x%llx): "
3572 "bad nested entry: start=0x%llx end=0x%llx\n",
3573 (long long)start_unnest, (long long)end_unnest,
3574 (long long)entry->vme_start, (long long)entry->vme_end);
3575 }
3576
3577 if (start_unnest > entry->vme_start) {
3578 _vm_map_clip_start(&map->hdr,
3579 entry,
3580 start_unnest);
3581 vm_map_store_update_first_free(map, map->first_free);
3582 }
3583 if (entry->vme_end > end_unnest) {
3584 _vm_map_clip_end(&map->hdr,
3585 entry,
3586 end_unnest);
3587 vm_map_store_update_first_free(map, map->first_free);
3588 }
3589
3590 pmap_unnest(map->pmap,
3591 entry->vme_start,
3592 entry->vme_end - entry->vme_start);
3593 if ((map->mapped_in_other_pmaps) && (map->ref_count)) {
3594 /* clean up parent map/maps */
3595 vm_map_submap_pmap_clean(
3596 map, entry->vme_start,
3597 entry->vme_end,
3598 entry->object.sub_map,
3599 entry->offset);
3600 }
3601 entry->use_pmap = FALSE;
3602 if (entry->alias == VM_MEMORY_SHARED_PMAP) {
3603 entry->alias = VM_MEMORY_UNSHARED_PMAP;
3604 }
3605}
3606#endif /* NO_NESTED_PMAP */
3607
3608/*
3609 * vm_map_clip_start: [ internal use only ]
3610 *
3611 * Asserts that the given entry begins at or after
3612 * the specified address; if necessary,
3613 * it splits the entry into two.
3614 */
3615void
3616vm_map_clip_start(
3617 vm_map_t map,
3618 vm_map_entry_t entry,
3619 vm_map_offset_t startaddr)
3620{
3621#ifndef NO_NESTED_PMAP
3622 if (entry->is_sub_map &&
3623 entry->use_pmap &&
3624 startaddr >= entry->vme_start) {
3625 vm_map_offset_t start_unnest, end_unnest;
3626
3627 /*
3628 * Make sure "startaddr" is no longer in a nested range
3629 * before we clip. Unnest only the minimum range the platform
3630 * can handle.
3631 * vm_map_clip_unnest may perform additional adjustments to
3632 * the unnest range.
3633 */
3634 start_unnest = startaddr & ~(pmap_nesting_size_min - 1);
3635 end_unnest = start_unnest + pmap_nesting_size_min;
3636 vm_map_clip_unnest(map, entry, start_unnest, end_unnest);
3637 }
3638#endif /* NO_NESTED_PMAP */
3639 if (startaddr > entry->vme_start) {
3640 if (entry->object.vm_object &&
3641 !entry->is_sub_map &&
3642 entry->object.vm_object->phys_contiguous) {
3643 pmap_remove(map->pmap,
3644 (addr64_t)(entry->vme_start),
3645 (addr64_t)(entry->vme_end));
3646 }
3647 _vm_map_clip_start(&map->hdr, entry, startaddr);
3648 vm_map_store_update_first_free(map, map->first_free);
3649 }
3650}
3651
3652
3653#define vm_map_copy_clip_start(copy, entry, startaddr) \
3654 MACRO_BEGIN \
3655 if ((startaddr) > (entry)->vme_start) \
3656 _vm_map_clip_start(&(copy)->cpy_hdr,(entry),(startaddr)); \
3657 MACRO_END
3658
3659/*
3660 * This routine is called only when it is known that
3661 * the entry must be split.
3662 */
3663static void
3664_vm_map_clip_start(
3665 register struct vm_map_header *map_header,
3666 register vm_map_entry_t entry,
3667 register vm_map_offset_t start)
3668{
3669 register vm_map_entry_t new_entry;
3670
3671 /*
3672 * Split off the front portion --
3673 * note that we must insert the new
3674 * entry BEFORE this one, so that
3675 * this entry has the specified starting
3676 * address.
3677 */
3678
3679 if (entry->map_aligned) {
3680 assert(VM_MAP_PAGE_ALIGNED(start,
3681 VM_MAP_HDR_PAGE_MASK(map_header)));
3682 }
3683
3684 new_entry = _vm_map_entry_create(map_header, !map_header->entries_pageable);
3685 vm_map_entry_copy_full(new_entry, entry);
3686
3687 new_entry->vme_end = start;
3688 assert(new_entry->vme_start < new_entry->vme_end);
3689 entry->offset += (start - entry->vme_start);
3690 assert(start < entry->vme_end);
3691 entry->vme_start = start;
3692
3693 _vm_map_store_entry_link(map_header, entry->vme_prev, new_entry);
3694
3695 if (entry->is_sub_map)
3696 vm_map_reference(new_entry->object.sub_map);
3697 else
3698 vm_object_reference(new_entry->object.vm_object);
3699}
3700
3701
3702/*
3703 * vm_map_clip_end: [ internal use only ]
3704 *
3705 * Asserts that the given entry ends at or before
3706 * the specified address; if necessary,
3707 * it splits the entry into two.
3708 */
3709void
3710vm_map_clip_end(
3711 vm_map_t map,
3712 vm_map_entry_t entry,
3713 vm_map_offset_t endaddr)
3714{
3715 if (endaddr > entry->vme_end) {
3716 /*
3717 * Within the scope of this clipping, limit "endaddr" to
3718 * the end of this map entry...
3719 */
3720 endaddr = entry->vme_end;
3721 }
3722#ifndef NO_NESTED_PMAP
3723 if (entry->is_sub_map && entry->use_pmap) {
3724 vm_map_offset_t start_unnest, end_unnest;
3725
3726 /*
3727 * Make sure the range between the start of this entry and
3728 * the new "endaddr" is no longer nested before we clip.
3729 * Unnest only the minimum range the platform can handle.
3730 * vm_map_clip_unnest may perform additional adjustments to
3731 * the unnest range.
3732 */
3733 start_unnest = entry->vme_start;
3734 end_unnest =
3735 (endaddr + pmap_nesting_size_min - 1) &
3736 ~(pmap_nesting_size_min - 1);
3737 vm_map_clip_unnest(map, entry, start_unnest, end_unnest);
3738 }
3739#endif /* NO_NESTED_PMAP */
3740 if (endaddr < entry->vme_end) {
3741 if (entry->object.vm_object &&
3742 !entry->is_sub_map &&
3743 entry->object.vm_object->phys_contiguous) {
3744 pmap_remove(map->pmap,
3745 (addr64_t)(entry->vme_start),
3746 (addr64_t)(entry->vme_end));
3747 }
3748 _vm_map_clip_end(&map->hdr, entry, endaddr);
3749 vm_map_store_update_first_free(map, map->first_free);
3750 }
3751}
3752
3753
3754#define vm_map_copy_clip_end(copy, entry, endaddr) \
3755 MACRO_BEGIN \
3756 if ((endaddr) < (entry)->vme_end) \
3757 _vm_map_clip_end(&(copy)->cpy_hdr,(entry),(endaddr)); \
3758 MACRO_END
3759
3760/*
3761 * This routine is called only when it is known that
3762 * the entry must be split.
3763 */
3764static void
3765_vm_map_clip_end(
3766 register struct vm_map_header *map_header,
3767 register vm_map_entry_t entry,
3768 register vm_map_offset_t end)
3769{
3770 register vm_map_entry_t new_entry;
3771
3772 /*
3773 * Create a new entry and insert it
3774 * AFTER the specified entry
3775 */
3776
3777 if (entry->map_aligned) {
3778 assert(VM_MAP_PAGE_ALIGNED(end,
3779 VM_MAP_HDR_PAGE_MASK(map_header)));
3780 }
3781
3782 new_entry = _vm_map_entry_create(map_header, !map_header->entries_pageable);
3783 vm_map_entry_copy_full(new_entry, entry);
3784
3785 assert(entry->vme_start < end);
3786 new_entry->vme_start = entry->vme_end = end;
3787 new_entry->offset += (end - entry->vme_start);
3788 assert(new_entry->vme_start < new_entry->vme_end);
3789
3790 _vm_map_store_entry_link(map_header, entry, new_entry);
3791
3792 if (entry->is_sub_map)
3793 vm_map_reference(new_entry->object.sub_map);
3794 else
3795 vm_object_reference(new_entry->object.vm_object);
3796}
3797
3798
3799/*
3800 * VM_MAP_RANGE_CHECK: [ internal use only ]
3801 *
3802 * Asserts that the starting and ending region
3803 * addresses fall within the valid range of the map.
3804 */
3805#define VM_MAP_RANGE_CHECK(map, start, end) \
3806 MACRO_BEGIN \
3807 if (start < vm_map_min(map)) \
3808 start = vm_map_min(map); \
3809 if (end > vm_map_max(map)) \
3810 end = vm_map_max(map); \
3811 if (start > end) \
3812 start = end; \
3813 MACRO_END
3814
3815/*
3816 * vm_map_range_check: [ internal use only ]
3817 *
3818 * Check that the region defined by the specified start and
3819 * end addresses are wholly contained within a single map
3820 * entry or set of adjacent map entries of the spacified map,
3821 * i.e. the specified region contains no unmapped space.
3822 * If any or all of the region is unmapped, FALSE is returned.
3823 * Otherwise, TRUE is returned and if the output argument 'entry'
3824 * is not NULL it points to the map entry containing the start
3825 * of the region.
3826 *
3827 * The map is locked for reading on entry and is left locked.
3828 */
3829static boolean_t
3830vm_map_range_check(
3831 register vm_map_t map,
3832 register vm_map_offset_t start,
3833 register vm_map_offset_t end,
3834 vm_map_entry_t *entry)
3835{
3836 vm_map_entry_t cur;
3837 register vm_map_offset_t prev;
3838
3839 /*
3840 * Basic sanity checks first
3841 */
3842 if (start < vm_map_min(map) || end > vm_map_max(map) || start > end)
3843 return (FALSE);
3844
3845 /*
3846 * Check first if the region starts within a valid
3847 * mapping for the map.
3848 */
3849 if (!vm_map_lookup_entry(map, start, &cur))
3850 return (FALSE);
3851
3852 /*
3853 * Optimize for the case that the region is contained
3854 * in a single map entry.
3855 */
3856 if (entry != (vm_map_entry_t *) NULL)
3857 *entry = cur;
3858 if (end <= cur->vme_end)
3859 return (TRUE);
3860
3861 /*
3862 * If the region is not wholly contained within a
3863 * single entry, walk the entries looking for holes.
3864 */
3865 prev = cur->vme_end;
3866 cur = cur->vme_next;
3867 while ((cur != vm_map_to_entry(map)) && (prev == cur->vme_start)) {
3868 if (end <= cur->vme_end)
3869 return (TRUE);
3870 prev = cur->vme_end;
3871 cur = cur->vme_next;
3872 }
3873 return (FALSE);
3874}
3875
3876/*
3877 * vm_map_submap: [ kernel use only ]
3878 *
3879 * Mark the given range as handled by a subordinate map.
3880 *
3881 * This range must have been created with vm_map_find using
3882 * the vm_submap_object, and no other operations may have been
3883 * performed on this range prior to calling vm_map_submap.
3884 *
3885 * Only a limited number of operations can be performed
3886 * within this rage after calling vm_map_submap:
3887 * vm_fault
3888 * [Don't try vm_map_copyin!]
3889 *
3890 * To remove a submapping, one must first remove the
3891 * range from the superior map, and then destroy the
3892 * submap (if desired). [Better yet, don't try it.]
3893 */
3894kern_return_t
3895vm_map_submap(
3896 vm_map_t map,
3897 vm_map_offset_t start,
3898 vm_map_offset_t end,
3899 vm_map_t submap,
3900 vm_map_offset_t offset,
3901#ifdef NO_NESTED_PMAP
3902 __unused
3903#endif /* NO_NESTED_PMAP */
3904 boolean_t use_pmap)
3905{
3906 vm_map_entry_t entry;
3907 register kern_return_t result = KERN_INVALID_ARGUMENT;
3908 register vm_object_t object;
3909
3910 vm_map_lock(map);
3911
3912 if (! vm_map_lookup_entry(map, start, &entry)) {
3913 entry = entry->vme_next;
3914 }
3915
3916 if (entry == vm_map_to_entry(map) ||
3917 entry->is_sub_map) {
3918 vm_map_unlock(map);
3919 return KERN_INVALID_ARGUMENT;
3920 }
3921
3922 vm_map_clip_start(map, entry, start);
3923 vm_map_clip_end(map, entry, end);
3924
3925 if ((entry->vme_start == start) && (entry->vme_end == end) &&
3926 (!entry->is_sub_map) &&
3927 ((object = entry->object.vm_object) == vm_submap_object) &&
3928 (object->resident_page_count == 0) &&
3929 (object->copy == VM_OBJECT_NULL) &&
3930 (object->shadow == VM_OBJECT_NULL) &&
3931 (!object->pager_created)) {
3932 entry->offset = (vm_object_offset_t)offset;
3933 entry->object.vm_object = VM_OBJECT_NULL;
3934 vm_object_deallocate(object);
3935 entry->is_sub_map = TRUE;
3936 entry->use_pmap = FALSE;
3937 entry->object.sub_map = submap;
3938 vm_map_reference(submap);
3939 if (submap->mapped_in_other_pmaps == FALSE &&
3940 vm_map_pmap(submap) != PMAP_NULL &&
3941 vm_map_pmap(submap) != vm_map_pmap(map)) {
3942 /*
3943 * This submap is being mapped in a map
3944 * that uses a different pmap.
3945 * Set its "mapped_in_other_pmaps" flag
3946 * to indicate that we now need to
3947 * remove mappings from all pmaps rather
3948 * than just the submap's pmap.
3949 */
3950 submap->mapped_in_other_pmaps = TRUE;
3951 }
3952
3953#ifndef NO_NESTED_PMAP
3954 if (use_pmap) {
3955 /* nest if platform code will allow */
3956 if(submap->pmap == NULL) {
3957 ledger_t ledger = map->pmap->ledger;
3958 submap->pmap = pmap_create(ledger,
3959 (vm_map_size_t) 0, FALSE);
3960 if(submap->pmap == PMAP_NULL) {
3961 vm_map_unlock(map);
3962 return(KERN_NO_SPACE);
3963 }
3964 }
3965 result = pmap_nest(map->pmap,
3966 (entry->object.sub_map)->pmap,
3967 (addr64_t)start,
3968 (addr64_t)start,
3969 (uint64_t)(end - start));
3970 if(result)
3971 panic("vm_map_submap: pmap_nest failed, rc = %08X\n", result);
3972 entry->use_pmap = TRUE;
3973 }
3974#else /* NO_NESTED_PMAP */
3975 pmap_remove(map->pmap, (addr64_t)start, (addr64_t)end);
3976#endif /* NO_NESTED_PMAP */
3977 result = KERN_SUCCESS;
3978 }
3979 vm_map_unlock(map);
3980
3981 return(result);
3982}
3983
3984/*
3985 * vm_map_protect:
3986 *
3987 * Sets the protection of the specified address
3988 * region in the target map. If "set_max" is
3989 * specified, the maximum protection is to be set;
3990 * otherwise, only the current protection is affected.
3991 */
3992kern_return_t
3993vm_map_protect(
3994 register vm_map_t map,
3995 register vm_map_offset_t start,
3996 register vm_map_offset_t end,
3997 register vm_prot_t new_prot,
3998 register boolean_t set_max)
3999{
4000 register vm_map_entry_t current;
4001 register vm_map_offset_t prev;
4002 vm_map_entry_t entry;
4003 vm_prot_t new_max;
4004
4005 XPR(XPR_VM_MAP,
4006 "vm_map_protect, 0x%X start 0x%X end 0x%X, new 0x%X %d",
4007 map, start, end, new_prot, set_max);
4008
4009 vm_map_lock(map);
4010
4011 /* LP64todo - remove this check when vm_map_commpage64()
4012 * no longer has to stuff in a map_entry for the commpage
4013 * above the map's max_offset.
4014 */
4015 if (start >= map->max_offset) {
4016 vm_map_unlock(map);
4017 return(KERN_INVALID_ADDRESS);
4018 }
4019
4020 while(1) {
4021 /*
4022 * Lookup the entry. If it doesn't start in a valid
4023 * entry, return an error.
4024 */
4025 if (! vm_map_lookup_entry(map, start, &entry)) {
4026 vm_map_unlock(map);
4027 return(KERN_INVALID_ADDRESS);
4028 }
4029
4030 if (entry->superpage_size && (start & (SUPERPAGE_SIZE-1))) { /* extend request to whole entry */
4031 start = SUPERPAGE_ROUND_DOWN(start);
4032 continue;
4033 }
4034 break;
4035 }
4036 if (entry->superpage_size)
4037 end = SUPERPAGE_ROUND_UP(end);
4038
4039 /*
4040 * Make a first pass to check for protection and address
4041 * violations.
4042 */
4043
4044 current = entry;
4045 prev = current->vme_start;
4046 while ((current != vm_map_to_entry(map)) &&
4047 (current->vme_start < end)) {
4048
4049 /*
4050 * If there is a hole, return an error.
4051 */
4052 if (current->vme_start != prev) {
4053 vm_map_unlock(map);
4054 return(KERN_INVALID_ADDRESS);
4055 }
4056
4057 new_max = current->max_protection;
4058 if(new_prot & VM_PROT_COPY) {
4059 new_max |= VM_PROT_WRITE;
4060 if ((new_prot & (new_max | VM_PROT_COPY)) != new_prot) {
4061 vm_map_unlock(map);
4062 return(KERN_PROTECTION_FAILURE);
4063 }
4064 } else {
4065 if ((new_prot & new_max) != new_prot) {
4066 vm_map_unlock(map);
4067 return(KERN_PROTECTION_FAILURE);
4068 }
4069 }
4070
4071
4072 prev = current->vme_end;
4073 current = current->vme_next;
4074 }
4075 if (end > prev) {
4076 vm_map_unlock(map);
4077 return(KERN_INVALID_ADDRESS);
4078 }
4079
4080 /*
4081 * Go back and fix up protections.
4082 * Clip to start here if the range starts within
4083 * the entry.
4084 */
4085
4086 current = entry;
4087 if (current != vm_map_to_entry(map)) {
4088 /* clip and unnest if necessary */
4089 vm_map_clip_start(map, current, start);
4090 }
4091
4092 while ((current != vm_map_to_entry(map)) &&
4093 (current->vme_start < end)) {
4094
4095 vm_prot_t old_prot;
4096
4097 vm_map_clip_end(map, current, end);
4098
4099 if (current->is_sub_map) {
4100 /* clipping did unnest if needed */
4101 assert(!current->use_pmap);
4102 }
4103
4104 old_prot = current->protection;
4105
4106 if(new_prot & VM_PROT_COPY) {
4107 /* caller is asking specifically to copy the */
4108 /* mapped data, this implies that max protection */
4109 /* will include write. Caller must be prepared */
4110 /* for loss of shared memory communication in the */
4111 /* target area after taking this step */
4112
4113 if (current->is_sub_map == FALSE && current->object.vm_object == VM_OBJECT_NULL){
4114 current->object.vm_object = vm_object_allocate((vm_map_size_t)(current->vme_end - current->vme_start));
4115 current->offset = 0;
4116 assert(current->use_pmap);
4117 }
4118 current->needs_copy = TRUE;
4119 current->max_protection |= VM_PROT_WRITE;
4120 }
4121
4122 if (set_max)
4123 current->protection =
4124 (current->max_protection =
4125 new_prot & ~VM_PROT_COPY) &
4126 old_prot;
4127 else
4128 current->protection = new_prot & ~VM_PROT_COPY;
4129
4130 /*
4131 * Update physical map if necessary.
4132 * If the request is to turn off write protection,
4133 * we won't do it for real (in pmap). This is because
4134 * it would cause copy-on-write to fail. We've already
4135 * set, the new protection in the map, so if a
4136 * write-protect fault occurred, it will be fixed up
4137 * properly, COW or not.
4138 */
4139 if (current->protection != old_prot) {
4140 /* Look one level in we support nested pmaps */
4141 /* from mapped submaps which are direct entries */
4142 /* in our map */
4143
4144 vm_prot_t prot;
4145
4146 prot = current->protection & ~VM_PROT_WRITE;
4147
4148 if (override_nx(map, current->alias) && prot)
4149 prot |= VM_PROT_EXECUTE;
4150
4151 if (current->is_sub_map && current->use_pmap) {
4152 pmap_protect(current->object.sub_map->pmap,
4153 current->vme_start,
4154 current->vme_end,
4155 prot);
4156 } else {
4157 pmap_protect(map->pmap,
4158 current->vme_start,
4159 current->vme_end,
4160 prot);
4161 }
4162 }
4163 current = current->vme_next;
4164 }
4165
4166 current = entry;
4167 while ((current != vm_map_to_entry(map)) &&
4168 (current->vme_start <= end)) {
4169 vm_map_simplify_entry(map, current);
4170 current = current->vme_next;
4171 }
4172
4173 vm_map_unlock(map);
4174 return(KERN_SUCCESS);
4175}
4176
4177/*
4178 * vm_map_inherit:
4179 *
4180 * Sets the inheritance of the specified address
4181 * range in the target map. Inheritance
4182 * affects how the map will be shared with
4183 * child maps at the time of vm_map_fork.
4184 */
4185kern_return_t
4186vm_map_inherit(
4187 register vm_map_t map,
4188 register vm_map_offset_t start,
4189 register vm_map_offset_t end,
4190 register vm_inherit_t new_inheritance)
4191{
4192 register vm_map_entry_t entry;
4193 vm_map_entry_t temp_entry;
4194
4195 vm_map_lock(map);
4196
4197 VM_MAP_RANGE_CHECK(map, start, end);
4198
4199 if (vm_map_lookup_entry(map, start, &temp_entry)) {
4200 entry = temp_entry;
4201 }
4202 else {
4203 temp_entry = temp_entry->vme_next;
4204 entry = temp_entry;
4205 }
4206
4207 /* first check entire range for submaps which can't support the */
4208 /* given inheritance. */
4209 while ((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
4210 if(entry->is_sub_map) {
4211 if(new_inheritance == VM_INHERIT_COPY) {
4212 vm_map_unlock(map);
4213 return(KERN_INVALID_ARGUMENT);
4214 }
4215 }
4216
4217 entry = entry->vme_next;
4218 }
4219
4220 entry = temp_entry;
4221 if (entry != vm_map_to_entry(map)) {
4222 /* clip and unnest if necessary */
4223 vm_map_clip_start(map, entry, start);
4224 }
4225
4226 while ((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
4227 vm_map_clip_end(map, entry, end);
4228 if (entry->is_sub_map) {
4229 /* clip did unnest if needed */
4230 assert(!entry->use_pmap);
4231 }
4232
4233 entry->inheritance = new_inheritance;
4234
4235 entry = entry->vme_next;
4236 }
4237
4238 vm_map_unlock(map);
4239 return(KERN_SUCCESS);
4240}
4241
4242/*
4243 * Update the accounting for the amount of wired memory in this map. If the user has
4244 * exceeded the defined limits, then we fail. Wiring on behalf of the kernel never fails.
4245 */
4246
4247static kern_return_t
4248add_wire_counts(
4249 vm_map_t map,
4250 vm_map_entry_t entry,
4251 boolean_t user_wire)
4252{
4253 vm_map_size_t size;
4254
4255 if (user_wire) {
4256 unsigned int total_wire_count = vm_page_wire_count + vm_lopage_free_count;
4257
4258 /*
4259 * We're wiring memory at the request of the user. Check if this is the first time the user is wiring
4260 * this map entry.
4261 */
4262
4263 if (entry->user_wired_count == 0) {
4264 size = entry->vme_end - entry->vme_start;
4265
4266 /*
4267 * Since this is the first time the user is wiring this map entry, check to see if we're
4268 * exceeding the user wire limits. There is a per map limit which is the smaller of either
4269 * the process's rlimit or the global vm_user_wire_limit which caps this value. There is also
4270 * a system-wide limit on the amount of memory all users can wire. If the user is over either
4271 * limit, then we fail.
4272 */
4273
4274 if(size + map->user_wire_size > MIN(map->user_wire_limit, vm_user_wire_limit) ||
4275 size + ptoa_64(total_wire_count) > vm_global_user_wire_limit ||
4276 size + ptoa_64(total_wire_count) > max_mem - vm_global_no_user_wire_amount)
4277 return KERN_RESOURCE_SHORTAGE;
4278
4279 /*
4280 * The first time the user wires an entry, we also increment the wired_count and add this to
4281 * the total that has been wired in the map.
4282 */
4283
4284 if (entry->wired_count >= MAX_WIRE_COUNT)
4285 return KERN_FAILURE;
4286
4287 entry->wired_count++;
4288 map->user_wire_size += size;
4289 }
4290
4291 if (entry->user_wired_count >= MAX_WIRE_COUNT)
4292 return KERN_FAILURE;
4293
4294 entry->user_wired_count++;
4295
4296 } else {
4297
4298 /*
4299 * The kernel's wiring the memory. Just bump the count and continue.
4300 */
4301
4302 if (entry->wired_count >= MAX_WIRE_COUNT)
4303 panic("vm_map_wire: too many wirings");
4304
4305 entry->wired_count++;
4306 }
4307
4308 return KERN_SUCCESS;
4309}
4310
4311/*
4312 * Update the memory wiring accounting now that the given map entry is being unwired.
4313 */
4314
4315static void
4316subtract_wire_counts(
4317 vm_map_t map,
4318 vm_map_entry_t entry,
4319 boolean_t user_wire)
4320{
4321
4322 if (user_wire) {
4323
4324 /*
4325 * We're unwiring memory at the request of the user. See if we're removing the last user wire reference.
4326 */
4327
4328 if (entry->user_wired_count == 1) {
4329
4330 /*
4331 * We're removing the last user wire reference. Decrement the wired_count and the total
4332 * user wired memory for this map.
4333 */
4334
4335 assert(entry->wired_count >= 1);
4336 entry->wired_count--;
4337 map->user_wire_size -= entry->vme_end - entry->vme_start;
4338 }
4339
4340 assert(entry->user_wired_count >= 1);
4341 entry->user_wired_count--;
4342
4343 } else {
4344
4345 /*
4346 * The kernel is unwiring the memory. Just update the count.
4347 */
4348
4349 assert(entry->wired_count >= 1);
4350 entry->wired_count--;
4351 }
4352}
4353
4354/*
4355 * vm_map_wire:
4356 *
4357 * Sets the pageability of the specified address range in the
4358 * target map as wired. Regions specified as not pageable require
4359 * locked-down physical memory and physical page maps. The
4360 * access_type variable indicates types of accesses that must not
4361 * generate page faults. This is checked against protection of
4362 * memory being locked-down.
4363 *
4364 * The map must not be locked, but a reference must remain to the
4365 * map throughout the call.
4366 */
4367static kern_return_t
4368vm_map_wire_nested(
4369 register vm_map_t map,
4370 register vm_map_offset_t start,
4371 register vm_map_offset_t end,
4372 register vm_prot_t access_type,
4373 boolean_t user_wire,
4374 pmap_t map_pmap,
4375 vm_map_offset_t pmap_addr,
4376 ppnum_t *physpage_p)
4377{
4378 register vm_map_entry_t entry;
4379 struct vm_map_entry *first_entry, tmp_entry;
4380 vm_map_t real_map;
4381 register vm_map_offset_t s,e;
4382 kern_return_t rc;
4383 boolean_t need_wakeup;
4384 boolean_t main_map = FALSE;
4385 wait_interrupt_t interruptible_state;
4386 thread_t cur_thread;
4387 unsigned int last_timestamp;
4388 vm_map_size_t size;
4389 boolean_t wire_and_extract;
4390
4391 wire_and_extract = FALSE;
4392 if (physpage_p != NULL) {
4393 /*
4394 * The caller wants the physical page number of the
4395 * wired page. We return only one physical page number
4396 * so this works for only one page at a time.
4397 */
4398 if ((end - start) != PAGE_SIZE) {
4399 return KERN_INVALID_ARGUMENT;
4400 }
4401 wire_and_extract = TRUE;
4402 *physpage_p = 0;
4403 }
4404
4405 vm_map_lock(map);
4406 if(map_pmap == NULL)
4407 main_map = TRUE;
4408 last_timestamp = map->timestamp;
4409
4410 VM_MAP_RANGE_CHECK(map, start, end);
4411 assert(page_aligned(start));
4412 assert(page_aligned(end));
4413 assert(VM_MAP_PAGE_ALIGNED(start, VM_MAP_PAGE_MASK(map)));
4414 assert(VM_MAP_PAGE_ALIGNED(end, VM_MAP_PAGE_MASK(map)));
4415 if (start == end) {
4416 /* We wired what the caller asked for, zero pages */
4417 vm_map_unlock(map);
4418 return KERN_SUCCESS;
4419 }
4420
4421 need_wakeup = FALSE;
4422 cur_thread = current_thread();
4423
4424 s = start;
4425 rc = KERN_SUCCESS;
4426
4427 if (vm_map_lookup_entry(map, s, &first_entry)) {
4428 entry = first_entry;
4429 /*
4430 * vm_map_clip_start will be done later.
4431 * We don't want to unnest any nested submaps here !
4432 */
4433 } else {
4434 /* Start address is not in map */
4435 rc = KERN_INVALID_ADDRESS;
4436 goto done;
4437 }
4438
4439 while ((entry != vm_map_to_entry(map)) && (s < end)) {
4440 /*
4441 * At this point, we have wired from "start" to "s".
4442 * We still need to wire from "s" to "end".
4443 *
4444 * "entry" hasn't been clipped, so it could start before "s"
4445 * and/or end after "end".
4446 */
4447
4448 /* "e" is how far we want to wire in this entry */
4449 e = entry->vme_end;
4450 if (e > end)
4451 e = end;
4452
4453 /*
4454 * If another thread is wiring/unwiring this entry then
4455 * block after informing other thread to wake us up.
4456 */
4457 if (entry->in_transition) {
4458 wait_result_t wait_result;
4459
4460 /*
4461 * We have not clipped the entry. Make sure that
4462 * the start address is in range so that the lookup
4463 * below will succeed.
4464 * "s" is the current starting point: we've already
4465 * wired from "start" to "s" and we still have
4466 * to wire from "s" to "end".
4467 */
4468
4469 entry->needs_wakeup = TRUE;
4470
4471 /*
4472 * wake up anybody waiting on entries that we have
4473 * already wired.
4474 */
4475 if (need_wakeup) {
4476 vm_map_entry_wakeup(map);
4477 need_wakeup = FALSE;
4478 }
4479 /*
4480 * User wiring is interruptible
4481 */
4482 wait_result = vm_map_entry_wait(map,
4483 (user_wire) ? THREAD_ABORTSAFE :
4484 THREAD_UNINT);
4485 if (user_wire && wait_result == THREAD_INTERRUPTED) {
4486 /*
4487 * undo the wirings we have done so far
4488 * We do not clear the needs_wakeup flag,
4489 * because we cannot tell if we were the
4490 * only one waiting.
4491 */
4492 rc = KERN_FAILURE;
4493 goto done;
4494 }
4495
4496 /*
4497 * Cannot avoid a lookup here. reset timestamp.
4498 */
4499 last_timestamp = map->timestamp;
4500
4501 /*
4502 * The entry could have been clipped, look it up again.
4503 * Worse that can happen is, it may not exist anymore.
4504 */
4505 if (!vm_map_lookup_entry(map, s, &first_entry)) {
4506 /*
4507 * User: undo everything upto the previous
4508 * entry. let vm_map_unwire worry about
4509 * checking the validity of the range.
4510 */
4511 rc = KERN_FAILURE;
4512 goto done;
4513 }
4514 entry = first_entry;
4515 continue;
4516 }
4517
4518 if (entry->is_sub_map) {
4519 vm_map_offset_t sub_start;
4520 vm_map_offset_t sub_end;
4521 vm_map_offset_t local_start;
4522 vm_map_offset_t local_end;
4523 pmap_t pmap;
4524
4525 if (wire_and_extract) {
4526 /*
4527 * Wiring would result in copy-on-write
4528 * which would not be compatible with
4529 * the sharing we have with the original
4530 * provider of this memory.
4531 */
4532 rc = KERN_INVALID_ARGUMENT;
4533 goto done;
4534 }
4535
4536 vm_map_clip_start(map, entry, s);
4537 vm_map_clip_end(map, entry, end);
4538
4539 sub_start = entry->offset;
4540 sub_end = entry->vme_end;
4541 sub_end += entry->offset - entry->vme_start;
4542
4543 local_end = entry->vme_end;
4544 if(map_pmap == NULL) {
4545 vm_object_t object;
4546 vm_object_offset_t offset;
4547 vm_prot_t prot;
4548 boolean_t wired;
4549 vm_map_entry_t local_entry;
4550 vm_map_version_t version;
4551 vm_map_t lookup_map;
4552
4553 if(entry->use_pmap) {
4554 pmap = entry->object.sub_map->pmap;
4555 /* ppc implementation requires that */
4556 /* submaps pmap address ranges line */
4557 /* up with parent map */
4558#ifdef notdef
4559 pmap_addr = sub_start;
4560#endif
4561 pmap_addr = s;
4562 } else {
4563 pmap = map->pmap;
4564 pmap_addr = s;
4565 }
4566
4567 if (entry->wired_count) {
4568 if ((rc = add_wire_counts(map, entry, user_wire)) != KERN_SUCCESS)
4569 goto done;
4570
4571 /*
4572 * The map was not unlocked:
4573 * no need to goto re-lookup.
4574 * Just go directly to next entry.
4575 */
4576 entry = entry->vme_next;
4577 s = entry->vme_start;
4578 continue;
4579
4580 }
4581
4582 /* call vm_map_lookup_locked to */
4583 /* cause any needs copy to be */
4584 /* evaluated */
4585 local_start = entry->vme_start;
4586 lookup_map = map;
4587 vm_map_lock_write_to_read(map);
4588 if(vm_map_lookup_locked(
4589 &lookup_map, local_start,
4590 access_type,
4591 OBJECT_LOCK_EXCLUSIVE,
4592 &version, &object,
4593 &offset, &prot, &wired,
4594 NULL,
4595 &real_map)) {
4596
4597 vm_map_unlock_read(lookup_map);
4598 vm_map_unwire(map, start,
4599 s, user_wire);
4600 return(KERN_FAILURE);
4601 }
4602 vm_object_unlock(object);
4603 if(real_map != lookup_map)
4604 vm_map_unlock(real_map);
4605 vm_map_unlock_read(lookup_map);
4606 vm_map_lock(map);
4607
4608 /* we unlocked, so must re-lookup */
4609 if (!vm_map_lookup_entry(map,
4610 local_start,
4611 &local_entry)) {
4612 rc = KERN_FAILURE;
4613 goto done;
4614 }
4615
4616 /*
4617 * entry could have been "simplified",
4618 * so re-clip
4619 */
4620 entry = local_entry;
4621 assert(s == local_start);
4622 vm_map_clip_start(map, entry, s);
4623 vm_map_clip_end(map, entry, end);
4624 /* re-compute "e" */
4625 e = entry->vme_end;
4626 if (e > end)
4627 e = end;
4628
4629 /* did we have a change of type? */
4630 if (!entry->is_sub_map) {
4631 last_timestamp = map->timestamp;
4632 continue;
4633 }
4634 } else {
4635 local_start = entry->vme_start;
4636 pmap = map_pmap;
4637 }
4638
4639 if ((rc = add_wire_counts(map, entry, user_wire)) != KERN_SUCCESS)
4640 goto done;
4641
4642 entry->in_transition = TRUE;
4643
4644 vm_map_unlock(map);
4645 rc = vm_map_wire_nested(entry->object.sub_map,
4646 sub_start, sub_end,
4647 access_type,
4648 user_wire, pmap, pmap_addr,
4649 NULL);
4650 vm_map_lock(map);
4651
4652 /*
4653 * Find the entry again. It could have been clipped
4654 * after we unlocked the map.
4655 */
4656 if (!vm_map_lookup_entry(map, local_start,
4657 &first_entry))
4658 panic("vm_map_wire: re-lookup failed");
4659 entry = first_entry;
4660
4661 assert(local_start == s);
4662 /* re-compute "e" */
4663 e = entry->vme_end;
4664 if (e > end)
4665 e = end;
4666
4667 last_timestamp = map->timestamp;
4668 while ((entry != vm_map_to_entry(map)) &&
4669 (entry->vme_start < e)) {
4670 assert(entry->in_transition);
4671 entry->in_transition = FALSE;
4672 if (entry->needs_wakeup) {
4673 entry->needs_wakeup = FALSE;
4674 need_wakeup = TRUE;
4675 }
4676 if (rc != KERN_SUCCESS) {/* from vm_*_wire */
4677 subtract_wire_counts(map, entry, user_wire);
4678 }
4679 entry = entry->vme_next;
4680 }
4681 if (rc != KERN_SUCCESS) { /* from vm_*_wire */
4682 goto done;
4683 }
4684
4685 /* no need to relookup again */
4686 s = entry->vme_start;
4687 continue;
4688 }
4689
4690 /*
4691 * If this entry is already wired then increment
4692 * the appropriate wire reference count.
4693 */
4694 if (entry->wired_count) {
4695
4696 if ((entry->protection & access_type) != access_type) {
4697 /* found a protection problem */
4698
4699 /*
4700 * XXX FBDP
4701 * We should always return an error
4702 * in this case but since we didn't
4703 * enforce it before, let's do
4704 * it only for the new "wire_and_extract"
4705 * code path for now...
4706 */
4707 if (wire_and_extract) {
4708 rc = KERN_PROTECTION_FAILURE;
4709 goto done;
4710 }
4711 }
4712
4713 /*
4714 * entry is already wired down, get our reference
4715 * after clipping to our range.
4716 */
4717 vm_map_clip_start(map, entry, s);
4718 vm_map_clip_end(map, entry, end);
4719
4720 if ((rc = add_wire_counts(map, entry, user_wire)) != KERN_SUCCESS)
4721 goto done;
4722
4723 if (wire_and_extract) {
4724 vm_object_t object;
4725 vm_object_offset_t offset;
4726 vm_page_t m;
4727
4728 /*
4729 * We don't have to "wire" the page again
4730 * bit we still have to "extract" its
4731 * physical page number, after some sanity
4732 * checks.
4733 */
4734 assert((entry->vme_end - entry->vme_start)
4735 == PAGE_SIZE);
4736 assert(!entry->needs_copy);
4737 assert(!entry->is_sub_map);
4738 assert(entry->object.vm_object);
4739 if (((entry->vme_end - entry->vme_start)
4740 != PAGE_SIZE) ||
4741 entry->needs_copy ||
4742 entry->is_sub_map ||
4743 entry->object.vm_object == VM_OBJECT_NULL) {
4744 rc = KERN_INVALID_ARGUMENT;
4745 goto done;
4746 }
4747
4748 object = entry->object.vm_object;
4749 offset = entry->offset;
4750 /* need exclusive lock to update m->dirty */
4751 if (entry->protection & VM_PROT_WRITE) {
4752 vm_object_lock(object);
4753 } else {
4754 vm_object_lock_shared(object);
4755 }
4756 m = vm_page_lookup(object, offset);
4757 assert(m != VM_PAGE_NULL);
4758 assert(m->wire_count);
4759 if (m != VM_PAGE_NULL && m->wire_count) {
4760 *physpage_p = m->phys_page;
4761 if (entry->protection & VM_PROT_WRITE) {
4762 vm_object_lock_assert_exclusive(
4763 m->object);
4764 m->dirty = TRUE;
4765 }
4766 } else {
4767 /* not already wired !? */
4768 *physpage_p = 0;
4769 }
4770 vm_object_unlock(object);
4771 }
4772
4773 /* map was not unlocked: no need to relookup */
4774 entry = entry->vme_next;
4775 s = entry->vme_start;
4776 continue;
4777 }
4778
4779 /*
4780 * Unwired entry or wire request transmitted via submap
4781 */
4782
4783
4784 /*
4785 * Perform actions of vm_map_lookup that need the write
4786 * lock on the map: create a shadow object for a
4787 * copy-on-write region, or an object for a zero-fill
4788 * region.
4789 */
4790 size = entry->vme_end - entry->vme_start;
4791 /*
4792 * If wiring a copy-on-write page, we need to copy it now
4793 * even if we're only (currently) requesting read access.
4794 * This is aggressive, but once it's wired we can't move it.
4795 */
4796 if (entry->needs_copy) {
4797 if (wire_and_extract) {
4798 /*
4799 * We're supposed to share with the original
4800 * provider so should not be "needs_copy"
4801 */
4802 rc = KERN_INVALID_ARGUMENT;
4803 goto done;
4804 }
4805
4806 vm_object_shadow(&entry->object.vm_object,
4807 &entry->offset, size);
4808 entry->needs_copy = FALSE;
4809 } else if (entry->object.vm_object == VM_OBJECT_NULL) {
4810 if (wire_and_extract) {
4811 /*
4812 * We're supposed to share with the original
4813 * provider so should already have an object.
4814 */
4815 rc = KERN_INVALID_ARGUMENT;
4816 goto done;
4817 }
4818 entry->object.vm_object = vm_object_allocate(size);
4819 entry->offset = (vm_object_offset_t)0;
4820 assert(entry->use_pmap);
4821 }
4822
4823 vm_map_clip_start(map, entry, s);
4824 vm_map_clip_end(map, entry, end);
4825
4826 /* re-compute "e" */
4827 e = entry->vme_end;
4828 if (e > end)
4829 e = end;
4830
4831 /*
4832 * Check for holes and protection mismatch.
4833 * Holes: Next entry should be contiguous unless this
4834 * is the end of the region.
4835 * Protection: Access requested must be allowed, unless
4836 * wiring is by protection class
4837 */
4838 if ((entry->vme_end < end) &&
4839 ((entry->vme_next == vm_map_to_entry(map)) ||
4840 (entry->vme_next->vme_start > entry->vme_end))) {
4841 /* found a hole */
4842 rc = KERN_INVALID_ADDRESS;
4843 goto done;
4844 }
4845 if ((entry->protection & access_type) != access_type) {
4846 /* found a protection problem */
4847 rc = KERN_PROTECTION_FAILURE;
4848 goto done;
4849 }
4850
4851 assert(entry->wired_count == 0 && entry->user_wired_count == 0);
4852
4853 if ((rc = add_wire_counts(map, entry, user_wire)) != KERN_SUCCESS)
4854 goto done;
4855
4856 entry->in_transition = TRUE;
4857
4858 /*
4859 * This entry might get split once we unlock the map.
4860 * In vm_fault_wire(), we need the current range as
4861 * defined by this entry. In order for this to work
4862 * along with a simultaneous clip operation, we make a
4863 * temporary copy of this entry and use that for the
4864 * wiring. Note that the underlying objects do not
4865 * change during a clip.
4866 */
4867 tmp_entry = *entry;
4868
4869 /*
4870 * The in_transition state guarentees that the entry
4871 * (or entries for this range, if split occured) will be
4872 * there when the map lock is acquired for the second time.
4873 */
4874 vm_map_unlock(map);
4875
4876 if (!user_wire && cur_thread != THREAD_NULL)
4877 interruptible_state = thread_interrupt_level(THREAD_UNINT);
4878 else
4879 interruptible_state = THREAD_UNINT;
4880
4881 if(map_pmap)
4882 rc = vm_fault_wire(map,
4883 &tmp_entry, map_pmap, pmap_addr,
4884 physpage_p);
4885 else
4886 rc = vm_fault_wire(map,
4887 &tmp_entry, map->pmap,
4888 tmp_entry.vme_start,
4889 physpage_p);
4890
4891 if (!user_wire && cur_thread != THREAD_NULL)
4892 thread_interrupt_level(interruptible_state);
4893
4894 vm_map_lock(map);
4895
4896 if (last_timestamp+1 != map->timestamp) {
4897 /*
4898 * Find the entry again. It could have been clipped
4899 * after we unlocked the map.
4900 */
4901 if (!vm_map_lookup_entry(map, tmp_entry.vme_start,
4902 &first_entry))
4903 panic("vm_map_wire: re-lookup failed");
4904
4905 entry = first_entry;
4906 }
4907
4908 last_timestamp = map->timestamp;
4909
4910 while ((entry != vm_map_to_entry(map)) &&
4911 (entry->vme_start < tmp_entry.vme_end)) {
4912 assert(entry->in_transition);
4913 entry->in_transition = FALSE;
4914 if (entry->needs_wakeup) {
4915 entry->needs_wakeup = FALSE;
4916 need_wakeup = TRUE;
4917 }
4918 if (rc != KERN_SUCCESS) { /* from vm_*_wire */
4919 subtract_wire_counts(map, entry, user_wire);
4920 }
4921 entry = entry->vme_next;
4922 }
4923
4924 if (rc != KERN_SUCCESS) { /* from vm_*_wire */
4925 goto done;
4926 }
4927
4928 s = entry->vme_start;
4929 } /* end while loop through map entries */
4930
4931done:
4932 if (rc == KERN_SUCCESS) {
4933 /* repair any damage we may have made to the VM map */
4934 vm_map_simplify_range(map, start, end);
4935 }
4936
4937 vm_map_unlock(map);
4938
4939 /*
4940 * wake up anybody waiting on entries we wired.
4941 */
4942 if (need_wakeup)
4943 vm_map_entry_wakeup(map);
4944
4945 if (rc != KERN_SUCCESS) {
4946 /* undo what has been wired so far */
4947 vm_map_unwire(map, start, s, user_wire);
4948 if (physpage_p) {
4949 *physpage_p = 0;
4950 }
4951 }
4952
4953 return rc;
4954
4955}
4956
4957kern_return_t
4958vm_map_wire(
4959 register vm_map_t map,
4960 register vm_map_offset_t start,
4961 register vm_map_offset_t end,
4962 register vm_prot_t access_type,
4963 boolean_t user_wire)
4964{
4965
4966 kern_return_t kret;
4967
4968 kret = vm_map_wire_nested(map, start, end, access_type,
4969 user_wire, (pmap_t)NULL, 0, NULL);
4970 return kret;
4971}
4972
4973kern_return_t
4974vm_map_wire_and_extract(
4975 vm_map_t map,
4976 vm_map_offset_t start,
4977 vm_prot_t access_type,
4978 boolean_t user_wire,
4979 ppnum_t *physpage_p)
4980{
4981
4982 kern_return_t kret;
4983
4984 kret = vm_map_wire_nested(map,
4985 start,
4986 start+VM_MAP_PAGE_SIZE(map),
4987 access_type,
4988 user_wire,
4989 (pmap_t)NULL,
4990 0,
4991 physpage_p);
4992 if (kret != KERN_SUCCESS &&
4993 physpage_p != NULL) {
4994 *physpage_p = 0;
4995 }
4996 return kret;
4997}
4998
4999/*
5000 * vm_map_unwire:
5001 *
5002 * Sets the pageability of the specified address range in the target
5003 * as pageable. Regions specified must have been wired previously.
5004 *
5005 * The map must not be locked, but a reference must remain to the map
5006 * throughout the call.
5007 *
5008 * Kernel will panic on failures. User unwire ignores holes and
5009 * unwired and intransition entries to avoid losing memory by leaving
5010 * it unwired.
5011 */
5012static kern_return_t
5013vm_map_unwire_nested(
5014 register vm_map_t map,
5015 register vm_map_offset_t start,
5016 register vm_map_offset_t end,
5017 boolean_t user_wire,
5018 pmap_t map_pmap,
5019 vm_map_offset_t pmap_addr)
5020{
5021 register vm_map_entry_t entry;
5022 struct vm_map_entry *first_entry, tmp_entry;
5023 boolean_t need_wakeup;
5024 boolean_t main_map = FALSE;
5025 unsigned int last_timestamp;
5026
5027 vm_map_lock(map);
5028 if(map_pmap == NULL)
5029 main_map = TRUE;
5030 last_timestamp = map->timestamp;
5031
5032 VM_MAP_RANGE_CHECK(map, start, end);
5033 assert(page_aligned(start));
5034 assert(page_aligned(end));
5035 assert(VM_MAP_PAGE_ALIGNED(start, VM_MAP_PAGE_MASK(map)));
5036 assert(VM_MAP_PAGE_ALIGNED(end, VM_MAP_PAGE_MASK(map)));
5037
5038 if (start == end) {
5039 /* We unwired what the caller asked for: zero pages */
5040 vm_map_unlock(map);
5041 return KERN_SUCCESS;
5042 }
5043
5044 if (vm_map_lookup_entry(map, start, &first_entry)) {
5045 entry = first_entry;
5046 /*
5047 * vm_map_clip_start will be done later.
5048 * We don't want to unnest any nested sub maps here !
5049 */
5050 }
5051 else {
5052 if (!user_wire) {
5053 panic("vm_map_unwire: start not found");
5054 }
5055 /* Start address is not in map. */
5056 vm_map_unlock(map);
5057 return(KERN_INVALID_ADDRESS);
5058 }
5059
5060 if (entry->superpage_size) {
5061 /* superpages are always wired */
5062 vm_map_unlock(map);
5063 return KERN_INVALID_ADDRESS;
5064 }
5065
5066 need_wakeup = FALSE;
5067 while ((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
5068 if (entry->in_transition) {
5069 /*
5070 * 1)
5071 * Another thread is wiring down this entry. Note
5072 * that if it is not for the other thread we would
5073 * be unwiring an unwired entry. This is not
5074 * permitted. If we wait, we will be unwiring memory
5075 * we did not wire.
5076 *
5077 * 2)
5078 * Another thread is unwiring this entry. We did not
5079 * have a reference to it, because if we did, this
5080 * entry will not be getting unwired now.
5081 */
5082 if (!user_wire) {
5083 /*
5084 * XXX FBDP
5085 * This could happen: there could be some
5086 * overlapping vslock/vsunlock operations
5087 * going on.
5088 * We should probably just wait and retry,
5089 * but then we have to be careful that this
5090 * entry could get "simplified" after
5091 * "in_transition" gets unset and before
5092 * we re-lookup the entry, so we would
5093 * have to re-clip the entry to avoid
5094 * re-unwiring what we have already unwired...
5095 * See vm_map_wire_nested().
5096 *
5097 * Or we could just ignore "in_transition"
5098 * here and proceed to decement the wired
5099 * count(s) on this entry. That should be fine
5100 * as long as "wired_count" doesn't drop all
5101 * the way to 0 (and we should panic if THAT
5102 * happens).
5103 */
5104 panic("vm_map_unwire: in_transition entry");
5105 }
5106
5107 entry = entry->vme_next;
5108 continue;
5109 }
5110
5111 if (entry->is_sub_map) {
5112 vm_map_offset_t sub_start;
5113 vm_map_offset_t sub_end;
5114 vm_map_offset_t local_end;
5115 pmap_t pmap;
5116
5117 vm_map_clip_start(map, entry, start);
5118 vm_map_clip_end(map, entry, end);
5119
5120 sub_start = entry->offset;
5121 sub_end = entry->vme_end - entry->vme_start;
5122 sub_end += entry->offset;
5123 local_end = entry->vme_end;
5124 if(map_pmap == NULL) {
5125 if(entry->use_pmap) {
5126 pmap = entry->object.sub_map->pmap;
5127 pmap_addr = sub_start;
5128 } else {
5129 pmap = map->pmap;
5130 pmap_addr = start;
5131 }
5132 if (entry->wired_count == 0 ||
5133 (user_wire && entry->user_wired_count == 0)) {
5134 if (!user_wire)
5135 panic("vm_map_unwire: entry is unwired");
5136 entry = entry->vme_next;
5137 continue;
5138 }
5139
5140 /*
5141 * Check for holes
5142 * Holes: Next entry should be contiguous unless
5143 * this is the end of the region.
5144 */
5145 if (((entry->vme_end < end) &&
5146 ((entry->vme_next == vm_map_to_entry(map)) ||
5147 (entry->vme_next->vme_start
5148 > entry->vme_end)))) {
5149 if (!user_wire)
5150 panic("vm_map_unwire: non-contiguous region");
5151/*
5152 entry = entry->vme_next;
5153 continue;
5154*/
5155 }
5156
5157 subtract_wire_counts(map, entry, user_wire);
5158
5159 if (entry->wired_count != 0) {
5160 entry = entry->vme_next;
5161 continue;
5162 }
5163
5164 entry->in_transition = TRUE;
5165 tmp_entry = *entry;/* see comment in vm_map_wire() */
5166
5167 /*
5168 * We can unlock the map now. The in_transition state
5169 * guarantees existance of the entry.
5170 */
5171 vm_map_unlock(map);
5172 vm_map_unwire_nested(entry->object.sub_map,
5173 sub_start, sub_end, user_wire, pmap, pmap_addr);
5174 vm_map_lock(map);
5175
5176 if (last_timestamp+1 != map->timestamp) {
5177 /*
5178 * Find the entry again. It could have been
5179 * clipped or deleted after we unlocked the map.
5180 */
5181 if (!vm_map_lookup_entry(map,
5182 tmp_entry.vme_start,
5183 &first_entry)) {
5184 if (!user_wire)
5185 panic("vm_map_unwire: re-lookup failed");
5186 entry = first_entry->vme_next;
5187 } else
5188 entry = first_entry;
5189 }
5190 last_timestamp = map->timestamp;
5191
5192 /*
5193 * clear transition bit for all constituent entries
5194 * that were in the original entry (saved in
5195 * tmp_entry). Also check for waiters.
5196 */
5197 while ((entry != vm_map_to_entry(map)) &&
5198 (entry->vme_start < tmp_entry.vme_end)) {
5199 assert(entry->in_transition);
5200 entry->in_transition = FALSE;
5201 if (entry->needs_wakeup) {
5202 entry->needs_wakeup = FALSE;
5203 need_wakeup = TRUE;
5204 }
5205 entry = entry->vme_next;
5206 }
5207 continue;
5208 } else {
5209 vm_map_unlock(map);
5210 vm_map_unwire_nested(entry->object.sub_map,
5211 sub_start, sub_end, user_wire, map_pmap,
5212 pmap_addr);
5213 vm_map_lock(map);
5214
5215 if (last_timestamp+1 != map->timestamp) {
5216 /*
5217 * Find the entry again. It could have been
5218 * clipped or deleted after we unlocked the map.
5219 */
5220 if (!vm_map_lookup_entry(map,
5221 tmp_entry.vme_start,
5222 &first_entry)) {
5223 if (!user_wire)
5224 panic("vm_map_unwire: re-lookup failed");
5225 entry = first_entry->vme_next;
5226 } else
5227 entry = first_entry;
5228 }
5229 last_timestamp = map->timestamp;
5230 }
5231 }
5232
5233
5234 if ((entry->wired_count == 0) ||
5235 (user_wire && entry->user_wired_count == 0)) {
5236 if (!user_wire)
5237 panic("vm_map_unwire: entry is unwired");
5238
5239 entry = entry->vme_next;
5240 continue;
5241 }
5242
5243 assert(entry->wired_count > 0 &&
5244 (!user_wire || entry->user_wired_count > 0));
5245
5246 vm_map_clip_start(map, entry, start);
5247 vm_map_clip_end(map, entry, end);
5248
5249 /*
5250 * Check for holes
5251 * Holes: Next entry should be contiguous unless
5252 * this is the end of the region.
5253 */
5254 if (((entry->vme_end < end) &&
5255 ((entry->vme_next == vm_map_to_entry(map)) ||
5256 (entry->vme_next->vme_start > entry->vme_end)))) {
5257
5258 if (!user_wire)
5259 panic("vm_map_unwire: non-contiguous region");
5260 entry = entry->vme_next;
5261 continue;
5262 }
5263
5264 subtract_wire_counts(map, entry, user_wire);
5265
5266 if (entry->wired_count != 0) {
5267 entry = entry->vme_next;
5268 continue;
5269 }
5270
5271 if(entry->zero_wired_pages) {
5272 entry->zero_wired_pages = FALSE;
5273 }
5274
5275 entry->in_transition = TRUE;
5276 tmp_entry = *entry; /* see comment in vm_map_wire() */
5277
5278 /*
5279 * We can unlock the map now. The in_transition state
5280 * guarantees existance of the entry.
5281 */
5282 vm_map_unlock(map);
5283 if(map_pmap) {
5284 vm_fault_unwire(map,
5285 &tmp_entry, FALSE, map_pmap, pmap_addr);
5286 } else {
5287 vm_fault_unwire(map,
5288 &tmp_entry, FALSE, map->pmap,
5289 tmp_entry.vme_start);
5290 }
5291 vm_map_lock(map);
5292
5293 if (last_timestamp+1 != map->timestamp) {
5294 /*
5295 * Find the entry again. It could have been clipped
5296 * or deleted after we unlocked the map.
5297 */
5298 if (!vm_map_lookup_entry(map, tmp_entry.vme_start,
5299 &first_entry)) {
5300 if (!user_wire)
5301 panic("vm_map_unwire: re-lookup failed");
5302 entry = first_entry->vme_next;
5303 } else
5304 entry = first_entry;
5305 }
5306 last_timestamp = map->timestamp;
5307
5308 /*
5309 * clear transition bit for all constituent entries that
5310 * were in the original entry (saved in tmp_entry). Also
5311 * check for waiters.
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 entry = entry->vme_next;
5322 }
5323 }
5324
5325 /*
5326 * We might have fragmented the address space when we wired this
5327 * range of addresses. Attempt to re-coalesce these VM map entries
5328 * with their neighbors now that they're no longer wired.
5329 * Under some circumstances, address space fragmentation can
5330 * prevent VM object shadow chain collapsing, which can cause
5331 * swap space leaks.
5332 */
5333 vm_map_simplify_range(map, start, end);
5334
5335 vm_map_unlock(map);
5336 /*
5337 * wake up anybody waiting on entries that we have unwired.
5338 */
5339 if (need_wakeup)
5340 vm_map_entry_wakeup(map);
5341 return(KERN_SUCCESS);
5342
5343}
5344
5345kern_return_t
5346vm_map_unwire(
5347 register vm_map_t map,
5348 register vm_map_offset_t start,
5349 register vm_map_offset_t end,
5350 boolean_t user_wire)
5351{
5352 return vm_map_unwire_nested(map, start, end,
5353 user_wire, (pmap_t)NULL, 0);
5354}
5355
5356
5357/*
5358 * vm_map_entry_delete: [ internal use only ]
5359 *
5360 * Deallocate the given entry from the target map.
5361 */
5362static void
5363vm_map_entry_delete(
5364 register vm_map_t map,
5365 register vm_map_entry_t entry)
5366{
5367 register vm_map_offset_t s, e;
5368 register vm_object_t object;
5369 register vm_map_t submap;
5370
5371 s = entry->vme_start;
5372 e = entry->vme_end;
5373 assert(page_aligned(s));
5374 assert(page_aligned(e));
5375 if (entry->map_aligned == TRUE) {
5376 assert(VM_MAP_PAGE_ALIGNED(s, VM_MAP_PAGE_MASK(map)));
5377 assert(VM_MAP_PAGE_ALIGNED(e, VM_MAP_PAGE_MASK(map)));
5378 }
5379 assert(entry->wired_count == 0);
5380 assert(entry->user_wired_count == 0);
5381 assert(!entry->permanent);
5382
5383 if (entry->is_sub_map) {
5384 object = NULL;
5385 submap = entry->object.sub_map;
5386 } else {
5387 submap = NULL;
5388 object = entry->object.vm_object;
5389 }
5390
5391 vm_map_store_entry_unlink(map, entry);
5392 map->size -= e - s;
5393
5394 vm_map_entry_dispose(map, entry);
5395
5396 vm_map_unlock(map);
5397 /*
5398 * Deallocate the object only after removing all
5399 * pmap entries pointing to its pages.
5400 */
5401 if (submap)
5402 vm_map_deallocate(submap);
5403 else
5404 vm_object_deallocate(object);
5405
5406}
5407
5408void
5409vm_map_submap_pmap_clean(
5410 vm_map_t map,
5411 vm_map_offset_t start,
5412 vm_map_offset_t end,
5413 vm_map_t sub_map,
5414 vm_map_offset_t offset)
5415{
5416 vm_map_offset_t submap_start;
5417 vm_map_offset_t submap_end;
5418 vm_map_size_t remove_size;
5419 vm_map_entry_t entry;
5420
5421 submap_end = offset + (end - start);
5422 submap_start = offset;
5423
5424 vm_map_lock_read(sub_map);
5425 if(vm_map_lookup_entry(sub_map, offset, &entry)) {
5426
5427 remove_size = (entry->vme_end - entry->vme_start);
5428 if(offset > entry->vme_start)
5429 remove_size -= offset - entry->vme_start;
5430
5431
5432 if(submap_end < entry->vme_end) {
5433 remove_size -=
5434 entry->vme_end - submap_end;
5435 }
5436 if(entry->is_sub_map) {
5437 vm_map_submap_pmap_clean(
5438 sub_map,
5439 start,
5440 start + remove_size,
5441 entry->object.sub_map,
5442 entry->offset);
5443 } else {
5444
5445 if((map->mapped_in_other_pmaps) && (map->ref_count)
5446 && (entry->object.vm_object != NULL)) {
5447 vm_object_pmap_protect(
5448 entry->object.vm_object,
5449 entry->offset+(offset-entry->vme_start),
5450 remove_size,
5451 PMAP_NULL,
5452 entry->vme_start,
5453 VM_PROT_NONE);
5454 } else {
5455 pmap_remove(map->pmap,
5456 (addr64_t)start,
5457 (addr64_t)(start + remove_size));
5458 }
5459 }
5460 }
5461
5462 entry = entry->vme_next;
5463
5464 while((entry != vm_map_to_entry(sub_map))
5465 && (entry->vme_start < submap_end)) {
5466 remove_size = (entry->vme_end - entry->vme_start);
5467 if(submap_end < entry->vme_end) {
5468 remove_size -= entry->vme_end - submap_end;
5469 }
5470 if(entry->is_sub_map) {
5471 vm_map_submap_pmap_clean(
5472 sub_map,
5473 (start + entry->vme_start) - offset,
5474 ((start + entry->vme_start) - offset) + remove_size,
5475 entry->object.sub_map,
5476 entry->offset);
5477 } else {
5478 if((map->mapped_in_other_pmaps) && (map->ref_count)
5479 && (entry->object.vm_object != NULL)) {
5480 vm_object_pmap_protect(
5481 entry->object.vm_object,
5482 entry->offset,
5483 remove_size,
5484 PMAP_NULL,
5485 entry->vme_start,
5486 VM_PROT_NONE);
5487 } else {
5488 pmap_remove(map->pmap,
5489 (addr64_t)((start + entry->vme_start)
5490 - offset),
5491 (addr64_t)(((start + entry->vme_start)
5492 - offset) + remove_size));
5493 }
5494 }
5495 entry = entry->vme_next;
5496 }
5497 vm_map_unlock_read(sub_map);
5498 return;
5499}
5500
5501/*
5502 * vm_map_delete: [ internal use only ]
5503 *
5504 * Deallocates the given address range from the target map.
5505 * Removes all user wirings. Unwires one kernel wiring if
5506 * VM_MAP_REMOVE_KUNWIRE is set. Waits for kernel wirings to go
5507 * away if VM_MAP_REMOVE_WAIT_FOR_KWIRE is set. Sleeps
5508 * interruptibly if VM_MAP_REMOVE_INTERRUPTIBLE is set.
5509 *
5510 * This routine is called with map locked and leaves map locked.
5511 */
5512static kern_return_t
5513vm_map_delete(
5514 vm_map_t map,
5515 vm_map_offset_t start,
5516 vm_map_offset_t end,
5517 int flags,
5518 vm_map_t zap_map)
5519{
5520 vm_map_entry_t entry, next;
5521 struct vm_map_entry *first_entry, tmp_entry;
5522 register vm_map_offset_t s;
5523 register vm_object_t object;
5524 boolean_t need_wakeup;
5525 unsigned int last_timestamp = ~0; /* unlikely value */
5526 int interruptible;
5527
5528 interruptible = (flags & VM_MAP_REMOVE_INTERRUPTIBLE) ?
5529 THREAD_ABORTSAFE : THREAD_UNINT;
5530
5531 /*
5532 * All our DMA I/O operations in IOKit are currently done by
5533 * wiring through the map entries of the task requesting the I/O.
5534 * Because of this, we must always wait for kernel wirings
5535 * to go away on the entries before deleting them.
5536 *
5537 * Any caller who wants to actually remove a kernel wiring
5538 * should explicitly set the VM_MAP_REMOVE_KUNWIRE flag to
5539 * properly remove one wiring instead of blasting through
5540 * them all.
5541 */
5542 flags |= VM_MAP_REMOVE_WAIT_FOR_KWIRE;
5543
5544 while(1) {
5545 /*
5546 * Find the start of the region, and clip it
5547 */
5548 if (vm_map_lookup_entry(map, start, &first_entry)) {
5549 entry = first_entry;
5550 if (map == kalloc_map &&
5551 (entry->vme_start != start ||
5552 entry->vme_end != end)) {
5553 panic("vm_map_delete(%p,0x%llx,0x%llx): "
5554 "mismatched entry %p [0x%llx:0x%llx]\n",
5555 map,
5556 (uint64_t)start,
5557 (uint64_t)end,
5558 entry,
5559 (uint64_t)entry->vme_start,
5560 (uint64_t)entry->vme_end);
5561 }
5562 if (entry->superpage_size && (start & ~SUPERPAGE_MASK)) { /* extend request to whole entry */ start = SUPERPAGE_ROUND_DOWN(start);
5563 start = SUPERPAGE_ROUND_DOWN(start);
5564 continue;
5565 }
5566 if (start == entry->vme_start) {
5567 /*
5568 * No need to clip. We don't want to cause
5569 * any unnecessary unnesting in this case...
5570 */
5571 } else {
5572 if ((flags & VM_MAP_REMOVE_NO_MAP_ALIGN) &&
5573 entry->map_aligned &&
5574 !VM_MAP_PAGE_ALIGNED(
5575 start,
5576 VM_MAP_PAGE_MASK(map))) {
5577 /*
5578 * The entry will no longer be
5579 * map-aligned after clipping
5580 * and the caller said it's OK.
5581 */
5582 entry->map_aligned = FALSE;
5583 }
5584 if (map == kalloc_map) {
5585 panic("vm_map_delete(%p,0x%llx,0x%llx):"
5586 " clipping %p at 0x%llx\n",
5587 map,
5588 (uint64_t)start,
5589 (uint64_t)end,
5590 entry,
5591 (uint64_t)start);
5592 }
5593 vm_map_clip_start(map, entry, start);
5594 }
5595
5596 /*
5597 * Fix the lookup hint now, rather than each
5598 * time through the loop.
5599 */
5600 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
5601 } else {
5602 if (map->pmap == kernel_pmap &&
5603 map->ref_count != 0) {
5604 panic("vm_map_delete(%p,0x%llx,0x%llx): "
5605 "no map entry at 0x%llx\n",
5606 map,
5607 (uint64_t)start,
5608 (uint64_t)end,
5609 (uint64_t)start);
5610 }
5611 entry = first_entry->vme_next;
5612 }
5613 break;
5614 }
5615 if (entry->superpage_size)
5616 end = SUPERPAGE_ROUND_UP(end);
5617
5618 need_wakeup = FALSE;
5619 /*
5620 * Step through all entries in this region
5621 */
5622 s = entry->vme_start;
5623 while ((entry != vm_map_to_entry(map)) && (s < end)) {
5624 /*
5625 * At this point, we have deleted all the memory entries
5626 * between "start" and "s". We still need to delete
5627 * all memory entries between "s" and "end".
5628 * While we were blocked and the map was unlocked, some
5629 * new memory entries could have been re-allocated between
5630 * "start" and "s" and we don't want to mess with those.
5631 * Some of those entries could even have been re-assembled
5632 * with an entry after "s" (in vm_map_simplify_entry()), so
5633 * we may have to vm_map_clip_start() again.
5634 */
5635
5636 if (entry->vme_start >= s) {
5637 /*
5638 * This entry starts on or after "s"
5639 * so no need to clip its start.
5640 */
5641 } else {
5642 /*
5643 * This entry has been re-assembled by a
5644 * vm_map_simplify_entry(). We need to
5645 * re-clip its start.
5646 */
5647 if ((flags & VM_MAP_REMOVE_NO_MAP_ALIGN) &&
5648 entry->map_aligned &&
5649 !VM_MAP_PAGE_ALIGNED(s,
5650 VM_MAP_PAGE_MASK(map))) {
5651 /*
5652 * The entry will no longer be map-aligned
5653 * after clipping and the caller said it's OK.
5654 */
5655 entry->map_aligned = FALSE;
5656 }
5657 if (map == kalloc_map) {
5658 panic("vm_map_delete(%p,0x%llx,0x%llx): "
5659 "clipping %p at 0x%llx\n",
5660 map,
5661 (uint64_t)start,
5662 (uint64_t)end,
5663 entry,
5664 (uint64_t)s);
5665 }
5666 vm_map_clip_start(map, entry, s);
5667 }
5668 if (entry->vme_end <= end) {
5669 /*
5670 * This entry is going away completely, so no need
5671 * to clip and possibly cause an unnecessary unnesting.
5672 */
5673 } else {
5674 if ((flags & VM_MAP_REMOVE_NO_MAP_ALIGN) &&
5675 entry->map_aligned &&
5676 !VM_MAP_PAGE_ALIGNED(end,
5677 VM_MAP_PAGE_MASK(map))) {
5678 /*
5679 * The entry will no longer be map-aligned
5680 * after clipping and the caller said it's OK.
5681 */
5682 entry->map_aligned = FALSE;
5683 }
5684 if (map == kalloc_map) {
5685 panic("vm_map_delete(%p,0x%llx,0x%llx): "
5686 "clipping %p at 0x%llx\n",
5687 map,
5688 (uint64_t)start,
5689 (uint64_t)end,
5690 entry,
5691 (uint64_t)end);
5692 }
5693 vm_map_clip_end(map, entry, end);
5694 }
5695
5696 if (entry->permanent) {
5697 panic("attempt to remove permanent VM map entry "
5698 "%p [0x%llx:0x%llx]\n",
5699 entry, (uint64_t) s, (uint64_t) end);
5700 }
5701
5702
5703 if (entry->in_transition) {
5704 wait_result_t wait_result;
5705
5706 /*
5707 * Another thread is wiring/unwiring this entry.
5708 * Let the other thread know we are waiting.
5709 */
5710 assert(s == entry->vme_start);
5711 entry->needs_wakeup = TRUE;
5712
5713 /*
5714 * wake up anybody waiting on entries that we have
5715 * already unwired/deleted.
5716 */
5717 if (need_wakeup) {
5718 vm_map_entry_wakeup(map);
5719 need_wakeup = FALSE;
5720 }
5721
5722 wait_result = vm_map_entry_wait(map, interruptible);
5723
5724 if (interruptible &&
5725 wait_result == THREAD_INTERRUPTED) {
5726 /*
5727 * We do not clear the needs_wakeup flag,
5728 * since we cannot tell if we were the only one.
5729 */
5730 return KERN_ABORTED;
5731 }
5732
5733 /*
5734 * The entry could have been clipped or it
5735 * may not exist anymore. Look it up again.
5736 */
5737 if (!vm_map_lookup_entry(map, s, &first_entry)) {
5738 assert((map != kernel_map) &&
5739 (!entry->is_sub_map));
5740 /*
5741 * User: use the next entry
5742 */
5743 entry = first_entry->vme_next;
5744 s = entry->vme_start;
5745 } else {
5746 entry = first_entry;
5747 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
5748 }
5749 last_timestamp = map->timestamp;
5750 continue;
5751 } /* end in_transition */
5752
5753 if (entry->wired_count) {
5754 boolean_t user_wire;
5755
5756 user_wire = entry->user_wired_count > 0;
5757
5758 /*
5759 * Remove a kernel wiring if requested
5760 */
5761 if (flags & VM_MAP_REMOVE_KUNWIRE) {
5762 entry->wired_count--;
5763 }
5764
5765 /*
5766 * Remove all user wirings for proper accounting
5767 */
5768 if (entry->user_wired_count > 0) {
5769 while (entry->user_wired_count)
5770 subtract_wire_counts(map, entry, user_wire);
5771 }
5772
5773 if (entry->wired_count != 0) {
5774 assert(map != kernel_map);
5775 /*
5776 * Cannot continue. Typical case is when
5777 * a user thread has physical io pending on
5778 * on this page. Either wait for the
5779 * kernel wiring to go away or return an
5780 * error.
5781 */
5782 if (flags & VM_MAP_REMOVE_WAIT_FOR_KWIRE) {
5783 wait_result_t wait_result;
5784
5785 assert(s == entry->vme_start);
5786 entry->needs_wakeup = TRUE;
5787 wait_result = vm_map_entry_wait(map,
5788 interruptible);
5789
5790 if (interruptible &&
5791 wait_result == THREAD_INTERRUPTED) {
5792 /*
5793 * We do not clear the
5794 * needs_wakeup flag, since we
5795 * cannot tell if we were the
5796 * only one.
5797 */
5798 return KERN_ABORTED;
5799 }
5800
5801 /*
5802 * The entry could have been clipped or
5803 * it may not exist anymore. Look it
5804 * up again.
5805 */
5806 if (!vm_map_lookup_entry(map, s,
5807 &first_entry)) {
5808 assert(map != kernel_map);
5809 /*
5810 * User: use the next entry
5811 */
5812 entry = first_entry->vme_next;
5813 s = entry->vme_start;
5814 } else {
5815 entry = first_entry;
5816 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
5817 }
5818 last_timestamp = map->timestamp;
5819 continue;
5820 }
5821 else {
5822 return KERN_FAILURE;
5823 }
5824 }
5825
5826 entry->in_transition = TRUE;
5827 /*
5828 * copy current entry. see comment in vm_map_wire()
5829 */
5830 tmp_entry = *entry;
5831 assert(s == entry->vme_start);
5832
5833 /*
5834 * We can unlock the map now. The in_transition
5835 * state guarentees existance of the entry.
5836 */
5837 vm_map_unlock(map);
5838
5839 if (tmp_entry.is_sub_map) {
5840 vm_map_t sub_map;
5841 vm_map_offset_t sub_start, sub_end;
5842 pmap_t pmap;
5843 vm_map_offset_t pmap_addr;
5844
5845
5846 sub_map = tmp_entry.object.sub_map;
5847 sub_start = tmp_entry.offset;
5848 sub_end = sub_start + (tmp_entry.vme_end -
5849 tmp_entry.vme_start);
5850 if (tmp_entry.use_pmap) {
5851 pmap = sub_map->pmap;
5852 pmap_addr = tmp_entry.vme_start;
5853 } else {
5854 pmap = map->pmap;
5855 pmap_addr = tmp_entry.vme_start;
5856 }
5857 (void) vm_map_unwire_nested(sub_map,
5858 sub_start, sub_end,
5859 user_wire,
5860 pmap, pmap_addr);
5861 } else {
5862
5863 if (tmp_entry.object.vm_object == kernel_object) {
5864 pmap_protect_options(
5865 map->pmap,
5866 tmp_entry.vme_start,
5867 tmp_entry.vme_end,
5868 VM_PROT_NONE,
5869 PMAP_OPTIONS_REMOVE,
5870 NULL);
5871 }
5872 vm_fault_unwire(map, &tmp_entry,
5873 tmp_entry.object.vm_object == kernel_object,
5874 map->pmap, tmp_entry.vme_start);
5875 }
5876
5877 vm_map_lock(map);
5878
5879 if (last_timestamp+1 != map->timestamp) {
5880 /*
5881 * Find the entry again. It could have
5882 * been clipped after we unlocked the map.
5883 */
5884 if (!vm_map_lookup_entry(map, s, &first_entry)){
5885 assert((map != kernel_map) &&
5886 (!entry->is_sub_map));
5887 first_entry = first_entry->vme_next;
5888 s = first_entry->vme_start;
5889 } else {
5890 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
5891 }
5892 } else {
5893 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
5894 first_entry = entry;
5895 }
5896
5897 last_timestamp = map->timestamp;
5898
5899 entry = first_entry;
5900 while ((entry != vm_map_to_entry(map)) &&
5901 (entry->vme_start < tmp_entry.vme_end)) {
5902 assert(entry->in_transition);
5903 entry->in_transition = FALSE;
5904 if (entry->needs_wakeup) {
5905 entry->needs_wakeup = FALSE;
5906 need_wakeup = TRUE;
5907 }
5908 entry = entry->vme_next;
5909 }
5910 /*
5911 * We have unwired the entry(s). Go back and
5912 * delete them.
5913 */
5914 entry = first_entry;
5915 continue;
5916 }
5917
5918 /* entry is unwired */
5919 assert(entry->wired_count == 0);
5920 assert(entry->user_wired_count == 0);
5921
5922 assert(s == entry->vme_start);
5923
5924 if (flags & VM_MAP_REMOVE_NO_PMAP_CLEANUP) {
5925 /*
5926 * XXX with the VM_MAP_REMOVE_SAVE_ENTRIES flag to
5927 * vm_map_delete(), some map entries might have been
5928 * transferred to a "zap_map", which doesn't have a
5929 * pmap. The original pmap has already been flushed
5930 * in the vm_map_delete() call targeting the original
5931 * map, but when we get to destroying the "zap_map",
5932 * we don't have any pmap to flush, so let's just skip
5933 * all this.
5934 */
5935 } else if (entry->is_sub_map) {
5936 if (entry->use_pmap) {
5937#ifndef NO_NESTED_PMAP
5938 pmap_unnest(map->pmap,
5939 (addr64_t)entry->vme_start,
5940 entry->vme_end - entry->vme_start);
5941#endif /* NO_NESTED_PMAP */
5942 if ((map->mapped_in_other_pmaps) && (map->ref_count)) {
5943 /* clean up parent map/maps */
5944 vm_map_submap_pmap_clean(
5945 map, entry->vme_start,
5946 entry->vme_end,
5947 entry->object.sub_map,
5948 entry->offset);
5949 }
5950 } else {
5951 vm_map_submap_pmap_clean(
5952 map, entry->vme_start, entry->vme_end,
5953 entry->object.sub_map,
5954 entry->offset);
5955 }
5956 } else if (entry->object.vm_object != kernel_object &&
5957 entry->object.vm_object != compressor_object) {
5958 object = entry->object.vm_object;
5959 if ((map->mapped_in_other_pmaps) && (map->ref_count)) {
5960 vm_object_pmap_protect_options(
5961 object, entry->offset,
5962 entry->vme_end - entry->vme_start,
5963 PMAP_NULL,
5964 entry->vme_start,
5965 VM_PROT_NONE,
5966 PMAP_OPTIONS_REMOVE);
5967 } else if ((entry->object.vm_object !=
5968 VM_OBJECT_NULL) ||
5969 (map->pmap == kernel_pmap)) {
5970 /* Remove translations associated
5971 * with this range unless the entry
5972 * does not have an object, or
5973 * it's the kernel map or a descendant
5974 * since the platform could potentially
5975 * create "backdoor" mappings invisible
5976 * to the VM. It is expected that
5977 * objectless, non-kernel ranges
5978 * do not have such VM invisible
5979 * translations.
5980 */
5981 pmap_remove_options(map->pmap,
5982 (addr64_t)entry->vme_start,
5983 (addr64_t)entry->vme_end,
5984 PMAP_OPTIONS_REMOVE);
5985 }
5986 }
5987
5988 if (entry->iokit_acct) {
5989 /* alternate accounting */
5990 vm_map_iokit_unmapped_region(map,
5991 (entry->vme_end -
5992 entry->vme_start));
5993 entry->iokit_acct = FALSE;
5994 }
5995
5996 /*
5997 * All pmap mappings for this map entry must have been
5998 * cleared by now.
5999 */
6000#if DEBUG
6001 assert(vm_map_pmap_is_empty(map,
6002 entry->vme_start,
6003 entry->vme_end));
6004#endif /* DEBUG */
6005
6006 next = entry->vme_next;
6007
6008 if (map->pmap == kernel_pmap &&
6009 map->ref_count != 0 &&
6010 entry->vme_end < end &&
6011 (next == vm_map_to_entry(map) ||
6012 next->vme_start != entry->vme_end)) {
6013 panic("vm_map_delete(%p,0x%llx,0x%llx): "
6014 "hole after %p at 0x%llx\n",
6015 map,
6016 (uint64_t)start,
6017 (uint64_t)end,
6018 entry,
6019 (uint64_t)entry->vme_end);
6020 }
6021
6022 s = next->vme_start;
6023 last_timestamp = map->timestamp;
6024
6025 if ((flags & VM_MAP_REMOVE_SAVE_ENTRIES) &&
6026 zap_map != VM_MAP_NULL) {
6027 vm_map_size_t entry_size;
6028 /*
6029 * The caller wants to save the affected VM map entries
6030 * into the "zap_map". The caller will take care of
6031 * these entries.
6032 */
6033 /* unlink the entry from "map" ... */
6034 vm_map_store_entry_unlink(map, entry);
6035 /* ... and add it to the end of the "zap_map" */
6036 vm_map_store_entry_link(zap_map,
6037 vm_map_last_entry(zap_map),
6038 entry);
6039 entry_size = entry->vme_end - entry->vme_start;
6040 map->size -= entry_size;
6041 zap_map->size += entry_size;
6042 /* we didn't unlock the map, so no timestamp increase */
6043 last_timestamp--;
6044 } else {
6045 vm_map_entry_delete(map, entry);
6046 /* vm_map_entry_delete unlocks the map */
6047 vm_map_lock(map);
6048 }
6049
6050 entry = next;
6051
6052 if(entry == vm_map_to_entry(map)) {
6053 break;
6054 }
6055 if (last_timestamp+1 != map->timestamp) {
6056 /*
6057 * we are responsible for deleting everything
6058 * from the give space, if someone has interfered
6059 * we pick up where we left off, back fills should
6060 * be all right for anyone except map_delete and
6061 * we have to assume that the task has been fully
6062 * disabled before we get here
6063 */
6064 if (!vm_map_lookup_entry(map, s, &entry)){
6065 entry = entry->vme_next;
6066 s = entry->vme_start;
6067 } else {
6068 SAVE_HINT_MAP_WRITE(map, entry->vme_prev);
6069 }
6070 /*
6071 * others can not only allocate behind us, we can
6072 * also see coalesce while we don't have the map lock
6073 */
6074 if(entry == vm_map_to_entry(map)) {
6075 break;
6076 }
6077 }
6078 last_timestamp = map->timestamp;
6079 }
6080
6081 if (map->wait_for_space)
6082 thread_wakeup((event_t) map);
6083 /*
6084 * wake up anybody waiting on entries that we have already deleted.
6085 */
6086 if (need_wakeup)
6087 vm_map_entry_wakeup(map);
6088
6089 return KERN_SUCCESS;
6090}
6091
6092/*
6093 * vm_map_remove:
6094 *
6095 * Remove the given address range from the target map.
6096 * This is the exported form of vm_map_delete.
6097 */
6098kern_return_t
6099vm_map_remove(
6100 register vm_map_t map,
6101 register vm_map_offset_t start,
6102 register vm_map_offset_t end,
6103 register boolean_t flags)
6104{
6105 register kern_return_t result;
6106
6107 vm_map_lock(map);
6108 VM_MAP_RANGE_CHECK(map, start, end);
6109 /*
6110 * For the zone_map, the kernel controls the allocation/freeing of memory.
6111 * Any free to the zone_map should be within the bounds of the map and
6112 * should free up memory. If the VM_MAP_RANGE_CHECK() silently converts a
6113 * free to the zone_map into a no-op, there is a problem and we should
6114 * panic.
6115 */
6116 if ((map == zone_map) && (start == end))
6117 panic("Nothing being freed to the zone_map. start = end = %p\n", (void *)start);
6118 result = vm_map_delete(map, start, end, flags, VM_MAP_NULL);
6119 vm_map_unlock(map);
6120
6121 return(result);
6122}
6123
6124
6125/*
6126 * Routine: vm_map_copy_discard
6127 *
6128 * Description:
6129 * Dispose of a map copy object (returned by
6130 * vm_map_copyin).
6131 */
6132void
6133vm_map_copy_discard(
6134 vm_map_copy_t copy)
6135{
6136 if (copy == VM_MAP_COPY_NULL)
6137 return;
6138
6139 switch (copy->type) {
6140 case VM_MAP_COPY_ENTRY_LIST:
6141 while (vm_map_copy_first_entry(copy) !=
6142 vm_map_copy_to_entry(copy)) {
6143 vm_map_entry_t entry = vm_map_copy_first_entry(copy);
6144
6145 vm_map_copy_entry_unlink(copy, entry);
6146 if (entry->is_sub_map) {
6147 vm_map_deallocate(entry->object.sub_map);
6148 } else {
6149 vm_object_deallocate(entry->object.vm_object);
6150 }
6151 vm_map_copy_entry_dispose(copy, entry);
6152 }
6153 break;
6154 case VM_MAP_COPY_OBJECT:
6155 vm_object_deallocate(copy->cpy_object);
6156 break;
6157 case VM_MAP_COPY_KERNEL_BUFFER:
6158
6159 /*
6160 * The vm_map_copy_t and possibly the data buffer were
6161 * allocated by a single call to kalloc(), i.e. the
6162 * vm_map_copy_t was not allocated out of the zone.
6163 */
6164 kfree(copy, copy->cpy_kalloc_size);
6165 return;
6166 }
6167 zfree(vm_map_copy_zone, copy);
6168}
6169
6170/*
6171 * Routine: vm_map_copy_copy
6172 *
6173 * Description:
6174 * Move the information in a map copy object to
6175 * a new map copy object, leaving the old one
6176 * empty.
6177 *
6178 * This is used by kernel routines that need
6179 * to look at out-of-line data (in copyin form)
6180 * before deciding whether to return SUCCESS.
6181 * If the routine returns FAILURE, the original
6182 * copy object will be deallocated; therefore,
6183 * these routines must make a copy of the copy
6184 * object and leave the original empty so that
6185 * deallocation will not fail.
6186 */
6187vm_map_copy_t
6188vm_map_copy_copy(
6189 vm_map_copy_t copy)
6190{
6191 vm_map_copy_t new_copy;
6192
6193 if (copy == VM_MAP_COPY_NULL)
6194 return VM_MAP_COPY_NULL;
6195
6196 /*
6197 * Allocate a new copy object, and copy the information
6198 * from the old one into it.
6199 */
6200
6201 new_copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
6202 new_copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
6203 *new_copy = *copy;
6204
6205 if (copy->type == VM_MAP_COPY_ENTRY_LIST) {
6206 /*
6207 * The links in the entry chain must be
6208 * changed to point to the new copy object.
6209 */
6210 vm_map_copy_first_entry(copy)->vme_prev
6211 = vm_map_copy_to_entry(new_copy);
6212 vm_map_copy_last_entry(copy)->vme_next
6213 = vm_map_copy_to_entry(new_copy);
6214 }
6215
6216 /*
6217 * Change the old copy object into one that contains
6218 * nothing to be deallocated.
6219 */
6220 copy->type = VM_MAP_COPY_OBJECT;
6221 copy->cpy_object = VM_OBJECT_NULL;
6222
6223 /*
6224 * Return the new object.
6225 */
6226 return new_copy;
6227}
6228
6229static kern_return_t
6230vm_map_overwrite_submap_recurse(
6231 vm_map_t dst_map,
6232 vm_map_offset_t dst_addr,
6233 vm_map_size_t dst_size)
6234{
6235 vm_map_offset_t dst_end;
6236 vm_map_entry_t tmp_entry;
6237 vm_map_entry_t entry;
6238 kern_return_t result;
6239 boolean_t encountered_sub_map = FALSE;
6240
6241
6242
6243 /*
6244 * Verify that the destination is all writeable
6245 * initially. We have to trunc the destination
6246 * address and round the copy size or we'll end up
6247 * splitting entries in strange ways.
6248 */
6249
6250 dst_end = vm_map_round_page(dst_addr + dst_size,
6251 VM_MAP_PAGE_MASK(dst_map));
6252 vm_map_lock(dst_map);
6253
6254start_pass_1:
6255 if (!vm_map_lookup_entry(dst_map, dst_addr, &tmp_entry)) {
6256 vm_map_unlock(dst_map);
6257 return(KERN_INVALID_ADDRESS);
6258 }
6259
6260 vm_map_clip_start(dst_map,
6261 tmp_entry,
6262 vm_map_trunc_page(dst_addr,
6263 VM_MAP_PAGE_MASK(dst_map)));
6264 if (tmp_entry->is_sub_map) {
6265 /* clipping did unnest if needed */
6266 assert(!tmp_entry->use_pmap);
6267 }
6268
6269 for (entry = tmp_entry;;) {
6270 vm_map_entry_t next;
6271
6272 next = entry->vme_next;
6273 while(entry->is_sub_map) {
6274 vm_map_offset_t sub_start;
6275 vm_map_offset_t sub_end;
6276 vm_map_offset_t local_end;
6277
6278 if (entry->in_transition) {
6279 /*
6280 * Say that we are waiting, and wait for entry.
6281 */
6282 entry->needs_wakeup = TRUE;
6283 vm_map_entry_wait(dst_map, THREAD_UNINT);
6284
6285 goto start_pass_1;
6286 }
6287
6288 encountered_sub_map = TRUE;
6289 sub_start = entry->offset;
6290
6291 if(entry->vme_end < dst_end)
6292 sub_end = entry->vme_end;
6293 else
6294 sub_end = dst_end;
6295 sub_end -= entry->vme_start;
6296 sub_end += entry->offset;
6297 local_end = entry->vme_end;
6298 vm_map_unlock(dst_map);
6299
6300 result = vm_map_overwrite_submap_recurse(
6301 entry->object.sub_map,
6302 sub_start,
6303 sub_end - sub_start);
6304
6305 if(result != KERN_SUCCESS)
6306 return result;
6307 if (dst_end <= entry->vme_end)
6308 return KERN_SUCCESS;
6309 vm_map_lock(dst_map);
6310 if(!vm_map_lookup_entry(dst_map, local_end,
6311 &tmp_entry)) {
6312 vm_map_unlock(dst_map);
6313 return(KERN_INVALID_ADDRESS);
6314 }
6315 entry = tmp_entry;
6316 next = entry->vme_next;
6317 }
6318
6319 if ( ! (entry->protection & VM_PROT_WRITE)) {
6320 vm_map_unlock(dst_map);
6321 return(KERN_PROTECTION_FAILURE);
6322 }
6323
6324 /*
6325 * If the entry is in transition, we must wait
6326 * for it to exit that state. Anything could happen
6327 * when we unlock the map, so start over.
6328 */
6329 if (entry->in_transition) {
6330
6331 /*
6332 * Say that we are waiting, and wait for entry.
6333 */
6334 entry->needs_wakeup = TRUE;
6335 vm_map_entry_wait(dst_map, THREAD_UNINT);
6336
6337 goto start_pass_1;
6338 }
6339
6340/*
6341 * our range is contained completely within this map entry
6342 */
6343 if (dst_end <= entry->vme_end) {
6344 vm_map_unlock(dst_map);
6345 return KERN_SUCCESS;
6346 }
6347/*
6348 * check that range specified is contiguous region
6349 */
6350 if ((next == vm_map_to_entry(dst_map)) ||
6351 (next->vme_start != entry->vme_end)) {
6352 vm_map_unlock(dst_map);
6353 return(KERN_INVALID_ADDRESS);
6354 }
6355
6356 /*
6357 * Check for permanent objects in the destination.
6358 */
6359 if ((entry->object.vm_object != VM_OBJECT_NULL) &&
6360 ((!entry->object.vm_object->internal) ||
6361 (entry->object.vm_object->true_share))) {
6362 if(encountered_sub_map) {
6363 vm_map_unlock(dst_map);
6364 return(KERN_FAILURE);
6365 }
6366 }
6367
6368
6369 entry = next;
6370 }/* for */
6371 vm_map_unlock(dst_map);
6372 return(KERN_SUCCESS);
6373}
6374
6375/*
6376 * Routine: vm_map_copy_overwrite
6377 *
6378 * Description:
6379 * Copy the memory described by the map copy
6380 * object (copy; returned by vm_map_copyin) onto
6381 * the specified destination region (dst_map, dst_addr).
6382 * The destination must be writeable.
6383 *
6384 * Unlike vm_map_copyout, this routine actually
6385 * writes over previously-mapped memory. If the
6386 * previous mapping was to a permanent (user-supplied)
6387 * memory object, it is preserved.
6388 *
6389 * The attributes (protection and inheritance) of the
6390 * destination region are preserved.
6391 *
6392 * If successful, consumes the copy object.
6393 * Otherwise, the caller is responsible for it.
6394 *
6395 * Implementation notes:
6396 * To overwrite aligned temporary virtual memory, it is
6397 * sufficient to remove the previous mapping and insert
6398 * the new copy. This replacement is done either on
6399 * the whole region (if no permanent virtual memory
6400 * objects are embedded in the destination region) or
6401 * in individual map entries.
6402 *
6403 * To overwrite permanent virtual memory , it is necessary
6404 * to copy each page, as the external memory management
6405 * interface currently does not provide any optimizations.
6406 *
6407 * Unaligned memory also has to be copied. It is possible
6408 * to use 'vm_trickery' to copy the aligned data. This is
6409 * not done but not hard to implement.
6410 *
6411 * Once a page of permanent memory has been overwritten,
6412 * it is impossible to interrupt this function; otherwise,
6413 * the call would be neither atomic nor location-independent.
6414 * The kernel-state portion of a user thread must be
6415 * interruptible.
6416 *
6417 * It may be expensive to forward all requests that might
6418 * overwrite permanent memory (vm_write, vm_copy) to
6419 * uninterruptible kernel threads. This routine may be
6420 * called by interruptible threads; however, success is
6421 * not guaranteed -- if the request cannot be performed
6422 * atomically and interruptibly, an error indication is
6423 * returned.
6424 */
6425
6426static kern_return_t
6427vm_map_copy_overwrite_nested(
6428 vm_map_t dst_map,
6429 vm_map_address_t dst_addr,
6430 vm_map_copy_t copy,
6431 boolean_t interruptible,
6432 pmap_t pmap,
6433 boolean_t discard_on_success)
6434{
6435 vm_map_offset_t dst_end;
6436 vm_map_entry_t tmp_entry;
6437 vm_map_entry_t entry;
6438 kern_return_t kr;
6439 boolean_t aligned = TRUE;
6440 boolean_t contains_permanent_objects = FALSE;
6441 boolean_t encountered_sub_map = FALSE;
6442 vm_map_offset_t base_addr;
6443 vm_map_size_t copy_size;
6444 vm_map_size_t total_size;
6445
6446
6447 /*
6448 * Check for null copy object.
6449 */
6450
6451 if (copy == VM_MAP_COPY_NULL)
6452 return(KERN_SUCCESS);
6453
6454 /*
6455 * Check for special kernel buffer allocated
6456 * by new_ipc_kmsg_copyin.
6457 */
6458
6459 if (copy->type == VM_MAP_COPY_KERNEL_BUFFER) {
6460 return(vm_map_copyout_kernel_buffer(
6461 dst_map, &dst_addr,
6462 copy, TRUE, discard_on_success));
6463 }
6464
6465 /*
6466 * Only works for entry lists at the moment. Will
6467 * support page lists later.
6468 */
6469
6470 assert(copy->type == VM_MAP_COPY_ENTRY_LIST);
6471
6472 if (copy->size == 0) {
6473 if (discard_on_success)
6474 vm_map_copy_discard(copy);
6475 return(KERN_SUCCESS);
6476 }
6477
6478 /*
6479 * Verify that the destination is all writeable
6480 * initially. We have to trunc the destination
6481 * address and round the copy size or we'll end up
6482 * splitting entries in strange ways.
6483 */
6484
6485 if (!VM_MAP_PAGE_ALIGNED(copy->size,
6486 VM_MAP_PAGE_MASK(dst_map)) ||
6487 !VM_MAP_PAGE_ALIGNED(copy->offset,
6488 VM_MAP_PAGE_MASK(dst_map)) ||
6489 !VM_MAP_PAGE_ALIGNED(dst_addr,
6490 VM_MAP_PAGE_MASK(dst_map)))
6491 {
6492 aligned = FALSE;
6493 dst_end = vm_map_round_page(dst_addr + copy->size,
6494 VM_MAP_PAGE_MASK(dst_map));
6495 } else {
6496 dst_end = dst_addr + copy->size;
6497 }
6498
6499 vm_map_lock(dst_map);
6500
6501 /* LP64todo - remove this check when vm_map_commpage64()
6502 * no longer has to stuff in a map_entry for the commpage
6503 * above the map's max_offset.
6504 */
6505 if (dst_addr >= dst_map->max_offset) {
6506 vm_map_unlock(dst_map);
6507 return(KERN_INVALID_ADDRESS);
6508 }
6509
6510start_pass_1:
6511 if (!vm_map_lookup_entry(dst_map, dst_addr, &tmp_entry)) {
6512 vm_map_unlock(dst_map);
6513 return(KERN_INVALID_ADDRESS);
6514 }
6515 vm_map_clip_start(dst_map,
6516 tmp_entry,
6517 vm_map_trunc_page(dst_addr,
6518 VM_MAP_PAGE_MASK(dst_map)));
6519 for (entry = tmp_entry;;) {
6520 vm_map_entry_t next = entry->vme_next;
6521
6522 while(entry->is_sub_map) {
6523 vm_map_offset_t sub_start;
6524 vm_map_offset_t sub_end;
6525 vm_map_offset_t local_end;
6526
6527 if (entry->in_transition) {
6528
6529 /*
6530 * Say that we are waiting, and wait for entry.
6531 */
6532 entry->needs_wakeup = TRUE;
6533 vm_map_entry_wait(dst_map, THREAD_UNINT);
6534
6535 goto start_pass_1;
6536 }
6537
6538 local_end = entry->vme_end;
6539 if (!(entry->needs_copy)) {
6540 /* if needs_copy we are a COW submap */
6541 /* in such a case we just replace so */
6542 /* there is no need for the follow- */
6543 /* ing check. */
6544 encountered_sub_map = TRUE;
6545 sub_start = entry->offset;
6546
6547 if(entry->vme_end < dst_end)
6548 sub_end = entry->vme_end;
6549 else
6550 sub_end = dst_end;
6551 sub_end -= entry->vme_start;
6552 sub_end += entry->offset;
6553 vm_map_unlock(dst_map);
6554
6555 kr = vm_map_overwrite_submap_recurse(
6556 entry->object.sub_map,
6557 sub_start,
6558 sub_end - sub_start);
6559 if(kr != KERN_SUCCESS)
6560 return kr;
6561 vm_map_lock(dst_map);
6562 }
6563
6564 if (dst_end <= entry->vme_end)
6565 goto start_overwrite;
6566 if(!vm_map_lookup_entry(dst_map, local_end,
6567 &entry)) {
6568 vm_map_unlock(dst_map);
6569 return(KERN_INVALID_ADDRESS);
6570 }
6571 next = entry->vme_next;
6572 }
6573
6574 if ( ! (entry->protection & VM_PROT_WRITE)) {
6575 vm_map_unlock(dst_map);
6576 return(KERN_PROTECTION_FAILURE);
6577 }
6578
6579 /*
6580 * If the entry is in transition, we must wait
6581 * for it to exit that state. Anything could happen
6582 * when we unlock the map, so start over.
6583 */
6584 if (entry->in_transition) {
6585
6586 /*
6587 * Say that we are waiting, and wait for entry.
6588 */
6589 entry->needs_wakeup = TRUE;
6590 vm_map_entry_wait(dst_map, THREAD_UNINT);
6591
6592 goto start_pass_1;
6593 }
6594
6595/*
6596 * our range is contained completely within this map entry
6597 */
6598 if (dst_end <= entry->vme_end)
6599 break;
6600/*
6601 * check that range specified is contiguous region
6602 */
6603 if ((next == vm_map_to_entry(dst_map)) ||
6604 (next->vme_start != entry->vme_end)) {
6605 vm_map_unlock(dst_map);
6606 return(KERN_INVALID_ADDRESS);
6607 }
6608
6609
6610 /*
6611 * Check for permanent objects in the destination.
6612 */
6613 if ((entry->object.vm_object != VM_OBJECT_NULL) &&
6614 ((!entry->object.vm_object->internal) ||
6615 (entry->object.vm_object->true_share))) {
6616 contains_permanent_objects = TRUE;
6617 }
6618
6619 entry = next;
6620 }/* for */
6621
6622start_overwrite:
6623 /*
6624 * If there are permanent objects in the destination, then
6625 * the copy cannot be interrupted.
6626 */
6627
6628 if (interruptible && contains_permanent_objects) {
6629 vm_map_unlock(dst_map);
6630 return(KERN_FAILURE); /* XXX */
6631 }
6632
6633 /*
6634 *
6635 * Make a second pass, overwriting the data
6636 * At the beginning of each loop iteration,
6637 * the next entry to be overwritten is "tmp_entry"
6638 * (initially, the value returned from the lookup above),
6639 * and the starting address expected in that entry
6640 * is "start".
6641 */
6642
6643 total_size = copy->size;
6644 if(encountered_sub_map) {
6645 copy_size = 0;
6646 /* re-calculate tmp_entry since we've had the map */
6647 /* unlocked */
6648 if (!vm_map_lookup_entry( dst_map, dst_addr, &tmp_entry)) {
6649 vm_map_unlock(dst_map);
6650 return(KERN_INVALID_ADDRESS);
6651 }
6652 } else {
6653 copy_size = copy->size;
6654 }
6655
6656 base_addr = dst_addr;
6657 while(TRUE) {
6658 /* deconstruct the copy object and do in parts */
6659 /* only in sub_map, interruptable case */
6660 vm_map_entry_t copy_entry;
6661 vm_map_entry_t previous_prev = VM_MAP_ENTRY_NULL;
6662 vm_map_entry_t next_copy = VM_MAP_ENTRY_NULL;
6663 int nentries;
6664 int remaining_entries = 0;
6665 vm_map_offset_t new_offset = 0;
6666
6667 for (entry = tmp_entry; copy_size == 0;) {
6668 vm_map_entry_t next;
6669
6670 next = entry->vme_next;
6671
6672 /* tmp_entry and base address are moved along */
6673 /* each time we encounter a sub-map. Otherwise */
6674 /* entry can outpase tmp_entry, and the copy_size */
6675 /* may reflect the distance between them */
6676 /* if the current entry is found to be in transition */
6677 /* we will start over at the beginning or the last */
6678 /* encounter of a submap as dictated by base_addr */
6679 /* we will zero copy_size accordingly. */
6680 if (entry->in_transition) {
6681 /*
6682 * Say that we are waiting, and wait for entry.
6683 */
6684 entry->needs_wakeup = TRUE;
6685 vm_map_entry_wait(dst_map, THREAD_UNINT);
6686
6687 if(!vm_map_lookup_entry(dst_map, base_addr,
6688 &tmp_entry)) {
6689 vm_map_unlock(dst_map);
6690 return(KERN_INVALID_ADDRESS);
6691 }
6692 copy_size = 0;
6693 entry = tmp_entry;
6694 continue;
6695 }
6696 if(entry->is_sub_map) {
6697 vm_map_offset_t sub_start;
6698 vm_map_offset_t sub_end;
6699 vm_map_offset_t local_end;
6700
6701 if (entry->needs_copy) {
6702 /* if this is a COW submap */
6703 /* just back the range with a */
6704 /* anonymous entry */
6705 if(entry->vme_end < dst_end)
6706 sub_end = entry->vme_end;
6707 else
6708 sub_end = dst_end;
6709 if(entry->vme_start < base_addr)
6710 sub_start = base_addr;
6711 else
6712 sub_start = entry->vme_start;
6713 vm_map_clip_end(
6714 dst_map, entry, sub_end);
6715 vm_map_clip_start(
6716 dst_map, entry, sub_start);
6717 assert(!entry->use_pmap);
6718 entry->is_sub_map = FALSE;
6719 vm_map_deallocate(
6720 entry->object.sub_map);
6721 entry->object.sub_map = NULL;
6722 entry->is_shared = FALSE;
6723 entry->needs_copy = FALSE;
6724 entry->offset = 0;
6725 /*
6726 * XXX FBDP
6727 * We should propagate the protections
6728 * of the submap entry here instead
6729 * of forcing them to VM_PROT_ALL...
6730 * Or better yet, we should inherit
6731 * the protection of the copy_entry.
6732 */
6733 entry->protection = VM_PROT_ALL;
6734 entry->max_protection = VM_PROT_ALL;
6735 entry->wired_count = 0;
6736 entry->user_wired_count = 0;
6737 if(entry->inheritance
6738 == VM_INHERIT_SHARE)
6739 entry->inheritance = VM_INHERIT_COPY;
6740 continue;
6741 }
6742 /* first take care of any non-sub_map */
6743 /* entries to send */
6744 if(base_addr < entry->vme_start) {
6745 /* stuff to send */
6746 copy_size =
6747 entry->vme_start - base_addr;
6748 break;
6749 }
6750 sub_start = entry->offset;
6751
6752 if(entry->vme_end < dst_end)
6753 sub_end = entry->vme_end;
6754 else
6755 sub_end = dst_end;
6756 sub_end -= entry->vme_start;
6757 sub_end += entry->offset;
6758 local_end = entry->vme_end;
6759 vm_map_unlock(dst_map);
6760 copy_size = sub_end - sub_start;
6761
6762 /* adjust the copy object */
6763 if (total_size > copy_size) {
6764 vm_map_size_t local_size = 0;
6765 vm_map_size_t entry_size;
6766
6767 nentries = 1;
6768 new_offset = copy->offset;
6769 copy_entry = vm_map_copy_first_entry(copy);
6770 while(copy_entry !=
6771 vm_map_copy_to_entry(copy)){
6772 entry_size = copy_entry->vme_end -
6773 copy_entry->vme_start;
6774 if((local_size < copy_size) &&
6775 ((local_size + entry_size)
6776 >= copy_size)) {
6777 vm_map_copy_clip_end(copy,
6778 copy_entry,
6779 copy_entry->vme_start +
6780 (copy_size - local_size));
6781 entry_size = copy_entry->vme_end -
6782 copy_entry->vme_start;
6783 local_size += entry_size;
6784 new_offset += entry_size;
6785 }
6786 if(local_size >= copy_size) {
6787 next_copy = copy_entry->vme_next;
6788 copy_entry->vme_next =
6789 vm_map_copy_to_entry(copy);
6790 previous_prev =
6791 copy->cpy_hdr.links.prev;
6792 copy->cpy_hdr.links.prev = copy_entry;
6793 copy->size = copy_size;
6794 remaining_entries =
6795 copy->cpy_hdr.nentries;
6796 remaining_entries -= nentries;
6797 copy->cpy_hdr.nentries = nentries;
6798 break;
6799 } else {
6800 local_size += entry_size;
6801 new_offset += entry_size;
6802 nentries++;
6803 }
6804 copy_entry = copy_entry->vme_next;
6805 }
6806 }
6807
6808 if((entry->use_pmap) && (pmap == NULL)) {
6809 kr = vm_map_copy_overwrite_nested(
6810 entry->object.sub_map,
6811 sub_start,
6812 copy,
6813 interruptible,
6814 entry->object.sub_map->pmap,
6815 TRUE);
6816 } else if (pmap != NULL) {
6817 kr = vm_map_copy_overwrite_nested(
6818 entry->object.sub_map,
6819 sub_start,
6820 copy,
6821 interruptible, pmap,
6822 TRUE);
6823 } else {
6824 kr = vm_map_copy_overwrite_nested(
6825 entry->object.sub_map,
6826 sub_start,
6827 copy,
6828 interruptible,
6829 dst_map->pmap,
6830 TRUE);
6831 }
6832 if(kr != KERN_SUCCESS) {
6833 if(next_copy != NULL) {
6834 copy->cpy_hdr.nentries +=
6835 remaining_entries;
6836 copy->cpy_hdr.links.prev->vme_next =
6837 next_copy;
6838 copy->cpy_hdr.links.prev
6839 = previous_prev;
6840 copy->size = total_size;
6841 }
6842 return kr;
6843 }
6844 if (dst_end <= local_end) {
6845 return(KERN_SUCCESS);
6846 }
6847 /* otherwise copy no longer exists, it was */
6848 /* destroyed after successful copy_overwrite */
6849 copy = (vm_map_copy_t)
6850 zalloc(vm_map_copy_zone);
6851 copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
6852 vm_map_copy_first_entry(copy) =
6853 vm_map_copy_last_entry(copy) =
6854 vm_map_copy_to_entry(copy);
6855 copy->type = VM_MAP_COPY_ENTRY_LIST;
6856 copy->offset = new_offset;
6857
6858 /*
6859 * XXX FBDP
6860 * this does not seem to deal with
6861 * the VM map store (R&B tree)
6862 */
6863
6864 total_size -= copy_size;
6865 copy_size = 0;
6866 /* put back remainder of copy in container */
6867 if(next_copy != NULL) {
6868 copy->cpy_hdr.nentries = remaining_entries;
6869 copy->cpy_hdr.links.next = next_copy;
6870 copy->cpy_hdr.links.prev = previous_prev;
6871 copy->size = total_size;
6872 next_copy->vme_prev =
6873 vm_map_copy_to_entry(copy);
6874 next_copy = NULL;
6875 }
6876 base_addr = local_end;
6877 vm_map_lock(dst_map);
6878 if(!vm_map_lookup_entry(dst_map,
6879 local_end, &tmp_entry)) {
6880 vm_map_unlock(dst_map);
6881 return(KERN_INVALID_ADDRESS);
6882 }
6883 entry = tmp_entry;
6884 continue;
6885 }
6886 if (dst_end <= entry->vme_end) {
6887 copy_size = dst_end - base_addr;
6888 break;
6889 }
6890
6891 if ((next == vm_map_to_entry(dst_map)) ||
6892 (next->vme_start != entry->vme_end)) {
6893 vm_map_unlock(dst_map);
6894 return(KERN_INVALID_ADDRESS);
6895 }
6896
6897 entry = next;
6898 }/* for */
6899
6900 next_copy = NULL;
6901 nentries = 1;
6902
6903 /* adjust the copy object */
6904 if (total_size > copy_size) {
6905 vm_map_size_t local_size = 0;
6906 vm_map_size_t entry_size;
6907
6908 new_offset = copy->offset;
6909 copy_entry = vm_map_copy_first_entry(copy);
6910 while(copy_entry != vm_map_copy_to_entry(copy)) {
6911 entry_size = copy_entry->vme_end -
6912 copy_entry->vme_start;
6913 if((local_size < copy_size) &&
6914 ((local_size + entry_size)
6915 >= copy_size)) {
6916 vm_map_copy_clip_end(copy, copy_entry,
6917 copy_entry->vme_start +
6918 (copy_size - local_size));
6919 entry_size = copy_entry->vme_end -
6920 copy_entry->vme_start;
6921 local_size += entry_size;
6922 new_offset += entry_size;
6923 }
6924 if(local_size >= copy_size) {
6925 next_copy = copy_entry->vme_next;
6926 copy_entry->vme_next =
6927 vm_map_copy_to_entry(copy);
6928 previous_prev =
6929 copy->cpy_hdr.links.prev;
6930 copy->cpy_hdr.links.prev = copy_entry;
6931 copy->size = copy_size;
6932 remaining_entries =
6933 copy->cpy_hdr.nentries;
6934 remaining_entries -= nentries;
6935 copy->cpy_hdr.nentries = nentries;
6936 break;
6937 } else {
6938 local_size += entry_size;
6939 new_offset += entry_size;
6940 nentries++;
6941 }
6942 copy_entry = copy_entry->vme_next;
6943 }
6944 }
6945
6946 if (aligned) {
6947 pmap_t local_pmap;
6948
6949 if(pmap)
6950 local_pmap = pmap;
6951 else
6952 local_pmap = dst_map->pmap;
6953
6954 if ((kr = vm_map_copy_overwrite_aligned(
6955 dst_map, tmp_entry, copy,
6956 base_addr, local_pmap)) != KERN_SUCCESS) {
6957 if(next_copy != NULL) {
6958 copy->cpy_hdr.nentries +=
6959 remaining_entries;
6960 copy->cpy_hdr.links.prev->vme_next =
6961 next_copy;
6962 copy->cpy_hdr.links.prev =
6963 previous_prev;
6964 copy->size += copy_size;
6965 }
6966 return kr;
6967 }
6968 vm_map_unlock(dst_map);
6969 } else {
6970 /*
6971 * Performance gain:
6972 *
6973 * if the copy and dst address are misaligned but the same
6974 * offset within the page we can copy_not_aligned the
6975 * misaligned parts and copy aligned the rest. If they are
6976 * aligned but len is unaligned we simply need to copy
6977 * the end bit unaligned. We'll need to split the misaligned
6978 * bits of the region in this case !
6979 */
6980 /* ALWAYS UNLOCKS THE dst_map MAP */
6981 kr = vm_map_copy_overwrite_unaligned(
6982 dst_map,
6983 tmp_entry,
6984 copy,
6985 base_addr,
6986 discard_on_success);
6987 if (kr != KERN_SUCCESS) {
6988 if(next_copy != NULL) {
6989 copy->cpy_hdr.nentries +=
6990 remaining_entries;
6991 copy->cpy_hdr.links.prev->vme_next =
6992 next_copy;
6993 copy->cpy_hdr.links.prev =
6994 previous_prev;
6995 copy->size += copy_size;
6996 }
6997 return kr;
6998 }
6999 }
7000 total_size -= copy_size;
7001 if(total_size == 0)
7002 break;
7003 base_addr += copy_size;
7004 copy_size = 0;
7005 copy->offset = new_offset;
7006 if(next_copy != NULL) {
7007 copy->cpy_hdr.nentries = remaining_entries;
7008 copy->cpy_hdr.links.next = next_copy;
7009 copy->cpy_hdr.links.prev = previous_prev;
7010 next_copy->vme_prev = vm_map_copy_to_entry(copy);
7011 copy->size = total_size;
7012 }
7013 vm_map_lock(dst_map);
7014 while(TRUE) {
7015 if (!vm_map_lookup_entry(dst_map,
7016 base_addr, &tmp_entry)) {
7017 vm_map_unlock(dst_map);
7018 return(KERN_INVALID_ADDRESS);
7019 }
7020 if (tmp_entry->in_transition) {
7021 entry->needs_wakeup = TRUE;
7022 vm_map_entry_wait(dst_map, THREAD_UNINT);
7023 } else {
7024 break;
7025 }
7026 }
7027 vm_map_clip_start(dst_map,
7028 tmp_entry,
7029 vm_map_trunc_page(base_addr,
7030 VM_MAP_PAGE_MASK(dst_map)));
7031
7032 entry = tmp_entry;
7033 } /* while */
7034
7035 /*
7036 * Throw away the vm_map_copy object
7037 */
7038 if (discard_on_success)
7039 vm_map_copy_discard(copy);
7040
7041 return(KERN_SUCCESS);
7042}/* vm_map_copy_overwrite */
7043
7044kern_return_t
7045vm_map_copy_overwrite(
7046 vm_map_t dst_map,
7047 vm_map_offset_t dst_addr,
7048 vm_map_copy_t copy,
7049 boolean_t interruptible)
7050{
7051 vm_map_size_t head_size, tail_size;
7052 vm_map_copy_t head_copy, tail_copy;
7053 vm_map_offset_t head_addr, tail_addr;
7054 vm_map_entry_t entry;
7055 kern_return_t kr;
7056
7057 head_size = 0;
7058 tail_size = 0;
7059 head_copy = NULL;
7060 tail_copy = NULL;
7061 head_addr = 0;
7062 tail_addr = 0;
7063
7064 if (interruptible ||
7065 copy == VM_MAP_COPY_NULL ||
7066 copy->type != VM_MAP_COPY_ENTRY_LIST) {
7067 /*
7068 * We can't split the "copy" map if we're interruptible
7069 * or if we don't have a "copy" map...
7070 */
7071 blunt_copy:
7072 return vm_map_copy_overwrite_nested(dst_map,
7073 dst_addr,
7074 copy,
7075 interruptible,
7076 (pmap_t) NULL,
7077 TRUE);
7078 }
7079
7080 if (copy->size < 3 * PAGE_SIZE) {
7081 /*
7082 * Too small to bother with optimizing...
7083 */
7084 goto blunt_copy;
7085 }
7086
7087 if ((dst_addr & VM_MAP_PAGE_MASK(dst_map)) !=
7088 (copy->offset & VM_MAP_PAGE_MASK(dst_map))) {
7089 /*
7090 * Incompatible mis-alignment of source and destination...
7091 */
7092 goto blunt_copy;
7093 }
7094
7095 /*
7096 * Proper alignment or identical mis-alignment at the beginning.
7097 * Let's try and do a small unaligned copy first (if needed)
7098 * and then an aligned copy for the rest.
7099 */
7100 if (!page_aligned(dst_addr)) {
7101 head_addr = dst_addr;
7102 head_size = (VM_MAP_PAGE_SIZE(dst_map) -
7103 (copy->offset & VM_MAP_PAGE_MASK(dst_map)));
7104 }
7105 if (!page_aligned(copy->offset + copy->size)) {
7106 /*
7107 * Mis-alignment at the end.
7108 * Do an aligned copy up to the last page and
7109 * then an unaligned copy for the remaining bytes.
7110 */
7111 tail_size = ((copy->offset + copy->size) &
7112 VM_MAP_PAGE_MASK(dst_map));
7113 tail_addr = dst_addr + copy->size - tail_size;
7114 }
7115
7116 if (head_size + tail_size == copy->size) {
7117 /*
7118 * It's all unaligned, no optimization possible...
7119 */
7120 goto blunt_copy;
7121 }
7122
7123 /*
7124 * Can't optimize if there are any submaps in the
7125 * destination due to the way we free the "copy" map
7126 * progressively in vm_map_copy_overwrite_nested()
7127 * in that case.
7128 */
7129 vm_map_lock_read(dst_map);
7130 if (! vm_map_lookup_entry(dst_map, dst_addr, &entry)) {
7131 vm_map_unlock_read(dst_map);
7132 goto blunt_copy;
7133 }
7134 for (;
7135 (entry != vm_map_copy_to_entry(copy) &&
7136 entry->vme_start < dst_addr + copy->size);
7137 entry = entry->vme_next) {
7138 if (entry->is_sub_map) {
7139 vm_map_unlock_read(dst_map);
7140 goto blunt_copy;
7141 }
7142 }
7143 vm_map_unlock_read(dst_map);
7144
7145 if (head_size) {
7146 /*
7147 * Unaligned copy of the first "head_size" bytes, to reach
7148 * a page boundary.
7149 */
7150
7151 /*
7152 * Extract "head_copy" out of "copy".
7153 */
7154 head_copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
7155 head_copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
7156 vm_map_copy_first_entry(head_copy) =
7157 vm_map_copy_to_entry(head_copy);
7158 vm_map_copy_last_entry(head_copy) =
7159 vm_map_copy_to_entry(head_copy);
7160 head_copy->type = VM_MAP_COPY_ENTRY_LIST;
7161 head_copy->cpy_hdr.nentries = 0;
7162 head_copy->cpy_hdr.entries_pageable =
7163 copy->cpy_hdr.entries_pageable;
7164 vm_map_store_init(&head_copy->cpy_hdr);
7165
7166 head_copy->offset = copy->offset;
7167 head_copy->size = head_size;
7168
7169 copy->offset += head_size;
7170 copy->size -= head_size;
7171
7172 entry = vm_map_copy_first_entry(copy);
7173 vm_map_copy_clip_end(copy, entry, copy->offset);
7174 vm_map_copy_entry_unlink(copy, entry);
7175 vm_map_copy_entry_link(head_copy,
7176 vm_map_copy_to_entry(head_copy),
7177 entry);
7178
7179 /*
7180 * Do the unaligned copy.
7181 */
7182 kr = vm_map_copy_overwrite_nested(dst_map,
7183 head_addr,
7184 head_copy,
7185 interruptible,
7186 (pmap_t) NULL,
7187 FALSE);
7188 if (kr != KERN_SUCCESS)
7189 goto done;
7190 }
7191
7192 if (tail_size) {
7193 /*
7194 * Extract "tail_copy" out of "copy".
7195 */
7196 tail_copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
7197 tail_copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
7198 vm_map_copy_first_entry(tail_copy) =
7199 vm_map_copy_to_entry(tail_copy);
7200 vm_map_copy_last_entry(tail_copy) =
7201 vm_map_copy_to_entry(tail_copy);
7202 tail_copy->type = VM_MAP_COPY_ENTRY_LIST;
7203 tail_copy->cpy_hdr.nentries = 0;
7204 tail_copy->cpy_hdr.entries_pageable =
7205 copy->cpy_hdr.entries_pageable;
7206 vm_map_store_init(&tail_copy->cpy_hdr);
7207
7208 tail_copy->offset = copy->offset + copy->size - tail_size;
7209 tail_copy->size = tail_size;
7210
7211 copy->size -= tail_size;
7212
7213 entry = vm_map_copy_last_entry(copy);
7214 vm_map_copy_clip_start(copy, entry, tail_copy->offset);
7215 entry = vm_map_copy_last_entry(copy);
7216 vm_map_copy_entry_unlink(copy, entry);
7217 vm_map_copy_entry_link(tail_copy,
7218 vm_map_copy_last_entry(tail_copy),
7219 entry);
7220 }
7221
7222 /*
7223 * Copy most (or possibly all) of the data.
7224 */
7225 kr = vm_map_copy_overwrite_nested(dst_map,
7226 dst_addr + head_size,
7227 copy,
7228 interruptible,
7229 (pmap_t) NULL,
7230 FALSE);
7231 if (kr != KERN_SUCCESS) {
7232 goto done;
7233 }
7234
7235 if (tail_size) {
7236 kr = vm_map_copy_overwrite_nested(dst_map,
7237 tail_addr,
7238 tail_copy,
7239 interruptible,
7240 (pmap_t) NULL,
7241 FALSE);
7242 }
7243
7244done:
7245 assert(copy->type == VM_MAP_COPY_ENTRY_LIST);
7246 if (kr == KERN_SUCCESS) {
7247 /*
7248 * Discard all the copy maps.
7249 */
7250 if (head_copy) {
7251 vm_map_copy_discard(head_copy);
7252 head_copy = NULL;
7253 }
7254 vm_map_copy_discard(copy);
7255 if (tail_copy) {
7256 vm_map_copy_discard(tail_copy);
7257 tail_copy = NULL;
7258 }
7259 } else {
7260 /*
7261 * Re-assemble the original copy map.
7262 */
7263 if (head_copy) {
7264 entry = vm_map_copy_first_entry(head_copy);
7265 vm_map_copy_entry_unlink(head_copy, entry);
7266 vm_map_copy_entry_link(copy,
7267 vm_map_copy_to_entry(copy),
7268 entry);
7269 copy->offset -= head_size;
7270 copy->size += head_size;
7271 vm_map_copy_discard(head_copy);
7272 head_copy = NULL;
7273 }
7274 if (tail_copy) {
7275 entry = vm_map_copy_last_entry(tail_copy);
7276 vm_map_copy_entry_unlink(tail_copy, entry);
7277 vm_map_copy_entry_link(copy,
7278 vm_map_copy_last_entry(copy),
7279 entry);
7280 copy->size += tail_size;
7281 vm_map_copy_discard(tail_copy);
7282 tail_copy = NULL;
7283 }
7284 }
7285 return kr;
7286}
7287
7288
7289/*
7290 * Routine: vm_map_copy_overwrite_unaligned [internal use only]
7291 *
7292 * Decription:
7293 * Physically copy unaligned data
7294 *
7295 * Implementation:
7296 * Unaligned parts of pages have to be physically copied. We use
7297 * a modified form of vm_fault_copy (which understands none-aligned
7298 * page offsets and sizes) to do the copy. We attempt to copy as
7299 * much memory in one go as possibly, however vm_fault_copy copies
7300 * within 1 memory object so we have to find the smaller of "amount left"
7301 * "source object data size" and "target object data size". With
7302 * unaligned data we don't need to split regions, therefore the source
7303 * (copy) object should be one map entry, the target range may be split
7304 * over multiple map entries however. In any event we are pessimistic
7305 * about these assumptions.
7306 *
7307 * Assumptions:
7308 * dst_map is locked on entry and is return locked on success,
7309 * unlocked on error.
7310 */
7311
7312static kern_return_t
7313vm_map_copy_overwrite_unaligned(
7314 vm_map_t dst_map,
7315 vm_map_entry_t entry,
7316 vm_map_copy_t copy,
7317 vm_map_offset_t start,
7318 boolean_t discard_on_success)
7319{
7320 vm_map_entry_t copy_entry;
7321 vm_map_entry_t copy_entry_next;
7322 vm_map_version_t version;
7323 vm_object_t dst_object;
7324 vm_object_offset_t dst_offset;
7325 vm_object_offset_t src_offset;
7326 vm_object_offset_t entry_offset;
7327 vm_map_offset_t entry_end;
7328 vm_map_size_t src_size,
7329 dst_size,
7330 copy_size,
7331 amount_left;
7332 kern_return_t kr = KERN_SUCCESS;
7333
7334
7335 copy_entry = vm_map_copy_first_entry(copy);
7336
7337 vm_map_lock_write_to_read(dst_map);
7338
7339 src_offset = copy->offset - vm_object_trunc_page(copy->offset);
7340 amount_left = copy->size;
7341/*
7342 * unaligned so we never clipped this entry, we need the offset into
7343 * the vm_object not just the data.
7344 */
7345 while (amount_left > 0) {
7346
7347 if (entry == vm_map_to_entry(dst_map)) {
7348 vm_map_unlock_read(dst_map);
7349 return KERN_INVALID_ADDRESS;
7350 }
7351
7352 /* "start" must be within the current map entry */
7353 assert ((start>=entry->vme_start) && (start<entry->vme_end));
7354
7355 dst_offset = start - entry->vme_start;
7356
7357 dst_size = entry->vme_end - start;
7358
7359 src_size = copy_entry->vme_end -
7360 (copy_entry->vme_start + src_offset);
7361
7362 if (dst_size < src_size) {
7363/*
7364 * we can only copy dst_size bytes before
7365 * we have to get the next destination entry
7366 */
7367 copy_size = dst_size;
7368 } else {
7369/*
7370 * we can only copy src_size bytes before
7371 * we have to get the next source copy entry
7372 */
7373 copy_size = src_size;
7374 }
7375
7376 if (copy_size > amount_left) {
7377 copy_size = amount_left;
7378 }
7379/*
7380 * Entry needs copy, create a shadow shadow object for
7381 * Copy on write region.
7382 */
7383 if (entry->needs_copy &&
7384 ((entry->protection & VM_PROT_WRITE) != 0))
7385 {
7386 if (vm_map_lock_read_to_write(dst_map)) {
7387 vm_map_lock_read(dst_map);
7388 goto RetryLookup;
7389 }
7390 vm_object_shadow(&entry->object.vm_object,
7391 &entry->offset,
7392 (vm_map_size_t)(entry->vme_end
7393 - entry->vme_start));
7394 entry->needs_copy = FALSE;
7395 vm_map_lock_write_to_read(dst_map);
7396 }
7397 dst_object = entry->object.vm_object;
7398/*
7399 * unlike with the virtual (aligned) copy we're going
7400 * to fault on it therefore we need a target object.
7401 */
7402 if (dst_object == VM_OBJECT_NULL) {
7403 if (vm_map_lock_read_to_write(dst_map)) {
7404 vm_map_lock_read(dst_map);
7405 goto RetryLookup;
7406 }
7407 dst_object = vm_object_allocate((vm_map_size_t)
7408 entry->vme_end - entry->vme_start);
7409 entry->object.vm_object = dst_object;
7410 entry->offset = 0;
7411 assert(entry->use_pmap);
7412 vm_map_lock_write_to_read(dst_map);
7413 }
7414/*
7415 * Take an object reference and unlock map. The "entry" may
7416 * disappear or change when the map is unlocked.
7417 */
7418 vm_object_reference(dst_object);
7419 version.main_timestamp = dst_map->timestamp;
7420 entry_offset = entry->offset;
7421 entry_end = entry->vme_end;
7422 vm_map_unlock_read(dst_map);
7423/*
7424 * Copy as much as possible in one pass
7425 */
7426 kr = vm_fault_copy(
7427 copy_entry->object.vm_object,
7428 copy_entry->offset + src_offset,
7429 &copy_size,
7430 dst_object,
7431 entry_offset + dst_offset,
7432 dst_map,
7433 &version,
7434 THREAD_UNINT );
7435
7436 start += copy_size;
7437 src_offset += copy_size;
7438 amount_left -= copy_size;
7439/*
7440 * Release the object reference
7441 */
7442 vm_object_deallocate(dst_object);
7443/*
7444 * If a hard error occurred, return it now
7445 */
7446 if (kr != KERN_SUCCESS)
7447 return kr;
7448
7449 if ((copy_entry->vme_start + src_offset) == copy_entry->vme_end
7450 || amount_left == 0)
7451 {
7452/*
7453 * all done with this copy entry, dispose.
7454 */
7455 copy_entry_next = copy_entry->vme_next;
7456
7457 if (discard_on_success) {
7458 vm_map_copy_entry_unlink(copy, copy_entry);
7459 assert(!copy_entry->is_sub_map);
7460 vm_object_deallocate(
7461 copy_entry->object.vm_object);
7462 vm_map_copy_entry_dispose(copy, copy_entry);
7463 }
7464
7465 if (copy_entry_next == vm_map_copy_to_entry(copy) &&
7466 amount_left) {
7467/*
7468 * not finished copying but run out of source
7469 */
7470 return KERN_INVALID_ADDRESS;
7471 }
7472
7473 copy_entry = copy_entry_next;
7474
7475 src_offset = 0;
7476 }
7477
7478 if (amount_left == 0)
7479 return KERN_SUCCESS;
7480
7481 vm_map_lock_read(dst_map);
7482 if (version.main_timestamp == dst_map->timestamp) {
7483 if (start == entry_end) {
7484/*
7485 * destination region is split. Use the version
7486 * information to avoid a lookup in the normal
7487 * case.
7488 */
7489 entry = entry->vme_next;
7490/*
7491 * should be contiguous. Fail if we encounter
7492 * a hole in the destination.
7493 */
7494 if (start != entry->vme_start) {
7495 vm_map_unlock_read(dst_map);
7496 return KERN_INVALID_ADDRESS ;
7497 }
7498 }
7499 } else {
7500/*
7501 * Map version check failed.
7502 * we must lookup the entry because somebody
7503 * might have changed the map behind our backs.
7504 */
7505 RetryLookup:
7506 if (!vm_map_lookup_entry(dst_map, start, &entry))
7507 {
7508 vm_map_unlock_read(dst_map);
7509 return KERN_INVALID_ADDRESS ;
7510 }
7511 }
7512 }/* while */
7513
7514 return KERN_SUCCESS;
7515}/* vm_map_copy_overwrite_unaligned */
7516
7517/*
7518 * Routine: vm_map_copy_overwrite_aligned [internal use only]
7519 *
7520 * Description:
7521 * Does all the vm_trickery possible for whole pages.
7522 *
7523 * Implementation:
7524 *
7525 * If there are no permanent objects in the destination,
7526 * and the source and destination map entry zones match,
7527 * and the destination map entry is not shared,
7528 * then the map entries can be deleted and replaced
7529 * with those from the copy. The following code is the
7530 * basic idea of what to do, but there are lots of annoying
7531 * little details about getting protection and inheritance
7532 * right. Should add protection, inheritance, and sharing checks
7533 * to the above pass and make sure that no wiring is involved.
7534 */
7535
7536int vm_map_copy_overwrite_aligned_src_not_internal = 0;
7537int vm_map_copy_overwrite_aligned_src_not_symmetric = 0;
7538int vm_map_copy_overwrite_aligned_src_large = 0;
7539
7540static kern_return_t
7541vm_map_copy_overwrite_aligned(
7542 vm_map_t dst_map,
7543 vm_map_entry_t tmp_entry,
7544 vm_map_copy_t copy,
7545 vm_map_offset_t start,
7546 __unused pmap_t pmap)
7547{
7548 vm_object_t object;
7549 vm_map_entry_t copy_entry;
7550 vm_map_size_t copy_size;
7551 vm_map_size_t size;
7552 vm_map_entry_t entry;
7553
7554 while ((copy_entry = vm_map_copy_first_entry(copy))
7555 != vm_map_copy_to_entry(copy))
7556 {
7557 copy_size = (copy_entry->vme_end - copy_entry->vme_start);
7558
7559 entry = tmp_entry;
7560 if (entry->is_sub_map) {
7561 /* unnested when clipped earlier */
7562 assert(!entry->use_pmap);
7563 }
7564 if (entry == vm_map_to_entry(dst_map)) {
7565 vm_map_unlock(dst_map);
7566 return KERN_INVALID_ADDRESS;
7567 }
7568 size = (entry->vme_end - entry->vme_start);
7569 /*
7570 * Make sure that no holes popped up in the
7571 * address map, and that the protection is
7572 * still valid, in case the map was unlocked
7573 * earlier.
7574 */
7575
7576 if ((entry->vme_start != start) || ((entry->is_sub_map)
7577 && !entry->needs_copy)) {
7578 vm_map_unlock(dst_map);
7579 return(KERN_INVALID_ADDRESS);
7580 }
7581 assert(entry != vm_map_to_entry(dst_map));
7582
7583 /*
7584 * Check protection again
7585 */
7586
7587 if ( ! (entry->protection & VM_PROT_WRITE)) {
7588 vm_map_unlock(dst_map);
7589 return(KERN_PROTECTION_FAILURE);
7590 }
7591
7592 /*
7593 * Adjust to source size first
7594 */
7595
7596 if (copy_size < size) {
7597 if (entry->map_aligned &&
7598 !VM_MAP_PAGE_ALIGNED(entry->vme_start + copy_size,
7599 VM_MAP_PAGE_MASK(dst_map))) {
7600 /* no longer map-aligned */
7601 entry->map_aligned = FALSE;
7602 }
7603 vm_map_clip_end(dst_map, entry, entry->vme_start + copy_size);
7604 size = copy_size;
7605 }
7606
7607 /*
7608 * Adjust to destination size
7609 */
7610
7611 if (size < copy_size) {
7612 vm_map_copy_clip_end(copy, copy_entry,
7613 copy_entry->vme_start + size);
7614 copy_size = size;
7615 }
7616
7617 assert((entry->vme_end - entry->vme_start) == size);
7618 assert((tmp_entry->vme_end - tmp_entry->vme_start) == size);
7619 assert((copy_entry->vme_end - copy_entry->vme_start) == size);
7620
7621 /*
7622 * If the destination contains temporary unshared memory,
7623 * we can perform the copy by throwing it away and
7624 * installing the source data.
7625 */
7626
7627 object = entry->object.vm_object;
7628 if ((!entry->is_shared &&
7629 ((object == VM_OBJECT_NULL) ||
7630 (object->internal && !object->true_share))) ||
7631 entry->needs_copy) {
7632 vm_object_t old_object = entry->object.vm_object;
7633 vm_object_offset_t old_offset = entry->offset;
7634 vm_object_offset_t offset;
7635
7636 /*
7637 * Ensure that the source and destination aren't
7638 * identical
7639 */
7640 if (old_object == copy_entry->object.vm_object &&
7641 old_offset == copy_entry->offset) {
7642 vm_map_copy_entry_unlink(copy, copy_entry);
7643 vm_map_copy_entry_dispose(copy, copy_entry);
7644
7645 if (old_object != VM_OBJECT_NULL)
7646 vm_object_deallocate(old_object);
7647
7648 start = tmp_entry->vme_end;
7649 tmp_entry = tmp_entry->vme_next;
7650 continue;
7651 }
7652
7653#define __TRADEOFF1_OBJ_SIZE (64 * 1024 * 1024) /* 64 MB */
7654#define __TRADEOFF1_COPY_SIZE (128 * 1024) /* 128 KB */
7655 if (copy_entry->object.vm_object != VM_OBJECT_NULL &&
7656 copy_entry->object.vm_object->vo_size >= __TRADEOFF1_OBJ_SIZE &&
7657 copy_size <= __TRADEOFF1_COPY_SIZE) {
7658 /*
7659 * Virtual vs. Physical copy tradeoff #1.
7660 *
7661 * Copying only a few pages out of a large
7662 * object: do a physical copy instead of
7663 * a virtual copy, to avoid possibly keeping
7664 * the entire large object alive because of
7665 * those few copy-on-write pages.
7666 */
7667 vm_map_copy_overwrite_aligned_src_large++;
7668 goto slow_copy;
7669 }
7670
7671 if (entry->alias >= VM_MEMORY_MALLOC &&
7672 entry->alias <= VM_MEMORY_MALLOC_LARGE_REUSED) {
7673 vm_object_t new_object, new_shadow;
7674
7675 /*
7676 * We're about to map something over a mapping
7677 * established by malloc()...
7678 */
7679 new_object = copy_entry->object.vm_object;
7680 if (new_object != VM_OBJECT_NULL) {
7681 vm_object_lock_shared(new_object);
7682 }
7683 while (new_object != VM_OBJECT_NULL &&
7684 !new_object->true_share &&
7685 new_object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC &&
7686 new_object->internal) {
7687 new_shadow = new_object->shadow;
7688 if (new_shadow == VM_OBJECT_NULL) {
7689 break;
7690 }
7691 vm_object_lock_shared(new_shadow);
7692 vm_object_unlock(new_object);
7693 new_object = new_shadow;
7694 }
7695 if (new_object != VM_OBJECT_NULL) {
7696 if (!new_object->internal) {
7697 /*
7698 * The new mapping is backed
7699 * by an external object. We
7700 * don't want malloc'ed memory
7701 * to be replaced with such a
7702 * non-anonymous mapping, so
7703 * let's go off the optimized
7704 * path...
7705 */
7706 vm_map_copy_overwrite_aligned_src_not_internal++;
7707 vm_object_unlock(new_object);
7708 goto slow_copy;
7709 }
7710 if (new_object->true_share ||
7711 new_object->copy_strategy != MEMORY_OBJECT_COPY_SYMMETRIC) {
7712 /*
7713 * Same if there's a "true_share"
7714 * object in the shadow chain, or
7715 * an object with a non-default
7716 * (SYMMETRIC) copy strategy.
7717 */
7718 vm_map_copy_overwrite_aligned_src_not_symmetric++;
7719 vm_object_unlock(new_object);
7720 goto slow_copy;
7721 }
7722 vm_object_unlock(new_object);
7723 }
7724 /*
7725 * The new mapping is still backed by
7726 * anonymous (internal) memory, so it's
7727 * OK to substitute it for the original
7728 * malloc() mapping.
7729 */
7730 }
7731
7732 if (old_object != VM_OBJECT_NULL) {
7733 if(entry->is_sub_map) {
7734 if(entry->use_pmap) {
7735#ifndef NO_NESTED_PMAP
7736 pmap_unnest(dst_map->pmap,
7737 (addr64_t)entry->vme_start,
7738 entry->vme_end - entry->vme_start);
7739#endif /* NO_NESTED_PMAP */
7740 if(dst_map->mapped_in_other_pmaps) {
7741 /* clean up parent */
7742 /* map/maps */
7743 vm_map_submap_pmap_clean(
7744 dst_map, entry->vme_start,
7745 entry->vme_end,
7746 entry->object.sub_map,
7747 entry->offset);
7748 }
7749 } else {
7750 vm_map_submap_pmap_clean(
7751 dst_map, entry->vme_start,
7752 entry->vme_end,
7753 entry->object.sub_map,
7754 entry->offset);
7755 }
7756 vm_map_deallocate(
7757 entry->object.sub_map);
7758 } else {
7759 if(dst_map->mapped_in_other_pmaps) {
7760 vm_object_pmap_protect_options(
7761 entry->object.vm_object,
7762 entry->offset,
7763 entry->vme_end
7764 - entry->vme_start,
7765 PMAP_NULL,
7766 entry->vme_start,
7767 VM_PROT_NONE,
7768 PMAP_OPTIONS_REMOVE);
7769 } else {
7770 pmap_remove_options(
7771 dst_map->pmap,
7772 (addr64_t)(entry->vme_start),
7773 (addr64_t)(entry->vme_end),
7774 PMAP_OPTIONS_REMOVE);
7775 }
7776 vm_object_deallocate(old_object);
7777 }
7778 }
7779
7780 entry->is_sub_map = FALSE;
7781 entry->object = copy_entry->object;
7782 object = entry->object.vm_object;
7783 entry->needs_copy = copy_entry->needs_copy;
7784 entry->wired_count = 0;
7785 entry->user_wired_count = 0;
7786 offset = entry->offset = copy_entry->offset;
7787
7788 vm_map_copy_entry_unlink(copy, copy_entry);
7789 vm_map_copy_entry_dispose(copy, copy_entry);
7790
7791 /*
7792 * we could try to push pages into the pmap at this point, BUT
7793 * this optimization only saved on average 2 us per page if ALL
7794 * the pages in the source were currently mapped
7795 * and ALL the pages in the dest were touched, if there were fewer
7796 * than 2/3 of the pages touched, this optimization actually cost more cycles
7797 * it also puts a lot of pressure on the pmap layer w/r to mapping structures
7798 */
7799
7800 /*
7801 * Set up for the next iteration. The map
7802 * has not been unlocked, so the next
7803 * address should be at the end of this
7804 * entry, and the next map entry should be
7805 * the one following it.
7806 */
7807
7808 start = tmp_entry->vme_end;
7809 tmp_entry = tmp_entry->vme_next;
7810 } else {
7811 vm_map_version_t version;
7812 vm_object_t dst_object;
7813 vm_object_offset_t dst_offset;
7814 kern_return_t r;
7815
7816 slow_copy:
7817 if (entry->needs_copy) {
7818 vm_object_shadow(&entry->object.vm_object,
7819 &entry->offset,
7820 (entry->vme_end -
7821 entry->vme_start));
7822 entry->needs_copy = FALSE;
7823 }
7824
7825 dst_object = entry->object.vm_object;
7826 dst_offset = entry->offset;
7827
7828 /*
7829 * Take an object reference, and record
7830 * the map version information so that the
7831 * map can be safely unlocked.
7832 */
7833
7834 if (dst_object == VM_OBJECT_NULL) {
7835 /*
7836 * We would usually have just taken the
7837 * optimized path above if the destination
7838 * object has not been allocated yet. But we
7839 * now disable that optimization if the copy
7840 * entry's object is not backed by anonymous
7841 * memory to avoid replacing malloc'ed
7842 * (i.e. re-usable) anonymous memory with a
7843 * not-so-anonymous mapping.
7844 * So we have to handle this case here and
7845 * allocate a new VM object for this map entry.
7846 */
7847 dst_object = vm_object_allocate(
7848 entry->vme_end - entry->vme_start);
7849 dst_offset = 0;
7850 entry->object.vm_object = dst_object;
7851 entry->offset = dst_offset;
7852 assert(entry->use_pmap);
7853
7854 }
7855
7856 vm_object_reference(dst_object);
7857
7858 /* account for unlock bumping up timestamp */
7859 version.main_timestamp = dst_map->timestamp + 1;
7860
7861 vm_map_unlock(dst_map);
7862
7863 /*
7864 * Copy as much as possible in one pass
7865 */
7866
7867 copy_size = size;
7868 r = vm_fault_copy(
7869 copy_entry->object.vm_object,
7870 copy_entry->offset,
7871 &copy_size,
7872 dst_object,
7873 dst_offset,
7874 dst_map,
7875 &version,
7876 THREAD_UNINT );
7877
7878 /*
7879 * Release the object reference
7880 */
7881
7882 vm_object_deallocate(dst_object);
7883
7884 /*
7885 * If a hard error occurred, return it now
7886 */
7887
7888 if (r != KERN_SUCCESS)
7889 return(r);
7890
7891 if (copy_size != 0) {
7892 /*
7893 * Dispose of the copied region
7894 */
7895
7896 vm_map_copy_clip_end(copy, copy_entry,
7897 copy_entry->vme_start + copy_size);
7898 vm_map_copy_entry_unlink(copy, copy_entry);
7899 vm_object_deallocate(copy_entry->object.vm_object);
7900 vm_map_copy_entry_dispose(copy, copy_entry);
7901 }
7902
7903 /*
7904 * Pick up in the destination map where we left off.
7905 *
7906 * Use the version information to avoid a lookup
7907 * in the normal case.
7908 */
7909
7910 start += copy_size;
7911 vm_map_lock(dst_map);
7912 if (version.main_timestamp == dst_map->timestamp &&
7913 copy_size != 0) {
7914 /* We can safely use saved tmp_entry value */
7915
7916 if (tmp_entry->map_aligned &&
7917 !VM_MAP_PAGE_ALIGNED(
7918 start,
7919 VM_MAP_PAGE_MASK(dst_map))) {
7920 /* no longer map-aligned */
7921 tmp_entry->map_aligned = FALSE;
7922 }
7923 vm_map_clip_end(dst_map, tmp_entry, start);
7924 tmp_entry = tmp_entry->vme_next;
7925 } else {
7926 /* Must do lookup of tmp_entry */
7927
7928 if (!vm_map_lookup_entry(dst_map, start, &tmp_entry)) {
7929 vm_map_unlock(dst_map);
7930 return(KERN_INVALID_ADDRESS);
7931 }
7932 if (tmp_entry->map_aligned &&
7933 !VM_MAP_PAGE_ALIGNED(
7934 start,
7935 VM_MAP_PAGE_MASK(dst_map))) {
7936 /* no longer map-aligned */
7937 tmp_entry->map_aligned = FALSE;
7938 }
7939 vm_map_clip_start(dst_map, tmp_entry, start);
7940 }
7941 }
7942 }/* while */
7943
7944 return(KERN_SUCCESS);
7945}/* vm_map_copy_overwrite_aligned */
7946
7947/*
7948 * Routine: vm_map_copyin_kernel_buffer [internal use only]
7949 *
7950 * Description:
7951 * Copy in data to a kernel buffer from space in the
7952 * source map. The original space may be optionally
7953 * deallocated.
7954 *
7955 * If successful, returns a new copy object.
7956 */
7957static kern_return_t
7958vm_map_copyin_kernel_buffer(
7959 vm_map_t src_map,
7960 vm_map_offset_t src_addr,
7961 vm_map_size_t len,
7962 boolean_t src_destroy,
7963 vm_map_copy_t *copy_result)
7964{
7965 kern_return_t kr;
7966 vm_map_copy_t copy;
7967 vm_size_t kalloc_size;
7968
7969 if ((vm_size_t) len != len) {
7970 /* "len" is too big and doesn't fit in a "vm_size_t" */
7971 return KERN_RESOURCE_SHORTAGE;
7972 }
7973 kalloc_size = (vm_size_t) (sizeof(struct vm_map_copy) + len);
7974 assert((vm_map_size_t) kalloc_size == sizeof (struct vm_map_copy) + len);
7975
7976 copy = (vm_map_copy_t) kalloc(kalloc_size);
7977 if (copy == VM_MAP_COPY_NULL) {
7978 return KERN_RESOURCE_SHORTAGE;
7979 }
7980 copy->type = VM_MAP_COPY_KERNEL_BUFFER;
7981 copy->size = len;
7982 copy->offset = 0;
7983 copy->cpy_kdata = (void *) (copy + 1);
7984 copy->cpy_kalloc_size = kalloc_size;
7985
7986 kr = copyinmap(src_map, src_addr, copy->cpy_kdata, (vm_size_t) len);
7987 if (kr != KERN_SUCCESS) {
7988 kfree(copy, kalloc_size);
7989 return kr;
7990 }
7991 if (src_destroy) {
7992 (void) vm_map_remove(
7993 src_map,
7994 vm_map_trunc_page(src_addr,
7995 VM_MAP_PAGE_MASK(src_map)),
7996 vm_map_round_page(src_addr + len,
7997 VM_MAP_PAGE_MASK(src_map)),
7998 (VM_MAP_REMOVE_INTERRUPTIBLE |
7999 VM_MAP_REMOVE_WAIT_FOR_KWIRE |
8000 (src_map == kernel_map) ? VM_MAP_REMOVE_KUNWIRE : 0));
8001 }
8002 *copy_result = copy;
8003 return KERN_SUCCESS;
8004}
8005
8006/*
8007 * Routine: vm_map_copyout_kernel_buffer [internal use only]
8008 *
8009 * Description:
8010 * Copy out data from a kernel buffer into space in the
8011 * destination map. The space may be otpionally dynamically
8012 * allocated.
8013 *
8014 * If successful, consumes the copy object.
8015 * Otherwise, the caller is responsible for it.
8016 */
8017static int vm_map_copyout_kernel_buffer_failures = 0;
8018static kern_return_t
8019vm_map_copyout_kernel_buffer(
8020 vm_map_t map,
8021 vm_map_address_t *addr, /* IN/OUT */
8022 vm_map_copy_t copy,
8023 boolean_t overwrite,
8024 boolean_t consume_on_success)
8025{
8026 kern_return_t kr = KERN_SUCCESS;
8027 thread_t thread = current_thread();
8028
8029 if (!overwrite) {
8030
8031 /*
8032 * Allocate space in the target map for the data
8033 */
8034 *addr = 0;
8035 kr = vm_map_enter(map,
8036 addr,
8037 vm_map_round_page(copy->size,
8038 VM_MAP_PAGE_MASK(map)),
8039 (vm_map_offset_t) 0,
8040 VM_FLAGS_ANYWHERE,
8041 VM_OBJECT_NULL,
8042 (vm_object_offset_t) 0,
8043 FALSE,
8044 VM_PROT_DEFAULT,
8045 VM_PROT_ALL,
8046 VM_INHERIT_DEFAULT);
8047 if (kr != KERN_SUCCESS)
8048 return kr;
8049 }
8050
8051 /*
8052 * Copyout the data from the kernel buffer to the target map.
8053 */
8054 if (thread->map == map) {
8055
8056 /*
8057 * If the target map is the current map, just do
8058 * the copy.
8059 */
8060 assert((vm_size_t) copy->size == copy->size);
8061 if (copyout(copy->cpy_kdata, *addr, (vm_size_t) copy->size)) {
8062 kr = KERN_INVALID_ADDRESS;
8063 }
8064 }
8065 else {
8066 vm_map_t oldmap;
8067
8068 /*
8069 * If the target map is another map, assume the
8070 * target's address space identity for the duration
8071 * of the copy.
8072 */
8073 vm_map_reference(map);
8074 oldmap = vm_map_switch(map);
8075
8076 assert((vm_size_t) copy->size == copy->size);
8077 if (copyout(copy->cpy_kdata, *addr, (vm_size_t) copy->size)) {
8078 vm_map_copyout_kernel_buffer_failures++;
8079 kr = KERN_INVALID_ADDRESS;
8080 }
8081
8082 (void) vm_map_switch(oldmap);
8083 vm_map_deallocate(map);
8084 }
8085
8086 if (kr != KERN_SUCCESS) {
8087 /* the copy failed, clean up */
8088 if (!overwrite) {
8089 /*
8090 * Deallocate the space we allocated in the target map.
8091 */
8092 (void) vm_map_remove(
8093 map,
8094 vm_map_trunc_page(*addr,
8095 VM_MAP_PAGE_MASK(map)),
8096 vm_map_round_page((*addr +
8097 vm_map_round_page(copy->size,
8098 VM_MAP_PAGE_MASK(map))),
8099 VM_MAP_PAGE_MASK(map)),
8100 VM_MAP_NO_FLAGS);
8101 *addr = 0;
8102 }
8103 } else {
8104 /* copy was successful, dicard the copy structure */
8105 if (consume_on_success) {
8106 kfree(copy, copy->cpy_kalloc_size);
8107 }
8108 }
8109
8110 return kr;
8111}
8112
8113/*
8114 * Macro: vm_map_copy_insert
8115 *
8116 * Description:
8117 * Link a copy chain ("copy") into a map at the
8118 * specified location (after "where").
8119 * Side effects:
8120 * The copy chain is destroyed.
8121 * Warning:
8122 * The arguments are evaluated multiple times.
8123 */
8124#define vm_map_copy_insert(map, where, copy) \
8125MACRO_BEGIN \
8126 vm_map_store_copy_insert(map, where, copy); \
8127 zfree(vm_map_copy_zone, copy); \
8128MACRO_END
8129
8130void
8131vm_map_copy_remap(
8132 vm_map_t map,
8133 vm_map_entry_t where,
8134 vm_map_copy_t copy,
8135 vm_map_offset_t adjustment,
8136 vm_prot_t cur_prot,
8137 vm_prot_t max_prot,
8138 vm_inherit_t inheritance)
8139{
8140 vm_map_entry_t copy_entry, new_entry;
8141
8142 for (copy_entry = vm_map_copy_first_entry(copy);
8143 copy_entry != vm_map_copy_to_entry(copy);
8144 copy_entry = copy_entry->vme_next) {
8145 /* get a new VM map entry for the map */
8146 new_entry = vm_map_entry_create(map,
8147 !map->hdr.entries_pageable);
8148 /* copy the "copy entry" to the new entry */
8149 vm_map_entry_copy(new_entry, copy_entry);
8150 /* adjust "start" and "end" */
8151 new_entry->vme_start += adjustment;
8152 new_entry->vme_end += adjustment;
8153 /* clear some attributes */
8154 new_entry->inheritance = inheritance;
8155 new_entry->protection = cur_prot;
8156 new_entry->max_protection = max_prot;
8157 new_entry->behavior = VM_BEHAVIOR_DEFAULT;
8158 /* take an extra reference on the entry's "object" */
8159 if (new_entry->is_sub_map) {
8160 assert(!new_entry->use_pmap); /* not nested */
8161 vm_map_lock(new_entry->object.sub_map);
8162 vm_map_reference(new_entry->object.sub_map);
8163 vm_map_unlock(new_entry->object.sub_map);
8164 } else {
8165 vm_object_reference(new_entry->object.vm_object);
8166 }
8167 /* insert the new entry in the map */
8168 vm_map_store_entry_link(map, where, new_entry);
8169 /* continue inserting the "copy entries" after the new entry */
8170 where = new_entry;
8171 }
8172}
8173
8174/*
8175 * Routine: vm_map_copyout
8176 *
8177 * Description:
8178 * Copy out a copy chain ("copy") into newly-allocated
8179 * space in the destination map.
8180 *
8181 * If successful, consumes the copy object.
8182 * Otherwise, the caller is responsible for it.
8183 */
8184
8185kern_return_t
8186vm_map_copyout(
8187 vm_map_t dst_map,
8188 vm_map_address_t *dst_addr, /* OUT */
8189 vm_map_copy_t copy)
8190{
8191 return vm_map_copyout_internal(dst_map, dst_addr, copy,
8192 TRUE, /* consume_on_success */
8193 VM_PROT_DEFAULT,
8194 VM_PROT_ALL,
8195 VM_INHERIT_DEFAULT);
8196}
8197
8198kern_return_t
8199vm_map_copyout_internal(
8200 vm_map_t dst_map,
8201 vm_map_address_t *dst_addr, /* OUT */
8202 vm_map_copy_t copy,
8203 boolean_t consume_on_success,
8204 vm_prot_t cur_protection,
8205 vm_prot_t max_protection,
8206 vm_inherit_t inheritance)
8207{
8208 vm_map_size_t size;
8209 vm_map_size_t adjustment;
8210 vm_map_offset_t start;
8211 vm_object_offset_t vm_copy_start;
8212 vm_map_entry_t last;
8213 vm_map_entry_t entry;
8214
8215 /*
8216 * Check for null copy object.
8217 */
8218
8219 if (copy == VM_MAP_COPY_NULL) {
8220 *dst_addr = 0;
8221 return(KERN_SUCCESS);
8222 }
8223
8224 /*
8225 * Check for special copy object, created
8226 * by vm_map_copyin_object.
8227 */
8228
8229 if (copy->type == VM_MAP_COPY_OBJECT) {
8230 vm_object_t object = copy->cpy_object;
8231 kern_return_t kr;
8232 vm_object_offset_t offset;
8233
8234 offset = vm_object_trunc_page(copy->offset);
8235 size = vm_map_round_page((copy->size +
8236 (vm_map_size_t)(copy->offset -
8237 offset)),
8238 VM_MAP_PAGE_MASK(dst_map));
8239 *dst_addr = 0;
8240 kr = vm_map_enter(dst_map, dst_addr, size,
8241 (vm_map_offset_t) 0, VM_FLAGS_ANYWHERE,
8242 object, offset, FALSE,
8243 VM_PROT_DEFAULT, VM_PROT_ALL,
8244 VM_INHERIT_DEFAULT);
8245 if (kr != KERN_SUCCESS)
8246 return(kr);
8247 /* Account for non-pagealigned copy object */
8248 *dst_addr += (vm_map_offset_t)(copy->offset - offset);
8249 if (consume_on_success)
8250 zfree(vm_map_copy_zone, copy);
8251 return(KERN_SUCCESS);
8252 }
8253
8254 /*
8255 * Check for special kernel buffer allocated
8256 * by new_ipc_kmsg_copyin.
8257 */
8258
8259 if (copy->type == VM_MAP_COPY_KERNEL_BUFFER) {
8260 return vm_map_copyout_kernel_buffer(dst_map, dst_addr,
8261 copy, FALSE,
8262 consume_on_success);
8263 }
8264
8265
8266 /*
8267 * Find space for the data
8268 */
8269
8270 vm_copy_start = vm_map_trunc_page((vm_map_size_t)copy->offset,
8271 VM_MAP_COPY_PAGE_MASK(copy));
8272 size = vm_map_round_page((vm_map_size_t)copy->offset + copy->size,
8273 VM_MAP_COPY_PAGE_MASK(copy))
8274 - vm_copy_start;
8275
8276
8277StartAgain: ;
8278
8279 vm_map_lock(dst_map);
8280 if( dst_map->disable_vmentry_reuse == TRUE) {
8281 VM_MAP_HIGHEST_ENTRY(dst_map, entry, start);
8282 last = entry;
8283 } else {
8284 assert(first_free_is_valid(dst_map));
8285 start = ((last = dst_map->first_free) == vm_map_to_entry(dst_map)) ?
8286 vm_map_min(dst_map) : last->vme_end;
8287 start = vm_map_round_page(start,
8288 VM_MAP_PAGE_MASK(dst_map));
8289 }
8290
8291 while (TRUE) {
8292 vm_map_entry_t next = last->vme_next;
8293 vm_map_offset_t end = start + size;
8294
8295 if ((end > dst_map->max_offset) || (end < start)) {
8296 if (dst_map->wait_for_space) {
8297 if (size <= (dst_map->max_offset - dst_map->min_offset)) {
8298 assert_wait((event_t) dst_map,
8299 THREAD_INTERRUPTIBLE);
8300 vm_map_unlock(dst_map);
8301 thread_block(THREAD_CONTINUE_NULL);
8302 goto StartAgain;
8303 }
8304 }
8305 vm_map_unlock(dst_map);
8306 return(KERN_NO_SPACE);
8307 }
8308
8309 if ((next == vm_map_to_entry(dst_map)) ||
8310 (next->vme_start >= end))
8311 break;
8312
8313 last = next;
8314 start = last->vme_end;
8315 start = vm_map_round_page(start,
8316 VM_MAP_PAGE_MASK(dst_map));
8317 }
8318
8319 adjustment = start - vm_copy_start;
8320 if (! consume_on_success) {
8321 /*
8322 * We're not allowed to consume "copy", so we'll have to
8323 * copy its map entries into the destination map below.
8324 * No need to re-allocate map entries from the correct
8325 * (pageable or not) zone, since we'll get new map entries
8326 * during the transfer.
8327 * We'll also adjust the map entries's "start" and "end"
8328 * during the transfer, to keep "copy"'s entries consistent
8329 * with its "offset".
8330 */
8331 goto after_adjustments;
8332 }
8333
8334 /*
8335 * Since we're going to just drop the map
8336 * entries from the copy into the destination
8337 * map, they must come from the same pool.
8338 */
8339
8340 if (copy->cpy_hdr.entries_pageable != dst_map->hdr.entries_pageable) {
8341 /*
8342 * Mismatches occur when dealing with the default
8343 * pager.
8344 */
8345 zone_t old_zone;
8346 vm_map_entry_t next, new;
8347
8348 /*
8349 * Find the zone that the copies were allocated from
8350 */
8351
8352 entry = vm_map_copy_first_entry(copy);
8353
8354 /*
8355 * Reinitialize the copy so that vm_map_copy_entry_link
8356 * will work.
8357 */
8358 vm_map_store_copy_reset(copy, entry);
8359 copy->cpy_hdr.entries_pageable = dst_map->hdr.entries_pageable;
8360
8361 /*
8362 * Copy each entry.
8363 */
8364 while (entry != vm_map_copy_to_entry(copy)) {
8365 new = vm_map_copy_entry_create(copy, !copy->cpy_hdr.entries_pageable);
8366 vm_map_entry_copy_full(new, entry);
8367 assert(!new->iokit_acct);
8368 if (new->is_sub_map) {
8369 /* clr address space specifics */
8370 new->use_pmap = FALSE;
8371 }
8372 vm_map_copy_entry_link(copy,
8373 vm_map_copy_last_entry(copy),
8374 new);
8375 next = entry->vme_next;
8376 old_zone = entry->from_reserved_zone ? vm_map_entry_reserved_zone : vm_map_entry_zone;
8377 zfree(old_zone, entry);
8378 entry = next;
8379 }
8380 }
8381
8382 /*
8383 * Adjust the addresses in the copy chain, and
8384 * reset the region attributes.
8385 */
8386
8387 for (entry = vm_map_copy_first_entry(copy);
8388 entry != vm_map_copy_to_entry(copy);
8389 entry = entry->vme_next) {
8390 if (VM_MAP_PAGE_SHIFT(dst_map) == PAGE_SHIFT) {
8391 /*
8392 * We're injecting this copy entry into a map that
8393 * has the standard page alignment, so clear
8394 * "map_aligned" (which might have been inherited
8395 * from the original map entry).
8396 */
8397 entry->map_aligned = FALSE;
8398 }
8399
8400 entry->vme_start += adjustment;
8401 entry->vme_end += adjustment;
8402
8403 if (entry->map_aligned) {
8404 assert(VM_MAP_PAGE_ALIGNED(entry->vme_start,
8405 VM_MAP_PAGE_MASK(dst_map)));
8406 assert(VM_MAP_PAGE_ALIGNED(entry->vme_end,
8407 VM_MAP_PAGE_MASK(dst_map)));
8408 }
8409
8410 entry->inheritance = VM_INHERIT_DEFAULT;
8411 entry->protection = VM_PROT_DEFAULT;
8412 entry->max_protection = VM_PROT_ALL;
8413 entry->behavior = VM_BEHAVIOR_DEFAULT;
8414
8415 /*
8416 * If the entry is now wired,
8417 * map the pages into the destination map.
8418 */
8419 if (entry->wired_count != 0) {
8420 register vm_map_offset_t va;
8421 vm_object_offset_t offset;
8422 register vm_object_t object;
8423 vm_prot_t prot;
8424 int type_of_fault;
8425
8426 object = entry->object.vm_object;
8427 offset = entry->offset;
8428 va = entry->vme_start;
8429
8430 pmap_pageable(dst_map->pmap,
8431 entry->vme_start,
8432 entry->vme_end,
8433 TRUE);
8434
8435 while (va < entry->vme_end) {
8436 register vm_page_t m;
8437
8438 /*
8439 * Look up the page in the object.
8440 * Assert that the page will be found in the
8441 * top object:
8442 * either
8443 * the object was newly created by
8444 * vm_object_copy_slowly, and has
8445 * copies of all of the pages from
8446 * the source object
8447 * or
8448 * the object was moved from the old
8449 * map entry; because the old map
8450 * entry was wired, all of the pages
8451 * were in the top-level object.
8452 * (XXX not true if we wire pages for
8453 * reading)
8454 */
8455 vm_object_lock(object);
8456
8457 m = vm_page_lookup(object, offset);
8458 if (m == VM_PAGE_NULL || !VM_PAGE_WIRED(m) ||
8459 m->absent)
8460 panic("vm_map_copyout: wiring %p", m);
8461
8462 /*
8463 * ENCRYPTED SWAP:
8464 * The page is assumed to be wired here, so it
8465 * shouldn't be encrypted. Otherwise, we
8466 * couldn't enter it in the page table, since
8467 * we don't want the user to see the encrypted
8468 * data.
8469 */
8470 ASSERT_PAGE_DECRYPTED(m);
8471
8472 prot = entry->protection;
8473
8474 if (override_nx(dst_map, entry->alias) && prot)
8475 prot |= VM_PROT_EXECUTE;
8476
8477 type_of_fault = DBG_CACHE_HIT_FAULT;
8478
8479 vm_fault_enter(m, dst_map->pmap, va, prot, prot,
8480 VM_PAGE_WIRED(m), FALSE, FALSE,
8481 FALSE, entry->alias,
8482 ((entry->iokit_acct ||
8483 (!entry->is_sub_map &&
8484 !entry->use_pmap))
8485 ? PMAP_OPTIONS_ALT_ACCT
8486 : 0),
8487 NULL, &type_of_fault);
8488
8489 vm_object_unlock(object);
8490
8491 offset += PAGE_SIZE_64;
8492 va += PAGE_SIZE;
8493 }
8494 }
8495 }
8496
8497after_adjustments:
8498
8499 /*
8500 * Correct the page alignment for the result
8501 */
8502
8503 *dst_addr = start + (copy->offset - vm_copy_start);
8504
8505 /*
8506 * Update the hints and the map size
8507 */
8508
8509 if (consume_on_success) {
8510 SAVE_HINT_MAP_WRITE(dst_map, vm_map_copy_last_entry(copy));
8511 } else {
8512 SAVE_HINT_MAP_WRITE(dst_map, last);
8513 }
8514
8515 dst_map->size += size;
8516
8517 /*
8518 * Link in the copy
8519 */
8520
8521 if (consume_on_success) {
8522 vm_map_copy_insert(dst_map, last, copy);
8523 } else {
8524 vm_map_copy_remap(dst_map, last, copy, adjustment,
8525 cur_protection, max_protection,
8526 inheritance);
8527 }
8528
8529 vm_map_unlock(dst_map);
8530
8531 /*
8532 * XXX If wiring_required, call vm_map_pageable
8533 */
8534
8535 return(KERN_SUCCESS);
8536}
8537
8538/*
8539 * Routine: vm_map_copyin
8540 *
8541 * Description:
8542 * see vm_map_copyin_common. Exported via Unsupported.exports.
8543 *
8544 */
8545
8546#undef vm_map_copyin
8547
8548kern_return_t
8549vm_map_copyin(
8550 vm_map_t src_map,
8551 vm_map_address_t src_addr,
8552 vm_map_size_t len,
8553 boolean_t src_destroy,
8554 vm_map_copy_t *copy_result) /* OUT */
8555{
8556 return(vm_map_copyin_common(src_map, src_addr, len, src_destroy,
8557 FALSE, copy_result, FALSE));
8558}
8559
8560/*
8561 * Routine: vm_map_copyin_common
8562 *
8563 * Description:
8564 * Copy the specified region (src_addr, len) from the
8565 * source address space (src_map), possibly removing
8566 * the region from the source address space (src_destroy).
8567 *
8568 * Returns:
8569 * A vm_map_copy_t object (copy_result), suitable for
8570 * insertion into another address space (using vm_map_copyout),
8571 * copying over another address space region (using
8572 * vm_map_copy_overwrite). If the copy is unused, it
8573 * should be destroyed (using vm_map_copy_discard).
8574 *
8575 * In/out conditions:
8576 * The source map should not be locked on entry.
8577 */
8578
8579typedef struct submap_map {
8580 vm_map_t parent_map;
8581 vm_map_offset_t base_start;
8582 vm_map_offset_t base_end;
8583 vm_map_size_t base_len;
8584 struct submap_map *next;
8585} submap_map_t;
8586
8587kern_return_t
8588vm_map_copyin_common(
8589 vm_map_t src_map,
8590 vm_map_address_t src_addr,
8591 vm_map_size_t len,
8592 boolean_t src_destroy,
8593 __unused boolean_t src_volatile,
8594 vm_map_copy_t *copy_result, /* OUT */
8595 boolean_t use_maxprot)
8596{
8597 vm_map_entry_t tmp_entry; /* Result of last map lookup --
8598 * in multi-level lookup, this
8599 * entry contains the actual
8600 * vm_object/offset.
8601 */
8602 register
8603 vm_map_entry_t new_entry = VM_MAP_ENTRY_NULL; /* Map entry for copy */
8604
8605 vm_map_offset_t src_start; /* Start of current entry --
8606 * where copy is taking place now
8607 */
8608 vm_map_offset_t src_end; /* End of entire region to be
8609 * copied */
8610 vm_map_offset_t src_base;
8611 vm_map_t base_map = src_map;
8612 boolean_t map_share=FALSE;
8613 submap_map_t *parent_maps = NULL;
8614
8615 register
8616 vm_map_copy_t copy; /* Resulting copy */
8617 vm_map_address_t copy_addr;
8618 vm_map_size_t copy_size;
8619
8620 /*
8621 * Check for copies of zero bytes.
8622 */
8623
8624 if (len == 0) {
8625 *copy_result = VM_MAP_COPY_NULL;
8626 return(KERN_SUCCESS);
8627 }
8628
8629 /*
8630 * Check that the end address doesn't overflow
8631 */
8632 src_end = src_addr + len;
8633 if (src_end < src_addr)
8634 return KERN_INVALID_ADDRESS;
8635
8636 /*
8637 * If the copy is sufficiently small, use a kernel buffer instead
8638 * of making a virtual copy. The theory being that the cost of
8639 * setting up VM (and taking C-O-W faults) dominates the copy costs
8640 * for small regions.
8641 */
8642 if ((len < msg_ool_size_small) && !use_maxprot)
8643 return vm_map_copyin_kernel_buffer(src_map, src_addr, len,
8644 src_destroy, copy_result);
8645
8646 /*
8647 * Compute (page aligned) start and end of region
8648 */
8649 src_start = vm_map_trunc_page(src_addr,
8650 VM_MAP_PAGE_MASK(src_map));
8651 src_end = vm_map_round_page(src_end,
8652 VM_MAP_PAGE_MASK(src_map));
8653
8654 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);
8655
8656 /*
8657 * Allocate a header element for the list.
8658 *
8659 * Use the start and end in the header to
8660 * remember the endpoints prior to rounding.
8661 */
8662
8663 copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
8664 copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
8665 vm_map_copy_first_entry(copy) =
8666 vm_map_copy_last_entry(copy) = vm_map_copy_to_entry(copy);
8667 copy->type = VM_MAP_COPY_ENTRY_LIST;
8668 copy->cpy_hdr.nentries = 0;
8669 copy->cpy_hdr.entries_pageable = TRUE;
8670#if 00
8671 copy->cpy_hdr.page_shift = src_map->hdr.page_shift;
8672#else
8673 /*
8674 * The copy entries can be broken down for a variety of reasons,
8675 * so we can't guarantee that they will remain map-aligned...
8676 * Will need to adjust the first copy_entry's "vme_start" and
8677 * the last copy_entry's "vme_end" to be rounded to PAGE_MASK
8678 * rather than the original map's alignment.
8679 */
8680 copy->cpy_hdr.page_shift = PAGE_SHIFT;
8681#endif
8682
8683 vm_map_store_init( &(copy->cpy_hdr) );
8684
8685 copy->offset = src_addr;
8686 copy->size = len;
8687
8688 new_entry = vm_map_copy_entry_create(copy, !copy->cpy_hdr.entries_pageable);
8689
8690#define RETURN(x) \
8691 MACRO_BEGIN \
8692 vm_map_unlock(src_map); \
8693 if(src_map != base_map) \
8694 vm_map_deallocate(src_map); \
8695 if (new_entry != VM_MAP_ENTRY_NULL) \
8696 vm_map_copy_entry_dispose(copy,new_entry); \
8697 vm_map_copy_discard(copy); \
8698 { \
8699 submap_map_t *_ptr; \
8700 \
8701 for(_ptr = parent_maps; _ptr != NULL; _ptr = parent_maps) { \
8702 parent_maps=parent_maps->next; \
8703 if (_ptr->parent_map != base_map) \
8704 vm_map_deallocate(_ptr->parent_map); \
8705 kfree(_ptr, sizeof(submap_map_t)); \
8706 } \
8707 } \
8708 MACRO_RETURN(x); \
8709 MACRO_END
8710
8711 /*
8712 * Find the beginning of the region.
8713 */
8714
8715 vm_map_lock(src_map);
8716
8717 /*
8718 * Lookup the original "src_addr" rather than the truncated
8719 * "src_start", in case "src_start" falls in a non-map-aligned
8720 * map entry *before* the map entry that contains "src_addr"...
8721 */
8722 if (!vm_map_lookup_entry(src_map, src_addr, &tmp_entry))
8723 RETURN(KERN_INVALID_ADDRESS);
8724 if(!tmp_entry->is_sub_map) {
8725 /*
8726 * ... but clip to the map-rounded "src_start" rather than
8727 * "src_addr" to preserve map-alignment. We'll adjust the
8728 * first copy entry at the end, if needed.
8729 */
8730 vm_map_clip_start(src_map, tmp_entry, src_start);
8731 }
8732 if (src_start < tmp_entry->vme_start) {
8733 /*
8734 * Move "src_start" up to the start of the
8735 * first map entry to copy.
8736 */
8737 src_start = tmp_entry->vme_start;
8738 }
8739 /* set for later submap fix-up */
8740 copy_addr = src_start;
8741
8742 /*
8743 * Go through entries until we get to the end.
8744 */
8745
8746 while (TRUE) {
8747 register
8748 vm_map_entry_t src_entry = tmp_entry; /* Top-level entry */
8749 vm_map_size_t src_size; /* Size of source
8750 * map entry (in both
8751 * maps)
8752 */
8753
8754 register
8755 vm_object_t src_object; /* Object to copy */
8756 vm_object_offset_t src_offset;
8757
8758 boolean_t src_needs_copy; /* Should source map
8759 * be made read-only
8760 * for copy-on-write?
8761 */
8762
8763 boolean_t new_entry_needs_copy; /* Will new entry be COW? */
8764
8765 boolean_t was_wired; /* Was source wired? */
8766 vm_map_version_t version; /* Version before locks
8767 * dropped to make copy
8768 */
8769 kern_return_t result; /* Return value from
8770 * copy_strategically.
8771 */
8772 while(tmp_entry->is_sub_map) {
8773 vm_map_size_t submap_len;
8774 submap_map_t *ptr;
8775
8776 ptr = (submap_map_t *)kalloc(sizeof(submap_map_t));
8777 ptr->next = parent_maps;
8778 parent_maps = ptr;
8779 ptr->parent_map = src_map;
8780 ptr->base_start = src_start;
8781 ptr->base_end = src_end;
8782 submap_len = tmp_entry->vme_end - src_start;
8783 if(submap_len > (src_end-src_start))
8784 submap_len = src_end-src_start;
8785 ptr->base_len = submap_len;
8786
8787 src_start -= tmp_entry->vme_start;
8788 src_start += tmp_entry->offset;
8789 src_end = src_start + submap_len;
8790 src_map = tmp_entry->object.sub_map;
8791 vm_map_lock(src_map);
8792 /* keep an outstanding reference for all maps in */
8793 /* the parents tree except the base map */
8794 vm_map_reference(src_map);
8795 vm_map_unlock(ptr->parent_map);
8796 if (!vm_map_lookup_entry(
8797 src_map, src_start, &tmp_entry))
8798 RETURN(KERN_INVALID_ADDRESS);
8799 map_share = TRUE;
8800 if(!tmp_entry->is_sub_map)
8801 vm_map_clip_start(src_map, tmp_entry, src_start);
8802 src_entry = tmp_entry;
8803 }
8804 /* we are now in the lowest level submap... */
8805
8806 if ((tmp_entry->object.vm_object != VM_OBJECT_NULL) &&
8807 (tmp_entry->object.vm_object->phys_contiguous)) {
8808 /* This is not, supported for now.In future */
8809 /* we will need to detect the phys_contig */
8810 /* condition and then upgrade copy_slowly */
8811 /* to do physical copy from the device mem */
8812 /* based object. We can piggy-back off of */
8813 /* the was wired boolean to set-up the */
8814 /* proper handling */
8815 RETURN(KERN_PROTECTION_FAILURE);
8816 }
8817 /*
8818 * Create a new address map entry to hold the result.
8819 * Fill in the fields from the appropriate source entries.
8820 * We must unlock the source map to do this if we need
8821 * to allocate a map entry.
8822 */
8823 if (new_entry == VM_MAP_ENTRY_NULL) {
8824 version.main_timestamp = src_map->timestamp;
8825 vm_map_unlock(src_map);
8826
8827 new_entry = vm_map_copy_entry_create(copy, !copy->cpy_hdr.entries_pageable);
8828
8829 vm_map_lock(src_map);
8830 if ((version.main_timestamp + 1) != src_map->timestamp) {
8831 if (!vm_map_lookup_entry(src_map, src_start,
8832 &tmp_entry)) {
8833 RETURN(KERN_INVALID_ADDRESS);
8834 }
8835 if (!tmp_entry->is_sub_map)
8836 vm_map_clip_start(src_map, tmp_entry, src_start);
8837 continue; /* restart w/ new tmp_entry */
8838 }
8839 }
8840
8841 /*
8842 * Verify that the region can be read.
8843 */
8844 if (((src_entry->protection & VM_PROT_READ) == VM_PROT_NONE &&
8845 !use_maxprot) ||
8846 (src_entry->max_protection & VM_PROT_READ) == 0)
8847 RETURN(KERN_PROTECTION_FAILURE);
8848
8849 /*
8850 * Clip against the endpoints of the entire region.
8851 */
8852
8853 vm_map_clip_end(src_map, src_entry, src_end);
8854
8855 src_size = src_entry->vme_end - src_start;
8856 src_object = src_entry->object.vm_object;
8857 src_offset = src_entry->offset;
8858 was_wired = (src_entry->wired_count != 0);
8859
8860 vm_map_entry_copy(new_entry, src_entry);
8861 if (new_entry->is_sub_map) {
8862 /* clr address space specifics */
8863 new_entry->use_pmap = FALSE;
8864 }
8865
8866 /*
8867 * Attempt non-blocking copy-on-write optimizations.
8868 */
8869
8870 if (src_destroy &&
8871 (src_object == VM_OBJECT_NULL ||
8872 (src_object->internal && !src_object->true_share
8873 && !map_share))) {
8874 /*
8875 * If we are destroying the source, and the object
8876 * is internal, we can move the object reference
8877 * from the source to the copy. The copy is
8878 * copy-on-write only if the source is.
8879 * We make another reference to the object, because
8880 * destroying the source entry will deallocate it.
8881 */
8882 vm_object_reference(src_object);
8883
8884 /*
8885 * Copy is always unwired. vm_map_copy_entry
8886 * set its wired count to zero.
8887 */
8888
8889 goto CopySuccessful;
8890 }
8891
8892
8893 RestartCopy:
8894 XPR(XPR_VM_MAP, "vm_map_copyin_common src_obj 0x%x ent 0x%x obj 0x%x was_wired %d\n",
8895 src_object, new_entry, new_entry->object.vm_object,
8896 was_wired, 0);
8897 if ((src_object == VM_OBJECT_NULL ||
8898 (!was_wired && !map_share && !tmp_entry->is_shared)) &&
8899 vm_object_copy_quickly(
8900 &new_entry->object.vm_object,
8901 src_offset,
8902 src_size,
8903 &src_needs_copy,
8904 &new_entry_needs_copy)) {
8905
8906 new_entry->needs_copy = new_entry_needs_copy;
8907
8908 /*
8909 * Handle copy-on-write obligations
8910 */
8911
8912 if (src_needs_copy && !tmp_entry->needs_copy) {
8913 vm_prot_t prot;
8914
8915 prot = src_entry->protection & ~VM_PROT_WRITE;
8916
8917 if (override_nx(src_map, src_entry->alias) && prot)
8918 prot |= VM_PROT_EXECUTE;
8919
8920 vm_object_pmap_protect(
8921 src_object,
8922 src_offset,
8923 src_size,
8924 (src_entry->is_shared ?
8925 PMAP_NULL
8926 : src_map->pmap),
8927 src_entry->vme_start,
8928 prot);
8929
8930 tmp_entry->needs_copy = TRUE;
8931 }
8932
8933 /*
8934 * The map has never been unlocked, so it's safe
8935 * to move to the next entry rather than doing
8936 * another lookup.
8937 */
8938
8939 goto CopySuccessful;
8940 }
8941
8942 /*
8943 * Take an object reference, so that we may
8944 * release the map lock(s).
8945 */
8946
8947 assert(src_object != VM_OBJECT_NULL);
8948 vm_object_reference(src_object);
8949
8950 /*
8951 * Record the timestamp for later verification.
8952 * Unlock the map.
8953 */
8954
8955 version.main_timestamp = src_map->timestamp;
8956 vm_map_unlock(src_map); /* Increments timestamp once! */
8957
8958 /*
8959 * Perform the copy
8960 */
8961
8962 if (was_wired) {
8963 CopySlowly:
8964 vm_object_lock(src_object);
8965 result = vm_object_copy_slowly(
8966 src_object,
8967 src_offset,
8968 src_size,
8969 THREAD_UNINT,
8970 &new_entry->object.vm_object);
8971 new_entry->offset = 0;
8972 new_entry->needs_copy = FALSE;
8973
8974 }
8975 else if (src_object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC &&
8976 (tmp_entry->is_shared || map_share)) {
8977 vm_object_t new_object;
8978
8979 vm_object_lock_shared(src_object);
8980 new_object = vm_object_copy_delayed(
8981 src_object,
8982 src_offset,
8983 src_size,
8984 TRUE);
8985 if (new_object == VM_OBJECT_NULL)
8986 goto CopySlowly;
8987
8988 new_entry->object.vm_object = new_object;
8989 new_entry->needs_copy = TRUE;
8990 assert(!new_entry->iokit_acct);
8991 assert(new_object->purgable == VM_PURGABLE_DENY);
8992 new_entry->use_pmap = TRUE;
8993 result = KERN_SUCCESS;
8994
8995 } else {
8996 result = vm_object_copy_strategically(src_object,
8997 src_offset,
8998 src_size,
8999 &new_entry->object.vm_object,
9000 &new_entry->offset,
9001 &new_entry_needs_copy);
9002
9003 new_entry->needs_copy = new_entry_needs_copy;
9004 }
9005
9006 if (result != KERN_SUCCESS &&
9007 result != KERN_MEMORY_RESTART_COPY) {
9008 vm_map_lock(src_map);
9009 RETURN(result);
9010 }
9011
9012 /*
9013 * Throw away the extra reference
9014 */
9015
9016 vm_object_deallocate(src_object);
9017
9018 /*
9019 * Verify that the map has not substantially
9020 * changed while the copy was being made.
9021 */
9022
9023 vm_map_lock(src_map);
9024
9025 if ((version.main_timestamp + 1) == src_map->timestamp)
9026 goto VerificationSuccessful;
9027
9028 /*
9029 * Simple version comparison failed.
9030 *
9031 * Retry the lookup and verify that the
9032 * same object/offset are still present.
9033 *
9034 * [Note: a memory manager that colludes with
9035 * the calling task can detect that we have
9036 * cheated. While the map was unlocked, the
9037 * mapping could have been changed and restored.]
9038 */
9039
9040 if (!vm_map_lookup_entry(src_map, src_start, &tmp_entry)) {
9041 if (result != KERN_MEMORY_RESTART_COPY) {
9042 vm_object_deallocate(new_entry->object.vm_object);
9043 new_entry->object.vm_object = VM_OBJECT_NULL;
9044 assert(!new_entry->iokit_acct);
9045 new_entry->use_pmap = TRUE;
9046 }
9047 RETURN(KERN_INVALID_ADDRESS);
9048 }
9049
9050 src_entry = tmp_entry;
9051 vm_map_clip_start(src_map, src_entry, src_start);
9052
9053 if ((((src_entry->protection & VM_PROT_READ) == VM_PROT_NONE) &&
9054 !use_maxprot) ||
9055 ((src_entry->max_protection & VM_PROT_READ) == 0))
9056 goto VerificationFailed;
9057
9058 if (src_entry->vme_end < new_entry->vme_end) {
9059 assert(VM_MAP_PAGE_ALIGNED(src_entry->vme_end,
9060 VM_MAP_COPY_PAGE_MASK(copy)));
9061 new_entry->vme_end = src_entry->vme_end;
9062 src_size = new_entry->vme_end - src_start;
9063 }
9064
9065 if ((src_entry->object.vm_object != src_object) ||
9066 (src_entry->offset != src_offset) ) {
9067
9068 /*
9069 * Verification failed.
9070 *
9071 * Start over with this top-level entry.
9072 */
9073
9074 VerificationFailed: ;
9075
9076 vm_object_deallocate(new_entry->object.vm_object);
9077 tmp_entry = src_entry;
9078 continue;
9079 }
9080
9081 /*
9082 * Verification succeeded.
9083 */
9084
9085 VerificationSuccessful: ;
9086
9087 if (result == KERN_MEMORY_RESTART_COPY)
9088 goto RestartCopy;
9089
9090 /*
9091 * Copy succeeded.
9092 */
9093
9094 CopySuccessful: ;
9095
9096 /*
9097 * Link in the new copy entry.
9098 */
9099
9100 vm_map_copy_entry_link(copy, vm_map_copy_last_entry(copy),
9101 new_entry);
9102
9103 /*
9104 * Determine whether the entire region
9105 * has been copied.
9106 */
9107 src_base = src_start;
9108 src_start = new_entry->vme_end;
9109 new_entry = VM_MAP_ENTRY_NULL;
9110 while ((src_start >= src_end) && (src_end != 0)) {
9111 submap_map_t *ptr;
9112
9113 if (src_map == base_map) {
9114 /* back to the top */
9115 break;
9116 }
9117
9118 ptr = parent_maps;
9119 assert(ptr != NULL);
9120 parent_maps = parent_maps->next;
9121
9122 /* fix up the damage we did in that submap */
9123 vm_map_simplify_range(src_map,
9124 src_base,
9125 src_end);
9126
9127 vm_map_unlock(src_map);
9128 vm_map_deallocate(src_map);
9129 vm_map_lock(ptr->parent_map);
9130 src_map = ptr->parent_map;
9131 src_base = ptr->base_start;
9132 src_start = ptr->base_start + ptr->base_len;
9133 src_end = ptr->base_end;
9134 if (!vm_map_lookup_entry(src_map,
9135 src_start,
9136 &tmp_entry) &&
9137 (src_end > src_start)) {
9138 RETURN(KERN_INVALID_ADDRESS);
9139 }
9140 kfree(ptr, sizeof(submap_map_t));
9141 if (parent_maps == NULL)
9142 map_share = FALSE;
9143 src_entry = tmp_entry->vme_prev;
9144 }
9145
9146 if ((VM_MAP_PAGE_SHIFT(src_map) != PAGE_SHIFT) &&
9147 (src_start >= src_addr + len) &&
9148 (src_addr + len != 0)) {
9149 /*
9150 * Stop copying now, even though we haven't reached
9151 * "src_end". We'll adjust the end of the last copy
9152 * entry at the end, if needed.
9153 *
9154 * If src_map's aligment is different from the
9155 * system's page-alignment, there could be
9156 * extra non-map-aligned map entries between
9157 * the original (non-rounded) "src_addr + len"
9158 * and the rounded "src_end".
9159 * We do not want to copy those map entries since
9160 * they're not part of the copied range.
9161 */
9162 break;
9163 }
9164
9165 if ((src_start >= src_end) && (src_end != 0))
9166 break;
9167
9168 /*
9169 * Verify that there are no gaps in the region
9170 */
9171
9172 tmp_entry = src_entry->vme_next;
9173 if ((tmp_entry->vme_start != src_start) ||
9174 (tmp_entry == vm_map_to_entry(src_map))) {
9175 RETURN(KERN_INVALID_ADDRESS);
9176 }
9177 }
9178
9179 /*
9180 * If the source should be destroyed, do it now, since the
9181 * copy was successful.
9182 */
9183 if (src_destroy) {
9184 (void) vm_map_delete(
9185 src_map,
9186 vm_map_trunc_page(src_addr,
9187 VM_MAP_PAGE_MASK(src_map)),
9188 src_end,
9189 ((src_map == kernel_map) ?
9190 VM_MAP_REMOVE_KUNWIRE :
9191 VM_MAP_NO_FLAGS),
9192 VM_MAP_NULL);
9193 } else {
9194 /* fix up the damage we did in the base map */
9195 vm_map_simplify_range(
9196 src_map,
9197 vm_map_trunc_page(src_addr,
9198 VM_MAP_PAGE_MASK(src_map)),
9199 vm_map_round_page(src_end,
9200 VM_MAP_PAGE_MASK(src_map)));
9201 }
9202
9203 vm_map_unlock(src_map);
9204
9205 if (VM_MAP_PAGE_SHIFT(src_map) != PAGE_SHIFT) {
9206 vm_map_offset_t original_start, original_offset, original_end;
9207
9208 assert(VM_MAP_COPY_PAGE_MASK(copy) == PAGE_MASK);
9209
9210 /* adjust alignment of first copy_entry's "vme_start" */
9211 tmp_entry = vm_map_copy_first_entry(copy);
9212 if (tmp_entry != vm_map_copy_to_entry(copy)) {
9213 vm_map_offset_t adjustment;
9214
9215 original_start = tmp_entry->vme_start;
9216 original_offset = tmp_entry->offset;
9217
9218 /* map-align the start of the first copy entry... */
9219 adjustment = (tmp_entry->vme_start -
9220 vm_map_trunc_page(
9221 tmp_entry->vme_start,
9222 VM_MAP_PAGE_MASK(src_map)));
9223 tmp_entry->vme_start -= adjustment;
9224 tmp_entry->offset -= adjustment;
9225 copy_addr -= adjustment;
9226 assert(tmp_entry->vme_start < tmp_entry->vme_end);
9227 /* ... adjust for mis-aligned start of copy range */
9228 adjustment =
9229 (vm_map_trunc_page(copy->offset,
9230 PAGE_MASK) -
9231 vm_map_trunc_page(copy->offset,
9232 VM_MAP_PAGE_MASK(src_map)));
9233 if (adjustment) {
9234 assert(page_aligned(adjustment));
9235 assert(adjustment < VM_MAP_PAGE_SIZE(src_map));
9236 tmp_entry->vme_start += adjustment;
9237 tmp_entry->offset += adjustment;
9238 copy_addr += adjustment;
9239 assert(tmp_entry->vme_start < tmp_entry->vme_end);
9240 }
9241
9242 /*
9243 * Assert that the adjustments haven't exposed
9244 * more than was originally copied...
9245 */
9246 assert(tmp_entry->vme_start >= original_start);
9247 assert(tmp_entry->offset >= original_offset);
9248 /*
9249 * ... and that it did not adjust outside of a
9250 * a single 16K page.
9251 */
9252 assert(vm_map_trunc_page(tmp_entry->vme_start,
9253 VM_MAP_PAGE_MASK(src_map)) ==
9254 vm_map_trunc_page(original_start,
9255 VM_MAP_PAGE_MASK(src_map)));
9256 }
9257
9258 /* adjust alignment of last copy_entry's "vme_end" */
9259 tmp_entry = vm_map_copy_last_entry(copy);
9260 if (tmp_entry != vm_map_copy_to_entry(copy)) {
9261 vm_map_offset_t adjustment;
9262
9263 original_end = tmp_entry->vme_end;
9264
9265 /* map-align the end of the last copy entry... */
9266 tmp_entry->vme_end =
9267 vm_map_round_page(tmp_entry->vme_end,
9268 VM_MAP_PAGE_MASK(src_map));
9269 /* ... adjust for mis-aligned end of copy range */
9270 adjustment =
9271 (vm_map_round_page((copy->offset +
9272 copy->size),
9273 VM_MAP_PAGE_MASK(src_map)) -
9274 vm_map_round_page((copy->offset +
9275 copy->size),
9276 PAGE_MASK));
9277 if (adjustment) {
9278 assert(page_aligned(adjustment));
9279 assert(adjustment < VM_MAP_PAGE_SIZE(src_map));
9280 tmp_entry->vme_end -= adjustment;
9281 assert(tmp_entry->vme_start < tmp_entry->vme_end);
9282 }
9283
9284 /*
9285 * Assert that the adjustments haven't exposed
9286 * more than was originally copied...
9287 */
9288 assert(tmp_entry->vme_end <= original_end);
9289 /*
9290 * ... and that it did not adjust outside of a
9291 * a single 16K page.
9292 */
9293 assert(vm_map_round_page(tmp_entry->vme_end,
9294 VM_MAP_PAGE_MASK(src_map)) ==
9295 vm_map_round_page(original_end,
9296 VM_MAP_PAGE_MASK(src_map)));
9297 }
9298 }
9299
9300 /* Fix-up start and end points in copy. This is necessary */
9301 /* when the various entries in the copy object were picked */
9302 /* up from different sub-maps */
9303
9304 tmp_entry = vm_map_copy_first_entry(copy);
9305 copy_size = 0; /* compute actual size */
9306 while (tmp_entry != vm_map_copy_to_entry(copy)) {
9307 assert(VM_MAP_PAGE_ALIGNED(
9308 copy_addr + (tmp_entry->vme_end -
9309 tmp_entry->vme_start),
9310 VM_MAP_COPY_PAGE_MASK(copy)));
9311 assert(VM_MAP_PAGE_ALIGNED(
9312 copy_addr,
9313 VM_MAP_COPY_PAGE_MASK(copy)));
9314
9315 /*
9316 * The copy_entries will be injected directly into the
9317 * destination map and might not be "map aligned" there...
9318 */
9319 tmp_entry->map_aligned = FALSE;
9320
9321 tmp_entry->vme_end = copy_addr +
9322 (tmp_entry->vme_end - tmp_entry->vme_start);
9323 tmp_entry->vme_start = copy_addr;
9324 assert(tmp_entry->vme_start < tmp_entry->vme_end);
9325 copy_addr += tmp_entry->vme_end - tmp_entry->vme_start;
9326 copy_size += tmp_entry->vme_end - tmp_entry->vme_start;
9327 tmp_entry = (struct vm_map_entry *)tmp_entry->vme_next;
9328 }
9329
9330 if (VM_MAP_PAGE_SHIFT(src_map) != PAGE_SHIFT &&
9331 copy_size < copy->size) {
9332 /*
9333 * The actual size of the VM map copy is smaller than what
9334 * was requested by the caller. This must be because some
9335 * PAGE_SIZE-sized pages are missing at the end of the last
9336 * VM_MAP_PAGE_SIZE(src_map)-sized chunk of the range.
9337 * The caller might not have been aware of those missing
9338 * pages and might not want to be aware of it, which is
9339 * fine as long as they don't try to access (and crash on)
9340 * those missing pages.
9341 * Let's adjust the size of the "copy", to avoid failing
9342 * in vm_map_copyout() or vm_map_copy_overwrite().
9343 */
9344 assert(vm_map_round_page(copy_size,
9345 VM_MAP_PAGE_MASK(src_map)) ==
9346 vm_map_round_page(copy->size,
9347 VM_MAP_PAGE_MASK(src_map)));
9348 copy->size = copy_size;
9349 }
9350
9351 *copy_result = copy;
9352 return(KERN_SUCCESS);
9353
9354#undef RETURN
9355}
9356
9357kern_return_t
9358vm_map_copy_extract(
9359 vm_map_t src_map,
9360 vm_map_address_t src_addr,
9361 vm_map_size_t len,
9362 vm_map_copy_t *copy_result, /* OUT */
9363 vm_prot_t *cur_prot, /* OUT */
9364 vm_prot_t *max_prot)
9365{
9366 vm_map_offset_t src_start, src_end;
9367 vm_map_copy_t copy;
9368 kern_return_t kr;
9369
9370 /*
9371 * Check for copies of zero bytes.
9372 */
9373
9374 if (len == 0) {
9375 *copy_result = VM_MAP_COPY_NULL;
9376 return(KERN_SUCCESS);
9377 }
9378
9379 /*
9380 * Check that the end address doesn't overflow
9381 */
9382 src_end = src_addr + len;
9383 if (src_end < src_addr)
9384 return KERN_INVALID_ADDRESS;
9385
9386 /*
9387 * Compute (page aligned) start and end of region
9388 */
9389 src_start = vm_map_trunc_page(src_addr, PAGE_MASK);
9390 src_end = vm_map_round_page(src_end, PAGE_MASK);
9391
9392 /*
9393 * Allocate a header element for the list.
9394 *
9395 * Use the start and end in the header to
9396 * remember the endpoints prior to rounding.
9397 */
9398
9399 copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
9400 copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
9401 vm_map_copy_first_entry(copy) =
9402 vm_map_copy_last_entry(copy) = vm_map_copy_to_entry(copy);
9403 copy->type = VM_MAP_COPY_ENTRY_LIST;
9404 copy->cpy_hdr.nentries = 0;
9405 copy->cpy_hdr.entries_pageable = TRUE;
9406
9407 vm_map_store_init(&copy->cpy_hdr);
9408
9409 copy->offset = 0;
9410 copy->size = len;
9411
9412 kr = vm_map_remap_extract(src_map,
9413 src_addr,
9414 len,
9415 FALSE, /* copy */
9416 &copy->cpy_hdr,
9417 cur_prot,
9418 max_prot,
9419 VM_INHERIT_SHARE,
9420 TRUE); /* pageable */
9421 if (kr != KERN_SUCCESS) {
9422 vm_map_copy_discard(copy);
9423 return kr;
9424 }
9425
9426 *copy_result = copy;
9427 return KERN_SUCCESS;
9428}
9429
9430/*
9431 * vm_map_copyin_object:
9432 *
9433 * Create a copy object from an object.
9434 * Our caller donates an object reference.
9435 */
9436
9437kern_return_t
9438vm_map_copyin_object(
9439 vm_object_t object,
9440 vm_object_offset_t offset, /* offset of region in object */
9441 vm_object_size_t size, /* size of region in object */
9442 vm_map_copy_t *copy_result) /* OUT */
9443{
9444 vm_map_copy_t copy; /* Resulting copy */
9445
9446 /*
9447 * We drop the object into a special copy object
9448 * that contains the object directly.
9449 */
9450
9451 copy = (vm_map_copy_t) zalloc(vm_map_copy_zone);
9452 copy->c_u.hdr.rb_head_store.rbh_root = (void*)(int)SKIP_RB_TREE;
9453 copy->type = VM_MAP_COPY_OBJECT;
9454 copy->cpy_object = object;
9455 copy->offset = offset;
9456 copy->size = size;
9457
9458 *copy_result = copy;
9459 return(KERN_SUCCESS);
9460}
9461
9462static void
9463vm_map_fork_share(
9464 vm_map_t old_map,
9465 vm_map_entry_t old_entry,
9466 vm_map_t new_map)
9467{
9468 vm_object_t object;
9469 vm_map_entry_t new_entry;
9470
9471 /*
9472 * New sharing code. New map entry
9473 * references original object. Internal
9474 * objects use asynchronous copy algorithm for
9475 * future copies. First make sure we have
9476 * the right object. If we need a shadow,
9477 * or someone else already has one, then
9478 * make a new shadow and share it.
9479 */
9480
9481 object = old_entry->object.vm_object;
9482 if (old_entry->is_sub_map) {
9483 assert(old_entry->wired_count == 0);
9484#ifndef NO_NESTED_PMAP
9485 if(old_entry->use_pmap) {
9486 kern_return_t result;
9487
9488 result = pmap_nest(new_map->pmap,
9489 (old_entry->object.sub_map)->pmap,
9490 (addr64_t)old_entry->vme_start,
9491 (addr64_t)old_entry->vme_start,
9492 (uint64_t)(old_entry->vme_end - old_entry->vme_start));
9493 if(result)
9494 panic("vm_map_fork_share: pmap_nest failed!");
9495 }
9496#endif /* NO_NESTED_PMAP */
9497 } else if (object == VM_OBJECT_NULL) {
9498 object = vm_object_allocate((vm_map_size_t)(old_entry->vme_end -
9499 old_entry->vme_start));
9500 old_entry->offset = 0;
9501 old_entry->object.vm_object = object;
9502 old_entry->use_pmap = TRUE;
9503 assert(!old_entry->needs_copy);
9504 } else if (object->copy_strategy !=
9505 MEMORY_OBJECT_COPY_SYMMETRIC) {
9506
9507 /*
9508 * We are already using an asymmetric
9509 * copy, and therefore we already have
9510 * the right object.
9511 */
9512
9513 assert(! old_entry->needs_copy);
9514 }
9515 else if (old_entry->needs_copy || /* case 1 */
9516 object->shadowed || /* case 2 */
9517 (!object->true_share && /* case 3 */
9518 !old_entry->is_shared &&
9519 (object->vo_size >
9520 (vm_map_size_t)(old_entry->vme_end -
9521 old_entry->vme_start)))) {
9522
9523 /*
9524 * We need to create a shadow.
9525 * There are three cases here.
9526 * In the first case, we need to
9527 * complete a deferred symmetrical
9528 * copy that we participated in.
9529 * In the second and third cases,
9530 * we need to create the shadow so
9531 * that changes that we make to the
9532 * object do not interfere with
9533 * any symmetrical copies which
9534 * have occured (case 2) or which
9535 * might occur (case 3).
9536 *
9537 * The first case is when we had
9538 * deferred shadow object creation
9539 * via the entry->needs_copy mechanism.
9540 * This mechanism only works when
9541 * only one entry points to the source
9542 * object, and we are about to create
9543 * a second entry pointing to the
9544 * same object. The problem is that
9545 * there is no way of mapping from
9546 * an object to the entries pointing
9547 * to it. (Deferred shadow creation
9548 * works with one entry because occurs
9549 * at fault time, and we walk from the
9550 * entry to the object when handling
9551 * the fault.)
9552 *
9553 * The second case is when the object
9554 * to be shared has already been copied
9555 * with a symmetric copy, but we point
9556 * directly to the object without
9557 * needs_copy set in our entry. (This
9558 * can happen because different ranges
9559 * of an object can be pointed to by
9560 * different entries. In particular,
9561 * a single entry pointing to an object
9562 * can be split by a call to vm_inherit,
9563 * which, combined with task_create, can
9564 * result in the different entries
9565 * having different needs_copy values.)
9566 * The shadowed flag in the object allows
9567 * us to detect this case. The problem
9568 * with this case is that if this object
9569 * has or will have shadows, then we
9570 * must not perform an asymmetric copy
9571 * of this object, since such a copy
9572 * allows the object to be changed, which
9573 * will break the previous symmetrical
9574 * copies (which rely upon the object
9575 * not changing). In a sense, the shadowed
9576 * flag says "don't change this object".
9577 * We fix this by creating a shadow
9578 * object for this object, and sharing
9579 * that. This works because we are free
9580 * to change the shadow object (and thus
9581 * to use an asymmetric copy strategy);
9582 * this is also semantically correct,
9583 * since this object is temporary, and
9584 * therefore a copy of the object is
9585 * as good as the object itself. (This
9586 * is not true for permanent objects,
9587 * since the pager needs to see changes,
9588 * which won't happen if the changes
9589 * are made to a copy.)
9590 *
9591 * The third case is when the object
9592 * to be shared has parts sticking
9593 * outside of the entry we're working
9594 * with, and thus may in the future
9595 * be subject to a symmetrical copy.
9596 * (This is a preemptive version of
9597 * case 2.)
9598 */
9599 vm_object_shadow(&old_entry->object.vm_object,
9600 &old_entry->offset,
9601 (vm_map_size_t) (old_entry->vme_end -
9602 old_entry->vme_start));
9603
9604 /*
9605 * If we're making a shadow for other than
9606 * copy on write reasons, then we have
9607 * to remove write permission.
9608 */
9609
9610 if (!old_entry->needs_copy &&
9611 (old_entry->protection & VM_PROT_WRITE)) {
9612 vm_prot_t prot;
9613
9614 prot = old_entry->protection & ~VM_PROT_WRITE;
9615
9616 if (override_nx(old_map, old_entry->alias) && prot)
9617 prot |= VM_PROT_EXECUTE;
9618
9619 if (old_map->mapped_in_other_pmaps) {
9620 vm_object_pmap_protect(
9621 old_entry->object.vm_object,
9622 old_entry->offset,
9623 (old_entry->vme_end -
9624 old_entry->vme_start),
9625 PMAP_NULL,
9626 old_entry->vme_start,
9627 prot);
9628 } else {
9629 pmap_protect(old_map->pmap,
9630 old_entry->vme_start,
9631 old_entry->vme_end,
9632 prot);
9633 }
9634 }
9635
9636 old_entry->needs_copy = FALSE;
9637 object = old_entry->object.vm_object;
9638 }
9639
9640
9641 /*
9642 * If object was using a symmetric copy strategy,
9643 * change its copy strategy to the default
9644 * asymmetric copy strategy, which is copy_delay
9645 * in the non-norma case and copy_call in the
9646 * norma case. Bump the reference count for the
9647 * new entry.
9648 */
9649
9650 if(old_entry->is_sub_map) {
9651 vm_map_lock(old_entry->object.sub_map);
9652 vm_map_reference(old_entry->object.sub_map);
9653 vm_map_unlock(old_entry->object.sub_map);
9654 } else {
9655 vm_object_lock(object);
9656 vm_object_reference_locked(object);
9657 if (object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC) {
9658 object->copy_strategy = MEMORY_OBJECT_COPY_DELAY;
9659 }
9660 vm_object_unlock(object);
9661 }
9662
9663 /*
9664 * Clone the entry, using object ref from above.
9665 * Mark both entries as shared.
9666 */
9667
9668 new_entry = vm_map_entry_create(new_map, FALSE); /* Never the kernel
9669 * map or descendants */
9670 vm_map_entry_copy(new_entry, old_entry);
9671 old_entry->is_shared = TRUE;
9672 new_entry->is_shared = TRUE;
9673
9674 /*
9675 * Insert the entry into the new map -- we
9676 * know we're inserting at the end of the new
9677 * map.
9678 */
9679
9680 vm_map_store_entry_link(new_map, vm_map_last_entry(new_map), new_entry);
9681
9682 /*
9683 * Update the physical map
9684 */
9685
9686 if (old_entry->is_sub_map) {
9687 /* Bill Angell pmap support goes here */
9688 } else {
9689 pmap_copy(new_map->pmap, old_map->pmap, new_entry->vme_start,
9690 old_entry->vme_end - old_entry->vme_start,
9691 old_entry->vme_start);
9692 }
9693}
9694
9695static boolean_t
9696vm_map_fork_copy(
9697 vm_map_t old_map,
9698 vm_map_entry_t *old_entry_p,
9699 vm_map_t new_map)
9700{
9701 vm_map_entry_t old_entry = *old_entry_p;
9702 vm_map_size_t entry_size = old_entry->vme_end - old_entry->vme_start;
9703 vm_map_offset_t start = old_entry->vme_start;
9704 vm_map_copy_t copy;
9705 vm_map_entry_t last = vm_map_last_entry(new_map);
9706
9707 vm_map_unlock(old_map);
9708 /*
9709 * Use maxprot version of copyin because we
9710 * care about whether this memory can ever
9711 * be accessed, not just whether it's accessible
9712 * right now.
9713 */
9714 if (vm_map_copyin_maxprot(old_map, start, entry_size, FALSE, &copy)
9715 != KERN_SUCCESS) {
9716 /*
9717 * The map might have changed while it
9718 * was unlocked, check it again. Skip
9719 * any blank space or permanently
9720 * unreadable region.
9721 */
9722 vm_map_lock(old_map);
9723 if (!vm_map_lookup_entry(old_map, start, &last) ||
9724 (last->max_protection & VM_PROT_READ) == VM_PROT_NONE) {
9725 last = last->vme_next;
9726 }
9727 *old_entry_p = last;
9728
9729 /*
9730 * XXX For some error returns, want to
9731 * XXX skip to the next element. Note
9732 * that INVALID_ADDRESS and
9733 * PROTECTION_FAILURE are handled above.
9734 */
9735
9736 return FALSE;
9737 }
9738
9739 /*
9740 * Insert the copy into the new map
9741 */
9742
9743 vm_map_copy_insert(new_map, last, copy);
9744
9745 /*
9746 * Pick up the traversal at the end of
9747 * the copied region.
9748 */
9749
9750 vm_map_lock(old_map);
9751 start += entry_size;
9752 if (! vm_map_lookup_entry(old_map, start, &last)) {
9753 last = last->vme_next;
9754 } else {
9755 if (last->vme_start == start) {
9756 /*
9757 * No need to clip here and we don't
9758 * want to cause any unnecessary
9759 * unnesting...
9760 */
9761 } else {
9762 vm_map_clip_start(old_map, last, start);
9763 }
9764 }
9765 *old_entry_p = last;
9766
9767 return TRUE;
9768}
9769
9770/*
9771 * vm_map_fork:
9772 *
9773 * Create and return a new map based on the old
9774 * map, according to the inheritance values on the
9775 * regions in that map.
9776 *
9777 * The source map must not be locked.
9778 */
9779vm_map_t
9780vm_map_fork(
9781 ledger_t ledger,
9782 vm_map_t old_map)
9783{
9784 pmap_t new_pmap;
9785 vm_map_t new_map;
9786 vm_map_entry_t old_entry;
9787 vm_map_size_t new_size = 0, entry_size;
9788 vm_map_entry_t new_entry;
9789 boolean_t src_needs_copy;
9790 boolean_t new_entry_needs_copy;
9791
9792 new_pmap = pmap_create(ledger, (vm_map_size_t) 0,
9793#if defined(__i386__) || defined(__x86_64__)
9794 old_map->pmap->pm_task_map != TASK_MAP_32BIT
9795#else
9796#error Unknown architecture.
9797#endif
9798 );
9799
9800 vm_map_reference_swap(old_map);
9801 vm_map_lock(old_map);
9802
9803 new_map = vm_map_create(new_pmap,
9804 old_map->min_offset,
9805 old_map->max_offset,
9806 old_map->hdr.entries_pageable);
9807 /* inherit the parent map's page size */
9808 vm_map_set_page_shift(new_map, VM_MAP_PAGE_SHIFT(old_map));
9809 for (
9810 old_entry = vm_map_first_entry(old_map);
9811 old_entry != vm_map_to_entry(old_map);
9812 ) {
9813
9814 entry_size = old_entry->vme_end - old_entry->vme_start;
9815
9816 switch (old_entry->inheritance) {
9817 case VM_INHERIT_NONE:
9818 break;
9819
9820 case VM_INHERIT_SHARE:
9821 vm_map_fork_share(old_map, old_entry, new_map);
9822 new_size += entry_size;
9823 break;
9824
9825 case VM_INHERIT_COPY:
9826
9827 /*
9828 * Inline the copy_quickly case;
9829 * upon failure, fall back on call
9830 * to vm_map_fork_copy.
9831 */
9832
9833 if(old_entry->is_sub_map)
9834 break;
9835 if ((old_entry->wired_count != 0) ||
9836 ((old_entry->object.vm_object != NULL) &&
9837 (old_entry->object.vm_object->true_share))) {
9838 goto slow_vm_map_fork_copy;
9839 }
9840
9841 new_entry = vm_map_entry_create(new_map, FALSE); /* never the kernel map or descendants */
9842 vm_map_entry_copy(new_entry, old_entry);
9843 if (new_entry->is_sub_map) {
9844 /* clear address space specifics */
9845 new_entry->use_pmap = FALSE;
9846 }
9847
9848 if (! vm_object_copy_quickly(
9849 &new_entry->object.vm_object,
9850 old_entry->offset,
9851 (old_entry->vme_end -
9852 old_entry->vme_start),
9853 &src_needs_copy,
9854 &new_entry_needs_copy)) {
9855 vm_map_entry_dispose(new_map, new_entry);
9856 goto slow_vm_map_fork_copy;
9857 }
9858
9859 /*
9860 * Handle copy-on-write obligations
9861 */
9862
9863 if (src_needs_copy && !old_entry->needs_copy) {
9864 vm_prot_t prot;
9865
9866 prot = old_entry->protection & ~VM_PROT_WRITE;
9867
9868 if (override_nx(old_map, old_entry->alias) && prot)
9869 prot |= VM_PROT_EXECUTE;
9870
9871 vm_object_pmap_protect(
9872 old_entry->object.vm_object,
9873 old_entry->offset,
9874 (old_entry->vme_end -
9875 old_entry->vme_start),
9876 ((old_entry->is_shared
9877 || old_map->mapped_in_other_pmaps)
9878 ? PMAP_NULL :
9879 old_map->pmap),
9880 old_entry->vme_start,
9881 prot);
9882
9883 old_entry->needs_copy = TRUE;
9884 }
9885 new_entry->needs_copy = new_entry_needs_copy;
9886
9887 /*
9888 * Insert the entry at the end
9889 * of the map.
9890 */
9891
9892 vm_map_store_entry_link(new_map, vm_map_last_entry(new_map),
9893 new_entry);
9894 new_size += entry_size;
9895 break;
9896
9897 slow_vm_map_fork_copy:
9898 if (vm_map_fork_copy(old_map, &old_entry, new_map)) {
9899 new_size += entry_size;
9900 }
9901 continue;
9902 }
9903 old_entry = old_entry->vme_next;
9904 }
9905
9906
9907 new_map->size = new_size;
9908 vm_map_unlock(old_map);
9909 vm_map_deallocate(old_map);
9910
9911 return(new_map);
9912}
9913
9914/*
9915 * vm_map_exec:
9916 *
9917 * Setup the "new_map" with the proper execution environment according
9918 * to the type of executable (platform, 64bit, chroot environment).
9919 * Map the comm page and shared region, etc...
9920 */
9921kern_return_t
9922vm_map_exec(
9923 vm_map_t new_map,
9924 task_t task,
9925 void *fsroot,
9926 cpu_type_t cpu)
9927{
9928 SHARED_REGION_TRACE_DEBUG(
9929 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): ->\n",
9930 (void *)VM_KERNEL_ADDRPERM(current_task()),
9931 (void *)VM_KERNEL_ADDRPERM(new_map),
9932 (void *)VM_KERNEL_ADDRPERM(task),
9933 (void *)VM_KERNEL_ADDRPERM(fsroot),
9934 cpu));
9935 (void) vm_commpage_enter(new_map, task);
9936 (void) vm_shared_region_enter(new_map, task, fsroot, cpu);
9937 SHARED_REGION_TRACE_DEBUG(
9938 ("shared_region: task %p: vm_map_exec(%p,%p,%p,0x%x): <-\n",
9939 (void *)VM_KERNEL_ADDRPERM(current_task()),
9940 (void *)VM_KERNEL_ADDRPERM(new_map),
9941 (void *)VM_KERNEL_ADDRPERM(task),
9942 (void *)VM_KERNEL_ADDRPERM(fsroot),
9943 cpu));
9944 return KERN_SUCCESS;
9945}
9946
9947/*
9948 * vm_map_lookup_locked:
9949 *
9950 * Finds the VM object, offset, and
9951 * protection for a given virtual address in the
9952 * specified map, assuming a page fault of the
9953 * type specified.
9954 *
9955 * Returns the (object, offset, protection) for
9956 * this address, whether it is wired down, and whether
9957 * this map has the only reference to the data in question.
9958 * In order to later verify this lookup, a "version"
9959 * is returned.
9960 *
9961 * The map MUST be locked by the caller and WILL be
9962 * locked on exit. In order to guarantee the
9963 * existence of the returned object, it is returned
9964 * locked.
9965 *
9966 * If a lookup is requested with "write protection"
9967 * specified, the map may be changed to perform virtual
9968 * copying operations, although the data referenced will
9969 * remain the same.
9970 */
9971kern_return_t
9972vm_map_lookup_locked(
9973 vm_map_t *var_map, /* IN/OUT */
9974 vm_map_offset_t vaddr,
9975 vm_prot_t fault_type,
9976 int object_lock_type,
9977 vm_map_version_t *out_version, /* OUT */
9978 vm_object_t *object, /* OUT */
9979 vm_object_offset_t *offset, /* OUT */
9980 vm_prot_t *out_prot, /* OUT */
9981 boolean_t *wired, /* OUT */
9982 vm_object_fault_info_t fault_info, /* OUT */
9983 vm_map_t *real_map)
9984{
9985 vm_map_entry_t entry;
9986 register vm_map_t map = *var_map;
9987 vm_map_t old_map = *var_map;
9988 vm_map_t cow_sub_map_parent = VM_MAP_NULL;
9989 vm_map_offset_t cow_parent_vaddr = 0;
9990 vm_map_offset_t old_start = 0;
9991 vm_map_offset_t old_end = 0;
9992 register vm_prot_t prot;
9993 boolean_t mask_protections;
9994 boolean_t force_copy;
9995 vm_prot_t original_fault_type;
9996
9997 /*
9998 * VM_PROT_MASK means that the caller wants us to use "fault_type"
9999 * as a mask against the mapping's actual protections, not as an
10000 * absolute value.
10001 */
10002 mask_protections = (fault_type & VM_PROT_IS_MASK) ? TRUE : FALSE;
10003 force_copy = (fault_type & VM_PROT_COPY) ? TRUE : FALSE;
10004 fault_type &= VM_PROT_ALL;
10005 original_fault_type = fault_type;
10006
10007 *real_map = map;
10008
10009RetryLookup:
10010 fault_type = original_fault_type;
10011
10012 /*
10013 * If the map has an interesting hint, try it before calling
10014 * full blown lookup routine.
10015 */
10016 entry = map->hint;
10017
10018 if ((entry == vm_map_to_entry(map)) ||
10019 (vaddr < entry->vme_start) || (vaddr >= entry->vme_end)) {
10020 vm_map_entry_t tmp_entry;
10021
10022 /*
10023 * Entry was either not a valid hint, or the vaddr
10024 * was not contained in the entry, so do a full lookup.
10025 */
10026 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
10027 if((cow_sub_map_parent) && (cow_sub_map_parent != map))
10028 vm_map_unlock(cow_sub_map_parent);
10029 if((*real_map != map)
10030 && (*real_map != cow_sub_map_parent))
10031 vm_map_unlock(*real_map);
10032 return KERN_INVALID_ADDRESS;
10033 }
10034
10035 entry = tmp_entry;
10036 }
10037 if(map == old_map) {
10038 old_start = entry->vme_start;
10039 old_end = entry->vme_end;
10040 }
10041
10042 /*
10043 * Handle submaps. Drop lock on upper map, submap is
10044 * returned locked.
10045 */
10046
10047submap_recurse:
10048 if (entry->is_sub_map) {
10049 vm_map_offset_t local_vaddr;
10050 vm_map_offset_t end_delta;
10051 vm_map_offset_t start_delta;
10052 vm_map_entry_t submap_entry;
10053 boolean_t mapped_needs_copy=FALSE;
10054
10055 local_vaddr = vaddr;
10056
10057 if ((entry->use_pmap && !(fault_type & VM_PROT_WRITE))) {
10058 /* if real_map equals map we unlock below */
10059 if ((*real_map != map) &&
10060 (*real_map != cow_sub_map_parent))
10061 vm_map_unlock(*real_map);
10062 *real_map = entry->object.sub_map;
10063 }
10064
10065 if(entry->needs_copy && (fault_type & VM_PROT_WRITE)) {
10066 if (!mapped_needs_copy) {
10067 if (vm_map_lock_read_to_write(map)) {
10068 vm_map_lock_read(map);
10069 *real_map = map;
10070 goto RetryLookup;
10071 }
10072 vm_map_lock_read(entry->object.sub_map);
10073 *var_map = entry->object.sub_map;
10074 cow_sub_map_parent = map;
10075 /* reset base to map before cow object */
10076 /* this is the map which will accept */
10077 /* the new cow object */
10078 old_start = entry->vme_start;
10079 old_end = entry->vme_end;
10080 cow_parent_vaddr = vaddr;
10081 mapped_needs_copy = TRUE;
10082 } else {
10083 vm_map_lock_read(entry->object.sub_map);
10084 *var_map = entry->object.sub_map;
10085 if((cow_sub_map_parent != map) &&
10086 (*real_map != map))
10087 vm_map_unlock(map);
10088 }
10089 } else {
10090 vm_map_lock_read(entry->object.sub_map);
10091 *var_map = entry->object.sub_map;
10092 /* leave map locked if it is a target */
10093 /* cow sub_map above otherwise, just */
10094 /* follow the maps down to the object */
10095 /* here we unlock knowing we are not */
10096 /* revisiting the map. */
10097 if((*real_map != map) && (map != cow_sub_map_parent))
10098 vm_map_unlock_read(map);
10099 }
10100
10101 map = *var_map;
10102
10103 /* calculate the offset in the submap for vaddr */
10104 local_vaddr = (local_vaddr - entry->vme_start) + entry->offset;
10105
10106 RetrySubMap:
10107 if(!vm_map_lookup_entry(map, local_vaddr, &submap_entry)) {
10108 if((cow_sub_map_parent) && (cow_sub_map_parent != map)){
10109 vm_map_unlock(cow_sub_map_parent);
10110 }
10111 if((*real_map != map)
10112 && (*real_map != cow_sub_map_parent)) {
10113 vm_map_unlock(*real_map);
10114 }
10115 *real_map = map;
10116 return KERN_INVALID_ADDRESS;
10117 }
10118
10119 /* find the attenuated shadow of the underlying object */
10120 /* on our target map */
10121
10122 /* in english the submap object may extend beyond the */
10123 /* region mapped by the entry or, may only fill a portion */
10124 /* of it. For our purposes, we only care if the object */
10125 /* doesn't fill. In this case the area which will */
10126 /* ultimately be clipped in the top map will only need */
10127 /* to be as big as the portion of the underlying entry */
10128 /* which is mapped */
10129 start_delta = submap_entry->vme_start > entry->offset ?
10130 submap_entry->vme_start - entry->offset : 0;
10131
10132 end_delta =
10133 (entry->offset + start_delta + (old_end - old_start)) <=
10134 submap_entry->vme_end ?
10135 0 : (entry->offset +
10136 (old_end - old_start))
10137 - submap_entry->vme_end;
10138
10139 old_start += start_delta;
10140 old_end -= end_delta;
10141
10142 if(submap_entry->is_sub_map) {
10143 entry = submap_entry;
10144 vaddr = local_vaddr;
10145 goto submap_recurse;
10146 }
10147
10148 if(((fault_type & VM_PROT_WRITE) && cow_sub_map_parent)) {
10149
10150 vm_object_t sub_object, copy_object;
10151 vm_object_offset_t copy_offset;
10152 vm_map_offset_t local_start;
10153 vm_map_offset_t local_end;
10154 boolean_t copied_slowly = FALSE;
10155
10156 if (vm_map_lock_read_to_write(map)) {
10157 vm_map_lock_read(map);
10158 old_start -= start_delta;
10159 old_end += end_delta;
10160 goto RetrySubMap;
10161 }
10162
10163
10164 sub_object = submap_entry->object.vm_object;
10165 if (sub_object == VM_OBJECT_NULL) {
10166 sub_object =
10167 vm_object_allocate(
10168 (vm_map_size_t)
10169 (submap_entry->vme_end -
10170 submap_entry->vme_start));
10171 submap_entry->object.vm_object = sub_object;
10172 submap_entry->offset = 0;
10173 }
10174 local_start = local_vaddr -
10175 (cow_parent_vaddr - old_start);
10176 local_end = local_vaddr +
10177 (old_end - cow_parent_vaddr);
10178 vm_map_clip_start(map, submap_entry, local_start);
10179 vm_map_clip_end(map, submap_entry, local_end);
10180 if (submap_entry->is_sub_map) {
10181 /* unnesting was done when clipping */
10182 assert(!submap_entry->use_pmap);
10183 }
10184
10185 /* This is the COW case, lets connect */
10186 /* an entry in our space to the underlying */
10187 /* object in the submap, bypassing the */
10188 /* submap. */
10189
10190
10191 if(submap_entry->wired_count != 0 ||
10192 (sub_object->copy_strategy ==
10193 MEMORY_OBJECT_COPY_NONE)) {
10194 vm_object_lock(sub_object);
10195 vm_object_copy_slowly(sub_object,
10196 submap_entry->offset,
10197 (submap_entry->vme_end -
10198 submap_entry->vme_start),
10199 FALSE,
10200 &copy_object);
10201 copied_slowly = TRUE;
10202 } else {
10203
10204 /* set up shadow object */
10205 copy_object = sub_object;
10206 vm_object_reference(copy_object);
10207 sub_object->shadowed = TRUE;
10208 submap_entry->needs_copy = TRUE;
10209
10210 prot = submap_entry->protection & ~VM_PROT_WRITE;
10211
10212 if (override_nx(old_map, submap_entry->alias) && prot)
10213 prot |= VM_PROT_EXECUTE;
10214
10215 vm_object_pmap_protect(
10216 sub_object,
10217 submap_entry->offset,
10218 submap_entry->vme_end -
10219 submap_entry->vme_start,
10220 (submap_entry->is_shared
10221 || map->mapped_in_other_pmaps) ?
10222 PMAP_NULL : map->pmap,
10223 submap_entry->vme_start,
10224 prot);
10225 }
10226
10227 /*
10228 * Adjust the fault offset to the submap entry.
10229 */
10230 copy_offset = (local_vaddr -
10231 submap_entry->vme_start +
10232 submap_entry->offset);
10233
10234 /* This works diffently than the */
10235 /* normal submap case. We go back */
10236 /* to the parent of the cow map and*/
10237 /* clip out the target portion of */
10238 /* the sub_map, substituting the */
10239 /* new copy object, */
10240
10241 vm_map_unlock(map);
10242 local_start = old_start;
10243 local_end = old_end;
10244 map = cow_sub_map_parent;
10245 *var_map = cow_sub_map_parent;
10246 vaddr = cow_parent_vaddr;
10247 cow_sub_map_parent = NULL;
10248
10249 if(!vm_map_lookup_entry(map,
10250 vaddr, &entry)) {
10251 vm_object_deallocate(
10252 copy_object);
10253 vm_map_lock_write_to_read(map);
10254 return KERN_INVALID_ADDRESS;
10255 }
10256
10257 /* clip out the portion of space */
10258 /* mapped by the sub map which */
10259 /* corresponds to the underlying */
10260 /* object */
10261
10262 /*
10263 * Clip (and unnest) the smallest nested chunk
10264 * possible around the faulting address...
10265 */
10266 local_start = vaddr & ~(pmap_nesting_size_min - 1);
10267 local_end = local_start + pmap_nesting_size_min;
10268 /*
10269 * ... but don't go beyond the "old_start" to "old_end"
10270 * range, to avoid spanning over another VM region
10271 * with a possibly different VM object and/or offset.
10272 */
10273 if (local_start < old_start) {
10274 local_start = old_start;
10275 }
10276 if (local_end > old_end) {
10277 local_end = old_end;
10278 }
10279 /*
10280 * Adjust copy_offset to the start of the range.
10281 */
10282 copy_offset -= (vaddr - local_start);
10283
10284 vm_map_clip_start(map, entry, local_start);
10285 vm_map_clip_end(map, entry, local_end);
10286 if (entry->is_sub_map) {
10287 /* unnesting was done when clipping */
10288 assert(!entry->use_pmap);
10289 }
10290
10291 /* substitute copy object for */
10292 /* shared map entry */
10293 vm_map_deallocate(entry->object.sub_map);
10294 assert(!entry->iokit_acct);
10295 entry->is_sub_map = FALSE;
10296 entry->use_pmap = TRUE;
10297 entry->object.vm_object = copy_object;
10298
10299 /* propagate the submap entry's protections */
10300 entry->protection |= submap_entry->protection;
10301 entry->max_protection |= submap_entry->max_protection;
10302
10303 if(copied_slowly) {
10304 entry->offset = local_start - old_start;
10305 entry->needs_copy = FALSE;
10306 entry->is_shared = FALSE;
10307 } else {
10308 entry->offset = copy_offset;
10309 entry->needs_copy = TRUE;
10310 if(entry->inheritance == VM_INHERIT_SHARE)
10311 entry->inheritance = VM_INHERIT_COPY;
10312 if (map != old_map)
10313 entry->is_shared = TRUE;
10314 }
10315 if(entry->inheritance == VM_INHERIT_SHARE)
10316 entry->inheritance = VM_INHERIT_COPY;
10317
10318 vm_map_lock_write_to_read(map);
10319 } else {
10320 if((cow_sub_map_parent)
10321 && (cow_sub_map_parent != *real_map)
10322 && (cow_sub_map_parent != map)) {
10323 vm_map_unlock(cow_sub_map_parent);
10324 }
10325 entry = submap_entry;
10326 vaddr = local_vaddr;
10327 }
10328 }
10329
10330 /*
10331 * Check whether this task is allowed to have
10332 * this page.
10333 */
10334
10335 prot = entry->protection;
10336
10337 if (override_nx(old_map, entry->alias) && prot) {
10338 /*
10339 * HACK -- if not a stack, then allow execution
10340 */
10341 prot |= VM_PROT_EXECUTE;
10342 }
10343
10344 if (mask_protections) {
10345 fault_type &= prot;
10346 if (fault_type == VM_PROT_NONE) {
10347 goto protection_failure;
10348 }
10349 }
10350 if ((fault_type & (prot)) != fault_type) {
10351 protection_failure:
10352 if (*real_map != map) {
10353 vm_map_unlock(*real_map);
10354 }
10355 *real_map = map;
10356
10357 if ((fault_type & VM_PROT_EXECUTE) && prot)
10358 log_stack_execution_failure((addr64_t)vaddr, prot);
10359
10360 DTRACE_VM2(prot_fault, int, 1, (uint64_t *), NULL);
10361 return KERN_PROTECTION_FAILURE;
10362 }
10363
10364 /*
10365 * If this page is not pageable, we have to get
10366 * it for all possible accesses.
10367 */
10368
10369 *wired = (entry->wired_count != 0);
10370 if (*wired)
10371 fault_type = prot;
10372
10373 /*
10374 * If the entry was copy-on-write, we either ...
10375 */
10376
10377 if (entry->needs_copy) {
10378 /*
10379 * If we want to write the page, we may as well
10380 * handle that now since we've got the map locked.
10381 *
10382 * If we don't need to write the page, we just
10383 * demote the permissions allowed.
10384 */
10385
10386 if ((fault_type & VM_PROT_WRITE) || *wired || force_copy) {
10387 /*
10388 * Make a new object, and place it in the
10389 * object chain. Note that no new references
10390 * have appeared -- one just moved from the
10391 * map to the new object.
10392 */
10393
10394 if (vm_map_lock_read_to_write(map)) {
10395 vm_map_lock_read(map);
10396 goto RetryLookup;
10397 }
10398 vm_object_shadow(&entry->object.vm_object,
10399 &entry->offset,
10400 (vm_map_size_t) (entry->vme_end -
10401 entry->vme_start));
10402
10403 entry->object.vm_object->shadowed = TRUE;
10404 entry->needs_copy = FALSE;
10405 vm_map_lock_write_to_read(map);
10406 }
10407 else {
10408 /*
10409 * We're attempting to read a copy-on-write
10410 * page -- don't allow writes.
10411 */
10412
10413 prot &= (~VM_PROT_WRITE);
10414 }
10415 }
10416
10417 /*
10418 * Create an object if necessary.
10419 */
10420 if (entry->object.vm_object == VM_OBJECT_NULL) {
10421
10422 if (vm_map_lock_read_to_write(map)) {
10423 vm_map_lock_read(map);
10424 goto RetryLookup;
10425 }
10426
10427 entry->object.vm_object = vm_object_allocate(
10428 (vm_map_size_t)(entry->vme_end - entry->vme_start));
10429 entry->offset = 0;
10430 vm_map_lock_write_to_read(map);
10431 }
10432
10433 /*
10434 * Return the object/offset from this entry. If the entry
10435 * was copy-on-write or empty, it has been fixed up. Also
10436 * return the protection.
10437 */
10438
10439 *offset = (vaddr - entry->vme_start) + entry->offset;
10440 *object = entry->object.vm_object;
10441 *out_prot = prot;
10442
10443 if (fault_info) {
10444 fault_info->interruptible = THREAD_UNINT; /* for now... */
10445 /* ... the caller will change "interruptible" if needed */
10446 fault_info->cluster_size = 0;
10447 fault_info->user_tag = entry->alias;
10448 fault_info->pmap_options = 0;
10449 if (entry->iokit_acct ||
10450 (!entry->is_sub_map && !entry->use_pmap)) {
10451 fault_info->pmap_options |= PMAP_OPTIONS_ALT_ACCT;
10452 }
10453 fault_info->behavior = entry->behavior;
10454 fault_info->lo_offset = entry->offset;
10455 fault_info->hi_offset = (entry->vme_end - entry->vme_start) + entry->offset;
10456 fault_info->no_cache = entry->no_cache;
10457 fault_info->stealth = FALSE;
10458 fault_info->io_sync = FALSE;
10459 fault_info->cs_bypass = (entry->used_for_jit)? TRUE : FALSE;
10460 fault_info->mark_zf_absent = FALSE;
10461 fault_info->batch_pmap_op = FALSE;
10462 }
10463
10464 /*
10465 * Lock the object to prevent it from disappearing
10466 */
10467 if (object_lock_type == OBJECT_LOCK_EXCLUSIVE)
10468 vm_object_lock(*object);
10469 else
10470 vm_object_lock_shared(*object);
10471
10472 /*
10473 * Save the version number
10474 */
10475
10476 out_version->main_timestamp = map->timestamp;
10477
10478 return KERN_SUCCESS;
10479}
10480
10481
10482/*
10483 * vm_map_verify:
10484 *
10485 * Verifies that the map in question has not changed
10486 * since the given version. If successful, the map
10487 * will not change until vm_map_verify_done() is called.
10488 */
10489boolean_t
10490vm_map_verify(
10491 register vm_map_t map,
10492 register vm_map_version_t *version) /* REF */
10493{
10494 boolean_t result;
10495
10496 vm_map_lock_read(map);
10497 result = (map->timestamp == version->main_timestamp);
10498
10499 if (!result)
10500 vm_map_unlock_read(map);
10501
10502 return(result);
10503}
10504
10505/*
10506 * vm_map_verify_done:
10507 *
10508 * Releases locks acquired by a vm_map_verify.
10509 *
10510 * This is now a macro in vm/vm_map.h. It does a
10511 * vm_map_unlock_read on the map.
10512 */
10513
10514
10515/*
10516 * TEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARYTEMPORARY
10517 * Goes away after regular vm_region_recurse function migrates to
10518 * 64 bits
10519 * vm_region_recurse: A form of vm_region which follows the
10520 * submaps in a target map
10521 *
10522 */
10523
10524kern_return_t
10525vm_map_region_recurse_64(
10526 vm_map_t map,
10527 vm_map_offset_t *address, /* IN/OUT */
10528 vm_map_size_t *size, /* OUT */
10529 natural_t *nesting_depth, /* IN/OUT */
10530 vm_region_submap_info_64_t submap_info, /* IN/OUT */
10531 mach_msg_type_number_t *count) /* IN/OUT */
10532{
10533 mach_msg_type_number_t original_count;
10534 vm_region_extended_info_data_t extended;
10535 vm_map_entry_t tmp_entry;
10536 vm_map_offset_t user_address;
10537 unsigned int user_max_depth;
10538
10539 /*
10540 * "curr_entry" is the VM map entry preceding or including the
10541 * address we're looking for.
10542 * "curr_map" is the map or sub-map containing "curr_entry".
10543 * "curr_address" is the equivalent of the top map's "user_address"
10544 * in the current map.
10545 * "curr_offset" is the cumulated offset of "curr_map" in the
10546 * target task's address space.
10547 * "curr_depth" is the depth of "curr_map" in the chain of
10548 * sub-maps.
10549 *
10550 * "curr_max_below" and "curr_max_above" limit the range (around
10551 * "curr_address") we should take into account in the current (sub)map.
10552 * They limit the range to what's visible through the map entries
10553 * we've traversed from the top map to the current map.
10554
10555 */
10556 vm_map_entry_t curr_entry;
10557 vm_map_address_t curr_address;
10558 vm_map_offset_t curr_offset;
10559 vm_map_t curr_map;
10560 unsigned int curr_depth;
10561 vm_map_offset_t curr_max_below, curr_max_above;
10562 vm_map_offset_t curr_skip;
10563
10564 /*
10565 * "next_" is the same as "curr_" but for the VM region immediately
10566 * after the address we're looking for. We need to keep track of this
10567 * too because we want to return info about that region if the
10568 * address we're looking for is not mapped.
10569 */
10570 vm_map_entry_t next_entry;
10571 vm_map_offset_t next_offset;
10572 vm_map_offset_t next_address;
10573 vm_map_t next_map;
10574 unsigned int next_depth;
10575 vm_map_offset_t next_max_below, next_max_above;
10576 vm_map_offset_t next_skip;
10577
10578 boolean_t look_for_pages;
10579 vm_region_submap_short_info_64_t short_info;
10580
10581 if (map == VM_MAP_NULL) {
10582 /* no address space to work on */
10583 return KERN_INVALID_ARGUMENT;
10584 }
10585
10586
10587 if (*count < VM_REGION_SUBMAP_SHORT_INFO_COUNT_64) {
10588 /*
10589 * "info" structure is not big enough and
10590 * would overflow
10591 */
10592 return KERN_INVALID_ARGUMENT;
10593 }
10594
10595 original_count = *count;
10596
10597 if (original_count < VM_REGION_SUBMAP_INFO_V0_COUNT_64) {
10598 *count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
10599 look_for_pages = FALSE;
10600 short_info = (vm_region_submap_short_info_64_t) submap_info;
10601 submap_info = NULL;
10602 } else {
10603 look_for_pages = TRUE;
10604 *count = VM_REGION_SUBMAP_INFO_V0_COUNT_64;
10605 short_info = NULL;
10606
10607 if (original_count >= VM_REGION_SUBMAP_INFO_V1_COUNT_64) {
10608 *count = VM_REGION_SUBMAP_INFO_V1_COUNT_64;
10609 }
10610 }
10611
10612 user_address = *address;
10613 user_max_depth = *nesting_depth;
10614
10615 curr_entry = NULL;
10616 curr_map = map;
10617 curr_address = user_address;
10618 curr_offset = 0;
10619 curr_skip = 0;
10620 curr_depth = 0;
10621 curr_max_above = ((vm_map_offset_t) -1) - curr_address;
10622 curr_max_below = curr_address;
10623
10624 next_entry = NULL;
10625 next_map = NULL;
10626 next_address = 0;
10627 next_offset = 0;
10628 next_skip = 0;
10629 next_depth = 0;
10630 next_max_above = (vm_map_offset_t) -1;
10631 next_max_below = (vm_map_offset_t) -1;
10632
10633 if (not_in_kdp) {
10634 vm_map_lock_read(curr_map);
10635 }
10636
10637 for (;;) {
10638 if (vm_map_lookup_entry(curr_map,
10639 curr_address,
10640 &tmp_entry)) {
10641 /* tmp_entry contains the address we're looking for */
10642 curr_entry = tmp_entry;
10643 } else {
10644 vm_map_offset_t skip;
10645 /*
10646 * The address is not mapped. "tmp_entry" is the
10647 * map entry preceding the address. We want the next
10648 * one, if it exists.
10649 */
10650 curr_entry = tmp_entry->vme_next;
10651
10652 if (curr_entry == vm_map_to_entry(curr_map) ||
10653 (curr_entry->vme_start >=
10654 curr_address + curr_max_above)) {
10655 /* no next entry at this level: stop looking */
10656 if (not_in_kdp) {
10657 vm_map_unlock_read(curr_map);
10658 }
10659 curr_entry = NULL;
10660 curr_map = NULL;
10661 curr_offset = 0;
10662 curr_depth = 0;
10663 curr_max_above = 0;
10664 curr_max_below = 0;
10665 break;
10666 }
10667
10668 /* adjust current address and offset */
10669 skip = curr_entry->vme_start - curr_address;
10670 curr_address = curr_entry->vme_start;
10671 curr_skip = skip;
10672 curr_offset += skip;
10673 curr_max_above -= skip;
10674 curr_max_below = 0;
10675 }
10676
10677 /*
10678 * Is the next entry at this level closer to the address (or
10679 * deeper in the submap chain) than the one we had
10680 * so far ?
10681 */
10682 tmp_entry = curr_entry->vme_next;
10683 if (tmp_entry == vm_map_to_entry(curr_map)) {
10684 /* no next entry at this level */
10685 } else if (tmp_entry->vme_start >=
10686 curr_address + curr_max_above) {
10687 /*
10688 * tmp_entry is beyond the scope of what we mapped of
10689 * this submap in the upper level: ignore it.
10690 */
10691 } else if ((next_entry == NULL) ||
10692 (tmp_entry->vme_start + curr_offset <=
10693 next_entry->vme_start + next_offset)) {
10694 /*
10695 * We didn't have a "next_entry" or this one is
10696 * closer to the address we're looking for:
10697 * use this "tmp_entry" as the new "next_entry".
10698 */
10699 if (next_entry != NULL) {
10700 /* unlock the last "next_map" */
10701 if (next_map != curr_map && not_in_kdp) {
10702 vm_map_unlock_read(next_map);
10703 }
10704 }
10705 next_entry = tmp_entry;
10706 next_map = curr_map;
10707 next_depth = curr_depth;
10708 next_address = next_entry->vme_start;
10709 next_skip = curr_skip;
10710 next_offset = curr_offset;
10711 next_offset += (next_address - curr_address);
10712 next_max_above = MIN(next_max_above, curr_max_above);
10713 next_max_above = MIN(next_max_above,
10714 next_entry->vme_end - next_address);
10715 next_max_below = MIN(next_max_below, curr_max_below);
10716 next_max_below = MIN(next_max_below,
10717 next_address - next_entry->vme_start);
10718 }
10719
10720 /*
10721 * "curr_max_{above,below}" allow us to keep track of the
10722 * portion of the submap that is actually mapped at this level:
10723 * the rest of that submap is irrelevant to us, since it's not
10724 * mapped here.
10725 * The relevant portion of the map starts at
10726 * "curr_entry->offset" up to the size of "curr_entry".
10727 */
10728 curr_max_above = MIN(curr_max_above,
10729 curr_entry->vme_end - curr_address);
10730 curr_max_below = MIN(curr_max_below,
10731 curr_address - curr_entry->vme_start);
10732
10733 if (!curr_entry->is_sub_map ||
10734 curr_depth >= user_max_depth) {
10735 /*
10736 * We hit a leaf map or we reached the maximum depth
10737 * we could, so stop looking. Keep the current map
10738 * locked.
10739 */
10740 break;
10741 }
10742
10743 /*
10744 * Get down to the next submap level.
10745 */
10746
10747 /*
10748 * Lock the next level and unlock the current level,
10749 * unless we need to keep it locked to access the "next_entry"
10750 * later.
10751 */
10752 if (not_in_kdp) {
10753 vm_map_lock_read(curr_entry->object.sub_map);
10754 }
10755 if (curr_map == next_map) {
10756 /* keep "next_map" locked in case we need it */
10757 } else {
10758 /* release this map */
10759 if (not_in_kdp)
10760 vm_map_unlock_read(curr_map);
10761 }
10762
10763 /*
10764 * Adjust the offset. "curr_entry" maps the submap
10765 * at relative address "curr_entry->vme_start" in the
10766 * curr_map but skips the first "curr_entry->offset"
10767 * bytes of the submap.
10768 * "curr_offset" always represents the offset of a virtual
10769 * address in the curr_map relative to the absolute address
10770 * space (i.e. the top-level VM map).
10771 */
10772 curr_offset +=
10773 (curr_entry->offset - curr_entry->vme_start);
10774 curr_address = user_address + curr_offset;
10775 /* switch to the submap */
10776 curr_map = curr_entry->object.sub_map;
10777 curr_depth++;
10778 curr_entry = NULL;
10779 }
10780
10781 if (curr_entry == NULL) {
10782 /* no VM region contains the address... */
10783 if (next_entry == NULL) {
10784 /* ... and no VM region follows it either */
10785 return KERN_INVALID_ADDRESS;
10786 }
10787 /* ... gather info about the next VM region */
10788 curr_entry = next_entry;
10789 curr_map = next_map; /* still locked ... */
10790 curr_address = next_address;
10791 curr_skip = next_skip;
10792 curr_offset = next_offset;
10793 curr_depth = next_depth;
10794 curr_max_above = next_max_above;
10795 curr_max_below = next_max_below;
10796 if (curr_map == map) {
10797 user_address = curr_address;
10798 }
10799 } else {
10800 /* we won't need "next_entry" after all */
10801 if (next_entry != NULL) {
10802 /* release "next_map" */
10803 if (next_map != curr_map && not_in_kdp) {
10804 vm_map_unlock_read(next_map);
10805 }
10806 }
10807 }
10808 next_entry = NULL;
10809 next_map = NULL;
10810 next_offset = 0;
10811 next_skip = 0;
10812 next_depth = 0;
10813 next_max_below = -1;
10814 next_max_above = -1;
10815
10816 *nesting_depth = curr_depth;
10817 *size = curr_max_above + curr_max_below;
10818 *address = user_address + curr_skip - curr_max_below;
10819
10820// LP64todo: all the current tools are 32bit, obviously never worked for 64b
10821// so probably should be a real 32b ID vs. ptr.
10822// Current users just check for equality
10823#define INFO_MAKE_OBJECT_ID(p) ((uint32_t)(uintptr_t)VM_KERNEL_ADDRPERM(p))
10824
10825 if (look_for_pages) {
10826 submap_info->user_tag = curr_entry->alias;
10827 submap_info->offset = curr_entry->offset;
10828 submap_info->protection = curr_entry->protection;
10829 submap_info->inheritance = curr_entry->inheritance;
10830 submap_info->max_protection = curr_entry->max_protection;
10831 submap_info->behavior = curr_entry->behavior;
10832 submap_info->user_wired_count = curr_entry->user_wired_count;
10833 submap_info->is_submap = curr_entry->is_sub_map;
10834 submap_info->object_id = INFO_MAKE_OBJECT_ID(curr_entry->object.vm_object);
10835 } else {
10836 short_info->user_tag = curr_entry->alias;
10837 short_info->offset = curr_entry->offset;
10838 short_info->protection = curr_entry->protection;
10839 short_info->inheritance = curr_entry->inheritance;
10840 short_info->max_protection = curr_entry->max_protection;
10841 short_info->behavior = curr_entry->behavior;
10842 short_info->user_wired_count = curr_entry->user_wired_count;
10843 short_info->is_submap = curr_entry->is_sub_map;
10844 short_info->object_id = INFO_MAKE_OBJECT_ID(curr_entry->object.vm_object);
10845 }
10846
10847 extended.pages_resident = 0;
10848 extended.pages_swapped_out = 0;
10849 extended.pages_shared_now_private = 0;
10850 extended.pages_dirtied = 0;
10851 extended.pages_reusable = 0;
10852 extended.external_pager = 0;
10853 extended.shadow_depth = 0;
10854
10855 if (not_in_kdp) {
10856 if (!curr_entry->is_sub_map) {
10857 vm_map_offset_t range_start, range_end;
10858 range_start = MAX((curr_address - curr_max_below),
10859 curr_entry->vme_start);
10860 range_end = MIN((curr_address + curr_max_above),
10861 curr_entry->vme_end);
10862 vm_map_region_walk(curr_map,
10863 range_start,
10864 curr_entry,
10865 (curr_entry->offset +
10866 (range_start -
10867 curr_entry->vme_start)),
10868 range_end - range_start,
10869 &extended,
10870 look_for_pages, VM_REGION_EXTENDED_INFO_COUNT);
10871 if (extended.external_pager &&
10872 extended.ref_count == 2 &&
10873 extended.share_mode == SM_SHARED) {
10874 extended.share_mode = SM_PRIVATE;
10875 }
10876 } else {
10877 if (curr_entry->use_pmap) {
10878 extended.share_mode = SM_TRUESHARED;
10879 } else {
10880 extended.share_mode = SM_PRIVATE;
10881 }
10882 extended.ref_count =
10883 curr_entry->object.sub_map->ref_count;
10884 }
10885 }
10886
10887 if (look_for_pages) {
10888 submap_info->pages_resident = extended.pages_resident;
10889 submap_info->pages_swapped_out = extended.pages_swapped_out;
10890 submap_info->pages_shared_now_private =
10891 extended.pages_shared_now_private;
10892 submap_info->pages_dirtied = extended.pages_dirtied;
10893 submap_info->external_pager = extended.external_pager;
10894 submap_info->shadow_depth = extended.shadow_depth;
10895 submap_info->share_mode = extended.share_mode;
10896 submap_info->ref_count = extended.ref_count;
10897
10898 if (original_count >= VM_REGION_SUBMAP_INFO_V1_COUNT_64) {
10899 submap_info->pages_reusable = extended.pages_reusable;
10900 }
10901 } else {
10902 short_info->external_pager = extended.external_pager;
10903 short_info->shadow_depth = extended.shadow_depth;
10904 short_info->share_mode = extended.share_mode;
10905 short_info->ref_count = extended.ref_count;
10906 }
10907
10908 if (not_in_kdp) {
10909 vm_map_unlock_read(curr_map);
10910 }
10911
10912 return KERN_SUCCESS;
10913}
10914
10915/*
10916 * vm_region:
10917 *
10918 * User call to obtain information about a region in
10919 * a task's address map. Currently, only one flavor is
10920 * supported.
10921 *
10922 * XXX The reserved and behavior fields cannot be filled
10923 * in until the vm merge from the IK is completed, and
10924 * vm_reserve is implemented.
10925 */
10926
10927kern_return_t
10928vm_map_region(
10929 vm_map_t map,
10930 vm_map_offset_t *address, /* IN/OUT */
10931 vm_map_size_t *size, /* OUT */
10932 vm_region_flavor_t flavor, /* IN */
10933 vm_region_info_t info, /* OUT */
10934 mach_msg_type_number_t *count, /* IN/OUT */
10935 mach_port_t *object_name) /* OUT */
10936{
10937 vm_map_entry_t tmp_entry;
10938 vm_map_entry_t entry;
10939 vm_map_offset_t start;
10940
10941 if (map == VM_MAP_NULL)
10942 return(KERN_INVALID_ARGUMENT);
10943
10944 switch (flavor) {
10945
10946 case VM_REGION_BASIC_INFO:
10947 /* legacy for old 32-bit objects info */
10948 {
10949 vm_region_basic_info_t basic;
10950
10951 if (*count < VM_REGION_BASIC_INFO_COUNT)
10952 return(KERN_INVALID_ARGUMENT);
10953
10954 basic = (vm_region_basic_info_t) info;
10955 *count = VM_REGION_BASIC_INFO_COUNT;
10956
10957 vm_map_lock_read(map);
10958
10959 start = *address;
10960 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
10961 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
10962 vm_map_unlock_read(map);
10963 return(KERN_INVALID_ADDRESS);
10964 }
10965 } else {
10966 entry = tmp_entry;
10967 }
10968
10969 start = entry->vme_start;
10970
10971 basic->offset = (uint32_t)entry->offset;
10972 basic->protection = entry->protection;
10973 basic->inheritance = entry->inheritance;
10974 basic->max_protection = entry->max_protection;
10975 basic->behavior = entry->behavior;
10976 basic->user_wired_count = entry->user_wired_count;
10977 basic->reserved = entry->is_sub_map;
10978 *address = start;
10979 *size = (entry->vme_end - start);
10980
10981 if (object_name) *object_name = IP_NULL;
10982 if (entry->is_sub_map) {
10983 basic->shared = FALSE;
10984 } else {
10985 basic->shared = entry->is_shared;
10986 }
10987
10988 vm_map_unlock_read(map);
10989 return(KERN_SUCCESS);
10990 }
10991
10992 case VM_REGION_BASIC_INFO_64:
10993 {
10994 vm_region_basic_info_64_t basic;
10995
10996 if (*count < VM_REGION_BASIC_INFO_COUNT_64)
10997 return(KERN_INVALID_ARGUMENT);
10998
10999 basic = (vm_region_basic_info_64_t) info;
11000 *count = VM_REGION_BASIC_INFO_COUNT_64;
11001
11002 vm_map_lock_read(map);
11003
11004 start = *address;
11005 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
11006 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
11007 vm_map_unlock_read(map);
11008 return(KERN_INVALID_ADDRESS);
11009 }
11010 } else {
11011 entry = tmp_entry;
11012 }
11013
11014 start = entry->vme_start;
11015
11016 basic->offset = entry->offset;
11017 basic->protection = entry->protection;
11018 basic->inheritance = entry->inheritance;
11019 basic->max_protection = entry->max_protection;
11020 basic->behavior = entry->behavior;
11021 basic->user_wired_count = entry->user_wired_count;
11022 basic->reserved = entry->is_sub_map;
11023 *address = start;
11024 *size = (entry->vme_end - start);
11025
11026 if (object_name) *object_name = IP_NULL;
11027 if (entry->is_sub_map) {
11028 basic->shared = FALSE;
11029 } else {
11030 basic->shared = entry->is_shared;
11031 }
11032
11033 vm_map_unlock_read(map);
11034 return(KERN_SUCCESS);
11035 }
11036 case VM_REGION_EXTENDED_INFO:
11037 if (*count < VM_REGION_EXTENDED_INFO_COUNT)
11038 return(KERN_INVALID_ARGUMENT);
11039 /*fallthru*/
11040 case VM_REGION_EXTENDED_INFO__legacy:
11041 if (*count < VM_REGION_EXTENDED_INFO_COUNT__legacy)
11042 return KERN_INVALID_ARGUMENT;
11043
11044 {
11045 vm_region_extended_info_t extended;
11046 mach_msg_type_number_t original_count;
11047
11048 extended = (vm_region_extended_info_t) info;
11049
11050 vm_map_lock_read(map);
11051
11052 start = *address;
11053 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
11054 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
11055 vm_map_unlock_read(map);
11056 return(KERN_INVALID_ADDRESS);
11057 }
11058 } else {
11059 entry = tmp_entry;
11060 }
11061 start = entry->vme_start;
11062
11063 extended->protection = entry->protection;
11064 extended->user_tag = entry->alias;
11065 extended->pages_resident = 0;
11066 extended->pages_swapped_out = 0;
11067 extended->pages_shared_now_private = 0;
11068 extended->pages_dirtied = 0;
11069 extended->external_pager = 0;
11070 extended->shadow_depth = 0;
11071
11072 original_count = *count;
11073 if (flavor == VM_REGION_EXTENDED_INFO__legacy) {
11074 *count = VM_REGION_EXTENDED_INFO_COUNT__legacy;
11075 } else {
11076 extended->pages_reusable = 0;
11077 *count = VM_REGION_EXTENDED_INFO_COUNT;
11078 }
11079
11080 vm_map_region_walk(map, start, entry, entry->offset, entry->vme_end - start, extended, TRUE, *count);
11081
11082 if (extended->external_pager && extended->ref_count == 2 && extended->share_mode == SM_SHARED)
11083 extended->share_mode = SM_PRIVATE;
11084
11085 if (object_name)
11086 *object_name = IP_NULL;
11087 *address = start;
11088 *size = (entry->vme_end - start);
11089
11090 vm_map_unlock_read(map);
11091 return(KERN_SUCCESS);
11092 }
11093 case VM_REGION_TOP_INFO:
11094 {
11095 vm_region_top_info_t top;
11096
11097 if (*count < VM_REGION_TOP_INFO_COUNT)
11098 return(KERN_INVALID_ARGUMENT);
11099
11100 top = (vm_region_top_info_t) info;
11101 *count = VM_REGION_TOP_INFO_COUNT;
11102
11103 vm_map_lock_read(map);
11104
11105 start = *address;
11106 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
11107 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
11108 vm_map_unlock_read(map);
11109 return(KERN_INVALID_ADDRESS);
11110 }
11111 } else {
11112 entry = tmp_entry;
11113
11114 }
11115 start = entry->vme_start;
11116
11117 top->private_pages_resident = 0;
11118 top->shared_pages_resident = 0;
11119
11120 vm_map_region_top_walk(entry, top);
11121
11122 if (object_name)
11123 *object_name = IP_NULL;
11124 *address = start;
11125 *size = (entry->vme_end - start);
11126
11127 vm_map_unlock_read(map);
11128 return(KERN_SUCCESS);
11129 }
11130 default:
11131 return(KERN_INVALID_ARGUMENT);
11132 }
11133}
11134
11135#define OBJ_RESIDENT_COUNT(obj, entry_size) \
11136 MIN((entry_size), \
11137 ((obj)->all_reusable ? \
11138 (obj)->wired_page_count : \
11139 (obj)->resident_page_count - (obj)->reusable_page_count))
11140
11141void
11142vm_map_region_top_walk(
11143 vm_map_entry_t entry,
11144 vm_region_top_info_t top)
11145{
11146
11147 if (entry->object.vm_object == 0 || entry->is_sub_map) {
11148 top->share_mode = SM_EMPTY;
11149 top->ref_count = 0;
11150 top->obj_id = 0;
11151 return;
11152 }
11153
11154 {
11155 struct vm_object *obj, *tmp_obj;
11156 int ref_count;
11157 uint32_t entry_size;
11158
11159 entry_size = (uint32_t) ((entry->vme_end - entry->vme_start) / PAGE_SIZE_64);
11160
11161 obj = entry->object.vm_object;
11162
11163 vm_object_lock(obj);
11164
11165 if ((ref_count = obj->ref_count) > 1 && obj->paging_in_progress)
11166 ref_count--;
11167
11168 assert(obj->reusable_page_count <= obj->resident_page_count);
11169 if (obj->shadow) {
11170 if (ref_count == 1)
11171 top->private_pages_resident =
11172 OBJ_RESIDENT_COUNT(obj, entry_size);
11173 else
11174 top->shared_pages_resident =
11175 OBJ_RESIDENT_COUNT(obj, entry_size);
11176 top->ref_count = ref_count;
11177 top->share_mode = SM_COW;
11178
11179 while ((tmp_obj = obj->shadow)) {
11180 vm_object_lock(tmp_obj);
11181 vm_object_unlock(obj);
11182 obj = tmp_obj;
11183
11184 if ((ref_count = obj->ref_count) > 1 && obj->paging_in_progress)
11185 ref_count--;
11186
11187 assert(obj->reusable_page_count <= obj->resident_page_count);
11188 top->shared_pages_resident +=
11189 OBJ_RESIDENT_COUNT(obj, entry_size);
11190 top->ref_count += ref_count - 1;
11191 }
11192 } else {
11193 if (entry->superpage_size) {
11194 top->share_mode = SM_LARGE_PAGE;
11195 top->shared_pages_resident = 0;
11196 top->private_pages_resident = entry_size;
11197 } else if (entry->needs_copy) {
11198 top->share_mode = SM_COW;
11199 top->shared_pages_resident =
11200 OBJ_RESIDENT_COUNT(obj, entry_size);
11201 } else {
11202 if (ref_count == 1 ||
11203 (ref_count == 2 && !(obj->pager_trusted) && !(obj->internal))) {
11204 top->share_mode = SM_PRIVATE;
11205 top->private_pages_resident =
11206 OBJ_RESIDENT_COUNT(obj,
11207 entry_size);
11208 } else {
11209 top->share_mode = SM_SHARED;
11210 top->shared_pages_resident =
11211 OBJ_RESIDENT_COUNT(obj,
11212 entry_size);
11213 }
11214 }
11215 top->ref_count = ref_count;
11216 }
11217 /* XXX K64: obj_id will be truncated */
11218 top->obj_id = (unsigned int) (uintptr_t)VM_KERNEL_ADDRPERM(obj);
11219
11220 vm_object_unlock(obj);
11221 }
11222}
11223
11224void
11225vm_map_region_walk(
11226 vm_map_t map,
11227 vm_map_offset_t va,
11228 vm_map_entry_t entry,
11229 vm_object_offset_t offset,
11230 vm_object_size_t range,
11231 vm_region_extended_info_t extended,
11232 boolean_t look_for_pages,
11233 mach_msg_type_number_t count)
11234{
11235 register struct vm_object *obj, *tmp_obj;
11236 register vm_map_offset_t last_offset;
11237 register int i;
11238 register int ref_count;
11239 struct vm_object *shadow_object;
11240 int shadow_depth;
11241
11242 if ((entry->object.vm_object == 0) ||
11243 (entry->is_sub_map) ||
11244 (entry->object.vm_object->phys_contiguous &&
11245 !entry->superpage_size)) {
11246 extended->share_mode = SM_EMPTY;
11247 extended->ref_count = 0;
11248 return;
11249 }
11250
11251 if (entry->superpage_size) {
11252 extended->shadow_depth = 0;
11253 extended->share_mode = SM_LARGE_PAGE;
11254 extended->ref_count = 1;
11255 extended->external_pager = 0;
11256 extended->pages_resident = (unsigned int)(range >> PAGE_SHIFT);
11257 extended->shadow_depth = 0;
11258 return;
11259 }
11260
11261 {
11262 obj = entry->object.vm_object;
11263
11264 vm_object_lock(obj);
11265
11266 if ((ref_count = obj->ref_count) > 1 && obj->paging_in_progress)
11267 ref_count--;
11268
11269 if (look_for_pages) {
11270 for (last_offset = offset + range;
11271 offset < last_offset;
11272 offset += PAGE_SIZE_64, va += PAGE_SIZE) {
11273 vm_map_region_look_for_page(map, va, obj,
11274 offset, ref_count,
11275 0, extended, count);
11276 }
11277 } else {
11278 shadow_object = obj->shadow;
11279 shadow_depth = 0;
11280
11281 if ( !(obj->pager_trusted) && !(obj->internal))
11282 extended->external_pager = 1;
11283
11284 if (shadow_object != VM_OBJECT_NULL) {
11285 vm_object_lock(shadow_object);
11286 for (;
11287 shadow_object != VM_OBJECT_NULL;
11288 shadow_depth++) {
11289 vm_object_t next_shadow;
11290
11291 if ( !(shadow_object->pager_trusted) &&
11292 !(shadow_object->internal))
11293 extended->external_pager = 1;
11294
11295 next_shadow = shadow_object->shadow;
11296 if (next_shadow) {
11297 vm_object_lock(next_shadow);
11298 }
11299 vm_object_unlock(shadow_object);
11300 shadow_object = next_shadow;
11301 }
11302 }
11303 extended->shadow_depth = shadow_depth;
11304 }
11305
11306 if (extended->shadow_depth || entry->needs_copy)
11307 extended->share_mode = SM_COW;
11308 else {
11309 if (ref_count == 1)
11310 extended->share_mode = SM_PRIVATE;
11311 else {
11312 if (obj->true_share)
11313 extended->share_mode = SM_TRUESHARED;
11314 else
11315 extended->share_mode = SM_SHARED;
11316 }
11317 }
11318 extended->ref_count = ref_count - extended->shadow_depth;
11319
11320 for (i = 0; i < extended->shadow_depth; i++) {
11321 if ((tmp_obj = obj->shadow) == 0)
11322 break;
11323 vm_object_lock(tmp_obj);
11324 vm_object_unlock(obj);
11325
11326 if ((ref_count = tmp_obj->ref_count) > 1 && tmp_obj->paging_in_progress)
11327 ref_count--;
11328
11329 extended->ref_count += ref_count;
11330 obj = tmp_obj;
11331 }
11332 vm_object_unlock(obj);
11333
11334 if (extended->share_mode == SM_SHARED) {
11335 register vm_map_entry_t cur;
11336 register vm_map_entry_t last;
11337 int my_refs;
11338
11339 obj = entry->object.vm_object;
11340 last = vm_map_to_entry(map);
11341 my_refs = 0;
11342
11343 if ((ref_count = obj->ref_count) > 1 && obj->paging_in_progress)
11344 ref_count--;
11345 for (cur = vm_map_first_entry(map); cur != last; cur = cur->vme_next)
11346 my_refs += vm_map_region_count_obj_refs(cur, obj);
11347
11348 if (my_refs == ref_count)
11349 extended->share_mode = SM_PRIVATE_ALIASED;
11350 else if (my_refs > 1)
11351 extended->share_mode = SM_SHARED_ALIASED;
11352 }
11353 }
11354}
11355
11356
11357/* object is locked on entry and locked on return */
11358
11359
11360static void
11361vm_map_region_look_for_page(
11362 __unused vm_map_t map,
11363 __unused vm_map_offset_t va,
11364 vm_object_t object,
11365 vm_object_offset_t offset,
11366 int max_refcnt,
11367 int depth,
11368 vm_region_extended_info_t extended,
11369 mach_msg_type_number_t count)
11370{
11371 register vm_page_t p;
11372 register vm_object_t shadow;
11373 register int ref_count;
11374 vm_object_t caller_object;
11375 kern_return_t kr;
11376 shadow = object->shadow;
11377 caller_object = object;
11378
11379
11380 while (TRUE) {
11381
11382 if ( !(object->pager_trusted) && !(object->internal))
11383 extended->external_pager = 1;
11384
11385 if ((p = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
11386 if (shadow && (max_refcnt == 1))
11387 extended->pages_shared_now_private++;
11388
11389 if (!p->fictitious &&
11390 (p->dirty || pmap_is_modified(p->phys_page)))
11391 extended->pages_dirtied++;
11392 else if (count >= VM_REGION_EXTENDED_INFO_COUNT) {
11393 if (p->reusable || p->object->all_reusable) {
11394 extended->pages_reusable++;
11395 }
11396 }
11397
11398 extended->pages_resident++;
11399
11400 if(object != caller_object)
11401 vm_object_unlock(object);
11402
11403 return;
11404 }
11405#if MACH_PAGEMAP
11406 if (object->existence_map) {
11407 if (vm_external_state_get(object->existence_map, offset) == VM_EXTERNAL_STATE_EXISTS) {
11408
11409 extended->pages_swapped_out++;
11410
11411 if(object != caller_object)
11412 vm_object_unlock(object);
11413
11414 return;
11415 }
11416 } else
11417#endif /* MACH_PAGEMAP */
11418 if (object->internal &&
11419 object->alive &&
11420 !object->terminating &&
11421 object->pager_ready) {
11422
11423 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
11424 if (VM_COMPRESSOR_PAGER_STATE_GET(object,
11425 offset)
11426 == VM_EXTERNAL_STATE_EXISTS) {
11427 /* the pager has that page */
11428 extended->pages_swapped_out++;
11429 if (object != caller_object)
11430 vm_object_unlock(object);
11431 return;
11432 }
11433 } else {
11434 memory_object_t pager;
11435
11436 vm_object_paging_begin(object);
11437 pager = object->pager;
11438 vm_object_unlock(object);
11439
11440 kr = memory_object_data_request(
11441 pager,
11442 offset + object->paging_offset,
11443 0, /* just poke the pager */
11444 VM_PROT_READ,
11445 NULL);
11446
11447 vm_object_lock(object);
11448 vm_object_paging_end(object);
11449
11450 if (kr == KERN_SUCCESS) {
11451 /* the pager has that page */
11452 extended->pages_swapped_out++;
11453 if (object != caller_object)
11454 vm_object_unlock(object);
11455 return;
11456 }
11457 }
11458 }
11459
11460 if (shadow) {
11461 vm_object_lock(shadow);
11462
11463 if ((ref_count = shadow->ref_count) > 1 && shadow->paging_in_progress)
11464 ref_count--;
11465
11466 if (++depth > extended->shadow_depth)
11467 extended->shadow_depth = depth;
11468
11469 if (ref_count > max_refcnt)
11470 max_refcnt = ref_count;
11471
11472 if(object != caller_object)
11473 vm_object_unlock(object);
11474
11475 offset = offset + object->vo_shadow_offset;
11476 object = shadow;
11477 shadow = object->shadow;
11478 continue;
11479 }
11480 if(object != caller_object)
11481 vm_object_unlock(object);
11482 break;
11483 }
11484}
11485
11486static int
11487vm_map_region_count_obj_refs(
11488 vm_map_entry_t entry,
11489 vm_object_t object)
11490{
11491 register int ref_count;
11492 register vm_object_t chk_obj;
11493 register vm_object_t tmp_obj;
11494
11495 if (entry->object.vm_object == 0)
11496 return(0);
11497
11498 if (entry->is_sub_map)
11499 return(0);
11500 else {
11501 ref_count = 0;
11502
11503 chk_obj = entry->object.vm_object;
11504 vm_object_lock(chk_obj);
11505
11506 while (chk_obj) {
11507 if (chk_obj == object)
11508 ref_count++;
11509 tmp_obj = chk_obj->shadow;
11510 if (tmp_obj)
11511 vm_object_lock(tmp_obj);
11512 vm_object_unlock(chk_obj);
11513
11514 chk_obj = tmp_obj;
11515 }
11516 }
11517 return(ref_count);
11518}
11519
11520
11521/*
11522 * Routine: vm_map_simplify
11523 *
11524 * Description:
11525 * Attempt to simplify the map representation in
11526 * the vicinity of the given starting address.
11527 * Note:
11528 * This routine is intended primarily to keep the
11529 * kernel maps more compact -- they generally don't
11530 * benefit from the "expand a map entry" technology
11531 * at allocation time because the adjacent entry
11532 * is often wired down.
11533 */
11534void
11535vm_map_simplify_entry(
11536 vm_map_t map,
11537 vm_map_entry_t this_entry)
11538{
11539 vm_map_entry_t prev_entry;
11540
11541 counter(c_vm_map_simplify_entry_called++);
11542
11543 prev_entry = this_entry->vme_prev;
11544
11545 if ((this_entry != vm_map_to_entry(map)) &&
11546 (prev_entry != vm_map_to_entry(map)) &&
11547
11548 (prev_entry->vme_end == this_entry->vme_start) &&
11549
11550 (prev_entry->is_sub_map == this_entry->is_sub_map) &&
11551 (prev_entry->object.vm_object == this_entry->object.vm_object) &&
11552 ((prev_entry->offset + (prev_entry->vme_end -
11553 prev_entry->vme_start))
11554 == this_entry->offset) &&
11555
11556 (prev_entry->behavior == this_entry->behavior) &&
11557 (prev_entry->needs_copy == this_entry->needs_copy) &&
11558 (prev_entry->protection == this_entry->protection) &&
11559 (prev_entry->max_protection == this_entry->max_protection) &&
11560 (prev_entry->inheritance == this_entry->inheritance) &&
11561 (prev_entry->use_pmap == this_entry->use_pmap) &&
11562 (prev_entry->alias == this_entry->alias) &&
11563 (prev_entry->no_cache == this_entry->no_cache) &&
11564 (prev_entry->permanent == this_entry->permanent) &&
11565 (prev_entry->map_aligned == this_entry->map_aligned) &&
11566 (prev_entry->zero_wired_pages == this_entry->zero_wired_pages) &&
11567 (prev_entry->used_for_jit == this_entry->used_for_jit) &&
11568 /* from_reserved_zone: OK if that field doesn't match */
11569 (prev_entry->iokit_acct == this_entry->iokit_acct) &&
11570
11571 (prev_entry->wired_count == this_entry->wired_count) &&
11572 (prev_entry->user_wired_count == this_entry->user_wired_count) &&
11573
11574 (prev_entry->in_transition == FALSE) &&
11575 (this_entry->in_transition == FALSE) &&
11576 (prev_entry->needs_wakeup == FALSE) &&
11577 (this_entry->needs_wakeup == FALSE) &&
11578 (prev_entry->is_shared == FALSE) &&
11579 (this_entry->is_shared == FALSE) &&
11580 (prev_entry->superpage_size == FALSE) &&
11581 (this_entry->superpage_size == FALSE)
11582 ) {
11583 vm_map_store_entry_unlink(map, prev_entry);
11584 assert(prev_entry->vme_start < this_entry->vme_end);
11585 if (prev_entry->map_aligned)
11586 assert(VM_MAP_PAGE_ALIGNED(prev_entry->vme_start,
11587 VM_MAP_PAGE_MASK(map)));
11588 this_entry->vme_start = prev_entry->vme_start;
11589 this_entry->offset = prev_entry->offset;
11590 if (prev_entry->is_sub_map) {
11591 vm_map_deallocate(prev_entry->object.sub_map);
11592 } else {
11593 vm_object_deallocate(prev_entry->object.vm_object);
11594 }
11595 vm_map_entry_dispose(map, prev_entry);
11596 SAVE_HINT_MAP_WRITE(map, this_entry);
11597 counter(c_vm_map_simplified++);
11598 }
11599}
11600
11601void
11602vm_map_simplify(
11603 vm_map_t map,
11604 vm_map_offset_t start)
11605{
11606 vm_map_entry_t this_entry;
11607
11608 vm_map_lock(map);
11609 if (vm_map_lookup_entry(map, start, &this_entry)) {
11610 vm_map_simplify_entry(map, this_entry);
11611 vm_map_simplify_entry(map, this_entry->vme_next);
11612 }
11613 counter(c_vm_map_simplify_called++);
11614 vm_map_unlock(map);
11615}
11616
11617static void
11618vm_map_simplify_range(
11619 vm_map_t map,
11620 vm_map_offset_t start,
11621 vm_map_offset_t end)
11622{
11623 vm_map_entry_t entry;
11624
11625 /*
11626 * The map should be locked (for "write") by the caller.
11627 */
11628
11629 if (start >= end) {
11630 /* invalid address range */
11631 return;
11632 }
11633
11634 start = vm_map_trunc_page(start,
11635 VM_MAP_PAGE_MASK(map));
11636 end = vm_map_round_page(end,
11637 VM_MAP_PAGE_MASK(map));
11638
11639 if (!vm_map_lookup_entry(map, start, &entry)) {
11640 /* "start" is not mapped and "entry" ends before "start" */
11641 if (entry == vm_map_to_entry(map)) {
11642 /* start with first entry in the map */
11643 entry = vm_map_first_entry(map);
11644 } else {
11645 /* start with next entry */
11646 entry = entry->vme_next;
11647 }
11648 }
11649
11650 while (entry != vm_map_to_entry(map) &&
11651 entry->vme_start <= end) {
11652 /* try and coalesce "entry" with its previous entry */
11653 vm_map_simplify_entry(map, entry);
11654 entry = entry->vme_next;
11655 }
11656}
11657
11658
11659/*
11660 * Routine: vm_map_machine_attribute
11661 * Purpose:
11662 * Provide machine-specific attributes to mappings,
11663 * such as cachability etc. for machines that provide
11664 * them. NUMA architectures and machines with big/strange
11665 * caches will use this.
11666 * Note:
11667 * Responsibilities for locking and checking are handled here,
11668 * everything else in the pmap module. If any non-volatile
11669 * information must be kept, the pmap module should handle
11670 * it itself. [This assumes that attributes do not
11671 * need to be inherited, which seems ok to me]
11672 */
11673kern_return_t
11674vm_map_machine_attribute(
11675 vm_map_t map,
11676 vm_map_offset_t start,
11677 vm_map_offset_t end,
11678 vm_machine_attribute_t attribute,
11679 vm_machine_attribute_val_t* value) /* IN/OUT */
11680{
11681 kern_return_t ret;
11682 vm_map_size_t sync_size;
11683 vm_map_entry_t entry;
11684
11685 if (start < vm_map_min(map) || end > vm_map_max(map))
11686 return KERN_INVALID_ADDRESS;
11687
11688 /* Figure how much memory we need to flush (in page increments) */
11689 sync_size = end - start;
11690
11691 vm_map_lock(map);
11692
11693 if (attribute != MATTR_CACHE) {
11694 /* If we don't have to find physical addresses, we */
11695 /* don't have to do an explicit traversal here. */
11696 ret = pmap_attribute(map->pmap, start, end-start,
11697 attribute, value);
11698 vm_map_unlock(map);
11699 return ret;
11700 }
11701
11702 ret = KERN_SUCCESS; /* Assume it all worked */
11703
11704 while(sync_size) {
11705 if (vm_map_lookup_entry(map, start, &entry)) {
11706 vm_map_size_t sub_size;
11707 if((entry->vme_end - start) > sync_size) {
11708 sub_size = sync_size;
11709 sync_size = 0;
11710 } else {
11711 sub_size = entry->vme_end - start;
11712 sync_size -= sub_size;
11713 }
11714 if(entry->is_sub_map) {
11715 vm_map_offset_t sub_start;
11716 vm_map_offset_t sub_end;
11717
11718 sub_start = (start - entry->vme_start)
11719 + entry->offset;
11720 sub_end = sub_start + sub_size;
11721 vm_map_machine_attribute(
11722 entry->object.sub_map,
11723 sub_start,
11724 sub_end,
11725 attribute, value);
11726 } else {
11727 if(entry->object.vm_object) {
11728 vm_page_t m;
11729 vm_object_t object;
11730 vm_object_t base_object;
11731 vm_object_t last_object;
11732 vm_object_offset_t offset;
11733 vm_object_offset_t base_offset;
11734 vm_map_size_t range;
11735 range = sub_size;
11736 offset = (start - entry->vme_start)
11737 + entry->offset;
11738 base_offset = offset;
11739 object = entry->object.vm_object;
11740 base_object = object;
11741 last_object = NULL;
11742
11743 vm_object_lock(object);
11744
11745 while (range) {
11746 m = vm_page_lookup(
11747 object, offset);
11748
11749 if (m && !m->fictitious) {
11750 ret =
11751 pmap_attribute_cache_sync(
11752 m->phys_page,
11753 PAGE_SIZE,
11754 attribute, value);
11755
11756 } else if (object->shadow) {
11757 offset = offset + object->vo_shadow_offset;
11758 last_object = object;
11759 object = object->shadow;
11760 vm_object_lock(last_object->shadow);
11761 vm_object_unlock(last_object);
11762 continue;
11763 }
11764 range -= PAGE_SIZE;
11765
11766 if (base_object != object) {
11767 vm_object_unlock(object);
11768 vm_object_lock(base_object);
11769 object = base_object;
11770 }
11771 /* Bump to the next page */
11772 base_offset += PAGE_SIZE;
11773 offset = base_offset;
11774 }
11775 vm_object_unlock(object);
11776 }
11777 }
11778 start += sub_size;
11779 } else {
11780 vm_map_unlock(map);
11781 return KERN_FAILURE;
11782 }
11783
11784 }
11785
11786 vm_map_unlock(map);
11787
11788 return ret;
11789}
11790
11791/*
11792 * vm_map_behavior_set:
11793 *
11794 * Sets the paging reference behavior of the specified address
11795 * range in the target map. Paging reference behavior affects
11796 * how pagein operations resulting from faults on the map will be
11797 * clustered.
11798 */
11799kern_return_t
11800vm_map_behavior_set(
11801 vm_map_t map,
11802 vm_map_offset_t start,
11803 vm_map_offset_t end,
11804 vm_behavior_t new_behavior)
11805{
11806 register vm_map_entry_t entry;
11807 vm_map_entry_t temp_entry;
11808
11809 XPR(XPR_VM_MAP,
11810 "vm_map_behavior_set, 0x%X start 0x%X end 0x%X behavior %d",
11811 map, start, end, new_behavior, 0);
11812
11813 if (start > end ||
11814 start < vm_map_min(map) ||
11815 end > vm_map_max(map)) {
11816 return KERN_NO_SPACE;
11817 }
11818
11819 switch (new_behavior) {
11820
11821 /*
11822 * This first block of behaviors all set a persistent state on the specified
11823 * memory range. All we have to do here is to record the desired behavior
11824 * in the vm_map_entry_t's.
11825 */
11826
11827 case VM_BEHAVIOR_DEFAULT:
11828 case VM_BEHAVIOR_RANDOM:
11829 case VM_BEHAVIOR_SEQUENTIAL:
11830 case VM_BEHAVIOR_RSEQNTL:
11831 case VM_BEHAVIOR_ZERO_WIRED_PAGES:
11832 vm_map_lock(map);
11833
11834 /*
11835 * The entire address range must be valid for the map.
11836 * Note that vm_map_range_check() does a
11837 * vm_map_lookup_entry() internally and returns the
11838 * entry containing the start of the address range if
11839 * the entire range is valid.
11840 */
11841 if (vm_map_range_check(map, start, end, &temp_entry)) {
11842 entry = temp_entry;
11843 vm_map_clip_start(map, entry, start);
11844 }
11845 else {
11846 vm_map_unlock(map);
11847 return(KERN_INVALID_ADDRESS);
11848 }
11849
11850 while ((entry != vm_map_to_entry(map)) && (entry->vme_start < end)) {
11851 vm_map_clip_end(map, entry, end);
11852 if (entry->is_sub_map) {
11853 assert(!entry->use_pmap);
11854 }
11855
11856 if( new_behavior == VM_BEHAVIOR_ZERO_WIRED_PAGES ) {
11857 entry->zero_wired_pages = TRUE;
11858 } else {
11859 entry->behavior = new_behavior;
11860 }
11861 entry = entry->vme_next;
11862 }
11863
11864 vm_map_unlock(map);
11865 break;
11866
11867 /*
11868 * The rest of these are different from the above in that they cause
11869 * an immediate action to take place as opposed to setting a behavior that
11870 * affects future actions.
11871 */
11872
11873 case VM_BEHAVIOR_WILLNEED:
11874 return vm_map_willneed(map, start, end);
11875
11876 case VM_BEHAVIOR_DONTNEED:
11877 return vm_map_msync(map, start, end - start, VM_SYNC_DEACTIVATE | VM_SYNC_CONTIGUOUS);
11878
11879 case VM_BEHAVIOR_FREE:
11880 return vm_map_msync(map, start, end - start, VM_SYNC_KILLPAGES | VM_SYNC_CONTIGUOUS);
11881
11882 case VM_BEHAVIOR_REUSABLE:
11883 return vm_map_reusable_pages(map, start, end);
11884
11885 case VM_BEHAVIOR_REUSE:
11886 return vm_map_reuse_pages(map, start, end);
11887
11888 case VM_BEHAVIOR_CAN_REUSE:
11889 return vm_map_can_reuse(map, start, end);
11890
11891 default:
11892 return(KERN_INVALID_ARGUMENT);
11893 }
11894
11895 return(KERN_SUCCESS);
11896}
11897
11898
11899/*
11900 * Internals for madvise(MADV_WILLNEED) system call.
11901 *
11902 * The present implementation is to do a read-ahead if the mapping corresponds
11903 * to a mapped regular file. If it's an anonymous mapping, then we do nothing
11904 * and basically ignore the "advice" (which we are always free to do).
11905 */
11906
11907
11908static kern_return_t
11909vm_map_willneed(
11910 vm_map_t map,
11911 vm_map_offset_t start,
11912 vm_map_offset_t end
11913)
11914{
11915 vm_map_entry_t entry;
11916 vm_object_t object;
11917 memory_object_t pager;
11918 struct vm_object_fault_info fault_info;
11919 kern_return_t kr;
11920 vm_object_size_t len;
11921 vm_object_offset_t offset;
11922
11923 /*
11924 * Fill in static values in fault_info. Several fields get ignored by the code
11925 * we call, but we'll fill them in anyway since uninitialized fields are bad
11926 * when it comes to future backwards compatibility.
11927 */
11928
11929 fault_info.interruptible = THREAD_UNINT; /* ignored value */
11930 fault_info.behavior = VM_BEHAVIOR_SEQUENTIAL;
11931 fault_info.no_cache = FALSE; /* ignored value */
11932 fault_info.stealth = TRUE;
11933 fault_info.io_sync = FALSE;
11934 fault_info.cs_bypass = FALSE;
11935 fault_info.mark_zf_absent = FALSE;
11936 fault_info.batch_pmap_op = FALSE;
11937
11938 /*
11939 * The MADV_WILLNEED operation doesn't require any changes to the
11940 * vm_map_entry_t's, so the read lock is sufficient.
11941 */
11942
11943 vm_map_lock_read(map);
11944
11945 /*
11946 * The madvise semantics require that the address range be fully
11947 * allocated with no holes. Otherwise, we're required to return
11948 * an error.
11949 */
11950
11951 if (! vm_map_range_check(map, start, end, &entry)) {
11952 vm_map_unlock_read(map);
11953 return KERN_INVALID_ADDRESS;
11954 }
11955
11956 /*
11957 * Examine each vm_map_entry_t in the range.
11958 */
11959 for (; entry != vm_map_to_entry(map) && start < end; ) {
11960
11961 /*
11962 * The first time through, the start address could be anywhere
11963 * within the vm_map_entry we found. So adjust the offset to
11964 * correspond. After that, the offset will always be zero to
11965 * correspond to the beginning of the current vm_map_entry.
11966 */
11967 offset = (start - entry->vme_start) + entry->offset;
11968
11969 /*
11970 * Set the length so we don't go beyond the end of the
11971 * map_entry or beyond the end of the range we were given.
11972 * This range could span also multiple map entries all of which
11973 * map different files, so make sure we only do the right amount
11974 * of I/O for each object. Note that it's possible for there
11975 * to be multiple map entries all referring to the same object
11976 * but with different page permissions, but it's not worth
11977 * trying to optimize that case.
11978 */
11979 len = MIN(entry->vme_end - start, end - start);
11980
11981 if ((vm_size_t) len != len) {
11982 /* 32-bit overflow */
11983 len = (vm_size_t) (0 - PAGE_SIZE);
11984 }
11985 fault_info.cluster_size = (vm_size_t) len;
11986 fault_info.lo_offset = offset;
11987 fault_info.hi_offset = offset + len;
11988 fault_info.user_tag = entry->alias;
11989 fault_info.pmap_options = 0;
11990 if (entry->iokit_acct ||
11991 (!entry->is_sub_map && !entry->use_pmap)) {
11992 fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT;
11993 }
11994
11995 /*
11996 * If there's no read permission to this mapping, then just
11997 * skip it.
11998 */
11999 if ((entry->protection & VM_PROT_READ) == 0) {
12000 entry = entry->vme_next;
12001 start = entry->vme_start;
12002 continue;
12003 }
12004
12005 /*
12006 * Find the file object backing this map entry. If there is
12007 * none, then we simply ignore the "will need" advice for this
12008 * entry and go on to the next one.
12009 */
12010 if ((object = find_vnode_object(entry)) == VM_OBJECT_NULL) {
12011 entry = entry->vme_next;
12012 start = entry->vme_start;
12013 continue;
12014 }
12015
12016 /*
12017 * The data_request() could take a long time, so let's
12018 * release the map lock to avoid blocking other threads.
12019 */
12020 vm_map_unlock_read(map);
12021
12022 vm_object_paging_begin(object);
12023 pager = object->pager;
12024 vm_object_unlock(object);
12025
12026 /*
12027 * Get the data from the object asynchronously.
12028 *
12029 * Note that memory_object_data_request() places limits on the
12030 * amount of I/O it will do. Regardless of the len we
12031 * specified, it won't do more than MAX_UPL_TRANSFER_BYTES and it
12032 * silently truncates the len to that size. This isn't
12033 * necessarily bad since madvise shouldn't really be used to
12034 * page in unlimited amounts of data. Other Unix variants
12035 * limit the willneed case as well. If this turns out to be an
12036 * issue for developers, then we can always adjust the policy
12037 * here and still be backwards compatible since this is all
12038 * just "advice".
12039 */
12040 kr = memory_object_data_request(
12041 pager,
12042 offset + object->paging_offset,
12043 0, /* ignored */
12044 VM_PROT_READ,
12045 (memory_object_fault_info_t)&fault_info);
12046
12047 vm_object_lock(object);
12048 vm_object_paging_end(object);
12049 vm_object_unlock(object);
12050
12051 /*
12052 * If we couldn't do the I/O for some reason, just give up on
12053 * the madvise. We still return success to the user since
12054 * madvise isn't supposed to fail when the advice can't be
12055 * taken.
12056 */
12057 if (kr != KERN_SUCCESS) {
12058 return KERN_SUCCESS;
12059 }
12060
12061 start += len;
12062 if (start >= end) {
12063 /* done */
12064 return KERN_SUCCESS;
12065 }
12066
12067 /* look up next entry */
12068 vm_map_lock_read(map);
12069 if (! vm_map_lookup_entry(map, start, &entry)) {
12070 /*
12071 * There's a new hole in the address range.
12072 */
12073 vm_map_unlock_read(map);
12074 return KERN_INVALID_ADDRESS;
12075 }
12076 }
12077
12078 vm_map_unlock_read(map);
12079 return KERN_SUCCESS;
12080}
12081
12082static boolean_t
12083vm_map_entry_is_reusable(
12084 vm_map_entry_t entry)
12085{
12086 vm_object_t object;
12087
12088 switch (entry->alias) {
12089 case VM_MEMORY_MALLOC:
12090 case VM_MEMORY_MALLOC_SMALL:
12091 case VM_MEMORY_MALLOC_LARGE:
12092 case VM_MEMORY_REALLOC:
12093 case VM_MEMORY_MALLOC_TINY:
12094 case VM_MEMORY_MALLOC_LARGE_REUSABLE:
12095 case VM_MEMORY_MALLOC_LARGE_REUSED:
12096 /*
12097 * This is a malloc() memory region: check if it's still
12098 * in its original state and can be re-used for more
12099 * malloc() allocations.
12100 */
12101 break;
12102 default:
12103 /*
12104 * Not a malloc() memory region: let the caller decide if
12105 * it's re-usable.
12106 */
12107 return TRUE;
12108 }
12109
12110 if (entry->is_shared ||
12111 entry->is_sub_map ||
12112 entry->in_transition ||
12113 entry->protection != VM_PROT_DEFAULT ||
12114 entry->max_protection != VM_PROT_ALL ||
12115 entry->inheritance != VM_INHERIT_DEFAULT ||
12116 entry->no_cache ||
12117 entry->permanent ||
12118 entry->superpage_size != FALSE ||
12119 entry->zero_wired_pages ||
12120 entry->wired_count != 0 ||
12121 entry->user_wired_count != 0) {
12122 return FALSE;
12123 }
12124
12125 object = entry->object.vm_object;
12126 if (object == VM_OBJECT_NULL) {
12127 return TRUE;
12128 }
12129 if (
12130#if 0
12131 /*
12132 * Let's proceed even if the VM object is potentially
12133 * shared.
12134 * We check for this later when processing the actual
12135 * VM pages, so the contents will be safe if shared.
12136 *
12137 * But we can still mark this memory region as "reusable" to
12138 * acknowledge that the caller did let us know that the memory
12139 * could be re-used and should not be penalized for holding
12140 * on to it. This allows its "resident size" to not include
12141 * the reusable range.
12142 */
12143 object->ref_count == 1 &&
12144#endif
12145 object->wired_page_count == 0 &&
12146 object->copy == VM_OBJECT_NULL &&
12147 object->shadow == VM_OBJECT_NULL &&
12148 object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC &&
12149 object->internal &&
12150 !object->true_share &&
12151 object->wimg_bits == VM_WIMG_USE_DEFAULT &&
12152 !object->code_signed) {
12153 return TRUE;
12154 }
12155 return FALSE;
12156
12157
12158}
12159
12160static kern_return_t
12161vm_map_reuse_pages(
12162 vm_map_t map,
12163 vm_map_offset_t start,
12164 vm_map_offset_t end)
12165{
12166 vm_map_entry_t entry;
12167 vm_object_t object;
12168 vm_object_offset_t start_offset, end_offset;
12169
12170 /*
12171 * The MADV_REUSE operation doesn't require any changes to the
12172 * vm_map_entry_t's, so the read lock is sufficient.
12173 */
12174
12175 vm_map_lock_read(map);
12176
12177 /*
12178 * The madvise semantics require that the address range be fully
12179 * allocated with no holes. Otherwise, we're required to return
12180 * an error.
12181 */
12182
12183 if (!vm_map_range_check(map, start, end, &entry)) {
12184 vm_map_unlock_read(map);
12185 vm_page_stats_reusable.reuse_pages_failure++;
12186 return KERN_INVALID_ADDRESS;
12187 }
12188
12189 /*
12190 * Examine each vm_map_entry_t in the range.
12191 */
12192 for (; entry != vm_map_to_entry(map) && entry->vme_start < end;
12193 entry = entry->vme_next) {
12194 /*
12195 * Sanity check on the VM map entry.
12196 */
12197 if (! vm_map_entry_is_reusable(entry)) {
12198 vm_map_unlock_read(map);
12199 vm_page_stats_reusable.reuse_pages_failure++;
12200 return KERN_INVALID_ADDRESS;
12201 }
12202
12203 /*
12204 * The first time through, the start address could be anywhere
12205 * within the vm_map_entry we found. So adjust the offset to
12206 * correspond.
12207 */
12208 if (entry->vme_start < start) {
12209 start_offset = start - entry->vme_start;
12210 } else {
12211 start_offset = 0;
12212 }
12213 end_offset = MIN(end, entry->vme_end) - entry->vme_start;
12214 start_offset += entry->offset;
12215 end_offset += entry->offset;
12216
12217 object = entry->object.vm_object;
12218 if (object != VM_OBJECT_NULL) {
12219 vm_object_lock(object);
12220 vm_object_reuse_pages(object, start_offset, end_offset,
12221 TRUE);
12222 vm_object_unlock(object);
12223 }
12224
12225 if (entry->alias == VM_MEMORY_MALLOC_LARGE_REUSABLE) {
12226 /*
12227 * XXX
12228 * We do not hold the VM map exclusively here.
12229 * The "alias" field is not that critical, so it's
12230 * safe to update it here, as long as it is the only
12231 * one that can be modified while holding the VM map
12232 * "shared".
12233 */
12234 entry->alias = VM_MEMORY_MALLOC_LARGE_REUSED;
12235 }
12236 }
12237
12238 vm_map_unlock_read(map);
12239 vm_page_stats_reusable.reuse_pages_success++;
12240 return KERN_SUCCESS;
12241}
12242
12243
12244static kern_return_t
12245vm_map_reusable_pages(
12246 vm_map_t map,
12247 vm_map_offset_t start,
12248 vm_map_offset_t end)
12249{
12250 vm_map_entry_t entry;
12251 vm_object_t object;
12252 vm_object_offset_t start_offset, end_offset;
12253
12254 /*
12255 * The MADV_REUSABLE operation doesn't require any changes to the
12256 * vm_map_entry_t's, so the read lock is sufficient.
12257 */
12258
12259 vm_map_lock_read(map);
12260
12261 /*
12262 * The madvise semantics require that the address range be fully
12263 * allocated with no holes. Otherwise, we're required to return
12264 * an error.
12265 */
12266
12267 if (!vm_map_range_check(map, start, end, &entry)) {
12268 vm_map_unlock_read(map);
12269 vm_page_stats_reusable.reusable_pages_failure++;
12270 return KERN_INVALID_ADDRESS;
12271 }
12272
12273 /*
12274 * Examine each vm_map_entry_t in the range.
12275 */
12276 for (; entry != vm_map_to_entry(map) && entry->vme_start < end;
12277 entry = entry->vme_next) {
12278 int kill_pages = 0;
12279
12280 /*
12281 * Sanity check on the VM map entry.
12282 */
12283 if (! vm_map_entry_is_reusable(entry)) {
12284 vm_map_unlock_read(map);
12285 vm_page_stats_reusable.reusable_pages_failure++;
12286 return KERN_INVALID_ADDRESS;
12287 }
12288
12289 /*
12290 * The first time through, the start address could be anywhere
12291 * within the vm_map_entry we found. So adjust the offset to
12292 * correspond.
12293 */
12294 if (entry->vme_start < start) {
12295 start_offset = start - entry->vme_start;
12296 } else {
12297 start_offset = 0;
12298 }
12299 end_offset = MIN(end, entry->vme_end) - entry->vme_start;
12300 start_offset += entry->offset;
12301 end_offset += entry->offset;
12302
12303 object = entry->object.vm_object;
12304 if (object == VM_OBJECT_NULL)
12305 continue;
12306
12307
12308 vm_object_lock(object);
12309 if (object->ref_count == 1 &&
12310 !object->shadow &&
12311 /*
12312 * "iokit_acct" entries are billed for their virtual size
12313 * (rather than for their resident pages only), so they
12314 * wouldn't benefit from making pages reusable, and it
12315 * would be hard to keep track of pages that are both
12316 * "iokit_acct" and "reusable" in the pmap stats and ledgers.
12317 */
12318 !(entry->iokit_acct ||
12319 (!entry->is_sub_map && !entry->use_pmap)))
12320 kill_pages = 1;
12321 else
12322 kill_pages = -1;
12323 if (kill_pages != -1) {
12324 vm_object_deactivate_pages(object,
12325 start_offset,
12326 end_offset - start_offset,
12327 kill_pages,
12328 TRUE /*reusable_pages*/);
12329 } else {
12330 vm_page_stats_reusable.reusable_pages_shared++;
12331 }
12332 vm_object_unlock(object);
12333
12334 if (entry->alias == VM_MEMORY_MALLOC_LARGE ||
12335 entry->alias == VM_MEMORY_MALLOC_LARGE_REUSED) {
12336 /*
12337 * XXX
12338 * We do not hold the VM map exclusively here.
12339 * The "alias" field is not that critical, so it's
12340 * safe to update it here, as long as it is the only
12341 * one that can be modified while holding the VM map
12342 * "shared".
12343 */
12344 entry->alias = VM_MEMORY_MALLOC_LARGE_REUSABLE;
12345 }
12346 }
12347
12348 vm_map_unlock_read(map);
12349 vm_page_stats_reusable.reusable_pages_success++;
12350 return KERN_SUCCESS;
12351}
12352
12353
12354static kern_return_t
12355vm_map_can_reuse(
12356 vm_map_t map,
12357 vm_map_offset_t start,
12358 vm_map_offset_t end)
12359{
12360 vm_map_entry_t entry;
12361
12362 /*
12363 * The MADV_REUSABLE operation doesn't require any changes to the
12364 * vm_map_entry_t's, so the read lock is sufficient.
12365 */
12366
12367 vm_map_lock_read(map);
12368
12369 /*
12370 * The madvise semantics require that the address range be fully
12371 * allocated with no holes. Otherwise, we're required to return
12372 * an error.
12373 */
12374
12375 if (!vm_map_range_check(map, start, end, &entry)) {
12376 vm_map_unlock_read(map);
12377 vm_page_stats_reusable.can_reuse_failure++;
12378 return KERN_INVALID_ADDRESS;
12379 }
12380
12381 /*
12382 * Examine each vm_map_entry_t in the range.
12383 */
12384 for (; entry != vm_map_to_entry(map) && entry->vme_start < end;
12385 entry = entry->vme_next) {
12386 /*
12387 * Sanity check on the VM map entry.
12388 */
12389 if (! vm_map_entry_is_reusable(entry)) {
12390 vm_map_unlock_read(map);
12391 vm_page_stats_reusable.can_reuse_failure++;
12392 return KERN_INVALID_ADDRESS;
12393 }
12394 }
12395
12396 vm_map_unlock_read(map);
12397 vm_page_stats_reusable.can_reuse_success++;
12398 return KERN_SUCCESS;
12399}
12400
12401
12402/*
12403 * Routine: vm_map_entry_insert
12404 *
12405 * Descritpion: This routine inserts a new vm_entry in a locked map.
12406 */
12407vm_map_entry_t
12408vm_map_entry_insert(
12409 vm_map_t map,
12410 vm_map_entry_t insp_entry,
12411 vm_map_offset_t start,
12412 vm_map_offset_t end,
12413 vm_object_t object,
12414 vm_object_offset_t offset,
12415 boolean_t needs_copy,
12416 boolean_t is_shared,
12417 boolean_t in_transition,
12418 vm_prot_t cur_protection,
12419 vm_prot_t max_protection,
12420 vm_behavior_t behavior,
12421 vm_inherit_t inheritance,
12422 unsigned wired_count,
12423 boolean_t no_cache,
12424 boolean_t permanent,
12425 unsigned int superpage_size,
12426 boolean_t clear_map_aligned,
12427 boolean_t is_submap)
12428{
12429 vm_map_entry_t new_entry;
12430
12431 assert(insp_entry != (vm_map_entry_t)0);
12432
12433 new_entry = vm_map_entry_create(map, !map->hdr.entries_pageable);
12434
12435 if (VM_MAP_PAGE_SHIFT(map) != PAGE_SHIFT) {
12436 new_entry->map_aligned = TRUE;
12437 } else {
12438 new_entry->map_aligned = FALSE;
12439 }
12440 if (clear_map_aligned &&
12441 (! VM_MAP_PAGE_ALIGNED(start, VM_MAP_PAGE_MASK(map)) ||
12442 ! VM_MAP_PAGE_ALIGNED(end, VM_MAP_PAGE_MASK(map)))) {
12443 new_entry->map_aligned = FALSE;
12444 }
12445
12446 new_entry->vme_start = start;
12447 new_entry->vme_end = end;
12448 assert(page_aligned(new_entry->vme_start));
12449 assert(page_aligned(new_entry->vme_end));
12450 if (new_entry->map_aligned) {
12451 assert(VM_MAP_PAGE_ALIGNED(new_entry->vme_start,
12452 VM_MAP_PAGE_MASK(map)));
12453 assert(VM_MAP_PAGE_ALIGNED(new_entry->vme_end,
12454 VM_MAP_PAGE_MASK(map)));
12455 }
12456 assert(new_entry->vme_start < new_entry->vme_end);
12457
12458 new_entry->object.vm_object = object;
12459 new_entry->offset = offset;
12460 new_entry->is_shared = is_shared;
12461 new_entry->is_sub_map = is_submap;
12462 new_entry->needs_copy = needs_copy;
12463 new_entry->in_transition = in_transition;
12464 new_entry->needs_wakeup = FALSE;
12465 new_entry->inheritance = inheritance;
12466 new_entry->protection = cur_protection;
12467 new_entry->max_protection = max_protection;
12468 new_entry->behavior = behavior;
12469 new_entry->wired_count = wired_count;
12470 new_entry->user_wired_count = 0;
12471 if (is_submap) {
12472 /*
12473 * submap: "use_pmap" means "nested".
12474 * default: false.
12475 */
12476 new_entry->use_pmap = FALSE;
12477 } else {
12478 /*
12479 * object: "use_pmap" means "use pmap accounting" for footprint.
12480 * default: true.
12481 */
12482 new_entry->use_pmap = TRUE;
12483 }
12484 new_entry->alias = 0;
12485 new_entry->zero_wired_pages = FALSE;
12486 new_entry->no_cache = no_cache;
12487 new_entry->permanent = permanent;
12488 if (superpage_size)
12489 new_entry->superpage_size = TRUE;
12490 else
12491 new_entry->superpage_size = FALSE;
12492 new_entry->used_for_jit = FALSE;
12493 new_entry->iokit_acct = FALSE;
12494
12495 /*
12496 * Insert the new entry into the list.
12497 */
12498
12499 vm_map_store_entry_link(map, insp_entry, new_entry);
12500 map->size += end - start;
12501
12502 /*
12503 * Update the free space hint and the lookup hint.
12504 */
12505
12506 SAVE_HINT_MAP_WRITE(map, new_entry);
12507 return new_entry;
12508}
12509
12510/*
12511 * Routine: vm_map_remap_extract
12512 *
12513 * Descritpion: This routine returns a vm_entry list from a map.
12514 */
12515static kern_return_t
12516vm_map_remap_extract(
12517 vm_map_t map,
12518 vm_map_offset_t addr,
12519 vm_map_size_t size,
12520 boolean_t copy,
12521 struct vm_map_header *map_header,
12522 vm_prot_t *cur_protection,
12523 vm_prot_t *max_protection,
12524 /* What, no behavior? */
12525 vm_inherit_t inheritance,
12526 boolean_t pageable)
12527{
12528 kern_return_t result;
12529 vm_map_size_t mapped_size;
12530 vm_map_size_t tmp_size;
12531 vm_map_entry_t src_entry; /* result of last map lookup */
12532 vm_map_entry_t new_entry;
12533 vm_object_offset_t offset;
12534 vm_map_offset_t map_address;
12535 vm_map_offset_t src_start; /* start of entry to map */
12536 vm_map_offset_t src_end; /* end of region to be mapped */
12537 vm_object_t object;
12538 vm_map_version_t version;
12539 boolean_t src_needs_copy;
12540 boolean_t new_entry_needs_copy;
12541
12542 assert(map != VM_MAP_NULL);
12543 assert(size != 0);
12544 assert(size == vm_map_round_page(size, PAGE_MASK));
12545 assert(inheritance == VM_INHERIT_NONE ||
12546 inheritance == VM_INHERIT_COPY ||
12547 inheritance == VM_INHERIT_SHARE);
12548
12549 /*
12550 * Compute start and end of region.
12551 */
12552 src_start = vm_map_trunc_page(addr, PAGE_MASK);
12553 src_end = vm_map_round_page(src_start + size, PAGE_MASK);
12554
12555
12556 /*
12557 * Initialize map_header.
12558 */
12559 map_header->links.next = (struct vm_map_entry *)&map_header->links;
12560 map_header->links.prev = (struct vm_map_entry *)&map_header->links;
12561 map_header->nentries = 0;
12562 map_header->entries_pageable = pageable;
12563 map_header->page_shift = PAGE_SHIFT;
12564
12565 vm_map_store_init( map_header );
12566
12567 *cur_protection = VM_PROT_ALL;
12568 *max_protection = VM_PROT_ALL;
12569
12570 map_address = 0;
12571 mapped_size = 0;
12572 result = KERN_SUCCESS;
12573
12574 /*
12575 * The specified source virtual space might correspond to
12576 * multiple map entries, need to loop on them.
12577 */
12578 vm_map_lock(map);
12579 while (mapped_size != size) {
12580 vm_map_size_t entry_size;
12581
12582 /*
12583 * Find the beginning of the region.
12584 */
12585 if (! vm_map_lookup_entry(map, src_start, &src_entry)) {
12586 result = KERN_INVALID_ADDRESS;
12587 break;
12588 }
12589
12590 if (src_start < src_entry->vme_start ||
12591 (mapped_size && src_start != src_entry->vme_start)) {
12592 result = KERN_INVALID_ADDRESS;
12593 break;
12594 }
12595
12596 tmp_size = size - mapped_size;
12597 if (src_end > src_entry->vme_end)
12598 tmp_size -= (src_end - src_entry->vme_end);
12599
12600 entry_size = (vm_map_size_t)(src_entry->vme_end -
12601 src_entry->vme_start);
12602
12603 if(src_entry->is_sub_map) {
12604 vm_map_reference(src_entry->object.sub_map);
12605 object = VM_OBJECT_NULL;
12606 } else {
12607 object = src_entry->object.vm_object;
12608 if (src_entry->iokit_acct) {
12609 /*
12610 * This entry uses "IOKit accounting".
12611 */
12612 } else if (object != VM_OBJECT_NULL &&
12613 object->purgable != VM_PURGABLE_DENY) {
12614 /*
12615 * Purgeable objects have their own accounting:
12616 * no pmap accounting for them.
12617 */
12618 assert(!src_entry->use_pmap);
12619 } else {
12620 /*
12621 * Not IOKit or purgeable:
12622 * must be accounted by pmap stats.
12623 */
12624 assert(src_entry->use_pmap);
12625 }
12626
12627 if (object == VM_OBJECT_NULL) {
12628 object = vm_object_allocate(entry_size);
12629 src_entry->offset = 0;
12630 src_entry->object.vm_object = object;
12631 } else if (object->copy_strategy !=
12632 MEMORY_OBJECT_COPY_SYMMETRIC) {
12633 /*
12634 * We are already using an asymmetric
12635 * copy, and therefore we already have
12636 * the right object.
12637 */
12638 assert(!src_entry->needs_copy);
12639 } else if (src_entry->needs_copy || object->shadowed ||
12640 (object->internal && !object->true_share &&
12641 !src_entry->is_shared &&
12642 object->vo_size > entry_size)) {
12643
12644 vm_object_shadow(&src_entry->object.vm_object,
12645 &src_entry->offset,
12646 entry_size);
12647
12648 if (!src_entry->needs_copy &&
12649 (src_entry->protection & VM_PROT_WRITE)) {
12650 vm_prot_t prot;
12651
12652 prot = src_entry->protection & ~VM_PROT_WRITE;
12653
12654 if (override_nx(map, src_entry->alias) && prot)
12655 prot |= VM_PROT_EXECUTE;
12656
12657 if(map->mapped_in_other_pmaps) {
12658 vm_object_pmap_protect(
12659 src_entry->object.vm_object,
12660 src_entry->offset,
12661 entry_size,
12662 PMAP_NULL,
12663 src_entry->vme_start,
12664 prot);
12665 } else {
12666 pmap_protect(vm_map_pmap(map),
12667 src_entry->vme_start,
12668 src_entry->vme_end,
12669 prot);
12670 }
12671 }
12672
12673 object = src_entry->object.vm_object;
12674 src_entry->needs_copy = FALSE;
12675 }
12676
12677
12678 vm_object_lock(object);
12679 vm_object_reference_locked(object); /* object ref. for new entry */
12680 if (object->copy_strategy ==
12681 MEMORY_OBJECT_COPY_SYMMETRIC) {
12682 object->copy_strategy =
12683 MEMORY_OBJECT_COPY_DELAY;
12684 }
12685 vm_object_unlock(object);
12686 }
12687
12688 offset = src_entry->offset + (src_start - src_entry->vme_start);
12689
12690 new_entry = _vm_map_entry_create(map_header, !map_header->entries_pageable);
12691 vm_map_entry_copy(new_entry, src_entry);
12692 if (new_entry->is_sub_map) {
12693 /* clr address space specifics */
12694 new_entry->use_pmap = FALSE;
12695 }
12696
12697 new_entry->map_aligned = FALSE;
12698
12699 new_entry->vme_start = map_address;
12700 new_entry->vme_end = map_address + tmp_size;
12701 assert(new_entry->vme_start < new_entry->vme_end);
12702 new_entry->inheritance = inheritance;
12703 new_entry->offset = offset;
12704
12705 /*
12706 * The new region has to be copied now if required.
12707 */
12708 RestartCopy:
12709 if (!copy) {
12710 /*
12711 * Cannot allow an entry describing a JIT
12712 * region to be shared across address spaces.
12713 */
12714 if (src_entry->used_for_jit == TRUE) {
12715 result = KERN_INVALID_ARGUMENT;
12716 break;
12717 }
12718 src_entry->is_shared = TRUE;
12719 new_entry->is_shared = TRUE;
12720 if (!(new_entry->is_sub_map))
12721 new_entry->needs_copy = FALSE;
12722
12723 } else if (src_entry->is_sub_map) {
12724 /* make this a COW sub_map if not already */
12725 new_entry->needs_copy = TRUE;
12726 object = VM_OBJECT_NULL;
12727 } else if (src_entry->wired_count == 0 &&
12728 vm_object_copy_quickly(&new_entry->object.vm_object,
12729 new_entry->offset,
12730 (new_entry->vme_end -
12731 new_entry->vme_start),
12732 &src_needs_copy,
12733 &new_entry_needs_copy)) {
12734
12735 new_entry->needs_copy = new_entry_needs_copy;
12736 new_entry->is_shared = FALSE;
12737
12738 /*
12739 * Handle copy_on_write semantics.
12740 */
12741 if (src_needs_copy && !src_entry->needs_copy) {
12742 vm_prot_t prot;
12743
12744 prot = src_entry->protection & ~VM_PROT_WRITE;
12745
12746 if (override_nx(map, src_entry->alias) && prot)
12747 prot |= VM_PROT_EXECUTE;
12748
12749 vm_object_pmap_protect(object,
12750 offset,
12751 entry_size,
12752 ((src_entry->is_shared
12753 || map->mapped_in_other_pmaps) ?
12754 PMAP_NULL : map->pmap),
12755 src_entry->vme_start,
12756 prot);
12757
12758 src_entry->needs_copy = TRUE;
12759 }
12760 /*
12761 * Throw away the old object reference of the new entry.
12762 */
12763 vm_object_deallocate(object);
12764
12765 } else {
12766 new_entry->is_shared = FALSE;
12767
12768 /*
12769 * The map can be safely unlocked since we
12770 * already hold a reference on the object.
12771 *
12772 * Record the timestamp of the map for later
12773 * verification, and unlock the map.
12774 */
12775 version.main_timestamp = map->timestamp;
12776 vm_map_unlock(map); /* Increments timestamp once! */
12777
12778 /*
12779 * Perform the copy.
12780 */
12781 if (src_entry->wired_count > 0) {
12782 vm_object_lock(object);
12783 result = vm_object_copy_slowly(
12784 object,
12785 offset,
12786 entry_size,
12787 THREAD_UNINT,
12788 &new_entry->object.vm_object);
12789
12790 new_entry->offset = 0;
12791 new_entry->needs_copy = FALSE;
12792 } else {
12793 result = vm_object_copy_strategically(
12794 object,
12795 offset,
12796 entry_size,
12797 &new_entry->object.vm_object,
12798 &new_entry->offset,
12799 &new_entry_needs_copy);
12800
12801 new_entry->needs_copy = new_entry_needs_copy;
12802 }
12803
12804 /*
12805 * Throw away the old object reference of the new entry.
12806 */
12807 vm_object_deallocate(object);
12808
12809 if (result != KERN_SUCCESS &&
12810 result != KERN_MEMORY_RESTART_COPY) {
12811 _vm_map_entry_dispose(map_header, new_entry);
12812 break;
12813 }
12814
12815 /*
12816 * Verify that the map has not substantially
12817 * changed while the copy was being made.
12818 */
12819
12820 vm_map_lock(map);
12821 if (version.main_timestamp + 1 != map->timestamp) {
12822 /*
12823 * Simple version comparison failed.
12824 *
12825 * Retry the lookup and verify that the
12826 * same object/offset are still present.
12827 */
12828 vm_object_deallocate(new_entry->
12829 object.vm_object);
12830 _vm_map_entry_dispose(map_header, new_entry);
12831 if (result == KERN_MEMORY_RESTART_COPY)
12832 result = KERN_SUCCESS;
12833 continue;
12834 }
12835
12836 if (result == KERN_MEMORY_RESTART_COPY) {
12837 vm_object_reference(object);
12838 goto RestartCopy;
12839 }
12840 }
12841
12842 _vm_map_store_entry_link(map_header,
12843 map_header->links.prev, new_entry);
12844
12845 /*Protections for submap mapping are irrelevant here*/
12846 if( !src_entry->is_sub_map ) {
12847 *cur_protection &= src_entry->protection;
12848 *max_protection &= src_entry->max_protection;
12849 }
12850 map_address += tmp_size;
12851 mapped_size += tmp_size;
12852 src_start += tmp_size;
12853
12854 } /* end while */
12855
12856 vm_map_unlock(map);
12857 if (result != KERN_SUCCESS) {
12858 /*
12859 * Free all allocated elements.
12860 */
12861 for (src_entry = map_header->links.next;
12862 src_entry != (struct vm_map_entry *)&map_header->links;
12863 src_entry = new_entry) {
12864 new_entry = src_entry->vme_next;
12865 _vm_map_store_entry_unlink(map_header, src_entry);
12866 if (src_entry->is_sub_map) {
12867 vm_map_deallocate(src_entry->object.sub_map);
12868 } else {
12869 vm_object_deallocate(src_entry->object.vm_object);
12870 }
12871 _vm_map_entry_dispose(map_header, src_entry);
12872 }
12873 }
12874 return result;
12875}
12876
12877/*
12878 * Routine: vm_remap
12879 *
12880 * Map portion of a task's address space.
12881 * Mapped region must not overlap more than
12882 * one vm memory object. Protections and
12883 * inheritance attributes remain the same
12884 * as in the original task and are out parameters.
12885 * Source and Target task can be identical
12886 * Other attributes are identical as for vm_map()
12887 */
12888kern_return_t
12889vm_map_remap(
12890 vm_map_t target_map,
12891 vm_map_address_t *address,
12892 vm_map_size_t size,
12893 vm_map_offset_t mask,
12894 int flags,
12895 vm_map_t src_map,
12896 vm_map_offset_t memory_address,
12897 boolean_t copy,
12898 vm_prot_t *cur_protection,
12899 vm_prot_t *max_protection,
12900 vm_inherit_t inheritance)
12901{
12902 kern_return_t result;
12903 vm_map_entry_t entry;
12904 vm_map_entry_t insp_entry = VM_MAP_ENTRY_NULL;
12905 vm_map_entry_t new_entry;
12906 struct vm_map_header map_header;
12907 vm_map_offset_t offset_in_mapping;
12908
12909 if (target_map == VM_MAP_NULL)
12910 return KERN_INVALID_ARGUMENT;
12911
12912 switch (inheritance) {
12913 case VM_INHERIT_NONE:
12914 case VM_INHERIT_COPY:
12915 case VM_INHERIT_SHARE:
12916 if (size != 0 && src_map != VM_MAP_NULL)
12917 break;
12918 /*FALL THRU*/
12919 default:
12920 return KERN_INVALID_ARGUMENT;
12921 }
12922
12923 /*
12924 * If the user is requesting that we return the address of the
12925 * first byte of the data (rather than the base of the page),
12926 * then we use different rounding semantics: specifically,
12927 * we assume that (memory_address, size) describes a region
12928 * all of whose pages we must cover, rather than a base to be truncated
12929 * down and a size to be added to that base. So we figure out
12930 * the highest page that the requested region includes and make
12931 * sure that the size will cover it.
12932 *
12933 * The key example we're worried about it is of the form:
12934 *
12935 * memory_address = 0x1ff0, size = 0x20
12936 *
12937 * With the old semantics, we round down the memory_address to 0x1000
12938 * and round up the size to 0x1000, resulting in our covering *only*
12939 * page 0x1000. With the new semantics, we'd realize that the region covers
12940 * 0x1ff0-0x2010, and compute a size of 0x2000. Thus, we cover both page
12941 * 0x1000 and page 0x2000 in the region we remap.
12942 */
12943 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
12944 offset_in_mapping = memory_address - vm_map_trunc_page(memory_address, PAGE_MASK);
12945 size = vm_map_round_page(memory_address + size - vm_map_trunc_page(memory_address, PAGE_MASK), PAGE_MASK);
12946 } else {
12947 size = vm_map_round_page(size, PAGE_MASK);
12948 }
12949
12950 result = vm_map_remap_extract(src_map, memory_address,
12951 size, copy, &map_header,
12952 cur_protection,
12953 max_protection,
12954 inheritance,
12955 target_map->hdr.entries_pageable);
12956
12957 if (result != KERN_SUCCESS) {
12958 return result;
12959 }
12960
12961 /*
12962 * Allocate/check a range of free virtual address
12963 * space for the target
12964 */
12965 *address = vm_map_trunc_page(*address,
12966 VM_MAP_PAGE_MASK(target_map));
12967 vm_map_lock(target_map);
12968 result = vm_map_remap_range_allocate(target_map, address, size,
12969 mask, flags, &insp_entry);
12970
12971 for (entry = map_header.links.next;
12972 entry != (struct vm_map_entry *)&map_header.links;
12973 entry = new_entry) {
12974 new_entry = entry->vme_next;
12975 _vm_map_store_entry_unlink(&map_header, entry);
12976 if (result == KERN_SUCCESS) {
12977 entry->vme_start += *address;
12978 entry->vme_end += *address;
12979 assert(!entry->map_aligned);
12980 vm_map_store_entry_link(target_map, insp_entry, entry);
12981 insp_entry = entry;
12982 } else {
12983 if (!entry->is_sub_map) {
12984 vm_object_deallocate(entry->object.vm_object);
12985 } else {
12986 vm_map_deallocate(entry->object.sub_map);
12987 }
12988 _vm_map_entry_dispose(&map_header, entry);
12989 }
12990 }
12991
12992 if( target_map->disable_vmentry_reuse == TRUE) {
12993 if( target_map->highest_entry_end < insp_entry->vme_end ){
12994 target_map->highest_entry_end = insp_entry->vme_end;
12995 }
12996 }
12997
12998 if (result == KERN_SUCCESS) {
12999 target_map->size += size;
13000 SAVE_HINT_MAP_WRITE(target_map, insp_entry);
13001 }
13002 vm_map_unlock(target_map);
13003
13004 if (result == KERN_SUCCESS && target_map->wiring_required)
13005 result = vm_map_wire(target_map, *address,
13006 *address + size, *cur_protection, TRUE);
13007
13008 /*
13009 * If requested, return the address of the data pointed to by the
13010 * request, rather than the base of the resulting page.
13011 */
13012 if ((flags & VM_FLAGS_RETURN_DATA_ADDR) != 0) {
13013 *address += offset_in_mapping;
13014 }
13015
13016 return result;
13017}
13018
13019/*
13020 * Routine: vm_map_remap_range_allocate
13021 *
13022 * Description:
13023 * Allocate a range in the specified virtual address map.
13024 * returns the address and the map entry just before the allocated
13025 * range
13026 *
13027 * Map must be locked.
13028 */
13029
13030static kern_return_t
13031vm_map_remap_range_allocate(
13032 vm_map_t map,
13033 vm_map_address_t *address, /* IN/OUT */
13034 vm_map_size_t size,
13035 vm_map_offset_t mask,
13036 int flags,
13037 vm_map_entry_t *map_entry) /* OUT */
13038{
13039 vm_map_entry_t entry;
13040 vm_map_offset_t start;
13041 vm_map_offset_t end;
13042 kern_return_t kr;
13043
13044StartAgain: ;
13045
13046 start = *address;
13047
13048 if (flags & VM_FLAGS_ANYWHERE)
13049 {
13050 /*
13051 * Calculate the first possible address.
13052 */
13053
13054 if (start < map->min_offset)
13055 start = map->min_offset;
13056 if (start > map->max_offset)
13057 return(KERN_NO_SPACE);
13058
13059 /*
13060 * Look for the first possible address;
13061 * if there's already something at this
13062 * address, we have to start after it.
13063 */
13064
13065 if( map->disable_vmentry_reuse == TRUE) {
13066 VM_MAP_HIGHEST_ENTRY(map, entry, start);
13067 } else {
13068 assert(first_free_is_valid(map));
13069 if (start == map->min_offset) {
13070 if ((entry = map->first_free) != vm_map_to_entry(map))
13071 start = entry->vme_end;
13072 } else {
13073 vm_map_entry_t tmp_entry;
13074 if (vm_map_lookup_entry(map, start, &tmp_entry))
13075 start = tmp_entry->vme_end;
13076 entry = tmp_entry;
13077 }
13078 start = vm_map_round_page(start,
13079 VM_MAP_PAGE_MASK(map));
13080 }
13081
13082 /*
13083 * In any case, the "entry" always precedes
13084 * the proposed new region throughout the
13085 * loop:
13086 */
13087
13088 while (TRUE) {
13089 register vm_map_entry_t next;
13090
13091 /*
13092 * Find the end of the proposed new region.
13093 * Be sure we didn't go beyond the end, or
13094 * wrap around the address.
13095 */
13096
13097 end = ((start + mask) & ~mask);
13098 end = vm_map_round_page(end,
13099 VM_MAP_PAGE_MASK(map));
13100 if (end < start)
13101 return(KERN_NO_SPACE);
13102 start = end;
13103 end += size;
13104
13105 if ((end > map->max_offset) || (end < start)) {
13106 if (map->wait_for_space) {
13107 if (size <= (map->max_offset -
13108 map->min_offset)) {
13109 assert_wait((event_t) map, THREAD_INTERRUPTIBLE);
13110 vm_map_unlock(map);
13111 thread_block(THREAD_CONTINUE_NULL);
13112 vm_map_lock(map);
13113 goto StartAgain;
13114 }
13115 }
13116
13117 return(KERN_NO_SPACE);
13118 }
13119
13120 /*
13121 * If there are no more entries, we must win.
13122 */
13123
13124 next = entry->vme_next;
13125 if (next == vm_map_to_entry(map))
13126 break;
13127
13128 /*
13129 * If there is another entry, it must be
13130 * after the end of the potential new region.
13131 */
13132
13133 if (next->vme_start >= end)
13134 break;
13135
13136 /*
13137 * Didn't fit -- move to the next entry.
13138 */
13139
13140 entry = next;
13141 start = entry->vme_end;
13142 }
13143 *address = start;
13144 } else {
13145 vm_map_entry_t temp_entry;
13146
13147 /*
13148 * Verify that:
13149 * the address doesn't itself violate
13150 * the mask requirement.
13151 */
13152
13153 if ((start & mask) != 0)
13154 return(KERN_NO_SPACE);
13155
13156
13157 /*
13158 * ... the address is within bounds
13159 */
13160
13161 end = start + size;
13162
13163 if ((start < map->min_offset) ||
13164 (end > map->max_offset) ||
13165 (start >= end)) {
13166 return(KERN_INVALID_ADDRESS);
13167 }
13168
13169 /*
13170 * If we're asked to overwrite whatever was mapped in that
13171 * range, first deallocate that range.
13172 */
13173 if (flags & VM_FLAGS_OVERWRITE) {
13174 vm_map_t zap_map;
13175
13176 /*
13177 * We use a "zap_map" to avoid having to unlock
13178 * the "map" in vm_map_delete(), which would compromise
13179 * the atomicity of the "deallocate" and then "remap"
13180 * combination.
13181 */
13182 zap_map = vm_map_create(PMAP_NULL,
13183 start,
13184 end,
13185 map->hdr.entries_pageable);
13186 if (zap_map == VM_MAP_NULL) {
13187 return KERN_RESOURCE_SHORTAGE;
13188 }
13189 vm_map_set_page_shift(zap_map, VM_MAP_PAGE_SHIFT(map));
13190
13191 kr = vm_map_delete(map, start, end,
13192 (VM_MAP_REMOVE_SAVE_ENTRIES |
13193 VM_MAP_REMOVE_NO_MAP_ALIGN),
13194 zap_map);
13195 if (kr == KERN_SUCCESS) {
13196 vm_map_destroy(zap_map,
13197 VM_MAP_REMOVE_NO_PMAP_CLEANUP);
13198 zap_map = VM_MAP_NULL;
13199 }
13200 }
13201
13202 /*
13203 * ... the starting address isn't allocated
13204 */
13205
13206 if (vm_map_lookup_entry(map, start, &temp_entry))
13207 return(KERN_NO_SPACE);
13208
13209 entry = temp_entry;
13210
13211 /*
13212 * ... the next region doesn't overlap the
13213 * end point.
13214 */
13215
13216 if ((entry->vme_next != vm_map_to_entry(map)) &&
13217 (entry->vme_next->vme_start < end))
13218 return(KERN_NO_SPACE);
13219 }
13220 *map_entry = entry;
13221 return(KERN_SUCCESS);
13222}
13223
13224/*
13225 * vm_map_switch:
13226 *
13227 * Set the address map for the current thread to the specified map
13228 */
13229
13230vm_map_t
13231vm_map_switch(
13232 vm_map_t map)
13233{
13234 int mycpu;
13235 thread_t thread = current_thread();
13236 vm_map_t oldmap = thread->map;
13237
13238 mp_disable_preemption();
13239 mycpu = cpu_number();
13240
13241 /*
13242 * Deactivate the current map and activate the requested map
13243 */
13244 PMAP_SWITCH_USER(thread, map, mycpu);
13245
13246 mp_enable_preemption();
13247 return(oldmap);
13248}
13249
13250
13251/*
13252 * Routine: vm_map_write_user
13253 *
13254 * Description:
13255 * Copy out data from a kernel space into space in the
13256 * destination map. The space must already exist in the
13257 * destination map.
13258 * NOTE: This routine should only be called by threads
13259 * which can block on a page fault. i.e. kernel mode user
13260 * threads.
13261 *
13262 */
13263kern_return_t
13264vm_map_write_user(
13265 vm_map_t map,
13266 void *src_p,
13267 vm_map_address_t dst_addr,
13268 vm_size_t size)
13269{
13270 kern_return_t kr = KERN_SUCCESS;
13271
13272 if(current_map() == map) {
13273 if (copyout(src_p, dst_addr, size)) {
13274 kr = KERN_INVALID_ADDRESS;
13275 }
13276 } else {
13277 vm_map_t oldmap;
13278
13279 /* take on the identity of the target map while doing */
13280 /* the transfer */
13281
13282 vm_map_reference(map);
13283 oldmap = vm_map_switch(map);
13284 if (copyout(src_p, dst_addr, size)) {
13285 kr = KERN_INVALID_ADDRESS;
13286 }
13287 vm_map_switch(oldmap);
13288 vm_map_deallocate(map);
13289 }
13290 return kr;
13291}
13292
13293/*
13294 * Routine: vm_map_read_user
13295 *
13296 * Description:
13297 * Copy in data from a user space source map into the
13298 * kernel map. The space must already exist in the
13299 * kernel map.
13300 * NOTE: This routine should only be called by threads
13301 * which can block on a page fault. i.e. kernel mode user
13302 * threads.
13303 *
13304 */
13305kern_return_t
13306vm_map_read_user(
13307 vm_map_t map,
13308 vm_map_address_t src_addr,
13309 void *dst_p,
13310 vm_size_t size)
13311{
13312 kern_return_t kr = KERN_SUCCESS;
13313
13314 if(current_map() == map) {
13315 if (copyin(src_addr, dst_p, size)) {
13316 kr = KERN_INVALID_ADDRESS;
13317 }
13318 } else {
13319 vm_map_t oldmap;
13320
13321 /* take on the identity of the target map while doing */
13322 /* the transfer */
13323
13324 vm_map_reference(map);
13325 oldmap = vm_map_switch(map);
13326 if (copyin(src_addr, dst_p, size)) {
13327 kr = KERN_INVALID_ADDRESS;
13328 }
13329 vm_map_switch(oldmap);
13330 vm_map_deallocate(map);
13331 }
13332 return kr;
13333}
13334
13335
13336/*
13337 * vm_map_check_protection:
13338 *
13339 * Assert that the target map allows the specified
13340 * privilege on the entire address region given.
13341 * The entire region must be allocated.
13342 */
13343boolean_t
13344vm_map_check_protection(vm_map_t map, vm_map_offset_t start,
13345 vm_map_offset_t end, vm_prot_t protection)
13346{
13347 vm_map_entry_t entry;
13348 vm_map_entry_t tmp_entry;
13349
13350 vm_map_lock(map);
13351
13352 if (start < vm_map_min(map) || end > vm_map_max(map) || start > end)
13353 {
13354 vm_map_unlock(map);
13355 return (FALSE);
13356 }
13357
13358 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
13359 vm_map_unlock(map);
13360 return(FALSE);
13361 }
13362
13363 entry = tmp_entry;
13364
13365 while (start < end) {
13366 if (entry == vm_map_to_entry(map)) {
13367 vm_map_unlock(map);
13368 return(FALSE);
13369 }
13370
13371 /*
13372 * No holes allowed!
13373 */
13374
13375 if (start < entry->vme_start) {
13376 vm_map_unlock(map);
13377 return(FALSE);
13378 }
13379
13380 /*
13381 * Check protection associated with entry.
13382 */
13383
13384 if ((entry->protection & protection) != protection) {
13385 vm_map_unlock(map);
13386 return(FALSE);
13387 }
13388
13389 /* go to next entry */
13390
13391 start = entry->vme_end;
13392 entry = entry->vme_next;
13393 }
13394 vm_map_unlock(map);
13395 return(TRUE);
13396}
13397
13398kern_return_t
13399vm_map_purgable_control(
13400 vm_map_t map,
13401 vm_map_offset_t address,
13402 vm_purgable_t control,
13403 int *state)
13404{
13405 vm_map_entry_t entry;
13406 vm_object_t object;
13407 kern_return_t kr;
13408 boolean_t was_nonvolatile;
13409
13410 /*
13411 * Vet all the input parameters and current type and state of the
13412 * underlaying object. Return with an error if anything is amiss.
13413 */
13414 if (map == VM_MAP_NULL)
13415 return(KERN_INVALID_ARGUMENT);
13416
13417 if (control != VM_PURGABLE_SET_STATE &&
13418 control != VM_PURGABLE_GET_STATE &&
13419 control != VM_PURGABLE_PURGE_ALL)
13420 return(KERN_INVALID_ARGUMENT);
13421
13422 if (control == VM_PURGABLE_PURGE_ALL) {
13423 vm_purgeable_object_purge_all();
13424 return KERN_SUCCESS;
13425 }
13426
13427 if (control == VM_PURGABLE_SET_STATE &&
13428 (((*state & ~(VM_PURGABLE_ALL_MASKS)) != 0) ||
13429 ((*state & VM_PURGABLE_STATE_MASK) > VM_PURGABLE_STATE_MASK)))
13430 return(KERN_INVALID_ARGUMENT);
13431
13432 vm_map_lock_read(map);
13433
13434 if (!vm_map_lookup_entry(map, address, &entry) || entry->is_sub_map) {
13435
13436 /*
13437 * Must pass a valid non-submap address.
13438 */
13439 vm_map_unlock_read(map);
13440 return(KERN_INVALID_ADDRESS);
13441 }
13442
13443 if ((entry->protection & VM_PROT_WRITE) == 0) {
13444 /*
13445 * Can't apply purgable controls to something you can't write.
13446 */
13447 vm_map_unlock_read(map);
13448 return(KERN_PROTECTION_FAILURE);
13449 }
13450
13451 object = entry->object.vm_object;
13452 if (object == VM_OBJECT_NULL ||
13453 object->purgable == VM_PURGABLE_DENY) {
13454 /*
13455 * Object must already be present and be purgeable.
13456 */
13457 vm_map_unlock_read(map);
13458 return KERN_INVALID_ARGUMENT;
13459 }
13460
13461 vm_object_lock(object);
13462
13463#if 00
13464 if (entry->offset != 0 ||
13465 entry->vme_end - entry->vme_start != object->vo_size) {
13466 /*
13467 * Can only apply purgable controls to the whole (existing)
13468 * object at once.
13469 */
13470 vm_map_unlock_read(map);
13471 vm_object_unlock(object);
13472 return KERN_INVALID_ARGUMENT;
13473 }
13474#endif
13475
13476 assert(!entry->is_sub_map);
13477 assert(!entry->use_pmap); /* purgeable has its own accounting */
13478
13479 vm_map_unlock_read(map);
13480
13481 was_nonvolatile = (object->purgable == VM_PURGABLE_NONVOLATILE);
13482
13483 kr = vm_object_purgable_control(object, control, state);
13484
13485 if (was_nonvolatile &&
13486 object->purgable != VM_PURGABLE_NONVOLATILE &&
13487 map->pmap == kernel_pmap) {
13488#if DEBUG
13489 object->vo_purgeable_volatilizer = kernel_task;
13490#endif /* DEBUG */
13491 }
13492
13493 vm_object_unlock(object);
13494
13495 return kr;
13496}
13497
13498kern_return_t
13499vm_map_page_query_internal(
13500 vm_map_t target_map,
13501 vm_map_offset_t offset,
13502 int *disposition,
13503 int *ref_count)
13504{
13505 kern_return_t kr;
13506 vm_page_info_basic_data_t info;
13507 mach_msg_type_number_t count;
13508
13509 count = VM_PAGE_INFO_BASIC_COUNT;
13510 kr = vm_map_page_info(target_map,
13511 offset,
13512 VM_PAGE_INFO_BASIC,
13513 (vm_page_info_t) &info,
13514 &count);
13515 if (kr == KERN_SUCCESS) {
13516 *disposition = info.disposition;
13517 *ref_count = info.ref_count;
13518 } else {
13519 *disposition = 0;
13520 *ref_count = 0;
13521 }
13522
13523 return kr;
13524}
13525
13526kern_return_t
13527vm_map_page_info(
13528 vm_map_t map,
13529 vm_map_offset_t offset,
13530 vm_page_info_flavor_t flavor,
13531 vm_page_info_t info,
13532 mach_msg_type_number_t *count)
13533{
13534 vm_map_entry_t map_entry;
13535 vm_object_t object;
13536 vm_page_t m;
13537 kern_return_t kr;
13538 kern_return_t retval = KERN_SUCCESS;
13539 boolean_t top_object;
13540 int disposition;
13541 int ref_count;
13542 vm_page_info_basic_t basic_info;
13543 int depth;
13544 vm_map_offset_t offset_in_page;
13545
13546 switch (flavor) {
13547 case VM_PAGE_INFO_BASIC:
13548 if (*count != VM_PAGE_INFO_BASIC_COUNT) {
13549 /*
13550 * The "vm_page_info_basic_data" structure was not
13551 * properly padded, so allow the size to be off by
13552 * one to maintain backwards binary compatibility...
13553 */
13554 if (*count != VM_PAGE_INFO_BASIC_COUNT - 1)
13555 return KERN_INVALID_ARGUMENT;
13556 }
13557 break;
13558 default:
13559 return KERN_INVALID_ARGUMENT;
13560 }
13561
13562 disposition = 0;
13563 ref_count = 0;
13564 top_object = TRUE;
13565 depth = 0;
13566
13567 retval = KERN_SUCCESS;
13568 offset_in_page = offset & PAGE_MASK;
13569 offset = vm_map_trunc_page(offset, PAGE_MASK);
13570
13571 vm_map_lock_read(map);
13572
13573 /*
13574 * First, find the map entry covering "offset", going down
13575 * submaps if necessary.
13576 */
13577 for (;;) {
13578 if (!vm_map_lookup_entry(map, offset, &map_entry)) {
13579 vm_map_unlock_read(map);
13580 return KERN_INVALID_ADDRESS;
13581 }
13582 /* compute offset from this map entry's start */
13583 offset -= map_entry->vme_start;
13584 /* compute offset into this map entry's object (or submap) */
13585 offset += map_entry->offset;
13586
13587 if (map_entry->is_sub_map) {
13588 vm_map_t sub_map;
13589
13590 sub_map = map_entry->object.sub_map;
13591 vm_map_lock_read(sub_map);
13592 vm_map_unlock_read(map);
13593
13594 map = sub_map;
13595
13596 ref_count = MAX(ref_count, map->ref_count);
13597 continue;
13598 }
13599 break;
13600 }
13601
13602 object = map_entry->object.vm_object;
13603 if (object == VM_OBJECT_NULL) {
13604 /* no object -> no page */
13605 vm_map_unlock_read(map);
13606 goto done;
13607 }
13608
13609 vm_object_lock(object);
13610 vm_map_unlock_read(map);
13611
13612 /*
13613 * Go down the VM object shadow chain until we find the page
13614 * we're looking for.
13615 */
13616 for (;;) {
13617 ref_count = MAX(ref_count, object->ref_count);
13618
13619 m = vm_page_lookup(object, offset);
13620
13621 if (m != VM_PAGE_NULL) {
13622 disposition |= VM_PAGE_QUERY_PAGE_PRESENT;
13623 break;
13624 } else {
13625#if MACH_PAGEMAP
13626 if (object->existence_map) {
13627 if (vm_external_state_get(object->existence_map,
13628 offset) ==
13629 VM_EXTERNAL_STATE_EXISTS) {
13630 /*
13631 * this page has been paged out
13632 */
13633 disposition |= VM_PAGE_QUERY_PAGE_PAGED_OUT;
13634 break;
13635 }
13636 } else
13637#endif
13638 if (object->internal &&
13639 object->alive &&
13640 !object->terminating &&
13641 object->pager_ready) {
13642
13643 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
13644 if (VM_COMPRESSOR_PAGER_STATE_GET(
13645 object,
13646 offset)
13647 == VM_EXTERNAL_STATE_EXISTS) {
13648 /* the pager has that page */
13649 disposition |= VM_PAGE_QUERY_PAGE_PAGED_OUT;
13650 break;
13651 }
13652 } else {
13653 memory_object_t pager;
13654
13655 vm_object_paging_begin(object);
13656 pager = object->pager;
13657 vm_object_unlock(object);
13658
13659 /*
13660 * Ask the default pager if
13661 * it has this page.
13662 */
13663 kr = memory_object_data_request(
13664 pager,
13665 offset + object->paging_offset,
13666 0, /* just poke the pager */
13667 VM_PROT_READ,
13668 NULL);
13669
13670 vm_object_lock(object);
13671 vm_object_paging_end(object);
13672
13673 if (kr == KERN_SUCCESS) {
13674 /* the default pager has it */
13675 disposition |= VM_PAGE_QUERY_PAGE_PAGED_OUT;
13676 break;
13677 }
13678 }
13679 }
13680
13681 if (object->shadow != VM_OBJECT_NULL) {
13682 vm_object_t shadow;
13683
13684 offset += object->vo_shadow_offset;
13685 shadow = object->shadow;
13686
13687 vm_object_lock(shadow);
13688 vm_object_unlock(object);
13689
13690 object = shadow;
13691 top_object = FALSE;
13692 depth++;
13693 } else {
13694// if (!object->internal)
13695// break;
13696// retval = KERN_FAILURE;
13697// goto done_with_object;
13698 break;
13699 }
13700 }
13701 }
13702 /* The ref_count is not strictly accurate, it measures the number */
13703 /* of entities holding a ref on the object, they may not be mapping */
13704 /* the object or may not be mapping the section holding the */
13705 /* target page but its still a ball park number and though an over- */
13706 /* count, it picks up the copy-on-write cases */
13707
13708 /* We could also get a picture of page sharing from pmap_attributes */
13709 /* but this would under count as only faulted-in mappings would */
13710 /* show up. */
13711
13712 if (top_object == TRUE && object->shadow)
13713 disposition |= VM_PAGE_QUERY_PAGE_COPIED;
13714
13715 if (! object->internal)
13716 disposition |= VM_PAGE_QUERY_PAGE_EXTERNAL;
13717
13718 if (m == VM_PAGE_NULL)
13719 goto done_with_object;
13720
13721 if (m->fictitious) {
13722 disposition |= VM_PAGE_QUERY_PAGE_FICTITIOUS;
13723 goto done_with_object;
13724 }
13725 if (m->dirty || pmap_is_modified(m->phys_page))
13726 disposition |= VM_PAGE_QUERY_PAGE_DIRTY;
13727
13728 if (m->reference || pmap_is_referenced(m->phys_page))
13729 disposition |= VM_PAGE_QUERY_PAGE_REF;
13730
13731 if (m->speculative)
13732 disposition |= VM_PAGE_QUERY_PAGE_SPECULATIVE;
13733
13734 if (m->cs_validated)
13735 disposition |= VM_PAGE_QUERY_PAGE_CS_VALIDATED;
13736 if (m->cs_tainted)
13737 disposition |= VM_PAGE_QUERY_PAGE_CS_TAINTED;
13738 if (m->cs_nx)
13739 disposition |= VM_PAGE_QUERY_PAGE_CS_NX;
13740
13741done_with_object:
13742 vm_object_unlock(object);
13743done:
13744
13745 switch (flavor) {
13746 case VM_PAGE_INFO_BASIC:
13747 basic_info = (vm_page_info_basic_t) info;
13748 basic_info->disposition = disposition;
13749 basic_info->ref_count = ref_count;
13750 basic_info->object_id = (vm_object_id_t) (uintptr_t)
13751 VM_KERNEL_ADDRPERM(object);
13752 basic_info->offset =
13753 (memory_object_offset_t) offset + offset_in_page;
13754 basic_info->depth = depth;
13755 break;
13756 }
13757
13758 return retval;
13759}
13760
13761/*
13762 * vm_map_msync
13763 *
13764 * Synchronises the memory range specified with its backing store
13765 * image by either flushing or cleaning the contents to the appropriate
13766 * memory manager engaging in a memory object synchronize dialog with
13767 * the manager. The client doesn't return until the manager issues
13768 * m_o_s_completed message. MIG Magically converts user task parameter
13769 * to the task's address map.
13770 *
13771 * interpretation of sync_flags
13772 * VM_SYNC_INVALIDATE - discard pages, only return precious
13773 * pages to manager.
13774 *
13775 * VM_SYNC_INVALIDATE & (VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS)
13776 * - discard pages, write dirty or precious
13777 * pages back to memory manager.
13778 *
13779 * VM_SYNC_SYNCHRONOUS | VM_SYNC_ASYNCHRONOUS
13780 * - write dirty or precious pages back to
13781 * the memory manager.
13782 *
13783 * VM_SYNC_CONTIGUOUS - does everything normally, but if there
13784 * is a hole in the region, and we would
13785 * have returned KERN_SUCCESS, return
13786 * KERN_INVALID_ADDRESS instead.
13787 *
13788 * NOTE
13789 * The memory object attributes have not yet been implemented, this
13790 * function will have to deal with the invalidate attribute
13791 *
13792 * RETURNS
13793 * KERN_INVALID_TASK Bad task parameter
13794 * KERN_INVALID_ARGUMENT both sync and async were specified.
13795 * KERN_SUCCESS The usual.
13796 * KERN_INVALID_ADDRESS There was a hole in the region.
13797 */
13798
13799kern_return_t
13800vm_map_msync(
13801 vm_map_t map,
13802 vm_map_address_t address,
13803 vm_map_size_t size,
13804 vm_sync_t sync_flags)
13805{
13806 msync_req_t msr;
13807 msync_req_t new_msr;
13808 queue_chain_t req_q; /* queue of requests for this msync */
13809 vm_map_entry_t entry;
13810 vm_map_size_t amount_left;
13811 vm_object_offset_t offset;
13812 boolean_t do_sync_req;
13813 boolean_t had_hole = FALSE;
13814 memory_object_t pager;
13815
13816 if ((sync_flags & VM_SYNC_ASYNCHRONOUS) &&
13817 (sync_flags & VM_SYNC_SYNCHRONOUS))
13818 return(KERN_INVALID_ARGUMENT);
13819
13820 /*
13821 * align address and size on page boundaries
13822 */
13823 size = (vm_map_round_page(address + size,
13824 VM_MAP_PAGE_MASK(map)) -
13825 vm_map_trunc_page(address,
13826 VM_MAP_PAGE_MASK(map)));
13827 address = vm_map_trunc_page(address,
13828 VM_MAP_PAGE_MASK(map));
13829
13830 if (map == VM_MAP_NULL)
13831 return(KERN_INVALID_TASK);
13832
13833 if (size == 0)
13834 return(KERN_SUCCESS);
13835
13836 queue_init(&req_q);
13837 amount_left = size;
13838
13839 while (amount_left > 0) {
13840 vm_object_size_t flush_size;
13841 vm_object_t object;
13842
13843 vm_map_lock(map);
13844 if (!vm_map_lookup_entry(map,
13845 vm_map_trunc_page(
13846 address,
13847 VM_MAP_PAGE_MASK(map)),
13848 &entry)) {
13849
13850 vm_map_size_t skip;
13851
13852 /*
13853 * hole in the address map.
13854 */
13855 had_hole = TRUE;
13856
13857 /*
13858 * Check for empty map.
13859 */
13860 if (entry == vm_map_to_entry(map) &&
13861 entry->vme_next == entry) {
13862 vm_map_unlock(map);
13863 break;
13864 }
13865 /*
13866 * Check that we don't wrap and that
13867 * we have at least one real map entry.
13868 */
13869 if ((map->hdr.nentries == 0) ||
13870 (entry->vme_next->vme_start < address)) {
13871 vm_map_unlock(map);
13872 break;
13873 }
13874 /*
13875 * Move up to the next entry if needed
13876 */
13877 skip = (entry->vme_next->vme_start - address);
13878 if (skip >= amount_left)
13879 amount_left = 0;
13880 else
13881 amount_left -= skip;
13882 address = entry->vme_next->vme_start;
13883 vm_map_unlock(map);
13884 continue;
13885 }
13886
13887 offset = address - entry->vme_start;
13888
13889 /*
13890 * do we have more to flush than is contained in this
13891 * entry ?
13892 */
13893 if (amount_left + entry->vme_start + offset > entry->vme_end) {
13894 flush_size = entry->vme_end -
13895 (entry->vme_start + offset);
13896 } else {
13897 flush_size = amount_left;
13898 }
13899 amount_left -= flush_size;
13900 address += flush_size;
13901
13902 if (entry->is_sub_map == TRUE) {
13903 vm_map_t local_map;
13904 vm_map_offset_t local_offset;
13905
13906 local_map = entry->object.sub_map;
13907 local_offset = entry->offset;
13908 vm_map_unlock(map);
13909 if (vm_map_msync(
13910 local_map,
13911 local_offset,
13912 flush_size,
13913 sync_flags) == KERN_INVALID_ADDRESS) {
13914 had_hole = TRUE;
13915 }
13916 continue;
13917 }
13918 object = entry->object.vm_object;
13919
13920 /*
13921 * We can't sync this object if the object has not been
13922 * created yet
13923 */
13924 if (object == VM_OBJECT_NULL) {
13925 vm_map_unlock(map);
13926 continue;
13927 }
13928 offset += entry->offset;
13929
13930 vm_object_lock(object);
13931
13932 if (sync_flags & (VM_SYNC_KILLPAGES | VM_SYNC_DEACTIVATE)) {
13933 int kill_pages = 0;
13934 boolean_t reusable_pages = FALSE;
13935
13936 if (sync_flags & VM_SYNC_KILLPAGES) {
13937 if (object->ref_count == 1 && !object->shadow)
13938 kill_pages = 1;
13939 else
13940 kill_pages = -1;
13941 }
13942 if (kill_pages != -1)
13943 vm_object_deactivate_pages(object, offset,
13944 (vm_object_size_t)flush_size, kill_pages, reusable_pages);
13945 vm_object_unlock(object);
13946 vm_map_unlock(map);
13947 continue;
13948 }
13949 /*
13950 * We can't sync this object if there isn't a pager.
13951 * Don't bother to sync internal objects, since there can't
13952 * be any "permanent" storage for these objects anyway.
13953 */
13954 if ((object->pager == MEMORY_OBJECT_NULL) ||
13955 (object->internal) || (object->private)) {
13956 vm_object_unlock(object);
13957 vm_map_unlock(map);
13958 continue;
13959 }
13960 /*
13961 * keep reference on the object until syncing is done
13962 */
13963 vm_object_reference_locked(object);
13964 vm_object_unlock(object);
13965
13966 vm_map_unlock(map);
13967
13968 do_sync_req = vm_object_sync(object,
13969 offset,
13970 flush_size,
13971 sync_flags & VM_SYNC_INVALIDATE,
13972 ((sync_flags & VM_SYNC_SYNCHRONOUS) ||
13973 (sync_flags & VM_SYNC_ASYNCHRONOUS)),
13974 sync_flags & VM_SYNC_SYNCHRONOUS);
13975 /*
13976 * only send a m_o_s if we returned pages or if the entry
13977 * is writable (ie dirty pages may have already been sent back)
13978 */
13979 if (!do_sync_req) {
13980 if ((sync_flags & VM_SYNC_INVALIDATE) && object->resident_page_count == 0) {
13981 /*
13982 * clear out the clustering and read-ahead hints
13983 */
13984 vm_object_lock(object);
13985
13986 object->pages_created = 0;
13987 object->pages_used = 0;
13988 object->sequential = 0;
13989 object->last_alloc = 0;
13990
13991 vm_object_unlock(object);
13992 }
13993 vm_object_deallocate(object);
13994 continue;
13995 }
13996 msync_req_alloc(new_msr);
13997
13998 vm_object_lock(object);
13999 offset += object->paging_offset;
14000
14001 new_msr->offset = offset;
14002 new_msr->length = flush_size;
14003 new_msr->object = object;
14004 new_msr->flag = VM_MSYNC_SYNCHRONIZING;
14005 re_iterate:
14006
14007 /*
14008 * We can't sync this object if there isn't a pager. The
14009 * pager can disappear anytime we're not holding the object
14010 * lock. So this has to be checked anytime we goto re_iterate.
14011 */
14012
14013 pager = object->pager;
14014
14015 if (pager == MEMORY_OBJECT_NULL) {
14016 vm_object_unlock(object);
14017 vm_object_deallocate(object);
14018 msync_req_free(new_msr);
14019 new_msr = NULL;
14020 continue;
14021 }
14022
14023 queue_iterate(&object->msr_q, msr, msync_req_t, msr_q) {
14024 /*
14025 * need to check for overlapping entry, if found, wait
14026 * on overlapping msr to be done, then reiterate
14027 */
14028 msr_lock(msr);
14029 if (msr->flag == VM_MSYNC_SYNCHRONIZING &&
14030 ((offset >= msr->offset &&
14031 offset < (msr->offset + msr->length)) ||
14032 (msr->offset >= offset &&
14033 msr->offset < (offset + flush_size))))
14034 {
14035 assert_wait((event_t) msr,THREAD_INTERRUPTIBLE);
14036 msr_unlock(msr);
14037 vm_object_unlock(object);
14038 thread_block(THREAD_CONTINUE_NULL);
14039 vm_object_lock(object);
14040 goto re_iterate;
14041 }
14042 msr_unlock(msr);
14043 }/* queue_iterate */
14044
14045 queue_enter(&object->msr_q, new_msr, msync_req_t, msr_q);
14046
14047 vm_object_paging_begin(object);
14048 vm_object_unlock(object);
14049
14050 queue_enter(&req_q, new_msr, msync_req_t, req_q);
14051
14052 (void) memory_object_synchronize(
14053 pager,
14054 offset,
14055 flush_size,
14056 sync_flags & ~VM_SYNC_CONTIGUOUS);
14057
14058 vm_object_lock(object);
14059 vm_object_paging_end(object);
14060 vm_object_unlock(object);
14061 }/* while */
14062
14063 /*
14064 * wait for memory_object_sychronize_completed messages from pager(s)
14065 */
14066
14067 while (!queue_empty(&req_q)) {
14068 msr = (msync_req_t)queue_first(&req_q);
14069 msr_lock(msr);
14070 while(msr->flag != VM_MSYNC_DONE) {
14071 assert_wait((event_t) msr, THREAD_INTERRUPTIBLE);
14072 msr_unlock(msr);
14073 thread_block(THREAD_CONTINUE_NULL);
14074 msr_lock(msr);
14075 }/* while */
14076 queue_remove(&req_q, msr, msync_req_t, req_q);
14077 msr_unlock(msr);
14078 vm_object_deallocate(msr->object);
14079 msync_req_free(msr);
14080 }/* queue_iterate */
14081
14082 /* for proper msync() behaviour */
14083 if (had_hole == TRUE && (sync_flags & VM_SYNC_CONTIGUOUS))
14084 return(KERN_INVALID_ADDRESS);
14085
14086 return(KERN_SUCCESS);
14087}/* vm_msync */
14088
14089/*
14090 * Routine: convert_port_entry_to_map
14091 * Purpose:
14092 * Convert from a port specifying an entry or a task
14093 * to a map. Doesn't consume the port ref; produces a map ref,
14094 * which may be null. Unlike convert_port_to_map, the
14095 * port may be task or a named entry backed.
14096 * Conditions:
14097 * Nothing locked.
14098 */
14099
14100
14101vm_map_t
14102convert_port_entry_to_map(
14103 ipc_port_t port)
14104{
14105 vm_map_t map;
14106 vm_named_entry_t named_entry;
14107 uint32_t try_failed_count = 0;
14108
14109 if(IP_VALID(port) && (ip_kotype(port) == IKOT_NAMED_ENTRY)) {
14110 while(TRUE) {
14111 ip_lock(port);
14112 if(ip_active(port) && (ip_kotype(port)
14113 == IKOT_NAMED_ENTRY)) {
14114 named_entry =
14115 (vm_named_entry_t)port->ip_kobject;
14116 if (!(lck_mtx_try_lock(&(named_entry)->Lock))) {
14117 ip_unlock(port);
14118
14119 try_failed_count++;
14120 mutex_pause(try_failed_count);
14121 continue;
14122 }
14123 named_entry->ref_count++;
14124 lck_mtx_unlock(&(named_entry)->Lock);
14125 ip_unlock(port);
14126 if ((named_entry->is_sub_map) &&
14127 (named_entry->protection
14128 & VM_PROT_WRITE)) {
14129 map = named_entry->backing.map;
14130 } else {
14131 mach_destroy_memory_entry(port);
14132 return VM_MAP_NULL;
14133 }
14134 vm_map_reference_swap(map);
14135 mach_destroy_memory_entry(port);
14136 break;
14137 }
14138 else
14139 return VM_MAP_NULL;
14140 }
14141 }
14142 else
14143 map = convert_port_to_map(port);
14144
14145 return map;
14146}
14147
14148/*
14149 * Routine: convert_port_entry_to_object
14150 * Purpose:
14151 * Convert from a port specifying a named entry to an
14152 * object. Doesn't consume the port ref; produces a map ref,
14153 * which may be null.
14154 * Conditions:
14155 * Nothing locked.
14156 */
14157
14158
14159vm_object_t
14160convert_port_entry_to_object(
14161 ipc_port_t port)
14162{
14163 vm_object_t object = VM_OBJECT_NULL;
14164 vm_named_entry_t named_entry;
14165 uint32_t try_failed_count = 0;
14166
14167 if (IP_VALID(port) &&
14168 (ip_kotype(port) == IKOT_NAMED_ENTRY)) {
14169 try_again:
14170 ip_lock(port);
14171 if (ip_active(port) &&
14172 (ip_kotype(port) == IKOT_NAMED_ENTRY)) {
14173 named_entry = (vm_named_entry_t)port->ip_kobject;
14174 if (!(lck_mtx_try_lock(&(named_entry)->Lock))) {
14175 ip_unlock(port);
14176 try_failed_count++;
14177 mutex_pause(try_failed_count);
14178 goto try_again;
14179 }
14180 named_entry->ref_count++;
14181 lck_mtx_unlock(&(named_entry)->Lock);
14182 ip_unlock(port);
14183 if (!(named_entry->is_sub_map) &&
14184 !(named_entry->is_pager) &&
14185 !(named_entry->is_copy) &&
14186 (named_entry->protection & VM_PROT_WRITE)) {
14187 object = named_entry->backing.object;
14188 vm_object_reference(object);
14189 }
14190 mach_destroy_memory_entry(port);
14191 }
14192 }
14193
14194 return object;
14195}
14196
14197/*
14198 * Export routines to other components for the things we access locally through
14199 * macros.
14200 */
14201#undef current_map
14202vm_map_t
14203current_map(void)
14204{
14205 return (current_map_fast());
14206}
14207
14208/*
14209 * vm_map_reference:
14210 *
14211 * Most code internal to the osfmk will go through a
14212 * macro defining this. This is always here for the
14213 * use of other kernel components.
14214 */
14215#undef vm_map_reference
14216void
14217vm_map_reference(
14218 register vm_map_t map)
14219{
14220 if (map == VM_MAP_NULL)
14221 return;
14222
14223 lck_mtx_lock(&map->s_lock);
14224#if TASK_SWAPPER
14225 assert(map->res_count > 0);
14226 assert(map->ref_count >= map->res_count);
14227 map->res_count++;
14228#endif
14229 map->ref_count++;
14230 lck_mtx_unlock(&map->s_lock);
14231}
14232
14233/*
14234 * vm_map_deallocate:
14235 *
14236 * Removes a reference from the specified map,
14237 * destroying it if no references remain.
14238 * The map should not be locked.
14239 */
14240void
14241vm_map_deallocate(
14242 register vm_map_t map)
14243{
14244 unsigned int ref;
14245
14246 if (map == VM_MAP_NULL)
14247 return;
14248
14249 lck_mtx_lock(&map->s_lock);
14250 ref = --map->ref_count;
14251 if (ref > 0) {
14252 vm_map_res_deallocate(map);
14253 lck_mtx_unlock(&map->s_lock);
14254 return;
14255 }
14256 assert(map->ref_count == 0);
14257 lck_mtx_unlock(&map->s_lock);
14258
14259#if TASK_SWAPPER
14260 /*
14261 * The map residence count isn't decremented here because
14262 * the vm_map_delete below will traverse the entire map,
14263 * deleting entries, and the residence counts on objects
14264 * and sharing maps will go away then.
14265 */
14266#endif
14267
14268 vm_map_destroy(map, VM_MAP_NO_FLAGS);
14269}
14270
14271
14272void
14273vm_map_disable_NX(vm_map_t map)
14274{
14275 if (map == NULL)
14276 return;
14277 if (map->pmap == NULL)
14278 return;
14279
14280 pmap_disable_NX(map->pmap);
14281}
14282
14283void
14284vm_map_disallow_data_exec(vm_map_t map)
14285{
14286 if (map == NULL)
14287 return;
14288
14289 map->map_disallow_data_exec = TRUE;
14290}
14291
14292/* XXX Consider making these constants (VM_MAX_ADDRESS and MACH_VM_MAX_ADDRESS)
14293 * more descriptive.
14294 */
14295void
14296vm_map_set_32bit(vm_map_t map)
14297{
14298 map->max_offset = (vm_map_offset_t)VM_MAX_ADDRESS;
14299}
14300
14301
14302void
14303vm_map_set_64bit(vm_map_t map)
14304{
14305 map->max_offset = (vm_map_offset_t)MACH_VM_MAX_ADDRESS;
14306}
14307
14308vm_map_offset_t
14309vm_compute_max_offset(unsigned is64)
14310{
14311 return (is64 ? (vm_map_offset_t)MACH_VM_MAX_ADDRESS : (vm_map_offset_t)VM_MAX_ADDRESS);
14312}
14313
14314uint64_t
14315vm_map_get_max_aslr_slide_pages(vm_map_t map)
14316{
14317 return (1 << (vm_map_is_64bit(map) ? 16 : 8));
14318}
14319
14320boolean_t
14321vm_map_is_64bit(
14322 vm_map_t map)
14323{
14324 return map->max_offset > ((vm_map_offset_t)VM_MAX_ADDRESS);
14325}
14326
14327boolean_t
14328vm_map_has_hard_pagezero(
14329 vm_map_t map,
14330 vm_map_offset_t pagezero_size)
14331{
14332 /*
14333 * XXX FBDP
14334 * We should lock the VM map (for read) here but we can get away
14335 * with it for now because there can't really be any race condition:
14336 * the VM map's min_offset is changed only when the VM map is created
14337 * and when the zero page is established (when the binary gets loaded),
14338 * and this routine gets called only when the task terminates and the
14339 * VM map is being torn down, and when a new map is created via
14340 * load_machfile()/execve().
14341 */
14342 return (map->min_offset >= pagezero_size);
14343}
14344
14345/*
14346 * Raise a VM map's maximun offset.
14347 */
14348kern_return_t
14349vm_map_raise_max_offset(
14350 vm_map_t map,
14351 vm_map_offset_t new_max_offset)
14352{
14353 kern_return_t ret;
14354
14355 vm_map_lock(map);
14356 ret = KERN_INVALID_ADDRESS;
14357
14358 if (new_max_offset >= map->max_offset) {
14359 if (!vm_map_is_64bit(map)) {
14360 if (new_max_offset <= (vm_map_offset_t)VM_MAX_ADDRESS) {
14361 map->max_offset = new_max_offset;
14362 ret = KERN_SUCCESS;
14363 }
14364 } else {
14365 if (new_max_offset <= (vm_map_offset_t)MACH_VM_MAX_ADDRESS) {
14366 map->max_offset = new_max_offset;
14367 ret = KERN_SUCCESS;
14368 }
14369 }
14370 }
14371
14372 vm_map_unlock(map);
14373 return ret;
14374}
14375
14376
14377/*
14378 * Raise a VM map's minimum offset.
14379 * To strictly enforce "page zero" reservation.
14380 */
14381kern_return_t
14382vm_map_raise_min_offset(
14383 vm_map_t map,
14384 vm_map_offset_t new_min_offset)
14385{
14386 vm_map_entry_t first_entry;
14387
14388 new_min_offset = vm_map_round_page(new_min_offset,
14389 VM_MAP_PAGE_MASK(map));
14390
14391 vm_map_lock(map);
14392
14393 if (new_min_offset < map->min_offset) {
14394 /*
14395 * Can't move min_offset backwards, as that would expose
14396 * a part of the address space that was previously, and for
14397 * possibly good reasons, inaccessible.
14398 */
14399 vm_map_unlock(map);
14400 return KERN_INVALID_ADDRESS;
14401 }
14402
14403 first_entry = vm_map_first_entry(map);
14404 if (first_entry != vm_map_to_entry(map) &&
14405 first_entry->vme_start < new_min_offset) {
14406 /*
14407 * Some memory was already allocated below the new
14408 * minimun offset. It's too late to change it now...
14409 */
14410 vm_map_unlock(map);
14411 return KERN_NO_SPACE;
14412 }
14413
14414 map->min_offset = new_min_offset;
14415
14416 vm_map_unlock(map);
14417
14418 return KERN_SUCCESS;
14419}
14420
14421/*
14422 * Set the limit on the maximum amount of user wired memory allowed for this map.
14423 * This is basically a copy of the MEMLOCK rlimit value maintained by the BSD side of
14424 * the kernel. The limits are checked in the mach VM side, so we keep a copy so we
14425 * don't have to reach over to the BSD data structures.
14426 */
14427
14428void
14429vm_map_set_user_wire_limit(vm_map_t map,
14430 vm_size_t limit)
14431{
14432 map->user_wire_limit = limit;
14433}
14434
14435
14436void vm_map_switch_protect(vm_map_t map,
14437 boolean_t val)
14438{
14439 vm_map_lock(map);
14440 map->switch_protect=val;
14441 vm_map_unlock(map);
14442}
14443
14444/*
14445 * IOKit has mapped a region into this map; adjust the pmap's ledgers appropriately.
14446 * phys_footprint is a composite limit consisting of iokit + physmem, so we need to
14447 * bump both counters.
14448 */
14449void
14450vm_map_iokit_mapped_region(vm_map_t map, vm_size_t bytes)
14451{
14452 pmap_t pmap = vm_map_pmap(map);
14453
14454 ledger_credit(pmap->ledger, task_ledgers.iokit_mapped, bytes);
14455 ledger_credit(pmap->ledger, task_ledgers.phys_footprint, bytes);
14456}
14457
14458void
14459vm_map_iokit_unmapped_region(vm_map_t map, vm_size_t bytes)
14460{
14461 pmap_t pmap = vm_map_pmap(map);
14462
14463 ledger_debit(pmap->ledger, task_ledgers.iokit_mapped, bytes);
14464 ledger_debit(pmap->ledger, task_ledgers.phys_footprint, bytes);
14465}
14466
14467/* Add (generate) code signature for memory range */
14468#if CONFIG_DYNAMIC_CODE_SIGNING
14469kern_return_t vm_map_sign(vm_map_t map,
14470 vm_map_offset_t start,
14471 vm_map_offset_t end)
14472{
14473 vm_map_entry_t entry;
14474 vm_page_t m;
14475 vm_object_t object;
14476
14477 /*
14478 * Vet all the input parameters and current type and state of the
14479 * underlaying object. Return with an error if anything is amiss.
14480 */
14481 if (map == VM_MAP_NULL)
14482 return(KERN_INVALID_ARGUMENT);
14483
14484 vm_map_lock_read(map);
14485
14486 if (!vm_map_lookup_entry(map, start, &entry) || entry->is_sub_map) {
14487 /*
14488 * Must pass a valid non-submap address.
14489 */
14490 vm_map_unlock_read(map);
14491 return(KERN_INVALID_ADDRESS);
14492 }
14493
14494 if((entry->vme_start > start) || (entry->vme_end < end)) {
14495 /*
14496 * Map entry doesn't cover the requested range. Not handling
14497 * this situation currently.
14498 */
14499 vm_map_unlock_read(map);
14500 return(KERN_INVALID_ARGUMENT);
14501 }
14502
14503 object = entry->object.vm_object;
14504 if (object == VM_OBJECT_NULL) {
14505 /*
14506 * Object must already be present or we can't sign.
14507 */
14508 vm_map_unlock_read(map);
14509 return KERN_INVALID_ARGUMENT;
14510 }
14511
14512 vm_object_lock(object);
14513 vm_map_unlock_read(map);
14514
14515 while(start < end) {
14516 uint32_t refmod;
14517
14518 m = vm_page_lookup(object, start - entry->vme_start + entry->offset );
14519 if (m==VM_PAGE_NULL) {
14520 /* shoud we try to fault a page here? we can probably
14521 * demand it exists and is locked for this request */
14522 vm_object_unlock(object);
14523 return KERN_FAILURE;
14524 }
14525 /* deal with special page status */
14526 if (m->busy ||
14527 (m->unusual && (m->error || m->restart || m->private || m->absent))) {
14528 vm_object_unlock(object);
14529 return KERN_FAILURE;
14530 }
14531
14532 /* Page is OK... now "validate" it */
14533 /* This is the place where we'll call out to create a code
14534 * directory, later */
14535 m->cs_validated = TRUE;
14536
14537 /* The page is now "clean" for codesigning purposes. That means
14538 * we don't consider it as modified (wpmapped) anymore. But
14539 * we'll disconnect the page so we note any future modification
14540 * attempts. */
14541 m->wpmapped = FALSE;
14542 refmod = pmap_disconnect(m->phys_page);
14543
14544 /* Pull the dirty status from the pmap, since we cleared the
14545 * wpmapped bit */
14546 if ((refmod & VM_MEM_MODIFIED) && !m->dirty) {
14547 SET_PAGE_DIRTY(m, FALSE);
14548 }
14549
14550 /* On to the next page */
14551 start += PAGE_SIZE;
14552 }
14553 vm_object_unlock(object);
14554
14555 return KERN_SUCCESS;
14556}
14557#endif
14558
14559kern_return_t vm_map_partial_reap(vm_map_t map, unsigned int *reclaimed_resident, unsigned int *reclaimed_compressed)
14560{
14561 vm_map_entry_t entry = VM_MAP_ENTRY_NULL;
14562 vm_map_entry_t next_entry;
14563 kern_return_t kr = KERN_SUCCESS;
14564 vm_map_t zap_map;
14565
14566 vm_map_lock(map);
14567
14568 /*
14569 * We use a "zap_map" to avoid having to unlock
14570 * the "map" in vm_map_delete().
14571 */
14572 zap_map = vm_map_create(PMAP_NULL,
14573 map->min_offset,
14574 map->max_offset,
14575 map->hdr.entries_pageable);
14576
14577 if (zap_map == VM_MAP_NULL) {
14578 return KERN_RESOURCE_SHORTAGE;
14579 }
14580
14581 vm_map_set_page_shift(zap_map,
14582 VM_MAP_PAGE_SHIFT(map));
14583
14584 for (entry = vm_map_first_entry(map);
14585 entry != vm_map_to_entry(map);
14586 entry = next_entry) {
14587 next_entry = entry->vme_next;
14588
14589 if (entry->object.vm_object && !entry->is_sub_map && (entry->object.vm_object->internal == TRUE)
14590 && (entry->object.vm_object->ref_count == 1)) {
14591
14592 *reclaimed_resident += entry->object.vm_object->resident_page_count;
14593 *reclaimed_compressed += vm_compressor_pager_get_count(entry->object.vm_object->pager);
14594
14595 (void)vm_map_delete(map,
14596 entry->vme_start,
14597 entry->vme_end,
14598 VM_MAP_REMOVE_SAVE_ENTRIES,
14599 zap_map);
14600 }
14601 }
14602
14603 vm_map_unlock(map);
14604
14605 /*
14606 * Get rid of the "zap_maps" and all the map entries that
14607 * they may still contain.
14608 */
14609 if (zap_map != VM_MAP_NULL) {
14610 vm_map_destroy(zap_map, VM_MAP_REMOVE_NO_PMAP_CLEANUP);
14611 zap_map = VM_MAP_NULL;
14612 }
14613
14614 return kr;
14615}
14616
14617#if CONFIG_FREEZE
14618
14619kern_return_t vm_map_freeze_walk(
14620 vm_map_t map,
14621 unsigned int *purgeable_count,
14622 unsigned int *wired_count,
14623 unsigned int *clean_count,
14624 unsigned int *dirty_count,
14625 unsigned int dirty_budget,
14626 boolean_t *has_shared)
14627{
14628 vm_map_entry_t entry;
14629
14630 vm_map_lock_read(map);
14631
14632 *purgeable_count = *wired_count = *clean_count = *dirty_count = 0;
14633 *has_shared = FALSE;
14634
14635 for (entry = vm_map_first_entry(map);
14636 entry != vm_map_to_entry(map);
14637 entry = entry->vme_next) {
14638 unsigned int purgeable, clean, dirty, wired;
14639 boolean_t shared;
14640
14641 if ((entry->object.vm_object == 0) ||
14642 (entry->is_sub_map) ||
14643 (entry->object.vm_object->phys_contiguous)) {
14644 continue;
14645 }
14646
14647 default_freezer_pack(&purgeable, &wired, &clean, &dirty, dirty_budget, &shared, entry->object.vm_object, NULL);
14648
14649 *purgeable_count += purgeable;
14650 *wired_count += wired;
14651 *clean_count += clean;
14652 *dirty_count += dirty;
14653
14654 if (shared) {
14655 *has_shared = TRUE;
14656 }
14657
14658 /* Adjust pageout budget and finish up if reached */
14659 if (dirty_budget) {
14660 dirty_budget -= dirty;
14661 if (dirty_budget == 0) {
14662 break;
14663 }
14664 }
14665 }
14666
14667 vm_map_unlock_read(map);
14668
14669 return KERN_SUCCESS;
14670}
14671
14672kern_return_t vm_map_freeze(
14673 vm_map_t map,
14674 unsigned int *purgeable_count,
14675 unsigned int *wired_count,
14676 unsigned int *clean_count,
14677 unsigned int *dirty_count,
14678 unsigned int dirty_budget,
14679 boolean_t *has_shared)
14680{
14681 vm_map_entry_t entry2 = VM_MAP_ENTRY_NULL;
14682 kern_return_t kr = KERN_SUCCESS;
14683 boolean_t default_freezer_active = TRUE;
14684
14685 *purgeable_count = *wired_count = *clean_count = *dirty_count = 0;
14686 *has_shared = FALSE;
14687
14688 /*
14689 * We need the exclusive lock here so that we can
14690 * block any page faults or lookups while we are
14691 * in the middle of freezing this vm map.
14692 */
14693 vm_map_lock(map);
14694
14695 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
14696 default_freezer_active = FALSE;
14697 }
14698
14699 if (default_freezer_active) {
14700 if (map->default_freezer_handle == NULL) {
14701 map->default_freezer_handle = default_freezer_handle_allocate();
14702 }
14703
14704 if ((kr = default_freezer_handle_init(map->default_freezer_handle)) != KERN_SUCCESS) {
14705 /*
14706 * Can happen if default_freezer_handle passed in is NULL
14707 * Or, a table has already been allocated and associated
14708 * with this handle, i.e. the map is already frozen.
14709 */
14710 goto done;
14711 }
14712 }
14713
14714 for (entry2 = vm_map_first_entry(map);
14715 entry2 != vm_map_to_entry(map);
14716 entry2 = entry2->vme_next) {
14717
14718 vm_object_t src_object = entry2->object.vm_object;
14719
14720 if (entry2->object.vm_object && !entry2->is_sub_map && !entry2->object.vm_object->phys_contiguous) {
14721 /* If eligible, scan the entry, moving eligible pages over to our parent object */
14722 if (default_freezer_active) {
14723 unsigned int purgeable, clean, dirty, wired;
14724 boolean_t shared;
14725
14726 default_freezer_pack(&purgeable, &wired, &clean, &dirty, dirty_budget, &shared,
14727 src_object, map->default_freezer_handle);
14728
14729 *purgeable_count += purgeable;
14730 *wired_count += wired;
14731 *clean_count += clean;
14732 *dirty_count += dirty;
14733
14734 /* Adjust pageout budget and finish up if reached */
14735 if (dirty_budget) {
14736 dirty_budget -= dirty;
14737 if (dirty_budget == 0) {
14738 break;
14739 }
14740 }
14741
14742 if (shared) {
14743 *has_shared = TRUE;
14744 }
14745 } else {
14746 /*
14747 * To the compressor.
14748 */
14749 if (entry2->object.vm_object->internal == TRUE) {
14750 vm_object_pageout(entry2->object.vm_object);
14751 }
14752 }
14753 }
14754 }
14755
14756 if (default_freezer_active) {
14757 /* Finally, throw out the pages to swap */
14758 default_freezer_pageout(map->default_freezer_handle);
14759 }
14760
14761done:
14762 vm_map_unlock(map);
14763
14764 return kr;
14765}
14766
14767kern_return_t
14768vm_map_thaw(
14769 vm_map_t map)
14770{
14771 kern_return_t kr = KERN_SUCCESS;
14772
14773 if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
14774 /*
14775 * We will on-demand thaw in the presence of the compressed pager.
14776 */
14777 return kr;
14778 }
14779
14780 vm_map_lock(map);
14781
14782 if (map->default_freezer_handle == NULL) {
14783 /*
14784 * This map is not in a frozen state.
14785 */
14786 kr = KERN_FAILURE;
14787 goto out;
14788 }
14789
14790 kr = default_freezer_unpack(map->default_freezer_handle);
14791out:
14792 vm_map_unlock(map);
14793
14794 return kr;
14795}
14796#endif
14797
14798/*
14799 * vm_map_entry_should_cow_for_true_share:
14800 *
14801 * Determines if the map entry should be clipped and setup for copy-on-write
14802 * to avoid applying "true_share" to a large VM object when only a subset is
14803 * targeted.
14804 *
14805 * For now, we target only the map entries created for the Objective C
14806 * Garbage Collector, which initially have the following properties:
14807 * - alias == VM_MEMORY_MALLOC
14808 * - wired_count == 0
14809 * - !needs_copy
14810 * and a VM object with:
14811 * - internal
14812 * - copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC
14813 * - !true_share
14814 * - vo_size == ANON_CHUNK_SIZE
14815 */
14816boolean_t
14817vm_map_entry_should_cow_for_true_share(
14818 vm_map_entry_t entry)
14819{
14820 vm_object_t object;
14821
14822 if (entry->is_sub_map) {
14823 /* entry does not point at a VM object */
14824 return FALSE;
14825 }
14826
14827 if (entry->needs_copy) {
14828 /* already set for copy_on_write: done! */
14829 return FALSE;
14830 }
14831
14832 if (entry->alias != VM_MEMORY_MALLOC &&
14833 entry->alias != VM_MEMORY_MALLOC_SMALL) {
14834 /* not a malloc heap or Obj-C Garbage Collector heap */
14835 return FALSE;
14836 }
14837
14838 if (entry->wired_count) {
14839 /* wired: can't change the map entry... */
14840 vm_counters.should_cow_but_wired++;
14841 return FALSE;
14842 }
14843
14844 object = entry->object.vm_object;
14845
14846 if (object == VM_OBJECT_NULL) {
14847 /* no object yet... */
14848 return FALSE;
14849 }
14850
14851 if (!object->internal) {
14852 /* not an internal object */
14853 return FALSE;
14854 }
14855
14856 if (object->copy_strategy != MEMORY_OBJECT_COPY_SYMMETRIC) {
14857 /* not the default copy strategy */
14858 return FALSE;
14859 }
14860
14861 if (object->true_share) {
14862 /* already true_share: too late to avoid it */
14863 return FALSE;
14864 }
14865
14866 if (entry->alias == VM_MEMORY_MALLOC &&
14867 object->vo_size != ANON_CHUNK_SIZE) {
14868 /* ... not an object created for the ObjC Garbage Collector */
14869 return FALSE;
14870 }
14871
14872 if (entry->alias == VM_MEMORY_MALLOC_SMALL &&
14873 object->vo_size != 2048 * 4096) {
14874 /* ... not a "MALLOC_SMALL" heap */
14875 return FALSE;
14876 }
14877
14878 /*
14879 * All the criteria match: we have a large object being targeted for "true_share".
14880 * To limit the adverse side-effects linked with "true_share", tell the caller to
14881 * try and avoid setting up the entire object for "true_share" by clipping the
14882 * targeted range and setting it up for copy-on-write.
14883 */
14884 return TRUE;
14885}
14886
14887vm_map_offset_t
14888vm_map_round_page_mask(
14889 vm_map_offset_t offset,
14890 vm_map_offset_t mask)
14891{
14892 return VM_MAP_ROUND_PAGE(offset, mask);
14893}
14894
14895vm_map_offset_t
14896vm_map_trunc_page_mask(
14897 vm_map_offset_t offset,
14898 vm_map_offset_t mask)
14899{
14900 return VM_MAP_TRUNC_PAGE(offset, mask);
14901}
14902
14903int
14904vm_map_page_shift(
14905 vm_map_t map)
14906{
14907 return VM_MAP_PAGE_SHIFT(map);
14908}
14909
14910int
14911vm_map_page_size(
14912 vm_map_t map)
14913{
14914 return VM_MAP_PAGE_SIZE(map);
14915}
14916
14917int
14918vm_map_page_mask(
14919 vm_map_t map)
14920{
14921 return VM_MAP_PAGE_MASK(map);
14922}
14923
14924kern_return_t
14925vm_map_set_page_shift(
14926 vm_map_t map,
14927 int pageshift)
14928{
14929 if (map->hdr.nentries != 0) {
14930 /* too late to change page size */
14931 return KERN_FAILURE;
14932 }
14933
14934 map->hdr.page_shift = pageshift;
14935
14936 return KERN_SUCCESS;
14937}
14938
14939int
14940vm_map_purge(
14941 vm_map_t map)
14942{
14943 int num_object_purged;
14944 vm_map_entry_t entry;
14945 vm_map_offset_t next_address;
14946 vm_object_t object;
14947 int state;
14948 kern_return_t kr;
14949
14950 num_object_purged = 0;
14951
14952 vm_map_lock_read(map);
14953 entry = vm_map_first_entry(map);
14954 while (entry != vm_map_to_entry(map)) {
14955 if (entry->is_sub_map) {
14956 goto next;
14957 }
14958 if (! (entry->protection & VM_PROT_WRITE)) {
14959 goto next;
14960 }
14961 object = entry->object.vm_object;
14962 if (object == VM_OBJECT_NULL) {
14963 goto next;
14964 }
14965 if (object->purgable != VM_PURGABLE_VOLATILE) {
14966 goto next;
14967 }
14968
14969 vm_object_lock(object);
14970#if 00
14971 if (entry->offset != 0 ||
14972 (entry->vme_end - entry->vme_start) != object->vo_size) {
14973 vm_object_unlock(object);
14974 goto next;
14975 }
14976#endif
14977 next_address = entry->vme_end;
14978 vm_map_unlock_read(map);
14979 state = VM_PURGABLE_EMPTY;
14980 kr = vm_object_purgable_control(object,
14981 VM_PURGABLE_SET_STATE,
14982 &state);
14983 if (kr == KERN_SUCCESS) {
14984 num_object_purged++;
14985 }
14986 vm_object_unlock(object);
14987
14988 vm_map_lock_read(map);
14989 if (vm_map_lookup_entry(map, next_address, &entry)) {
14990 continue;
14991 }
14992 next:
14993 entry = entry->vme_next;
14994 }
14995 vm_map_unlock_read(map);
14996
14997 return num_object_purged;
14998}
14999
15000kern_return_t
15001vm_map_query_volatile(
15002 vm_map_t map,
15003 mach_vm_size_t *volatile_virtual_size_p,
15004 mach_vm_size_t *volatile_resident_size_p,
15005 mach_vm_size_t *volatile_pmap_size_p)
15006{
15007 mach_vm_size_t volatile_virtual_size;
15008 mach_vm_size_t volatile_resident_count;
15009 mach_vm_size_t volatile_pmap_count;
15010 mach_vm_size_t resident_count;
15011 vm_map_entry_t entry;
15012 vm_object_t object;
15013
15014 /* map should be locked by caller */
15015
15016 volatile_virtual_size = 0;
15017 volatile_resident_count = 0;
15018 volatile_pmap_count = 0;
15019
15020 for (entry = vm_map_first_entry(map);
15021 entry != vm_map_to_entry(map);
15022 entry = entry->vme_next) {
15023 if (entry->is_sub_map) {
15024 continue;
15025 }
15026 if (! (entry->protection & VM_PROT_WRITE)) {
15027 continue;
15028 }
15029 object = entry->object.vm_object;
15030 if (object == VM_OBJECT_NULL) {
15031 continue;
15032 }
15033 if (object->purgable != VM_PURGABLE_VOLATILE) {
15034 continue;
15035 }
15036 if (entry->offset != 0) {
15037 /*
15038 * If the map entry has been split and the object now
15039 * appears several times in the VM map, we don't want
15040 * to count the object's resident_page_count more than
15041 * once. We count it only for the first one, starting
15042 * at offset 0 and ignore the other VM map entries.
15043 */
15044 continue;
15045 }
15046 resident_count = object->resident_page_count;
15047 if ((entry->offset / PAGE_SIZE) >= resident_count) {
15048 resident_count = 0;
15049 } else {
15050 resident_count -= (entry->offset / PAGE_SIZE);
15051 }
15052
15053 volatile_virtual_size += entry->vme_end - entry->vme_start;
15054 volatile_resident_count += resident_count;
15055 volatile_pmap_count += pmap_query_resident(map->pmap,
15056 entry->vme_start,
15057 entry->vme_end);
15058 }
15059
15060 /* map is still locked on return */
15061
15062 *volatile_virtual_size_p = volatile_virtual_size;
15063 *volatile_resident_size_p = volatile_resident_count * PAGE_SIZE;
15064 *volatile_pmap_size_p = volatile_pmap_count * PAGE_SIZE;
15065
15066 return KERN_SUCCESS;
15067}
15068
15069#if VM_SCAN_FOR_SHADOW_CHAIN
15070int vm_map_shadow_max(vm_map_t map);
15071int vm_map_shadow_max(
15072 vm_map_t map)
15073{
15074 int shadows, shadows_max;
15075 vm_map_entry_t entry;
15076 vm_object_t object, next_object;
15077
15078 if (map == NULL)
15079 return 0;
15080
15081 shadows_max = 0;
15082
15083 vm_map_lock_read(map);
15084
15085 for (entry = vm_map_first_entry(map);
15086 entry != vm_map_to_entry(map);
15087 entry = entry->vme_next) {
15088 if (entry->is_sub_map) {
15089 continue;
15090 }
15091 object = entry->object.vm_object;
15092 if (object == NULL) {
15093 continue;
15094 }
15095 vm_object_lock_shared(object);
15096 for (shadows = 0;
15097 object->shadow != NULL;
15098 shadows++, object = next_object) {
15099 next_object = object->shadow;
15100 vm_object_lock_shared(next_object);
15101 vm_object_unlock(object);
15102 }
15103 vm_object_unlock(object);
15104 if (shadows > shadows_max) {
15105 shadows_max = shadows;
15106 }
15107 }
15108
15109 vm_map_unlock_read(map);
15110
15111 return shadows_max;
15112}
15113#endif /* VM_SCAN_FOR_SHADOW_CHAIN */