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