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 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
|
/*
* Mach Operating System
* Copyright (c) 1991,1990,1989,1988,1987 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.
*/
/*
* File: ipc_tt.c
* Purpose:
* Task and thread related IPC functions.
*/
#include <mach/boolean.h>
#include <mach/kern_return.h>
#include <mach/mach_param.h>
#include <mach/task_special_ports.h>
#include <mach/thread_special_ports.h>
#include <vm/vm_kern.h>
#include <kern/debug.h>
#include <kern/kalloc.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/ipc_kobject.h>
#include <kern/ipc_tt.h>
#include <ipc/ipc_space.h>
#include <ipc/ipc_table.h>
#include <ipc/ipc_port.h>
#include <ipc/ipc_right.h>
#include <ipc/ipc_entry.h>
#include <ipc/ipc_object.h>
/*
* Routine: ipc_task_init
* Purpose:
* Initialize a task's IPC state.
*
* If non-null, some state will be inherited from the parent.
* The parent must be appropriately initialized.
* Conditions:
* Nothing locked.
*/
void
ipc_task_init(
task_t task,
task_t parent)
{
ipc_space_t space;
ipc_port_t kport;
kern_return_t kr;
int i;
kr = ipc_space_create(&space);
if (kr != KERN_SUCCESS)
panic("ipc_task_init");
kport = ipc_port_alloc_kernel();
if (kport == IP_NULL)
panic("ipc_task_init");
itk_lock_init(task);
task->itk_self = kport;
task->itk_sself = ipc_port_make_send(kport);
task->itk_space = space;
if (parent == TASK_NULL) {
task->itk_exception = IP_NULL;
task->itk_bootstrap = IP_NULL;
for (i = 0; i < TASK_PORT_REGISTER_MAX; i++)
task->itk_registered[i] = IP_NULL;
} else {
itk_lock(parent);
assert(parent->itk_self != IP_NULL);
/* inherit registered ports */
for (i = 0; i < TASK_PORT_REGISTER_MAX; i++)
task->itk_registered[i] =
ipc_port_copy_send(parent->itk_registered[i]);
/* inherit exception and bootstrap ports */
task->itk_exception =
ipc_port_copy_send(parent->itk_exception);
task->itk_bootstrap =
ipc_port_copy_send(parent->itk_bootstrap);
itk_unlock(parent);
}
}
/*
* Routine: ipc_task_enable
* Purpose:
* Enable a task for IPC access.
* Conditions:
* Nothing locked.
*/
void
ipc_task_enable(
task_t task)
{
ipc_port_t kport;
itk_lock(task);
kport = task->itk_self;
if (kport != IP_NULL)
ipc_kobject_set(kport, (ipc_kobject_t) task, IKOT_TASK);
itk_unlock(task);
}
/*
* Routine: ipc_task_disable
* Purpose:
* Disable IPC access to a task.
* Conditions:
* Nothing locked.
*/
void
ipc_task_disable(
task_t task)
{
ipc_port_t kport;
itk_lock(task);
kport = task->itk_self;
if (kport != IP_NULL)
ipc_kobject_set(kport, IKO_NULL, IKOT_NONE);
itk_unlock(task);
}
/*
* Routine: ipc_task_terminate
* Purpose:
* Clean up and destroy a task's IPC state.
* Conditions:
* Nothing locked. The task must be suspended.
* (Or the current thread must be in the task.)
*/
void
ipc_task_terminate(
task_t task)
{
ipc_port_t kport;
int i;
itk_lock(task);
kport = task->itk_self;
if (kport == IP_NULL) {
/* the task is already terminated (can this happen?) */
itk_unlock(task);
return;
}
task->itk_self = IP_NULL;
itk_unlock(task);
/* release the naked send rights */
if (IP_VALID(task->itk_sself))
ipc_port_release_send(task->itk_sself);
if (IP_VALID(task->itk_exception))
ipc_port_release_send(task->itk_exception);
if (IP_VALID(task->itk_bootstrap))
ipc_port_release_send(task->itk_bootstrap);
for (i = 0; i < TASK_PORT_REGISTER_MAX; i++)
if (IP_VALID(task->itk_registered[i]))
ipc_port_release_send(task->itk_registered[i]);
/* destroy the space, leaving just a reference for it */
ipc_space_destroy(task->itk_space);
/* destroy the kernel port */
ipc_port_dealloc_kernel(kport);
}
/*
* Routine: ipc_thread_init
* Purpose:
* Initialize a thread's IPC state.
* Conditions:
* Nothing locked.
*/
void
ipc_thread_init(thread_t thread)
{
ipc_port_t kport;
kport = ipc_port_alloc_kernel();
if (kport == IP_NULL)
panic("ipc_thread_init");
ipc_thread_links_init(thread);
ipc_kmsg_queue_init(&thread->ith_messages);
ith_lock_init(thread);
thread->ith_self = kport;
thread->ith_sself = ipc_port_make_send(kport);
thread->ith_exception = IP_NULL;
thread->ith_mig_reply = MACH_PORT_NULL;
thread->ith_rpc_reply = IP_NULL;
}
/*
* Routine: ipc_thread_enable
* Purpose:
* Enable a thread for IPC access.
* Conditions:
* Nothing locked.
*/
void
ipc_thread_enable(thread_t thread)
{
ipc_port_t kport;
ith_lock(thread);
kport = thread->ith_self;
if (kport != IP_NULL)
ipc_kobject_set(kport, (ipc_kobject_t) thread, IKOT_THREAD);
ith_unlock(thread);
}
/*
* Routine: ipc_thread_disable
* Purpose:
* Disable IPC access to a thread.
* Conditions:
* Nothing locked.
*/
void
ipc_thread_disable(thread_t thread)
{
ipc_port_t kport;
ith_lock(thread);
kport = thread->ith_self;
if (kport != IP_NULL)
ipc_kobject_set(kport, IKO_NULL, IKOT_NONE);
ith_unlock(thread);
}
/*
* Routine: ipc_thread_terminate
* Purpose:
* Clean up and destroy a thread's IPC state.
* Conditions:
* Nothing locked. The thread must be suspended.
* (Or be the current thread.)
*/
void
ipc_thread_terminate(thread_t thread)
{
ipc_port_t kport;
ith_lock(thread);
kport = thread->ith_self;
if (kport == IP_NULL) {
/* the thread is already terminated (can this happen?) */
ith_unlock(thread);
return;
}
thread->ith_self = IP_NULL;
ith_unlock(thread);
assert(ipc_kmsg_queue_empty(&thread->ith_messages));
/* release the naked send rights */
if (IP_VALID(thread->ith_sself))
ipc_port_release_send(thread->ith_sself);
if (IP_VALID(thread->ith_exception))
ipc_port_release_send(thread->ith_exception);
/* destroy the kernel port */
ipc_port_dealloc_kernel(kport);
}
#if 0
/*
* Routine: retrieve_task_self
* Purpose:
* Return a send right (possibly null/dead)
* for the task's user-visible self port.
* Conditions:
* Nothing locked.
*/
ipc_port_t
retrieve_task_self(task)
task_t task;
{
ipc_port_t port;
assert(task != TASK_NULL);
itk_lock(task);
if (task->itk_self != IP_NULL)
port = ipc_port_copy_send(task->itk_sself);
else
port = IP_NULL;
itk_unlock(task);
return port;
}
/*
* Routine: retrieve_thread_self
* Purpose:
* Return a send right (possibly null/dead)
* for the thread's user-visible self port.
* Conditions:
* Nothing locked.
*/
ipc_port_t
retrieve_thread_self(thread)
thread_t thread;
{
ipc_port_t port;
assert(thread != ITH_NULL);
ith_lock(thread);
if (thread->ith_self != IP_NULL)
port = ipc_port_copy_send(thread->ith_sself);
else
port = IP_NULL;
ith_unlock(thread);
return port;
}
#endif /* 0 */
/*
* Routine: retrieve_task_self_fast
* Purpose:
* Optimized version of retrieve_task_self,
* that only works for the current task.
*
* Return a send right (possibly null/dead)
* for the task's user-visible self port.
* Conditions:
* Nothing locked.
*/
ipc_port_t
retrieve_task_self_fast(
task_t task)
{
ipc_port_t port;
assert(task == current_task());
itk_lock(task);
assert(task->itk_self != IP_NULL);
if ((port = task->itk_sself) == task->itk_self) {
/* no interposing */
ip_lock(port);
assert(ip_active(port));
ip_reference(port);
port->ip_srights++;
ip_unlock(port);
} else
port = ipc_port_copy_send(port);
itk_unlock(task);
return port;
}
/*
* Routine: retrieve_thread_self_fast
* Purpose:
* Optimized version of retrieve_thread_self,
* that only works for the current thread.
*
* Return a send right (possibly null/dead)
* for the thread's user-visible self port.
* Conditions:
* Nothing locked.
*/
ipc_port_t
retrieve_thread_self_fast(thread_t thread)
{
ipc_port_t port;
assert(thread == current_thread());
ith_lock(thread);
assert(thread->ith_self != IP_NULL);
if ((port = thread->ith_sself) == thread->ith_self) {
/* no interposing */
ip_lock(port);
assert(ip_active(port));
ip_reference(port);
port->ip_srights++;
ip_unlock(port);
} else
port = ipc_port_copy_send(port);
ith_unlock(thread);
return port;
}
#if 0
/*
* Routine: retrieve_task_exception
* Purpose:
* Return a send right (possibly null/dead)
* for the task's exception port.
* Conditions:
* Nothing locked.
*/
ipc_port_t
retrieve_task_exception(task)
task_t task;
{
ipc_port_t port;
assert(task != TASK_NULL);
itk_lock(task);
if (task->itk_self != IP_NULL)
port = ipc_port_copy_send(task->itk_exception);
else
port = IP_NULL;
itk_unlock(task);
return port;
}
/*
* Routine: retrieve_thread_exception
* Purpose:
* Return a send right (possibly null/dead)
* for the thread's exception port.
* Conditions:
* Nothing locked.
*/
ipc_port_t
retrieve_thread_exception(thread)
thread_t thread;
{
ipc_port_t port;
assert(thread != ITH_NULL);
ith_lock(thread);
if (thread->ith_self != IP_NULL)
port = ipc_port_copy_send(thread->ith_exception);
else
port = IP_NULL;
ith_unlock(thread);
return port;
}
#endif /* 0 */
/*
* Routine: mach_task_self [mach trap]
* Purpose:
* Give the caller send rights for his own task port.
* Conditions:
* Nothing locked.
* Returns:
* MACH_PORT_NULL if there are any resource failures
* or other errors.
*/
mach_port_t
mach_task_self(void)
{
task_t task = current_task();
ipc_port_t sright;
sright = retrieve_task_self_fast(task);
return ipc_port_copyout_send(sright, task->itk_space);
}
/*
* Routine: mach_thread_self [mach trap]
* Purpose:
* Give the caller send rights for his own thread port.
* Conditions:
* Nothing locked.
* Returns:
* MACH_PORT_NULL if there are any resource failures
* or other errors.
*/
mach_port_t
mach_thread_self(void)
{
thread_t thread = current_thread();
task_t task = thread->task;
ipc_port_t sright;
sright = retrieve_thread_self_fast(thread);
return ipc_port_copyout_send(sright, task->itk_space);
}
/*
* Routine: mach_reply_port [mach trap]
* Purpose:
* Allocate a port for the caller.
* Conditions:
* Nothing locked.
* Returns:
* MACH_PORT_NULL if there are any resource failures
* or other errors.
*/
mach_port_t
mach_reply_port(void)
{
ipc_port_t port;
mach_port_t name;
kern_return_t kr;
kr = ipc_port_alloc(current_task()->itk_space, &name, &port);
if (kr == KERN_SUCCESS)
ip_unlock(port);
else
name = MACH_PORT_NULL;
return name;
}
/*
* Routine: task_get_special_port [kernel call]
* Purpose:
* Clones a send right for one of the task's
* special ports.
* Conditions:
* Nothing locked.
* Returns:
* KERN_SUCCESS Extracted a send right.
* KERN_INVALID_ARGUMENT The task is null.
* KERN_FAILURE The task/space is dead.
* KERN_INVALID_ARGUMENT Invalid special port.
*/
kern_return_t
task_get_special_port(
task_t task,
int which,
ipc_port_t *portp)
{
ipc_port_t *whichp;
ipc_port_t port;
if (task == TASK_NULL)
return KERN_INVALID_ARGUMENT;
switch (which) {
case TASK_KERNEL_PORT:
whichp = &task->itk_sself;
break;
case TASK_EXCEPTION_PORT:
whichp = &task->itk_exception;
break;
case TASK_BOOTSTRAP_PORT:
whichp = &task->itk_bootstrap;
break;
default:
return KERN_INVALID_ARGUMENT;
}
itk_lock(task);
if (task->itk_self == IP_NULL) {
itk_unlock(task);
return KERN_FAILURE;
}
port = ipc_port_copy_send(*whichp);
itk_unlock(task);
*portp = port;
return KERN_SUCCESS;
}
/*
* Routine: task_set_special_port [kernel call]
* Purpose:
* Changes one of the task's special ports,
* setting it to the supplied send right.
* Conditions:
* Nothing locked. If successful, consumes
* the supplied send right.
* Returns:
* KERN_SUCCESS Changed the special port.
* KERN_INVALID_ARGUMENT The task is null.
* KERN_FAILURE The task/space is dead.
* KERN_INVALID_ARGUMENT Invalid special port.
*/
kern_return_t
task_set_special_port(
task_t task,
int which,
const ipc_port_t port)
{
ipc_port_t *whichp;
ipc_port_t old;
if (task == TASK_NULL)
return KERN_INVALID_ARGUMENT;
switch (which) {
case TASK_KERNEL_PORT:
whichp = &task->itk_sself;
break;
case TASK_EXCEPTION_PORT:
whichp = &task->itk_exception;
break;
case TASK_BOOTSTRAP_PORT:
whichp = &task->itk_bootstrap;
break;
default:
return KERN_INVALID_ARGUMENT;
}
itk_lock(task);
if (task->itk_self == IP_NULL) {
itk_unlock(task);
return KERN_FAILURE;
}
old = *whichp;
*whichp = port;
itk_unlock(task);
if (IP_VALID(old))
ipc_port_release_send(old);
return KERN_SUCCESS;
}
/*
* Routine: thread_get_special_port [kernel call]
* Purpose:
* Clones a send right for one of the thread's
* special ports.
* Conditions:
* Nothing locked.
* Returns:
* KERN_SUCCESS Extracted a send right.
* KERN_INVALID_ARGUMENT The thread is null.
* KERN_FAILURE The thread is dead.
* KERN_INVALID_ARGUMENT Invalid special port.
*/
kern_return_t
thread_get_special_port(
thread_t thread,
int which,
ipc_port_t *portp)
{
ipc_port_t *whichp;
ipc_port_t port;
if (thread == ITH_NULL)
return KERN_INVALID_ARGUMENT;
switch (which) {
case THREAD_KERNEL_PORT:
whichp = &thread->ith_sself;
break;
case THREAD_EXCEPTION_PORT:
whichp = &thread->ith_exception;
break;
default:
return KERN_INVALID_ARGUMENT;
}
ith_lock(thread);
if (thread->ith_self == IP_NULL) {
ith_unlock(thread);
return KERN_FAILURE;
}
port = ipc_port_copy_send(*whichp);
ith_unlock(thread);
*portp = port;
return KERN_SUCCESS;
}
/*
* Routine: thread_set_special_port [kernel call]
* Purpose:
* Changes one of the thread's special ports,
* setting it to the supplied send right.
* Conditions:
* Nothing locked. If successful, consumes
* the supplied send right.
* Returns:
* KERN_SUCCESS Changed the special port.
* KERN_INVALID_ARGUMENT The thread is null.
* KERN_FAILURE The thread is dead.
* KERN_INVALID_ARGUMENT Invalid special port.
*/
kern_return_t
thread_set_special_port(
thread_t thread,
int which,
ipc_port_t port)
{
ipc_port_t *whichp;
ipc_port_t old;
if (thread == ITH_NULL)
return KERN_INVALID_ARGUMENT;
switch (which) {
case THREAD_KERNEL_PORT:
whichp = &thread->ith_sself;
break;
case THREAD_EXCEPTION_PORT:
whichp = &thread->ith_exception;
break;
default:
return KERN_INVALID_ARGUMENT;
}
ith_lock(thread);
if (thread->ith_self == IP_NULL) {
ith_unlock(thread);
return KERN_FAILURE;
}
old = *whichp;
*whichp = port;
ith_unlock(thread);
if (IP_VALID(old))
ipc_port_release_send(old);
return KERN_SUCCESS;
}
/*
* Routine: mach_ports_register [kernel call]
* Purpose:
* Stash a handful of port send rights in the task.
* Child tasks will inherit these rights, but they
* must use mach_ports_lookup to acquire them.
*
* The rights are supplied in a (wired) kalloc'd segment.
* Rights which aren't supplied are assumed to be null.
* Conditions:
* Nothing locked. If successful, consumes
* the supplied rights and memory.
* Returns:
* KERN_SUCCESS Stashed the port rights.
* KERN_INVALID_ARGUMENT The task is null.
* KERN_INVALID_ARGUMENT The task is dead.
* KERN_INVALID_ARGUMENT Too many port rights supplied.
*/
kern_return_t
mach_ports_register(
task_t task,
mach_port_array_t memory,
mach_msg_type_number_t portsCnt)
{
ipc_port_t ports[TASK_PORT_REGISTER_MAX];
unsigned i;
if ((task == TASK_NULL) ||
(portsCnt > TASK_PORT_REGISTER_MAX))
return KERN_INVALID_ARGUMENT;
/*
* Pad the port rights with nulls.
*/
for (i = 0; i < portsCnt; i++)
ports[i] = (ipc_port_t)memory[i];
for (; i < TASK_PORT_REGISTER_MAX; i++)
ports[i] = IP_NULL;
itk_lock(task);
if (task->itk_self == IP_NULL) {
itk_unlock(task);
return KERN_INVALID_ARGUMENT;
}
/*
* Replace the old send rights with the new.
* Release the old rights after unlocking.
*/
for (i = 0; i < TASK_PORT_REGISTER_MAX; i++) {
ipc_port_t old;
old = task->itk_registered[i];
task->itk_registered[i] = ports[i];
ports[i] = old;
}
itk_unlock(task);
for (i = 0; i < TASK_PORT_REGISTER_MAX; i++)
if (IP_VALID(ports[i]))
ipc_port_release_send(ports[i]);
/*
* Now that the operation is known to be successful,
* we can free the memory.
*/
if (portsCnt != 0)
kfree((vm_offset_t) memory,
(vm_size_t) (portsCnt * sizeof(mach_port_t)));
return KERN_SUCCESS;
}
/*
* Routine: mach_ports_lookup [kernel call]
* Purpose:
* Retrieves (clones) the stashed port send rights.
* Conditions:
* Nothing locked. If successful, the caller gets
* rights and memory.
* Returns:
* KERN_SUCCESS Retrieved the send rights.
* KERN_INVALID_ARGUMENT The task is null.
* KERN_INVALID_ARGUMENT The task is dead.
* KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
*/
kern_return_t
mach_ports_lookup(
task_t task,
ipc_port_t **portsp,
mach_msg_type_number_t *portsCnt)
{
vm_offset_t memory;
vm_size_t size;
ipc_port_t *ports;
int i;
if (task == TASK_NULL)
return KERN_INVALID_ARGUMENT;
size = (vm_size_t) (TASK_PORT_REGISTER_MAX * sizeof(ipc_port_t));
memory = kalloc(size);
if (memory == 0)
return KERN_RESOURCE_SHORTAGE;
itk_lock(task);
if (task->itk_self == IP_NULL) {
itk_unlock(task);
kfree(memory, size);
return KERN_INVALID_ARGUMENT;
}
ports = (ipc_port_t *) memory;
/*
* Clone port rights. Because kalloc'd memory
* is wired, we won't fault while holding the task lock.
*/
for (i = 0; i < TASK_PORT_REGISTER_MAX; i++)
ports[i] = ipc_port_copy_send(task->itk_registered[i]);
itk_unlock(task);
*portsp = ports;
*portsCnt = TASK_PORT_REGISTER_MAX;
return KERN_SUCCESS;
}
/*
* Routine: convert_port_to_task
* Purpose:
* Convert from a port to a task.
* Doesn't consume the port ref; produces a task ref,
* which may be null.
* Conditions:
* Nothing locked.
*/
task_t
convert_port_to_task(
ipc_port_t port)
{
task_t task = TASK_NULL;
if (IP_VALID(port)) {
ip_lock(port);
if (ip_active(port) &&
(ip_kotype(port) == IKOT_TASK)) {
task = (task_t) port->ip_kobject;
task_reference(task);
}
ip_unlock(port);
}
return task;
}
/*
* Routine: convert_port_to_space
* Purpose:
* Convert from a port to a space.
* Doesn't consume the port ref; produces a space ref,
* which may be null.
* Conditions:
* Nothing locked.
*/
ipc_space_t
convert_port_to_space(
ipc_port_t port)
{
ipc_space_t space = IS_NULL;
if (IP_VALID(port)) {
ip_lock(port);
if (ip_active(port) &&
(ip_kotype(port) == IKOT_TASK)) {
space = ((task_t) port->ip_kobject)->itk_space;
is_reference(space);
}
ip_unlock(port);
}
return space;
}
/*
* Routine: convert_port_to_map
* Purpose:
* Convert from a port to a map.
* Doesn't consume the port ref; produces a map ref,
* which may be null.
* Conditions:
* Nothing locked.
*/
vm_map_t
convert_port_to_map(ipc_port_t port)
{
vm_map_t map = VM_MAP_NULL;
if (IP_VALID(port)) {
ip_lock(port);
if (ip_active(port) &&
(ip_kotype(port) == IKOT_TASK)) {
map = ((task_t) port->ip_kobject)->map;
vm_map_reference(map);
}
ip_unlock(port);
}
return map;
}
/*
* Routine: convert_port_to_thread
* Purpose:
* Convert from a port to a thread.
* Doesn't consume the port ref; produces a thread ref,
* which may be null.
* Conditions:
* Nothing locked.
*/
thread_t
convert_port_to_thread(ipc_port_t port)
{
thread_t thread = THREAD_NULL;
if (IP_VALID(port)) {
ip_lock(port);
if (ip_active(port) &&
(ip_kotype(port) == IKOT_THREAD)) {
thread = (thread_t) port->ip_kobject;
thread_reference(thread);
}
ip_unlock(port);
}
return thread;
}
/*
* Routine: convert_task_to_port
* Purpose:
* Convert from a task to a port.
* Consumes a task ref; produces a naked send right
* which may be invalid.
* Conditions:
* Nothing locked.
*/
ipc_port_t
convert_task_to_port(task_t task)
{
ipc_port_t port;
itk_lock(task);
if (task->itk_self != IP_NULL)
port = ipc_port_make_send(task->itk_self);
else
port = IP_NULL;
itk_unlock(task);
task_deallocate(task);
return port;
}
/*
* Routine: convert_thread_to_port
* Purpose:
* Convert from a thread to a port.
* Consumes a thread ref; produces a naked send right
* which may be invalid.
* Conditions:
* Nothing locked.
*/
ipc_port_t
convert_thread_to_port(thread_t thread)
{
ipc_port_t port;
ith_lock(thread);
if (thread->ith_self != IP_NULL)
port = ipc_port_make_send(thread->ith_self);
else
port = IP_NULL;
ith_unlock(thread);
thread_deallocate(thread);
return port;
}
/*
* Routine: space_deallocate
* Purpose:
* Deallocate a space ref produced by convert_port_to_space.
* Conditions:
* Nothing locked.
*/
void
space_deallocate(ipc_space_t space)
{
if (space != IS_NULL)
is_release(space);
}
|