| 1 | /* |
| 2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 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. |
| 14 | * |
| 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 |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 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. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | /* |
| 29 | * @OSF_COPYRIGHT@ |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #include <kern/sync_sema.h> |
| 34 | #include <kern/sync_lock.h> |
| 35 | #include <kern/ipc_kobject.h> |
| 36 | #include <kern/ipc_sync.h> |
| 37 | #include <ipc/port.h> |
| 38 | #include <ipc/ipc_space.h> |
| 39 | #include <ipc/ipc_port.h> |
| 40 | #include <mach/mach_types.h> |
| 41 | #include <mach/semaphore.h> |
| 42 | #include <mach/lock_set_server.h> |
| 43 | #include <mach/mach_port_server.h> |
| 44 | #include <mach/port.h> |
| 45 | |
| 46 | |
| 47 | /* |
| 48 | * Routine: port_name_to_semaphore |
| 49 | * Purpose: |
| 50 | * Convert from a port name in the current space to a semaphore. |
| 51 | * Produces a semaphore ref, which may be null. |
| 52 | * Conditions: |
| 53 | * Nothing locked. |
| 54 | */ |
| 55 | kern_return_t |
| 56 | port_name_to_semaphore( |
| 57 | mach_port_name_t name, |
| 58 | semaphore_t *semaphorep) |
| 59 | { |
| 60 | ipc_port_t kern_port; |
| 61 | kern_return_t kr; |
| 62 | |
| 63 | if (!MACH_PORT_VALID(name)) { |
| 64 | *semaphorep = SEMAPHORE_NULL; |
| 65 | return KERN_INVALID_NAME; |
| 66 | } |
| 67 | |
| 68 | kr = ipc_object_translate(current_space(), name, MACH_PORT_RIGHT_SEND, |
| 69 | (ipc_object_t *) &kern_port); |
| 70 | if (kr != KERN_SUCCESS) { |
| 71 | *semaphorep = SEMAPHORE_NULL; |
| 72 | return kr; |
| 73 | } |
| 74 | /* have the port locked */ |
| 75 | assert(IP_VALID(kern_port)); |
| 76 | |
| 77 | *semaphorep = convert_port_to_semaphore(kern_port); |
| 78 | if (*semaphorep == SEMAPHORE_NULL) { |
| 79 | /* the port is valid, but doesn't denote a semaphore */ |
| 80 | kr = KERN_INVALID_CAPABILITY; |
| 81 | } else { |
| 82 | kr = KERN_SUCCESS; |
| 83 | } |
| 84 | ip_unlock(kern_port); |
| 85 | |
| 86 | return kr; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Routine: convert_port_to_semaphore |
| 91 | * Purpose: |
| 92 | * Convert from a port to a semaphore. |
| 93 | * Doesn't consume the port [send-right] ref; |
| 94 | * produces a semaphore ref, which may be null. |
| 95 | * Conditions: |
| 96 | * Caller has a send-right reference to port. |
| 97 | * Port may or may not be locked. |
| 98 | */ |
| 99 | semaphore_t |
| 100 | convert_port_to_semaphore (ipc_port_t port) |
| 101 | { |
| 102 | |
| 103 | if (IP_VALID(port)) { |
| 104 | semaphore_t semaphore; |
| 105 | |
| 106 | /* |
| 107 | * No need to lock because we have a reference on the |
| 108 | * port, and if it is a true semaphore port, that reference |
| 109 | * keeps the semaphore bound to the port (and active). |
| 110 | */ |
| 111 | if (ip_kotype(port) == IKOT_SEMAPHORE) { |
| 112 | assert(ip_active(port)); |
| 113 | semaphore = (semaphore_t) port->ip_kobject; |
| 114 | semaphore_reference(semaphore); |
| 115 | return (semaphore); |
| 116 | } |
| 117 | } |
| 118 | return SEMAPHORE_NULL; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /* |
| 123 | * Routine: convert_semaphore_to_port |
| 124 | * Purpose: |
| 125 | * Convert a semaphore reference to a send right to a |
| 126 | * semaphore port. |
| 127 | * |
| 128 | * Consumes the semaphore reference. If the semaphore |
| 129 | * port currently has no send rights (or doesn't exist |
| 130 | * yet), the reference is donated to the port to represent |
| 131 | * all extant send rights collectively. |
| 132 | */ |
| 133 | ipc_port_t |
| 134 | convert_semaphore_to_port (semaphore_t semaphore) |
| 135 | { |
| 136 | ipc_port_t port, send; |
| 137 | |
| 138 | if (semaphore == SEMAPHORE_NULL) |
| 139 | return (IP_NULL); |
| 140 | |
| 141 | /* caller is donating a reference */ |
| 142 | port = semaphore->port; |
| 143 | |
| 144 | if (!IP_VALID(port)) { |
| 145 | port = ipc_port_alloc_kernel(); |
| 146 | assert(IP_VALID(port)); |
| 147 | ipc_kobject_set_atomically(port, (ipc_kobject_t) semaphore, IKOT_SEMAPHORE); |
| 148 | |
| 149 | /* If we lose the race, deallocate and pick up the other guy's port */ |
| 150 | if (!OSCompareAndSwapPtr(IP_NULL, port, &semaphore->port)) { |
| 151 | ipc_port_dealloc_kernel(port); |
| 152 | port = semaphore->port; |
| 153 | assert(ip_kotype(port) == IKOT_SEMAPHORE); |
| 154 | assert(port->ip_kobject == (ipc_kobject_t)semaphore); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | ip_lock(port); |
| 159 | assert(ip_active(port)); |
| 160 | send = ipc_port_make_send_locked(port); |
| 161 | |
| 162 | if (1 == port->ip_srights) { |
| 163 | ipc_port_t old_notify; |
| 164 | |
| 165 | /* transfer our ref to the port, and arm the no-senders notification */ |
| 166 | assert(IP_NULL == port->ip_nsrequest); |
| 167 | ipc_port_nsrequest(port, port->ip_mscount, ipc_port_make_sonce_locked(port), &old_notify); |
| 168 | /* port unlocked */ |
| 169 | assert(IP_NULL == old_notify); |
| 170 | } else { |
| 171 | /* piggyback on the existing port reference, so consume ours */ |
| 172 | ip_unlock(port); |
| 173 | semaphore_dereference(semaphore); |
| 174 | } |
| 175 | return (send); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Routine: semaphore_notify |
| 180 | * Purpose: |
| 181 | * Called whenever the Mach port system detects no-senders |
| 182 | * on the semaphore port. |
| 183 | * |
| 184 | * When a send-right is first created, a no-senders |
| 185 | * notification is armed (and a semaphore reference is donated). |
| 186 | * |
| 187 | * A no-senders notification will be posted when no one else holds a |
| 188 | * send-right (reference) to the semaphore's port. This notification function |
| 189 | * will consume the semaphore reference donated to the extant collection of |
| 190 | * send-rights. |
| 191 | */ |
| 192 | void |
| 193 | semaphore_notify(mach_msg_header_t *msg) |
| 194 | { |
| 195 | mach_no_senders_notification_t *notification = (void *)msg; |
| 196 | ipc_port_t port = notification->not_header.msgh_remote_port; |
| 197 | semaphore_t semaphore; |
| 198 | |
| 199 | assert(ip_active(port)); |
| 200 | assert(IKOT_SEMAPHORE == ip_kotype(port)); |
| 201 | semaphore = (semaphore_t)port->ip_kobject; |
| 202 | |
| 203 | semaphore_dereference(semaphore); |
| 204 | } |
| 205 | |
| 206 | lock_set_t |
| 207 | convert_port_to_lock_set (__unused ipc_port_t port) |
| 208 | { |
| 209 | return (LOCK_SET_NULL); |
| 210 | } |
| 211 | |
| 212 | ipc_port_t |
| 213 | convert_lock_set_to_port (__unused lock_set_t lock_set) |
| 214 | { |
| 215 | return (IP_NULL); |
| 216 | } |
| 217 | |
| 218 | |