]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/zalloc.h
xnu-3789.60.24.tar.gz
[apple/xnu.git] / osfmk / kern / zalloc.h
CommitLineData
1c79356b 1/*
fe8ab488 2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
59 * File: zalloc.h
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1985
62 *
63 */
64
91447636
A
65#ifdef KERNEL_PRIVATE
66
1c79356b
A
67#ifndef _KERN_ZALLOC_H_
68#define _KERN_ZALLOC_H_
69
70#include <mach/machine/vm_types.h>
71#include <kern/kern_types.h>
91447636 72#include <sys/cdefs.h>
9bccf70c
A
73
74#ifdef MACH_KERNEL_PRIVATE
75
1c79356b 76#include <zone_debug.h>
2d21ac55 77#include <kern/locks.h>
1c79356b 78#include <kern/queue.h>
316670eb 79#include <kern/thread_call.h>
39037602 80#include <kern/btlog.h>
316670eb
A
81
82#if CONFIG_GZALLOC
83typedef struct gzalloc_data {
84 uint32_t gzfc_index;
85 vm_offset_t *gzfc;
86} gzalloc_data_t;
87#endif
1c79356b
A
88
89/*
90 * A zone is a collection of fixed size blocks for which there
91 * is fast allocation/deallocation access. Kernel routines can
92 * use zones to manage data structures dynamically, creating a zone
93 * for each type of data structure to be managed.
94 *
95 */
96
39236c6e
A
97struct zone_free_element;
98struct zone_page_metadata;
99
1c79356b 100struct zone {
39236c6e
A
101 struct zone_free_element *free_elements; /* free elements directly linked */
102 struct {
103 queue_head_t any_free_foreign; /* foreign pages crammed into zone */
104 queue_head_t all_free;
105 queue_head_t intermediate;
106 queue_head_t all_used;
107 } pages; /* list of zone_page_metadata structs, which maintain per-page free element lists */
1c79356b 108 int count; /* Number of elements used now */
39236c6e 109 int countfree; /* Number of free elements */
39037602 110 int count_all_free_pages; /* Number of pages collectable by GC */
39236c6e 111 lck_attr_t lock_attr; /* zone lock attribute */
2d21ac55
A
112 decl_lck_mtx_data(,lock) /* zone lock */
113 lck_mtx_ext_t lock_ext; /* placeholder for indirect mutex */
1c79356b
A
114 vm_size_t cur_size; /* current memory utilization */
115 vm_size_t max_size; /* how large can this zone grow */
116 vm_size_t elem_size; /* size of an element */
117 vm_size_t alloc_size; /* size used for more memory */
39236c6e 118 uint64_t page_count __attribute__((aligned(8))); /* number of pages used by this zone */
6d2010ae 119 uint64_t sum_count; /* count of allocs (life of zone) */
39236c6e
A
120 uint32_t
121 /* boolean_t */ exhaustible :1, /* (F) merely return if empty? */
122 /* boolean_t */ collectable :1, /* (F) garbage collect empty pages */
123 /* boolean_t */ expandable :1, /* (T) expand zone (with message)? */
124 /* boolean_t */ allows_foreign :1, /* (F) allow non-zalloc space */
3e170ce0
A
125 /* boolean_t */ doing_alloc_without_vm_priv:1, /* is zone expanding now via a non-vm_privileged thread? */
126 /* boolean_t */ doing_alloc_with_vm_priv:1, /* is zone expanding now via a vm_privileged thread? */
39236c6e
A
127 /* boolean_t */ waiting :1, /* is thread waiting for expansion? */
128 /* boolean_t */ async_pending :1, /* asynchronous allocation pending? */
129 /* boolean_t */ zleak_on :1, /* Are we collecting allocation information? */
130 /* boolean_t */ caller_acct :1, /* do we account allocation/free to the caller? */
39236c6e
A
131 /* boolean_t */ noencrypt :1,
132 /* boolean_t */ no_callout :1,
133 /* boolean_t */ async_prio_refill :1,
134 /* boolean_t */ gzalloc_exempt :1,
135 /* boolean_t */ alignment_required :1,
39037602
A
136 /* boolean_t */ zone_logging :1, /* Enable zone logging for this zone. */
137 /* boolean_t */ zone_replenishing :1,
3e170ce0 138 /* future */ _reserved :15;
39236c6e 139
6d2010ae 140 int index; /* index into zone_info arrays for this zone */
91447636 141 const char *zone_name; /* a name for the zone */
6d2010ae
A
142
143#if CONFIG_ZLEAKS
316670eb 144 uint32_t zleak_capture; /* per-zone counter for capturing every N allocations */
6d2010ae 145#endif /* CONFIG_ZLEAKS */
39236c6e 146 uint32_t zp_count; /* counter for poisoning every N frees */
7ddcb079
A
147 vm_size_t prio_refill_watermark;
148 thread_t zone_replenish_thread;
316670eb
A
149#if CONFIG_GZALLOC
150 gzalloc_data_t gz;
151#endif /* CONFIG_GZALLOC */
39037602
A
152
153 btlog_t *zlog_btlog; /* zone logging structure to hold stacks and element references to those stacks. */
1c79356b
A
154};
155
6d2010ae
A
156/*
157 * structure for tracking zone usage
158 * Used either one per task/thread for all zones or <per-task,per-zone>.
159 */
160typedef struct zinfo_usage_store_t {
161 /* These fields may be updated atomically, and so must be 8 byte aligned */
162 uint64_t alloc __attribute__((aligned(8))); /* allocation counter */
163 uint64_t free __attribute__((aligned(8))); /* free counter */
164} zinfo_usage_store_t;
6d2010ae 165
39037602
A
166extern void zone_gc(void);
167extern void consider_zone_gc(void);
1c79356b
A
168
169/* Bootstrap zone module (create zone zone) */
39236c6e 170extern void zone_bootstrap(void);
1c79356b
A
171
172/* Init zone module */
91447636 173extern void zone_init(
39236c6e 174 vm_size_t map_size);
1c79356b 175
91447636 176/* Stack use statistics */
6d2010ae 177extern void stack_fake_zone_init(int zone_index);
91447636
A
178extern void stack_fake_zone_info(
179 int *count,
180 vm_size_t *cur_size,
181 vm_size_t *max_size,
182 vm_size_t *elem_size,
183 vm_size_t *alloc_size,
6d2010ae 184 uint64_t *sum_size,
91447636 185 int *collectable,
6d2010ae
A
186 int *exhaustable,
187 int *caller_acct);
1c79356b 188
91447636 189#if ZONE_DEBUG
1c79356b 190
91447636
A
191extern void zone_debug_enable(
192 zone_t z);
193
194extern void zone_debug_disable(
195 zone_t z);
196
316670eb
A
197#define zone_debug_enabled(z) z->active_zones.next
198#define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
199#define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
91447636
A
200#endif /* ZONE_DEBUG */
201
39037602
A
202extern unsigned int num_zones;
203extern struct zone zone_array[];
204
91447636
A
205#endif /* MACH_KERNEL_PRIVATE */
206
207__BEGIN_DECLS
208
91447636 209
813fb2f6
A
210/* Item definitions for zalloc/zinit/zone_change */
211#define Z_EXHAUST 1 /* Make zone exhaustible */
212#define Z_COLLECT 2 /* Make zone collectable */
213#define Z_EXPAND 3 /* Make zone expandable */
214#define Z_FOREIGN 4 /* Allow collectable zone to contain foreign elements */
215#define Z_CALLERACCT 5 /* Account alloc/free against the caller */
216#define Z_NOENCRYPT 6 /* Don't encrypt zone during hibernation */
217#define Z_NOCALLOUT 7 /* Don't asynchronously replenish the zone via callouts */
218#define Z_ALIGNMENT_REQUIRED 8
219#define Z_GZALLOC_EXEMPT 9 /* Not tracked in guard allocation mode */
fe8ab488
A
220
221
1c79356b 222
813fb2f6 223#ifdef XNU_KERNEL_PRIVATE
91447636 224
813fb2f6
A
225extern vm_offset_t zone_map_min_address;
226extern vm_offset_t zone_map_max_address;
91447636
A
227
228
fe8ab488
A
229/* Non-waiting for memory version of zalloc */
230extern void * zalloc_nopagewait(
231 zone_t zone);
232
91447636
A
233/* Non-blocking version of zalloc */
234extern void * zalloc_noblock(
235 zone_t zone);
236
fe8ab488 237/* selective version of zalloc */
91447636
A
238extern void * zalloc_canblock(
239 zone_t zone,
240 boolean_t canblock);
241
242/* Get from zone free list */
243extern void * zget(
244 zone_t zone);
1c79356b
A
245
246/* Fill zone with memory */
247extern void zcram(
91447636 248 zone_t zone,
7ddcb079 249 vm_offset_t newmem,
91447636 250 vm_size_t size);
1c79356b
A
251
252/* Initially fill zone with specified number of elements */
253extern int zfill(
91447636
A
254 zone_t zone,
255 int nelem);
256
7ddcb079 257extern void zone_prio_refill_configure(zone_t, vm_size_t);
813fb2f6
A
258
259/* See above/top of file. Z_* definitions moved so they would be usable by kexts */
39236c6e 260
91447636
A
261/* Preallocate space for zone from zone map */
262extern void zprealloc(
263 zone_t zone,
264 vm_size_t size);
9bccf70c 265
91447636
A
266extern integer_t zone_free_count(
267 zone_t zone);
9bccf70c 268
39037602
A
269extern vm_size_t zone_element_size(
270 void *addr,
271 zone_t *z);
272
6d2010ae
A
273/*
274 * MAX_ZTRACE_DEPTH configures how deep of a stack trace is taken on each zalloc in the zone of interest. 15
275 * levels is usually enough to get past all the layers of code in kalloc and IOKit and see who the actual
276 * caller is up above these lower levels.
277 *
278 * This is used both for the zone leak detector and the zone corruption log.
279 */
280
281#define MAX_ZTRACE_DEPTH 15
282
283/*
284 * Structure for keeping track of a backtrace, used for leak detection.
285 * This is in the .h file because it is used during panic, see kern/debug.c
286 * A non-zero size indicates that the trace is in use.
287 */
288struct ztrace {
289 vm_size_t zt_size; /* How much memory are all the allocations referring to this trace taking up? */
290 uint32_t zt_depth; /* depth of stack (0 to MAX_ZTRACE_DEPTH) */
291 void* zt_stack[MAX_ZTRACE_DEPTH]; /* series of return addresses from OSBacktrace */
292 uint32_t zt_collisions; /* How many times did a different stack land here while it was occupied? */
293 uint32_t zt_hit_count; /* for determining effectiveness of hash function */
294};
295
296#if CONFIG_ZLEAKS
297
298/* support for the kern.zleak.* sysctls */
299
300extern kern_return_t zleak_activate(void);
301extern vm_size_t zleak_max_zonemap_size;
302extern vm_size_t zleak_global_tracking_threshold;
303extern vm_size_t zleak_per_zone_tracking_threshold;
304
305extern int get_zleak_state(void);
306
307#endif /* CONFIG_ZLEAKS */
308
309/* These functions used for leak detection both in zalloc.c and mbuf.c */
316670eb
A
310extern uintptr_t hash_mix(uintptr_t);
311extern uint32_t hashbacktrace(uintptr_t *, uint32_t, uint32_t);
312extern uint32_t hashaddr(uintptr_t, uint32_t);
313
314#define lock_zone(zone) \
315MACRO_BEGIN \
316 lck_mtx_lock_spin(&(zone)->lock); \
317MACRO_END
318
319#define unlock_zone(zone) \
320MACRO_BEGIN \
321 lck_mtx_unlock(&(zone)->lock); \
322MACRO_END
323
324#if CONFIG_GZALLOC
325void gzalloc_init(vm_size_t);
326void gzalloc_zone_init(zone_t);
327void gzalloc_configure(void);
328void gzalloc_reconfigure(zone_t);
329boolean_t gzalloc_enabled(void);
330
331vm_offset_t gzalloc_alloc(zone_t, boolean_t);
332boolean_t gzalloc_free(zone_t, void *);
39037602 333boolean_t gzalloc_element_size(void *, zone_t *, vm_size_t *);
316670eb 334#endif /* CONFIG_GZALLOC */
6d2010ae 335
39037602
A
336/* Callbacks for btlog lock/unlock */
337void zlog_btlog_lock(__unused void *);
338void zlog_btlog_unlock(__unused void *);
339
91447636 340#endif /* XNU_KERNEL_PRIVATE */
9bccf70c 341
813fb2f6
A
342/* Allocate from zone */
343extern void * zalloc(
344 zone_t zone);
345
346/* Free zone element */
347extern void zfree(
348 zone_t zone,
349 void *elem);
350
351/* Create zone */
352extern zone_t zinit(
353 vm_size_t size, /* the size of an element */
354 vm_size_t maxmem, /* maximum memory to use */
355 vm_size_t alloc, /* allocation size */
356 const char *name); /* a name for the zone */
357
358/* Change zone parameters */
359extern void zone_change(
360 zone_t zone,
361 unsigned int item,
362 boolean_t value);
363
364
91447636 365__END_DECLS
1c79356b
A
366
367#endif /* _KERN_ZALLOC_H_ */
91447636
A
368
369#endif /* KERNEL_PRIVATE */