xref: /titanic_50/usr/src/lib/libumem/common/umem_impl.h (revision 4f364e7c95ee7fd9d5bbeddc1940e92405bb0e72)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*4f364e7cSRobert Mustacchi /*
28*4f364e7cSRobert Mustacchi  * Copyright (c) 2012 Joyent, Inc.  All rights reserved.
29*4f364e7cSRobert Mustacchi  */
30*4f364e7cSRobert Mustacchi 
317c478bd9Sstevel@tonic-gate #ifndef _UMEM_IMPL_H
327c478bd9Sstevel@tonic-gate #define	_UMEM_IMPL_H
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <umem.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/time.h>
387c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
397c478bd9Sstevel@tonic-gate #include <thread.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
427c478bd9Sstevel@tonic-gate extern "C" {
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * umem memory allocator: implementation-private data structures
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Internal flags for umem_cache_create
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate #define	UMC_QCACHE	0x00100000
537c478bd9Sstevel@tonic-gate #define	UMC_INTERNAL	0x80000000
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Cache flags
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate #define	UMF_AUDIT	0x00000001	/* transaction auditing */
597c478bd9Sstevel@tonic-gate #define	UMF_DEADBEEF	0x00000002	/* deadbeef checking */
607c478bd9Sstevel@tonic-gate #define	UMF_REDZONE	0x00000004	/* redzone checking */
617c478bd9Sstevel@tonic-gate #define	UMF_CONTENTS	0x00000008	/* freed-buffer content logging */
627c478bd9Sstevel@tonic-gate #define	UMF_CHECKSIGNAL	0x00000010	/* abort when in signal context */
637c478bd9Sstevel@tonic-gate #define	UMF_NOMAGAZINE	0x00000020	/* disable per-cpu magazines */
647c478bd9Sstevel@tonic-gate #define	UMF_FIREWALL	0x00000040	/* put all bufs before unmapped pages */
657c478bd9Sstevel@tonic-gate #define	UMF_LITE	0x00000100	/* lightweight debugging */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	UMF_HASH	0x00000200	/* cache has hash table */
687c478bd9Sstevel@tonic-gate #define	UMF_RANDOMIZE	0x00000400	/* randomize other umem_flags */
69*4f364e7cSRobert Mustacchi #define	UMF_PTC		0x00000800	/* cache has per-thread caching */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	UMF_BUFTAG	(UMF_DEADBEEF | UMF_REDZONE)
727c478bd9Sstevel@tonic-gate #define	UMF_TOUCH	(UMF_BUFTAG | UMF_LITE | UMF_CONTENTS)
737c478bd9Sstevel@tonic-gate #define	UMF_RANDOM	(UMF_TOUCH | UMF_AUDIT | UMF_NOMAGAZINE)
747c478bd9Sstevel@tonic-gate #define	UMF_DEBUG	(UMF_RANDOM | UMF_FIREWALL)
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	UMEM_STACK_DEPTH	umem_stack_depth
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	UMEM_FREE_PATTERN		0xdeadbeefdeadbeefULL
797c478bd9Sstevel@tonic-gate #define	UMEM_UNINITIALIZED_PATTERN	0xbaddcafebaddcafeULL
807c478bd9Sstevel@tonic-gate #define	UMEM_REDZONE_PATTERN		0xfeedfacefeedfaceULL
817c478bd9Sstevel@tonic-gate #define	UMEM_REDZONE_BYTE		0xbb
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	UMEM_FATAL_FLAGS	(UMEM_NOFAIL)
847c478bd9Sstevel@tonic-gate #define	UMEM_SLEEP_FLAGS	(0)
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Redzone size encodings for umem_alloc() / umem_free().  We encode the
887c478bd9Sstevel@tonic-gate  * allocation size, rather than storing it directly, so that umem_free()
897c478bd9Sstevel@tonic-gate  * can distinguish frees of the wrong size from redzone violations.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate #define	UMEM_SIZE_ENCODE(x)	(251 * (x) + 1)
927c478bd9Sstevel@tonic-gate #define	UMEM_SIZE_DECODE(x)	((x) / 251)
937c478bd9Sstevel@tonic-gate #define	UMEM_SIZE_VALID(x)	((x) % 251 == 1)
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * The bufctl (buffer control) structure keeps some minimal information
977c478bd9Sstevel@tonic-gate  * about each buffer: its address, its slab, and its current linkage,
987c478bd9Sstevel@tonic-gate  * which is either on the slab's freelist (if the buffer is free), or
997c478bd9Sstevel@tonic-gate  * on the cache's buf-to-bufctl hash table (if the buffer is allocated).
1007c478bd9Sstevel@tonic-gate  * In the case of non-hashed, or "raw", caches (the common case), only
1017c478bd9Sstevel@tonic-gate  * the freelist linkage is necessary: the buffer address is at a fixed
1027c478bd9Sstevel@tonic-gate  * offset from the bufctl address, and the slab is at the end of the page.
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  * NOTE: bc_next must be the first field; raw buffers have linkage only.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate typedef struct umem_bufctl {
1077c478bd9Sstevel@tonic-gate 	struct umem_bufctl	*bc_next;	/* next bufctl struct */
1087c478bd9Sstevel@tonic-gate 	void			*bc_addr;	/* address of buffer */
1097c478bd9Sstevel@tonic-gate 	struct umem_slab	*bc_slab;	/* controlling slab */
1107c478bd9Sstevel@tonic-gate } umem_bufctl_t;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * The UMF_AUDIT version of the bufctl structure.  The beginning of this
1147c478bd9Sstevel@tonic-gate  * structure must be identical to the normal bufctl structure so that
1157c478bd9Sstevel@tonic-gate  * pointers are interchangeable.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL_AUDIT_SIZE_DEPTH(frames) \
1197c478bd9Sstevel@tonic-gate 	((size_t)(&((umem_bufctl_audit_t *)0)->bc_stack[frames]))
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * umem_bufctl_audits must be allocated from a UMC_NOHASH cache, so we
1237c478bd9Sstevel@tonic-gate  * require that 2 of them, plus 2 buftags, plus a umem_slab_t, all fit on
1247c478bd9Sstevel@tonic-gate  * a single page.
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  * For ILP32, this is about 1000 frames.
1277c478bd9Sstevel@tonic-gate  * For LP64, this is about 490 frames.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL_AUDIT_ALIGN	32
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL_AUDIT_MAX_SIZE					\
1337c478bd9Sstevel@tonic-gate 	(P2ALIGN((PAGESIZE - sizeof (umem_slab_t))/2 -			\
1347c478bd9Sstevel@tonic-gate 	    sizeof (umem_buftag_t), UMEM_BUFCTL_AUDIT_ALIGN))
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	UMEM_MAX_STACK_DEPTH						\
1377c478bd9Sstevel@tonic-gate 	((UMEM_BUFCTL_AUDIT_MAX_SIZE -					\
1387c478bd9Sstevel@tonic-gate 	    UMEM_BUFCTL_AUDIT_SIZE_DEPTH(0)) / sizeof (uintptr_t))
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate typedef struct umem_bufctl_audit {
1417c478bd9Sstevel@tonic-gate 	struct umem_bufctl	*bc_next;	/* next bufctl struct */
1427c478bd9Sstevel@tonic-gate 	void			*bc_addr;	/* address of buffer */
1437c478bd9Sstevel@tonic-gate 	struct umem_slab	*bc_slab;	/* controlling slab */
1447c478bd9Sstevel@tonic-gate 	umem_cache_t		*bc_cache;	/* controlling cache */
1457c478bd9Sstevel@tonic-gate 	hrtime_t		bc_timestamp;	/* transaction time */
1467c478bd9Sstevel@tonic-gate 	thread_t		bc_thread;	/* thread doing transaction */
1477c478bd9Sstevel@tonic-gate 	struct umem_bufctl	*bc_lastlog;	/* last log entry */
1487c478bd9Sstevel@tonic-gate 	void			*bc_contents;	/* contents at last free */
1497c478bd9Sstevel@tonic-gate 	int			bc_depth;	/* stack depth */
1507c478bd9Sstevel@tonic-gate 	uintptr_t		bc_stack[1];	/* pc stack */
1517c478bd9Sstevel@tonic-gate } umem_bufctl_audit_t;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #define	UMEM_LOCAL_BUFCTL_AUDIT(bcpp)					\
1547c478bd9Sstevel@tonic-gate 		*(bcpp) = (umem_bufctl_audit_t *)			\
1557c478bd9Sstevel@tonic-gate 		    alloca(UMEM_BUFCTL_AUDIT_SIZE)
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL_AUDIT_SIZE						\
1587c478bd9Sstevel@tonic-gate 	UMEM_BUFCTL_AUDIT_SIZE_DEPTH(UMEM_STACK_DEPTH)
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * A umem_buftag structure is appended to each buffer whenever any of the
1627c478bd9Sstevel@tonic-gate  * UMF_BUFTAG flags (UMF_DEADBEEF, UMF_REDZONE, UMF_VERIFY) are set.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate typedef struct umem_buftag {
1657c478bd9Sstevel@tonic-gate 	uint64_t		bt_redzone;	/* 64-bit redzone pattern */
1667c478bd9Sstevel@tonic-gate 	umem_bufctl_t		*bt_bufctl;	/* bufctl */
1677c478bd9Sstevel@tonic-gate 	intptr_t		bt_bxstat;	/* bufctl ^ (alloc/free) */
1687c478bd9Sstevel@tonic-gate } umem_buftag_t;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate #define	UMEM_BUFTAG(cp, buf)		\
1717c478bd9Sstevel@tonic-gate 	((umem_buftag_t *)((char *)(buf) + (cp)->cache_buftag))
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL(cp, buf)		\
1747c478bd9Sstevel@tonic-gate 	((umem_bufctl_t *)((char *)(buf) + (cp)->cache_bufctl))
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #define	UMEM_BUF(cp, bcp)		\
1777c478bd9Sstevel@tonic-gate 	((void *)((char *)(bcp) - (cp)->cache_bufctl))
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #define	UMEM_SLAB(cp, buf)		\
1807c478bd9Sstevel@tonic-gate 	((umem_slab_t *)P2END((uintptr_t)(buf), (cp)->cache_slabsize) - 1)
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #define	UMEM_CPU_CACHE(cp, cpu)		\
1837c478bd9Sstevel@tonic-gate 	(umem_cpu_cache_t *)((char *)cp + cpu->cpu_cache_offset)
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate #define	UMEM_MAGAZINE_VALID(cp, mp)	\
1867c478bd9Sstevel@tonic-gate 	(((umem_slab_t *)P2END((uintptr_t)(mp), PAGESIZE) - 1)->slab_cache == \
1877c478bd9Sstevel@tonic-gate 	    (cp)->cache_magtype->mt_cache)
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate #define	UMEM_SLAB_MEMBER(sp, buf)	\
1907c478bd9Sstevel@tonic-gate 	((size_t)(buf) - (size_t)(sp)->slab_base < \
1917c478bd9Sstevel@tonic-gate 	    (sp)->slab_cache->cache_slabsize)
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate #define	UMEM_BUFTAG_ALLOC	0xa110c8edUL
1947c478bd9Sstevel@tonic-gate #define	UMEM_BUFTAG_FREE	0xf4eef4eeUL
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate typedef struct umem_slab {
1977c478bd9Sstevel@tonic-gate 	struct umem_cache	*slab_cache;	/* controlling cache */
1987c478bd9Sstevel@tonic-gate 	void			*slab_base;	/* base of allocated memory */
1997c478bd9Sstevel@tonic-gate 	struct umem_slab	*slab_next;	/* next slab on freelist */
2007c478bd9Sstevel@tonic-gate 	struct umem_slab	*slab_prev;	/* prev slab on freelist */
2017c478bd9Sstevel@tonic-gate 	struct umem_bufctl	*slab_head;	/* first free buffer */
2027c478bd9Sstevel@tonic-gate 	long			slab_refcnt;	/* outstanding allocations */
2037c478bd9Sstevel@tonic-gate 	long			slab_chunks;	/* chunks (bufs) in this slab */
2047c478bd9Sstevel@tonic-gate } umem_slab_t;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate #define	UMEM_HASH_INITIAL	64
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate #define	UMEM_HASH(cp, buf)	\
2097c478bd9Sstevel@tonic-gate 	((cp)->cache_hash_table +	\
2107c478bd9Sstevel@tonic-gate 	(((uintptr_t)(buf) >> (cp)->cache_hash_shift) & (cp)->cache_hash_mask))
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate typedef struct umem_magazine {
2137c478bd9Sstevel@tonic-gate 	void	*mag_next;
2147c478bd9Sstevel@tonic-gate 	void	*mag_round[1];		/* one or more rounds */
2157c478bd9Sstevel@tonic-gate } umem_magazine_t;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * The magazine types for fast per-cpu allocation
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate typedef struct umem_magtype {
2217c478bd9Sstevel@tonic-gate 	int		mt_magsize;	/* magazine size (number of rounds) */
2227c478bd9Sstevel@tonic-gate 	int		mt_align;	/* magazine alignment */
2237c478bd9Sstevel@tonic-gate 	size_t		mt_minbuf;	/* all smaller buffers qualify */
2247c478bd9Sstevel@tonic-gate 	size_t		mt_maxbuf;	/* no larger buffers qualify */
2257c478bd9Sstevel@tonic-gate 	umem_cache_t	*mt_cache;	/* magazine cache */
2267c478bd9Sstevel@tonic-gate } umem_magtype_t;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate #define	UMEM_CPU_CACHE_SIZE	64	/* must be power of 2 */
2297c478bd9Sstevel@tonic-gate #define	UMEM_CPU_PAD		(UMEM_CPU_CACHE_SIZE - sizeof (mutex_t) - \
2307c478bd9Sstevel@tonic-gate 	2 * sizeof (uint_t) - 2 * sizeof (void *) - 4 * sizeof (int))
2317c478bd9Sstevel@tonic-gate #define	UMEM_CACHE_SIZE(ncpus)	\
2327c478bd9Sstevel@tonic-gate 	((size_t)(&((umem_cache_t *)0)->cache_cpu[ncpus]))
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate typedef struct umem_cpu_cache {
2357c478bd9Sstevel@tonic-gate 	mutex_t		cc_lock;	/* protects this cpu's local cache */
2367c478bd9Sstevel@tonic-gate 	uint_t		cc_alloc;	/* allocations from this cpu */
2377c478bd9Sstevel@tonic-gate 	uint_t		cc_free;	/* frees to this cpu */
2387c478bd9Sstevel@tonic-gate 	umem_magazine_t	*cc_loaded;	/* the currently loaded magazine */
2397c478bd9Sstevel@tonic-gate 	umem_magazine_t	*cc_ploaded;	/* the previously loaded magazine */
2407c478bd9Sstevel@tonic-gate 	int		cc_rounds;	/* number of objects in loaded mag */
2417c478bd9Sstevel@tonic-gate 	int		cc_prounds;	/* number of objects in previous mag */
2427c478bd9Sstevel@tonic-gate 	int		cc_magsize;	/* number of rounds in a full mag */
2437c478bd9Sstevel@tonic-gate 	int		cc_flags;	/* CPU-local copy of cache_flags */
2447c478bd9Sstevel@tonic-gate #ifndef _LP64
2457c478bd9Sstevel@tonic-gate 	char		cc_pad[UMEM_CPU_PAD]; /* for nice alignment (32-bit) */
2467c478bd9Sstevel@tonic-gate #endif
2477c478bd9Sstevel@tonic-gate } umem_cpu_cache_t;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * The magazine lists used in the depot.
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate typedef struct umem_maglist {
2537c478bd9Sstevel@tonic-gate 	umem_magazine_t	*ml_list;	/* magazine list */
2547c478bd9Sstevel@tonic-gate 	long		ml_total;	/* number of magazines */
2557c478bd9Sstevel@tonic-gate 	long		ml_min;		/* min since last update */
2567c478bd9Sstevel@tonic-gate 	long		ml_reaplimit;	/* max reapable magazines */
2577c478bd9Sstevel@tonic-gate 	uint64_t	ml_alloc;	/* allocations from this list */
2587c478bd9Sstevel@tonic-gate } umem_maglist_t;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate #define	UMEM_CACHE_NAMELEN	31
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate struct umem_cache {
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * Statistics
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_create;	/* slab creates */
2677c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_destroy;	/* slab destroys */
2687c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_alloc;	/* slab layer allocations */
2697c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_free;	/* slab layer frees */
2707c478bd9Sstevel@tonic-gate 	uint64_t	cache_alloc_fail;	/* total failed allocations */
2717c478bd9Sstevel@tonic-gate 	uint64_t	cache_buftotal;		/* total buffers */
2727c478bd9Sstevel@tonic-gate 	uint64_t	cache_bufmax;		/* max buffers ever */
2737c478bd9Sstevel@tonic-gate 	uint64_t	cache_rescale;		/* # of hash table rescales */
2747c478bd9Sstevel@tonic-gate 	uint64_t	cache_lookup_depth;	/* hash lookup depth */
2757c478bd9Sstevel@tonic-gate 	uint64_t	cache_depot_contention;	/* mutex contention count */
2767c478bd9Sstevel@tonic-gate 	uint64_t	cache_depot_contention_prev; /* previous snapshot */
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * Cache properties
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	char		cache_name[UMEM_CACHE_NAMELEN + 1];
2827c478bd9Sstevel@tonic-gate 	size_t		cache_bufsize;		/* object size */
2837c478bd9Sstevel@tonic-gate 	size_t		cache_align;		/* object alignment */
2847c478bd9Sstevel@tonic-gate 	umem_constructor_t *cache_constructor;
2857c478bd9Sstevel@tonic-gate 	umem_destructor_t *cache_destructor;
2867c478bd9Sstevel@tonic-gate 	umem_reclaim_t	*cache_reclaim;
2877c478bd9Sstevel@tonic-gate 	void		*cache_private;		/* opaque arg to callbacks */
2887c478bd9Sstevel@tonic-gate 	vmem_t		*cache_arena;		/* vmem source for slabs */
2897c478bd9Sstevel@tonic-gate 	int		cache_cflags;		/* cache creation flags */
2907c478bd9Sstevel@tonic-gate 	int		cache_flags;		/* various cache state info */
2917c478bd9Sstevel@tonic-gate 	int		cache_uflags;		/* UMU_* flags */
2927c478bd9Sstevel@tonic-gate 	uint32_t	cache_mtbf;		/* induced alloc failure rate */
2937c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_next;		/* forward cache linkage */
2947c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_prev;		/* backward cache linkage */
2957c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_unext;		/* next in update list */
2967c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_uprev;		/* prev in update list */
2977c478bd9Sstevel@tonic-gate 	uint32_t	cache_cpu_mask;		/* mask for cpu offset */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * Slab layer
3017c478bd9Sstevel@tonic-gate 	 */
3027c478bd9Sstevel@tonic-gate 	mutex_t		cache_lock;		/* protects slab layer */
3037c478bd9Sstevel@tonic-gate 	size_t		cache_chunksize;	/* buf + alignment [+ debug] */
3047c478bd9Sstevel@tonic-gate 	size_t		cache_slabsize;		/* size of a slab */
3057c478bd9Sstevel@tonic-gate 	size_t		cache_bufctl;		/* buf-to-bufctl distance */
3067c478bd9Sstevel@tonic-gate 	size_t		cache_buftag;		/* buf-to-buftag distance */
3077c478bd9Sstevel@tonic-gate 	size_t		cache_verify;		/* bytes to verify */
3087c478bd9Sstevel@tonic-gate 	size_t		cache_contents;		/* bytes of saved content */
3097c478bd9Sstevel@tonic-gate 	size_t		cache_color;		/* next slab color */
3107c478bd9Sstevel@tonic-gate 	size_t		cache_mincolor;		/* maximum slab color */
3117c478bd9Sstevel@tonic-gate 	size_t		cache_maxcolor;		/* maximum slab color */
3127c478bd9Sstevel@tonic-gate 	size_t		cache_hash_shift;	/* get to interesting bits */
3137c478bd9Sstevel@tonic-gate 	size_t		cache_hash_mask;	/* hash table mask */
3147c478bd9Sstevel@tonic-gate 	umem_slab_t	*cache_freelist;	/* slab free list */
3157c478bd9Sstevel@tonic-gate 	umem_slab_t	cache_nullslab;		/* end of freelist marker */
3167c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_bufctl_cache;	/* source of bufctls */
3177c478bd9Sstevel@tonic-gate 	umem_bufctl_t	**cache_hash_table;	/* hash table base */
3187c478bd9Sstevel@tonic-gate 	/*
3197c478bd9Sstevel@tonic-gate 	 * Depot layer
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 	mutex_t		cache_depot_lock;	/* protects depot */
3227c478bd9Sstevel@tonic-gate 	umem_magtype_t	*cache_magtype;		/* magazine type */
3237c478bd9Sstevel@tonic-gate 	umem_maglist_t	cache_full;		/* full magazines */
3247c478bd9Sstevel@tonic-gate 	umem_maglist_t	cache_empty;		/* empty magazines */
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Per-CPU layer
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	umem_cpu_cache_t cache_cpu[1];		/* cache_cpu_mask + 1 entries */
3307c478bd9Sstevel@tonic-gate };
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate typedef struct umem_cpu_log_header {
3337c478bd9Sstevel@tonic-gate 	mutex_t		clh_lock;
3347c478bd9Sstevel@tonic-gate 	char		*clh_current;
3357c478bd9Sstevel@tonic-gate 	size_t		clh_avail;
3367c478bd9Sstevel@tonic-gate 	int		clh_chunk;
3377c478bd9Sstevel@tonic-gate 	int		clh_hits;
3387c478bd9Sstevel@tonic-gate 	char		clh_pad[64 - sizeof (mutex_t) - sizeof (char *) -
3397c478bd9Sstevel@tonic-gate 				sizeof (size_t) - 2 * sizeof (int)];
3407c478bd9Sstevel@tonic-gate } umem_cpu_log_header_t;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate typedef struct umem_log_header {
3437c478bd9Sstevel@tonic-gate 	mutex_t		lh_lock;
3447c478bd9Sstevel@tonic-gate 	char		*lh_base;
3457c478bd9Sstevel@tonic-gate 	int		*lh_free;
3467c478bd9Sstevel@tonic-gate 	size_t		lh_chunksize;
3477c478bd9Sstevel@tonic-gate 	int		lh_nchunks;
3487c478bd9Sstevel@tonic-gate 	int		lh_head;
3497c478bd9Sstevel@tonic-gate 	int		lh_tail;
3507c478bd9Sstevel@tonic-gate 	int		lh_hits;
3517c478bd9Sstevel@tonic-gate 	umem_cpu_log_header_t lh_cpu[1];	/* actually umem_max_ncpus */
3527c478bd9Sstevel@tonic-gate } umem_log_header_t;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate typedef struct umem_cpu {
3557c478bd9Sstevel@tonic-gate 	uint32_t cpu_cache_offset;
3567c478bd9Sstevel@tonic-gate 	uint32_t cpu_number;
3577c478bd9Sstevel@tonic-gate } umem_cpu_t;
3587c478bd9Sstevel@tonic-gate 
35938849194SRobert Mustacchi #define	UMEM_MAXBUF	131072
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate #define	UMEM_ALIGN		8	/* min guaranteed alignment */
3627c478bd9Sstevel@tonic-gate #define	UMEM_ALIGN_SHIFT	3	/* log2(UMEM_ALIGN) */
3637c478bd9Sstevel@tonic-gate #define	UMEM_VOID_FRACTION	8	/* never waste more than 1/8 of slab */
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * For 64 bits, buffers >= 16 bytes must be 16-byte aligned
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate #ifdef _LP64
3697c478bd9Sstevel@tonic-gate #define	UMEM_SECOND_ALIGN 16
3707c478bd9Sstevel@tonic-gate #else
3717c478bd9Sstevel@tonic-gate #define	UMEM_SECOND_ALIGN UMEM_ALIGN
3727c478bd9Sstevel@tonic-gate #endif
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate #define	MALLOC_MAGIC			0x3a10c000 /* 8-byte tag */
3757c478bd9Sstevel@tonic-gate #define	MEMALIGN_MAGIC			0x3e3a1000
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate #ifdef _LP64
3787c478bd9Sstevel@tonic-gate #define	MALLOC_SECOND_MAGIC		0x16ba7000 /* 8-byte tag, 16-aligned */
3797c478bd9Sstevel@tonic-gate #define	MALLOC_OVERSIZE_MAGIC		0x06e47000 /* 16-byte tag, _LP64 */
3807c478bd9Sstevel@tonic-gate #endif
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate #define	UMEM_MALLOC_ENCODE(type, sz)	(uint32_t)((type) - (sz))
3837c478bd9Sstevel@tonic-gate #define	UMEM_MALLOC_DECODE(stat, sz)	(uint32_t)((stat) + (sz))
3847c478bd9Sstevel@tonic-gate #define	UMEM_FREE_PATTERN_32		(uint32_t)(UMEM_FREE_PATTERN)
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate #define	UMU_MAGAZINE_RESIZE	0x00000001
3877c478bd9Sstevel@tonic-gate #define	UMU_HASH_RESCALE	0x00000002
3887c478bd9Sstevel@tonic-gate #define	UMU_REAP		0x00000004
3897c478bd9Sstevel@tonic-gate #define	UMU_NOTIFY		0x08000000
3907c478bd9Sstevel@tonic-gate #define	UMU_ACTIVE		0x80000000
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate #define	UMEM_READY_INIT_FAILED		-1
3937c478bd9Sstevel@tonic-gate #define	UMEM_READY_STARTUP		1
3947c478bd9Sstevel@tonic-gate #define	UMEM_READY_INITING		2
3957c478bd9Sstevel@tonic-gate #define	UMEM_READY			3
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate #ifdef UMEM_STANDALONE
3987c478bd9Sstevel@tonic-gate extern void umem_startup(caddr_t, size_t, size_t, caddr_t, caddr_t);
3997c478bd9Sstevel@tonic-gate extern int umem_add(caddr_t, size_t);
4007c478bd9Sstevel@tonic-gate #endif
4017c478bd9Sstevel@tonic-gate 
402*4f364e7cSRobert Mustacchi /*
403*4f364e7cSRobert Mustacchi  * Private interface with libc for tcumem.
404*4f364e7cSRobert Mustacchi  */
405*4f364e7cSRobert Mustacchi extern uintptr_t _tmem_get_base(void);
406*4f364e7cSRobert Mustacchi extern int _tmem_get_nentries(void);
407*4f364e7cSRobert Mustacchi extern void _tmem_set_cleanup(void(*)(void *, int));
408*4f364e7cSRobert Mustacchi 
4097c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate #endif
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate #endif	/* _UMEM_IMPL_H */
414