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