Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
954 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
 
1352 palkovsky 29
#include <ipc/ipc.h>
954 palkovsky 30
#include <libc.h>
999 palkovsky 31
#include <malloc.h>
32
#include <errno.h>
1352 palkovsky 33
#include <libadt/list.h>
999 palkovsky 34
#include <stdio.h>
35
#include <unistd.h>
1350 palkovsky 36
#include <futex.h>
1365 jermar 37
#include <kernel/synch/synch.h>
1463 palkovsky 38
#include <async.h>
39
#include <psthread.h>
954 palkovsky 40
 
999 palkovsky 41
/** Structure used for keeping track of sent async msgs
42
 * and queing unsent msgs
43
 *
44
 */
45
typedef struct {
46
    link_t list;
47
 
48
    ipc_async_callback_t callback;
49
    void *private;
50
    union {
51
        ipc_callid_t callid;
52
        struct {
1091 palkovsky 53
            ipc_call_t data;
999 palkovsky 54
            int phoneid;
55
        } msg;
56
    }u;
1463 palkovsky 57
    pstid_t ptid;   /**< Thread waiting for sending this msg */
999 palkovsky 58
} async_call_t;
59
 
60
LIST_INITIALIZE(dispatched_calls);
61
 
1463 palkovsky 62
/* queued_calls is protcted by async_futex, because if the
63
 * call cannot be sent into kernel, async framework is used
64
 * automatically
65
 */
66
LIST_INITIALIZE(queued_calls); /**< List of async calls that were not accepted
67
                *   by kernel */
68
 
1392 palkovsky 69
static atomic_t ipc_futex = FUTEX_INITIALIZER;
1350 palkovsky 70
 
999 palkovsky 71
int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1,
72
          ipcarg_t *result)
954 palkovsky 73
{
1091 palkovsky 74
    ipc_call_t resdata;
966 palkovsky 75
    int callres;
76
 
999 palkovsky 77
    callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
966 palkovsky 78
                 (sysarg_t)&resdata);
79
    if (callres)
80
        return callres;
81
    if (result)
82
        *result = IPC_GET_ARG1(resdata);
83
    return IPC_GET_RETVAL(resdata);
954 palkovsky 84
}
85
 
999 palkovsky 86
int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
87
            ipcarg_t arg2, ipcarg_t arg3,
88
            ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3)
954 palkovsky 89
{
1091 palkovsky 90
    ipc_call_t data;
966 palkovsky 91
    int callres;
92
 
93
    IPC_SET_METHOD(data, method);
94
    IPC_SET_ARG1(data, arg1);
95
    IPC_SET_ARG2(data, arg2);
96
    IPC_SET_ARG3(data, arg3);
97
 
1006 palkovsky 98
    callres = __SYSCALL3(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data,
99
                 (sysarg_t)&data);
966 palkovsky 100
    if (callres)
101
        return callres;
102
 
103
    if (result1)
104
        *result1 = IPC_GET_ARG1(data);
105
    if (result2)
106
        *result2 = IPC_GET_ARG2(data);
107
    if (result3)
108
        *result3 = IPC_GET_ARG3(data);
109
    return IPC_GET_RETVAL(data);
110
}
111
 
999 palkovsky 112
/** Syscall to send asynchronous message */
1091 palkovsky 113
static  ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data)
999 palkovsky 114
{
115
    return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data);
116
}
117
 
966 palkovsky 118
/** Send asynchronous message
119
 *
120
 * - if fatal error, call callback handler with proper error code
121
 * - if message cannot be temporarily sent, add to queue
122
 */
999 palkovsky 123
void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1,
124
              ipcarg_t arg2, void *private,
966 palkovsky 125
              ipc_async_callback_t callback)
126
{
999 palkovsky 127
    async_call_t *call;
966 palkovsky 128
    ipc_callid_t callid;
129
 
999 palkovsky 130
    call = malloc(sizeof(*call));
131
    if (!call) {
1443 palkovsky 132
        if (callback)
133
            callback(private, ENOMEM, NULL);
1028 palkovsky 134
        return;
999 palkovsky 135
    }
1463 palkovsky 136
 
137
    call->callback = callback;
138
    call->private = private;
139
 
140
    /* We need to make sure that we get callid before
141
     * another thread accesses the queue again */
142
    futex_down(&ipc_futex);
999 palkovsky 143
    callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2);
966 palkovsky 144
    if (callid == IPC_CALLRET_FATAL) {
1463 palkovsky 145
        futex_up(&ipc_futex);
966 palkovsky 146
        /* Call asynchronous handler with error code */
1443 palkovsky 147
        if (callback)
148
            callback(private, ENOENT, NULL);
999 palkovsky 149
        free(call);
966 palkovsky 150
        return;
151
    }
999 palkovsky 152
 
1463 palkovsky 153
    if (callid == IPC_CALLRET_TEMPORARY) {
154
        futex_up(&ipc_futex);
999 palkovsky 155
 
156
        call->u.msg.phoneid = phoneid;
157
        IPC_SET_METHOD(call->u.msg.data, method);
158
        IPC_SET_ARG1(call->u.msg.data, arg1);
159
        IPC_SET_ARG2(call->u.msg.data, arg2);
1463 palkovsky 160
 
161
        call->ptid = psthread_get_id();
162
        futex_down(&async_futex);
999 palkovsky 163
        list_append(&call->list, &queued_calls);
1463 palkovsky 164
 
165
        psthread_schedule_next_adv(PS_TO_MANAGER);
166
        /* Async futex unlocked by previous call */
966 palkovsky 167
        return;
168
    }
999 palkovsky 169
    call->u.callid = callid;
170
    /* Add call to list of dispatched calls */
171
    list_append(&call->list, &dispatched_calls);
1350 palkovsky 172
    futex_up(&ipc_futex);
954 palkovsky 173
}
174
 
966 palkovsky 175
 
1343 jermar 176
/** Send a fast answer to a received call.
177
 *
178
 * The fast answer makes use of passing retval and first two arguments in registers.
179
 * If you need to return more, use the ipc_answer() instead.
180
 *
181
 * @param callid ID of the call being answered.
182
 * @param retval Return value.
183
 * @param arg1 First return argument.
184
 * @param arg2 Second return argument.
185
 *
186
 * @return Zero on success or a value from @ref errno.h on failure.
187
 */
188
ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
999 palkovsky 189
        ipcarg_t arg2)
954 palkovsky 190
{
1330 palkovsky 191
    return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2);
954 palkovsky 192
}
193
 
1343 jermar 194
/** Send a full answer to a received call.
195
 *
196
 * @param callid ID of the call being answered.
197
 * @param call Call data. Must be already initialized by the responder.
198
 *
199
 * @return Zero on success or a value from @ref errno.h on failure.
200
 */
201
ipcarg_t ipc_answer(ipc_callid_t callid, ipc_call_t *call)
202
{
203
    return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call);
204
}
205
 
206
 
999 palkovsky 207
/** Try to dispatch queed calls from async queue */
208
static void try_dispatch_queued_calls(void)
209
{
210
    async_call_t *call;
211
    ipc_callid_t callid;
212
 
1463 palkovsky 213
    /* TODO: integrate intelligently ipc_futex, so that it
214
     * is locked during ipc_call_async, until it is added
215
     * to dispatched_calls
216
     */
217
    futex_down(&async_futex);
999 palkovsky 218
    while (!list_empty(&queued_calls)) {
219
        call = list_get_instance(queued_calls.next, async_call_t,
220
                     list);
221
 
222
        callid = _ipc_call_async(call->u.msg.phoneid,
223
                     &call->u.msg.data);
1463 palkovsky 224
        if (callid == IPC_CALLRET_TEMPORARY) {
999 palkovsky 225
            break;
1463 palkovsky 226
        }
999 palkovsky 227
        list_remove(&call->list);
1350 palkovsky 228
 
1463 palkovsky 229
        futex_up(&async_futex);
230
        psthread_add_ready(call->ptid);
231
 
999 palkovsky 232
        if (callid == IPC_CALLRET_FATAL) {
1443 palkovsky 233
            if (call->callback)
234
                call->callback(call->private, ENOENT, NULL);
999 palkovsky 235
            free(call);
236
        } else {
237
            call->u.callid = callid;
1463 palkovsky 238
            futex_down(&ipc_futex);
999 palkovsky 239
            list_append(&call->list, &dispatched_calls);
1463 palkovsky 240
            futex_up(&ipc_futex);
999 palkovsky 241
        }
1463 palkovsky 242
        futex_down(&async_futex);
999 palkovsky 243
    }
1463 palkovsky 244
    futex_up(&async_futex);
999 palkovsky 245
}
246
 
247
/** Handle received answer
248
 *
249
 * TODO: Make it use hash table
250
 *
251
 * @param callid Callid (with first bit set) of the answered call
252
 */
1091 palkovsky 253
static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
999 palkovsky 254
{
255
    link_t *item;
256
    async_call_t *call;
257
 
258
    callid &= ~IPC_CALLID_ANSWERED;
259
 
1350 palkovsky 260
    futex_down(&ipc_futex);
999 palkovsky 261
    for (item = dispatched_calls.next; item != &dispatched_calls;
262
         item = item->next) {
263
        call = list_get_instance(item, async_call_t, list);
264
        if (call->u.callid == callid) {
265
            list_remove(&call->list);
1350 palkovsky 266
            futex_up(&ipc_futex);
1443 palkovsky 267
            if (call->callback)
268
                call->callback(call->private,
269
                           IPC_GET_RETVAL(*data),
270
                           data);
271
            free(call);
999 palkovsky 272
            return;
273
        }
274
    }
1350 palkovsky 275
    futex_up(&ipc_futex);
999 palkovsky 276
    printf("Received unidentified answer: %P!!!\n", callid);
277
}
278
 
279
 
1392 palkovsky 280
/** One cycle of ipc wait for call call
954 palkovsky 281
 *
282
 * - dispatch ASYNC reoutines in the background
1365 jermar 283
 * @param call Space where the message is stored
1392 palkovsky 284
 * @param usec Timeout in microseconds
285
 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking)
1365 jermar 286
 * @return Callid of the answer.
954 palkovsky 287
 */
1392 palkovsky 288
ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags)
954 palkovsky 289
{
290
    ipc_callid_t callid;
291
 
1392 palkovsky 292
    callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
293
    /* Handle received answers */
1463 palkovsky 294
    if (callid & IPC_CALLID_ANSWERED) {
1392 palkovsky 295
        handle_answer(callid, call);
1463 palkovsky 296
        try_dispatch_queued_calls();
297
    }
999 palkovsky 298
 
954 palkovsky 299
    return callid;
300
}
1028 palkovsky 301
 
1365 jermar 302
/** Wait some time for an IPC call.
303
 *
304
 * - dispatch ASYNC reoutines in the background
305
 * @param call Space where the message is stored
306
 * @param usec Timeout in microseconds.
307
 * @return Callid of the answer.
308
 */
309
ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec)
310
{
311
    ipc_callid_t callid;
312
 
313
    do {
1392 palkovsky 314
        callid = ipc_wait_cycle(call, usec, SYNCH_BLOCKING);
1365 jermar 315
    } while (callid & IPC_CALLID_ANSWERED);
316
 
317
    return callid;
318
}
319
 
320
/** Check if there is an IPC call waiting to be picked up.
321
 *
322
 * - dispatch ASYNC reoutines in the background
323
 * @param call Space where the message is stored
324
 * @return Callid of the answer.
325
 */
326
ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
327
{
328
    ipc_callid_t callid;
329
 
330
    do {
1392 palkovsky 331
        callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT, SYNCH_NON_BLOCKING);
1365 jermar 332
    } while (callid & IPC_CALLID_ANSWERED);
333
 
334
    return callid;
335
}
336
 
1091 palkovsky 337
/** Ask destination to do a callback connection
338
 *
339
 * @return 0 - OK, error code
340
 */
341
int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phone)
1028 palkovsky 342
{
1091 palkovsky 343
    return ipc_call_sync_3(phoneid, IPC_M_CONNECT_TO_ME, arg1,
344
                   arg2, 0, 0, 0, phone);
1028 palkovsky 345
}
1061 palkovsky 346
 
1091 palkovsky 347
/** Ask through phone for a new connection to some service
348
 *
349
 * @return new phoneid - OK, error code
350
 */
1061 palkovsky 351
int ipc_connect_me_to(int phoneid, int arg1, int arg2)
352
{
1092 palkovsky 353
    ipcarg_t newphid;
1091 palkovsky 354
    int res;
355
 
356
    res =  ipc_call_sync_3(phoneid, IPC_M_CONNECT_ME_TO, arg1,
357
                   arg2, 0, 0, 0, &newphid);
358
    if (res)
359
        return res;
360
    return newphid;
1061 palkovsky 361
}
362
 
1089 palkovsky 363
/* Hang up specified phone */
364
int ipc_hangup(int phoneid)
365
{
366
    return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
367
}
1259 palkovsky 368
 
1282 palkovsky 369
int ipc_register_irq(int irq, irq_code_t *ucode)
1259 palkovsky 370
{
1282 palkovsky 371
    return __SYSCALL2(SYS_IPC_REGISTER_IRQ, irq, (sysarg_t) ucode);
1259 palkovsky 372
}
373
 
374
int ipc_unregister_irq(int irq)
375
{
376
    return __SYSCALL1(SYS_IPC_UNREGISTER_IRQ, irq);
377
}
1330 palkovsky 378
 
1336 jermar 379
int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1)
380
{
381
    return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1);
382
}
383
 
1350 palkovsky 384
 
385
/** Open shared memory connection over specified phoneid
386
 *
387
 *
1360 jermar 388
 * Allocate as_area, notify the other side about our intention
1350 palkovsky 389
 * to open the connection
390
 *
391
 * @return Connection id identifying this connection
392
 */
393
//int ipc_dgr_open(int pohoneid, size_t bufsize)
394
//{
395
    /* Find new file descriptor in local descriptor table */
396
    /* Create AS_area, initialize structures */
397
    /* Send AS to other side, handle error states */
398
 
399
//}
1330 palkovsky 400
/*
1350 palkovsky 401
void ipc_dgr_close(int cid)
1330 palkovsky 402
{
403
}
404
 
1350 palkovsky 405
void * ipc_dgr_alloc(int cid, size_t size)
406
{
407
}
1330 palkovsky 408
 
1350 palkovsky 409
void ipc_dgr_free(int cid, void *area)
410
{
411
 
412
}
413
 
414
int ipc_dgr_send(int cid, void *area)
415
{
416
}
417
 
418
 
419
int ipc_dgr_send_data(int cid, void *data, size_t size)
420
{
421
}
422
 
1330 palkovsky 423
*/