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