2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * File: vm/vm_debug.c.
65 * Exported kernel calls. See mach_debug/mach_debug.defs.
67 #include <mach_vm_debug.h>
68 #include <mach/kern_return.h>
69 #include <mach/mach_host_server.h>
70 #include <mach/vm_map_server.h>
71 #include <mach_debug/vm_info.h>
72 #include <mach_debug/page_info.h>
73 #include <mach_debug/hash_info.h>
76 #include <mach/machine/vm_types.h>
77 #include <mach/memory_object_types.h>
78 #include <mach/vm_prot.h>
79 #include <mach/vm_inherit.h>
80 #include <mach/vm_param.h>
81 #include <kern/thread.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_kern.h>
84 #include <vm/vm_object.h>
85 #include <kern/task.h>
86 #include <kern/host.h>
87 #include <ipc/ipc_port.h>
88 #include <vm/vm_debug.h>
92 #define __DEBUG_ONLY __unused
93 #else /* !MACH_VM_DEBUG */
95 #endif /* !MACH_VM_DEBUG */
98 * Routine: mach_vm_region_info [kernel call]
100 * Retrieve information about a VM region,
101 * including info about the object chain.
105 * KERN_SUCCESS Retrieve region/object info.
106 * KERN_INVALID_TASK The map is null.
107 * KERN_NO_SPACE There is no entry at/after the address.
108 * KERN_RESOURCE_SHORTAGE Can't allocate memory.
113 __DEBUG_ONLY vm_map_t map
,
114 __DEBUG_ONLY vm_offset_t address
,
115 __DEBUG_ONLY vm_info_region_t
*regionp
,
116 __DEBUG_ONLY vm_info_object_array_t
*objectsp
,
117 __DEBUG_ONLY mach_msg_type_number_t
*objectsCntp
)
123 vm_offset_t addr
; /* memory for OOL data */
124 vm_size_t size
; /* size of the memory */
125 unsigned int room
; /* room for this many objects */
126 unsigned int used
; /* actually this many objects */
127 vm_info_region_t region
;
130 if (map
== VM_MAP_NULL
)
131 return KERN_INVALID_TASK
;
133 size
= 0; /* no memory allocated yet */
136 vm_map_t cmap
; /* current map in traversal */
137 vm_map_t nmap
; /* next map to look at */
138 vm_map_entry_t entry
;
139 vm_object_t object
, cobject
, nobject
;
141 /* nothing is locked */
143 vm_map_lock_read(map
);
144 for (cmap
= map
;; cmap
= nmap
) {
145 /* cmap is read-locked */
147 if (!vm_map_lookup_entry(cmap
,
148 (vm_map_address_t
)address
, &entry
)) {
150 entry
= entry
->vme_next
;
151 if (entry
== vm_map_to_entry(cmap
)) {
152 vm_map_unlock_read(cmap
);
154 kmem_free(ipc_kernel_map
,
156 return KERN_NO_SPACE
;
160 if (entry
->is_sub_map
)
161 nmap
= entry
->object
.sub_map
;
165 /* move down to the lower map */
167 vm_map_lock_read(nmap
);
168 vm_map_unlock_read(cmap
);
171 /* cmap is read-locked; we have a real entry */
173 object
= entry
->object
.vm_object
;
174 region
.vir_start
= entry
->vme_start
;
175 region
.vir_end
= entry
->vme_end
;
176 region
.vir_object
= (vm_offset_t
) object
;
177 region
.vir_offset
= entry
->offset
;
178 region
.vir_needs_copy
= entry
->needs_copy
;
179 region
.vir_protection
= entry
->protection
;
180 region
.vir_max_protection
= entry
->max_protection
;
181 region
.vir_inheritance
= entry
->inheritance
;
182 region
.vir_wired_count
= entry
->wired_count
;
183 region
.vir_user_wired_count
= entry
->user_wired_count
;
186 room
= size
/ sizeof(vm_info_object_t
);
188 if (object
== VM_OBJECT_NULL
) {
189 vm_map_unlock_read(cmap
);
190 /* no memory needed */
194 vm_object_lock(object
);
195 vm_map_unlock_read(cmap
);
197 for (cobject
= object
;; cobject
= nobject
) {
198 /* cobject is locked */
201 vm_info_object_t
*vio
=
202 &((vm_info_object_t
*) addr
)[used
];
205 (vm_offset_t
) cobject
;
210 vio
->vio_resident_page_count
=
211 cobject
->resident_page_count
;
212 vio
->vio_absent_count
=
213 cobject
->absent_count
;
215 (vm_offset_t
) cobject
->copy
;
217 (vm_offset_t
) cobject
->shadow
;
218 vio
->vio_shadow_offset
=
219 cobject
->shadow_offset
;
220 vio
->vio_paging_offset
=
221 cobject
->paging_offset
;
222 vio
->vio_copy_strategy
=
223 cobject
->copy_strategy
;
224 vio
->vio_last_alloc
=
226 vio
->vio_paging_in_progress
=
227 cobject
->paging_in_progress
;
228 vio
->vio_pager_created
=
229 cobject
->pager_created
;
230 vio
->vio_pager_initialized
=
231 cobject
->pager_initialized
;
232 vio
->vio_pager_ready
=
233 cobject
->pager_ready
;
234 vio
->vio_can_persist
=
235 cobject
->can_persist
;
243 (cobject
->purgable
!= VM_OBJECT_NONPURGABLE
);
244 vio
->vio_purgable_volatile
=
245 (cobject
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
246 cobject
->purgable
== VM_OBJECT_PURGABLE_EMPTY
);
250 nobject
= cobject
->shadow
;
251 if (nobject
== VM_OBJECT_NULL
) {
252 vm_object_unlock(cobject
);
256 vm_object_lock(nobject
);
257 vm_object_unlock(cobject
);
265 /* must allocate more memory */
268 kmem_free(ipc_kernel_map
, addr
, size
);
269 size
= round_page_32(2 * used
* sizeof(vm_info_object_t
));
271 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, VM_FLAGS_ANYWHERE
);
272 if (kr
!= KERN_SUCCESS
)
273 return KERN_RESOURCE_SHORTAGE
;
275 kr
= vm_map_wire(ipc_kernel_map
, vm_map_trunc_page(addr
),
276 vm_map_round_page(addr
+ size
),
277 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
278 assert(kr
== KERN_SUCCESS
);
281 /* free excess memory; make remaining memory pageable */
284 copy
= VM_MAP_COPY_NULL
;
287 kmem_free(ipc_kernel_map
, addr
, size
);
289 vm_size_t size_used
=
290 round_page_32(used
* sizeof(vm_info_object_t
));
292 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
293 vm_map_round_page(addr
+ size_used
), FALSE
);
294 assert(kr
== KERN_SUCCESS
);
296 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
297 (vm_map_size_t
)size_used
, TRUE
, ©
);
298 assert(kr
== KERN_SUCCESS
);
300 if (size
!= size_used
)
301 kmem_free(ipc_kernel_map
,
302 addr
+ size_used
, size
- size_used
);
306 *objectsp
= (vm_info_object_array_t
) copy
;
309 #endif /* MACH_VM_DEBUG */
313 * Temporary call for 64 bit data path interface transiotion
317 mach_vm_region_info_64(
318 __DEBUG_ONLY vm_map_t map
,
319 __DEBUG_ONLY vm_offset_t address
,
320 __DEBUG_ONLY vm_info_region_64_t
*regionp
,
321 __DEBUG_ONLY vm_info_object_array_t
*objectsp
,
322 __DEBUG_ONLY mach_msg_type_number_t
*objectsCntp
)
328 vm_offset_t addr
; /* memory for OOL data */
329 vm_size_t size
; /* size of the memory */
330 unsigned int room
; /* room for this many objects */
331 unsigned int used
; /* actually this many objects */
332 vm_info_region_64_t region
;
335 if (map
== VM_MAP_NULL
)
336 return KERN_INVALID_TASK
;
338 size
= 0; /* no memory allocated yet */
341 vm_map_t cmap
; /* current map in traversal */
342 vm_map_t nmap
; /* next map to look at */
343 vm_map_entry_t entry
;
344 vm_object_t object
, cobject
, nobject
;
346 /* nothing is locked */
348 vm_map_lock_read(map
);
349 for (cmap
= map
;; cmap
= nmap
) {
350 /* cmap is read-locked */
352 if (!vm_map_lookup_entry(cmap
, address
, &entry
)) {
353 entry
= entry
->vme_next
;
354 if (entry
== vm_map_to_entry(cmap
)) {
355 vm_map_unlock_read(cmap
);
357 kmem_free(ipc_kernel_map
,
359 return KERN_NO_SPACE
;
363 if (entry
->is_sub_map
)
364 nmap
= entry
->object
.sub_map
;
368 /* move down to the lower map */
370 vm_map_lock_read(nmap
);
371 vm_map_unlock_read(cmap
);
374 /* cmap is read-locked; we have a real entry */
376 object
= entry
->object
.vm_object
;
377 region
.vir_start
= entry
->vme_start
;
378 region
.vir_end
= entry
->vme_end
;
379 region
.vir_object
= (vm_offset_t
) object
;
380 region
.vir_offset
= entry
->offset
;
381 region
.vir_needs_copy
= entry
->needs_copy
;
382 region
.vir_protection
= entry
->protection
;
383 region
.vir_max_protection
= entry
->max_protection
;
384 region
.vir_inheritance
= entry
->inheritance
;
385 region
.vir_wired_count
= entry
->wired_count
;
386 region
.vir_user_wired_count
= entry
->user_wired_count
;
389 room
= size
/ sizeof(vm_info_object_t
);
391 if (object
== VM_OBJECT_NULL
) {
392 vm_map_unlock_read(cmap
);
393 /* no memory needed */
397 vm_object_lock(object
);
398 vm_map_unlock_read(cmap
);
400 for (cobject
= object
;; cobject
= nobject
) {
401 /* cobject is locked */
404 vm_info_object_t
*vio
=
405 &((vm_info_object_t
*) addr
)[used
];
408 (vm_offset_t
) cobject
;
413 vio
->vio_resident_page_count
=
414 cobject
->resident_page_count
;
415 vio
->vio_absent_count
=
416 cobject
->absent_count
;
418 (vm_offset_t
) cobject
->copy
;
420 (vm_offset_t
) cobject
->shadow
;
421 vio
->vio_shadow_offset
=
422 cobject
->shadow_offset
;
423 vio
->vio_paging_offset
=
424 cobject
->paging_offset
;
425 vio
->vio_copy_strategy
=
426 cobject
->copy_strategy
;
427 vio
->vio_last_alloc
=
429 vio
->vio_paging_in_progress
=
430 cobject
->paging_in_progress
;
431 vio
->vio_pager_created
=
432 cobject
->pager_created
;
433 vio
->vio_pager_initialized
=
434 cobject
->pager_initialized
;
435 vio
->vio_pager_ready
=
436 cobject
->pager_ready
;
437 vio
->vio_can_persist
=
438 cobject
->can_persist
;
446 (cobject
->purgable
!= VM_OBJECT_NONPURGABLE
);
447 vio
->vio_purgable_volatile
=
448 (cobject
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
449 cobject
->purgable
== VM_OBJECT_PURGABLE_EMPTY
);
453 nobject
= cobject
->shadow
;
454 if (nobject
== VM_OBJECT_NULL
) {
455 vm_object_unlock(cobject
);
459 vm_object_lock(nobject
);
460 vm_object_unlock(cobject
);
468 /* must allocate more memory */
471 kmem_free(ipc_kernel_map
, addr
, size
);
472 size
= round_page_32(2 * used
* sizeof(vm_info_object_t
));
474 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, VM_FLAGS_ANYWHERE
);
475 if (kr
!= KERN_SUCCESS
)
476 return KERN_RESOURCE_SHORTAGE
;
478 kr
= vm_map_wire(ipc_kernel_map
, vm_map_trunc_page(addr
),
479 vm_map_round_page(addr
+ size
),
480 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
481 assert(kr
== KERN_SUCCESS
);
484 /* free excess memory; make remaining memory pageable */
487 copy
= VM_MAP_COPY_NULL
;
490 kmem_free(ipc_kernel_map
, addr
, size
);
492 vm_size_t size_used
=
493 round_page_32(used
* sizeof(vm_info_object_t
));
495 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
496 vm_map_round_page(addr
+ size_used
), FALSE
);
497 assert(kr
== KERN_SUCCESS
);
499 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
500 (vm_map_size_t
)size_used
, TRUE
, ©
);
501 assert(kr
== KERN_SUCCESS
);
503 if (size
!= size_used
)
504 kmem_free(ipc_kernel_map
,
505 addr
+ size_used
, size
- size_used
);
509 *objectsp
= (vm_info_object_array_t
) copy
;
512 #endif /* MACH_VM_DEBUG */
515 * Return an array of virtual pages that are mapped to a task.
518 vm_mapped_pages_info(
519 __DEBUG_ONLY vm_map_t map
,
520 __DEBUG_ONLY page_address_array_t
*pages
,
521 __DEBUG_ONLY mach_msg_type_number_t
*pages_count
)
527 vm_size_t size
, size_used
;
528 unsigned int actual
, space
;
529 page_address_array_t list
;
532 if (map
== VM_MAP_NULL
)
533 return (KERN_INVALID_ARGUMENT
);
536 size
= pmap_resident_count(pmap
) * sizeof(vm_offset_t
);
537 size
= round_page_32(size
);
540 (void) vm_allocate(ipc_kernel_map
, &addr
, size
, VM_FLAGS_ANYWHERE
);
541 (void) vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
542 vm_map_round_page(addr
+ size
), FALSE
);
544 list
= (page_address_array_t
) addr
;
545 space
= size
/ sizeof(vm_offset_t
);
547 actual
= pmap_list_resident_pages(pmap
,
554 * Free memory if not enough
556 (void) kmem_free(ipc_kernel_map
, addr
, size
);
559 * Try again, doubling the size
561 size
= round_page_32(actual
* sizeof(vm_offset_t
));
566 (void) kmem_free(ipc_kernel_map
, addr
, size
);
569 *pages_count
= actual
;
570 size_used
= round_page_32(actual
* sizeof(vm_offset_t
));
571 (void) vm_map_wire(ipc_kernel_map
, vm_map_trunc_page(addr
),
572 vm_map_round_page(addr
+ size
),
573 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
574 (void) vm_map_copyin(ipc_kernel_map
,
575 (vm_map_address_t
)addr
,
576 (vm_map_size_t
)size_used
,
578 (vm_map_copy_t
*)pages
);
579 if (size_used
!= size
) {
580 (void) kmem_free(ipc_kernel_map
,
586 return (KERN_SUCCESS
);
587 #endif /* MACH_VM_DEBUG */
591 * Routine: host_virtual_physical_table_info
593 * Return information about the VP table.
595 * Nothing locked. Obeys CountInOut protocol.
597 * KERN_SUCCESS Returned information.
598 * KERN_INVALID_HOST The host is null.
599 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
603 host_virtual_physical_table_info(
604 __DEBUG_ONLY host_t host
,
605 __DEBUG_ONLY hash_info_bucket_array_t
*infop
,
606 __DEBUG_ONLY mach_msg_type_number_t
*countp
)
613 hash_info_bucket_t
*info
;
614 unsigned int potential
, actual
;
617 if (host
== HOST_NULL
)
618 return KERN_INVALID_HOST
;
620 /* start with in-line data */
626 actual
= vm_page_info(info
, potential
);
627 if (actual
<= potential
)
630 /* allocate more memory */
633 kmem_free(ipc_kernel_map
, addr
, size
);
635 size
= round_page_32(actual
* sizeof *info
);
636 kr
= kmem_alloc_pageable(ipc_kernel_map
, &addr
, size
);
637 if (kr
!= KERN_SUCCESS
)
638 return KERN_RESOURCE_SHORTAGE
;
640 info
= (hash_info_bucket_t
*) addr
;
641 potential
= size
/sizeof *info
;
644 if (info
== *infop
) {
645 /* data fit in-line; nothing to deallocate */
648 } else if (actual
== 0) {
649 kmem_free(ipc_kernel_map
, addr
, size
);
656 used
= round_page_32(actual
* sizeof *info
);
659 kmem_free(ipc_kernel_map
, addr
+ used
, size
- used
);
661 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
662 (vm_map_size_t
)used
, TRUE
, ©
);
663 assert(kr
== KERN_SUCCESS
);
665 *infop
= (hash_info_bucket_t
*) copy
;
670 #endif /* MACH_VM_DEBUG */