Subversion Repositories HelenOS

Rev

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