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