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 #include <sys/vmem.h>
28*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
32*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gate static vmem_t *id32_arena;
35*7c478bd9Sstevel@tonic-gate static kmem_cache_t *id32_cache;
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate #define ID32_BITS 5
38*7c478bd9Sstevel@tonic-gate #define ID32_ALIGN (1 << ID32_BITS)
39*7c478bd9Sstevel@tonic-gate #define ID32_MOD (ID32_ALIGN - 1)
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate #if defined(__amd64)
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate * For amd64 the 32 bit id is the offset of the entry in the arena.
44*7c478bd9Sstevel@tonic-gate */
45*7c478bd9Sstevel@tonic-gate extern char *heap_core_base;
46*7c478bd9Sstevel@tonic-gate #define ID32_ENCODE(x) (((x) - (uintptr_t)heap_core_base) | \
47*7c478bd9Sstevel@tonic-gate ((((x) - (uintptr_t)heap_core_base) % ID32_MOD)) + 1)
48*7c478bd9Sstevel@tonic-gate #define ID32_DECODE(x) (P2ALIGN((x), (uintptr_t)ID32_ALIGN) + \
49*7c478bd9Sstevel@tonic-gate (uintptr_t)heap_core_base)
50*7c478bd9Sstevel@tonic-gate #define ID32_VALID(x) (ID32_ENCODE(ID32_DECODE(x)) == (x))
51*7c478bd9Sstevel@tonic-gate #else /* __amd64 */
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate * All other architectures use the 32 bit pointer value for the 32 bit id.
54*7c478bd9Sstevel@tonic-gate */
55*7c478bd9Sstevel@tonic-gate #define ID32_ENCODE(x) (((x) | ((x) % ID32_MOD)) + 1)
56*7c478bd9Sstevel@tonic-gate #define ID32_DECODE(x) P2ALIGN((x), ID32_ALIGN)
57*7c478bd9Sstevel@tonic-gate #define ID32_VALID(x) (ID32_ENCODE(ID32_DECODE(x)) == (x))
58*7c478bd9Sstevel@tonic-gate #endif /* __amd64 */
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate void
id32_init(void)61*7c478bd9Sstevel@tonic-gate id32_init(void)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate id32_arena = vmem_create("id32", NULL, 0, PAGESIZE,
64*7c478bd9Sstevel@tonic-gate segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate id32_cache = kmem_cache_create("id32_cache", ID32_ALIGN, ID32_ALIGN,
67*7c478bd9Sstevel@tonic-gate NULL, NULL, NULL, NULL, id32_arena, 0);
68*7c478bd9Sstevel@tonic-gate }
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate * Return a 32-bit identifier for the specified pointer.
72*7c478bd9Sstevel@tonic-gate */
73*7c478bd9Sstevel@tonic-gate uint32_t
id32_alloc(void * ptr,int kmflag)74*7c478bd9Sstevel@tonic-gate id32_alloc(void *ptr, int kmflag)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate void **hent = kmem_cache_alloc(id32_cache, kmflag);
77*7c478bd9Sstevel@tonic-gate uintptr_t id;
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate if (hent == NULL)
80*7c478bd9Sstevel@tonic-gate return (0);
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate *hent = ptr;
83*7c478bd9Sstevel@tonic-gate id = ID32_ENCODE((uintptr_t)hent);
84*7c478bd9Sstevel@tonic-gate ASSERT64(id <= UINT32_MAX);
85*7c478bd9Sstevel@tonic-gate return ((uint32_t)id);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate * Free a 32-bit ID.
90*7c478bd9Sstevel@tonic-gate */
91*7c478bd9Sstevel@tonic-gate void
id32_free(uint32_t id)92*7c478bd9Sstevel@tonic-gate id32_free(uint32_t id)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate if (!ID32_VALID(id)) {
95*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "id32_free(%x): bad ID rejected\n", id);
96*7c478bd9Sstevel@tonic-gate return;
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate kmem_cache_free(id32_cache, (void *)(uintptr_t)ID32_DECODE(id));
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate /*
103*7c478bd9Sstevel@tonic-gate * Return the pointer described by a 32-bit ID, or NULL if the ID is bad.
104*7c478bd9Sstevel@tonic-gate */
105*7c478bd9Sstevel@tonic-gate void *
id32_lookup(uint32_t id)106*7c478bd9Sstevel@tonic-gate id32_lookup(uint32_t id)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate if (!ID32_VALID(id)) {
109*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "id32_lookup(%x): bad ID rejected\n", id);
110*7c478bd9Sstevel@tonic-gate return (NULL);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate return (((void **)(uintptr_t)ID32_DECODE(id))[0]);
114*7c478bd9Sstevel@tonic-gate }
115