| [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 |
|
|---|
| [63e27ef] | 40 | #include <assert.h>
|
|---|
| [2ba7810] | 41 | #include <synch/spinlock.h>
|
|---|
| [ff48a15] | 42 | #include <synch/mutex.h>
|
|---|
| [2ba7810] | 43 | #include <synch/waitq.h>
|
|---|
| [6d9c49a] | 44 | #include <ipc/ipc.h>
|
|---|
| [c0699467] | 45 | #include <abi/ipc/methods.h>
|
|---|
| [e028660] | 46 | #include <ipc/kbox.h>
|
|---|
| [13a638d] | 47 | #include <ipc/event.h>
|
|---|
| [b1e6269] | 48 | #include <ipc/sysipc_ops.h>
|
|---|
| [5d3ed34] | 49 | #include <ipc/sysipc_priv.h>
|
|---|
| [6d9c49a] | 50 | #include <errno.h>
|
|---|
| 51 | #include <mm/slab.h>
|
|---|
| 52 | #include <arch.h>
|
|---|
| 53 | #include <proc/task.h>
|
|---|
| [44a7ee5] | 54 | #include <mem.h>
|
|---|
| [6d9c49a] | 55 | #include <print.h>
|
|---|
| [9a1b20c] | 56 | #include <console/console.h>
|
|---|
| [6d9c49a] | 57 | #include <proc/thread.h>
|
|---|
| [5626277] | 58 | #include <arch/interrupt.h>
|
|---|
| [162f919] | 59 | #include <ipc/irq.h>
|
|---|
| [6d9c49a] | 60 |
|
|---|
| [43e2cbc] | 61 | static void ipc_forget_call(call_t *);
|
|---|
| 62 |
|
|---|
| [8b243f2] | 63 | /** Open channel that is assigned automatically to new tasks */
|
|---|
| [e74cb73] | 64 | answerbox_t *ipc_phone_0 = NULL;
|
|---|
| [6d9c49a] | 65 |
|
|---|
| 66 | static slab_cache_t *ipc_call_slab;
|
|---|
| [c70ce74] | 67 | static slab_cache_t *ipc_answerbox_slab;
|
|---|
| [6d9c49a] | 68 |
|
|---|
| [8b243f2] | 69 | /** Initialize a call structure.
|
|---|
| 70 | *
|
|---|
| [da1bafb] | 71 | * @param call Call structure to be initialized.
|
|---|
| 72 | *
|
|---|
| [8b243f2] | 73 | */
|
|---|
| [ba81cab] | 74 | static void _ipc_call_init(call_t *call)
|
|---|
| 75 | {
|
|---|
| [e32e092] | 76 | memsetb(call, sizeof(*call), 0);
|
|---|
| [2405bb5] | 77 | spinlock_initialize(&call->forget_lock, "forget_lock");
|
|---|
| [20282ef3] | 78 | call->active = false;
|
|---|
| [2405bb5] | 79 | call->forget = false;
|
|---|
| [03a8a8e] | 80 | call->sender = NULL;
|
|---|
| [0b00599] | 81 | call->callerbox = NULL;
|
|---|
| [7918fce] | 82 | call->buffer = NULL;
|
|---|
| [ba81cab] | 83 | }
|
|---|
| 84 |
|
|---|
| [cd671c3] | 85 | void ipc_call_hold(call_t *call)
|
|---|
| 86 | {
|
|---|
| 87 | atomic_inc(&call->refcnt);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void ipc_call_release(call_t *call)
|
|---|
| 91 | {
|
|---|
| 92 | if (atomic_predec(&call->refcnt) == 0) {
|
|---|
| 93 | if (call->buffer)
|
|---|
| 94 | free(call->buffer);
|
|---|
| 95 | slab_free(ipc_call_slab, call);
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| [8b243f2] | 99 | /** Allocate and initialize a call structure.
|
|---|
| [da1bafb] | 100 | *
|
|---|
| [8b243f2] | 101 | * The call is initialized, so that the reply will be directed to
|
|---|
| 102 | * TASK->answerbox.
|
|---|
| 103 | *
|
|---|
| [da1bafb] | 104 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
|
|---|
| 105 | *
|
|---|
| 106 | * @return If flags permit it, return NULL, or initialized kernel
|
|---|
| [cd671c3] | 107 | * call structure with one reference.
|
|---|
| [5626277] | 108 | *
|
|---|
| [6d9c49a] | 109 | */
|
|---|
| [da1bafb] | 110 | call_t *ipc_call_alloc(unsigned int flags)
|
|---|
| [6d9c49a] | 111 | {
|
|---|
| [da1bafb] | 112 | call_t *call = slab_alloc(ipc_call_slab, flags);
|
|---|
| [cd671c3] | 113 | if (call) {
|
|---|
| [69eac4aa] | 114 | _ipc_call_init(call);
|
|---|
| [cd671c3] | 115 | ipc_call_hold(call);
|
|---|
| 116 | }
|
|---|
| [da1bafb] | 117 |
|
|---|
| [6d9c49a] | 118 | return call;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| [bd72c3e9] | 121 | /** Deallocate a call structure.
|
|---|
| [8b243f2] | 122 | *
|
|---|
| [da1bafb] | 123 | * @param call Call structure to be freed.
|
|---|
| 124 | *
|
|---|
| [8b243f2] | 125 | */
|
|---|
| [6d9c49a] | 126 | void ipc_call_free(call_t *call)
|
|---|
| 127 | {
|
|---|
| [cd671c3] | 128 | ipc_call_release(call);
|
|---|
| [6d9c49a] | 129 | }
|
|---|
| 130 |
|
|---|
| [8b243f2] | 131 | /** Initialize an answerbox structure.
|
|---|
| 132 | *
|
|---|
| [da1bafb] | 133 | * @param box Answerbox structure to be initialized.
|
|---|
| 134 | * @param task Task to which the answerbox belongs.
|
|---|
| 135 | *
|
|---|
| [6d9c49a] | 136 | */
|
|---|
| [12ab886] | 137 | void ipc_answerbox_init(answerbox_t *box, task_t *task)
|
|---|
| [6d9c49a] | 138 | {
|
|---|
| [da1bafb] | 139 | irq_spinlock_initialize(&box->lock, "ipc.box.lock");
|
|---|
| 140 | irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
|
|---|
| [2ba7810] | 141 | waitq_initialize(&box->wq);
|
|---|
| [6d9c49a] | 142 | list_initialize(&box->connected_phones);
|
|---|
| 143 | list_initialize(&box->calls);
|
|---|
| 144 | list_initialize(&box->dispatched_calls);
|
|---|
| 145 | list_initialize(&box->answers);
|
|---|
| [5626277] | 146 | list_initialize(&box->irq_notifs);
|
|---|
| [55b77d9] | 147 | list_initialize(&box->irq_list);
|
|---|
| [12ab886] | 148 | box->task = task;
|
|---|
| [6d9c49a] | 149 | }
|
|---|
| 150 |
|
|---|
| [8b243f2] | 151 | /** Connect a phone to an answerbox.
|
|---|
| 152 | *
|
|---|
| [da1bafb] | 153 | * @param phone Initialized phone structure.
|
|---|
| 154 | * @param box Initialized answerbox structure.
|
|---|
| [c33f39f] | 155 | * @return True if the phone was connected, false otherwise.
|
|---|
| [8b243f2] | 156 | */
|
|---|
| [c33f39f] | 157 | bool ipc_phone_connect(phone_t *phone, answerbox_t *box)
|
|---|
| [6d9c49a] | 158 | {
|
|---|
| [c33f39f] | 159 | bool active;
|
|---|
| 160 |
|
|---|
| [ff48a15] | 161 | mutex_lock(&phone->lock);
|
|---|
| [da1bafb] | 162 | irq_spinlock_lock(&box->lock, true);
|
|---|
| [c33f39f] | 163 |
|
|---|
| 164 | active = box->active;
|
|---|
| 165 | if (active) {
|
|---|
| 166 | phone->state = IPC_PHONE_CONNECTED;
|
|---|
| 167 | phone->callee = box;
|
|---|
| 168 | list_append(&phone->link, &box->connected_phones);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| [da1bafb] | 171 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| [ff48a15] | 172 | mutex_unlock(&phone->lock);
|
|---|
| [c33f39f] | 173 |
|
|---|
| 174 | return active;
|
|---|
| [2ba7810] | 175 | }
|
|---|
| 176 |
|
|---|
| [8b243f2] | 177 | /** Initialize a phone structure.
|
|---|
| 178 | *
|
|---|
| [da1bafb] | 179 | * @param phone Phone structure to be initialized.
|
|---|
| [03a8a8e] | 180 | * @param caller Owning task.
|
|---|
| [da1bafb] | 181 | *
|
|---|
| [2ba7810] | 182 | */
|
|---|
| [03a8a8e] | 183 | void ipc_phone_init(phone_t *phone, task_t *caller)
|
|---|
| [2ba7810] | 184 | {
|
|---|
| [08a19ba] | 185 | mutex_initialize(&phone->lock, MUTEX_PASSIVE);
|
|---|
| [03a8a8e] | 186 | phone->caller = caller;
|
|---|
| [2ba7810] | 187 | phone->callee = NULL;
|
|---|
| [eb3d379] | 188 | phone->state = IPC_PHONE_FREE;
|
|---|
| [9f22213] | 189 | atomic_set(&phone->active_calls, 0);
|
|---|
| [6d9c49a] | 190 | }
|
|---|
| 191 |
|
|---|
| [bc117a5] | 192 | /** Helper function to facilitate synchronous calls.
|
|---|
| 193 | *
|
|---|
| 194 | * @param phone Destination kernel phone structure.
|
|---|
| 195 | * @param request Call structure with request.
|
|---|
| 196 | *
|
|---|
| 197 | * @return EOK on success or a negative error code.
|
|---|
| 198 | *
|
|---|
| 199 | */
|
|---|
| 200 | int ipc_call_sync(phone_t *phone, call_t *request)
|
|---|
| 201 | {
|
|---|
| 202 | answerbox_t *mybox = slab_alloc(ipc_answerbox_slab, 0);
|
|---|
| 203 | ipc_answerbox_init(mybox, TASK);
|
|---|
| 204 |
|
|---|
| 205 | /* We will receive data in a special box. */
|
|---|
| 206 | request->callerbox = mybox;
|
|---|
| 207 |
|
|---|
| 208 | int rc = ipc_call(phone, request);
|
|---|
| 209 | if (rc != EOK) {
|
|---|
| 210 | slab_free(ipc_answerbox_slab, mybox);
|
|---|
| 211 | return rc;
|
|---|
| 212 | }
|
|---|
| [43e2cbc] | 213 |
|
|---|
| 214 | call_t *answer = ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
|
|---|
| 215 | SYNCH_FLAGS_INTERRUPTIBLE);
|
|---|
| 216 | if (!answer) {
|
|---|
| 217 |
|
|---|
| 218 | /*
|
|---|
| 219 | * The sleep was interrupted.
|
|---|
| 220 | *
|
|---|
| 221 | * There are two possibilities now:
|
|---|
| 222 | * 1) the call gets answered before we manage to forget it
|
|---|
| 223 | * 2) we manage to forget the call before it gets answered
|
|---|
| 224 | */
|
|---|
| 225 |
|
|---|
| 226 | spinlock_lock(&request->forget_lock);
|
|---|
| 227 | spinlock_lock(&TASK->active_calls_lock);
|
|---|
| 228 |
|
|---|
| [63e27ef] | 229 | assert(!request->forget);
|
|---|
| [43e2cbc] | 230 |
|
|---|
| 231 | bool answered = !request->active;
|
|---|
| 232 | if (!answered) {
|
|---|
| 233 | /*
|
|---|
| 234 | * The call is not yet answered and we won the race to
|
|---|
| 235 | * forget it.
|
|---|
| 236 | */
|
|---|
| 237 | ipc_forget_call(request); /* releases locks */
|
|---|
| 238 | rc = EINTR;
|
|---|
| 239 |
|
|---|
| 240 | } else {
|
|---|
| 241 | spinlock_unlock(&TASK->active_calls_lock);
|
|---|
| 242 | spinlock_unlock(&request->forget_lock);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | if (answered) {
|
|---|
| 246 | /*
|
|---|
| 247 | * The other side won the race to answer the call.
|
|---|
| 248 | * It is safe to wait for the answer uninterruptibly
|
|---|
| 249 | * now.
|
|---|
| 250 | */
|
|---|
| 251 | answer = ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
|
|---|
| 252 | SYNCH_FLAGS_NONE);
|
|---|
| 253 | }
|
|---|
| 254 | }
|
|---|
| [63e27ef] | 255 | assert(!answer || request == answer);
|
|---|
| [bc117a5] | 256 |
|
|---|
| 257 | slab_free(ipc_answerbox_slab, mybox);
|
|---|
| [43e2cbc] | 258 | return rc;
|
|---|
| [bc117a5] | 259 | }
|
|---|
| 260 |
|
|---|
| [8b243f2] | 261 | /** Answer a message which was not dispatched and is not listed in any queue.
|
|---|
| 262 | *
|
|---|
| [da1bafb] | 263 | * @param call Call structure to be answered.
|
|---|
| 264 | * @param selflocked If true, then TASK->answebox is locked.
|
|---|
| 265 | *
|
|---|
| [ba81cab] | 266 | */
|
|---|
| [f9841e69] | 267 | void _ipc_answer_free_call(call_t *call, bool selflocked)
|
|---|
| [ba81cab] | 268 | {
|
|---|
| [95319bd] | 269 | /* Count sent answer */
|
|---|
| [da1bafb] | 270 | irq_spinlock_lock(&TASK->lock, true);
|
|---|
| [a307beb] | 271 | TASK->ipc_info.answer_sent++;
|
|---|
| [da1bafb] | 272 | irq_spinlock_unlock(&TASK->lock, true);
|
|---|
| [2405bb5] | 273 |
|
|---|
| 274 | spinlock_lock(&call->forget_lock);
|
|---|
| 275 | if (call->forget) {
|
|---|
| 276 | /* This is a forgotten call and call->sender is not valid. */
|
|---|
| 277 | spinlock_unlock(&call->forget_lock);
|
|---|
| [7975433] | 278 | ipc_call_free(call);
|
|---|
| [2405bb5] | 279 | return;
|
|---|
| 280 | } else {
|
|---|
| 281 | /*
|
|---|
| [20282ef3] | 282 | * If the call is still active, i.e. it was answered
|
|---|
| 283 | * in a non-standard way, remove the call from the
|
|---|
| 284 | * sender's active call list.
|
|---|
| [2405bb5] | 285 | */
|
|---|
| [20282ef3] | 286 | if (call->active) {
|
|---|
| 287 | spinlock_lock(&call->sender->active_calls_lock);
|
|---|
| 288 | list_remove(&call->ta_link);
|
|---|
| 289 | spinlock_unlock(&call->sender->active_calls_lock);
|
|---|
| 290 | }
|
|---|
| [2405bb5] | 291 | }
|
|---|
| 292 | spinlock_unlock(&call->forget_lock);
|
|---|
| 293 |
|
|---|
| [0b00599] | 294 | answerbox_t *callerbox = call->callerbox ? call->callerbox :
|
|---|
| 295 | &call->sender->answerbox;
|
|---|
| [2405bb5] | 296 | bool do_lock = ((!selflocked) || (callerbox != &TASK->answerbox));
|
|---|
| [da1bafb] | 297 |
|
|---|
| [ba81cab] | 298 | call->flags |= IPC_CALL_ANSWERED;
|
|---|
| [da1bafb] | 299 |
|
|---|
| [ab34cc9] | 300 | call->data.task_id = TASK->taskid;
|
|---|
| [da1bafb] | 301 |
|
|---|
| [c713aa56] | 302 | if (do_lock)
|
|---|
| [da1bafb] | 303 | irq_spinlock_lock(&callerbox->lock, true);
|
|---|
| 304 |
|
|---|
| [cfaa35a] | 305 | list_append(&call->ab_link, &callerbox->answers);
|
|---|
| [da1bafb] | 306 |
|
|---|
| [c713aa56] | 307 | if (do_lock)
|
|---|
| [da1bafb] | 308 | irq_spinlock_unlock(&callerbox->lock, true);
|
|---|
| 309 |
|
|---|
| [5c8ba05] | 310 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
|
|---|
| [ba81cab] | 311 | }
|
|---|
| 312 |
|
|---|
| [8b243f2] | 313 | /** Answer a message which is in a callee queue.
|
|---|
| [ba81cab] | 314 | *
|
|---|
| [da1bafb] | 315 | * @param box Answerbox that is answering the message.
|
|---|
| 316 | * @param call Modified request that is being sent back.
|
|---|
| 317 | *
|
|---|
| [ba81cab] | 318 | */
|
|---|
| 319 | void ipc_answer(answerbox_t *box, call_t *call)
|
|---|
| 320 | {
|
|---|
| 321 | /* Remove from active box */
|
|---|
| [da1bafb] | 322 | irq_spinlock_lock(&box->lock, true);
|
|---|
| [cfaa35a] | 323 | list_remove(&call->ab_link);
|
|---|
| [da1bafb] | 324 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 325 |
|
|---|
| [ba81cab] | 326 | /* Send back answer */
|
|---|
| [c713aa56] | 327 | _ipc_answer_free_call(call, false);
|
|---|
| [ba81cab] | 328 | }
|
|---|
| 329 |
|
|---|
| [97b8ca9] | 330 | static void _ipc_call_actions_internal(phone_t *phone, call_t *call,
|
|---|
| 331 | bool preforget)
|
|---|
| [7c7aae16] | 332 | {
|
|---|
| [03a8a8e] | 333 | task_t *caller = phone->caller;
|
|---|
| 334 |
|
|---|
| [c97b086] | 335 | call->caller_phone = phone;
|
|---|
| [86939b1] | 336 |
|
|---|
| [97b8ca9] | 337 | if (preforget) {
|
|---|
| 338 | call->forget = true;
|
|---|
| 339 | } else {
|
|---|
| 340 | atomic_inc(&phone->active_calls);
|
|---|
| 341 | call->sender = caller;
|
|---|
| 342 | call->active = true;
|
|---|
| 343 | spinlock_lock(&caller->active_calls_lock);
|
|---|
| 344 | list_append(&call->ta_link, &caller->active_calls);
|
|---|
| 345 | spinlock_unlock(&caller->active_calls_lock);
|
|---|
| 346 | }
|
|---|
| [86939b1] | 347 |
|
|---|
| [c97b086] | 348 | call->data.phone = phone;
|
|---|
| [6f9c8f6] | 349 | call->data.task_id = caller->taskid;
|
|---|
| 350 | }
|
|---|
| [c97b086] | 351 |
|
|---|
| [6f9c8f6] | 352 | /** Simulate sending back a message.
|
|---|
| 353 | *
|
|---|
| 354 | * Most errors are better handled by forming a normal backward
|
|---|
| 355 | * message and sending it as a normal answer.
|
|---|
| 356 | *
|
|---|
| 357 | * @param phone Phone structure the call should appear to come from.
|
|---|
| 358 | * @param call Call structure to be answered.
|
|---|
| 359 | * @param err Return value to be used for the answer.
|
|---|
| 360 | *
|
|---|
| 361 | */
|
|---|
| 362 | void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
|
|---|
| 363 | {
|
|---|
| [97b8ca9] | 364 | _ipc_call_actions_internal(phone, call, false);
|
|---|
| [eb3d379] | 365 | IPC_SET_RETVAL(call->data, err);
|
|---|
| [c713aa56] | 366 | _ipc_answer_free_call(call, false);
|
|---|
| [7c7aae16] | 367 | }
|
|---|
| 368 |
|
|---|
| [8b243f2] | 369 | /** Unsafe unchecking version of ipc_call.
|
|---|
| 370 | *
|
|---|
| [97b8ca9] | 371 | * @param phone Phone structure the call comes from.
|
|---|
| 372 | * @param box Destination answerbox structure.
|
|---|
| 373 | * @param call Call structure with request.
|
|---|
| 374 | * @param preforget If true, the call will be delivered already forgotten.
|
|---|
| [da1bafb] | 375 | *
|
|---|
| [8b243f2] | 376 | */
|
|---|
| [97b8ca9] | 377 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call,
|
|---|
| 378 | bool preforget)
|
|---|
| [fbcfd458] | 379 | {
|
|---|
| [03a8a8e] | 380 | task_t *caller = phone->caller;
|
|---|
| 381 |
|
|---|
| [95319bd] | 382 | /* Count sent ipc call */
|
|---|
| [03a8a8e] | 383 | irq_spinlock_lock(&caller->lock, true);
|
|---|
| 384 | caller->ipc_info.call_sent++;
|
|---|
| 385 | irq_spinlock_unlock(&caller->lock, true);
|
|---|
| [da1bafb] | 386 |
|
|---|
| [6f9c8f6] | 387 | if (!(call->flags & IPC_CALL_FORWARDED))
|
|---|
| [97b8ca9] | 388 | _ipc_call_actions_internal(phone, call, preforget);
|
|---|
| [da1bafb] | 389 |
|
|---|
| 390 | irq_spinlock_lock(&box->lock, true);
|
|---|
| [cfaa35a] | 391 | list_append(&call->ab_link, &box->calls);
|
|---|
| [da1bafb] | 392 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 393 |
|
|---|
| [5c8ba05] | 394 | waitq_wakeup(&box->wq, WAKEUP_FIRST);
|
|---|
| [fbcfd458] | 395 | }
|
|---|
| 396 |
|
|---|
| [8b243f2] | 397 | /** Send an asynchronous request using a phone to an answerbox.
|
|---|
| 398 | *
|
|---|
| [da1bafb] | 399 | * @param phone Phone structure the call comes from and which is
|
|---|
| 400 | * connected to the destination answerbox.
|
|---|
| 401 | * @param call Call structure with request.
|
|---|
| 402 | *
|
|---|
| 403 | * @return Return 0 on success, ENOENT on error.
|
|---|
| [6d9c49a] | 404 | *
|
|---|
| 405 | */
|
|---|
| [9f22213] | 406 | int ipc_call(phone_t *phone, call_t *call)
|
|---|
| [6d9c49a] | 407 | {
|
|---|
| [ff48a15] | 408 | mutex_lock(&phone->lock);
|
|---|
| [eb3d379] | 409 | if (phone->state != IPC_PHONE_CONNECTED) {
|
|---|
| [ff48a15] | 410 | mutex_unlock(&phone->lock);
|
|---|
| [f9841e69] | 411 | if (!(call->flags & IPC_CALL_FORWARDED)) {
|
|---|
| [eb3d379] | 412 | if (phone->state == IPC_PHONE_HUNGUP)
|
|---|
| [7c7aae16] | 413 | ipc_backsend_err(phone, call, EHANGUP);
|
|---|
| [9f22213] | 414 | else
|
|---|
| [7c7aae16] | 415 | ipc_backsend_err(phone, call, ENOENT);
|
|---|
| [9f22213] | 416 | }
|
|---|
| [da1bafb] | 417 |
|
|---|
| [9f22213] | 418 | return ENOENT;
|
|---|
| [ba81cab] | 419 | }
|
|---|
| [da1bafb] | 420 |
|
|---|
| 421 | answerbox_t *box = phone->callee;
|
|---|
| [97b8ca9] | 422 | _ipc_call(phone, box, call, false);
|
|---|
| [fbcfd458] | 423 |
|
|---|
| [ff48a15] | 424 | mutex_unlock(&phone->lock);
|
|---|
| [9f22213] | 425 | return 0;
|
|---|
| [fbcfd458] | 426 | }
|
|---|
| 427 |
|
|---|
| [8b243f2] | 428 | /** Disconnect phone from answerbox.
|
|---|
| [fbcfd458] | 429 | *
|
|---|
| [8b243f2] | 430 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done
|
|---|
| [eb3d379] | 431 | * lazily later.
|
|---|
| [fbcfd458] | 432 | *
|
|---|
| [da1bafb] | 433 | * @param phone Phone structure to be hung up.
|
|---|
| 434 | *
|
|---|
| 435 | * @return 0 if the phone is disconnected.
|
|---|
| 436 | * @return -1 if the phone was already disconnected.
|
|---|
| 437 | *
|
|---|
| [fbcfd458] | 438 | */
|
|---|
| [d8f7362] | 439 | int ipc_phone_hangup(phone_t *phone)
|
|---|
| [fbcfd458] | 440 | {
|
|---|
| [ff48a15] | 441 | mutex_lock(&phone->lock);
|
|---|
| [7918fce] | 442 | if (phone->state == IPC_PHONE_FREE ||
|
|---|
| 443 | phone->state == IPC_PHONE_HUNGUP ||
|
|---|
| [8b243f2] | 444 | phone->state == IPC_PHONE_CONNECTING) {
|
|---|
| [ff48a15] | 445 | mutex_unlock(&phone->lock);
|
|---|
| [eb3d379] | 446 | return -1;
|
|---|
| [fbcfd458] | 447 | }
|
|---|
| [da1bafb] | 448 |
|
|---|
| 449 | answerbox_t *box = phone->callee;
|
|---|
| [eb3d379] | 450 | if (phone->state != IPC_PHONE_SLAMMED) {
|
|---|
| 451 | /* Remove myself from answerbox */
|
|---|
| [da1bafb] | 452 | irq_spinlock_lock(&box->lock, true);
|
|---|
| [c4e4507] | 453 | list_remove(&phone->link);
|
|---|
| [da1bafb] | 454 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 455 |
|
|---|
| 456 | call_t *call = ipc_call_alloc(0);
|
|---|
| [228e490] | 457 | IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
|
|---|
| [796692c] | 458 | call->request_method = IPC_M_PHONE_HUNGUP;
|
|---|
| [287e83f] | 459 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
|---|
| [97b8ca9] | 460 | _ipc_call(phone, box, call, false);
|
|---|
| [eb3d379] | 461 | }
|
|---|
| [da1bafb] | 462 |
|
|---|
| [eb3d379] | 463 | phone->state = IPC_PHONE_HUNGUP;
|
|---|
| [ff48a15] | 464 | mutex_unlock(&phone->lock);
|
|---|
| [da1bafb] | 465 |
|
|---|
| [fbcfd458] | 466 | return 0;
|
|---|
| [2ba7810] | 467 | }
|
|---|
| 468 |
|
|---|
| [8b243f2] | 469 | /** Forwards call from one answerbox to another one.
|
|---|
| [2ba7810] | 470 | *
|
|---|
| [da1bafb] | 471 | * @param call Call structure to be redirected.
|
|---|
| 472 | * @param newphone Phone structure to target answerbox.
|
|---|
| 473 | * @param oldbox Old answerbox structure.
|
|---|
| 474 | * @param mode Flags that specify mode of the forward operation.
|
|---|
| 475 | *
|
|---|
| 476 | * @return 0 if forwarding succeeded or an error code if
|
|---|
| 477 | * there was an error.
|
|---|
| [8b243f2] | 478 | *
|
|---|
| 479 | * The return value serves only as an information for the forwarder,
|
|---|
| 480 | * the original caller is notified automatically with EFORWARD.
|
|---|
| [da1bafb] | 481 | *
|
|---|
| [2ba7810] | 482 | */
|
|---|
| [da1bafb] | 483 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
|
|---|
| 484 | unsigned int mode)
|
|---|
| [2ba7810] | 485 | {
|
|---|
| [95319bd] | 486 | /* Count forwarded calls */
|
|---|
| [da1bafb] | 487 | irq_spinlock_lock(&TASK->lock, true);
|
|---|
| [a307beb] | 488 | TASK->ipc_info.forwarded++;
|
|---|
| [da1bafb] | 489 | irq_spinlock_pass(&TASK->lock, &oldbox->lock);
|
|---|
| [cfaa35a] | 490 | list_remove(&call->ab_link);
|
|---|
| [da1bafb] | 491 | irq_spinlock_unlock(&oldbox->lock, true);
|
|---|
| 492 |
|
|---|
| [645d9ed2] | 493 | if (mode & IPC_FF_ROUTE_FROM_ME) {
|
|---|
| [9201f47] | 494 | call->data.phone = newphone;
|
|---|
| [e2ab36f1] | 495 | call->data.task_id = TASK->taskid;
|
|---|
| [645d9ed2] | 496 | }
|
|---|
| [da1bafb] | 497 |
|
|---|
| [9f22213] | 498 | return ipc_call(newphone, call);
|
|---|
| [6d9c49a] | 499 | }
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| [8b243f2] | 502 | /** Wait for a phone call.
|
|---|
| [6d9c49a] | 503 | *
|
|---|
| [da1bafb] | 504 | * @param box Answerbox expecting the call.
|
|---|
| 505 | * @param usec Timeout in microseconds. See documentation for
|
|---|
| 506 | * waitq_sleep_timeout() for decription of its special
|
|---|
| 507 | * meaning.
|
|---|
| 508 | * @param flags Select mode of sleep operation. See documentation for
|
|---|
| 509 | * waitq_sleep_timeout() for description of its special
|
|---|
| 510 | * meaning.
|
|---|
| 511 | *
|
|---|
| 512 | * @return Recived call structure or NULL.
|
|---|
| 513 | *
|
|---|
| [8b243f2] | 514 | * To distinguish between a call and an answer, have a look at call->flags.
|
|---|
| [da1bafb] | 515 | *
|
|---|
| [6d9c49a] | 516 | */
|
|---|
| [da1bafb] | 517 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
|
|---|
| [6d9c49a] | 518 | {
|
|---|
| 519 | call_t *request;
|
|---|
| [aa028db] | 520 | uint64_t irq_cnt = 0;
|
|---|
| 521 | uint64_t answer_cnt = 0;
|
|---|
| 522 | uint64_t call_cnt = 0;
|
|---|
| [bd5a663] | 523 | int rc;
|
|---|
| [da1bafb] | 524 |
|
|---|
| [bd5a663] | 525 | restart:
|
|---|
| [116d1ef4] | 526 | rc = waitq_sleep_timeout(&box->wq, usec, flags);
|
|---|
| [bd5a663] | 527 | if (SYNCH_FAILED(rc))
|
|---|
| 528 | return NULL;
|
|---|
| [fbcfd458] | 529 |
|
|---|
| [da1bafb] | 530 | irq_spinlock_lock(&box->lock, true);
|
|---|
| [5626277] | 531 | if (!list_empty(&box->irq_notifs)) {
|
|---|
| [be06914] | 532 | /* Count received IRQ notification */
|
|---|
| [da1bafb] | 533 | irq_cnt++;
|
|---|
| 534 |
|
|---|
| 535 | irq_spinlock_lock(&box->irq_lock, false);
|
|---|
| 536 |
|
|---|
| [55b77d9] | 537 | request = list_get_instance(list_first(&box->irq_notifs),
|
|---|
| [cfaa35a] | 538 | call_t, ab_link);
|
|---|
| 539 | list_remove(&request->ab_link);
|
|---|
| [da1bafb] | 540 |
|
|---|
| 541 | irq_spinlock_unlock(&box->irq_lock, false);
|
|---|
| [5626277] | 542 | } else if (!list_empty(&box->answers)) {
|
|---|
| [be06914] | 543 | /* Count received answer */
|
|---|
| [aa028db] | 544 | answer_cnt++;
|
|---|
| [da1bafb] | 545 |
|
|---|
| [fbcfd458] | 546 | /* Handle asynchronous answers */
|
|---|
| [55b77d9] | 547 | request = list_get_instance(list_first(&box->answers),
|
|---|
| [cfaa35a] | 548 | call_t, ab_link);
|
|---|
| 549 | list_remove(&request->ab_link);
|
|---|
| [5a77550] | 550 | atomic_dec(&request->caller_phone->active_calls);
|
|---|
| [fbcfd458] | 551 | } else if (!list_empty(&box->calls)) {
|
|---|
| [be06914] | 552 | /* Count received call */
|
|---|
| [aa028db] | 553 | call_cnt++;
|
|---|
| [da1bafb] | 554 |
|
|---|
| [fbcfd458] | 555 | /* Handle requests */
|
|---|
| [55b77d9] | 556 | request = list_get_instance(list_first(&box->calls),
|
|---|
| [cfaa35a] | 557 | call_t, ab_link);
|
|---|
| 558 | list_remove(&request->ab_link);
|
|---|
| [da1bafb] | 559 |
|
|---|
| [fbcfd458] | 560 | /* Append request to dispatch queue */
|
|---|
| [cfaa35a] | 561 | list_append(&request->ab_link, &box->dispatched_calls);
|
|---|
| [fbcfd458] | 562 | } else {
|
|---|
| [874621f] | 563 | /* This can happen regularly after ipc_cleanup */
|
|---|
| [da1bafb] | 564 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| [fbcfd458] | 565 | goto restart;
|
|---|
| [6d9c49a] | 566 | }
|
|---|
| [aa028db] | 567 |
|
|---|
| [da1bafb] | 568 | irq_spinlock_pass(&box->lock, &TASK->lock);
|
|---|
| 569 |
|
|---|
| [be06914] | 570 | TASK->ipc_info.irq_notif_received += irq_cnt;
|
|---|
| 571 | TASK->ipc_info.answer_received += answer_cnt;
|
|---|
| 572 | TASK->ipc_info.call_received += call_cnt;
|
|---|
| [da1bafb] | 573 |
|
|---|
| 574 | irq_spinlock_unlock(&TASK->lock, true);
|
|---|
| 575 |
|
|---|
| [6d9c49a] | 576 | return request;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| [8b243f2] | 579 | /** Answer all calls from list with EHANGUP answer.
|
|---|
| 580 | *
|
|---|
| [5d3ed34] | 581 | * @param box Answerbox with the list.
|
|---|
| [da1bafb] | 582 | * @param lst Head of the list to be cleaned up.
|
|---|
| [8b243f2] | 583 | */
|
|---|
| [5d3ed34] | 584 | void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
|
|---|
| [ca687ad] | 585 | {
|
|---|
| [5d3ed34] | 586 | irq_spinlock_lock(&box->lock, true);
|
|---|
| [ca687ad] | 587 | while (!list_empty(lst)) {
|
|---|
| [cfaa35a] | 588 | call_t *call = list_get_instance(list_first(lst), call_t,
|
|---|
| 589 | ab_link);
|
|---|
| [da1bafb] | 590 |
|
|---|
| [cfaa35a] | 591 | list_remove(&call->ab_link);
|
|---|
| [5d3ed34] | 592 |
|
|---|
| 593 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 594 |
|
|---|
| [466e95f7] | 595 | if (lst == &box->calls)
|
|---|
| 596 | SYSIPC_OP(request_process, call, box);
|
|---|
| [716185d] | 597 |
|
|---|
| [5d3ed34] | 598 | ipc_data_t old = call->data;
|
|---|
| [ca687ad] | 599 | IPC_SET_RETVAL(call->data, EHANGUP);
|
|---|
| [5d3ed34] | 600 | answer_preprocess(call, &old);
|
|---|
| [c713aa56] | 601 | _ipc_answer_free_call(call, true);
|
|---|
| [5d3ed34] | 602 |
|
|---|
| 603 | irq_spinlock_lock(&box->lock, true);
|
|---|
| [ca687ad] | 604 | }
|
|---|
| [5d3ed34] | 605 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| [ca687ad] | 606 | }
|
|---|
| 607 |
|
|---|
| [9a1b20c] | 608 | /** Disconnects all phones connected to an answerbox.
|
|---|
| [4e49572] | 609 | *
|
|---|
| [da1bafb] | 610 | * @param box Answerbox to disconnect phones from.
|
|---|
| 611 | * @param notify_box If true, the answerbox will get a hangup message for
|
|---|
| 612 | * each disconnected phone.
|
|---|
| 613 | *
|
|---|
| [4e49572] | 614 | */
|
|---|
| [9a1b20c] | 615 | void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
|
|---|
| [4e49572] | 616 | {
|
|---|
| [ca687ad] | 617 | phone_t *phone;
|
|---|
| [31d8e10] | 618 | DEADLOCK_PROBE_INIT(p_phonelck);
|
|---|
| [da1bafb] | 619 |
|
|---|
| [ca687ad] | 620 | /* Disconnect all phones connected to our answerbox */
|
|---|
| 621 | restart_phones:
|
|---|
| [da1bafb] | 622 | irq_spinlock_lock(&box->lock, true);
|
|---|
| [9a1b20c] | 623 | while (!list_empty(&box->connected_phones)) {
|
|---|
| [55b77d9] | 624 | phone = list_get_instance(list_first(&box->connected_phones),
|
|---|
| [31d8e10] | 625 | phone_t, link);
|
|---|
| [ff48a15] | 626 | if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
|
|---|
| [da1bafb] | 627 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| [31d8e10] | 628 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
|
|---|
| [ca687ad] | 629 | goto restart_phones;
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | /* Disconnect phone */
|
|---|
| [63e27ef] | 633 | assert(phone->state == IPC_PHONE_CONNECTED);
|
|---|
| [da1bafb] | 634 |
|
|---|
| [c4e4507] | 635 | list_remove(&phone->link);
|
|---|
| [9a1b20c] | 636 | phone->state = IPC_PHONE_SLAMMED;
|
|---|
| [da1bafb] | 637 |
|
|---|
| [9a1b20c] | 638 | if (notify_box) {
|
|---|
| [97b8ca9] | 639 | task_hold(phone->caller);
|
|---|
| [9a1b20c] | 640 | mutex_unlock(&phone->lock);
|
|---|
| [da1bafb] | 641 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| [f1d5ef8] | 642 |
|
|---|
| [9a1b20c] | 643 | /*
|
|---|
| [97b8ca9] | 644 | * Send one call to the answerbox for each phone.
|
|---|
| 645 | * Used to make sure the kbox thread wakes up after
|
|---|
| 646 | * the last phone has been disconnected. The call is
|
|---|
| 647 | * forgotten upon sending, so the "caller" may cease
|
|---|
| 648 | * to exist as soon as we release it.
|
|---|
| [9a1b20c] | 649 | */
|
|---|
| [97b8ca9] | 650 | call_t *call = ipc_call_alloc(0);
|
|---|
| [228e490] | 651 | IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
|
|---|
| [796692c] | 652 | call->request_method = IPC_M_PHONE_HUNGUP;
|
|---|
| [9a1b20c] | 653 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
|---|
| [97b8ca9] | 654 | _ipc_call(phone, box, call, true);
|
|---|
| 655 |
|
|---|
| 656 | task_release(phone->caller);
|
|---|
| [da1bafb] | 657 |
|
|---|
| [9a1b20c] | 658 | /* Must start again */
|
|---|
| 659 | goto restart_phones;
|
|---|
| 660 | }
|
|---|
| [da1bafb] | 661 |
|
|---|
| [ff48a15] | 662 | mutex_unlock(&phone->lock);
|
|---|
| [ca687ad] | 663 | }
|
|---|
| [da1bafb] | 664 |
|
|---|
| 665 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| [9a1b20c] | 666 | }
|
|---|
| 667 |
|
|---|
| [43e2cbc] | 668 | static void ipc_forget_call(call_t *call)
|
|---|
| 669 | {
|
|---|
| [63e27ef] | 670 | assert(spinlock_locked(&TASK->active_calls_lock));
|
|---|
| 671 | assert(spinlock_locked(&call->forget_lock));
|
|---|
| [43e2cbc] | 672 |
|
|---|
| 673 | /*
|
|---|
| 674 | * Forget the call and donate it to the task which holds up the answer.
|
|---|
| 675 | */
|
|---|
| 676 |
|
|---|
| 677 | call->forget = true;
|
|---|
| 678 | call->sender = NULL;
|
|---|
| 679 | list_remove(&call->ta_link);
|
|---|
| 680 |
|
|---|
| 681 | /*
|
|---|
| 682 | * The call may be freed by _ipc_answer_free_call() before we are done
|
|---|
| 683 | * with it; to avoid working with a destroyed call_t structure, we
|
|---|
| 684 | * must hold a reference to it.
|
|---|
| 685 | */
|
|---|
| 686 | ipc_call_hold(call);
|
|---|
| 687 |
|
|---|
| 688 | spinlock_unlock(&call->forget_lock);
|
|---|
| 689 | spinlock_unlock(&TASK->active_calls_lock);
|
|---|
| 690 |
|
|---|
| 691 | atomic_dec(&call->caller_phone->active_calls);
|
|---|
| 692 |
|
|---|
| 693 | SYSIPC_OP(request_forget, call);
|
|---|
| 694 |
|
|---|
| 695 | ipc_call_release(call);
|
|---|
| 696 | }
|
|---|
| 697 |
|
|---|
| [2405bb5] | 698 | static void ipc_forget_all_active_calls(void)
|
|---|
| 699 | {
|
|---|
| 700 | call_t *call;
|
|---|
| 701 |
|
|---|
| 702 | restart:
|
|---|
| 703 | spinlock_lock(&TASK->active_calls_lock);
|
|---|
| 704 | if (list_empty(&TASK->active_calls)) {
|
|---|
| 705 | /*
|
|---|
| 706 | * We are done, there are no more active calls.
|
|---|
| 707 | * Nota bene: there may still be answers waiting for pick up.
|
|---|
| 708 | */
|
|---|
| 709 | spinlock_unlock(&TASK->active_calls_lock);
|
|---|
| 710 | return;
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | call = list_get_instance(list_first(&TASK->active_calls), call_t,
|
|---|
| 714 | ta_link);
|
|---|
| 715 |
|
|---|
| 716 | if (!spinlock_trylock(&call->forget_lock)) {
|
|---|
| 717 | /*
|
|---|
| [20282ef3] | 718 | * Avoid deadlock and let async_answer() or
|
|---|
| 719 | * _ipc_answer_free_call() win the race to dequeue the first
|
|---|
| 720 | * call on the list.
|
|---|
| [2405bb5] | 721 | */
|
|---|
| 722 | spinlock_unlock(&TASK->active_calls_lock);
|
|---|
| 723 | goto restart;
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| [43e2cbc] | 726 | ipc_forget_call(call);
|
|---|
| [cd671c3] | 727 |
|
|---|
| [2405bb5] | 728 | goto restart;
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | /** Wait for all answers to asynchronous calls to arrive. */
|
|---|
| 732 | static void ipc_wait_for_all_answered_calls(void)
|
|---|
| 733 | {
|
|---|
| 734 | call_t *call;
|
|---|
| 735 | size_t i;
|
|---|
| 736 |
|
|---|
| 737 | restart:
|
|---|
| 738 | /*
|
|---|
| [d891cba] | 739 | * Go through all phones, until they are all free.
|
|---|
| 740 | * Locking is needed as there may be connection handshakes in progress.
|
|---|
| [2405bb5] | 741 | */
|
|---|
| 742 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
|---|
| [d891cba] | 743 | phone_t *phone = &TASK->phones[i];
|
|---|
| 744 |
|
|---|
| 745 | mutex_lock(&phone->lock);
|
|---|
| 746 | if ((phone->state == IPC_PHONE_HUNGUP) &&
|
|---|
| 747 | (atomic_get(&phone->active_calls) == 0)) {
|
|---|
| 748 | phone->state = IPC_PHONE_FREE;
|
|---|
| 749 | phone->callee = NULL;
|
|---|
| [2405bb5] | 750 | }
|
|---|
| 751 |
|
|---|
| 752 | /*
|
|---|
| [f9bd2e3] | 753 | * We might have had some IPC_PHONE_CONNECTING phones at the
|
|---|
| 754 | * beginning of ipc_cleanup(). Depending on whether these were
|
|---|
| 755 | * forgotten or answered, they will eventually enter the
|
|---|
| 756 | * IPC_PHONE_FREE or IPC_PHONE_CONNECTED states, respectively.
|
|---|
| 757 | * In the latter case, the other side may slam the open phones
|
|---|
| 758 | * at any time, in which case we will get an IPC_PHONE_SLAMMED
|
|---|
| 759 | * phone.
|
|---|
| [2405bb5] | 760 | */
|
|---|
| [d891cba] | 761 | if ((phone->state == IPC_PHONE_CONNECTED) ||
|
|---|
| 762 | (phone->state == IPC_PHONE_SLAMMED)) {
|
|---|
| 763 | mutex_unlock(&phone->lock);
|
|---|
| 764 | ipc_phone_hangup(phone);
|
|---|
| [8f6858d0] | 765 | /*
|
|---|
| [f9bd2e3] | 766 | * Now there may be one extra active call, which needs
|
|---|
| 767 | * to be forgotten.
|
|---|
| [8f6858d0] | 768 | */
|
|---|
| 769 | ipc_forget_all_active_calls();
|
|---|
| 770 | goto restart;
|
|---|
| 771 | }
|
|---|
| [2405bb5] | 772 |
|
|---|
| 773 | /*
|
|---|
| 774 | * If the hangup succeeded, it has sent a HANGUP message, the
|
|---|
| 775 | * IPC is now in HUNGUP state, we wait for the reply to come
|
|---|
| 776 | */
|
|---|
| [d891cba] | 777 | if (phone->state != IPC_PHONE_FREE) {
|
|---|
| 778 | mutex_unlock(&phone->lock);
|
|---|
| [2405bb5] | 779 | break;
|
|---|
| [d891cba] | 780 | }
|
|---|
| 781 |
|
|---|
| 782 | mutex_unlock(&phone->lock);
|
|---|
| [2405bb5] | 783 | }
|
|---|
| 784 |
|
|---|
| 785 | /* Got into cleanup */
|
|---|
| 786 | if (i == IPC_MAX_PHONES)
|
|---|
| 787 | return;
|
|---|
| 788 |
|
|---|
| 789 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
|
|---|
| 790 | SYNCH_FLAGS_NONE);
|
|---|
| [63e27ef] | 791 | assert(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
|
|---|
| [675fcbd] | 792 |
|
|---|
| [466e95f7] | 793 | SYSIPC_OP(answer_process, call);
|
|---|
| [675fcbd] | 794 |
|
|---|
| [2405bb5] | 795 | ipc_call_free(call);
|
|---|
| 796 | goto restart;
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| [da1bafb] | 799 | /** Clean up all IPC communication of the current task.
|
|---|
| [9a1b20c] | 800 | *
|
|---|
| 801 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
|
|---|
| 802 | * have to change it as well if you want to cleanup other tasks than TASK.
|
|---|
| [da1bafb] | 803 | *
|
|---|
| [9a1b20c] | 804 | */
|
|---|
| 805 | void ipc_cleanup(void)
|
|---|
| 806 | {
|
|---|
| [c33f39f] | 807 | /*
|
|---|
| 808 | * Mark the answerbox as inactive.
|
|---|
| 809 | *
|
|---|
| 810 | * The main purpose for doing this is to prevent any pending callback
|
|---|
| 811 | * connections from getting established beyond this point.
|
|---|
| 812 | */
|
|---|
| 813 | irq_spinlock_lock(&TASK->answerbox.lock, true);
|
|---|
| 814 | TASK->answerbox.active = false;
|
|---|
| 815 | irq_spinlock_unlock(&TASK->answerbox.lock, true);
|
|---|
| 816 |
|
|---|
| [9a1b20c] | 817 | /* Disconnect all our phones ('ipc_phone_hangup') */
|
|---|
| [2405bb5] | 818 | for (size_t i = 0; i < IPC_MAX_PHONES; i++)
|
|---|
| [9a1b20c] | 819 | ipc_phone_hangup(&TASK->phones[i]);
|
|---|
| [da1bafb] | 820 |
|
|---|
| [05641a9e] | 821 | /* Unsubscribe from any event notifications. */
|
|---|
| 822 | event_cleanup_answerbox(&TASK->answerbox);
|
|---|
| [da1bafb] | 823 |
|
|---|
| [9a1b20c] | 824 | /* Disconnect all connected irqs */
|
|---|
| 825 | ipc_irq_cleanup(&TASK->answerbox);
|
|---|
| [da1bafb] | 826 |
|
|---|
| [9a1b20c] | 827 | /* Disconnect all phones connected to our regular answerbox */
|
|---|
| 828 | ipc_answerbox_slam_phones(&TASK->answerbox, false);
|
|---|
| [da1bafb] | 829 |
|
|---|
| [9a1b20c] | 830 | #ifdef CONFIG_UDEBUG
|
|---|
| 831 | /* Clean up kbox thread and communications */
|
|---|
| 832 | ipc_kbox_cleanup();
|
|---|
| 833 | #endif
|
|---|
| [da1bafb] | 834 |
|
|---|
| [9f22213] | 835 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */
|
|---|
| [716185d] | 836 | ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
|
|---|
| [5d3ed34] | 837 | ipc_cleanup_call_list(&TASK->answerbox,
|
|---|
| 838 | &TASK->answerbox.dispatched_calls);
|
|---|
| [bc117a5] | 839 |
|
|---|
| [2405bb5] | 840 | ipc_forget_all_active_calls();
|
|---|
| 841 | ipc_wait_for_all_answered_calls();
|
|---|
| [4e49572] | 842 | }
|
|---|
| [5626277] | 843 |
|
|---|
| [da1bafb] | 844 | /** Initilize IPC subsystem
|
|---|
| 845 | *
|
|---|
| 846 | */
|
|---|
| [5626277] | 847 | void ipc_init(void)
|
|---|
| 848 | {
|
|---|
| [f97f1e51] | 849 | ipc_call_slab = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
|
|---|
| [8b243f2] | 850 | NULL, 0);
|
|---|
| [f97f1e51] | 851 | ipc_answerbox_slab = slab_cache_create("answerbox_t",
|
|---|
| [c70ce74] | 852 | sizeof(answerbox_t), 0, NULL, NULL, 0);
|
|---|
| [5626277] | 853 | }
|
|---|
| 854 |
|
|---|
| [9c9903a9] | 855 |
|
|---|
| 856 | static void ipc_print_call_list(list_t *list)
|
|---|
| 857 | {
|
|---|
| [feeac0d] | 858 | list_foreach(*list, ab_link, call_t, call) {
|
|---|
| [9c9903a9] | 859 | #ifdef __32_BITS__
|
|---|
| 860 | printf("%10p ", call);
|
|---|
| 861 | #endif
|
|---|
| 862 |
|
|---|
| 863 | #ifdef __64_BITS__
|
|---|
| 864 | printf("%18p ", call);
|
|---|
| 865 | #endif
|
|---|
| 866 |
|
|---|
| 867 | spinlock_lock(&call->forget_lock);
|
|---|
| 868 |
|
|---|
| 869 | printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
|
|---|
| 870 | " %-6" PRIun " %-6" PRIun " %-7x",
|
|---|
| 871 | IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
|
|---|
| 872 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
|---|
| 873 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
|---|
| 874 | call->flags);
|
|---|
| 875 |
|
|---|
| 876 | if (call->forget) {
|
|---|
| 877 | printf(" ? (call forgotten)\n");
|
|---|
| 878 | } else {
|
|---|
| 879 | printf(" %" PRIu64 " (%s)\n",
|
|---|
| 880 | call->sender->taskid, call->sender->name);
|
|---|
| 881 | }
|
|---|
| 882 |
|
|---|
| 883 | spinlock_unlock(&call->forget_lock);
|
|---|
| 884 | }
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| [8b243f2] | 887 | /** List answerbox contents.
|
|---|
| 888 | *
|
|---|
| [da1bafb] | 889 | * @param taskid Task ID.
|
|---|
| 890 | *
|
|---|
| [8b243f2] | 891 | */
|
|---|
| [c4e4507] | 892 | void ipc_print_task(task_id_t taskid)
|
|---|
| 893 | {
|
|---|
| [da1bafb] | 894 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 895 | task_t *task = task_find_by_id(taskid);
|
|---|
| 896 |
|
|---|
| [170332d] | 897 | if (!task) {
|
|---|
| [da1bafb] | 898 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| [c4e4507] | 899 | return;
|
|---|
| [170332d] | 900 | }
|
|---|
| [da1bafb] | 901 |
|
|---|
| 902 | /* Hand-over-hand locking */
|
|---|
| 903 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
|---|
| 904 |
|
|---|
| [5378f99] | 905 | printf("[phone id] [calls] [state\n");
|
|---|
| [da1bafb] | 906 |
|
|---|
| 907 | size_t i;
|
|---|
| [8b243f2] | 908 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
|---|
| [ff48a15] | 909 | if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
|
|---|
| [5378f99] | 910 | printf("%-10zu (mutex busy)\n", i);
|
|---|
| [ff48a15] | 911 | continue;
|
|---|
| 912 | }
|
|---|
| [da1bafb] | 913 |
|
|---|
| [c4e4507] | 914 | if (task->phones[i].state != IPC_PHONE_FREE) {
|
|---|
| [5378f99] | 915 | printf("%-10zu %7" PRIun " ", i,
|
|---|
| 916 | atomic_get(&task->phones[i].active_calls));
|
|---|
| [da1bafb] | 917 |
|
|---|
| [c4e4507] | 918 | switch (task->phones[i].state) {
|
|---|
| 919 | case IPC_PHONE_CONNECTING:
|
|---|
| [5378f99] | 920 | printf("connecting");
|
|---|
| [c4e4507] | 921 | break;
|
|---|
| 922 | case IPC_PHONE_CONNECTED:
|
|---|
| [5378f99] | 923 | printf("connected to %" PRIu64 " (%s)",
|
|---|
| 924 | task->phones[i].callee->task->taskid,
|
|---|
| 925 | task->phones[i].callee->task->name);
|
|---|
| [c4e4507] | 926 | break;
|
|---|
| 927 | case IPC_PHONE_SLAMMED:
|
|---|
| [5378f99] | 928 | printf("slammed by %p",
|
|---|
| [da1bafb] | 929 | task->phones[i].callee);
|
|---|
| [c4e4507] | 930 | break;
|
|---|
| 931 | case IPC_PHONE_HUNGUP:
|
|---|
| [5378f99] | 932 | printf("hung up by %p",
|
|---|
| [da1bafb] | 933 | task->phones[i].callee);
|
|---|
| [c4e4507] | 934 | break;
|
|---|
| 935 | default:
|
|---|
| 936 | break;
|
|---|
| 937 | }
|
|---|
| [da1bafb] | 938 |
|
|---|
| [5378f99] | 939 | printf("\n");
|
|---|
| [c4e4507] | 940 | }
|
|---|
| [da1bafb] | 941 |
|
|---|
| [ff48a15] | 942 | mutex_unlock(&task->phones[i].lock);
|
|---|
| [c4e4507] | 943 | }
|
|---|
| [da1bafb] | 944 |
|
|---|
| 945 | irq_spinlock_lock(&task->answerbox.lock, false);
|
|---|
| 946 |
|
|---|
| [5378f99] | 947 | #ifdef __32_BITS__
|
|---|
| 948 | printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
|
|---|
| 949 | " [flags] [sender\n");
|
|---|
| 950 | #endif
|
|---|
| 951 |
|
|---|
| 952 | #ifdef __64_BITS__
|
|---|
| 953 | printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
|
|---|
| 954 | " [arg5] [flags] [sender\n");
|
|---|
| 955 | #endif
|
|---|
| 956 |
|
|---|
| 957 | printf(" --- incomming calls ---\n");
|
|---|
| [9c9903a9] | 958 | ipc_print_call_list(&task->answerbox.calls);
|
|---|
| [5378f99] | 959 | printf(" --- dispatched calls ---\n");
|
|---|
| [9c9903a9] | 960 | ipc_print_call_list(&task->answerbox.dispatched_calls);
|
|---|
| [6aef742] | 961 | printf(" --- incoming answers ---\n");
|
|---|
| [9c9903a9] | 962 | ipc_print_call_list(&task->answerbox.answers);
|
|---|
| [da1bafb] | 963 |
|
|---|
| 964 | irq_spinlock_unlock(&task->answerbox.lock, false);
|
|---|
| 965 | irq_spinlock_unlock(&task->lock, true);
|
|---|
| [c4e4507] | 966 | }
|
|---|
| [b45c443] | 967 |
|
|---|
| [cc73a8a1] | 968 | /** @}
|
|---|
| [b45c443] | 969 | */
|
|---|