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