xref: /illumos-gate/usr/src/uts/common/sys/kmem.h (revision ed093b41)
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
5b5fca8f8Stomee  * Common Development and Distribution License (the "License").
6b5fca8f8Stomee  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21b5fca8f8Stomee 
227c478bd9Sstevel@tonic-gate /*
23b942e89bSDavid Valin  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24e36d7b11SSebastien Roy  * Copyright (c) 2012 by Delphix. All rights reserved.
25b819cea2SGordon Ross  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
26ca783257SDan McDonald  * Copyright 2022 Joyent, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifndef _SYS_KMEM_H
337c478bd9Sstevel@tonic-gate #define	_SYS_KMEM_H
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
397c478bd9Sstevel@tonic-gate extern "C" {
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Kernel memory allocator: DDI interfaces.
447c478bd9Sstevel@tonic-gate  * See kmem_alloc(9F) for details.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	KM_SLEEP	0x0000	/* can block for memory; success guaranteed */
487c478bd9Sstevel@tonic-gate #define	KM_NOSLEEP	0x0001	/* cannot block for memory; may fail */
497c478bd9Sstevel@tonic-gate #define	KM_PANIC	0x0002	/* if memory cannot be allocated, panic */
507c478bd9Sstevel@tonic-gate #define	KM_PUSHPAGE	0x0004	/* can block for memory; may use reserve */
5123a80de1SStan Studzinski #define	KM_NORMALPRI	0x0008  /* with KM_NOSLEEP, lower priority allocation */
52ca783257SDan McDonald #define	KM_NOSLEEP_LAZY	(KM_NOSLEEP | KM_NORMALPRI)  /* Syntactic sugar. */
537c478bd9Sstevel@tonic-gate #define	KM_VMFLAGS	0x00ff	/* flags that must match VM_* flags */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	KM_FLAGS	0xffff	/* all settable kmem flags */
567c478bd9Sstevel@tonic-gate 
57b819cea2SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate extern void *kmem_alloc(size_t size, int kmflags);
607c478bd9Sstevel@tonic-gate extern void *kmem_zalloc(size_t size, int kmflags);
617c478bd9Sstevel@tonic-gate extern void kmem_free(void *buf, size_t size);
627c478bd9Sstevel@tonic-gate extern void *kmem_alloc_tryhard(size_t size, size_t *alloc_size, int kmflags);
639dd77bc8SDave Plauger extern void kmem_dump_init(size_t);
649dd77bc8SDave Plauger extern void kmem_dump_begin(void);
659dd77bc8SDave Plauger extern size_t kmem_dump_finish(char *buf, size_t size);
66*ed093b41SRobert Mustacchi extern void *kmem_rezalloc(void *, size_t, size_t, int);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Kernel memory allocator: private interfaces.
727c478bd9Sstevel@tonic-gate  * These interfaces are still evolving.
737c478bd9Sstevel@tonic-gate  * Do not use them in unbundled drivers.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Flags for kmem_cache_create()
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate #define	KMC_NOTOUCH	0x00010000
807c478bd9Sstevel@tonic-gate #define	KMC_NODEBUG	0x00020000
817c478bd9Sstevel@tonic-gate #define	KMC_NOMAGAZINE	0x00040000
827c478bd9Sstevel@tonic-gate #define	KMC_NOHASH	0x00080000
837c478bd9Sstevel@tonic-gate #define	KMC_QCACHE	0x00100000
847c478bd9Sstevel@tonic-gate #define	KMC_KMEM_ALLOC	0x00200000	/* internal use only */
857c478bd9Sstevel@tonic-gate #define	KMC_IDENTIFIER	0x00400000	/* internal use only */
86b942e89bSDavid Valin #define	KMC_PREFILL	0x00800000
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate struct kmem_cache;		/* cache structure is opaque to kmem clients */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate typedef struct kmem_cache kmem_cache_t;
917c478bd9Sstevel@tonic-gate 
92b5fca8f8Stomee /* Client response to kmem move callback */
93b5fca8f8Stomee typedef enum kmem_cbrc {
94b5fca8f8Stomee 	KMEM_CBRC_YES,
95b5fca8f8Stomee 	KMEM_CBRC_NO,
96b5fca8f8Stomee 	KMEM_CBRC_LATER,
97b5fca8f8Stomee 	KMEM_CBRC_DONT_NEED,
98b5fca8f8Stomee 	KMEM_CBRC_DONT_KNOW
99b5fca8f8Stomee } kmem_cbrc_t;
100b5fca8f8Stomee 
101b819cea2SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
1027c478bd9Sstevel@tonic-gate 
103744947dcSTom Erickson /*
104744947dcSTom Erickson  * Helps clients implementing the move() callback to recognize known objects by
105744947dcSTom Erickson  * testing a client-designated pointer member. Takes advantage of the fact that
106744947dcSTom Erickson  * any scribbling to freed memory done by kmem is guaranteed to set one of the
107744947dcSTom Erickson  * two low order bits.
108744947dcSTom Erickson  */
109744947dcSTom Erickson #define	POINTER_IS_VALID(p)	(!((uintptr_t)(p) & 0x3))
110744947dcSTom Erickson #define	POINTER_INVALIDATE(pp)	(*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
111744947dcSTom Erickson 
1127c478bd9Sstevel@tonic-gate extern int kmem_ready;
1137c478bd9Sstevel@tonic-gate extern pgcnt_t kmem_reapahead;
114e36d7b11SSebastien Roy extern size_t kmem_max_cached;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate extern void kmem_init(void);
1177c478bd9Sstevel@tonic-gate extern void kmem_thread_init(void);
1187c478bd9Sstevel@tonic-gate extern void kmem_mp_init(void);
1197c478bd9Sstevel@tonic-gate extern void kmem_reap(void);
1207c478bd9Sstevel@tonic-gate extern void kmem_reap_idspace(void);
121fa9e4066Sahrens extern int kmem_debugging(void);
1227c478bd9Sstevel@tonic-gate extern size_t kmem_avail(void);
1237c478bd9Sstevel@tonic-gate extern size_t kmem_maxavail(void);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate extern kmem_cache_t *kmem_cache_create(char *, size_t, size_t,
1267c478bd9Sstevel@tonic-gate 	int (*)(void *, void *, int), void (*)(void *, void *),
1277c478bd9Sstevel@tonic-gate 	void (*)(void *), void *, vmem_t *, int);
128b5fca8f8Stomee extern void kmem_cache_set_move(kmem_cache_t *,
129b5fca8f8Stomee 	kmem_cbrc_t (*)(void *, void *, size_t, void *));
1307c478bd9Sstevel@tonic-gate extern void kmem_cache_destroy(kmem_cache_t *);
1317c478bd9Sstevel@tonic-gate extern void *kmem_cache_alloc(kmem_cache_t *, int);
1327c478bd9Sstevel@tonic-gate extern void kmem_cache_free(kmem_cache_t *, void *);
1337c478bd9Sstevel@tonic-gate extern uint64_t kmem_cache_stat(kmem_cache_t *, char *);
13436a64e62STim Kordas extern boolean_t kmem_cache_reap_active(void);
13536a64e62STim Kordas extern void kmem_cache_reap_soon(kmem_cache_t *);
136b5fca8f8Stomee extern void kmem_cache_move_notify(kmem_cache_t *, void *);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate #endif
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #endif	/* _SYS_KMEM_H */
145