]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_mig.c
c031c089d3d3183999d7bd3fae5543c5289236a6
[apple/xnu.git] / osfmk / kern / ipc_mig.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52
53 #include <mach/boolean.h>
54 #include <mach/port.h>
55 #include <mach/mig.h>
56 #include <mach/mig_errors.h>
57 #include <mach/mach_types.h>
58 #include <mach/mach_traps.h>
59
60 #include <kern/ipc_tt.h>
61 #include <kern/ipc_mig.h>
62 #include <kern/kalloc.h>
63 #include <kern/task.h>
64 #include <kern/thread.h>
65 #include <kern/ipc_kobject.h>
66 #include <kern/misc_protos.h>
67
68 #include <ipc/port.h>
69 #include <ipc/ipc_kmsg.h>
70 #include <ipc/ipc_entry.h>
71 #include <ipc/ipc_object.h>
72 #include <ipc/ipc_mqueue.h>
73 #include <ipc/ipc_space.h>
74 #include <ipc/ipc_port.h>
75 #include <ipc/ipc_pset.h>
76 #include <vm/vm_map.h>
77
78 /*
79 * Routine: mach_msg_send_from_kernel
80 * Purpose:
81 * Send a message from the kernel.
82 *
83 * This is used by the client side of KernelUser interfaces
84 * to implement SimpleRoutines. Currently, this includes
85 * memory_object messages.
86 * Conditions:
87 * Nothing locked.
88 * Returns:
89 * MACH_MSG_SUCCESS Sent the message.
90 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
91 * MACH_SEND_INVALID_DEST Bad destination port.
92 */
93
94 mach_msg_return_t
95 mach_msg_send_from_kernel(
96 mach_msg_header_t *msg,
97 mach_msg_size_t send_size)
98 {
99 ipc_kmsg_t kmsg;
100 mach_msg_return_t mr;
101
102 if (!MACH_PORT_VALID((mach_port_name_t)msg->msgh_remote_port))
103 return MACH_SEND_INVALID_DEST;
104
105 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
106 if (mr != MACH_MSG_SUCCESS)
107 return mr;
108
109 ipc_kmsg_copyin_from_kernel(kmsg);
110 ipc_kmsg_send_always(kmsg);
111
112 return MACH_MSG_SUCCESS;
113 }
114
115 /*
116 * Routine: mach_msg_rpc_from_kernel
117 * Purpose:
118 * Send a message from the kernel and receive a reply.
119 * Uses ith_rpc_reply for the reply port.
120 *
121 * This is used by the client side of KernelUser interfaces
122 * to implement Routines.
123 * Conditions:
124 * Nothing locked.
125 * Returns:
126 * MACH_MSG_SUCCESS Sent the message.
127 * MACH_RCV_PORT_DIED The reply port was deallocated.
128 */
129
130 mach_msg_return_t
131 mach_msg_rpc_from_kernel(
132 mach_msg_header_t *msg,
133 mach_msg_size_t send_size,
134 mach_msg_size_t rcv_size)
135 {
136 thread_t self = current_thread();
137 ipc_port_t reply;
138 ipc_kmsg_t kmsg;
139 mach_port_seqno_t seqno;
140 mach_msg_return_t mr;
141
142 assert(MACH_PORT_VALID((mach_port_name_t)msg->msgh_remote_port));
143 assert(msg->msgh_local_port == MACH_PORT_NULL);
144
145 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
146 if (mr != MACH_MSG_SUCCESS)
147 return mr;
148
149 reply = self->ith_rpc_reply;
150 if (reply == IP_NULL) {
151 reply = ipc_port_alloc_reply();
152 if ((reply == IP_NULL) ||
153 (self->ith_rpc_reply != IP_NULL))
154 panic("mach_msg_rpc_from_kernel");
155 self->ith_rpc_reply = reply;
156 }
157
158 /* insert send-once right for the reply port */
159 kmsg->ikm_header->msgh_local_port = reply;
160 kmsg->ikm_header->msgh_bits |=
161 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE);
162
163 ipc_port_reference(reply);
164
165 ipc_kmsg_copyin_from_kernel(kmsg);
166
167 ipc_kmsg_send_always(kmsg);
168
169 for (;;) {
170 ipc_mqueue_t mqueue;
171
172 ip_lock(reply);
173 if ( !ip_active(reply)) {
174 ip_unlock(reply);
175 ipc_port_release(reply);
176 return MACH_RCV_PORT_DIED;
177 }
178 if (!self->active) {
179 ip_unlock(reply);
180 ipc_port_release(reply);
181 return MACH_RCV_INTERRUPTED;
182 }
183
184 assert(reply->ip_pset_count == 0);
185 mqueue = &reply->ip_messages;
186 ip_unlock(reply);
187
188 self->ith_continuation = (void (*)(mach_msg_return_t))0;
189
190 ipc_mqueue_receive(mqueue,
191 MACH_MSG_OPTION_NONE,
192 MACH_MSG_SIZE_MAX,
193 MACH_MSG_TIMEOUT_NONE,
194 THREAD_INTERRUPTIBLE);
195
196 mr = self->ith_state;
197 kmsg = self->ith_kmsg;
198 seqno = self->ith_seqno;
199
200 if (mr == MACH_MSG_SUCCESS)
201 {
202 break;
203 }
204
205 assert(mr == MACH_RCV_INTERRUPTED);
206
207 if (self->handlers) {
208 ipc_port_release(reply);
209 return(mr);
210 }
211 }
212 ipc_port_release(reply);
213
214 /*
215 * XXXXX Set manually for now ...
216 * No, why even bother, since the effort is wasted?
217 *
218 { mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
219 ((vm_offset_t)&kmsg->ikm_header + kmsg->ikm_header.msgh_size);
220 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
221 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
222 }
223 *****/
224
225 if (rcv_size < kmsg->ikm_header->msgh_size) {
226 ipc_kmsg_copyout_dest(kmsg, ipc_space_reply);
227 ipc_kmsg_put_to_kernel(msg, kmsg, kmsg->ikm_header->msgh_size);
228 return MACH_RCV_TOO_LARGE;
229 }
230
231 /*
232 * We want to preserve rights and memory in reply!
233 * We don't have to put them anywhere; just leave them
234 * as they are.
235 */
236
237 ipc_kmsg_copyout_to_kernel(kmsg, ipc_space_reply);
238 ipc_kmsg_put_to_kernel(msg, kmsg, kmsg->ikm_header->msgh_size);
239 return MACH_MSG_SUCCESS;
240 }
241
242
243 /************** These Calls are set up for kernel-loaded tasks/threads **************/
244
245 /*
246 * Routine: mach_msg_overwrite
247 * Purpose:
248 * Like mach_msg_overwrite_trap except that message buffers
249 * live in kernel space. Doesn't handle any options.
250 *
251 * This is used by in-kernel server threads to make
252 * kernel calls, to receive request messages, and
253 * to send reply messages.
254 * Conditions:
255 * Nothing locked.
256 * Returns:
257 */
258
259 mach_msg_return_t
260 mach_msg_overwrite(
261 mach_msg_header_t *msg,
262 mach_msg_option_t option,
263 mach_msg_size_t send_size,
264 mach_msg_size_t rcv_size,
265 mach_port_name_t rcv_name,
266 __unused mach_msg_timeout_t msg_timeout,
267 __unused mach_port_name_t notify,
268 __unused mach_msg_header_t *rcv_msg,
269 __unused mach_msg_size_t rcv_msg_size)
270 {
271 ipc_space_t space = current_space();
272 vm_map_t map = current_map();
273 ipc_kmsg_t kmsg;
274 mach_port_seqno_t seqno;
275 mach_msg_return_t mr;
276 mach_msg_format_0_trailer_t *trailer;
277
278 if (option & MACH_SEND_MSG) {
279 mach_msg_size_t msg_and_trailer_size;
280 mach_msg_max_trailer_t *max_trailer;
281
282 if ((send_size < sizeof(mach_msg_header_t)) || (send_size & 3))
283 return MACH_SEND_MSG_TOO_SMALL;
284
285 msg_and_trailer_size = send_size + MAX_TRAILER_SIZE;
286
287 kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
288
289 if (kmsg == IKM_NULL)
290 return MACH_SEND_NO_BUFFER;
291
292 (void) memcpy((void *) kmsg->ikm_header, (const void *) msg, send_size);
293
294 kmsg->ikm_header->msgh_size = send_size;
295
296 /*
297 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
298 * However, the internal size field of the trailer (msgh_trailer_size)
299 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
300 * the cases where no implicit data is requested.
301 */
302 max_trailer = (mach_msg_max_trailer_t *) ((vm_offset_t)kmsg->ikm_header + send_size);
303 max_trailer->msgh_sender = current_thread()->task->sec_token;
304 max_trailer->msgh_audit = current_thread()->task->audit_token;
305 max_trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
306 max_trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
307
308 mr = ipc_kmsg_copyin(kmsg, space, map, MACH_PORT_NULL);
309 if (mr != MACH_MSG_SUCCESS) {
310 ipc_kmsg_free(kmsg);
311 return mr;
312 }
313
314 do
315 mr = ipc_kmsg_send(kmsg, MACH_MSG_OPTION_NONE,
316 MACH_MSG_TIMEOUT_NONE);
317 while (mr == MACH_SEND_INTERRUPTED);
318 assert(mr == MACH_MSG_SUCCESS);
319 }
320
321 if (option & MACH_RCV_MSG) {
322 thread_t self = current_thread();
323
324 do {
325 ipc_object_t object;
326 ipc_mqueue_t mqueue;
327
328 mr = ipc_mqueue_copyin(space, rcv_name,
329 &mqueue, &object);
330 if (mr != MACH_MSG_SUCCESS)
331 return mr;
332 /* hold ref for object */
333
334 self->ith_continuation = (void (*)(mach_msg_return_t))0;
335 ipc_mqueue_receive(mqueue,
336 MACH_MSG_OPTION_NONE,
337 MACH_MSG_SIZE_MAX,
338 MACH_MSG_TIMEOUT_NONE,
339 THREAD_ABORTSAFE);
340 mr = self->ith_state;
341 kmsg = self->ith_kmsg;
342 seqno = self->ith_seqno;
343
344 ipc_object_release(object);
345
346 } while (mr == MACH_RCV_INTERRUPTED);
347 if (mr != MACH_MSG_SUCCESS)
348 return mr;
349
350 trailer = (mach_msg_format_0_trailer_t *)
351 ((vm_offset_t)kmsg->ikm_header + kmsg->ikm_header->msgh_size);
352 if (option & MACH_RCV_TRAILER_MASK) {
353 trailer->msgh_seqno = seqno;
354 trailer->msgh_trailer_size = REQUESTED_TRAILER_SIZE(option);
355 }
356
357 if (rcv_size < (kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size)) {
358 ipc_kmsg_copyout_dest(kmsg, space);
359 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg);
360 ipc_kmsg_free(kmsg);
361 return MACH_RCV_TOO_LARGE;
362 }
363
364 mr = ipc_kmsg_copyout(kmsg, space, map, MACH_PORT_NULL,
365 MACH_MSG_BODY_NULL);
366 if (mr != MACH_MSG_SUCCESS) {
367 if ((mr &~ MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) {
368 ipc_kmsg_put_to_kernel(msg, kmsg,
369 kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size);
370 } else {
371 ipc_kmsg_copyout_dest(kmsg, space);
372 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg);
373 ipc_kmsg_free(kmsg);
374 }
375
376 return mr;
377 }
378
379 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header,
380 kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size);
381 ipc_kmsg_free(kmsg);
382 }
383
384 return MACH_MSG_SUCCESS;
385 }
386
387 /*
388 * Routine: mig_get_reply_port
389 * Purpose:
390 * Called by client side interfaces living in the kernel
391 * to get a reply port.
392 */
393 mach_port_t
394 mig_get_reply_port(void)
395 {
396 return (MACH_PORT_NULL);
397 }
398
399 /*
400 * Routine: mig_dealloc_reply_port
401 * Purpose:
402 * Called by client side interfaces to get rid of a reply port.
403 */
404
405 void
406 mig_dealloc_reply_port(
407 __unused mach_port_t reply_port)
408 {
409 panic("mig_dealloc_reply_port");
410 }
411
412 /*
413 * Routine: mig_put_reply_port
414 * Purpose:
415 * Called by client side interfaces after each RPC to
416 * let the client recycle the reply port if it wishes.
417 */
418 void
419 mig_put_reply_port(
420 __unused mach_port_t reply_port)
421 {
422 }
423
424 /*
425 * mig_strncpy.c - by Joshua Block
426 *
427 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
428 * OUGHT to do: Copies the (null terminated) string in src into dest, a
429 * buffer of length len. Assures that the copy is still null terminated
430 * and doesn't overflow the buffer, truncating the copy if necessary.
431 *
432 * Parameters:
433 *
434 * dest - Pointer to destination buffer.
435 *
436 * src - Pointer to source string.
437 *
438 * len - Length of destination buffer.
439 */
440 int
441 mig_strncpy(
442 char *dest,
443 const char *src,
444 int len)
445 {
446 int i = 0;
447
448 if (len > 0)
449 if (dest != NULL) {
450 if (src != NULL)
451 for (i=1; i<len; i++)
452 if (! (*dest++ = *src++))
453 return i;
454 *dest = '\0';
455 }
456 return i;
457 }
458
459 char *
460 mig_user_allocate(
461 vm_size_t size)
462 {
463 return (char *)kalloc(size);
464 }
465
466 void
467 mig_user_deallocate(
468 char *data,
469 vm_size_t size)
470 {
471 kfree(data, size);
472 }
473
474 /*
475 * Routine: mig_object_init
476 * Purpose:
477 * Initialize the base class portion of a MIG object. We
478 * will lazy init the port, so just clear it for now.
479 */
480 kern_return_t
481 mig_object_init(
482 mig_object_t mig_object,
483 const IMIGObject *interface)
484 {
485 if (mig_object == MIG_OBJECT_NULL)
486 return KERN_INVALID_ARGUMENT;
487 mig_object->pVtbl = (const IMIGObjectVtbl *)interface;
488 mig_object->port = MACH_PORT_NULL;
489 return KERN_SUCCESS;
490 }
491
492 /*
493 * Routine: mig_object_destroy
494 * Purpose:
495 * The object is being freed. This call lets us clean
496 * up any state we have have built up over the object's
497 * lifetime.
498 * Conditions:
499 * Since notifications and the port hold references on
500 * on the object, neither can exist when this is called.
501 * This is a good place to assert() that condition.
502 */
503 void
504 mig_object_destroy(
505 __assert_only mig_object_t mig_object)
506 {
507 assert(mig_object->port == MACH_PORT_NULL);
508 return;
509 }
510
511 /*
512 * Routine: mig_object_reference
513 * Purpose:
514 * Pure virtual helper to invoke the MIG object's AddRef
515 * method.
516 * Conditions:
517 * MIG object port may be locked.
518 */
519 void
520 mig_object_reference(
521 mig_object_t mig_object)
522 {
523 assert(mig_object != MIG_OBJECT_NULL);
524 mig_object->pVtbl->AddRef((IMIGObject *)mig_object);
525 }
526
527 /*
528 * Routine: mig_object_deallocate
529 * Purpose:
530 * Pure virtual helper to invoke the MIG object's Release
531 * method.
532 * Conditions:
533 * Nothing locked.
534 */
535 void
536 mig_object_deallocate(
537 mig_object_t mig_object)
538 {
539 assert(mig_object != MIG_OBJECT_NULL);
540 mig_object->pVtbl->Release((IMIGObject *)mig_object);
541 }
542
543 /*
544 * Routine: convert_mig_object_to_port [interface]
545 * Purpose:
546 * Base implementation of MIG outtrans routine to convert from
547 * a mig object reference to a new send right on the object's
548 * port. The object reference is consumed.
549 * Returns:
550 * IP_NULL - Null MIG object supplied
551 * Otherwise, a newly made send right for the port
552 * Conditions:
553 * Nothing locked.
554 */
555 ipc_port_t
556 convert_mig_object_to_port(
557 mig_object_t mig_object)
558 {
559 ipc_port_t port;
560 boolean_t deallocate = TRUE;
561
562 if (mig_object == MIG_OBJECT_NULL)
563 return IP_NULL;
564
565 port = mig_object->port;
566 while ((port == IP_NULL) ||
567 ((port = ipc_port_make_send(port)) == IP_NULL)) {
568 ipc_port_t previous;
569
570 /*
571 * Either the port was never set up, or it was just
572 * deallocated out from under us by the no-senders
573 * processing. In either case, we must:
574 * Attempt to make one
575 * Arrange for no senders
576 * Try to atomically register it with the object
577 * Destroy it if we are raced.
578 */
579 port = ipc_port_alloc_kernel();
580 ip_lock(port);
581 ipc_kobject_set_atomically(port,
582 (ipc_kobject_t) mig_object,
583 IKOT_MIG);
584
585 /* make a sonce right for the notification */
586 port->ip_sorights++;
587 ip_reference(port);
588
589 ipc_port_nsrequest(port, 1, port, &previous);
590 /* port unlocked */
591
592 assert(previous == IP_NULL);
593
594 if (hw_compare_and_store((uint32_t)IP_NULL, (uint32_t)port,
595 (uint32_t *)&mig_object->port)) {
596 deallocate = FALSE;
597 } else {
598 ipc_port_dealloc_kernel(port);
599 port = mig_object->port;
600 }
601 }
602
603 if (deallocate)
604 mig_object->pVtbl->Release((IMIGObject *)mig_object);
605
606 return (port);
607 }
608
609
610 /*
611 * Routine: convert_port_to_mig_object [interface]
612 * Purpose:
613 * Base implementation of MIG intrans routine to convert from
614 * an incoming port reference to a new reference on the
615 * underlying object. A new reference must be created, because
616 * the port's reference could go away asynchronously.
617 * Returns:
618 * NULL - Not an active MIG object port or iid not supported
619 * Otherwise, a reference to the underlying MIG interface
620 * Conditions:
621 * Nothing locked.
622 */
623 mig_object_t
624 convert_port_to_mig_object(
625 ipc_port_t port,
626 const MIGIID *iid)
627 {
628 mig_object_t mig_object;
629 void *ppv;
630
631 if (!IP_VALID(port))
632 return NULL;
633
634 ip_lock(port);
635 if (!ip_active(port) || (ip_kotype(port) != IKOT_MIG)) {
636 ip_unlock(port);
637 return NULL;
638 }
639
640 /*
641 * Our port points to some MIG object interface. Now
642 * query it to get a reference to the desired interface.
643 */
644 ppv = NULL;
645 mig_object = (mig_object_t)port->ip_kobject;
646 mig_object->pVtbl->QueryInterface((IMIGObject *)mig_object, iid, &ppv);
647 ip_unlock(port);
648 return (mig_object_t)ppv;
649 }
650
651 /*
652 * Routine: mig_object_no_senders [interface]
653 * Purpose:
654 * Base implementation of a no-senders notification handler
655 * for MIG objects. If there truly are no more senders, must
656 * destroy the port and drop its reference on the object.
657 * Returns:
658 * TRUE - port deallocate and reference dropped
659 * FALSE - more senders arrived, re-registered for notification
660 * Conditions:
661 * Nothing locked.
662 */
663
664 boolean_t
665 mig_object_no_senders(
666 ipc_port_t port,
667 mach_port_mscount_t mscount)
668 {
669 mig_object_t mig_object;
670
671 ip_lock(port);
672 if (port->ip_mscount > mscount) {
673 ipc_port_t previous;
674
675 /*
676 * Somebody created new send rights while the
677 * notification was in-flight. Just create a
678 * new send-once right and re-register with
679 * the new (higher) mscount threshold.
680 */
681 /* make a sonce right for the notification */
682 port->ip_sorights++;
683 ip_reference(port);
684 ipc_port_nsrequest(port, mscount, port, &previous);
685 /* port unlocked */
686
687 assert(previous == IP_NULL);
688 return (FALSE);
689 }
690
691 /*
692 * Clear the port pointer while we have it locked.
693 */
694 mig_object = (mig_object_t)port->ip_kobject;
695 mig_object->port = IP_NULL;
696
697 /*
698 * Bring the sequence number and mscount in
699 * line with ipc_port_destroy assertion.
700 */
701 port->ip_mscount = 0;
702 port->ip_messages.imq_seqno = 0;
703 ipc_port_destroy(port); /* releases lock */
704
705 /*
706 * Release the port's reference on the object.
707 */
708 mig_object->pVtbl->Release((IMIGObject *)mig_object);
709 return (TRUE);
710 }
711
712 /*
713 * Kernel implementation of the notification chain for MIG object
714 * is kept separate from the actual objects, since there are expected
715 * to be much fewer of them than actual objects.
716 *
717 * The implementation of this part of MIG objects is coming
718 * "Real Soon Now"(TM).
719 */