]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/zalloc.c
xnu-201.tar.gz
[apple/xnu.git] / osfmk / kern / zalloc.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
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.
35 *
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.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * File: kern/zalloc.c
54 * Author: Avadis Tevanian, Jr.
55 *
56 * Zone-based memory allocator. A zone is a collection of fixed size
57 * data blocks for which quick allocation/deallocation is possible.
58 */
59 #include <zone_debug.h>
60 #include <norma_vm.h>
61 #include <mach_kdb.h>
62 #include <kern/ast.h>
63 #include <kern/assert.h>
64 #include <kern/macro_help.h>
65 #include <kern/sched.h>
66 #include <kern/lock.h>
67 #include <kern/sched_prim.h>
68 #include <kern/misc_protos.h>
69 #include <kern/thread_call.h>
70 #include <kern/zalloc.h>
71 #include <mach/vm_param.h>
72 #include <vm/vm_kern.h>
73 #include <machine/machparam.h>
74
75
76 #if MACH_ASSERT
77 /* Detect use of zone elt after freeing it by two methods:
78 * (1) Range-check the free-list "next" ptr for sanity.
79 * (2) Store the ptr in two different words, and compare them against
80 * each other when re-using the zone elt, to detect modifications;
81 */
82
83 #if defined(__alpha)
84
85 #define is_kernel_data_addr(a) \
86 (!(a) || IS_SYS_VA(a) && !((a) & (sizeof(long)-1)))
87
88 #else /* !defined(__alpha) */
89
90 #define is_kernel_data_addr(a) \
91 (!(a) || (a) >= VM_MIN_KERNEL_ADDRESS && !((a) & 0x3))
92
93 #endif /* defined(__alpha) */
94
95 /* Should we set all words of the zone element to an illegal address
96 * when it is freed, to help catch usage after freeing? The down-side
97 * is that this obscures the identity of the freed element.
98 */
99 boolean_t zfree_clear = FALSE;
100
101 #define ADD_TO_ZONE(zone, element) \
102 MACRO_BEGIN \
103 if (zfree_clear) \
104 { int i; \
105 for (i=1; \
106 i < zone->elem_size/sizeof(vm_offset_t) - 1; \
107 i++) \
108 ((vm_offset_t *)(element))[i] = 0xdeadbeef; \
109 } \
110 ((vm_offset_t *)(element))[0] = (zone)->free_elements; \
111 (zone)->free_elements = (vm_offset_t) (element); \
112 (zone)->count--; \
113 MACRO_END
114
115 #define REMOVE_FROM_ZONE(zone, ret, type) \
116 MACRO_BEGIN \
117 (ret) = (type) (zone)->free_elements; \
118 if ((ret) != (type) 0) { \
119 if (!is_kernel_data_addr(((vm_offset_t *)(ret))[0])) { \
120 panic("A freed zone element has been modified.\n"); \
121 } \
122 (zone)->count++; \
123 (zone)->free_elements = *((vm_offset_t *)(ret)); \
124 } \
125 MACRO_END
126 #else /* MACH_ASSERT */
127
128 #define ADD_TO_ZONE(zone, element) \
129 MACRO_BEGIN \
130 *((vm_offset_t *)(element)) = (zone)->free_elements; \
131 (zone)->free_elements = (vm_offset_t) (element); \
132 (zone)->count--; \
133 MACRO_END
134
135 #define REMOVE_FROM_ZONE(zone, ret, type) \
136 MACRO_BEGIN \
137 (ret) = (type) (zone)->free_elements; \
138 if ((ret) != (type) 0) { \
139 (zone)->count++; \
140 (zone)->free_elements = *((vm_offset_t *)(ret)); \
141 } \
142 MACRO_END
143
144 #endif /* MACH_ASSERT */
145
146 #if ZONE_DEBUG
147 #define zone_debug_enabled(z) z->active_zones.next
148 #endif /* ZONE_DEBUG */
149
150 /*
151 * Support for garbage collection of unused zone pages:
152 */
153
154 struct zone_page_table_entry {
155 struct zone_page_table_entry *next;
156 short in_free_list;
157 short alloc_count;
158 };
159
160 extern struct zone_page_table_entry * zone_page_table;
161
162 #define lock_zone_page_table() simple_lock(&zone_page_table_lock)
163 #define unlock_zone_page_table() simple_unlock(&zone_page_table_lock)
164
165 #define zone_page(addr) \
166 (&(zone_page_table[(atop(((vm_offset_t)addr) - zone_map_min_address))]))
167
168 /* Forwards */
169 void zone_page_init(
170 vm_offset_t addr,
171 vm_size_t size,
172 int value);
173
174 void zone_page_alloc(
175 vm_offset_t addr,
176 vm_size_t size);
177
178 void zone_add_free_page_list(
179 struct zone_page_table_entry **free_list,
180 vm_offset_t addr,
181 vm_size_t size);
182 void zone_page_dealloc(
183 vm_offset_t addr,
184 vm_size_t size);
185
186 void zone_page_in_use(
187 vm_offset_t addr,
188 vm_size_t size);
189
190 void zone_page_free(
191 vm_offset_t addr,
192 vm_size_t size);
193
194 boolean_t zone_page_collectable(
195 vm_offset_t addr,
196 vm_size_t size);
197
198 void zone_page_keep(
199 vm_offset_t addr,
200 vm_size_t size);
201
202 void zalloc_async(
203 thread_call_param_t p0,
204 thread_call_param_t p1);
205
206
207 #if ZONE_DEBUG && MACH_KDB
208 int zone_count(
209 zone_t z,
210 int tail);
211 #endif /* ZONE_DEBUG && MACH_KDB */
212
213 vm_map_t zone_map = VM_MAP_NULL;
214
215 zone_t zone_zone = ZONE_NULL; /* the zone containing other zones */
216
217 /*
218 * The VM system gives us an initial chunk of memory.
219 * It has to be big enough to allocate the zone_zone
220 */
221
222 vm_offset_t zdata;
223 vm_size_t zdata_size;
224
225 #define lock_zone(zone) \
226 MACRO_BEGIN \
227 simple_lock(&zone->lock); \
228 MACRO_END
229
230 #define unlock_zone(zone) \
231 MACRO_BEGIN \
232 simple_unlock(&zone->lock); \
233 MACRO_END
234
235 #define lock_zone_init(zone) \
236 MACRO_BEGIN \
237 simple_lock_init(&zone->lock, ETAP_MISC_ZONE); \
238 MACRO_END
239
240 #define lock_try_zone(zone) simple_lock_try(&zone->lock)
241
242 kern_return_t zget_space(
243 vm_offset_t size,
244 vm_offset_t *result);
245
246 decl_simple_lock_data(,zget_space_lock)
247 vm_offset_t zalloc_next_space;
248 vm_offset_t zalloc_end_of_space;
249 vm_size_t zalloc_wasted_space;
250
251 /*
252 * Garbage collection map information
253 */
254 decl_simple_lock_data(, zone_page_table_lock)
255 struct zone_page_table_entry * zone_page_table;
256 vm_offset_t zone_map_min_address;
257 vm_offset_t zone_map_max_address;
258 integer_t zone_pages;
259
260 /*
261 * Exclude more than one concurrent garbage collection
262 */
263 decl_mutex_data(, zone_gc_lock)
264
265 #define from_zone_map(addr) \
266 ((vm_offset_t)(addr) >= zone_map_min_address && \
267 (vm_offset_t)(addr) < zone_map_max_address)
268
269 #define ZONE_PAGE_USED 0
270 #define ZONE_PAGE_UNUSED -1
271
272
273 /*
274 * Protects first_zone, last_zone, num_zones,
275 * and the next_zone field of zones.
276 */
277 decl_simple_lock_data(, all_zones_lock)
278 zone_t first_zone;
279 zone_t *last_zone;
280 int num_zones;
281
282 boolean_t zone_gc_allowed = TRUE;
283 boolean_t zone_gc_forced = FALSE;
284 unsigned zone_gc_last_tick = 0;
285 unsigned zone_gc_max_rate = 0; /* in ticks */
286
287
288 /*
289 * zinit initializes a new zone. The zone data structures themselves
290 * are stored in a zone, which is initially a static structure that
291 * is initialized by zone_init.
292 */
293 zone_t
294 zinit(
295 vm_size_t size, /* the size of an element */
296 vm_size_t max, /* maximum memory to use */
297 vm_size_t alloc, /* allocation size */
298 char *name) /* a name for the zone */
299 {
300 zone_t z;
301
302 if (zone_zone == ZONE_NULL) {
303 if (zget_space(sizeof(struct zone), (vm_offset_t *)&z)
304 != KERN_SUCCESS)
305 return(ZONE_NULL);
306 } else
307 z = (zone_t) zalloc(zone_zone);
308 if (z == ZONE_NULL)
309 return(ZONE_NULL);
310
311 /*
312 * Round off all the parameters appropriately.
313 */
314 if (size < sizeof(z->free_elements))
315 size = sizeof(z->free_elements);
316 size = ((size-1) + sizeof(z->free_elements)) -
317 ((size-1) % sizeof(z->free_elements));
318 if (alloc == 0)
319 alloc = PAGE_SIZE;
320 alloc = round_page(alloc);
321 max = round_page(max);
322 /*
323 * We look for an allocation size with least fragmentation
324 * in the range of 1 - 5 pages. This size will be used unless
325 * the user suggestion is larger AND has less fragmentation
326 */
327 { vm_size_t best, waste; unsigned int i;
328 best = PAGE_SIZE;
329 waste = best % size;
330 for (i = 2; i <= 5; i++){ vm_size_t tsize, twaste;
331 tsize = i * PAGE_SIZE;
332 twaste = tsize % size;
333 if (twaste < waste)
334 best = tsize, waste = twaste;
335 }
336 if (alloc <= best || (alloc % size >= waste))
337 alloc = best;
338 }
339 if (max && (max < alloc))
340 max = alloc;
341
342 z->free_elements = 0;
343 z->cur_size = 0;
344 z->max_size = max;
345 z->elem_size = size;
346 z->alloc_size = alloc;
347 z->zone_name = name;
348 z->count = 0;
349 z->doing_alloc = FALSE;
350 z->exhaustible = FALSE;
351 z->collectable = TRUE;
352 z->allows_foreign = FALSE;
353 z->expandable = TRUE;
354 z->waiting = FALSE;
355 z->async_pending = FALSE;
356
357 #if ZONE_DEBUG
358 z->active_zones.next = z->active_zones.prev = 0;
359 zone_debug_enable(z);
360 #endif /* ZONE_DEBUG */
361 lock_zone_init(z);
362
363 /*
364 * Add the zone to the all-zones list.
365 */
366
367 z->next_zone = ZONE_NULL;
368 thread_call_setup(&z->call_async_alloc, zalloc_async, z);
369 simple_lock(&all_zones_lock);
370 *last_zone = z;
371 last_zone = &z->next_zone;
372 num_zones++;
373 simple_unlock(&all_zones_lock);
374
375 return(z);
376 }
377
378 /*
379 * Cram the given memory into the specified zone.
380 */
381 void
382 zcram(
383 register zone_t zone,
384 vm_offset_t newmem,
385 vm_size_t size)
386 {
387 register vm_size_t elem_size;
388
389 /* Basic sanity checks */
390 assert(zone != ZONE_NULL && newmem != (vm_offset_t)0);
391 assert(!zone->collectable || zone->allows_foreign
392 || (from_zone_map(newmem) && from_zone_map(newmem+size-1)));
393
394 elem_size = zone->elem_size;
395
396 lock_zone(zone);
397 while (size >= elem_size) {
398 ADD_TO_ZONE(zone, newmem);
399 if (from_zone_map(newmem))
400 zone_page_alloc(newmem, elem_size);
401 zone->count++; /* compensate for ADD_TO_ZONE */
402 size -= elem_size;
403 newmem += elem_size;
404 zone->cur_size += elem_size;
405 }
406 unlock_zone(zone);
407 }
408
409 /*
410 * Contiguous space allocator for non-paged zones. Allocates "size" amount
411 * of memory from zone_map.
412 */
413
414 kern_return_t
415 zget_space(
416 vm_offset_t size,
417 vm_offset_t *result)
418 {
419 vm_offset_t new_space = 0;
420 vm_size_t space_to_add;
421
422 simple_lock(&zget_space_lock);
423 while ((zalloc_next_space + size) > zalloc_end_of_space) {
424 /*
425 * Add at least one page to allocation area.
426 */
427
428 space_to_add = round_page(size);
429
430 if (new_space == 0) {
431 kern_return_t retval;
432 /*
433 * Memory cannot be wired down while holding
434 * any locks that the pageout daemon might
435 * need to free up pages. [Making the zget_space
436 * lock a complex lock does not help in this
437 * regard.]
438 *
439 * Unlock and allocate memory. Because several
440 * threads might try to do this at once, don't
441 * use the memory before checking for available
442 * space again.
443 */
444
445 simple_unlock(&zget_space_lock);
446
447 retval = kernel_memory_allocate(zone_map, &new_space,
448 space_to_add, 0, KMA_KOBJECT|KMA_NOPAGEWAIT);
449 if (retval != KERN_SUCCESS)
450 return(retval);
451 zone_page_init(new_space, space_to_add,
452 ZONE_PAGE_USED);
453 simple_lock(&zget_space_lock);
454 continue;
455 }
456
457
458 /*
459 * Memory was allocated in a previous iteration.
460 *
461 * Check whether the new region is contiguous
462 * with the old one.
463 */
464
465 if (new_space != zalloc_end_of_space) {
466 /*
467 * Throw away the remainder of the
468 * old space, and start a new one.
469 */
470 zalloc_wasted_space +=
471 zalloc_end_of_space - zalloc_next_space;
472 zalloc_next_space = new_space;
473 }
474
475 zalloc_end_of_space = new_space + space_to_add;
476
477 new_space = 0;
478 }
479 *result = zalloc_next_space;
480 zalloc_next_space += size;
481 simple_unlock(&zget_space_lock);
482
483 if (new_space != 0)
484 kmem_free(zone_map, new_space, space_to_add);
485
486 return(KERN_SUCCESS);
487 }
488
489
490 /*
491 * Steal memory for the zone package. Called from
492 * vm_page_bootstrap().
493 */
494 void
495 zone_steal_memory(void)
496 {
497 zdata_size = round_page(128*sizeof(struct zone));
498 zdata = pmap_steal_memory(zdata_size);
499 }
500
501
502 /*
503 * Fill a zone with enough memory to contain at least nelem elements.
504 * Memory is obtained with kmem_alloc_wired from the kernel_map.
505 * Return the number of elements actually put into the zone, which may
506 * be more than the caller asked for since the memory allocation is
507 * rounded up to a full page.
508 */
509 int
510 zfill(
511 zone_t zone,
512 int nelem)
513 {
514 kern_return_t kr;
515 vm_size_t size;
516 vm_offset_t memory;
517 int nalloc;
518
519 assert(nelem > 0);
520 if (nelem <= 0)
521 return 0;
522 size = nelem * zone->elem_size;
523 size = round_page(size);
524 kr = kmem_alloc_wired(kernel_map, &memory, size);
525 if (kr != KERN_SUCCESS)
526 return 0;
527
528 zone_change(zone, Z_FOREIGN, TRUE);
529 zcram(zone, memory, size);
530 nalloc = size / zone->elem_size;
531 assert(nalloc >= nelem);
532
533 return nalloc;
534 }
535
536 /*
537 * Initialize the "zone of zones" which uses fixed memory allocated
538 * earlier in memory initialization. zone_bootstrap is called
539 * before zone_init.
540 */
541 void
542 zone_bootstrap(void)
543 {
544 vm_size_t zone_zone_size;
545 vm_offset_t zone_zone_space;
546
547 simple_lock_init(&all_zones_lock, ETAP_MISC_ZONE_ALL);
548
549 first_zone = ZONE_NULL;
550 last_zone = &first_zone;
551 num_zones = 0;
552
553 simple_lock_init(&zget_space_lock, ETAP_MISC_ZONE_GET);
554 zalloc_next_space = zdata;
555 zalloc_end_of_space = zdata + zdata_size;
556 zalloc_wasted_space = 0;
557
558 /* assertion: nobody else called zinit before us */
559 assert(zone_zone == ZONE_NULL);
560 zone_zone = zinit(sizeof(struct zone), 128 * sizeof(struct zone),
561 sizeof(struct zone), "zones");
562 zone_change(zone_zone, Z_COLLECT, FALSE);
563 zone_zone_size = zalloc_end_of_space - zalloc_next_space;
564 zget_space(zone_zone_size, &zone_zone_space);
565 zcram(zone_zone, zone_zone_space, zone_zone_size);
566 }
567
568 void
569 zone_init(
570 vm_size_t max_zonemap_size)
571 {
572 kern_return_t retval;
573 vm_offset_t zone_min;
574 vm_offset_t zone_max;
575 vm_size_t zone_table_size;
576
577 retval = kmem_suballoc(kernel_map, &zone_min, max_zonemap_size,
578 FALSE, TRUE, &zone_map);
579 if (retval != KERN_SUCCESS)
580 panic("zone_init: kmem_suballoc failed");
581 zone_max = zone_min + round_page(max_zonemap_size);
582 /*
583 * Setup garbage collection information:
584 */
585 zone_table_size = atop(zone_max - zone_min) *
586 sizeof(struct zone_page_table_entry);
587 if (kmem_alloc_wired(zone_map, (vm_offset_t *) &zone_page_table,
588 zone_table_size) != KERN_SUCCESS)
589 panic("zone_init");
590 zone_min = (vm_offset_t)zone_page_table + round_page(zone_table_size);
591 zone_pages = atop(zone_max - zone_min);
592 zone_map_min_address = zone_min;
593 zone_map_max_address = zone_max;
594 simple_lock_init(&zone_page_table_lock, ETAP_MISC_ZONE_PTABLE);
595 mutex_init(&zone_gc_lock, ETAP_NO_TRACE);
596 zone_page_init(zone_min, zone_max - zone_min, ZONE_PAGE_UNUSED);
597 }
598
599
600 /*
601 * zalloc returns an element from the specified zone.
602 */
603 vm_offset_t
604 zalloc_canblock(
605 register zone_t zone,
606 boolean_t canblock)
607 {
608 vm_offset_t addr;
609 kern_return_t retval;
610
611 assert(zone != ZONE_NULL);
612 check_simple_locks();
613
614 lock_zone(zone);
615
616 REMOVE_FROM_ZONE(zone, addr, vm_offset_t);
617
618 while ((addr == 0) && canblock) {
619 /*
620 * If nothing was there, try to get more
621 */
622 if (zone->doing_alloc) {
623 /*
624 * Someone is allocating memory for this zone.
625 * Wait for it to show up, then try again.
626 */
627 assert_wait((event_t)zone, THREAD_UNINT);
628 zone->waiting = TRUE;
629 unlock_zone(zone);
630 thread_block((void (*)(void)) 0);
631 lock_zone(zone);
632 }
633 else {
634 if ((zone->cur_size + zone->elem_size) >
635 zone->max_size) {
636 if (zone->exhaustible)
637 break;
638 if (zone->expandable) {
639 /*
640 * We're willing to overflow certain
641 * zones, but not without complaining.
642 *
643 * This is best used in conjunction
644 * with the collectable flag. What we
645 * want is an assurance we can get the
646 * memory back, assuming there's no
647 * leak.
648 */
649 zone->max_size += (zone->max_size >> 1);
650 } else {
651 unlock_zone(zone);
652
653 panic("zalloc: zone \"%s\" empty.", zone->zone_name);
654 }
655 }
656 zone->doing_alloc = TRUE;
657 unlock_zone(zone);
658
659 if (zone->collectable) {
660 vm_offset_t space;
661 vm_size_t alloc_size;
662
663 if (vm_pool_low())
664 alloc_size =
665 round_page(zone->elem_size);
666 else
667 alloc_size = zone->alloc_size;
668
669 retval = kernel_memory_allocate(zone_map,
670 &space, alloc_size, 0,
671 KMA_KOBJECT|KMA_NOPAGEWAIT);
672 if (retval == KERN_SUCCESS) {
673 zone_page_init(space, alloc_size,
674 ZONE_PAGE_USED);
675 zcram(zone, space, alloc_size);
676 } else if (retval != KERN_RESOURCE_SHORTAGE) {
677 /* would like to cause a zone_gc() */
678
679 panic("zalloc");
680 }
681 lock_zone(zone);
682 zone->doing_alloc = FALSE;
683 if (zone->waiting) {
684 zone->waiting = FALSE;
685 thread_wakeup((event_t)zone);
686 }
687 REMOVE_FROM_ZONE(zone, addr, vm_offset_t);
688 if (addr == 0 &&
689 retval == KERN_RESOURCE_SHORTAGE) {
690 unlock_zone(zone);
691
692 VM_PAGE_WAIT();
693 lock_zone(zone);
694 }
695 } else {
696 vm_offset_t space;
697 retval = zget_space(zone->elem_size, &space);
698
699 lock_zone(zone);
700 zone->doing_alloc = FALSE;
701 if (zone->waiting) {
702 zone->waiting = FALSE;
703 thread_wakeup((event_t)zone);
704 }
705 if (retval == KERN_SUCCESS) {
706 zone->count++;
707 zone->cur_size += zone->elem_size;
708 #if ZONE_DEBUG
709 if (zone_debug_enabled(zone)) {
710 enqueue_tail(&zone->active_zones, (queue_entry_t)space);
711 }
712 #endif
713 unlock_zone(zone);
714 zone_page_alloc(space, zone->elem_size);
715 #if ZONE_DEBUG
716 if (zone_debug_enabled(zone))
717 space += sizeof(queue_chain_t);
718 #endif
719 return(space);
720 }
721 if (retval == KERN_RESOURCE_SHORTAGE) {
722 unlock_zone(zone);
723
724 VM_PAGE_WAIT();
725 lock_zone(zone);
726 } else {
727 panic("zalloc");
728 }
729 }
730 }
731 if (addr == 0)
732 REMOVE_FROM_ZONE(zone, addr, vm_offset_t);
733 }
734
735 if ((addr == 0) && !canblock && (zone->async_pending == FALSE) && (!vm_pool_low())) {
736 zone->async_pending = TRUE;
737 unlock_zone(zone);
738 thread_call_enter(&zone->call_async_alloc);
739 lock_zone(zone);
740 REMOVE_FROM_ZONE(zone, addr, vm_offset_t);
741 }
742
743 #if ZONE_DEBUG
744 if (addr && zone_debug_enabled(zone)) {
745 enqueue_tail(&zone->active_zones, (queue_entry_t)addr);
746 addr += sizeof(queue_chain_t);
747 }
748 #endif
749
750 unlock_zone(zone);
751
752 return(addr);
753 }
754
755
756 vm_offset_t
757 zalloc(
758 register zone_t zone)
759 {
760 return( zalloc_canblock(zone, TRUE) );
761 }
762
763 vm_offset_t
764 zalloc_noblock(
765 register zone_t zone)
766 {
767 return( zalloc_canblock(zone, FALSE) );
768 }
769
770 void
771 zalloc_async(
772 thread_call_param_t p0,
773 thread_call_param_t p1)
774 {
775 vm_offset_t elt;
776
777 elt = zalloc_canblock((zone_t)p0, TRUE);
778 zfree((zone_t)p0, elt);
779 lock_zone(((zone_t)p0));
780 ((zone_t)p0)->async_pending = FALSE;
781 unlock_zone(((zone_t)p0));
782 }
783
784
785 /*
786 * zget returns an element from the specified zone
787 * and immediately returns nothing if there is nothing there.
788 *
789 * This form should be used when you can not block (like when
790 * processing an interrupt).
791 */
792 vm_offset_t
793 zget(
794 register zone_t zone)
795 {
796 register vm_offset_t addr;
797
798 assert( zone != ZONE_NULL );
799
800 if (!lock_try_zone(zone))
801 return ((vm_offset_t)0);
802
803 REMOVE_FROM_ZONE(zone, addr, vm_offset_t);
804 #if ZONE_DEBUG
805 if (addr && zone_debug_enabled(zone)) {
806 enqueue_tail(&zone->active_zones, (queue_entry_t)addr);
807 addr += sizeof(queue_chain_t);
808 }
809 #endif /* ZONE_DEBUG */
810 unlock_zone(zone);
811
812 return(addr);
813 }
814
815 /* Keep this FALSE by default. Large memory machine run orders of magnitude
816 slower in debug mode when true. Use debugger to enable if needed */
817 boolean_t zone_check = FALSE;
818
819 void
820 zfree(
821 register zone_t zone,
822 vm_offset_t elem)
823 {
824
825 #if MACH_ASSERT
826 /* Basic sanity checks */
827 if (zone == ZONE_NULL || elem == (vm_offset_t)0)
828 panic("zfree: NULL");
829 /* zone_gc assumes zones are never freed */
830 if (zone == zone_zone)
831 panic("zfree: freeing to zone_zone breaks zone_gc!");
832 if (zone->collectable && !zone->allows_foreign &&
833 (!from_zone_map(elem) || !from_zone_map(elem+zone->elem_size-1)))
834 panic("zfree: non-allocated memory in collectable zone!");
835 #endif
836
837 lock_zone(zone);
838 #if ZONE_DEBUG
839 if (zone_debug_enabled(zone)) {
840 queue_t tmp_elem;
841
842 elem -= sizeof(queue_chain_t);
843 if (zone_check) {
844 /* check the zone's consistency */
845
846 for (tmp_elem = queue_first(&zone->active_zones);
847 !queue_end(tmp_elem, &zone->active_zones);
848 tmp_elem = queue_next(tmp_elem))
849 if (elem == (vm_offset_t)tmp_elem)
850 break;
851 if (elem != (vm_offset_t)tmp_elem)
852 panic("zfree()ing element from wrong zone");
853 }
854 remqueue(&zone->active_zones, (queue_t) elem);
855 }
856 #endif /* ZONE_DEBUG */
857 if (zone_check) {
858 vm_offset_t this;
859
860 /* check the zone's consistency */
861
862 for (this = zone->free_elements;
863 this != 0;
864 this = * (vm_offset_t *) this)
865 if (!pmap_kernel_va(this) || this == elem)
866 panic("zfree");
867 }
868 ADD_TO_ZONE(zone, elem);
869
870 /*
871 * If elements have one or more pages, and memory is low,
872 * request to run the garbage collection in the zone the next
873 * time the pageout thread runs.
874 */
875 if (zone->elem_size >= PAGE_SIZE &&
876 vm_pool_low()){
877 zone_gc_forced = TRUE;
878 }
879 unlock_zone(zone);
880 }
881
882
883 /* Change a zone's flags.
884 * This routine must be called immediately after zinit.
885 */
886 void
887 zone_change(
888 zone_t zone,
889 unsigned int item,
890 boolean_t value)
891 {
892 assert( zone != ZONE_NULL );
893 assert( value == TRUE || value == FALSE );
894
895 switch(item){
896 case Z_EXHAUST:
897 zone->exhaustible = value;
898 break;
899 case Z_COLLECT:
900 zone->collectable = value;
901 break;
902 case Z_EXPAND:
903 zone->expandable = value;
904 break;
905 case Z_FOREIGN:
906 zone->allows_foreign = value;
907 break;
908 #if MACH_ASSERT
909 default:
910 panic("Zone_change: Wrong Item Type!");
911 /* break; */
912 #endif
913 }
914 lock_zone_init(zone);
915 }
916
917 /*
918 * Return the expected number of free elements in the zone.
919 * This calculation will be incorrect if items are zfree'd that
920 * were never zalloc'd/zget'd. The correct way to stuff memory
921 * into a zone is by zcram.
922 */
923
924 integer_t
925 zone_free_count(zone_t zone)
926 {
927 integer_t free_count;
928
929 lock_zone(zone);
930 free_count = zone->cur_size/zone->elem_size - zone->count;
931 unlock_zone(zone);
932
933 assert(free_count >= 0);
934
935 return(free_count);
936 }
937
938 /*
939 * zprealloc preallocates wired memory, exanding the specified
940 * zone to the specified size
941 */
942 void
943 zprealloc(
944 zone_t zone,
945 vm_size_t size)
946 {
947 vm_offset_t addr;
948
949 if (size != 0) {
950 if (kmem_alloc_wired(zone_map, &addr, size) != KERN_SUCCESS)
951 panic("zprealloc");
952 zone_page_init(addr, size, ZONE_PAGE_USED);
953 zcram(zone, addr, size);
954 }
955 }
956
957 /*
958 * Zone garbage collection subroutines
959 *
960 * These routines have in common the modification of entries in the
961 * zone_page_table. The latter contains one entry for every page
962 * in the zone_map.
963 *
964 * For each page table entry in the given range:
965 *
966 * zone_page_collectable - test if one (in_free_list == alloc_count)
967 * zone_page_keep - reset in_free_list
968 * zone_page_in_use - decrements in_free_list
969 * zone_page_free - increments in_free_list
970 * zone_page_init - initializes in_free_list and alloc_count
971 * zone_page_alloc - increments alloc_count
972 * zone_page_dealloc - decrements alloc_count
973 * zone_add_free_page_list - adds the page to the free list
974 *
975 * Two counts are maintained for each page, the in_free_list count and
976 * alloc_count. The alloc_count is how many zone elements have been
977 * allocated from a page. (Note that the page could contain elements
978 * that span page boundaries. The count includes these elements so
979 * one element may be counted in two pages.) In_free_list is a count
980 * of how many zone elements are currently free. If in_free_list is
981 * equal to alloc_count then the page is eligible for garbage
982 * collection.
983 *
984 * Alloc_count and in_free_list are initialized to the correct values
985 * for a particular zone when a page is zcram'ed into a zone. Subsequent
986 * gets and frees of zone elements will call zone_page_in_use and
987 * zone_page_free which modify the in_free_list count. When the zones
988 * garbage collector runs it will walk through a zones free element list,
989 * remove the elements that reside on collectable pages, and use
990 * zone_add_free_page_list to create a list of pages to be collected.
991 */
992 boolean_t
993 zone_page_collectable(
994 vm_offset_t addr,
995 vm_size_t size)
996 {
997 natural_t i, j;
998
999 #if MACH_ASSERT
1000 if (!from_zone_map(addr) || !from_zone_map(addr+size-1))
1001 panic("zone_page_collectable");
1002 #endif
1003
1004 i = atop(addr-zone_map_min_address);
1005 j = atop((addr+size-1) - zone_map_min_address);
1006 lock_zone_page_table();
1007 for (; i <= j; i++) {
1008 if (zone_page_table[i].in_free_list ==
1009 zone_page_table[i].alloc_count) {
1010 unlock_zone_page_table();
1011 return (TRUE);
1012 }
1013 }
1014 unlock_zone_page_table();
1015 return (FALSE);
1016 }
1017
1018 void
1019 zone_page_keep(
1020 vm_offset_t addr,
1021 vm_size_t size)
1022 {
1023 natural_t i, j;
1024
1025 #if MACH_ASSERT
1026 if (!from_zone_map(addr) || !from_zone_map(addr+size-1))
1027 panic("zone_page_keep");
1028 #endif
1029
1030 i = atop(addr-zone_map_min_address);
1031 j = atop((addr+size-1) - zone_map_min_address);
1032 lock_zone_page_table();
1033 for (; i <= j; i++) {
1034 zone_page_table[i].in_free_list = 0;
1035 }
1036 unlock_zone_page_table();
1037 }
1038
1039 void
1040 zone_page_in_use(
1041 vm_offset_t addr,
1042 vm_size_t size)
1043 {
1044 natural_t i, j;
1045
1046 #if MACH_ASSERT
1047 if (!from_zone_map(addr) || !from_zone_map(addr+size-1))
1048 panic("zone_page_in_use");
1049 #endif
1050
1051 i = atop(addr-zone_map_min_address);
1052 j = atop((addr+size-1) - zone_map_min_address);
1053 lock_zone_page_table();
1054 for (; i <= j; i++) {
1055 if (zone_page_table[i].in_free_list > 0)
1056 zone_page_table[i].in_free_list--;
1057 }
1058 unlock_zone_page_table();
1059 }
1060
1061 void
1062 zone_page_free(
1063 vm_offset_t addr,
1064 vm_size_t size)
1065 {
1066 natural_t i, j;
1067
1068 #if MACH_ASSERT
1069 if (!from_zone_map(addr) || !from_zone_map(addr+size-1))
1070 panic("zone_page_free");
1071 #endif
1072
1073 i = atop(addr-zone_map_min_address);
1074 j = atop((addr+size-1) - zone_map_min_address);
1075 lock_zone_page_table();
1076 for (; i <= j; i++) {
1077 assert(zone_page_table[i].in_free_list >= 0);
1078 zone_page_table[i].in_free_list++;
1079 }
1080 unlock_zone_page_table();
1081 }
1082
1083 void
1084 zone_page_init(
1085 vm_offset_t addr,
1086 vm_size_t size,
1087 int value)
1088 {
1089 natural_t i, j;
1090
1091 #if MACH_ASSERT
1092 if (!from_zone_map(addr) || !from_zone_map(addr+size-1))
1093 panic("zone_page_init");
1094 #endif
1095
1096 i = atop(addr-zone_map_min_address);
1097 j = atop((addr+size-1) - zone_map_min_address);
1098 lock_zone_page_table();
1099 for (; i <= j; i++) {
1100 zone_page_table[i].alloc_count = value;
1101 zone_page_table[i].in_free_list = 0;
1102 }
1103 unlock_zone_page_table();
1104 }
1105
1106 void
1107 zone_page_alloc(
1108 vm_offset_t addr,
1109 vm_size_t size)
1110 {
1111 natural_t i, j;
1112
1113 #if MACH_ASSERT
1114 if (!from_zone_map(addr) || !from_zone_map(addr+size-1))
1115 panic("zone_page_alloc");
1116 #endif
1117
1118 i = atop(addr-zone_map_min_address);
1119 j = atop((addr+size-1) - zone_map_min_address);
1120 lock_zone_page_table();
1121 for (; i <= j; i++) {
1122 /* Set alloc_count to (ZONE_PAGE_USED + 1) if
1123 * it was previously set to ZONE_PAGE_UNUSED.
1124 */
1125 if (zone_page_table[i].alloc_count == ZONE_PAGE_UNUSED) {
1126 zone_page_table[i].alloc_count = 1;
1127 } else {
1128 zone_page_table[i].alloc_count++;
1129 }
1130 }
1131 unlock_zone_page_table();
1132 }
1133
1134 void
1135 zone_page_dealloc(
1136 vm_offset_t addr,
1137 vm_size_t size)
1138 {
1139 natural_t i, j;
1140
1141 #if MACH_ASSERT
1142 if (!from_zone_map(addr) || !from_zone_map(addr+size-1))
1143 panic("zone_page_dealloc");
1144 #endif
1145
1146 i = atop(addr-zone_map_min_address);
1147 j = atop((addr+size-1) - zone_map_min_address);
1148 lock_zone_page_table();
1149 for (; i <= j; i++) {
1150 zone_page_table[i].alloc_count--;
1151 }
1152 unlock_zone_page_table();
1153 }
1154
1155 void
1156 zone_add_free_page_list(
1157 struct zone_page_table_entry **free_list,
1158 vm_offset_t addr,
1159 vm_size_t size)
1160 {
1161 natural_t i, j;
1162
1163 #if MACH_ASSERT
1164 if (!from_zone_map(addr) || !from_zone_map(addr+size-1))
1165 panic("zone_add_free_page_list");
1166 #endif
1167
1168 i = atop(addr-zone_map_min_address);
1169 j = atop((addr+size-1) - zone_map_min_address);
1170 lock_zone_page_table();
1171 for (; i <= j; i++) {
1172 if (zone_page_table[i].alloc_count == 0) {
1173 zone_page_table[i].next = *free_list;
1174 *free_list = &zone_page_table[i];
1175 zone_page_table[i].alloc_count = ZONE_PAGE_UNUSED;
1176 zone_page_table[i].in_free_list = 0;
1177 }
1178 }
1179 unlock_zone_page_table();
1180 }
1181
1182
1183 /* This is used for walking through a zone's free element list.
1184 */
1185 struct zone_free_entry {
1186 struct zone_free_entry * next;
1187 };
1188
1189 int reclaim_page_count = 0;
1190
1191 /* Zone garbage collection
1192 *
1193 * zone_gc will walk through all the free elements in all the
1194 * zones that are marked collectable looking for reclaimable
1195 * pages. zone_gc is called by consider_zone_gc when the system
1196 * begins to run out of memory.
1197 */
1198 void
1199 zone_gc(void)
1200 {
1201 unsigned int max_zones;
1202 zone_t z;
1203 unsigned int i;
1204 struct zone_page_table_entry *freep;
1205 struct zone_page_table_entry *zone_free_page_list;
1206
1207 mutex_lock(&zone_gc_lock);
1208
1209 /*
1210 * Note that this scheme of locking only to walk the zone list
1211 * assumes that zones are never freed (checked by zfree)
1212 */
1213 simple_lock(&all_zones_lock);
1214 max_zones = num_zones;
1215 z = first_zone;
1216 simple_unlock(&all_zones_lock);
1217
1218 #if MACH_ASSERT
1219 lock_zone_page_table();
1220 for (i = 0; i < zone_pages; i++)
1221 assert(zone_page_table[i].in_free_list == 0);
1222 unlock_zone_page_table();
1223 #endif /* MACH_ASSERT */
1224
1225 zone_free_page_list = (struct zone_page_table_entry *) 0;
1226
1227 for (i = 0; i < max_zones; i++, z = z->next_zone) {
1228 struct zone_free_entry * prev;
1229 struct zone_free_entry * elt;
1230 struct zone_free_entry * end;
1231
1232 assert(z != ZONE_NULL);
1233
1234 if (!z->collectable)
1235 continue;
1236
1237 lock_zone(z);
1238
1239 /*
1240 * Do a quick feasability check before we scan the zone:
1241 * skip unless there is likelihood of getting 1+ pages back.
1242 */
1243 if ((z->cur_size - z->count * z->elem_size) <= (2*PAGE_SIZE)){
1244 unlock_zone(z);
1245 continue;
1246 }
1247
1248 /* Count the free elements in each page. This loop
1249 * requires that all in_free_list entries are zero.
1250 *
1251 * Exit the loop early if we need to hurry up and drop
1252 * the lock to allow preemption - but we must fully process
1253 * all elements we looked at so far.
1254 */
1255 elt = (struct zone_free_entry *)(z->free_elements);
1256 while (!ast_urgency() && (elt != (struct zone_free_entry *)0)) {
1257 if (from_zone_map(elt))
1258 zone_page_free((vm_offset_t)elt, z->elem_size);
1259 elt = elt->next;
1260 }
1261 end = elt;
1262
1263 /* Now determine which elements should be removed
1264 * from the free list and, after all the elements
1265 * on a page have been removed, add the element's
1266 * page to a list of pages to be freed.
1267 */
1268 prev = elt = (struct zone_free_entry *)(z->free_elements);
1269 while (elt != end) {
1270 if (!from_zone_map(elt)) {
1271 prev = elt;
1272 elt = elt->next;
1273 continue;
1274 }
1275 if (zone_page_collectable((vm_offset_t)elt,
1276 z->elem_size)) {
1277 z->cur_size -= z->elem_size;
1278 zone_page_in_use((vm_offset_t)elt,
1279 z->elem_size);
1280 zone_page_dealloc((vm_offset_t)elt,
1281 z->elem_size);
1282 zone_add_free_page_list(&zone_free_page_list,
1283 (vm_offset_t)elt,
1284 z->elem_size);
1285 if (elt == prev) {
1286 elt = elt->next;
1287 z->free_elements =(vm_offset_t)elt;
1288 prev = elt;
1289 } else {
1290 prev->next = elt->next;
1291 elt = elt->next;
1292 }
1293 } else {
1294 /* This element is not eligible for collection
1295 * so clear in_free_list in preparation for a
1296 * subsequent garbage collection pass.
1297 */
1298 zone_page_keep((vm_offset_t)elt, z->elem_size);
1299 prev = elt;
1300 elt = elt->next;
1301 }
1302 } /* end while(elt != end) */
1303
1304 unlock_zone(z);
1305 }
1306
1307 for (freep = zone_free_page_list; freep != 0; freep = freep->next) {
1308 vm_offset_t free_addr;
1309
1310 free_addr = zone_map_min_address +
1311 PAGE_SIZE * (freep - zone_page_table);
1312 kmem_free(zone_map, free_addr, PAGE_SIZE);
1313 reclaim_page_count++;
1314 }
1315 mutex_unlock(&zone_gc_lock);
1316 }
1317
1318 /*
1319 * consider_zone_gc:
1320 *
1321 * Called by the pageout daemon when the system needs more free pages.
1322 */
1323
1324 void
1325 consider_zone_gc(void)
1326 {
1327 /*
1328 * By default, don't attempt zone GC more frequently
1329 * than once a second.
1330 */
1331
1332 if (zone_gc_max_rate == 0)
1333 zone_gc_max_rate = (1 << SCHED_TICK_SHIFT) + 1;
1334
1335 if (zone_gc_allowed &&
1336 ((sched_tick > (zone_gc_last_tick + zone_gc_max_rate)) ||
1337 zone_gc_forced)) {
1338 zone_gc_forced = FALSE;
1339 zone_gc_last_tick = sched_tick;
1340 zone_gc();
1341 }
1342 }
1343
1344 #include <mach/kern_return.h>
1345 #include <mach/machine/vm_types.h>
1346 #include <mach_debug/zone_info.h>
1347 #include <kern/host.h>
1348 #include <vm/vm_map.h>
1349 #include <vm/vm_kern.h>
1350
1351 #include <mach/mach_host_server.h>
1352
1353 kern_return_t
1354 host_zone_info(
1355 host_t host,
1356 zone_name_array_t *namesp,
1357 mach_msg_type_number_t *namesCntp,
1358 zone_info_array_t *infop,
1359 mach_msg_type_number_t *infoCntp)
1360 {
1361 zone_name_t *names;
1362 vm_offset_t names_addr;
1363 vm_size_t names_size;
1364 zone_info_t *info;
1365 vm_offset_t info_addr;
1366 vm_size_t info_size;
1367 unsigned int max_zones, i;
1368 zone_t z;
1369 zone_name_t *zn;
1370 zone_info_t *zi;
1371 kern_return_t kr;
1372
1373 if (host == HOST_NULL)
1374 return KERN_INVALID_HOST;
1375
1376 /*
1377 * We assume that zones aren't freed once allocated.
1378 * We won't pick up any zones that are allocated later.
1379 */
1380
1381 simple_lock(&all_zones_lock);
1382 #ifdef ppc
1383 max_zones = num_zones + 4;
1384 #else
1385 max_zones = num_zones + 2;
1386 #endif
1387 z = first_zone;
1388 simple_unlock(&all_zones_lock);
1389
1390 if (max_zones <= *namesCntp) {
1391 /* use in-line memory */
1392
1393 names = *namesp;
1394 } else {
1395 names_size = round_page(max_zones * sizeof *names);
1396 kr = kmem_alloc_pageable(ipc_kernel_map,
1397 &names_addr, names_size);
1398 if (kr != KERN_SUCCESS)
1399 return kr;
1400 names = (zone_name_t *) names_addr;
1401 }
1402
1403 if (max_zones <= *infoCntp) {
1404 /* use in-line memory */
1405
1406 info = *infop;
1407 } else {
1408 info_size = round_page(max_zones * sizeof *info);
1409 kr = kmem_alloc_pageable(ipc_kernel_map,
1410 &info_addr, info_size);
1411 if (kr != KERN_SUCCESS) {
1412 if (names != *namesp)
1413 kmem_free(ipc_kernel_map,
1414 names_addr, names_size);
1415 return kr;
1416 }
1417
1418 info = (zone_info_t *) info_addr;
1419 }
1420 zn = &names[0];
1421 zi = &info[0];
1422
1423 for (i = 0; i < num_zones; i++) {
1424 struct zone zcopy;
1425
1426 assert(z != ZONE_NULL);
1427
1428 lock_zone(z);
1429 zcopy = *z;
1430 unlock_zone(z);
1431
1432 simple_lock(&all_zones_lock);
1433 z = z->next_zone;
1434 simple_unlock(&all_zones_lock);
1435
1436 /* assuming here the name data is static */
1437 (void) strncpy(zn->zn_name, zcopy.zone_name,
1438 sizeof zn->zn_name);
1439
1440 zi->zi_count = zcopy.count;
1441 zi->zi_cur_size = zcopy.cur_size;
1442 zi->zi_max_size = zcopy.max_size;
1443 zi->zi_elem_size = zcopy.elem_size;
1444 zi->zi_alloc_size = zcopy.alloc_size;
1445 zi->zi_exhaustible = zcopy.exhaustible;
1446 zi->zi_collectable = zcopy.collectable;
1447
1448 zn++;
1449 zi++;
1450 }
1451 strcpy(zn->zn_name, "kernel_stacks");
1452 stack_fake_zone_info(&zi->zi_count, &zi->zi_cur_size, &zi->zi_max_size, &zi->zi_elem_size,
1453 &zi->zi_alloc_size, &zi->zi_collectable, &zi->zi_exhaustible);
1454 zn++;
1455 zi++;
1456 #ifdef ppc
1457 strcpy(zn->zn_name, "save_areas");
1458 save_fake_zone_info(&zi->zi_count, &zi->zi_cur_size, &zi->zi_max_size, &zi->zi_elem_size,
1459 &zi->zi_alloc_size, &zi->zi_collectable, &zi->zi_exhaustible);
1460 zn++;
1461 zi++;
1462
1463 strcpy(zn->zn_name, "pmap_mappings");
1464 mapping_fake_zone_info(&zi->zi_count, &zi->zi_cur_size, &zi->zi_max_size, &zi->zi_elem_size,
1465 &zi->zi_alloc_size, &zi->zi_collectable, &zi->zi_exhaustible);
1466 zn++;
1467 zi++;
1468 #endif
1469 strcpy(zn->zn_name, "kalloc.large");
1470 kalloc_fake_zone_info(&zi->zi_count, &zi->zi_cur_size, &zi->zi_max_size, &zi->zi_elem_size,
1471 &zi->zi_alloc_size, &zi->zi_collectable, &zi->zi_exhaustible);
1472
1473 if (names != *namesp) {
1474 vm_size_t used;
1475 vm_map_copy_t copy;
1476
1477 used = max_zones * sizeof *names;
1478
1479 if (used != names_size)
1480 bzero((char *) (names_addr + used), names_size - used);
1481
1482 kr = vm_map_copyin(ipc_kernel_map, names_addr, names_size,
1483 TRUE, &copy);
1484 assert(kr == KERN_SUCCESS);
1485
1486 *namesp = (zone_name_t *) copy;
1487 }
1488 *namesCntp = max_zones;
1489
1490 if (info != *infop) {
1491 vm_size_t used;
1492 vm_map_copy_t copy;
1493
1494 used = max_zones * sizeof *info;
1495
1496 if (used != info_size)
1497 bzero((char *) (info_addr + used), info_size - used);
1498
1499 kr = vm_map_copyin(ipc_kernel_map, info_addr, info_size,
1500 TRUE, &copy);
1501 assert(kr == KERN_SUCCESS);
1502
1503 *infop = (zone_info_t *) copy;
1504 }
1505 *infoCntp = max_zones;
1506
1507 return KERN_SUCCESS;
1508 }
1509
1510 #if MACH_KDB
1511 #include <ddb/db_command.h>
1512 #include <ddb/db_output.h>
1513 #include <kern/kern_print.h>
1514
1515 const char *zone_labels =
1516 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1517
1518 /* Forwards */
1519 void db_print_zone(
1520 zone_t addr);
1521
1522 #if ZONE_DEBUG
1523 void db_zone_check_active(
1524 zone_t zone);
1525 void db_zone_print_active(
1526 zone_t zone);
1527 #endif /* ZONE_DEBUG */
1528 void db_zone_print_free(
1529 zone_t zone);
1530 void
1531 db_print_zone(
1532 zone_t addr)
1533 {
1534 struct zone zcopy;
1535
1536 zcopy = *addr;
1537
1538 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1539 addr, zcopy.count, zcopy.cur_size,
1540 zcopy.max_size, zcopy.elem_size,
1541 zcopy.alloc_size, zcopy.zone_name);
1542 if (zcopy.exhaustible)
1543 db_printf("H");
1544 if (zcopy.collectable)
1545 db_printf("C");
1546 if (zcopy.expandable)
1547 db_printf("X");
1548 db_printf("\n");
1549 }
1550
1551 /*ARGSUSED*/
1552 void
1553 db_show_one_zone(
1554 db_expr_t addr,
1555 int have_addr,
1556 db_expr_t count,
1557 char * modif)
1558 {
1559 struct zone *z = (zone_t)addr;
1560
1561 if (z == ZONE_NULL || !have_addr){
1562 db_error("No Zone\n");
1563 /*NOTREACHED*/
1564 }
1565
1566 db_printf("%s\n", zone_labels);
1567 db_print_zone(z);
1568 }
1569
1570 /*ARGSUSED*/
1571 void
1572 db_show_all_zones(
1573 db_expr_t addr,
1574 int have_addr,
1575 db_expr_t count,
1576 char * modif)
1577 {
1578 zone_t z;
1579 unsigned total = 0;
1580
1581 /*
1582 * Don't risk hanging by unconditionally locking,
1583 * risk of incoherent data is small (zones aren't freed).
1584 */
1585 have_addr = simple_lock_try(&all_zones_lock);
1586 count = num_zones;
1587 z = first_zone;
1588 if (have_addr) {
1589 simple_unlock(&all_zones_lock);
1590 }
1591
1592 db_printf("%s\n", zone_labels);
1593 for ( ; count > 0; count--) {
1594 if (!z) {
1595 db_error("Mangled Zone List\n");
1596 /*NOTREACHED*/
1597 }
1598 db_print_zone(z);
1599 total += z->cur_size,
1600
1601 have_addr = simple_lock_try(&all_zones_lock);
1602 z = z->next_zone;
1603 if (have_addr) {
1604 simple_unlock(&all_zones_lock);
1605 }
1606 }
1607 db_printf("\nTotal %8x", total);
1608 db_printf("\n\nzone_gc() has reclaimed %d pages\n",
1609 reclaim_page_count);
1610 }
1611
1612 #if ZONE_DEBUG
1613 void
1614 db_zone_check_active(
1615 zone_t zone)
1616 {
1617 int count = 0;
1618 queue_t tmp_elem;
1619
1620 if (!zone_debug_enabled(zone) || !zone_check)
1621 return;
1622 tmp_elem = queue_first(&zone->active_zones);
1623 while (count < zone->count) {
1624 count++;
1625 if (tmp_elem == 0) {
1626 printf("unexpected zero element, zone=0x%x, count=%d\n",
1627 zone, count);
1628 assert(FALSE);
1629 break;
1630 }
1631 if (queue_end(tmp_elem, &zone->active_zones)) {
1632 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1633 zone, count);
1634 assert(FALSE);
1635 break;
1636 }
1637 tmp_elem = queue_next(tmp_elem);
1638 }
1639 if (!queue_end(tmp_elem, &zone->active_zones)) {
1640 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1641 zone, tmp_elem);
1642 assert(FALSE);
1643 }
1644 }
1645
1646 void
1647 db_zone_print_active(
1648 zone_t zone)
1649 {
1650 int count = 0;
1651 queue_t tmp_elem;
1652
1653 if (!zone_debug_enabled(zone)) {
1654 printf("zone 0x%x debug not enabled\n", zone);
1655 return;
1656 }
1657 if (!zone_check) {
1658 printf("zone_check FALSE\n");
1659 return;
1660 }
1661
1662 printf("zone 0x%x, active elements %d\n", zone, zone->count);
1663 printf("active list:\n");
1664 tmp_elem = queue_first(&zone->active_zones);
1665 while (count < zone->count) {
1666 printf(" 0x%x", tmp_elem);
1667 count++;
1668 if ((count % 6) == 0)
1669 printf("\n");
1670 if (tmp_elem == 0) {
1671 printf("\nunexpected zero element, count=%d\n", count);
1672 break;
1673 }
1674 if (queue_end(tmp_elem, &zone->active_zones)) {
1675 printf("\nunexpected queue_end, count=%d\n", count);
1676 break;
1677 }
1678 tmp_elem = queue_next(tmp_elem);
1679 }
1680 if (!queue_end(tmp_elem, &zone->active_zones))
1681 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem);
1682 else
1683 printf("\n");
1684 }
1685 #endif /* ZONE_DEBUG */
1686
1687 void
1688 db_zone_print_free(
1689 zone_t zone)
1690 {
1691 int count = 0;
1692 int freecount;
1693 vm_offset_t elem;
1694
1695 freecount = zone_free_count(zone);
1696 printf("zone 0x%x, free elements %d\n", zone, freecount);
1697 printf("free list:\n");
1698 elem = zone->free_elements;
1699 while (count < freecount) {
1700 printf(" 0x%x", elem);
1701 count++;
1702 if ((count % 6) == 0)
1703 printf("\n");
1704 if (elem == 0) {
1705 printf("\nunexpected zero element, count=%d\n", count);
1706 break;
1707 }
1708 elem = *((vm_offset_t *)elem);
1709 }
1710 if (elem != 0)
1711 printf("\nnot at end of free list, elem=0x%x\n", elem);
1712 else
1713 printf("\n");
1714 }
1715
1716 #endif /* MACH_KDB */
1717
1718
1719 #if ZONE_DEBUG
1720
1721 /* should we care about locks here ? */
1722
1723 #if MACH_KDB
1724 vm_offset_t
1725 next_element(
1726 zone_t z,
1727 vm_offset_t elt)
1728 {
1729 if (!zone_debug_enabled(z))
1730 return(0);
1731 elt -= sizeof(queue_chain_t);
1732 elt = (vm_offset_t) queue_next((queue_t) elt);
1733 if ((queue_t) elt == &z->active_zones)
1734 return(0);
1735 elt += sizeof(queue_chain_t);
1736 return(elt);
1737 }
1738
1739 vm_offset_t
1740 first_element(
1741 zone_t z)
1742 {
1743 vm_offset_t elt;
1744
1745 if (!zone_debug_enabled(z))
1746 return(0);
1747 if (queue_empty(&z->active_zones))
1748 return(0);
1749 elt = (vm_offset_t) queue_first(&z->active_zones);
1750 elt += sizeof(queue_chain_t);
1751 return(elt);
1752 }
1753
1754 /*
1755 * Second arg controls how many zone elements are printed:
1756 * 0 => none
1757 * n, n < 0 => all
1758 * n, n > 0 => last n on active list
1759 */
1760 int
1761 zone_count(
1762 zone_t z,
1763 int tail)
1764 {
1765 vm_offset_t elt;
1766 int count = 0;
1767 boolean_t print = (tail != 0);
1768
1769 if (tail < 0)
1770 tail = z->count;
1771 if (z->count < tail)
1772 tail = 0;
1773 tail = z->count - tail;
1774 for (elt = first_element(z); elt; elt = next_element(z, elt)) {
1775 if (print && tail <= count)
1776 db_printf("%8x\n", elt);
1777 count++;
1778 }
1779 assert(count == z->count);
1780 return(count);
1781 }
1782 #endif /* MACH_KDB */
1783
1784 #define zone_in_use(z) ( z->count || z->free_elements )
1785
1786 void
1787 zone_debug_enable(
1788 zone_t z)
1789 {
1790 if (zone_debug_enabled(z) || zone_in_use(z) ||
1791 z->alloc_size < (z->elem_size + sizeof(queue_chain_t)))
1792 return;
1793 queue_init(&z->active_zones);
1794 z->elem_size += sizeof(queue_chain_t);
1795 }
1796
1797 void
1798 zone_debug_disable(
1799 zone_t z)
1800 {
1801 if (!zone_debug_enabled(z) || zone_in_use(z))
1802 return;
1803 z->elem_size -= sizeof(queue_chain_t);
1804 z->active_zones.next = z->active_zones.prev = 0;
1805 }
1806 #endif /* ZONE_DEBUG */