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