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