]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_kern.c
4d4ee31cb09e79d02445622f8f183333fb7c87f0
[apple/xnu.git] / osfmk / vm / vm_kern.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * File: vm/vm_kern.c
54 * Author: Avadis Tevanian, Jr., Michael Wayne Young
55 * Date: 1985
56 *
57 * Kernel memory management.
58 */
59
60 #include <cpus.h>
61 #include <mach/kern_return.h>
62 #include <mach/vm_param.h>
63 #include <kern/assert.h>
64 #include <kern/lock.h>
65 #include <kern/thread.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pageout.h>
71 #include <kern/misc_protos.h>
72 #include <vm/cpm.h>
73
74 #include <string.h>
75 /*
76 * Variables exported by this module.
77 */
78
79 vm_map_t kernel_map;
80 vm_map_t kernel_pageable_map;
81
82 /*
83 * Forward declarations for internal functions.
84 */
85 extern kern_return_t kmem_alloc_pages(
86 register vm_object_t object,
87 register vm_object_offset_t offset,
88 register vm_size_t size);
89
90 extern void kmem_remap_pages(
91 register vm_object_t object,
92 register vm_object_offset_t offset,
93 register vm_offset_t start,
94 register vm_offset_t end,
95 vm_prot_t protection);
96
97 kern_return_t
98 kmem_alloc_contig(
99 vm_map_t map,
100 vm_offset_t *addrp,
101 vm_size_t size,
102 vm_offset_t mask,
103 int flags)
104 {
105 vm_object_t object;
106 vm_page_t m, pages;
107 kern_return_t kr;
108 vm_offset_t addr, i;
109 vm_object_offset_t offset;
110 vm_map_entry_t entry;
111
112 if (map == VM_MAP_NULL || (flags && (flags ^ KMA_KOBJECT)))
113 return KERN_INVALID_ARGUMENT;
114
115 if (size == 0) {
116 *addrp = 0;
117 return KERN_INVALID_ARGUMENT;
118 }
119
120 size = round_page(size);
121 if ((flags & KMA_KOBJECT) == 0) {
122 object = vm_object_allocate(size);
123 kr = vm_map_find_space(map, &addr, size, mask, &entry);
124 }
125 else {
126 object = kernel_object;
127 kr = vm_map_find_space(map, &addr, size, mask, &entry);
128 }
129
130 if ((flags & KMA_KOBJECT) == 0) {
131 entry->object.vm_object = object;
132 entry->offset = offset = 0;
133 } else {
134 offset = addr - VM_MIN_KERNEL_ADDRESS;
135
136 if (entry->object.vm_object == VM_OBJECT_NULL) {
137 vm_object_reference(object);
138 entry->object.vm_object = object;
139 entry->offset = offset;
140 }
141 }
142
143 if (kr != KERN_SUCCESS) {
144 if ((flags & KMA_KOBJECT) == 0)
145 vm_object_deallocate(object);
146 return kr;
147 }
148
149 vm_map_unlock(map);
150
151 kr = cpm_allocate(size, &pages, FALSE);
152
153 if (kr != KERN_SUCCESS) {
154 vm_map_remove(map, addr, addr + size, 0);
155 *addrp = 0;
156 return kr;
157 }
158
159 vm_object_lock(object);
160 for (i = 0; i < size; i += PAGE_SIZE) {
161 m = pages;
162 pages = NEXT_PAGE(m);
163 m->busy = FALSE;
164 vm_page_insert(m, object, offset + i);
165 }
166 vm_object_unlock(object);
167
168 if ((kr = vm_map_wire(map, addr, addr + size, VM_PROT_DEFAULT, FALSE))
169 != KERN_SUCCESS) {
170 if (object == kernel_object) {
171 vm_object_lock(object);
172 vm_object_page_remove(object, offset, offset + size);
173 vm_object_unlock(object);
174 }
175 vm_map_remove(map, addr, addr + size, 0);
176 return kr;
177 }
178 if (object == kernel_object)
179 vm_map_simplify(map, addr);
180
181 *addrp = addr;
182 return KERN_SUCCESS;
183 }
184
185 /*
186 * Master entry point for allocating kernel memory.
187 * NOTE: this routine is _never_ interrupt safe.
188 *
189 * map : map to allocate into
190 * addrp : pointer to start address of new memory
191 * size : size of memory requested
192 * flags : options
193 * KMA_HERE *addrp is base address, else "anywhere"
194 * KMA_NOPAGEWAIT don't wait for pages if unavailable
195 * KMA_KOBJECT use kernel_object
196 */
197
198 kern_return_t
199 kernel_memory_allocate(
200 register vm_map_t map,
201 register vm_offset_t *addrp,
202 register vm_size_t size,
203 register vm_offset_t mask,
204 int flags)
205 {
206 vm_object_t object = VM_OBJECT_NULL;
207 vm_map_entry_t entry;
208 vm_object_offset_t offset;
209 vm_offset_t addr;
210 vm_offset_t i;
211 kern_return_t kr;
212
213 size = round_page(size);
214 if ((flags & KMA_KOBJECT) == 0) {
215 /*
216 * Allocate a new object. We must do this before locking
217 * the map, or risk deadlock with the default pager:
218 * device_read_alloc uses kmem_alloc,
219 * which tries to allocate an object,
220 * which uses kmem_alloc_wired to get memory,
221 * which blocks for pages.
222 * then the default pager needs to read a block
223 * to process a memory_object_data_write,
224 * and device_read_alloc calls kmem_alloc
225 * and deadlocks on the map lock.
226 */
227 object = vm_object_allocate(size);
228 kr = vm_map_find_space(map, &addr, size, mask, &entry);
229 }
230 else {
231 object = kernel_object;
232 kr = vm_map_find_space(map, &addr, size, mask, &entry);
233 }
234 if (kr != KERN_SUCCESS) {
235 if ((flags & KMA_KOBJECT) == 0)
236 vm_object_deallocate(object);
237 return kr;
238 }
239
240 if ((flags & KMA_KOBJECT) == 0) {
241 entry->object.vm_object = object;
242 entry->offset = offset = 0;
243 } else {
244 offset = addr - VM_MIN_KERNEL_ADDRESS;
245
246 if (entry->object.vm_object == VM_OBJECT_NULL) {
247 vm_object_reference(object);
248 entry->object.vm_object = object;
249 entry->offset = offset;
250 }
251 }
252
253 /*
254 * Since we have not given out this address yet,
255 * it is safe to unlock the map. Except of course
256 * we must make certain no one coalesces our address
257 * or does a blind vm_deallocate and removes the object
258 * an extra object reference will suffice to protect
259 * against both contingencies.
260 */
261 vm_object_reference(object);
262 vm_map_unlock(map);
263
264 vm_object_lock(object);
265 for (i = 0; i < size; i += PAGE_SIZE) {
266 vm_page_t mem;
267
268 while ((mem = vm_page_alloc(object,
269 offset + (vm_object_offset_t)i))
270 == VM_PAGE_NULL) {
271 if (flags & KMA_NOPAGEWAIT) {
272 if (object == kernel_object)
273 vm_object_page_remove(object, offset,
274 offset + (vm_object_offset_t)i);
275 vm_object_unlock(object);
276 vm_map_remove(map, addr, addr + size, 0);
277 vm_object_deallocate(object);
278 return KERN_RESOURCE_SHORTAGE;
279 }
280 vm_object_unlock(object);
281 VM_PAGE_WAIT();
282 vm_object_lock(object);
283 }
284 mem->busy = FALSE;
285 }
286 vm_object_unlock(object);
287
288 if ((kr = vm_map_wire(map, addr, addr + size, VM_PROT_DEFAULT, FALSE))
289 != KERN_SUCCESS) {
290 if (object == kernel_object) {
291 vm_object_lock(object);
292 vm_object_page_remove(object, offset, offset + size);
293 vm_object_unlock(object);
294 }
295 vm_map_remove(map, addr, addr + size, 0);
296 vm_object_deallocate(object);
297 return (kr);
298 }
299 /* now that the page is wired, we no longer have to fear coalesce */
300 vm_object_deallocate(object);
301 if (object == kernel_object)
302 vm_map_simplify(map, addr);
303
304 /*
305 * Return the memory, not zeroed.
306 */
307 #if (NCPUS > 1) && i860
308 bzero( addr, size );
309 #endif /* #if (NCPUS > 1) && i860 */
310 *addrp = addr;
311 return KERN_SUCCESS;
312 }
313
314 /*
315 * kmem_alloc:
316 *
317 * Allocate wired-down memory in the kernel's address map
318 * or a submap. The memory is not zero-filled.
319 */
320
321 kern_return_t
322 kmem_alloc(
323 vm_map_t map,
324 vm_offset_t *addrp,
325 vm_size_t size)
326 {
327 return kernel_memory_allocate(map, addrp, size, 0, 0);
328 }
329
330 /*
331 * kmem_realloc:
332 *
333 * Reallocate wired-down memory in the kernel's address map
334 * or a submap. Newly allocated pages are not zeroed.
335 * This can only be used on regions allocated with kmem_alloc.
336 *
337 * If successful, the pages in the old region are mapped twice.
338 * The old region is unchanged. Use kmem_free to get rid of it.
339 */
340 kern_return_t
341 kmem_realloc(
342 vm_map_t map,
343 vm_offset_t oldaddr,
344 vm_size_t oldsize,
345 vm_offset_t *newaddrp,
346 vm_size_t newsize)
347 {
348 vm_offset_t oldmin, oldmax;
349 vm_offset_t newaddr;
350 vm_offset_t offset;
351 vm_object_t object;
352 vm_map_entry_t oldentry, newentry;
353 vm_page_t mem;
354 kern_return_t kr;
355
356 oldmin = trunc_page(oldaddr);
357 oldmax = round_page(oldaddr + oldsize);
358 oldsize = oldmax - oldmin;
359 newsize = round_page(newsize);
360
361
362 /*
363 * Find the VM object backing the old region.
364 */
365
366 vm_map_lock(map);
367
368 if (!vm_map_lookup_entry(map, oldmin, &oldentry))
369 panic("kmem_realloc");
370 object = oldentry->object.vm_object;
371
372 /*
373 * Increase the size of the object and
374 * fill in the new region.
375 */
376
377 vm_object_reference(object);
378 /* by grabbing the object lock before unlocking the map */
379 /* we guarantee that we will panic if more than one */
380 /* attempt is made to realloc a kmem_alloc'd area */
381 vm_object_lock(object);
382 vm_map_unlock(map);
383 if (object->size != oldsize)
384 panic("kmem_realloc");
385 object->size = newsize;
386 vm_object_unlock(object);
387
388 /* allocate the new pages while expanded portion of the */
389 /* object is still not mapped */
390 kmem_alloc_pages(object, oldsize, newsize-oldsize);
391
392
393 /*
394 * Find space for the new region.
395 */
396
397 kr = vm_map_find_space(map, &newaddr, newsize, (vm_offset_t) 0,
398 &newentry);
399 if (kr != KERN_SUCCESS) {
400 vm_object_lock(object);
401 for(offset = oldsize;
402 offset<newsize; offset+=PAGE_SIZE) {
403 if ((mem = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
404 vm_page_lock_queues();
405 vm_page_free(mem);
406 vm_page_unlock_queues();
407 }
408 }
409 object->size = oldsize;
410 vm_object_unlock(object);
411 vm_object_deallocate(object);
412 return kr;
413 }
414 newentry->object.vm_object = object;
415 newentry->offset = 0;
416 assert (newentry->wired_count == 0);
417
418
419 /* add an extra reference in case we have someone doing an */
420 /* unexpected deallocate */
421 vm_object_reference(object);
422 vm_map_unlock(map);
423
424 if ((kr = vm_map_wire(map, newaddr, newaddr + newsize,
425 VM_PROT_DEFAULT, FALSE)) != KERN_SUCCESS) {
426 vm_map_remove(map, newaddr, newaddr + newsize, 0);
427 vm_object_lock(object);
428 for(offset = oldsize;
429 offset<newsize; offset+=PAGE_SIZE) {
430 if ((mem = vm_page_lookup(object, offset)) != VM_PAGE_NULL) {
431 vm_page_lock_queues();
432 vm_page_free(mem);
433 vm_page_unlock_queues();
434 }
435 }
436 object->size = oldsize;
437 vm_object_unlock(object);
438 vm_object_deallocate(object);
439 return (kr);
440 }
441 vm_object_deallocate(object);
442
443
444 *newaddrp = newaddr;
445 return KERN_SUCCESS;
446 }
447
448 /*
449 * kmem_alloc_wired:
450 *
451 * Allocate wired-down memory in the kernel's address map
452 * or a submap. The memory is not zero-filled.
453 *
454 * The memory is allocated in the kernel_object.
455 * It may not be copied with vm_map_copy, and
456 * it may not be reallocated with kmem_realloc.
457 */
458
459 kern_return_t
460 kmem_alloc_wired(
461 vm_map_t map,
462 vm_offset_t *addrp,
463 vm_size_t size)
464 {
465 return kernel_memory_allocate(map, addrp, size, 0, KMA_KOBJECT);
466 }
467
468 /*
469 * kmem_alloc_aligned:
470 *
471 * Like kmem_alloc_wired, except that the memory is aligned.
472 * The size should be a power-of-2.
473 */
474
475 kern_return_t
476 kmem_alloc_aligned(
477 vm_map_t map,
478 vm_offset_t *addrp,
479 vm_size_t size)
480 {
481 if ((size & (size - 1)) != 0)
482 panic("kmem_alloc_aligned: size not aligned");
483 return kernel_memory_allocate(map, addrp, size, size - 1, KMA_KOBJECT);
484 }
485
486 /*
487 * kmem_alloc_pageable:
488 *
489 * Allocate pageable memory in the kernel's address map.
490 */
491
492 kern_return_t
493 kmem_alloc_pageable(
494 vm_map_t map,
495 vm_offset_t *addrp,
496 vm_size_t size)
497 {
498 vm_offset_t addr;
499 kern_return_t kr;
500
501 #ifndef normal
502 addr = (vm_map_min(map)) + 0x1000;
503 #else
504 addr = vm_map_min(map);
505 #endif
506 kr = vm_map_enter(map, &addr, round_page(size),
507 (vm_offset_t) 0, TRUE,
508 VM_OBJECT_NULL, (vm_object_offset_t) 0, FALSE,
509 VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT);
510 if (kr != KERN_SUCCESS)
511 return kr;
512
513 *addrp = addr;
514 return KERN_SUCCESS;
515 }
516
517 /*
518 * kmem_free:
519 *
520 * Release a region of kernel virtual memory allocated
521 * with kmem_alloc, kmem_alloc_wired, or kmem_alloc_pageable,
522 * and return the physical pages associated with that region.
523 */
524
525 void
526 kmem_free(
527 vm_map_t map,
528 vm_offset_t addr,
529 vm_size_t size)
530 {
531 kern_return_t kr;
532
533 kr = vm_map_remove(map, trunc_page(addr),
534 round_page(addr + size), VM_MAP_REMOVE_KUNWIRE);
535 if (kr != KERN_SUCCESS)
536 panic("kmem_free");
537 }
538
539 /*
540 * Allocate new pages in an object.
541 */
542
543 kern_return_t
544 kmem_alloc_pages(
545 register vm_object_t object,
546 register vm_object_offset_t offset,
547 register vm_size_t size)
548 {
549
550 size = round_page(size);
551 vm_object_lock(object);
552 while (size) {
553 register vm_page_t mem;
554
555
556 /*
557 * Allocate a page
558 */
559 while ((mem = vm_page_alloc(object, offset))
560 == VM_PAGE_NULL) {
561 vm_object_unlock(object);
562 VM_PAGE_WAIT();
563 vm_object_lock(object);
564 }
565
566
567 offset += PAGE_SIZE;
568 size -= PAGE_SIZE;
569 mem->busy = FALSE;
570 }
571 vm_object_unlock(object);
572 return KERN_SUCCESS;
573 }
574
575 /*
576 * Remap wired pages in an object into a new region.
577 * The object is assumed to be mapped into the kernel map or
578 * a submap.
579 */
580 void
581 kmem_remap_pages(
582 register vm_object_t object,
583 register vm_object_offset_t offset,
584 register vm_offset_t start,
585 register vm_offset_t end,
586 vm_prot_t protection)
587 {
588 /*
589 * Mark the pmap region as not pageable.
590 */
591 pmap_pageable(kernel_pmap, start, end, FALSE);
592
593 while (start < end) {
594 register vm_page_t mem;
595
596 vm_object_lock(object);
597
598 /*
599 * Find a page
600 */
601 if ((mem = vm_page_lookup(object, offset)) == VM_PAGE_NULL)
602 panic("kmem_remap_pages");
603
604 /*
605 * Wire it down (again)
606 */
607 vm_page_lock_queues();
608 vm_page_wire(mem);
609 vm_page_unlock_queues();
610 vm_object_unlock(object);
611
612 /*
613 * Enter it in the kernel pmap. The page isn't busy,
614 * but this shouldn't be a problem because it is wired.
615 */
616 PMAP_ENTER(kernel_pmap, start, mem, protection,
617 VM_WIMG_USE_DEFAULT, TRUE);
618
619 start += PAGE_SIZE;
620 offset += PAGE_SIZE;
621 }
622 }
623
624 /*
625 * kmem_suballoc:
626 *
627 * Allocates a map to manage a subrange
628 * of the kernel virtual address space.
629 *
630 * Arguments are as follows:
631 *
632 * parent Map to take range from
633 * addr Address of start of range (IN/OUT)
634 * size Size of range to find
635 * pageable Can region be paged
636 * anywhere Can region be located anywhere in map
637 * new_map Pointer to new submap
638 */
639 kern_return_t
640 kmem_suballoc(
641 vm_map_t parent,
642 vm_offset_t *addr,
643 vm_size_t size,
644 boolean_t pageable,
645 boolean_t anywhere,
646 vm_map_t *new_map)
647 {
648 vm_map_t map;
649 kern_return_t kr;
650
651 size = round_page(size);
652
653 /*
654 * Need reference on submap object because it is internal
655 * to the vm_system. vm_object_enter will never be called
656 * on it (usual source of reference for vm_map_enter).
657 */
658 vm_object_reference(vm_submap_object);
659
660 if (anywhere == TRUE)
661 *addr = (vm_offset_t)vm_map_min(parent);
662 kr = vm_map_enter(parent, addr, size,
663 (vm_offset_t) 0, anywhere,
664 vm_submap_object, (vm_object_offset_t) 0, FALSE,
665 VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT);
666 if (kr != KERN_SUCCESS) {
667 vm_object_deallocate(vm_submap_object);
668 return (kr);
669 }
670
671 pmap_reference(vm_map_pmap(parent));
672 map = vm_map_create(vm_map_pmap(parent), *addr, *addr + size, pageable);
673 if (map == VM_MAP_NULL)
674 panic("kmem_suballoc: vm_map_create failed"); /* "can't happen" */
675
676 kr = vm_map_submap(parent, *addr, *addr + size, map, *addr, FALSE);
677 if (kr != KERN_SUCCESS) {
678 /*
679 * See comment preceding vm_map_submap().
680 */
681 vm_map_remove(parent, *addr, *addr + size, VM_MAP_NO_FLAGS);
682 vm_map_deallocate(map); /* also removes ref to pmap */
683 vm_object_deallocate(vm_submap_object);
684 return (kr);
685 }
686 *new_map = map;
687 return (KERN_SUCCESS);
688 }
689
690 /*
691 * kmem_init:
692 *
693 * Initialize the kernel's virtual memory map, taking
694 * into account all memory allocated up to this time.
695 */
696 void
697 kmem_init(
698 vm_offset_t start,
699 vm_offset_t end)
700 {
701 kernel_map = vm_map_create(pmap_kernel(),
702 VM_MIN_KERNEL_ADDRESS, end,
703 FALSE);
704
705 /*
706 * Reserve virtual memory allocated up to this time.
707 */
708
709 if (start != VM_MIN_KERNEL_ADDRESS) {
710 vm_offset_t addr = VM_MIN_KERNEL_ADDRESS;
711 (void) vm_map_enter(kernel_map,
712 &addr, start - VM_MIN_KERNEL_ADDRESS,
713 (vm_offset_t) 0, TRUE,
714 VM_OBJECT_NULL,
715 (vm_object_offset_t) 0, FALSE,
716 VM_PROT_DEFAULT, VM_PROT_ALL,
717 VM_INHERIT_DEFAULT);
718 }
719
720 /*
721 * Account for kernel memory (text, data, bss, vm shenanigans).
722 * This may include inaccessible "holes" as determined by what
723 * the machine-dependent init code includes in mem_size.
724 */
725 vm_page_wire_count = (atop(mem_size) - (vm_page_free_count
726 + vm_page_active_count
727 + vm_page_inactive_count));
728 }
729
730
731 /*
732 * kmem_io_object_trunc:
733 *
734 * Truncate an object vm_map_copy_t.
735 * Called by the scatter/gather list network code to remove pages from
736 * the tail end of a packet. Also unwires the objects pages.
737 */
738
739 kern_return_t
740 kmem_io_object_trunc(copy, new_size)
741 vm_map_copy_t copy; /* IN/OUT copy object */
742 register vm_size_t new_size; /* IN new object size */
743 {
744 register vm_size_t offset, old_size;
745
746 assert(copy->type == VM_MAP_COPY_OBJECT);
747
748 old_size = (vm_size_t)round_page_64(copy->size);
749 copy->size = new_size;
750 new_size = round_page(new_size);
751
752 vm_object_lock(copy->cpy_object);
753 vm_object_page_remove(copy->cpy_object,
754 (vm_object_offset_t)new_size, (vm_object_offset_t)old_size);
755 for (offset = 0; offset < new_size; offset += PAGE_SIZE) {
756 register vm_page_t mem;
757
758 if ((mem = vm_page_lookup(copy->cpy_object,
759 (vm_object_offset_t)offset)) == VM_PAGE_NULL)
760 panic("kmem_io_object_trunc: unable to find object page");
761
762 /*
763 * Make sure these pages are marked dirty
764 */
765 mem->dirty = TRUE;
766 vm_page_lock_queues();
767 vm_page_unwire(mem);
768 vm_page_unlock_queues();
769 }
770 copy->cpy_object->size = new_size; /* adjust size of object */
771 vm_object_unlock(copy->cpy_object);
772 return(KERN_SUCCESS);
773 }
774
775 /*
776 * kmem_io_object_deallocate:
777 *
778 * Free an vm_map_copy_t.
779 * Called by the scatter/gather list network code to free a packet.
780 */
781
782 void
783 kmem_io_object_deallocate(
784 vm_map_copy_t copy) /* IN/OUT copy object */
785 {
786 kern_return_t ret;
787
788 /*
789 * Clear out all the object pages (this will leave an empty object).
790 */
791 ret = kmem_io_object_trunc(copy, 0);
792 if (ret != KERN_SUCCESS)
793 panic("kmem_io_object_deallocate: unable to truncate object");
794 /*
795 * ...and discard the copy object.
796 */
797 vm_map_copy_discard(copy);
798 }
799
800 /*
801 * Routine: copyinmap
802 * Purpose:
803 * Like copyin, except that fromaddr is an address
804 * in the specified VM map. This implementation
805 * is incomplete; it handles the current user map
806 * and the kernel map/submaps.
807 */
808 boolean_t
809 copyinmap(
810 vm_map_t map,
811 vm_offset_t fromaddr,
812 vm_offset_t toaddr,
813 vm_size_t length)
814 {
815 if (vm_map_pmap(map) == pmap_kernel()) {
816 /* assume a correct copy */
817 memcpy((void *)toaddr, (void *)fromaddr, length);
818 return FALSE;
819 }
820
821 if (current_map() == map)
822 return copyin((char *)fromaddr, (char *)toaddr, length);
823
824 return TRUE;
825 }
826
827 /*
828 * Routine: copyoutmap
829 * Purpose:
830 * Like copyout, except that toaddr is an address
831 * in the specified VM map. This implementation
832 * is incomplete; it handles the current user map
833 * and the kernel map/submaps.
834 */
835 boolean_t
836 copyoutmap(
837 vm_map_t map,
838 vm_offset_t fromaddr,
839 vm_offset_t toaddr,
840 vm_size_t length)
841 {
842 if (vm_map_pmap(map) == pmap_kernel()) {
843 /* assume a correct copy */
844 memcpy((void *)toaddr, (void *)fromaddr, length);
845 return FALSE;
846 }
847
848 if (current_map() == map)
849 return copyout((char *)fromaddr, (char *)toaddr, length);
850
851 return TRUE;
852 }
853
854
855 kern_return_t
856 vm_conflict_check(
857 vm_map_t map,
858 vm_offset_t off,
859 vm_size_t len,
860 memory_object_t pager,
861 vm_object_offset_t file_off)
862 {
863 vm_map_entry_t entry;
864 vm_object_t obj;
865 vm_object_offset_t obj_off;
866 vm_map_t base_map;
867 vm_offset_t base_offset;
868 vm_offset_t original_offset;
869 kern_return_t kr;
870 vm_size_t local_len;
871
872 base_map = map;
873 base_offset = off;
874 original_offset = off;
875 kr = KERN_SUCCESS;
876 vm_map_lock(map);
877 while(vm_map_lookup_entry(map, off, &entry)) {
878 local_len = len;
879
880 if (entry->object.vm_object == VM_OBJECT_NULL) {
881 vm_map_unlock(map);
882 return KERN_SUCCESS;
883 }
884 if (entry->is_sub_map) {
885 vm_map_t old_map;
886 old_map = map;
887 vm_map_lock(entry->object.sub_map);
888 map = entry->object.sub_map;
889 off = entry->offset + (off - entry->vme_start);
890 vm_map_unlock(old_map);
891 continue;
892 }
893 obj = entry->object.vm_object;
894 obj_off = (off - entry->vme_start) + entry->offset;
895 while(obj->shadow) {
896 obj_off += obj->shadow_offset;
897 obj = obj->shadow;
898 }
899 if((obj->pager_created) && (obj->pager == pager)) {
900 if(((obj->paging_offset) + obj_off) == file_off) {
901 if(off != base_offset) {
902 vm_map_unlock(map);
903 return KERN_FAILURE;
904 }
905 kr = KERN_ALREADY_WAITING;
906 } else if(
907 ((file_off < ((obj->paging_offset) + obj_off)) &&
908 ((file_off + len) >
909 ((obj->paging_offset) + obj_off))) ||
910 ((file_off > ((obj->paging_offset) + obj_off)) &&
911 (((((obj->paging_offset) + obj_off)) + len)
912 > file_off))) {
913 vm_map_unlock(map);
914 return KERN_FAILURE;
915 }
916 } else if(kr != KERN_SUCCESS) {
917 return KERN_FAILURE;
918 }
919
920 if(len < ((entry->vme_end - entry->vme_start) -
921 (off - entry->vme_start))) {
922 vm_map_unlock(map);
923 return kr;
924 } else {
925 len -= (entry->vme_end - entry->vme_start) -
926 (off - entry->vme_start);
927 }
928 base_offset = base_offset + (local_len - len);
929 file_off = file_off + (local_len - len);
930 off = base_offset;
931 if(map != base_map) {
932 vm_map_unlock(map);
933 vm_map_lock(base_map);
934 map = base_map;
935 }
936 }
937
938 vm_map_unlock(map);
939 return kr;
940
941
942 }