]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_kern.c
c75b23835407c30f6c8900904ed11e37b71ec696
[apple/xnu.git] / osfmk / vm / vm_kern.c
1 /*
2 * Copyright (c) 2000-2007 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_kern.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
61 * Date: 1985
62 *
63 * Kernel memory management.
64 */
65
66 #include <mach/kern_return.h>
67 #include <mach/vm_param.h>
68 #include <kern/assert.h>
69 #include <kern/thread.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pageout.h>
75 #include <kern/misc_protos.h>
76 #include <vm/cpm.h>
77
78 #include <string.h>
79
80 #include <libkern/OSDebug.h>
81 #include <sys/kdebug.h>
82
83 /*
84 * Variables exported by this module.
85 */
86
87 vm_map_t kernel_map;
88 vm_map_t kernel_pageable_map;
89
90 extern boolean_t vm_kernel_ready;
91
92 /*
93 * Forward declarations for internal functions.
94 */
95 extern kern_return_t kmem_alloc_pages(
96 register vm_object_t object,
97 register vm_object_offset_t offset,
98 register vm_object_size_t size);
99
100 kern_return_t
101 kmem_alloc_contig(
102 vm_map_t map,
103 vm_offset_t *addrp,
104 vm_size_t size,
105 vm_offset_t mask,
106 ppnum_t max_pnum,
107 ppnum_t pnum_mask,
108 int flags,
109 vm_tag_t tag)
110 {
111 vm_object_t object;
112 vm_object_offset_t offset;
113 vm_map_offset_t map_addr;
114 vm_map_offset_t map_mask;
115 vm_map_size_t map_size, i;
116 vm_map_entry_t entry;
117 vm_page_t m, pages;
118 kern_return_t kr;
119
120 if (map == VM_MAP_NULL || (flags & ~(KMA_KOBJECT | KMA_LOMEM | KMA_NOPAGEWAIT)))
121 return KERN_INVALID_ARGUMENT;
122
123 map_size = vm_map_round_page(size,
124 VM_MAP_PAGE_MASK(map));
125 map_mask = (vm_map_offset_t)mask;
126
127 /* Check for zero allocation size (either directly or via overflow) */
128 if (map_size == 0) {
129 *addrp = 0;
130 return KERN_INVALID_ARGUMENT;
131 }
132
133 /*
134 * Allocate a new object (if necessary) and the reference we
135 * will be donating to the map entry. We must do this before
136 * locking the map, or risk deadlock with the default pager.
137 */
138 if ((flags & KMA_KOBJECT) != 0) {
139 object = kernel_object;
140 vm_object_reference(object);
141 } else {
142 object = vm_object_allocate(map_size);
143 }
144
145 kr = vm_map_find_space(map, &map_addr, map_size, map_mask, 0, &entry);
146 if (KERN_SUCCESS != kr) {
147 vm_object_deallocate(object);
148 return kr;
149 }
150
151 if (object == kernel_object) {
152 offset = map_addr;
153 } else {
154 offset = 0;
155 }
156 VME_OBJECT_SET(entry, object);
157 VME_OFFSET_SET(entry, offset);
158 VME_ALIAS_SET(entry, tag);
159
160 /* Take an extra object ref in case the map entry gets deleted */
161 vm_object_reference(object);
162 vm_map_unlock(map);
163
164 kr = cpm_allocate(CAST_DOWN(vm_size_t, map_size), &pages, max_pnum, pnum_mask, FALSE, flags);
165
166 if (kr != KERN_SUCCESS) {
167 vm_map_remove(map,
168 vm_map_trunc_page(map_addr,
169 VM_MAP_PAGE_MASK(map)),
170 vm_map_round_page(map_addr + map_size,
171 VM_MAP_PAGE_MASK(map)),
172 0);
173 vm_object_deallocate(object);
174 *addrp = 0;
175 return kr;
176 }
177
178 vm_object_lock(object);
179 for (i = 0; i < map_size; i += PAGE_SIZE) {
180 m = pages;
181 pages = NEXT_PAGE(m);
182 *(NEXT_PAGE_PTR(m)) = VM_PAGE_NULL;
183 m->busy = FALSE;
184 vm_page_insert(m, object, offset + i);
185 }
186 vm_object_unlock(object);
187
188 kr = vm_map_wire(map,
189 vm_map_trunc_page(map_addr,
190 VM_MAP_PAGE_MASK(map)),
191 vm_map_round_page(map_addr + map_size,
192 VM_MAP_PAGE_MASK(map)),
193 VM_PROT_DEFAULT | VM_PROT_MEMORY_TAG_MAKE(tag),
194 FALSE);
195
196 if (kr != KERN_SUCCESS) {
197 if (object == kernel_object) {
198 vm_object_lock(object);
199 vm_object_page_remove(object, offset, offset + map_size);
200 vm_object_unlock(object);
201 }
202 vm_map_remove(map,
203 vm_map_trunc_page(map_addr,
204 VM_MAP_PAGE_MASK(map)),
205 vm_map_round_page(map_addr + map_size,
206 VM_MAP_PAGE_MASK(map)),
207 0);
208 vm_object_deallocate(object);
209 return kr;
210 }
211 vm_object_deallocate(object);
212
213 if (object == kernel_object)
214 vm_map_simplify(map, map_addr);
215
216 *addrp = (vm_offset_t) map_addr;
217 assert((vm_map_offset_t) *addrp == map_addr);
218 return KERN_SUCCESS;
219 }
220
221 /*
222 * Master entry point for allocating kernel memory.
223 * NOTE: this routine is _never_ interrupt safe.
224 *
225 * map : map to allocate into
226 * addrp : pointer to start address of new memory
227 * size : size of memory requested
228 * flags : options
229 * KMA_HERE *addrp is base address, else "anywhere"
230 * KMA_NOPAGEWAIT don't wait for pages if unavailable
231 * KMA_KOBJECT use kernel_object
232 * KMA_LOMEM support for 32 bit devices in a 64 bit world
233 * if set and a lomemory pool is available
234 * grab pages from it... this also implies
235 * KMA_NOPAGEWAIT
236 */
237
238 kern_return_t
239 kernel_memory_allocate(
240 register vm_map_t map,
241 register vm_offset_t *addrp,
242 register vm_size_t size,
243 register vm_offset_t mask,
244 int flags,
245 vm_tag_t tag)
246 {
247 vm_object_t object;
248 vm_object_offset_t offset;
249 vm_object_offset_t pg_offset;
250 vm_map_entry_t entry = NULL;
251 vm_map_offset_t map_addr, fill_start;
252 vm_map_offset_t map_mask;
253 vm_map_size_t map_size, fill_size;
254 kern_return_t kr, pe_result;
255 vm_page_t mem;
256 vm_page_t guard_page_list = NULL;
257 vm_page_t wired_page_list = NULL;
258 int guard_page_count = 0;
259 int wired_page_count = 0;
260 int i;
261 int vm_alloc_flags;
262 vm_prot_t kma_prot;
263
264 if (! vm_kernel_ready) {
265 panic("kernel_memory_allocate: VM is not ready");
266 }
267
268 map_size = vm_map_round_page(size,
269 VM_MAP_PAGE_MASK(map));
270 map_mask = (vm_map_offset_t) mask;
271
272 vm_alloc_flags = VM_MAKE_TAG(tag);
273
274 /* Check for zero allocation size (either directly or via overflow) */
275 if (map_size == 0) {
276 *addrp = 0;
277 return KERN_INVALID_ARGUMENT;
278 }
279
280 /*
281 * limit the size of a single extent of wired memory
282 * to try and limit the damage to the system if
283 * too many pages get wired down
284 * limit raised to 2GB with 128GB max physical limit,
285 * but scaled by installed memory above this
286 */
287 if ( !(flags & KMA_VAONLY) && map_size > MAX(1ULL<<31, sane_size/64)) {
288 return KERN_RESOURCE_SHORTAGE;
289 }
290
291 /*
292 * Guard pages:
293 *
294 * Guard pages are implemented as ficticious pages. By placing guard pages
295 * on either end of a stack, they can help detect cases where a thread walks
296 * off either end of its stack. They are allocated and set up here and attempts
297 * to access those pages are trapped in vm_fault_page().
298 *
299 * The map_size we were passed may include extra space for
300 * guard pages. If those were requested, then back it out of fill_size
301 * since vm_map_find_space() takes just the actual size not including
302 * guard pages. Similarly, fill_start indicates where the actual pages
303 * will begin in the range.
304 */
305
306 fill_start = 0;
307 fill_size = map_size;
308
309 if (flags & KMA_GUARD_FIRST) {
310 vm_alloc_flags |= VM_FLAGS_GUARD_BEFORE;
311 fill_start += PAGE_SIZE_64;
312 fill_size -= PAGE_SIZE_64;
313 if (map_size < fill_start + fill_size) {
314 /* no space for a guard page */
315 *addrp = 0;
316 return KERN_INVALID_ARGUMENT;
317 }
318 guard_page_count++;
319 }
320 if (flags & KMA_GUARD_LAST) {
321 vm_alloc_flags |= VM_FLAGS_GUARD_AFTER;
322 fill_size -= PAGE_SIZE_64;
323 if (map_size <= fill_start + fill_size) {
324 /* no space for a guard page */
325 *addrp = 0;
326 return KERN_INVALID_ARGUMENT;
327 }
328 guard_page_count++;
329 }
330 wired_page_count = (int) (fill_size / PAGE_SIZE_64);
331 assert(wired_page_count * PAGE_SIZE_64 == fill_size);
332
333 for (i = 0; i < guard_page_count; i++) {
334 for (;;) {
335 mem = vm_page_grab_guard();
336
337 if (mem != VM_PAGE_NULL)
338 break;
339 if (flags & KMA_NOPAGEWAIT) {
340 kr = KERN_RESOURCE_SHORTAGE;
341 goto out;
342 }
343 vm_page_more_fictitious();
344 }
345 mem->pageq.next = (queue_entry_t)guard_page_list;
346 guard_page_list = mem;
347 }
348
349 if (! (flags & KMA_VAONLY)) {
350 for (i = 0; i < wired_page_count; i++) {
351 uint64_t unavailable;
352
353 for (;;) {
354 if (flags & KMA_LOMEM)
355 mem = vm_page_grablo();
356 else
357 mem = vm_page_grab();
358
359 if (mem != VM_PAGE_NULL)
360 break;
361
362 if (flags & KMA_NOPAGEWAIT) {
363 kr = KERN_RESOURCE_SHORTAGE;
364 goto out;
365 }
366 if ((flags & KMA_LOMEM) && (vm_lopage_needed == TRUE)) {
367 kr = KERN_RESOURCE_SHORTAGE;
368 goto out;
369 }
370 unavailable = (vm_page_wire_count + vm_page_free_target) * PAGE_SIZE;
371
372 if (unavailable > max_mem || map_size > (max_mem - unavailable)) {
373 kr = KERN_RESOURCE_SHORTAGE;
374 goto out;
375 }
376 VM_PAGE_WAIT();
377 }
378 mem->pageq.next = (queue_entry_t)wired_page_list;
379 wired_page_list = mem;
380 }
381 }
382
383 /*
384 * Allocate a new object (if necessary). We must do this before
385 * locking the map, or risk deadlock with the default pager.
386 */
387 if ((flags & KMA_KOBJECT) != 0) {
388 object = kernel_object;
389 vm_object_reference(object);
390 } else if ((flags & KMA_COMPRESSOR) != 0) {
391 object = compressor_object;
392 vm_object_reference(object);
393 } else {
394 object = vm_object_allocate(map_size);
395 }
396
397 kr = vm_map_find_space(map, &map_addr,
398 fill_size, map_mask,
399 vm_alloc_flags, &entry);
400 if (KERN_SUCCESS != kr) {
401 vm_object_deallocate(object);
402 goto out;
403 }
404
405 if (object == kernel_object || object == compressor_object) {
406 offset = map_addr;
407 } else {
408 offset = 0;
409 }
410 VME_OBJECT_SET(entry, object);
411 VME_OFFSET_SET(entry, offset);
412
413 if (object != compressor_object)
414 entry->wired_count++;
415
416 if (flags & KMA_PERMANENT)
417 entry->permanent = TRUE;
418
419 if (object != kernel_object && object != compressor_object)
420 vm_object_reference(object);
421
422 vm_object_lock(object);
423 vm_map_unlock(map);
424
425 pg_offset = 0;
426
427 if (fill_start) {
428 if (guard_page_list == NULL)
429 panic("kernel_memory_allocate: guard_page_list == NULL");
430
431 mem = guard_page_list;
432 guard_page_list = (vm_page_t)mem->pageq.next;
433 mem->pageq.next = NULL;
434
435 vm_page_insert(mem, object, offset + pg_offset);
436
437 mem->busy = FALSE;
438 pg_offset += PAGE_SIZE_64;
439 }
440
441 kma_prot = VM_PROT_READ | VM_PROT_WRITE;
442
443 if (flags & KMA_VAONLY) {
444 pg_offset = fill_start + fill_size;
445 } else {
446 for (pg_offset = fill_start; pg_offset < fill_start + fill_size; pg_offset += PAGE_SIZE_64) {
447 if (wired_page_list == NULL)
448 panic("kernel_memory_allocate: wired_page_list == NULL");
449
450 mem = wired_page_list;
451 wired_page_list = (vm_page_t)mem->pageq.next;
452 mem->pageq.next = NULL;
453 mem->wire_count++;
454
455 vm_page_insert_wired(mem, object, offset + pg_offset, tag);
456
457 mem->busy = FALSE;
458 mem->pmapped = TRUE;
459 mem->wpmapped = TRUE;
460
461 PMAP_ENTER_OPTIONS(kernel_pmap, map_addr + pg_offset, mem,
462 kma_prot, VM_PROT_NONE, ((flags & KMA_KSTACK) ? VM_MEM_STACK : 0), TRUE,
463 PMAP_OPTIONS_NOWAIT, pe_result);
464
465 if (pe_result == KERN_RESOURCE_SHORTAGE) {
466 vm_object_unlock(object);
467
468 PMAP_ENTER(kernel_pmap, map_addr + pg_offset, mem,
469 kma_prot, VM_PROT_NONE, ((flags & KMA_KSTACK) ? VM_MEM_STACK : 0), TRUE);
470
471 vm_object_lock(object);
472 }
473 if (flags & KMA_NOENCRYPT) {
474 bzero(CAST_DOWN(void *, (map_addr + pg_offset)), PAGE_SIZE);
475
476 pmap_set_noencrypt(mem->phys_page);
477 }
478 }
479 }
480 if ((fill_start + fill_size) < map_size) {
481 if (guard_page_list == NULL)
482 panic("kernel_memory_allocate: guard_page_list == NULL");
483
484 mem = guard_page_list;
485 guard_page_list = (vm_page_t)mem->pageq.next;
486 mem->pageq.next = NULL;
487
488 vm_page_insert(mem, object, offset + pg_offset);
489
490 mem->busy = FALSE;
491 }
492 if (guard_page_list || wired_page_list)
493 panic("kernel_memory_allocate: non empty list\n");
494
495 if (! (flags & KMA_VAONLY)) {
496 vm_page_lockspin_queues();
497 vm_page_wire_count += wired_page_count;
498 vm_page_unlock_queues();
499 }
500
501 vm_object_unlock(object);
502
503 /*
504 * now that the pages are wired, we no longer have to fear coalesce
505 */
506 if (object == kernel_object || object == compressor_object)
507 vm_map_simplify(map, map_addr);
508 else
509 vm_object_deallocate(object);
510
511 /*
512 * Return the memory, not zeroed.
513 */
514 *addrp = CAST_DOWN(vm_offset_t, map_addr);
515 return KERN_SUCCESS;
516
517 out:
518 if (guard_page_list)
519 vm_page_free_list(guard_page_list, FALSE);
520
521 if (wired_page_list)
522 vm_page_free_list(wired_page_list, FALSE);
523
524 return kr;
525 }
526
527 kern_return_t
528 kernel_memory_populate(
529 vm_map_t map,
530 vm_offset_t addr,
531 vm_size_t size,
532 int flags,
533 vm_tag_t tag)
534 {
535 vm_object_t object;
536 vm_object_offset_t offset, pg_offset;
537 kern_return_t kr, pe_result;
538 vm_page_t mem;
539 vm_page_t page_list = NULL;
540 int page_count = 0;
541 int i;
542
543 page_count = (int) (size / PAGE_SIZE_64);
544
545 assert((flags & (KMA_COMPRESSOR|KMA_KOBJECT)) != (KMA_COMPRESSOR|KMA_KOBJECT));
546
547 if (flags & KMA_COMPRESSOR) {
548
549 pg_offset = page_count * PAGE_SIZE_64;
550
551 do {
552 for (;;) {
553 mem = vm_page_grab();
554
555 if (mem != VM_PAGE_NULL)
556 break;
557
558 VM_PAGE_WAIT();
559 }
560 mem->pageq.next = (queue_entry_t) page_list;
561 page_list = mem;
562
563 pg_offset -= PAGE_SIZE_64;
564
565 kr = pmap_enter_options(kernel_pmap,
566 addr + pg_offset, mem->phys_page,
567 VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE, 0, TRUE,
568 PMAP_OPTIONS_INTERNAL, NULL);
569 assert(kr == KERN_SUCCESS);
570
571 } while (pg_offset);
572
573 offset = addr;
574 object = compressor_object;
575
576 vm_object_lock(object);
577
578 for (pg_offset = 0;
579 pg_offset < size;
580 pg_offset += PAGE_SIZE_64) {
581
582 mem = page_list;
583 page_list = (vm_page_t) mem->pageq.next;
584 mem->pageq.next = NULL;
585
586 vm_page_insert(mem, object, offset + pg_offset);
587 assert(mem->busy);
588
589 mem->busy = FALSE;
590 mem->pmapped = TRUE;
591 mem->wpmapped = TRUE;
592 mem->compressor = TRUE;
593 }
594 vm_object_unlock(object);
595
596 return KERN_SUCCESS;
597 }
598
599 for (i = 0; i < page_count; i++) {
600 for (;;) {
601 if (flags & KMA_LOMEM)
602 mem = vm_page_grablo();
603 else
604 mem = vm_page_grab();
605
606 if (mem != VM_PAGE_NULL)
607 break;
608
609 if (flags & KMA_NOPAGEWAIT) {
610 kr = KERN_RESOURCE_SHORTAGE;
611 goto out;
612 }
613 if ((flags & KMA_LOMEM) &&
614 (vm_lopage_needed == TRUE)) {
615 kr = KERN_RESOURCE_SHORTAGE;
616 goto out;
617 }
618 VM_PAGE_WAIT();
619 }
620 mem->pageq.next = (queue_entry_t) page_list;
621 page_list = mem;
622 }
623 if (flags & KMA_KOBJECT) {
624 offset = addr;
625 object = kernel_object;
626
627 vm_object_lock(object);
628 } else {
629 /*
630 * If it's not the kernel object, we need to:
631 * lock map;
632 * lookup entry;
633 * lock object;
634 * take reference on object;
635 * unlock map;
636 */
637 panic("kernel_memory_populate(%p,0x%llx,0x%llx,0x%x): "
638 "!KMA_KOBJECT",
639 map, (uint64_t) addr, (uint64_t) size, flags);
640 }
641
642 for (pg_offset = 0;
643 pg_offset < size;
644 pg_offset += PAGE_SIZE_64) {
645
646 if (page_list == NULL)
647 panic("kernel_memory_populate: page_list == NULL");
648
649 mem = page_list;
650 page_list = (vm_page_t) mem->pageq.next;
651 mem->pageq.next = NULL;
652
653 mem->wire_count++;
654
655 vm_page_insert_wired(mem, object, offset + pg_offset, tag);
656
657 mem->busy = FALSE;
658 mem->pmapped = TRUE;
659 mem->wpmapped = TRUE;
660
661 PMAP_ENTER_OPTIONS(kernel_pmap, addr + pg_offset, mem,
662 VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE,
663 ((flags & KMA_KSTACK) ? VM_MEM_STACK : 0), TRUE,
664 PMAP_OPTIONS_NOWAIT, pe_result);
665
666 if (pe_result == KERN_RESOURCE_SHORTAGE) {
667
668 vm_object_unlock(object);
669
670 PMAP_ENTER(kernel_pmap, addr + pg_offset, mem,
671 VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE,
672 ((flags & KMA_KSTACK) ? VM_MEM_STACK : 0), TRUE);
673
674 vm_object_lock(object);
675 }
676 if (flags & KMA_NOENCRYPT) {
677 bzero(CAST_DOWN(void *, (addr + pg_offset)), PAGE_SIZE);
678 pmap_set_noencrypt(mem->phys_page);
679 }
680 }
681 vm_page_lock_queues();
682 vm_page_wire_count += page_count;
683 vm_page_unlock_queues();
684
685 vm_object_unlock(object);
686
687 return KERN_SUCCESS;
688
689 out:
690 if (page_list)
691 vm_page_free_list(page_list, FALSE);
692
693 return kr;
694 }
695
696
697 void
698 kernel_memory_depopulate(
699 vm_map_t map,
700 vm_offset_t addr,
701 vm_size_t size,
702 int flags)
703 {
704 vm_object_t object;
705 vm_object_offset_t offset, pg_offset;
706 vm_page_t mem;
707 vm_page_t local_freeq = NULL;
708
709 assert((flags & (KMA_COMPRESSOR|KMA_KOBJECT)) != (KMA_COMPRESSOR|KMA_KOBJECT));
710
711 if (flags & KMA_COMPRESSOR) {
712 offset = addr;
713 object = compressor_object;
714
715 vm_object_lock(object);
716 } else if (flags & KMA_KOBJECT) {
717 offset = addr;
718 object = kernel_object;
719
720 vm_object_lock(object);
721 } else {
722 offset = 0;
723 object = NULL;
724 /*
725 * If it's not the kernel object, we need to:
726 * lock map;
727 * lookup entry;
728 * lock object;
729 * unlock map;
730 */
731 panic("kernel_memory_depopulate(%p,0x%llx,0x%llx,0x%x): "
732 "!KMA_KOBJECT",
733 map, (uint64_t) addr, (uint64_t) size, flags);
734 }
735 pmap_protect(kernel_map->pmap, offset, offset + size, VM_PROT_NONE);
736
737 for (pg_offset = 0;
738 pg_offset < size;
739 pg_offset += PAGE_SIZE_64) {
740
741 mem = vm_page_lookup(object, offset + pg_offset);
742
743 assert(mem);
744
745 pmap_disconnect(mem->phys_page);
746
747 mem->busy = TRUE;
748
749 assert(mem->tabled);
750 vm_page_remove(mem, TRUE);
751 assert(mem->busy);
752
753 assert(mem->pageq.next == NULL &&
754 mem->pageq.prev == NULL);
755 mem->pageq.next = (queue_entry_t)local_freeq;
756 local_freeq = mem;
757 }
758 vm_object_unlock(object);
759
760 if (local_freeq)
761 vm_page_free_list(local_freeq, TRUE);
762 }
763
764 /*
765 * kmem_alloc:
766 *
767 * Allocate wired-down memory in the kernel's address map
768 * or a submap. The memory is not zero-filled.
769 */
770
771 kern_return_t
772 kmem_alloc_external(
773 vm_map_t map,
774 vm_offset_t *addrp,
775 vm_size_t size)
776 {
777 return (kmem_alloc(map, addrp, size, vm_tag_bt()));
778 }
779
780 kern_return_t
781 kmem_alloc(
782 vm_map_t map,
783 vm_offset_t *addrp,
784 vm_size_t size,
785 vm_tag_t tag)
786 {
787 kern_return_t kr = kernel_memory_allocate(map, addrp, size, 0, 0, tag);
788 TRACE_MACHLEAKS(KMEM_ALLOC_CODE, KMEM_ALLOC_CODE_2, size, *addrp);
789 return kr;
790 }
791
792 /*
793 * kmem_realloc:
794 *
795 * Reallocate wired-down memory in the kernel's address map
796 * or a submap. Newly allocated pages are not zeroed.
797 * This can only be used on regions allocated with kmem_alloc.
798 *
799 * If successful, the pages in the old region are mapped twice.
800 * The old region is unchanged. Use kmem_free to get rid of it.
801 */
802 kern_return_t
803 kmem_realloc(
804 vm_map_t map,
805 vm_offset_t oldaddr,
806 vm_size_t oldsize,
807 vm_offset_t *newaddrp,
808 vm_size_t newsize,
809 vm_tag_t tag)
810 {
811 vm_object_t object;
812 vm_object_offset_t offset;
813 vm_map_offset_t oldmapmin;
814 vm_map_offset_t oldmapmax;
815 vm_map_offset_t newmapaddr;
816 vm_map_size_t oldmapsize;
817 vm_map_size_t newmapsize;
818 vm_map_entry_t oldentry;
819 vm_map_entry_t newentry;
820 vm_page_t mem;
821 kern_return_t kr;
822
823 oldmapmin = vm_map_trunc_page(oldaddr,
824 VM_MAP_PAGE_MASK(map));
825 oldmapmax = vm_map_round_page(oldaddr + oldsize,
826 VM_MAP_PAGE_MASK(map));
827 oldmapsize = oldmapmax - oldmapmin;
828 newmapsize = vm_map_round_page(newsize,
829 VM_MAP_PAGE_MASK(map));
830
831
832 /*
833 * Find the VM object backing the old region.
834 */
835
836 vm_map_lock(map);
837
838 if (!vm_map_lookup_entry(map, oldmapmin, &oldentry))
839 panic("kmem_realloc");
840 object = VME_OBJECT(oldentry);
841
842 /*
843 * Increase the size of the object and
844 * fill in the new region.
845 */
846
847 vm_object_reference(object);
848 /* by grabbing the object lock before unlocking the map */
849 /* we guarantee that we will panic if more than one */
850 /* attempt is made to realloc a kmem_alloc'd area */
851 vm_object_lock(object);
852 vm_map_unlock(map);
853 if (object->vo_size != oldmapsize)
854 panic("kmem_realloc");
855 object->vo_size = newmapsize;
856 vm_object_unlock(object);
857
858 /* allocate the new pages while expanded portion of the */
859 /* object is still not mapped */
860 kmem_alloc_pages(object, vm_object_round_page(oldmapsize),
861 vm_object_round_page(newmapsize-oldmapsize));
862
863 /*
864 * Find space for the new region.
865 */
866
867 kr = vm_map_find_space(map, &newmapaddr, newmapsize,
868 (vm_map_offset_t) 0, 0, &newentry);
869 if (kr != KERN_SUCCESS) {
870 vm_object_lock(object);
871 for(offset = oldmapsize;
872 offset < newmapsize; offset += PAGE_SIZE) {
873 if ((mem = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
874 VM_PAGE_FREE(mem);
875 }
876 }
877 object->vo_size = oldmapsize;
878 vm_object_unlock(object);
879 vm_object_deallocate(object);
880 return kr;
881 }
882 VME_OBJECT_SET(newentry, object);
883 VME_OFFSET_SET(newentry, 0);
884 VME_ALIAS_SET(newentry, tag);
885 assert(newentry->wired_count == 0);
886
887
888 /* add an extra reference in case we have someone doing an */
889 /* unexpected deallocate */
890 vm_object_reference(object);
891 vm_map_unlock(map);
892
893 kr = vm_map_wire(map, newmapaddr, newmapaddr + newmapsize,
894 VM_PROT_DEFAULT | VM_PROT_MEMORY_TAG_MAKE(tag), FALSE);
895 if (KERN_SUCCESS != kr) {
896 vm_map_remove(map, newmapaddr, newmapaddr + newmapsize, 0);
897 vm_object_lock(object);
898 for(offset = oldsize; offset < newmapsize; offset += PAGE_SIZE) {
899 if ((mem = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
900 VM_PAGE_FREE(mem);
901 }
902 }
903 object->vo_size = oldmapsize;
904 vm_object_unlock(object);
905 vm_object_deallocate(object);
906 return (kr);
907 }
908 vm_object_deallocate(object);
909
910 *newaddrp = CAST_DOWN(vm_offset_t, newmapaddr);
911 return KERN_SUCCESS;
912 }
913
914 /*
915 * kmem_alloc_kobject:
916 *
917 * Allocate wired-down memory in the kernel's address map
918 * or a submap. The memory is not zero-filled.
919 *
920 * The memory is allocated in the kernel_object.
921 * It may not be copied with vm_map_copy, and
922 * it may not be reallocated with kmem_realloc.
923 */
924
925 kern_return_t
926 kmem_alloc_kobject_external(
927 vm_map_t map,
928 vm_offset_t *addrp,
929 vm_size_t size)
930 {
931 return (kmem_alloc_kobject(map, addrp, size, vm_tag_bt()));
932 }
933
934 kern_return_t
935 kmem_alloc_kobject(
936 vm_map_t map,
937 vm_offset_t *addrp,
938 vm_size_t size,
939 vm_tag_t tag)
940 {
941 return kernel_memory_allocate(map, addrp, size, 0, KMA_KOBJECT, tag);
942 }
943
944 /*
945 * kmem_alloc_aligned:
946 *
947 * Like kmem_alloc_kobject, except that the memory is aligned.
948 * The size should be a power-of-2.
949 */
950
951 kern_return_t
952 kmem_alloc_aligned(
953 vm_map_t map,
954 vm_offset_t *addrp,
955 vm_size_t size,
956 vm_tag_t tag)
957 {
958 if ((size & (size - 1)) != 0)
959 panic("kmem_alloc_aligned: size not aligned");
960 return kernel_memory_allocate(map, addrp, size, size - 1, KMA_KOBJECT, tag);
961 }
962
963 /*
964 * kmem_alloc_pageable:
965 *
966 * Allocate pageable memory in the kernel's address map.
967 */
968
969 kern_return_t
970 kmem_alloc_pageable_external(
971 vm_map_t map,
972 vm_offset_t *addrp,
973 vm_size_t size)
974 {
975 return (kmem_alloc_pageable(map, addrp, size, vm_tag_bt()));
976 }
977
978 kern_return_t
979 kmem_alloc_pageable(
980 vm_map_t map,
981 vm_offset_t *addrp,
982 vm_size_t size,
983 vm_tag_t tag)
984 {
985 vm_map_offset_t map_addr;
986 vm_map_size_t map_size;
987 kern_return_t kr;
988
989 #ifndef normal
990 map_addr = (vm_map_min(map)) + PAGE_SIZE;
991 #else
992 map_addr = vm_map_min(map);
993 #endif
994 map_size = vm_map_round_page(size,
995 VM_MAP_PAGE_MASK(map));
996
997 kr = vm_map_enter(map, &map_addr, map_size,
998 (vm_map_offset_t) 0,
999 VM_FLAGS_ANYWHERE | VM_MAKE_TAG(tag),
1000 VM_OBJECT_NULL, (vm_object_offset_t) 0, FALSE,
1001 VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT);
1002
1003 if (kr != KERN_SUCCESS)
1004 return kr;
1005
1006 *addrp = CAST_DOWN(vm_offset_t, map_addr);
1007 return KERN_SUCCESS;
1008 }
1009
1010 /*
1011 * kmem_free:
1012 *
1013 * Release a region of kernel virtual memory allocated
1014 * with kmem_alloc, kmem_alloc_kobject, or kmem_alloc_pageable,
1015 * and return the physical pages associated with that region.
1016 */
1017
1018 void
1019 kmem_free(
1020 vm_map_t map,
1021 vm_offset_t addr,
1022 vm_size_t size)
1023 {
1024 kern_return_t kr;
1025
1026 assert(addr >= VM_MIN_KERNEL_AND_KEXT_ADDRESS);
1027
1028 TRACE_MACHLEAKS(KMEM_FREE_CODE, KMEM_FREE_CODE_2, size, addr);
1029
1030 if(size == 0) {
1031 #if MACH_ASSERT
1032 printf("kmem_free called with size==0 for map: %p with addr: 0x%llx\n",map,(uint64_t)addr);
1033 #endif
1034 return;
1035 }
1036
1037 kr = vm_map_remove(map,
1038 vm_map_trunc_page(addr,
1039 VM_MAP_PAGE_MASK(map)),
1040 vm_map_round_page(addr + size,
1041 VM_MAP_PAGE_MASK(map)),
1042 VM_MAP_REMOVE_KUNWIRE);
1043 if (kr != KERN_SUCCESS)
1044 panic("kmem_free");
1045 }
1046
1047 /*
1048 * Allocate new pages in an object.
1049 */
1050
1051 kern_return_t
1052 kmem_alloc_pages(
1053 register vm_object_t object,
1054 register vm_object_offset_t offset,
1055 register vm_object_size_t size)
1056 {
1057 vm_object_size_t alloc_size;
1058
1059 alloc_size = vm_object_round_page(size);
1060 vm_object_lock(object);
1061 while (alloc_size) {
1062 register vm_page_t mem;
1063
1064
1065 /*
1066 * Allocate a page
1067 */
1068 while (VM_PAGE_NULL ==
1069 (mem = vm_page_alloc(object, offset))) {
1070 vm_object_unlock(object);
1071 VM_PAGE_WAIT();
1072 vm_object_lock(object);
1073 }
1074 mem->busy = FALSE;
1075
1076 alloc_size -= PAGE_SIZE;
1077 offset += PAGE_SIZE;
1078 }
1079 vm_object_unlock(object);
1080 return KERN_SUCCESS;
1081 }
1082
1083 /*
1084 * kmem_suballoc:
1085 *
1086 * Allocates a map to manage a subrange
1087 * of the kernel virtual address space.
1088 *
1089 * Arguments are as follows:
1090 *
1091 * parent Map to take range from
1092 * addr Address of start of range (IN/OUT)
1093 * size Size of range to find
1094 * pageable Can region be paged
1095 * anywhere Can region be located anywhere in map
1096 * new_map Pointer to new submap
1097 */
1098 kern_return_t
1099 kmem_suballoc(
1100 vm_map_t parent,
1101 vm_offset_t *addr,
1102 vm_size_t size,
1103 boolean_t pageable,
1104 int flags,
1105 vm_map_t *new_map)
1106 {
1107 vm_map_t map;
1108 vm_map_offset_t map_addr;
1109 vm_map_size_t map_size;
1110 kern_return_t kr;
1111
1112 map_size = vm_map_round_page(size,
1113 VM_MAP_PAGE_MASK(parent));
1114
1115 /*
1116 * Need reference on submap object because it is internal
1117 * to the vm_system. vm_object_enter will never be called
1118 * on it (usual source of reference for vm_map_enter).
1119 */
1120 vm_object_reference(vm_submap_object);
1121
1122 map_addr = ((flags & VM_FLAGS_ANYWHERE)
1123 ? vm_map_min(parent)
1124 : vm_map_trunc_page(*addr,
1125 VM_MAP_PAGE_MASK(parent)));
1126
1127 kr = vm_map_enter(parent, &map_addr, map_size,
1128 (vm_map_offset_t) 0, flags,
1129 vm_submap_object, (vm_object_offset_t) 0, FALSE,
1130 VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT);
1131 if (kr != KERN_SUCCESS) {
1132 vm_object_deallocate(vm_submap_object);
1133 return (kr);
1134 }
1135
1136 pmap_reference(vm_map_pmap(parent));
1137 map = vm_map_create(vm_map_pmap(parent), map_addr, map_addr + map_size, pageable);
1138 if (map == VM_MAP_NULL)
1139 panic("kmem_suballoc: vm_map_create failed"); /* "can't happen" */
1140 /* inherit the parent map's page size */
1141 vm_map_set_page_shift(map, VM_MAP_PAGE_SHIFT(parent));
1142
1143 kr = vm_map_submap(parent, map_addr, map_addr + map_size, map, map_addr, FALSE);
1144 if (kr != KERN_SUCCESS) {
1145 /*
1146 * See comment preceding vm_map_submap().
1147 */
1148 vm_map_remove(parent, map_addr, map_addr + map_size, VM_MAP_NO_FLAGS);
1149 vm_map_deallocate(map); /* also removes ref to pmap */
1150 vm_object_deallocate(vm_submap_object);
1151 return (kr);
1152 }
1153 *addr = CAST_DOWN(vm_offset_t, map_addr);
1154 *new_map = map;
1155 return (KERN_SUCCESS);
1156 }
1157
1158 /*
1159 * kmem_init:
1160 *
1161 * Initialize the kernel's virtual memory map, taking
1162 * into account all memory allocated up to this time.
1163 */
1164 void
1165 kmem_init(
1166 vm_offset_t start,
1167 vm_offset_t end)
1168 {
1169 vm_map_offset_t map_start;
1170 vm_map_offset_t map_end;
1171
1172 map_start = vm_map_trunc_page(start,
1173 VM_MAP_PAGE_MASK(kernel_map));
1174 map_end = vm_map_round_page(end,
1175 VM_MAP_PAGE_MASK(kernel_map));
1176
1177 kernel_map = vm_map_create(pmap_kernel(),VM_MIN_KERNEL_AND_KEXT_ADDRESS,
1178 map_end, FALSE);
1179 /*
1180 * Reserve virtual memory allocated up to this time.
1181 */
1182 if (start != VM_MIN_KERNEL_AND_KEXT_ADDRESS) {
1183 vm_map_offset_t map_addr;
1184 kern_return_t kr;
1185
1186 map_addr = VM_MIN_KERNEL_AND_KEXT_ADDRESS;
1187 kr = vm_map_enter(kernel_map,
1188 &map_addr,
1189 (vm_map_size_t)(map_start - VM_MIN_KERNEL_AND_KEXT_ADDRESS),
1190 (vm_map_offset_t) 0,
1191 VM_FLAGS_FIXED | VM_FLAGS_NO_PMAP_CHECK,
1192 VM_OBJECT_NULL,
1193 (vm_object_offset_t) 0, FALSE,
1194 VM_PROT_NONE, VM_PROT_NONE,
1195 VM_INHERIT_DEFAULT);
1196
1197 if (kr != KERN_SUCCESS) {
1198 panic("kmem_init(0x%llx,0x%llx): vm_map_enter(0x%llx,0x%llx) error 0x%x\n",
1199 (uint64_t) start, (uint64_t) end,
1200 (uint64_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS,
1201 (uint64_t) (map_start - VM_MIN_KERNEL_AND_KEXT_ADDRESS),
1202 kr);
1203 }
1204 }
1205
1206 /*
1207 * Set the default global user wire limit which limits the amount of
1208 * memory that can be locked via mlock(). We set this to the total
1209 * amount of memory that are potentially usable by a user app (max_mem)
1210 * minus a certain amount. This can be overridden via a sysctl.
1211 */
1212 vm_global_no_user_wire_amount = MIN(max_mem*20/100,
1213 VM_NOT_USER_WIREABLE);
1214 vm_global_user_wire_limit = max_mem - vm_global_no_user_wire_amount;
1215
1216 /* the default per user limit is the same as the global limit */
1217 vm_user_wire_limit = vm_global_user_wire_limit;
1218 }
1219
1220
1221 /*
1222 * Routine: copyinmap
1223 * Purpose:
1224 * Like copyin, except that fromaddr is an address
1225 * in the specified VM map. This implementation
1226 * is incomplete; it handles the current user map
1227 * and the kernel map/submaps.
1228 */
1229 kern_return_t
1230 copyinmap(
1231 vm_map_t map,
1232 vm_map_offset_t fromaddr,
1233 void *todata,
1234 vm_size_t length)
1235 {
1236 kern_return_t kr = KERN_SUCCESS;
1237 vm_map_t oldmap;
1238
1239 if (vm_map_pmap(map) == pmap_kernel())
1240 {
1241 /* assume a correct copy */
1242 memcpy(todata, CAST_DOWN(void *, fromaddr), length);
1243 }
1244 else if (current_map() == map)
1245 {
1246 if (copyin(fromaddr, todata, length) != 0)
1247 kr = KERN_INVALID_ADDRESS;
1248 }
1249 else
1250 {
1251 vm_map_reference(map);
1252 oldmap = vm_map_switch(map);
1253 if (copyin(fromaddr, todata, length) != 0)
1254 kr = KERN_INVALID_ADDRESS;
1255 vm_map_switch(oldmap);
1256 vm_map_deallocate(map);
1257 }
1258 return kr;
1259 }
1260
1261 /*
1262 * Routine: copyoutmap
1263 * Purpose:
1264 * Like copyout, except that toaddr is an address
1265 * in the specified VM map. This implementation
1266 * is incomplete; it handles the current user map
1267 * and the kernel map/submaps.
1268 */
1269 kern_return_t
1270 copyoutmap(
1271 vm_map_t map,
1272 void *fromdata,
1273 vm_map_address_t toaddr,
1274 vm_size_t length)
1275 {
1276 if (vm_map_pmap(map) == pmap_kernel()) {
1277 /* assume a correct copy */
1278 memcpy(CAST_DOWN(void *, toaddr), fromdata, length);
1279 return KERN_SUCCESS;
1280 }
1281
1282 if (current_map() != map)
1283 return KERN_NOT_SUPPORTED;
1284
1285 if (copyout(fromdata, toaddr, length) != 0)
1286 return KERN_INVALID_ADDRESS;
1287
1288 return KERN_SUCCESS;
1289 }
1290
1291
1292 kern_return_t
1293 vm_conflict_check(
1294 vm_map_t map,
1295 vm_map_offset_t off,
1296 vm_map_size_t len,
1297 memory_object_t pager,
1298 vm_object_offset_t file_off)
1299 {
1300 vm_map_entry_t entry;
1301 vm_object_t obj;
1302 vm_object_offset_t obj_off;
1303 vm_map_t base_map;
1304 vm_map_offset_t base_offset;
1305 vm_map_offset_t original_offset;
1306 kern_return_t kr;
1307 vm_map_size_t local_len;
1308
1309 base_map = map;
1310 base_offset = off;
1311 original_offset = off;
1312 kr = KERN_SUCCESS;
1313 vm_map_lock(map);
1314 while(vm_map_lookup_entry(map, off, &entry)) {
1315 local_len = len;
1316
1317 if (VME_OBJECT(entry) == VM_OBJECT_NULL) {
1318 vm_map_unlock(map);
1319 return KERN_SUCCESS;
1320 }
1321 if (entry->is_sub_map) {
1322 vm_map_t old_map;
1323
1324 old_map = map;
1325 vm_map_lock(VME_SUBMAP(entry));
1326 map = VME_SUBMAP(entry);
1327 off = VME_OFFSET(entry) + (off - entry->vme_start);
1328 vm_map_unlock(old_map);
1329 continue;
1330 }
1331 obj = VME_OBJECT(entry);
1332 obj_off = (off - entry->vme_start) + VME_OFFSET(entry);
1333 while(obj->shadow) {
1334 obj_off += obj->vo_shadow_offset;
1335 obj = obj->shadow;
1336 }
1337 if((obj->pager_created) && (obj->pager == pager)) {
1338 if(((obj->paging_offset) + obj_off) == file_off) {
1339 if(off != base_offset) {
1340 vm_map_unlock(map);
1341 return KERN_FAILURE;
1342 }
1343 kr = KERN_ALREADY_WAITING;
1344 } else {
1345 vm_object_offset_t obj_off_aligned;
1346 vm_object_offset_t file_off_aligned;
1347
1348 obj_off_aligned = obj_off & ~PAGE_MASK;
1349 file_off_aligned = file_off & ~PAGE_MASK;
1350
1351 if (file_off_aligned == (obj->paging_offset + obj_off_aligned)) {
1352 /*
1353 * the target map and the file offset start in the same page
1354 * but are not identical...
1355 */
1356 vm_map_unlock(map);
1357 return KERN_FAILURE;
1358 }
1359 if ((file_off < (obj->paging_offset + obj_off_aligned)) &&
1360 ((file_off + len) > (obj->paging_offset + obj_off_aligned))) {
1361 /*
1362 * some portion of the tail of the I/O will fall
1363 * within the encompass of the target map
1364 */
1365 vm_map_unlock(map);
1366 return KERN_FAILURE;
1367 }
1368 if ((file_off_aligned > (obj->paging_offset + obj_off)) &&
1369 (file_off_aligned < (obj->paging_offset + obj_off) + len)) {
1370 /*
1371 * the beginning page of the file offset falls within
1372 * the target map's encompass
1373 */
1374 vm_map_unlock(map);
1375 return KERN_FAILURE;
1376 }
1377 }
1378 } else if(kr != KERN_SUCCESS) {
1379 vm_map_unlock(map);
1380 return KERN_FAILURE;
1381 }
1382
1383 if(len <= ((entry->vme_end - entry->vme_start) -
1384 (off - entry->vme_start))) {
1385 vm_map_unlock(map);
1386 return kr;
1387 } else {
1388 len -= (entry->vme_end - entry->vme_start) -
1389 (off - entry->vme_start);
1390 }
1391 base_offset = base_offset + (local_len - len);
1392 file_off = file_off + (local_len - len);
1393 off = base_offset;
1394 if(map != base_map) {
1395 vm_map_unlock(map);
1396 vm_map_lock(base_map);
1397 map = base_map;
1398 }
1399 }
1400
1401 vm_map_unlock(map);
1402 return kr;
1403 }
1404
1405 /*
1406 *
1407 * The following two functions are to be used when exposing kernel
1408 * addresses to userspace via any of the various debug or info
1409 * facilities that exist. These are basically the same as VM_KERNEL_ADDRPERM()
1410 * and VM_KERNEL_UNSLIDE_OR_PERM() except they use a different random seed and
1411 * are exported to KEXTs.
1412 *
1413 * NOTE: USE THE MACRO VERSIONS OF THESE FUNCTIONS (in vm_param.h) FROM WITHIN THE KERNEL
1414 */
1415
1416 /*
1417 * vm_kernel_addrperm_external:
1418 *
1419 * Used when exposing an address to userspace which is in the kernel's
1420 * "heap". These addresses are not loaded from anywhere and are resultingly
1421 * unslid. We apply a permutation value to obscure the address.
1422 */
1423 void
1424 vm_kernel_addrperm_external(
1425 vm_offset_t addr,
1426 vm_offset_t *perm_addr)
1427 {
1428 if (addr == 0) {
1429 *perm_addr = 0;
1430 return;
1431 }
1432
1433 *perm_addr = (addr + vm_kernel_addrperm_ext);
1434 return;
1435 }
1436
1437 /*
1438 * vm_kernel_unslide_or_perm_external:
1439 *
1440 * Use this macro when exposing an address to userspace that could come from
1441 * either kernel text/data *or* the heap.
1442 */
1443 void
1444 vm_kernel_unslide_or_perm_external(
1445 vm_offset_t addr,
1446 vm_offset_t *up_addr)
1447 {
1448 if (VM_KERNEL_IS_SLID(addr) || VM_KERNEL_IS_KEXT(addr) ||
1449 VM_KERNEL_IS_PRELINKTEXT(addr) || VM_KERNEL_IS_PRELINKINFO(addr) ||
1450 VM_KERNEL_IS_KEXT_LINKEDIT(addr)) {
1451 *up_addr = addr - vm_kernel_slide;
1452 return;
1453 }
1454
1455 vm_kernel_addrperm_external(addr, up_addr);
1456 return;
1457 }