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