]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/vm_kern.c
xnu-1699.32.7.tar.gz
[apple/xnu.git] / osfmk / vm / vm_kern.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
1c79356b
A
66#include <mach/kern_return.h>
67#include <mach/vm_param.h>
68#include <kern/assert.h>
69#include <kern/lock.h>
70#include <kern/thread.h>
71#include <vm/vm_kern.h>
72#include <vm/vm_map.h>
73#include <vm/vm_object.h>
74#include <vm/vm_page.h>
75#include <vm/vm_pageout.h>
76#include <kern/misc_protos.h>
77#include <vm/cpm.h>
78
79#include <string.h>
2d21ac55
A
80
81#include <libkern/OSDebug.h>
82#include <sys/kdebug.h>
83
1c79356b
A
84/*
85 * Variables exported by this module.
86 */
87
88vm_map_t kernel_map;
89vm_map_t kernel_pageable_map;
90
2d21ac55
A
91extern boolean_t vm_kernel_ready;
92
1c79356b
A
93/*
94 * Forward declarations for internal functions.
95 */
96extern kern_return_t kmem_alloc_pages(
97 register vm_object_t object,
98 register vm_object_offset_t offset,
91447636 99 register vm_object_size_t size);
1c79356b
A
100
101extern void kmem_remap_pages(
102 register vm_object_t object,
103 register vm_object_offset_t offset,
104 register vm_offset_t start,
105 register vm_offset_t end,
106 vm_prot_t protection);
107
108kern_return_t
109kmem_alloc_contig(
91447636
A
110 vm_map_t map,
111 vm_offset_t *addrp,
112 vm_size_t size,
113 vm_offset_t mask,
2d21ac55 114 ppnum_t max_pnum,
b0d623f7 115 ppnum_t pnum_mask,
91447636 116 int flags)
1c79356b
A
117{
118 vm_object_t object;
1c79356b 119 vm_object_offset_t offset;
91447636
A
120 vm_map_offset_t map_addr;
121 vm_map_offset_t map_mask;
122 vm_map_size_t map_size, i;
1c79356b 123 vm_map_entry_t entry;
91447636
A
124 vm_page_t m, pages;
125 kern_return_t kr;
1c79356b 126
b0d623f7 127 if (map == VM_MAP_NULL || (flags & ~(KMA_KOBJECT | KMA_LOMEM | KMA_NOPAGEWAIT)))
1c79356b
A
128 return KERN_INVALID_ARGUMENT;
129
130 if (size == 0) {
131 *addrp = 0;
132 return KERN_INVALID_ARGUMENT;
133 }
134
91447636
A
135 map_size = vm_map_round_page(size);
136 map_mask = (vm_map_offset_t)mask;
1c79356b 137
91447636
A
138 /*
139 * Allocate a new object (if necessary) and the reference we
140 * will be donating to the map entry. We must do this before
141 * locking the map, or risk deadlock with the default pager.
142 */
143 if ((flags & KMA_KOBJECT) != 0) {
144 object = kernel_object;
145 vm_object_reference(object);
1c79356b 146 } else {
91447636 147 object = vm_object_allocate(map_size);
1c79356b
A
148 }
149
0c530ab8 150 kr = vm_map_find_space(map, &map_addr, map_size, map_mask, 0, &entry);
91447636
A
151 if (KERN_SUCCESS != kr) {
152 vm_object_deallocate(object);
1c79356b
A
153 return kr;
154 }
155
91447636
A
156 entry->object.vm_object = object;
157 entry->offset = offset = (object == kernel_object) ?
b0d623f7 158 map_addr : 0;
91447636
A
159
160 /* Take an extra object ref in case the map entry gets deleted */
161 vm_object_reference(object);
1c79356b
A
162 vm_map_unlock(map);
163
b0d623f7 164 kr = cpm_allocate(CAST_DOWN(vm_size_t, map_size), &pages, max_pnum, pnum_mask, FALSE, flags);
1c79356b
A
165
166 if (kr != KERN_SUCCESS) {
91447636
A
167 vm_map_remove(map, vm_map_trunc_page(map_addr),
168 vm_map_round_page(map_addr + map_size), 0);
169 vm_object_deallocate(object);
1c79356b
A
170 *addrp = 0;
171 return kr;
172 }
173
174 vm_object_lock(object);
91447636 175 for (i = 0; i < map_size; i += PAGE_SIZE) {
1c79356b
A
176 m = pages;
177 pages = NEXT_PAGE(m);
0c530ab8 178 *(NEXT_PAGE_PTR(m)) = VM_PAGE_NULL;
1c79356b
A
179 m->busy = FALSE;
180 vm_page_insert(m, object, offset + i);
181 }
182 vm_object_unlock(object);
183
91447636
A
184 if ((kr = vm_map_wire(map, vm_map_trunc_page(map_addr),
185 vm_map_round_page(map_addr + map_size), VM_PROT_DEFAULT, FALSE))
1c79356b
A
186 != KERN_SUCCESS) {
187 if (object == kernel_object) {
188 vm_object_lock(object);
91447636 189 vm_object_page_remove(object, offset, offset + map_size);
1c79356b
A
190 vm_object_unlock(object);
191 }
91447636
A
192 vm_map_remove(map, vm_map_trunc_page(map_addr),
193 vm_map_round_page(map_addr + map_size), 0);
194 vm_object_deallocate(object);
1c79356b
A
195 return kr;
196 }
91447636
A
197 vm_object_deallocate(object);
198
1c79356b 199 if (object == kernel_object)
91447636 200 vm_map_simplify(map, map_addr);
1c79356b 201
b0d623f7
A
202 *addrp = (vm_offset_t) map_addr;
203 assert((vm_map_offset_t) *addrp == map_addr);
1c79356b
A
204 return KERN_SUCCESS;
205}
206
207/*
208 * Master entry point for allocating kernel memory.
209 * NOTE: this routine is _never_ interrupt safe.
210 *
211 * map : map to allocate into
212 * addrp : pointer to start address of new memory
213 * size : size of memory requested
214 * flags : options
215 * KMA_HERE *addrp is base address, else "anywhere"
216 * KMA_NOPAGEWAIT don't wait for pages if unavailable
217 * KMA_KOBJECT use kernel_object
0c530ab8
A
218 * KMA_LOMEM support for 32 bit devices in a 64 bit world
219 * if set and a lomemory pool is available
220 * grab pages from it... this also implies
221 * KMA_NOPAGEWAIT
1c79356b
A
222 */
223
224kern_return_t
225kernel_memory_allocate(
226 register vm_map_t map,
227 register vm_offset_t *addrp,
228 register vm_size_t size,
229 register vm_offset_t mask,
230 int flags)
231{
91447636
A
232 vm_object_t object;
233 vm_object_offset_t offset;
b0d623f7 234 vm_object_offset_t pg_offset;
1c79356b 235 vm_map_entry_t entry;
2d21ac55 236 vm_map_offset_t map_addr, fill_start;
91447636 237 vm_map_offset_t map_mask;
2d21ac55 238 vm_map_size_t map_size, fill_size;
1c79356b 239 kern_return_t kr;
2d21ac55 240 vm_page_t mem;
b0d623f7
A
241 vm_page_t guard_page_list = NULL;
242 vm_page_t wired_page_list = NULL;
243 int guard_page_count = 0;
244 int wired_page_count = 0;
245 int i;
2d21ac55
A
246 int vm_alloc_flags;
247
248 if (! vm_kernel_ready) {
249 panic("kernel_memory_allocate: VM is not ready");
250 }
1c79356b 251
91447636
A
252 if (size == 0) {
253 *addrp = 0;
254 return KERN_INVALID_ARGUMENT;
1c79356b 255 }
91447636
A
256 map_size = vm_map_round_page(size);
257 map_mask = (vm_map_offset_t) mask;
2d21ac55
A
258 vm_alloc_flags = 0;
259
b0d623f7
A
260
261 /*
262 * limit the size of a single extent of wired memory
263 * to try and limit the damage to the system if
264 * too many pages get wired down
265 */
266 if (map_size > (1 << 30)) {
267 return KERN_RESOURCE_SHORTAGE;
268 }
269
2d21ac55
A
270 /*
271 * Guard pages:
272 *
273 * Guard pages are implemented as ficticious pages. By placing guard pages
274 * on either end of a stack, they can help detect cases where a thread walks
275 * off either end of its stack. They are allocated and set up here and attempts
276 * to access those pages are trapped in vm_fault_page().
277 *
278 * The map_size we were passed may include extra space for
279 * guard pages. If those were requested, then back it out of fill_size
280 * since vm_map_find_space() takes just the actual size not including
281 * guard pages. Similarly, fill_start indicates where the actual pages
282 * will begin in the range.
283 */
284
285 fill_start = 0;
286 fill_size = map_size;
b0d623f7 287
2d21ac55
A
288 if (flags & KMA_GUARD_FIRST) {
289 vm_alloc_flags |= VM_FLAGS_GUARD_BEFORE;
290 fill_start += PAGE_SIZE_64;
291 fill_size -= PAGE_SIZE_64;
292 if (map_size < fill_start + fill_size) {
293 /* no space for a guard page */
294 *addrp = 0;
295 return KERN_INVALID_ARGUMENT;
296 }
b0d623f7 297 guard_page_count++;
2d21ac55
A
298 }
299 if (flags & KMA_GUARD_LAST) {
300 vm_alloc_flags |= VM_FLAGS_GUARD_AFTER;
301 fill_size -= PAGE_SIZE_64;
302 if (map_size <= fill_start + fill_size) {
303 /* no space for a guard page */
304 *addrp = 0;
305 return KERN_INVALID_ARGUMENT;
306 }
b0d623f7
A
307 guard_page_count++;
308 }
309 wired_page_count = (int) (fill_size / PAGE_SIZE_64);
310 assert(wired_page_count * PAGE_SIZE_64 == fill_size);
311
312 for (i = 0; i < guard_page_count; i++) {
313 for (;;) {
314 mem = vm_page_grab_guard();
315
316 if (mem != VM_PAGE_NULL)
317 break;
318 if (flags & KMA_NOPAGEWAIT) {
319 kr = KERN_RESOURCE_SHORTAGE;
320 goto out;
321 }
322 vm_page_more_fictitious();
323 }
324 mem->pageq.next = (queue_entry_t)guard_page_list;
325 guard_page_list = mem;
326 }
327
328 for (i = 0; i < wired_page_count; i++) {
329 uint64_t unavailable;
330
331 for (;;) {
332 if (flags & KMA_LOMEM)
333 mem = vm_page_grablo();
334 else
335 mem = vm_page_grab();
336
337 if (mem != VM_PAGE_NULL)
338 break;
339
340 if (flags & KMA_NOPAGEWAIT) {
341 kr = KERN_RESOURCE_SHORTAGE;
342 goto out;
343 }
0b4c1975
A
344 if ((flags & KMA_LOMEM) && (vm_lopage_needed == TRUE)) {
345 kr = KERN_RESOURCE_SHORTAGE;
346 goto out;
347 }
b0d623f7
A
348 unavailable = (vm_page_wire_count + vm_page_free_target) * PAGE_SIZE;
349
350 if (unavailable > max_mem || map_size > (max_mem - unavailable)) {
351 kr = KERN_RESOURCE_SHORTAGE;
352 goto out;
353 }
354 VM_PAGE_WAIT();
355 }
356 mem->pageq.next = (queue_entry_t)wired_page_list;
357 wired_page_list = mem;
2d21ac55 358 }
91447636
A
359
360 /*
361 * Allocate a new object (if necessary). We must do this before
362 * locking the map, or risk deadlock with the default pager.
363 */
364 if ((flags & KMA_KOBJECT) != 0) {
1c79356b 365 object = kernel_object;
91447636
A
366 vm_object_reference(object);
367 } else {
368 object = vm_object_allocate(map_size);
1c79356b 369 }
91447636 370
2d21ac55
A
371 kr = vm_map_find_space(map, &map_addr,
372 fill_size, map_mask,
373 vm_alloc_flags, &entry);
91447636
A
374 if (KERN_SUCCESS != kr) {
375 vm_object_deallocate(object);
b0d623f7 376 goto out;
1c79356b 377 }
2d21ac55 378
91447636
A
379 entry->object.vm_object = object;
380 entry->offset = offset = (object == kernel_object) ?
b0d623f7 381 map_addr : 0;
1c79356b 382
b0d623f7
A
383 entry->wired_count++;
384
385 if (flags & KMA_PERMANENT)
386 entry->permanent = TRUE;
387
388 if (object != kernel_object)
389 vm_object_reference(object);
1c79356b
A
390
391 vm_object_lock(object);
b0d623f7 392 vm_map_unlock(map);
1c79356b 393
b0d623f7
A
394 pg_offset = 0;
395
396 if (fill_start) {
397 if (guard_page_list == NULL)
398 panic("kernel_memory_allocate: guard_page_list == NULL");
399
400 mem = guard_page_list;
401 guard_page_list = (vm_page_t)mem->pageq.next;
402 mem->pageq.next = NULL;
403
404 vm_page_insert(mem, object, offset + pg_offset);
2d21ac55 405
2d21ac55 406 mem->busy = FALSE;
b0d623f7 407 pg_offset += PAGE_SIZE_64;
2d21ac55 408 }
b0d623f7
A
409 for (pg_offset = fill_start; pg_offset < fill_start + fill_size; pg_offset += PAGE_SIZE_64) {
410 if (wired_page_list == NULL)
411 panic("kernel_memory_allocate: wired_page_list == NULL");
2d21ac55 412
b0d623f7
A
413 mem = wired_page_list;
414 wired_page_list = (vm_page_t)mem->pageq.next;
415 mem->pageq.next = NULL;
416 mem->wire_count++;
2d21ac55 417
b0d623f7 418 vm_page_insert(mem, object, offset + pg_offset);
0c530ab8 419
1c79356b 420 mem->busy = FALSE;
b0d623f7
A
421 mem->pmapped = TRUE;
422 mem->wpmapped = TRUE;
423
424 PMAP_ENTER(kernel_pmap, map_addr + pg_offset, mem,
6d2010ae 425 VM_PROT_READ | VM_PROT_WRITE, 0, TRUE);
0b4c1975
A
426
427 if (flags & KMA_NOENCRYPT) {
428 bzero(CAST_DOWN(void *, (map_addr + pg_offset)), PAGE_SIZE);
429
430 pmap_set_noencrypt(mem->phys_page);
431 }
1c79356b 432 }
b0d623f7
A
433 if ((fill_start + fill_size) < map_size) {
434 if (guard_page_list == NULL)
435 panic("kernel_memory_allocate: guard_page_list == NULL");
1c79356b 436
b0d623f7
A
437 mem = guard_page_list;
438 guard_page_list = (vm_page_t)mem->pageq.next;
439 mem->pageq.next = NULL;
440
441 vm_page_insert(mem, object, offset + pg_offset);
2d21ac55 442
2d21ac55 443 mem->busy = FALSE;
1c79356b 444 }
b0d623f7
A
445 if (guard_page_list || wired_page_list)
446 panic("kernel_memory_allocate: non empty list\n");
2d21ac55 447
b0d623f7
A
448 vm_page_lockspin_queues();
449 vm_page_wire_count += wired_page_count;
450 vm_page_unlock_queues();
2d21ac55 451
b0d623f7
A
452 vm_object_unlock(object);
453
454 /*
455 * now that the pages are wired, we no longer have to fear coalesce
456 */
1c79356b 457 if (object == kernel_object)
91447636 458 vm_map_simplify(map, map_addr);
b0d623f7
A
459 else
460 vm_object_deallocate(object);
1c79356b
A
461
462 /*
463 * Return the memory, not zeroed.
464 */
91447636 465 *addrp = CAST_DOWN(vm_offset_t, map_addr);
1c79356b 466 return KERN_SUCCESS;
2d21ac55 467
b0d623f7
A
468out:
469 if (guard_page_list)
470 vm_page_free_list(guard_page_list, FALSE);
471
472 if (wired_page_list)
473 vm_page_free_list(wired_page_list, FALSE);
474
475 return kr;
1c79356b
A
476}
477
478/*
479 * kmem_alloc:
480 *
481 * Allocate wired-down memory in the kernel's address map
482 * or a submap. The memory is not zero-filled.
483 */
484
485kern_return_t
486kmem_alloc(
487 vm_map_t map,
488 vm_offset_t *addrp,
489 vm_size_t size)
490{
2d21ac55
A
491 kern_return_t kr = kernel_memory_allocate(map, addrp, size, 0, 0);
492 TRACE_MACHLEAKS(KMEM_ALLOC_CODE, KMEM_ALLOC_CODE_2, size, *addrp);
493 return kr;
1c79356b
A
494}
495
496/*
497 * kmem_realloc:
498 *
499 * Reallocate wired-down memory in the kernel's address map
500 * or a submap. Newly allocated pages are not zeroed.
501 * This can only be used on regions allocated with kmem_alloc.
502 *
503 * If successful, the pages in the old region are mapped twice.
504 * The old region is unchanged. Use kmem_free to get rid of it.
505 */
506kern_return_t
507kmem_realloc(
91447636
A
508 vm_map_t map,
509 vm_offset_t oldaddr,
510 vm_size_t oldsize,
511 vm_offset_t *newaddrp,
512 vm_size_t newsize)
1c79356b 513{
91447636
A
514 vm_object_t object;
515 vm_object_offset_t offset;
516 vm_map_offset_t oldmapmin;
517 vm_map_offset_t oldmapmax;
518 vm_map_offset_t newmapaddr;
519 vm_map_size_t oldmapsize;
520 vm_map_size_t newmapsize;
521 vm_map_entry_t oldentry;
522 vm_map_entry_t newentry;
523 vm_page_t mem;
524 kern_return_t kr;
1c79356b 525
91447636
A
526 oldmapmin = vm_map_trunc_page(oldaddr);
527 oldmapmax = vm_map_round_page(oldaddr + oldsize);
528 oldmapsize = oldmapmax - oldmapmin;
529 newmapsize = vm_map_round_page(newsize);
1c79356b 530
1c79356b
A
531
532 /*
533 * Find the VM object backing the old region.
534 */
535
b4c24cb9
A
536 vm_map_lock(map);
537
91447636 538 if (!vm_map_lookup_entry(map, oldmapmin, &oldentry))
1c79356b
A
539 panic("kmem_realloc");
540 object = oldentry->object.vm_object;
541
542 /*
543 * Increase the size of the object and
544 * fill in the new region.
545 */
546
547 vm_object_reference(object);
b4c24cb9
A
548 /* by grabbing the object lock before unlocking the map */
549 /* we guarantee that we will panic if more than one */
550 /* attempt is made to realloc a kmem_alloc'd area */
1c79356b 551 vm_object_lock(object);
b4c24cb9 552 vm_map_unlock(map);
6d2010ae 553 if (object->vo_size != oldmapsize)
1c79356b 554 panic("kmem_realloc");
6d2010ae 555 object->vo_size = newmapsize;
1c79356b
A
556 vm_object_unlock(object);
557
b4c24cb9
A
558 /* allocate the new pages while expanded portion of the */
559 /* object is still not mapped */
91447636
A
560 kmem_alloc_pages(object, vm_object_round_page(oldmapsize),
561 vm_object_round_page(newmapsize-oldmapsize));
1c79356b
A
562
563 /*
b4c24cb9 564 * Find space for the new region.
1c79356b
A
565 */
566
91447636 567 kr = vm_map_find_space(map, &newmapaddr, newmapsize,
0c530ab8 568 (vm_map_offset_t) 0, 0, &newentry);
b4c24cb9
A
569 if (kr != KERN_SUCCESS) {
570 vm_object_lock(object);
91447636
A
571 for(offset = oldmapsize;
572 offset < newmapsize; offset += PAGE_SIZE) {
b4c24cb9 573 if ((mem = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
b0d623f7 574 VM_PAGE_FREE(mem);
b4c24cb9
A
575 }
576 }
6d2010ae 577 object->vo_size = oldmapsize;
b4c24cb9
A
578 vm_object_unlock(object);
579 vm_object_deallocate(object);
580 return kr;
581 }
582 newentry->object.vm_object = object;
583 newentry->offset = 0;
584 assert (newentry->wired_count == 0);
585
586
587 /* add an extra reference in case we have someone doing an */
588 /* unexpected deallocate */
589 vm_object_reference(object);
1c79356b
A
590 vm_map_unlock(map);
591
91447636
A
592 kr = vm_map_wire(map, newmapaddr, newmapaddr + newmapsize, VM_PROT_DEFAULT, FALSE);
593 if (KERN_SUCCESS != kr) {
594 vm_map_remove(map, newmapaddr, newmapaddr + newmapsize, 0);
b4c24cb9 595 vm_object_lock(object);
91447636 596 for(offset = oldsize; offset < newmapsize; offset += PAGE_SIZE) {
b4c24cb9 597 if ((mem = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
b0d623f7 598 VM_PAGE_FREE(mem);
b4c24cb9
A
599 }
600 }
6d2010ae 601 object->vo_size = oldmapsize;
b4c24cb9
A
602 vm_object_unlock(object);
603 vm_object_deallocate(object);
604 return (kr);
605 }
606 vm_object_deallocate(object);
1c79356b 607
91447636 608 *newaddrp = CAST_DOWN(vm_offset_t, newmapaddr);
1c79356b
A
609 return KERN_SUCCESS;
610}
611
612/*
b0d623f7 613 * kmem_alloc_kobject:
1c79356b
A
614 *
615 * Allocate wired-down memory in the kernel's address map
616 * or a submap. The memory is not zero-filled.
617 *
618 * The memory is allocated in the kernel_object.
619 * It may not be copied with vm_map_copy, and
620 * it may not be reallocated with kmem_realloc.
621 */
622
623kern_return_t
b0d623f7 624kmem_alloc_kobject(
1c79356b
A
625 vm_map_t map,
626 vm_offset_t *addrp,
627 vm_size_t size)
628{
629 return kernel_memory_allocate(map, addrp, size, 0, KMA_KOBJECT);
630}
631
632/*
633 * kmem_alloc_aligned:
634 *
b0d623f7 635 * Like kmem_alloc_kobject, except that the memory is aligned.
1c79356b
A
636 * The size should be a power-of-2.
637 */
638
639kern_return_t
640kmem_alloc_aligned(
641 vm_map_t map,
642 vm_offset_t *addrp,
643 vm_size_t size)
644{
645 if ((size & (size - 1)) != 0)
646 panic("kmem_alloc_aligned: size not aligned");
647 return kernel_memory_allocate(map, addrp, size, size - 1, KMA_KOBJECT);
648}
649
650/*
651 * kmem_alloc_pageable:
652 *
653 * Allocate pageable memory in the kernel's address map.
654 */
655
656kern_return_t
657kmem_alloc_pageable(
658 vm_map_t map,
659 vm_offset_t *addrp,
660 vm_size_t size)
661{
91447636
A
662 vm_map_offset_t map_addr;
663 vm_map_size_t map_size;
1c79356b
A
664 kern_return_t kr;
665
666#ifndef normal
91447636 667 map_addr = (vm_map_min(map)) + 0x1000;
1c79356b 668#else
91447636 669 map_addr = vm_map_min(map);
1c79356b 670#endif
91447636
A
671 map_size = vm_map_round_page(size);
672
673 kr = vm_map_enter(map, &map_addr, map_size,
674 (vm_map_offset_t) 0, VM_FLAGS_ANYWHERE,
1c79356b
A
675 VM_OBJECT_NULL, (vm_object_offset_t) 0, FALSE,
676 VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT);
91447636 677
1c79356b
A
678 if (kr != KERN_SUCCESS)
679 return kr;
680
91447636 681 *addrp = CAST_DOWN(vm_offset_t, map_addr);
1c79356b
A
682 return KERN_SUCCESS;
683}
684
685/*
686 * kmem_free:
687 *
688 * Release a region of kernel virtual memory allocated
b0d623f7 689 * with kmem_alloc, kmem_alloc_kobject, or kmem_alloc_pageable,
1c79356b
A
690 * and return the physical pages associated with that region.
691 */
692
693void
694kmem_free(
695 vm_map_t map,
696 vm_offset_t addr,
697 vm_size_t size)
698{
699 kern_return_t kr;
700
b0d623f7
A
701 assert(addr >= VM_MIN_KERNEL_AND_KEXT_ADDRESS);
702
2d21ac55
A
703 TRACE_MACHLEAKS(KMEM_FREE_CODE, KMEM_FREE_CODE_2, size, addr);
704
b0d623f7
A
705 if(size == 0) {
706#if MACH_ASSERT
707 printf("kmem_free called with size==0 for map: %p with addr: 0x%llx\n",map,(uint64_t)addr);
708#endif
709 return;
710 }
711
91447636
A
712 kr = vm_map_remove(map, vm_map_trunc_page(addr),
713 vm_map_round_page(addr + size),
55e303ae 714 VM_MAP_REMOVE_KUNWIRE);
1c79356b
A
715 if (kr != KERN_SUCCESS)
716 panic("kmem_free");
717}
718
719/*
b4c24cb9 720 * Allocate new pages in an object.
1c79356b
A
721 */
722
723kern_return_t
724kmem_alloc_pages(
725 register vm_object_t object,
726 register vm_object_offset_t offset,
91447636 727 register vm_object_size_t size)
1c79356b 728{
91447636 729 vm_object_size_t alloc_size;
1c79356b 730
91447636 731 alloc_size = vm_object_round_page(size);
b4c24cb9 732 vm_object_lock(object);
91447636 733 while (alloc_size) {
1c79356b
A
734 register vm_page_t mem;
735
1c79356b
A
736
737 /*
738 * Allocate a page
739 */
91447636
A
740 while (VM_PAGE_NULL ==
741 (mem = vm_page_alloc(object, offset))) {
1c79356b
A
742 vm_object_unlock(object);
743 VM_PAGE_WAIT();
744 vm_object_lock(object);
745 }
91447636 746 mem->busy = FALSE;
1c79356b 747
91447636 748 alloc_size -= PAGE_SIZE;
b4c24cb9 749 offset += PAGE_SIZE;
1c79356b 750 }
b4c24cb9 751 vm_object_unlock(object);
1c79356b
A
752 return KERN_SUCCESS;
753}
754
755/*
756 * Remap wired pages in an object into a new region.
757 * The object is assumed to be mapped into the kernel map or
758 * a submap.
759 */
760void
761kmem_remap_pages(
762 register vm_object_t object,
763 register vm_object_offset_t offset,
764 register vm_offset_t start,
765 register vm_offset_t end,
766 vm_prot_t protection)
767{
91447636
A
768
769 vm_map_offset_t map_start;
770 vm_map_offset_t map_end;
771
1c79356b
A
772 /*
773 * Mark the pmap region as not pageable.
774 */
91447636
A
775 map_start = vm_map_trunc_page(start);
776 map_end = vm_map_round_page(end);
1c79356b 777
91447636
A
778 pmap_pageable(kernel_pmap, map_start, map_end, FALSE);
779
780 while (map_start < map_end) {
1c79356b
A
781 register vm_page_t mem;
782
783 vm_object_lock(object);
784
785 /*
786 * Find a page
787 */
788 if ((mem = vm_page_lookup(object, offset)) == VM_PAGE_NULL)
789 panic("kmem_remap_pages");
790
791 /*
792 * Wire it down (again)
793 */
2d21ac55 794 vm_page_lockspin_queues();
1c79356b
A
795 vm_page_wire(mem);
796 vm_page_unlock_queues();
797 vm_object_unlock(object);
798
91447636
A
799 /*
800 * ENCRYPTED SWAP:
801 * The page is supposed to be wired now, so it
802 * shouldn't be encrypted at this point. It can
803 * safely be entered in the page table.
804 */
805 ASSERT_PAGE_DECRYPTED(mem);
806
1c79356b
A
807 /*
808 * Enter it in the kernel pmap. The page isn't busy,
809 * but this shouldn't be a problem because it is wired.
810 */
b0d623f7
A
811
812 mem->pmapped = TRUE;
813 mem->wpmapped = TRUE;
814
6d2010ae 815 PMAP_ENTER(kernel_pmap, map_start, mem, protection, 0, TRUE);
1c79356b 816
91447636 817 map_start += PAGE_SIZE;
1c79356b
A
818 offset += PAGE_SIZE;
819 }
820}
821
822/*
823 * kmem_suballoc:
824 *
825 * Allocates a map to manage a subrange
826 * of the kernel virtual address space.
827 *
828 * Arguments are as follows:
829 *
830 * parent Map to take range from
831 * addr Address of start of range (IN/OUT)
832 * size Size of range to find
833 * pageable Can region be paged
834 * anywhere Can region be located anywhere in map
835 * new_map Pointer to new submap
836 */
837kern_return_t
838kmem_suballoc(
839 vm_map_t parent,
840 vm_offset_t *addr,
841 vm_size_t size,
842 boolean_t pageable,
91447636 843 int flags,
1c79356b
A
844 vm_map_t *new_map)
845{
91447636
A
846 vm_map_t map;
847 vm_map_offset_t map_addr;
848 vm_map_size_t map_size;
849 kern_return_t kr;
1c79356b 850
91447636 851 map_size = vm_map_round_page(size);
1c79356b
A
852
853 /*
854 * Need reference on submap object because it is internal
855 * to the vm_system. vm_object_enter will never be called
856 * on it (usual source of reference for vm_map_enter).
857 */
858 vm_object_reference(vm_submap_object);
859
91447636
A
860 map_addr = (flags & VM_FLAGS_ANYWHERE) ?
861 vm_map_min(parent) : vm_map_trunc_page(*addr);
862
863 kr = vm_map_enter(parent, &map_addr, map_size,
864 (vm_map_offset_t) 0, flags,
1c79356b
A
865 vm_submap_object, (vm_object_offset_t) 0, FALSE,
866 VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT);
867 if (kr != KERN_SUCCESS) {
868 vm_object_deallocate(vm_submap_object);
869 return (kr);
870 }
871
872 pmap_reference(vm_map_pmap(parent));
91447636 873 map = vm_map_create(vm_map_pmap(parent), map_addr, map_addr + map_size, pageable);
1c79356b
A
874 if (map == VM_MAP_NULL)
875 panic("kmem_suballoc: vm_map_create failed"); /* "can't happen" */
876
91447636 877 kr = vm_map_submap(parent, map_addr, map_addr + map_size, map, map_addr, FALSE);
1c79356b
A
878 if (kr != KERN_SUCCESS) {
879 /*
880 * See comment preceding vm_map_submap().
881 */
91447636 882 vm_map_remove(parent, map_addr, map_addr + map_size, VM_MAP_NO_FLAGS);
1c79356b
A
883 vm_map_deallocate(map); /* also removes ref to pmap */
884 vm_object_deallocate(vm_submap_object);
885 return (kr);
886 }
91447636 887 *addr = CAST_DOWN(vm_offset_t, map_addr);
1c79356b
A
888 *new_map = map;
889 return (KERN_SUCCESS);
890}
891
892/*
893 * kmem_init:
894 *
895 * Initialize the kernel's virtual memory map, taking
896 * into account all memory allocated up to this time.
897 */
898void
899kmem_init(
900 vm_offset_t start,
901 vm_offset_t end)
902{
91447636
A
903 vm_map_offset_t map_start;
904 vm_map_offset_t map_end;
905
906 map_start = vm_map_trunc_page(start);
907 map_end = vm_map_round_page(end);
908
6d2010ae 909 kernel_map = vm_map_create(pmap_kernel(),VM_MIN_KERNEL_AND_KEXT_ADDRESS,
0c530ab8 910 map_end, FALSE);
1c79356b
A
911 /*
912 * Reserve virtual memory allocated up to this time.
913 */
6d2010ae 914 if (start != VM_MIN_KERNEL_AND_KEXT_ADDRESS) {
91447636 915 vm_map_offset_t map_addr;
6d2010ae 916 kern_return_t kr;
0c530ab8 917
6d2010ae
A
918 map_addr = VM_MIN_KERNEL_AND_KEXT_ADDRESS;
919 kr = vm_map_enter(kernel_map,
920 &map_addr,
921 (vm_map_size_t)(map_start - VM_MIN_KERNEL_AND_KEXT_ADDRESS),
922 (vm_map_offset_t) 0,
923 VM_FLAGS_FIXED | VM_FLAGS_NO_PMAP_CHECK,
924 VM_OBJECT_NULL,
925 (vm_object_offset_t) 0, FALSE,
926 VM_PROT_NONE, VM_PROT_NONE,
927 VM_INHERIT_DEFAULT);
928
929 if (kr != KERN_SUCCESS) {
930 panic("kmem_init(0x%llx,0x%llx): vm_map_enter(0x%llx,0x%llx) error 0x%x\n",
931 (uint64_t) start, (uint64_t) end,
932 (uint64_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS,
933 (uint64_t) (map_start - VM_MIN_KERNEL_AND_KEXT_ADDRESS),
934 kr);
935 }
1c79356b 936 }
6d2010ae 937
2d21ac55
A
938 /*
939 * Set the default global user wire limit which limits the amount of
b0d623f7
A
940 * memory that can be locked via mlock(). We set this to the total
941 * amount of memory that are potentially usable by a user app (max_mem)
942 * minus a certain amount. This can be overridden via a sysctl.
2d21ac55 943 */
b0d623f7
A
944 vm_global_no_user_wire_amount = MIN(max_mem*20/100,
945 VM_NOT_USER_WIREABLE);
946 vm_global_user_wire_limit = max_mem - vm_global_no_user_wire_amount;
2d21ac55 947
b0d623f7
A
948 /* the default per user limit is the same as the global limit */
949 vm_user_wire_limit = vm_global_user_wire_limit;
1c79356b
A
950}
951
1c79356b 952
1c79356b
A
953/*
954 * Routine: copyinmap
955 * Purpose:
956 * Like copyin, except that fromaddr is an address
957 * in the specified VM map. This implementation
958 * is incomplete; it handles the current user map
959 * and the kernel map/submaps.
960 */
91447636 961kern_return_t
1c79356b 962copyinmap(
91447636
A
963 vm_map_t map,
964 vm_map_offset_t fromaddr,
965 void *todata,
966 vm_size_t length)
1c79356b 967{
91447636
A
968 kern_return_t kr = KERN_SUCCESS;
969 vm_map_t oldmap;
970
971 if (vm_map_pmap(map) == pmap_kernel())
972 {
1c79356b 973 /* assume a correct copy */
91447636
A
974 memcpy(todata, CAST_DOWN(void *, fromaddr), length);
975 }
976 else if (current_map() == map)
977 {
978 if (copyin(fromaddr, todata, length) != 0)
979 kr = KERN_INVALID_ADDRESS;
1c79356b 980 }
91447636
A
981 else
982 {
983 vm_map_reference(map);
984 oldmap = vm_map_switch(map);
985 if (copyin(fromaddr, todata, length) != 0)
986 kr = KERN_INVALID_ADDRESS;
987 vm_map_switch(oldmap);
988 vm_map_deallocate(map);
989 }
990 return kr;
1c79356b
A
991}
992
993/*
994 * Routine: copyoutmap
995 * Purpose:
996 * Like copyout, except that toaddr is an address
997 * in the specified VM map. This implementation
998 * is incomplete; it handles the current user map
999 * and the kernel map/submaps.
1000 */
91447636 1001kern_return_t
1c79356b 1002copyoutmap(
91447636
A
1003 vm_map_t map,
1004 void *fromdata,
1005 vm_map_address_t toaddr,
1006 vm_size_t length)
1c79356b
A
1007{
1008 if (vm_map_pmap(map) == pmap_kernel()) {
1009 /* assume a correct copy */
91447636
A
1010 memcpy(CAST_DOWN(void *, toaddr), fromdata, length);
1011 return KERN_SUCCESS;
1c79356b
A
1012 }
1013
91447636
A
1014 if (current_map() != map)
1015 return KERN_NOT_SUPPORTED;
1016
1017 if (copyout(fromdata, toaddr, length) != 0)
1018 return KERN_INVALID_ADDRESS;
1c79356b 1019
91447636 1020 return KERN_SUCCESS;
1c79356b 1021}
9bccf70c
A
1022
1023
1024kern_return_t
1025vm_conflict_check(
1026 vm_map_t map,
91447636
A
1027 vm_map_offset_t off,
1028 vm_map_size_t len,
1029 memory_object_t pager,
9bccf70c
A
1030 vm_object_offset_t file_off)
1031{
1032 vm_map_entry_t entry;
1033 vm_object_t obj;
1034 vm_object_offset_t obj_off;
1035 vm_map_t base_map;
91447636
A
1036 vm_map_offset_t base_offset;
1037 vm_map_offset_t original_offset;
9bccf70c 1038 kern_return_t kr;
91447636 1039 vm_map_size_t local_len;
9bccf70c
A
1040
1041 base_map = map;
1042 base_offset = off;
1043 original_offset = off;
1044 kr = KERN_SUCCESS;
1045 vm_map_lock(map);
1046 while(vm_map_lookup_entry(map, off, &entry)) {
1047 local_len = len;
1048
1049 if (entry->object.vm_object == VM_OBJECT_NULL) {
1050 vm_map_unlock(map);
1051 return KERN_SUCCESS;
1052 }
1053 if (entry->is_sub_map) {
1054 vm_map_t old_map;
55e303ae 1055
9bccf70c
A
1056 old_map = map;
1057 vm_map_lock(entry->object.sub_map);
1058 map = entry->object.sub_map;
1059 off = entry->offset + (off - entry->vme_start);
1060 vm_map_unlock(old_map);
1061 continue;
1062 }
1063 obj = entry->object.vm_object;
1064 obj_off = (off - entry->vme_start) + entry->offset;
1065 while(obj->shadow) {
6d2010ae 1066 obj_off += obj->vo_shadow_offset;
9bccf70c
A
1067 obj = obj->shadow;
1068 }
1069 if((obj->pager_created) && (obj->pager == pager)) {
1070 if(((obj->paging_offset) + obj_off) == file_off) {
1071 if(off != base_offset) {
1072 vm_map_unlock(map);
1073 return KERN_FAILURE;
1074 }
1075 kr = KERN_ALREADY_WAITING;
55e303ae
A
1076 } else {
1077 vm_object_offset_t obj_off_aligned;
1078 vm_object_offset_t file_off_aligned;
1079
1080 obj_off_aligned = obj_off & ~PAGE_MASK;
1081 file_off_aligned = file_off & ~PAGE_MASK;
1082
1083 if (file_off_aligned == (obj->paging_offset + obj_off_aligned)) {
1084 /*
1085 * the target map and the file offset start in the same page
1086 * but are not identical...
1087 */
1088 vm_map_unlock(map);
1089 return KERN_FAILURE;
1090 }
1091 if ((file_off < (obj->paging_offset + obj_off_aligned)) &&
1092 ((file_off + len) > (obj->paging_offset + obj_off_aligned))) {
1093 /*
1094 * some portion of the tail of the I/O will fall
1095 * within the encompass of the target map
1096 */
1097 vm_map_unlock(map);
1098 return KERN_FAILURE;
1099 }
1100 if ((file_off_aligned > (obj->paging_offset + obj_off)) &&
1101 (file_off_aligned < (obj->paging_offset + obj_off) + len)) {
1102 /*
1103 * the beginning page of the file offset falls within
1104 * the target map's encompass
1105 */
1106 vm_map_unlock(map);
1107 return KERN_FAILURE;
1108 }
9bccf70c
A
1109 }
1110 } else if(kr != KERN_SUCCESS) {
55e303ae 1111 vm_map_unlock(map);
9bccf70c
A
1112 return KERN_FAILURE;
1113 }
1114
55e303ae 1115 if(len <= ((entry->vme_end - entry->vme_start) -
9bccf70c
A
1116 (off - entry->vme_start))) {
1117 vm_map_unlock(map);
1118 return kr;
1119 } else {
1120 len -= (entry->vme_end - entry->vme_start) -
1121 (off - entry->vme_start);
1122 }
1123 base_offset = base_offset + (local_len - len);
1124 file_off = file_off + (local_len - len);
1125 off = base_offset;
1126 if(map != base_map) {
1127 vm_map_unlock(map);
1128 vm_map_lock(base_map);
1129 map = base_map;
1130 }
1131 }
1132
1133 vm_map_unlock(map);
1134 return kr;
9bccf70c 1135}