Subversion Repositories HelenOS

Rev

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