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