[6d9c49a] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[6d9c49a] | 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 |
|
---|
[6d9c49a] | 35 | /* Lock ordering
|
---|
| 36 | *
|
---|
[8b243f2] | 37 | * First the answerbox, then the phone.
|
---|
[6d9c49a] | 38 | */
|
---|
| 39 |
|
---|
[ff48a15] | 40 | #include <synch/synch.h>
|
---|
[2ba7810] | 41 | #include <synch/spinlock.h>
|
---|
[ff48a15] | 42 | #include <synch/mutex.h>
|
---|
[2ba7810] | 43 | #include <synch/waitq.h>
|
---|
[bd5a663] | 44 | #include <synch/synch.h>
|
---|
[6d9c49a] | 45 | #include <ipc/ipc.h>
|
---|
| 46 | #include <errno.h>
|
---|
| 47 | #include <mm/slab.h>
|
---|
| 48 | #include <arch.h>
|
---|
| 49 | #include <proc/task.h>
|
---|
| 50 | #include <memstr.h>
|
---|
| 51 | #include <debug.h>
|
---|
| 52 |
|
---|
| 53 | #include <print.h>
|
---|
| 54 | #include <proc/thread.h>
|
---|
[5626277] | 55 | #include <arch/interrupt.h>
|
---|
[162f919] | 56 | #include <ipc/irq.h>
|
---|
[6d9c49a] | 57 |
|
---|
[8b243f2] | 58 | /** Open channel that is assigned automatically to new tasks */
|
---|
[e74cb73] | 59 | answerbox_t *ipc_phone_0 = NULL;
|
---|
[6d9c49a] | 60 |
|
---|
| 61 | static slab_cache_t *ipc_call_slab;
|
---|
| 62 |
|
---|
[8b243f2] | 63 | /** Initialize a call structure.
|
---|
| 64 | *
|
---|
| 65 | * @param call Call structure to be initialized.
|
---|
| 66 | */
|
---|
[ba81cab] | 67 | static void _ipc_call_init(call_t *call)
|
---|
| 68 | {
|
---|
[e32e092] | 69 | memsetb(call, sizeof(*call), 0);
|
---|
[ba81cab] | 70 | call->callerbox = &TASK->answerbox;
|
---|
| 71 | call->sender = TASK;
|
---|
[7918fce] | 72 | call->buffer = NULL;
|
---|
[ba81cab] | 73 | }
|
---|
| 74 |
|
---|
[8b243f2] | 75 | /** Allocate and initialize a call structure.
|
---|
[6d9c49a] | 76 | *
|
---|
[8b243f2] | 77 | * The call is initialized, so that the reply will be directed to
|
---|
| 78 | * TASK->answerbox.
|
---|
| 79 | *
|
---|
| 80 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
|
---|
[5626277] | 81 | *
|
---|
[8b243f2] | 82 | * @return If flags permit it, return NULL, or initialized kernel
|
---|
| 83 | * call structure.
|
---|
[6d9c49a] | 84 | */
|
---|
[8b243f2] | 85 | call_t *ipc_call_alloc(int flags)
|
---|
[6d9c49a] | 86 | {
|
---|
| 87 | call_t *call;
|
---|
| 88 |
|
---|
[5626277] | 89 | call = slab_alloc(ipc_call_slab, flags);
|
---|
[69eac4aa] | 90 | if (call)
|
---|
| 91 | _ipc_call_init(call);
|
---|
[6d9c49a] | 92 |
|
---|
| 93 | return call;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[8b243f2] | 96 | /** Initialize a statically allocated call structure.
|
---|
| 97 | *
|
---|
| 98 | * @param call Statically allocated kernel call structure to be
|
---|
| 99 | * initialized.
|
---|
| 100 | */
|
---|
[ba81cab] | 101 | void ipc_call_static_init(call_t *call)
|
---|
[e74cb73] | 102 | {
|
---|
[ba81cab] | 103 | _ipc_call_init(call);
|
---|
| 104 | call->flags |= IPC_CALL_STATIC_ALLOC;
|
---|
[e74cb73] | 105 | }
|
---|
| 106 |
|
---|
[bd72c3e9] | 107 | /** Deallocate a call structure.
|
---|
[8b243f2] | 108 | *
|
---|
| 109 | * @param call Call structure to be freed.
|
---|
| 110 | */
|
---|
[6d9c49a] | 111 | void ipc_call_free(call_t *call)
|
---|
| 112 | {
|
---|
[8b243f2] | 113 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
[7918fce] | 114 | /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
|
---|
| 115 | if (call->buffer)
|
---|
| 116 | free(call->buffer);
|
---|
[6d9c49a] | 117 | slab_free(ipc_call_slab, call);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[8b243f2] | 120 | /** Initialize an answerbox structure.
|
---|
| 121 | *
|
---|
| 122 | * @param box Answerbox structure to be initialized.
|
---|
[12ab886] | 123 | * @param task Task to which the answerbox belongs.
|
---|
[6d9c49a] | 124 | */
|
---|
[12ab886] | 125 | void ipc_answerbox_init(answerbox_t *box, task_t *task)
|
---|
[6d9c49a] | 126 | {
|
---|
[2ba7810] | 127 | spinlock_initialize(&box->lock, "ipc_box_lock");
|
---|
[5626277] | 128 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
|
---|
[2ba7810] | 129 | waitq_initialize(&box->wq);
|
---|
[6d9c49a] | 130 | list_initialize(&box->connected_phones);
|
---|
| 131 | list_initialize(&box->calls);
|
---|
| 132 | list_initialize(&box->dispatched_calls);
|
---|
| 133 | list_initialize(&box->answers);
|
---|
[5626277] | 134 | list_initialize(&box->irq_notifs);
|
---|
[b14e35f2] | 135 | list_initialize(&box->irq_head);
|
---|
[12ab886] | 136 | box->task = task;
|
---|
[6d9c49a] | 137 | }
|
---|
| 138 |
|
---|
[8b243f2] | 139 | /** Connect a phone to an answerbox.
|
---|
| 140 | *
|
---|
| 141 | * @param phone Initialized phone structure.
|
---|
| 142 | * @param box Initialized answerbox structure.
|
---|
| 143 | */
|
---|
[2ba7810] | 144 | void ipc_phone_connect(phone_t *phone, answerbox_t *box)
|
---|
[6d9c49a] | 145 | {
|
---|
[ff48a15] | 146 | mutex_lock(&phone->lock);
|
---|
[ba81cab] | 147 |
|
---|
[eb3d379] | 148 | phone->state = IPC_PHONE_CONNECTED;
|
---|
[6d9c49a] | 149 | phone->callee = box;
|
---|
[e74cb73] | 150 |
|
---|
[2ba7810] | 151 | spinlock_lock(&box->lock);
|
---|
[c4e4507] | 152 | list_append(&phone->link, &box->connected_phones);
|
---|
[2ba7810] | 153 | spinlock_unlock(&box->lock);
|
---|
[ba81cab] | 154 |
|
---|
[ff48a15] | 155 | mutex_unlock(&phone->lock);
|
---|
[2ba7810] | 156 | }
|
---|
| 157 |
|
---|
[8b243f2] | 158 | /** Initialize a phone structure.
|
---|
| 159 | *
|
---|
| 160 | * @param phone Phone structure to be initialized.
|
---|
[2ba7810] | 161 | */
|
---|
| 162 | void ipc_phone_init(phone_t *phone)
|
---|
| 163 | {
|
---|
[08a19ba] | 164 | mutex_initialize(&phone->lock, MUTEX_PASSIVE);
|
---|
[2ba7810] | 165 | phone->callee = NULL;
|
---|
[eb3d379] | 166 | phone->state = IPC_PHONE_FREE;
|
---|
[9f22213] | 167 | atomic_set(&phone->active_calls, 0);
|
---|
[6d9c49a] | 168 | }
|
---|
| 169 |
|
---|
[8b243f2] | 170 | /** Helper function to facilitate synchronous calls.
|
---|
| 171 | *
|
---|
| 172 | * @param phone Destination kernel phone structure.
|
---|
| 173 | * @param request Call structure with request.
|
---|
[79872cd] | 174 | *
|
---|
| 175 | * @return EOK on success or EINTR if the sleep was interrupted.
|
---|
[8b243f2] | 176 | */
|
---|
[79872cd] | 177 | int ipc_call_sync(phone_t *phone, call_t *request)
|
---|
[631ca4d] | 178 | {
|
---|
| 179 | answerbox_t sync_box;
|
---|
| 180 |
|
---|
[12ab886] | 181 | ipc_answerbox_init(&sync_box, TASK);
|
---|
[631ca4d] | 182 |
|
---|
[8b243f2] | 183 | /* We will receive data in a special box. */
|
---|
[631ca4d] | 184 | request->callerbox = &sync_box;
|
---|
| 185 |
|
---|
| 186 | ipc_call(phone, request);
|
---|
[79872cd] | 187 | if (!ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT,
|
---|
| 188 | SYNCH_FLAGS_INTERRUPTIBLE))
|
---|
| 189 | return EINTR;
|
---|
| 190 | return EOK;
|
---|
[631ca4d] | 191 | }
|
---|
[6d9c49a] | 192 |
|
---|
[8b243f2] | 193 | /** Answer a message which was not dispatched and is not listed in any queue.
|
---|
| 194 | *
|
---|
| 195 | * @param call Call structure to be answered.
|
---|
[ba81cab] | 196 | */
|
---|
| 197 | static void _ipc_answer_free_call(call_t *call)
|
---|
| 198 | {
|
---|
| 199 | answerbox_t *callerbox = call->callerbox;
|
---|
| 200 |
|
---|
| 201 | call->flags |= IPC_CALL_ANSWERED;
|
---|
| 202 |
|
---|
[74965d2] | 203 | if (call->flags & IPC_CALL_FORWARDED) {
|
---|
[27526e87] | 204 | if (call->caller_phone) {
|
---|
[74965d2] | 205 | /* Demasquerade the caller phone. */
|
---|
[27526e87] | 206 | call->data.phone = call->caller_phone;
|
---|
[74965d2] | 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[ba81cab] | 210 | spinlock_lock(&callerbox->lock);
|
---|
[c4e4507] | 211 | list_append(&call->link, &callerbox->answers);
|
---|
[ba81cab] | 212 | spinlock_unlock(&callerbox->lock);
|
---|
[5c8ba05] | 213 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
|
---|
[ba81cab] | 214 | }
|
---|
| 215 |
|
---|
[8b243f2] | 216 | /** Answer a message which is in a callee queue.
|
---|
[ba81cab] | 217 | *
|
---|
[8b243f2] | 218 | * @param box Answerbox that is answering the message.
|
---|
| 219 | * @param call Modified request that is being sent back.
|
---|
[ba81cab] | 220 | */
|
---|
| 221 | void ipc_answer(answerbox_t *box, call_t *call)
|
---|
| 222 | {
|
---|
| 223 | /* Remove from active box */
|
---|
| 224 | spinlock_lock(&box->lock);
|
---|
[c4e4507] | 225 | list_remove(&call->link);
|
---|
[ba81cab] | 226 | spinlock_unlock(&box->lock);
|
---|
| 227 | /* Send back answer */
|
---|
| 228 | _ipc_answer_free_call(call);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[8b243f2] | 231 | /** Simulate sending back a message.
|
---|
[7c7aae16] | 232 | *
|
---|
| 233 | * Most errors are better handled by forming a normal backward
|
---|
| 234 | * message and sending it as a normal answer.
|
---|
[8b243f2] | 235 | *
|
---|
| 236 | * @param phone Phone structure the call should appear to come from.
|
---|
| 237 | * @param call Call structure to be answered.
|
---|
| 238 | * @param err Return value to be used for the answer.
|
---|
[7c7aae16] | 239 | */
|
---|
[7f1c620] | 240 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
|
---|
[7c7aae16] | 241 | {
|
---|
| 242 | call->data.phone = phone;
|
---|
| 243 | atomic_inc(&phone->active_calls);
|
---|
[eb3d379] | 244 | IPC_SET_RETVAL(call->data, err);
|
---|
[7c7aae16] | 245 | _ipc_answer_free_call(call);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[8b243f2] | 248 | /** Unsafe unchecking version of ipc_call.
|
---|
| 249 | *
|
---|
| 250 | * @param phone Phone structure the call comes from.
|
---|
| 251 | * @param box Destination answerbox structure.
|
---|
[7c5bcc0] | 252 | * @param call Call structure with request.
|
---|
[8b243f2] | 253 | */
|
---|
[fbcfd458] | 254 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
|
---|
| 255 | {
|
---|
[8b243f2] | 256 | if (!(call->flags & IPC_CALL_FORWARDED)) {
|
---|
[9f22213] | 257 | atomic_inc(&phone->active_calls);
|
---|
| 258 | call->data.phone = phone;
|
---|
| 259 | }
|
---|
[fbcfd458] | 260 |
|
---|
| 261 | spinlock_lock(&box->lock);
|
---|
[c4e4507] | 262 | list_append(&call->link, &box->calls);
|
---|
[fbcfd458] | 263 | spinlock_unlock(&box->lock);
|
---|
[5c8ba05] | 264 | waitq_wakeup(&box->wq, WAKEUP_FIRST);
|
---|
[fbcfd458] | 265 | }
|
---|
| 266 |
|
---|
[8b243f2] | 267 | /** Send an asynchronous request using a phone to an answerbox.
|
---|
| 268 | *
|
---|
| 269 | * @param phone Phone structure the call comes from and which is
|
---|
| 270 | * connected to the destination answerbox.
|
---|
| 271 | * @param call Call structure with request.
|
---|
[6d9c49a] | 272 | *
|
---|
[8b243f2] | 273 | * @return Return 0 on success, ENOENT on error.
|
---|
[6d9c49a] | 274 | */
|
---|
[9f22213] | 275 | int ipc_call(phone_t *phone, call_t *call)
|
---|
[6d9c49a] | 276 | {
|
---|
[ba81cab] | 277 | answerbox_t *box;
|
---|
[6d9c49a] | 278 |
|
---|
[ff48a15] | 279 | mutex_lock(&phone->lock);
|
---|
[eb3d379] | 280 | if (phone->state != IPC_PHONE_CONNECTED) {
|
---|
[ff48a15] | 281 | mutex_unlock(&phone->lock);
|
---|
[9f22213] | 282 | if (call->flags & IPC_CALL_FORWARDED) {
|
---|
| 283 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
[7c7aae16] | 284 | _ipc_answer_free_call(call);
|
---|
[eb3d379] | 285 | } else {
|
---|
| 286 | if (phone->state == IPC_PHONE_HUNGUP)
|
---|
[7c7aae16] | 287 | ipc_backsend_err(phone, call, EHANGUP);
|
---|
[9f22213] | 288 | else
|
---|
[7c7aae16] | 289 | ipc_backsend_err(phone, call, ENOENT);
|
---|
[9f22213] | 290 | }
|
---|
| 291 | return ENOENT;
|
---|
[ba81cab] | 292 | }
|
---|
[eb3d379] | 293 | box = phone->callee;
|
---|
[fbcfd458] | 294 | _ipc_call(phone, box, call);
|
---|
| 295 |
|
---|
[ff48a15] | 296 | mutex_unlock(&phone->lock);
|
---|
[9f22213] | 297 | return 0;
|
---|
[fbcfd458] | 298 | }
|
---|
| 299 |
|
---|
[8b243f2] | 300 | /** Disconnect phone from answerbox.
|
---|
[fbcfd458] | 301 | *
|
---|
[8b243f2] | 302 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done
|
---|
[eb3d379] | 303 | * lazily later.
|
---|
[fbcfd458] | 304 | *
|
---|
[8b243f2] | 305 | * @param phone Phone structure to be hung up.
|
---|
[eb3d379] | 306 | *
|
---|
[8b243f2] | 307 | * @return Return 0 if the phone is disconnected.
|
---|
| 308 | * Return -1 if the phone was already disconnected.
|
---|
[fbcfd458] | 309 | */
|
---|
[d8f7362] | 310 | int ipc_phone_hangup(phone_t *phone)
|
---|
[fbcfd458] | 311 | {
|
---|
| 312 | answerbox_t *box;
|
---|
| 313 | call_t *call;
|
---|
| 314 |
|
---|
[ff48a15] | 315 | mutex_lock(&phone->lock);
|
---|
[7918fce] | 316 | if (phone->state == IPC_PHONE_FREE ||
|
---|
| 317 | phone->state == IPC_PHONE_HUNGUP ||
|
---|
[8b243f2] | 318 | phone->state == IPC_PHONE_CONNECTING) {
|
---|
[ff48a15] | 319 | mutex_unlock(&phone->lock);
|
---|
[eb3d379] | 320 | return -1;
|
---|
[fbcfd458] | 321 | }
|
---|
[eb3d379] | 322 | box = phone->callee;
|
---|
| 323 | if (phone->state != IPC_PHONE_SLAMMED) {
|
---|
| 324 | /* Remove myself from answerbox */
|
---|
| 325 | spinlock_lock(&box->lock);
|
---|
[c4e4507] | 326 | list_remove(&phone->link);
|
---|
[eb3d379] | 327 | spinlock_unlock(&box->lock);
|
---|
[6d9c49a] | 328 |
|
---|
[eb3d379] | 329 | if (phone->state != IPC_PHONE_SLAMMED) {
|
---|
| 330 | call = ipc_call_alloc(0);
|
---|
| 331 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
|
---|
| 332 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
---|
| 333 | _ipc_call(phone, box, call);
|
---|
| 334 | }
|
---|
| 335 | }
|
---|
[fbcfd458] | 336 |
|
---|
[eb3d379] | 337 | phone->state = IPC_PHONE_HUNGUP;
|
---|
[ff48a15] | 338 | mutex_unlock(&phone->lock);
|
---|
[fbcfd458] | 339 |
|
---|
| 340 | return 0;
|
---|
[2ba7810] | 341 | }
|
---|
| 342 |
|
---|
[8b243f2] | 343 | /** Forwards call from one answerbox to another one.
|
---|
[2ba7810] | 344 | *
|
---|
[8b243f2] | 345 | * @param call Call structure to be redirected.
|
---|
| 346 | * @param newphone Phone structure to target answerbox.
|
---|
| 347 | * @param oldbox Old answerbox structure.
|
---|
[d40a8ff] | 348 | * @param mode Flags that specify mode of the forward operation.
|
---|
[8b243f2] | 349 | *
|
---|
| 350 | * @return Return 0 if forwarding succeeded or an error code if
|
---|
| 351 | * there was error.
|
---|
[9f22213] | 352 | *
|
---|
[8b243f2] | 353 | * The return value serves only as an information for the forwarder,
|
---|
| 354 | * the original caller is notified automatically with EFORWARD.
|
---|
[2ba7810] | 355 | */
|
---|
[d40a8ff] | 356 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
|
---|
[2ba7810] | 357 | {
|
---|
| 358 | spinlock_lock(&oldbox->lock);
|
---|
[c4e4507] | 359 | list_remove(&call->link);
|
---|
[2ba7810] | 360 | spinlock_unlock(&oldbox->lock);
|
---|
| 361 |
|
---|
[645d9ed2] | 362 | if (mode & IPC_FF_ROUTE_FROM_ME) {
|
---|
[27526e87] | 363 | if (!call->caller_phone)
|
---|
| 364 | call->caller_phone = call->data.phone;
|
---|
[9201f47] | 365 | call->data.phone = newphone;
|
---|
[645d9ed2] | 366 | }
|
---|
[9201f47] | 367 |
|
---|
[9f22213] | 368 | return ipc_call(newphone, call);
|
---|
[6d9c49a] | 369 | }
|
---|
| 370 |
|
---|
| 371 |
|
---|
[8b243f2] | 372 | /** Wait for a phone call.
|
---|
[6d9c49a] | 373 | *
|
---|
[8b243f2] | 374 | * @param box Answerbox expecting the call.
|
---|
| 375 | * @param usec Timeout in microseconds. See documentation for
|
---|
| 376 | * waitq_sleep_timeout() for decription of its special
|
---|
| 377 | * meaning.
|
---|
| 378 | * @param flags Select mode of sleep operation. See documentation for
|
---|
| 379 | * waitq_sleep_timeout() for description of its special
|
---|
| 380 | * meaning.
|
---|
| 381 | * @return Recived call structure or NULL.
|
---|
| 382 | *
|
---|
| 383 | * To distinguish between a call and an answer, have a look at call->flags.
|
---|
[6d9c49a] | 384 | */
|
---|
[8b243f2] | 385 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
|
---|
[6d9c49a] | 386 | {
|
---|
| 387 | call_t *request;
|
---|
[5626277] | 388 | ipl_t ipl;
|
---|
[bd5a663] | 389 | int rc;
|
---|
[6d9c49a] | 390 |
|
---|
[bd5a663] | 391 | restart:
|
---|
[116d1ef4] | 392 | rc = waitq_sleep_timeout(&box->wq, usec, flags);
|
---|
[bd5a663] | 393 | if (SYNCH_FAILED(rc))
|
---|
| 394 | return NULL;
|
---|
[fbcfd458] | 395 |
|
---|
[2ba7810] | 396 | spinlock_lock(&box->lock);
|
---|
[5626277] | 397 | if (!list_empty(&box->irq_notifs)) {
|
---|
| 398 | ipl = interrupts_disable();
|
---|
| 399 | spinlock_lock(&box->irq_lock);
|
---|
| 400 |
|
---|
[c4e4507] | 401 | request = list_get_instance(box->irq_notifs.next, call_t, link);
|
---|
| 402 | list_remove(&request->link);
|
---|
[5626277] | 403 |
|
---|
| 404 | spinlock_unlock(&box->irq_lock);
|
---|
| 405 | interrupts_restore(ipl);
|
---|
| 406 | } else if (!list_empty(&box->answers)) {
|
---|
[fbcfd458] | 407 | /* Handle asynchronous answers */
|
---|
[c4e4507] | 408 | request = list_get_instance(box->answers.next, call_t, link);
|
---|
| 409 | list_remove(&request->link);
|
---|
[74965d2] | 410 | atomic_dec(&request->data.phone->active_calls);
|
---|
[fbcfd458] | 411 | } else if (!list_empty(&box->calls)) {
|
---|
| 412 | /* Handle requests */
|
---|
[c4e4507] | 413 | request = list_get_instance(box->calls.next, call_t, link);
|
---|
| 414 | list_remove(&request->link);
|
---|
[fbcfd458] | 415 | /* Append request to dispatch queue */
|
---|
[c4e4507] | 416 | list_append(&request->link, &box->dispatched_calls);
|
---|
[fbcfd458] | 417 | } else {
|
---|
[874621f] | 418 | /* This can happen regularly after ipc_cleanup */
|
---|
[fbcfd458] | 419 | spinlock_unlock(&box->lock);
|
---|
| 420 | goto restart;
|
---|
[6d9c49a] | 421 | }
|
---|
[2ba7810] | 422 | spinlock_unlock(&box->lock);
|
---|
[6d9c49a] | 423 | return request;
|
---|
| 424 | }
|
---|
| 425 |
|
---|
[8b243f2] | 426 | /** Answer all calls from list with EHANGUP answer.
|
---|
| 427 | *
|
---|
| 428 | * @param lst Head of the list to be cleaned up.
|
---|
| 429 | */
|
---|
[ca687ad] | 430 | static void ipc_cleanup_call_list(link_t *lst)
|
---|
| 431 | {
|
---|
| 432 | call_t *call;
|
---|
| 433 |
|
---|
| 434 | while (!list_empty(lst)) {
|
---|
[c4e4507] | 435 | call = list_get_instance(lst->next, call_t, link);
|
---|
[a55d5f9f] | 436 | if (call->buffer)
|
---|
| 437 | free(call->buffer);
|
---|
[c4e4507] | 438 | list_remove(&call->link);
|
---|
[ca687ad] | 439 |
|
---|
| 440 | IPC_SET_RETVAL(call->data, EHANGUP);
|
---|
| 441 | _ipc_answer_free_call(call);
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[8b243f2] | 445 | /** Cleans up all IPC communication of the current task.
|
---|
[4e49572] | 446 | *
|
---|
[214c5a0] | 447 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
|
---|
[8b243f2] | 448 | * have to change it as well if you want to cleanup other tasks than TASK.
|
---|
[4e49572] | 449 | */
|
---|
[214c5a0] | 450 | void ipc_cleanup(void)
|
---|
[4e49572] | 451 | {
|
---|
[9f22213] | 452 | int i;
|
---|
[ca687ad] | 453 | call_t *call;
|
---|
| 454 | phone_t *phone;
|
---|
[31d8e10] | 455 | DEADLOCK_PROBE_INIT(p_phonelck);
|
---|
[214c5a0] | 456 |
|
---|
[9f22213] | 457 | /* Disconnect all our phones ('ipc_phone_hangup') */
|
---|
[8b243f2] | 458 | for (i = 0; i < IPC_MAX_PHONES; i++)
|
---|
[d8f7362] | 459 | ipc_phone_hangup(&TASK->phones[i]);
|
---|
[9f22213] | 460 |
|
---|
[5626277] | 461 | /* Disconnect all connected irqs */
|
---|
[214c5a0] | 462 | ipc_irq_cleanup(&TASK->answerbox);
|
---|
[5626277] | 463 |
|
---|
[ca687ad] | 464 | /* Disconnect all phones connected to our answerbox */
|
---|
| 465 | restart_phones:
|
---|
[214c5a0] | 466 | spinlock_lock(&TASK->answerbox.lock);
|
---|
| 467 | while (!list_empty(&TASK->answerbox.connected_phones)) {
|
---|
| 468 | phone = list_get_instance(TASK->answerbox.connected_phones.next,
|
---|
[31d8e10] | 469 | phone_t, link);
|
---|
[ff48a15] | 470 | if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
|
---|
[214c5a0] | 471 | spinlock_unlock(&TASK->answerbox.lock);
|
---|
[31d8e10] | 472 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
|
---|
[ca687ad] | 473 | goto restart_phones;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | /* Disconnect phone */
|
---|
[eb3d379] | 477 | ASSERT(phone->state == IPC_PHONE_CONNECTED);
|
---|
| 478 | phone->state = IPC_PHONE_SLAMMED;
|
---|
[c4e4507] | 479 | list_remove(&phone->link);
|
---|
[ca687ad] | 480 |
|
---|
[ff48a15] | 481 | mutex_unlock(&phone->lock);
|
---|
[ca687ad] | 482 | }
|
---|
[9f22213] | 483 |
|
---|
| 484 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */
|
---|
[214c5a0] | 485 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
|
---|
| 486 | ipc_cleanup_call_list(&TASK->answerbox.calls);
|
---|
| 487 | spinlock_unlock(&TASK->answerbox.lock);
|
---|
[4e49572] | 488 |
|
---|
[9f22213] | 489 | /* Wait for all async answers to arrive */
|
---|
[eb3d379] | 490 | while (1) {
|
---|
| 491 | /* Go through all phones, until all are FREE... */
|
---|
| 492 | /* Locking not needed, no one else should modify
|
---|
| 493 | * it, when we are in cleanup */
|
---|
[8b243f2] | 494 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
| 495 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
|
---|
[214c5a0] | 496 | atomic_get(&TASK->phones[i].active_calls) == 0)
|
---|
| 497 | TASK->phones[i].state = IPC_PHONE_FREE;
|
---|
| 498 |
|
---|
[d8f7362] | 499 | /* Just for sure, we might have had some
|
---|
| 500 | * IPC_PHONE_CONNECTING phones */
|
---|
[214c5a0] | 501 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
|
---|
[d8f7362] | 502 | ipc_phone_hangup(&TASK->phones[i]);
|
---|
| 503 | /* If the hangup succeeded, it has sent a HANGUP
|
---|
| 504 | * message, the IPC is now in HUNGUP state, we
|
---|
| 505 | * wait for the reply to come */
|
---|
[214c5a0] | 506 |
|
---|
| 507 | if (TASK->phones[i].state != IPC_PHONE_FREE)
|
---|
[eb3d379] | 508 | break;
|
---|
| 509 | }
|
---|
| 510 | /* Voila, got into cleanup */
|
---|
| 511 | if (i == IPC_MAX_PHONES)
|
---|
| 512 | break;
|
---|
| 513 |
|
---|
[8b243f2] | 514 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
|
---|
| 515 | SYNCH_FLAGS_NONE);
|
---|
| 516 | ASSERT((call->flags & IPC_CALL_ANSWERED) ||
|
---|
| 517 | (call->flags & IPC_CALL_NOTIF));
|
---|
| 518 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
[ca687ad] | 519 |
|
---|
[214c5a0] | 520 | atomic_dec(&TASK->active_calls);
|
---|
[ca687ad] | 521 | ipc_call_free(call);
|
---|
| 522 | }
|
---|
[4e49572] | 523 | }
|
---|
[5626277] | 524 |
|
---|
| 525 |
|
---|
[2b017ba] | 526 | /** Initilize IPC subsystem */
|
---|
[5626277] | 527 | void ipc_init(void)
|
---|
| 528 | {
|
---|
[8b243f2] | 529 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
|
---|
| 530 | NULL, 0);
|
---|
[5626277] | 531 | }
|
---|
| 532 |
|
---|
[c4e4507] | 533 |
|
---|
[8b243f2] | 534 | /** List answerbox contents.
|
---|
| 535 | *
|
---|
| 536 | * @param taskid Task ID.
|
---|
| 537 | */
|
---|
[c4e4507] | 538 | void ipc_print_task(task_id_t taskid)
|
---|
| 539 | {
|
---|
| 540 | task_t *task;
|
---|
| 541 | int i;
|
---|
| 542 | call_t *call;
|
---|
| 543 | link_t *tmp;
|
---|
| 544 |
|
---|
| 545 | spinlock_lock(&tasks_lock);
|
---|
| 546 | task = task_find_by_id(taskid);
|
---|
| 547 | if (task)
|
---|
| 548 | spinlock_lock(&task->lock);
|
---|
| 549 | spinlock_unlock(&tasks_lock);
|
---|
| 550 | if (!task)
|
---|
| 551 | return;
|
---|
| 552 |
|
---|
| 553 | /* Print opened phones & details */
|
---|
| 554 | printf("PHONE:\n");
|
---|
[8b243f2] | 555 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
[ff48a15] | 556 | if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
|
---|
| 557 | printf("%d: mutex busy\n", i);
|
---|
| 558 | continue;
|
---|
| 559 | }
|
---|
[c4e4507] | 560 | if (task->phones[i].state != IPC_PHONE_FREE) {
|
---|
[8b243f2] | 561 | printf("%d: ", i);
|
---|
[c4e4507] | 562 | switch (task->phones[i].state) {
|
---|
| 563 | case IPC_PHONE_CONNECTING:
|
---|
| 564 | printf("connecting ");
|
---|
| 565 | break;
|
---|
| 566 | case IPC_PHONE_CONNECTED:
|
---|
[fbf7b4c] | 567 | printf("connected to: %p ",
|
---|
[c4e4507] | 568 | task->phones[i].callee);
|
---|
| 569 | break;
|
---|
| 570 | case IPC_PHONE_SLAMMED:
|
---|
[fbf7b4c] | 571 | printf("slammed by: %p ",
|
---|
[c4e4507] | 572 | task->phones[i].callee);
|
---|
| 573 | break;
|
---|
| 574 | case IPC_PHONE_HUNGUP:
|
---|
[fbf7b4c] | 575 | printf("hung up - was: %p ",
|
---|
[c4e4507] | 576 | task->phones[i].callee);
|
---|
| 577 | break;
|
---|
| 578 | default:
|
---|
| 579 | break;
|
---|
| 580 | }
|
---|
[9fe962d6] | 581 | printf("active: %ld\n",
|
---|
[bd72c3e9] | 582 | atomic_get(&task->phones[i].active_calls));
|
---|
[c4e4507] | 583 | }
|
---|
[ff48a15] | 584 | mutex_unlock(&task->phones[i].lock);
|
---|
[c4e4507] | 585 | }
|
---|
| 586 |
|
---|
| 587 |
|
---|
| 588 | /* Print answerbox - calls */
|
---|
| 589 | spinlock_lock(&task->answerbox.lock);
|
---|
| 590 | printf("ABOX - CALLS:\n");
|
---|
[bd72c3e9] | 591 | for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
|
---|
[78d0da8] | 592 | tmp = tmp->next) {
|
---|
[c4e4507] | 593 | call = list_get_instance(tmp, call_t, link);
|
---|
[9fe962d6] | 594 | printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
|
---|
[e40e3007] | 595 | " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
|
---|
| 596 | " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
|
---|
| 597 | call->sender->taskid,
|
---|
[bd72c3e9] | 598 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
| 599 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
[38c706cc] | 600 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
[bd72c3e9] | 601 | call->flags);
|
---|
[c4e4507] | 602 | }
|
---|
| 603 | /* Print answerbox - calls */
|
---|
| 604 | printf("ABOX - DISPATCHED CALLS:\n");
|
---|
[9fe962d6] | 605 | for (tmp = task->answerbox.dispatched_calls.next;
|
---|
[78d0da8] | 606 | tmp != &task->answerbox.dispatched_calls;
|
---|
| 607 | tmp = tmp->next) {
|
---|
[c4e4507] | 608 | call = list_get_instance(tmp, call_t, link);
|
---|
[9fe962d6] | 609 | printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
|
---|
[e40e3007] | 610 | " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
|
---|
| 611 | " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
|
---|
| 612 | call->sender->taskid,
|
---|
[bd72c3e9] | 613 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
| 614 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
[38c706cc] | 615 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
[bd72c3e9] | 616 | call->flags);
|
---|
[c4e4507] | 617 | }
|
---|
| 618 | /* Print answerbox - calls */
|
---|
| 619 | printf("ABOX - ANSWERS:\n");
|
---|
[e40e3007] | 620 | for (tmp = task->answerbox.answers.next;
|
---|
| 621 | tmp != &task->answerbox.answers;
|
---|
[78d0da8] | 622 | tmp = tmp->next) {
|
---|
[c4e4507] | 623 | call = list_get_instance(tmp, call_t, link);
|
---|
[9fe962d6] | 624 | printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
|
---|
[e40e3007] | 625 | " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
|
---|
[38c706cc] | 626 | call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
[bd72c3e9] | 627 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
[38c706cc] | 628 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
[bd72c3e9] | 629 | call->flags);
|
---|
[c4e4507] | 630 | }
|
---|
| 631 |
|
---|
| 632 | spinlock_unlock(&task->answerbox.lock);
|
---|
| 633 | spinlock_unlock(&task->lock);
|
---|
| 634 | }
|
---|
[b45c443] | 635 |
|
---|
[cc73a8a1] | 636 | /** @}
|
---|
[b45c443] | 637 | */
|
---|