]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_port.c
xnu-3789.51.2.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_port.c
1 /*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_FREE_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 */
62 /*
63 */
64 /*
65 * File: ipc/ipc_port.c
66 * Author: Rich Draves
67 * Date: 1989
68 *
69 * Functions to manipulate IPC ports.
70 */
71
72 #include <zone_debug.h>
73 #include <mach_assert.h>
74
75 #include <mach/port.h>
76 #include <mach/kern_return.h>
77 #include <kern/ipc_kobject.h>
78 #include <kern/thread.h>
79 #include <kern/misc_protos.h>
80 #include <kern/waitq.h>
81 #include <kern/policy_internal.h>
82 #include <kern/debug.h>
83 #include <kern/kcdata.h>
84 #include <ipc/ipc_entry.h>
85 #include <ipc/ipc_space.h>
86 #include <ipc/ipc_object.h>
87 #include <ipc/ipc_port.h>
88 #include <ipc/ipc_pset.h>
89 #include <ipc/ipc_kmsg.h>
90 #include <ipc/ipc_mqueue.h>
91 #include <ipc/ipc_notify.h>
92 #include <ipc/ipc_table.h>
93 #include <ipc/ipc_importance.h>
94
95 #include <security/mac_mach_internal.h>
96
97 #include <string.h>
98
99 decl_lck_spin_data(, ipc_port_multiple_lock_data)
100 ipc_port_timestamp_t ipc_port_timestamp_data;
101 int ipc_portbt;
102
103 #if MACH_ASSERT
104 void ipc_port_init_debug(
105 ipc_port_t port,
106 uintptr_t *callstack,
107 unsigned int callstack_max);
108
109 void ipc_port_callstack_init_debug(
110 uintptr_t *callstack,
111 unsigned int callstack_max);
112
113 #endif /* MACH_ASSERT */
114
115 void kdp_mqueue_send_find_owner(struct waitq * waitq, event64_t event, thread_waitinfo_t *waitinfo);
116 void kdp_mqueue_recv_find_owner(struct waitq * waitq, event64_t event, thread_waitinfo_t *waitinfo);
117
118 void
119 ipc_port_release(ipc_port_t port)
120 {
121 ip_release(port);
122 }
123
124 void
125 ipc_port_reference(ipc_port_t port)
126 {
127 ip_reference(port);
128 }
129
130 /*
131 * Routine: ipc_port_timestamp
132 * Purpose:
133 * Retrieve a timestamp value.
134 */
135
136 ipc_port_timestamp_t
137 ipc_port_timestamp(void)
138 {
139 return OSIncrementAtomic(&ipc_port_timestamp_data);
140 }
141
142 /*
143 * Routine: ipc_port_request_alloc
144 * Purpose:
145 * Try to allocate a request slot.
146 * If successful, returns the request index.
147 * Otherwise returns zero.
148 * Conditions:
149 * The port is locked and active.
150 * Returns:
151 * KERN_SUCCESS A request index was found.
152 * KERN_NO_SPACE No index allocated.
153 */
154
155 #if IMPORTANCE_INHERITANCE
156 kern_return_t
157 ipc_port_request_alloc(
158 ipc_port_t port,
159 mach_port_name_t name,
160 ipc_port_t soright,
161 boolean_t send_possible,
162 boolean_t immediate,
163 ipc_port_request_index_t *indexp,
164 boolean_t *importantp)
165 #else
166 kern_return_t
167 ipc_port_request_alloc(
168 ipc_port_t port,
169 mach_port_name_t name,
170 ipc_port_t soright,
171 boolean_t send_possible,
172 boolean_t immediate,
173 ipc_port_request_index_t *indexp)
174 #endif /* IMPORTANCE_INHERITANCE */
175 {
176 ipc_port_request_t ipr, table;
177 ipc_port_request_index_t index;
178 uintptr_t mask = 0;
179
180 #if IMPORTANCE_INHERITANCE
181 *importantp = FALSE;
182 #endif /* IMPORTANCE_INHERITANCE */
183
184 assert(ip_active(port));
185 assert(name != MACH_PORT_NULL);
186 assert(soright != IP_NULL);
187
188 table = port->ip_requests;
189
190 if (table == IPR_NULL)
191 return KERN_NO_SPACE;
192
193 index = table->ipr_next;
194 if (index == 0)
195 return KERN_NO_SPACE;
196
197 ipr = &table[index];
198 assert(ipr->ipr_name == MACH_PORT_NULL);
199
200 table->ipr_next = ipr->ipr_next;
201 ipr->ipr_name = name;
202
203 if (send_possible) {
204 mask |= IPR_SOR_SPREQ_MASK;
205 if (immediate) {
206 mask |= IPR_SOR_SPARM_MASK;
207 if (port->ip_sprequests == 0) {
208 port->ip_sprequests = 1;
209 #if IMPORTANCE_INHERITANCE
210 /* TODO: Live importance support in send-possible */
211 if (port->ip_impdonation != 0 &&
212 port->ip_spimportant == 0 &&
213 (task_is_importance_donor(current_task()))) {
214 *importantp = TRUE;
215 }
216 #endif /* IMPORTANCE_INHERTANCE */
217 }
218 }
219 }
220 ipr->ipr_soright = IPR_SOR_MAKE(soright, mask);
221
222 *indexp = index;
223
224 return KERN_SUCCESS;
225 }
226
227 /*
228 * Routine: ipc_port_request_grow
229 * Purpose:
230 * Grow a port's table of requests.
231 * Conditions:
232 * The port must be locked and active.
233 * Nothing else locked; will allocate memory.
234 * Upon return the port is unlocked.
235 * Returns:
236 * KERN_SUCCESS Grew the table.
237 * KERN_SUCCESS Somebody else grew the table.
238 * KERN_SUCCESS The port died.
239 * KERN_RESOURCE_SHORTAGE Couldn't allocate new table.
240 * KERN_NO_SPACE Couldn't grow to desired size
241 */
242
243 kern_return_t
244 ipc_port_request_grow(
245 ipc_port_t port,
246 ipc_table_elems_t target_size)
247 {
248 ipc_table_size_t its;
249 ipc_port_request_t otable, ntable;
250
251 assert(ip_active(port));
252
253 otable = port->ip_requests;
254 if (otable == IPR_NULL)
255 its = &ipc_table_requests[0];
256 else
257 its = otable->ipr_size + 1;
258
259 if (target_size != ITS_SIZE_NONE) {
260 if ((otable != IPR_NULL) &&
261 (target_size <= otable->ipr_size->its_size)) {
262 ip_unlock(port);
263 return KERN_SUCCESS;
264 }
265 while ((its->its_size) && (its->its_size < target_size)) {
266 its++;
267 }
268 if (its->its_size == 0) {
269 ip_unlock(port);
270 return KERN_NO_SPACE;
271 }
272 }
273
274 ip_reference(port);
275 ip_unlock(port);
276
277 if ((its->its_size == 0) ||
278 ((ntable = it_requests_alloc(its)) == IPR_NULL)) {
279 ip_release(port);
280 return KERN_RESOURCE_SHORTAGE;
281 }
282
283 ip_lock(port);
284
285 /*
286 * Check that port is still active and that nobody else
287 * has slipped in and grown the table on us. Note that
288 * just checking if the current table pointer == otable
289 * isn't sufficient; must check ipr_size.
290 */
291
292 if (ip_active(port) && (port->ip_requests == otable) &&
293 ((otable == IPR_NULL) || (otable->ipr_size+1 == its))) {
294 ipc_table_size_t oits;
295 ipc_table_elems_t osize, nsize;
296 ipc_port_request_index_t free, i;
297
298 /* copy old table to new table */
299
300 if (otable != IPR_NULL) {
301 oits = otable->ipr_size;
302 osize = oits->its_size;
303 free = otable->ipr_next;
304
305 (void) memcpy((void *)(ntable + 1),
306 (const void *)(otable + 1),
307 (osize - 1) * sizeof(struct ipc_port_request));
308 } else {
309 osize = 1;
310 oits = 0;
311 free = 0;
312 }
313
314 nsize = its->its_size;
315 assert(nsize > osize);
316
317 /* add new elements to the new table's free list */
318
319 for (i = osize; i < nsize; i++) {
320 ipc_port_request_t ipr = &ntable[i];
321
322 ipr->ipr_name = MACH_PORT_NULL;
323 ipr->ipr_next = free;
324 free = i;
325 }
326
327 ntable->ipr_next = free;
328 ntable->ipr_size = its;
329 port->ip_requests = ntable;
330 ip_unlock(port);
331 ip_release(port);
332
333 if (otable != IPR_NULL) {
334 it_requests_free(oits, otable);
335 }
336 } else {
337 ip_unlock(port);
338 ip_release(port);
339 it_requests_free(its, ntable);
340 }
341
342 return KERN_SUCCESS;
343 }
344
345 /*
346 * Routine: ipc_port_request_sparm
347 * Purpose:
348 * Arm delayed send-possible request.
349 * Conditions:
350 * The port must be locked and active.
351 *
352 * Returns TRUE if the request was armed
353 * (or armed with importance in that version).
354 */
355
356 boolean_t
357 ipc_port_request_sparm(
358 ipc_port_t port,
359 __assert_only mach_port_name_t name,
360 ipc_port_request_index_t index,
361 mach_msg_option_t option,
362 mach_msg_priority_t override)
363 {
364 if (index != IE_REQ_NONE) {
365 ipc_port_request_t ipr, table;
366
367 assert(ip_active(port));
368
369 table = port->ip_requests;
370 assert(table != IPR_NULL);
371
372 ipr = &table[index];
373 assert(ipr->ipr_name == name);
374
375 /* Is there a valid destination? */
376 if (IPR_SOR_SPREQ(ipr->ipr_soright)) {
377 ipr->ipr_soright = IPR_SOR_MAKE(ipr->ipr_soright, IPR_SOR_SPARM_MASK);
378 port->ip_sprequests = 1;
379
380 if (option & MACH_SEND_OVERRIDE) {
381 /* apply override to message queue */
382 ipc_mqueue_override_send(&port->ip_messages, override);
383 }
384
385 #if IMPORTANCE_INHERITANCE
386 if (((option & MACH_SEND_NOIMPORTANCE) == 0) &&
387 (port->ip_impdonation != 0) &&
388 (port->ip_spimportant == 0) &&
389 (((option & MACH_SEND_IMPORTANCE) != 0) ||
390 (task_is_importance_donor(current_task())))) {
391 return TRUE;
392 }
393 #else
394 return TRUE;
395 #endif /* IMPORTANCE_INHERITANCE */
396 }
397 }
398 return FALSE;
399 }
400
401 /*
402 * Routine: ipc_port_request_type
403 * Purpose:
404 * Determine the type(s) of port requests enabled for a name.
405 * Conditions:
406 * The port must be locked or inactive (to avoid table growth).
407 * The index must not be IE_REQ_NONE and for the name in question.
408 */
409 mach_port_type_t
410 ipc_port_request_type(
411 ipc_port_t port,
412 __assert_only mach_port_name_t name,
413 ipc_port_request_index_t index)
414 {
415 ipc_port_request_t ipr, table;
416 mach_port_type_t type = 0;
417
418 table = port->ip_requests;
419 assert (table != IPR_NULL);
420
421 assert(index != IE_REQ_NONE);
422 ipr = &table[index];
423 assert(ipr->ipr_name == name);
424
425 if (IP_VALID(IPR_SOR_PORT(ipr->ipr_soright))) {
426 type |= MACH_PORT_TYPE_DNREQUEST;
427
428 if (IPR_SOR_SPREQ(ipr->ipr_soright)) {
429 type |= MACH_PORT_TYPE_SPREQUEST;
430
431 if (!IPR_SOR_SPARMED(ipr->ipr_soright)) {
432 type |= MACH_PORT_TYPE_SPREQUEST_DELAYED;
433 }
434 }
435 }
436 return type;
437 }
438
439 /*
440 * Routine: ipc_port_request_cancel
441 * Purpose:
442 * Cancel a dead-name/send-possible request and return the send-once right.
443 * Conditions:
444 * The port must be locked and active.
445 * The index must not be IPR_REQ_NONE and must correspond with name.
446 */
447
448 ipc_port_t
449 ipc_port_request_cancel(
450 ipc_port_t port,
451 __assert_only mach_port_name_t name,
452 ipc_port_request_index_t index)
453 {
454 ipc_port_request_t ipr, table;
455 ipc_port_t request = IP_NULL;
456
457 assert(ip_active(port));
458 table = port->ip_requests;
459 assert(table != IPR_NULL);
460
461 assert (index != IE_REQ_NONE);
462 ipr = &table[index];
463 assert(ipr->ipr_name == name);
464 request = IPR_SOR_PORT(ipr->ipr_soright);
465
466 /* return ipr to the free list inside the table */
467 ipr->ipr_name = MACH_PORT_NULL;
468 ipr->ipr_next = table->ipr_next;
469 table->ipr_next = index;
470
471 return request;
472 }
473
474 /*
475 * Routine: ipc_port_pdrequest
476 * Purpose:
477 * Make a port-deleted request, returning the
478 * previously registered send-once right.
479 * Just cancels the previous request if notify is IP_NULL.
480 * Conditions:
481 * The port is locked and active. It is unlocked.
482 * Consumes a ref for notify (if non-null), and
483 * returns previous with a ref (if non-null).
484 */
485
486 void
487 ipc_port_pdrequest(
488 ipc_port_t port,
489 ipc_port_t notify,
490 ipc_port_t *previousp)
491 {
492 ipc_port_t previous;
493
494 assert(ip_active(port));
495
496 previous = port->ip_pdrequest;
497 port->ip_pdrequest = notify;
498 ip_unlock(port);
499
500 *previousp = previous;
501 }
502
503 /*
504 * Routine: ipc_port_nsrequest
505 * Purpose:
506 * Make a no-senders request, returning the
507 * previously registered send-once right.
508 * Just cancels the previous request if notify is IP_NULL.
509 * Conditions:
510 * The port is locked and active. It is unlocked.
511 * Consumes a ref for notify (if non-null), and
512 * returns previous with a ref (if non-null).
513 */
514
515 void
516 ipc_port_nsrequest(
517 ipc_port_t port,
518 mach_port_mscount_t sync,
519 ipc_port_t notify,
520 ipc_port_t *previousp)
521 {
522 ipc_port_t previous;
523 mach_port_mscount_t mscount;
524
525 assert(ip_active(port));
526
527 previous = port->ip_nsrequest;
528 mscount = port->ip_mscount;
529
530 if ((port->ip_srights == 0) && (sync <= mscount) &&
531 (notify != IP_NULL)) {
532 port->ip_nsrequest = IP_NULL;
533 ip_unlock(port);
534 ipc_notify_no_senders(notify, mscount);
535 } else {
536 port->ip_nsrequest = notify;
537 ip_unlock(port);
538 }
539
540 *previousp = previous;
541 }
542
543
544 /*
545 * Routine: ipc_port_clear_receiver
546 * Purpose:
547 * Prepares a receive right for transmission/destruction,
548 * optionally performs mqueue destruction (with port lock held)
549 *
550 * Conditions:
551 * The port is locked and active.
552 * Returns:
553 * If should_destroy is TRUE, then the return value indicates
554 * whether the caller needs to reap kmsg structures that should
555 * be destroyed (by calling ipc_kmsg_reap_delayed)
556 *
557 * If should_destroy is FALSE, this always returns FALSE
558 */
559
560 boolean_t
561 ipc_port_clear_receiver(
562 ipc_port_t port,
563 boolean_t should_destroy)
564 {
565 ipc_mqueue_t mqueue = &port->ip_messages;
566 boolean_t reap_messages = FALSE;
567
568 /*
569 * Pull ourselves out of any sets to which we belong.
570 * We hold the port locked, so even though this acquires and releases
571 * the mqueue lock, we know we won't be added to any other sets.
572 */
573 if (port->ip_in_pset != 0) {
574 ipc_pset_remove_from_all(port);
575 assert(port->ip_in_pset == 0);
576 }
577
578 /*
579 * Send anyone waiting on the port's queue directly away.
580 * Also clear the mscount and seqno.
581 */
582 imq_lock(mqueue);
583 ipc_mqueue_changed(mqueue);
584 port->ip_mscount = 0;
585 mqueue->imq_seqno = 0;
586 port->ip_context = port->ip_guarded = port->ip_strict_guard = 0;
587
588 if (should_destroy) {
589 /*
590 * Mark the mqueue invalid, preventing further send/receive
591 * operations from succeeding. It's important for this to be
592 * done under the same lock hold as the ipc_mqueue_changed
593 * call to avoid additional threads blocking on an mqueue
594 * that's being destroyed.
595 */
596 reap_messages = ipc_mqueue_destroy_locked(mqueue);
597 }
598
599 imq_unlock(&port->ip_messages);
600
601 return reap_messages;
602 }
603
604 /*
605 * Routine: ipc_port_init
606 * Purpose:
607 * Initializes a newly-allocated port.
608 * Doesn't touch the ip_object fields.
609 */
610
611 void
612 ipc_port_init(
613 ipc_port_t port,
614 ipc_space_t space,
615 mach_port_name_t name)
616 {
617 /* port->ip_kobject doesn't have to be initialized */
618
619 port->ip_receiver = space;
620 port->ip_receiver_name = name;
621
622 port->ip_mscount = 0;
623 port->ip_srights = 0;
624 port->ip_sorights = 0;
625
626 port->ip_nsrequest = IP_NULL;
627 port->ip_pdrequest = IP_NULL;
628 port->ip_requests = IPR_NULL;
629
630 port->ip_premsg = IKM_NULL;
631 port->ip_context = 0;
632
633 port->ip_sprequests = 0;
634 port->ip_spimportant = 0;
635 port->ip_impdonation = 0;
636 port->ip_tempowner = 0;
637
638 port->ip_guarded = 0;
639 port->ip_strict_guard = 0;
640 port->ip_impcount = 0;
641
642 port->ip_reserved = 0;
643
644 ipc_mqueue_init(&port->ip_messages,
645 FALSE /* !set */, NULL /* no reserved link */);
646 }
647
648 /*
649 * Routine: ipc_port_alloc
650 * Purpose:
651 * Allocate a port.
652 * Conditions:
653 * Nothing locked. If successful, the port is returned
654 * locked. (The caller doesn't have a reference.)
655 * Returns:
656 * KERN_SUCCESS The port is allocated.
657 * KERN_INVALID_TASK The space is dead.
658 * KERN_NO_SPACE No room for an entry in the space.
659 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
660 */
661
662 kern_return_t
663 ipc_port_alloc(
664 ipc_space_t space,
665 mach_port_name_t *namep,
666 ipc_port_t *portp)
667 {
668 ipc_port_t port;
669 mach_port_name_t name;
670 kern_return_t kr;
671
672 #if MACH_ASSERT
673 uintptr_t buf[IP_CALLSTACK_MAX];
674 ipc_port_callstack_init_debug(&buf[0], IP_CALLSTACK_MAX);
675 #endif /* MACH_ASSERT */
676
677 kr = ipc_object_alloc(space, IOT_PORT,
678 MACH_PORT_TYPE_RECEIVE, 0,
679 &name, (ipc_object_t *) &port);
680 if (kr != KERN_SUCCESS)
681 return kr;
682
683 /* port and space are locked */
684 ipc_port_init(port, space, name);
685
686 #if MACH_ASSERT
687 ipc_port_init_debug(port, &buf[0], IP_CALLSTACK_MAX);
688 #endif /* MACH_ASSERT */
689
690 /* unlock space after init */
691 is_write_unlock(space);
692
693 *namep = name;
694 *portp = port;
695
696 return KERN_SUCCESS;
697 }
698
699 /*
700 * Routine: ipc_port_alloc_name
701 * Purpose:
702 * Allocate a port, with a specific name.
703 * Conditions:
704 * Nothing locked. If successful, the port is returned
705 * locked. (The caller doesn't have a reference.)
706 * Returns:
707 * KERN_SUCCESS The port is allocated.
708 * KERN_INVALID_TASK The space is dead.
709 * KERN_NAME_EXISTS The name already denotes a right.
710 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
711 */
712
713 kern_return_t
714 ipc_port_alloc_name(
715 ipc_space_t space,
716 mach_port_name_t name,
717 ipc_port_t *portp)
718 {
719 ipc_port_t port;
720 kern_return_t kr;
721
722 #if MACH_ASSERT
723 uintptr_t buf[IP_CALLSTACK_MAX];
724 ipc_port_callstack_init_debug(&buf[0], IP_CALLSTACK_MAX);
725 #endif /* MACH_ASSERT */
726
727 kr = ipc_object_alloc_name(space, IOT_PORT,
728 MACH_PORT_TYPE_RECEIVE, 0,
729 name, (ipc_object_t *) &port);
730 if (kr != KERN_SUCCESS)
731 return kr;
732
733 /* port is locked */
734
735 ipc_port_init(port, space, name);
736
737 #if MACH_ASSERT
738 ipc_port_init_debug(port, &buf[0], IP_CALLSTACK_MAX);
739 #endif /* MACH_ASSERT */
740
741 *portp = port;
742
743 return KERN_SUCCESS;
744 }
745
746 /*
747 * Routine: ipc_port_spnotify
748 * Purpose:
749 * Generate send-possible port notifications.
750 * Conditions:
751 * Nothing locked, reference held on port.
752 */
753 void
754 ipc_port_spnotify(
755 ipc_port_t port)
756 {
757 ipc_port_request_index_t index = 0;
758 ipc_table_elems_t size = 0;
759
760 /*
761 * If the port has no send-possible request
762 * armed, don't bother to lock the port.
763 */
764 if (port->ip_sprequests == 0)
765 return;
766
767 ip_lock(port);
768
769 #if IMPORTANCE_INHERITANCE
770 if (port->ip_spimportant != 0) {
771 port->ip_spimportant = 0;
772 if (ipc_port_importance_delta(port, IPID_OPTION_NORMAL, -1) == TRUE) {
773 ip_lock(port);
774 }
775 }
776 #endif /* IMPORTANCE_INHERITANCE */
777
778 if (port->ip_sprequests == 0) {
779 ip_unlock(port);
780 return;
781 }
782 port->ip_sprequests = 0;
783
784 revalidate:
785 if (ip_active(port)) {
786 ipc_port_request_t requests;
787
788 /* table may change each time port unlocked (reload) */
789 requests = port->ip_requests;
790 assert(requests != IPR_NULL);
791
792 /*
793 * no need to go beyond table size when first
794 * we entered - those are future notifications.
795 */
796 if (size == 0)
797 size = requests->ipr_size->its_size;
798
799 /* no need to backtrack either */
800 while (++index < size) {
801 ipc_port_request_t ipr = &requests[index];
802 mach_port_name_t name = ipr->ipr_name;
803 ipc_port_t soright = IPR_SOR_PORT(ipr->ipr_soright);
804 boolean_t armed = IPR_SOR_SPARMED(ipr->ipr_soright);
805
806 if (MACH_PORT_VALID(name) && armed && IP_VALID(soright)) {
807 /* claim send-once right - slot still inuse */
808 ipr->ipr_soright = IP_NULL;
809 ip_unlock(port);
810
811 ipc_notify_send_possible(soright, name);
812
813 ip_lock(port);
814 goto revalidate;
815 }
816 }
817 }
818 ip_unlock(port);
819 return;
820 }
821
822 /*
823 * Routine: ipc_port_dnnotify
824 * Purpose:
825 * Generate dead name notifications for
826 * all outstanding dead-name and send-
827 * possible requests.
828 * Conditions:
829 * Nothing locked.
830 * Port must be inactive.
831 * Reference held on port.
832 */
833 void
834 ipc_port_dnnotify(
835 ipc_port_t port)
836 {
837 ipc_port_request_t requests = port->ip_requests;
838
839 assert(!ip_active(port));
840 if (requests != IPR_NULL) {
841 ipc_table_size_t its = requests->ipr_size;
842 ipc_table_elems_t size = its->its_size;
843 ipc_port_request_index_t index;
844 for (index = 1; index < size; index++) {
845 ipc_port_request_t ipr = &requests[index];
846 mach_port_name_t name = ipr->ipr_name;
847 ipc_port_t soright = IPR_SOR_PORT(ipr->ipr_soright);
848
849 if (MACH_PORT_VALID(name) && IP_VALID(soright)) {
850 ipc_notify_dead_name(soright, name);
851 }
852 }
853 }
854 }
855
856
857 /*
858 * Routine: ipc_port_destroy
859 * Purpose:
860 * Destroys a port. Cleans up queued messages.
861 *
862 * If the port has a backup, it doesn't get destroyed,
863 * but is sent in a port-destroyed notification to the backup.
864 * Conditions:
865 * The port is locked and alive; nothing else locked.
866 * The caller has a reference, which is consumed.
867 * Afterwards, the port is unlocked and dead.
868 */
869
870 void
871 ipc_port_destroy(ipc_port_t port)
872 {
873 ipc_port_t pdrequest, nsrequest;
874 ipc_mqueue_t mqueue;
875 ipc_kmsg_t kmsg;
876
877 #if IMPORTANCE_INHERITANCE
878 ipc_importance_task_t release_imp_task = IIT_NULL;
879 thread_t self = current_thread();
880 boolean_t top = (self->ith_assertions == 0);
881 natural_t assertcnt = 0;
882 #endif /* IMPORTANCE_INHERITANCE */
883
884 assert(ip_active(port));
885 /* port->ip_receiver_name is garbage */
886 /* port->ip_receiver/port->ip_destination is garbage */
887
888 /* check for a backup port */
889 pdrequest = port->ip_pdrequest;
890
891 #if IMPORTANCE_INHERITANCE
892 /* determine how many assertions to drop and from whom */
893 if (port->ip_tempowner != 0) {
894 assert(top);
895 release_imp_task = port->ip_imp_task;
896 if (IIT_NULL != release_imp_task) {
897 port->ip_imp_task = IIT_NULL;
898 assertcnt = port->ip_impcount;
899 }
900 /* Otherwise, nothing to drop */
901 } else {
902 assertcnt = port->ip_impcount;
903 if (pdrequest != IP_NULL)
904 /* mark in limbo for the journey */
905 port->ip_tempowner = 1;
906 }
907
908 if (top)
909 self->ith_assertions = assertcnt;
910 #endif /* IMPORTANCE_INHERITANCE */
911
912 if (pdrequest != IP_NULL) {
913 /* clear receiver, don't destroy the port */
914 (void)ipc_port_clear_receiver(port, FALSE);
915 assert(port->ip_in_pset == 0);
916 assert(port->ip_mscount == 0);
917
918 /* we assume the ref for pdrequest */
919 port->ip_pdrequest = IP_NULL;
920
921 /* make port be in limbo */
922 port->ip_receiver_name = MACH_PORT_NULL;
923 port->ip_destination = IP_NULL;
924 ip_unlock(port);
925
926 /* consumes our refs for port and pdrequest */
927 ipc_notify_port_destroyed(pdrequest, port);
928
929 goto drop_assertions;
930 }
931
932 port->ip_object.io_bits &= ~IO_BITS_ACTIVE;
933 port->ip_timestamp = ipc_port_timestamp();
934 nsrequest = port->ip_nsrequest;
935
936 /*
937 * The mach_msg_* paths don't hold a port lock, they only hold a
938 * reference to the port object. If a thread raced us and is now
939 * blocked waiting for message reception on this mqueue (or waiting
940 * for ipc_mqueue_full), it will never be woken up. We call
941 * ipc_port_clear_receiver() here, _after_ the port has been marked
942 * inactive, to wakeup any threads which may be blocked and ensure
943 * that no other thread can get lost waiting for a wake up on a
944 * port/mqueue that's been destroyed.
945 */
946 boolean_t reap_msgs = FALSE;
947 reap_msgs = ipc_port_clear_receiver(port, TRUE); /* marks mqueue inactive */
948 assert(port->ip_in_pset == 0);
949 assert(port->ip_mscount == 0);
950
951 /*
952 * If the port has a preallocated message buffer and that buffer
953 * is not inuse, free it. If it has an inuse one, then the kmsg
954 * free will detect that we freed the association and it can free it
955 * like a normal buffer.
956 *
957 * Once the port is marked inactive we don't need to keep it locked.
958 */
959 if (IP_PREALLOC(port)) {
960 ipc_port_t inuse_port;
961
962 kmsg = port->ip_premsg;
963 assert(kmsg != IKM_NULL);
964 inuse_port = ikm_prealloc_inuse_port(kmsg);
965 IP_CLEAR_PREALLOC(port, kmsg);
966 ip_unlock(port);
967 if (inuse_port != IP_NULL) {
968 assert(inuse_port == port);
969 } else {
970 ipc_kmsg_free(kmsg);
971 }
972 } else {
973 ip_unlock(port);
974 }
975
976 /* throw away no-senders request */
977 if (nsrequest != IP_NULL)
978 ipc_notify_send_once(nsrequest); /* consumes ref */
979
980 /*
981 * Reap any kmsg objects waiting to be destroyed.
982 * This must be done after we've released the port lock.
983 */
984 if (reap_msgs)
985 ipc_kmsg_reap_delayed();
986
987 mqueue = &port->ip_messages;
988
989 /* cleanup waitq related resources */
990 ipc_mqueue_deinit(mqueue);
991
992 /* generate dead-name notifications */
993 ipc_port_dnnotify(port);
994
995 ipc_kobject_destroy(port);
996
997 ip_release(port); /* consume caller's ref */
998
999 drop_assertions:
1000 #if IMPORTANCE_INHERITANCE
1001 if (release_imp_task != IIT_NULL) {
1002 if (assertcnt > 0) {
1003 assert(top);
1004 self->ith_assertions = 0;
1005 assert(ipc_importance_task_is_any_receiver_type(release_imp_task));
1006 ipc_importance_task_drop_internal_assertion(release_imp_task, assertcnt);
1007 }
1008 ipc_importance_task_release(release_imp_task);
1009
1010 } else if (assertcnt > 0) {
1011 if (top) {
1012 self->ith_assertions = 0;
1013 release_imp_task = current_task()->task_imp_base;
1014 if (ipc_importance_task_is_any_receiver_type(release_imp_task)) {
1015 ipc_importance_task_drop_internal_assertion(release_imp_task, assertcnt);
1016 }
1017 }
1018 }
1019 #endif /* IMPORTANCE_INHERITANCE */
1020 }
1021
1022 /*
1023 * Routine: ipc_port_check_circularity
1024 * Purpose:
1025 * Check if queueing "port" in a message for "dest"
1026 * would create a circular group of ports and messages.
1027 *
1028 * If no circularity (FALSE returned), then "port"
1029 * is changed from "in limbo" to "in transit".
1030 *
1031 * That is, we want to set port->ip_destination == dest,
1032 * but guaranteeing that this doesn't create a circle
1033 * port->ip_destination->ip_destination->... == port
1034 *
1035 * Conditions:
1036 * No ports locked. References held for "port" and "dest".
1037 */
1038
1039 boolean_t
1040 ipc_port_check_circularity(
1041 ipc_port_t port,
1042 ipc_port_t dest)
1043 {
1044 #if IMPORTANCE_INHERITANCE
1045 /* adjust importance counts at the same time */
1046 return ipc_importance_check_circularity(port, dest);
1047 #else
1048 ipc_port_t base;
1049
1050 assert(port != IP_NULL);
1051 assert(dest != IP_NULL);
1052
1053 if (port == dest)
1054 return TRUE;
1055 base = dest;
1056
1057 /*
1058 * First try a quick check that can run in parallel.
1059 * No circularity if dest is not in transit.
1060 */
1061 ip_lock(port);
1062 if (ip_lock_try(dest)) {
1063 if (!ip_active(dest) ||
1064 (dest->ip_receiver_name != MACH_PORT_NULL) ||
1065 (dest->ip_destination == IP_NULL))
1066 goto not_circular;
1067
1068 /* dest is in transit; further checking necessary */
1069
1070 ip_unlock(dest);
1071 }
1072 ip_unlock(port);
1073
1074 ipc_port_multiple_lock(); /* massive serialization */
1075
1076 /*
1077 * Search for the end of the chain (a port not in transit),
1078 * acquiring locks along the way.
1079 */
1080
1081 for (;;) {
1082 ip_lock(base);
1083
1084 if (!ip_active(base) ||
1085 (base->ip_receiver_name != MACH_PORT_NULL) ||
1086 (base->ip_destination == IP_NULL))
1087 break;
1088
1089 base = base->ip_destination;
1090 }
1091
1092 /* all ports in chain from dest to base, inclusive, are locked */
1093
1094 if (port == base) {
1095 /* circularity detected! */
1096
1097 ipc_port_multiple_unlock();
1098
1099 /* port (== base) is in limbo */
1100
1101 assert(ip_active(port));
1102 assert(port->ip_receiver_name == MACH_PORT_NULL);
1103 assert(port->ip_destination == IP_NULL);
1104
1105 while (dest != IP_NULL) {
1106 ipc_port_t next;
1107
1108 /* dest is in transit or in limbo */
1109
1110 assert(ip_active(dest));
1111 assert(dest->ip_receiver_name == MACH_PORT_NULL);
1112
1113 next = dest->ip_destination;
1114 ip_unlock(dest);
1115 dest = next;
1116 }
1117
1118 return TRUE;
1119 }
1120
1121 /*
1122 * The guarantee: lock port while the entire chain is locked.
1123 * Once port is locked, we can take a reference to dest,
1124 * add port to the chain, and unlock everything.
1125 */
1126
1127 ip_lock(port);
1128 ipc_port_multiple_unlock();
1129
1130 not_circular:
1131
1132 /* port is in limbo */
1133
1134 assert(ip_active(port));
1135 assert(port->ip_receiver_name == MACH_PORT_NULL);
1136 assert(port->ip_destination == IP_NULL);
1137
1138 ip_reference(dest);
1139 port->ip_destination = dest;
1140
1141 /* now unlock chain */
1142
1143 ip_unlock(port);
1144
1145 for (;;) {
1146 if (dest == base)
1147 break;
1148
1149 /* port is in transit */
1150
1151 assert(ip_active(dest));
1152 assert(dest->ip_receiver_name == MACH_PORT_NULL);
1153 assert(dest->ip_destination != IP_NULL);
1154
1155 port = dest->ip_destination;
1156 ip_unlock(dest);
1157 dest = port;
1158 }
1159
1160 /* base is not in transit */
1161 assert(!ip_active(base) ||
1162 (base->ip_receiver_name != MACH_PORT_NULL) ||
1163 (base->ip_destination == IP_NULL));
1164
1165 ip_unlock(base);
1166
1167 return FALSE;
1168 #endif /* !IMPORTANCE_INHERITANCE */
1169 }
1170
1171 /*
1172 * Routine: ipc_port_impcount_delta
1173 * Purpose:
1174 * Adjust only the importance count associated with a port.
1175 * If there are any adjustments to be made to receiver task,
1176 * those are handled elsewhere.
1177 *
1178 * For now, be defensive during deductions to make sure the
1179 * impcount for the port doesn't underflow zero. This will
1180 * go away when the port boost addition is made atomic (see
1181 * note in ipc_port_importance_delta()).
1182 * Conditions:
1183 * The port is referenced and locked.
1184 * Nothing else is locked.
1185 */
1186 mach_port_delta_t
1187 ipc_port_impcount_delta(
1188 ipc_port_t port,
1189 mach_port_delta_t delta,
1190 ipc_port_t __unused base)
1191 {
1192 mach_port_delta_t absdelta;
1193
1194 if (!ip_active(port)) {
1195 return 0;
1196 }
1197
1198 /* adding/doing nothing is easy */
1199 if (delta >= 0) {
1200 port->ip_impcount += delta;
1201 return delta;
1202 }
1203
1204 absdelta = 0 - delta;
1205 if (port->ip_impcount >= absdelta) {
1206 port->ip_impcount -= absdelta;
1207 return delta;
1208 }
1209
1210 #if (DEVELOPMENT || DEBUG)
1211 if (port->ip_receiver_name != MACH_PORT_NULL) {
1212 task_t target_task = port->ip_receiver->is_task;
1213 ipc_importance_task_t target_imp = target_task->task_imp_base;
1214 const char *target_procname;
1215 int target_pid;
1216
1217 if (target_imp != IIT_NULL) {
1218 target_procname = target_imp->iit_procname;
1219 target_pid = target_imp->iit_bsd_pid;
1220 } else {
1221 target_procname = "unknown";
1222 target_pid = -1;
1223 }
1224 printf("Over-release of importance assertions for port 0x%x receiver pid %d (%s), "
1225 "dropping %d assertion(s) but port only has %d remaining.\n",
1226 port->ip_receiver_name,
1227 target_pid, target_procname,
1228 absdelta, port->ip_impcount);
1229
1230 } else if (base != IP_NULL) {
1231 task_t target_task = base->ip_receiver->is_task;
1232 ipc_importance_task_t target_imp = target_task->task_imp_base;
1233 const char *target_procname;
1234 int target_pid;
1235
1236 if (target_imp != IIT_NULL) {
1237 target_procname = target_imp->iit_procname;
1238 target_pid = target_imp->iit_bsd_pid;
1239 } else {
1240 target_procname = "unknown";
1241 target_pid = -1;
1242 }
1243 printf("Over-release of importance assertions for port 0x%lx "
1244 "enqueued on port 0x%x with receiver pid %d (%s), "
1245 "dropping %d assertion(s) but port only has %d remaining.\n",
1246 (unsigned long)VM_KERNEL_UNSLIDE_OR_PERM((uintptr_t)port),
1247 base->ip_receiver_name,
1248 target_pid, target_procname,
1249 absdelta, port->ip_impcount);
1250 }
1251 #endif
1252
1253 delta = 0 - port->ip_impcount;
1254 port->ip_impcount = 0;
1255 return delta;
1256 }
1257
1258 /*
1259 * Routine: ipc_port_importance_delta_internal
1260 * Purpose:
1261 * Adjust the importance count through the given port.
1262 * If the port is in transit, apply the delta throughout
1263 * the chain. Determine if the there is a task at the
1264 * base of the chain that wants/needs to be adjusted,
1265 * and if so, apply the delta.
1266 * Conditions:
1267 * The port is referenced and locked on entry.
1268 * Importance may be locked.
1269 * Nothing else is locked.
1270 * The lock may be dropped on exit.
1271 * Returns TRUE if lock was dropped.
1272 */
1273 #if IMPORTANCE_INHERITANCE
1274
1275 boolean_t
1276 ipc_port_importance_delta_internal(
1277 ipc_port_t port,
1278 natural_t options,
1279 mach_port_delta_t *deltap,
1280 ipc_importance_task_t *imp_task)
1281 {
1282 ipc_port_t next, base;
1283 boolean_t dropped = FALSE;
1284
1285 *imp_task = IIT_NULL;
1286
1287 if (*deltap == 0)
1288 return FALSE;
1289
1290 assert(options == IPID_OPTION_NORMAL || options == IPID_OPTION_SENDPOSSIBLE);
1291
1292 base = port;
1293
1294 /* if port is in transit, have to search for end of chain */
1295 if (ip_active(port) &&
1296 port->ip_destination != IP_NULL &&
1297 port->ip_receiver_name == MACH_PORT_NULL) {
1298
1299 dropped = TRUE;
1300
1301 ip_unlock(port);
1302 ipc_port_multiple_lock(); /* massive serialization */
1303 ip_lock(base);
1304
1305 while(ip_active(base) &&
1306 base->ip_destination != IP_NULL &&
1307 base->ip_receiver_name == MACH_PORT_NULL) {
1308
1309 base = base->ip_destination;
1310 ip_lock(base);
1311 }
1312 ipc_port_multiple_unlock();
1313 }
1314
1315 /*
1316 * If the port lock is dropped b/c the port is in transit, there is a
1317 * race window where another thread can drain messages and/or fire a
1318 * send possible notification before we get here.
1319 *
1320 * We solve this race by checking to see if our caller armed the send
1321 * possible notification, whether or not it's been fired yet, and
1322 * whether or not we've already set the port's ip_spimportant bit. If
1323 * we don't need a send-possible boost, then we'll just apply a
1324 * harmless 0-boost to the port.
1325 */
1326 if (options & IPID_OPTION_SENDPOSSIBLE) {
1327 assert(*deltap == 1);
1328 if (port->ip_sprequests && port->ip_spimportant == 0)
1329 port->ip_spimportant = 1;
1330 else
1331 *deltap = 0;
1332 }
1333
1334 /* unlock down to the base, adjusting boost(s) at each level */
1335 for (;;) {
1336 *deltap = ipc_port_impcount_delta(port, *deltap, base);
1337
1338 if (port == base) {
1339 break;
1340 }
1341
1342 /* port is in transit */
1343 assert(port->ip_tempowner == 0);
1344 next = port->ip_destination;
1345 ip_unlock(port);
1346 port = next;
1347 }
1348
1349 /* find the task (if any) to boost according to the base */
1350 if (ip_active(base)) {
1351 if (base->ip_tempowner != 0) {
1352 if (IIT_NULL != base->ip_imp_task)
1353 *imp_task = base->ip_imp_task;
1354 /* otherwise don't boost */
1355
1356 } else if (base->ip_receiver_name != MACH_PORT_NULL) {
1357 ipc_space_t space = base->ip_receiver;
1358
1359 /* only spaces with boost-accepting tasks */
1360 if (space->is_task != TASK_NULL &&
1361 ipc_importance_task_is_any_receiver_type(space->is_task->task_imp_base)) {
1362 *imp_task = space->is_task->task_imp_base;
1363 }
1364 }
1365 }
1366
1367 /*
1368 * Only the base is locked. If we have to hold or drop task
1369 * importance assertions, we'll have to drop that lock as well.
1370 */
1371 if (*imp_task != IIT_NULL) {
1372 /* take a reference before unlocking base */
1373 ipc_importance_task_reference(*imp_task);
1374 }
1375
1376 if (dropped == TRUE) {
1377 ip_unlock(base);
1378 }
1379
1380 return dropped;
1381 }
1382 #endif /* IMPORTANCE_INHERITANCE */
1383
1384 /*
1385 * Routine: ipc_port_importance_delta
1386 * Purpose:
1387 * Adjust the importance count through the given port.
1388 * If the port is in transit, apply the delta throughout
1389 * the chain.
1390 *
1391 * If there is a task at the base of the chain that wants/needs
1392 * to be adjusted, apply the delta.
1393 * Conditions:
1394 * The port is referenced and locked on entry.
1395 * Nothing else is locked.
1396 * The lock may be dropped on exit.
1397 * Returns TRUE if lock was dropped.
1398 */
1399 #if IMPORTANCE_INHERITANCE
1400
1401 boolean_t
1402 ipc_port_importance_delta(
1403 ipc_port_t port,
1404 natural_t options,
1405 mach_port_delta_t delta)
1406 {
1407 ipc_importance_task_t imp_task = IIT_NULL;
1408 boolean_t dropped;
1409
1410 dropped = ipc_port_importance_delta_internal(port, options, &delta, &imp_task);
1411
1412 if (IIT_NULL == imp_task || delta == 0)
1413 return dropped;
1414
1415 if (!dropped)
1416 ip_unlock(port);
1417
1418 assert(ipc_importance_task_is_any_receiver_type(imp_task));
1419
1420 if (delta > 0)
1421 ipc_importance_task_hold_internal_assertion(imp_task, delta);
1422 else
1423 ipc_importance_task_drop_internal_assertion(imp_task, -delta);
1424
1425 ipc_importance_task_release(imp_task);
1426 return TRUE;
1427 }
1428 #endif /* IMPORTANCE_INHERITANCE */
1429
1430 /*
1431 * Routine: ipc_port_lookup_notify
1432 * Purpose:
1433 * Make a send-once notify port from a receive right.
1434 * Returns IP_NULL if name doesn't denote a receive right.
1435 * Conditions:
1436 * The space must be locked (read or write) and active.
1437 * Being the active space, we can rely on thread server_id
1438 * context to give us the proper server level sub-order
1439 * within the space.
1440 */
1441
1442 ipc_port_t
1443 ipc_port_lookup_notify(
1444 ipc_space_t space,
1445 mach_port_name_t name)
1446 {
1447 ipc_port_t port;
1448 ipc_entry_t entry;
1449
1450 assert(is_active(space));
1451
1452 entry = ipc_entry_lookup(space, name);
1453 if (entry == IE_NULL)
1454 return IP_NULL;
1455 if ((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0)
1456 return IP_NULL;
1457
1458 __IGNORE_WCASTALIGN(port = (ipc_port_t) entry->ie_object);
1459 assert(port != IP_NULL);
1460
1461 ip_lock(port);
1462 assert(ip_active(port));
1463 assert(port->ip_receiver_name == name);
1464 assert(port->ip_receiver == space);
1465
1466 ip_reference(port);
1467 port->ip_sorights++;
1468 ip_unlock(port);
1469
1470 return port;
1471 }
1472
1473 /*
1474 * Routine: ipc_port_make_send_locked
1475 * Purpose:
1476 * Make a naked send right from a receive right.
1477 *
1478 * Conditions:
1479 * port locked and active.
1480 */
1481 ipc_port_t
1482 ipc_port_make_send_locked(
1483 ipc_port_t port)
1484 {
1485 assert(ip_active(port));
1486 port->ip_mscount++;
1487 port->ip_srights++;
1488 ip_reference(port);
1489 return port;
1490 }
1491
1492 /*
1493 * Routine: ipc_port_make_send
1494 * Purpose:
1495 * Make a naked send right from a receive right.
1496 */
1497
1498 ipc_port_t
1499 ipc_port_make_send(
1500 ipc_port_t port)
1501 {
1502
1503 if (!IP_VALID(port))
1504 return port;
1505
1506 ip_lock(port);
1507 if (ip_active(port)) {
1508 port->ip_mscount++;
1509 port->ip_srights++;
1510 ip_reference(port);
1511 ip_unlock(port);
1512 return port;
1513 }
1514 ip_unlock(port);
1515 return IP_DEAD;
1516 }
1517
1518 /*
1519 * Routine: ipc_port_copy_send
1520 * Purpose:
1521 * Make a naked send right from another naked send right.
1522 * IP_NULL -> IP_NULL
1523 * IP_DEAD -> IP_DEAD
1524 * dead port -> IP_DEAD
1525 * live port -> port + ref
1526 * Conditions:
1527 * Nothing locked except possibly a space.
1528 */
1529
1530 ipc_port_t
1531 ipc_port_copy_send(
1532 ipc_port_t port)
1533 {
1534 ipc_port_t sright;
1535
1536 if (!IP_VALID(port))
1537 return port;
1538
1539 ip_lock(port);
1540 if (ip_active(port)) {
1541 assert(port->ip_srights > 0);
1542
1543 ip_reference(port);
1544 port->ip_srights++;
1545 sright = port;
1546 } else
1547 sright = IP_DEAD;
1548 ip_unlock(port);
1549
1550 return sright;
1551 }
1552
1553 /*
1554 * Routine: ipc_port_copyout_send
1555 * Purpose:
1556 * Copyout a naked send right (possibly null/dead),
1557 * or if that fails, destroy the right.
1558 * Conditions:
1559 * Nothing locked.
1560 */
1561
1562 mach_port_name_t
1563 ipc_port_copyout_send(
1564 ipc_port_t sright,
1565 ipc_space_t space)
1566 {
1567 mach_port_name_t name;
1568
1569 if (IP_VALID(sright)) {
1570 kern_return_t kr;
1571
1572 kr = ipc_object_copyout(space, (ipc_object_t) sright,
1573 MACH_MSG_TYPE_PORT_SEND, TRUE, &name);
1574 if (kr != KERN_SUCCESS) {
1575 ipc_port_release_send(sright);
1576
1577 if (kr == KERN_INVALID_CAPABILITY)
1578 name = MACH_PORT_DEAD;
1579 else
1580 name = MACH_PORT_NULL;
1581 }
1582 } else
1583 name = CAST_MACH_PORT_TO_NAME(sright);
1584
1585 return name;
1586 }
1587
1588 /*
1589 * Routine: ipc_port_release_send
1590 * Purpose:
1591 * Release a naked send right.
1592 * Consumes a ref for the port.
1593 * Conditions:
1594 * Nothing locked.
1595 */
1596
1597 void
1598 ipc_port_release_send(
1599 ipc_port_t port)
1600 {
1601 ipc_port_t nsrequest = IP_NULL;
1602 mach_port_mscount_t mscount;
1603
1604 if (!IP_VALID(port))
1605 return;
1606
1607 ip_lock(port);
1608
1609 assert(port->ip_srights > 0);
1610 port->ip_srights--;
1611
1612 if (!ip_active(port)) {
1613 ip_unlock(port);
1614 ip_release(port);
1615 return;
1616 }
1617
1618 if (port->ip_srights == 0 &&
1619 port->ip_nsrequest != IP_NULL) {
1620 nsrequest = port->ip_nsrequest;
1621 port->ip_nsrequest = IP_NULL;
1622 mscount = port->ip_mscount;
1623 ip_unlock(port);
1624 ip_release(port);
1625 ipc_notify_no_senders(nsrequest, mscount);
1626 } else {
1627 ip_unlock(port);
1628 ip_release(port);
1629 }
1630 }
1631
1632 /*
1633 * Routine: ipc_port_make_sonce_locked
1634 * Purpose:
1635 * Make a naked send-once right from a receive right.
1636 * Conditions:
1637 * The port is locked and active.
1638 */
1639
1640 ipc_port_t
1641 ipc_port_make_sonce_locked(
1642 ipc_port_t port)
1643 {
1644 assert(ip_active(port));
1645 port->ip_sorights++;
1646 ip_reference(port);
1647 return port;
1648 }
1649
1650 /*
1651 * Routine: ipc_port_make_sonce
1652 * Purpose:
1653 * Make a naked send-once right from a receive right.
1654 * Conditions:
1655 * The port is not locked.
1656 */
1657
1658 ipc_port_t
1659 ipc_port_make_sonce(
1660 ipc_port_t port)
1661 {
1662 if (!IP_VALID(port))
1663 return port;
1664
1665 ip_lock(port);
1666 if (ip_active(port)) {
1667 port->ip_sorights++;
1668 ip_reference(port);
1669 ip_unlock(port);
1670 return port;
1671 }
1672 ip_unlock(port);
1673 return IP_DEAD;
1674 }
1675
1676 /*
1677 * Routine: ipc_port_release_sonce
1678 * Purpose:
1679 * Release a naked send-once right.
1680 * Consumes a ref for the port.
1681 *
1682 * In normal situations, this is never used.
1683 * Send-once rights are only consumed when
1684 * a message (possibly a send-once notification)
1685 * is sent to them.
1686 * Conditions:
1687 * Nothing locked except possibly a space.
1688 */
1689
1690 void
1691 ipc_port_release_sonce(
1692 ipc_port_t port)
1693 {
1694 if (!IP_VALID(port))
1695 return;
1696
1697 ip_lock(port);
1698
1699 assert(port->ip_sorights > 0);
1700
1701 port->ip_sorights--;
1702
1703 ip_unlock(port);
1704 ip_release(port);
1705 }
1706
1707 /*
1708 * Routine: ipc_port_release_receive
1709 * Purpose:
1710 * Release a naked (in limbo or in transit) receive right.
1711 * Consumes a ref for the port; destroys the port.
1712 * Conditions:
1713 * Nothing locked.
1714 */
1715
1716 void
1717 ipc_port_release_receive(
1718 ipc_port_t port)
1719 {
1720 ipc_port_t dest;
1721
1722 if (!IP_VALID(port))
1723 return;
1724
1725 ip_lock(port);
1726 assert(ip_active(port));
1727 assert(port->ip_receiver_name == MACH_PORT_NULL);
1728 dest = port->ip_destination;
1729
1730 ipc_port_destroy(port); /* consumes ref, unlocks */
1731
1732 if (dest != IP_NULL)
1733 ip_release(dest);
1734 }
1735
1736 /*
1737 * Routine: ipc_port_alloc_special
1738 * Purpose:
1739 * Allocate a port in a special space.
1740 * The new port is returned with one ref.
1741 * If unsuccessful, IP_NULL is returned.
1742 * Conditions:
1743 * Nothing locked.
1744 */
1745
1746 ipc_port_t
1747 ipc_port_alloc_special(
1748 ipc_space_t space)
1749 {
1750 ipc_port_t port;
1751
1752 __IGNORE_WCASTALIGN(port = (ipc_port_t) io_alloc(IOT_PORT));
1753 if (port == IP_NULL)
1754 return IP_NULL;
1755
1756 #if MACH_ASSERT
1757 uintptr_t buf[IP_CALLSTACK_MAX];
1758 ipc_port_callstack_init_debug(&buf[0], IP_CALLSTACK_MAX);
1759 #endif /* MACH_ASSERT */
1760
1761 bzero((char *)port, sizeof(*port));
1762 io_lock_init(&port->ip_object);
1763 port->ip_references = 1;
1764 port->ip_object.io_bits = io_makebits(TRUE, IOT_PORT, 0);
1765
1766 ipc_port_init(port, space, 1);
1767
1768 #if MACH_ASSERT
1769 ipc_port_init_debug(port, &buf[0], IP_CALLSTACK_MAX);
1770 #endif /* MACH_ASSERT */
1771
1772 return port;
1773 }
1774
1775 /*
1776 * Routine: ipc_port_dealloc_special
1777 * Purpose:
1778 * Deallocate a port in a special space.
1779 * Consumes one ref for the port.
1780 * Conditions:
1781 * Nothing locked.
1782 */
1783
1784 void
1785 ipc_port_dealloc_special(
1786 ipc_port_t port,
1787 __assert_only ipc_space_t space)
1788 {
1789 ip_lock(port);
1790 assert(ip_active(port));
1791 // assert(port->ip_receiver_name != MACH_PORT_NULL);
1792 assert(port->ip_receiver == space);
1793
1794 /*
1795 * We clear ip_receiver_name and ip_receiver to simplify
1796 * the ipc_space_kernel check in ipc_mqueue_send.
1797 */
1798
1799 port->ip_receiver_name = MACH_PORT_NULL;
1800 port->ip_receiver = IS_NULL;
1801
1802 /* relevant part of ipc_port_clear_receiver */
1803 ipc_port_set_mscount(port, 0);
1804 port->ip_messages.imq_seqno = 0;
1805
1806 ipc_port_destroy(port);
1807 }
1808
1809 /*
1810 * Routine: ipc_port_finalize
1811 * Purpose:
1812 * Called on last reference deallocate to
1813 * free any remaining data associated with the
1814 * port.
1815 * Conditions:
1816 * Nothing locked.
1817 */
1818 void
1819 ipc_port_finalize(
1820 ipc_port_t port)
1821 {
1822 ipc_port_request_t requests = port->ip_requests;
1823
1824 assert(!ip_active(port));
1825 if (requests != IPR_NULL) {
1826 ipc_table_size_t its = requests->ipr_size;
1827 it_requests_free(its, requests);
1828 port->ip_requests = IPR_NULL;
1829 }
1830
1831 ipc_mqueue_deinit(&port->ip_messages);
1832
1833 #if MACH_ASSERT
1834 ipc_port_track_dealloc(port);
1835 #endif /* MACH_ASSERT */
1836 }
1837
1838 /*
1839 * Routine: kdp_mqueue_send_find_owner
1840 * Purpose:
1841 * Discover the owner of the ipc_mqueue that contains the input
1842 * waitq object. The thread blocked on the waitq should be
1843 * waiting for an IPC_MQUEUE_FULL event.
1844 * Conditions:
1845 * The 'waitinfo->wait_type' value should already be set to
1846 * kThreadWaitPortSend.
1847 * Note:
1848 * If we find out that the containing port is actually in
1849 * transit, we reset the wait_type field to reflect this.
1850 */
1851 void
1852 kdp_mqueue_send_find_owner(struct waitq * waitq, __assert_only event64_t event, thread_waitinfo_t * waitinfo)
1853 {
1854 assert(waitinfo->wait_type == kThreadWaitPortSend);
1855 assert(event == IPC_MQUEUE_FULL);
1856
1857 ipc_mqueue_t mqueue = imq_from_waitq(waitq);
1858 ipc_port_t port = ip_from_mq(mqueue); /* we are blocking on send */
1859 assert(kdp_is_in_zone(port, "ipc ports"));
1860
1861 waitinfo->owner = 0;
1862 waitinfo->context = VM_KERNEL_UNSLIDE_OR_PERM(port);
1863 if (ip_lock_held_kdp(port)) {
1864 /*
1865 * someone has the port locked: it may be in an
1866 * inconsistent state: bail
1867 */
1868 waitinfo->owner = STACKSHOT_WAITOWNER_PORT_LOCKED;
1869 return;
1870 }
1871
1872 if (ip_active(port)) {
1873 if (port->ip_tempowner) {
1874 if (port->ip_imp_task != IIT_NULL) {
1875 /* port is held by a tempowner */
1876 waitinfo->owner = pid_from_task(port->ip_imp_task->iit_task);
1877 } else {
1878 waitinfo->owner = STACKSHOT_WAITOWNER_INTRANSIT;
1879 }
1880 } else if (port->ip_receiver_name) {
1881 /* port in a space */
1882 if (port->ip_receiver == ipc_space_kernel) {
1883 /*
1884 * The kernel pid is 0, make this
1885 * distinguishable from no-owner and
1886 * inconsistent port state.
1887 */
1888 waitinfo->owner = STACKSHOT_WAITOWNER_KERNEL;
1889 } else {
1890 waitinfo->owner = pid_from_task(port->ip_receiver->is_task);
1891 }
1892 } else if (port->ip_destination != IP_NULL) {
1893 /* port in transit */
1894 waitinfo->wait_type = kThreadWaitPortSendInTransit;
1895 waitinfo->owner = VM_KERNEL_UNSLIDE_OR_PERM(port->ip_destination);
1896 }
1897 }
1898 }
1899
1900 /*
1901 * Routine: kdp_mqueue_recv_find_owner
1902 * Purpose:
1903 * Discover the "owner" of the ipc_mqueue that contains the input
1904 * waitq object. The thread blocked on the waitq is trying to
1905 * receive on the mqueue.
1906 * Conditions:
1907 * The 'waitinfo->wait_type' value should already be set to
1908 * kThreadWaitPortReceive.
1909 * Note:
1910 * If we find that we are actualy waiting on a port set, we reset
1911 * the wait_type field to reflect this.
1912 */
1913 void
1914 kdp_mqueue_recv_find_owner(struct waitq * waitq, __assert_only event64_t event, thread_waitinfo_t * waitinfo)
1915 {
1916 assert(waitinfo->wait_type == kThreadWaitPortReceive);
1917 assert(event == IPC_MQUEUE_RECEIVE);
1918
1919 ipc_mqueue_t mqueue = imq_from_waitq(waitq);
1920 waitinfo->owner = 0;
1921 if (imq_is_set(mqueue)) { /* we are waiting on a port set */
1922 ipc_pset_t set = ips_from_mq(mqueue);
1923 assert(kdp_is_in_zone(set, "ipc port sets"));
1924
1925 /* Reset wait type to specify waiting on port set receive */
1926 waitinfo->wait_type = kThreadWaitPortSetReceive;
1927 waitinfo->context = VM_KERNEL_UNSLIDE_OR_PERM(set);
1928 if (ips_lock_held_kdp(set)) {
1929 waitinfo->owner = STACKSHOT_WAITOWNER_PSET_LOCKED;
1930 }
1931 /* There is no specific owner "at the other end" of a port set, so leave unset. */
1932 } else {
1933 ipc_port_t port = ip_from_mq(mqueue);
1934 assert(kdp_is_in_zone(port, "ipc ports"));
1935
1936 waitinfo->context = VM_KERNEL_UNSLIDE_OR_PERM(port);
1937 if (ip_lock_held_kdp(port)) {
1938 waitinfo->owner = STACKSHOT_WAITOWNER_PORT_LOCKED;
1939 return;
1940 }
1941
1942 if (ip_active(port)) {
1943 if (port->ip_receiver_name != MACH_PORT_NULL) {
1944 waitinfo->owner = port->ip_receiver_name;
1945 } else {
1946 waitinfo->owner = STACKSHOT_WAITOWNER_INTRANSIT;
1947 }
1948 }
1949 }
1950 }
1951
1952 #if MACH_ASSERT
1953 #include <kern/machine.h>
1954
1955 /*
1956 * Keep a list of all allocated ports.
1957 * Allocation is intercepted via ipc_port_init;
1958 * deallocation is intercepted via io_free.
1959 */
1960 #if 0
1961 queue_head_t port_alloc_queue;
1962 lck_spin_t port_alloc_queue_lock;
1963 #endif
1964
1965 unsigned long port_count = 0;
1966 unsigned long port_count_warning = 20000;
1967 unsigned long port_timestamp = 0;
1968
1969 void db_port_stack_trace(
1970 ipc_port_t port);
1971 void db_ref(
1972 int refs);
1973 int db_port_walk(
1974 unsigned int verbose,
1975 unsigned int display,
1976 unsigned int ref_search,
1977 unsigned int ref_target);
1978
1979 /*
1980 * Initialize global state needed for run-time
1981 * port debugging.
1982 */
1983 void
1984 ipc_port_debug_init(void)
1985 {
1986 #if 0
1987 queue_init(&port_alloc_queue);
1988 lck_spin_init(&port_alloc_queue_lock, &ipc_lck_grp, &ipc_lck_attr);
1989 #endif
1990
1991 if (!PE_parse_boot_argn("ipc_portbt", &ipc_portbt, sizeof (ipc_portbt)))
1992 ipc_portbt = 0;
1993 }
1994
1995 #ifdef MACH_BSD
1996 extern int proc_pid(struct proc*);
1997 #endif /* MACH_BSD */
1998
1999 /*
2000 * Initialize all of the debugging state in a port.
2001 * Insert the port into a global list of all allocated ports.
2002 */
2003 void
2004 ipc_port_init_debug(
2005 ipc_port_t port,
2006 uintptr_t *callstack,
2007 unsigned int callstack_max)
2008 {
2009 unsigned int i;
2010
2011 port->ip_thread = current_thread();
2012 port->ip_timetrack = port_timestamp++;
2013 for (i = 0; i < callstack_max; ++i)
2014 port->ip_callstack[i] = callstack[i];
2015 for (i = 0; i < IP_NSPARES; ++i)
2016 port->ip_spares[i] = 0;
2017
2018 #ifdef MACH_BSD
2019 task_t task = current_task();
2020 if (task != TASK_NULL) {
2021 struct proc* proc = (struct proc*) get_bsdtask_info(task);
2022 if (proc)
2023 port->ip_spares[0] = proc_pid(proc);
2024 }
2025 #endif /* MACH_BSD */
2026
2027 #if 0
2028 lck_spin_lock(&port_alloc_queue_lock);
2029 ++port_count;
2030 if (port_count_warning > 0 && port_count >= port_count_warning)
2031 assert(port_count < port_count_warning);
2032 queue_enter(&port_alloc_queue, port, ipc_port_t, ip_port_links);
2033 lck_spin_unlock(&port_alloc_queue_lock);
2034 #endif
2035 }
2036
2037 /*
2038 * Routine: ipc_port_callstack_init_debug
2039 * Purpose:
2040 * Calls the machine-dependent routine to
2041 * fill in an array with up to IP_CALLSTACK_MAX
2042 * levels of return pc information
2043 * Conditions:
2044 * May block (via copyin)
2045 */
2046 void
2047 ipc_port_callstack_init_debug(
2048 uintptr_t *callstack,
2049 unsigned int callstack_max)
2050 {
2051 unsigned int i;
2052
2053 /* guarantee the callstack is initialized */
2054 for (i=0; i < callstack_max; i++)
2055 callstack[i] = 0;
2056
2057 if (ipc_portbt)
2058 machine_callstack(callstack, callstack_max);
2059 }
2060
2061 /*
2062 * Remove a port from the queue of allocated ports.
2063 * This routine should be invoked JUST prior to
2064 * deallocating the actual memory occupied by the port.
2065 */
2066 #if 1
2067 void
2068 ipc_port_track_dealloc(
2069 __unused ipc_port_t port)
2070 {
2071 }
2072 #else
2073 void
2074 ipc_port_track_dealloc(
2075 ipc_port_t port)
2076 {
2077 lck_spin_lock(&port_alloc_queue_lock);
2078 assert(port_count > 0);
2079 --port_count;
2080 queue_remove(&port_alloc_queue, port, ipc_port_t, ip_port_links);
2081 lck_spin_unlock(&port_alloc_queue_lock);
2082 }
2083 #endif
2084
2085
2086 #endif /* MACH_ASSERT */