1 /*
2 * Copyright 1991-1998 by Open Software Foundation, Inc.
3 * All Rights Reserved
4 *
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both the copyright notice and this permission notice appear in
9 * supporting documentation.
10 *
11 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
12 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
14 *
15 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
16 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
18 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
19 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21 /*
22 * MkLinux
23 */
24 /* CMU_HIST */
25 /*
26 * Revision 2.6 91/10/09 16:09:27 af
27 * Revision 2.5.2.1 91/09/16 10:15:46 rpd
28 * Added (unconditional) ipc_object_print declaration.
29 * [91/09/02 rpd]
30 *
31 * Revision 2.5.2.1 91/09/16 10:15:46 rpd
32 * Added (unconditional) ipc_object_print declaration.
33 * [91/09/02 rpd]
34 *
35 * Revision 2.5 91/05/14 16:35:04 mrt
36 * Correcting copyright
37 *
38 * Revision 2.4 91/02/05 17:22:53 mrt
39 * Changed to new Mach copyright
40 * [91/02/01 15:49:25 mrt]
41 *
42 * Revision 2.3 90/11/05 14:29:21 rpd
43 * Removed ipc_object_reference_macro, ipc_object_release_macro.
44 * Created new io_reference, io_release macros.
45 * [90/10/29 rpd]
46 *
47 * Revision 2.2 90/06/02 14:51:04 rpd
48 * Created for new IPC.
49 * [90/03/26 21:00:05 rpd]
50 *
51 */
52 /* CMU_ENDHIST */
53 /*
54 * Mach Operating System
55 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
56 * All Rights Reserved.
57 *
58 * Permission to use, copy, modify and distribute this software and its
59 * documentation is hereby granted, provided that both the copyright
60 * notice and this permission notice appear in all copies of the
61 * software, derivative works or modified versions, and any portions
62 * thereof, and that both notices appear in supporting documentation.
63 *
64 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
65 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
66 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
67 *
68 * Carnegie Mellon requests users of this software to return to
69 *
70 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
71 * School of Computer Science
72 * Carnegie Mellon University
73 * Pittsburgh PA 15213-3890
74 *
75 * any improvements or extensions that they make and grant Carnegie Mellon
76 * the rights to redistribute these changes.
77 */
78 /*
79 */
80 /*
81 * File: ipc/ipc_object.h
82 * Author: Rich Draves
83 * Date: 1989
84 *
85 * Definitions for IPC objects, for which tasks have capabilities.
86 */
87
88 #ifndef _IPC_IPC_OBJECT_H_
89 #define _IPC_IPC_OBJECT_H_
90
91 #include <vm/uma.h>
92 #include <sys/mach/kern_return.h>
93 #include <sys/mach/mach_types.h>
94 #include <sys/mach/message.h>
95 #include <sys/mach/ipc/ipc_types.h>
96 #include <sys/mach/ipc_common.h> /* for ipc_object data defs */
97
98 typedef struct ipc_object *ipc_object_t;
99
100 #define IO_NULL ((ipc_object_t) 0)
101 #define IO_DEAD ((ipc_object_t) -1)
102
103 #define IO_VALID(io) (((io) != IO_NULL) && ((io) != IO_DEAD))
104
105 /*
106 * IPC steals the high-order bits from the kotype to use
107 * for its own purposes. This allows IPC to record facts
108 * about ports that aren't otherwise obvious from the
109 * existing port fields. In particular, IPC can optionally
110 * mark a port for no more senders detection. Any change
111 * to IO_BITS_PORT_INFO must be coordinated with bitfield
112 * definitions in ipc_port.h.
113 */
114 #define IO_BITS_PORT_INFO 0x0000f000 /* stupid port tricks */
115 #define IO_BITS_KOTYPE 0x00000fff /* used by the object */
116 #define IO_BITS_OTYPE 0x7fff0000 /* determines a zone */
117 #define IO_BITS_ACTIVE 0x80000000 /* is object alive? */
118
119 #define io_active(io) ((io)->io_bits & IO_BITS_ACTIVE)
120 /* 1.3b26 hacked this to .. < 0 why? */
121
122 #define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16)
123 #define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE)
124
125 #define io_makebits(active, otype, kotype) \
126 (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype))
127
128 /*
129 * Object types: ports, port sets, kernel-loaded ports
130 */
131 #define IOT_PORT 0
132 #define IOT_PORT_SET 1
133 #define IOT_NUMBER 2 /* number of types used */
134 extern uma_zone_t ipc_object_zones[IOT_NUMBER];
135 #define io_alloc(otype) \
136 ((ipc_object_t) uma_zalloc(ipc_object_zones[(otype)], M_WAITOK))
137
138 #define io_free(otype, io) \
139 uma_zfree(ipc_object_zones[(otype)], (io))
140 /*
141 * Here we depend on the ipc_object being first within the ipc_common_data,
142 * which is first within the rpc_common_data, which in turn must be first
143 * within any kernel data structure needing to lock an ipc_object
144 * (ipc_port and ipc_pset).
145 */
146
147 #define io_lock_init(io) \
148 mtx_init(&((rpc_common_t)(io))->rcd_io_lock_data, "ETAP_IPC_RPC", NULL, MTX_DEF|MTX_DUPOK)
149 #define io_lock(io) \
150 mtx_lock(&((rpc_common_t)(io))->rcd_io_lock_data)
151 #define io_lock_owned(io) \
152 mtx_owned(&((rpc_common_t)(io))->rcd_io_lock_data)
153 #define io_lock_try(io) \
154 mtx_trylock(&((rpc_common_t)(io))->rcd_io_lock_data)
155 #define io_unlock(io) \
156 mtx_unlock(&((rpc_common_t)(io))->rcd_io_lock_data)
157 #define io_unlock_assert(io) mtx_assert(&((rpc_common_t)(io))->rcd_io_lock_data, MA_NOTOWNED)
158 #define io_lock_assert(io) mtx_assert(&((rpc_common_t)(io))->rcd_io_lock_data, MA_OWNED)
159
160 #ifdef SMP
161 #define _VOLATILE_ volatile
162 #else /* NCPUS > 1 */
163 #define _VOLATILE_
164 #endif /* NCPUS > 1 */
165
166 #define VERBOSE_DEBUGGING 0
167
168 /* Sanity check the ref count. If it is 0, we may be doubly zfreeing.
169 * If it is larger than max int, it has been corrupted, probably by being
170 * modified into an address (this is architecture dependent, but it's
171 * safe to assume there cannot really be max int references).
172 *
173 * NOTE: The 0 test alone will not catch double zfreeing of ipc_port
174 * structs, because the io_references field is the first word of the struct,
175 * and zfree modifies that to point to the next free zone element.
176 */
177 #define IO_MAX_REFERENCES \
178 (unsigned)(~0 ^ (1 << (sizeof(int)*BYTE_SIZE - 1)))
179
180
181 extern void io_validate(ipc_object_t io);
182
183 static inline void
_io_reference(ipc_object_t io,char * file,int line)184 _io_reference(ipc_object_t io, char *file, int line) {
185
186 assert((io)->io_references > 0);
187 assert((io)->io_references < IO_MAX_REFERENCES);
188 atomic_add_int(&(io)->io_references, 1);
189 #if VERBOSE_DEBUGGING
190 if (io->io_references < 5)
191 printf("ref %p refs: %d %s:%d\n", io, io->io_references, file, line);
192 #endif
193 }
194
195 static inline void
_io_release(ipc_object_t io,char * file,int line)196 _io_release(ipc_object_t io, char* file, int line) {
197
198 #if VERBOSE_DEBUGGING
199 if (io->io_references < 5)
200 printf("rel %p refs: %d %s:%d\n", io, io->io_references, file, line);
201 #endif
202 assert((io)->io_references > 0);
203 MACH_VERIFY((io)->io_references < IO_MAX_REFERENCES, ("io_references: %d\n", (io)->io_references));
204 assert((io)->io_references < IO_MAX_REFERENCES);
205 if (atomic_fetchadd_int(&(io)->io_references, -1) == 1) {
206 #ifdef INVARIANTS
207 io_validate(io);
208 #endif
209 io_free(io_otype(io), io);
210 }
211 }
212
213 #define io_release(io) _io_release(io, __FILE__, __LINE__)
214 #define io_reference(io) _io_reference(io, __FILE__, __LINE__)
215
216 #define ipc_object_reference(io) io_reference(io)
217 #define ipc_object_release(io) io_release(io)
218
219 /*
220 * Exported interfaces
221 */
222
223 /* Look up an object in a space */
224 extern kern_return_t ipc_object_translate(
225 ipc_space_t space,
226 mach_port_name_t name,
227 mach_port_right_t right,
228 ipc_object_t *objectp);
229
230 /* Allocate a dead-name entry */
231 extern kern_return_t
232 ipc_object_alloc_dead(
233 ipc_space_t space,
234 mach_port_name_t *namep);
235
236 /* Allocate a dead-name entry, with a specific name */
237 extern kern_return_t ipc_object_alloc_dead_name(
238 ipc_space_t space,
239 mach_port_name_t name);
240
241 /* Allocate an object */
242 extern kern_return_t ipc_object_alloc(
243 ipc_space_t space,
244 ipc_object_type_t otype,
245 mach_port_type_t type,
246 mach_port_name_t *namep,
247 ipc_object_t *objectp);
248
249 /* Allocate an object, with a specific name */
250 extern kern_return_t ipc_object_alloc_name(
251 ipc_space_t space,
252 ipc_object_type_t otype,
253 mach_port_type_t type,
254 mach_port_name_t name,
255 ipc_object_t *objectp);
256
257 /* Convert a send type name to a received type name */
258 extern mach_msg_type_name_t ipc_object_copyin_type(
259 mach_msg_type_name_t msgt_name);
260
261 /* Copyin a capability from a space */
262 extern kern_return_t ipc_object_copyin(
263 ipc_space_t space,
264 mach_port_name_t name,
265 mach_msg_type_name_t msgt_name,
266 ipc_object_t *objectp);
267
268 /* Copyin a naked capability from the kernel */
269 extern void ipc_object_copyin_from_kernel(
270 ipc_object_t object,
271 mach_msg_type_name_t msgt_name);
272
273 /* Destroy a naked capability */
274 extern void ipc_object_destroy(
275 ipc_object_t object,
276 mach_msg_type_name_t msgt_name);
277
278 /* Copyout a capability, placing it into a space */
279 extern kern_return_t ipc_object_copyout(
280 ipc_space_t space,
281 ipc_object_t object,
282 mach_msg_type_name_t msgt_name,
283 mach_port_name_t *namep);
284
285 /* Copyout a capability with a name, placing it into a space */
286 extern kern_return_t ipc_object_copyout_name(
287 ipc_space_t space,
288 ipc_object_t object,
289 mach_msg_type_name_t msgt_name,
290 mach_port_name_t name);
291
292 /* Translate/consume the destination right of a message */
293 extern void ipc_object_copyout_dest(
294 ipc_space_t space,
295 ipc_object_t object,
296 mach_msg_type_name_t msgt_name,
297 mach_port_name_t *namep);
298
299 /* Rename an entry in a space */
300 extern kern_return_t ipc_object_rename(
301 ipc_space_t space,
302 mach_port_name_t oname,
303 mach_port_name_t nname);
304
305
306 #if MACH_KDB
307 /* Pretty-print an ipc object */
308
309 extern void ipc_object_print(
310 ipc_object_t object);
311
312 #endif /* MACH_KDB */
313
314 #endif /* _IPC_IPC_OBJECT_H_ */
315