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