2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: vm/vm_debug.c.
63 * Exported kernel calls. See mach_debug/mach_debug.defs.
65 #include <mach_vm_debug.h>
66 #include <mach/kern_return.h>
67 #include <mach/mach_host_server.h>
68 #include <mach/vm_map_server.h>
69 #include <mach_debug/vm_info.h>
70 #include <mach_debug/page_info.h>
71 #include <mach_debug/hash_info.h>
74 #include <mach/machine/vm_types.h>
75 #include <mach/memory_object_types.h>
76 #include <mach/vm_prot.h>
77 #include <mach/vm_inherit.h>
78 #include <mach/vm_param.h>
79 #include <kern/thread.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_kern.h>
82 #include <vm/vm_object.h>
83 #include <kern/task.h>
84 #include <kern/host.h>
85 #include <ipc/ipc_port.h>
86 #include <vm/vm_debug.h>
90 #define __DEBUG_ONLY __unused
91 #else /* !MACH_VM_DEBUG */
93 #endif /* !MACH_VM_DEBUG */
96 * Routine: mach_vm_region_info [kernel call]
98 * Retrieve information about a VM region,
99 * including info about the object chain.
103 * KERN_SUCCESS Retrieve region/object info.
104 * KERN_INVALID_TASK The map is null.
105 * KERN_NO_SPACE There is no entry at/after the address.
106 * KERN_RESOURCE_SHORTAGE Can't allocate memory.
111 __DEBUG_ONLY vm_map_t map
,
112 __DEBUG_ONLY vm_offset_t address
,
113 __DEBUG_ONLY vm_info_region_t
*regionp
,
114 __DEBUG_ONLY vm_info_object_array_t
*objectsp
,
115 __DEBUG_ONLY mach_msg_type_number_t
*objectsCntp
)
121 vm_offset_t addr
; /* memory for OOL data */
122 vm_size_t size
; /* size of the memory */
123 unsigned int room
; /* room for this many objects */
124 unsigned int used
; /* actually this many objects */
125 vm_info_region_t region
;
128 if (map
== VM_MAP_NULL
)
129 return KERN_INVALID_TASK
;
131 size
= 0; /* no memory allocated yet */
134 vm_map_t cmap
; /* current map in traversal */
135 vm_map_t nmap
; /* next map to look at */
136 vm_map_entry_t entry
;
137 vm_object_t object
, cobject
, nobject
;
139 /* nothing is locked */
141 vm_map_lock_read(map
);
142 for (cmap
= map
;; cmap
= nmap
) {
143 /* cmap is read-locked */
145 if (!vm_map_lookup_entry(cmap
,
146 (vm_map_address_t
)address
, &entry
)) {
148 entry
= entry
->vme_next
;
149 if (entry
== vm_map_to_entry(cmap
)) {
150 vm_map_unlock_read(cmap
);
152 kmem_free(ipc_kernel_map
,
154 return KERN_NO_SPACE
;
158 if (entry
->is_sub_map
)
159 nmap
= entry
->object
.sub_map
;
163 /* move down to the lower map */
165 vm_map_lock_read(nmap
);
166 vm_map_unlock_read(cmap
);
169 /* cmap is read-locked; we have a real entry */
171 object
= entry
->object
.vm_object
;
172 region
.vir_start
= entry
->vme_start
;
173 region
.vir_end
= entry
->vme_end
;
174 region
.vir_object
= (vm_offset_t
) object
;
175 region
.vir_offset
= entry
->offset
;
176 region
.vir_needs_copy
= entry
->needs_copy
;
177 region
.vir_protection
= entry
->protection
;
178 region
.vir_max_protection
= entry
->max_protection
;
179 region
.vir_inheritance
= entry
->inheritance
;
180 region
.vir_wired_count
= entry
->wired_count
;
181 region
.vir_user_wired_count
= entry
->user_wired_count
;
184 room
= size
/ sizeof(vm_info_object_t
);
186 if (object
== VM_OBJECT_NULL
) {
187 vm_map_unlock_read(cmap
);
188 /* no memory needed */
192 vm_object_lock(object
);
193 vm_map_unlock_read(cmap
);
195 for (cobject
= object
;; cobject
= nobject
) {
196 /* cobject is locked */
199 vm_info_object_t
*vio
=
200 &((vm_info_object_t
*) addr
)[used
];
203 (vm_offset_t
) cobject
;
208 vio
->vio_resident_page_count
=
209 cobject
->resident_page_count
;
211 (vm_offset_t
) cobject
->copy
;
213 (vm_offset_t
) cobject
->shadow
;
214 vio
->vio_shadow_offset
=
215 cobject
->shadow_offset
;
216 vio
->vio_paging_offset
=
217 cobject
->paging_offset
;
218 vio
->vio_copy_strategy
=
219 cobject
->copy_strategy
;
220 vio
->vio_last_alloc
=
222 vio
->vio_paging_in_progress
=
223 cobject
->paging_in_progress
;
224 vio
->vio_pager_created
=
225 cobject
->pager_created
;
226 vio
->vio_pager_initialized
=
227 cobject
->pager_initialized
;
228 vio
->vio_pager_ready
=
229 cobject
->pager_ready
;
230 vio
->vio_can_persist
=
231 cobject
->can_persist
;
239 (cobject
->purgable
!= VM_PURGABLE_DENY
);
240 vio
->vio_purgable_volatile
=
241 (cobject
->purgable
== VM_PURGABLE_VOLATILE
||
242 cobject
->purgable
== VM_PURGABLE_EMPTY
);
246 nobject
= cobject
->shadow
;
247 if (nobject
== VM_OBJECT_NULL
) {
248 vm_object_unlock(cobject
);
252 vm_object_lock(nobject
);
253 vm_object_unlock(cobject
);
261 /* must allocate more memory */
264 kmem_free(ipc_kernel_map
, addr
, size
);
265 size
= round_page_32(2 * used
* sizeof(vm_info_object_t
));
267 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, VM_FLAGS_ANYWHERE
);
268 if (kr
!= KERN_SUCCESS
)
269 return KERN_RESOURCE_SHORTAGE
;
271 kr
= vm_map_wire(ipc_kernel_map
, vm_map_trunc_page(addr
),
272 vm_map_round_page(addr
+ size
),
273 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
274 assert(kr
== KERN_SUCCESS
);
277 /* free excess memory; make remaining memory pageable */
280 copy
= VM_MAP_COPY_NULL
;
283 kmem_free(ipc_kernel_map
, addr
, size
);
285 vm_size_t size_used
=
286 round_page_32(used
* sizeof(vm_info_object_t
));
288 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
289 vm_map_round_page(addr
+ size_used
), FALSE
);
290 assert(kr
== KERN_SUCCESS
);
292 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
293 (vm_map_size_t
)size_used
, TRUE
, ©
);
294 assert(kr
== KERN_SUCCESS
);
296 if (size
!= size_used
)
297 kmem_free(ipc_kernel_map
,
298 addr
+ size_used
, size
- size_used
);
302 *objectsp
= (vm_info_object_array_t
) copy
;
305 #endif /* MACH_VM_DEBUG */
309 * Temporary call for 64 bit data path interface transiotion
313 mach_vm_region_info_64(
314 __DEBUG_ONLY vm_map_t map
,
315 __DEBUG_ONLY vm_offset_t address
,
316 __DEBUG_ONLY vm_info_region_64_t
*regionp
,
317 __DEBUG_ONLY vm_info_object_array_t
*objectsp
,
318 __DEBUG_ONLY mach_msg_type_number_t
*objectsCntp
)
324 vm_offset_t addr
; /* memory for OOL data */
325 vm_size_t size
; /* size of the memory */
326 unsigned int room
; /* room for this many objects */
327 unsigned int used
; /* actually this many objects */
328 vm_info_region_64_t region
;
331 if (map
== VM_MAP_NULL
)
332 return KERN_INVALID_TASK
;
334 size
= 0; /* no memory allocated yet */
337 vm_map_t cmap
; /* current map in traversal */
338 vm_map_t nmap
; /* next map to look at */
339 vm_map_entry_t entry
;
340 vm_object_t object
, cobject
, nobject
;
342 /* nothing is locked */
344 vm_map_lock_read(map
);
345 for (cmap
= map
;; cmap
= nmap
) {
346 /* cmap is read-locked */
348 if (!vm_map_lookup_entry(cmap
, address
, &entry
)) {
349 entry
= entry
->vme_next
;
350 if (entry
== vm_map_to_entry(cmap
)) {
351 vm_map_unlock_read(cmap
);
353 kmem_free(ipc_kernel_map
,
355 return KERN_NO_SPACE
;
359 if (entry
->is_sub_map
)
360 nmap
= entry
->object
.sub_map
;
364 /* move down to the lower map */
366 vm_map_lock_read(nmap
);
367 vm_map_unlock_read(cmap
);
370 /* cmap is read-locked; we have a real entry */
372 object
= entry
->object
.vm_object
;
373 region
.vir_start
= entry
->vme_start
;
374 region
.vir_end
= entry
->vme_end
;
375 region
.vir_object
= (vm_offset_t
) object
;
376 region
.vir_offset
= entry
->offset
;
377 region
.vir_needs_copy
= entry
->needs_copy
;
378 region
.vir_protection
= entry
->protection
;
379 region
.vir_max_protection
= entry
->max_protection
;
380 region
.vir_inheritance
= entry
->inheritance
;
381 region
.vir_wired_count
= entry
->wired_count
;
382 region
.vir_user_wired_count
= entry
->user_wired_count
;
385 room
= size
/ sizeof(vm_info_object_t
);
387 if (object
== VM_OBJECT_NULL
) {
388 vm_map_unlock_read(cmap
);
389 /* no memory needed */
393 vm_object_lock(object
);
394 vm_map_unlock_read(cmap
);
396 for (cobject
= object
;; cobject
= nobject
) {
397 /* cobject is locked */
400 vm_info_object_t
*vio
=
401 &((vm_info_object_t
*) addr
)[used
];
404 (vm_offset_t
) cobject
;
409 vio
->vio_resident_page_count
=
410 cobject
->resident_page_count
;
412 (vm_offset_t
) cobject
->copy
;
414 (vm_offset_t
) cobject
->shadow
;
415 vio
->vio_shadow_offset
=
416 cobject
->shadow_offset
;
417 vio
->vio_paging_offset
=
418 cobject
->paging_offset
;
419 vio
->vio_copy_strategy
=
420 cobject
->copy_strategy
;
421 vio
->vio_last_alloc
=
423 vio
->vio_paging_in_progress
=
424 cobject
->paging_in_progress
;
425 vio
->vio_pager_created
=
426 cobject
->pager_created
;
427 vio
->vio_pager_initialized
=
428 cobject
->pager_initialized
;
429 vio
->vio_pager_ready
=
430 cobject
->pager_ready
;
431 vio
->vio_can_persist
=
432 cobject
->can_persist
;
440 (cobject
->purgable
!= VM_PURGABLE_DENY
);
441 vio
->vio_purgable_volatile
=
442 (cobject
->purgable
== VM_PURGABLE_VOLATILE
||
443 cobject
->purgable
== VM_PURGABLE_EMPTY
);
447 nobject
= cobject
->shadow
;
448 if (nobject
== VM_OBJECT_NULL
) {
449 vm_object_unlock(cobject
);
453 vm_object_lock(nobject
);
454 vm_object_unlock(cobject
);
462 /* must allocate more memory */
465 kmem_free(ipc_kernel_map
, addr
, size
);
466 size
= round_page_32(2 * used
* sizeof(vm_info_object_t
));
468 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, VM_FLAGS_ANYWHERE
);
469 if (kr
!= KERN_SUCCESS
)
470 return KERN_RESOURCE_SHORTAGE
;
472 kr
= vm_map_wire(ipc_kernel_map
, vm_map_trunc_page(addr
),
473 vm_map_round_page(addr
+ size
),
474 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
475 assert(kr
== KERN_SUCCESS
);
478 /* free excess memory; make remaining memory pageable */
481 copy
= VM_MAP_COPY_NULL
;
484 kmem_free(ipc_kernel_map
, addr
, size
);
486 vm_size_t size_used
=
487 round_page_32(used
* sizeof(vm_info_object_t
));
489 kr
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
490 vm_map_round_page(addr
+ size_used
), FALSE
);
491 assert(kr
== KERN_SUCCESS
);
493 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
494 (vm_map_size_t
)size_used
, TRUE
, ©
);
495 assert(kr
== KERN_SUCCESS
);
497 if (size
!= size_used
)
498 kmem_free(ipc_kernel_map
,
499 addr
+ size_used
, size
- size_used
);
503 *objectsp
= (vm_info_object_array_t
) copy
;
506 #endif /* MACH_VM_DEBUG */
509 * Return an array of virtual pages that are mapped to a task.
512 vm_mapped_pages_info(
513 __DEBUG_ONLY vm_map_t map
,
514 __DEBUG_ONLY page_address_array_t
*pages
,
515 __DEBUG_ONLY mach_msg_type_number_t
*pages_count
)
521 vm_size_t size
, size_used
;
522 unsigned int actual
, space
;
523 page_address_array_t list
;
526 if (map
== VM_MAP_NULL
)
527 return (KERN_INVALID_ARGUMENT
);
530 size
= pmap_resident_count(pmap
) * sizeof(vm_offset_t
);
531 size
= round_page_32(size
);
534 (void) vm_allocate(ipc_kernel_map
, &addr
, size
, VM_FLAGS_ANYWHERE
);
535 (void) vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
536 vm_map_round_page(addr
+ size
), FALSE
);
538 list
= (page_address_array_t
) addr
;
539 space
= size
/ sizeof(vm_offset_t
);
541 actual
= pmap_list_resident_pages(pmap
,
548 * Free memory if not enough
550 (void) kmem_free(ipc_kernel_map
, addr
, size
);
553 * Try again, doubling the size
555 size
= round_page_32(actual
* sizeof(vm_offset_t
));
560 (void) kmem_free(ipc_kernel_map
, addr
, size
);
563 *pages_count
= actual
;
564 size_used
= round_page_32(actual
* sizeof(vm_offset_t
));
565 (void) vm_map_wire(ipc_kernel_map
, vm_map_trunc_page(addr
),
566 vm_map_round_page(addr
+ size
),
567 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
568 (void) vm_map_copyin(ipc_kernel_map
,
569 (vm_map_address_t
)addr
,
570 (vm_map_size_t
)size_used
,
572 (vm_map_copy_t
*)pages
);
573 if (size_used
!= size
) {
574 (void) kmem_free(ipc_kernel_map
,
580 return (KERN_SUCCESS
);
581 #endif /* MACH_VM_DEBUG */
585 * Routine: host_virtual_physical_table_info
587 * Return information about the VP table.
589 * Nothing locked. Obeys CountInOut protocol.
591 * KERN_SUCCESS Returned information.
592 * KERN_INVALID_HOST The host is null.
593 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
597 host_virtual_physical_table_info(
598 __DEBUG_ONLY host_t host
,
599 __DEBUG_ONLY hash_info_bucket_array_t
*infop
,
600 __DEBUG_ONLY mach_msg_type_number_t
*countp
)
607 hash_info_bucket_t
*info
;
608 unsigned int potential
, actual
;
611 if (host
== HOST_NULL
)
612 return KERN_INVALID_HOST
;
614 /* start with in-line data */
620 actual
= vm_page_info(info
, potential
);
621 if (actual
<= potential
)
624 /* allocate more memory */
627 kmem_free(ipc_kernel_map
, addr
, size
);
629 size
= round_page_32(actual
* sizeof *info
);
630 kr
= kmem_alloc_pageable(ipc_kernel_map
, &addr
, size
);
631 if (kr
!= KERN_SUCCESS
)
632 return KERN_RESOURCE_SHORTAGE
;
634 info
= (hash_info_bucket_t
*) addr
;
635 potential
= size
/sizeof *info
;
638 if (info
== *infop
) {
639 /* data fit in-line; nothing to deallocate */
642 } else if (actual
== 0) {
643 kmem_free(ipc_kernel_map
, addr
, size
);
650 used
= round_page_32(actual
* sizeof *info
);
653 kmem_free(ipc_kernel_map
, addr
+ used
, size
- used
);
655 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
656 (vm_map_size_t
)used
, TRUE
, ©
);
657 assert(kr
== KERN_SUCCESS
);
659 *infop
= (hash_info_bucket_t
*) copy
;
664 #endif /* MACH_VM_DEBUG */