]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_mig.c
xnu-3789.60.24.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_mig.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, 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_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990 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 */
58
59 #include <mach/boolean.h>
60 #include <mach/port.h>
61 #include <mach/mig.h>
62 #include <mach/mig_errors.h>
63 #include <mach/mach_types.h>
64 #include <mach/mach_traps.h>
65
66 #include <kern/ipc_tt.h>
67 #include <kern/ipc_mig.h>
68 #include <kern/kalloc.h>
69 #include <kern/task.h>
70 #include <kern/thread.h>
71 #include <kern/ipc_kobject.h>
72 #include <kern/misc_protos.h>
73
74 #include <ipc/port.h>
75 #include <ipc/ipc_kmsg.h>
76 #include <ipc/ipc_entry.h>
77 #include <ipc/ipc_object.h>
78 #include <ipc/ipc_mqueue.h>
79 #include <ipc/ipc_space.h>
80 #include <ipc/ipc_port.h>
81 #include <ipc/ipc_pset.h>
82 #include <ipc/ipc_notify.h>
83 #include <vm/vm_map.h>
84
85 #include <libkern/OSAtomic.h>
86
87 /*
88 * Routine: mach_msg_send_from_kernel
89 * Purpose:
90 * Send a message from the kernel.
91 *
92 * This is used by the client side of KernelUser interfaces
93 * to implement SimpleRoutines. Currently, this includes
94 * memory_object messages.
95 * Conditions:
96 * Nothing locked.
97 * Returns:
98 * MACH_MSG_SUCCESS Sent the message.
99 * MACH_SEND_INVALID_DEST Bad destination port.
100 * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer
101 * or destination is above kernel limit
102 */
103
104 #if IKM_SUPPORT_LEGACY
105
106 #undef mach_msg_send_from_kernel
107 mach_msg_return_t mach_msg_send_from_kernel(
108 mach_msg_header_t *msg,
109 mach_msg_size_t send_size);
110
111 mach_msg_return_t
112 mach_msg_send_from_kernel(
113 mach_msg_header_t *msg,
114 mach_msg_size_t send_size)
115 {
116 ipc_kmsg_t kmsg;
117 mach_msg_return_t mr;
118
119 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
120
121 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
122 if (mr != MACH_MSG_SUCCESS) {
123 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
124 return mr;
125 }
126
127 mr = ipc_kmsg_copyin_from_kernel_legacy(kmsg);
128 if (mr != MACH_MSG_SUCCESS) {
129 ipc_kmsg_free(kmsg);
130 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
131 return mr;
132 }
133
134 /*
135 * respect the thread's SEND_IMPORTANCE option to allow importance
136 * donation from the kernel-side of user threads
137 * (11938665 & 23925818)
138 */
139 mach_msg_option_t option = MACH_SEND_KERNEL_DEFAULT;
140 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
141 option &= ~MACH_SEND_NOIMPORTANCE;
142
143 mr = ipc_kmsg_send(kmsg, option, MACH_MSG_TIMEOUT_NONE);
144 if (mr != MACH_MSG_SUCCESS) {
145 ipc_kmsg_destroy(kmsg);
146 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
147 }
148
149 return mr;
150 }
151
152 #endif /* IKM_SUPPORT_LEGACY */
153
154 mach_msg_return_t
155 mach_msg_send_from_kernel_proper(
156 mach_msg_header_t *msg,
157 mach_msg_size_t send_size)
158 {
159 ipc_kmsg_t kmsg;
160 mach_msg_return_t mr;
161
162 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
163
164 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
165 if (mr != MACH_MSG_SUCCESS) {
166 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
167 return mr;
168 }
169
170 mr = ipc_kmsg_copyin_from_kernel(kmsg);
171 if (mr != MACH_MSG_SUCCESS) {
172 ipc_kmsg_free(kmsg);
173 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
174 return mr;
175 }
176
177 /*
178 * respect the thread's SEND_IMPORTANCE option to force importance
179 * donation from the kernel-side of user threads
180 * (11938665 & 23925818)
181 */
182 mach_msg_option_t option = MACH_SEND_KERNEL_DEFAULT;
183 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
184 option &= ~MACH_SEND_NOIMPORTANCE;
185
186 mr = ipc_kmsg_send(kmsg, option, MACH_MSG_TIMEOUT_NONE);
187 if (mr != MACH_MSG_SUCCESS) {
188 ipc_kmsg_destroy(kmsg);
189 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
190 }
191
192 return mr;
193 }
194
195 mach_msg_return_t
196 mach_msg_send_from_kernel_with_options(
197 mach_msg_header_t *msg,
198 mach_msg_size_t send_size,
199 mach_msg_option_t option,
200 mach_msg_timeout_t timeout_val)
201 {
202 ipc_kmsg_t kmsg;
203 mach_msg_return_t mr;
204
205 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
206
207 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
208 if (mr != MACH_MSG_SUCCESS) {
209 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
210 return mr;
211 }
212
213 mr = ipc_kmsg_copyin_from_kernel(kmsg);
214 if (mr != MACH_MSG_SUCCESS) {
215 ipc_kmsg_free(kmsg);
216 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
217 return mr;
218 }
219
220 /*
221 * Until we are sure of its effects, we are disabling
222 * importance donation from the kernel-side of user
223 * threads in importance-donating tasks - unless the
224 * option to force importance donation is passed in,
225 * or the thread's SEND_IMPORTANCE option has been set.
226 * (11938665 & 23925818)
227 */
228 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
229 option &= ~MACH_SEND_NOIMPORTANCE;
230 else if ((option & MACH_SEND_IMPORTANCE) == 0)
231 option |= MACH_SEND_NOIMPORTANCE;
232
233 mr = ipc_kmsg_send(kmsg, option, timeout_val);
234
235 if (mr != MACH_MSG_SUCCESS) {
236 ipc_kmsg_destroy(kmsg);
237 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
238 }
239
240 return mr;
241 }
242
243
244 #if IKM_SUPPORT_LEGACY
245
246 mach_msg_return_t
247 mach_msg_send_from_kernel_with_options_legacy(
248 mach_msg_header_t *msg,
249 mach_msg_size_t send_size,
250 mach_msg_option_t option,
251 mach_msg_timeout_t timeout_val)
252 {
253 ipc_kmsg_t kmsg;
254 mach_msg_return_t mr;
255
256 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
257
258 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
259 if (mr != MACH_MSG_SUCCESS) {
260 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
261 return mr;
262 }
263
264 mr = ipc_kmsg_copyin_from_kernel_legacy(kmsg);
265 if (mr != MACH_MSG_SUCCESS) {
266 ipc_kmsg_free(kmsg);
267 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
268 return mr;
269 }
270
271 /*
272 * Until we are sure of its effects, we are disabling
273 * importance donation from the kernel-side of user
274 * threads in importance-donating tasks.
275 * (11938665 & 23925818)
276 */
277 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
278 option &= ~MACH_SEND_NOIMPORTANCE;
279 else
280 option |= MACH_SEND_NOIMPORTANCE;
281
282 mr = ipc_kmsg_send(kmsg, option, timeout_val);
283
284 if (mr != MACH_MSG_SUCCESS) {
285 ipc_kmsg_destroy(kmsg);
286 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
287 }
288
289 return mr;
290 }
291
292 #endif /* IKM_SUPPORT_LEGACY */
293
294 /*
295 * Routine: mach_msg_rpc_from_kernel
296 * Purpose:
297 * Send a message from the kernel and receive a reply.
298 * Uses ith_rpc_reply for the reply port.
299 *
300 * This is used by the client side of KernelUser interfaces
301 * to implement Routines.
302 * Conditions:
303 * Nothing locked.
304 * Returns:
305 * MACH_MSG_SUCCESS Sent the message.
306 * MACH_RCV_PORT_DIED The reply port was deallocated.
307 */
308
309 mach_msg_return_t mach_msg_rpc_from_kernel_body(mach_msg_header_t *msg,
310 mach_msg_size_t send_size, mach_msg_size_t rcv_size, boolean_t legacy);
311
312 #if IKM_SUPPORT_LEGACY
313
314 #undef mach_msg_rpc_from_kernel
315 mach_msg_return_t
316 mach_msg_rpc_from_kernel(
317 mach_msg_header_t *msg,
318 mach_msg_size_t send_size,
319 mach_msg_size_t rcv_size);
320
321 mach_msg_return_t
322 mach_msg_rpc_from_kernel(
323 mach_msg_header_t *msg,
324 mach_msg_size_t send_size,
325 mach_msg_size_t rcv_size)
326 {
327 return mach_msg_rpc_from_kernel_body(msg, send_size, rcv_size, TRUE);
328 }
329
330 #endif /* IKM_SUPPORT_LEGACY */
331
332 mach_msg_return_t
333 mach_msg_rpc_from_kernel_proper(
334 mach_msg_header_t *msg,
335 mach_msg_size_t send_size,
336 mach_msg_size_t rcv_size)
337 {
338 return mach_msg_rpc_from_kernel_body(msg, send_size, rcv_size, FALSE);
339 }
340
341 mach_msg_return_t
342 mach_msg_rpc_from_kernel_body(
343 mach_msg_header_t *msg,
344 mach_msg_size_t send_size,
345 mach_msg_size_t rcv_size,
346 #if !IKM_SUPPORT_LEGACY
347 __unused
348 #endif
349 boolean_t legacy)
350 {
351 thread_t self = current_thread();
352 ipc_port_t reply;
353 ipc_kmsg_t kmsg;
354 mach_port_seqno_t seqno;
355 mach_msg_return_t mr;
356
357 assert(msg->msgh_local_port == MACH_PORT_NULL);
358
359 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
360
361 mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg);
362 if (mr != MACH_MSG_SUCCESS) {
363 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
364 return mr;
365 }
366
367 reply = self->ith_rpc_reply;
368 if (reply == IP_NULL) {
369 reply = ipc_port_alloc_reply();
370 if ((reply == IP_NULL) ||
371 (self->ith_rpc_reply != IP_NULL))
372 panic("mach_msg_rpc_from_kernel");
373 self->ith_rpc_reply = reply;
374 }
375
376 /* insert send-once right for the reply port */
377 kmsg->ikm_header->msgh_local_port = reply;
378 kmsg->ikm_header->msgh_bits |=
379 MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE);
380
381 #if IKM_SUPPORT_LEGACY
382 if(legacy)
383 mr = ipc_kmsg_copyin_from_kernel_legacy(kmsg);
384 else
385 mr = ipc_kmsg_copyin_from_kernel(kmsg);
386 #else
387 mr = ipc_kmsg_copyin_from_kernel(kmsg);
388 #endif
389 if (mr != MACH_MSG_SUCCESS) {
390 ipc_kmsg_free(kmsg);
391 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
392 return mr;
393 }
394
395 /*
396 * respect the thread's SEND_IMPORTANCE option to force importance
397 * donation from the kernel-side of user threads
398 * (11938665 & 23925818)
399 */
400 mach_msg_option_t option = MACH_SEND_KERNEL_DEFAULT;
401 if (current_thread()->options & TH_OPT_SEND_IMPORTANCE)
402 option &= ~MACH_SEND_NOIMPORTANCE;
403
404 mr = ipc_kmsg_send(kmsg, option, MACH_MSG_TIMEOUT_NONE);
405 if (mr != MACH_MSG_SUCCESS) {
406 ipc_kmsg_destroy(kmsg);
407 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
408 return mr;
409 }
410
411 for (;;) {
412 ipc_mqueue_t mqueue;
413
414 assert(reply->ip_in_pset == 0);
415 assert(ip_active(reply));
416
417 /* JMM - why this check? */
418 if (!self->active && !self->inspection) {
419 ipc_port_dealloc_reply(reply);
420 self->ith_rpc_reply = IP_NULL;
421 return MACH_RCV_INTERRUPTED;
422 }
423
424 self->ith_continuation = (void (*)(mach_msg_return_t))0;
425
426 mqueue = &reply->ip_messages;
427 ipc_mqueue_receive(mqueue,
428 MACH_MSG_OPTION_NONE,
429 MACH_MSG_SIZE_MAX,
430 MACH_MSG_TIMEOUT_NONE,
431 THREAD_INTERRUPTIBLE);
432
433 mr = self->ith_state;
434 kmsg = self->ith_kmsg;
435 seqno = self->ith_seqno;
436
437 if (mr == MACH_MSG_SUCCESS)
438 {
439 break;
440 }
441
442 assert(mr == MACH_RCV_INTERRUPTED);
443
444 assert(reply == self->ith_rpc_reply);
445
446 if (self->ast & AST_APC) {
447 ipc_port_dealloc_reply(reply);
448 self->ith_rpc_reply = IP_NULL;
449 return(mr);
450 }
451 }
452
453 /*
454 * Check to see how much of the message/trailer can be received.
455 * We chose the maximum trailer that will fit, since we don't
456 * have options telling us which trailer elements the caller needed.
457 */
458 if (rcv_size >= kmsg->ikm_header->msgh_size) {
459 mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *)
460 ((vm_offset_t)kmsg->ikm_header + kmsg->ikm_header->msgh_size);
461
462 if (rcv_size >= kmsg->ikm_header->msgh_size + MAX_TRAILER_SIZE) {
463 /* Enough room for a maximum trailer */
464 trailer->msgh_trailer_size = MAX_TRAILER_SIZE;
465 }
466 else if (rcv_size < kmsg->ikm_header->msgh_size +
467 trailer->msgh_trailer_size) {
468 /* no room for even the basic (default) trailer */
469 trailer->msgh_trailer_size = 0;
470 }
471 assert(trailer->msgh_trailer_type == MACH_MSG_TRAILER_FORMAT_0);
472 rcv_size = kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size;
473 mr = MACH_MSG_SUCCESS;
474 } else {
475 mr = MACH_RCV_TOO_LARGE;
476 }
477
478
479 /*
480 * We want to preserve rights and memory in reply!
481 * We don't have to put them anywhere; just leave them
482 * as they are.
483 */
484 #if IKM_SUPPORT_LEGACY
485 if(legacy)
486 ipc_kmsg_copyout_to_kernel_legacy(kmsg, ipc_space_reply);
487 else
488 ipc_kmsg_copyout_to_kernel(kmsg, ipc_space_reply);
489 #else
490 ipc_kmsg_copyout_to_kernel(kmsg, ipc_space_reply);
491 #endif
492 ipc_kmsg_put_to_kernel(msg, kmsg, rcv_size);
493 return mr;
494 }
495
496
497 /************** These Calls are set up for kernel-loaded tasks/threads **************/
498
499 /*
500 * Routine: mach_msg_overwrite
501 * Purpose:
502 * Like mach_msg_overwrite_trap except that message buffers
503 * live in kernel space. Doesn't handle any options.
504 *
505 * This is used by in-kernel server threads to make
506 * kernel calls, to receive request messages, and
507 * to send reply messages.
508 * Conditions:
509 * Nothing locked.
510 * Returns:
511 */
512
513 mach_msg_return_t
514 mach_msg_overwrite(
515 mach_msg_header_t *msg,
516 mach_msg_option_t option,
517 mach_msg_size_t send_size,
518 mach_msg_size_t rcv_size,
519 mach_port_name_t rcv_name,
520 __unused mach_msg_timeout_t msg_timeout,
521 mach_msg_priority_t override,
522 __unused mach_msg_header_t *rcv_msg,
523 __unused mach_msg_size_t rcv_msg_size)
524 {
525 ipc_space_t space = current_space();
526 vm_map_t map = current_map();
527 ipc_kmsg_t kmsg;
528 mach_port_seqno_t seqno;
529 mach_msg_return_t mr;
530 mach_msg_trailer_size_t trailer_size;
531
532 if (option & MACH_SEND_MSG) {
533 mach_msg_size_t msg_and_trailer_size;
534 mach_msg_max_trailer_t *max_trailer;
535
536 if ((send_size < sizeof(mach_msg_header_t)) || (send_size & 3))
537 return MACH_SEND_MSG_TOO_SMALL;
538
539 if (send_size > MACH_MSG_SIZE_MAX - MAX_TRAILER_SIZE)
540 return MACH_SEND_TOO_LARGE;
541
542 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_START);
543
544 msg_and_trailer_size = send_size + MAX_TRAILER_SIZE;
545 kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
546
547 if (kmsg == IKM_NULL) {
548 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, MACH_SEND_NO_BUFFER);
549 return MACH_SEND_NO_BUFFER;
550 }
551
552 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE,
553 (uintptr_t)0, /* this should only be called from the kernel! */
554 VM_KERNEL_ADDRPERM((uintptr_t)kmsg),
555 0, 0,
556 0);
557 (void) memcpy((void *) kmsg->ikm_header, (const void *) msg, send_size);
558
559 kmsg->ikm_header->msgh_size = send_size;
560
561 /*
562 * Reserve for the trailer the largest space (MAX_TRAILER_SIZE)
563 * However, the internal size field of the trailer (msgh_trailer_size)
564 * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize
565 * the cases where no implicit data is requested.
566 */
567 max_trailer = (mach_msg_max_trailer_t *) ((vm_offset_t)kmsg->ikm_header + send_size);
568 max_trailer->msgh_sender = current_thread()->task->sec_token;
569 max_trailer->msgh_audit = current_thread()->task->audit_token;
570 max_trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
571 max_trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
572
573 mr = ipc_kmsg_copyin(kmsg, space, map, override, &option);
574
575 if (mr != MACH_MSG_SUCCESS) {
576 ipc_kmsg_free(kmsg);
577 KDBG(MACHDBG_CODE(DBG_MACH_IPC,MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr);
578 return mr;
579 }
580
581 do {
582 mr = ipc_kmsg_send(kmsg, MACH_MSG_OPTION_NONE, MACH_MSG_TIMEOUT_NONE);
583 } while (mr == MACH_SEND_INTERRUPTED);
584
585 assert(mr == MACH_MSG_SUCCESS);
586 }
587
588 if (option & MACH_RCV_MSG) {
589 thread_t self = current_thread();
590
591 do {
592 ipc_object_t object;
593 ipc_mqueue_t mqueue;
594
595 mr = ipc_mqueue_copyin(space, rcv_name,
596 &mqueue, &object);
597 if (mr != MACH_MSG_SUCCESS)
598 return mr;
599 /* hold ref for object */
600
601 self->ith_continuation = (void (*)(mach_msg_return_t))0;
602 ipc_mqueue_receive(mqueue,
603 MACH_MSG_OPTION_NONE,
604 MACH_MSG_SIZE_MAX,
605 MACH_MSG_TIMEOUT_NONE,
606 THREAD_ABORTSAFE);
607 mr = self->ith_state;
608 kmsg = self->ith_kmsg;
609 seqno = self->ith_seqno;
610
611 io_release(object);
612
613 } while (mr == MACH_RCV_INTERRUPTED);
614
615 if (mr != MACH_MSG_SUCCESS)
616 return mr;
617
618 trailer_size = ipc_kmsg_add_trailer(kmsg, space, option, current_thread(), seqno, TRUE,
619 kmsg->ikm_header->msgh_remote_port->ip_context);
620
621 if (rcv_size < (kmsg->ikm_header->msgh_size + trailer_size)) {
622 ipc_kmsg_copyout_dest(kmsg, space);
623 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg);
624 ipc_kmsg_free(kmsg);
625 return MACH_RCV_TOO_LARGE;
626 }
627
628 mr = ipc_kmsg_copyout(kmsg, space, map, MACH_MSG_BODY_NULL, option);
629 if (mr != MACH_MSG_SUCCESS) {
630 if ((mr &~ MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) {
631 ipc_kmsg_put_to_kernel(msg, kmsg,
632 kmsg->ikm_header->msgh_size + trailer_size);
633 } else {
634 ipc_kmsg_copyout_dest(kmsg, space);
635 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg);
636 ipc_kmsg_free(kmsg);
637 }
638
639 return mr;
640 }
641
642 (void) memcpy((void *) msg, (const void *) kmsg->ikm_header,
643 kmsg->ikm_header->msgh_size + trailer_size);
644 ipc_kmsg_free(kmsg);
645 }
646
647 return MACH_MSG_SUCCESS;
648 }
649
650 /*
651 * Routine: mig_get_reply_port
652 * Purpose:
653 * Called by client side interfaces living in the kernel
654 * to get a reply port.
655 */
656 mach_port_t
657 mig_get_reply_port(void)
658 {
659 return (MACH_PORT_NULL);
660 }
661
662 /*
663 * Routine: mig_dealloc_reply_port
664 * Purpose:
665 * Called by client side interfaces to get rid of a reply port.
666 */
667
668 void
669 mig_dealloc_reply_port(
670 __unused mach_port_t reply_port)
671 {
672 }
673
674 /*
675 * Routine: mig_put_reply_port
676 * Purpose:
677 * Called by client side interfaces after each RPC to
678 * let the client recycle the reply port if it wishes.
679 */
680 void
681 mig_put_reply_port(
682 __unused mach_port_t reply_port)
683 {
684 }
685
686 /*
687 * mig_strncpy.c - by Joshua Block
688 *
689 * mig_strncp -- Bounded string copy. Does what the library routine strncpy
690 * OUGHT to do: Copies the (null terminated) string in src into dest, a
691 * buffer of length len. Assures that the copy is still null terminated
692 * and doesn't overflow the buffer, truncating the copy if necessary.
693 *
694 * Parameters:
695 *
696 * dest - Pointer to destination buffer.
697 *
698 * src - Pointer to source string.
699 *
700 * len - Length of destination buffer.
701 */
702 int
703 mig_strncpy(
704 char *dest,
705 const char *src,
706 int len)
707 {
708 int i = 0;
709
710 if (len > 0)
711 if (dest != NULL) {
712 if (src != NULL)
713 for (i=1; i<len; i++)
714 if (! (*dest++ = *src++))
715 return i;
716 *dest = '\0';
717 }
718 return i;
719 }
720
721 /*
722 * mig_strncpy_zerofill -- Bounded string copy. Does what the
723 * library routine strncpy OUGHT to do: Copies the (null terminated)
724 * string in src into dest, a buffer of length len. Assures that
725 * the copy is still null terminated and doesn't overflow the buffer,
726 * truncating the copy if necessary. If the string in src is smaller
727 * than given length len, it will zero fill the remaining bytes in dest.
728 *
729 * Parameters:
730 *
731 * dest - Pointer to destination buffer.
732 *
733 * src - Pointer to source string.
734 *
735 * len - Length of destination buffer.
736 */
737 int
738 mig_strncpy_zerofill(
739 char *dest,
740 const char *src,
741 int len)
742 {
743 int i = 0;
744 boolean_t terminated = FALSE;
745 int retval = 0;
746
747 if (len <= 0 || dest == NULL) {
748 return 0;
749 }
750
751 if (src == NULL) {
752 terminated = TRUE;
753 }
754
755 for (i = 1; i < len; i++) {
756 if (!terminated) {
757 if (!(*dest++ = *src++)) {
758 retval = i;
759 terminated = TRUE;
760 }
761 } else {
762 *dest++ = '\0';
763 }
764 }
765
766 *dest = '\0';
767 if (!terminated) {
768 retval = i;
769 }
770
771 return retval;
772 }
773
774 char *
775 mig_user_allocate(
776 vm_size_t size)
777 {
778 return (char *)kalloc(size);
779 }
780
781 void
782 mig_user_deallocate(
783 char *data,
784 vm_size_t size)
785 {
786 kfree(data, size);
787 }
788
789 /*
790 * Routine: mig_object_init
791 * Purpose:
792 * Initialize the base class portion of a MIG object. We
793 * will lazy init the port, so just clear it for now.
794 */
795 kern_return_t
796 mig_object_init(
797 mig_object_t mig_object,
798 const IMIGObject *interface)
799 {
800 if (mig_object == MIG_OBJECT_NULL)
801 return KERN_INVALID_ARGUMENT;
802 mig_object->pVtbl = (const IMIGObjectVtbl *)interface;
803 mig_object->port = MACH_PORT_NULL;
804 return KERN_SUCCESS;
805 }
806
807 /*
808 * Routine: mig_object_destroy
809 * Purpose:
810 * The object is being freed. This call lets us clean
811 * up any state we have have built up over the object's
812 * lifetime.
813 * Conditions:
814 * Since notifications and the port hold references on
815 * on the object, neither can exist when this is called.
816 * This is a good place to assert() that condition.
817 */
818 void
819 mig_object_destroy(
820 __assert_only mig_object_t mig_object)
821 {
822 assert(mig_object->port == MACH_PORT_NULL);
823 return;
824 }
825
826 /*
827 * Routine: mig_object_reference
828 * Purpose:
829 * Pure virtual helper to invoke the MIG object's AddRef
830 * method.
831 * Conditions:
832 * MIG object port may be locked.
833 */
834 void
835 mig_object_reference(
836 mig_object_t mig_object)
837 {
838 assert(mig_object != MIG_OBJECT_NULL);
839 mig_object->pVtbl->AddRef((IMIGObject *)mig_object);
840 }
841
842 /*
843 * Routine: mig_object_deallocate
844 * Purpose:
845 * Pure virtual helper to invoke the MIG object's Release
846 * method.
847 * Conditions:
848 * Nothing locked.
849 */
850 void
851 mig_object_deallocate(
852 mig_object_t mig_object)
853 {
854 assert(mig_object != MIG_OBJECT_NULL);
855 mig_object->pVtbl->Release((IMIGObject *)mig_object);
856 }
857
858 /*
859 * Routine: convert_mig_object_to_port [interface]
860 * Purpose:
861 * Base implementation of MIG outtrans routine to convert from
862 * a mig object reference to a new send right on the object's
863 * port. The object reference is consumed.
864 * Returns:
865 * IP_NULL - Null MIG object supplied
866 * Otherwise, a newly made send right for the port
867 * Conditions:
868 * Nothing locked.
869 */
870 ipc_port_t
871 convert_mig_object_to_port(
872 mig_object_t mig_object)
873 {
874 ipc_port_t port;
875 boolean_t deallocate = TRUE;
876
877 if (mig_object == MIG_OBJECT_NULL)
878 return IP_NULL;
879
880 port = mig_object->port;
881 while ((port == IP_NULL) ||
882 ((port = ipc_port_make_send(port)) == IP_NULL)) {
883 ipc_port_t previous;
884
885 /*
886 * Either the port was never set up, or it was just
887 * deallocated out from under us by the no-senders
888 * processing. In either case, we must:
889 * Attempt to make one
890 * Arrange for no senders
891 * Try to atomically register it with the object
892 * Destroy it if we are raced.
893 */
894 port = ipc_port_alloc_kernel();
895 ip_lock(port);
896 ipc_kobject_set_atomically(port,
897 (ipc_kobject_t) mig_object,
898 IKOT_MIG);
899
900 /* make a sonce right for the notification */
901 port->ip_sorights++;
902 ip_reference(port);
903
904 ipc_port_nsrequest(port, 1, port, &previous);
905 /* port unlocked */
906
907 assert(previous == IP_NULL);
908
909 if (OSCompareAndSwapPtr((void *)IP_NULL, (void *)port,
910 (void * volatile *)&mig_object->port)) {
911 deallocate = FALSE;
912 } else {
913 ipc_port_dealloc_kernel(port);
914 port = mig_object->port;
915 }
916 }
917
918 if (deallocate)
919 mig_object->pVtbl->Release((IMIGObject *)mig_object);
920
921 return (port);
922 }
923
924
925 /*
926 * Routine: convert_port_to_mig_object [interface]
927 * Purpose:
928 * Base implementation of MIG intrans routine to convert from
929 * an incoming port reference to a new reference on the
930 * underlying object. A new reference must be created, because
931 * the port's reference could go away asynchronously.
932 * Returns:
933 * NULL - Not an active MIG object port or iid not supported
934 * Otherwise, a reference to the underlying MIG interface
935 * Conditions:
936 * Nothing locked.
937 */
938 mig_object_t
939 convert_port_to_mig_object(
940 ipc_port_t port,
941 const MIGIID *iid)
942 {
943 mig_object_t mig_object;
944 void *ppv;
945
946 if (!IP_VALID(port))
947 return NULL;
948
949 ip_lock(port);
950 if (!ip_active(port) || (ip_kotype(port) != IKOT_MIG)) {
951 ip_unlock(port);
952 return NULL;
953 }
954
955 /*
956 * Our port points to some MIG object interface. Now
957 * query it to get a reference to the desired interface.
958 */
959 ppv = NULL;
960 mig_object = (mig_object_t)port->ip_kobject;
961 mig_object->pVtbl->QueryInterface((IMIGObject *)mig_object, iid, &ppv);
962 ip_unlock(port);
963 return (mig_object_t)ppv;
964 }
965
966 /*
967 * Routine: mig_object_no_senders [interface]
968 * Purpose:
969 * Base implementation of a no-senders notification handler
970 * for MIG objects. If there truly are no more senders, must
971 * destroy the port and drop its reference on the object.
972 * Returns:
973 * TRUE - port deallocate and reference dropped
974 * FALSE - more senders arrived, re-registered for notification
975 * Conditions:
976 * Nothing locked.
977 */
978
979 boolean_t
980 mig_object_no_senders(
981 ipc_port_t port,
982 mach_port_mscount_t mscount)
983 {
984 mig_object_t mig_object;
985
986 ip_lock(port);
987 if (port->ip_mscount > mscount) {
988 ipc_port_t previous;
989
990 /*
991 * Somebody created new send rights while the
992 * notification was in-flight. Just create a
993 * new send-once right and re-register with
994 * the new (higher) mscount threshold.
995 */
996 /* make a sonce right for the notification */
997 port->ip_sorights++;
998 ip_reference(port);
999 ipc_port_nsrequest(port, mscount, port, &previous);
1000 /* port unlocked */
1001
1002 assert(previous == IP_NULL);
1003 return (FALSE);
1004 }
1005
1006 /*
1007 * Clear the port pointer while we have it locked.
1008 */
1009 mig_object = (mig_object_t)port->ip_kobject;
1010 mig_object->port = IP_NULL;
1011
1012 /*
1013 * Bring the sequence number and mscount in
1014 * line with ipc_port_destroy assertion.
1015 */
1016 port->ip_mscount = 0;
1017 port->ip_messages.imq_seqno = 0;
1018 ipc_port_destroy(port); /* releases lock */
1019
1020 /*
1021 * Release the port's reference on the object.
1022 */
1023 mig_object->pVtbl->Release((IMIGObject *)mig_object);
1024 return (TRUE);
1025 }
1026
1027 /*
1028 * Kernel implementation of the notification chain for MIG object
1029 * is kept separate from the actual objects, since there are expected
1030 * to be much fewer of them than actual objects.
1031 *
1032 * The implementation of this part of MIG objects is coming
1033 * "Real Soon Now"(TM).
1034 */