[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>
|
---|
| 41 | #include <synch/waitq.h>
|
---|
[bd5a663] | 42 | #include <synch/synch.h>
|
---|
[6d9c49a] | 43 | #include <ipc/ipc.h>
|
---|
| 44 | #include <errno.h>
|
---|
| 45 | #include <mm/slab.h>
|
---|
| 46 | #include <arch.h>
|
---|
| 47 | #include <proc/task.h>
|
---|
| 48 | #include <memstr.h>
|
---|
| 49 | #include <debug.h>
|
---|
| 50 |
|
---|
| 51 | #include <print.h>
|
---|
| 52 | #include <proc/thread.h>
|
---|
[5626277] | 53 | #include <arch/interrupt.h>
|
---|
[162f919] | 54 | #include <ipc/irq.h>
|
---|
[6d9c49a] | 55 |
|
---|
[8b243f2] | 56 | /** Open channel that is assigned automatically to new tasks */
|
---|
[e74cb73] | 57 | answerbox_t *ipc_phone_0 = NULL;
|
---|
[6d9c49a] | 58 |
|
---|
| 59 | static slab_cache_t *ipc_call_slab;
|
---|
| 60 |
|
---|
[8b243f2] | 61 | /** Initialize a call structure.
|
---|
| 62 | *
|
---|
| 63 | * @param call Call structure to be initialized.
|
---|
| 64 | */
|
---|
[ba81cab] | 65 | static void _ipc_call_init(call_t *call)
|
---|
| 66 | {
|
---|
[8b243f2] | 67 | memsetb((uintptr_t) call, sizeof(*call), 0);
|
---|
[ba81cab] | 68 | call->callerbox = &TASK->answerbox;
|
---|
| 69 | call->sender = TASK;
|
---|
[7918fce] | 70 | call->buffer = NULL;
|
---|
[ba81cab] | 71 | }
|
---|
| 72 |
|
---|
[8b243f2] | 73 | /** Allocate and initialize a call structure.
|
---|
[6d9c49a] | 74 | *
|
---|
[8b243f2] | 75 | * The call is initialized, so that the reply will be directed to
|
---|
| 76 | * TASK->answerbox.
|
---|
| 77 | *
|
---|
| 78 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
|
---|
[5626277] | 79 | *
|
---|
[8b243f2] | 80 | * @return If flags permit it, return NULL, or initialized kernel
|
---|
| 81 | * call structure.
|
---|
[6d9c49a] | 82 | */
|
---|
[8b243f2] | 83 | call_t *ipc_call_alloc(int flags)
|
---|
[6d9c49a] | 84 | {
|
---|
| 85 | call_t *call;
|
---|
| 86 |
|
---|
[5626277] | 87 | call = slab_alloc(ipc_call_slab, flags);
|
---|
[ba81cab] | 88 | _ipc_call_init(call);
|
---|
[6d9c49a] | 89 |
|
---|
| 90 | return call;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[8b243f2] | 93 | /** Initialize a statically allocated call structure.
|
---|
| 94 | *
|
---|
| 95 | * @param call Statically allocated kernel call structure to be
|
---|
| 96 | * initialized.
|
---|
| 97 | */
|
---|
[ba81cab] | 98 | void ipc_call_static_init(call_t *call)
|
---|
[e74cb73] | 99 | {
|
---|
[ba81cab] | 100 | _ipc_call_init(call);
|
---|
| 101 | call->flags |= IPC_CALL_STATIC_ALLOC;
|
---|
[e74cb73] | 102 | }
|
---|
| 103 |
|
---|
[bd72c3e9] | 104 | /** Deallocate a call structure.
|
---|
[8b243f2] | 105 | *
|
---|
| 106 | * @param call Call structure to be freed.
|
---|
| 107 | */
|
---|
[6d9c49a] | 108 | void ipc_call_free(call_t *call)
|
---|
| 109 | {
|
---|
[8b243f2] | 110 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
[7918fce] | 111 | /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
|
---|
| 112 | if (call->buffer)
|
---|
| 113 | free(call->buffer);
|
---|
[6d9c49a] | 114 | slab_free(ipc_call_slab, call);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[8b243f2] | 117 | /** Initialize an answerbox structure.
|
---|
| 118 | *
|
---|
| 119 | * @param box Answerbox structure to be initialized.
|
---|
[6d9c49a] | 120 | */
|
---|
| 121 | void ipc_answerbox_init(answerbox_t *box)
|
---|
| 122 | {
|
---|
[2ba7810] | 123 | spinlock_initialize(&box->lock, "ipc_box_lock");
|
---|
[5626277] | 124 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
|
---|
[2ba7810] | 125 | waitq_initialize(&box->wq);
|
---|
[6d9c49a] | 126 | list_initialize(&box->connected_phones);
|
---|
| 127 | list_initialize(&box->calls);
|
---|
| 128 | list_initialize(&box->dispatched_calls);
|
---|
| 129 | list_initialize(&box->answers);
|
---|
[5626277] | 130 | list_initialize(&box->irq_notifs);
|
---|
[b14e35f2] | 131 | list_initialize(&box->irq_head);
|
---|
[2d5a54f3] | 132 | box->task = TASK;
|
---|
[6d9c49a] | 133 | }
|
---|
| 134 |
|
---|
[8b243f2] | 135 | /** Connect a phone to an answerbox.
|
---|
| 136 | *
|
---|
| 137 | * @param phone Initialized phone structure.
|
---|
| 138 | * @param box Initialized answerbox structure.
|
---|
| 139 | */
|
---|
[2ba7810] | 140 | void ipc_phone_connect(phone_t *phone, answerbox_t *box)
|
---|
[6d9c49a] | 141 | {
|
---|
[ba81cab] | 142 | spinlock_lock(&phone->lock);
|
---|
| 143 |
|
---|
[eb3d379] | 144 | phone->state = IPC_PHONE_CONNECTED;
|
---|
[6d9c49a] | 145 | phone->callee = box;
|
---|
[e74cb73] | 146 |
|
---|
[2ba7810] | 147 | spinlock_lock(&box->lock);
|
---|
[c4e4507] | 148 | list_append(&phone->link, &box->connected_phones);
|
---|
[2ba7810] | 149 | spinlock_unlock(&box->lock);
|
---|
[ba81cab] | 150 |
|
---|
| 151 | spinlock_unlock(&phone->lock);
|
---|
[2ba7810] | 152 | }
|
---|
| 153 |
|
---|
[8b243f2] | 154 | /** Initialize a phone structure.
|
---|
| 155 | *
|
---|
| 156 | * @param phone Phone structure to be initialized.
|
---|
[2ba7810] | 157 | */
|
---|
| 158 | void ipc_phone_init(phone_t *phone)
|
---|
| 159 | {
|
---|
| 160 | spinlock_initialize(&phone->lock, "phone_lock");
|
---|
| 161 | phone->callee = NULL;
|
---|
[eb3d379] | 162 | phone->state = IPC_PHONE_FREE;
|
---|
[9f22213] | 163 | atomic_set(&phone->active_calls, 0);
|
---|
[6d9c49a] | 164 | }
|
---|
| 165 |
|
---|
[8b243f2] | 166 | /** Helper function to facilitate synchronous calls.
|
---|
| 167 | *
|
---|
| 168 | * @param phone Destination kernel phone structure.
|
---|
| 169 | * @param request Call structure with request.
|
---|
| 170 | */
|
---|
[631ca4d] | 171 | void ipc_call_sync(phone_t *phone, call_t *request)
|
---|
| 172 | {
|
---|
| 173 | answerbox_t sync_box;
|
---|
| 174 |
|
---|
| 175 | ipc_answerbox_init(&sync_box);
|
---|
| 176 |
|
---|
[8b243f2] | 177 | /* We will receive data in a special box. */
|
---|
[631ca4d] | 178 | request->callerbox = &sync_box;
|
---|
| 179 |
|
---|
| 180 | ipc_call(phone, request);
|
---|
[116d1ef4] | 181 | ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
|
---|
[631ca4d] | 182 | }
|
---|
[6d9c49a] | 183 |
|
---|
[8b243f2] | 184 | /** Answer a message which was not dispatched and is not listed in any queue.
|
---|
| 185 | *
|
---|
| 186 | * @param call Call structure to be answered.
|
---|
[ba81cab] | 187 | */
|
---|
| 188 | static void _ipc_answer_free_call(call_t *call)
|
---|
| 189 | {
|
---|
| 190 | answerbox_t *callerbox = call->callerbox;
|
---|
| 191 |
|
---|
| 192 | call->flags |= IPC_CALL_ANSWERED;
|
---|
| 193 |
|
---|
| 194 | spinlock_lock(&callerbox->lock);
|
---|
[c4e4507] | 195 | list_append(&call->link, &callerbox->answers);
|
---|
[ba81cab] | 196 | spinlock_unlock(&callerbox->lock);
|
---|
[5c8ba05] | 197 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
|
---|
[ba81cab] | 198 | }
|
---|
| 199 |
|
---|
[8b243f2] | 200 | /** Answer a message which is in a callee queue.
|
---|
[ba81cab] | 201 | *
|
---|
[8b243f2] | 202 | * @param box Answerbox that is answering the message.
|
---|
| 203 | * @param call Modified request that is being sent back.
|
---|
[ba81cab] | 204 | */
|
---|
| 205 | void ipc_answer(answerbox_t *box, call_t *call)
|
---|
| 206 | {
|
---|
| 207 | /* Remove from active box */
|
---|
| 208 | spinlock_lock(&box->lock);
|
---|
[c4e4507] | 209 | list_remove(&call->link);
|
---|
[ba81cab] | 210 | spinlock_unlock(&box->lock);
|
---|
| 211 | /* Send back answer */
|
---|
| 212 | _ipc_answer_free_call(call);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[8b243f2] | 215 | /** Simulate sending back a message.
|
---|
[7c7aae16] | 216 | *
|
---|
| 217 | * Most errors are better handled by forming a normal backward
|
---|
| 218 | * message and sending it as a normal answer.
|
---|
[8b243f2] | 219 | *
|
---|
| 220 | * @param phone Phone structure the call should appear to come from.
|
---|
| 221 | * @param call Call structure to be answered.
|
---|
| 222 | * @param err Return value to be used for the answer.
|
---|
[7c7aae16] | 223 | */
|
---|
[7f1c620] | 224 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
|
---|
[7c7aae16] | 225 | {
|
---|
| 226 | call->data.phone = phone;
|
---|
| 227 | atomic_inc(&phone->active_calls);
|
---|
[eb3d379] | 228 | IPC_SET_RETVAL(call->data, err);
|
---|
[7c7aae16] | 229 | _ipc_answer_free_call(call);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[8b243f2] | 232 | /** Unsafe unchecking version of ipc_call.
|
---|
| 233 | *
|
---|
| 234 | * @param phone Phone structure the call comes from.
|
---|
| 235 | * @param box Destination answerbox structure.
|
---|
[7c5bcc0] | 236 | * @param call Call structure with request.
|
---|
[8b243f2] | 237 | */
|
---|
[fbcfd458] | 238 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
|
---|
| 239 | {
|
---|
[8b243f2] | 240 | if (!(call->flags & IPC_CALL_FORWARDED)) {
|
---|
[9f22213] | 241 | atomic_inc(&phone->active_calls);
|
---|
| 242 | call->data.phone = phone;
|
---|
| 243 | }
|
---|
[fbcfd458] | 244 |
|
---|
| 245 | spinlock_lock(&box->lock);
|
---|
[c4e4507] | 246 | list_append(&call->link, &box->calls);
|
---|
[fbcfd458] | 247 | spinlock_unlock(&box->lock);
|
---|
[5c8ba05] | 248 | waitq_wakeup(&box->wq, WAKEUP_FIRST);
|
---|
[fbcfd458] | 249 | }
|
---|
| 250 |
|
---|
[8b243f2] | 251 | /** Send an asynchronous request using a phone to an answerbox.
|
---|
| 252 | *
|
---|
| 253 | * @param phone Phone structure the call comes from and which is
|
---|
| 254 | * connected to the destination answerbox.
|
---|
| 255 | * @param call Call structure with request.
|
---|
[6d9c49a] | 256 | *
|
---|
[8b243f2] | 257 | * @return Return 0 on success, ENOENT on error.
|
---|
[6d9c49a] | 258 | */
|
---|
[9f22213] | 259 | int ipc_call(phone_t *phone, call_t *call)
|
---|
[6d9c49a] | 260 | {
|
---|
[ba81cab] | 261 | answerbox_t *box;
|
---|
[6d9c49a] | 262 |
|
---|
[ba81cab] | 263 | spinlock_lock(&phone->lock);
|
---|
[eb3d379] | 264 | if (phone->state != IPC_PHONE_CONNECTED) {
|
---|
[fbcfd458] | 265 | spinlock_unlock(&phone->lock);
|
---|
[9f22213] | 266 | if (call->flags & IPC_CALL_FORWARDED) {
|
---|
| 267 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
[7c7aae16] | 268 | _ipc_answer_free_call(call);
|
---|
[eb3d379] | 269 | } else {
|
---|
| 270 | if (phone->state == IPC_PHONE_HUNGUP)
|
---|
[7c7aae16] | 271 | ipc_backsend_err(phone, call, EHANGUP);
|
---|
[9f22213] | 272 | else
|
---|
[7c7aae16] | 273 | ipc_backsend_err(phone, call, ENOENT);
|
---|
[9f22213] | 274 | }
|
---|
| 275 | return ENOENT;
|
---|
[ba81cab] | 276 | }
|
---|
[eb3d379] | 277 | box = phone->callee;
|
---|
[fbcfd458] | 278 | _ipc_call(phone, box, call);
|
---|
| 279 |
|
---|
| 280 | spinlock_unlock(&phone->lock);
|
---|
[9f22213] | 281 | return 0;
|
---|
[fbcfd458] | 282 | }
|
---|
| 283 |
|
---|
[8b243f2] | 284 | /** Disconnect phone from answerbox.
|
---|
[fbcfd458] | 285 | *
|
---|
[8b243f2] | 286 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done
|
---|
[eb3d379] | 287 | * lazily later.
|
---|
[fbcfd458] | 288 | *
|
---|
[8b243f2] | 289 | * @param phone Phone structure to be hung up.
|
---|
[eb3d379] | 290 | *
|
---|
[8b243f2] | 291 | * @return Return 0 if the phone is disconnected.
|
---|
| 292 | * Return -1 if the phone was already disconnected.
|
---|
[fbcfd458] | 293 | */
|
---|
[d8f7362] | 294 | int ipc_phone_hangup(phone_t *phone)
|
---|
[fbcfd458] | 295 | {
|
---|
| 296 | answerbox_t *box;
|
---|
| 297 | call_t *call;
|
---|
| 298 |
|
---|
| 299 | spinlock_lock(&phone->lock);
|
---|
[7918fce] | 300 | if (phone->state == IPC_PHONE_FREE ||
|
---|
| 301 | phone->state == IPC_PHONE_HUNGUP ||
|
---|
[8b243f2] | 302 | phone->state == IPC_PHONE_CONNECTING) {
|
---|
[fbcfd458] | 303 | spinlock_unlock(&phone->lock);
|
---|
[eb3d379] | 304 | return -1;
|
---|
[fbcfd458] | 305 | }
|
---|
[eb3d379] | 306 | box = phone->callee;
|
---|
| 307 | if (phone->state != IPC_PHONE_SLAMMED) {
|
---|
| 308 | /* Remove myself from answerbox */
|
---|
| 309 | spinlock_lock(&box->lock);
|
---|
[c4e4507] | 310 | list_remove(&phone->link);
|
---|
[eb3d379] | 311 | spinlock_unlock(&box->lock);
|
---|
[6d9c49a] | 312 |
|
---|
[eb3d379] | 313 | if (phone->state != IPC_PHONE_SLAMMED) {
|
---|
| 314 | call = ipc_call_alloc(0);
|
---|
| 315 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
|
---|
| 316 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
---|
| 317 | _ipc_call(phone, box, call);
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
[fbcfd458] | 320 |
|
---|
[eb3d379] | 321 | phone->state = IPC_PHONE_HUNGUP;
|
---|
[ba81cab] | 322 | spinlock_unlock(&phone->lock);
|
---|
[fbcfd458] | 323 |
|
---|
| 324 | return 0;
|
---|
[2ba7810] | 325 | }
|
---|
| 326 |
|
---|
[8b243f2] | 327 | /** Forwards call from one answerbox to another one.
|
---|
[2ba7810] | 328 | *
|
---|
[8b243f2] | 329 | * @param call Call structure to be redirected.
|
---|
| 330 | * @param newphone Phone structure to target answerbox.
|
---|
| 331 | * @param oldbox Old answerbox structure.
|
---|
[d40a8ff] | 332 | * @param mode Flags that specify mode of the forward operation.
|
---|
[8b243f2] | 333 | *
|
---|
| 334 | * @return Return 0 if forwarding succeeded or an error code if
|
---|
| 335 | * there was error.
|
---|
[9f22213] | 336 | *
|
---|
[8b243f2] | 337 | * The return value serves only as an information for the forwarder,
|
---|
| 338 | * the original caller is notified automatically with EFORWARD.
|
---|
[2ba7810] | 339 | */
|
---|
[d40a8ff] | 340 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
|
---|
[2ba7810] | 341 | {
|
---|
| 342 | spinlock_lock(&oldbox->lock);
|
---|
[c4e4507] | 343 | list_remove(&call->link);
|
---|
[2ba7810] | 344 | spinlock_unlock(&oldbox->lock);
|
---|
| 345 |
|
---|
[9201f47] | 346 | if (mode & IPC_FF_ROUTE_FROM_ME)
|
---|
| 347 | call->data.phone = newphone;
|
---|
| 348 |
|
---|
[9f22213] | 349 | return ipc_call(newphone, call);
|
---|
[6d9c49a] | 350 | }
|
---|
| 351 |
|
---|
| 352 |
|
---|
[8b243f2] | 353 | /** Wait for a phone call.
|
---|
[6d9c49a] | 354 | *
|
---|
[8b243f2] | 355 | * @param box Answerbox expecting the call.
|
---|
| 356 | * @param usec Timeout in microseconds. See documentation for
|
---|
| 357 | * waitq_sleep_timeout() for decription of its special
|
---|
| 358 | * meaning.
|
---|
| 359 | * @param flags Select mode of sleep operation. See documentation for
|
---|
| 360 | * waitq_sleep_timeout() for description of its special
|
---|
| 361 | * meaning.
|
---|
| 362 | * @return Recived call structure or NULL.
|
---|
| 363 | *
|
---|
| 364 | * To distinguish between a call and an answer, have a look at call->flags.
|
---|
[6d9c49a] | 365 | */
|
---|
[8b243f2] | 366 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
|
---|
[6d9c49a] | 367 | {
|
---|
| 368 | call_t *request;
|
---|
[5626277] | 369 | ipl_t ipl;
|
---|
[bd5a663] | 370 | int rc;
|
---|
[6d9c49a] | 371 |
|
---|
[bd5a663] | 372 | restart:
|
---|
[116d1ef4] | 373 | rc = waitq_sleep_timeout(&box->wq, usec, flags);
|
---|
[bd5a663] | 374 | if (SYNCH_FAILED(rc))
|
---|
| 375 | return NULL;
|
---|
[fbcfd458] | 376 |
|
---|
[2ba7810] | 377 | spinlock_lock(&box->lock);
|
---|
[5626277] | 378 | if (!list_empty(&box->irq_notifs)) {
|
---|
| 379 | ipl = interrupts_disable();
|
---|
| 380 | spinlock_lock(&box->irq_lock);
|
---|
| 381 |
|
---|
[c4e4507] | 382 | request = list_get_instance(box->irq_notifs.next, call_t, link);
|
---|
| 383 | list_remove(&request->link);
|
---|
[5626277] | 384 |
|
---|
| 385 | spinlock_unlock(&box->irq_lock);
|
---|
| 386 | interrupts_restore(ipl);
|
---|
| 387 | } else if (!list_empty(&box->answers)) {
|
---|
[fbcfd458] | 388 | /* Handle asynchronous answers */
|
---|
[c4e4507] | 389 | request = list_get_instance(box->answers.next, call_t, link);
|
---|
| 390 | list_remove(&request->link);
|
---|
[fbcfd458] | 391 | atomic_dec(&request->data.phone->active_calls);
|
---|
| 392 | } else if (!list_empty(&box->calls)) {
|
---|
| 393 | /* Handle requests */
|
---|
[c4e4507] | 394 | request = list_get_instance(box->calls.next, call_t, link);
|
---|
| 395 | list_remove(&request->link);
|
---|
[fbcfd458] | 396 | /* Append request to dispatch queue */
|
---|
[c4e4507] | 397 | list_append(&request->link, &box->dispatched_calls);
|
---|
[fbcfd458] | 398 | } else {
|
---|
[874621f] | 399 | /* This can happen regularly after ipc_cleanup */
|
---|
[fbcfd458] | 400 | spinlock_unlock(&box->lock);
|
---|
| 401 | goto restart;
|
---|
[6d9c49a] | 402 | }
|
---|
[2ba7810] | 403 | spinlock_unlock(&box->lock);
|
---|
[6d9c49a] | 404 | return request;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[8b243f2] | 407 | /** Answer all calls from list with EHANGUP answer.
|
---|
| 408 | *
|
---|
| 409 | * @param lst Head of the list to be cleaned up.
|
---|
| 410 | */
|
---|
[ca687ad] | 411 | static void ipc_cleanup_call_list(link_t *lst)
|
---|
| 412 | {
|
---|
| 413 | call_t *call;
|
---|
| 414 |
|
---|
| 415 | while (!list_empty(lst)) {
|
---|
[c4e4507] | 416 | call = list_get_instance(lst->next, call_t, link);
|
---|
| 417 | list_remove(&call->link);
|
---|
[ca687ad] | 418 |
|
---|
| 419 | IPC_SET_RETVAL(call->data, EHANGUP);
|
---|
| 420 | _ipc_answer_free_call(call);
|
---|
| 421 | }
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[8b243f2] | 424 | /** Cleans up all IPC communication of the current task.
|
---|
[4e49572] | 425 | *
|
---|
[214c5a0] | 426 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
|
---|
[8b243f2] | 427 | * have to change it as well if you want to cleanup other tasks than TASK.
|
---|
[4e49572] | 428 | */
|
---|
[214c5a0] | 429 | void ipc_cleanup(void)
|
---|
[4e49572] | 430 | {
|
---|
[9f22213] | 431 | int i;
|
---|
[ca687ad] | 432 | call_t *call;
|
---|
| 433 | phone_t *phone;
|
---|
[31d8e10] | 434 | DEADLOCK_PROBE_INIT(p_phonelck);
|
---|
[214c5a0] | 435 |
|
---|
[9f22213] | 436 | /* Disconnect all our phones ('ipc_phone_hangup') */
|
---|
[8b243f2] | 437 | for (i = 0; i < IPC_MAX_PHONES; i++)
|
---|
[d8f7362] | 438 | ipc_phone_hangup(&TASK->phones[i]);
|
---|
[9f22213] | 439 |
|
---|
[5626277] | 440 | /* Disconnect all connected irqs */
|
---|
[214c5a0] | 441 | ipc_irq_cleanup(&TASK->answerbox);
|
---|
[5626277] | 442 |
|
---|
[ca687ad] | 443 | /* Disconnect all phones connected to our answerbox */
|
---|
| 444 | restart_phones:
|
---|
[214c5a0] | 445 | spinlock_lock(&TASK->answerbox.lock);
|
---|
| 446 | while (!list_empty(&TASK->answerbox.connected_phones)) {
|
---|
| 447 | phone = list_get_instance(TASK->answerbox.connected_phones.next,
|
---|
[31d8e10] | 448 | phone_t, link);
|
---|
[bd72c3e9] | 449 | if (!spinlock_trylock(&phone->lock)) {
|
---|
[214c5a0] | 450 | spinlock_unlock(&TASK->answerbox.lock);
|
---|
[31d8e10] | 451 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
|
---|
[ca687ad] | 452 | goto restart_phones;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | /* Disconnect phone */
|
---|
[eb3d379] | 456 | ASSERT(phone->state == IPC_PHONE_CONNECTED);
|
---|
| 457 | phone->state = IPC_PHONE_SLAMMED;
|
---|
[c4e4507] | 458 | list_remove(&phone->link);
|
---|
[ca687ad] | 459 |
|
---|
| 460 | spinlock_unlock(&phone->lock);
|
---|
| 461 | }
|
---|
[9f22213] | 462 |
|
---|
| 463 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */
|
---|
[214c5a0] | 464 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
|
---|
| 465 | ipc_cleanup_call_list(&TASK->answerbox.calls);
|
---|
| 466 | spinlock_unlock(&TASK->answerbox.lock);
|
---|
[4e49572] | 467 |
|
---|
[9f22213] | 468 | /* Wait for all async answers to arrive */
|
---|
[eb3d379] | 469 | while (1) {
|
---|
| 470 | /* Go through all phones, until all are FREE... */
|
---|
| 471 | /* Locking not needed, no one else should modify
|
---|
| 472 | * it, when we are in cleanup */
|
---|
[8b243f2] | 473 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
| 474 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
|
---|
[214c5a0] | 475 | atomic_get(&TASK->phones[i].active_calls) == 0)
|
---|
| 476 | TASK->phones[i].state = IPC_PHONE_FREE;
|
---|
| 477 |
|
---|
[d8f7362] | 478 | /* Just for sure, we might have had some
|
---|
| 479 | * IPC_PHONE_CONNECTING phones */
|
---|
[214c5a0] | 480 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
|
---|
[d8f7362] | 481 | ipc_phone_hangup(&TASK->phones[i]);
|
---|
| 482 | /* If the hangup succeeded, it has sent a HANGUP
|
---|
| 483 | * message, the IPC is now in HUNGUP state, we
|
---|
| 484 | * wait for the reply to come */
|
---|
[214c5a0] | 485 |
|
---|
| 486 | if (TASK->phones[i].state != IPC_PHONE_FREE)
|
---|
[eb3d379] | 487 | break;
|
---|
| 488 | }
|
---|
| 489 | /* Voila, got into cleanup */
|
---|
| 490 | if (i == IPC_MAX_PHONES)
|
---|
| 491 | break;
|
---|
| 492 |
|
---|
[8b243f2] | 493 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
|
---|
| 494 | SYNCH_FLAGS_NONE);
|
---|
| 495 | ASSERT((call->flags & IPC_CALL_ANSWERED) ||
|
---|
| 496 | (call->flags & IPC_CALL_NOTIF));
|
---|
| 497 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
|
---|
[ca687ad] | 498 |
|
---|
[214c5a0] | 499 | atomic_dec(&TASK->active_calls);
|
---|
[ca687ad] | 500 | ipc_call_free(call);
|
---|
| 501 | }
|
---|
[4e49572] | 502 | }
|
---|
[5626277] | 503 |
|
---|
| 504 |
|
---|
[2b017ba] | 505 | /** Initilize IPC subsystem */
|
---|
[5626277] | 506 | void ipc_init(void)
|
---|
| 507 | {
|
---|
[8b243f2] | 508 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
|
---|
| 509 | NULL, 0);
|
---|
[5626277] | 510 | }
|
---|
| 511 |
|
---|
[c4e4507] | 512 |
|
---|
[8b243f2] | 513 | /** List answerbox contents.
|
---|
| 514 | *
|
---|
| 515 | * @param taskid Task ID.
|
---|
| 516 | */
|
---|
[c4e4507] | 517 | void ipc_print_task(task_id_t taskid)
|
---|
| 518 | {
|
---|
| 519 | task_t *task;
|
---|
| 520 | int i;
|
---|
| 521 | call_t *call;
|
---|
| 522 | link_t *tmp;
|
---|
| 523 |
|
---|
| 524 | spinlock_lock(&tasks_lock);
|
---|
| 525 | task = task_find_by_id(taskid);
|
---|
| 526 | if (task)
|
---|
| 527 | spinlock_lock(&task->lock);
|
---|
| 528 | spinlock_unlock(&tasks_lock);
|
---|
| 529 | if (!task)
|
---|
| 530 | return;
|
---|
| 531 |
|
---|
| 532 | /* Print opened phones & details */
|
---|
| 533 | printf("PHONE:\n");
|
---|
[8b243f2] | 534 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
[c4e4507] | 535 | spinlock_lock(&task->phones[i].lock);
|
---|
| 536 | if (task->phones[i].state != IPC_PHONE_FREE) {
|
---|
[8b243f2] | 537 | printf("%d: ", i);
|
---|
[c4e4507] | 538 | switch (task->phones[i].state) {
|
---|
| 539 | case IPC_PHONE_CONNECTING:
|
---|
| 540 | printf("connecting ");
|
---|
| 541 | break;
|
---|
| 542 | case IPC_PHONE_CONNECTED:
|
---|
[fbf7b4c] | 543 | printf("connected to: %p ",
|
---|
[c4e4507] | 544 | task->phones[i].callee);
|
---|
| 545 | break;
|
---|
| 546 | case IPC_PHONE_SLAMMED:
|
---|
[fbf7b4c] | 547 | printf("slammed by: %p ",
|
---|
[c4e4507] | 548 | task->phones[i].callee);
|
---|
| 549 | break;
|
---|
| 550 | case IPC_PHONE_HUNGUP:
|
---|
[fbf7b4c] | 551 | printf("hung up - was: %p ",
|
---|
[c4e4507] | 552 | task->phones[i].callee);
|
---|
| 553 | break;
|
---|
| 554 | default:
|
---|
| 555 | break;
|
---|
| 556 | }
|
---|
[bd72c3e9] | 557 | printf("active: %d\n",
|
---|
| 558 | atomic_get(&task->phones[i].active_calls));
|
---|
[c4e4507] | 559 | }
|
---|
| 560 | spinlock_unlock(&task->phones[i].lock);
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 |
|
---|
| 564 | /* Print answerbox - calls */
|
---|
| 565 | spinlock_lock(&task->answerbox.lock);
|
---|
| 566 | printf("ABOX - CALLS:\n");
|
---|
[bd72c3e9] | 567 | for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
|
---|
| 568 | tmp = tmp->next) {
|
---|
[c4e4507] | 569 | call = list_get_instance(tmp, call_t, link);
|
---|
[bd72c3e9] | 570 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d "
|
---|
[38c706cc] | 571 | "A4:%d A5:%d Flags:%x\n", call, call->sender->taskid,
|
---|
[bd72c3e9] | 572 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
| 573 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
[38c706cc] | 574 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
[bd72c3e9] | 575 | call->flags);
|
---|
[c4e4507] | 576 | }
|
---|
| 577 | /* Print answerbox - calls */
|
---|
| 578 | printf("ABOX - DISPATCHED CALLS:\n");
|
---|
| 579 | for (tmp=task->answerbox.dispatched_calls.next;
|
---|
| 580 | tmp != &task->answerbox.dispatched_calls;
|
---|
| 581 | tmp = tmp->next) {
|
---|
| 582 | call = list_get_instance(tmp, call_t, link);
|
---|
[bd72c3e9] | 583 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d "
|
---|
[38c706cc] | 584 | "A4:%d A5:%d Flags:%x\n", call, call->sender->taskid,
|
---|
[bd72c3e9] | 585 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
| 586 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
[38c706cc] | 587 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
[bd72c3e9] | 588 | call->flags);
|
---|
[c4e4507] | 589 | }
|
---|
| 590 | /* Print answerbox - calls */
|
---|
| 591 | printf("ABOX - ANSWERS:\n");
|
---|
[bd72c3e9] | 592 | for (tmp = task->answerbox.answers.next; tmp != &task->answerbox.answers;
|
---|
| 593 | tmp = tmp->next) {
|
---|
[c4e4507] | 594 | call = list_get_instance(tmp, call_t, link);
|
---|
[38c706cc] | 595 | printf("Callid:%p M:%d A1:%d A2:%d A3:%d A4:%d A5:%d Flags:%x\n",
|
---|
| 596 | call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
[bd72c3e9] | 597 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
[38c706cc] | 598 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
[bd72c3e9] | 599 | call->flags);
|
---|
[c4e4507] | 600 | }
|
---|
| 601 |
|
---|
| 602 | spinlock_unlock(&task->answerbox.lock);
|
---|
| 603 | spinlock_unlock(&task->lock);
|
---|
| 604 | }
|
---|
[b45c443] | 605 |
|
---|
[cc73a8a1] | 606 | /** @}
|
---|
[b45c443] | 607 | */
|
---|