Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | /* * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* * @OSF_COPYRIGHT@ */ /* * Mach Operating System * Copyright (c) 1991,1990,1989 Carnegie Mellon University * All Rights Reserved. * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie Mellon * the rights to redistribute these changes. */ /* */ /* * File: ipc/ipc_object.h * Author: Rich Draves * Date: 1989 * * Definitions for IPC objects, for which tasks have capabilities. */ #ifndef _IPC_IPC_OBJECT_H_ #define _IPC_IPC_OBJECT_H_ #include <mach_rt.h> #include <cpus.h> #include <mach_kdb.h> #include <mach/kern_return.h> #include <mach/message.h> #include <kern/lock.h> #include <kern/macro_help.h> #include <kern/zalloc.h> #include <ipc/ipc_types.h> typedef natural_t ipc_object_refs_t; /* for ipc/ipc_object.h */ typedef natural_t ipc_object_bits_t; typedef natural_t ipc_object_type_t; /* * There is no lock in the ipc_object; it is in the enclosing kernel * data structure (rpc_common_data) used by both ipc_port and ipc_pset. * The ipc_object is used to both tag and reference count these two data * structures, and (Noto Bene!) pointers to either of these or the * ipc_object at the head of these are freely cast back and forth; hence * the ipc_object MUST BE FIRST in the ipc_common_data. * * If the RPC implementation enabled user-mode code to use kernel-level * data structures (as ours used to), this peculiar structuring would * avoid having anything in user code depend on the kernel configuration * (with which lock size varies). */ struct ipc_object { ipc_object_refs_t io_references; ipc_object_bits_t io_bits; port_name_t io_receiver_name; #if NCPUS == 1 usimple_lock_data_t io_lock_data; #else decl_mutex_data(, io_lock_data) #endif }; /* * Legacy defines. Should use IPC_OBJECT_NULL, etc... */ #define IO_NULL ((ipc_object_t) 0) #define IO_DEAD ((ipc_object_t) -1) #define IO_VALID(io) (((io) != IO_NULL) && ((io) != IO_DEAD)) /* * IPC steals the high-order bits from the kotype to use * for its own purposes. This allows IPC to record facts * about ports that aren't otherwise obvious from the * existing port fields. In particular, IPC can optionally * mark a port for no more senders detection. Any change * to IO_BITS_PORT_INFO must be coordinated with bitfield * definitions in ipc_port.h. */ #define IO_BITS_PORT_INFO 0x0000f000 /* stupid port tricks */ #define IO_BITS_KOTYPE 0x00000fff /* used by the object */ #define IO_BITS_OTYPE 0x7fff0000 /* determines a zone */ #define IO_BITS_ACTIVE 0x80000000 /* is object alive? */ #define io_active(io) ((io)->io_bits & IO_BITS_ACTIVE) #define io_otype(io) (((io)->io_bits & IO_BITS_OTYPE) >> 16) #define io_kotype(io) ((io)->io_bits & IO_BITS_KOTYPE) #define io_makebits(active, otype, kotype) \ (((active) ? IO_BITS_ACTIVE : 0) | ((otype) << 16) | (kotype)) /* * Object types: ports, port sets, kernel-loaded ports */ #define IOT_PORT 0 #define IOT_PORT_SET 1 #define IOT_NUMBER 2 /* number of types used */ extern zone_t ipc_object_zones[IOT_NUMBER]; #define io_alloc(otype) \ ((ipc_object_t) zalloc(ipc_object_zones[(otype)])) #if MACH_ASSERT /* * Call the routine for io_free so that checking can be performed. */ extern void io_free( unsigned int otype, ipc_object_t object); #else /* MACH_ASSERT */ #define io_free(otype, io) \ zfree(ipc_object_zones[(otype)], (vm_offset_t) (io)) #endif /* MACH_ASSERT */ /* * Here we depend on the ipc_object being first within the ipc_common_data, * which is first within the rpc_common_data, which in turn must be first * within any kernel data structure needing to lock an ipc_object * (ipc_port and ipc_pset). */ #if NCPUS == 1 #define io_lock_init(io) \ usimple_lock_init(&(io)-io_lock_data, ETAP_IPC_OBJECT) #define io_lock(io) \ usimple_lock(&(io)->io_lock_data) #define io_lock_try(io) \ usimple_lock_try(&(io)->io_lock_data) #define io_unlock(io) \ usimple_unlock(&(io)->io_lock_data) #else /* NCPUS == 1 */ #define io_lock_init(io) \ mutex_init(&(io)->io_lock_data, ETAP_IPC_OBJECT) #define io_lock(io) \ mutex_lock(&(io)->io_lock_data) #define io_lock_try(io) \ mutex_try(&(io)->io_lock_data) #define io_unlock(io) \ mutex_unlock(&(io)->io_lock_data) #endif /* NCPUS == 1 */ #if NCPUS > 1 #define _VOLATILE_ volatile #else /* NCPUS > 1 */ #define _VOLATILE_ #endif /* NCPUS > 1 */ #define io_check_unlock(io) \ MACRO_BEGIN \ _VOLATILE_ ipc_object_refs_t _refs = (io)->io_references; \ \ io_unlock(io); \ if (_refs == 0) \ io_free(io_otype(io), io); \ MACRO_END /* Sanity check the ref count. If it is 0, we may be doubly zfreeing. * If it is larger than max int, it has been corrupted, probably by being * modified into an address (this is architecture dependent, but it's * safe to assume there cannot really be max int references). * * NOTE: The 0 test alone will not catch double zfreeing of ipc_port * structs, because the io_references field is the first word of the struct, * and zfree modifies that to point to the next free zone element. */ #define IO_MAX_REFERENCES \ (unsigned)(~0 ^ (1 << (sizeof(int)*BYTE_SIZE - 1))) #define io_reference(io) \ MACRO_BEGIN \ assert((io)->io_references < IO_MAX_REFERENCES); \ (io)->io_references++; \ MACRO_END #define io_release(io) \ MACRO_BEGIN \ assert((io)->io_references > 0 && \ (io)->io_references <= IO_MAX_REFERENCES); \ (io)->io_references--; \ MACRO_END /* * Exported interfaces */ /* Take a reference to an object */ extern void ipc_object_reference( ipc_object_t object); /* Release a reference to an object */ extern void ipc_object_release( ipc_object_t object); /* Look up an object in a space */ extern kern_return_t ipc_object_translate( ipc_space_t space, mach_port_name_t name, mach_port_right_t right, ipc_object_t *objectp); /* Look up two objects in a space, locking them in the order described */ extern kern_return_t ipc_object_translate_two( ipc_space_t space, mach_port_name_t name1, mach_port_right_t right1, ipc_object_t *objectp1, mach_port_name_t name2, mach_port_right_t right2, ipc_object_t *objectp2); /* Allocate a dead-name entry */ extern kern_return_t ipc_object_alloc_dead( ipc_space_t space, mach_port_name_t *namep); /* Allocate a dead-name entry, with a specific name */ extern kern_return_t ipc_object_alloc_dead_name( ipc_space_t space, mach_port_name_t name); /* Allocate an object */ extern kern_return_t ipc_object_alloc( ipc_space_t space, ipc_object_type_t otype, mach_port_type_t type, mach_port_urefs_t urefs, mach_port_name_t *namep, ipc_object_t *objectp); /* Allocate an object, with a specific name */ extern kern_return_t ipc_object_alloc_name( ipc_space_t space, ipc_object_type_t otype, mach_port_type_t type, mach_port_urefs_t urefs, mach_port_name_t name, ipc_object_t *objectp); /* Convert a send type name to a received type name */ extern mach_msg_type_name_t ipc_object_copyin_type( mach_msg_type_name_t msgt_name); /* Copyin a capability from a space */ extern kern_return_t ipc_object_copyin( ipc_space_t space, mach_port_name_t name, mach_msg_type_name_t msgt_name, ipc_object_t *objectp); /* Copyin a naked capability from the kernel */ extern void ipc_object_copyin_from_kernel( ipc_object_t object, mach_msg_type_name_t msgt_name); /* Destroy a naked capability */ extern void ipc_object_destroy( ipc_object_t object, mach_msg_type_name_t msgt_name); /* Copyout a capability, placing it into a space */ extern kern_return_t ipc_object_copyout( ipc_space_t space, ipc_object_t object, mach_msg_type_name_t msgt_name, boolean_t overflow, mach_port_name_t *namep); /* Copyout a capability with a name, placing it into a space */ extern kern_return_t ipc_object_copyout_name( ipc_space_t space, ipc_object_t object, mach_msg_type_name_t msgt_name, boolean_t overflow, mach_port_name_t name); /* Translate/consume the destination right of a message */ extern void ipc_object_copyout_dest( ipc_space_t space, ipc_object_t object, mach_msg_type_name_t msgt_name, mach_port_name_t *namep); /* Rename an entry in a space */ extern kern_return_t ipc_object_rename( ipc_space_t space, mach_port_name_t oname, mach_port_name_t nname); #if MACH_KDB /* Pretty-print an ipc object */ extern void ipc_object_print( ipc_object_t object); #endif /* MACH_KDB */ #endif /* _IPC_IPC_OBJECT_H_ */ |