Subversion Repositories HelenOS

Rev

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