[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 |
|
---|
[2ba7810] | 40 | #include <synch/spinlock.h>
|
---|
[ff48a15] | 41 | #include <synch/mutex.h>
|
---|
[2ba7810] | 42 | #include <synch/waitq.h>
|
---|
[6d9c49a] | 43 | #include <ipc/ipc.h>
|
---|
[c0699467] | 44 | #include <abi/ipc/methods.h>
|
---|
[e028660] | 45 | #include <ipc/kbox.h>
|
---|
[13a638d] | 46 | #include <ipc/event.h>
|
---|
[b1e6269] | 47 | #include <ipc/sysipc_ops.h>
|
---|
[5d3ed34] | 48 | #include <ipc/sysipc_priv.h>
|
---|
[6d9c49a] | 49 | #include <errno.h>
|
---|
| 50 | #include <mm/slab.h>
|
---|
| 51 | #include <arch.h>
|
---|
| 52 | #include <proc/task.h>
|
---|
| 53 | #include <memstr.h>
|
---|
| 54 | #include <debug.h>
|
---|
| 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 |
|
---|
| 229 | ASSERT(!request->forget);
|
---|
| 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 | }
|
---|
| 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 |
|
---|
[6f9c8f6] | 330 | static void _ipc_call_actions_internal(phone_t *phone, call_t *call)
|
---|
[7c7aae16] | 331 | {
|
---|
[03a8a8e] | 332 | task_t *caller = phone->caller;
|
---|
| 333 |
|
---|
[7c7aae16] | 334 | atomic_inc(&phone->active_calls);
|
---|
[c97b086] | 335 | call->caller_phone = phone;
|
---|
[03a8a8e] | 336 | call->sender = caller;
|
---|
[86939b1] | 337 |
|
---|
[c97b086] | 338 | call->active = true;
|
---|
[03a8a8e] | 339 | spinlock_lock(&caller->active_calls_lock);
|
---|
| 340 | list_append(&call->ta_link, &caller->active_calls);
|
---|
| 341 | spinlock_unlock(&caller->active_calls_lock);
|
---|
[86939b1] | 342 |
|
---|
[c97b086] | 343 | call->data.phone = phone;
|
---|
[6f9c8f6] | 344 | call->data.task_id = caller->taskid;
|
---|
| 345 | }
|
---|
[c97b086] | 346 |
|
---|
[6f9c8f6] | 347 | /** Simulate sending back a message.
|
---|
| 348 | *
|
---|
| 349 | * Most errors are better handled by forming a normal backward
|
---|
| 350 | * message and sending it as a normal answer.
|
---|
| 351 | *
|
---|
| 352 | * @param phone Phone structure the call should appear to come from.
|
---|
| 353 | * @param call Call structure to be answered.
|
---|
| 354 | * @param err Return value to be used for the answer.
|
---|
| 355 | *
|
---|
| 356 | */
|
---|
| 357 | void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
|
---|
| 358 | {
|
---|
| 359 | _ipc_call_actions_internal(phone, call);
|
---|
[eb3d379] | 360 | IPC_SET_RETVAL(call->data, err);
|
---|
[c713aa56] | 361 | _ipc_answer_free_call(call, false);
|
---|
[7c7aae16] | 362 | }
|
---|
| 363 |
|
---|
[8b243f2] | 364 | /** Unsafe unchecking version of ipc_call.
|
---|
| 365 | *
|
---|
[da1bafb] | 366 | * @param phone Phone structure the call comes from.
|
---|
| 367 | * @param box Destination answerbox structure.
|
---|
| 368 | * @param call Call structure with request.
|
---|
| 369 | *
|
---|
[8b243f2] | 370 | */
|
---|
[fbcfd458] | 371 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
|
---|
| 372 | {
|
---|
[03a8a8e] | 373 | task_t *caller = phone->caller;
|
---|
| 374 |
|
---|
[95319bd] | 375 | /* Count sent ipc call */
|
---|
[03a8a8e] | 376 | irq_spinlock_lock(&caller->lock, true);
|
---|
| 377 | caller->ipc_info.call_sent++;
|
---|
| 378 | irq_spinlock_unlock(&caller->lock, true);
|
---|
[da1bafb] | 379 |
|
---|
[6f9c8f6] | 380 | if (!(call->flags & IPC_CALL_FORWARDED))
|
---|
| 381 | _ipc_call_actions_internal(phone, call);
|
---|
[da1bafb] | 382 |
|
---|
| 383 | irq_spinlock_lock(&box->lock, true);
|
---|
[cfaa35a] | 384 | list_append(&call->ab_link, &box->calls);
|
---|
[da1bafb] | 385 | irq_spinlock_unlock(&box->lock, true);
|
---|
| 386 |
|
---|
[5c8ba05] | 387 | waitq_wakeup(&box->wq, WAKEUP_FIRST);
|
---|
[fbcfd458] | 388 | }
|
---|
| 389 |
|
---|
[8b243f2] | 390 | /** Send an asynchronous request using a phone to an answerbox.
|
---|
| 391 | *
|
---|
[da1bafb] | 392 | * @param phone Phone structure the call comes from and which is
|
---|
| 393 | * connected to the destination answerbox.
|
---|
| 394 | * @param call Call structure with request.
|
---|
| 395 | *
|
---|
| 396 | * @return Return 0 on success, ENOENT on error.
|
---|
[6d9c49a] | 397 | *
|
---|
| 398 | */
|
---|
[9f22213] | 399 | int ipc_call(phone_t *phone, call_t *call)
|
---|
[6d9c49a] | 400 | {
|
---|
[ff48a15] | 401 | mutex_lock(&phone->lock);
|
---|
[eb3d379] | 402 | if (phone->state != IPC_PHONE_CONNECTED) {
|
---|
[ff48a15] | 403 | mutex_unlock(&phone->lock);
|
---|
[f9841e69] | 404 | if (!(call->flags & IPC_CALL_FORWARDED)) {
|
---|
[eb3d379] | 405 | if (phone->state == IPC_PHONE_HUNGUP)
|
---|
[7c7aae16] | 406 | ipc_backsend_err(phone, call, EHANGUP);
|
---|
[9f22213] | 407 | else
|
---|
[7c7aae16] | 408 | ipc_backsend_err(phone, call, ENOENT);
|
---|
[9f22213] | 409 | }
|
---|
[da1bafb] | 410 |
|
---|
[9f22213] | 411 | return ENOENT;
|
---|
[ba81cab] | 412 | }
|
---|
[da1bafb] | 413 |
|
---|
| 414 | answerbox_t *box = phone->callee;
|
---|
[fbcfd458] | 415 | _ipc_call(phone, box, call);
|
---|
| 416 |
|
---|
[ff48a15] | 417 | mutex_unlock(&phone->lock);
|
---|
[9f22213] | 418 | return 0;
|
---|
[fbcfd458] | 419 | }
|
---|
| 420 |
|
---|
[8b243f2] | 421 | /** Disconnect phone from answerbox.
|
---|
[fbcfd458] | 422 | *
|
---|
[8b243f2] | 423 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done
|
---|
[eb3d379] | 424 | * lazily later.
|
---|
[fbcfd458] | 425 | *
|
---|
[da1bafb] | 426 | * @param phone Phone structure to be hung up.
|
---|
| 427 | *
|
---|
| 428 | * @return 0 if the phone is disconnected.
|
---|
| 429 | * @return -1 if the phone was already disconnected.
|
---|
| 430 | *
|
---|
[fbcfd458] | 431 | */
|
---|
[d8f7362] | 432 | int ipc_phone_hangup(phone_t *phone)
|
---|
[fbcfd458] | 433 | {
|
---|
[ff48a15] | 434 | mutex_lock(&phone->lock);
|
---|
[7918fce] | 435 | if (phone->state == IPC_PHONE_FREE ||
|
---|
| 436 | phone->state == IPC_PHONE_HUNGUP ||
|
---|
[8b243f2] | 437 | phone->state == IPC_PHONE_CONNECTING) {
|
---|
[ff48a15] | 438 | mutex_unlock(&phone->lock);
|
---|
[eb3d379] | 439 | return -1;
|
---|
[fbcfd458] | 440 | }
|
---|
[da1bafb] | 441 |
|
---|
| 442 | answerbox_t *box = phone->callee;
|
---|
[eb3d379] | 443 | if (phone->state != IPC_PHONE_SLAMMED) {
|
---|
| 444 | /* Remove myself from answerbox */
|
---|
[da1bafb] | 445 | irq_spinlock_lock(&box->lock, true);
|
---|
[c4e4507] | 446 | list_remove(&phone->link);
|
---|
[da1bafb] | 447 | irq_spinlock_unlock(&box->lock, true);
|
---|
| 448 |
|
---|
| 449 | call_t *call = ipc_call_alloc(0);
|
---|
[228e490] | 450 | IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
|
---|
[796692c] | 451 | call->request_method = IPC_M_PHONE_HUNGUP;
|
---|
[287e83f] | 452 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
---|
| 453 | _ipc_call(phone, box, call);
|
---|
[eb3d379] | 454 | }
|
---|
[da1bafb] | 455 |
|
---|
[eb3d379] | 456 | phone->state = IPC_PHONE_HUNGUP;
|
---|
[ff48a15] | 457 | mutex_unlock(&phone->lock);
|
---|
[da1bafb] | 458 |
|
---|
[fbcfd458] | 459 | return 0;
|
---|
[2ba7810] | 460 | }
|
---|
| 461 |
|
---|
[8b243f2] | 462 | /** Forwards call from one answerbox to another one.
|
---|
[2ba7810] | 463 | *
|
---|
[da1bafb] | 464 | * @param call Call structure to be redirected.
|
---|
| 465 | * @param newphone Phone structure to target answerbox.
|
---|
| 466 | * @param oldbox Old answerbox structure.
|
---|
| 467 | * @param mode Flags that specify mode of the forward operation.
|
---|
| 468 | *
|
---|
| 469 | * @return 0 if forwarding succeeded or an error code if
|
---|
| 470 | * there was an error.
|
---|
[8b243f2] | 471 | *
|
---|
| 472 | * The return value serves only as an information for the forwarder,
|
---|
| 473 | * the original caller is notified automatically with EFORWARD.
|
---|
[da1bafb] | 474 | *
|
---|
[2ba7810] | 475 | */
|
---|
[da1bafb] | 476 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
|
---|
| 477 | unsigned int mode)
|
---|
[2ba7810] | 478 | {
|
---|
[95319bd] | 479 | /* Count forwarded calls */
|
---|
[da1bafb] | 480 | irq_spinlock_lock(&TASK->lock, true);
|
---|
[a307beb] | 481 | TASK->ipc_info.forwarded++;
|
---|
[da1bafb] | 482 | irq_spinlock_pass(&TASK->lock, &oldbox->lock);
|
---|
[cfaa35a] | 483 | list_remove(&call->ab_link);
|
---|
[da1bafb] | 484 | irq_spinlock_unlock(&oldbox->lock, true);
|
---|
| 485 |
|
---|
[645d9ed2] | 486 | if (mode & IPC_FF_ROUTE_FROM_ME) {
|
---|
[9201f47] | 487 | call->data.phone = newphone;
|
---|
[e2ab36f1] | 488 | call->data.task_id = TASK->taskid;
|
---|
[645d9ed2] | 489 | }
|
---|
[da1bafb] | 490 |
|
---|
[9f22213] | 491 | return ipc_call(newphone, call);
|
---|
[6d9c49a] | 492 | }
|
---|
| 493 |
|
---|
| 494 |
|
---|
[8b243f2] | 495 | /** Wait for a phone call.
|
---|
[6d9c49a] | 496 | *
|
---|
[da1bafb] | 497 | * @param box Answerbox expecting the call.
|
---|
| 498 | * @param usec Timeout in microseconds. See documentation for
|
---|
| 499 | * waitq_sleep_timeout() for decription of its special
|
---|
| 500 | * meaning.
|
---|
| 501 | * @param flags Select mode of sleep operation. See documentation for
|
---|
| 502 | * waitq_sleep_timeout() for description of its special
|
---|
| 503 | * meaning.
|
---|
| 504 | *
|
---|
| 505 | * @return Recived call structure or NULL.
|
---|
| 506 | *
|
---|
[8b243f2] | 507 | * To distinguish between a call and an answer, have a look at call->flags.
|
---|
[da1bafb] | 508 | *
|
---|
[6d9c49a] | 509 | */
|
---|
[da1bafb] | 510 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
|
---|
[6d9c49a] | 511 | {
|
---|
| 512 | call_t *request;
|
---|
[aa028db] | 513 | uint64_t irq_cnt = 0;
|
---|
| 514 | uint64_t answer_cnt = 0;
|
---|
| 515 | uint64_t call_cnt = 0;
|
---|
[bd5a663] | 516 | int rc;
|
---|
[da1bafb] | 517 |
|
---|
[bd5a663] | 518 | restart:
|
---|
[116d1ef4] | 519 | rc = waitq_sleep_timeout(&box->wq, usec, flags);
|
---|
[bd5a663] | 520 | if (SYNCH_FAILED(rc))
|
---|
| 521 | return NULL;
|
---|
[fbcfd458] | 522 |
|
---|
[da1bafb] | 523 | irq_spinlock_lock(&box->lock, true);
|
---|
[5626277] | 524 | if (!list_empty(&box->irq_notifs)) {
|
---|
[be06914] | 525 | /* Count received IRQ notification */
|
---|
[da1bafb] | 526 | irq_cnt++;
|
---|
| 527 |
|
---|
| 528 | irq_spinlock_lock(&box->irq_lock, false);
|
---|
| 529 |
|
---|
[55b77d9] | 530 | request = list_get_instance(list_first(&box->irq_notifs),
|
---|
[cfaa35a] | 531 | call_t, ab_link);
|
---|
| 532 | list_remove(&request->ab_link);
|
---|
[da1bafb] | 533 |
|
---|
| 534 | irq_spinlock_unlock(&box->irq_lock, false);
|
---|
[5626277] | 535 | } else if (!list_empty(&box->answers)) {
|
---|
[be06914] | 536 | /* Count received answer */
|
---|
[aa028db] | 537 | answer_cnt++;
|
---|
[da1bafb] | 538 |
|
---|
[fbcfd458] | 539 | /* Handle asynchronous answers */
|
---|
[55b77d9] | 540 | request = list_get_instance(list_first(&box->answers),
|
---|
[cfaa35a] | 541 | call_t, ab_link);
|
---|
| 542 | list_remove(&request->ab_link);
|
---|
[5a77550] | 543 | atomic_dec(&request->caller_phone->active_calls);
|
---|
[fbcfd458] | 544 | } else if (!list_empty(&box->calls)) {
|
---|
[be06914] | 545 | /* Count received call */
|
---|
[aa028db] | 546 | call_cnt++;
|
---|
[da1bafb] | 547 |
|
---|
[fbcfd458] | 548 | /* Handle requests */
|
---|
[55b77d9] | 549 | request = list_get_instance(list_first(&box->calls),
|
---|
[cfaa35a] | 550 | call_t, ab_link);
|
---|
| 551 | list_remove(&request->ab_link);
|
---|
[da1bafb] | 552 |
|
---|
[fbcfd458] | 553 | /* Append request to dispatch queue */
|
---|
[cfaa35a] | 554 | list_append(&request->ab_link, &box->dispatched_calls);
|
---|
[fbcfd458] | 555 | } else {
|
---|
[874621f] | 556 | /* This can happen regularly after ipc_cleanup */
|
---|
[da1bafb] | 557 | irq_spinlock_unlock(&box->lock, true);
|
---|
[fbcfd458] | 558 | goto restart;
|
---|
[6d9c49a] | 559 | }
|
---|
[aa028db] | 560 |
|
---|
[da1bafb] | 561 | irq_spinlock_pass(&box->lock, &TASK->lock);
|
---|
| 562 |
|
---|
[be06914] | 563 | TASK->ipc_info.irq_notif_received += irq_cnt;
|
---|
| 564 | TASK->ipc_info.answer_received += answer_cnt;
|
---|
| 565 | TASK->ipc_info.call_received += call_cnt;
|
---|
[da1bafb] | 566 |
|
---|
| 567 | irq_spinlock_unlock(&TASK->lock, true);
|
---|
| 568 |
|
---|
[6d9c49a] | 569 | return request;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
[8b243f2] | 572 | /** Answer all calls from list with EHANGUP answer.
|
---|
| 573 | *
|
---|
[5d3ed34] | 574 | * @param box Answerbox with the list.
|
---|
[da1bafb] | 575 | * @param lst Head of the list to be cleaned up.
|
---|
[8b243f2] | 576 | */
|
---|
[5d3ed34] | 577 | void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
|
---|
[ca687ad] | 578 | {
|
---|
[5d3ed34] | 579 | irq_spinlock_lock(&box->lock, true);
|
---|
[ca687ad] | 580 | while (!list_empty(lst)) {
|
---|
[cfaa35a] | 581 | call_t *call = list_get_instance(list_first(lst), call_t,
|
---|
| 582 | ab_link);
|
---|
[da1bafb] | 583 |
|
---|
[cfaa35a] | 584 | list_remove(&call->ab_link);
|
---|
[5d3ed34] | 585 |
|
---|
| 586 | irq_spinlock_unlock(&box->lock, true);
|
---|
| 587 |
|
---|
[466e95f7] | 588 | if (lst == &box->calls)
|
---|
| 589 | SYSIPC_OP(request_process, call, box);
|
---|
[716185d] | 590 |
|
---|
[5d3ed34] | 591 | ipc_data_t old = call->data;
|
---|
[ca687ad] | 592 | IPC_SET_RETVAL(call->data, EHANGUP);
|
---|
[5d3ed34] | 593 | answer_preprocess(call, &old);
|
---|
[c713aa56] | 594 | _ipc_answer_free_call(call, true);
|
---|
[5d3ed34] | 595 |
|
---|
| 596 | irq_spinlock_lock(&box->lock, true);
|
---|
[ca687ad] | 597 | }
|
---|
[5d3ed34] | 598 | irq_spinlock_unlock(&box->lock, true);
|
---|
[ca687ad] | 599 | }
|
---|
| 600 |
|
---|
[9a1b20c] | 601 | /** Disconnects all phones connected to an answerbox.
|
---|
[4e49572] | 602 | *
|
---|
[da1bafb] | 603 | * @param box Answerbox to disconnect phones from.
|
---|
| 604 | * @param notify_box If true, the answerbox will get a hangup message for
|
---|
| 605 | * each disconnected phone.
|
---|
| 606 | *
|
---|
[4e49572] | 607 | */
|
---|
[9a1b20c] | 608 | void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
|
---|
[4e49572] | 609 | {
|
---|
[ca687ad] | 610 | phone_t *phone;
|
---|
[31d8e10] | 611 | DEADLOCK_PROBE_INIT(p_phonelck);
|
---|
[da1bafb] | 612 |
|
---|
| 613 | call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
|
---|
| 614 |
|
---|
[ca687ad] | 615 | /* Disconnect all phones connected to our answerbox */
|
---|
| 616 | restart_phones:
|
---|
[da1bafb] | 617 | irq_spinlock_lock(&box->lock, true);
|
---|
[9a1b20c] | 618 | while (!list_empty(&box->connected_phones)) {
|
---|
[55b77d9] | 619 | phone = list_get_instance(list_first(&box->connected_phones),
|
---|
[31d8e10] | 620 | phone_t, link);
|
---|
[ff48a15] | 621 | if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
|
---|
[da1bafb] | 622 | irq_spinlock_unlock(&box->lock, true);
|
---|
[31d8e10] | 623 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
|
---|
[ca687ad] | 624 | goto restart_phones;
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | /* Disconnect phone */
|
---|
[eb3d379] | 628 | ASSERT(phone->state == IPC_PHONE_CONNECTED);
|
---|
[da1bafb] | 629 |
|
---|
[c4e4507] | 630 | list_remove(&phone->link);
|
---|
[9a1b20c] | 631 | phone->state = IPC_PHONE_SLAMMED;
|
---|
[da1bafb] | 632 |
|
---|
[9a1b20c] | 633 | if (notify_box) {
|
---|
| 634 | mutex_unlock(&phone->lock);
|
---|
[da1bafb] | 635 | irq_spinlock_unlock(&box->lock, true);
|
---|
[f1d5ef8] | 636 |
|
---|
| 637 | // FIXME: phone can become deallocated at any time now
|
---|
| 638 |
|
---|
[9a1b20c] | 639 | /*
|
---|
| 640 | * Send one message to the answerbox for each
|
---|
| 641 | * phone. Used to make sure the kbox thread
|
---|
| 642 | * wakes up after the last phone has been
|
---|
| 643 | * disconnected.
|
---|
| 644 | */
|
---|
[228e490] | 645 | IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
|
---|
[796692c] | 646 | call->request_method = IPC_M_PHONE_HUNGUP;
|
---|
[9a1b20c] | 647 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
---|
| 648 | _ipc_call(phone, box, call);
|
---|
[da1bafb] | 649 |
|
---|
[9a1b20c] | 650 | /* Allocate another call in advance */
|
---|
| 651 | call = ipc_call_alloc(0);
|
---|
[da1bafb] | 652 |
|
---|
[9a1b20c] | 653 | /* Must start again */
|
---|
| 654 | goto restart_phones;
|
---|
| 655 | }
|
---|
[da1bafb] | 656 |
|
---|
[ff48a15] | 657 | mutex_unlock(&phone->lock);
|
---|
[ca687ad] | 658 | }
|
---|
[da1bafb] | 659 |
|
---|
| 660 | irq_spinlock_unlock(&box->lock, true);
|
---|
| 661 |
|
---|
[9a1b20c] | 662 | /* Free unused call */
|
---|
[119c335] | 663 | if (call)
|
---|
| 664 | ipc_call_free(call);
|
---|
[9a1b20c] | 665 | }
|
---|
| 666 |
|
---|
[43e2cbc] | 667 | static void ipc_forget_call(call_t *call)
|
---|
| 668 | {
|
---|
| 669 | ASSERT(spinlock_locked(&TASK->active_calls_lock));
|
---|
| 670 | ASSERT(spinlock_locked(&call->forget_lock));
|
---|
| 671 |
|
---|
| 672 | /*
|
---|
| 673 | * Forget the call and donate it to the task which holds up the answer.
|
---|
| 674 | */
|
---|
| 675 |
|
---|
| 676 | call->forget = true;
|
---|
| 677 | call->sender = NULL;
|
---|
| 678 | list_remove(&call->ta_link);
|
---|
| 679 |
|
---|
| 680 | /*
|
---|
| 681 | * The call may be freed by _ipc_answer_free_call() before we are done
|
---|
| 682 | * with it; to avoid working with a destroyed call_t structure, we
|
---|
| 683 | * must hold a reference to it.
|
---|
| 684 | */
|
---|
| 685 | ipc_call_hold(call);
|
---|
| 686 |
|
---|
| 687 | spinlock_unlock(&call->forget_lock);
|
---|
| 688 | spinlock_unlock(&TASK->active_calls_lock);
|
---|
| 689 |
|
---|
| 690 | atomic_dec(&call->caller_phone->active_calls);
|
---|
| 691 |
|
---|
| 692 | SYSIPC_OP(request_forget, call);
|
---|
| 693 |
|
---|
| 694 | ipc_call_release(call);
|
---|
| 695 | }
|
---|
| 696 |
|
---|
[2405bb5] | 697 | static void ipc_forget_all_active_calls(void)
|
---|
| 698 | {
|
---|
| 699 | call_t *call;
|
---|
| 700 |
|
---|
| 701 | restart:
|
---|
| 702 | spinlock_lock(&TASK->active_calls_lock);
|
---|
| 703 | if (list_empty(&TASK->active_calls)) {
|
---|
| 704 | /*
|
---|
| 705 | * We are done, there are no more active calls.
|
---|
| 706 | * Nota bene: there may still be answers waiting for pick up.
|
---|
| 707 | */
|
---|
| 708 | spinlock_unlock(&TASK->active_calls_lock);
|
---|
| 709 | return;
|
---|
| 710 | }
|
---|
| 711 |
|
---|
| 712 | call = list_get_instance(list_first(&TASK->active_calls), call_t,
|
---|
| 713 | ta_link);
|
---|
| 714 |
|
---|
| 715 | if (!spinlock_trylock(&call->forget_lock)) {
|
---|
| 716 | /*
|
---|
[20282ef3] | 717 | * Avoid deadlock and let async_answer() or
|
---|
| 718 | * _ipc_answer_free_call() win the race to dequeue the first
|
---|
| 719 | * call on the list.
|
---|
[2405bb5] | 720 | */
|
---|
| 721 | spinlock_unlock(&TASK->active_calls_lock);
|
---|
| 722 | goto restart;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
[43e2cbc] | 725 | ipc_forget_call(call);
|
---|
[cd671c3] | 726 |
|
---|
[2405bb5] | 727 | goto restart;
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | /** Wait for all answers to asynchronous calls to arrive. */
|
---|
| 731 | static void ipc_wait_for_all_answered_calls(void)
|
---|
| 732 | {
|
---|
| 733 | call_t *call;
|
---|
| 734 | size_t i;
|
---|
| 735 |
|
---|
| 736 | restart:
|
---|
| 737 | /*
|
---|
[d891cba] | 738 | * Go through all phones, until they are all free.
|
---|
| 739 | * Locking is needed as there may be connection handshakes in progress.
|
---|
[2405bb5] | 740 | */
|
---|
| 741 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
[d891cba] | 742 | phone_t *phone = &TASK->phones[i];
|
---|
| 743 |
|
---|
| 744 | mutex_lock(&phone->lock);
|
---|
| 745 | if ((phone->state == IPC_PHONE_HUNGUP) &&
|
---|
| 746 | (atomic_get(&phone->active_calls) == 0)) {
|
---|
| 747 | phone->state = IPC_PHONE_FREE;
|
---|
| 748 | phone->callee = NULL;
|
---|
[2405bb5] | 749 | }
|
---|
| 750 |
|
---|
| 751 | /*
|
---|
[f9bd2e3] | 752 | * We might have had some IPC_PHONE_CONNECTING phones at the
|
---|
| 753 | * beginning of ipc_cleanup(). Depending on whether these were
|
---|
| 754 | * forgotten or answered, they will eventually enter the
|
---|
| 755 | * IPC_PHONE_FREE or IPC_PHONE_CONNECTED states, respectively.
|
---|
| 756 | * In the latter case, the other side may slam the open phones
|
---|
| 757 | * at any time, in which case we will get an IPC_PHONE_SLAMMED
|
---|
| 758 | * phone.
|
---|
[2405bb5] | 759 | */
|
---|
[d891cba] | 760 | if ((phone->state == IPC_PHONE_CONNECTED) ||
|
---|
| 761 | (phone->state == IPC_PHONE_SLAMMED)) {
|
---|
| 762 | mutex_unlock(&phone->lock);
|
---|
| 763 | ipc_phone_hangup(phone);
|
---|
[8f6858d0] | 764 | /*
|
---|
[f9bd2e3] | 765 | * Now there may be one extra active call, which needs
|
---|
| 766 | * to be forgotten.
|
---|
[8f6858d0] | 767 | */
|
---|
| 768 | ipc_forget_all_active_calls();
|
---|
| 769 | goto restart;
|
---|
| 770 | }
|
---|
[2405bb5] | 771 |
|
---|
| 772 | /*
|
---|
| 773 | * If the hangup succeeded, it has sent a HANGUP message, the
|
---|
| 774 | * IPC is now in HUNGUP state, we wait for the reply to come
|
---|
| 775 | */
|
---|
[d891cba] | 776 | if (phone->state != IPC_PHONE_FREE) {
|
---|
| 777 | mutex_unlock(&phone->lock);
|
---|
[2405bb5] | 778 | break;
|
---|
[d891cba] | 779 | }
|
---|
| 780 |
|
---|
| 781 | mutex_unlock(&phone->lock);
|
---|
[2405bb5] | 782 | }
|
---|
| 783 |
|
---|
| 784 | /* Got into cleanup */
|
---|
| 785 | if (i == IPC_MAX_PHONES)
|
---|
| 786 | return;
|
---|
| 787 |
|
---|
| 788 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
|
---|
| 789 | SYNCH_FLAGS_NONE);
|
---|
[525e91b] | 790 | ASSERT(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
|
---|
[675fcbd] | 791 |
|
---|
[466e95f7] | 792 | SYSIPC_OP(answer_process, call);
|
---|
[675fcbd] | 793 |
|
---|
[2405bb5] | 794 | ipc_call_free(call);
|
---|
| 795 | goto restart;
|
---|
| 796 | }
|
---|
| 797 |
|
---|
[da1bafb] | 798 | /** Clean up all IPC communication of the current task.
|
---|
[9a1b20c] | 799 | *
|
---|
| 800 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
|
---|
| 801 | * have to change it as well if you want to cleanup other tasks than TASK.
|
---|
[da1bafb] | 802 | *
|
---|
[9a1b20c] | 803 | */
|
---|
| 804 | void ipc_cleanup(void)
|
---|
| 805 | {
|
---|
[c33f39f] | 806 | /*
|
---|
| 807 | * Mark the answerbox as inactive.
|
---|
| 808 | *
|
---|
| 809 | * The main purpose for doing this is to prevent any pending callback
|
---|
| 810 | * connections from getting established beyond this point.
|
---|
| 811 | */
|
---|
| 812 | irq_spinlock_lock(&TASK->answerbox.lock, true);
|
---|
| 813 | TASK->answerbox.active = false;
|
---|
| 814 | irq_spinlock_unlock(&TASK->answerbox.lock, true);
|
---|
| 815 |
|
---|
[9a1b20c] | 816 | /* Disconnect all our phones ('ipc_phone_hangup') */
|
---|
[2405bb5] | 817 | for (size_t i = 0; i < IPC_MAX_PHONES; i++)
|
---|
[9a1b20c] | 818 | ipc_phone_hangup(&TASK->phones[i]);
|
---|
[da1bafb] | 819 |
|
---|
[05641a9e] | 820 | /* Unsubscribe from any event notifications. */
|
---|
| 821 | event_cleanup_answerbox(&TASK->answerbox);
|
---|
[da1bafb] | 822 |
|
---|
[9a1b20c] | 823 | /* Disconnect all connected irqs */
|
---|
| 824 | ipc_irq_cleanup(&TASK->answerbox);
|
---|
[da1bafb] | 825 |
|
---|
[9a1b20c] | 826 | /* Disconnect all phones connected to our regular answerbox */
|
---|
| 827 | ipc_answerbox_slam_phones(&TASK->answerbox, false);
|
---|
[da1bafb] | 828 |
|
---|
[9a1b20c] | 829 | #ifdef CONFIG_UDEBUG
|
---|
| 830 | /* Clean up kbox thread and communications */
|
---|
| 831 | ipc_kbox_cleanup();
|
---|
| 832 | #endif
|
---|
[da1bafb] | 833 |
|
---|
[9f22213] | 834 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */
|
---|
[716185d] | 835 | ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
|
---|
[5d3ed34] | 836 | ipc_cleanup_call_list(&TASK->answerbox,
|
---|
| 837 | &TASK->answerbox.dispatched_calls);
|
---|
[bc117a5] | 838 |
|
---|
[2405bb5] | 839 | ipc_forget_all_active_calls();
|
---|
| 840 | ipc_wait_for_all_answered_calls();
|
---|
[4e49572] | 841 | }
|
---|
[5626277] | 842 |
|
---|
[da1bafb] | 843 | /** Initilize IPC subsystem
|
---|
| 844 | *
|
---|
| 845 | */
|
---|
[5626277] | 846 | void ipc_init(void)
|
---|
| 847 | {
|
---|
[f97f1e51] | 848 | ipc_call_slab = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
|
---|
[8b243f2] | 849 | NULL, 0);
|
---|
[f97f1e51] | 850 | ipc_answerbox_slab = slab_cache_create("answerbox_t",
|
---|
[c70ce74] | 851 | sizeof(answerbox_t), 0, NULL, NULL, 0);
|
---|
[5626277] | 852 | }
|
---|
| 853 |
|
---|
[9c9903a] | 854 |
|
---|
| 855 | static void ipc_print_call_list(list_t *list)
|
---|
| 856 | {
|
---|
[feeac0d] | 857 | list_foreach(*list, ab_link, call_t, call) {
|
---|
[9c9903a] | 858 | #ifdef __32_BITS__
|
---|
| 859 | printf("%10p ", call);
|
---|
| 860 | #endif
|
---|
| 861 |
|
---|
| 862 | #ifdef __64_BITS__
|
---|
| 863 | printf("%18p ", call);
|
---|
| 864 | #endif
|
---|
| 865 |
|
---|
| 866 | spinlock_lock(&call->forget_lock);
|
---|
| 867 |
|
---|
| 868 | printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
|
---|
| 869 | " %-6" PRIun " %-6" PRIun " %-7x",
|
---|
| 870 | IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
| 871 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
| 872 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
| 873 | call->flags);
|
---|
| 874 |
|
---|
| 875 | if (call->forget) {
|
---|
| 876 | printf(" ? (call forgotten)\n");
|
---|
| 877 | } else {
|
---|
| 878 | printf(" %" PRIu64 " (%s)\n",
|
---|
| 879 | call->sender->taskid, call->sender->name);
|
---|
| 880 | }
|
---|
| 881 |
|
---|
| 882 | spinlock_unlock(&call->forget_lock);
|
---|
| 883 | }
|
---|
| 884 | }
|
---|
| 885 |
|
---|
[8b243f2] | 886 | /** List answerbox contents.
|
---|
| 887 | *
|
---|
[da1bafb] | 888 | * @param taskid Task ID.
|
---|
| 889 | *
|
---|
[8b243f2] | 890 | */
|
---|
[c4e4507] | 891 | void ipc_print_task(task_id_t taskid)
|
---|
| 892 | {
|
---|
[da1bafb] | 893 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 894 | task_t *task = task_find_by_id(taskid);
|
---|
| 895 |
|
---|
[170332d] | 896 | if (!task) {
|
---|
[da1bafb] | 897 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[c4e4507] | 898 | return;
|
---|
[170332d] | 899 | }
|
---|
[da1bafb] | 900 |
|
---|
| 901 | /* Hand-over-hand locking */
|
---|
| 902 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
---|
| 903 |
|
---|
[5378f99] | 904 | printf("[phone id] [calls] [state\n");
|
---|
[da1bafb] | 905 |
|
---|
| 906 | size_t i;
|
---|
[8b243f2] | 907 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
[ff48a15] | 908 | if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
|
---|
[5378f99] | 909 | printf("%-10zu (mutex busy)\n", i);
|
---|
[ff48a15] | 910 | continue;
|
---|
| 911 | }
|
---|
[da1bafb] | 912 |
|
---|
[c4e4507] | 913 | if (task->phones[i].state != IPC_PHONE_FREE) {
|
---|
[5378f99] | 914 | printf("%-10zu %7" PRIun " ", i,
|
---|
| 915 | atomic_get(&task->phones[i].active_calls));
|
---|
[da1bafb] | 916 |
|
---|
[c4e4507] | 917 | switch (task->phones[i].state) {
|
---|
| 918 | case IPC_PHONE_CONNECTING:
|
---|
[5378f99] | 919 | printf("connecting");
|
---|
[c4e4507] | 920 | break;
|
---|
| 921 | case IPC_PHONE_CONNECTED:
|
---|
[5378f99] | 922 | printf("connected to %" PRIu64 " (%s)",
|
---|
| 923 | task->phones[i].callee->task->taskid,
|
---|
| 924 | task->phones[i].callee->task->name);
|
---|
[c4e4507] | 925 | break;
|
---|
| 926 | case IPC_PHONE_SLAMMED:
|
---|
[5378f99] | 927 | printf("slammed by %p",
|
---|
[da1bafb] | 928 | task->phones[i].callee);
|
---|
[c4e4507] | 929 | break;
|
---|
| 930 | case IPC_PHONE_HUNGUP:
|
---|
[5378f99] | 931 | printf("hung up by %p",
|
---|
[da1bafb] | 932 | task->phones[i].callee);
|
---|
[c4e4507] | 933 | break;
|
---|
| 934 | default:
|
---|
| 935 | break;
|
---|
| 936 | }
|
---|
[da1bafb] | 937 |
|
---|
[5378f99] | 938 | printf("\n");
|
---|
[c4e4507] | 939 | }
|
---|
[da1bafb] | 940 |
|
---|
[ff48a15] | 941 | mutex_unlock(&task->phones[i].lock);
|
---|
[c4e4507] | 942 | }
|
---|
[da1bafb] | 943 |
|
---|
| 944 | irq_spinlock_lock(&task->answerbox.lock, false);
|
---|
| 945 |
|
---|
[5378f99] | 946 | #ifdef __32_BITS__
|
---|
| 947 | printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
|
---|
| 948 | " [flags] [sender\n");
|
---|
| 949 | #endif
|
---|
| 950 |
|
---|
| 951 | #ifdef __64_BITS__
|
---|
| 952 | printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
|
---|
| 953 | " [arg5] [flags] [sender\n");
|
---|
| 954 | #endif
|
---|
| 955 |
|
---|
| 956 | printf(" --- incomming calls ---\n");
|
---|
[9c9903a] | 957 | ipc_print_call_list(&task->answerbox.calls);
|
---|
[5378f99] | 958 | printf(" --- dispatched calls ---\n");
|
---|
[9c9903a] | 959 | ipc_print_call_list(&task->answerbox.dispatched_calls);
|
---|
[6aef742] | 960 | printf(" --- incoming answers ---\n");
|
---|
[9c9903a] | 961 | ipc_print_call_list(&task->answerbox.answers);
|
---|
[da1bafb] | 962 |
|
---|
| 963 | irq_spinlock_unlock(&task->answerbox.lock, false);
|
---|
| 964 | irq_spinlock_unlock(&task->lock, true);
|
---|
[c4e4507] | 965 | }
|
---|
[b45c443] | 966 |
|
---|
[cc73a8a1] | 967 | /** @}
|
---|
[b45c443] | 968 | */
|
---|