Subversion Repositories HelenOS

Rev

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