Subversion Repositories HelenOS

Rev

Rev 3476 | Rev 4252 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
955 palkovsky 1
/*
2071 jermar 2
 * Copyright (c) 2006 Ondrej Palkovsky
955 palkovsky 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1757 jermar 29
/** @addtogroup genericipc
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
955 palkovsky 35
/* Lock ordering
36
 *
2471 jermar 37
 * First the answerbox, then the phone.
955 palkovsky 38
 */
39
 
3020 jermar 40
#include <synch/synch.h>
1040 palkovsky 41
#include <synch/spinlock.h>
3020 jermar 42
#include <synch/mutex.h>
1040 palkovsky 43
#include <synch/waitq.h>
1364 jermar 44
#include <synch/synch.h>
955 palkovsky 45
#include <ipc/ipc.h>
3445 jermar 46
#include <ipc/kbox.h>
4173 jermar 47
#include <event/event.h>
955 palkovsky 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
 
4173 jermar 55
 
955 palkovsky 56
#include <print.h>
3438 svoboda 57
#include <console/console.h>
955 palkovsky 58
#include <proc/thread.h>
1258 palkovsky 59
#include <arch/interrupt.h>
1281 palkovsky 60
#include <ipc/irq.h>
955 palkovsky 61
 
2471 jermar 62
/** Open channel that is assigned automatically to new tasks */
965 palkovsky 63
answerbox_t *ipc_phone_0 = NULL;
955 palkovsky 64
 
65
static slab_cache_t *ipc_call_slab;
66
 
2471 jermar 67
/** Initialize a call structure.
68
 *
69
 * @param call      Call structure to be initialized.
70
 */
1084 palkovsky 71
static void _ipc_call_init(call_t *call)
72
{
3104 svoboda 73
    memsetb(call, sizeof(*call), 0);
1084 palkovsky 74
    call->callerbox = &TASK->answerbox;
75
    call->sender = TASK;
2494 jermar 76
    call->buffer = NULL;
1084 palkovsky 77
}
78
 
2471 jermar 79
/** Allocate and initialize a call structure.
955 palkovsky 80
 *
2471 jermar 81
 * The call is initialized, so that the reply will be directed to
82
 * TASK->answerbox.
1258 palkovsky 83
 *
2471 jermar 84
 * @param flags     Parameters for slab_alloc (e.g FRAME_ATOMIC).
85
 *
86
 * @return      If flags permit it, return NULL, or initialized kernel
87
 *          call structure.
955 palkovsky 88
 */
2471 jermar 89
call_t *ipc_call_alloc(int flags)
955 palkovsky 90
{
91
    call_t *call;
92
 
1258 palkovsky 93
    call = slab_alloc(ipc_call_slab, flags);
3184 jermar 94
    if (call)
95
        _ipc_call_init(call);
955 palkovsky 96
 
97
    return call;
98
}
99
 
2471 jermar 100
/** Initialize a statically allocated call structure.
101
 *
102
 * @param call      Statically allocated kernel call structure to be
103
 *          initialized.
104
 */
1084 palkovsky 105
void ipc_call_static_init(call_t *call)
965 palkovsky 106
{
1084 palkovsky 107
    _ipc_call_init(call);
108
    call->flags |= IPC_CALL_STATIC_ALLOC;
965 palkovsky 109
}
110
 
2472 jermar 111
/** Deallocate a call structure.
2471 jermar 112
 *
113
 * @param call      Call structure to be freed.
114
 */
955 palkovsky 115
void ipc_call_free(call_t *call)
116
{
2471 jermar 117
    ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
2494 jermar 118
    /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
119
    if (call->buffer)
120
        free(call->buffer);
955 palkovsky 121
    slab_free(ipc_call_slab, call);
122
}
123
 
2471 jermar 124
/** Initialize an answerbox structure.
125
 *
126
 * @param box       Answerbox structure to be initialized.
2802 jermar 127
 * @param task      Task to which the answerbox belongs.
955 palkovsky 128
 */
2802 jermar 129
void ipc_answerbox_init(answerbox_t *box, task_t *task)
955 palkovsky 130
{
1040 palkovsky 131
    spinlock_initialize(&box->lock, "ipc_box_lock");
1258 palkovsky 132
    spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
1040 palkovsky 133
    waitq_initialize(&box->wq);
955 palkovsky 134
    list_initialize(&box->connected_phones);
135
    list_initialize(&box->calls);
136
    list_initialize(&box->dispatched_calls);
137
    list_initialize(&box->answers);
1258 palkovsky 138
    list_initialize(&box->irq_notifs);
1933 jermar 139
    list_initialize(&box->irq_head);
2802 jermar 140
    box->task = task;
955 palkovsky 141
}
142
 
2471 jermar 143
/** Connect a phone to an answerbox.
144
 *
145
 * @param phone     Initialized phone structure.
146
 * @param box       Initialized answerbox structure.
147
 */
1040 palkovsky 148
void ipc_phone_connect(phone_t *phone, answerbox_t *box)
955 palkovsky 149
{
3020 jermar 150
    mutex_lock(&phone->lock);
1084 palkovsky 151
 
1568 palkovsky 152
    phone->state = IPC_PHONE_CONNECTED;
955 palkovsky 153
    phone->callee = box;
965 palkovsky 154
 
1040 palkovsky 155
    spinlock_lock(&box->lock);
1573 palkovsky 156
    list_append(&phone->link, &box->connected_phones);
1040 palkovsky 157
    spinlock_unlock(&box->lock);
1084 palkovsky 158
 
3020 jermar 159
    mutex_unlock(&phone->lock);
955 palkovsky 160
}
161
 
2471 jermar 162
/** Initialize a phone structure.
163
 *
164
 * @param phone     Phone structure to be initialized.
1040 palkovsky 165
 */
166
void ipc_phone_init(phone_t *phone)
167
{
3186 jermar 168
    mutex_initialize(&phone->lock, MUTEX_PASSIVE);
1040 palkovsky 169
    phone->callee = NULL;
1568 palkovsky 170
    phone->state = IPC_PHONE_FREE;
1088 palkovsky 171
    atomic_set(&phone->active_calls, 0);
1040 palkovsky 172
}
173
 
2471 jermar 174
/** Helper function to facilitate synchronous calls.
175
 *
176
 * @param phone     Destination kernel phone structure.
177
 * @param request   Call structure with request.
3370 jermar 178
 *
179
 * @return      EOK on success or EINTR if the sleep was interrupted.
2471 jermar 180
 */
3370 jermar 181
int ipc_call_sync(phone_t *phone, call_t *request)
959 palkovsky 182
{
183
    answerbox_t sync_box;
955 palkovsky 184
 
2802 jermar 185
    ipc_answerbox_init(&sync_box, TASK);
959 palkovsky 186
 
2471 jermar 187
    /* We will receive data in a special box. */
959 palkovsky 188
    request->callerbox = &sync_box;
189
 
190
    ipc_call(phone, request);
3370 jermar 191
    if (!ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT,
192
        SYNCH_FLAGS_INTERRUPTIBLE))
193
        return EINTR;
194
    return EOK;
959 palkovsky 195
}
196
 
2471 jermar 197
/** Answer a message which was not dispatched and is not listed in any queue.
198
 *
199
 * @param call      Call structure to be answered.
1084 palkovsky 200
 */
201
static void _ipc_answer_free_call(call_t *call)
202
{
203
    answerbox_t *callerbox = call->callerbox;
204
 
205
    call->flags |= IPC_CALL_ANSWERED;
206
 
3363 jermar 207
    if (call->flags & IPC_CALL_FORWARDED) {
3395 jermar 208
        if (call->caller_phone) {
3363 jermar 209
            /* Demasquerade the caller phone. */
3395 jermar 210
            call->data.phone = call->caller_phone;
3363 jermar 211
        }
212
    }
213
 
1084 palkovsky 214
    spinlock_lock(&callerbox->lock);
1573 palkovsky 215
    list_append(&call->link, &callerbox->answers);
1084 palkovsky 216
    spinlock_unlock(&callerbox->lock);
2310 jermar 217
    waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
1084 palkovsky 218
}
219
 
2471 jermar 220
/** Answer a message which is in a callee queue.
1084 palkovsky 221
 *
2471 jermar 222
 * @param box       Answerbox that is answering the message.
223
 * @param call      Modified request that is being sent back.
1084 palkovsky 224
 */
225
void ipc_answer(answerbox_t *box, call_t *call)
226
{
227
    /* Remove from active box */
228
    spinlock_lock(&box->lock);
1573 palkovsky 229
    list_remove(&call->link);
1084 palkovsky 230
    spinlock_unlock(&box->lock);
231
    /* Send back answer */
232
    _ipc_answer_free_call(call);
233
}
234
 
2471 jermar 235
/** Simulate sending back a message.
1090 palkovsky 236
 *
237
 * Most errors are better handled by forming a normal backward
238
 * message and sending it as a normal answer.
2471 jermar 239
 *
240
 * @param phone     Phone structure the call should appear to come from.
241
 * @param call      Call structure to be answered.
242
 * @param err       Return value to be used for the answer.
1090 palkovsky 243
 */
1780 jermar 244
void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
1090 palkovsky 245
{
246
    call->data.phone = phone;
247
    atomic_inc(&phone->active_calls);
1568 palkovsky 248
    IPC_SET_RETVAL(call->data, err);
1090 palkovsky 249
    _ipc_answer_free_call(call);
250
}
251
 
2471 jermar 252
/** Unsafe unchecking version of ipc_call.
253
 *
254
 * @param phone     Phone structure the call comes from.
255
 * @param box       Destination answerbox structure.
2601 jermar 256
 * @param call      Call structure with request.
2471 jermar 257
 */
1086 palkovsky 258
static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
259
{
2471 jermar 260
    if (!(call->flags & IPC_CALL_FORWARDED)) {
1088 palkovsky 261
        atomic_inc(&phone->active_calls);
262
        call->data.phone = phone;
263
    }
1086 palkovsky 264
 
265
    spinlock_lock(&box->lock);
1573 palkovsky 266
    list_append(&call->link, &box->calls);
1086 palkovsky 267
    spinlock_unlock(&box->lock);
2310 jermar 268
    waitq_wakeup(&box->wq, WAKEUP_FIRST);
1086 palkovsky 269
}
270
 
2471 jermar 271
/** Send an asynchronous request using a phone to an answerbox.
955 palkovsky 272
 *
2471 jermar 273
 * @param phone     Phone structure the call comes from and which is
274
 *          connected to the destination answerbox.
275
 * @param call      Call structure with request.
276
 *
277
 * @return      Return 0 on success, ENOENT on error.
955 palkovsky 278
 */
1088 palkovsky 279
int ipc_call(phone_t *phone, call_t *call)
955 palkovsky 280
{
1084 palkovsky 281
    answerbox_t *box;
955 palkovsky 282
 
3020 jermar 283
    mutex_lock(&phone->lock);
1568 palkovsky 284
    if (phone->state != IPC_PHONE_CONNECTED) {
3020 jermar 285
        mutex_unlock(&phone->lock);
1088 palkovsky 286
        if (call->flags & IPC_CALL_FORWARDED) {
287
            IPC_SET_RETVAL(call->data, EFORWARD);
1090 palkovsky 288
            _ipc_answer_free_call(call);
1568 palkovsky 289
        } else {
290
            if (phone->state == IPC_PHONE_HUNGUP)
1090 palkovsky 291
                ipc_backsend_err(phone, call, EHANGUP);
1088 palkovsky 292
            else
1090 palkovsky 293
                ipc_backsend_err(phone, call, ENOENT);
1088 palkovsky 294
        }
295
        return ENOENT;
1084 palkovsky 296
    }
1568 palkovsky 297
    box = phone->callee;
1086 palkovsky 298
    _ipc_call(phone, box, call);
299
 
3020 jermar 300
    mutex_unlock(&phone->lock);
1088 palkovsky 301
    return 0;
1086 palkovsky 302
}
955 palkovsky 303
 
2471 jermar 304
/** Disconnect phone from answerbox.
1086 palkovsky 305
 *
2471 jermar 306
 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
1568 palkovsky 307
 * lazily later.
1086 palkovsky 308
 *
2471 jermar 309
 * @param phone     Phone structure to be hung up.
1568 palkovsky 310
 *              
2471 jermar 311
 * @return      Return 0 if the phone is disconnected.
312
 *          Return -1 if the phone was already disconnected.
1086 palkovsky 313
 */
1591 palkovsky 314
int ipc_phone_hangup(phone_t *phone)
1086 palkovsky 315
{
316
    answerbox_t *box;
317
    call_t *call;
318
 
3020 jermar 319
    mutex_lock(&phone->lock);
2494 jermar 320
    if (phone->state == IPC_PHONE_FREE ||
321
        phone->state == IPC_PHONE_HUNGUP ||
2471 jermar 322
        phone->state == IPC_PHONE_CONNECTING) {
3020 jermar 323
        mutex_unlock(&phone->lock);
1568 palkovsky 324
        return -1;
325
    }
1086 palkovsky 326
    box = phone->callee;
1568 palkovsky 327
    if (phone->state != IPC_PHONE_SLAMMED) {
328
        /* Remove myself from answerbox */
329
        spinlock_lock(&box->lock);
1573 palkovsky 330
        list_remove(&phone->link);
1568 palkovsky 331
        spinlock_unlock(&box->lock);
332
 
333
        if (phone->state != IPC_PHONE_SLAMMED) {
334
            call = ipc_call_alloc(0);
335
            IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
336
            call->flags |= IPC_CALL_DISCARD_ANSWER;
337
            _ipc_call(phone, box, call);
1088 palkovsky 338
        }
1086 palkovsky 339
    }
340
 
1568 palkovsky 341
    phone->state = IPC_PHONE_HUNGUP;
3020 jermar 342
    mutex_unlock(&phone->lock);
1086 palkovsky 343
 
344
    return 0;
955 palkovsky 345
}
346
 
2471 jermar 347
/** Forwards call from one answerbox to another one.
1040 palkovsky 348
 *
2471 jermar 349
 * @param call      Call structure to be redirected.
350
 * @param newphone  Phone structure to target answerbox.
351
 * @param oldbox    Old answerbox structure.
2622 jermar 352
 * @param mode      Flags that specify mode of the forward operation.
2471 jermar 353
 *
354
 * @return      Return 0 if forwarding succeeded or an error code if
355
 *          there was error.
1088 palkovsky 356
 *
2471 jermar 357
 * The return value serves only as an information for the forwarder,
358
 * the original caller is notified automatically with EFORWARD.
1040 palkovsky 359
 */
2622 jermar 360
int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
1040 palkovsky 361
{
362
    spinlock_lock(&oldbox->lock);
1573 palkovsky 363
    list_remove(&call->link);
1040 palkovsky 364
    spinlock_unlock(&oldbox->lock);
365
 
3362 jermar 366
    if (mode & IPC_FF_ROUTE_FROM_ME) {
3395 jermar 367
        if (!call->caller_phone)
368
            call->caller_phone = call->data.phone;
2623 jermar 369
        call->data.phone = newphone;
3362 jermar 370
    }
2623 jermar 371
 
1088 palkovsky 372
    return ipc_call(newphone, call);
1040 palkovsky 373
}
374
 
955 palkovsky 375
 
2471 jermar 376
/** Wait for a phone call.
955 palkovsky 377
 *
2471 jermar 378
 * @param box       Answerbox expecting the call.
379
 * @param usec      Timeout in microseconds. See documentation for
380
 *          waitq_sleep_timeout() for decription of its special
381
 *          meaning.
382
 * @param flags     Select mode of sleep operation. See documentation for
383
 *          waitq_sleep_timeout() for description of its special
384
 *          meaning.
385
 * @return      Recived call structure or NULL.
386
 *
387
 * To distinguish between a call and an answer, have a look at call->flags.
955 palkovsky 388
 */
2471 jermar 389
call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
955 palkovsky 390
{
391
    call_t *request;
1258 palkovsky 392
    ipl_t ipl;
1364 jermar 393
    int rc;
955 palkovsky 394
 
1364 jermar 395
restart:
1502 jermar 396
    rc = waitq_sleep_timeout(&box->wq, usec, flags);
1364 jermar 397
    if (SYNCH_FAILED(rc))
398
        return NULL;
1086 palkovsky 399
 
1040 palkovsky 400
    spinlock_lock(&box->lock);
1258 palkovsky 401
    if (!list_empty(&box->irq_notifs)) {
402
        ipl = interrupts_disable();
403
        spinlock_lock(&box->irq_lock);
404
 
1573 palkovsky 405
        request = list_get_instance(box->irq_notifs.next, call_t, link);
406
        list_remove(&request->link);
1258 palkovsky 407
 
408
        spinlock_unlock(&box->irq_lock);
409
        interrupts_restore(ipl);
410
    } else if (!list_empty(&box->answers)) {
1086 palkovsky 411
        /* Handle asynchronous answers */
1573 palkovsky 412
        request = list_get_instance(box->answers.next, call_t, link);
413
        list_remove(&request->link);
3363 jermar 414
        atomic_dec(&request->data.phone->active_calls);
1086 palkovsky 415
    } else if (!list_empty(&box->calls)) {
416
        /* Handle requests */
1573 palkovsky 417
        request = list_get_instance(box->calls.next, call_t, link);
418
        list_remove(&request->link);
1086 palkovsky 419
        /* Append request to dispatch queue */
1573 palkovsky 420
        list_append(&request->link, &box->dispatched_calls);
1086 palkovsky 421
    } else {
1595 palkovsky 422
        /* This can happen regularly after ipc_cleanup */
1086 palkovsky 423
        spinlock_unlock(&box->lock);
424
        goto restart;
955 palkovsky 425
    }
1040 palkovsky 426
    spinlock_unlock(&box->lock);
955 palkovsky 427
    return request;
428
}
429
 
2471 jermar 430
/** Answer all calls from list with EHANGUP answer.
431
 *
432
 * @param lst       Head of the list to be cleaned up.
433
 */
3438 svoboda 434
void ipc_cleanup_call_list(link_t *lst)
1141 palkovsky 435
{
436
    call_t *call;
437
 
438
    while (!list_empty(lst)) {
1573 palkovsky 439
        call = list_get_instance(lst->next, call_t, link);
2662 jermar 440
        if (call->buffer)
441
            free(call->buffer);
1573 palkovsky 442
        list_remove(&call->link);
1141 palkovsky 443
 
444
        IPC_SET_RETVAL(call->data, EHANGUP);
445
        _ipc_answer_free_call(call);
446
    }
447
}
448
 
3438 svoboda 449
/** Disconnects all phones connected to an answerbox.
1072 palkovsky 450
 *
3438 svoboda 451
 * @param box       Answerbox to disconnect phones from.
452
 * @param notify_box    If true, the answerbox will get a hangup message for
453
 *          each disconnected phone.
1072 palkovsky 454
 */
3438 svoboda 455
void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
1072 palkovsky 456
{
1141 palkovsky 457
    phone_t *phone;
2183 jermar 458
    DEADLOCK_PROBE_INIT(p_phonelck);
3438 svoboda 459
    ipl_t ipl;
460
    call_t *call;
1582 palkovsky 461
 
3438 svoboda 462
    call = notify_box ? ipc_call_alloc(0) : NULL;
1088 palkovsky 463
 
1141 palkovsky 464
    /* Disconnect all phones connected to our answerbox */
465
restart_phones:
3438 svoboda 466
    ipl = interrupts_disable();
467
    spinlock_lock(&box->lock);
468
    while (!list_empty(&box->connected_phones)) {
469
        phone = list_get_instance(box->connected_phones.next,
2183 jermar 470
            phone_t, link);
3020 jermar 471
        if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
3438 svoboda 472
            spinlock_unlock(&box->lock);
473
            interrupts_restore(ipl);
2183 jermar 474
            DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
1141 palkovsky 475
            goto restart_phones;
476
        }
477
 
478
        /* Disconnect phone */
1568 palkovsky 479
        ASSERT(phone->state == IPC_PHONE_CONNECTED);
3438 svoboda 480
 
481
        list_remove(&phone->link);
1568 palkovsky 482
        phone->state = IPC_PHONE_SLAMMED;
1088 palkovsky 483
 
3438 svoboda 484
        if (notify_box) {
485
            mutex_unlock(&phone->lock);
486
            spinlock_unlock(&box->lock);
487
            interrupts_restore(ipl);
488
 
489
            /*
490
             * Send one message to the answerbox for each
491
             * phone. Used to make sure the kbox thread
492
             * wakes up after the last phone has been
493
             * disconnected.
494
             */
495
            IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
496
            call->flags |= IPC_CALL_DISCARD_ANSWER;
497
            _ipc_call(phone, box, call);
498
 
499
            /* Allocate another call in advance */
500
            call = ipc_call_alloc(0);
501
 
502
            /* Must start again */
503
            goto restart_phones;
504
        }
505
 
3020 jermar 506
        mutex_unlock(&phone->lock);
1141 palkovsky 507
    }
508
 
3438 svoboda 509
    spinlock_unlock(&box->lock);
510
    interrupts_restore(ipl);
511
 
512
    /* Free unused call */
3476 jermar 513
    if (call)
514
        ipc_call_free(call);
3438 svoboda 515
}
516
 
517
/** Cleans up all IPC communication of the current task.
518
 *
519
 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
520
 * have to change it as well if you want to cleanup other tasks than TASK.
521
 */
522
void ipc_cleanup(void)
523
{
524
    int i;
525
    call_t *call;
526
 
527
    /* Disconnect all our phones ('ipc_phone_hangup') */
528
    for (i = 0; i < IPC_MAX_PHONES; i++)
529
        ipc_phone_hangup(&TASK->phones[i]);
530
 
4173 jermar 531
    /* Unsubscribe from any event notifications. */
532
    event_cleanup_answerbox(&TASK->answerbox);
533
 
3438 svoboda 534
    /* Disconnect all connected irqs */
535
    ipc_irq_cleanup(&TASK->answerbox);
536
 
537
    /* Disconnect all phones connected to our regular answerbox */
538
    ipc_answerbox_slam_phones(&TASK->answerbox, false);
539
 
540
#ifdef CONFIG_UDEBUG
541
    /* Clean up kbox thread and communications */
542
    ipc_kbox_cleanup();
543
#endif
544
 
1088 palkovsky 545
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
3438 svoboda 546
    spinlock_lock(&TASK->answerbox.lock);
1582 palkovsky 547
    ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
548
    ipc_cleanup_call_list(&TASK->answerbox.calls);
549
    spinlock_unlock(&TASK->answerbox.lock);
1072 palkovsky 550
 
1088 palkovsky 551
    /* Wait for all async answers to arrive */
1568 palkovsky 552
    while (1) {
553
        /* Go through all phones, until all are FREE... */
554
        /* Locking not needed, no one else should modify
555
         * it, when we are in cleanup */
2471 jermar 556
        for (i = 0; i < IPC_MAX_PHONES; i++) {
557
            if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
1582 palkovsky 558
                atomic_get(&TASK->phones[i].active_calls) == 0)
559
                TASK->phones[i].state = IPC_PHONE_FREE;
560
 
1591 palkovsky 561
            /* Just for sure, we might have had some
562
             * IPC_PHONE_CONNECTING phones */
1582 palkovsky 563
            if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
1591 palkovsky 564
                ipc_phone_hangup(&TASK->phones[i]);
565
            /* If the hangup succeeded, it has sent a HANGUP
566
             * message, the IPC is now in HUNGUP state, we
567
             * wait for the reply to come */
1582 palkovsky 568
 
569
            if (TASK->phones[i].state != IPC_PHONE_FREE)
1568 palkovsky 570
                break;
571
        }
572
        /* Voila, got into cleanup */
573
        if (i == IPC_MAX_PHONES)
574
            break;
575
 
2471 jermar 576
        call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
577
            SYNCH_FLAGS_NONE);
578
        ASSERT((call->flags & IPC_CALL_ANSWERED) ||
579
            (call->flags & IPC_CALL_NOTIF));
580
        ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
1141 palkovsky 581
 
3476 jermar 582
        /*
583
         * Record the receipt of this call in the current task's counter
584
         * of active calls. IPC_M_PHONE_HUNGUP calls do not contribute
585
         * to this counter so do not record answers to them either.
586
         */
3449 jermar 587
        if (!(call->flags & IPC_CALL_DISCARD_ANSWER))
588
            atomic_dec(&TASK->active_calls);
1141 palkovsky 589
        ipc_call_free(call);
590
    }
1072 palkovsky 591
}
1258 palkovsky 592
 
593
 
1923 jermar 594
/** Initilize IPC subsystem */
1258 palkovsky 595
void ipc_init(void)
596
{
2471 jermar 597
    ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
598
        NULL, 0);
1258 palkovsky 599
}
600
 
1573 palkovsky 601
 
2471 jermar 602
/** List answerbox contents.
603
 *
604
 * @param taskid    Task ID.
605
 */
1573 palkovsky 606
void ipc_print_task(task_id_t taskid)
607
{
608
    task_t *task;
609
    int i;
610
    call_t *call;
611
    link_t *tmp;
612
 
613
    spinlock_lock(&tasks_lock);
614
    task = task_find_by_id(taskid);
615
    if (task)
616
        spinlock_lock(&task->lock);
617
    spinlock_unlock(&tasks_lock);
618
    if (!task)
619
        return;
620
 
621
    /* Print opened phones & details */
622
    printf("PHONE:\n");
2471 jermar 623
    for (i = 0; i < IPC_MAX_PHONES; i++) {
3020 jermar 624
        if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
625
            printf("%d: mutex busy\n", i);
626
            continue;
627
        }
1573 palkovsky 628
        if (task->phones[i].state != IPC_PHONE_FREE) {
2471 jermar 629
            printf("%d: ", i);
1573 palkovsky 630
            switch (task->phones[i].state) {
631
            case IPC_PHONE_CONNECTING:
632
                printf("connecting ");
633
                break;
634
            case IPC_PHONE_CONNECTED:
1735 decky 635
                printf("connected to: %p ",
1573 palkovsky 636
                       task->phones[i].callee);
637
                break;
638
            case IPC_PHONE_SLAMMED:
1735 decky 639
                printf("slammed by: %p ",
1573 palkovsky 640
                       task->phones[i].callee);
641
                break;
642
            case IPC_PHONE_HUNGUP:
1735 decky 643
                printf("hung up - was: %p ",
1573 palkovsky 644
                       task->phones[i].callee);
645
                break;
646
            default:
647
                break;
648
            }
3054 decky 649
            printf("active: %ld\n",
2472 jermar 650
                atomic_get(&task->phones[i].active_calls));
1573 palkovsky 651
        }
3020 jermar 652
        mutex_unlock(&task->phones[i].lock);
1573 palkovsky 653
    }
654
 
655
 
656
    /* Print answerbox - calls */
657
    spinlock_lock(&task->answerbox.lock);
658
    printf("ABOX - CALLS:\n");
2472 jermar 659
    for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
3090 decky 660
        tmp = tmp->next) {
1573 palkovsky 661
        call = list_get_instance(tmp, call_t, link);
3054 decky 662
        printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
3394 jermar 663
            " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
664
            " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
665
            call->sender->taskid,
2472 jermar 666
            IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
667
            IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
2637 cejka 668
            IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
2472 jermar 669
            call->flags);
1573 palkovsky 670
    }
671
    /* Print answerbox - calls */
672
    printf("ABOX - DISPATCHED CALLS:\n");
3054 decky 673
    for (tmp = task->answerbox.dispatched_calls.next;
3090 decky 674
        tmp != &task->answerbox.dispatched_calls;
675
        tmp = tmp->next) {
1573 palkovsky 676
        call = list_get_instance(tmp, call_t, link);
3054 decky 677
        printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
3394 jermar 678
            " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
679
            " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
680
            call->sender->taskid,
2472 jermar 681
            IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
682
            IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
2637 cejka 683
            IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
2472 jermar 684
            call->flags);
1573 palkovsky 685
    }
686
    /* Print answerbox - calls */
687
    printf("ABOX - ANSWERS:\n");
3394 jermar 688
    for (tmp = task->answerbox.answers.next;
689
        tmp != &task->answerbox.answers;
3090 decky 690
        tmp = tmp->next) {
1573 palkovsky 691
        call = list_get_instance(tmp, call_t, link);
3054 decky 692
        printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
3394 jermar 693
            " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
2637 cejka 694
            call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
2472 jermar 695
            IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
2637 cejka 696
            IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
2472 jermar 697
            call->flags);
1573 palkovsky 698
    }
699
 
700
    spinlock_unlock(&task->answerbox.lock);
701
    spinlock_unlock(&task->lock);
702
}
1702 cejka 703
 
1757 jermar 704
/** @}
1702 cejka 705
 */