xref: /titanic_41/usr/src/uts/common/os/id32.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
34*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate static vmem_t *id32_arena;
37*7c478bd9Sstevel@tonic-gate static kmem_cache_t *id32_cache;
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #define	ID32_BITS	5
40*7c478bd9Sstevel@tonic-gate #define	ID32_ALIGN	(1 << ID32_BITS)
41*7c478bd9Sstevel@tonic-gate #define	ID32_MOD	(ID32_ALIGN - 1)
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * For amd64 the 32 bit id is the offset of the entry in the arena.
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate extern char *heap_core_base;
48*7c478bd9Sstevel@tonic-gate #define	ID32_ENCODE(x)	(((x) - (uintptr_t)heap_core_base) | \
49*7c478bd9Sstevel@tonic-gate 	((((x) - (uintptr_t)heap_core_base) % ID32_MOD)) + 1)
50*7c478bd9Sstevel@tonic-gate #define	ID32_DECODE(x)	(P2ALIGN((x), (uintptr_t)ID32_ALIGN) + \
51*7c478bd9Sstevel@tonic-gate 	(uintptr_t)heap_core_base)
52*7c478bd9Sstevel@tonic-gate #define	ID32_VALID(x)	(ID32_ENCODE(ID32_DECODE(x)) == (x))
53*7c478bd9Sstevel@tonic-gate #else	/* __amd64 */
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate  * All other architectures use the 32 bit pointer value for the 32 bit id.
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate #define	ID32_ENCODE(x)	(((x) | ((x) % ID32_MOD)) + 1)
58*7c478bd9Sstevel@tonic-gate #define	ID32_DECODE(x)	P2ALIGN((x), ID32_ALIGN)
59*7c478bd9Sstevel@tonic-gate #define	ID32_VALID(x)	(ID32_ENCODE(ID32_DECODE(x)) == (x))
60*7c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate void
id32_init(void)63*7c478bd9Sstevel@tonic-gate id32_init(void)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	id32_arena = vmem_create("id32", NULL, 0, PAGESIZE,
66*7c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	id32_cache = kmem_cache_create("id32_cache", ID32_ALIGN, ID32_ALIGN,
69*7c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, id32_arena, 0);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate  * Return a 32-bit identifier for the specified pointer.
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate uint32_t
id32_alloc(void * ptr,int kmflag)76*7c478bd9Sstevel@tonic-gate id32_alloc(void *ptr, int kmflag)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	void **hent = kmem_cache_alloc(id32_cache, kmflag);
79*7c478bd9Sstevel@tonic-gate 	uintptr_t id;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (hent == NULL)
82*7c478bd9Sstevel@tonic-gate 		return (0);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	*hent = ptr;
85*7c478bd9Sstevel@tonic-gate 	id = ID32_ENCODE((uintptr_t)hent);
86*7c478bd9Sstevel@tonic-gate 	ASSERT64(id <= UINT32_MAX);
87*7c478bd9Sstevel@tonic-gate 	return ((uint32_t)id);
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate /*
91*7c478bd9Sstevel@tonic-gate  * Free a 32-bit ID.
92*7c478bd9Sstevel@tonic-gate  */
93*7c478bd9Sstevel@tonic-gate void
id32_free(uint32_t id)94*7c478bd9Sstevel@tonic-gate id32_free(uint32_t id)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	if (!ID32_VALID(id)) {
97*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "id32_free(%x): bad ID rejected\n", id);
98*7c478bd9Sstevel@tonic-gate 		return;
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(id32_cache, (void *)(uintptr_t)ID32_DECODE(id));
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * Return the pointer described by a 32-bit ID, or NULL if the ID is bad.
106*7c478bd9Sstevel@tonic-gate  */
107*7c478bd9Sstevel@tonic-gate void *
id32_lookup(uint32_t id)108*7c478bd9Sstevel@tonic-gate id32_lookup(uint32_t id)
109*7c478bd9Sstevel@tonic-gate {
110*7c478bd9Sstevel@tonic-gate 	if (!ID32_VALID(id)) {
111*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "id32_lookup(%x): bad ID rejected\n", id);
112*7c478bd9Sstevel@tonic-gate 		return (NULL);
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	return (((void **)(uintptr_t)ID32_DECODE(id))[0]);
116*7c478bd9Sstevel@tonic-gate }
117