Subversion Repositories HelenOS-historic

Rev

Rev 1610 | Rev 1709 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1610 Rev 1653
1
/*
1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
 
27
 */
-
 
28
 
-
 
29
/** @addtogroup libc
-
 
30
 * @{
-
 
31
 * @}
-
 
32
 */
-
 
33
 
-
 
34
 /** @addtogroup libcipc IPC
-
 
35
 * @brief HelenOS uspace IPC
-
 
36
 * @{
-
 
37
 * @ingroup libc
-
 
38
 */
-
 
39
/** @file
27
 */
40
 */
28
 
41
 
29
#include <ipc/ipc.h>
42
#include <ipc/ipc.h>
30
#include <libc.h>
43
#include <libc.h>
31
#include <malloc.h>
44
#include <malloc.h>
32
#include <errno.h>
45
#include <errno.h>
33
#include <libadt/list.h>
46
#include <libadt/list.h>
34
#include <stdio.h>
47
#include <stdio.h>
35
#include <unistd.h>
48
#include <unistd.h>
36
#include <futex.h>
49
#include <futex.h>
37
#include <kernel/synch/synch.h>
50
#include <kernel/synch/synch.h>
38
#include <async.h>
51
#include <async.h>
39
#include <psthread.h>
52
#include <psthread.h>
40
 
53
 
41
/** Structure used for keeping track of sent async msgs
54
/** Structure used for keeping track of sent async msgs
42
 * and queing unsent msgs
55
 * and queing unsent msgs
43
 *
56
 *
44
 */
57
 */
45
typedef struct {
58
typedef struct {
46
    link_t list;
59
    link_t list;
47
 
60
 
48
    ipc_async_callback_t callback;
61
    ipc_async_callback_t callback;
49
    void *private;
62
    void *private;
50
    union {
63
    union {
51
        ipc_callid_t callid;
64
        ipc_callid_t callid;
52
        struct {
65
        struct {
53
            ipc_call_t data;
66
            ipc_call_t data;
54
            int phoneid;
67
            int phoneid;
55
        } msg;
68
        } msg;
56
    }u;
69
    }u;
57
    pstid_t ptid;   /**< Thread waiting for sending this msg */
70
    pstid_t ptid;   /**< Thread waiting for sending this msg */
58
} async_call_t;
71
} async_call_t;
59
 
72
 
60
LIST_INITIALIZE(dispatched_calls);
73
LIST_INITIALIZE(dispatched_calls);
61
 
74
 
62
/* queued_calls is protcted by async_futex, because if the
75
/* queued_calls is protcted by async_futex, because if the
63
 * call cannot be sent into kernel, async framework is used
76
 * call cannot be sent into kernel, async framework is used
64
 * automatically
77
 * automatically
65
 */
78
 */
66
LIST_INITIALIZE(queued_calls); /**< List of async calls that were not accepted
79
LIST_INITIALIZE(queued_calls); /**< List of async calls that were not accepted
67
                *   by kernel */
80
                *   by kernel */
68
 
81
 
69
static atomic_t ipc_futex = FUTEX_INITIALIZER;
82
static atomic_t ipc_futex = FUTEX_INITIALIZER;
70
 
83
 
71
int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1,
84
int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1,
72
          ipcarg_t *result)
85
          ipcarg_t *result)
73
{
86
{
74
    ipc_call_t resdata;
87
    ipc_call_t resdata;
75
    int callres;
88
    int callres;
76
   
89
   
77
    callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
90
    callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
78
                 (sysarg_t)&resdata);
91
                 (sysarg_t)&resdata);
79
    if (callres)
92
    if (callres)
80
        return callres;
93
        return callres;
81
    if (result)
94
    if (result)
82
        *result = IPC_GET_ARG1(resdata);
95
        *result = IPC_GET_ARG1(resdata);
83
    return IPC_GET_RETVAL(resdata);
96
    return IPC_GET_RETVAL(resdata);
84
}
97
}
85
 
98
 
86
int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
99
int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
87
            ipcarg_t arg2, ipcarg_t arg3,
100
            ipcarg_t arg2, ipcarg_t arg3,
88
            ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3)
101
            ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3)
89
{
102
{
90
    ipc_call_t data;
103
    ipc_call_t data;
91
    int callres;
104
    int callres;
92
 
105
 
93
    IPC_SET_METHOD(data, method);
106
    IPC_SET_METHOD(data, method);
94
    IPC_SET_ARG1(data, arg1);
107
    IPC_SET_ARG1(data, arg1);
95
    IPC_SET_ARG2(data, arg2);
108
    IPC_SET_ARG2(data, arg2);
96
    IPC_SET_ARG3(data, arg3);
109
    IPC_SET_ARG3(data, arg3);
97
 
110
 
98
    callres = __SYSCALL3(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data,
111
    callres = __SYSCALL3(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data,
99
                 (sysarg_t)&data);
112
                 (sysarg_t)&data);
100
    if (callres)
113
    if (callres)
101
        return callres;
114
        return callres;
102
 
115
 
103
    if (result1)
116
    if (result1)
104
        *result1 = IPC_GET_ARG1(data);
117
        *result1 = IPC_GET_ARG1(data);
105
    if (result2)
118
    if (result2)
106
        *result2 = IPC_GET_ARG2(data);
119
        *result2 = IPC_GET_ARG2(data);
107
    if (result3)
120
    if (result3)
108
        *result3 = IPC_GET_ARG3(data);
121
        *result3 = IPC_GET_ARG3(data);
109
    return IPC_GET_RETVAL(data);
122
    return IPC_GET_RETVAL(data);
110
}
123
}
111
 
124
 
112
/** Syscall to send asynchronous message */
125
/** Syscall to send asynchronous message */
113
static  ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data)
126
static  ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data)
114
{
127
{
115
    return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data);
128
    return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data);
116
}
129
}
117
 
130
 
118
/** Prolog to ipc_async_send functions */
131
/** Prolog to ipc_async_send functions */
119
static inline async_call_t *ipc_prepare_async(void *private, ipc_async_callback_t callback)
132
static inline async_call_t *ipc_prepare_async(void *private, ipc_async_callback_t callback)
120
{
133
{
121
    async_call_t *call;
134
    async_call_t *call;
122
 
135
 
123
    call = malloc(sizeof(*call));
136
    call = malloc(sizeof(*call));
124
    if (!call) {
137
    if (!call) {
125
        if (callback)
138
        if (callback)
126
            callback(private, ENOMEM, NULL);
139
            callback(private, ENOMEM, NULL);
127
        return NULL;
140
        return NULL;
128
    }
141
    }
129
    call->callback = callback;
142
    call->callback = callback;
130
    call->private = private;
143
    call->private = private;
131
 
144
 
132
    return call;
145
    return call;
133
}
146
}
134
 
147
 
135
/** Epilogue of ipc_async_send functions */
148
/** Epilogue of ipc_async_send functions */
136
static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
149
static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
137
                    async_call_t *call, int can_preempt)
150
                    async_call_t *call, int can_preempt)
138
{
151
{
139
    if (callid == IPC_CALLRET_FATAL) {
152
    if (callid == IPC_CALLRET_FATAL) {
140
        futex_up(&ipc_futex);
153
        futex_up(&ipc_futex);
141
        /* Call asynchronous handler with error code */
154
        /* Call asynchronous handler with error code */
142
        if (call->callback)
155
        if (call->callback)
143
            call->callback(call->private, ENOENT, NULL);
156
            call->callback(call->private, ENOENT, NULL);
144
        free(call);
157
        free(call);
145
        return;
158
        return;
146
    }
159
    }
147
 
160
 
148
    if (callid == IPC_CALLRET_TEMPORARY) {
161
    if (callid == IPC_CALLRET_TEMPORARY) {
149
        futex_up(&ipc_futex);
162
        futex_up(&ipc_futex);
150
 
163
 
151
        call->u.msg.phoneid = phoneid;
164
        call->u.msg.phoneid = phoneid;
152
       
165
       
153
        futex_down(&async_futex);
166
        futex_down(&async_futex);
154
        list_append(&call->list, &queued_calls);
167
        list_append(&call->list, &queued_calls);
155
 
168
 
156
        if (can_preempt) {
169
        if (can_preempt) {
157
            call->ptid = psthread_get_id();
170
            call->ptid = psthread_get_id();
158
            psthread_schedule_next_adv(PS_TO_MANAGER);
171
            psthread_schedule_next_adv(PS_TO_MANAGER);
159
            /* Async futex unlocked by previous call */
172
            /* Async futex unlocked by previous call */
160
        } else {
173
        } else {
161
            call->ptid = 0;
174
            call->ptid = 0;
162
            futex_up(&async_futex);
175
            futex_up(&async_futex);
163
        }
176
        }
164
        return;
177
        return;
165
    }
178
    }
166
    call->u.callid = callid;
179
    call->u.callid = callid;
167
    /* Add call to list of dispatched calls */
180
    /* Add call to list of dispatched calls */
168
    list_append(&call->list, &dispatched_calls);
181
    list_append(&call->list, &dispatched_calls);
169
    futex_up(&ipc_futex);
182
    futex_up(&ipc_futex);
170
   
183
   
171
}
184
}
172
 
185
 
173
/** Send asynchronous message
186
/** Send asynchronous message
174
 *
187
 *
175
 * - if fatal error, call callback handler with proper error code
188
 * - if fatal error, call callback handler with proper error code
176
 * - if message cannot be temporarily sent, add to queue
189
 * - if message cannot be temporarily sent, add to queue
177
 */
190
 */
178
void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1,
191
void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1,
179
              ipcarg_t arg2, void *private,
192
              ipcarg_t arg2, void *private,
180
              ipc_async_callback_t callback, int can_preempt)
193
              ipc_async_callback_t callback, int can_preempt)
181
{
194
{
182
    async_call_t *call;
195
    async_call_t *call;
183
    ipc_callid_t callid;
196
    ipc_callid_t callid;
184
 
197
 
185
    call = ipc_prepare_async(private, callback);
198
    call = ipc_prepare_async(private, callback);
186
    if (!call)
199
    if (!call)
187
        return;
200
        return;
188
 
201
 
189
    /* We need to make sure that we get callid before
202
    /* We need to make sure that we get callid before
190
     * another thread accesses the queue again */
203
     * another thread accesses the queue again */
191
    futex_down(&ipc_futex);
204
    futex_down(&ipc_futex);
192
    callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2);
205
    callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2);
193
 
206
 
194
    if (callid == IPC_CALLRET_TEMPORARY) {
207
    if (callid == IPC_CALLRET_TEMPORARY) {
195
        IPC_SET_METHOD(call->u.msg.data, method);
208
        IPC_SET_METHOD(call->u.msg.data, method);
196
        IPC_SET_ARG1(call->u.msg.data, arg1);
209
        IPC_SET_ARG1(call->u.msg.data, arg1);
197
        IPC_SET_ARG2(call->u.msg.data, arg2);
210
        IPC_SET_ARG2(call->u.msg.data, arg2);
198
    }
211
    }
199
    ipc_finish_async(callid, phoneid, call, can_preempt);
212
    ipc_finish_async(callid, phoneid, call, can_preempt);
200
}
213
}
201
 
214
 
202
/** Send asynchronous message
215
/** Send asynchronous message
203
 *
216
 *
204
 * - if fatal error, call callback handler with proper error code
217
 * - if fatal error, call callback handler with proper error code
205
 * - if message cannot be temporarily sent, add to queue
218
 * - if message cannot be temporarily sent, add to queue
206
 */
219
 */
207
void ipc_call_async_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
220
void ipc_call_async_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
208
              ipcarg_t arg2, ipcarg_t arg3, void *private,
221
              ipcarg_t arg2, ipcarg_t arg3, void *private,
209
              ipc_async_callback_t callback, int can_preempt)
222
              ipc_async_callback_t callback, int can_preempt)
210
{
223
{
211
    async_call_t *call;
224
    async_call_t *call;
212
    ipc_callid_t callid;
225
    ipc_callid_t callid;
213
 
226
 
214
    call = ipc_prepare_async(private, callback);
227
    call = ipc_prepare_async(private, callback);
215
    if (!call)
228
    if (!call)
216
        return;
229
        return;
217
 
230
 
218
    IPC_SET_METHOD(call->u.msg.data, method);
231
    IPC_SET_METHOD(call->u.msg.data, method);
219
    IPC_SET_ARG1(call->u.msg.data, arg1);
232
    IPC_SET_ARG1(call->u.msg.data, arg1);
220
    IPC_SET_ARG2(call->u.msg.data, arg2);
233
    IPC_SET_ARG2(call->u.msg.data, arg2);
221
    IPC_SET_ARG3(call->u.msg.data, arg3);
234
    IPC_SET_ARG3(call->u.msg.data, arg3);
222
    /* We need to make sure that we get callid before
235
    /* We need to make sure that we get callid before
223
     * another thread accesses the queue again */
236
     * another thread accesses the queue again */
224
    futex_down(&ipc_futex);
237
    futex_down(&ipc_futex);
225
    callid = _ipc_call_async(phoneid, &call->u.msg.data);
238
    callid = _ipc_call_async(phoneid, &call->u.msg.data);
226
 
239
 
227
    ipc_finish_async(callid, phoneid, call, can_preempt);
240
    ipc_finish_async(callid, phoneid, call, can_preempt);
228
}
241
}
229
 
242
 
230
 
243
 
231
/** Send a fast answer to a received call.
244
/** Send a fast answer to a received call.
232
 *
245
 *
233
 * The fast answer makes use of passing retval and first two arguments in registers.
246
 * The fast answer makes use of passing retval and first two arguments in registers.
234
 * If you need to return more, use the ipc_answer() instead.
247
 * If you need to return more, use the ipc_answer() instead.
235
 *
248
 *
236
 * @param callid ID of the call being answered.
249
 * @param callid ID of the call being answered.
237
 * @param retval Return value.
250
 * @param retval Return value.
238
 * @param arg1 First return argument.
251
 * @param arg1 First return argument.
239
 * @param arg2 Second return argument.
252
 * @param arg2 Second return argument.
240
 *
253
 *
241
 * @return Zero on success or a value from @ref errno.h on failure.
254
 * @return Zero on success or a value from @ref errno.h on failure.
242
 */
255
 */
243
ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
256
ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
244
        ipcarg_t arg2)
257
        ipcarg_t arg2)
245
{
258
{
246
    return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2);
259
    return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2);
247
}
260
}
248
 
261
 
249
/** Send a full answer to a received call.
262
/** Send a full answer to a received call.
250
 *
263
 *
251
 * @param callid ID of the call being answered.
264
 * @param callid ID of the call being answered.
252
 * @param call Call data. Must be already initialized by the responder.
265
 * @param call Call data. Must be already initialized by the responder.
253
 *
266
 *
254
 * @return Zero on success or a value from @ref errno.h on failure.
267
 * @return Zero on success or a value from @ref errno.h on failure.
255
 */
268
 */
256
ipcarg_t ipc_answer(ipc_callid_t callid, ipc_call_t *call)
269
ipcarg_t ipc_answer(ipc_callid_t callid, ipc_call_t *call)
257
{
270
{
258
    return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call);
271
    return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call);
259
}
272
}
260
 
273
 
261
 
274
 
262
/** Try to dispatch queed calls from async queue */
275
/** Try to dispatch queed calls from async queue */
263
static void try_dispatch_queued_calls(void)
276
static void try_dispatch_queued_calls(void)
264
{
277
{
265
    async_call_t *call;
278
    async_call_t *call;
266
    ipc_callid_t callid;
279
    ipc_callid_t callid;
267
 
280
 
268
    /* TODO: integrate intelligently ipc_futex, so that it
281
    /* TODO: integrate intelligently ipc_futex, so that it
269
     * is locked during ipc_call_async, until it is added
282
     * is locked during ipc_call_async, until it is added
270
     * to dispatched_calls
283
     * to dispatched_calls
271
     */
284
     */
272
    futex_down(&async_futex);
285
    futex_down(&async_futex);
273
    while (!list_empty(&queued_calls)) {
286
    while (!list_empty(&queued_calls)) {
274
        call = list_get_instance(queued_calls.next, async_call_t,
287
        call = list_get_instance(queued_calls.next, async_call_t,
275
                     list);
288
                     list);
276
 
289
 
277
        callid = _ipc_call_async(call->u.msg.phoneid,
290
        callid = _ipc_call_async(call->u.msg.phoneid,
278
                     &call->u.msg.data);
291
                     &call->u.msg.data);
279
        if (callid == IPC_CALLRET_TEMPORARY) {
292
        if (callid == IPC_CALLRET_TEMPORARY) {
280
            break;
293
            break;
281
        }
294
        }
282
        list_remove(&call->list);
295
        list_remove(&call->list);
283
 
296
 
284
        futex_up(&async_futex);
297
        futex_up(&async_futex);
285
        if (call->ptid)
298
        if (call->ptid)
286
            psthread_add_ready(call->ptid);
299
            psthread_add_ready(call->ptid);
287
       
300
       
288
        if (callid == IPC_CALLRET_FATAL) {
301
        if (callid == IPC_CALLRET_FATAL) {
289
            if (call->callback)
302
            if (call->callback)
290
                call->callback(call->private, ENOENT, NULL);
303
                call->callback(call->private, ENOENT, NULL);
291
            free(call);
304
            free(call);
292
        } else {
305
        } else {
293
            call->u.callid = callid;
306
            call->u.callid = callid;
294
            futex_down(&ipc_futex);
307
            futex_down(&ipc_futex);
295
            list_append(&call->list, &dispatched_calls);
308
            list_append(&call->list, &dispatched_calls);
296
            futex_up(&ipc_futex);
309
            futex_up(&ipc_futex);
297
        }
310
        }
298
        futex_down(&async_futex);
311
        futex_down(&async_futex);
299
    }
312
    }
300
    futex_up(&async_futex);
313
    futex_up(&async_futex);
301
}
314
}
302
 
315
 
303
/** Handle received answer
316
/** Handle received answer
304
 *
317
 *
305
 * TODO: Make it use hash table
318
 * TODO: Make it use hash table
306
 *
319
 *
307
 * @param callid Callid (with first bit set) of the answered call
320
 * @param callid Callid (with first bit set) of the answered call
308
 */
321
 */
309
static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
322
static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
310
{
323
{
311
    link_t *item;
324
    link_t *item;
312
    async_call_t *call;
325
    async_call_t *call;
313
 
326
 
314
    callid &= ~IPC_CALLID_ANSWERED;
327
    callid &= ~IPC_CALLID_ANSWERED;
315
   
328
   
316
    futex_down(&ipc_futex);
329
    futex_down(&ipc_futex);
317
    for (item = dispatched_calls.next; item != &dispatched_calls;
330
    for (item = dispatched_calls.next; item != &dispatched_calls;
318
         item = item->next) {
331
         item = item->next) {
319
        call = list_get_instance(item, async_call_t, list);
332
        call = list_get_instance(item, async_call_t, list);
320
        if (call->u.callid == callid) {
333
        if (call->u.callid == callid) {
321
            list_remove(&call->list);
334
            list_remove(&call->list);
322
            futex_up(&ipc_futex);
335
            futex_up(&ipc_futex);
323
            if (call->callback)
336
            if (call->callback)
324
                call->callback(call->private,
337
                call->callback(call->private,
325
                           IPC_GET_RETVAL(*data),
338
                           IPC_GET_RETVAL(*data),
326
                           data);
339
                           data);
327
            free(call);
340
            free(call);
328
            return;
341
            return;
329
        }
342
        }
330
    }
343
    }
331
    futex_up(&ipc_futex);
344
    futex_up(&ipc_futex);
332
    printf("Received unidentified answer: %P!!!\n", callid);
345
    printf("Received unidentified answer: %P!!!\n", callid);
333
}
346
}
334
 
347
 
335
 
348
 
336
/** One cycle of ipc wait for call call
349
/** One cycle of ipc wait for call call
337
 *
350
 *
338
 * - dispatch ASYNC reoutines in the background
351
 * - dispatch ASYNC reoutines in the background
339
 * @param call Space where the message is stored
352
 * @param call Space where the message is stored
340
 * @param usec Timeout in microseconds
353
 * @param usec Timeout in microseconds
341
 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking)
354
 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking)
342
 * @return Callid of the answer.
355
 * @return Callid of the answer.
343
 */
356
 */
344
ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags)
357
ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags)
345
{
358
{
346
    ipc_callid_t callid;
359
    ipc_callid_t callid;
347
 
360
 
348
    callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
361
    callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
349
    /* Handle received answers */
362
    /* Handle received answers */
350
    if (callid & IPC_CALLID_ANSWERED) {
363
    if (callid & IPC_CALLID_ANSWERED) {
351
        handle_answer(callid, call);
364
        handle_answer(callid, call);
352
        try_dispatch_queued_calls();
365
        try_dispatch_queued_calls();
353
    }
366
    }
354
 
367
 
355
    return callid;
368
    return callid;
356
}
369
}
357
 
370
 
358
/** Wait some time for an IPC call.
371
/** Wait some time for an IPC call.
359
 *
372
 *
360
 * - dispatch ASYNC reoutines in the background
373
 * - dispatch ASYNC reoutines in the background
361
 * @param call Space where the message is stored
374
 * @param call Space where the message is stored
362
 * @param usec Timeout in microseconds.
375
 * @param usec Timeout in microseconds.
363
 * @return Callid of the answer.
376
 * @return Callid of the answer.
364
 */
377
 */
365
ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec)
378
ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec)
366
{
379
{
367
    ipc_callid_t callid;
380
    ipc_callid_t callid;
368
 
381
 
369
    do {
382
    do {
370
        callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
383
        callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
371
    } while (callid & IPC_CALLID_ANSWERED);
384
    } while (callid & IPC_CALLID_ANSWERED);
372
 
385
 
373
    return callid;
386
    return callid;
374
}
387
}
375
 
388
 
376
/** Check if there is an IPC call waiting to be picked up.
389
/** Check if there is an IPC call waiting to be picked up.
377
 *
390
 *
378
 * - dispatch ASYNC reoutines in the background
391
 * - dispatch ASYNC reoutines in the background
379
 * @param call Space where the message is stored
392
 * @param call Space where the message is stored
380
 * @return Callid of the answer.
393
 * @return Callid of the answer.
381
 */
394
 */
382
ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
395
ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
383
{
396
{
384
    ipc_callid_t callid;
397
    ipc_callid_t callid;
385
 
398
 
386
    do {
399
    do {
387
        callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NON_BLOCKING);
400
        callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NON_BLOCKING);
388
    } while (callid & IPC_CALLID_ANSWERED);
401
    } while (callid & IPC_CALLID_ANSWERED);
389
 
402
 
390
    return callid;
403
    return callid;
391
}
404
}
392
 
405
 
393
/** Ask destination to do a callback connection
406
/** Ask destination to do a callback connection
394
 *
407
 *
395
 * @return 0 - OK, error code
408
 * @return 0 - OK, error code
396
 */
409
 */
397
int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phone)
410
int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phone)
398
{
411
{
399
    return ipc_call_sync_3(phoneid, IPC_M_CONNECT_TO_ME, arg1,
412
    return ipc_call_sync_3(phoneid, IPC_M_CONNECT_TO_ME, arg1,
400
                   arg2, 0, 0, 0, phone);
413
                   arg2, 0, 0, 0, phone);
401
}
414
}
402
 
415
 
403
/** Ask through phone for a new connection to some service
416
/** Ask through phone for a new connection to some service
404
 *
417
 *
405
 * @return new phoneid - OK, error code
418
 * @return new phoneid - OK, error code
406
 */
419
 */
407
int ipc_connect_me_to(int phoneid, int arg1, int arg2)
420
int ipc_connect_me_to(int phoneid, int arg1, int arg2)
408
{
421
{
409
    ipcarg_t newphid;
422
    ipcarg_t newphid;
410
    int res;
423
    int res;
411
 
424
 
412
    res =  ipc_call_sync_3(phoneid, IPC_M_CONNECT_ME_TO, arg1,
425
    res =  ipc_call_sync_3(phoneid, IPC_M_CONNECT_ME_TO, arg1,
413
                   arg2, 0, 0, 0, &newphid);
426
                   arg2, 0, 0, 0, &newphid);
414
    if (res)
427
    if (res)
415
        return res;
428
        return res;
416
    return newphid;
429
    return newphid;
417
}
430
}
418
 
431
 
419
/* Hang up specified phone */
432
/* Hang up specified phone */
420
int ipc_hangup(int phoneid)
433
int ipc_hangup(int phoneid)
421
{
434
{
422
    return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
435
    return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
423
}
436
}
424
 
437
 
425
int ipc_register_irq(int irq, irq_code_t *ucode)
438
int ipc_register_irq(int irq, irq_code_t *ucode)
426
{
439
{
427
    return __SYSCALL2(SYS_IPC_REGISTER_IRQ, irq, (sysarg_t) ucode);
440
    return __SYSCALL2(SYS_IPC_REGISTER_IRQ, irq, (sysarg_t) ucode);
428
}
441
}
429
 
442
 
430
int ipc_unregister_irq(int irq)
443
int ipc_unregister_irq(int irq)
431
{
444
{
432
    return __SYSCALL1(SYS_IPC_UNREGISTER_IRQ, irq);
445
    return __SYSCALL1(SYS_IPC_UNREGISTER_IRQ, irq);
433
}
446
}
434
 
447
 
435
int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1)
448
int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1)
436
{
449
{
437
    return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1);
450
    return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1);
438
}
451
}
439
 
452
 
-
 
453
 
-
 
454
 
-
 
455
 /** @}
-
 
456
 */
-
 
457
 
-
 
458
 
440
 
459