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