]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_port.c
cd8c04b817db9f067c89650b18f7f37ca8d325b6
[apple/xnu.git] / osfmk / ipc / ipc_port.c
1 /*
2 * Copyright (c) 2000-2019 Apple 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_FREE_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 Software.Distribution@CS.CMU.EDU
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 * 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 */
62 /*
63 */
64 /*
65 * File: ipc/ipc_port.c
66 * Author: Rich Draves
67 * Date: 1989
68 *
69 * Functions to manipulate IPC ports.
70 */
71
72 #include <zone_debug.h>
73 #include <mach_assert.h>
74
75 #include <mach/port.h>
76 #include <mach/kern_return.h>
77 #include <kern/ipc_kobject.h>
78 #include <kern/thread.h>
79 #include <kern/misc_protos.h>
80 #include <kern/waitq.h>
81 #include <kern/policy_internal.h>
82 #include <kern/debug.h>
83 #include <kern/kcdata.h>
84 #include <ipc/ipc_entry.h>
85 #include <ipc/ipc_space.h>
86 #include <ipc/ipc_object.h>
87 #include <ipc/ipc_port.h>
88 #include <ipc/ipc_pset.h>
89 #include <ipc/ipc_kmsg.h>
90 #include <ipc/ipc_mqueue.h>
91 #include <ipc/ipc_notify.h>
92 #include <ipc/ipc_table.h>
93 #include <ipc/ipc_importance.h>
94 #include <machine/limits.h>
95 #include <kern/turnstile.h>
96
97 #include <security/mac_mach_internal.h>
98
99 #include <string.h>
100
101 decl_lck_spin_data(, ipc_port_multiple_lock_data);
102 ipc_port_timestamp_t ipc_port_timestamp_data;
103 int ipc_portbt;
104 extern int prioritize_launch;
105
106 #if MACH_ASSERT
107 void ipc_port_init_debug(
108 ipc_port_t port,
109 uintptr_t *callstack,
110 unsigned int callstack_max);
111
112 void ipc_port_callstack_init_debug(
113 uintptr_t *callstack,
114 unsigned int callstack_max);
115
116 #endif /* MACH_ASSERT */
117
118 static void
119 ipc_port_send_turnstile_recompute_push_locked(
120 ipc_port_t port);
121
122 static thread_t
123 ipc_port_get_watchport_inheritor(
124 ipc_port_t port);
125
126 void
127 ipc_port_release(ipc_port_t port)
128 {
129 ip_release(port);
130 }
131
132 void
133 ipc_port_reference(ipc_port_t port)
134 {
135 ip_reference(port);
136 }
137
138 /*
139 * Routine: ipc_port_timestamp
140 * Purpose:
141 * Retrieve a timestamp value.
142 */
143
144 ipc_port_timestamp_t
145 ipc_port_timestamp(void)
146 {
147 return OSIncrementAtomic(&ipc_port_timestamp_data);
148 }
149
150 /*
151 * Routine: ipc_port_request_alloc
152 * Purpose:
153 * Try to allocate a request slot.
154 * If successful, returns the request index.
155 * Otherwise returns zero.
156 * Conditions:
157 * The port is locked and active.
158 * Returns:
159 * KERN_SUCCESS A request index was found.
160 * KERN_NO_SPACE No index allocated.
161 */
162
163 #if IMPORTANCE_INHERITANCE
164 kern_return_t
165 ipc_port_request_alloc(
166 ipc_port_t port,
167 mach_port_name_t name,
168 ipc_port_t soright,
169 boolean_t send_possible,
170 boolean_t immediate,
171 ipc_port_request_index_t *indexp,
172 boolean_t *importantp)
173 #else
174 kern_return_t
175 ipc_port_request_alloc(
176 ipc_port_t port,
177 mach_port_name_t name,
178 ipc_port_t soright,
179 boolean_t send_possible,
180 boolean_t immediate,
181 ipc_port_request_index_t *indexp)
182 #endif /* IMPORTANCE_INHERITANCE */
183 {
184 ipc_port_request_t ipr, table;
185 ipc_port_request_index_t index;
186 uintptr_t mask = 0;
187
188 #if IMPORTANCE_INHERITANCE
189 *importantp = FALSE;
190 #endif /* IMPORTANCE_INHERITANCE */
191
192 require_ip_active(port);
193 assert(name != MACH_PORT_NULL);
194 assert(soright != IP_NULL);
195
196 table = port->ip_requests;
197
198 if (table == IPR_NULL) {
199 return KERN_NO_SPACE;
200 }
201
202 index = table->ipr_next;
203 if (index == 0) {
204 return KERN_NO_SPACE;
205 }
206
207 ipr = &table[index];
208 assert(ipr->ipr_name == MACH_PORT_NULL);
209
210 table->ipr_next = ipr->ipr_next;
211 ipr->ipr_name = name;
212
213 if (send_possible) {
214 mask |= IPR_SOR_SPREQ_MASK;
215 if (immediate) {
216 mask |= IPR_SOR_SPARM_MASK;
217 if (port->ip_sprequests == 0) {
218 port->ip_sprequests = 1;
219 #if IMPORTANCE_INHERITANCE
220 /* TODO: Live importance support in send-possible */
221 if (port->ip_impdonation != 0 &&
222 port->ip_spimportant == 0 &&
223 (task_is_importance_donor(current_task()))) {
224 *importantp = TRUE;
225 }
226 #endif /* IMPORTANCE_INHERTANCE */
227 }
228 }
229 }
230 ipr->ipr_soright = IPR_SOR_MAKE(soright, mask);
231
232 *indexp = index;
233
234 return KERN_SUCCESS;
235 }
236
237 /*
238 * Routine: ipc_port_request_grow
239 * Purpose:
240 * Grow a port's table of requests.
241 * Conditions:
242 * The port must be locked and active.
243 * Nothing else locked; will allocate memory.
244 * Upon return the port is unlocked.
245 * Returns:
246 * KERN_SUCCESS Grew the table.
247 * KERN_SUCCESS Somebody else grew the table.
248 * KERN_SUCCESS The port died.
249 * KERN_RESOURCE_SHORTAGE Couldn't allocate new table.
250 * KERN_NO_SPACE Couldn't grow to desired size
251 */
252
253 kern_return_t
254 ipc_port_request_grow(
255 ipc_port_t port,
256 ipc_table_elems_t target_size)
257 {
258 ipc_table_size_t its;
259 ipc_port_request_t otable, ntable;
260 require_ip_active(port);
261
262 otable = port->ip_requests;
263 if (otable == IPR_NULL) {
264 its = &ipc_table_requests[0];
265 } else {
266 its = otable->ipr_size + 1;
267 }
268
269 if (target_size != ITS_SIZE_NONE) {
270 if ((otable != IPR_NULL) &&
271 (target_size <= otable->ipr_size->its_size)) {
272 ip_unlock(port);
273 return KERN_SUCCESS;
274 }
275 while ((its->its_size) && (its->its_size < target_size)) {
276 its++;
277 }
278 if (its->its_size == 0) {
279 ip_unlock(port);
280 return KERN_NO_SPACE;
281 }
282 }
283
284 ip_reference(port);
285 ip_unlock(port);
286
287 if ((its->its_size == 0) ||
288 ((ntable = it_requests_alloc(its)) == IPR_NULL)) {
289 ip_release(port);
290 return KERN_RESOURCE_SHORTAGE;
291 }
292
293 ip_lock(port);
294
295 /*
296 * Check that port is still active and that nobody else
297 * has slipped in and grown the table on us. Note that
298 * just checking if the current table pointer == otable
299 * isn't sufficient; must check ipr_size.
300 */
301
302 if (ip_active(port) && (port->ip_requests == otable) &&
303 ((otable == IPR_NULL) || (otable->ipr_size + 1 == its))) {
304 ipc_table_size_t oits;
305 ipc_table_elems_t osize, nsize;
306 ipc_port_request_index_t free, i;
307
308 /* copy old table to new table */
309
310 if (otable != IPR_NULL) {
311 oits = otable->ipr_size;
312 osize = oits->its_size;
313 free = otable->ipr_next;
314
315 (void) memcpy((void *)(ntable + 1),
316 (const void *)(otable + 1),
317 (osize - 1) * sizeof(struct ipc_port_request));
318 } else {
319 osize = 1;
320 oits = 0;
321 free = 0;
322 }
323
324 nsize = its->its_size;
325 assert(nsize > osize);
326
327 /* add new elements to the new table's free list */
328
329 for (i = osize; i < nsize; i++) {
330 ipc_port_request_t ipr = &ntable[i];
331
332 ipr->ipr_name = MACH_PORT_NULL;
333 ipr->ipr_next = free;
334 free = i;
335 }
336
337 ntable->ipr_next = free;
338 ntable->ipr_size = its;
339 port->ip_requests = ntable;
340 ip_unlock(port);
341 ip_release(port);
342
343 if (otable != IPR_NULL) {
344 it_requests_free(oits, otable);
345 }
346 } else {
347 ip_unlock(port);
348 ip_release(port);
349 it_requests_free(its, ntable);
350 }
351
352 return KERN_SUCCESS;
353 }
354
355 /*
356 * Routine: ipc_port_request_sparm
357 * Purpose:
358 * Arm delayed send-possible request.
359 * Conditions:
360 * The port must be locked and active.
361 *
362 * Returns TRUE if the request was armed
363 * (or armed with importance in that version).
364 */
365
366 boolean_t
367 ipc_port_request_sparm(
368 ipc_port_t port,
369 __assert_only mach_port_name_t name,
370 ipc_port_request_index_t index,
371 mach_msg_option_t option,
372 mach_msg_priority_t override)
373 {
374 if (index != IE_REQ_NONE) {
375 ipc_port_request_t ipr, table;
376
377 require_ip_active(port);
378
379 table = port->ip_requests;
380 assert(table != IPR_NULL);
381
382 ipr = &table[index];
383 assert(ipr->ipr_name == name);
384
385 /* Is there a valid destination? */
386 if (IPR_SOR_SPREQ(ipr->ipr_soright)) {
387 ipr->ipr_soright = IPR_SOR_MAKE(ipr->ipr_soright, IPR_SOR_SPARM_MASK);
388 port->ip_sprequests = 1;
389
390 if (option & MACH_SEND_OVERRIDE) {
391 /* apply override to message queue */
392 ipc_mqueue_override_send(&port->ip_messages, override);
393 }
394
395 #if IMPORTANCE_INHERITANCE
396 if (((option & MACH_SEND_NOIMPORTANCE) == 0) &&
397 (port->ip_impdonation != 0) &&
398 (port->ip_spimportant == 0) &&
399 (((option & MACH_SEND_IMPORTANCE) != 0) ||
400 (task_is_importance_donor(current_task())))) {
401 return TRUE;
402 }
403 #else
404 return TRUE;
405 #endif /* IMPORTANCE_INHERITANCE */
406 }
407 }
408 return FALSE;
409 }
410
411 /*
412 * Routine: ipc_port_request_type
413 * Purpose:
414 * Determine the type(s) of port requests enabled for a name.
415 * Conditions:
416 * The port must be locked or inactive (to avoid table growth).
417 * The index must not be IE_REQ_NONE and for the name in question.
418 */
419 mach_port_type_t
420 ipc_port_request_type(
421 ipc_port_t port,
422 __assert_only mach_port_name_t name,
423 ipc_port_request_index_t index)
424 {
425 ipc_port_request_t ipr, table;
426 mach_port_type_t type = 0;
427
428 table = port->ip_requests;
429 assert(table != IPR_NULL);
430
431 assert(index != IE_REQ_NONE);
432 ipr = &table[index];
433 assert(ipr->ipr_name == name);
434
435 if (IP_VALID(IPR_SOR_PORT(ipr->ipr_soright))) {
436 type |= MACH_PORT_TYPE_DNREQUEST;
437
438 if (IPR_SOR_SPREQ(ipr->ipr_soright)) {
439 type |= MACH_PORT_TYPE_SPREQUEST;
440
441 if (!IPR_SOR_SPARMED(ipr->ipr_soright)) {
442 type |= MACH_PORT_TYPE_SPREQUEST_DELAYED;
443 }
444 }
445 }
446 return type;
447 }
448
449 /*
450 * Routine: ipc_port_request_cancel
451 * Purpose:
452 * Cancel a dead-name/send-possible request and return the send-once right.
453 * Conditions:
454 * The port must be locked and active.
455 * The index must not be IPR_REQ_NONE and must correspond with name.
456 */
457
458 ipc_port_t
459 ipc_port_request_cancel(
460 ipc_port_t port,
461 __assert_only mach_port_name_t name,
462 ipc_port_request_index_t index)
463 {
464 ipc_port_request_t ipr, table;
465 ipc_port_t request = IP_NULL;
466
467 require_ip_active(port);
468 table = port->ip_requests;
469 assert(table != IPR_NULL);
470
471 assert(index != IE_REQ_NONE);
472 ipr = &table[index];
473 assert(ipr->ipr_name == name);
474 request = IPR_SOR_PORT(ipr->ipr_soright);
475
476 /* return ipr to the free list inside the table */
477 ipr->ipr_name = MACH_PORT_NULL;
478 ipr->ipr_next = table->ipr_next;
479 table->ipr_next = index;
480
481 return request;
482 }
483
484 /*
485 * Routine: ipc_port_pdrequest
486 * Purpose:
487 * Make a port-deleted request, returning the
488 * previously registered send-once right.
489 * Just cancels the previous request if notify is IP_NULL.
490 * Conditions:
491 * The port is locked and active. It is unlocked.
492 * Consumes a ref for notify (if non-null), and
493 * returns previous with a ref (if non-null).
494 */
495
496 void
497 ipc_port_pdrequest(
498 ipc_port_t port,
499 ipc_port_t notify,
500 ipc_port_t *previousp)
501 {
502 ipc_port_t previous;
503 require_ip_active(port);
504
505 previous = port->ip_pdrequest;
506 port->ip_pdrequest = notify;
507 ip_unlock(port);
508
509 *previousp = previous;
510 }
511
512 /*
513 * Routine: ipc_port_nsrequest
514 * Purpose:
515 * Make a no-senders request, returning the
516 * previously registered send-once right.
517 * Just cancels the previous request if notify is IP_NULL.
518 * Conditions:
519 * The port is locked and active. It is unlocked.
520 * Consumes a ref for notify (if non-null), and
521 * returns previous with a ref (if non-null).
522 */
523
524 void
525 ipc_port_nsrequest(
526 ipc_port_t port,
527 mach_port_mscount_t sync,
528 ipc_port_t notify,
529 ipc_port_t *previousp)
530 {
531 ipc_port_t previous;
532 mach_port_mscount_t mscount;
533 require_ip_active(port);
534
535 previous = port->ip_nsrequest;
536 mscount = port->ip_mscount;
537
538 if ((port->ip_srights == 0) && (sync <= mscount) &&
539 (notify != IP_NULL)) {
540 port->ip_nsrequest = IP_NULL;
541 ip_unlock(port);
542 ipc_notify_no_senders(notify, mscount);
543 } else {
544 port->ip_nsrequest = notify;
545 ip_unlock(port);
546 }
547
548 *previousp = previous;
549 }
550
551
552 /*
553 * Routine: ipc_port_clear_receiver
554 * Purpose:
555 * Prepares a receive right for transmission/destruction,
556 * optionally performs mqueue destruction (with port lock held)
557 *
558 * Conditions:
559 * The port is locked and active.
560 * Returns:
561 * If should_destroy is TRUE, then the return value indicates
562 * whether the caller needs to reap kmsg structures that should
563 * be destroyed (by calling ipc_kmsg_reap_delayed)
564 *
565 * If should_destroy is FALSE, this always returns FALSE
566 */
567
568 boolean_t
569 ipc_port_clear_receiver(
570 ipc_port_t port,
571 boolean_t should_destroy)
572 {
573 ipc_mqueue_t mqueue = &port->ip_messages;
574 boolean_t reap_messages = FALSE;
575
576 /*
577 * Pull ourselves out of any sets to which we belong.
578 * We hold the port locked, so even though this acquires and releases
579 * the mqueue lock, we know we won't be added to any other sets.
580 */
581 if (port->ip_in_pset != 0) {
582 ipc_pset_remove_from_all(port);
583 assert(port->ip_in_pset == 0);
584 }
585
586 /*
587 * Send anyone waiting on the port's queue directly away.
588 * Also clear the mscount, seqno, guard bits
589 */
590 imq_lock(mqueue);
591 if (port->ip_receiver_name) {
592 ipc_mqueue_changed(port->ip_receiver, mqueue);
593 } else {
594 ipc_mqueue_changed(NULL, mqueue);
595 }
596 port->ip_mscount = 0;
597 mqueue->imq_seqno = 0;
598 port->ip_context = port->ip_guarded = port->ip_strict_guard = 0;
599 /*
600 * clear the immovable bit so the port can move back to anyone listening
601 * for the port destroy notification
602 */
603 port->ip_immovable_receive = 0;
604
605 if (should_destroy) {
606 /*
607 * Mark the port and mqueue invalid, preventing further send/receive
608 * operations from succeeding. It's important for this to be
609 * done under the same lock hold as the ipc_mqueue_changed
610 * call to avoid additional threads blocking on an mqueue
611 * that's being destroyed.
612 *
613 * The port active bit needs to be guarded under mqueue lock for
614 * turnstiles
615 */
616 port->ip_object.io_bits &= ~IO_BITS_ACTIVE;
617 port->ip_timestamp = ipc_port_timestamp();
618 reap_messages = ipc_mqueue_destroy_locked(mqueue);
619 } else {
620 /* make port be in limbo */
621 port->ip_receiver_name = MACH_PORT_NULL;
622 port->ip_destination = IP_NULL;
623 }
624
625 imq_unlock(&port->ip_messages);
626
627 return reap_messages;
628 }
629
630 /*
631 * Routine: ipc_port_init
632 * Purpose:
633 * Initializes a newly-allocated port.
634 * Doesn't touch the ip_object fields.
635 */
636
637 void
638 ipc_port_init(
639 ipc_port_t port,
640 ipc_space_t space,
641 mach_port_name_t name)
642 {
643 /* port->ip_kobject doesn't have to be initialized */
644
645 port->ip_receiver = space;
646 port->ip_receiver_name = name;
647
648 port->ip_mscount = 0;
649 port->ip_srights = 0;
650 port->ip_sorights = 0;
651
652 port->ip_nsrequest = IP_NULL;
653 port->ip_pdrequest = IP_NULL;
654 port->ip_requests = IPR_NULL;
655
656 port->ip_premsg = IKM_NULL;
657 port->ip_context = 0;
658 port->ip_reply_context = 0;
659
660 port->ip_sprequests = 0;
661 port->ip_spimportant = 0;
662 port->ip_impdonation = 0;
663 port->ip_tempowner = 0;
664
665 port->ip_guarded = 0;
666 port->ip_strict_guard = 0;
667 port->ip_immovable_receive = 0;
668 port->ip_no_grant = 0;
669 port->ip_immovable_send = 0;
670 port->ip_impcount = 0;
671
672 port->ip_specialreply = 0;
673 port->ip_sync_link_state = PORT_SYNC_LINK_ANY;
674 port->ip_sync_bootstrap_checkin = 0;
675 port->ip_watchport_elem = NULL;
676
677 ipc_special_reply_port_bits_reset(port);
678
679 port->ip_send_turnstile = TURNSTILE_NULL;
680
681 ipc_mqueue_init(&port->ip_messages,
682 FALSE /* !set */);
683 }
684
685 /*
686 * Routine: ipc_port_alloc
687 * Purpose:
688 * Allocate a port.
689 * Conditions:
690 * Nothing locked. If successful, the port is returned
691 * locked. (The caller doesn't have a reference.)
692 * Returns:
693 * KERN_SUCCESS The port is allocated.
694 * KERN_INVALID_TASK The space is dead.
695 * KERN_NO_SPACE No room for an entry in the space.
696 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
697 */
698
699 kern_return_t
700 ipc_port_alloc(
701 ipc_space_t space,
702 bool make_send_right,
703 mach_port_name_t *namep,
704 ipc_port_t *portp)
705 {
706 ipc_port_t port;
707 mach_port_name_t name;
708 kern_return_t kr;
709 mach_port_type_t type = MACH_PORT_TYPE_RECEIVE;
710 mach_port_urefs_t urefs = 0;
711
712 #if MACH_ASSERT
713 uintptr_t buf[IP_CALLSTACK_MAX];
714 ipc_port_callstack_init_debug(&buf[0], IP_CALLSTACK_MAX);
715 #endif /* MACH_ASSERT */
716
717 if (make_send_right) {
718 type |= MACH_PORT_TYPE_SEND;
719 urefs = 1;
720 }
721 kr = ipc_object_alloc(space, IOT_PORT, type, urefs,
722 &name, (ipc_object_t *) &port);
723 if (kr != KERN_SUCCESS) {
724 return kr;
725 }
726
727 /* port and space are locked */
728 ipc_port_init(port, space, name);
729
730 if (make_send_right) {
731 /* ipc_object_alloc() already made the entry reference */
732 port->ip_srights++;
733 port->ip_mscount++;
734 }
735
736 #if MACH_ASSERT
737 ipc_port_init_debug(port, &buf[0], IP_CALLSTACK_MAX);
738 #endif /* MACH_ASSERT */
739
740 /* unlock space after init */
741 is_write_unlock(space);
742
743 *namep = name;
744 *portp = port;
745
746 return KERN_SUCCESS;
747 }
748
749 /*
750 * Routine: ipc_port_alloc_name
751 * Purpose:
752 * Allocate a port, with a specific name.
753 * Conditions:
754 * Nothing locked. If successful, the port is returned
755 * locked. (The caller doesn't have a reference.)
756 * Returns:
757 * KERN_SUCCESS The port is allocated.
758 * KERN_INVALID_TASK The space is dead.
759 * KERN_NAME_EXISTS The name already denotes a right.
760 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
761 */
762
763 kern_return_t
764 ipc_port_alloc_name(
765 ipc_space_t space,
766 mach_port_name_t name,
767 ipc_port_t *portp)
768 {
769 ipc_port_t port;
770 kern_return_t kr;
771
772 #if MACH_ASSERT
773 uintptr_t buf[IP_CALLSTACK_MAX];
774 ipc_port_callstack_init_debug(&buf[0], IP_CALLSTACK_MAX);
775 #endif /* MACH_ASSERT */
776
777 kr = ipc_object_alloc_name(space, IOT_PORT,
778 MACH_PORT_TYPE_RECEIVE, 0,
779 name, (ipc_object_t *) &port);
780 if (kr != KERN_SUCCESS) {
781 return kr;
782 }
783
784 /* port is locked */
785
786 ipc_port_init(port, space, name);
787
788 #if MACH_ASSERT
789 ipc_port_init_debug(port, &buf[0], IP_CALLSTACK_MAX);
790 #endif /* MACH_ASSERT */
791
792 *portp = port;
793
794 return KERN_SUCCESS;
795 }
796
797 /*
798 * Routine: ipc_port_spnotify
799 * Purpose:
800 * Generate send-possible port notifications.
801 * Conditions:
802 * Nothing locked, reference held on port.
803 */
804 void
805 ipc_port_spnotify(
806 ipc_port_t port)
807 {
808 ipc_port_request_index_t index = 0;
809 ipc_table_elems_t size = 0;
810
811 /*
812 * If the port has no send-possible request
813 * armed, don't bother to lock the port.
814 */
815 if (port->ip_sprequests == 0) {
816 return;
817 }
818
819 ip_lock(port);
820
821 #if IMPORTANCE_INHERITANCE
822 if (port->ip_spimportant != 0) {
823 port->ip_spimportant = 0;
824 if (ipc_port_importance_delta(port, IPID_OPTION_NORMAL, -1) == TRUE) {
825 ip_lock(port);
826 }
827 }
828 #endif /* IMPORTANCE_INHERITANCE */
829
830 if (port->ip_sprequests == 0) {
831 ip_unlock(port);
832 return;
833 }
834 port->ip_sprequests = 0;
835
836 revalidate:
837 if (ip_active(port)) {
838 ipc_port_request_t requests;
839
840 /* table may change each time port unlocked (reload) */
841 requests = port->ip_requests;
842 assert(requests != IPR_NULL);
843
844 /*
845 * no need to go beyond table size when first
846 * we entered - those are future notifications.
847 */
848 if (size == 0) {
849 size = requests->ipr_size->its_size;
850 }
851
852 /* no need to backtrack either */
853 while (++index < size) {
854 ipc_port_request_t ipr = &requests[index];
855 mach_port_name_t name = ipr->ipr_name;
856 ipc_port_t soright = IPR_SOR_PORT(ipr->ipr_soright);
857 boolean_t armed = IPR_SOR_SPARMED(ipr->ipr_soright);
858
859 if (MACH_PORT_VALID(name) && armed && IP_VALID(soright)) {
860 /* claim send-once right - slot still inuse */
861 ipr->ipr_soright = IP_NULL;
862 ip_unlock(port);
863
864 ipc_notify_send_possible(soright, name);
865
866 ip_lock(port);
867 goto revalidate;
868 }
869 }
870 }
871 ip_unlock(port);
872 return;
873 }
874
875 /*
876 * Routine: ipc_port_dnnotify
877 * Purpose:
878 * Generate dead name notifications for
879 * all outstanding dead-name and send-
880 * possible requests.
881 * Conditions:
882 * Nothing locked.
883 * Port must be inactive.
884 * Reference held on port.
885 */
886 void
887 ipc_port_dnnotify(
888 ipc_port_t port)
889 {
890 ipc_port_request_t requests = port->ip_requests;
891
892 assert(!ip_active(port));
893 if (requests != IPR_NULL) {
894 ipc_table_size_t its = requests->ipr_size;
895 ipc_table_elems_t size = its->its_size;
896 ipc_port_request_index_t index;
897 for (index = 1; index < size; index++) {
898 ipc_port_request_t ipr = &requests[index];
899 mach_port_name_t name = ipr->ipr_name;
900 ipc_port_t soright = IPR_SOR_PORT(ipr->ipr_soright);
901
902 if (MACH_PORT_VALID(name) && IP_VALID(soright)) {
903 ipc_notify_dead_name(soright, name);
904 }
905 }
906 }
907 }
908
909
910 /*
911 * Routine: ipc_port_destroy
912 * Purpose:
913 * Destroys a port. Cleans up queued messages.
914 *
915 * If the port has a backup, it doesn't get destroyed,
916 * but is sent in a port-destroyed notification to the backup.
917 * Conditions:
918 * The port is locked and alive; nothing else locked.
919 * The caller has a reference, which is consumed.
920 * Afterwards, the port is unlocked and dead.
921 */
922
923 void
924 ipc_port_destroy(ipc_port_t port)
925 {
926 ipc_port_t pdrequest, nsrequest;
927 ipc_mqueue_t mqueue;
928 ipc_kmsg_t kmsg;
929 boolean_t special_reply = port->ip_specialreply;
930 struct task_watchport_elem *watchport_elem = NULL;
931
932 #if IMPORTANCE_INHERITANCE
933 ipc_importance_task_t release_imp_task = IIT_NULL;
934 thread_t self = current_thread();
935 boolean_t top = (self->ith_assertions == 0);
936 natural_t assertcnt = 0;
937 #endif /* IMPORTANCE_INHERITANCE */
938
939 require_ip_active(port);
940 /* port->ip_receiver_name is garbage */
941 /* port->ip_receiver/port->ip_destination is garbage */
942
943 /* clear any reply-port context */
944 port->ip_reply_context = 0;
945
946 /* check for a backup port */
947 pdrequest = port->ip_pdrequest;
948
949 #if IMPORTANCE_INHERITANCE
950 /* determine how many assertions to drop and from whom */
951 if (port->ip_tempowner != 0) {
952 assert(top);
953 release_imp_task = port->ip_imp_task;
954 if (IIT_NULL != release_imp_task) {
955 port->ip_imp_task = IIT_NULL;
956 assertcnt = port->ip_impcount;
957 }
958 /* Otherwise, nothing to drop */
959 } else {
960 assertcnt = port->ip_impcount;
961 if (pdrequest != IP_NULL) {
962 /* mark in limbo for the journey */
963 port->ip_tempowner = 1;
964 }
965 }
966
967 if (top) {
968 self->ith_assertions = assertcnt;
969 }
970 #endif /* IMPORTANCE_INHERITANCE */
971
972 if (pdrequest != IP_NULL) {
973 /* clear receiver, don't destroy the port */
974 (void)ipc_port_clear_receiver(port, FALSE);
975 assert(port->ip_in_pset == 0);
976 assert(port->ip_mscount == 0);
977
978 /* we assume the ref for pdrequest */
979 port->ip_pdrequest = IP_NULL;
980
981 imq_lock(&port->ip_messages);
982 watchport_elem = ipc_port_clear_watchport_elem_internal(port);
983 ipc_port_send_turnstile_recompute_push_locked(port);
984 /* mqueue and port unlocked */
985
986 if (special_reply) {
987 ipc_port_adjust_special_reply_port(port,
988 IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE, FALSE);
989 }
990
991 if (watchport_elem) {
992 task_watchport_elem_deallocate(watchport_elem);
993 watchport_elem = NULL;
994 }
995 /* consumes our refs for port and pdrequest */
996 ipc_notify_port_destroyed(pdrequest, port);
997
998 goto drop_assertions;
999 }
1000
1001 /*
1002 * The mach_msg_* paths don't hold a port lock, they only hold a
1003 * reference to the port object. If a thread raced us and is now
1004 * blocked waiting for message reception on this mqueue (or waiting
1005 * for ipc_mqueue_full), it will never be woken up. We call
1006 * ipc_port_clear_receiver() here, _after_ the port has been marked
1007 * inactive, to wakeup any threads which may be blocked and ensure
1008 * that no other thread can get lost waiting for a wake up on a
1009 * port/mqueue that's been destroyed.
1010 */
1011 boolean_t reap_msgs = FALSE;
1012 reap_msgs = ipc_port_clear_receiver(port, TRUE); /* marks port and mqueue inactive */
1013 assert(port->ip_in_pset == 0);
1014 assert(port->ip_mscount == 0);
1015
1016 imq_lock(&port->ip_messages);
1017 watchport_elem = ipc_port_clear_watchport_elem_internal(port);
1018 imq_unlock(&port->ip_messages);
1019 nsrequest = port->ip_nsrequest;
1020
1021 /*
1022 * If the port has a preallocated message buffer and that buffer
1023 * is not inuse, free it. If it has an inuse one, then the kmsg
1024 * free will detect that we freed the association and it can free it
1025 * like a normal buffer.
1026 *
1027 * Once the port is marked inactive we don't need to keep it locked.
1028 */
1029 if (IP_PREALLOC(port)) {
1030 ipc_port_t inuse_port;
1031
1032 kmsg = port->ip_premsg;
1033 assert(kmsg != IKM_NULL);
1034 inuse_port = ikm_prealloc_inuse_port(kmsg);
1035 ipc_kmsg_clear_prealloc(kmsg, port);
1036
1037 imq_lock(&port->ip_messages);
1038 ipc_port_send_turnstile_recompute_push_locked(port);
1039 /* mqueue and port unlocked */
1040
1041 if (inuse_port != IP_NULL) {
1042 assert(inuse_port == port);
1043 } else {
1044 ipc_kmsg_free(kmsg);
1045 }
1046 } else {
1047 imq_lock(&port->ip_messages);
1048 ipc_port_send_turnstile_recompute_push_locked(port);
1049 /* mqueue and port unlocked */
1050 }
1051
1052 /* Deallocate the watchport element */
1053 if (watchport_elem) {
1054 task_watchport_elem_deallocate(watchport_elem);
1055 watchport_elem = NULL;
1056 }
1057
1058 /* unlink the kmsg from special reply port */
1059 if (special_reply) {
1060 ipc_port_adjust_special_reply_port(port,
1061 IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE, FALSE);
1062 }
1063
1064 /* throw away no-senders request */
1065 if (nsrequest != IP_NULL) {
1066 ipc_notify_send_once(nsrequest); /* consumes ref */
1067 }
1068 /*
1069 * Reap any kmsg objects waiting to be destroyed.
1070 * This must be done after we've released the port lock.
1071 */
1072 if (reap_msgs) {
1073 ipc_kmsg_reap_delayed();
1074 }
1075
1076 mqueue = &port->ip_messages;
1077
1078 /* cleanup waitq related resources */
1079 ipc_mqueue_deinit(mqueue);
1080
1081 /* generate dead-name notifications */
1082 ipc_port_dnnotify(port);
1083
1084 ipc_kobject_destroy(port);
1085
1086 ip_release(port); /* consume caller's ref */
1087
1088 drop_assertions:
1089 #if IMPORTANCE_INHERITANCE
1090 if (release_imp_task != IIT_NULL) {
1091 if (assertcnt > 0) {
1092 assert(top);
1093 self->ith_assertions = 0;
1094 assert(ipc_importance_task_is_any_receiver_type(release_imp_task));
1095 ipc_importance_task_drop_internal_assertion(release_imp_task, assertcnt);
1096 }
1097 ipc_importance_task_release(release_imp_task);
1098 } else if (assertcnt > 0) {
1099 if (top) {
1100 self->ith_assertions = 0;
1101 release_imp_task = current_task()->task_imp_base;
1102 if (ipc_importance_task_is_any_receiver_type(release_imp_task)) {
1103 ipc_importance_task_drop_internal_assertion(release_imp_task, assertcnt);
1104 }
1105 }
1106 }
1107 #endif /* IMPORTANCE_INHERITANCE */
1108 }
1109
1110 /*
1111 * Routine: ipc_port_check_circularity
1112 * Purpose:
1113 * Check if queueing "port" in a message for "dest"
1114 * would create a circular group of ports and messages.
1115 *
1116 * If no circularity (FALSE returned), then "port"
1117 * is changed from "in limbo" to "in transit".
1118 *
1119 * That is, we want to set port->ip_destination == dest,
1120 * but guaranteeing that this doesn't create a circle
1121 * port->ip_destination->ip_destination->... == port
1122 *
1123 * Conditions:
1124 * No ports locked. References held for "port" and "dest".
1125 */
1126
1127 boolean_t
1128 ipc_port_check_circularity(
1129 ipc_port_t port,
1130 ipc_port_t dest)
1131 {
1132 #if IMPORTANCE_INHERITANCE
1133 /* adjust importance counts at the same time */
1134 return ipc_importance_check_circularity(port, dest);
1135 #else
1136 ipc_port_t base;
1137 struct task_watchport_elem *watchport_elem = NULL;
1138
1139 assert(port != IP_NULL);
1140 assert(dest != IP_NULL);
1141
1142 if (port == dest) {
1143 return TRUE;
1144 }
1145 base = dest;
1146
1147 /* Check if destination needs a turnstile */
1148 ipc_port_send_turnstile_prepare(dest);
1149
1150 /*
1151 * First try a quick check that can run in parallel.
1152 * No circularity if dest is not in transit.
1153 */
1154 ip_lock(port);
1155 if (ip_lock_try(dest)) {
1156 if (!ip_active(dest) ||
1157 (dest->ip_receiver_name != MACH_PORT_NULL) ||
1158 (dest->ip_destination == IP_NULL)) {
1159 goto not_circular;
1160 }
1161
1162 /* dest is in transit; further checking necessary */
1163
1164 ip_unlock(dest);
1165 }
1166 ip_unlock(port);
1167
1168 ipc_port_multiple_lock(); /* massive serialization */
1169
1170 /*
1171 * Search for the end of the chain (a port not in transit),
1172 * acquiring locks along the way.
1173 */
1174
1175 for (;;) {
1176 ip_lock(base);
1177
1178 if (!ip_active(base) ||
1179 (base->ip_receiver_name != MACH_PORT_NULL) ||
1180 (base->ip_destination == IP_NULL)) {
1181 break;
1182 }
1183
1184 base = base->ip_destination;
1185 }
1186
1187 /* all ports in chain from dest to base, inclusive, are locked */
1188
1189 if (port == base) {
1190 /* circularity detected! */
1191
1192 ipc_port_multiple_unlock();
1193
1194 /* port (== base) is in limbo */
1195 require_ip_active(port);
1196 assert(port->ip_receiver_name == MACH_PORT_NULL);
1197 assert(port->ip_destination == IP_NULL);
1198
1199 base = dest;
1200 while (base != IP_NULL) {
1201 ipc_port_t next;
1202
1203 /* dest is in transit or in limbo */
1204 require_ip_active(base);
1205 assert(base->ip_receiver_name == MACH_PORT_NULL);
1206
1207 next = base->ip_destination;
1208 ip_unlock(base);
1209 base = next;
1210 }
1211
1212 ipc_port_send_turnstile_complete(dest);
1213 return TRUE;
1214 }
1215
1216 /*
1217 * The guarantee: lock port while the entire chain is locked.
1218 * Once port is locked, we can take a reference to dest,
1219 * add port to the chain, and unlock everything.
1220 */
1221
1222 ip_lock(port);
1223 ipc_port_multiple_unlock();
1224
1225 not_circular:
1226 imq_lock(&port->ip_messages);
1227
1228 /* port is in limbo */
1229 require_ip_active(port);
1230 assert(port->ip_receiver_name == MACH_PORT_NULL);
1231 assert(port->ip_destination == IP_NULL);
1232
1233 /* Clear the watchport boost */
1234 watchport_elem = ipc_port_clear_watchport_elem_internal(port);
1235
1236 /* Check if the port is being enqueued as a part of sync bootstrap checkin */
1237 if (dest->ip_specialreply && dest->ip_sync_bootstrap_checkin) {
1238 port->ip_sync_bootstrap_checkin = 1;
1239 }
1240
1241 ip_reference(dest);
1242 port->ip_destination = dest;
1243
1244 /* Setup linkage for source port if it has sync ipc push */
1245 struct turnstile *send_turnstile = TURNSTILE_NULL;
1246 if (port_send_turnstile(port)) {
1247 send_turnstile = turnstile_prepare((uintptr_t)port,
1248 port_send_turnstile_address(port),
1249 TURNSTILE_NULL, TURNSTILE_SYNC_IPC);
1250
1251 /*
1252 * What ipc_port_adjust_port_locked would do,
1253 * but we need to also drop even more locks before
1254 * calling turnstile_update_inheritor_complete().
1255 */
1256 ipc_port_adjust_sync_link_state_locked(port, PORT_SYNC_LINK_ANY, NULL);
1257
1258 turnstile_update_inheritor(send_turnstile, port_send_turnstile(dest),
1259 (TURNSTILE_INHERITOR_TURNSTILE | TURNSTILE_IMMEDIATE_UPDATE));
1260
1261 /* update complete and turnstile complete called after dropping all locks */
1262 }
1263 imq_unlock(&port->ip_messages);
1264
1265 /* now unlock chain */
1266
1267 ip_unlock(port);
1268
1269 for (;;) {
1270 ipc_port_t next;
1271
1272 if (dest == base) {
1273 break;
1274 }
1275
1276 /* port is in transit */
1277 require_ip_active(dest);
1278 assert(dest->ip_receiver_name == MACH_PORT_NULL);
1279 assert(dest->ip_destination != IP_NULL);
1280
1281 next = dest->ip_destination;
1282 ip_unlock(dest);
1283 dest = next;
1284 }
1285
1286 /* base is not in transit */
1287 assert(!ip_active(base) ||
1288 (base->ip_receiver_name != MACH_PORT_NULL) ||
1289 (base->ip_destination == IP_NULL));
1290
1291 ip_unlock(base);
1292
1293 /* All locks dropped, call turnstile_update_inheritor_complete for source port's turnstile */
1294 if (send_turnstile) {
1295 turnstile_update_inheritor_complete(send_turnstile, TURNSTILE_INTERLOCK_NOT_HELD);
1296
1297 /* Take the mq lock to call turnstile complete */
1298 imq_lock(&port->ip_messages);
1299 turnstile_complete((uintptr_t)port, port_send_turnstile_address(port), NULL, TURNSTILE_SYNC_IPC);
1300 send_turnstile = TURNSTILE_NULL;
1301 imq_unlock(&port->ip_messages);
1302 turnstile_cleanup();
1303 }
1304
1305 if (watchport_elem) {
1306 task_watchport_elem_deallocate(watchport_elem);
1307 }
1308
1309 return FALSE;
1310 #endif /* !IMPORTANCE_INHERITANCE */
1311 }
1312
1313 /*
1314 * Update the recv turnstile inheritor for a port.
1315 *
1316 * Sync IPC through the port receive turnstile only happens for the special
1317 * reply port case. It has three sub-cases:
1318 *
1319 * 1. a send-once right is in transit, and pushes on the send turnstile of its
1320 * destination mqueue.
1321 *
1322 * 2. a send-once right has been stashed on a knote it was copied out "through",
1323 * as the first such copied out port.
1324 *
1325 * 3. a send-once right has been stashed on a knote it was copied out "through",
1326 * as the second or more copied out port.
1327 */
1328 void
1329 ipc_port_recv_update_inheritor(
1330 ipc_port_t port,
1331 struct turnstile *rcv_turnstile,
1332 turnstile_update_flags_t flags)
1333 {
1334 struct turnstile *inheritor = TURNSTILE_NULL;
1335 struct knote *kn;
1336
1337 if (ip_active(port) && port->ip_specialreply) {
1338 imq_held(&port->ip_messages);
1339
1340 switch (port->ip_sync_link_state) {
1341 case PORT_SYNC_LINK_PORT:
1342 if (port->ip_sync_inheritor_port != NULL) {
1343 inheritor = port_send_turnstile(port->ip_sync_inheritor_port);
1344 }
1345 break;
1346
1347 case PORT_SYNC_LINK_WORKLOOP_KNOTE:
1348 kn = port->ip_sync_inheritor_knote;
1349 inheritor = filt_ipc_kqueue_turnstile(kn);
1350 break;
1351
1352 case PORT_SYNC_LINK_WORKLOOP_STASH:
1353 inheritor = port->ip_sync_inheritor_ts;
1354 break;
1355 }
1356 }
1357
1358 turnstile_update_inheritor(rcv_turnstile, inheritor,
1359 flags | TURNSTILE_INHERITOR_TURNSTILE);
1360 }
1361
1362 /*
1363 * Update the send turnstile inheritor for a port.
1364 *
1365 * Sync IPC through the port send turnstile has 7 possible reasons to be linked:
1366 *
1367 * 1. a special reply port is part of sync ipc for bootstrap checkin and needs
1368 * to push on thread doing the sync ipc.
1369 *
1370 * 2. a receive right is in transit, and pushes on the send turnstile of its
1371 * destination mqueue.
1372 *
1373 * 3. port was passed as an exec watchport and port is pushing on main thread
1374 * of the task.
1375 *
1376 * 4. a receive right has been stashed on a knote it was copied out "through",
1377 * as the first such copied out port (same as PORT_SYNC_LINK_WORKLOOP_KNOTE
1378 * for the special reply port)
1379 *
1380 * 5. a receive right has been stashed on a knote it was copied out "through",
1381 * as the second or more copied out port (same as
1382 * PORT_SYNC_LINK_WORKLOOP_STASH for the special reply port)
1383 *
1384 * 6. a receive right has been copied out as a part of sync bootstrap checkin
1385 * and needs to push on thread doing the sync bootstrap checkin.
1386 *
1387 * 7. the receive right is monitored by a knote, and pushes on any that is
1388 * registered on a workloop. filt_machport makes sure that if such a knote
1389 * exists, it is kept as the first item in the knote list, so we never need
1390 * to walk.
1391 */
1392 void
1393 ipc_port_send_update_inheritor(
1394 ipc_port_t port,
1395 struct turnstile *send_turnstile,
1396 turnstile_update_flags_t flags)
1397 {
1398 ipc_mqueue_t mqueue = &port->ip_messages;
1399 turnstile_inheritor_t inheritor = TURNSTILE_INHERITOR_NULL;
1400 struct knote *kn;
1401 turnstile_update_flags_t inheritor_flags = TURNSTILE_INHERITOR_TURNSTILE;
1402
1403 assert(imq_held(mqueue));
1404
1405 if (!ip_active(port)) {
1406 /* this port is no longer active, it should not push anywhere */
1407 } else if (port->ip_specialreply) {
1408 /* Case 1. */
1409 if (port->ip_sync_bootstrap_checkin && prioritize_launch) {
1410 inheritor = port->ip_messages.imq_srp_owner_thread;
1411 inheritor_flags = TURNSTILE_INHERITOR_THREAD;
1412 }
1413 } else if (port->ip_receiver_name == MACH_PORT_NULL &&
1414 port->ip_destination != NULL) {
1415 /* Case 2. */
1416 inheritor = port_send_turnstile(port->ip_destination);
1417 } else if (port->ip_watchport_elem != NULL) {
1418 /* Case 3. */
1419 if (prioritize_launch) {
1420 assert(port->ip_sync_link_state == PORT_SYNC_LINK_ANY);
1421 inheritor = ipc_port_get_watchport_inheritor(port);
1422 inheritor_flags = TURNSTILE_INHERITOR_THREAD;
1423 }
1424 } else if (port->ip_sync_link_state == PORT_SYNC_LINK_WORKLOOP_KNOTE) {
1425 /* Case 4. */
1426 inheritor = filt_ipc_kqueue_turnstile(mqueue->imq_inheritor_knote);
1427 } else if (port->ip_sync_link_state == PORT_SYNC_LINK_WORKLOOP_STASH) {
1428 /* Case 5. */
1429 inheritor = mqueue->imq_inheritor_turnstile;
1430 } else if (port->ip_sync_link_state == PORT_SYNC_LINK_RCV_THREAD) {
1431 /* Case 6. */
1432 if (prioritize_launch) {
1433 inheritor = port->ip_messages.imq_inheritor_thread_ref;
1434 inheritor_flags = TURNSTILE_INHERITOR_THREAD;
1435 }
1436 } else if ((kn = SLIST_FIRST(&mqueue->imq_klist))) {
1437 /* Case 7. Push on a workloop that is interested */
1438 if (filt_machport_kqueue_has_turnstile(kn)) {
1439 assert(port->ip_sync_link_state == PORT_SYNC_LINK_ANY);
1440 inheritor = filt_ipc_kqueue_turnstile(kn);
1441 }
1442 }
1443
1444 turnstile_update_inheritor(send_turnstile, inheritor,
1445 flags | inheritor_flags);
1446 }
1447
1448 /*
1449 * Routine: ipc_port_send_turnstile_prepare
1450 * Purpose:
1451 * Get a reference on port's send turnstile, if
1452 * port does not have a send turnstile then allocate one.
1453 *
1454 * Conditions:
1455 * Nothing is locked.
1456 */
1457 void
1458 ipc_port_send_turnstile_prepare(ipc_port_t port)
1459 {
1460 struct turnstile *turnstile = TURNSTILE_NULL;
1461 struct turnstile *send_turnstile = TURNSTILE_NULL;
1462
1463 retry_alloc:
1464 imq_lock(&port->ip_messages);
1465
1466 if (port_send_turnstile(port) == NULL ||
1467 port_send_turnstile(port)->ts_port_ref == 0) {
1468 if (turnstile == TURNSTILE_NULL) {
1469 imq_unlock(&port->ip_messages);
1470 turnstile = turnstile_alloc();
1471 goto retry_alloc;
1472 }
1473
1474 send_turnstile = turnstile_prepare((uintptr_t)port,
1475 port_send_turnstile_address(port),
1476 turnstile, TURNSTILE_SYNC_IPC);
1477 turnstile = TURNSTILE_NULL;
1478
1479 ipc_port_send_update_inheritor(port, send_turnstile,
1480 TURNSTILE_IMMEDIATE_UPDATE);
1481
1482 /* turnstile complete will be called in ipc_port_send_turnstile_complete */
1483 }
1484
1485 /* Increment turnstile counter */
1486 port_send_turnstile(port)->ts_port_ref++;
1487 imq_unlock(&port->ip_messages);
1488
1489 if (send_turnstile) {
1490 turnstile_update_inheritor_complete(send_turnstile,
1491 TURNSTILE_INTERLOCK_NOT_HELD);
1492 }
1493 if (turnstile != TURNSTILE_NULL) {
1494 turnstile_deallocate(turnstile);
1495 }
1496 }
1497
1498
1499 /*
1500 * Routine: ipc_port_send_turnstile_complete
1501 * Purpose:
1502 * Drop a ref on the port's send turnstile, if the
1503 * ref becomes zero, deallocate the turnstile.
1504 *
1505 * Conditions:
1506 * The space might be locked, use safe deallocate.
1507 */
1508 void
1509 ipc_port_send_turnstile_complete(ipc_port_t port)
1510 {
1511 struct turnstile *turnstile = TURNSTILE_NULL;
1512
1513 /* Drop turnstile count on dest port */
1514 imq_lock(&port->ip_messages);
1515
1516 port_send_turnstile(port)->ts_port_ref--;
1517 if (port_send_turnstile(port)->ts_port_ref == 0) {
1518 turnstile_complete((uintptr_t)port, port_send_turnstile_address(port),
1519 &turnstile, TURNSTILE_SYNC_IPC);
1520 assert(turnstile != TURNSTILE_NULL);
1521 }
1522 imq_unlock(&port->ip_messages);
1523 turnstile_cleanup();
1524
1525 if (turnstile != TURNSTILE_NULL) {
1526 turnstile_deallocate_safe(turnstile);
1527 turnstile = TURNSTILE_NULL;
1528 }
1529 }
1530
1531 /*
1532 * Routine: ipc_port_rcv_turnstile
1533 * Purpose:
1534 * Get the port's receive turnstile
1535 *
1536 * Conditions:
1537 * mqueue locked or thread waiting on turnstile is locked.
1538 */
1539 static struct turnstile *
1540 ipc_port_rcv_turnstile(ipc_port_t port)
1541 {
1542 return turnstile_lookup_by_proprietor((uintptr_t)port, TURNSTILE_SYNC_IPC);
1543 }
1544
1545
1546 /*
1547 * Routine: ipc_port_rcv_turnstile_waitq
1548 * Purpose:
1549 * Given the mqueue's waitq, find the port's
1550 * rcv turnstile and return its waitq.
1551 *
1552 * Conditions:
1553 * mqueue locked or thread waiting on turnstile is locked.
1554 */
1555 struct waitq *
1556 ipc_port_rcv_turnstile_waitq(struct waitq *waitq)
1557 {
1558 struct waitq *safeq;
1559
1560 ipc_mqueue_t mqueue = imq_from_waitq(waitq);
1561 ipc_port_t port = ip_from_mq(mqueue);
1562 struct turnstile *rcv_turnstile = ipc_port_rcv_turnstile(port);
1563
1564 /* Check if the port has a rcv turnstile */
1565 if (rcv_turnstile != TURNSTILE_NULL) {
1566 safeq = &rcv_turnstile->ts_waitq;
1567 } else {
1568 safeq = global_eventq(waitq);
1569 }
1570 return safeq;
1571 }
1572
1573
1574 /*
1575 * Routine: ipc_port_link_special_reply_port
1576 * Purpose:
1577 * Link the special reply port with the destination port.
1578 * Allocates turnstile to dest port.
1579 *
1580 * Conditions:
1581 * Nothing is locked.
1582 */
1583 void
1584 ipc_port_link_special_reply_port(
1585 ipc_port_t special_reply_port,
1586 ipc_port_t dest_port,
1587 boolean_t sync_bootstrap_checkin)
1588 {
1589 boolean_t drop_turnstile_ref = FALSE;
1590
1591 /* Check if dest_port needs a turnstile */
1592 ipc_port_send_turnstile_prepare(dest_port);
1593
1594 /* Lock the special reply port and establish the linkage */
1595 ip_lock(special_reply_port);
1596 imq_lock(&special_reply_port->ip_messages);
1597
1598 if (sync_bootstrap_checkin && special_reply_port->ip_specialreply) {
1599 special_reply_port->ip_sync_bootstrap_checkin = 1;
1600 }
1601
1602 /* Check if we need to drop the acquired turnstile ref on dest port */
1603 if (!special_reply_port->ip_specialreply ||
1604 special_reply_port->ip_sync_link_state != PORT_SYNC_LINK_ANY ||
1605 special_reply_port->ip_sync_inheritor_port != IPC_PORT_NULL) {
1606 drop_turnstile_ref = TRUE;
1607 } else {
1608 /* take a reference on dest_port */
1609 ip_reference(dest_port);
1610 special_reply_port->ip_sync_inheritor_port = dest_port;
1611 special_reply_port->ip_sync_link_state = PORT_SYNC_LINK_PORT;
1612 }
1613
1614 imq_unlock(&special_reply_port->ip_messages);
1615 ip_unlock(special_reply_port);
1616
1617 if (drop_turnstile_ref) {
1618 ipc_port_send_turnstile_complete(dest_port);
1619 }
1620
1621 return;
1622 }
1623
1624 #if DEVELOPMENT || DEBUG
1625 inline void
1626 ipc_special_reply_port_bits_reset(ipc_port_t special_reply_port)
1627 {
1628 special_reply_port->ip_srp_lost_link = 0;
1629 special_reply_port->ip_srp_msg_sent = 0;
1630 }
1631
1632 static inline void
1633 ipc_special_reply_port_msg_sent_reset(ipc_port_t special_reply_port)
1634 {
1635 if (special_reply_port->ip_specialreply == 1) {
1636 special_reply_port->ip_srp_msg_sent = 0;
1637 }
1638 }
1639
1640 inline void
1641 ipc_special_reply_port_msg_sent(ipc_port_t special_reply_port)
1642 {
1643 if (special_reply_port->ip_specialreply == 1) {
1644 special_reply_port->ip_srp_msg_sent = 1;
1645 }
1646 }
1647
1648 static inline void
1649 ipc_special_reply_port_lost_link(ipc_port_t special_reply_port)
1650 {
1651 if (special_reply_port->ip_specialreply == 1 && special_reply_port->ip_srp_msg_sent == 0) {
1652 special_reply_port->ip_srp_lost_link = 1;
1653 }
1654 }
1655
1656 #else /* DEVELOPMENT || DEBUG */
1657 inline void
1658 ipc_special_reply_port_bits_reset(__unused ipc_port_t special_reply_port)
1659 {
1660 return;
1661 }
1662
1663 static inline void
1664 ipc_special_reply_port_msg_sent_reset(__unused ipc_port_t special_reply_port)
1665 {
1666 return;
1667 }
1668
1669 inline void
1670 ipc_special_reply_port_msg_sent(__unused ipc_port_t special_reply_port)
1671 {
1672 return;
1673 }
1674
1675 static inline void
1676 ipc_special_reply_port_lost_link(__unused ipc_port_t special_reply_port)
1677 {
1678 return;
1679 }
1680 #endif /* DEVELOPMENT || DEBUG */
1681
1682 /*
1683 * Routine: ipc_port_adjust_special_reply_port_locked
1684 * Purpose:
1685 * If the special port has a turnstile, update its inheritor.
1686 * Condition:
1687 * Special reply port locked on entry.
1688 * Special reply port unlocked on return.
1689 * The passed in port is a special reply port.
1690 * Returns:
1691 * None.
1692 */
1693 void
1694 ipc_port_adjust_special_reply_port_locked(
1695 ipc_port_t special_reply_port,
1696 struct knote *kn,
1697 uint8_t flags,
1698 boolean_t get_turnstile)
1699 {
1700 ipc_port_t dest_port = IPC_PORT_NULL;
1701 int sync_link_state = PORT_SYNC_LINK_NO_LINKAGE;
1702 turnstile_inheritor_t inheritor = TURNSTILE_INHERITOR_NULL;
1703 struct turnstile *ts = TURNSTILE_NULL;
1704
1705 assert(special_reply_port->ip_specialreply);
1706
1707 ip_lock_held(special_reply_port); // ip_sync_link_state is touched
1708 imq_lock(&special_reply_port->ip_messages);
1709
1710 if (flags & IPC_PORT_ADJUST_SR_RECEIVED_MSG) {
1711 ipc_special_reply_port_msg_sent_reset(special_reply_port);
1712 }
1713
1714 if (flags & IPC_PORT_ADJUST_UNLINK_THREAD) {
1715 special_reply_port->ip_messages.imq_srp_owner_thread = NULL;
1716 }
1717
1718 if (flags & IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN) {
1719 special_reply_port->ip_sync_bootstrap_checkin = 0;
1720 }
1721
1722 /* Check if the special reply port is marked non-special */
1723 if (special_reply_port->ip_sync_link_state == PORT_SYNC_LINK_ANY) {
1724 if (get_turnstile) {
1725 turnstile_complete((uintptr_t)special_reply_port,
1726 port_rcv_turnstile_address(special_reply_port), NULL, TURNSTILE_SYNC_IPC);
1727 }
1728 imq_unlock(&special_reply_port->ip_messages);
1729 ip_unlock(special_reply_port);
1730 if (get_turnstile) {
1731 turnstile_cleanup();
1732 }
1733 return;
1734 }
1735
1736 if (flags & IPC_PORT_ADJUST_SR_LINK_WORKLOOP) {
1737 if (ITH_KNOTE_VALID(kn, MACH_MSG_TYPE_PORT_SEND_ONCE)) {
1738 inheritor = filt_machport_stash_port(kn, special_reply_port,
1739 &sync_link_state);
1740 }
1741 } else if (flags & IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE) {
1742 sync_link_state = PORT_SYNC_LINK_ANY;
1743 }
1744
1745 /* Check if need to break linkage */
1746 if (!get_turnstile && sync_link_state == PORT_SYNC_LINK_NO_LINKAGE &&
1747 special_reply_port->ip_sync_link_state == PORT_SYNC_LINK_NO_LINKAGE) {
1748 imq_unlock(&special_reply_port->ip_messages);
1749 ip_unlock(special_reply_port);
1750 return;
1751 }
1752
1753 switch (special_reply_port->ip_sync_link_state) {
1754 case PORT_SYNC_LINK_PORT:
1755 dest_port = special_reply_port->ip_sync_inheritor_port;
1756 special_reply_port->ip_sync_inheritor_port = IPC_PORT_NULL;
1757 break;
1758 case PORT_SYNC_LINK_WORKLOOP_KNOTE:
1759 special_reply_port->ip_sync_inheritor_knote = NULL;
1760 break;
1761 case PORT_SYNC_LINK_WORKLOOP_STASH:
1762 special_reply_port->ip_sync_inheritor_ts = NULL;
1763 break;
1764 }
1765
1766 special_reply_port->ip_sync_link_state = sync_link_state;
1767
1768 switch (sync_link_state) {
1769 case PORT_SYNC_LINK_WORKLOOP_KNOTE:
1770 special_reply_port->ip_sync_inheritor_knote = kn;
1771 break;
1772 case PORT_SYNC_LINK_WORKLOOP_STASH:
1773 special_reply_port->ip_sync_inheritor_ts = inheritor;
1774 break;
1775 case PORT_SYNC_LINK_NO_LINKAGE:
1776 if (flags & IPC_PORT_ADJUST_SR_ENABLE_EVENT) {
1777 ipc_special_reply_port_lost_link(special_reply_port);
1778 }
1779 break;
1780 }
1781
1782 /* Get thread's turnstile donated to special reply port */
1783 if (get_turnstile) {
1784 turnstile_complete((uintptr_t)special_reply_port,
1785 port_rcv_turnstile_address(special_reply_port), NULL, TURNSTILE_SYNC_IPC);
1786 } else {
1787 ts = ipc_port_rcv_turnstile(special_reply_port);
1788 if (ts) {
1789 turnstile_reference(ts);
1790 ipc_port_recv_update_inheritor(special_reply_port, ts,
1791 TURNSTILE_IMMEDIATE_UPDATE);
1792 }
1793 }
1794
1795 imq_unlock(&special_reply_port->ip_messages);
1796 ip_unlock(special_reply_port);
1797
1798 if (get_turnstile) {
1799 turnstile_cleanup();
1800 } else if (ts) {
1801 /* Call turnstile cleanup after dropping the interlock */
1802 turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_NOT_HELD);
1803 turnstile_deallocate_safe(ts);
1804 }
1805
1806 /* Release the ref on the dest port and its turnstile */
1807 if (dest_port) {
1808 ipc_port_send_turnstile_complete(dest_port);
1809 /* release the reference on the dest port */
1810 ip_release(dest_port);
1811 }
1812 }
1813
1814 /*
1815 * Routine: ipc_port_adjust_special_reply_port
1816 * Purpose:
1817 * If the special port has a turnstile, update its inheritor.
1818 * Condition:
1819 * Nothing locked.
1820 * Returns:
1821 * None.
1822 */
1823 void
1824 ipc_port_adjust_special_reply_port(
1825 ipc_port_t special_reply_port,
1826 uint8_t flags,
1827 boolean_t get_turnstile)
1828 {
1829 if (special_reply_port->ip_specialreply) {
1830 ip_lock(special_reply_port);
1831 ipc_port_adjust_special_reply_port_locked(special_reply_port, NULL,
1832 flags, get_turnstile);
1833 /* special_reply_port unlocked */
1834 }
1835 if (get_turnstile) {
1836 assert(current_thread()->turnstile != TURNSTILE_NULL);
1837 }
1838 }
1839
1840 /*
1841 * Routine: ipc_port_adjust_sync_link_state_locked
1842 * Purpose:
1843 * Update the sync link state of the port and the
1844 * turnstile inheritor.
1845 * Condition:
1846 * Port and mqueue locked on entry.
1847 * Port and mqueue locked on return.
1848 * Returns:
1849 * None.
1850 */
1851 void
1852 ipc_port_adjust_sync_link_state_locked(
1853 ipc_port_t port,
1854 int sync_link_state,
1855 turnstile_inheritor_t inheritor)
1856 {
1857 switch (port->ip_sync_link_state) {
1858 case PORT_SYNC_LINK_RCV_THREAD:
1859 /* deallocate the thread reference for the inheritor */
1860 thread_deallocate_safe(port->ip_messages.imq_inheritor_thread_ref);
1861 /* Fall through */
1862
1863 default:
1864 klist_init(&port->ip_messages.imq_klist);
1865 }
1866
1867 switch (sync_link_state) {
1868 case PORT_SYNC_LINK_WORKLOOP_KNOTE:
1869 port->ip_messages.imq_inheritor_knote = inheritor;
1870 break;
1871 case PORT_SYNC_LINK_WORKLOOP_STASH:
1872 port->ip_messages.imq_inheritor_turnstile = inheritor;
1873 break;
1874 case PORT_SYNC_LINK_RCV_THREAD:
1875 /* The thread could exit without clearing port state, take a thread ref */
1876 thread_reference((thread_t)inheritor);
1877 port->ip_messages.imq_inheritor_thread_ref = inheritor;
1878 break;
1879 default:
1880 klist_init(&port->ip_messages.imq_klist);
1881 sync_link_state = PORT_SYNC_LINK_ANY;
1882 }
1883
1884 port->ip_sync_link_state = sync_link_state;
1885 }
1886
1887
1888 /*
1889 * Routine: ipc_port_adjust_port_locked
1890 * Purpose:
1891 * If the port has a turnstile, update its inheritor.
1892 * Condition:
1893 * Port locked on entry.
1894 * Port unlocked on return.
1895 * Returns:
1896 * None.
1897 */
1898 void
1899 ipc_port_adjust_port_locked(
1900 ipc_port_t port,
1901 struct knote *kn,
1902 boolean_t sync_bootstrap_checkin)
1903 {
1904 int sync_link_state = PORT_SYNC_LINK_ANY;
1905 turnstile_inheritor_t inheritor = TURNSTILE_INHERITOR_NULL;
1906
1907 ip_lock_held(port); // ip_sync_link_state is touched
1908 imq_held(&port->ip_messages);
1909
1910 assert(!port->ip_specialreply);
1911
1912 if (kn) {
1913 inheritor = filt_machport_stash_port(kn, port, &sync_link_state);
1914 if (sync_link_state == PORT_SYNC_LINK_WORKLOOP_KNOTE) {
1915 inheritor = kn;
1916 }
1917 } else if (sync_bootstrap_checkin) {
1918 inheritor = current_thread();
1919 sync_link_state = PORT_SYNC_LINK_RCV_THREAD;
1920 }
1921
1922 ipc_port_adjust_sync_link_state_locked(port, sync_link_state, inheritor);
1923 port->ip_sync_bootstrap_checkin = 0;
1924
1925 ipc_port_send_turnstile_recompute_push_locked(port);
1926 /* port and mqueue unlocked */
1927 }
1928
1929 /*
1930 * Routine: ipc_port_clear_sync_rcv_thread_boost_locked
1931 * Purpose:
1932 * If the port is pushing on rcv thread, clear it.
1933 * Condition:
1934 * Port locked on entry
1935 * mqueue is not locked.
1936 * Port unlocked on return.
1937 * Returns:
1938 * None.
1939 */
1940 void
1941 ipc_port_clear_sync_rcv_thread_boost_locked(
1942 ipc_port_t port)
1943 {
1944 ip_lock_held(port); // ip_sync_link_state is touched
1945
1946 if (port->ip_sync_link_state != PORT_SYNC_LINK_RCV_THREAD) {
1947 ip_unlock(port);
1948 return;
1949 }
1950
1951 imq_lock(&port->ip_messages);
1952 ipc_port_adjust_sync_link_state_locked(port, PORT_SYNC_LINK_ANY, NULL);
1953
1954 ipc_port_send_turnstile_recompute_push_locked(port);
1955 /* port and mqueue unlocked */
1956 }
1957
1958 /*
1959 * Routine: ipc_port_add_watchport_elem_locked
1960 * Purpose:
1961 * Transfer the turnstile boost of watchport to task calling exec.
1962 * Condition:
1963 * Port locked on entry.
1964 * Port unlocked on return.
1965 * Returns:
1966 * KERN_SUCESS on success.
1967 * KERN_FAILURE otherwise.
1968 */
1969 kern_return_t
1970 ipc_port_add_watchport_elem_locked(
1971 ipc_port_t port,
1972 struct task_watchport_elem *watchport_elem,
1973 struct task_watchport_elem **old_elem)
1974 {
1975 ip_lock_held(port);
1976 imq_held(&port->ip_messages);
1977
1978 /* Watchport boost only works for non-special active ports mapped in an ipc space */
1979 if (!ip_active(port) || port->ip_specialreply ||
1980 port->ip_receiver_name == MACH_PORT_NULL) {
1981 imq_unlock(&port->ip_messages);
1982 ip_unlock(port);
1983 return KERN_FAILURE;
1984 }
1985
1986 if (port->ip_sync_link_state != PORT_SYNC_LINK_ANY) {
1987 /* Sever the linkage if the port was pushing on knote */
1988 ipc_port_adjust_sync_link_state_locked(port, PORT_SYNC_LINK_ANY, NULL);
1989 }
1990
1991 *old_elem = port->ip_watchport_elem;
1992 port->ip_watchport_elem = watchport_elem;
1993
1994 ipc_port_send_turnstile_recompute_push_locked(port);
1995 /* port and mqueue unlocked */
1996 return KERN_SUCCESS;
1997 }
1998
1999 /*
2000 * Routine: ipc_port_clear_watchport_elem_internal_conditional_locked
2001 * Purpose:
2002 * Remove the turnstile boost of watchport and recompute the push.
2003 * Condition:
2004 * Port locked on entry.
2005 * Port unlocked on return.
2006 * Returns:
2007 * KERN_SUCESS on success.
2008 * KERN_FAILURE otherwise.
2009 */
2010 kern_return_t
2011 ipc_port_clear_watchport_elem_internal_conditional_locked(
2012 ipc_port_t port,
2013 struct task_watchport_elem *watchport_elem)
2014 {
2015 ip_lock_held(port);
2016 imq_held(&port->ip_messages);
2017
2018 if (port->ip_watchport_elem != watchport_elem) {
2019 imq_unlock(&port->ip_messages);
2020 ip_unlock(port);
2021 return KERN_FAILURE;
2022 }
2023
2024 ipc_port_clear_watchport_elem_internal(port);
2025 ipc_port_send_turnstile_recompute_push_locked(port);
2026 /* port and mqueue unlocked */
2027 return KERN_SUCCESS;
2028 }
2029
2030 /*
2031 * Routine: ipc_port_replace_watchport_elem_conditional_locked
2032 * Purpose:
2033 * Replace the turnstile boost of watchport and recompute the push.
2034 * Condition:
2035 * Port locked on entry.
2036 * Port unlocked on return.
2037 * Returns:
2038 * KERN_SUCESS on success.
2039 * KERN_FAILURE otherwise.
2040 */
2041 kern_return_t
2042 ipc_port_replace_watchport_elem_conditional_locked(
2043 ipc_port_t port,
2044 struct task_watchport_elem *old_watchport_elem,
2045 struct task_watchport_elem *new_watchport_elem)
2046 {
2047 ip_lock_held(port);
2048 imq_held(&port->ip_messages);
2049
2050 if (port->ip_watchport_elem != old_watchport_elem) {
2051 imq_unlock(&port->ip_messages);
2052 ip_unlock(port);
2053 return KERN_FAILURE;
2054 }
2055
2056 port->ip_watchport_elem = new_watchport_elem;
2057 ipc_port_send_turnstile_recompute_push_locked(port);
2058 /* port and mqueue unlocked */
2059 return KERN_SUCCESS;
2060 }
2061
2062 /*
2063 * Routine: ipc_port_clear_watchport_elem_internal
2064 * Purpose:
2065 * Remove the turnstile boost of watchport.
2066 * Condition:
2067 * Port locked on entry.
2068 * Port locked on return.
2069 * Returns:
2070 * Old task_watchport_elem returned.
2071 */
2072 struct task_watchport_elem *
2073 ipc_port_clear_watchport_elem_internal(
2074 ipc_port_t port)
2075 {
2076 struct task_watchport_elem *watchport_elem;
2077
2078 ip_lock_held(port);
2079 imq_held(&port->ip_messages);
2080
2081 watchport_elem = port->ip_watchport_elem;
2082 port->ip_watchport_elem = NULL;
2083
2084 return watchport_elem;
2085 }
2086
2087 /*
2088 * Routine: ipc_port_send_turnstile_recompute_push_locked
2089 * Purpose:
2090 * Update send turnstile inheritor of port and recompute the push.
2091 * Condition:
2092 * Port locked on entry.
2093 * Port unlocked on return.
2094 * Returns:
2095 * None.
2096 */
2097 static void
2098 ipc_port_send_turnstile_recompute_push_locked(
2099 ipc_port_t port)
2100 {
2101 struct turnstile *send_turnstile = port_send_turnstile(port);
2102 if (send_turnstile) {
2103 turnstile_reference(send_turnstile);
2104 ipc_port_send_update_inheritor(port, send_turnstile,
2105 TURNSTILE_IMMEDIATE_UPDATE);
2106 }
2107 imq_unlock(&port->ip_messages);
2108 ip_unlock(port);
2109
2110 if (send_turnstile) {
2111 turnstile_update_inheritor_complete(send_turnstile,
2112 TURNSTILE_INTERLOCK_NOT_HELD);
2113 turnstile_deallocate_safe(send_turnstile);
2114 }
2115 }
2116
2117 /*
2118 * Routine: ipc_port_get_watchport_inheritor
2119 * Purpose:
2120 * Returns inheritor for watchport.
2121 *
2122 * Conditions:
2123 * mqueue locked.
2124 * Returns:
2125 * watchport inheritor.
2126 */
2127 static thread_t
2128 ipc_port_get_watchport_inheritor(
2129 ipc_port_t port)
2130 {
2131 imq_held(&port->ip_messages);
2132 return port->ip_watchport_elem->twe_task->watchports->tw_thread;
2133 }
2134
2135 /*
2136 * Routine: ipc_port_impcount_delta
2137 * Purpose:
2138 * Adjust only the importance count associated with a port.
2139 * If there are any adjustments to be made to receiver task,
2140 * those are handled elsewhere.
2141 *
2142 * For now, be defensive during deductions to make sure the
2143 * impcount for the port doesn't underflow zero. This will
2144 * go away when the port boost addition is made atomic (see
2145 * note in ipc_port_importance_delta()).
2146 * Conditions:
2147 * The port is referenced and locked.
2148 * Nothing else is locked.
2149 */
2150 mach_port_delta_t
2151 ipc_port_impcount_delta(
2152 ipc_port_t port,
2153 mach_port_delta_t delta,
2154 ipc_port_t __unused base)
2155 {
2156 mach_port_delta_t absdelta;
2157
2158 if (!ip_active(port)) {
2159 return 0;
2160 }
2161
2162 /* adding/doing nothing is easy */
2163 if (delta >= 0) {
2164 port->ip_impcount += delta;
2165 return delta;
2166 }
2167
2168 absdelta = 0 - delta;
2169 if (port->ip_impcount >= absdelta) {
2170 port->ip_impcount -= absdelta;
2171 return delta;
2172 }
2173
2174 #if (DEVELOPMENT || DEBUG)
2175 if (port->ip_receiver_name != MACH_PORT_NULL) {
2176 task_t target_task = port->ip_receiver->is_task;
2177 ipc_importance_task_t target_imp = target_task->task_imp_base;
2178 const char *target_procname;
2179 int target_pid;
2180
2181 if (target_imp != IIT_NULL) {
2182 target_procname = target_imp->iit_procname;
2183 target_pid = target_imp->iit_bsd_pid;
2184 } else {
2185 target_procname = "unknown";
2186 target_pid = -1;
2187 }
2188 printf("Over-release of importance assertions for port 0x%x receiver pid %d (%s), "
2189 "dropping %d assertion(s) but port only has %d remaining.\n",
2190 port->ip_receiver_name,
2191 target_pid, target_procname,
2192 absdelta, port->ip_impcount);
2193 } else if (base != IP_NULL) {
2194 task_t target_task = base->ip_receiver->is_task;
2195 ipc_importance_task_t target_imp = target_task->task_imp_base;
2196 const char *target_procname;
2197 int target_pid;
2198
2199 if (target_imp != IIT_NULL) {
2200 target_procname = target_imp->iit_procname;
2201 target_pid = target_imp->iit_bsd_pid;
2202 } else {
2203 target_procname = "unknown";
2204 target_pid = -1;
2205 }
2206 printf("Over-release of importance assertions for port 0x%lx "
2207 "enqueued on port 0x%x with receiver pid %d (%s), "
2208 "dropping %d assertion(s) but port only has %d remaining.\n",
2209 (unsigned long)VM_KERNEL_UNSLIDE_OR_PERM((uintptr_t)port),
2210 base->ip_receiver_name,
2211 target_pid, target_procname,
2212 absdelta, port->ip_impcount);
2213 }
2214 #endif
2215
2216 delta = 0 - port->ip_impcount;
2217 port->ip_impcount = 0;
2218 return delta;
2219 }
2220
2221 /*
2222 * Routine: ipc_port_importance_delta_internal
2223 * Purpose:
2224 * Adjust the importance count through the given port.
2225 * If the port is in transit, apply the delta throughout
2226 * the chain. Determine if the there is a task at the
2227 * base of the chain that wants/needs to be adjusted,
2228 * and if so, apply the delta.
2229 * Conditions:
2230 * The port is referenced and locked on entry.
2231 * Importance may be locked.
2232 * Nothing else is locked.
2233 * The lock may be dropped on exit.
2234 * Returns TRUE if lock was dropped.
2235 */
2236 #if IMPORTANCE_INHERITANCE
2237
2238 boolean_t
2239 ipc_port_importance_delta_internal(
2240 ipc_port_t port,
2241 natural_t options,
2242 mach_port_delta_t *deltap,
2243 ipc_importance_task_t *imp_task)
2244 {
2245 ipc_port_t next, base;
2246 boolean_t dropped = FALSE;
2247
2248 *imp_task = IIT_NULL;
2249
2250 if (*deltap == 0) {
2251 return FALSE;
2252 }
2253
2254 assert(options == IPID_OPTION_NORMAL || options == IPID_OPTION_SENDPOSSIBLE);
2255
2256 base = port;
2257
2258 /* if port is in transit, have to search for end of chain */
2259 if (ip_active(port) &&
2260 port->ip_destination != IP_NULL &&
2261 port->ip_receiver_name == MACH_PORT_NULL) {
2262 dropped = TRUE;
2263
2264 ip_unlock(port);
2265 ipc_port_multiple_lock(); /* massive serialization */
2266 ip_lock(base);
2267
2268 while (ip_active(base) &&
2269 base->ip_destination != IP_NULL &&
2270 base->ip_receiver_name == MACH_PORT_NULL) {
2271 base = base->ip_destination;
2272 ip_lock(base);
2273 }
2274 ipc_port_multiple_unlock();
2275 }
2276
2277 /*
2278 * If the port lock is dropped b/c the port is in transit, there is a
2279 * race window where another thread can drain messages and/or fire a
2280 * send possible notification before we get here.
2281 *
2282 * We solve this race by checking to see if our caller armed the send
2283 * possible notification, whether or not it's been fired yet, and
2284 * whether or not we've already set the port's ip_spimportant bit. If
2285 * we don't need a send-possible boost, then we'll just apply a
2286 * harmless 0-boost to the port.
2287 */
2288 if (options & IPID_OPTION_SENDPOSSIBLE) {
2289 assert(*deltap == 1);
2290 if (port->ip_sprequests && port->ip_spimportant == 0) {
2291 port->ip_spimportant = 1;
2292 } else {
2293 *deltap = 0;
2294 }
2295 }
2296
2297 /* unlock down to the base, adjusting boost(s) at each level */
2298 for (;;) {
2299 *deltap = ipc_port_impcount_delta(port, *deltap, base);
2300
2301 if (port == base) {
2302 break;
2303 }
2304
2305 /* port is in transit */
2306 assert(port->ip_tempowner == 0);
2307 next = port->ip_destination;
2308 ip_unlock(port);
2309 port = next;
2310 }
2311
2312 /* find the task (if any) to boost according to the base */
2313 if (ip_active(base)) {
2314 if (base->ip_tempowner != 0) {
2315 if (IIT_NULL != base->ip_imp_task) {
2316 *imp_task = base->ip_imp_task;
2317 }
2318 /* otherwise don't boost */
2319 } else if (base->ip_receiver_name != MACH_PORT_NULL) {
2320 ipc_space_t space = base->ip_receiver;
2321
2322 /* only spaces with boost-accepting tasks */
2323 if (space->is_task != TASK_NULL &&
2324 ipc_importance_task_is_any_receiver_type(space->is_task->task_imp_base)) {
2325 *imp_task = space->is_task->task_imp_base;
2326 }
2327 }
2328 }
2329
2330 /*
2331 * Only the base is locked. If we have to hold or drop task
2332 * importance assertions, we'll have to drop that lock as well.
2333 */
2334 if (*imp_task != IIT_NULL) {
2335 /* take a reference before unlocking base */
2336 ipc_importance_task_reference(*imp_task);
2337 }
2338
2339 if (dropped == TRUE) {
2340 ip_unlock(base);
2341 }
2342
2343 return dropped;
2344 }
2345 #endif /* IMPORTANCE_INHERITANCE */
2346
2347 /*
2348 * Routine: ipc_port_importance_delta
2349 * Purpose:
2350 * Adjust the importance count through the given port.
2351 * If the port is in transit, apply the delta throughout
2352 * the chain.
2353 *
2354 * If there is a task at the base of the chain that wants/needs
2355 * to be adjusted, apply the delta.
2356 * Conditions:
2357 * The port is referenced and locked on entry.
2358 * Nothing else is locked.
2359 * The lock may be dropped on exit.
2360 * Returns TRUE if lock was dropped.
2361 */
2362 #if IMPORTANCE_INHERITANCE
2363
2364 boolean_t
2365 ipc_port_importance_delta(
2366 ipc_port_t port,
2367 natural_t options,
2368 mach_port_delta_t delta)
2369 {
2370 ipc_importance_task_t imp_task = IIT_NULL;
2371 boolean_t dropped;
2372
2373 dropped = ipc_port_importance_delta_internal(port, options, &delta, &imp_task);
2374
2375 if (IIT_NULL == imp_task || delta == 0) {
2376 return dropped;
2377 }
2378
2379 if (!dropped) {
2380 ip_unlock(port);
2381 }
2382
2383 assert(ipc_importance_task_is_any_receiver_type(imp_task));
2384
2385 if (delta > 0) {
2386 ipc_importance_task_hold_internal_assertion(imp_task, delta);
2387 } else {
2388 ipc_importance_task_drop_internal_assertion(imp_task, -delta);
2389 }
2390
2391 ipc_importance_task_release(imp_task);
2392 return TRUE;
2393 }
2394 #endif /* IMPORTANCE_INHERITANCE */
2395
2396 /*
2397 * Routine: ipc_port_make_send_locked
2398 * Purpose:
2399 * Make a naked send right from a receive right.
2400 *
2401 * Conditions:
2402 * port locked and active.
2403 */
2404 ipc_port_t
2405 ipc_port_make_send_locked(
2406 ipc_port_t port)
2407 {
2408 require_ip_active(port);
2409 port->ip_mscount++;
2410 port->ip_srights++;
2411 ip_reference(port);
2412 return port;
2413 }
2414
2415 /*
2416 * Routine: ipc_port_make_send
2417 * Purpose:
2418 * Make a naked send right from a receive right.
2419 */
2420
2421 ipc_port_t
2422 ipc_port_make_send(
2423 ipc_port_t port)
2424 {
2425 if (!IP_VALID(port)) {
2426 return port;
2427 }
2428
2429 ip_lock(port);
2430 if (ip_active(port)) {
2431 ipc_port_make_send_locked(port);
2432 ip_unlock(port);
2433 return port;
2434 }
2435 ip_unlock(port);
2436 return IP_DEAD;
2437 }
2438
2439 /*
2440 * Routine: ipc_port_copy_send_locked
2441 * Purpose:
2442 * Make a naked send right from another naked send right.
2443 * Conditions:
2444 * port locked and active.
2445 */
2446 void
2447 ipc_port_copy_send_locked(
2448 ipc_port_t port)
2449 {
2450 assert(port->ip_srights > 0);
2451 port->ip_srights++;
2452 ip_reference(port);
2453 }
2454
2455 /*
2456 * Routine: ipc_port_copy_send
2457 * Purpose:
2458 * Make a naked send right from another naked send right.
2459 * IP_NULL -> IP_NULL
2460 * IP_DEAD -> IP_DEAD
2461 * dead port -> IP_DEAD
2462 * live port -> port + ref
2463 * Conditions:
2464 * Nothing locked except possibly a space.
2465 */
2466
2467 ipc_port_t
2468 ipc_port_copy_send(
2469 ipc_port_t port)
2470 {
2471 ipc_port_t sright;
2472
2473 if (!IP_VALID(port)) {
2474 return port;
2475 }
2476
2477 ip_lock(port);
2478 if (ip_active(port)) {
2479 ipc_port_copy_send_locked(port);
2480 sright = port;
2481 } else {
2482 sright = IP_DEAD;
2483 }
2484 ip_unlock(port);
2485
2486 return sright;
2487 }
2488
2489 /*
2490 * Routine: ipc_port_copyout_send
2491 * Purpose:
2492 * Copyout a naked send right (possibly null/dead),
2493 * or if that fails, destroy the right.
2494 * Conditions:
2495 * Nothing locked.
2496 */
2497
2498 mach_port_name_t
2499 ipc_port_copyout_send(
2500 ipc_port_t sright,
2501 ipc_space_t space)
2502 {
2503 mach_port_name_t name;
2504
2505 if (IP_VALID(sright)) {
2506 kern_return_t kr;
2507
2508 kr = ipc_object_copyout(space, ip_to_object(sright),
2509 MACH_MSG_TYPE_PORT_SEND, NULL, NULL, &name);
2510 if (kr != KERN_SUCCESS) {
2511 ipc_port_release_send(sright);
2512
2513 if (kr == KERN_INVALID_CAPABILITY) {
2514 name = MACH_PORT_DEAD;
2515 } else {
2516 name = MACH_PORT_NULL;
2517 }
2518 }
2519 } else {
2520 name = CAST_MACH_PORT_TO_NAME(sright);
2521 }
2522
2523 return name;
2524 }
2525
2526 /*
2527 * Routine: ipc_port_release_send
2528 * Purpose:
2529 * Release a naked send right.
2530 * Consumes a ref for the port.
2531 * Conditions:
2532 * Nothing locked.
2533 */
2534
2535 void
2536 ipc_port_release_send(
2537 ipc_port_t port)
2538 {
2539 ipc_port_t nsrequest = IP_NULL;
2540 mach_port_mscount_t mscount;
2541
2542 if (!IP_VALID(port)) {
2543 return;
2544 }
2545
2546 ip_lock(port);
2547
2548 assert(port->ip_srights > 0);
2549 if (port->ip_srights == 0) {
2550 panic("Over-release of port %p send right!", port);
2551 }
2552
2553 port->ip_srights--;
2554
2555 if (!ip_active(port)) {
2556 ip_unlock(port);
2557 ip_release(port);
2558 return;
2559 }
2560
2561 if (port->ip_srights == 0 &&
2562 port->ip_nsrequest != IP_NULL) {
2563 nsrequest = port->ip_nsrequest;
2564 port->ip_nsrequest = IP_NULL;
2565 mscount = port->ip_mscount;
2566 ip_unlock(port);
2567 ip_release(port);
2568 ipc_notify_no_senders(nsrequest, mscount);
2569 } else {
2570 ip_unlock(port);
2571 ip_release(port);
2572 }
2573 }
2574
2575 /*
2576 * Routine: ipc_port_make_sonce_locked
2577 * Purpose:
2578 * Make a naked send-once right from a receive right.
2579 * Conditions:
2580 * The port is locked and active.
2581 */
2582
2583 ipc_port_t
2584 ipc_port_make_sonce_locked(
2585 ipc_port_t port)
2586 {
2587 require_ip_active(port);
2588 port->ip_sorights++;
2589 ip_reference(port);
2590 return port;
2591 }
2592
2593 /*
2594 * Routine: ipc_port_make_sonce
2595 * Purpose:
2596 * Make a naked send-once right from a receive right.
2597 * Conditions:
2598 * The port is not locked.
2599 */
2600
2601 ipc_port_t
2602 ipc_port_make_sonce(
2603 ipc_port_t port)
2604 {
2605 if (!IP_VALID(port)) {
2606 return port;
2607 }
2608
2609 ip_lock(port);
2610 if (ip_active(port)) {
2611 ipc_port_make_sonce_locked(port);
2612 ip_unlock(port);
2613 return port;
2614 }
2615 ip_unlock(port);
2616 return IP_DEAD;
2617 }
2618
2619 /*
2620 * Routine: ipc_port_release_sonce
2621 * Purpose:
2622 * Release a naked send-once right.
2623 * Consumes a ref for the port.
2624 *
2625 * In normal situations, this is never used.
2626 * Send-once rights are only consumed when
2627 * a message (possibly a send-once notification)
2628 * is sent to them.
2629 * Conditions:
2630 * Nothing locked except possibly a space.
2631 */
2632
2633 void
2634 ipc_port_release_sonce(
2635 ipc_port_t port)
2636 {
2637 if (!IP_VALID(port)) {
2638 return;
2639 }
2640
2641 ipc_port_adjust_special_reply_port(port, IPC_PORT_ADJUST_RESET_BOOSTRAP_CHECKIN, FALSE);
2642
2643 ip_lock(port);
2644
2645 assert(port->ip_sorights > 0);
2646 if (port->ip_sorights == 0) {
2647 panic("Over-release of port %p send-once right!", port);
2648 }
2649
2650 port->ip_sorights--;
2651
2652 ip_unlock(port);
2653 ip_release(port);
2654 }
2655
2656 /*
2657 * Routine: ipc_port_release_receive
2658 * Purpose:
2659 * Release a naked (in limbo or in transit) receive right.
2660 * Consumes a ref for the port; destroys the port.
2661 * Conditions:
2662 * Nothing locked.
2663 */
2664
2665 void
2666 ipc_port_release_receive(
2667 ipc_port_t port)
2668 {
2669 ipc_port_t dest;
2670
2671 if (!IP_VALID(port)) {
2672 return;
2673 }
2674
2675 ip_lock(port);
2676 require_ip_active(port);
2677 assert(port->ip_receiver_name == MACH_PORT_NULL);
2678 dest = port->ip_destination;
2679
2680 ipc_port_destroy(port); /* consumes ref, unlocks */
2681
2682 if (dest != IP_NULL) {
2683 ipc_port_send_turnstile_complete(dest);
2684 ip_release(dest);
2685 }
2686 }
2687
2688 /*
2689 * Routine: ipc_port_alloc_special
2690 * Purpose:
2691 * Allocate a port in a special space.
2692 * The new port is returned with one ref.
2693 * If unsuccessful, IP_NULL is returned.
2694 * Conditions:
2695 * Nothing locked.
2696 */
2697
2698 ipc_port_t
2699 ipc_port_alloc_special(
2700 ipc_space_t space)
2701 {
2702 ipc_port_t port;
2703
2704 port = ip_object_to_port(io_alloc(IOT_PORT));
2705 if (port == IP_NULL) {
2706 return IP_NULL;
2707 }
2708
2709 #if MACH_ASSERT
2710 uintptr_t buf[IP_CALLSTACK_MAX];
2711 ipc_port_callstack_init_debug(&buf[0], IP_CALLSTACK_MAX);
2712 #endif /* MACH_ASSERT */
2713
2714 bzero((char *)port, sizeof(*port));
2715 io_lock_init(ip_to_object(port));
2716 port->ip_references = 1;
2717 port->ip_object.io_bits = io_makebits(TRUE, IOT_PORT, 0);
2718
2719 ipc_port_init(port, space, 1);
2720
2721 #if MACH_ASSERT
2722 ipc_port_init_debug(port, &buf[0], IP_CALLSTACK_MAX);
2723 #endif /* MACH_ASSERT */
2724
2725 return port;
2726 }
2727
2728 /*
2729 * Routine: ipc_port_dealloc_special
2730 * Purpose:
2731 * Deallocate a port in a special space.
2732 * Consumes one ref for the port.
2733 * Conditions:
2734 * Nothing locked.
2735 */
2736
2737 void
2738 ipc_port_dealloc_special(
2739 ipc_port_t port,
2740 __assert_only ipc_space_t space)
2741 {
2742 ip_lock(port);
2743 require_ip_active(port);
2744 // assert(port->ip_receiver_name != MACH_PORT_NULL);
2745 assert(port->ip_receiver == space);
2746
2747 /*
2748 * We clear ip_receiver_name and ip_receiver to simplify
2749 * the ipc_space_kernel check in ipc_mqueue_send.
2750 */
2751
2752 imq_lock(&port->ip_messages);
2753 port->ip_receiver_name = MACH_PORT_NULL;
2754 port->ip_receiver = IS_NULL;
2755 imq_unlock(&port->ip_messages);
2756
2757 /* relevant part of ipc_port_clear_receiver */
2758 port->ip_mscount = 0;
2759 port->ip_messages.imq_seqno = 0;
2760
2761 ipc_port_destroy(port);
2762 }
2763
2764 /*
2765 * Routine: ipc_port_finalize
2766 * Purpose:
2767 * Called on last reference deallocate to
2768 * free any remaining data associated with the
2769 * port.
2770 * Conditions:
2771 * Nothing locked.
2772 */
2773 void
2774 ipc_port_finalize(
2775 ipc_port_t port)
2776 {
2777 ipc_port_request_t requests = port->ip_requests;
2778
2779 assert(port_send_turnstile(port) == TURNSTILE_NULL);
2780 assert(ipc_port_rcv_turnstile(port) == TURNSTILE_NULL);
2781
2782 if (ip_active(port)) {
2783 panic("Trying to free an active port. port %p", port);
2784 }
2785
2786 if (requests != IPR_NULL) {
2787 ipc_table_size_t its = requests->ipr_size;
2788 it_requests_free(its, requests);
2789 port->ip_requests = IPR_NULL;
2790 }
2791
2792 ipc_mqueue_deinit(&port->ip_messages);
2793
2794 #if MACH_ASSERT
2795 ipc_port_track_dealloc(port);
2796 #endif /* MACH_ASSERT */
2797 }
2798
2799 /*
2800 * Routine: kdp_mqueue_send_find_owner
2801 * Purpose:
2802 * Discover the owner of the ipc_mqueue that contains the input
2803 * waitq object. The thread blocked on the waitq should be
2804 * waiting for an IPC_MQUEUE_FULL event.
2805 * Conditions:
2806 * The 'waitinfo->wait_type' value should already be set to
2807 * kThreadWaitPortSend.
2808 * Note:
2809 * If we find out that the containing port is actually in
2810 * transit, we reset the wait_type field to reflect this.
2811 */
2812 void
2813 kdp_mqueue_send_find_owner(struct waitq * waitq, __assert_only event64_t event, thread_waitinfo_t * waitinfo)
2814 {
2815 struct turnstile *turnstile;
2816 assert(waitinfo->wait_type == kThreadWaitPortSend);
2817 assert(event == IPC_MQUEUE_FULL);
2818 assert(waitq_is_turnstile_queue(waitq));
2819
2820 turnstile = waitq_to_turnstile(waitq);
2821 ipc_port_t port = (ipc_port_t)turnstile->ts_proprietor; /* we are blocking on send */
2822 assert(kdp_is_in_zone(port, "ipc ports"));
2823
2824 waitinfo->owner = 0;
2825 waitinfo->context = VM_KERNEL_UNSLIDE_OR_PERM(port);
2826 if (ip_lock_held_kdp(port)) {
2827 /*
2828 * someone has the port locked: it may be in an
2829 * inconsistent state: bail
2830 */
2831 waitinfo->owner = STACKSHOT_WAITOWNER_PORT_LOCKED;
2832 return;
2833 }
2834
2835 if (ip_active(port)) {
2836 if (port->ip_tempowner) {
2837 if (port->ip_imp_task != IIT_NULL && port->ip_imp_task->iit_task != NULL) {
2838 /* port is held by a tempowner */
2839 waitinfo->owner = pid_from_task(port->ip_imp_task->iit_task);
2840 } else {
2841 waitinfo->owner = STACKSHOT_WAITOWNER_INTRANSIT;
2842 }
2843 } else if (port->ip_receiver_name) {
2844 /* port in a space */
2845 if (port->ip_receiver == ipc_space_kernel) {
2846 /*
2847 * The kernel pid is 0, make this
2848 * distinguishable from no-owner and
2849 * inconsistent port state.
2850 */
2851 waitinfo->owner = STACKSHOT_WAITOWNER_KERNEL;
2852 } else {
2853 waitinfo->owner = pid_from_task(port->ip_receiver->is_task);
2854 }
2855 } else if (port->ip_destination != IP_NULL) {
2856 /* port in transit */
2857 waitinfo->wait_type = kThreadWaitPortSendInTransit;
2858 waitinfo->owner = VM_KERNEL_UNSLIDE_OR_PERM(port->ip_destination);
2859 }
2860 }
2861 }
2862
2863 /*
2864 * Routine: kdp_mqueue_recv_find_owner
2865 * Purpose:
2866 * Discover the "owner" of the ipc_mqueue that contains the input
2867 * waitq object. The thread blocked on the waitq is trying to
2868 * receive on the mqueue.
2869 * Conditions:
2870 * The 'waitinfo->wait_type' value should already be set to
2871 * kThreadWaitPortReceive.
2872 * Note:
2873 * If we find that we are actualy waiting on a port set, we reset
2874 * the wait_type field to reflect this.
2875 */
2876 void
2877 kdp_mqueue_recv_find_owner(struct waitq * waitq, __assert_only event64_t event, thread_waitinfo_t * waitinfo)
2878 {
2879 assert(waitinfo->wait_type == kThreadWaitPortReceive);
2880 assert(event == IPC_MQUEUE_RECEIVE);
2881
2882 ipc_mqueue_t mqueue = imq_from_waitq(waitq);
2883 waitinfo->owner = 0;
2884 if (imq_is_set(mqueue)) { /* we are waiting on a port set */
2885 ipc_pset_t set = ips_from_mq(mqueue);
2886 assert(kdp_is_in_zone(set, "ipc port sets"));
2887
2888 /* Reset wait type to specify waiting on port set receive */
2889 waitinfo->wait_type = kThreadWaitPortSetReceive;
2890 waitinfo->context = VM_KERNEL_UNSLIDE_OR_PERM(set);
2891 if (ips_lock_held_kdp(set)) {
2892 waitinfo->owner = STACKSHOT_WAITOWNER_PSET_LOCKED;
2893 }
2894 /* There is no specific owner "at the other end" of a port set, so leave unset. */
2895 } else {
2896 ipc_port_t port = ip_from_mq(mqueue);
2897 assert(kdp_is_in_zone(port, "ipc ports"));
2898
2899 waitinfo->context = VM_KERNEL_UNSLIDE_OR_PERM(port);
2900 if (ip_lock_held_kdp(port)) {
2901 waitinfo->owner = STACKSHOT_WAITOWNER_PORT_LOCKED;
2902 return;
2903 }
2904
2905 if (ip_active(port)) {
2906 if (port->ip_receiver_name != MACH_PORT_NULL) {
2907 waitinfo->owner = port->ip_receiver_name;
2908 } else {
2909 waitinfo->owner = STACKSHOT_WAITOWNER_INTRANSIT;
2910 }
2911 }
2912 }
2913 }
2914
2915 #if MACH_ASSERT
2916 #include <kern/machine.h>
2917
2918 /*
2919 * Keep a list of all allocated ports.
2920 * Allocation is intercepted via ipc_port_init;
2921 * deallocation is intercepted via io_free.
2922 */
2923 #if 0
2924 queue_head_t port_alloc_queue;
2925 lck_spin_t port_alloc_queue_lock;
2926 #endif
2927
2928 unsigned long port_count = 0;
2929 unsigned long port_count_warning = 20000;
2930 unsigned long port_timestamp = 0;
2931
2932 void db_port_stack_trace(
2933 ipc_port_t port);
2934 void db_ref(
2935 int refs);
2936 int db_port_walk(
2937 unsigned int verbose,
2938 unsigned int display,
2939 unsigned int ref_search,
2940 unsigned int ref_target);
2941
2942 /*
2943 * Initialize global state needed for run-time
2944 * port debugging.
2945 */
2946 void
2947 ipc_port_debug_init(void)
2948 {
2949 #if 0
2950 queue_init(&port_alloc_queue);
2951 lck_spin_init(&port_alloc_queue_lock, &ipc_lck_grp, &ipc_lck_attr);
2952 #endif
2953
2954 if (!PE_parse_boot_argn("ipc_portbt", &ipc_portbt, sizeof(ipc_portbt))) {
2955 ipc_portbt = 0;
2956 }
2957 }
2958
2959 #ifdef MACH_BSD
2960 extern int proc_pid(struct proc*);
2961 #endif /* MACH_BSD */
2962
2963 /*
2964 * Initialize all of the debugging state in a port.
2965 * Insert the port into a global list of all allocated ports.
2966 */
2967 void
2968 ipc_port_init_debug(
2969 ipc_port_t port,
2970 uintptr_t *callstack,
2971 unsigned int callstack_max)
2972 {
2973 unsigned int i;
2974
2975 port->ip_thread = current_thread();
2976 port->ip_timetrack = port_timestamp++;
2977 for (i = 0; i < callstack_max; ++i) {
2978 port->ip_callstack[i] = callstack[i];
2979 }
2980 for (i = 0; i < IP_NSPARES; ++i) {
2981 port->ip_spares[i] = 0;
2982 }
2983
2984 #ifdef MACH_BSD
2985 task_t task = current_task();
2986 if (task != TASK_NULL) {
2987 struct proc* proc = (struct proc*) get_bsdtask_info(task);
2988 if (proc) {
2989 port->ip_spares[0] = proc_pid(proc);
2990 }
2991 }
2992 #endif /* MACH_BSD */
2993
2994 #if 0
2995 lck_spin_lock(&port_alloc_queue_lock);
2996 ++port_count;
2997 if (port_count_warning > 0 && port_count >= port_count_warning) {
2998 assert(port_count < port_count_warning);
2999 }
3000 queue_enter(&port_alloc_queue, port, ipc_port_t, ip_port_links);
3001 lck_spin_unlock(&port_alloc_queue_lock);
3002 #endif
3003 }
3004
3005 /*
3006 * Routine: ipc_port_callstack_init_debug
3007 * Purpose:
3008 * Calls the machine-dependent routine to
3009 * fill in an array with up to IP_CALLSTACK_MAX
3010 * levels of return pc information
3011 * Conditions:
3012 * May block (via copyin)
3013 */
3014 void
3015 ipc_port_callstack_init_debug(
3016 uintptr_t *callstack,
3017 unsigned int callstack_max)
3018 {
3019 unsigned int i;
3020
3021 /* guarantee the callstack is initialized */
3022 for (i = 0; i < callstack_max; i++) {
3023 callstack[i] = 0;
3024 }
3025
3026 if (ipc_portbt) {
3027 machine_callstack(callstack, callstack_max);
3028 }
3029 }
3030
3031 /*
3032 * Remove a port from the queue of allocated ports.
3033 * This routine should be invoked JUST prior to
3034 * deallocating the actual memory occupied by the port.
3035 */
3036 #if 1
3037 void
3038 ipc_port_track_dealloc(
3039 __unused ipc_port_t port)
3040 {
3041 }
3042 #else
3043 void
3044 ipc_port_track_dealloc(
3045 ipc_port_t port)
3046 {
3047 lck_spin_lock(&port_alloc_queue_lock);
3048 assert(port_count > 0);
3049 --port_count;
3050 queue_remove(&port_alloc_queue, port, ipc_port_t, ip_port_links);
3051 lck_spin_unlock(&port_alloc_queue_lock);
3052 }
3053 #endif
3054
3055
3056 #endif /* MACH_ASSERT */