Subversion Repositories HelenOS-historic

Rev

Rev 1502 | Rev 1573 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
955 palkovsky 1
/*
2
 * Copyright (C) 2006 Ondrej 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
 
29
/* Lock ordering
30
 *
31
 * First the answerbox, then the phone
32
 */
33
 
1040 palkovsky 34
#include <synch/spinlock.h>
35
#include <synch/waitq.h>
1364 jermar 36
#include <synch/synch.h>
955 palkovsky 37
#include <ipc/ipc.h>
38
#include <errno.h>
39
#include <mm/slab.h>
40
#include <arch.h>
41
#include <proc/task.h>
42
#include <memstr.h>
43
#include <debug.h>
44
 
45
#include <print.h>
46
#include <proc/thread.h>
1258 palkovsky 47
#include <arch/interrupt.h>
1281 palkovsky 48
#include <ipc/irq.h>
955 palkovsky 49
 
965 palkovsky 50
/* Open channel that is assigned automatically to new tasks */
51
answerbox_t *ipc_phone_0 = NULL;
955 palkovsky 52
 
53
static slab_cache_t *ipc_call_slab;
54
 
1084 palkovsky 55
/* Initialize new call */
56
static void _ipc_call_init(call_t *call)
57
{
58
    memsetb((__address)call, sizeof(*call), 0);
59
    call->callerbox = &TASK->answerbox;
60
    call->sender = TASK;
61
}
62
 
955 palkovsky 63
/** Allocate & initialize call structure
64
 *
65
 * The call is initialized, so that the reply will be directed
66
 * to TASK->answerbox
1258 palkovsky 67
 *
68
 * @param flags Parameters for slab_alloc (ATOMIC, etc.)
955 palkovsky 69
 */
1258 palkovsky 70
call_t * ipc_call_alloc(int flags)
955 palkovsky 71
{
72
    call_t *call;
73
 
1258 palkovsky 74
    call = slab_alloc(ipc_call_slab, flags);
1084 palkovsky 75
    _ipc_call_init(call);
955 palkovsky 76
 
77
    return call;
78
}
79
 
965 palkovsky 80
/** Initialize allocated call */
1084 palkovsky 81
void ipc_call_static_init(call_t *call)
965 palkovsky 82
{
1084 palkovsky 83
    _ipc_call_init(call);
84
    call->flags |= IPC_CALL_STATIC_ALLOC;
965 palkovsky 85
}
86
 
955 palkovsky 87
/** Deallocate call stracuture */
88
void ipc_call_free(call_t *call)
89
{
90
    slab_free(ipc_call_slab, call);
91
}
92
 
93
/** Initialize answerbox structure
94
 */
95
void ipc_answerbox_init(answerbox_t *box)
96
{
1040 palkovsky 97
    spinlock_initialize(&box->lock, "ipc_box_lock");
1258 palkovsky 98
    spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
1040 palkovsky 99
    waitq_initialize(&box->wq);
955 palkovsky 100
    list_initialize(&box->connected_phones);
101
    list_initialize(&box->calls);
102
    list_initialize(&box->dispatched_calls);
103
    list_initialize(&box->answers);
1258 palkovsky 104
    list_initialize(&box->irq_notifs);
1027 palkovsky 105
    box->task = TASK;
955 palkovsky 106
}
107
 
1040 palkovsky 108
/** Connect phone to answerbox */
109
void ipc_phone_connect(phone_t *phone, answerbox_t *box)
955 palkovsky 110
{
1084 palkovsky 111
    spinlock_lock(&phone->lock);
112
 
1040 palkovsky 113
    ASSERT(!phone->callee);
1568 palkovsky 114
    phone->state = IPC_PHONE_CONNECTED;
955 palkovsky 115
    phone->callee = box;
965 palkovsky 116
 
1040 palkovsky 117
    spinlock_lock(&box->lock);
955 palkovsky 118
    list_append(&phone->list, &box->connected_phones);
1040 palkovsky 119
    spinlock_unlock(&box->lock);
1084 palkovsky 120
 
121
    spinlock_unlock(&phone->lock);
955 palkovsky 122
}
123
 
1223 jermar 124
/** Initialize phone structure and connect phone to answerbox
1040 palkovsky 125
 */
126
void ipc_phone_init(phone_t *phone)
127
{
128
    spinlock_initialize(&phone->lock, "phone_lock");
129
    phone->callee = NULL;
1568 palkovsky 130
    phone->state = IPC_PHONE_FREE;
1088 palkovsky 131
    atomic_set(&phone->active_calls, 0);
1040 palkovsky 132
}
133
 
959 palkovsky 134
/** Helper function to facilitate synchronous calls */
135
void ipc_call_sync(phone_t *phone, call_t *request)
136
{
137
    answerbox_t sync_box;
955 palkovsky 138
 
959 palkovsky 139
    ipc_answerbox_init(&sync_box);
140
 
141
    /* We will receive data on special box */
142
    request->callerbox = &sync_box;
143
 
144
    ipc_call(phone, request);
1502 jermar 145
    ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
959 palkovsky 146
}
147
 
1084 palkovsky 148
/** Answer message that was not dispatched and is not entered in
149
 * any queue
150
 */
151
static void _ipc_answer_free_call(call_t *call)
152
{
153
    answerbox_t *callerbox = call->callerbox;
154
 
155
    call->flags |= IPC_CALL_ANSWERED;
156
 
157
    spinlock_lock(&callerbox->lock);
158
    list_append(&call->list, &callerbox->answers);
159
    spinlock_unlock(&callerbox->lock);
160
    waitq_wakeup(&callerbox->wq, 0);
161
}
162
 
163
/** Answer message, that is in callee queue
164
 *
165
 * @param box Answerbox that is answering the message
166
 * @param call Modified request that is being sent back
167
 */
168
void ipc_answer(answerbox_t *box, call_t *call)
169
{
170
    /* Remove from active box */
171
    spinlock_lock(&box->lock);
172
    list_remove(&call->list);
173
    spinlock_unlock(&box->lock);
174
    /* Send back answer */
175
    _ipc_answer_free_call(call);
176
}
177
 
1090 palkovsky 178
/** Simulate sending back a message
179
 *
180
 * Most errors are better handled by forming a normal backward
181
 * message and sending it as a normal answer.
182
 */
183
void ipc_backsend_err(phone_t *phone, call_t *call, __native err)
184
{
185
    call->data.phone = phone;
186
    atomic_inc(&phone->active_calls);
1568 palkovsky 187
    IPC_SET_RETVAL(call->data, err);
1090 palkovsky 188
    _ipc_answer_free_call(call);
189
}
190
 
1086 palkovsky 191
/* Unsafe unchecking ipc_call */
192
static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
193
{
1088 palkovsky 194
    if (! (call->flags & IPC_CALL_FORWARDED)) {
195
        atomic_inc(&phone->active_calls);
196
        call->data.phone = phone;
197
    }
1086 palkovsky 198
 
199
    spinlock_lock(&box->lock);
200
    list_append(&call->list, &box->calls);
201
    spinlock_unlock(&box->lock);
202
    waitq_wakeup(&box->wq, 0);
203
}
204
 
959 palkovsky 205
/** Send a asynchronous request using phone to answerbox
955 palkovsky 206
 *
207
 * @param phone Phone connected to answerbox
208
 * @param request Request to be sent
209
 */
1088 palkovsky 210
int ipc_call(phone_t *phone, call_t *call)
955 palkovsky 211
{
1084 palkovsky 212
    answerbox_t *box;
955 palkovsky 213
 
1084 palkovsky 214
    spinlock_lock(&phone->lock);
1568 palkovsky 215
    if (phone->state != IPC_PHONE_CONNECTED) {
1086 palkovsky 216
        spinlock_unlock(&phone->lock);
1088 palkovsky 217
        if (call->flags & IPC_CALL_FORWARDED) {
218
            IPC_SET_RETVAL(call->data, EFORWARD);
1090 palkovsky 219
            _ipc_answer_free_call(call);
1568 palkovsky 220
        } else {
221
            if (phone->state == IPC_PHONE_HUNGUP)
1090 palkovsky 222
                ipc_backsend_err(phone, call, EHANGUP);
1088 palkovsky 223
            else
1090 palkovsky 224
                ipc_backsend_err(phone, call, ENOENT);
1088 palkovsky 225
        }
226
        return ENOENT;
1084 palkovsky 227
    }
1568 palkovsky 228
    box = phone->callee;
1086 palkovsky 229
    _ipc_call(phone, box, call);
230
 
231
    spinlock_unlock(&phone->lock);
1088 palkovsky 232
    return 0;
1086 palkovsky 233
}
955 palkovsky 234
 
1086 palkovsky 235
/** Disconnect phone from answerbox
236
 *
1568 palkovsky 237
 * This call leaves the phone in HUNGUP state. The change to 'free' is done
238
 * lazily later.
1086 palkovsky 239
 *
1568 palkovsky 240
 * @param phone Phone to be hung up
241
 * @param aggressive If false, the phone is only marked hungup, and all
242
 *              messages are allowed to complete.
243
 *              If true, all messages in all queues are discarded. There
244
 *              may still be some messages that will be 'in-transit' on
245
 *              other CPU.
246
 *              
1086 palkovsky 247
 * @return 0 - phone disconnected, -1 - the phone was already disconnected
248
 */
1568 palkovsky 249
int ipc_phone_hangup(phone_t *phone, int aggressive)
1086 palkovsky 250
{
251
    answerbox_t *box;
252
    call_t *call;
253
 
254
    spinlock_lock(&phone->lock);
1568 palkovsky 255
    if (phone->state == IPC_PHONE_FREE || phone->state ==IPC_PHONE_HUNGUP \
256
        || phone->state == IPC_PHONE_CONNECTING) {
257
        spinlock_unlock(&phone->lock);
258
        return -1;
259
    }
1086 palkovsky 260
    box = phone->callee;
1568 palkovsky 261
    if (phone->state != IPC_PHONE_SLAMMED) {
262
        /* Remove myself from answerbox */
263
        spinlock_lock(&box->lock);
264
        list_remove(&phone->list);
265
        spinlock_unlock(&box->lock);
266
 
267
        if (phone->state != IPC_PHONE_SLAMMED) {
268
            call = ipc_call_alloc(0);
269
            IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
270
            call->flags |= IPC_CALL_DISCARD_ANSWER;
271
            _ipc_call(phone, box, call);
1088 palkovsky 272
        }
1086 palkovsky 273
    }
274
 
1568 palkovsky 275
    if (aggressive && atomic_get(&phone->active_calls) > 0) {
276
        /* TODO: Do some stuff be VERY aggressive */
277
    }
1086 palkovsky 278
 
1568 palkovsky 279
    phone->callee = 0;
280
 
281
    phone->state = IPC_PHONE_HUNGUP;
1084 palkovsky 282
    spinlock_unlock(&phone->lock);
1086 palkovsky 283
 
284
    return 0;
955 palkovsky 285
}
286
 
1040 palkovsky 287
/** Forwards call from one answerbox to a new one
288
 *
1342 jermar 289
 * @param call Call to be redirected.
290
 * @param newphone Phone to target answerbox.
1040 palkovsky 291
 * @param oldbox Old answerbox
1088 palkovsky 292
 * @return 0 on forward ok, error code, if there was error
293
 *
294
 * - the return value serves only as an information for the forwarder,
295
 *   the original caller is notified automatically with EFORWARD
1040 palkovsky 296
 */
1088 palkovsky 297
int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox)
1040 palkovsky 298
{
299
    spinlock_lock(&oldbox->lock);
300
    list_remove(&call->list);
301
    spinlock_unlock(&oldbox->lock);
302
 
1088 palkovsky 303
    return ipc_call(newphone, call);
1040 palkovsky 304
}
305
 
955 palkovsky 306
 
307
/** Wait for phone call
308
 *
1364 jermar 309
 * @param box Answerbox expecting the call.
310
 * @param usec Timeout in microseconds. See documentation for waitq_sleep_timeout() for
311
 *         decription of its special meaning.
1502 jermar 312
 * @param flags Select mode of sleep operation. See documentation for waitq_sleep_timeout()i
313
 *      for description of its special meaning.
955 palkovsky 314
 * @return Recived message address
315
 * - to distinguish between call and answer, look at call->flags
316
 */
1502 jermar 317
call_t * ipc_wait_for_call(answerbox_t *box, __u32 usec, int flags)
955 palkovsky 318
{
319
    call_t *request;
1258 palkovsky 320
    ipl_t ipl;
1364 jermar 321
    int rc;
955 palkovsky 322
 
1364 jermar 323
restart:
1502 jermar 324
    rc = waitq_sleep_timeout(&box->wq, usec, flags);
1364 jermar 325
    if (SYNCH_FAILED(rc))
326
        return NULL;
1086 palkovsky 327
 
1040 palkovsky 328
    spinlock_lock(&box->lock);
1258 palkovsky 329
    if (!list_empty(&box->irq_notifs)) {
330
        ipl = interrupts_disable();
331
        spinlock_lock(&box->irq_lock);
332
 
1281 palkovsky 333
        request = list_get_instance(box->irq_notifs.next, call_t, list);
1258 palkovsky 334
        list_remove(&request->list);
335
 
336
        spinlock_unlock(&box->irq_lock);
337
        interrupts_restore(ipl);
338
    } else if (!list_empty(&box->answers)) {
1086 palkovsky 339
        /* Handle asynchronous answers */
340
        request = list_get_instance(box->answers.next, call_t, list);
341
        list_remove(&request->list);
342
        atomic_dec(&request->data.phone->active_calls);
343
    } else if (!list_empty(&box->calls)) {
344
        /* Handle requests */
345
        request = list_get_instance(box->calls.next, call_t, list);
346
        list_remove(&request->list);
347
        /* Append request to dispatch queue */
348
        list_append(&request->list, &box->dispatched_calls);
349
    } else {
1141 palkovsky 350
        /* This can happen regularly after ipc_cleanup, remove
351
         * the warning in the future when the IPC is
352
         * more debugged */
1086 palkovsky 353
        printf("WARNING: Spurious IPC wakeup.\n");
354
        spinlock_unlock(&box->lock);
355
        goto restart;
955 palkovsky 356
    }
1040 palkovsky 357
    spinlock_unlock(&box->lock);
955 palkovsky 358
    return request;
359
}
360
 
1141 palkovsky 361
/** Answer all calls from list with EHANGUP msg */
362
static void ipc_cleanup_call_list(link_t *lst)
363
{
364
    call_t *call;
365
 
366
    while (!list_empty(lst)) {
367
        call = list_get_instance(lst->next, call_t, list);
368
        list_remove(&call->list);
369
 
370
        IPC_SET_RETVAL(call->data, EHANGUP);
371
        _ipc_answer_free_call(call);
372
    }
373
}
374
 
1072 palkovsky 375
/** Cleans up all IPC communication of the given task
376
 *
377
 *
378
 */
379
void ipc_cleanup(task_t *task)
380
{
1088 palkovsky 381
    int i;
1141 palkovsky 382
    call_t *call;
383
    phone_t *phone;
384
 
1088 palkovsky 385
    /* Disconnect all our phones ('ipc_phone_hangup') */
386
    for (i=0;i < IPC_MAX_PHONES; i++)
1568 palkovsky 387
        ipc_phone_hangup(&task->phones[i], 1);
1088 palkovsky 388
 
1258 palkovsky 389
    /* Disconnect all connected irqs */
390
    ipc_irq_cleanup(&task->answerbox);
391
 
1141 palkovsky 392
    /* Disconnect all phones connected to our answerbox */
393
restart_phones:
394
    spinlock_lock(&task->answerbox.lock);
395
    while (!list_empty(&task->answerbox.connected_phones)) {
396
        phone = list_get_instance(task->answerbox.connected_phones.next,
397
                      phone_t,
398
                      list);
399
        if (! spinlock_trylock(&phone->lock)) {
400
            spinlock_unlock(&task->answerbox.lock);
401
            goto restart_phones;
402
        }
403
 
404
        /* Disconnect phone */
1568 palkovsky 405
        ASSERT(phone->state == IPC_PHONE_CONNECTED);
406
        phone->state = IPC_PHONE_SLAMMED;
1141 palkovsky 407
        list_remove(&phone->list);
1088 palkovsky 408
 
1141 palkovsky 409
        spinlock_unlock(&phone->lock);
410
    }
411
 
1088 palkovsky 412
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
1141 palkovsky 413
    spinlock_lock(&task->answerbox.lock);
414
    ipc_cleanup_call_list(&task->answerbox.dispatched_calls);
415
    ipc_cleanup_call_list(&task->answerbox.calls);
416
    spinlock_unlock(&task->answerbox.lock);
1072 palkovsky 417
 
1088 palkovsky 418
    /* Wait for all async answers to arrive */
1568 palkovsky 419
    while (1) {
420
        /* Go through all phones, until all are FREE... */
421
        /* Locking not needed, no one else should modify
422
         * it, when we are in cleanup */
423
        for (i=0;i < IPC_MAX_PHONES; i++) {
424
            if (task->phones[i].state == IPC_PHONE_HUNGUP && \
425
                atomic_get(&task->phones[i].active_calls) == 0)
426
                task->phones[i].state = IPC_PHONE_FREE;
427
            if (task->phones[i].state != IPC_PHONE_FREE)
428
                break;
429
        }
430
        /* Voila, got into cleanup */
431
        if (i == IPC_MAX_PHONES)
432
            break;
433
 
1502 jermar 434
        call = ipc_wait_for_call(&task->answerbox, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
1260 palkovsky 435
        ASSERT((call->flags & IPC_CALL_ANSWERED) || (call->flags & IPC_CALL_NOTIF));
1141 palkovsky 436
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
437
 
438
        atomic_dec(&task->active_calls);
439
        ipc_call_free(call);
440
    }
1072 palkovsky 441
}
1258 palkovsky 442
 
443
 
444
/** Initilize ipc subsystem */
445
void ipc_init(void)
446
{
447
    ipc_call_slab = slab_cache_create("ipc_call",
448
                      sizeof(call_t),
449
                      0,
450
                      NULL, NULL, 0);
451
    ipc_irq_make_table(IRQ_COUNT);
452
}
453