2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: vm/vm_debug.c.
57 * Exported kernel calls. See mach_debug/mach_debug.defs.
59 #include <mach_vm_debug.h>
60 #include <mach/kern_return.h>
61 #include <mach/mach_host_server.h>
62 #include <mach/vm_map_server.h>
63 #include <mach_debug/vm_info.h>
64 #include <mach_debug/page_info.h>
65 #include <mach_debug/hash_info.h>
68 #include <mach/machine/vm_types.h>
69 #include <mach/memory_object_types.h>
70 #include <mach/vm_prot.h>
71 #include <mach/vm_inherit.h>
72 #include <mach/vm_param.h>
73 #include <kern/thread.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_object.h>
77 #include <kern/task.h>
78 #include <kern/host.h>
79 #include <ipc/ipc_port.h>
80 #include <vm/vm_debug.h>
84 * Routine: mach_vm_region_info [kernel call]
86 * Retrieve information about a VM region,
87 * including info about the object chain.
91 * KERN_SUCCESS Retrieve region/object info.
92 * KERN_INVALID_TASK The map is null.
93 * KERN_NO_SPACE There is no entry at/after the address.
94 * KERN_RESOURCE_SHORTAGE Can't allocate memory.
101 vm_info_region_t
*regionp
,
102 vm_info_object_array_t
*objectsp
,
103 mach_msg_type_number_t
*objectsCntp
)
109 vm_offset_t addr
; /* memory for OOL data */
110 vm_size_t size
; /* size of the memory */
111 unsigned int room
; /* room for this many objects */
112 unsigned int used
; /* actually this many objects */
113 vm_info_region_t region
;
116 if (map
== VM_MAP_NULL
)
117 return KERN_INVALID_TASK
;
119 size
= 0; /* no memory allocated yet */
122 vm_map_t cmap
; /* current map in traversal */
123 vm_map_t nmap
; /* next map to look at */
124 vm_map_entry_t entry
;
125 vm_object_t object
, cobject
, nobject
;
127 /* nothing is locked */
129 vm_map_lock_read(map
);
130 for (cmap
= map
;; cmap
= nmap
) {
131 /* cmap is read-locked */
133 if (!vm_map_lookup_entry(cmap
, address
, &entry
)) {
134 entry
= entry
->vme_next
;
135 if (entry
== vm_map_to_entry(cmap
)) {
136 vm_map_unlock_read(cmap
);
138 kmem_free(ipc_kernel_map
,
140 return KERN_NO_SPACE
;
144 if (entry
->is_sub_map
)
145 nmap
= entry
->object
.sub_map
;
149 /* move down to the lower map */
151 vm_map_lock_read(nmap
);
152 vm_map_unlock_read(cmap
);
155 /* cmap is read-locked; we have a real entry */
157 object
= entry
->object
.vm_object
;
158 region
.vir_start
= entry
->vme_start
;
159 region
.vir_end
= entry
->vme_end
;
160 region
.vir_object
= (vm_offset_t
) object
;
161 region
.vir_offset
= entry
->offset
;
162 region
.vir_needs_copy
= entry
->needs_copy
;
163 region
.vir_protection
= entry
->protection
;
164 region
.vir_max_protection
= entry
->max_protection
;
165 region
.vir_inheritance
= entry
->inheritance
;
166 region
.vir_wired_count
= entry
->wired_count
;
167 region
.vir_user_wired_count
= entry
->user_wired_count
;
170 room
= size
/ sizeof(vm_info_object_t
);
172 if (object
== VM_OBJECT_NULL
) {
173 vm_map_unlock_read(cmap
);
174 /* no memory needed */
178 vm_object_lock(object
);
179 vm_map_unlock_read(cmap
);
181 for (cobject
= object
;; cobject
= nobject
) {
182 /* cobject is locked */
185 vm_info_object_t
*vio
=
186 &((vm_info_object_t
*) addr
)[used
];
189 (vm_offset_t
) cobject
;
194 vio
->vio_resident_page_count
=
195 cobject
->resident_page_count
;
196 vio
->vio_absent_count
=
197 cobject
->absent_count
;
199 (vm_offset_t
) cobject
->copy
;
201 (vm_offset_t
) cobject
->shadow
;
202 vio
->vio_shadow_offset
=
203 cobject
->shadow_offset
;
204 vio
->vio_paging_offset
=
205 cobject
->paging_offset
;
206 vio
->vio_copy_strategy
=
207 cobject
->copy_strategy
;
208 vio
->vio_last_alloc
=
210 vio
->vio_paging_in_progress
=
211 cobject
->paging_in_progress
;
212 vio
->vio_pager_created
=
213 cobject
->pager_created
;
214 vio
->vio_pager_initialized
=
215 cobject
->pager_initialized
;
216 vio
->vio_pager_ready
=
217 cobject
->pager_ready
;
218 vio
->vio_can_persist
=
219 cobject
->can_persist
;
226 vio
->vio_lock_in_progress
=
227 cobject
->lock_in_progress
;
228 vio
->vio_lock_restart
=
229 cobject
->lock_restart
;
233 nobject
= cobject
->shadow
;
234 if (nobject
== VM_OBJECT_NULL
) {
235 vm_object_unlock(cobject
);
239 vm_object_lock(nobject
);
240 vm_object_unlock(cobject
);
248 /* must allocate more memory */
251 kmem_free(ipc_kernel_map
, addr
, size
);
252 size
= round_page(2 * used
* sizeof(vm_info_object_t
));
254 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, TRUE
);
255 if (kr
!= KERN_SUCCESS
)
256 return KERN_RESOURCE_SHORTAGE
;
258 kr
= vm_map_wire(ipc_kernel_map
, addr
, addr
+ size
,
259 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
260 assert(kr
== KERN_SUCCESS
);
263 /* free excess memory; make remaining memory pageable */
266 copy
= VM_MAP_COPY_NULL
;
269 kmem_free(ipc_kernel_map
, addr
, size
);
271 vm_size_t size_used
=
272 round_page(used
* sizeof(vm_info_object_t
));
274 kr
= vm_map_unwire(ipc_kernel_map
, addr
, addr
+ size_used
, FALSE
);
275 assert(kr
== KERN_SUCCESS
);
277 kr
= vm_map_copyin(ipc_kernel_map
, addr
, size_used
,
279 assert(kr
== KERN_SUCCESS
);
281 if (size
!= size_used
)
282 kmem_free(ipc_kernel_map
,
283 addr
+ size_used
, size
- size_used
);
287 *objectsp
= (vm_info_object_array_t
) copy
;
290 #endif /* MACH_VM_DEBUG */
293 * Temporary call for 64 bit data path interface transiotion
297 mach_vm_region_info_64(
300 vm_info_region_64_t
*regionp
,
301 vm_info_object_array_t
*objectsp
,
302 mach_msg_type_number_t
*objectsCntp
)
308 vm_offset_t addr
; /* memory for OOL data */
309 vm_size_t size
; /* size of the memory */
310 unsigned int room
; /* room for this many objects */
311 unsigned int used
; /* actually this many objects */
312 vm_info_region_64_t region
;
315 if (map
== VM_MAP_NULL
)
316 return KERN_INVALID_TASK
;
318 size
= 0; /* no memory allocated yet */
321 vm_map_t cmap
; /* current map in traversal */
322 vm_map_t nmap
; /* next map to look at */
323 vm_map_entry_t entry
;
324 vm_object_t object
, cobject
, nobject
;
326 /* nothing is locked */
328 vm_map_lock_read(map
);
329 for (cmap
= map
;; cmap
= nmap
) {
330 /* cmap is read-locked */
332 if (!vm_map_lookup_entry(cmap
, address
, &entry
)) {
333 entry
= entry
->vme_next
;
334 if (entry
== vm_map_to_entry(cmap
)) {
335 vm_map_unlock_read(cmap
);
337 kmem_free(ipc_kernel_map
,
339 return KERN_NO_SPACE
;
343 if (entry
->is_sub_map
)
344 nmap
= entry
->object
.sub_map
;
348 /* move down to the lower map */
350 vm_map_lock_read(nmap
);
351 vm_map_unlock_read(cmap
);
354 /* cmap is read-locked; we have a real entry */
356 object
= entry
->object
.vm_object
;
357 region
.vir_start
= entry
->vme_start
;
358 region
.vir_end
= entry
->vme_end
;
359 region
.vir_object
= (vm_offset_t
) object
;
360 region
.vir_offset
= entry
->offset
;
361 region
.vir_needs_copy
= entry
->needs_copy
;
362 region
.vir_protection
= entry
->protection
;
363 region
.vir_max_protection
= entry
->max_protection
;
364 region
.vir_inheritance
= entry
->inheritance
;
365 region
.vir_wired_count
= entry
->wired_count
;
366 region
.vir_user_wired_count
= entry
->user_wired_count
;
369 room
= size
/ sizeof(vm_info_object_t
);
371 if (object
== VM_OBJECT_NULL
) {
372 vm_map_unlock_read(cmap
);
373 /* no memory needed */
377 vm_object_lock(object
);
378 vm_map_unlock_read(cmap
);
380 for (cobject
= object
;; cobject
= nobject
) {
381 /* cobject is locked */
384 vm_info_object_t
*vio
=
385 &((vm_info_object_t
*) addr
)[used
];
388 (vm_offset_t
) cobject
;
393 vio
->vio_resident_page_count
=
394 cobject
->resident_page_count
;
395 vio
->vio_absent_count
=
396 cobject
->absent_count
;
398 (vm_offset_t
) cobject
->copy
;
400 (vm_offset_t
) cobject
->shadow
;
401 vio
->vio_shadow_offset
=
402 cobject
->shadow_offset
;
403 vio
->vio_paging_offset
=
404 cobject
->paging_offset
;
405 vio
->vio_copy_strategy
=
406 cobject
->copy_strategy
;
407 vio
->vio_last_alloc
=
409 vio
->vio_paging_in_progress
=
410 cobject
->paging_in_progress
;
411 vio
->vio_pager_created
=
412 cobject
->pager_created
;
413 vio
->vio_pager_initialized
=
414 cobject
->pager_initialized
;
415 vio
->vio_pager_ready
=
416 cobject
->pager_ready
;
417 vio
->vio_can_persist
=
418 cobject
->can_persist
;
425 vio
->vio_lock_in_progress
=
426 cobject
->lock_in_progress
;
427 vio
->vio_lock_restart
=
428 cobject
->lock_restart
;
432 nobject
= cobject
->shadow
;
433 if (nobject
== VM_OBJECT_NULL
) {
434 vm_object_unlock(cobject
);
438 vm_object_lock(nobject
);
439 vm_object_unlock(cobject
);
447 /* must allocate more memory */
450 kmem_free(ipc_kernel_map
, addr
, size
);
451 size
= round_page(2 * used
* sizeof(vm_info_object_t
));
453 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, TRUE
);
454 if (kr
!= KERN_SUCCESS
)
455 return KERN_RESOURCE_SHORTAGE
;
457 kr
= vm_map_wire(ipc_kernel_map
, addr
, addr
+ size
,
458 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
459 assert(kr
== KERN_SUCCESS
);
462 /* free excess memory; make remaining memory pageable */
465 copy
= VM_MAP_COPY_NULL
;
468 kmem_free(ipc_kernel_map
, addr
, size
);
470 vm_size_t size_used
=
471 round_page(used
* sizeof(vm_info_object_t
));
473 kr
= vm_map_unwire(ipc_kernel_map
, addr
, addr
+ size_used
, FALSE
);
474 assert(kr
== KERN_SUCCESS
);
476 kr
= vm_map_copyin(ipc_kernel_map
, addr
, size_used
,
478 assert(kr
== KERN_SUCCESS
);
480 if (size
!= size_used
)
481 kmem_free(ipc_kernel_map
,
482 addr
+ size_used
, size
- size_used
);
486 *objectsp
= (vm_info_object_array_t
) copy
;
489 #endif /* MACH_VM_DEBUG */
492 * Return an array of virtual pages that are mapped to a task.
495 vm_mapped_pages_info(
497 page_address_array_t
*pages
,
498 mach_msg_type_number_t
*pages_count
)
504 vm_size_t size
, size_used
;
505 unsigned int actual
, space
;
506 page_address_array_t list
;
509 if (map
== VM_MAP_NULL
)
510 return (KERN_INVALID_ARGUMENT
);
513 size
= pmap_resident_count(pmap
) * sizeof(vm_offset_t
);
514 size
= round_page(size
);
517 (void) vm_allocate(ipc_kernel_map
, &addr
, size
, TRUE
);
518 (void) vm_map_unwire(ipc_kernel_map
, addr
, addr
+ size
, FALSE
);
520 list
= (page_address_array_t
) addr
;
521 space
= size
/ sizeof(vm_offset_t
);
523 actual
= pmap_list_resident_pages(pmap
,
530 * Free memory if not enough
532 (void) kmem_free(ipc_kernel_map
, addr
, size
);
535 * Try again, doubling the size
537 size
= round_page(actual
* sizeof(vm_offset_t
));
542 (void) kmem_free(ipc_kernel_map
, addr
, size
);
545 *pages_count
= actual
;
546 size_used
= round_page(actual
* sizeof(vm_offset_t
));
547 (void) vm_map_wire(ipc_kernel_map
,
549 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
550 (void) vm_map_copyin(
555 (vm_map_copy_t
*)pages
);
556 if (size_used
!= size
) {
557 (void) kmem_free(ipc_kernel_map
,
563 return (KERN_SUCCESS
);
564 #endif /* MACH_VM_DEBUG */
568 * Routine: host_virtual_physical_table_info
570 * Return information about the VP table.
572 * Nothing locked. Obeys CountInOut protocol.
574 * KERN_SUCCESS Returned information.
575 * KERN_INVALID_HOST The host is null.
576 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
580 host_virtual_physical_table_info(
582 hash_info_bucket_array_t
*infop
,
583 mach_msg_type_number_t
*countp
)
590 hash_info_bucket_t
*info
;
591 unsigned int potential
, actual
;
594 if (host
== HOST_NULL
)
595 return KERN_INVALID_HOST
;
597 /* start with in-line data */
603 actual
= vm_page_info(info
, potential
);
604 if (actual
<= potential
)
607 /* allocate more memory */
610 kmem_free(ipc_kernel_map
, addr
, size
);
612 size
= round_page(actual
* sizeof *info
);
613 kr
= kmem_alloc_pageable(ipc_kernel_map
, &addr
, size
);
614 if (kr
!= KERN_SUCCESS
)
615 return KERN_RESOURCE_SHORTAGE
;
617 info
= (hash_info_bucket_t
*) addr
;
618 potential
= size
/sizeof *info
;
621 if (info
== *infop
) {
622 /* data fit in-line; nothing to deallocate */
625 } else if (actual
== 0) {
626 kmem_free(ipc_kernel_map
, addr
, size
);
633 used
= round_page(actual
* sizeof *info
);
636 kmem_free(ipc_kernel_map
, addr
+ used
, size
- used
);
638 kr
= vm_map_copyin(ipc_kernel_map
, addr
, used
,
640 assert(kr
== KERN_SUCCESS
);
642 *infop
= (hash_info_bucket_t
*) copy
;
647 #endif /* MACH_VM_DEBUG */