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