xref: /xnu-8792.61.2/osfmk/ipc/ipc_entry.h (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1 /*
2  * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * @OSF_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34  * All Rights Reserved.
35  *
36  * Permission to use, copy, modify and distribute this software and its
37  * documentation is hereby granted, provided that both the copyright
38  * notice and this permission notice appear in all copies of the
39  * software, derivative works or modified versions, and any portions
40  * thereof, and that both notices appear in supporting documentation.
41  *
42  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45  *
46  * Carnegie Mellon requests users of this software to return to
47  *
48  *  Software Distribution Coordinator  or  [email protected]
49  *  School of Computer Science
50  *  Carnegie Mellon University
51  *  Pittsburgh PA 15213-3890
52  *
53  * any improvements or extensions that they make and grant Carnegie Mellon
54  * the rights to redistribute these changes.
55  */
56 /*
57  */
58 /*
59  *	File:	ipc/ipc_entry.h
60  *	Author:	Rich Draves
61  *	Date:	1989
62  *
63  *	Definitions for translation entries, which represent
64  *	tasks' capabilities for ports and port sets.
65  */
66 
67 #ifndef _IPC_IPC_ENTRY_H_
68 #define _IPC_IPC_ENTRY_H_
69 
70 #include <mach/mach_types.h>
71 #include <mach/port.h>
72 #include <mach/kern_return.h>
73 
74 #include <kern/kern_types.h>
75 #include <kern/kalloc.h>
76 
77 #include <ipc/ipc_types.h>
78 
79 /*
80  *	Spaces hold capabilities for ipc_object_t's.
81  *	Each ipc_entry_t records a capability.  Most capabilities have
82  *	small names, and the entries are elements of a table.
83  *
84  *	The ie_index field of entries in the table implements
85  *	a ordered hash table with open addressing and linear probing.
86  *
87  *	The ie_dist field holds the distance to the desired spot,
88  *	which is used to implement robin-hood hashing.
89  *
90  *	This hash table converts (space, object) -> name.
91  *	It is used independently of the other fields.
92  *
93  *	Free (unallocated) entries in the table have null ie_object
94  *	fields.  The ie_bits field is zero except for IE_BITS_GEN.
95  *	The ie_next (ie_request) field links free entries into a free list.
96  *
97  *	The first entry in the table (index 0) is always free.
98  *	It is used as the head of the free list.
99  */
100 
101 #define IPC_ENTRY_DIST_BITS   12
102 #define IPC_ENTRY_DIST_MAX    ((1 << IPC_ENTRY_DIST_BITS) - 1)
103 #ifdef __LP64__
104 #define IPC_ENTRY_INDEX_BITS  32
105 #define IPC_ENTRY_INDEX_MAX   (UINT32_MAX)
106 #else
107 #define IPC_ENTRY_INDEX_BITS  20
108 #define IPC_ENTRY_INDEX_MAX   ((1 << IPC_ENTRY_INDEX_BITS) - 1)
109 #endif
110 
111 struct ipc_entry {
112 	union {
113 		struct ipc_object *XNU_PTRAUTH_SIGNED_PTR("ipc_entry.ie_object") ie_object;
114 		struct ipc_object *XNU_PTRAUTH_SIGNED_PTR("ipc_entry.ie_object") volatile ie_volatile_object;
115 	};
116 	ipc_entry_bits_t    ie_bits;
117 	uint32_t            ie_dist  : IPC_ENTRY_DIST_BITS;
118 	mach_port_index_t   ie_index : IPC_ENTRY_INDEX_BITS;
119 	union {
120 		mach_port_index_t ie_next;         /* next in freelist, or...  */
121 		ipc_table_index_t ie_request;      /* dead name request notify */
122 	};
123 };
124 
125 #define IPC_ENTRY_TABLE_MIN     32
126 #define IPC_ENTRY_TABLE_PERIOD  16
127 KALLOC_ARRAY_TYPE_DECL(ipc_entry_table, struct ipc_entry);
128 
129 #define IE_REQ_NONE             0               /* no request */
130 
131 #define IE_BITS_UREFS_MASK      0x0000ffff      /* 16 bits of user-reference */
132 #define IE_BITS_UREFS(bits)     ((bits) & IE_BITS_UREFS_MASK)
133 
134 #define IE_BITS_TYPE_MASK       0x001f0000      /* 5 bits of capability type */
135 #define IE_BITS_TYPE(bits)      ((bits) & IE_BITS_TYPE_MASK)
136 
137 #define IE_BITS_EXTYPE_MASK     0x00200000      /* 1 bit for extended capability */
138 
139 #ifndef NO_PORT_GEN
140 #define IE_BITS_GEN_MASK        0xff000000      /* 8 bits for generation */
141 #define IE_BITS_GEN(bits)       ((bits) & IE_BITS_GEN_MASK)
142 #define IE_BITS_GEN_ONE         0x04000000      /* low bit of generation */
143 #define IE_BITS_ROLL_POS        22              /* LSB pos of generation rollover */
144 #define IE_BITS_ROLL_BITS       2               /* number of generation rollover bits */
145 #define IE_BITS_ROLL_MASK       (((1 << IE_BITS_ROLL_BITS) - 1) << IE_BITS_ROLL_POS)
146 #define IE_BITS_ROLL(bits)      ((((bits) & IE_BITS_ROLL_MASK) << 8) ^ IE_BITS_GEN_MASK)
147 
148 /*
149  * Restart a generation counter with the specified bits for the rollover point.
150  * There are 4 different rollover points:
151  * bits    rollover period
152  * 0 0     64
153  * 0 1     48
154  * 1 0     32
155  * 1 1     16
156  */
157 static inline ipc_entry_bits_t
ipc_entry_new_rollpoint(ipc_entry_bits_t rollbits)158 ipc_entry_new_rollpoint(
159 	ipc_entry_bits_t rollbits)
160 {
161 	rollbits = (rollbits << IE_BITS_ROLL_POS) & IE_BITS_ROLL_MASK;
162 	ipc_entry_bits_t newgen = IE_BITS_GEN_MASK + IE_BITS_GEN_ONE;
163 	return newgen | rollbits;
164 }
165 
166 /*
167  * Get the next gencount, modulo the entry's rollover point. If the sum rolls over,
168  * the caller should re-start the generation counter with a different rollpoint.
169  */
170 static inline ipc_entry_bits_t
ipc_entry_new_gen(ipc_entry_bits_t oldgen)171 ipc_entry_new_gen(
172 	ipc_entry_bits_t oldgen)
173 {
174 	ipc_entry_bits_t sum  = (oldgen + IE_BITS_GEN_ONE) & IE_BITS_GEN_MASK;
175 	ipc_entry_bits_t roll = oldgen & IE_BITS_ROLL_MASK;
176 	ipc_entry_bits_t newgen = (sum % IE_BITS_ROLL(oldgen)) | roll;
177 	return newgen;
178 }
179 
180 /* Determine if a gencount has rolled over or not. */
181 static inline boolean_t
ipc_entry_gen_rolled(ipc_entry_bits_t oldgen,ipc_entry_bits_t newgen)182 ipc_entry_gen_rolled(
183 	ipc_entry_bits_t oldgen,
184 	ipc_entry_bits_t newgen)
185 {
186 	return (oldgen & IE_BITS_GEN_MASK) > (newgen & IE_BITS_GEN_MASK);
187 }
188 
189 #else
190 #define IE_BITS_GEN_MASK        0
191 #define IE_BITS_GEN(bits)       0
192 #define IE_BITS_GEN_ONE         0
193 #define IE_BITS_ROLL_POS        0
194 #define IE_BITS_ROLL_MASK       0
195 #define IE_BITS_ROLL(bits)      (bits)
196 
197 static inline ipc_entry_bits_t
ipc_entry_new_rollpoint(ipc_entry_bits_t rollbits)198 ipc_entry_new_rollpoint(
199 	ipc_entry_bits_t rollbits)
200 {
201 	return 0;
202 }
203 
204 static inline ipc_entry_bits_t
ipc_entry_new_gen(ipc_entry_bits_t oldgen)205 ipc_entry_new_gen(
206 	ipc_entry_bits_t oldgen)
207 {
208 	return 0;
209 }
210 
211 static inline boolean_t
ipc_entry_gen_rolled(ipc_entry_bits_t oldgen,ipc_entry_bits_t newgen)212 ipc_entry_gen_rolled(
213 	ipc_entry_bits_t oldgen,
214 	ipc_entry_bits_t newgen)
215 {
216 	return FALSE;
217 }
218 
219 #endif  /* !USE_PORT_GEN */
220 
221 #define IE_BITS_RIGHT_MASK      0x007fffff      /* relevant to the right */
222 /*
223  * Exported interfaces
224  */
225 
226 extern unsigned int ipc_entry_table_count_max(void) __pure2;
227 
228 /* Search for entry in a space by name */
229 extern ipc_entry_t ipc_entry_lookup(
230 	ipc_space_t             space,
231 	mach_port_name_t        name);
232 
233 /* Hold a number of entries in a locked space */
234 extern kern_return_t ipc_entries_hold(
235 	ipc_space_t             space,
236 	natural_t               count);
237 
238 /* claim and initialize a held entry in a locked space */
239 extern kern_return_t ipc_entry_claim(
240 	ipc_space_t             space,
241 	ipc_object_t            object,
242 	mach_port_name_t        *namep,
243 	ipc_entry_t             *entryp);
244 
245 /* Allocate an entry in a space, growing the space if necessary */
246 extern kern_return_t ipc_entry_alloc(
247 	ipc_space_t             space,
248 	ipc_object_t            object,
249 	mach_port_name_t        *namep,
250 	ipc_entry_t             *entryp);
251 
252 /* Allocate/find an entry in a space with a specific name */
253 extern kern_return_t ipc_entry_alloc_name(
254 	ipc_space_t             space,
255 	mach_port_name_t        name,
256 	ipc_entry_t             *entryp);
257 
258 /* Deallocate an entry from a space */
259 extern void ipc_entry_dealloc(
260 	ipc_space_t             space,
261 	ipc_object_t            object,
262 	mach_port_name_t        name,
263 	ipc_entry_t             entry);
264 
265 /* Mark and entry modified in a space */
266 extern void ipc_entry_modified(
267 	ipc_space_t             space,
268 	mach_port_name_t        name,
269 	ipc_entry_t             entry);
270 
271 /* Grow the table in a space */
272 extern kern_return_t ipc_entry_grow_table(
273 	ipc_space_t             space,
274 	ipc_table_elems_t       target_size);
275 
276 /* mask on/off default entry generation bits */
277 extern mach_port_name_t ipc_entry_name_mask(
278 	mach_port_name_t name);
279 #endif  /* _IPC_IPC_ENTRY_H_ */
280