2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
57 * Author: Avadis Tevanian, Jr., Michael Wayne Young
60 * Kernel memory management.
64 #include <mach/kern_return.h>
65 #include <mach/vm_param.h>
66 #include <kern/assert.h>
67 #include <kern/lock.h>
68 #include <kern/thread.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_pageout.h>
74 #include <kern/misc_protos.h>
79 * Variables exported by this module.
83 vm_map_t kernel_pageable_map
;
86 * Forward declarations for internal functions.
88 extern kern_return_t
kmem_alloc_pages(
89 register vm_object_t object
,
90 register vm_object_offset_t offset
,
91 register vm_size_t size
);
93 extern void kmem_remap_pages(
94 register vm_object_t object
,
95 register vm_object_offset_t offset
,
96 register vm_offset_t start
,
97 register vm_offset_t end
,
98 vm_prot_t protection
);
112 vm_object_offset_t offset
;
113 vm_map_entry_t entry
;
115 if (map
== VM_MAP_NULL
|| (flags
&& (flags
^ KMA_KOBJECT
)))
116 return KERN_INVALID_ARGUMENT
;
120 return KERN_INVALID_ARGUMENT
;
123 size
= round_page(size
);
124 if ((flags
& KMA_KOBJECT
) == 0) {
125 object
= vm_object_allocate(size
);
126 kr
= vm_map_find_space(map
, &addr
, size
, mask
, &entry
);
129 object
= kernel_object
;
130 kr
= vm_map_find_space(map
, &addr
, size
, mask
, &entry
);
133 if ((flags
& KMA_KOBJECT
) == 0) {
134 entry
->object
.vm_object
= object
;
135 entry
->offset
= offset
= 0;
137 offset
= addr
- VM_MIN_KERNEL_ADDRESS
;
139 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
140 vm_object_reference(object
);
141 entry
->object
.vm_object
= object
;
142 entry
->offset
= offset
;
146 if (kr
!= KERN_SUCCESS
) {
147 if ((flags
& KMA_KOBJECT
) == 0)
148 vm_object_deallocate(object
);
154 kr
= cpm_allocate(size
, &pages
, FALSE
);
156 if (kr
!= KERN_SUCCESS
) {
157 vm_map_remove(map
, addr
, addr
+ size
, 0);
162 vm_object_lock(object
);
163 for (i
= 0; i
< size
; i
+= PAGE_SIZE
) {
165 pages
= NEXT_PAGE(m
);
167 vm_page_insert(m
, object
, offset
+ i
);
169 vm_object_unlock(object
);
171 if ((kr
= vm_map_wire(map
, addr
, addr
+ size
, VM_PROT_DEFAULT
, FALSE
))
173 if (object
== kernel_object
) {
174 vm_object_lock(object
);
175 vm_object_page_remove(object
, offset
, offset
+ size
);
176 vm_object_unlock(object
);
178 vm_map_remove(map
, addr
, addr
+ size
, 0);
181 if (object
== kernel_object
)
182 vm_map_simplify(map
, addr
);
189 * Master entry point for allocating kernel memory.
190 * NOTE: this routine is _never_ interrupt safe.
192 * map : map to allocate into
193 * addrp : pointer to start address of new memory
194 * size : size of memory requested
196 * KMA_HERE *addrp is base address, else "anywhere"
197 * KMA_NOPAGEWAIT don't wait for pages if unavailable
198 * KMA_KOBJECT use kernel_object
202 kernel_memory_allocate(
203 register vm_map_t map
,
204 register vm_offset_t
*addrp
,
205 register vm_size_t size
,
206 register vm_offset_t mask
,
209 vm_object_t object
= VM_OBJECT_NULL
;
210 vm_map_entry_t entry
;
211 vm_object_offset_t offset
;
216 size
= round_page(size
);
217 if ((flags
& KMA_KOBJECT
) == 0) {
219 * Allocate a new object. We must do this before locking
220 * the map, or risk deadlock with the default pager:
221 * device_read_alloc uses kmem_alloc,
222 * which tries to allocate an object,
223 * which uses kmem_alloc_wired to get memory,
224 * which blocks for pages.
225 * then the default pager needs to read a block
226 * to process a memory_object_data_write,
227 * and device_read_alloc calls kmem_alloc
228 * and deadlocks on the map lock.
230 object
= vm_object_allocate(size
);
231 kr
= vm_map_find_space(map
, &addr
, size
, mask
, &entry
);
234 object
= kernel_object
;
235 kr
= vm_map_find_space(map
, &addr
, size
, mask
, &entry
);
237 if (kr
!= KERN_SUCCESS
) {
238 if ((flags
& KMA_KOBJECT
) == 0)
239 vm_object_deallocate(object
);
243 if ((flags
& KMA_KOBJECT
) == 0) {
244 entry
->object
.vm_object
= object
;
245 entry
->offset
= offset
= 0;
247 offset
= addr
- VM_MIN_KERNEL_ADDRESS
;
249 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
250 vm_object_reference(object
);
251 entry
->object
.vm_object
= object
;
252 entry
->offset
= offset
;
257 * Since we have not given out this address yet,
258 * it is safe to unlock the map. Except of course
259 * we must make certain no one coalesces our address
260 * or does a blind vm_deallocate and removes the object
261 * an extra object reference will suffice to protect
262 * against both contingencies.
264 vm_object_reference(object
);
267 vm_object_lock(object
);
268 for (i
= 0; i
< size
; i
+= PAGE_SIZE
) {
271 while ((mem
= vm_page_alloc(object
,
272 offset
+ (vm_object_offset_t
)i
))
274 if (flags
& KMA_NOPAGEWAIT
) {
275 if (object
== kernel_object
)
276 vm_object_page_remove(object
, offset
,
277 offset
+ (vm_object_offset_t
)i
);
278 vm_object_unlock(object
);
279 vm_map_remove(map
, addr
, addr
+ size
, 0);
280 vm_object_deallocate(object
);
281 return KERN_RESOURCE_SHORTAGE
;
283 vm_object_unlock(object
);
285 vm_object_lock(object
);
289 vm_object_unlock(object
);
291 if ((kr
= vm_map_wire(map
, addr
, addr
+ size
, VM_PROT_DEFAULT
, FALSE
))
293 if (object
== kernel_object
) {
294 vm_object_lock(object
);
295 vm_object_page_remove(object
, offset
, offset
+ size
);
296 vm_object_unlock(object
);
298 vm_map_remove(map
, addr
, addr
+ size
, 0);
299 vm_object_deallocate(object
);
302 /* now that the page is wired, we no longer have to fear coalesce */
303 vm_object_deallocate(object
);
304 if (object
== kernel_object
)
305 vm_map_simplify(map
, addr
);
308 * Return the memory, not zeroed.
310 #if (NCPUS > 1) && i860
312 #endif /* #if (NCPUS > 1) && i860 */
320 * Allocate wired-down memory in the kernel's address map
321 * or a submap. The memory is not zero-filled.
330 return kernel_memory_allocate(map
, addrp
, size
, 0, 0);
336 * Reallocate wired-down memory in the kernel's address map
337 * or a submap. Newly allocated pages are not zeroed.
338 * This can only be used on regions allocated with kmem_alloc.
340 * If successful, the pages in the old region are mapped twice.
341 * The old region is unchanged. Use kmem_free to get rid of it.
348 vm_offset_t
*newaddrp
,
351 vm_offset_t oldmin
, oldmax
;
355 vm_map_entry_t oldentry
, newentry
;
359 oldmin
= trunc_page(oldaddr
);
360 oldmax
= round_page(oldaddr
+ oldsize
);
361 oldsize
= oldmax
- oldmin
;
362 newsize
= round_page(newsize
);
366 * Find the VM object backing the old region.
371 if (!vm_map_lookup_entry(map
, oldmin
, &oldentry
))
372 panic("kmem_realloc");
373 object
= oldentry
->object
.vm_object
;
376 * Increase the size of the object and
377 * fill in the new region.
380 vm_object_reference(object
);
381 /* by grabbing the object lock before unlocking the map */
382 /* we guarantee that we will panic if more than one */
383 /* attempt is made to realloc a kmem_alloc'd area */
384 vm_object_lock(object
);
386 if (object
->size
!= oldsize
)
387 panic("kmem_realloc");
388 object
->size
= newsize
;
389 vm_object_unlock(object
);
391 /* allocate the new pages while expanded portion of the */
392 /* object is still not mapped */
393 kmem_alloc_pages(object
, oldsize
, newsize
-oldsize
);
397 * Find space for the new region.
400 kr
= vm_map_find_space(map
, &newaddr
, newsize
, (vm_offset_t
) 0,
402 if (kr
!= KERN_SUCCESS
) {
403 vm_object_lock(object
);
404 for(offset
= oldsize
;
405 offset
<newsize
; offset
+=PAGE_SIZE
) {
406 if ((mem
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
407 vm_page_lock_queues();
409 vm_page_unlock_queues();
412 object
->size
= oldsize
;
413 vm_object_unlock(object
);
414 vm_object_deallocate(object
);
417 newentry
->object
.vm_object
= object
;
418 newentry
->offset
= 0;
419 assert (newentry
->wired_count
== 0);
422 /* add an extra reference in case we have someone doing an */
423 /* unexpected deallocate */
424 vm_object_reference(object
);
427 if ((kr
= vm_map_wire(map
, newaddr
, newaddr
+ newsize
,
428 VM_PROT_DEFAULT
, FALSE
)) != KERN_SUCCESS
) {
429 vm_map_remove(map
, newaddr
, newaddr
+ newsize
, 0);
430 vm_object_lock(object
);
431 for(offset
= oldsize
;
432 offset
<newsize
; offset
+=PAGE_SIZE
) {
433 if ((mem
= vm_page_lookup(object
, offset
)) != VM_PAGE_NULL
) {
434 vm_page_lock_queues();
436 vm_page_unlock_queues();
439 object
->size
= oldsize
;
440 vm_object_unlock(object
);
441 vm_object_deallocate(object
);
444 vm_object_deallocate(object
);
454 * Allocate wired-down memory in the kernel's address map
455 * or a submap. The memory is not zero-filled.
457 * The memory is allocated in the kernel_object.
458 * It may not be copied with vm_map_copy, and
459 * it may not be reallocated with kmem_realloc.
468 return kernel_memory_allocate(map
, addrp
, size
, 0, KMA_KOBJECT
);
472 * kmem_alloc_aligned:
474 * Like kmem_alloc_wired, except that the memory is aligned.
475 * The size should be a power-of-2.
484 if ((size
& (size
- 1)) != 0)
485 panic("kmem_alloc_aligned: size not aligned");
486 return kernel_memory_allocate(map
, addrp
, size
, size
- 1, KMA_KOBJECT
);
490 * kmem_alloc_pageable:
492 * Allocate pageable memory in the kernel's address map.
505 addr
= (vm_map_min(map
)) + 0x1000;
507 addr
= vm_map_min(map
);
509 kr
= vm_map_enter(map
, &addr
, round_page(size
),
510 (vm_offset_t
) 0, TRUE
,
511 VM_OBJECT_NULL
, (vm_object_offset_t
) 0, FALSE
,
512 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
513 if (kr
!= KERN_SUCCESS
)
523 * Release a region of kernel virtual memory allocated
524 * with kmem_alloc, kmem_alloc_wired, or kmem_alloc_pageable,
525 * and return the physical pages associated with that region.
536 kr
= vm_map_remove(map
, trunc_page(addr
),
537 round_page(addr
+ size
), VM_MAP_REMOVE_KUNWIRE
);
538 if (kr
!= KERN_SUCCESS
)
543 * Allocate new pages in an object.
548 register vm_object_t object
,
549 register vm_object_offset_t offset
,
550 register vm_size_t size
)
553 size
= round_page(size
);
554 vm_object_lock(object
);
556 register vm_page_t mem
;
562 while ((mem
= vm_page_alloc(object
, offset
))
564 vm_object_unlock(object
);
566 vm_object_lock(object
);
574 vm_object_unlock(object
);
579 * Remap wired pages in an object into a new region.
580 * The object is assumed to be mapped into the kernel map or
585 register vm_object_t object
,
586 register vm_object_offset_t offset
,
587 register vm_offset_t start
,
588 register vm_offset_t end
,
589 vm_prot_t protection
)
592 * Mark the pmap region as not pageable.
594 pmap_pageable(kernel_pmap
, start
, end
, FALSE
);
596 while (start
< end
) {
597 register vm_page_t mem
;
599 vm_object_lock(object
);
604 if ((mem
= vm_page_lookup(object
, offset
)) == VM_PAGE_NULL
)
605 panic("kmem_remap_pages");
608 * Wire it down (again)
610 vm_page_lock_queues();
612 vm_page_unlock_queues();
613 vm_object_unlock(object
);
616 * Enter it in the kernel pmap. The page isn't busy,
617 * but this shouldn't be a problem because it is wired.
619 PMAP_ENTER(kernel_pmap
, start
, mem
, protection
,
620 VM_WIMG_USE_DEFAULT
, TRUE
);
630 * Allocates a map to manage a subrange
631 * of the kernel virtual address space.
633 * Arguments are as follows:
635 * parent Map to take range from
636 * addr Address of start of range (IN/OUT)
637 * size Size of range to find
638 * pageable Can region be paged
639 * anywhere Can region be located anywhere in map
640 * new_map Pointer to new submap
654 size
= round_page(size
);
657 * Need reference on submap object because it is internal
658 * to the vm_system. vm_object_enter will never be called
659 * on it (usual source of reference for vm_map_enter).
661 vm_object_reference(vm_submap_object
);
663 if (anywhere
== TRUE
)
664 *addr
= (vm_offset_t
)vm_map_min(parent
);
665 kr
= vm_map_enter(parent
, addr
, size
,
666 (vm_offset_t
) 0, anywhere
,
667 vm_submap_object
, (vm_object_offset_t
) 0, FALSE
,
668 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
669 if (kr
!= KERN_SUCCESS
) {
670 vm_object_deallocate(vm_submap_object
);
674 pmap_reference(vm_map_pmap(parent
));
675 map
= vm_map_create(vm_map_pmap(parent
), *addr
, *addr
+ size
, pageable
);
676 if (map
== VM_MAP_NULL
)
677 panic("kmem_suballoc: vm_map_create failed"); /* "can't happen" */
679 kr
= vm_map_submap(parent
, *addr
, *addr
+ size
, map
, *addr
, FALSE
);
680 if (kr
!= KERN_SUCCESS
) {
682 * See comment preceding vm_map_submap().
684 vm_map_remove(parent
, *addr
, *addr
+ size
, VM_MAP_NO_FLAGS
);
685 vm_map_deallocate(map
); /* also removes ref to pmap */
686 vm_object_deallocate(vm_submap_object
);
690 return (KERN_SUCCESS
);
696 * Initialize the kernel's virtual memory map, taking
697 * into account all memory allocated up to this time.
704 kernel_map
= vm_map_create(pmap_kernel(),
705 VM_MIN_KERNEL_ADDRESS
, end
,
709 * Reserve virtual memory allocated up to this time.
712 if (start
!= VM_MIN_KERNEL_ADDRESS
) {
713 vm_offset_t addr
= VM_MIN_KERNEL_ADDRESS
;
714 (void) vm_map_enter(kernel_map
,
715 &addr
, start
- VM_MIN_KERNEL_ADDRESS
,
716 (vm_offset_t
) 0, TRUE
,
718 (vm_object_offset_t
) 0, FALSE
,
719 VM_PROT_DEFAULT
, VM_PROT_ALL
,
724 * Account for kernel memory (text, data, bss, vm shenanigans).
725 * This may include inaccessible "holes" as determined by what
726 * the machine-dependent init code includes in mem_size.
728 vm_page_wire_count
= (atop(mem_size
) - (vm_page_free_count
729 + vm_page_active_count
730 + vm_page_inactive_count
));
735 * kmem_io_object_trunc:
737 * Truncate an object vm_map_copy_t.
738 * Called by the scatter/gather list network code to remove pages from
739 * the tail end of a packet. Also unwires the objects pages.
743 kmem_io_object_trunc(copy
, new_size
)
744 vm_map_copy_t copy
; /* IN/OUT copy object */
745 register vm_size_t new_size
; /* IN new object size */
747 register vm_size_t offset
, old_size
;
749 assert(copy
->type
== VM_MAP_COPY_OBJECT
);
751 old_size
= (vm_size_t
)round_page_64(copy
->size
);
752 copy
->size
= new_size
;
753 new_size
= round_page(new_size
);
755 vm_object_lock(copy
->cpy_object
);
756 vm_object_page_remove(copy
->cpy_object
,
757 (vm_object_offset_t
)new_size
, (vm_object_offset_t
)old_size
);
758 for (offset
= 0; offset
< new_size
; offset
+= PAGE_SIZE
) {
759 register vm_page_t mem
;
761 if ((mem
= vm_page_lookup(copy
->cpy_object
,
762 (vm_object_offset_t
)offset
)) == VM_PAGE_NULL
)
763 panic("kmem_io_object_trunc: unable to find object page");
766 * Make sure these pages are marked dirty
769 vm_page_lock_queues();
771 vm_page_unlock_queues();
773 copy
->cpy_object
->size
= new_size
; /* adjust size of object */
774 vm_object_unlock(copy
->cpy_object
);
775 return(KERN_SUCCESS
);
779 * kmem_io_object_deallocate:
781 * Free an vm_map_copy_t.
782 * Called by the scatter/gather list network code to free a packet.
786 kmem_io_object_deallocate(
787 vm_map_copy_t copy
) /* IN/OUT copy object */
792 * Clear out all the object pages (this will leave an empty object).
794 ret
= kmem_io_object_trunc(copy
, 0);
795 if (ret
!= KERN_SUCCESS
)
796 panic("kmem_io_object_deallocate: unable to truncate object");
798 * ...and discard the copy object.
800 vm_map_copy_discard(copy
);
806 * Like copyin, except that fromaddr is an address
807 * in the specified VM map. This implementation
808 * is incomplete; it handles the current user map
809 * and the kernel map/submaps.
814 vm_offset_t fromaddr
,
818 if (vm_map_pmap(map
) == pmap_kernel()) {
819 /* assume a correct copy */
820 memcpy((void *)toaddr
, (void *)fromaddr
, length
);
824 if (current_map() == map
)
825 return copyin((char *)fromaddr
, (char *)toaddr
, length
);
831 * Routine: copyoutmap
833 * Like copyout, except that toaddr is an address
834 * in the specified VM map. This implementation
835 * is incomplete; it handles the current user map
836 * and the kernel map/submaps.
841 vm_offset_t fromaddr
,
845 if (vm_map_pmap(map
) == pmap_kernel()) {
846 /* assume a correct copy */
847 memcpy((void *)toaddr
, (void *)fromaddr
, length
);
851 if (current_map() == map
)
852 return copyout((char *)fromaddr
, (char *)toaddr
, length
);
863 memory_object_t pager
,
864 vm_object_offset_t file_off
)
866 vm_map_entry_t entry
;
868 vm_object_offset_t obj_off
;
870 vm_offset_t base_offset
;
871 vm_offset_t original_offset
;
877 original_offset
= off
;
880 while(vm_map_lookup_entry(map
, off
, &entry
)) {
883 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
887 if (entry
->is_sub_map
) {
890 vm_map_lock(entry
->object
.sub_map
);
891 map
= entry
->object
.sub_map
;
892 off
= entry
->offset
+ (off
- entry
->vme_start
);
893 vm_map_unlock(old_map
);
896 obj
= entry
->object
.vm_object
;
897 obj_off
= (off
- entry
->vme_start
) + entry
->offset
;
899 obj_off
+= obj
->shadow_offset
;
902 if((obj
->pager_created
) && (obj
->pager
== pager
)) {
903 if(((obj
->paging_offset
) + obj_off
) == file_off
) {
904 if(off
!= base_offset
) {
908 kr
= KERN_ALREADY_WAITING
;
910 ((file_off
< ((obj
->paging_offset
) + obj_off
)) &&
912 ((obj
->paging_offset
) + obj_off
))) ||
913 ((file_off
> ((obj
->paging_offset
) + obj_off
)) &&
914 (((((obj
->paging_offset
) + obj_off
)) + len
)
919 } else if(kr
!= KERN_SUCCESS
) {
923 if(len
< ((entry
->vme_end
- entry
->vme_start
) -
924 (off
- entry
->vme_start
))) {
928 len
-= (entry
->vme_end
- entry
->vme_start
) -
929 (off
- entry
->vme_start
);
931 base_offset
= base_offset
+ (local_len
- len
);
932 file_off
= file_off
+ (local_len
- len
);
934 if(map
!= base_map
) {
936 vm_map_lock(base_map
);