| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup genericipc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | /* Lock ordering
|
|---|
| 36 | *
|
|---|
| 37 | * First the answerbox, then the phone.
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| 40 | #include <synch/spinlock.h>
|
|---|
| 41 | #include <synch/mutex.h>
|
|---|
| 42 | #include <synch/waitq.h>
|
|---|
| 43 | #include <ipc/ipc.h>
|
|---|
| 44 | #include <abi/ipc/methods.h>
|
|---|
| 45 | #include <ipc/kbox.h>
|
|---|
| 46 | #include <ipc/event.h>
|
|---|
| 47 | #include <errno.h>
|
|---|
| 48 | #include <mm/slab.h>
|
|---|
| 49 | #include <arch.h>
|
|---|
| 50 | #include <proc/task.h>
|
|---|
| 51 | #include <memstr.h>
|
|---|
| 52 | #include <debug.h>
|
|---|
| 53 | #include <print.h>
|
|---|
| 54 | #include <console/console.h>
|
|---|
| 55 | #include <proc/thread.h>
|
|---|
| 56 | #include <arch/interrupt.h>
|
|---|
| 57 | #include <ipc/irq.h>
|
|---|
| 58 |
|
|---|
| 59 | /** Open channel that is assigned automatically to new tasks */
|
|---|
| 60 | answerbox_t *ipc_phone_0 = NULL;
|
|---|
| 61 |
|
|---|
| 62 | static slab_cache_t *ipc_call_slab;
|
|---|
| 63 | static slab_cache_t *ipc_answerbox_slab;
|
|---|
| 64 |
|
|---|
| 65 | /** Initialize a call structure.
|
|---|
| 66 | *
|
|---|
| 67 | * @param call Call structure to be initialized.
|
|---|
| 68 | *
|
|---|
| 69 | */
|
|---|
| 70 | static void _ipc_call_init(call_t *call)
|
|---|
| 71 | {
|
|---|
| 72 | memsetb(call, sizeof(*call), 0);
|
|---|
| 73 | call->callerbox = &TASK->answerbox;
|
|---|
| 74 | call->sender = TASK;
|
|---|
| 75 | call->buffer = NULL;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /** Allocate and initialize a call structure.
|
|---|
| 79 | *
|
|---|
| 80 | * The call is initialized, so that the reply will be directed to
|
|---|
| 81 | * TASK->answerbox.
|
|---|
| 82 | *
|
|---|
| 83 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
|
|---|
| 84 | *
|
|---|
| 85 | * @return If flags permit it, return NULL, or initialized kernel
|
|---|
| 86 | * call structure.
|
|---|
| 87 | *
|
|---|
| 88 | */
|
|---|
| 89 | call_t *ipc_call_alloc(unsigned int flags)
|
|---|
| 90 | {
|
|---|
| 91 | call_t *call = slab_alloc(ipc_call_slab, flags);
|
|---|
| 92 | if (call)
|
|---|
| 93 | _ipc_call_init(call);
|
|---|
| 94 |
|
|---|
| 95 | return call;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /** Deallocate a call structure.
|
|---|
| 99 | *
|
|---|
| 100 | * @param call Call structure to be freed.
|
|---|
| 101 | *
|
|---|
| 102 | */
|
|---|
| 103 | void ipc_call_free(call_t *call)
|
|---|
| 104 | {
|
|---|
| 105 | /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
|
|---|
| 106 | if (call->buffer)
|
|---|
| 107 | free(call->buffer);
|
|---|
| 108 | slab_free(ipc_call_slab, call);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /** Initialize an answerbox structure.
|
|---|
| 112 | *
|
|---|
| 113 | * @param box Answerbox structure to be initialized.
|
|---|
| 114 | * @param task Task to which the answerbox belongs.
|
|---|
| 115 | *
|
|---|
| 116 | */
|
|---|
| 117 | void ipc_answerbox_init(answerbox_t *box, task_t *task)
|
|---|
| 118 | {
|
|---|
| 119 | irq_spinlock_initialize(&box->lock, "ipc.box.lock");
|
|---|
| 120 | irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
|
|---|
| 121 | waitq_initialize(&box->wq);
|
|---|
| 122 | link_initialize(&box->sync_box_link);
|
|---|
| 123 | list_initialize(&box->connected_phones);
|
|---|
| 124 | list_initialize(&box->calls);
|
|---|
| 125 | list_initialize(&box->dispatched_calls);
|
|---|
| 126 | list_initialize(&box->answers);
|
|---|
| 127 | list_initialize(&box->irq_notifs);
|
|---|
| 128 | list_initialize(&box->irq_list);
|
|---|
| 129 | box->task = task;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /** Connect a phone to an answerbox.
|
|---|
| 133 | *
|
|---|
| 134 | * @param phone Initialized phone structure.
|
|---|
| 135 | * @param box Initialized answerbox structure.
|
|---|
| 136 | *
|
|---|
| 137 | */
|
|---|
| 138 | void ipc_phone_connect(phone_t *phone, answerbox_t *box)
|
|---|
| 139 | {
|
|---|
| 140 | mutex_lock(&phone->lock);
|
|---|
| 141 |
|
|---|
| 142 | phone->state = IPC_PHONE_CONNECTED;
|
|---|
| 143 | phone->callee = box;
|
|---|
| 144 |
|
|---|
| 145 | irq_spinlock_lock(&box->lock, true);
|
|---|
| 146 | list_append(&phone->link, &box->connected_phones);
|
|---|
| 147 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 148 |
|
|---|
| 149 | mutex_unlock(&phone->lock);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /** Initialize a phone structure.
|
|---|
| 153 | *
|
|---|
| 154 | * @param phone Phone structure to be initialized.
|
|---|
| 155 | *
|
|---|
| 156 | */
|
|---|
| 157 | void ipc_phone_init(phone_t *phone)
|
|---|
| 158 | {
|
|---|
| 159 | mutex_initialize(&phone->lock, MUTEX_PASSIVE);
|
|---|
| 160 | phone->callee = NULL;
|
|---|
| 161 | phone->state = IPC_PHONE_FREE;
|
|---|
| 162 | atomic_set(&phone->active_calls, 0);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** Helper function to facilitate synchronous calls.
|
|---|
| 166 | *
|
|---|
| 167 | * @param phone Destination kernel phone structure.
|
|---|
| 168 | * @param request Call structure with request.
|
|---|
| 169 | *
|
|---|
| 170 | * @return EOK on success or EINTR if the sleep was interrupted.
|
|---|
| 171 | *
|
|---|
| 172 | */
|
|---|
| 173 | int ipc_call_sync(phone_t *phone, call_t *request)
|
|---|
| 174 | {
|
|---|
| 175 | answerbox_t *sync_box = slab_alloc(ipc_answerbox_slab, 0);
|
|---|
| 176 | ipc_answerbox_init(sync_box, TASK);
|
|---|
| 177 |
|
|---|
| 178 | /*
|
|---|
| 179 | * Put the answerbox on the TASK's list of synchronous answerboxes so
|
|---|
| 180 | * that it can be cleaned up if the call is interrupted.
|
|---|
| 181 | */
|
|---|
| 182 | irq_spinlock_lock(&TASK->lock, true);
|
|---|
| 183 | list_append(&sync_box->sync_box_link, &TASK->sync_boxes);
|
|---|
| 184 | irq_spinlock_unlock(&TASK->lock, true);
|
|---|
| 185 |
|
|---|
| 186 | /* We will receive data in a special box. */
|
|---|
| 187 | request->callerbox = sync_box;
|
|---|
| 188 |
|
|---|
| 189 | ipc_call(phone, request);
|
|---|
| 190 | if (!ipc_wait_for_call(sync_box, SYNCH_NO_TIMEOUT,
|
|---|
| 191 | SYNCH_FLAGS_INTERRUPTIBLE)) {
|
|---|
| 192 | /* The answerbox and the call will be freed by ipc_cleanup(). */
|
|---|
| 193 | return EINTR;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /*
|
|---|
| 197 | * The answer arrived without interruption so we can remove the
|
|---|
| 198 | * answerbox from the TASK's list of synchronous answerboxes.
|
|---|
| 199 | */
|
|---|
| 200 | irq_spinlock_lock(&TASK->lock, true);
|
|---|
| 201 | list_remove(&sync_box->sync_box_link);
|
|---|
| 202 | irq_spinlock_unlock(&TASK->lock, true);
|
|---|
| 203 |
|
|---|
| 204 | slab_free(ipc_answerbox_slab, sync_box);
|
|---|
| 205 | return EOK;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /** Answer a message which was not dispatched and is not listed in any queue.
|
|---|
| 209 | *
|
|---|
| 210 | * @param call Call structure to be answered.
|
|---|
| 211 | * @param selflocked If true, then TASK->answebox is locked.
|
|---|
| 212 | *
|
|---|
| 213 | */
|
|---|
| 214 | static void _ipc_answer_free_call(call_t *call, bool selflocked)
|
|---|
| 215 | {
|
|---|
| 216 | answerbox_t *callerbox = call->callerbox;
|
|---|
| 217 | bool do_lock = ((!selflocked) || callerbox != (&TASK->answerbox));
|
|---|
| 218 |
|
|---|
| 219 | /* Count sent answer */
|
|---|
| 220 | irq_spinlock_lock(&TASK->lock, true);
|
|---|
| 221 | TASK->ipc_info.answer_sent++;
|
|---|
| 222 | irq_spinlock_unlock(&TASK->lock, true);
|
|---|
| 223 |
|
|---|
| 224 | call->flags |= IPC_CALL_ANSWERED;
|
|---|
| 225 |
|
|---|
| 226 | if (call->flags & IPC_CALL_FORWARDED) {
|
|---|
| 227 | if (call->caller_phone) {
|
|---|
| 228 | /* Demasquerade the caller phone. */
|
|---|
| 229 | call->data.phone = call->caller_phone;
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | call->data.task_id = TASK->taskid;
|
|---|
| 234 |
|
|---|
| 235 | if (do_lock)
|
|---|
| 236 | irq_spinlock_lock(&callerbox->lock, true);
|
|---|
| 237 |
|
|---|
| 238 | list_append(&call->link, &callerbox->answers);
|
|---|
| 239 |
|
|---|
| 240 | if (do_lock)
|
|---|
| 241 | irq_spinlock_unlock(&callerbox->lock, true);
|
|---|
| 242 |
|
|---|
| 243 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | /** Answer a message which is in a callee queue.
|
|---|
| 247 | *
|
|---|
| 248 | * @param box Answerbox that is answering the message.
|
|---|
| 249 | * @param call Modified request that is being sent back.
|
|---|
| 250 | *
|
|---|
| 251 | */
|
|---|
| 252 | void ipc_answer(answerbox_t *box, call_t *call)
|
|---|
| 253 | {
|
|---|
| 254 | /* Remove from active box */
|
|---|
| 255 | irq_spinlock_lock(&box->lock, true);
|
|---|
| 256 | list_remove(&call->link);
|
|---|
| 257 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 258 |
|
|---|
| 259 | /* Send back answer */
|
|---|
| 260 | _ipc_answer_free_call(call, false);
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /** Simulate sending back a message.
|
|---|
| 264 | *
|
|---|
| 265 | * Most errors are better handled by forming a normal backward
|
|---|
| 266 | * message and sending it as a normal answer.
|
|---|
| 267 | *
|
|---|
| 268 | * @param phone Phone structure the call should appear to come from.
|
|---|
| 269 | * @param call Call structure to be answered.
|
|---|
| 270 | * @param err Return value to be used for the answer.
|
|---|
| 271 | *
|
|---|
| 272 | */
|
|---|
| 273 | void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
|
|---|
| 274 | {
|
|---|
| 275 | call->data.phone = phone;
|
|---|
| 276 | atomic_inc(&phone->active_calls);
|
|---|
| 277 | IPC_SET_RETVAL(call->data, err);
|
|---|
| 278 | _ipc_answer_free_call(call, false);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /** Unsafe unchecking version of ipc_call.
|
|---|
| 282 | *
|
|---|
| 283 | * @param phone Phone structure the call comes from.
|
|---|
| 284 | * @param box Destination answerbox structure.
|
|---|
| 285 | * @param call Call structure with request.
|
|---|
| 286 | *
|
|---|
| 287 | */
|
|---|
| 288 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
|
|---|
| 289 | {
|
|---|
| 290 | /* Count sent ipc call */
|
|---|
| 291 | irq_spinlock_lock(&TASK->lock, true);
|
|---|
| 292 | TASK->ipc_info.call_sent++;
|
|---|
| 293 | irq_spinlock_unlock(&TASK->lock, true);
|
|---|
| 294 |
|
|---|
| 295 | if (!(call->flags & IPC_CALL_FORWARDED)) {
|
|---|
| 296 | atomic_inc(&phone->active_calls);
|
|---|
| 297 | call->data.phone = phone;
|
|---|
| 298 | call->data.task_id = TASK->taskid;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | irq_spinlock_lock(&box->lock, true);
|
|---|
| 302 | list_append(&call->link, &box->calls);
|
|---|
| 303 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 304 |
|
|---|
| 305 | waitq_wakeup(&box->wq, WAKEUP_FIRST);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /** Send an asynchronous request using a phone to an answerbox.
|
|---|
| 309 | *
|
|---|
| 310 | * @param phone Phone structure the call comes from and which is
|
|---|
| 311 | * connected to the destination answerbox.
|
|---|
| 312 | * @param call Call structure with request.
|
|---|
| 313 | *
|
|---|
| 314 | * @return Return 0 on success, ENOENT on error.
|
|---|
| 315 | *
|
|---|
| 316 | */
|
|---|
| 317 | int ipc_call(phone_t *phone, call_t *call)
|
|---|
| 318 | {
|
|---|
| 319 | mutex_lock(&phone->lock);
|
|---|
| 320 | if (phone->state != IPC_PHONE_CONNECTED) {
|
|---|
| 321 | mutex_unlock(&phone->lock);
|
|---|
| 322 | if (call->flags & IPC_CALL_FORWARDED) {
|
|---|
| 323 | IPC_SET_RETVAL(call->data, EFORWARD);
|
|---|
| 324 | _ipc_answer_free_call(call, false);
|
|---|
| 325 | } else {
|
|---|
| 326 | if (phone->state == IPC_PHONE_HUNGUP)
|
|---|
| 327 | ipc_backsend_err(phone, call, EHANGUP);
|
|---|
| 328 | else
|
|---|
| 329 | ipc_backsend_err(phone, call, ENOENT);
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | return ENOENT;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | answerbox_t *box = phone->callee;
|
|---|
| 336 | _ipc_call(phone, box, call);
|
|---|
| 337 |
|
|---|
| 338 | mutex_unlock(&phone->lock);
|
|---|
| 339 | return 0;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | /** Disconnect phone from answerbox.
|
|---|
| 343 | *
|
|---|
| 344 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done
|
|---|
| 345 | * lazily later.
|
|---|
| 346 | *
|
|---|
| 347 | * @param phone Phone structure to be hung up.
|
|---|
| 348 | *
|
|---|
| 349 | * @return 0 if the phone is disconnected.
|
|---|
| 350 | * @return -1 if the phone was already disconnected.
|
|---|
| 351 | *
|
|---|
| 352 | */
|
|---|
| 353 | int ipc_phone_hangup(phone_t *phone)
|
|---|
| 354 | {
|
|---|
| 355 | mutex_lock(&phone->lock);
|
|---|
| 356 | if (phone->state == IPC_PHONE_FREE ||
|
|---|
| 357 | phone->state == IPC_PHONE_HUNGUP ||
|
|---|
| 358 | phone->state == IPC_PHONE_CONNECTING) {
|
|---|
| 359 | mutex_unlock(&phone->lock);
|
|---|
| 360 | return -1;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | answerbox_t *box = phone->callee;
|
|---|
| 364 | if (phone->state != IPC_PHONE_SLAMMED) {
|
|---|
| 365 | /* Remove myself from answerbox */
|
|---|
| 366 | irq_spinlock_lock(&box->lock, true);
|
|---|
| 367 | list_remove(&phone->link);
|
|---|
| 368 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 369 |
|
|---|
| 370 | call_t *call = ipc_call_alloc(0);
|
|---|
| 371 | IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
|
|---|
| 372 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
|---|
| 373 | _ipc_call(phone, box, call);
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | phone->state = IPC_PHONE_HUNGUP;
|
|---|
| 377 | mutex_unlock(&phone->lock);
|
|---|
| 378 |
|
|---|
| 379 | return 0;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | /** Forwards call from one answerbox to another one.
|
|---|
| 383 | *
|
|---|
| 384 | * @param call Call structure to be redirected.
|
|---|
| 385 | * @param newphone Phone structure to target answerbox.
|
|---|
| 386 | * @param oldbox Old answerbox structure.
|
|---|
| 387 | * @param mode Flags that specify mode of the forward operation.
|
|---|
| 388 | *
|
|---|
| 389 | * @return 0 if forwarding succeeded or an error code if
|
|---|
| 390 | * there was an error.
|
|---|
| 391 | *
|
|---|
| 392 | * The return value serves only as an information for the forwarder,
|
|---|
| 393 | * the original caller is notified automatically with EFORWARD.
|
|---|
| 394 | *
|
|---|
| 395 | */
|
|---|
| 396 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
|
|---|
| 397 | unsigned int mode)
|
|---|
| 398 | {
|
|---|
| 399 | /* Count forwarded calls */
|
|---|
| 400 | irq_spinlock_lock(&TASK->lock, true);
|
|---|
| 401 | TASK->ipc_info.forwarded++;
|
|---|
| 402 | irq_spinlock_pass(&TASK->lock, &oldbox->lock);
|
|---|
| 403 | list_remove(&call->link);
|
|---|
| 404 | irq_spinlock_unlock(&oldbox->lock, true);
|
|---|
| 405 |
|
|---|
| 406 | if (mode & IPC_FF_ROUTE_FROM_ME) {
|
|---|
| 407 | if (!call->caller_phone)
|
|---|
| 408 | call->caller_phone = call->data.phone;
|
|---|
| 409 | call->data.phone = newphone;
|
|---|
| 410 | call->data.task_id = TASK->taskid;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | return ipc_call(newphone, call);
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 | /** Wait for a phone call.
|
|---|
| 418 | *
|
|---|
| 419 | * @param box Answerbox expecting the call.
|
|---|
| 420 | * @param usec Timeout in microseconds. See documentation for
|
|---|
| 421 | * waitq_sleep_timeout() for decription of its special
|
|---|
| 422 | * meaning.
|
|---|
| 423 | * @param flags Select mode of sleep operation. See documentation for
|
|---|
| 424 | * waitq_sleep_timeout() for description of its special
|
|---|
| 425 | * meaning.
|
|---|
| 426 | *
|
|---|
| 427 | * @return Recived call structure or NULL.
|
|---|
| 428 | *
|
|---|
| 429 | * To distinguish between a call and an answer, have a look at call->flags.
|
|---|
| 430 | *
|
|---|
| 431 | */
|
|---|
| 432 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
|
|---|
| 433 | {
|
|---|
| 434 | call_t *request;
|
|---|
| 435 | uint64_t irq_cnt = 0;
|
|---|
| 436 | uint64_t answer_cnt = 0;
|
|---|
| 437 | uint64_t call_cnt = 0;
|
|---|
| 438 | int rc;
|
|---|
| 439 |
|
|---|
| 440 | restart:
|
|---|
| 441 | rc = waitq_sleep_timeout(&box->wq, usec, flags);
|
|---|
| 442 | if (SYNCH_FAILED(rc))
|
|---|
| 443 | return NULL;
|
|---|
| 444 |
|
|---|
| 445 | irq_spinlock_lock(&box->lock, true);
|
|---|
| 446 | if (!list_empty(&box->irq_notifs)) {
|
|---|
| 447 | /* Count received IRQ notification */
|
|---|
| 448 | irq_cnt++;
|
|---|
| 449 |
|
|---|
| 450 | irq_spinlock_lock(&box->irq_lock, false);
|
|---|
| 451 |
|
|---|
| 452 | request = list_get_instance(list_first(&box->irq_notifs),
|
|---|
| 453 | call_t, link);
|
|---|
| 454 | list_remove(&request->link);
|
|---|
| 455 |
|
|---|
| 456 | irq_spinlock_unlock(&box->irq_lock, false);
|
|---|
| 457 | } else if (!list_empty(&box->answers)) {
|
|---|
| 458 | /* Count received answer */
|
|---|
| 459 | answer_cnt++;
|
|---|
| 460 |
|
|---|
| 461 | /* Handle asynchronous answers */
|
|---|
| 462 | request = list_get_instance(list_first(&box->answers),
|
|---|
| 463 | call_t, link);
|
|---|
| 464 | list_remove(&request->link);
|
|---|
| 465 | atomic_dec(&request->data.phone->active_calls);
|
|---|
| 466 | } else if (!list_empty(&box->calls)) {
|
|---|
| 467 | /* Count received call */
|
|---|
| 468 | call_cnt++;
|
|---|
| 469 |
|
|---|
| 470 | /* Handle requests */
|
|---|
| 471 | request = list_get_instance(list_first(&box->calls),
|
|---|
| 472 | call_t, link);
|
|---|
| 473 | list_remove(&request->link);
|
|---|
| 474 |
|
|---|
| 475 | /* Append request to dispatch queue */
|
|---|
| 476 | list_append(&request->link, &box->dispatched_calls);
|
|---|
| 477 | } else {
|
|---|
| 478 | /* This can happen regularly after ipc_cleanup */
|
|---|
| 479 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 480 | goto restart;
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | irq_spinlock_pass(&box->lock, &TASK->lock);
|
|---|
| 484 |
|
|---|
| 485 | TASK->ipc_info.irq_notif_received += irq_cnt;
|
|---|
| 486 | TASK->ipc_info.answer_received += answer_cnt;
|
|---|
| 487 | TASK->ipc_info.call_received += call_cnt;
|
|---|
| 488 |
|
|---|
| 489 | irq_spinlock_unlock(&TASK->lock, true);
|
|---|
| 490 |
|
|---|
| 491 | return request;
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | /** Answer all calls from list with EHANGUP answer.
|
|---|
| 495 | *
|
|---|
| 496 | * @param lst Head of the list to be cleaned up.
|
|---|
| 497 | *
|
|---|
| 498 | */
|
|---|
| 499 | void ipc_cleanup_call_list(list_t *lst)
|
|---|
| 500 | {
|
|---|
| 501 | while (!list_empty(lst)) {
|
|---|
| 502 | call_t *call = list_get_instance(list_first(lst), call_t, link);
|
|---|
| 503 | if (call->buffer)
|
|---|
| 504 | free(call->buffer);
|
|---|
| 505 |
|
|---|
| 506 | list_remove(&call->link);
|
|---|
| 507 |
|
|---|
| 508 | IPC_SET_RETVAL(call->data, EHANGUP);
|
|---|
| 509 | _ipc_answer_free_call(call, true);
|
|---|
| 510 | }
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | /** Disconnects all phones connected to an answerbox.
|
|---|
| 514 | *
|
|---|
| 515 | * @param box Answerbox to disconnect phones from.
|
|---|
| 516 | * @param notify_box If true, the answerbox will get a hangup message for
|
|---|
| 517 | * each disconnected phone.
|
|---|
| 518 | *
|
|---|
| 519 | */
|
|---|
| 520 | void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
|
|---|
| 521 | {
|
|---|
| 522 | phone_t *phone;
|
|---|
| 523 | DEADLOCK_PROBE_INIT(p_phonelck);
|
|---|
| 524 |
|
|---|
| 525 | call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
|
|---|
| 526 |
|
|---|
| 527 | /* Disconnect all phones connected to our answerbox */
|
|---|
| 528 | restart_phones:
|
|---|
| 529 | irq_spinlock_lock(&box->lock, true);
|
|---|
| 530 | while (!list_empty(&box->connected_phones)) {
|
|---|
| 531 | phone = list_get_instance(list_first(&box->connected_phones),
|
|---|
| 532 | phone_t, link);
|
|---|
| 533 | if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
|
|---|
| 534 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 535 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
|
|---|
| 536 | goto restart_phones;
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | /* Disconnect phone */
|
|---|
| 540 | ASSERT(phone->state == IPC_PHONE_CONNECTED);
|
|---|
| 541 |
|
|---|
| 542 | list_remove(&phone->link);
|
|---|
| 543 | phone->state = IPC_PHONE_SLAMMED;
|
|---|
| 544 |
|
|---|
| 545 | if (notify_box) {
|
|---|
| 546 | mutex_unlock(&phone->lock);
|
|---|
| 547 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 548 |
|
|---|
| 549 | /*
|
|---|
| 550 | * Send one message to the answerbox for each
|
|---|
| 551 | * phone. Used to make sure the kbox thread
|
|---|
| 552 | * wakes up after the last phone has been
|
|---|
| 553 | * disconnected.
|
|---|
| 554 | */
|
|---|
| 555 | IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
|
|---|
| 556 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
|---|
| 557 | _ipc_call(phone, box, call);
|
|---|
| 558 |
|
|---|
| 559 | /* Allocate another call in advance */
|
|---|
| 560 | call = ipc_call_alloc(0);
|
|---|
| 561 |
|
|---|
| 562 | /* Must start again */
|
|---|
| 563 | goto restart_phones;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | mutex_unlock(&phone->lock);
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | irq_spinlock_unlock(&box->lock, true);
|
|---|
| 570 |
|
|---|
| 571 | /* Free unused call */
|
|---|
| 572 | if (call)
|
|---|
| 573 | ipc_call_free(call);
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | /** Clean up all IPC communication of the current task.
|
|---|
| 577 | *
|
|---|
| 578 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
|
|---|
| 579 | * have to change it as well if you want to cleanup other tasks than TASK.
|
|---|
| 580 | *
|
|---|
| 581 | */
|
|---|
| 582 | void ipc_cleanup(void)
|
|---|
| 583 | {
|
|---|
| 584 | /* Disconnect all our phones ('ipc_phone_hangup') */
|
|---|
| 585 | size_t i;
|
|---|
| 586 | for (i = 0; i < IPC_MAX_PHONES; i++)
|
|---|
| 587 | ipc_phone_hangup(&TASK->phones[i]);
|
|---|
| 588 |
|
|---|
| 589 | /* Unsubscribe from any event notifications. */
|
|---|
| 590 | event_cleanup_answerbox(&TASK->answerbox);
|
|---|
| 591 |
|
|---|
| 592 | /* Disconnect all connected irqs */
|
|---|
| 593 | ipc_irq_cleanup(&TASK->answerbox);
|
|---|
| 594 |
|
|---|
| 595 | /* Disconnect all phones connected to our regular answerbox */
|
|---|
| 596 | ipc_answerbox_slam_phones(&TASK->answerbox, false);
|
|---|
| 597 |
|
|---|
| 598 | #ifdef CONFIG_UDEBUG
|
|---|
| 599 | /* Clean up kbox thread and communications */
|
|---|
| 600 | ipc_kbox_cleanup();
|
|---|
| 601 | #endif
|
|---|
| 602 |
|
|---|
| 603 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */
|
|---|
| 604 | irq_spinlock_lock(&TASK->answerbox.lock, true);
|
|---|
| 605 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
|
|---|
| 606 | ipc_cleanup_call_list(&TASK->answerbox.calls);
|
|---|
| 607 | irq_spinlock_unlock(&TASK->answerbox.lock, true);
|
|---|
| 608 |
|
|---|
| 609 | /* Wait for all answers to interrupted synchronous calls to arrive */
|
|---|
| 610 | ipl_t ipl = interrupts_disable();
|
|---|
| 611 | while (!list_empty(&TASK->sync_boxes)) {
|
|---|
| 612 | answerbox_t *box = list_get_instance(
|
|---|
| 613 | list_first(&TASK->sync_boxes), answerbox_t, sync_box_link);
|
|---|
| 614 |
|
|---|
| 615 | list_remove(&box->sync_box_link);
|
|---|
| 616 | call_t *call = ipc_wait_for_call(box, SYNCH_NO_TIMEOUT,
|
|---|
| 617 | SYNCH_FLAGS_NONE);
|
|---|
| 618 | ipc_call_free(call);
|
|---|
| 619 | slab_free(ipc_answerbox_slab, box);
|
|---|
| 620 | }
|
|---|
| 621 | interrupts_restore(ipl);
|
|---|
| 622 |
|
|---|
| 623 | /* Wait for all answers to asynchronous calls to arrive */
|
|---|
| 624 | while (true) {
|
|---|
| 625 | /*
|
|---|
| 626 | * Go through all phones, until they are all FREE
|
|---|
| 627 | * Locking is not needed, no one else should modify
|
|---|
| 628 | * it when we are in cleanup
|
|---|
| 629 | */
|
|---|
| 630 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
|---|
| 631 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
|
|---|
| 632 | atomic_get(&TASK->phones[i].active_calls) == 0) {
|
|---|
| 633 | TASK->phones[i].state = IPC_PHONE_FREE;
|
|---|
| 634 | TASK->phones[i].callee = NULL;
|
|---|
| 635 | }
|
|---|
| 636 |
|
|---|
| 637 | /*
|
|---|
| 638 | * Just for sure, we might have had some
|
|---|
| 639 | * IPC_PHONE_CONNECTING phones
|
|---|
| 640 | */
|
|---|
| 641 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
|
|---|
| 642 | ipc_phone_hangup(&TASK->phones[i]);
|
|---|
| 643 |
|
|---|
| 644 | /*
|
|---|
| 645 | * If the hangup succeeded, it has sent a HANGUP
|
|---|
| 646 | * message, the IPC is now in HUNGUP state, we
|
|---|
| 647 | * wait for the reply to come
|
|---|
| 648 | */
|
|---|
| 649 |
|
|---|
| 650 | if (TASK->phones[i].state != IPC_PHONE_FREE)
|
|---|
| 651 | break;
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 | /* Got into cleanup */
|
|---|
| 655 | if (i == IPC_MAX_PHONES)
|
|---|
| 656 | break;
|
|---|
| 657 |
|
|---|
| 658 | call_t *call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
|
|---|
| 659 | SYNCH_FLAGS_NONE);
|
|---|
| 660 | ASSERT((call->flags & IPC_CALL_ANSWERED) ||
|
|---|
| 661 | (call->flags & IPC_CALL_NOTIF));
|
|---|
| 662 |
|
|---|
| 663 | ipc_call_free(call);
|
|---|
| 664 | }
|
|---|
| 665 | }
|
|---|
| 666 |
|
|---|
| 667 | /** Initilize IPC subsystem
|
|---|
| 668 | *
|
|---|
| 669 | */
|
|---|
| 670 | void ipc_init(void)
|
|---|
| 671 | {
|
|---|
| 672 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
|
|---|
| 673 | NULL, 0);
|
|---|
| 674 | ipc_answerbox_slab = slab_cache_create("ipc_answerbox",
|
|---|
| 675 | sizeof(answerbox_t), 0, NULL, NULL, 0);
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | /** List answerbox contents.
|
|---|
| 679 | *
|
|---|
| 680 | * @param taskid Task ID.
|
|---|
| 681 | *
|
|---|
| 682 | */
|
|---|
| 683 | void ipc_print_task(task_id_t taskid)
|
|---|
| 684 | {
|
|---|
| 685 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 686 | task_t *task = task_find_by_id(taskid);
|
|---|
| 687 |
|
|---|
| 688 | if (!task) {
|
|---|
| 689 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 690 | return;
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | /* Hand-over-hand locking */
|
|---|
| 694 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
|---|
| 695 |
|
|---|
| 696 | printf("[phone id] [calls] [state\n");
|
|---|
| 697 |
|
|---|
| 698 | size_t i;
|
|---|
| 699 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
|---|
| 700 | if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
|
|---|
| 701 | printf("%-10zu (mutex busy)\n", i);
|
|---|
| 702 | continue;
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | if (task->phones[i].state != IPC_PHONE_FREE) {
|
|---|
| 706 | printf("%-10zu %7" PRIun " ", i,
|
|---|
| 707 | atomic_get(&task->phones[i].active_calls));
|
|---|
| 708 |
|
|---|
| 709 | switch (task->phones[i].state) {
|
|---|
| 710 | case IPC_PHONE_CONNECTING:
|
|---|
| 711 | printf("connecting");
|
|---|
| 712 | break;
|
|---|
| 713 | case IPC_PHONE_CONNECTED:
|
|---|
| 714 | printf("connected to %" PRIu64 " (%s)",
|
|---|
| 715 | task->phones[i].callee->task->taskid,
|
|---|
| 716 | task->phones[i].callee->task->name);
|
|---|
| 717 | break;
|
|---|
| 718 | case IPC_PHONE_SLAMMED:
|
|---|
| 719 | printf("slammed by %p",
|
|---|
| 720 | task->phones[i].callee);
|
|---|
| 721 | break;
|
|---|
| 722 | case IPC_PHONE_HUNGUP:
|
|---|
| 723 | printf("hung up by %p",
|
|---|
| 724 | task->phones[i].callee);
|
|---|
| 725 | break;
|
|---|
| 726 | default:
|
|---|
| 727 | break;
|
|---|
| 728 | }
|
|---|
| 729 |
|
|---|
| 730 | printf("\n");
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | mutex_unlock(&task->phones[i].lock);
|
|---|
| 734 | }
|
|---|
| 735 |
|
|---|
| 736 | irq_spinlock_lock(&task->answerbox.lock, false);
|
|---|
| 737 |
|
|---|
| 738 | #ifdef __32_BITS__
|
|---|
| 739 | printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
|
|---|
| 740 | " [flags] [sender\n");
|
|---|
| 741 | #endif
|
|---|
| 742 |
|
|---|
| 743 | #ifdef __64_BITS__
|
|---|
| 744 | printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
|
|---|
| 745 | " [arg5] [flags] [sender\n");
|
|---|
| 746 | #endif
|
|---|
| 747 |
|
|---|
| 748 | printf(" --- incomming calls ---\n");
|
|---|
| 749 | list_foreach(task->answerbox.calls, cur) {
|
|---|
| 750 | call_t *call = list_get_instance(cur, call_t, link);
|
|---|
| 751 |
|
|---|
| 752 | #ifdef __32_BITS__
|
|---|
| 753 | printf("%10p ", call);
|
|---|
| 754 | #endif
|
|---|
| 755 |
|
|---|
| 756 | #ifdef __64_BITS__
|
|---|
| 757 | printf("%18p ", call);
|
|---|
| 758 | #endif
|
|---|
| 759 |
|
|---|
| 760 | printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
|
|---|
| 761 | " %-6" PRIun " %-6" PRIun " %-7x %" PRIu64 " (%s)\n",
|
|---|
| 762 | IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
|
|---|
| 763 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
|---|
| 764 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
|---|
| 765 | call->flags, call->sender->taskid, call->sender->name);
|
|---|
| 766 | }
|
|---|
| 767 |
|
|---|
| 768 | printf(" --- dispatched calls ---\n");
|
|---|
| 769 | list_foreach(task->answerbox.dispatched_calls, cur) {
|
|---|
| 770 | call_t *call = list_get_instance(cur, call_t, link);
|
|---|
| 771 |
|
|---|
| 772 | #ifdef __32_BITS__
|
|---|
| 773 | printf("%10p ", call);
|
|---|
| 774 | #endif
|
|---|
| 775 |
|
|---|
| 776 | #ifdef __64_BITS__
|
|---|
| 777 | printf("%18p ", call);
|
|---|
| 778 | #endif
|
|---|
| 779 |
|
|---|
| 780 | printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
|
|---|
| 781 | " %-6" PRIun " %-6" PRIun " %-7x %" PRIu64 " (%s)\n",
|
|---|
| 782 | IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
|
|---|
| 783 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
|---|
| 784 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
|---|
| 785 | call->flags, call->sender->taskid, call->sender->name);
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 | printf(" --- incoming answers ---\n");
|
|---|
| 789 | list_foreach(task->answerbox.answers, cur) {
|
|---|
| 790 | call_t *call = list_get_instance(cur, call_t, link);
|
|---|
| 791 |
|
|---|
| 792 | #ifdef __32_BITS__
|
|---|
| 793 | printf("%10p ", call);
|
|---|
| 794 | #endif
|
|---|
| 795 |
|
|---|
| 796 | #ifdef __64_BITS__
|
|---|
| 797 | printf("%18p ", call);
|
|---|
| 798 | #endif
|
|---|
| 799 |
|
|---|
| 800 | printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
|
|---|
| 801 | " %-6" PRIun " %-6" PRIun " %-7x %" PRIu64 " (%s)\n",
|
|---|
| 802 | IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
|
|---|
| 803 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
|---|
| 804 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
|---|
| 805 | call->flags, call->sender->taskid, call->sender->name);
|
|---|
| 806 | }
|
|---|
| 807 |
|
|---|
| 808 | irq_spinlock_unlock(&task->answerbox.lock, false);
|
|---|
| 809 | irq_spinlock_unlock(&task->lock, true);
|
|---|
| 810 | }
|
|---|
| 811 |
|
|---|
| 812 | /** @}
|
|---|
| 813 | */
|
|---|