xref: /titanic_50/usr/src/uts/common/sys/kmem.h (revision b97d6ca7333c353b6ca20c20c99fb1be8d32a8de)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /*	  All Rights Reserved	*/
28 
29 #ifndef _SYS_KMEM_H
30 #define	_SYS_KMEM_H
31 
32 #include <sys/types.h>
33 #include <sys/vmem.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 /*
40  * Kernel memory allocator: DDI interfaces.
41  * See kmem_alloc(9F) for details.
42  */
43 
44 #define	KM_SLEEP	0x0000	/* can block for memory; success guaranteed */
45 #define	KM_NOSLEEP	0x0001	/* cannot block for memory; may fail */
46 #define	KM_PANIC	0x0002	/* if memory cannot be allocated, panic */
47 #define	KM_PUSHPAGE	0x0004	/* can block for memory; may use reserve */
48 #define	KM_NORMALPRI	0x0008  /* with KM_NOSLEEP, lower priority allocation */
49 #define	KM_VMFLAGS	0x00ff	/* flags that must match VM_* flags */
50 
51 #define	KM_FLAGS	0xffff	/* all settable kmem flags */
52 
53 #ifdef _KERNEL
54 
55 extern void *kmem_alloc(size_t size, int kmflags);
56 extern void *kmem_zalloc(size_t size, int kmflags);
57 extern void kmem_free(void *buf, size_t size);
58 extern void *kmem_alloc_tryhard(size_t size, size_t *alloc_size, int kmflags);
59 extern void kmem_dump_init(size_t);
60 extern void kmem_dump_begin(void);
61 extern size_t kmem_dump_finish(char *buf, size_t size);
62 
63 #endif	/* _KERNEL */
64 
65 /*
66  * Kernel memory allocator: private interfaces.
67  * These interfaces are still evolving.
68  * Do not use them in unbundled drivers.
69  */
70 
71 /*
72  * Flags for kmem_cache_create()
73  */
74 #define	KMC_NOTOUCH	0x00010000
75 #define	KMC_NODEBUG	0x00020000
76 #define	KMC_NOMAGAZINE	0x00040000
77 #define	KMC_NOHASH	0x00080000
78 #define	KMC_QCACHE	0x00100000
79 #define	KMC_KMEM_ALLOC	0x00200000	/* internal use only */
80 #define	KMC_IDENTIFIER	0x00400000	/* internal use only */
81 #define	KMC_PREFILL	0x00800000
82 
83 struct kmem_cache;		/* cache structure is opaque to kmem clients */
84 
85 typedef struct kmem_cache kmem_cache_t;
86 
87 /* Client response to kmem move callback */
88 typedef enum kmem_cbrc {
89 	KMEM_CBRC_YES,
90 	KMEM_CBRC_NO,
91 	KMEM_CBRC_LATER,
92 	KMEM_CBRC_DONT_NEED,
93 	KMEM_CBRC_DONT_KNOW
94 } kmem_cbrc_t;
95 
96 #ifdef _KERNEL
97 
98 /*
99  * Helps clients implementing the move() callback to recognize known objects by
100  * testing a client-designated pointer member. Takes advantage of the fact that
101  * any scribbling to freed memory done by kmem is guaranteed to set one of the
102  * two low order bits.
103  */
104 #define	POINTER_IS_VALID(p)	(!((uintptr_t)(p) & 0x3))
105 #define	POINTER_INVALIDATE(pp)	(*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
106 
107 extern int kmem_ready;
108 extern pgcnt_t kmem_reapahead;
109 
110 extern void kmem_init(void);
111 extern void kmem_thread_init(void);
112 extern void kmem_mp_init(void);
113 extern void kmem_reap(void);
114 extern void kmem_reap_idspace(void);
115 extern int kmem_debugging(void);
116 extern size_t kmem_avail(void);
117 extern size_t kmem_maxavail(void);
118 
119 extern kmem_cache_t *kmem_cache_create(char *, size_t, size_t,
120 	int (*)(void *, void *, int), void (*)(void *, void *),
121 	void (*)(void *), void *, vmem_t *, int);
122 extern void kmem_cache_set_move(kmem_cache_t *,
123 	kmem_cbrc_t (*)(void *, void *, size_t, void *));
124 extern void kmem_cache_destroy(kmem_cache_t *);
125 extern void *kmem_cache_alloc(kmem_cache_t *, int);
126 extern void kmem_cache_free(kmem_cache_t *, void *);
127 extern uint64_t kmem_cache_stat(kmem_cache_t *, char *);
128 extern void kmem_cache_reap_now(kmem_cache_t *);
129 extern void kmem_cache_move_notify(kmem_cache_t *, void *);
130 
131 #endif	/* _KERNEL */
132 
133 #ifdef	__cplusplus
134 }
135 #endif
136 
137 #endif	/* _SYS_KMEM_H */
138