[4e49572] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[4e49572] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[cc73a8a1] | 29 | /** @addtogroup genericipc
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[4e49572] | 35 | /* IPC resources management
|
---|
| 36 | *
|
---|
[51ec40f] | 37 | * The goal of this source code is to properly manage IPC resources and allow
|
---|
| 38 | * straight and clean clean-up procedure upon task termination.
|
---|
[4e49572] | 39 | *
|
---|
| 40 | * The pattern of usage of the resources is:
|
---|
| 41 | * - allocate empty phone slot, connect | deallocate slot
|
---|
| 42 | * - disconnect connected phone (some messages might be on the fly)
|
---|
| 43 | * - find phone in slot and send a message using phone
|
---|
| 44 | * - answer message to phone
|
---|
[fbcfd458] | 45 | * - hangup phone (the caller has hung up)
|
---|
| 46 | * - hangup phone (the answerbox is exiting)
|
---|
[da1bafb] | 47 | *
|
---|
[ba81cab] | 48 | * Locking strategy
|
---|
| 49 | *
|
---|
[51ec40f] | 50 | * - To use a phone, disconnect a phone etc., the phone must be first locked and
|
---|
| 51 | * then checked that it is connected
|
---|
| 52 | * - To connect an allocated phone it need not be locked (assigning pointer is
|
---|
| 53 | * atomic on all platforms)
|
---|
[ba81cab] | 54 | *
|
---|
| 55 | * - To find an empty phone slot, the TASK must be locked
|
---|
| 56 | * - To answer a message, the answerbox must be locked
|
---|
| 57 | * - The locking of phone and answerbox is done at the ipc_ level.
|
---|
[51ec40f] | 58 | * It is perfectly correct to pass unconnected phone to these functions and
|
---|
| 59 | * proper reply will be generated.
|
---|
[ba81cab] | 60 | *
|
---|
| 61 | * Locking order
|
---|
| 62 | *
|
---|
| 63 | * - first phone, then answerbox
|
---|
| 64 | * + Easy locking on calls
|
---|
[51ec40f] | 65 | * - Very hard traversing list of phones when disconnecting because the phones
|
---|
| 66 | * may disconnect during traversal of list of connected phones. The only
|
---|
| 67 | * possibility is try_lock with restart of list traversal.
|
---|
[ba81cab] | 68 | *
|
---|
[fbcfd458] | 69 | * Destroying is less frequent, this approach is taken.
|
---|
| 70 | *
|
---|
[7c7aae16] | 71 | * Phone call
|
---|
| 72 | *
|
---|
| 73 | * *** Connect_me_to ***
|
---|
[51ec40f] | 74 | * The caller sends IPC_M_CONNECT_ME_TO to an answerbox. The server receives
|
---|
[38c706cc] | 75 | * 'phoneid' of the connecting phone as an ARG5. If it answers with RETVAL=0,
|
---|
[51ec40f] | 76 | * the phonecall is accepted, otherwise it is refused.
|
---|
[7c7aae16] | 77 | *
|
---|
| 78 | * *** Connect_to_me ***
|
---|
[51ec40f] | 79 | * The caller sends IPC_M_CONNECT_TO_ME.
|
---|
| 80 | * The server receives an automatically opened phoneid. If it accepts
|
---|
| 81 | * (RETVAL=0), it can use the phoneid immediately.
|
---|
| 82 | * Possible race condition can arise, when the client receives messages from new
|
---|
| 83 | * connection before getting response for connect_to_me message. Userspace
|
---|
| 84 | * should implement handshake protocol that would control it.
|
---|
[7c7aae16] | 85 | *
|
---|
[fbcfd458] | 86 | * Phone hangup
|
---|
[da1bafb] | 87 | *
|
---|
[fbcfd458] | 88 | * *** The caller hangs up (sys_ipc_hangup) ***
|
---|
| 89 | * - The phone is disconnected (no more messages can be sent over this phone),
|
---|
[51ec40f] | 90 | * all in-progress messages are correctly handled. The answerbox receives
|
---|
[fbcfd458] | 91 | * IPC_M_PHONE_HUNGUP call from the phone that hung up. When all async
|
---|
| 92 | * calls are answered, the phone is deallocated.
|
---|
[ba81cab] | 93 | *
|
---|
[9f22213] | 94 | * *** The answerbox hangs up (ipc_answer(EHANGUP))
|
---|
| 95 | * - The phone is disconnected. EHANGUP response code is sent
|
---|
[f3a3f0d7] | 96 | * to the calling task. All new calls through this phone
|
---|
[9f22213] | 97 | * get a EHUNGUP error code, the task is expected to
|
---|
[06e1e95] | 98 | * send an sys_ipc_hangup after cleaning up its internal structures.
|
---|
[ba81cab] | 99 | *
|
---|
[9f22213] | 100 | * Call forwarding
|
---|
[da1bafb] | 101 | *
|
---|
[9f22213] | 102 | * The call can be forwarded, so that the answer to call is passed directly
|
---|
| 103 | * to the original sender. However, this poses special problems regarding
|
---|
| 104 | * routing of hangup messages.
|
---|
| 105 | *
|
---|
| 106 | * sys_ipc_hangup -> IPC_M_PHONE_HUNGUP
|
---|
| 107 | * - this message CANNOT be forwarded
|
---|
| 108 | *
|
---|
| 109 | * EHANGUP during forward
|
---|
| 110 | * - The *forwarding* phone will be closed, EFORWARD is sent to receiver.
|
---|
| 111 | *
|
---|
| 112 | * EHANGUP, ENOENT during forward
|
---|
| 113 | * - EFORWARD is sent to the receiver, ipc_forward returns error code EFORWARD
|
---|
| 114 | *
|
---|
[ba81cab] | 115 | * Cleanup strategy
|
---|
[da1bafb] | 116 | *
|
---|
[9f22213] | 117 | * 1) Disconnect all our phones ('ipc_phone_hangup').
|
---|
[fbcfd458] | 118 | *
|
---|
| 119 | * 2) Disconnect all phones connected to answerbox.
|
---|
[ba81cab] | 120 | *
|
---|
[fbcfd458] | 121 | * 3) Answer all messages in 'calls' and 'dispatched_calls' queues with
|
---|
[9f22213] | 122 | * appropriate error code (EHANGUP, EFORWARD).
|
---|
[ba81cab] | 123 | *
|
---|
[7c7aae16] | 124 | * 4) Wait for all async answers to arrive and dispose of them.
|
---|
[da1bafb] | 125 | *
|
---|
[4e49572] | 126 | */
|
---|
| 127 |
|
---|
| 128 | #include <synch/spinlock.h>
|
---|
| 129 | #include <ipc/ipc.h>
|
---|
| 130 | #include <arch.h>
|
---|
| 131 | #include <proc/task.h>
|
---|
| 132 | #include <ipc/ipcrsc.h>
|
---|
[63e27ef] | 133 | #include <assert.h>
|
---|
[e8039a86] | 134 | #include <abi/errno.h>
|
---|
[3f74275] | 135 | #include <cap/cap.h>
|
---|
[4e49572] | 136 |
|
---|
[8b243f2] | 137 | /** Find call_t * in call table according to callid.
|
---|
[4e49572] | 138 | *
|
---|
[8b243f2] | 139 | * @todo Some speedup (hash table?)
|
---|
| 140 | *
|
---|
[da1bafb] | 141 | * @param callid Userspace hash of the call. Currently it is the call
|
---|
| 142 | * structure kernel address.
|
---|
| 143 | *
|
---|
| 144 | * @return NULL on not found, otherwise pointer to the call
|
---|
| 145 | * structure.
|
---|
[8b243f2] | 146 | *
|
---|
[4e49572] | 147 | */
|
---|
[96b02eb9] | 148 | call_t *get_call(sysarg_t callid)
|
---|
[4e49572] | 149 | {
|
---|
[da1bafb] | 150 | call_t *result = NULL;
|
---|
| 151 |
|
---|
| 152 | irq_spinlock_lock(&TASK->answerbox.lock, true);
|
---|
[55b77d9] | 153 |
|
---|
[feeac0d] | 154 | list_foreach(TASK->answerbox.dispatched_calls, ab_link, call_t, call) {
|
---|
[96b02eb9] | 155 | if ((sysarg_t) call == callid) {
|
---|
[ca687ad] | 156 | result = call;
|
---|
| 157 | break;
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
[da1bafb] | 160 |
|
---|
| 161 | irq_spinlock_unlock(&TASK->answerbox.lock, true);
|
---|
[ca687ad] | 162 | return result;
|
---|
[4e49572] | 163 | }
|
---|
| 164 |
|
---|
[3f74275] | 165 | /** Get phone from the current task by capability handle.
|
---|
[e8039a86] | 166 | *
|
---|
[3f74275] | 167 | * @param handle Phone capability handle.
|
---|
| 168 | * @param phone Place to store pointer to phone.
|
---|
[e8039a86] | 169 | *
|
---|
[05ffb41] | 170 | * @return Address of the phone kernel object.
|
---|
| 171 | * @return NULL if the capability is invalid.
|
---|
[e8039a86] | 172 | *
|
---|
| 173 | */
|
---|
[3f74275] | 174 | phone_t *phone_get(task_t *task, int handle)
|
---|
[e8039a86] | 175 | {
|
---|
[3f74275] | 176 | cap_t *cap = cap_get(task, handle, CAP_TYPE_PHONE);
|
---|
| 177 | if (!cap)
|
---|
[05ffb41] | 178 | return NULL;
|
---|
[e8039a86] | 179 |
|
---|
[3f74275] | 180 | return &cap->phone;
|
---|
[e8039a86] | 181 | }
|
---|
| 182 |
|
---|
[3f74275] | 183 | phone_t *phone_get_current(int handle)
|
---|
[4e49572] | 184 | {
|
---|
[3f74275] | 185 | return phone_get(TASK, handle);
|
---|
[05ffb41] | 186 | }
|
---|
[239acce] | 187 |
|
---|
[3f74275] | 188 | static bool phone_can_reclaim(cap_t *cap)
|
---|
[05ffb41] | 189 | {
|
---|
[3f74275] | 190 | assert(cap->type == CAP_TYPE_PHONE);
|
---|
[05ffb41] | 191 |
|
---|
[3f74275] | 192 | return (cap->phone.state == IPC_PHONE_HUNGUP) &&
|
---|
| 193 | (atomic_get(&cap->phone.active_calls) == 0);
|
---|
[4e49572] | 194 | }
|
---|
| 195 |
|
---|
[05ffb41] | 196 | /** Allocate new phone in the specified task.
|
---|
[8b243f2] | 197 | *
|
---|
[05ffb41] | 198 | * @param task Task for which to allocate a new phone.
|
---|
[da1bafb] | 199 | *
|
---|
[3f74275] | 200 | * @return New phone capability handle.
|
---|
[e9d15d9] | 201 | * @return Negative error code if a new capability cannot be allocated.
|
---|
[8b243f2] | 202 | */
|
---|
[05ffb41] | 203 | int phone_alloc(task_t *task)
|
---|
[7c7aae16] | 204 | {
|
---|
[3f74275] | 205 | int handle = cap_alloc(task);
|
---|
| 206 | if (handle >= 0) {
|
---|
[05ffb41] | 207 | irq_spinlock_lock(&task->lock, true);
|
---|
[88db88d] | 208 | cap_t *cap = cap_get(task, handle, CAP_TYPE_ALLOCATED);
|
---|
[3f74275] | 209 | ipc_phone_init(&cap->phone, task);
|
---|
| 210 | cap->type = CAP_TYPE_PHONE;
|
---|
| 211 | cap->can_reclaim = phone_can_reclaim;
|
---|
| 212 | cap->phone.state = IPC_PHONE_CONNECTING;
|
---|
[05ffb41] | 213 | irq_spinlock_unlock(&task->lock, true);
|
---|
| 214 | }
|
---|
[7c7aae16] | 215 |
|
---|
[3f74275] | 216 | return handle;
|
---|
[7c7aae16] | 217 | }
|
---|
| 218 |
|
---|
[8b243f2] | 219 | /** Free slot from a disconnected phone.
|
---|
| 220 | *
|
---|
| 221 | * All already sent messages will be correctly processed.
|
---|
[fbcfd458] | 222 | *
|
---|
[3f74275] | 223 | * @param handle Phone capability handle of the phone to be freed.
|
---|
[da1bafb] | 224 | *
|
---|
[fbcfd458] | 225 | */
|
---|
[3f74275] | 226 | void phone_dealloc(int handle)
|
---|
[4e49572] | 227 | {
|
---|
[3f74275] | 228 | phone_t *phone = phone_get_current(handle);
|
---|
[05ffb41] | 229 |
|
---|
| 230 | assert(phone);
|
---|
| 231 | assert(phone->state == IPC_PHONE_CONNECTING);
|
---|
| 232 |
|
---|
[3f74275] | 233 | cap_free(TASK, handle);
|
---|
[4e49572] | 234 | }
|
---|
| 235 |
|
---|
[8b243f2] | 236 | /** Connect phone to a given answerbox.
|
---|
[ba81cab] | 237 | *
|
---|
[3f74275] | 238 | * @param handle Capability handle of the phone to be connected.
|
---|
| 239 | * @param box Answerbox to which to connect the phone.
|
---|
| 240 | * @return True if the phone was connected, false otherwise.
|
---|
[ba81cab] | 241 | *
|
---|
[05ffb41] | 242 | * The procedure _enforces_ that the user first marks the phone busy (e.g. via
|
---|
| 243 | * phone_alloc) and then connects the phone, otherwise race condition may
|
---|
| 244 | * appear.
|
---|
[da1bafb] | 245 | *
|
---|
[ba81cab] | 246 | */
|
---|
[3f74275] | 247 | bool phone_connect(int handle, answerbox_t *box)
|
---|
[4e49572] | 248 | {
|
---|
[3f74275] | 249 | phone_t *phone = phone_get_current(handle);
|
---|
[4e49572] | 250 |
|
---|
[05ffb41] | 251 | assert(phone);
|
---|
[63e27ef] | 252 | assert(phone->state == IPC_PHONE_CONNECTING);
|
---|
[05ffb41] | 253 |
|
---|
[c33f39f] | 254 | return ipc_phone_connect(phone, box);
|
---|
[4e49572] | 255 | }
|
---|
[b45c443] | 256 |
|
---|
[cc73a8a1] | 257 | /** @}
|
---|
[b45c443] | 258 | */
|
---|