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