]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
6601e61a | 4 | * @APPLE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
6601e61a A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
8f6c56a5 | 11 | * |
6601e61a A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
6601e61a A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
8f6c56a5 | 19 | * |
6601e61a | 20 | * @APPLE_LICENSE_HEADER_END@ |
1c79356b A |
21 | */ |
22 | /* | |
23 | * @OSF_COPYRIGHT@ | |
24 | */ | |
25 | /* | |
26 | * Mach Operating System | |
27 | * Copyright (c) 1991,1990 Carnegie Mellon University | |
28 | * All Rights Reserved. | |
29 | * | |
30 | * Permission to use, copy, modify and distribute this software and its | |
31 | * documentation is hereby granted, provided that both the copyright | |
32 | * notice and this permission notice appear in all copies of the | |
33 | * software, derivative works or modified versions, and any portions | |
34 | * thereof, and that both notices appear in supporting documentation. | |
35 | * | |
36 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
37 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
38 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
39 | * | |
40 | * Carnegie Mellon requests users of this software to return to | |
41 | * | |
42 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
43 | * School of Computer Science | |
44 | * Carnegie Mellon University | |
45 | * Pittsburgh PA 15213-3890 | |
46 | * | |
47 | * any improvements or extensions that they make and grant Carnegie Mellon | |
48 | * the rights to redistribute these changes. | |
49 | */ | |
50 | /* | |
51 | */ | |
52 | ||
1c79356b A |
53 | #include <mach/boolean.h> |
54 | #include <mach/port.h> | |
0b4e3aa0 | 55 | #include <mach/mig.h> |
1c79356b A |
56 | #include <mach/mig_errors.h> |
57 | #include <mach/mach_types.h> | |
58 | #include <mach/mach_traps.h> | |
0b4e3aa0 | 59 | |
1c79356b A |
60 | #include <kern/ipc_tt.h> |
61 | #include <kern/ipc_mig.h> | |
91447636 | 62 | #include <kern/kalloc.h> |
1c79356b A |
63 | #include <kern/task.h> |
64 | #include <kern/thread.h> | |
65 | #include <kern/ipc_kobject.h> | |
66 | #include <kern/misc_protos.h> | |
91447636 | 67 | |
1c79356b A |
68 | #include <ipc/port.h> |
69 | #include <ipc/ipc_kmsg.h> | |
70 | #include <ipc/ipc_entry.h> | |
71 | #include <ipc/ipc_object.h> | |
72 | #include <ipc/ipc_mqueue.h> | |
73 | #include <ipc/ipc_space.h> | |
74 | #include <ipc/ipc_port.h> | |
75 | #include <ipc/ipc_pset.h> | |
0b4e3aa0 | 76 | #include <vm/vm_map.h> |
1c79356b A |
77 | |
78 | /* | |
79 | * Routine: mach_msg_send_from_kernel | |
80 | * Purpose: | |
81 | * Send a message from the kernel. | |
82 | * | |
83 | * This is used by the client side of KernelUser interfaces | |
84 | * to implement SimpleRoutines. Currently, this includes | |
85 | * memory_object messages. | |
86 | * Conditions: | |
87 | * Nothing locked. | |
88 | * Returns: | |
89 | * MACH_MSG_SUCCESS Sent the message. | |
90 | * MACH_MSG_SEND_NO_BUFFER Destination port had inuse fixed bufer | |
91 | * MACH_SEND_INVALID_DEST Bad destination port. | |
92 | */ | |
93 | ||
94 | mach_msg_return_t | |
95 | mach_msg_send_from_kernel( | |
96 | mach_msg_header_t *msg, | |
97 | mach_msg_size_t send_size) | |
98 | { | |
99 | ipc_kmsg_t kmsg; | |
100 | mach_msg_return_t mr; | |
101 | ||
102 | if (!MACH_PORT_VALID((mach_port_name_t)msg->msgh_remote_port)) | |
103 | return MACH_SEND_INVALID_DEST; | |
104 | ||
105 | mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg); | |
106 | if (mr != MACH_MSG_SUCCESS) | |
107 | return mr; | |
108 | ||
109 | ipc_kmsg_copyin_from_kernel(kmsg); | |
110 | ipc_kmsg_send_always(kmsg); | |
111 | ||
112 | return MACH_MSG_SUCCESS; | |
113 | } | |
114 | ||
115 | /* | |
116 | * Routine: mach_msg_rpc_from_kernel | |
117 | * Purpose: | |
118 | * Send a message from the kernel and receive a reply. | |
119 | * Uses ith_rpc_reply for the reply port. | |
120 | * | |
121 | * This is used by the client side of KernelUser interfaces | |
122 | * to implement Routines. | |
123 | * Conditions: | |
124 | * Nothing locked. | |
125 | * Returns: | |
126 | * MACH_MSG_SUCCESS Sent the message. | |
127 | * MACH_RCV_PORT_DIED The reply port was deallocated. | |
128 | */ | |
129 | ||
130 | mach_msg_return_t | |
131 | mach_msg_rpc_from_kernel( | |
132 | mach_msg_header_t *msg, | |
133 | mach_msg_size_t send_size, | |
134 | mach_msg_size_t rcv_size) | |
135 | { | |
136 | thread_t self = current_thread(); | |
137 | ipc_port_t reply; | |
138 | ipc_kmsg_t kmsg; | |
139 | mach_port_seqno_t seqno; | |
140 | mach_msg_return_t mr; | |
141 | ||
142 | assert(MACH_PORT_VALID((mach_port_name_t)msg->msgh_remote_port)); | |
143 | assert(msg->msgh_local_port == MACH_PORT_NULL); | |
144 | ||
145 | mr = ipc_kmsg_get_from_kernel(msg, send_size, &kmsg); | |
146 | if (mr != MACH_MSG_SUCCESS) | |
147 | return mr; | |
148 | ||
1c79356b A |
149 | reply = self->ith_rpc_reply; |
150 | if (reply == IP_NULL) { | |
1c79356b | 151 | reply = ipc_port_alloc_reply(); |
1c79356b A |
152 | if ((reply == IP_NULL) || |
153 | (self->ith_rpc_reply != IP_NULL)) | |
154 | panic("mach_msg_rpc_from_kernel"); | |
155 | self->ith_rpc_reply = reply; | |
156 | } | |
157 | ||
158 | /* insert send-once right for the reply port */ | |
91447636 A |
159 | kmsg->ikm_header->msgh_local_port = reply; |
160 | kmsg->ikm_header->msgh_bits |= | |
1c79356b A |
161 | MACH_MSGH_BITS(0, MACH_MSG_TYPE_MAKE_SEND_ONCE); |
162 | ||
163 | ipc_port_reference(reply); | |
1c79356b A |
164 | |
165 | ipc_kmsg_copyin_from_kernel(kmsg); | |
166 | ||
167 | ipc_kmsg_send_always(kmsg); | |
168 | ||
169 | for (;;) { | |
170 | ipc_mqueue_t mqueue; | |
171 | ||
172 | ip_lock(reply); | |
173 | if ( !ip_active(reply)) { | |
174 | ip_unlock(reply); | |
175 | ipc_port_release(reply); | |
176 | return MACH_RCV_PORT_DIED; | |
177 | } | |
91447636 | 178 | if (!self->active) { |
1c79356b A |
179 | ip_unlock(reply); |
180 | ipc_port_release(reply); | |
181 | return MACH_RCV_INTERRUPTED; | |
182 | } | |
183 | ||
184 | assert(reply->ip_pset_count == 0); | |
185 | mqueue = &reply->ip_messages; | |
186 | ip_unlock(reply); | |
187 | ||
188 | self->ith_continuation = (void (*)(mach_msg_return_t))0; | |
189 | ||
190 | ipc_mqueue_receive(mqueue, | |
191 | MACH_MSG_OPTION_NONE, | |
192 | MACH_MSG_SIZE_MAX, | |
193 | MACH_MSG_TIMEOUT_NONE, | |
194 | THREAD_INTERRUPTIBLE); | |
195 | ||
196 | mr = self->ith_state; | |
197 | kmsg = self->ith_kmsg; | |
198 | seqno = self->ith_seqno; | |
199 | ||
200 | if (mr == MACH_MSG_SUCCESS) | |
201 | { | |
202 | break; | |
203 | } | |
204 | ||
205 | assert(mr == MACH_RCV_INTERRUPTED); | |
206 | ||
91447636 | 207 | if (self->handlers) { |
1c79356b A |
208 | ipc_port_release(reply); |
209 | return(mr); | |
210 | } | |
211 | } | |
212 | ipc_port_release(reply); | |
213 | ||
214 | /* | |
215 | * XXXXX Set manually for now ... | |
216 | * No, why even bother, since the effort is wasted? | |
217 | * | |
218 | { mach_msg_format_0_trailer_t *trailer = (mach_msg_format_0_trailer_t *) | |
219 | ((vm_offset_t)&kmsg->ikm_header + kmsg->ikm_header.msgh_size); | |
220 | trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0; | |
221 | trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE; | |
222 | } | |
223 | *****/ | |
224 | ||
91447636 | 225 | if (rcv_size < kmsg->ikm_header->msgh_size) { |
1c79356b | 226 | ipc_kmsg_copyout_dest(kmsg, ipc_space_reply); |
91447636 | 227 | ipc_kmsg_put_to_kernel(msg, kmsg, kmsg->ikm_header->msgh_size); |
1c79356b A |
228 | return MACH_RCV_TOO_LARGE; |
229 | } | |
230 | ||
231 | /* | |
232 | * We want to preserve rights and memory in reply! | |
233 | * We don't have to put them anywhere; just leave them | |
234 | * as they are. | |
235 | */ | |
236 | ||
237 | ipc_kmsg_copyout_to_kernel(kmsg, ipc_space_reply); | |
91447636 | 238 | ipc_kmsg_put_to_kernel(msg, kmsg, kmsg->ikm_header->msgh_size); |
1c79356b A |
239 | return MACH_MSG_SUCCESS; |
240 | } | |
241 | ||
242 | ||
91447636 | 243 | /************** These Calls are set up for kernel-loaded tasks/threads **************/ |
1c79356b A |
244 | |
245 | /* | |
91447636 | 246 | * Routine: mach_msg_overwrite |
1c79356b A |
247 | * Purpose: |
248 | * Like mach_msg_overwrite_trap except that message buffers | |
249 | * live in kernel space. Doesn't handle any options. | |
250 | * | |
251 | * This is used by in-kernel server threads to make | |
252 | * kernel calls, to receive request messages, and | |
253 | * to send reply messages. | |
254 | * Conditions: | |
255 | * Nothing locked. | |
256 | * Returns: | |
257 | */ | |
258 | ||
259 | mach_msg_return_t | |
260 | mach_msg_overwrite( | |
91447636 A |
261 | mach_msg_header_t *msg, |
262 | mach_msg_option_t option, | |
1c79356b A |
263 | mach_msg_size_t send_size, |
264 | mach_msg_size_t rcv_size, | |
91447636 A |
265 | mach_port_name_t rcv_name, |
266 | __unused mach_msg_timeout_t msg_timeout, | |
267 | __unused mach_port_name_t notify, | |
268 | __unused mach_msg_header_t *rcv_msg, | |
269 | __unused mach_msg_size_t rcv_msg_size) | |
1c79356b A |
270 | { |
271 | ipc_space_t space = current_space(); | |
272 | vm_map_t map = current_map(); | |
273 | ipc_kmsg_t kmsg; | |
274 | mach_port_seqno_t seqno; | |
275 | mach_msg_return_t mr; | |
276 | mach_msg_format_0_trailer_t *trailer; | |
277 | ||
278 | if (option & MACH_SEND_MSG) { | |
91447636 A |
279 | mach_msg_size_t msg_and_trailer_size; |
280 | mach_msg_max_trailer_t *max_trailer; | |
281 | ||
282 | if ((send_size < sizeof(mach_msg_header_t)) || (send_size & 3)) | |
283 | return MACH_SEND_MSG_TOO_SMALL; | |
284 | ||
8ad349bb A |
285 | if (send_size > MACH_MSG_SIZE_MAX - MAX_TRAILER_SIZE) |
286 | return MACH_SEND_TOO_LARGE; | |
91447636 | 287 | |
8ad349bb | 288 | msg_and_trailer_size = send_size + MAX_TRAILER_SIZE; |
91447636 A |
289 | kmsg = ipc_kmsg_alloc(msg_and_trailer_size); |
290 | ||
291 | if (kmsg == IKM_NULL) | |
292 | return MACH_SEND_NO_BUFFER; | |
1c79356b | 293 | |
91447636 A |
294 | (void) memcpy((void *) kmsg->ikm_header, (const void *) msg, send_size); |
295 | ||
296 | kmsg->ikm_header->msgh_size = send_size; | |
297 | ||
298 | /* | |
299 | * Reserve for the trailer the largest space (MAX_TRAILER_SIZE) | |
300 | * However, the internal size field of the trailer (msgh_trailer_size) | |
301 | * is initialized to the minimum (sizeof(mach_msg_trailer_t)), to optimize | |
302 | * the cases where no implicit data is requested. | |
303 | */ | |
304 | max_trailer = (mach_msg_max_trailer_t *) ((vm_offset_t)kmsg->ikm_header + send_size); | |
305 | max_trailer->msgh_sender = current_thread()->task->sec_token; | |
306 | max_trailer->msgh_audit = current_thread()->task->audit_token; | |
307 | max_trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0; | |
308 | max_trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE; | |
309 | ||
1c79356b A |
310 | mr = ipc_kmsg_copyin(kmsg, space, map, MACH_PORT_NULL); |
311 | if (mr != MACH_MSG_SUCCESS) { | |
312 | ipc_kmsg_free(kmsg); | |
313 | return mr; | |
314 | } | |
315 | ||
316 | do | |
317 | mr = ipc_kmsg_send(kmsg, MACH_MSG_OPTION_NONE, | |
318 | MACH_MSG_TIMEOUT_NONE); | |
319 | while (mr == MACH_SEND_INTERRUPTED); | |
320 | assert(mr == MACH_MSG_SUCCESS); | |
321 | } | |
322 | ||
323 | if (option & MACH_RCV_MSG) { | |
324 | thread_t self = current_thread(); | |
325 | ||
326 | do { | |
327 | ipc_object_t object; | |
328 | ipc_mqueue_t mqueue; | |
329 | ||
330 | mr = ipc_mqueue_copyin(space, rcv_name, | |
331 | &mqueue, &object); | |
332 | if (mr != MACH_MSG_SUCCESS) | |
333 | return mr; | |
334 | /* hold ref for object */ | |
335 | ||
336 | self->ith_continuation = (void (*)(mach_msg_return_t))0; | |
337 | ipc_mqueue_receive(mqueue, | |
338 | MACH_MSG_OPTION_NONE, | |
339 | MACH_MSG_SIZE_MAX, | |
340 | MACH_MSG_TIMEOUT_NONE, | |
341 | THREAD_ABORTSAFE); | |
342 | mr = self->ith_state; | |
343 | kmsg = self->ith_kmsg; | |
344 | seqno = self->ith_seqno; | |
345 | ||
346 | ipc_object_release(object); | |
347 | ||
348 | } while (mr == MACH_RCV_INTERRUPTED); | |
349 | if (mr != MACH_MSG_SUCCESS) | |
350 | return mr; | |
351 | ||
352 | trailer = (mach_msg_format_0_trailer_t *) | |
91447636 | 353 | ((vm_offset_t)kmsg->ikm_header + kmsg->ikm_header->msgh_size); |
1c79356b A |
354 | if (option & MACH_RCV_TRAILER_MASK) { |
355 | trailer->msgh_seqno = seqno; | |
356 | trailer->msgh_trailer_size = REQUESTED_TRAILER_SIZE(option); | |
357 | } | |
358 | ||
91447636 | 359 | if (rcv_size < (kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size)) { |
1c79356b | 360 | ipc_kmsg_copyout_dest(kmsg, space); |
91447636 A |
361 | (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg); |
362 | ipc_kmsg_free(kmsg); | |
1c79356b A |
363 | return MACH_RCV_TOO_LARGE; |
364 | } | |
365 | ||
366 | mr = ipc_kmsg_copyout(kmsg, space, map, MACH_PORT_NULL, | |
367 | MACH_MSG_BODY_NULL); | |
368 | if (mr != MACH_MSG_SUCCESS) { | |
369 | if ((mr &~ MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) { | |
370 | ipc_kmsg_put_to_kernel(msg, kmsg, | |
91447636 | 371 | kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size); |
1c79356b A |
372 | } else { |
373 | ipc_kmsg_copyout_dest(kmsg, space); | |
91447636 A |
374 | (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, sizeof *msg); |
375 | ipc_kmsg_free(kmsg); | |
1c79356b A |
376 | } |
377 | ||
378 | return mr; | |
379 | } | |
380 | ||
91447636 A |
381 | (void) memcpy((void *) msg, (const void *) kmsg->ikm_header, |
382 | kmsg->ikm_header->msgh_size + trailer->msgh_trailer_size); | |
383 | ipc_kmsg_free(kmsg); | |
1c79356b A |
384 | } |
385 | ||
386 | return MACH_MSG_SUCCESS; | |
387 | } | |
388 | ||
389 | /* | |
390 | * Routine: mig_get_reply_port | |
391 | * Purpose: | |
392 | * Called by client side interfaces living in the kernel | |
91447636 | 393 | * to get a reply port. |
1c79356b A |
394 | */ |
395 | mach_port_t | |
396 | mig_get_reply_port(void) | |
397 | { | |
91447636 | 398 | return (MACH_PORT_NULL); |
1c79356b A |
399 | } |
400 | ||
401 | /* | |
402 | * Routine: mig_dealloc_reply_port | |
403 | * Purpose: | |
404 | * Called by client side interfaces to get rid of a reply port. | |
1c79356b A |
405 | */ |
406 | ||
407 | void | |
408 | mig_dealloc_reply_port( | |
91447636 | 409 | __unused mach_port_t reply_port) |
1c79356b A |
410 | { |
411 | panic("mig_dealloc_reply_port"); | |
412 | } | |
413 | ||
414 | /* | |
415 | * Routine: mig_put_reply_port | |
416 | * Purpose: | |
417 | * Called by client side interfaces after each RPC to | |
418 | * let the client recycle the reply port if it wishes. | |
419 | */ | |
420 | void | |
421 | mig_put_reply_port( | |
91447636 | 422 | __unused mach_port_t reply_port) |
1c79356b A |
423 | { |
424 | } | |
425 | ||
426 | /* | |
427 | * mig_strncpy.c - by Joshua Block | |
428 | * | |
429 | * mig_strncp -- Bounded string copy. Does what the library routine strncpy | |
430 | * OUGHT to do: Copies the (null terminated) string in src into dest, a | |
431 | * buffer of length len. Assures that the copy is still null terminated | |
432 | * and doesn't overflow the buffer, truncating the copy if necessary. | |
433 | * | |
434 | * Parameters: | |
435 | * | |
436 | * dest - Pointer to destination buffer. | |
437 | * | |
438 | * src - Pointer to source string. | |
439 | * | |
440 | * len - Length of destination buffer. | |
441 | */ | |
442 | int | |
443 | mig_strncpy( | |
9bccf70c A |
444 | char *dest, |
445 | const char *src, | |
446 | int len) | |
1c79356b A |
447 | { |
448 | int i = 0; | |
449 | ||
450 | if (len > 0) | |
451 | if (dest != NULL) { | |
452 | if (src != NULL) | |
453 | for (i=1; i<len; i++) | |
454 | if (! (*dest++ = *src++)) | |
455 | return i; | |
456 | *dest = '\0'; | |
457 | } | |
458 | return i; | |
459 | } | |
460 | ||
461 | char * | |
462 | mig_user_allocate( | |
463 | vm_size_t size) | |
464 | { | |
465 | return (char *)kalloc(size); | |
466 | } | |
467 | ||
468 | void | |
469 | mig_user_deallocate( | |
470 | char *data, | |
471 | vm_size_t size) | |
472 | { | |
91447636 | 473 | kfree(data, size); |
1c79356b A |
474 | } |
475 | ||
0b4e3aa0 A |
476 | /* |
477 | * Routine: mig_object_init | |
478 | * Purpose: | |
479 | * Initialize the base class portion of a MIG object. We | |
480 | * will lazy init the port, so just clear it for now. | |
481 | */ | |
482 | kern_return_t | |
483 | mig_object_init( | |
484 | mig_object_t mig_object, | |
485 | const IMIGObject *interface) | |
486 | { | |
91447636 A |
487 | if (mig_object == MIG_OBJECT_NULL) |
488 | return KERN_INVALID_ARGUMENT; | |
489 | mig_object->pVtbl = (const IMIGObjectVtbl *)interface; | |
0b4e3aa0 | 490 | mig_object->port = MACH_PORT_NULL; |
91447636 | 491 | return KERN_SUCCESS; |
0b4e3aa0 A |
492 | } |
493 | ||
494 | /* | |
495 | * Routine: mig_object_destroy | |
496 | * Purpose: | |
497 | * The object is being freed. This call lets us clean | |
498 | * up any state we have have built up over the object's | |
499 | * lifetime. | |
500 | * Conditions: | |
501 | * Since notifications and the port hold references on | |
502 | * on the object, neither can exist when this is called. | |
503 | * This is a good place to assert() that condition. | |
504 | */ | |
505 | void | |
506 | mig_object_destroy( | |
91447636 | 507 | __assert_only mig_object_t mig_object) |
0b4e3aa0 A |
508 | { |
509 | assert(mig_object->port == MACH_PORT_NULL); | |
510 | return; | |
511 | } | |
512 | ||
513 | /* | |
514 | * Routine: mig_object_reference | |
515 | * Purpose: | |
516 | * Pure virtual helper to invoke the MIG object's AddRef | |
517 | * method. | |
518 | * Conditions: | |
519 | * MIG object port may be locked. | |
520 | */ | |
521 | void | |
522 | mig_object_reference( | |
523 | mig_object_t mig_object) | |
524 | { | |
525 | assert(mig_object != MIG_OBJECT_NULL); | |
526 | mig_object->pVtbl->AddRef((IMIGObject *)mig_object); | |
527 | } | |
528 | ||
529 | /* | |
530 | * Routine: mig_object_deallocate | |
531 | * Purpose: | |
532 | * Pure virtual helper to invoke the MIG object's Release | |
533 | * method. | |
534 | * Conditions: | |
535 | * Nothing locked. | |
536 | */ | |
537 | void | |
538 | mig_object_deallocate( | |
539 | mig_object_t mig_object) | |
540 | { | |
541 | assert(mig_object != MIG_OBJECT_NULL); | |
542 | mig_object->pVtbl->Release((IMIGObject *)mig_object); | |
543 | } | |
544 | ||
545 | /* | |
546 | * Routine: convert_mig_object_to_port [interface] | |
547 | * Purpose: | |
548 | * Base implementation of MIG outtrans routine to convert from | |
549 | * a mig object reference to a new send right on the object's | |
550 | * port. The object reference is consumed. | |
551 | * Returns: | |
552 | * IP_NULL - Null MIG object supplied | |
553 | * Otherwise, a newly made send right for the port | |
554 | * Conditions: | |
555 | * Nothing locked. | |
556 | */ | |
557 | ipc_port_t | |
558 | convert_mig_object_to_port( | |
559 | mig_object_t mig_object) | |
560 | { | |
561 | ipc_port_t port; | |
562 | boolean_t deallocate = TRUE; | |
563 | ||
564 | if (mig_object == MIG_OBJECT_NULL) | |
565 | return IP_NULL; | |
566 | ||
567 | port = mig_object->port; | |
568 | while ((port == IP_NULL) || | |
569 | ((port = ipc_port_make_send(port)) == IP_NULL)) { | |
570 | ipc_port_t previous; | |
571 | ||
572 | /* | |
573 | * Either the port was never set up, or it was just | |
574 | * deallocated out from under us by the no-senders | |
575 | * processing. In either case, we must: | |
576 | * Attempt to make one | |
577 | * Arrange for no senders | |
578 | * Try to atomically register it with the object | |
579 | * Destroy it if we are raced. | |
580 | */ | |
581 | port = ipc_port_alloc_kernel(); | |
582 | ip_lock(port); | |
583 | ipc_kobject_set_atomically(port, | |
584 | (ipc_kobject_t) mig_object, | |
585 | IKOT_MIG); | |
586 | ||
587 | /* make a sonce right for the notification */ | |
588 | port->ip_sorights++; | |
589 | ip_reference(port); | |
590 | ||
591 | ipc_port_nsrequest(port, 1, port, &previous); | |
592 | /* port unlocked */ | |
593 | ||
594 | assert(previous == IP_NULL); | |
595 | ||
9bccf70c A |
596 | if (hw_compare_and_store((uint32_t)IP_NULL, (uint32_t)port, |
597 | (uint32_t *)&mig_object->port)) { | |
0b4e3aa0 A |
598 | deallocate = FALSE; |
599 | } else { | |
600 | ipc_port_dealloc_kernel(port); | |
601 | port = mig_object->port; | |
602 | } | |
603 | } | |
604 | ||
605 | if (deallocate) | |
606 | mig_object->pVtbl->Release((IMIGObject *)mig_object); | |
607 | ||
608 | return (port); | |
609 | } | |
610 | ||
611 | ||
612 | /* | |
613 | * Routine: convert_port_to_mig_object [interface] | |
614 | * Purpose: | |
615 | * Base implementation of MIG intrans routine to convert from | |
616 | * an incoming port reference to a new reference on the | |
617 | * underlying object. A new reference must be created, because | |
618 | * the port's reference could go away asynchronously. | |
619 | * Returns: | |
620 | * NULL - Not an active MIG object port or iid not supported | |
621 | * Otherwise, a reference to the underlying MIG interface | |
622 | * Conditions: | |
623 | * Nothing locked. | |
624 | */ | |
625 | mig_object_t | |
626 | convert_port_to_mig_object( | |
627 | ipc_port_t port, | |
628 | const MIGIID *iid) | |
629 | { | |
630 | mig_object_t mig_object; | |
631 | void *ppv; | |
632 | ||
633 | if (!IP_VALID(port)) | |
634 | return NULL; | |
635 | ||
636 | ip_lock(port); | |
637 | if (!ip_active(port) || (ip_kotype(port) != IKOT_MIG)) { | |
638 | ip_unlock(port); | |
639 | return NULL; | |
640 | } | |
641 | ||
642 | /* | |
643 | * Our port points to some MIG object interface. Now | |
644 | * query it to get a reference to the desired interface. | |
645 | */ | |
646 | ppv = NULL; | |
647 | mig_object = (mig_object_t)port->ip_kobject; | |
648 | mig_object->pVtbl->QueryInterface((IMIGObject *)mig_object, iid, &ppv); | |
649 | ip_unlock(port); | |
650 | return (mig_object_t)ppv; | |
651 | } | |
652 | ||
653 | /* | |
654 | * Routine: mig_object_no_senders [interface] | |
655 | * Purpose: | |
656 | * Base implementation of a no-senders notification handler | |
657 | * for MIG objects. If there truly are no more senders, must | |
658 | * destroy the port and drop its reference on the object. | |
659 | * Returns: | |
660 | * TRUE - port deallocate and reference dropped | |
661 | * FALSE - more senders arrived, re-registered for notification | |
662 | * Conditions: | |
663 | * Nothing locked. | |
664 | */ | |
665 | ||
666 | boolean_t | |
667 | mig_object_no_senders( | |
668 | ipc_port_t port, | |
669 | mach_port_mscount_t mscount) | |
670 | { | |
671 | mig_object_t mig_object; | |
672 | ||
673 | ip_lock(port); | |
674 | if (port->ip_mscount > mscount) { | |
675 | ipc_port_t previous; | |
676 | ||
677 | /* | |
678 | * Somebody created new send rights while the | |
679 | * notification was in-flight. Just create a | |
680 | * new send-once right and re-register with | |
681 | * the new (higher) mscount threshold. | |
682 | */ | |
683 | /* make a sonce right for the notification */ | |
684 | port->ip_sorights++; | |
685 | ip_reference(port); | |
686 | ipc_port_nsrequest(port, mscount, port, &previous); | |
687 | /* port unlocked */ | |
688 | ||
689 | assert(previous == IP_NULL); | |
690 | return (FALSE); | |
691 | } | |
692 | ||
693 | /* | |
694 | * Clear the port pointer while we have it locked. | |
695 | */ | |
696 | mig_object = (mig_object_t)port->ip_kobject; | |
697 | mig_object->port = IP_NULL; | |
698 | ||
699 | /* | |
700 | * Bring the sequence number and mscount in | |
701 | * line with ipc_port_destroy assertion. | |
702 | */ | |
703 | port->ip_mscount = 0; | |
704 | port->ip_messages.imq_seqno = 0; | |
705 | ipc_port_destroy(port); /* releases lock */ | |
706 | ||
707 | /* | |
708 | * Release the port's reference on the object. | |
709 | */ | |
710 | mig_object->pVtbl->Release((IMIGObject *)mig_object); | |
711 | return (TRUE); | |
712 | } | |
713 | ||
714 | /* | |
715 | * Kernel implementation of the notification chain for MIG object | |
716 | * is kept separate from the actual objects, since there are expected | |
717 | * to be much fewer of them than actual objects. | |
718 | * | |
719 | * The implementation of this part of MIG objects is coming | |
720 | * "Real Soon Now"(TM). | |
721 | */ |