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