Subversion Repositories HelenOS-historic

Rev

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

Rev 1463 Rev 1489
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
/** Send asynchronous message
118
/** Prolog to ipc_async_send functions */
119
 *
-
 
120
 * - if fatal error, call callback handler with proper error code
-
 
121
 * - if message cannot be temporarily sent, add to queue
-
 
122
 */
-
 
123
void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1,
-
 
124
              ipcarg_t arg2, void *private,
-
 
125
              ipc_async_callback_t callback)
119
static inline async_call_t *ipc_prepare_async(void *private, ipc_async_callback_t callback)
126
{
120
{
127
    async_call_t *call;
121
    async_call_t *call;
128
    ipc_callid_t callid;
-
 
129
 
122
 
130
    call = malloc(sizeof(*call));
123
    call = malloc(sizeof(*call));
131
    if (!call) {
124
    if (!call) {
132
        if (callback)
125
        if (callback)
133
            callback(private, ENOMEM, NULL);
126
            callback(private, ENOMEM, NULL);
134
        return;
127
        return NULL;
135
    }
128
    }
136
 
-
 
137
    call->callback = callback;
129
    call->callback = callback;
138
    call->private = private;
130
    call->private = private;
139
 
131
 
140
    /* We need to make sure that we get callid before
132
    return call;
-
 
133
}
-
 
134
 
141
     * another thread accesses the queue again */
135
/** Epilogue of ipc_async_send functions */
142
    futex_down(&ipc_futex);
-
 
143
    callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2);
136
static inline void ipc_finish_async(ipc_callid_t callid, int phoneid, async_call_t *call)
-
 
137
{
144
    if (callid == IPC_CALLRET_FATAL) {
138
    if (callid == IPC_CALLRET_FATAL) {
145
        futex_up(&ipc_futex);
139
        futex_up(&ipc_futex);
146
        /* Call asynchronous handler with error code */
140
        /* Call asynchronous handler with error code */
147
        if (callback)
141
        if (call->callback)
148
            callback(private, ENOENT, NULL);
142
            call->callback(call->private, ENOENT, NULL);
149
        free(call);
143
        free(call);
150
        return;
144
        return;
151
    }
145
    }
152
 
146
 
153
    if (callid == IPC_CALLRET_TEMPORARY) {
147
    if (callid == IPC_CALLRET_TEMPORARY) {
154
        futex_up(&ipc_futex);
148
        futex_up(&ipc_futex);
155
 
149
 
156
        call->u.msg.phoneid = phoneid;
150
        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);
-
 
160
 
151
 
161
        call->ptid = psthread_get_id();
152
        call->ptid = psthread_get_id();
162
        futex_down(&async_futex);
153
        futex_down(&async_futex);
163
        list_append(&call->list, &queued_calls);
154
        list_append(&call->list, &queued_calls);
164
 
155
 
165
        psthread_schedule_next_adv(PS_TO_MANAGER);
156
        psthread_schedule_next_adv(PS_TO_MANAGER);
166
        /* Async futex unlocked by previous call */
157
        /* Async futex unlocked by previous call */
167
        return;
158
        return;
168
    }
159
    }
169
    call->u.callid = callid;
160
    call->u.callid = callid;
170
    /* Add call to list of dispatched calls */
161
    /* Add call to list of dispatched calls */
171
    list_append(&call->list, &dispatched_calls);
162
    list_append(&call->list, &dispatched_calls);
172
    futex_up(&ipc_futex);
163
    futex_up(&ipc_futex);
-
 
164
   
-
 
165
}
-
 
166
 
-
 
167
/** Send asynchronous message
-
 
168
 *
-
 
169
 * - if fatal error, call callback handler with proper error code
-
 
170
 * - if message cannot be temporarily sent, add to queue
-
 
171
 */
-
 
172
void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1,
-
 
173
              ipcarg_t arg2, void *private,
-
 
174
              ipc_async_callback_t callback)
-
 
175
{
-
 
176
    async_call_t *call;
-
 
177
    ipc_callid_t callid;
-
 
178
 
-
 
179
    call = ipc_prepare_async(private, callback);
-
 
180
    if (!call)
-
 
181
        return;
-
 
182
 
-
 
183
    /* We need to make sure that we get callid before
-
 
184
     * another thread accesses the queue again */
-
 
185
    futex_down(&ipc_futex);
-
 
186
    callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2);
-
 
187
 
-
 
188
    if (callid == IPC_CALLRET_TEMPORARY) {
-
 
189
        IPC_SET_METHOD(call->u.msg.data, method);
-
 
190
        IPC_SET_ARG1(call->u.msg.data, arg1);
-
 
191
        IPC_SET_ARG2(call->u.msg.data, arg2);
-
 
192
    }
-
 
193
    ipc_finish_async(callid, phoneid, call);
-
 
194
}
-
 
195
 
-
 
196
/** Send asynchronous message
-
 
197
 *
-
 
198
 * - if fatal error, call callback handler with proper error code
-
 
199
 * - if message cannot be temporarily sent, add to queue
-
 
200
 */
-
 
201
void ipc_call_async_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
-
 
202
              ipcarg_t arg2, ipcarg_t arg3, void *private,
-
 
203
              ipc_async_callback_t callback)
-
 
204
{
-
 
205
    async_call_t *call;
-
 
206
    ipc_callid_t callid;
-
 
207
 
-
 
208
    call = ipc_prepare_async(private, callback);
-
 
209
    if (!call)
-
 
210
        return;
-
 
211
 
-
 
212
    IPC_SET_METHOD(call->u.msg.data, method);
-
 
213
    IPC_SET_ARG1(call->u.msg.data, arg1);
-
 
214
    IPC_SET_ARG2(call->u.msg.data, arg2);
-
 
215
    IPC_SET_ARG3(call->u.msg.data, arg3);
-
 
216
    /* We need to make sure that we get callid before
-
 
217
     * another thread accesses the queue again */
-
 
218
    futex_down(&ipc_futex);
-
 
219
    callid = _ipc_call_async(phoneid, &call->u.msg.data);
-
 
220
 
-
 
221
    ipc_finish_async(callid, phoneid, call);
173
}
222
}
174
 
223
 
175
 
224
 
176
/** Send a fast answer to a received call.
225
/** Send a fast answer to a received call.
177
 *
226
 *
178
 * 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.
179
 * If you need to return more, use the ipc_answer() instead.
228
 * If you need to return more, use the ipc_answer() instead.
180
 *
229
 *
181
 * @param callid ID of the call being answered.
230
 * @param callid ID of the call being answered.
182
 * @param retval Return value.
231
 * @param retval Return value.
183
 * @param arg1 First return argument.
232
 * @param arg1 First return argument.
184
 * @param arg2 Second return argument.
233
 * @param arg2 Second return argument.
185
 *
234
 *
186
 * @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.
187
 */
236
 */
188
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,
189
        ipcarg_t arg2)
238
        ipcarg_t arg2)
190
{
239
{
191
    return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2);
240
    return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2);
192
}
241
}
193
 
242
 
194
/** Send a full answer to a received call.
243
/** Send a full answer to a received call.
195
 *
244
 *
196
 * @param callid ID of the call being answered.
245
 * @param callid ID of the call being answered.
197
 * @param call Call data. Must be already initialized by the responder.
246
 * @param call Call data. Must be already initialized by the responder.
198
 *
247
 *
199
 * @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.
200
 */
249
 */
201
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)
202
{
251
{
203
    return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call);
252
    return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call);
204
}
253
}
205
 
254
 
206
 
255
 
207
/** Try to dispatch queed calls from async queue */
256
/** Try to dispatch queed calls from async queue */
208
static void try_dispatch_queued_calls(void)
257
static void try_dispatch_queued_calls(void)
209
{
258
{
210
    async_call_t *call;
259
    async_call_t *call;
211
    ipc_callid_t callid;
260
    ipc_callid_t callid;
212
 
261
 
213
    /* TODO: integrate intelligently ipc_futex, so that it
262
    /* TODO: integrate intelligently ipc_futex, so that it
214
     * is locked during ipc_call_async, until it is added
263
     * is locked during ipc_call_async, until it is added
215
     * to dispatched_calls
264
     * to dispatched_calls
216
     */
265
     */
217
    futex_down(&async_futex);
266
    futex_down(&async_futex);
218
    while (!list_empty(&queued_calls)) {
267
    while (!list_empty(&queued_calls)) {
219
        call = list_get_instance(queued_calls.next, async_call_t,
268
        call = list_get_instance(queued_calls.next, async_call_t,
220
                     list);
269
                     list);
221
 
270
 
222
        callid = _ipc_call_async(call->u.msg.phoneid,
271
        callid = _ipc_call_async(call->u.msg.phoneid,
223
                     &call->u.msg.data);
272
                     &call->u.msg.data);
224
        if (callid == IPC_CALLRET_TEMPORARY) {
273
        if (callid == IPC_CALLRET_TEMPORARY) {
225
            break;
274
            break;
226
        }
275
        }
227
        list_remove(&call->list);
276
        list_remove(&call->list);
228
 
277
 
229
        futex_up(&async_futex);
278
        futex_up(&async_futex);
230
        psthread_add_ready(call->ptid);
279
        psthread_add_ready(call->ptid);
231
       
280
       
232
        if (callid == IPC_CALLRET_FATAL) {
281
        if (callid == IPC_CALLRET_FATAL) {
233
            if (call->callback)
282
            if (call->callback)
234
                call->callback(call->private, ENOENT, NULL);
283
                call->callback(call->private, ENOENT, NULL);
235
            free(call);
284
            free(call);
236
        } else {
285
        } else {
237
            call->u.callid = callid;
286
            call->u.callid = callid;
238
            futex_down(&ipc_futex);
287
            futex_down(&ipc_futex);
239
            list_append(&call->list, &dispatched_calls);
288
            list_append(&call->list, &dispatched_calls);
240
            futex_up(&ipc_futex);
289
            futex_up(&ipc_futex);
241
        }
290
        }
242
        futex_down(&async_futex);
291
        futex_down(&async_futex);
243
    }
292
    }
244
    futex_up(&async_futex);
293
    futex_up(&async_futex);
245
}
294
}
246
 
295
 
247
/** Handle received answer
296
/** Handle received answer
248
 *
297
 *
249
 * TODO: Make it use hash table
298
 * TODO: Make it use hash table
250
 *
299
 *
251
 * @param callid Callid (with first bit set) of the answered call
300
 * @param callid Callid (with first bit set) of the answered call
252
 */
301
 */
253
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)
254
{
303
{
255
    link_t *item;
304
    link_t *item;
256
    async_call_t *call;
305
    async_call_t *call;
257
 
306
 
258
    callid &= ~IPC_CALLID_ANSWERED;
307
    callid &= ~IPC_CALLID_ANSWERED;
259
   
308
   
260
    futex_down(&ipc_futex);
309
    futex_down(&ipc_futex);
261
    for (item = dispatched_calls.next; item != &dispatched_calls;
310
    for (item = dispatched_calls.next; item != &dispatched_calls;
262
         item = item->next) {
311
         item = item->next) {
263
        call = list_get_instance(item, async_call_t, list);
312
        call = list_get_instance(item, async_call_t, list);
264
        if (call->u.callid == callid) {
313
        if (call->u.callid == callid) {
265
            list_remove(&call->list);
314
            list_remove(&call->list);
266
            futex_up(&ipc_futex);
315
            futex_up(&ipc_futex);
267
            if (call->callback)
316
            if (call->callback)
268
                call->callback(call->private,
317
                call->callback(call->private,
269
                           IPC_GET_RETVAL(*data),
318
                           IPC_GET_RETVAL(*data),
270
                           data);
319
                           data);
271
            free(call);
320
            free(call);
272
            return;
321
            return;
273
        }
322
        }
274
    }
323
    }
275
    futex_up(&ipc_futex);
324
    futex_up(&ipc_futex);
276
    printf("Received unidentified answer: %P!!!\n", callid);
325
    printf("Received unidentified answer: %P!!!\n", callid);
277
}
326
}
278
 
327
 
279
 
328
 
280
/** One cycle of ipc wait for call call
329
/** One cycle of ipc wait for call call
281
 *
330
 *
282
 * - dispatch ASYNC reoutines in the background
331
 * - dispatch ASYNC reoutines in the background
283
 * @param call Space where the message is stored
332
 * @param call Space where the message is stored
284
 * @param usec Timeout in microseconds
333
 * @param usec Timeout in microseconds
285
 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking)
334
 * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking)
286
 * @return Callid of the answer.
335
 * @return Callid of the answer.
287
 */
336
 */
288
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)
289
{
338
{
290
    ipc_callid_t callid;
339
    ipc_callid_t callid;
291
 
340
 
292
    callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
341
    callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
293
    /* Handle received answers */
342
    /* Handle received answers */
294
    if (callid & IPC_CALLID_ANSWERED) {
343
    if (callid & IPC_CALLID_ANSWERED) {
295
        handle_answer(callid, call);
344
        handle_answer(callid, call);
296
        try_dispatch_queued_calls();
345
        try_dispatch_queued_calls();
297
    }
346
    }
298
 
347
 
299
    return callid;
348
    return callid;
300
}
349
}
301
 
350
 
302
/** Wait some time for an IPC call.
351
/** Wait some time for an IPC call.
303
 *
352
 *
304
 * - dispatch ASYNC reoutines in the background
353
 * - dispatch ASYNC reoutines in the background
305
 * @param call Space where the message is stored
354
 * @param call Space where the message is stored
306
 * @param usec Timeout in microseconds.
355
 * @param usec Timeout in microseconds.
307
 * @return Callid of the answer.
356
 * @return Callid of the answer.
308
 */
357
 */
309
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)
310
{
359
{
311
    ipc_callid_t callid;
360
    ipc_callid_t callid;
312
 
361
 
313
    do {
362
    do {
314
        callid = ipc_wait_cycle(call, usec, SYNCH_BLOCKING);
363
        callid = ipc_wait_cycle(call, usec, SYNCH_BLOCKING);
315
    } while (callid & IPC_CALLID_ANSWERED);
364
    } while (callid & IPC_CALLID_ANSWERED);
316
 
365
 
317
    return callid;
366
    return callid;
318
}
367
}
319
 
368
 
320
/** 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.
321
 *
370
 *
322
 * - dispatch ASYNC reoutines in the background
371
 * - dispatch ASYNC reoutines in the background
323
 * @param call Space where the message is stored
372
 * @param call Space where the message is stored
324
 * @return Callid of the answer.
373
 * @return Callid of the answer.
325
 */
374
 */
326
ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
375
ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
327
{
376
{
328
    ipc_callid_t callid;
377
    ipc_callid_t callid;
329
 
378
 
330
    do {
379
    do {
331
        callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT, SYNCH_NON_BLOCKING);
380
        callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT, SYNCH_NON_BLOCKING);
332
    } while (callid & IPC_CALLID_ANSWERED);
381
    } while (callid & IPC_CALLID_ANSWERED);
333
 
382
 
334
    return callid;
383
    return callid;
335
}
384
}
336
 
385
 
337
/** Ask destination to do a callback connection
386
/** Ask destination to do a callback connection
338
 *
387
 *
339
 * @return 0 - OK, error code
388
 * @return 0 - OK, error code
340
 */
389
 */
341
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)
342
{
391
{
343
    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,
344
                   arg2, 0, 0, 0, phone);
393
                   arg2, 0, 0, 0, phone);
345
}
394
}
346
 
395
 
347
/** Ask through phone for a new connection to some service
396
/** Ask through phone for a new connection to some service
348
 *
397
 *
349
 * @return new phoneid - OK, error code
398
 * @return new phoneid - OK, error code
350
 */
399
 */
351
int ipc_connect_me_to(int phoneid, int arg1, int arg2)
400
int ipc_connect_me_to(int phoneid, int arg1, int arg2)
352
{
401
{
353
    ipcarg_t newphid;
402
    ipcarg_t newphid;
354
    int res;
403
    int res;
355
 
404
 
356
    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,
357
                   arg2, 0, 0, 0, &newphid);
406
                   arg2, 0, 0, 0, &newphid);
358
    if (res)
407
    if (res)
359
        return res;
408
        return res;
360
    return newphid;
409
    return newphid;
361
}
410
}
362
 
411
 
363
/* Hang up specified phone */
412
/* Hang up specified phone */
364
int ipc_hangup(int phoneid)
413
int ipc_hangup(int phoneid)
365
{
414
{
366
    return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
415
    return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
367
}
416
}
368
 
417
 
369
int ipc_register_irq(int irq, irq_code_t *ucode)
418
int ipc_register_irq(int irq, irq_code_t *ucode)
370
{
419
{
371
    return __SYSCALL2(SYS_IPC_REGISTER_IRQ, irq, (sysarg_t) ucode);
420
    return __SYSCALL2(SYS_IPC_REGISTER_IRQ, irq, (sysarg_t) ucode);
372
}
421
}
373
 
422
 
374
int ipc_unregister_irq(int irq)
423
int ipc_unregister_irq(int irq)
375
{
424
{
376
    return __SYSCALL1(SYS_IPC_UNREGISTER_IRQ, irq);
425
    return __SYSCALL1(SYS_IPC_UNREGISTER_IRQ, irq);
377
}
426
}
378
 
427
 
379
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)
380
{
429
{
381
    return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1);
430
    return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1);
382
}
431
}
383
 
432
 
384
 
433
 
385
/** Open shared memory connection over specified phoneid
434
/** Open shared memory connection over specified phoneid
386
 *
435
 *
387
 *
436
 *
388
 * Allocate as_area, notify the other side about our intention
437
 * Allocate as_area, notify the other side about our intention
389
 * to open the connection
438
 * to open the connection
390
 *
439
 *
391
 * @return Connection id identifying this connection
440
 * @return Connection id identifying this connection
392
 */
441
 */
393
//int ipc_dgr_open(int pohoneid, size_t bufsize)
442
//int ipc_dgr_open(int pohoneid, size_t bufsize)
394
//{
443
//{
395
    /* Find new file descriptor in local descriptor table */
444
    /* Find new file descriptor in local descriptor table */
396
    /* Create AS_area, initialize structures */
445
    /* Create AS_area, initialize structures */
397
    /* Send AS to other side, handle error states */
446
    /* Send AS to other side, handle error states */
398
 
447
 
399
//}
448
//}
400
/*
449
/*
401
void ipc_dgr_close(int cid)
450
void ipc_dgr_close(int cid)
402
{
451
{
403
}
452
}
404
 
453
 
405
void * ipc_dgr_alloc(int cid, size_t size)
454
void * ipc_dgr_alloc(int cid, size_t size)
406
{
455
{
407
}
456
}
408
 
457
 
409
void ipc_dgr_free(int cid, void *area)
458
void ipc_dgr_free(int cid, void *area)
410
{
459
{
411
 
460
 
412
}
461
}
413
 
462
 
414
int ipc_dgr_send(int cid, void *area)
463
int ipc_dgr_send(int cid, void *area)
415
{
464
{
416
}
465
}
417
 
466
 
418
 
467
 
419
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)
420
{
469
{
421
}
470
}
422
 
471
 
423
*/
472
*/
424
 
473