xref: /titanic_50/usr/src/uts/common/sys/kmem.h (revision b819cea2f73f98c5662230cc9affc8cc84f77fcf)
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.
25*b819cea2SGordon Ross  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifndef _SYS_KMEM_H
327c478bd9Sstevel@tonic-gate #define	_SYS_KMEM_H
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
387c478bd9Sstevel@tonic-gate extern "C" {
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Kernel memory allocator: DDI interfaces.
437c478bd9Sstevel@tonic-gate  * See kmem_alloc(9F) for details.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	KM_SLEEP	0x0000	/* can block for memory; success guaranteed */
477c478bd9Sstevel@tonic-gate #define	KM_NOSLEEP	0x0001	/* cannot block for memory; may fail */
487c478bd9Sstevel@tonic-gate #define	KM_PANIC	0x0002	/* if memory cannot be allocated, panic */
497c478bd9Sstevel@tonic-gate #define	KM_PUSHPAGE	0x0004	/* can block for memory; may use reserve */
5023a80de1SStan Studzinski #define	KM_NORMALPRI	0x0008  /* with KM_NOSLEEP, lower priority allocation */
517c478bd9Sstevel@tonic-gate #define	KM_VMFLAGS	0x00ff	/* flags that must match VM_* flags */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	KM_FLAGS	0xffff	/* all settable kmem flags */
547c478bd9Sstevel@tonic-gate 
55*b819cea2SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate extern void *kmem_alloc(size_t size, int kmflags);
587c478bd9Sstevel@tonic-gate extern void *kmem_zalloc(size_t size, int kmflags);
597c478bd9Sstevel@tonic-gate extern void kmem_free(void *buf, size_t size);
607c478bd9Sstevel@tonic-gate extern void *kmem_alloc_tryhard(size_t size, size_t *alloc_size, int kmflags);
619dd77bc8SDave Plauger extern void kmem_dump_init(size_t);
629dd77bc8SDave Plauger extern void kmem_dump_begin(void);
639dd77bc8SDave Plauger extern size_t kmem_dump_finish(char *buf, size_t size);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Kernel memory allocator: private interfaces.
697c478bd9Sstevel@tonic-gate  * These interfaces are still evolving.
707c478bd9Sstevel@tonic-gate  * Do not use them in unbundled drivers.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Flags for kmem_cache_create()
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate #define	KMC_NOTOUCH	0x00010000
777c478bd9Sstevel@tonic-gate #define	KMC_NODEBUG	0x00020000
787c478bd9Sstevel@tonic-gate #define	KMC_NOMAGAZINE	0x00040000
797c478bd9Sstevel@tonic-gate #define	KMC_NOHASH	0x00080000
807c478bd9Sstevel@tonic-gate #define	KMC_QCACHE	0x00100000
817c478bd9Sstevel@tonic-gate #define	KMC_KMEM_ALLOC	0x00200000	/* internal use only */
827c478bd9Sstevel@tonic-gate #define	KMC_IDENTIFIER	0x00400000	/* internal use only */
83b942e89bSDavid Valin #define	KMC_PREFILL	0x00800000
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate struct kmem_cache;		/* cache structure is opaque to kmem clients */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate typedef struct kmem_cache kmem_cache_t;
887c478bd9Sstevel@tonic-gate 
89b5fca8f8Stomee /* Client response to kmem move callback */
90b5fca8f8Stomee typedef enum kmem_cbrc {
91b5fca8f8Stomee 	KMEM_CBRC_YES,
92b5fca8f8Stomee 	KMEM_CBRC_NO,
93b5fca8f8Stomee 	KMEM_CBRC_LATER,
94b5fca8f8Stomee 	KMEM_CBRC_DONT_NEED,
95b5fca8f8Stomee 	KMEM_CBRC_DONT_KNOW
96b5fca8f8Stomee } kmem_cbrc_t;
97b5fca8f8Stomee 
98*b819cea2SGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
997c478bd9Sstevel@tonic-gate 
100744947dcSTom Erickson /*
101744947dcSTom Erickson  * Helps clients implementing the move() callback to recognize known objects by
102744947dcSTom Erickson  * testing a client-designated pointer member. Takes advantage of the fact that
103744947dcSTom Erickson  * any scribbling to freed memory done by kmem is guaranteed to set one of the
104744947dcSTom Erickson  * two low order bits.
105744947dcSTom Erickson  */
106744947dcSTom Erickson #define	POINTER_IS_VALID(p)	(!((uintptr_t)(p) & 0x3))
107744947dcSTom Erickson #define	POINTER_INVALIDATE(pp)	(*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
108744947dcSTom Erickson 
1097c478bd9Sstevel@tonic-gate extern int kmem_ready;
1107c478bd9Sstevel@tonic-gate extern pgcnt_t kmem_reapahead;
111e36d7b11SSebastien Roy extern size_t kmem_max_cached;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate extern void kmem_init(void);
1147c478bd9Sstevel@tonic-gate extern void kmem_thread_init(void);
1157c478bd9Sstevel@tonic-gate extern void kmem_mp_init(void);
1167c478bd9Sstevel@tonic-gate extern void kmem_reap(void);
1177c478bd9Sstevel@tonic-gate extern void kmem_reap_idspace(void);
118fa9e4066Sahrens extern int kmem_debugging(void);
1197c478bd9Sstevel@tonic-gate extern size_t kmem_avail(void);
1207c478bd9Sstevel@tonic-gate extern size_t kmem_maxavail(void);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate extern kmem_cache_t *kmem_cache_create(char *, size_t, size_t,
1237c478bd9Sstevel@tonic-gate 	int (*)(void *, void *, int), void (*)(void *, void *),
1247c478bd9Sstevel@tonic-gate 	void (*)(void *), void *, vmem_t *, int);
125b5fca8f8Stomee extern void kmem_cache_set_move(kmem_cache_t *,
126b5fca8f8Stomee 	kmem_cbrc_t (*)(void *, void *, size_t, void *));
1277c478bd9Sstevel@tonic-gate extern void kmem_cache_destroy(kmem_cache_t *);
1287c478bd9Sstevel@tonic-gate extern void *kmem_cache_alloc(kmem_cache_t *, int);
1297c478bd9Sstevel@tonic-gate extern void kmem_cache_free(kmem_cache_t *, void *);
1307c478bd9Sstevel@tonic-gate extern uint64_t kmem_cache_stat(kmem_cache_t *, char *);
131fa9e4066Sahrens extern void kmem_cache_reap_now(kmem_cache_t *);
132b5fca8f8Stomee extern void kmem_cache_move_notify(kmem_cache_t *, void *);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #endif	/* _SYS_KMEM_H */
141