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