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