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_32(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_32(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_32(oldaddr
);
360 oldmax
= round_page_32(oldaddr
+ oldsize
);
361 oldsize
= oldmax
- oldmin
;
362 newsize
= round_page_32(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_32(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_32(addr
),
537 round_page_32(addr
+ size
),
538 VM_MAP_REMOVE_KUNWIRE
);
539 if (kr
!= KERN_SUCCESS
)
544 * Allocate new pages in an object.
549 register vm_object_t object
,
550 register vm_object_offset_t offset
,
551 register vm_size_t size
)
554 size
= round_page_32(size
);
555 vm_object_lock(object
);
557 register vm_page_t mem
;
563 while ((mem
= vm_page_alloc(object
, offset
))
565 vm_object_unlock(object
);
567 vm_object_lock(object
);
575 vm_object_unlock(object
);
580 * Remap wired pages in an object into a new region.
581 * The object is assumed to be mapped into the kernel map or
586 register vm_object_t object
,
587 register vm_object_offset_t offset
,
588 register vm_offset_t start
,
589 register vm_offset_t end
,
590 vm_prot_t protection
)
593 * Mark the pmap region as not pageable.
595 pmap_pageable(kernel_pmap
, start
, end
, FALSE
);
597 while (start
< end
) {
598 register vm_page_t mem
;
600 vm_object_lock(object
);
605 if ((mem
= vm_page_lookup(object
, offset
)) == VM_PAGE_NULL
)
606 panic("kmem_remap_pages");
609 * Wire it down (again)
611 vm_page_lock_queues();
613 vm_page_unlock_queues();
614 vm_object_unlock(object
);
617 * Enter it in the kernel pmap. The page isn't busy,
618 * but this shouldn't be a problem because it is wired.
620 PMAP_ENTER(kernel_pmap
, start
, mem
, protection
,
621 ((unsigned int)(mem
->object
->wimg_bits
))
633 * Allocates a map to manage a subrange
634 * of the kernel virtual address space.
636 * Arguments are as follows:
638 * parent Map to take range from
639 * addr Address of start of range (IN/OUT)
640 * size Size of range to find
641 * pageable Can region be paged
642 * anywhere Can region be located anywhere in map
643 * new_map Pointer to new submap
657 size
= round_page_32(size
);
660 * Need reference on submap object because it is internal
661 * to the vm_system. vm_object_enter will never be called
662 * on it (usual source of reference for vm_map_enter).
664 vm_object_reference(vm_submap_object
);
666 if (anywhere
== TRUE
)
667 *addr
= (vm_offset_t
)vm_map_min(parent
);
668 kr
= vm_map_enter(parent
, addr
, size
,
669 (vm_offset_t
) 0, anywhere
,
670 vm_submap_object
, (vm_object_offset_t
) 0, FALSE
,
671 VM_PROT_DEFAULT
, VM_PROT_ALL
, VM_INHERIT_DEFAULT
);
672 if (kr
!= KERN_SUCCESS
) {
673 vm_object_deallocate(vm_submap_object
);
677 pmap_reference(vm_map_pmap(parent
));
678 map
= vm_map_create(vm_map_pmap(parent
), *addr
, *addr
+ size
, pageable
);
679 if (map
== VM_MAP_NULL
)
680 panic("kmem_suballoc: vm_map_create failed"); /* "can't happen" */
682 kr
= vm_map_submap(parent
, *addr
, *addr
+ size
, map
, *addr
, FALSE
);
683 if (kr
!= KERN_SUCCESS
) {
685 * See comment preceding vm_map_submap().
687 vm_map_remove(parent
, *addr
, *addr
+ size
, VM_MAP_NO_FLAGS
);
688 vm_map_deallocate(map
); /* also removes ref to pmap */
689 vm_object_deallocate(vm_submap_object
);
693 return (KERN_SUCCESS
);
699 * Initialize the kernel's virtual memory map, taking
700 * into account all memory allocated up to this time.
707 kernel_map
= vm_map_create(pmap_kernel(),
708 VM_MIN_KERNEL_ADDRESS
, end
,
712 * Reserve virtual memory allocated up to this time.
715 if (start
!= VM_MIN_KERNEL_ADDRESS
) {
716 vm_offset_t addr
= VM_MIN_KERNEL_ADDRESS
;
717 (void) vm_map_enter(kernel_map
,
718 &addr
, start
- VM_MIN_KERNEL_ADDRESS
,
719 (vm_offset_t
) 0, TRUE
,
721 (vm_object_offset_t
) 0, FALSE
,
722 VM_PROT_DEFAULT
, VM_PROT_ALL
,
727 * Account for kernel memory (text, data, bss, vm shenanigans).
728 * This may include inaccessible "holes" as determined by what
729 * the machine-dependent init code includes in max_mem.
731 vm_page_wire_count
= (atop_64(max_mem
) - (vm_page_free_count
732 + vm_page_active_count
733 + vm_page_inactive_count
));
738 * kmem_io_object_trunc:
740 * Truncate an object vm_map_copy_t.
741 * Called by the scatter/gather list network code to remove pages from
742 * the tail end of a packet. Also unwires the objects pages.
746 kmem_io_object_trunc(copy
, new_size
)
747 vm_map_copy_t copy
; /* IN/OUT copy object */
748 register vm_size_t new_size
; /* IN new object size */
750 register vm_size_t offset
, old_size
;
752 assert(copy
->type
== VM_MAP_COPY_OBJECT
);
754 old_size
= (vm_size_t
)round_page_64(copy
->size
);
755 copy
->size
= new_size
;
756 new_size
= round_page_32(new_size
);
758 vm_object_lock(copy
->cpy_object
);
759 vm_object_page_remove(copy
->cpy_object
,
760 (vm_object_offset_t
)new_size
, (vm_object_offset_t
)old_size
);
761 for (offset
= 0; offset
< new_size
; offset
+= PAGE_SIZE
) {
762 register vm_page_t mem
;
764 if ((mem
= vm_page_lookup(copy
->cpy_object
,
765 (vm_object_offset_t
)offset
)) == VM_PAGE_NULL
)
766 panic("kmem_io_object_trunc: unable to find object page");
769 * Make sure these pages are marked dirty
772 vm_page_lock_queues();
774 vm_page_unlock_queues();
776 copy
->cpy_object
->size
= new_size
; /* adjust size of object */
777 vm_object_unlock(copy
->cpy_object
);
778 return(KERN_SUCCESS
);
782 * kmem_io_object_deallocate:
784 * Free an vm_map_copy_t.
785 * Called by the scatter/gather list network code to free a packet.
789 kmem_io_object_deallocate(
790 vm_map_copy_t copy
) /* IN/OUT copy object */
795 * Clear out all the object pages (this will leave an empty object).
797 ret
= kmem_io_object_trunc(copy
, 0);
798 if (ret
!= KERN_SUCCESS
)
799 panic("kmem_io_object_deallocate: unable to truncate object");
801 * ...and discard the copy object.
803 vm_map_copy_discard(copy
);
809 * Like copyin, except that fromaddr is an address
810 * in the specified VM map. This implementation
811 * is incomplete; it handles the current user map
812 * and the kernel map/submaps.
817 vm_offset_t fromaddr
,
821 if (vm_map_pmap(map
) == pmap_kernel()) {
822 /* assume a correct copy */
823 memcpy((void *)toaddr
, (void *)fromaddr
, length
);
827 if (current_map() == map
)
828 return copyin((char *)fromaddr
, (char *)toaddr
, length
);
834 * Routine: copyoutmap
836 * Like copyout, except that toaddr is an address
837 * in the specified VM map. This implementation
838 * is incomplete; it handles the current user map
839 * and the kernel map/submaps.
844 vm_offset_t fromaddr
,
848 if (vm_map_pmap(map
) == pmap_kernel()) {
849 /* assume a correct copy */
850 memcpy((void *)toaddr
, (void *)fromaddr
, length
);
854 if (current_map() == map
)
855 return copyout((char *)fromaddr
, (char *)toaddr
, length
);
866 memory_object_t pager
,
867 vm_object_offset_t file_off
)
869 vm_map_entry_t entry
;
871 vm_object_offset_t obj_off
;
873 vm_offset_t base_offset
;
874 vm_offset_t original_offset
;
880 original_offset
= off
;
883 while(vm_map_lookup_entry(map
, off
, &entry
)) {
886 if (entry
->object
.vm_object
== VM_OBJECT_NULL
) {
890 if (entry
->is_sub_map
) {
893 vm_map_lock(entry
->object
.sub_map
);
894 map
= entry
->object
.sub_map
;
895 off
= entry
->offset
+ (off
- entry
->vme_start
);
896 vm_map_unlock(old_map
);
899 obj
= entry
->object
.vm_object
;
900 obj_off
= (off
- entry
->vme_start
) + entry
->offset
;
902 obj_off
+= obj
->shadow_offset
;
905 if((obj
->pager_created
) && (obj
->pager
== pager
)) {
906 if(((obj
->paging_offset
) + obj_off
) == file_off
) {
907 if(off
!= base_offset
) {
911 kr
= KERN_ALREADY_WAITING
;
913 ((file_off
< ((obj
->paging_offset
) + obj_off
)) &&
915 ((obj
->paging_offset
) + obj_off
))) ||
916 ((file_off
> ((obj
->paging_offset
) + obj_off
)) &&
917 (((((obj
->paging_offset
) + obj_off
)) + len
)
922 } else if(kr
!= KERN_SUCCESS
) {
926 if(len
< ((entry
->vme_end
- entry
->vme_start
) -
927 (off
- entry
->vme_start
))) {
931 len
-= (entry
->vme_end
- entry
->vme_start
) -
932 (off
- entry
->vme_start
);
934 base_offset
= base_offset
+ (local_len
- len
);
935 file_off
= file_off
+ (local_len
- len
);
937 if(map
!= base_map
) {
939 vm_map_lock(base_map
);