]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_object.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_object.c
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0a7de745 31/*
1c79356b
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
1c79356b
A
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.
0a7de745 41 *
1c79356b
A
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.
0a7de745 45 *
1c79356b 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
1c79356b
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
0a7de745 52 *
1c79356b
A
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
2d21ac55
A
56/*
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 * Copyright (c) 2005-2006 SPARTA, Inc.
62 */
1c79356b
A
63/*
64 */
65/*
66 * File: ipc/ipc_object.c
67 * Author: Rich Draves
68 * Date: 1989
69 *
70 * Functions to manipulate IPC objects.
71 */
72
91447636 73#include <mach/mach_types.h>
1c79356b
A
74#include <mach/boolean.h>
75#include <mach/kern_return.h>
76#include <mach/port.h>
77#include <mach/message.h>
91447636
A
78
79#include <kern/kern_types.h>
1c79356b 80#include <kern/misc_protos.h>
2d21ac55 81#include <kern/ipc_kobject.h>
91447636
A
82
83#include <ipc/ipc_types.h>
fe8ab488 84#include <ipc/ipc_importance.h>
1c79356b
A
85#include <ipc/port.h>
86#include <ipc/ipc_space.h>
87#include <ipc/ipc_entry.h>
88#include <ipc/ipc_object.h>
89#include <ipc/ipc_hash.h>
90#include <ipc/ipc_right.h>
91#include <ipc/ipc_notify.h>
6d2010ae 92#include <ipc/ipc_port.h>
1c79356b 93#include <ipc/ipc_pset.h>
2d21ac55
A
94
95#include <security/mac_mach_internal.h>
1c79356b 96
f427ee49
A
97SECURITY_READ_ONLY_LATE(zone_t) ipc_object_zones[IOT_NUMBER];
98
99ZONE_INIT(&ipc_object_zones[IOT_PORT], "ipc ports", sizeof(struct ipc_port),
100 ZC_NOENCRYPT | ZC_CACHING | ZC_ZFREE_CLEARMEM | ZC_NOSEQUESTER,
101 ZONE_ID_IPC_PORT, NULL);
102
103ZONE_INIT(&ipc_object_zones[IOT_PORT_SET], "ipc port sets",
104 sizeof(struct ipc_pset),
105 ZC_NOENCRYPT | ZC_ZFREE_CLEARMEM | ZC_NOSEQUESTER,
106 ZONE_ID_IPC_PORT_SET, NULL);
1c79356b
A
107
108/*
109 * Routine: ipc_object_reference
110 * Purpose:
111 * Take a reference to an object.
112 */
113
114void
115ipc_object_reference(
0a7de745 116 ipc_object_t object)
1c79356b 117{
1c79356b 118 io_reference(object);
1c79356b
A
119}
120
121/*
122 * Routine: ipc_object_release
123 * Purpose:
124 * Release a reference to an object.
125 */
126
127void
128ipc_object_release(
0a7de745 129 ipc_object_t object)
1c79356b 130{
1c79356b 131 io_release(object);
1c79356b
A
132}
133
134/*
135 * Routine: ipc_object_translate
136 * Purpose:
137 * Look up an object in a space.
138 * Conditions:
139 * Nothing locked before. If successful, the object
cb323159 140 * is returned active and locked. The caller doesn't get a ref.
1c79356b
A
141 * Returns:
142 * KERN_SUCCESS Object returned locked.
143 * KERN_INVALID_TASK The space is dead.
00867663
A
144 * KERN_INVALID_NAME The name doesn't denote a right
145 * KERN_INVALID_RIGHT Name doesn't denote the correct right
1c79356b 146 */
1c79356b
A
147kern_return_t
148ipc_object_translate(
0a7de745
A
149 ipc_space_t space,
150 mach_port_name_t name,
151 mach_port_right_t right,
152 ipc_object_t *objectp)
1c79356b
A
153{
154 ipc_entry_t entry;
155 ipc_object_t object;
156 kern_return_t kr;
157
cb323159
A
158 if (!MACH_PORT_RIGHT_VALID_TRANSLATE(right)) {
159 return KERN_INVALID_RIGHT;
160 }
161
1c79356b 162 kr = ipc_right_lookup_read(space, name, &entry);
0a7de745 163 if (kr != KERN_SUCCESS) {
1c79356b 164 return kr;
0a7de745 165 }
1c79356b
A
166 /* space is read-locked and active */
167
168 if ((entry->ie_bits & MACH_PORT_TYPE(right)) == MACH_PORT_TYPE_NONE) {
169 is_read_unlock(space);
170 return KERN_INVALID_RIGHT;
171 }
172
173 object = entry->ie_object;
174 assert(object != IO_NULL);
175
176 io_lock(object);
177 is_read_unlock(space);
178
cb323159
A
179 if (!io_active(object)) {
180 io_unlock(object);
181 return KERN_INVALID_NAME;
182 }
183
1c79356b
A
184 *objectp = object;
185 return KERN_SUCCESS;
186}
187
188/*
189 * Routine: ipc_object_translate_two
190 * Purpose:
191 * Look up two objects in a space.
192 * Conditions:
193 * Nothing locked before. If successful, the objects
194 * are returned locked. The caller doesn't get a ref.
195 * Returns:
196 * KERN_SUCCESS Objects returned locked.
197 * KERN_INVALID_TASK The space is dead.
198 * KERN_INVALID_NAME A name doesn't denote a right.
199 * KERN_INVALID_RIGHT A name doesn't denote the correct right.
200 */
201
202kern_return_t
203ipc_object_translate_two(
0a7de745
A
204 ipc_space_t space,
205 mach_port_name_t name1,
206 mach_port_right_t right1,
207 ipc_object_t *objectp1,
208 mach_port_name_t name2,
209 mach_port_right_t right2,
210 ipc_object_t *objectp2)
1c79356b
A
211{
212 ipc_entry_t entry1;
213 ipc_entry_t entry2;
cb323159 214 ipc_object_t object1, object2;
1c79356b 215 kern_return_t kr;
cb323159 216 boolean_t doguard = TRUE;
1c79356b
A
217
218 kr = ipc_right_lookup_two_read(space, name1, &entry1, name2, &entry2);
0a7de745 219 if (kr != KERN_SUCCESS) {
1c79356b 220 return kr;
0a7de745 221 }
1c79356b
A
222 /* space is read-locked and active */
223
224 if ((entry1->ie_bits & MACH_PORT_TYPE(right1)) == MACH_PORT_TYPE_NONE) {
cb323159
A
225 /* If looking for receive, and the entry used to hold one, give a pass on EXC_GUARD */
226 if ((right1 & MACH_PORT_RIGHT_RECEIVE) == MACH_PORT_RIGHT_RECEIVE &&
227 (entry1->ie_bits & MACH_PORT_TYPE_EX_RECEIVE) == MACH_PORT_TYPE_EX_RECEIVE) {
228 doguard = FALSE;
229 }
1c79356b 230 is_read_unlock(space);
cb323159
A
231 if (doguard) {
232 mach_port_guard_exception(name1, 0, 0, kGUARD_EXC_INVALID_RIGHT);
233 }
1c79356b
A
234 return KERN_INVALID_RIGHT;
235 }
236
237 if ((entry2->ie_bits & MACH_PORT_TYPE(right2)) == MACH_PORT_TYPE_NONE) {
cb323159
A
238 /* If looking for receive, and the entry used to hold one, give a pass on EXC_GUARD */
239 if ((right2 & MACH_PORT_RIGHT_RECEIVE) == MACH_PORT_RIGHT_RECEIVE &&
240 (entry2->ie_bits & MACH_PORT_TYPE_EX_RECEIVE) == MACH_PORT_TYPE_EX_RECEIVE) {
241 doguard = FALSE;
242 }
1c79356b 243 is_read_unlock(space);
cb323159
A
244 if (doguard) {
245 mach_port_guard_exception(name2, 0, 0, kGUARD_EXC_INVALID_RIGHT);
246 }
1c79356b
A
247 return KERN_INVALID_RIGHT;
248 }
249
cb323159
A
250 object1 = entry1->ie_object;
251 assert(object1 != IO_NULL);
252 io_lock(object1);
253 if (!io_active(object1)) {
254 io_unlock(object1);
255 is_read_unlock(space);
256 return KERN_INVALID_NAME;
257 }
1c79356b 258
cb323159
A
259 object2 = entry2->ie_object;
260 assert(object2 != IO_NULL);
261 io_lock(object2);
262 if (!io_active(object2)) {
263 io_unlock(object1);
264 io_unlock(object2);
265 is_read_unlock(space);
266 return KERN_INVALID_NAME;
267 }
268
269 *objectp1 = object1;
270 *objectp2 = object2;
1c79356b
A
271
272 is_read_unlock(space);
273 return KERN_SUCCESS;
274}
275
276/*
277 * Routine: ipc_object_alloc_dead
278 * Purpose:
279 * Allocate a dead-name entry.
280 * Conditions:
281 * Nothing locked.
282 * Returns:
283 * KERN_SUCCESS The dead name is allocated.
284 * KERN_INVALID_TASK The space is dead.
285 * KERN_NO_SPACE No room for an entry in the space.
286 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
287 */
288
289kern_return_t
290ipc_object_alloc_dead(
0a7de745
A
291 ipc_space_t space,
292 mach_port_name_t *namep)
1c79356b
A
293{
294 ipc_entry_t entry;
295 kern_return_t kr;
296
1c79356b 297 kr = ipc_entry_alloc(space, namep, &entry);
0a7de745 298 if (kr != KERN_SUCCESS) {
1c79356b 299 return kr;
0a7de745 300 }
1c79356b
A
301 /* space is write-locked */
302
303 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
304
305 assert(entry->ie_object == IO_NULL);
306 entry->ie_bits |= MACH_PORT_TYPE_DEAD_NAME | 1;
316670eb 307 ipc_entry_modified(space, *namep, entry);
1c79356b
A
308 is_write_unlock(space);
309 return KERN_SUCCESS;
310}
311
312/*
313 * Routine: ipc_object_alloc_dead_name
314 * Purpose:
315 * Allocate a dead-name entry, with a specific name.
316 * Conditions:
317 * Nothing locked.
318 * Returns:
319 * KERN_SUCCESS The dead name is allocated.
320 * KERN_INVALID_TASK The space is dead.
321 * KERN_NAME_EXISTS The name already denotes a right.
322 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
323 */
324
325kern_return_t
326ipc_object_alloc_dead_name(
0a7de745
A
327 ipc_space_t space,
328 mach_port_name_t name)
1c79356b
A
329{
330 ipc_entry_t entry;
331 kern_return_t kr;
332
1c79356b 333 kr = ipc_entry_alloc_name(space, name, &entry);
0a7de745 334 if (kr != KERN_SUCCESS) {
1c79356b 335 return kr;
0a7de745 336 }
1c79356b
A
337 /* space is write-locked */
338
0a7de745 339 if (ipc_right_inuse(space, name, entry)) {
1c79356b 340 return KERN_NAME_EXISTS;
0a7de745 341 }
1c79356b
A
342
343 /* null object, MACH_PORT_TYPE_DEAD_NAME, 1 uref */
344
345 assert(entry->ie_object == IO_NULL);
346 entry->ie_bits |= MACH_PORT_TYPE_DEAD_NAME | 1;
316670eb 347 ipc_entry_modified(space, name, entry);
1c79356b
A
348 is_write_unlock(space);
349 return KERN_SUCCESS;
350}
351
352/*
353 * Routine: ipc_object_alloc
354 * Purpose:
355 * Allocate an object.
356 * Conditions:
357 * Nothing locked. If successful, the object is returned locked.
0a7de745 358 * The space is write locked on successful return.
1c79356b
A
359 * The caller doesn't get a reference for the object.
360 * Returns:
361 * KERN_SUCCESS The object is allocated.
362 * KERN_INVALID_TASK The space is dead.
363 * KERN_NO_SPACE No room for an entry in the space.
364 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
365 */
366
367kern_return_t
368ipc_object_alloc(
0a7de745
A
369 ipc_space_t space,
370 ipc_object_type_t otype,
371 mach_port_type_t type,
372 mach_port_urefs_t urefs,
373 mach_port_name_t *namep,
374 ipc_object_t *objectp)
1c79356b
A
375{
376 ipc_object_t object;
377 ipc_entry_t entry;
378 kern_return_t kr;
379
380 assert(otype < IOT_NUMBER);
381 assert((type & MACH_PORT_TYPE_ALL_RIGHTS) == type);
382 assert(type != MACH_PORT_TYPE_NONE);
383 assert(urefs <= MACH_PORT_UREFS_MAX);
384
385 object = io_alloc(otype);
0a7de745 386 if (object == IO_NULL) {
1c79356b 387 return KERN_RESOURCE_SHORTAGE;
0a7de745 388 }
1c79356b
A
389
390 if (otype == IOT_PORT) {
cb323159 391 ipc_port_t port = ip_object_to_port(object);
1c79356b
A
392
393 bzero((char *)port, sizeof(*port));
394 } else if (otype == IOT_PORT_SET) {
cb323159 395 ipc_pset_t pset = ips_object_to_pset(object);
1c79356b
A
396
397 bzero((char *)pset, sizeof(*pset));
398 }
399
400 io_lock_init(object);
b0d623f7 401 *namep = CAST_MACH_PORT_TO_NAME(object);
1c79356b
A
402 kr = ipc_entry_alloc(space, namep, &entry);
403 if (kr != KERN_SUCCESS) {
404 io_free(otype, object);
405 return kr;
406 }
407 /* space is write-locked */
408
409 entry->ie_bits |= type | urefs;
410 entry->ie_object = object;
316670eb 411 ipc_entry_modified(space, *namep, entry);
1c79356b 412
cb323159 413 object->io_bits = io_makebits(TRUE, otype, 0);
1c79356b 414 io_lock(object);
1c79356b
A
415
416 object->io_references = 1; /* for entry, not caller */
1c79356b
A
417
418 *objectp = object;
419 return KERN_SUCCESS;
420}
421
422/*
423 * Routine: ipc_object_alloc_name
424 * Purpose:
425 * Allocate an object, with a specific name.
426 * Conditions:
427 * Nothing locked. If successful, the object is returned locked.
428 * The caller doesn't get a reference for the object.
429 * Returns:
430 * KERN_SUCCESS The object is allocated.
431 * KERN_INVALID_TASK The space is dead.
432 * KERN_NAME_EXISTS The name already denotes a right.
433 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
434 */
435
436kern_return_t
437ipc_object_alloc_name(
0a7de745
A
438 ipc_space_t space,
439 ipc_object_type_t otype,
440 mach_port_type_t type,
441 mach_port_urefs_t urefs,
442 mach_port_name_t name,
443 ipc_object_t *objectp)
1c79356b
A
444{
445 ipc_object_t object;
446 ipc_entry_t entry;
447 kern_return_t kr;
448
449 assert(otype < IOT_NUMBER);
450 assert((type & MACH_PORT_TYPE_ALL_RIGHTS) == type);
451 assert(type != MACH_PORT_TYPE_NONE);
452 assert(urefs <= MACH_PORT_UREFS_MAX);
453
454 object = io_alloc(otype);
0a7de745 455 if (object == IO_NULL) {
1c79356b 456 return KERN_RESOURCE_SHORTAGE;
0a7de745 457 }
1c79356b
A
458
459 if (otype == IOT_PORT) {
cb323159 460 ipc_port_t port = ip_object_to_port(object);
1c79356b
A
461
462 bzero((char *)port, sizeof(*port));
463 } else if (otype == IOT_PORT_SET) {
cb323159 464 ipc_pset_t pset = ips_object_to_pset(object);
1c79356b
A
465
466 bzero((char *)pset, sizeof(*pset));
467 }
468
469 io_lock_init(object);
470 kr = ipc_entry_alloc_name(space, name, &entry);
471 if (kr != KERN_SUCCESS) {
472 io_free(otype, object);
473 return kr;
474 }
475 /* space is write-locked */
476
477 if (ipc_right_inuse(space, name, entry)) {
478 io_free(otype, object);
479 return KERN_NAME_EXISTS;
480 }
481
482 entry->ie_bits |= type | urefs;
483 entry->ie_object = object;
316670eb 484 ipc_entry_modified(space, name, entry);
1c79356b 485
cb323159
A
486 object->io_bits = io_makebits(TRUE, otype, 0);
487
1c79356b
A
488 io_lock(object);
489 is_write_unlock(space);
490
491 object->io_references = 1; /* for entry, not caller */
1c79356b
A
492
493 *objectp = object;
494 return KERN_SUCCESS;
495}
496
cb323159
A
497/* Routine: ipc_object_validate
498 * Purpose:
499 * Validates an ipc port or port set as belonging to the correct
500 * zone.
501 */
502
503void
504ipc_object_validate(
505 ipc_object_t object)
506{
f427ee49
A
507 if (io_otype(object) != IOT_PORT_SET) {
508 zone_id_require(ZONE_ID_IPC_PORT,
509 sizeof(struct ipc_port), object);
510 } else {
511 zone_id_require(ZONE_ID_IPC_PORT_SET,
512 sizeof(struct ipc_pset), object);
513 }
cb323159
A
514}
515
1c79356b
A
516/*
517 * Routine: ipc_object_copyin_type
518 * Purpose:
519 * Convert a send type name to a received type name.
520 */
521
522mach_msg_type_name_t
523ipc_object_copyin_type(
0a7de745 524 mach_msg_type_name_t msgt_name)
1c79356b
A
525{
526 switch (msgt_name) {
0a7de745 527 case MACH_MSG_TYPE_MOVE_RECEIVE:
1c79356b
A
528 return MACH_MSG_TYPE_PORT_RECEIVE;
529
0a7de745
A
530 case MACH_MSG_TYPE_MOVE_SEND_ONCE:
531 case MACH_MSG_TYPE_MAKE_SEND_ONCE:
1c79356b
A
532 return MACH_MSG_TYPE_PORT_SEND_ONCE;
533
0a7de745
A
534 case MACH_MSG_TYPE_MOVE_SEND:
535 case MACH_MSG_TYPE_MAKE_SEND:
536 case MACH_MSG_TYPE_COPY_SEND:
1c79356b
A
537 return MACH_MSG_TYPE_PORT_SEND;
538
0a7de745
A
539 case MACH_MSG_TYPE_DISPOSE_RECEIVE:
540 case MACH_MSG_TYPE_DISPOSE_SEND:
541 case MACH_MSG_TYPE_DISPOSE_SEND_ONCE:
542 /* fall thru */
543 default:
1c79356b
A
544 return MACH_MSG_TYPE_PORT_NONE;
545 }
546}
547
548/*
549 * Routine: ipc_object_copyin
550 * Purpose:
551 * Copyin a capability from a space.
552 * If successful, the caller gets a ref
553 * for the resulting object, unless it is IO_DEAD.
554 * Conditions:
555 * Nothing locked.
556 * Returns:
557 * KERN_SUCCESS Acquired an object, possibly IO_DEAD.
558 * KERN_INVALID_TASK The space is dead.
559 * KERN_INVALID_NAME Name doesn't exist in space.
560 * KERN_INVALID_RIGHT Name doesn't denote correct right.
561 */
562
563kern_return_t
564ipc_object_copyin(
0a7de745
A
565 ipc_space_t space,
566 mach_port_name_t name,
567 mach_msg_type_name_t msgt_name,
cb323159
A
568 ipc_object_t *objectp,
569 mach_port_context_t context,
570 mach_msg_guard_flags_t *guard_flags,
571 ipc_kmsg_flags_t kmsg_flags)
1c79356b
A
572{
573 ipc_entry_t entry;
574 ipc_port_t soright;
316670eb 575 ipc_port_t release_port;
1c79356b 576 kern_return_t kr;
39236c6e 577 int assertcnt = 0;
1c79356b 578
cb323159
A
579 ipc_right_copyin_flags_t irc_flags = IPC_RIGHT_COPYIN_FLAGS_DEADOK;
580 if (kmsg_flags & IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND) {
581 irc_flags |= IPC_RIGHT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND;
582 }
583
1c79356b
A
584 /*
585 * Could first try a read lock when doing
586 * MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND,
587 * and MACH_MSG_TYPE_MAKE_SEND_ONCE.
588 */
589
590 kr = ipc_right_lookup_write(space, name, &entry);
0a7de745 591 if (kr != KERN_SUCCESS) {
1c79356b 592 return kr;
0a7de745 593 }
1c79356b
A
594 /* space is write-locked and active */
595
316670eb 596 release_port = IP_NULL;
1c79356b 597 kr = ipc_right_copyin(space, name, entry,
cb323159 598 msgt_name, irc_flags,
0a7de745
A
599 objectp, &soright,
600 &release_port,
cb323159
A
601 &assertcnt,
602 context,
603 guard_flags);
0a7de745 604 if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE) {
1c79356b 605 ipc_entry_dealloc(space, name, entry);
0a7de745 606 }
1c79356b
A
607 is_write_unlock(space);
608
39236c6e 609#if IMPORTANCE_INHERITANCE
fe8ab488
A
610 if (0 < assertcnt && ipc_importance_task_is_any_receiver_type(current_task()->task_imp_base)) {
611 ipc_importance_task_drop_internal_assertion(current_task()->task_imp_base, assertcnt);
39236c6e
A
612 }
613#endif /* IMPORTANCE_INHERITANCE */
614
0a7de745 615 if (release_port != IP_NULL) {
316670eb 616 ip_release(release_port);
0a7de745 617 }
316670eb 618
0a7de745 619 if ((kr == KERN_SUCCESS) && (soright != IP_NULL)) {
1c79356b 620 ipc_notify_port_deleted(soright, name);
0a7de745 621 }
1c79356b
A
622
623 return kr;
624}
625
626/*
627 * Routine: ipc_object_copyin_from_kernel
628 * Purpose:
629 * Copyin a naked capability from the kernel.
630 *
631 * MACH_MSG_TYPE_MOVE_RECEIVE
55e303ae
A
632 * The receiver must be ipc_space_kernel
633 * or the receive right must already be in limbo.
1c79356b
A
634 * Consumes the naked receive right.
635 * MACH_MSG_TYPE_COPY_SEND
636 * A naked send right must be supplied.
637 * The port gains a reference, and a send right
638 * if the port is still active.
639 * MACH_MSG_TYPE_MAKE_SEND
640 * The receiver must be ipc_space_kernel.
641 * The port gains a reference and a send right.
642 * MACH_MSG_TYPE_MOVE_SEND
643 * Consumes a naked send right.
644 * MACH_MSG_TYPE_MAKE_SEND_ONCE
645 * The port gains a reference and a send-once right.
646 * Receiver also be the caller of device subsystem,
647 * so no assertion.
648 * MACH_MSG_TYPE_MOVE_SEND_ONCE
649 * Consumes a naked send-once right.
650 * Conditions:
651 * Nothing locked.
652 */
653
654void
655ipc_object_copyin_from_kernel(
0a7de745
A
656 ipc_object_t object,
657 mach_msg_type_name_t msgt_name)
1c79356b
A
658{
659 assert(IO_VALID(object));
660
661 switch (msgt_name) {
0a7de745 662 case MACH_MSG_TYPE_MOVE_RECEIVE: {
cb323159 663 ipc_port_t port = ip_object_to_port(object);
1c79356b
A
664
665 ip_lock(port);
d9a64523 666 imq_lock(&port->ip_messages);
cb323159 667 require_ip_active(port);
55e303ae
A
668 if (port->ip_destination != IP_NULL) {
669 assert(port->ip_receiver == ipc_space_kernel);
cb323159 670 assert(port->ip_immovable_receive == 0);
1c79356b 671
55e303ae 672 /* relevant part of ipc_port_clear_receiver */
cb323159 673 port->ip_mscount = 0;
55e303ae
A
674 port->ip_receiver_name = MACH_PORT_NULL;
675 port->ip_destination = IP_NULL;
676 }
d9a64523 677 imq_unlock(&port->ip_messages);
1c79356b
A
678 ip_unlock(port);
679 break;
0a7de745 680 }
1c79356b 681
0a7de745 682 case MACH_MSG_TYPE_COPY_SEND: {
cb323159 683 ipc_port_t port = ip_object_to_port(object);
1c79356b
A
684
685 ip_lock(port);
686 if (ip_active(port)) {
687 assert(port->ip_srights > 0);
688 port->ip_srights++;
689 }
690 ip_reference(port);
691 ip_unlock(port);
692 break;
0a7de745 693 }
1c79356b 694
0a7de745 695 case MACH_MSG_TYPE_MAKE_SEND: {
cb323159 696 ipc_port_t port = ip_object_to_port(object);
1c79356b
A
697
698 ip_lock(port);
39236c6e
A
699 if (ip_active(port)) {
700 assert(port->ip_receiver_name != MACH_PORT_NULL);
39037602 701 assert((port->ip_receiver == ipc_space_kernel) ||
0a7de745 702 (port->ip_receiver->is_node_id != HOST_LOCAL_NODE));
39236c6e
A
703 port->ip_mscount++;
704 }
1c79356b 705
1c79356b 706 port->ip_srights++;
39236c6e 707 ip_reference(port);
1c79356b
A
708 ip_unlock(port);
709 break;
0a7de745 710 }
1c79356b 711
0a7de745 712 case MACH_MSG_TYPE_MOVE_SEND: {
1c79356b 713 /* move naked send right into the message */
cb323159 714 assert(ip_object_to_port(object)->ip_srights);
1c79356b 715 break;
0a7de745 716 }
1c79356b 717
0a7de745 718 case MACH_MSG_TYPE_MAKE_SEND_ONCE: {
cb323159 719 ipc_port_t port = ip_object_to_port(object);
1c79356b
A
720
721 ip_lock(port);
39236c6e
A
722 if (ip_active(port)) {
723 assert(port->ip_receiver_name != MACH_PORT_NULL);
724 }
cb323159 725 ipc_port_make_sonce_locked(port);
1c79356b
A
726 ip_unlock(port);
727 break;
0a7de745 728 }
1c79356b 729
0a7de745 730 case MACH_MSG_TYPE_MOVE_SEND_ONCE: {
1c79356b 731 /* move naked send-once right into the message */
cb323159 732 assert(ip_object_to_port(object)->ip_sorights);
1c79356b 733 break;
0a7de745 734 }
1c79356b 735
0a7de745 736 default:
1c79356b
A
737 panic("ipc_object_copyin_from_kernel: strange rights");
738 }
739}
740
741/*
742 * Routine: ipc_object_destroy
743 * Purpose:
744 * Destroys a naked capability.
745 * Consumes a ref for the object.
746 *
747 * A receive right should be in limbo or in transit.
748 * Conditions:
749 * Nothing locked.
750 */
751
752void
753ipc_object_destroy(
0a7de745
A
754 ipc_object_t object,
755 mach_msg_type_name_t msgt_name)
1c79356b
A
756{
757 assert(IO_VALID(object));
758 assert(io_otype(object) == IOT_PORT);
759
760 switch (msgt_name) {
0a7de745 761 case MACH_MSG_TYPE_PORT_SEND:
cb323159 762 ipc_port_release_send(ip_object_to_port(object));
1c79356b
A
763 break;
764
0a7de745 765 case MACH_MSG_TYPE_PORT_SEND_ONCE:
cb323159 766 ipc_notify_send_once(ip_object_to_port(object));
1c79356b
A
767 break;
768
0a7de745 769 case MACH_MSG_TYPE_PORT_RECEIVE:
cb323159 770 ipc_port_release_receive(ip_object_to_port(object));
1c79356b
A
771 break;
772
0a7de745 773 default:
1c79356b
A
774 panic("ipc_object_destroy: strange rights");
775 }
776}
777
6d2010ae
A
778/*
779 * Routine: ipc_object_destroy_dest
780 * Purpose:
781 * Destroys a naked capability for the destination of
782 * of a message. Consumes a ref for the object.
783 *
784 * Conditions:
785 * Nothing locked.
786 */
787
788void
789ipc_object_destroy_dest(
0a7de745
A
790 ipc_object_t object,
791 mach_msg_type_name_t msgt_name)
6d2010ae
A
792{
793 assert(IO_VALID(object));
794 assert(io_otype(object) == IOT_PORT);
795
796 switch (msgt_name) {
0a7de745 797 case MACH_MSG_TYPE_PORT_SEND:
cb323159 798 ipc_port_release_send(ip_object_to_port(object));
6d2010ae
A
799 break;
800
0a7de745
A
801 case MACH_MSG_TYPE_PORT_SEND_ONCE:
802 if (io_active(object) &&
cb323159
A
803 !ip_full_kernel(ip_object_to_port(object))) {
804 ipc_notify_send_once(ip_object_to_port(object));
0a7de745 805 } else {
cb323159 806 ipc_port_release_sonce(ip_object_to_port(object));
0a7de745 807 }
6d2010ae
A
808 break;
809
0a7de745 810 default:
6d2010ae
A
811 panic("ipc_object_destroy_dest: strange rights");
812 }
813}
814
cb323159
A
815/*
816 * Routine: ipc_object_insert_send_right
817 * Purpose:
818 * Insert a send right into an object already in the space.
819 * The specified name must already point to a valid object.
820 *
821 * Note: This really is a combined copyin()/copyout(),
822 * that avoids most of the overhead of being implemented that way.
823 *
824 * This is the fastpath for mach_port_insert_right.
825 *
826 * Conditions:
827 * Nothing locked.
828 *
829 * msgt_name must be MACH_MSG_TYPE_MAKE_SEND_ONCE or
830 * MACH_MSG_TYPE_MOVE_SEND_ONCE.
831 *
832 * Returns:
833 * KERN_SUCCESS Copied out object, consumed ref.
834 * KERN_INVALID_TASK The space is dead.
835 * KERN_INVALID_NAME Name doesn't exist in space.
836 * KERN_INVALID_CAPABILITY The object is dead.
837 * KERN_RIGHT_EXISTS Space has rights under another name.
838 */
839kern_return_t
840ipc_object_insert_send_right(
841 ipc_space_t space,
842 mach_port_name_t name,
843 mach_msg_type_name_t msgt_name)
844{
845 ipc_entry_bits_t bits;
846 ipc_object_t object;
847 ipc_entry_t entry;
848 kern_return_t kr;
849
850 assert(msgt_name == MACH_MSG_TYPE_MAKE_SEND ||
851 msgt_name == MACH_MSG_TYPE_COPY_SEND);
852
853 kr = ipc_right_lookup_write(space, name, &entry);
854 if (kr != KERN_SUCCESS) {
855 return kr;
856 }
857 /* space is write-locked and active */
858
859 if (!IO_VALID(entry->ie_object)) {
860 is_write_unlock(space);
861 return KERN_INVALID_CAPABILITY;
862 }
863
864 bits = entry->ie_bits;
865 object = entry->ie_object;
866
867 io_lock(object);
868 if (!io_active(object)) {
869 kr = KERN_INVALID_CAPABILITY;
870 } else if (msgt_name == MACH_MSG_TYPE_MAKE_SEND) {
871 if (bits & MACH_PORT_TYPE_RECEIVE) {
872 ipc_port_t port = ip_object_to_port(object);
873 port->ip_mscount++;
874 if ((bits & MACH_PORT_TYPE_SEND) == 0) {
875 port->ip_srights++;
876 bits |= MACH_PORT_TYPE_SEND;
877 }
878 /* leave urefs pegged to maximum if it overflowed */
879 if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) {
880 bits += 1; /* increment urefs */
881 }
882 entry->ie_bits = bits;
883 ipc_entry_modified(space, name, entry);
884 kr = KERN_SUCCESS;
885 } else {
886 kr = KERN_INVALID_RIGHT;
887 }
888 } else { // MACH_MSG_TYPE_COPY_SEND
889 if (bits & MACH_PORT_TYPE_SEND) {
890 /* leave urefs pegged to maximum if it overflowed */
891 if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) {
892 entry->ie_bits = bits + 1; /* increment urefs */
893 }
894 ipc_entry_modified(space, name, entry);
895 kr = KERN_SUCCESS;
896 } else {
897 kr = KERN_INVALID_RIGHT;
898 }
899 }
900
901 io_unlock(object);
902 is_write_unlock(space);
903
904 return kr;
905}
906
1c79356b
A
907/*
908 * Routine: ipc_object_copyout
909 * Purpose:
910 * Copyout a capability, placing it into a space.
911 * If successful, consumes a ref for the object.
912 * Conditions:
913 * Nothing locked.
914 * Returns:
915 * KERN_SUCCESS Copied out object, consumed ref.
916 * KERN_INVALID_TASK The space is dead.
917 * KERN_INVALID_CAPABILITY The object is dead.
918 * KERN_NO_SPACE No room in space for another right.
919 * KERN_RESOURCE_SHORTAGE No memory available.
920 * KERN_UREFS_OVERFLOW Urefs limit exceeded
921 * and overflow wasn't specified.
922 */
923
924kern_return_t
925ipc_object_copyout(
0a7de745
A
926 ipc_space_t space,
927 ipc_object_t object,
928 mach_msg_type_name_t msgt_name,
cb323159
A
929 mach_port_context_t *context,
930 mach_msg_guard_flags_t *guard_flags,
0a7de745 931 mach_port_name_t *namep)
1c79356b 932{
d9a64523 933 struct knote *kn = current_thread()->ith_knote;
1c79356b
A
934 mach_port_name_t name;
935 ipc_entry_t entry;
936 kern_return_t kr;
937
938 assert(IO_VALID(object));
939 assert(io_otype(object) == IOT_PORT);
940
d9a64523
A
941 if (ITH_KNOTE_VALID(kn, msgt_name)) {
942 filt_machport_turnstile_prepare_lazily(kn,
cb323159 943 msgt_name, ip_object_to_port(object));
d9a64523
A
944 }
945
1c79356b
A
946 is_write_lock(space);
947
948 for (;;) {
316670eb 949 if (!is_active(space)) {
1c79356b
A
950 is_write_unlock(space);
951 return KERN_INVALID_TASK;
952 }
953
954 if ((msgt_name != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
0a7de745 955 ipc_right_reverse(space, object, &name, &entry)) {
1c79356b
A
956 /* object is locked and active */
957
958 assert(entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE);
959 break;
960 }
961
ea3f0419 962
b0d623f7 963 name = CAST_MACH_PORT_TO_NAME(object);
1c79356b
A
964 kr = ipc_entry_get(space, &name, &entry);
965 if (kr != KERN_SUCCESS) {
966 /* unlocks/locks space, so must start again */
967
968 kr = ipc_entry_grow_table(space, ITS_SIZE_NONE);
0a7de745 969 if (kr != KERN_SUCCESS) {
1c79356b 970 return kr; /* space is unlocked */
0a7de745 971 }
1c79356b
A
972 continue;
973 }
974
975 assert(IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE);
976 assert(entry->ie_object == IO_NULL);
977
978 io_lock(object);
979 if (!io_active(object)) {
980 io_unlock(object);
981 ipc_entry_dealloc(space, name, entry);
982 is_write_unlock(space);
983 return KERN_INVALID_CAPABILITY;
984 }
985
ea3f0419
A
986 /* Don't actually copyout rights we aren't allowed to */
987 if (!ip_label_check(space, ip_object_to_port(object), msgt_name)) {
988 io_unlock(object);
989 ipc_entry_dealloc(space, name, entry);
990 is_write_unlock(space);
ea3f0419
A
991 return KERN_INVALID_CAPABILITY;
992 }
993
1c79356b
A
994 entry->ie_object = object;
995 break;
996 }
997
998 /* space is write-locked and active, object is locked and active */
999
1000 kr = ipc_right_copyout(space, name, entry,
cb323159 1001 msgt_name, context, guard_flags, object);
39236c6e 1002
1c79356b
A
1003 /* object is unlocked */
1004 is_write_unlock(space);
1005
0a7de745 1006 if (kr == KERN_SUCCESS) {
1c79356b 1007 *namep = name;
0a7de745 1008 }
1c79356b
A
1009 return kr;
1010}
1011
1012/*
1013 * Routine: ipc_object_copyout_name
1014 * Purpose:
1015 * Copyout a capability, placing it into a space.
1016 * The specified name is used for the capability.
1017 * If successful, consumes a ref for the object.
1018 * Conditions:
1019 * Nothing locked.
1020 * Returns:
1021 * KERN_SUCCESS Copied out object, consumed ref.
1022 * KERN_INVALID_TASK The space is dead.
1023 * KERN_INVALID_CAPABILITY The object is dead.
1024 * KERN_RESOURCE_SHORTAGE No memory available.
1025 * KERN_UREFS_OVERFLOW Urefs limit exceeded
1026 * and overflow wasn't specified.
1027 * KERN_RIGHT_EXISTS Space has rights under another name.
1028 * KERN_NAME_EXISTS Name is already used.
1029 */
1030
1031kern_return_t
1032ipc_object_copyout_name(
0a7de745
A
1033 ipc_space_t space,
1034 ipc_object_t object,
1035 mach_msg_type_name_t msgt_name,
0a7de745 1036 mach_port_name_t name)
1c79356b
A
1037{
1038 mach_port_name_t oname;
1039 ipc_entry_t oentry;
1040 ipc_entry_t entry;
1041 kern_return_t kr;
1042
39236c6e
A
1043#if IMPORTANCE_INHERITANCE
1044 int assertcnt = 0;
fe8ab488 1045 ipc_importance_task_t task_imp = IIT_NULL;
39236c6e
A
1046#endif /* IMPORTANCE_INHERITANCE */
1047
1c79356b
A
1048 assert(IO_VALID(object));
1049 assert(io_otype(object) == IOT_PORT);
1050
1051 kr = ipc_entry_alloc_name(space, name, &entry);
0a7de745 1052 if (kr != KERN_SUCCESS) {
1c79356b 1053 return kr;
0a7de745 1054 }
1c79356b
A
1055 /* space is write-locked and active */
1056
1057 if ((msgt_name != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
1058 ipc_right_reverse(space, object, &oname, &oentry)) {
1059 /* object is locked and active */
1060
1061 if (name != oname) {
1062 io_unlock(object);
1063
0a7de745 1064 if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE) {
1c79356b 1065 ipc_entry_dealloc(space, name, entry);
0a7de745 1066 }
1c79356b
A
1067
1068 is_write_unlock(space);
1069 return KERN_RIGHT_EXISTS;
1070 }
1071
1072 assert(entry == oentry);
1073 assert(entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE);
1074 } else {
0a7de745 1075 if (ipc_right_inuse(space, name, entry)) {
1c79356b 1076 return KERN_NAME_EXISTS;
0a7de745 1077 }
1c79356b
A
1078
1079 assert(IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE);
1080 assert(entry->ie_object == IO_NULL);
1081
1082 io_lock(object);
1083 if (!io_active(object)) {
1084 io_unlock(object);
1085 ipc_entry_dealloc(space, name, entry);
1086 is_write_unlock(space);
1087 return KERN_INVALID_CAPABILITY;
1088 }
1089
ea3f0419
A
1090 /* Don't actually copyout rights we aren't allowed to */
1091 if (!ip_label_check(space, ip_object_to_port(object), msgt_name)) {
1092 io_unlock(object);
1093 ipc_entry_dealloc(space, name, entry);
1094 is_write_unlock(space);
ea3f0419
A
1095 return KERN_INVALID_CAPABILITY;
1096 }
1097
1c79356b
A
1098 entry->ie_object = object;
1099 }
1100
1101 /* space is write-locked and active, object is locked and active */
1102
39236c6e
A
1103#if IMPORTANCE_INHERITANCE
1104 /*
1105 * We are slamming a receive right into the space, without
1106 * first having been enqueued on a port destined there. So,
1107 * we have to arrange to boost the task appropriately if this
1108 * port has assertions (and the task wants them).
1109 */
1110 if (msgt_name == MACH_MSG_TYPE_PORT_RECEIVE) {
cb323159 1111 ipc_port_t port = ip_object_to_port(object);
39236c6e 1112
fe8ab488
A
1113 if (space->is_task != TASK_NULL) {
1114 task_imp = space->is_task->task_imp_base;
1115 if (ipc_importance_task_is_any_receiver_type(task_imp)) {
1116 assertcnt = port->ip_impcount;
1117 ipc_importance_task_reference(task_imp);
5ba3f43e
A
1118 } else {
1119 task_imp = IIT_NULL;
fe8ab488 1120 }
39236c6e
A
1121 }
1122
1123 /* take port out of limbo */
1124 assert(port->ip_tempowner != 0);
1125 port->ip_tempowner = 0;
1126 }
1127
1128#endif /* IMPORTANCE_INHERITANCE */
1129
1c79356b 1130 kr = ipc_right_copyout(space, name, entry,
cb323159 1131 msgt_name, NULL, NULL, object);
39236c6e 1132
1c79356b
A
1133 /* object is unlocked */
1134 is_write_unlock(space);
39236c6e
A
1135
1136#if IMPORTANCE_INHERITANCE
1137 /*
1138 * Add the assertions to the task that we captured before
1139 */
fe8ab488
A
1140 if (task_imp != IIT_NULL) {
1141 ipc_importance_task_hold_internal_assertion(task_imp, assertcnt);
1142 ipc_importance_task_release(task_imp);
39236c6e
A
1143 }
1144#endif /* IMPORTANCE_INHERITANCE */
1145
1c79356b
A
1146 return kr;
1147}
1148
1149/*
1150 * Routine: ipc_object_copyout_dest
1151 * Purpose:
1152 * Translates/consumes the destination right of a message.
1153 * This is unlike normal copyout because the right is consumed
1154 * in a funny way instead of being given to the receiving space.
1155 * The receiver gets his name for the port, if he has receive
1156 * rights, otherwise MACH_PORT_NULL.
1157 * Conditions:
1158 * The object is locked and active. Nothing else locked.
1159 * The object is unlocked and loses a reference.
1160 */
1161
1162void
1163ipc_object_copyout_dest(
0a7de745
A
1164 ipc_space_t space,
1165 ipc_object_t object,
1166 mach_msg_type_name_t msgt_name,
1167 mach_port_name_t *namep)
1c79356b
A
1168{
1169 mach_port_name_t name;
1170
1171 assert(IO_VALID(object));
1172 assert(io_active(object));
1173
1c79356b
A
1174 /*
1175 * If the space is the receiver/owner of the object,
1176 * then we quietly consume the right and return
1177 * the space's name for the object. Otherwise
1178 * we destroy the right and return MACH_PORT_NULL.
1179 */
1180
1181 switch (msgt_name) {
0a7de745 1182 case MACH_MSG_TYPE_PORT_SEND: {
cb323159 1183 ipc_port_t port = ip_object_to_port(object);
1c79356b
A
1184 ipc_port_t nsrequest = IP_NULL;
1185 mach_port_mscount_t mscount;
1186
0a7de745 1187 if (port->ip_receiver == space) {
1c79356b 1188 name = port->ip_receiver_name;
0a7de745 1189 } else {
1c79356b 1190 name = MACH_PORT_NULL;
0a7de745 1191 }
1c79356b
A
1192
1193 assert(port->ip_srights > 0);
1194 if (--port->ip_srights == 0 &&
1195 port->ip_nsrequest != IP_NULL) {
1196 nsrequest = port->ip_nsrequest;
1197 port->ip_nsrequest = IP_NULL;
1198 mscount = port->ip_mscount;
cb323159
A
1199 ipc_port_clear_sync_rcv_thread_boost_locked(port);
1200 /* port unlocked */
1c79356b 1201 ipc_notify_no_senders(nsrequest, mscount);
0a7de745 1202 } else {
cb323159
A
1203 ipc_port_clear_sync_rcv_thread_boost_locked(port);
1204 /* port unlocked */
0a7de745 1205 }
cb323159
A
1206
1207 ip_release(port);
1c79356b 1208 break;
0a7de745 1209 }
1c79356b 1210
0a7de745 1211 case MACH_MSG_TYPE_PORT_SEND_ONCE: {
cb323159 1212 ipc_port_t port = ip_object_to_port(object);
1c79356b
A
1213
1214 assert(port->ip_sorights > 0);
1215
1216 if (port->ip_receiver == space) {
1217 /* quietly consume the send-once right */
1218
1219 port->ip_sorights--;
1220 name = port->ip_receiver_name;
cb323159
A
1221 ipc_port_clear_sync_rcv_thread_boost_locked(port);
1222 /* port unlocked */
1223 ip_release(port);
1c79356b
A
1224 } else {
1225 /*
1226 * A very bizarre case. The message
1227 * was received, but before this copyout
1228 * happened the space lost receive rights.
1229 * We can't quietly consume the soright
1230 * out from underneath some other task,
1231 * so generate a send-once notification.
1232 */
1233
1c79356b
A
1234 ip_unlock(port);
1235
1236 ipc_notify_send_once(port);
1237 name = MACH_PORT_NULL;
1238 }
1239
1240 break;
0a7de745 1241 }
1c79356b 1242
0a7de745 1243 default:
1c79356b 1244 panic("ipc_object_copyout_dest: strange rights");
91447636 1245 name = MACH_PORT_DEAD;
1c79356b
A
1246 }
1247
1248 *namep = name;
1249}
1250
1251/*
cb323159 1252 * Routine: io_lock
1c79356b 1253 * Purpose:
cb323159 1254 * Validate, then acquire a lock on an ipc object
1c79356b
A
1255 */
1256
cb323159
A
1257void
1258io_lock(ipc_object_t io)
1c79356b 1259{
cb323159
A
1260 ipc_object_validate(io);
1261 lck_spin_lock_grp(&(io)->io_lock_data, &ipc_lck_grp);
1262}
1c79356b 1263
cb323159
A
1264/*
1265 * Routine: io_lock_try
1266 * Purpose:
1267 * Validate, then try to acquire a lock on an object,
1268 * fail if there is an existing busy lock
1269 */
1c79356b 1270
cb323159
A
1271boolean_t
1272io_lock_try(ipc_object_t io)
1273{
1274 ipc_object_validate(io);
1275 return lck_spin_try_lock_grp(&(io)->io_lock_data, &ipc_lck_grp);
1c79356b
A
1276}
1277
1c79356b
A
1278/*
1279 * Check whether the object is a port if so, free it. But
1280 * keep track of that fact.
1281 */
1282void
1283io_free(
0a7de745
A
1284 unsigned int otype,
1285 ipc_object_t object)
1c79356b 1286{
1c79356b 1287 if (otype == IOT_PORT) {
cb323159 1288 ipc_port_finalize(ip_object_to_port(object));
1c79356b 1289 }
b0d623f7 1290 io_lock_destroy(object);
91447636 1291 zfree(ipc_object_zones[otype], object);
1c79356b 1292}