17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5b5fca8ftomee * Common Development and Distribution License (the "License").
6b5fca8ftomee * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21b5fca8ftomee
227c478bdstevel@tonic-gate/*
23b942e89David Valin * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24e36d7b1Sebastien Roy * Copyright (c) 2012 by Delphix. All rights reserved.
25b819ceaGordon Ross * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
2636a64e6Tim Kordas * Copyright 2018, Joyent, Inc.
277c478bdstevel@tonic-gate */
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
307c478bdstevel@tonic-gate/*	  All Rights Reserved	*/
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate#ifndef _SYS_KMEM_H
337c478bdstevel@tonic-gate#define	_SYS_KMEM_H
347c478bdstevel@tonic-gate
357c478bdstevel@tonic-gate#include <sys/types.h>
367c478bdstevel@tonic-gate#include <sys/vmem.h>
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#ifdef	__cplusplus
397c478bdstevel@tonic-gateextern "C" {
407c478bdstevel@tonic-gate#endif
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate/*
437c478bdstevel@tonic-gate * Kernel memory allocator: DDI interfaces.
447c478bdstevel@tonic-gate * See kmem_alloc(9F) for details.
457c478bdstevel@tonic-gate */
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate#define	KM_SLEEP	0x0000	/* can block for memory; success guaranteed */
487c478bdstevel@tonic-gate#define	KM_NOSLEEP	0x0001	/* cannot block for memory; may fail */
497c478bdstevel@tonic-gate#define	KM_PANIC	0x0002	/* if memory cannot be allocated, panic */
507c478bdstevel@tonic-gate#define	KM_PUSHPAGE	0x0004	/* can block for memory; may use reserve */
5123a80deStan Studzinski#define	KM_NORMALPRI	0x0008  /* with KM_NOSLEEP, lower priority allocation */
527c478bdstevel@tonic-gate#define	KM_VMFLAGS	0x00ff	/* flags that must match VM_* flags */
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate#define	KM_FLAGS	0xffff	/* all settable kmem flags */
557c478bdstevel@tonic-gate
56b819ceaGordon Ross#if defined(_KERNEL) || defined(_FAKE_KERNEL)
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gateextern void *kmem_alloc(size_t size, int kmflags);
597c478bdstevel@tonic-gateextern void *kmem_zalloc(size_t size, int kmflags);
607c478bdstevel@tonic-gateextern void kmem_free(void *buf, size_t size);
617c478bdstevel@tonic-gateextern void *kmem_alloc_tryhard(size_t size, size_t *alloc_size, int kmflags);
629dd77bcDave Plaugerextern void kmem_dump_init(size_t);
639dd77bcDave Plaugerextern void kmem_dump_begin(void);
649dd77bcDave Plaugerextern size_t kmem_dump_finish(char *buf, size_t size);
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate#endif	/* _KERNEL */
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate/*
697c478bdstevel@tonic-gate * Kernel memory allocator: private interfaces.
707c478bdstevel@tonic-gate * These interfaces are still evolving.
717c478bdstevel@tonic-gate * Do not use them in unbundled drivers.
727c478bdstevel@tonic-gate */
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate/*
757c478bdstevel@tonic-gate * Flags for kmem_cache_create()
767c478bdstevel@tonic-gate */
777c478bdstevel@tonic-gate#define	KMC_NOTOUCH	0x00010000
787c478bdstevel@tonic-gate#define	KMC_NODEBUG	0x00020000
797c478bdstevel@tonic-gate#define	KMC_NOMAGAZINE	0x00040000
807c478bdstevel@tonic-gate#define	KMC_NOHASH	0x00080000
817c478bdstevel@tonic-gate#define	KMC_QCACHE	0x00100000
827c478bdstevel@tonic-gate#define	KMC_KMEM_ALLOC	0x00200000	/* internal use only */
837c478bdstevel@tonic-gate#define	KMC_IDENTIFIER	0x00400000	/* internal use only */
84b942e89David Valin#define	KMC_PREFILL	0x00800000
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gatestruct kmem_cache;		/* cache structure is opaque to kmem clients */
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gatetypedef struct kmem_cache kmem_cache_t;
897c478bdstevel@tonic-gate
90b5fca8ftomee/* Client response to kmem move callback */
91b5fca8ftomeetypedef enum kmem_cbrc {
92b5fca8ftomee	KMEM_CBRC_YES,
93b5fca8ftomee	KMEM_CBRC_NO,
94b5fca8ftomee	KMEM_CBRC_LATER,
95b5fca8ftomee	KMEM_CBRC_DONT_NEED,
96b5fca8ftomee	KMEM_CBRC_DONT_KNOW
97b5fca8ftomee} kmem_cbrc_t;
98b5fca8ftomee
99b819ceaGordon Ross#if defined(_KERNEL) || defined(_FAKE_KERNEL)
1007c478bdstevel@tonic-gate
101744947dTom Erickson/*
102744947dTom Erickson * Helps clients implementing the move() callback to recognize known objects by
103744947dTom Erickson * testing a client-designated pointer member. Takes advantage of the fact that
104744947dTom Erickson * any scribbling to freed memory done by kmem is guaranteed to set one of the
105744947dTom Erickson * two low order bits.
106744947dTom Erickson */
107744947dTom Erickson#define	POINTER_IS_VALID(p)	(!((uintptr_t)(p) & 0x3))
108744947dTom Erickson#define	POINTER_INVALIDATE(pp)	(*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
109744947dTom Erickson
1107c478bdstevel@tonic-gateextern int kmem_ready;
1117c478bdstevel@tonic-gateextern pgcnt_t kmem_reapahead;
112e36d7b1Sebastien Royextern size_t kmem_max_cached;
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gateextern void kmem_init(void);
1157c478bdstevel@tonic-gateextern void kmem_thread_init(void);
1167c478bdstevel@tonic-gateextern void kmem_mp_init(void);
1177c478bdstevel@tonic-gateextern void kmem_reap(void);
1187c478bdstevel@tonic-gateextern void kmem_reap_idspace(void);
119fa9e406ahrensextern int kmem_debugging(void);
1207c478bdstevel@tonic-gateextern size_t kmem_avail(void);
1217c478bdstevel@tonic-gateextern size_t kmem_maxavail(void);
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gateextern kmem_cache_t *kmem_cache_create(char *, size_t, size_t,
1247c478bdstevel@tonic-gate	int (*)(void *, void *, int), void (*)(void *, void *),
1257c478bdstevel@tonic-gate	void (*)(void *), void *, vmem_t *, int);
126b5fca8ftomeeextern void kmem_cache_set_move(kmem_cache_t *,
127b5fca8ftomee	kmem_cbrc_t (*)(void *, void *, size_t, void *));
1287c478bdstevel@tonic-gateextern void kmem_cache_destroy(kmem_cache_t *);
1297c478bdstevel@tonic-gateextern void *kmem_cache_alloc(kmem_cache_t *, int);
1307c478bdstevel@tonic-gateextern void kmem_cache_free(kmem_cache_t *, void *);
1317c478bdstevel@tonic-gateextern uint64_t kmem_cache_stat(kmem_cache_t *, char *);
13236a64e6Tim Kordasextern boolean_t kmem_cache_reap_active(void);
13336a64e6Tim Kordasextern void kmem_cache_reap_soon(kmem_cache_t *);
134b5fca8ftomeeextern void kmem_cache_move_notify(kmem_cache_t *, void *);
1357c478bdstevel@tonic-gate
1367c478bdstevel@tonic-gate#endif	/* _KERNEL */
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gate#ifdef	__cplusplus
1397c478bdstevel@tonic-gate}
1407c478bdstevel@tonic-gate#endif
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate#endif	/* _SYS_KMEM_H */
143