Subversion Repositories HelenOS-historic

Rev

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

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