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