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