]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/zalloc.h
xnu-4570.51.1.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 81
5ba3f43e
A
82#if KASAN
83#include <sys/queue.h>
84#include <san/kasan.h>
85#endif
86
316670eb
A
87#if CONFIG_GZALLOC
88typedef struct gzalloc_data {
89 uint32_t gzfc_index;
90 vm_offset_t *gzfc;
91} gzalloc_data_t;
92#endif
1c79356b
A
93
94/*
95 * A zone is a collection of fixed size blocks for which there
96 * is fast allocation/deallocation access. Kernel routines can
97 * use zones to manage data structures dynamically, creating a zone
98 * for each type of data structure to be managed.
99 *
100 */
101
39236c6e
A
102struct zone_free_element;
103struct zone_page_metadata;
104
1c79356b 105struct zone {
39236c6e
A
106 struct zone_free_element *free_elements; /* free elements directly linked */
107 struct {
108 queue_head_t any_free_foreign; /* foreign pages crammed into zone */
109 queue_head_t all_free;
110 queue_head_t intermediate;
111 queue_head_t all_used;
112 } pages; /* list of zone_page_metadata structs, which maintain per-page free element lists */
1c79356b 113 int count; /* Number of elements used now */
39236c6e 114 int countfree; /* Number of free elements */
39037602 115 int count_all_free_pages; /* Number of pages collectable by GC */
39236c6e 116 lck_attr_t lock_attr; /* zone lock attribute */
2d21ac55
A
117 decl_lck_mtx_data(,lock) /* zone lock */
118 lck_mtx_ext_t lock_ext; /* placeholder for indirect mutex */
1c79356b
A
119 vm_size_t cur_size; /* current memory utilization */
120 vm_size_t max_size; /* how large can this zone grow */
121 vm_size_t elem_size; /* size of an element */
122 vm_size_t alloc_size; /* size used for more memory */
39236c6e 123 uint64_t page_count __attribute__((aligned(8))); /* number of pages used by this zone */
6d2010ae 124 uint64_t sum_count; /* count of allocs (life of zone) */
39236c6e
A
125 uint32_t
126 /* boolean_t */ exhaustible :1, /* (F) merely return if empty? */
127 /* boolean_t */ collectable :1, /* (F) garbage collect empty pages */
128 /* boolean_t */ expandable :1, /* (T) expand zone (with message)? */
129 /* boolean_t */ allows_foreign :1, /* (F) allow non-zalloc space */
3e170ce0
A
130 /* boolean_t */ doing_alloc_without_vm_priv:1, /* is zone expanding now via a non-vm_privileged thread? */
131 /* boolean_t */ doing_alloc_with_vm_priv:1, /* is zone expanding now via a vm_privileged thread? */
39236c6e
A
132 /* boolean_t */ waiting :1, /* is thread waiting for expansion? */
133 /* boolean_t */ async_pending :1, /* asynchronous allocation pending? */
134 /* boolean_t */ zleak_on :1, /* Are we collecting allocation information? */
135 /* boolean_t */ caller_acct :1, /* do we account allocation/free to the caller? */
39236c6e
A
136 /* boolean_t */ noencrypt :1,
137 /* boolean_t */ no_callout :1,
138 /* boolean_t */ async_prio_refill :1,
139 /* boolean_t */ gzalloc_exempt :1,
140 /* boolean_t */ alignment_required :1,
39037602
A
141 /* boolean_t */ zone_logging :1, /* Enable zone logging for this zone. */
142 /* boolean_t */ zone_replenishing :1,
5ba3f43e
A
143 /* boolean_t */ kasan_quarantine :1,
144 /* boolean_t */ tags :1,
145 /* boolean_t */ tags_inline :1,
146 /* future */ tag_zone_index :6,
147 /* boolean_t */ zone_valid :1,
148 /* future */ _reserved :5;
39236c6e 149
6d2010ae 150 int index; /* index into zone_info arrays for this zone */
91447636 151 const char *zone_name; /* a name for the zone */
6d2010ae
A
152
153#if CONFIG_ZLEAKS
316670eb 154 uint32_t zleak_capture; /* per-zone counter for capturing every N allocations */
6d2010ae 155#endif /* CONFIG_ZLEAKS */
39236c6e 156 uint32_t zp_count; /* counter for poisoning every N frees */
7ddcb079
A
157 vm_size_t prio_refill_watermark;
158 thread_t zone_replenish_thread;
316670eb
A
159#if CONFIG_GZALLOC
160 gzalloc_data_t gz;
161#endif /* CONFIG_GZALLOC */
39037602 162
5ba3f43e
A
163#if KASAN_ZALLOC
164 vm_size_t kasan_redzone;
165#endif
166
39037602 167 btlog_t *zlog_btlog; /* zone logging structure to hold stacks and element references to those stacks. */
1c79356b
A
168};
169
6d2010ae
A
170/*
171 * structure for tracking zone usage
172 * Used either one per task/thread for all zones or <per-task,per-zone>.
173 */
174typedef struct zinfo_usage_store_t {
175 /* These fields may be updated atomically, and so must be 8 byte aligned */
176 uint64_t alloc __attribute__((aligned(8))); /* allocation counter */
177 uint64_t free __attribute__((aligned(8))); /* free counter */
178} zinfo_usage_store_t;
6d2010ae 179
5ba3f43e
A
180/*
181 * For sysctl kern.zones_collectable_bytes used by memory_maintenance to check if a
182 * userspace reboot is needed. The only other way to query for this information
183 * is via mach_memory_info() which is unavailable on release kernels.
184 */
185extern uint64_t get_zones_collectable_bytes(void);
186
187/*
188 * zone_gc also checks if the zone_map is getting close to full and triggers jetsams if needed, provided
189 * consider_jetsams is set to TRUE. To avoid deadlocks, we only pass a value of TRUE from within the
190 * vm_pageout_garbage_collect thread.
191 */
192extern void zone_gc(boolean_t consider_jetsams);
193extern void consider_zone_gc(boolean_t consider_jetsams);
194extern void drop_free_elements(zone_t z);
195
196/* Debug logging for zone-map-exhaustion jetsams. */
197extern void get_zone_map_size(uint64_t *current_size, uint64_t *capacity);
198extern void get_largest_zone_info(char *zone_name, size_t zone_name_len, uint64_t *zone_size);
1c79356b
A
199
200/* Bootstrap zone module (create zone zone) */
39236c6e 201extern void zone_bootstrap(void);
1c79356b
A
202
203/* Init zone module */
91447636 204extern void zone_init(
39236c6e 205 vm_size_t map_size);
1c79356b 206
91447636 207/* Stack use statistics */
6d2010ae 208extern void stack_fake_zone_init(int zone_index);
91447636
A
209extern void stack_fake_zone_info(
210 int *count,
211 vm_size_t *cur_size,
212 vm_size_t *max_size,
213 vm_size_t *elem_size,
214 vm_size_t *alloc_size,
6d2010ae 215 uint64_t *sum_size,
91447636 216 int *collectable,
6d2010ae
A
217 int *exhaustable,
218 int *caller_acct);
1c79356b 219
91447636 220#if ZONE_DEBUG
1c79356b 221
91447636
A
222extern void zone_debug_enable(
223 zone_t z);
224
225extern void zone_debug_disable(
226 zone_t z);
227
316670eb
A
228#define zone_debug_enabled(z) z->active_zones.next
229#define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
230#define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
91447636
A
231#endif /* ZONE_DEBUG */
232
39037602
A
233extern unsigned int num_zones;
234extern struct zone zone_array[];
235
5ba3f43e
A
236/* zindex and page_count must pack into 16 bits
237 * update tools/lldbmacros/memory.py:GetRealMetadata
238 * when these values change */
239
240#define ZINDEX_BITS (10U)
241#define PAGECOUNT_BITS (16U - ZINDEX_BITS)
242#define MULTIPAGE_METADATA_MAGIC ((1UL << ZINDEX_BITS) - 1)
243#define ZONE_CHUNK_MAXPAGES ((1UL << PAGECOUNT_BITS) - 1)
244
245/*
246 * The max # of elements in a chunk should fit into zone_page_metadata.free_count (uint16_t).
247 * Update this if the type of free_count changes.
248 */
249#define ZONE_CHUNK_MAXELEMENTS (UINT16_MAX)
250
91447636
A
251#endif /* MACH_KERNEL_PRIVATE */
252
253__BEGIN_DECLS
254
91447636 255
813fb2f6
A
256/* Item definitions for zalloc/zinit/zone_change */
257#define Z_EXHAUST 1 /* Make zone exhaustible */
258#define Z_COLLECT 2 /* Make zone collectable */
259#define Z_EXPAND 3 /* Make zone expandable */
260#define Z_FOREIGN 4 /* Allow collectable zone to contain foreign elements */
261#define Z_CALLERACCT 5 /* Account alloc/free against the caller */
262#define Z_NOENCRYPT 6 /* Don't encrypt zone during hibernation */
263#define Z_NOCALLOUT 7 /* Don't asynchronously replenish the zone via callouts */
264#define Z_ALIGNMENT_REQUIRED 8
265#define Z_GZALLOC_EXEMPT 9 /* Not tracked in guard allocation mode */
5ba3f43e
A
266#define Z_KASAN_QUARANTINE 10 /* Allow zone elements to be quarantined on free */
267#ifdef XNU_KERNEL_PRIVATE
268#define Z_TAGS_ENABLED 11 /* Store tags */
269#endif /* XNU_KERNEL_PRIVATE */
1c79356b 270
813fb2f6 271#ifdef XNU_KERNEL_PRIVATE
91447636 272
813fb2f6
A
273extern vm_offset_t zone_map_min_address;
274extern vm_offset_t zone_map_max_address;
91447636
A
275
276
fe8ab488
A
277/* Non-waiting for memory version of zalloc */
278extern void * zalloc_nopagewait(
279 zone_t zone);
280
fe8ab488 281/* selective version of zalloc */
91447636
A
282extern void * zalloc_canblock(
283 zone_t zone,
284 boolean_t canblock);
285
5ba3f43e
A
286/* selective version of zalloc */
287extern void * zalloc_canblock_tag(
288 zone_t zone,
289 boolean_t canblock,
290 vm_size_t reqsize,
291 vm_tag_t tag);
292
91447636
A
293/* Get from zone free list */
294extern void * zget(
295 zone_t zone);
1c79356b
A
296
297/* Fill zone with memory */
298extern void zcram(
91447636 299 zone_t zone,
7ddcb079 300 vm_offset_t newmem,
91447636 301 vm_size_t size);
1c79356b
A
302
303/* Initially fill zone with specified number of elements */
304extern int zfill(
91447636
A
305 zone_t zone,
306 int nelem);
307
7ddcb079 308extern void zone_prio_refill_configure(zone_t, vm_size_t);
813fb2f6
A
309
310/* See above/top of file. Z_* definitions moved so they would be usable by kexts */
39236c6e 311
91447636
A
312/* Preallocate space for zone from zone map */
313extern void zprealloc(
314 zone_t zone,
315 vm_size_t size);
9bccf70c 316
91447636
A
317extern integer_t zone_free_count(
318 zone_t zone);
9bccf70c 319
39037602
A
320extern vm_size_t zone_element_size(
321 void *addr,
322 zone_t *z);
323
6d2010ae
A
324/*
325 * MAX_ZTRACE_DEPTH configures how deep of a stack trace is taken on each zalloc in the zone of interest. 15
326 * levels is usually enough to get past all the layers of code in kalloc and IOKit and see who the actual
327 * caller is up above these lower levels.
328 *
329 * This is used both for the zone leak detector and the zone corruption log.
330 */
331
332#define MAX_ZTRACE_DEPTH 15
333
334/*
335 * Structure for keeping track of a backtrace, used for leak detection.
336 * This is in the .h file because it is used during panic, see kern/debug.c
337 * A non-zero size indicates that the trace is in use.
338 */
339struct ztrace {
340 vm_size_t zt_size; /* How much memory are all the allocations referring to this trace taking up? */
341 uint32_t zt_depth; /* depth of stack (0 to MAX_ZTRACE_DEPTH) */
342 void* zt_stack[MAX_ZTRACE_DEPTH]; /* series of return addresses from OSBacktrace */
343 uint32_t zt_collisions; /* How many times did a different stack land here while it was occupied? */
344 uint32_t zt_hit_count; /* for determining effectiveness of hash function */
345};
346
347#if CONFIG_ZLEAKS
348
349/* support for the kern.zleak.* sysctls */
350
351extern kern_return_t zleak_activate(void);
352extern vm_size_t zleak_max_zonemap_size;
353extern vm_size_t zleak_global_tracking_threshold;
354extern vm_size_t zleak_per_zone_tracking_threshold;
355
356extern int get_zleak_state(void);
357
358#endif /* CONFIG_ZLEAKS */
359
5ba3f43e
A
360#ifndef VM_MAX_TAG_ZONES
361#error MAX_TAG_ZONES
362#endif
363
364#if VM_MAX_TAG_ZONES
365
366extern boolean_t zone_tagging_on;
367extern uint32_t zone_index_from_tag_index(uint32_t tag_zone_index, vm_size_t * elem_size);
368
369#endif /* VM_MAX_TAG_ZONES */
370
6d2010ae 371/* These functions used for leak detection both in zalloc.c and mbuf.c */
316670eb
A
372extern uintptr_t hash_mix(uintptr_t);
373extern uint32_t hashbacktrace(uintptr_t *, uint32_t, uint32_t);
374extern uint32_t hashaddr(uintptr_t, uint32_t);
375
376#define lock_zone(zone) \
377MACRO_BEGIN \
5ba3f43e 378 lck_mtx_lock_spin_always(&(zone)->lock); \
316670eb
A
379MACRO_END
380
381#define unlock_zone(zone) \
382MACRO_BEGIN \
383 lck_mtx_unlock(&(zone)->lock); \
384MACRO_END
385
386#if CONFIG_GZALLOC
387void gzalloc_init(vm_size_t);
388void gzalloc_zone_init(zone_t);
389void gzalloc_configure(void);
390void gzalloc_reconfigure(zone_t);
5ba3f43e 391void gzalloc_empty_free_cache(zone_t);
316670eb
A
392boolean_t gzalloc_enabled(void);
393
394vm_offset_t gzalloc_alloc(zone_t, boolean_t);
395boolean_t gzalloc_free(zone_t, void *);
39037602 396boolean_t gzalloc_element_size(void *, zone_t *, vm_size_t *);
316670eb 397#endif /* CONFIG_GZALLOC */
6d2010ae 398
39037602
A
399/* Callbacks for btlog lock/unlock */
400void zlog_btlog_lock(__unused void *);
401void zlog_btlog_unlock(__unused void *);
402
5ba3f43e
A
403#ifdef MACH_KERNEL_PRIVATE
404#define MAX_ZONE_NAME 32 /* max length of a zone name we can take from the boot-args */
405int track_this_zone(const char *zonename, const char *logname);
406#endif
407
408#if DEBUG || DEVELOPMENT
409extern boolean_t run_zone_test(void);
410extern vm_size_t zone_element_info(void *addr, vm_tag_t * ptag);
411#endif /* DEBUG || DEVELOPMENT */
412
91447636 413#endif /* XNU_KERNEL_PRIVATE */
9bccf70c 414
813fb2f6
A
415/* Allocate from zone */
416extern void * zalloc(
417 zone_t zone);
418
5ba3f43e
A
419/* Non-blocking version of zalloc */
420extern void * zalloc_noblock(
421 zone_t zone);
422
813fb2f6
A
423/* Free zone element */
424extern void zfree(
425 zone_t zone,
426 void *elem);
427
428/* Create zone */
429extern zone_t zinit(
430 vm_size_t size, /* the size of an element */
431 vm_size_t maxmem, /* maximum memory to use */
432 vm_size_t alloc, /* allocation size */
433 const char *name); /* a name for the zone */
434
435/* Change zone parameters */
436extern void zone_change(
437 zone_t zone,
438 unsigned int item,
439 boolean_t value);
440
5ba3f43e
A
441/* Destroy the zone */
442extern void zdestroy(
443 zone_t zone);
813fb2f6 444
91447636 445__END_DECLS
1c79356b
A
446
447#endif /* _KERN_ZALLOC_H_ */
91447636
A
448
449#endif /* KERNEL_PRIVATE */