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