Subversion Repositories HelenOS

Rev

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

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