Subversion Repositories HelenOS

Rev

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