Subversion Repositories HelenOS

Rev

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

Rev 2619 Rev 2620
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 <fibril.h>
52
#include <fibril.h>
53
#include <assert.h>
53
#include <assert.h>
54
 
54
 
55
/**
55
/**
56
 * Structures of this type are used for keeping track of sent asynchronous calls
56
 * Structures of this type are used for keeping track of sent asynchronous calls
57
 * and queing unsent calls.
57
 * and queing unsent calls.
58
 */
58
 */
59
typedef struct {
59
typedef struct {
60
    link_t list;
60
    link_t list;
61
 
61
 
62
    ipc_async_callback_t callback;
62
    ipc_async_callback_t callback;
63
    void *private;
63
    void *private;
64
    union {
64
    union {
65
        ipc_callid_t callid;
65
        ipc_callid_t callid;
66
        struct {
66
        struct {
67
            ipc_call_t data;
67
            ipc_call_t data;
68
            int phoneid;
68
            int phoneid;
69
        } msg;
69
        } msg;
70
    } u;
70
    } u;
71
    fid_t fid;  /**< Fibril waiting for sending this call. */
71
    fid_t fid;  /**< Fibril waiting for sending this call. */
72
} async_call_t;
72
} async_call_t;
73
 
73
 
74
LIST_INITIALIZE(dispatched_calls);
74
LIST_INITIALIZE(dispatched_calls);
75
 
75
 
76
/** List of asynchronous calls that were not accepted by kernel.
76
/** List of asynchronous calls that were not accepted by kernel.
77
 *
77
 *
78
 * It is protected by async_futex, because if the call cannot be sent into the
78
 * It is protected by async_futex, because if the call cannot be sent into the
79
 * kernel, the async framework is used automatically.
79
 * kernel, the async framework is used automatically.
80
 */
80
 */
81
LIST_INITIALIZE(queued_calls);
81
LIST_INITIALIZE(queued_calls);
82
 
82
 
83
static atomic_t ipc_futex = FUTEX_INITIALIZER;
83
static atomic_t ipc_futex = FUTEX_INITIALIZER;
84
 
84
 
85
/** Make a fast synchronous call.
85
/** Make a fast synchronous call.
86
 *
86
 *
87
 * Only three payload arguments can be passed using this function. However, this
87
 * Only three payload arguments can be passed using this function. However, this
88
 * function is faster than the generic ipc_call_sync_slow() because the payload
88
 * function is faster than the generic ipc_call_sync_slow() because the payload
89
 * is passed directly in registers.
89
 * is passed directly in registers.
90
 *
90
 *
91
 * @param phoneid   Phone handle for the call.
91
 * @param phoneid   Phone handle for the call.
92
 * @param method    Requested method.
92
 * @param method    Requested method.
93
 * @param arg1      Service-defined payload argument.
93
 * @param arg1      Service-defined payload argument.
94
 * @param arg2      Service-defined payload argument.
94
 * @param arg2      Service-defined payload argument.
95
 * @param arg3      Service-defined payload argument.
95
 * @param arg3      Service-defined payload argument.
96
 * @param result1   If non-NULL, the return ARG1 will be stored there.
96
 * @param result1   If non-NULL, the return ARG1 will be stored there.
97
 * @param result2   If non-NULL, the return ARG2 will be stored there.
97
 * @param result2   If non-NULL, the return ARG2 will be stored there.
98
 * @param result3   If non-NULL, the return ARG3 will be stored there.
98
 * @param result3   If non-NULL, the return ARG3 will be stored there.
99
 * @param result4   If non-NULL, the return ARG4 will be stored there.
99
 * @param result4   If non-NULL, the return ARG4 will be stored there.
100
 * @param result5   If non-NULL, the return ARG5 will be stored there.
100
 * @param result5   If non-NULL, the return ARG5 will be stored there.
101
 *
101
 *
102
 * @return      Negative values represent errors returned by IPC.
102
 * @return      Negative values represent errors returned by IPC.
103
 *          Otherwise the RETVAL of the answer is returned.
103
 *          Otherwise the RETVAL of the answer is returned.
104
 */
104
 */
105
int
105
int
106
ipc_call_sync_fast(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
106
ipc_call_sync_fast(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
107
    ipcarg_t arg3, ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3,
107
    ipcarg_t arg3, ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3,
108
    ipcarg_t *result4, ipcarg_t *result5)
108
    ipcarg_t *result4, ipcarg_t *result5)
109
{
109
{
110
    ipc_call_t resdata;
110
    ipc_call_t resdata;
111
    int callres;
111
    int callres;
112
   
112
   
113
    callres = __SYSCALL6(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
113
    callres = __SYSCALL6(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1,
114
        arg2, arg3, (sysarg_t) &resdata);
114
        arg2, arg3, (sysarg_t) &resdata);
115
    if (callres)
115
    if (callres)
116
        return callres;
116
        return callres;
117
    if (result1)
117
    if (result1)
118
        *result1 = IPC_GET_ARG1(resdata);
118
        *result1 = IPC_GET_ARG1(resdata);
119
    if (result2)
119
    if (result2)
120
        *result2 = IPC_GET_ARG2(resdata);
120
        *result2 = IPC_GET_ARG2(resdata);
121
    if (result3)
121
    if (result3)
122
        *result3 = IPC_GET_ARG3(resdata);
122
        *result3 = IPC_GET_ARG3(resdata);
123
    if (result4)
123
    if (result4)
124
        *result4 = IPC_GET_ARG4(resdata);
124
        *result4 = IPC_GET_ARG4(resdata);
125
    if (result5)
125
    if (result5)
126
        *result5 = IPC_GET_ARG5(resdata);
126
        *result5 = IPC_GET_ARG5(resdata);
127
 
127
 
128
    return IPC_GET_RETVAL(resdata);
128
    return IPC_GET_RETVAL(resdata);
129
}
129
}
130
 
130
 
131
/** Make a synchronous call transmitting 5 arguments of payload.
131
/** Make a synchronous call transmitting 5 arguments of payload.
132
 *
132
 *
133
 * @param phoneid   Phone handle for the call.
133
 * @param phoneid   Phone handle for the call.
134
 * @param method    Requested method.
134
 * @param method    Requested method.
135
 * @param arg1      Service-defined payload argument.
135
 * @param arg1      Service-defined payload argument.
136
 * @param arg2      Service-defined payload argument.
136
 * @param arg2      Service-defined payload argument.
137
 * @param arg3      Service-defined payload argument.
137
 * @param arg3      Service-defined payload argument.
138
 * @param arg4      Service-defined payload argument.
138
 * @param arg4      Service-defined payload argument.
139
 * @param arg5      Service-defined payload argument.
139
 * @param arg5      Service-defined payload argument.
140
 * @param result1   If non-NULL, storage for the first return argument.
140
 * @param result1   If non-NULL, storage for the first return argument.
141
 * @param result2   If non-NULL, storage for the second return argument.
141
 * @param result2   If non-NULL, storage for the second return argument.
142
 * @param result3   If non-NULL, storage for the third return argument.
142
 * @param result3   If non-NULL, storage for the third return argument.
143
 * @param result4   If non-NULL, storage for the fourth return argument.
143
 * @param result4   If non-NULL, storage for the fourth return argument.
144
 * @param result5   If non-NULL, storage for the fifth return argument.
144
 * @param result5   If non-NULL, storage for the fifth return argument.
145
 *
145
 *
146
 * @return      Negative value means IPC error.
146
 * @return      Negative value means IPC error.
147
 *          Otherwise the RETVAL of the answer.
147
 *          Otherwise the RETVAL of the answer.
148
 */
148
 */
149
int
149
int
150
ipc_call_sync_slow(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
150
ipc_call_sync_slow(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
151
    ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *result1,
151
    ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *result1,
152
    ipcarg_t *result2, ipcarg_t *result3, ipcarg_t *result4, ipcarg_t *result5)
152
    ipcarg_t *result2, ipcarg_t *result3, ipcarg_t *result4, ipcarg_t *result5)
153
{
153
{
154
    ipc_call_t data;
154
    ipc_call_t data;
155
    int callres;
155
    int callres;
156
 
156
 
157
    IPC_SET_METHOD(data, method);
157
    IPC_SET_METHOD(data, method);
158
    IPC_SET_ARG1(data, arg1);
158
    IPC_SET_ARG1(data, arg1);
159
    IPC_SET_ARG2(data, arg2);
159
    IPC_SET_ARG2(data, arg2);
160
    IPC_SET_ARG3(data, arg3);
160
    IPC_SET_ARG3(data, arg3);
161
    IPC_SET_ARG4(data, arg4);
161
    IPC_SET_ARG4(data, arg4);
162
    IPC_SET_ARG5(data, arg5);
162
    IPC_SET_ARG5(data, arg5);
163
 
163
 
164
    callres = __SYSCALL3(SYS_IPC_CALL_SYNC_SLOW, phoneid, (sysarg_t) &data,
164
    callres = __SYSCALL3(SYS_IPC_CALL_SYNC_SLOW, phoneid, (sysarg_t) &data,
165
        (sysarg_t) &data);
165
        (sysarg_t) &data);
166
    if (callres)
166
    if (callres)
167
        return callres;
167
        return callres;
168
 
168
 
169
    if (result1)
169
    if (result1)
170
        *result1 = IPC_GET_ARG1(data);
170
        *result1 = IPC_GET_ARG1(data);
171
    if (result2)
171
    if (result2)
172
        *result2 = IPC_GET_ARG2(data);
172
        *result2 = IPC_GET_ARG2(data);
173
    if (result3)
173
    if (result3)
174
        *result3 = IPC_GET_ARG3(data);
174
        *result3 = IPC_GET_ARG3(data);
175
    if (result4)
175
    if (result4)
176
        *result4 = IPC_GET_ARG4(data);
176
        *result4 = IPC_GET_ARG4(data);
177
    if (result5)
177
    if (result5)
178
        *result5 = IPC_GET_ARG5(data);
178
        *result5 = IPC_GET_ARG5(data);
179
 
179
 
180
    return IPC_GET_RETVAL(data);
180
    return IPC_GET_RETVAL(data);
181
}
181
}
182
 
182
 
183
/** Syscall to send asynchronous message.
183
/** Syscall to send asynchronous message.
184
 *
184
 *
185
 * @param phoneid   Phone handle for the call.
185
 * @param phoneid   Phone handle for the call.
186
 * @param data      Call data with the request.
186
 * @param data      Call data with the request.
187
 *
187
 *
188
 * @return      Hash of the call or an error code.
188
 * @return      Hash of the call or an error code.
189
 */
189
 */
190
static ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data)
190
static ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data)
191
{
191
{
192
    return __SYSCALL2(SYS_IPC_CALL_ASYNC_SLOW, phoneid, (sysarg_t) data);
192
    return __SYSCALL2(SYS_IPC_CALL_ASYNC_SLOW, phoneid, (sysarg_t) data);
193
}
193
}
194
 
194
 
195
/** Prolog to ipc_call_async_*() functions.
195
/** Prolog to ipc_call_async_*() functions.
196
 *
196
 *
197
 * @param private   Argument for the answer/error callback.
197
 * @param private   Argument for the answer/error callback.
198
 * @param callback  Answer/error callback.
198
 * @param callback  Answer/error callback.
199
 *
199
 *
200
 * @return      New, partially initialized async_call structure or NULL.
200
 * @return      New, partially initialized async_call structure or NULL.
201
 */
201
 */
202
static inline async_call_t *ipc_prepare_async(void *private,
202
static inline async_call_t *ipc_prepare_async(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
 
206
 
207
    call = malloc(sizeof(*call));
207
    call = malloc(sizeof(*call));
208
    if (!call) {
208
    if (!call) {
209
        if (callback)
209
        if (callback)
210
            callback(private, ENOMEM, NULL);
210
            callback(private, ENOMEM, NULL);
211
        return NULL;
211
        return NULL;
212
    }
212
    }
213
    call->callback = callback;
213
    call->callback = callback;
214
    call->private = private;
214
    call->private = private;
215
 
215
 
216
    return call;
216
    return call;
217
}
217
}
218
 
218
 
219
/** Epilogue of ipc_call_async_*() functions.
219
/** Epilogue of ipc_call_async_*() functions.
220
 *
220
 *
221
 * @param callid    Value returned by the SYS_IPC_CALL_ASYNC_* syscall.
221
 * @param callid    Value returned by the SYS_IPC_CALL_ASYNC_* syscall.
222
 * @param phoneid   Phone handle through which the call was made.
222
 * @param phoneid   Phone handle through which the call was made.
223
 * @param call      async_call structure returned by ipc_prepare_async().
223
 * @param call      async_call structure returned by ipc_prepare_async().
224
 * @param can_preempt   If non-zero, the current fibril can be preempted in this
224
 * @param can_preempt   If non-zero, the current fibril can be preempted in this
225
 *          call.
225
 *          call.
226
 */
226
 */
227
static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
227
static inline void ipc_finish_async(ipc_callid_t callid, int phoneid,
228
    async_call_t *call, int can_preempt)
228
    async_call_t *call, int can_preempt)
229
{
229
{
230
    if (!call) { /* Nothing to do regardless if failed or not */
230
    if (!call) { /* Nothing to do regardless if failed or not */
231
        futex_up(&ipc_futex);
231
        futex_up(&ipc_futex);
232
        return;
232
        return;
233
    }
233
    }
234
 
234
 
235
    if (callid == IPC_CALLRET_FATAL) {
235
    if (callid == IPC_CALLRET_FATAL) {
236
        futex_up(&ipc_futex);
236
        futex_up(&ipc_futex);
237
        /* Call asynchronous handler with error code */
237
        /* Call asynchronous handler with error code */
238
        if (call->callback)
238
        if (call->callback)
239
            call->callback(call->private, ENOENT, NULL);
239
            call->callback(call->private, ENOENT, NULL);
240
        free(call);
240
        free(call);
241
        return;
241
        return;
242
    }
242
    }
243
 
243
 
244
    if (callid == IPC_CALLRET_TEMPORARY) {
244
    if (callid == IPC_CALLRET_TEMPORARY) {
245
        futex_up(&ipc_futex);
245
        futex_up(&ipc_futex);
246
 
246
 
247
        call->u.msg.phoneid = phoneid;
247
        call->u.msg.phoneid = phoneid;
248
       
248
       
249
        futex_down(&async_futex);
249
        futex_down(&async_futex);
250
        list_append(&call->list, &queued_calls);
250
        list_append(&call->list, &queued_calls);
251
 
251
 
252
        if (can_preempt) {
252
        if (can_preempt) {
253
            call->fid = fibril_get_id();
253
            call->fid = fibril_get_id();
254
            fibril_switch(FIBRIL_TO_MANAGER);
254
            fibril_switch(FIBRIL_TO_MANAGER);
255
            /* Async futex unlocked by previous call */
255
            /* Async futex unlocked by previous call */
256
        } else {
256
        } else {
257
            call->fid = 0;
257
            call->fid = 0;
258
            futex_up(&async_futex);
258
            futex_up(&async_futex);
259
        }
259
        }
260
        return;
260
        return;
261
    }
261
    }
262
    call->u.callid = callid;
262
    call->u.callid = callid;
263
    /* Add call to the list of dispatched calls */
263
    /* Add call to the list of dispatched calls */
264
    list_append(&call->list, &dispatched_calls);
264
    list_append(&call->list, &dispatched_calls);
265
    futex_up(&ipc_futex);
265
    futex_up(&ipc_futex);
266
   
266
   
267
}
267
}
268
 
268
 
269
/** Make a fast asynchronous call.
269
/** Make a fast asynchronous call.
270
 *
270
 *
271
 * This function can only handle four arguments of payload. It is, however,
271
 * This function can only handle four arguments of payload. It is, however,
272
 * faster than the more generic ipc_call_async_slow().
272
 * faster than the more generic ipc_call_async_slow().
273
 *
273
 *
274
 * Note that this function is a void function.
274
 * Note that this function is a void function.
275
 * During normal opertation, answering this call will trigger the callback.
275
 * During normal opertation, answering this call will trigger the callback.
276
 * In case of fatal error, call the callback handler with the proper error code.
276
 * In case of fatal error, call the callback handler with the proper error code.
277
 * If the call cannot be temporarily made, queue it.
277
 * If the call cannot be temporarily made, queue it.
278
 *
278
 *
279
 * @param phoneid   Phone handle for the call.
279
 * @param phoneid   Phone handle for the call.
280
 * @param method    Requested method.
280
 * @param method    Requested method.
281
 * @param arg1      Service-defined payload argument.
281
 * @param arg1      Service-defined payload argument.
282
 * @param arg2      Service-defined payload argument.
282
 * @param arg2      Service-defined payload argument.
283
 * @param arg3      Service-defined payload argument.
283
 * @param arg3      Service-defined payload argument.
284
 * @param arg4      Service-defined payload argument.
284
 * @param arg4      Service-defined payload argument.
285
 * @param private   Argument to be passed to the answer/error callback.
285
 * @param private   Argument to be passed to the answer/error callback.
286
 * @param callback  Answer or error callback.
286
 * @param callback  Answer or error callback.
287
 * @param can_preempt   If non-zero, the current fibril will be preempted in
287
 * @param can_preempt   If non-zero, the current fibril will be preempted in
288
 *          case the kernel temporarily refuses to accept more
288
 *          case the kernel temporarily refuses to accept more
289
 *          asynchronous calls.
289
 *          asynchronous calls.
290
 */
290
 */
291
void ipc_call_async_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
291
void ipc_call_async_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
292
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, void *private,
292
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, void *private,
293
    ipc_async_callback_t callback, int can_preempt)
293
    ipc_async_callback_t callback, int can_preempt)
294
{
294
{
295
    async_call_t *call = NULL;
295
    async_call_t *call = NULL;
296
    ipc_callid_t callid;
296
    ipc_callid_t callid;
297
 
297
 
298
    if (callback) {
298
    if (callback) {
299
        call = ipc_prepare_async(private, callback);
299
        call = ipc_prepare_async(private, callback);
300
        if (!call)
300
        if (!call)
301
            return;
301
            return;
302
    }
302
    }
303
 
303
 
304
    /*
304
    /*
305
     * We need to make sure that we get callid before another thread
305
     * We need to make sure that we get callid before another thread
306
     * accesses the queue again.
306
     * accesses the queue again.
307
     */
307
     */
308
    futex_down(&ipc_futex);
308
    futex_down(&ipc_futex);
309
    callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1,
309
    callid = __SYSCALL6(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1,
310
        arg2, arg3, arg4);
310
        arg2, arg3, arg4);
311
 
311
 
312
    if (callid == IPC_CALLRET_TEMPORARY) {
312
    if (callid == IPC_CALLRET_TEMPORARY) {
313
        if (!call) {
313
        if (!call) {
314
            call = ipc_prepare_async(private, callback);
314
            call = ipc_prepare_async(private, callback);
315
            if (!call)
315
            if (!call)
316
                return;
316
                return;
317
        }
317
        }
318
        IPC_SET_METHOD(call->u.msg.data, method);
318
        IPC_SET_METHOD(call->u.msg.data, method);
319
        IPC_SET_ARG1(call->u.msg.data, arg1);
319
        IPC_SET_ARG1(call->u.msg.data, arg1);
320
        IPC_SET_ARG2(call->u.msg.data, arg2);
320
        IPC_SET_ARG2(call->u.msg.data, arg2);
321
        IPC_SET_ARG3(call->u.msg.data, arg3);
321
        IPC_SET_ARG3(call->u.msg.data, arg3);
322
        IPC_SET_ARG4(call->u.msg.data, arg4);
322
        IPC_SET_ARG4(call->u.msg.data, arg4);
-
 
323
        /*
-
 
324
         * To achieve deterministic behavior, we always zero out the
-
 
325
         * arguments that are beyond the limits of the fast version.
-
 
326
         */
-
 
327
        IPC_SET_ARG5(call->u.msg.data, 0);
323
    }
328
    }
324
    ipc_finish_async(callid, phoneid, call, can_preempt);
329
    ipc_finish_async(callid, phoneid, call, can_preempt);
325
}
330
}
326
 
331
 
327
/** Make an asynchronous call transmitting the entire payload.
332
/** Make an asynchronous call transmitting the entire payload.
328
 *
333
 *
329
 * Note that this function is a void function.
334
 * Note that this function is a void function.
330
 * During normal opertation, answering this call will trigger the callback.
335
 * During normal opertation, answering this call will trigger the callback.
331
 * In case of fatal error, call the callback handler with the proper error code.
336
 * In case of fatal error, call the callback handler with the proper error code.
332
 * If the call cannot be temporarily made, queue it.
337
 * If the call cannot be temporarily made, queue it.
333
 *
338
 *
334
 * @param phoneid   Phone handle for the call.
339
 * @param phoneid   Phone handle for the call.
335
 * @param method    Requested method.
340
 * @param method    Requested method.
336
 * @param arg1      Service-defined payload argument.
341
 * @param arg1      Service-defined payload argument.
337
 * @param arg2      Service-defined payload argument.
342
 * @param arg2      Service-defined payload argument.
338
 * @param arg3      Service-defined payload argument.
343
 * @param arg3      Service-defined payload argument.
339
 * @param arg4      Service-defined payload argument.
344
 * @param arg4      Service-defined payload argument.
340
 * @param arg5      Service-defined payload argument.
345
 * @param arg5      Service-defined payload argument.
341
 * @param private   Argument to be passed to the answer/error callback.
346
 * @param private   Argument to be passed to the answer/error callback.
342
 * @param callback  Answer or error callback.
347
 * @param callback  Answer or error callback.
343
 * @param can_preempt   If non-zero, the current fibril will be preempted in
348
 * @param can_preempt   If non-zero, the current fibril will be preempted in
344
 *          case the kernel temporarily refuses to accept more
349
 *          case the kernel temporarily refuses to accept more
345
 *          asynchronous calls.
350
 *          asynchronous calls.
346
 *
351
 *
347
 */
352
 */
348
void ipc_call_async_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
353
void ipc_call_async_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
349
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, void *private,
354
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, void *private,
350
    ipc_async_callback_t callback, int can_preempt)
355
    ipc_async_callback_t callback, int can_preempt)
351
{
356
{
352
    async_call_t *call;
357
    async_call_t *call;
353
    ipc_callid_t callid;
358
    ipc_callid_t callid;
354
 
359
 
355
    call = ipc_prepare_async(private, callback);
360
    call = ipc_prepare_async(private, callback);
356
    if (!call)
361
    if (!call)
357
        return;
362
        return;
358
 
363
 
359
    IPC_SET_METHOD(call->u.msg.data, method);
364
    IPC_SET_METHOD(call->u.msg.data, method);
360
    IPC_SET_ARG1(call->u.msg.data, arg1);
365
    IPC_SET_ARG1(call->u.msg.data, arg1);
361
    IPC_SET_ARG2(call->u.msg.data, arg2);
366
    IPC_SET_ARG2(call->u.msg.data, arg2);
362
    IPC_SET_ARG3(call->u.msg.data, arg3);
367
    IPC_SET_ARG3(call->u.msg.data, arg3);
363
    IPC_SET_ARG4(call->u.msg.data, arg4);
368
    IPC_SET_ARG4(call->u.msg.data, arg4);
364
    IPC_SET_ARG5(call->u.msg.data, arg5);
369
    IPC_SET_ARG5(call->u.msg.data, arg5);
365
    /*
370
    /*
366
     * We need to make sure that we get callid before another thread
371
     * We need to make sure that we get callid before another thread
367
     * accesses the queue again.
372
     * accesses the queue again.
368
     */
373
     */
369
    futex_down(&ipc_futex);
374
    futex_down(&ipc_futex);
370
    callid = _ipc_call_async(phoneid, &call->u.msg.data);
375
    callid = _ipc_call_async(phoneid, &call->u.msg.data);
371
 
376
 
372
    ipc_finish_async(callid, phoneid, call, can_preempt);
377
    ipc_finish_async(callid, phoneid, call, can_preempt);
373
}
378
}
374
 
379
 
375
 
380
 
376
/** Answer a received call - fast version.
381
/** Answer a received call - fast version.
377
 *
382
 *
378
 * The fast answer makes use of passing retval and first four arguments in
383
 * The fast answer makes use of passing retval and first four arguments in
379
 * registers. If you need to return more, use the ipc_answer_slow() instead.
384
 * registers. If you need to return more, use the ipc_answer_slow() instead.
380
 *
385
 *
381
 * @param callid    Hash of the call being answered.
386
 * @param callid    Hash of the call being answered.
382
 * @param retval    Return value.
387
 * @param retval    Return value.
383
 * @param arg1      First return argument.
388
 * @param arg1      First return argument.
384
 * @param arg2      Second return argument.
389
 * @param arg2      Second return argument.
385
 * @param arg3      Third return argument.
390
 * @param arg3      Third return argument.
386
 * @param arg4      Fourth return argument.
391
 * @param arg4      Fourth return argument.
387
 *
392
 *
388
 * @return      Zero on success or a value from @ref errno.h on failure.
393
 * @return      Zero on success or a value from @ref errno.h on failure.
389
 */
394
 */
390
ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
395
ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
391
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4)
396
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4)
392
{
397
{
393
    return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
398
    return __SYSCALL6(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2, arg3,
394
        arg4);
399
        arg4);
395
}
400
}
396
 
401
 
397
/** Answer a received call - slow full version.
402
/** Answer a received call - slow full version.
398
 *
403
 *
399
 * @param callid    Hash of the call being answered.
404
 * @param callid    Hash of the call being answered.
400
 * @param retval    Return value.
405
 * @param retval    Return value.
401
 * @param arg1      First return argument.
406
 * @param arg1      First return argument.
402
 * @param arg2      Second return argument.
407
 * @param arg2      Second return argument.
403
 * @param arg3      Third return argument.
408
 * @param arg3      Third return argument.
404
 * @param arg4      Fourth return argument.
409
 * @param arg4      Fourth return argument.
405
 * @param arg5      Fifth return argument.
410
 * @param arg5      Fifth return argument.
406
 *
411
 *
407
 * @return      Zero on success or a value from @ref errno.h on failure.
412
 * @return      Zero on success or a value from @ref errno.h on failure.
408
 */
413
 */
409
ipcarg_t ipc_answer_slow(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
414
ipcarg_t ipc_answer_slow(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1,
410
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5)
415
    ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5)
411
{
416
{
412
    ipc_call_t data;
417
    ipc_call_t data;
413
 
418
 
414
    IPC_SET_RETVAL(data, retval);
419
    IPC_SET_RETVAL(data, retval);
415
    IPC_SET_ARG1(data, arg1);
420
    IPC_SET_ARG1(data, arg1);
416
    IPC_SET_ARG2(data, arg2);
421
    IPC_SET_ARG2(data, arg2);
417
    IPC_SET_ARG3(data, arg3);
422
    IPC_SET_ARG3(data, arg3);
418
    IPC_SET_ARG4(data, arg4);
423
    IPC_SET_ARG4(data, arg4);
419
    IPC_SET_ARG5(data, arg5);
424
    IPC_SET_ARG5(data, arg5);
420
 
425
 
421
    return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
426
    return __SYSCALL2(SYS_IPC_ANSWER_SLOW, callid, (sysarg_t) &data);
422
}
427
}
423
 
428
 
424
 
429
 
425
/** Try to dispatch queued calls from the async queue. */
430
/** Try to dispatch queued calls from the async queue. */
426
static void try_dispatch_queued_calls(void)
431
static void try_dispatch_queued_calls(void)
427
{
432
{
428
    async_call_t *call;
433
    async_call_t *call;
429
    ipc_callid_t callid;
434
    ipc_callid_t callid;
430
 
435
 
431
    /** @todo
436
    /** @todo
432
     * Integrate intelligently ipc_futex, so that it is locked during
437
     * Integrate intelligently ipc_futex, so that it is locked during
433
     * ipc_call_async_*(), until it is added to dispatched_calls.
438
     * ipc_call_async_*(), until it is added to dispatched_calls.
434
     */
439
     */
435
    futex_down(&async_futex);
440
    futex_down(&async_futex);
436
    while (!list_empty(&queued_calls)) {
441
    while (!list_empty(&queued_calls)) {
437
        call = list_get_instance(queued_calls.next, async_call_t, list);
442
        call = list_get_instance(queued_calls.next, async_call_t, list);
438
        callid = _ipc_call_async(call->u.msg.phoneid, &call->u.msg.data);
443
        callid = _ipc_call_async(call->u.msg.phoneid,
-
 
444
            &call->u.msg.data);
439
        if (callid == IPC_CALLRET_TEMPORARY) {
445
        if (callid == IPC_CALLRET_TEMPORARY) {
440
            break;
446
            break;
441
        }
447
        }
442
        list_remove(&call->list);
448
        list_remove(&call->list);
443
 
449
 
444
        futex_up(&async_futex);
450
        futex_up(&async_futex);
445
        if (call->fid)
451
        if (call->fid)
446
            fibril_add_ready(call->fid);
452
            fibril_add_ready(call->fid);
447
       
453
       
448
        if (callid == IPC_CALLRET_FATAL) {
454
        if (callid == IPC_CALLRET_FATAL) {
449
            if (call->callback)
455
            if (call->callback)
450
                call->callback(call->private, ENOENT, NULL);
456
                call->callback(call->private, ENOENT, NULL);
451
            free(call);
457
            free(call);
452
        } else {
458
        } else {
453
            call->u.callid = callid;
459
            call->u.callid = callid;
454
            futex_down(&ipc_futex);
460
            futex_down(&ipc_futex);
455
            list_append(&call->list, &dispatched_calls);
461
            list_append(&call->list, &dispatched_calls);
456
            futex_up(&ipc_futex);
462
            futex_up(&ipc_futex);
457
        }
463
        }
458
        futex_down(&async_futex);
464
        futex_down(&async_futex);
459
    }
465
    }
460
    futex_up(&async_futex);
466
    futex_up(&async_futex);
461
}
467
}
462
 
468
 
463
/** Handle a received answer.
469
/** Handle a received answer.
464
 *
470
 *
465
 * Find the hash of the answer and call the answer callback.
471
 * Find the hash of the answer and call the answer callback.
466
 *
472
 *
467
 * @todo Make it use hash table.
473
 * @todo Make it use hash table.
468
 *
474
 *
469
 * @param callid    Hash of the received answer.
475
 * @param callid    Hash of the received answer.
470
 *          The answer has the same hash as the request OR'ed with
476
 *          The answer has the same hash as the request OR'ed with
471
 *          the IPC_CALLID_ANSWERED bit.
477
 *          the IPC_CALLID_ANSWERED bit.
472
 * @param data      Call data of the answer.
478
 * @param data      Call data of the answer.
473
 */
479
 */
474
static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
480
static void handle_answer(ipc_callid_t callid, ipc_call_t *data)
475
{
481
{
476
    link_t *item;
482
    link_t *item;
477
    async_call_t *call;
483
    async_call_t *call;
478
 
484
 
479
    callid &= ~IPC_CALLID_ANSWERED;
485
    callid &= ~IPC_CALLID_ANSWERED;
480
   
486
   
481
    futex_down(&ipc_futex);
487
    futex_down(&ipc_futex);
482
    for (item = dispatched_calls.next; item != &dispatched_calls;
488
    for (item = dispatched_calls.next; item != &dispatched_calls;
483
        item = item->next) {
489
        item = item->next) {
484
        call = list_get_instance(item, async_call_t, list);
490
        call = list_get_instance(item, async_call_t, list);
485
        if (call->u.callid == callid) {
491
        if (call->u.callid == callid) {
486
            list_remove(&call->list);
492
            list_remove(&call->list);
487
            futex_up(&ipc_futex);
493
            futex_up(&ipc_futex);
488
            if (call->callback)
494
            if (call->callback)
489
                call->callback(call->private,
495
                call->callback(call->private,
490
                    IPC_GET_RETVAL(*data), data);
496
                    IPC_GET_RETVAL(*data), data);
491
            free(call);
497
            free(call);
492
            return;
498
            return;
493
        }
499
        }
494
    }
500
    }
495
    futex_up(&ipc_futex);
501
    futex_up(&ipc_futex);
496
}
502
}
497
 
503
 
498
 
504
 
499
/** Wait for a first call to come.
505
/** Wait for a first call to come.
500
 *
506
 *
501
 * @param call      Storage where the incoming call data will be stored.
507
 * @param call      Storage where the incoming call data will be stored.
502
 * @param usec      Timeout in microseconds
508
 * @param usec      Timeout in microseconds
503
 * @param flags     Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
509
 * @param flags     Flags passed to SYS_IPC_WAIT (blocking, nonblocking).
504
 *
510
 *
505
 * @return      Hash of the call. Note that certain bits have special
511
 * @return      Hash of the call. Note that certain bits have special
506
 *          meaning. IPC_CALLID_ANSWERED will be set in an answer
512
 *          meaning. IPC_CALLID_ANSWERED will be set in an answer
507
 *          and IPC_CALLID_NOTIFICATION is used for notifications.
513
 *          and IPC_CALLID_NOTIFICATION is used for notifications.
508
 *         
514
 *         
509
 */
515
 */
510
ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags)
516
ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags)
511
{
517
{
512
    ipc_callid_t callid;
518
    ipc_callid_t callid;
513
 
519
 
514
    callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
520
    callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags);
515
    /* Handle received answers */
521
    /* Handle received answers */
516
    if (callid & IPC_CALLID_ANSWERED) {
522
    if (callid & IPC_CALLID_ANSWERED) {
517
        handle_answer(callid, call);
523
        handle_answer(callid, call);
518
        try_dispatch_queued_calls();
524
        try_dispatch_queued_calls();
519
    }
525
    }
520
 
526
 
521
    return callid;
527
    return callid;
522
}
528
}
523
 
529
 
524
/** Wait some time for an IPC call.
530
/** Wait some time for an IPC call.
525
 *
531
 *
526
 * The call will return after an answer is received.
532
 * The call will return after an answer is received.
527
 *
533
 *
528
 * @param call      Storage where the incoming call data will be stored.
534
 * @param call      Storage where the incoming call data will be stored.
529
 * @param usec      Timeout in microseconds.
535
 * @param usec      Timeout in microseconds.
530
 *
536
 *
531
 * @return      Hash of the answer.
537
 * @return      Hash of the answer.
532
 */
538
 */
533
ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec)
539
ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec)
534
{
540
{
535
    ipc_callid_t callid;
541
    ipc_callid_t callid;
536
 
542
 
537
    do {
543
    do {
538
        callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
544
        callid = ipc_wait_cycle(call, usec, SYNCH_FLAGS_NONE);
539
    } while (callid & IPC_CALLID_ANSWERED);
545
    } while (callid & IPC_CALLID_ANSWERED);
540
 
546
 
541
    return callid;
547
    return callid;
542
}
548
}
543
 
549
 
544
/** Check if there is an IPC call waiting to be picked up.
550
/** Check if there is an IPC call waiting to be picked up.
545
 *
551
 *
546
 * @param call      Storage where the incoming call will be stored.
552
 * @param call      Storage where the incoming call will be stored.
547
 * @return      Hash of the answer.
553
 * @return      Hash of the answer.
548
 */
554
 */
549
ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
555
ipc_callid_t ipc_trywait_for_call(ipc_call_t *call)
550
{
556
{
551
    ipc_callid_t callid;
557
    ipc_callid_t callid;
552
 
558
 
553
    do {
559
    do {
554
        callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
560
        callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT,
555
            SYNCH_FLAGS_NON_BLOCKING);
561
            SYNCH_FLAGS_NON_BLOCKING);
556
    } while (callid & IPC_CALLID_ANSWERED);
562
    } while (callid & IPC_CALLID_ANSWERED);
557
 
563
 
558
    return callid;
564
    return callid;
559
}
565
}
560
 
566
 
561
/** Ask destination to do a callback connection.
567
/** Ask destination to do a callback connection.
562
 *
568
 *
563
 * @param phoneid   Phone handle used for contacting the other side.
569
 * @param phoneid   Phone handle used for contacting the other side.
564
 * @param arg1      Service-defined argument.
570
 * @param arg1      Service-defined argument.
565
 * @param arg2      Service-defined argument.
571
 * @param arg2      Service-defined argument.
566
 * @param phonehash Storage where the library will store an opaque
572
 * @param phonehash Storage where the library will store an opaque
567
 *          identifier of the phone that will be used for incoming
573
 *          identifier of the phone that will be used for incoming
568
 *          calls. This identifier can be used for connection
574
 *          calls. This identifier can be used for connection
569
 *          tracking.
575
 *          tracking.
570
 *
576
 *
571
 * @return      Zero on success or a negative error code.
577
 * @return      Zero on success or a negative error code.
572
 */
578
 */
573
int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phonehash)
579
int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phonehash)
574
{
580
{
575
    return ipc_call_sync_2_3(phoneid, IPC_M_CONNECT_TO_ME, arg1, arg2,
581
    return ipc_call_sync_2_3(phoneid, IPC_M_CONNECT_TO_ME, arg1, arg2,
576
        NULL, NULL, phonehash);
582
        NULL, NULL, phonehash);
577
}
583
}
578
 
584
 
579
/** Ask through phone for a new connection to some service.
585
/** Ask through phone for a new connection to some service.
580
 *
586
 *
581
 * @param phoneid   Phone handle used for contacting the other side.
587
 * @param phoneid   Phone handle used for contacting the other side.
582
 * @param arg1      User defined argument.
588
 * @param arg1      User defined argument.
583
 * @param arg2      User defined argument.
589
 * @param arg2      User defined argument.
584
 *
590
 *
585
 * @return      New phone handle on success or a negative error code.
591
 * @return      New phone handle on success or a negative error code.
586
 */
592
 */
587
int ipc_connect_me_to(int phoneid, int arg1, int arg2)
593
int ipc_connect_me_to(int phoneid, int arg1, int arg2)
588
{
594
{
589
    ipcarg_t newphid;
595
    ipcarg_t newphid;
590
    int res;
596
    int res;
591
 
597
 
592
    res = ipc_call_sync_2_3(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, NULL,
598
    res = ipc_call_sync_2_3(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, NULL,
593
        NULL, &newphid);
599
        NULL, &newphid);
594
    if (res)
600
    if (res)
595
        return res;
601
        return res;
596
    return newphid;
602
    return newphid;
597
}
603
}
598
 
604
 
599
/** Hang up a phone.
605
/** Hang up a phone.
600
 *
606
 *
601
 * @param phoneid   Handle of the phone to be hung up.
607
 * @param phoneid   Handle of the phone to be hung up.
602
 *
608
 *
603
 * @return      Zero on success or a negative error code.
609
 * @return      Zero on success or a negative error code.
604
 */
610
 */
605
int ipc_hangup(int phoneid)
611
int ipc_hangup(int phoneid)
606
{
612
{
607
    return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
613
    return __SYSCALL1(SYS_IPC_HANGUP, phoneid);
608
}
614
}
609
 
615
 
610
/** Register IRQ notification.
616
/** Register IRQ notification.
611
 *
617
 *
612
 * @param inr       IRQ number.
618
 * @param inr       IRQ number.
613
 * @param devno     Device number of the device generating inr.
619
 * @param devno     Device number of the device generating inr.
614
 * @param method    Use this method for notifying me.
620
 * @param method    Use this method for notifying me.
615
 * @param ucode     Top-half pseudocode handler.
621
 * @param ucode     Top-half pseudocode handler.
616
 *
622
 *
617
 * @return      Value returned by the kernel.
623
 * @return      Value returned by the kernel.
618
 */
624
 */
619
int ipc_register_irq(int inr, int devno, int method, irq_code_t *ucode)
625
int ipc_register_irq(int inr, int devno, int method, irq_code_t *ucode)
620
{
626
{
621
    return __SYSCALL4(SYS_IPC_REGISTER_IRQ, inr, devno, method,
627
    return __SYSCALL4(SYS_IPC_REGISTER_IRQ, inr, devno, method,
622
        (sysarg_t) ucode);
628
        (sysarg_t) ucode);
623
}
629
}
624
 
630
 
625
/** Unregister IRQ notification.
631
/** Unregister IRQ notification.
626
 *
632
 *
627
 * @param inr       IRQ number.
633
 * @param inr       IRQ number.
628
 * @param devno     Device number of the device generating inr.
634
 * @param devno     Device number of the device generating inr.
629
 *
635
 *
630
 * @return      Value returned by the kernel.
636
 * @return      Value returned by the kernel.
631
 */
637
 */
632
int ipc_unregister_irq(int inr, int devno)
638
int ipc_unregister_irq(int inr, int devno)
633
{
639
{
634
    return __SYSCALL2(SYS_IPC_UNREGISTER_IRQ, inr, devno);
640
    return __SYSCALL2(SYS_IPC_UNREGISTER_IRQ, inr, devno);
635
}
641
}
636
 
642
 
637
/** Forward a received call to another destination.
643
/** Forward a received call to another destination.
638
 *
644
 *
639
 * @param callid    Hash of the call to forward.
645
 * @param callid    Hash of the call to forward.
640
 * @param phoneid   Phone handle to use for forwarding.
646
 * @param phoneid   Phone handle to use for forwarding.
641
 * @param method    New method for the forwarded call.
647
 * @param method    New method for the forwarded call.
642
 * @param arg1      New value of the first argument for the forwarded call.
648
 * @param arg1      New value of the first argument for the forwarded call.
643
 *
649
 *
644
 * @return      Zero on success or an error code.
650
 * @return      Zero on success or an error code.
645
 *
651
 *
646
 * For non-system methods, the old method and arg1 are rewritten by the new
652
 * For non-system methods, the old method and arg1 are rewritten by the new
647
 * values. For system methods, the new method and arg1 are written to the old
653
 * values. For system methods, the new method and arg1 are written to the old
648
 * arg1 and arg2, respectivelly.
654
 * arg1 and arg2, respectivelly. Calls with immutable methods are forwarded
-
 
655
 * verbatim.
649
 */
656
 */
650
int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1)
657
int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method,
-
 
658
    ipcarg_t arg1)
651
{
659
{
652
    return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1);
660
    return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1);
653
}
661
}
654
 
662
 
655
/** Wrapper for making IPC_M_DATA_SEND calls.
663
/** Wrapper for making IPC_M_DATA_SEND calls.
656
 *
664
 *
657
 * @param phoneid   Phone that will be used to contact the receiving side.
665
 * @param phoneid   Phone that will be used to contact the receiving side.
658
 * @param src       Address of the beginning of the source buffer.
666
 * @param src       Address of the beginning of the source buffer.
659
 * @param size      Size of the source buffer.
667
 * @param size      Size of the source buffer.
660
 *
668
 *
661
 * @return      Zero on success or a negative error code from errno.h.
669
 * @return      Zero on success or a negative error code from errno.h.
662
 */
670
 */
663
int ipc_data_send(int phoneid, void *src, size_t size)
671
int ipc_data_send(int phoneid, void *src, size_t size)
664
{
672
{
665
    return ipc_call_sync_3_0(phoneid, IPC_M_DATA_SEND, 0, (ipcarg_t) src,
673
    return ipc_call_sync_3_0(phoneid, IPC_M_DATA_SEND, 0, (ipcarg_t) src,
666
        (ipcarg_t) size);
674
        (ipcarg_t) size);
667
}
675
}
668
 
676
 
669
/** Wrapper for receiving the IPC_M_DATA_SEND calls.
677
/** Wrapper for receiving the IPC_M_DATA_SEND calls.
670
 *
678
 *
671
 * This wrapper only makes it more comfortable to receive IPC_M_DATA_SEND calls
679
 * This wrapper only makes it more comfortable to receive IPC_M_DATA_SEND calls
672
 * so that the user doesn't have to remember the meaning of each IPC argument.
680
 * so that the user doesn't have to remember the meaning of each IPC argument.
673
 *
681
 *
674
 * So far, this wrapper is to be used from within a connection fibril.
682
 * So far, this wrapper is to be used from within a connection fibril.
675
 *
683
 *
676
 * @param callid    Storage where the hash of the IPC_M_DATA_SEND call will
684
 * @param callid    Storage where the hash of the IPC_M_DATA_SEND call will
677
 *          be stored.
685
 *          be stored.
678
 * @param dst       Storage where the suggested destination address will
686
 * @param dst       Storage where the suggested destination address will
679
 *          be stored. May be NULL.
687
 *          be stored. May be NULL.
680
 * @param size      Storage where the suggested size will be stored. May be
688
 * @param size      Storage where the suggested size will be stored. May be
681
 *          NULL
689
 *          NULL
682
 *
690
 *
683
 * @return      Non-zero on success, zero on failure.
691
 * @return      Non-zero on success, zero on failure.
684
 */
692
 */
685
int ipc_data_receive(ipc_callid_t *callid, void **dst, size_t *size)
693
int ipc_data_receive(ipc_callid_t *callid, void **dst, size_t *size)
686
{
694
{
687
    ipc_call_t data;
695
    ipc_call_t data;
688
   
696
   
689
    assert(callid);
697
    assert(callid);
690
 
698
 
691
    *callid = async_get_call(&data);
699
    *callid = async_get_call(&data);
692
    if (IPC_GET_METHOD(data) != IPC_M_DATA_SEND)
700
    if (IPC_GET_METHOD(data) != IPC_M_DATA_SEND)
693
        return 0;
701
        return 0;
694
    if (dst)
702
    if (dst)
695
        *dst = (void *) IPC_GET_ARG1(data);
703
        *dst = (void *) IPC_GET_ARG1(data);
696
    if (size)
704
    if (size)
697
        *size = (size_t) IPC_GET_ARG3(data);
705
        *size = (size_t) IPC_GET_ARG3(data);
698
    return 1;
706
    return 1;
699
}
707
}
700
 
708
 
701
/** Wrapper for answering the IPC_M_DATA_SEND calls.
709
/** Wrapper for answering the IPC_M_DATA_SEND calls.
702
 *
710
 *
703
 * This wrapper only makes it more comfortable to answer IPC_M_DATA_SEND calls
711
 * This wrapper only makes it more comfortable to answer IPC_M_DATA_SEND calls
704
 * so that the user doesn't have to remember the meaning of each IPC argument.
712
 * so that the user doesn't have to remember the meaning of each IPC argument.
705
 *
713
 *
706
 * @param callid    Hash of the IPC_M_DATA_SEND call to answer.
714
 * @param callid    Hash of the IPC_M_DATA_SEND call to answer.
707
 * @param dst       Final destination address for the IPC_M_DATA_SEND call.
715
 * @param dst       Final destination address for the IPC_M_DATA_SEND call.
708
 * @param size      Final size for the IPC_M_DATA_SEND call.
716
 * @param size      Final size for the IPC_M_DATA_SEND call.
709
 *
717
 *
710
 * @return      Zero on success or a value from @ref errno.h on failure.
718
 * @return      Zero on success or a value from @ref errno.h on failure.
711
 */
719
 */
712
ipcarg_t ipc_data_deliver(ipc_callid_t callid, void *dst, size_t size)
720
ipcarg_t ipc_data_deliver(ipc_callid_t callid, void *dst, size_t size)
713
{
721
{
714
    return ipc_answer_3(callid, EOK, (ipcarg_t) dst, 0, (ipcarg_t) size);
722
    return ipc_answer_3(callid, EOK, (ipcarg_t) dst, 0, (ipcarg_t) size);
715
}
723
}
716
 
724
 
717
/** @}
725
/** @}
718
 */
726
 */
719
 
727