Subversion Repositories HelenOS

Rev

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

Rev 2556 Rev 2557
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 genericipc
29
/** @addtogroup genericipc
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <arch.h>
35
#include <arch.h>
36
#include <proc/task.h>
36
#include <proc/task.h>
37
#include <proc/thread.h>
37
#include <proc/thread.h>
38
#include <errno.h>
38
#include <errno.h>
39
#include <memstr.h>
39
#include <memstr.h>
40
#include <debug.h>
40
#include <debug.h>
41
#include <ipc/ipc.h>
41
#include <ipc/ipc.h>
42
#include <ipc/sysipc.h>
42
#include <ipc/sysipc.h>
43
#include <ipc/irq.h>
43
#include <ipc/irq.h>
44
#include <ipc/ipcrsc.h>
44
#include <ipc/ipcrsc.h>
45
#include <arch/interrupt.h>
45
#include <arch/interrupt.h>
46
#include <print.h>
46
#include <print.h>
47
#include <syscall/copy.h>
47
#include <syscall/copy.h>
48
#include <security/cap.h>
48
#include <security/cap.h>
49
#include <mm/as.h>
49
#include <mm/as.h>
50
#include <print.h>
50
#include <print.h>
51
 
51
 
52
/** Maximum buffer size allowed for IPC_M_DATA_SEND requests. */
52
/** Maximum buffer size allowed for IPC_M_DATA_SEND requests. */
53
#define DATA_SEND_LIMIT     (64 * 1024)
53
#define DATA_SEND_LIMIT     (64 * 1024)
54
 
54
 
55
#define GET_CHECK_PHONE(phone, phoneid, err) \
55
#define GET_CHECK_PHONE(phone, phoneid, err) \
56
{ \
56
{ \
57
    if (phoneid > IPC_MAX_PHONES) { \
57
    if (phoneid > IPC_MAX_PHONES) { \
58
        err; \
58
        err; \
59
    } \
59
    } \
60
    phone = &TASK->phones[phoneid]; \
60
    phone = &TASK->phones[phoneid]; \
61
}
61
}
62
 
62
 
63
#define STRUCT_TO_USPACE(dst, src)  copy_to_uspace(dst, src, sizeof(*(src)))
63
#define STRUCT_TO_USPACE(dst, src)  copy_to_uspace(dst, src, sizeof(*(src)))
64
 
64
 
65
/** Decide if the method is a system method.
65
/** Decide if the method is a system method.
66
 *
66
 *
67
 * @param method    Method to be decided.
67
 * @param method    Method to be decided.
68
 *
68
 *
69
 * @return      Return 1 if the method is a system method.
69
 * @return      Return 1 if the method is a system method.
70
 *          Otherwise return 0.
70
 *          Otherwise return 0.
71
 */
71
 */
72
static inline int is_system_method(unative_t method)
72
static inline int method_is_system(unative_t method)
73
{
73
{
74
    if (method <= IPC_M_LAST_SYSTEM)
74
    if (method <= IPC_M_LAST_SYSTEM)
75
        return 1;
75
        return 1;
76
    return 0;
76
    return 0;
77
}
77
}
78
 
78
 
79
/** Decide if the message with this method is forwardable.
79
/** Decide if the message with this method is forwardable.
80
 *
80
 *
81
 * - some system messages may be forwarded, for some of them
81
 * - some system messages may be forwarded, for some of them
82
 *   it is useless
82
 *   it is useless
83
 *
83
 *
84
 * @param method    Method to be decided.
84
 * @param method    Method to be decided.
85
 *
85
 *
86
 * @return      Return 1 if the method is forwardable.
86
 * @return      Return 1 if the method is forwardable.
87
 *          Otherwise return 0.
87
 *          Otherwise return 0.
88
 */
88
 */
89
static inline int is_forwardable(unative_t method)
89
static inline int method_is_forwardable(unative_t method)
90
{
90
{
91
    switch (method) {
91
    switch (method) {
92
    case IPC_M_PHONE_HUNGUP:
92
    case IPC_M_PHONE_HUNGUP:
93
    case IPC_M_AS_AREA_SEND:
-
 
94
    case IPC_M_AS_AREA_RECV:
-
 
95
    case IPC_M_DATA_SEND:
-
 
96
        /* This message is meant only for the original recipient. */
93
        /* This message is meant only for the original recipient. */
97
        return 0;
94
        return 0;
98
    default:
95
    default:
99
        return 1;
96
        return 1;
100
    }
97
    }
101
}
98
}
102
 
99
 
-
 
100
/** Decide if the message with this method is immutable on forward.
-
 
101
 *
-
 
102
 * - some system messages may be forwarded, for some of them
-
 
103
 *   it is useless
-
 
104
 *
-
 
105
 * @param method    Method to be decided.
-
 
106
 *
-
 
107
 * @return      Return 1 if the method is immutable on forward.
-
 
108
 *          Otherwise return 0.
-
 
109
 */
-
 
110
static inline int method_is_immutable(unative_t method)
-
 
111
{
-
 
112
    switch (method) {
-
 
113
    case IPC_M_AS_AREA_SEND:
-
 
114
    case IPC_M_AS_AREA_RECV:
-
 
115
    case IPC_M_DATA_SEND:
-
 
116
        return 1;
-
 
117
        break;
-
 
118
    default:
-
 
119
        return 0;
-
 
120
    }
-
 
121
}
-
 
122
 
103
 
123
 
104
/***********************************************************************
124
/***********************************************************************
105
 * Functions that preprocess answer before sending it to the recepient.
125
 * Functions that preprocess answer before sending it to the recepient.
106
 ***********************************************************************/
126
 ***********************************************************************/
107
 
127
 
108
/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
128
/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
109
 * for answer_preprocess().
129
 * for answer_preprocess().
110
 *
130
 *
111
 * @param call      Call structure to be decided.
131
 * @param call      Call structure to be decided.
112
 *
132
 *
113
 * @return      Return 1 if the old call contents should be saved.
133
 * @return      Return 1 if the old call contents should be saved.
114
 *          Return 0 otherwise.
134
 *          Return 0 otherwise.
115
 */
135
 */
116
static inline int answer_need_old(call_t *call)
136
static inline int answer_need_old(call_t *call)
117
{
137
{
118
    switch (IPC_GET_METHOD(call->data)) {
138
    switch (IPC_GET_METHOD(call->data)) {
119
    case IPC_M_CONNECT_TO_ME:
139
    case IPC_M_CONNECT_TO_ME:
120
    case IPC_M_CONNECT_ME_TO:
140
    case IPC_M_CONNECT_ME_TO:
121
    case IPC_M_AS_AREA_SEND:
141
    case IPC_M_AS_AREA_SEND:
122
    case IPC_M_AS_AREA_RECV:
142
    case IPC_M_AS_AREA_RECV:
123
    case IPC_M_DATA_SEND:
143
    case IPC_M_DATA_SEND:
124
        return 1;
144
        return 1;
125
    default:
145
    default:
126
        return 0;
146
        return 0;
127
    }
147
    }
128
}
148
}
129
 
149
 
130
/** Interpret process answer as control information.
150
/** Interpret process answer as control information.
131
 *
151
 *
132
 * This function is called directly after sys_ipc_answer().
152
 * This function is called directly after sys_ipc_answer().
133
 *
153
 *
134
 * @param answer    Call structure with the answer.
154
 * @param answer    Call structure with the answer.
135
 * @param olddata   Saved data of the request.
155
 * @param olddata   Saved data of the request.
136
 *
156
 *
137
 * @return      Return 0 on success or an error code.
157
 * @return      Return 0 on success or an error code.
138
 */
158
 */
139
static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
159
static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
140
{
160
{
141
    int phoneid;
161
    int phoneid;
142
 
162
 
143
    if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
163
    if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
144
        /* In case of forward, hangup the forwared phone,
164
        /* In case of forward, hangup the forwared phone,
145
         * not the originator
165
         * not the originator
146
         */
166
         */
147
        spinlock_lock(&answer->data.phone->lock);
167
        spinlock_lock(&answer->data.phone->lock);
148
        spinlock_lock(&TASK->answerbox.lock);
168
        spinlock_lock(&TASK->answerbox.lock);
149
        if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
169
        if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
150
            list_remove(&answer->data.phone->link);
170
            list_remove(&answer->data.phone->link);
151
            answer->data.phone->state = IPC_PHONE_SLAMMED;
171
            answer->data.phone->state = IPC_PHONE_SLAMMED;
152
        }
172
        }
153
        spinlock_unlock(&TASK->answerbox.lock);
173
        spinlock_unlock(&TASK->answerbox.lock);
154
        spinlock_unlock(&answer->data.phone->lock);
174
        spinlock_unlock(&answer->data.phone->lock);
155
    }
175
    }
156
 
176
 
157
    if (!olddata)
177
    if (!olddata)
158
        return 0;
178
        return 0;
159
 
179
 
160
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
180
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
161
        phoneid = IPC_GET_ARG3(*olddata);
181
        phoneid = IPC_GET_ARG3(*olddata);
162
        if (IPC_GET_RETVAL(answer->data)) {
182
        if (IPC_GET_RETVAL(answer->data)) {
163
            /* The connection was not accepted */
183
            /* The connection was not accepted */
164
            phone_dealloc(phoneid);
184
            phone_dealloc(phoneid);
165
        } else {
185
        } else {
166
            /* The connection was accepted */
186
            /* The connection was accepted */
167
            phone_connect(phoneid, &answer->sender->answerbox);
187
            phone_connect(phoneid, &answer->sender->answerbox);
168
            /* Set 'phone hash' as arg3 of response */
188
            /* Set 'phone hash' as arg3 of response */
169
            IPC_SET_ARG3(answer->data,
189
            IPC_SET_ARG3(answer->data,
170
                (unative_t) &TASK->phones[phoneid]);
190
                (unative_t) &TASK->phones[phoneid]);
171
        }
191
        }
172
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
192
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
173
        /* If the users accepted call, connect */
193
        /* If the users accepted call, connect */
174
        if (!IPC_GET_RETVAL(answer->data)) {
194
        if (!IPC_GET_RETVAL(answer->data)) {
175
            ipc_phone_connect((phone_t *) IPC_GET_ARG3(*olddata),
195
            ipc_phone_connect((phone_t *) IPC_GET_ARG3(*olddata),
176
                &TASK->answerbox);
196
                &TASK->answerbox);
177
        }
197
        }
178
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) {
198
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) {
179
        if (!IPC_GET_RETVAL(answer->data)) {
199
        if (!IPC_GET_RETVAL(answer->data)) {
180
            /* Accepted, handle as_area receipt */
200
            /* Accepted, handle as_area receipt */
181
            ipl_t ipl;
201
            ipl_t ipl;
182
            int rc;
202
            int rc;
183
            as_t *as;
203
            as_t *as;
184
           
204
           
185
            ipl = interrupts_disable();
205
            ipl = interrupts_disable();
186
            spinlock_lock(&answer->sender->lock);
206
            spinlock_lock(&answer->sender->lock);
187
            as = answer->sender->as;
207
            as = answer->sender->as;
188
            spinlock_unlock(&answer->sender->lock);
208
            spinlock_unlock(&answer->sender->lock);
189
            interrupts_restore(ipl);
209
            interrupts_restore(ipl);
190
           
210
           
191
            rc = as_area_share(as, IPC_GET_ARG1(*olddata),
211
            rc = as_area_share(as, IPC_GET_ARG1(*olddata),
192
                IPC_GET_ARG2(*olddata), AS,
212
                IPC_GET_ARG2(*olddata), AS,
193
                IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
213
                IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
194
            IPC_SET_RETVAL(answer->data, rc);
214
            IPC_SET_RETVAL(answer->data, rc);
195
            return rc;
215
            return rc;
196
        }
216
        }
197
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) {
217
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) {
198
        if (!IPC_GET_RETVAL(answer->data)) {
218
        if (!IPC_GET_RETVAL(answer->data)) {
199
            ipl_t ipl;
219
            ipl_t ipl;
200
            as_t *as;
220
            as_t *as;
201
            int rc;
221
            int rc;
202
           
222
           
203
            ipl = interrupts_disable();
223
            ipl = interrupts_disable();
204
            spinlock_lock(&answer->sender->lock);
224
            spinlock_lock(&answer->sender->lock);
205
            as = answer->sender->as;
225
            as = answer->sender->as;
206
            spinlock_unlock(&answer->sender->lock);
226
            spinlock_unlock(&answer->sender->lock);
207
            interrupts_restore(ipl);
227
            interrupts_restore(ipl);
208
           
228
           
209
            rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
229
            rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
210
                IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
230
                IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
211
                IPC_GET_ARG2(answer->data));
231
                IPC_GET_ARG2(answer->data));
212
            IPC_SET_RETVAL(answer->data, rc);
232
            IPC_SET_RETVAL(answer->data, rc);
213
        }
233
        }
214
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_SEND) {
234
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_SEND) {
215
        if (!IPC_GET_RETVAL(answer->data)) {
235
        if (!IPC_GET_RETVAL(answer->data)) {
216
            int rc;
236
            int rc;
217
            uintptr_t dst;
237
            uintptr_t dst;
218
            uintptr_t size;
238
            uintptr_t size;
219
 
239
 
220
            ASSERT(answer->buffer);
240
            ASSERT(answer->buffer);
221
 
241
 
222
            dst = IPC_GET_ARG1(answer->data);
242
            dst = IPC_GET_ARG1(answer->data);
223
            size = IPC_GET_ARG3(answer->data);
243
            size = IPC_GET_ARG3(answer->data);
224
 
244
 
225
            rc = copy_to_uspace((void *) dst, answer->buffer, size);
245
            rc = copy_to_uspace((void *) dst, answer->buffer, size);
226
            if (rc != 0)
246
            if (rc != 0)
227
                IPC_SET_RETVAL(answer->data, rc);
247
                IPC_SET_RETVAL(answer->data, rc);
228
            free(answer->buffer);
248
            free(answer->buffer);
229
            answer->buffer = NULL;
249
            answer->buffer = NULL;
230
        }
250
        }
231
    }
251
    }
232
    return 0;
252
    return 0;
233
}
253
}
234
 
254
 
235
/** Called before the request is sent.
255
/** Called before the request is sent.
236
 *
256
 *
237
 * @param call      Call structure with the request.
257
 * @param call      Call structure with the request.
238
 *
258
 *
239
 * @return      Return 0 on success, ELIMIT or EPERM on error.
259
 * @return      Return 0 on success, ELIMIT or EPERM on error.
240
 */
260
 */
241
static int request_preprocess(call_t *call)
261
static int request_preprocess(call_t *call)
242
{
262
{
243
    int newphid;
263
    int newphid;
244
    size_t size;
264
    size_t size;
245
    uintptr_t src;
265
    uintptr_t src;
246
    int rc;
266
    int rc;
247
 
267
 
248
    switch (IPC_GET_METHOD(call->data)) {
268
    switch (IPC_GET_METHOD(call->data)) {
249
    case IPC_M_CONNECT_ME_TO:
269
    case IPC_M_CONNECT_ME_TO:
250
        newphid = phone_alloc();
270
        newphid = phone_alloc();
251
        if (newphid < 0)
271
        if (newphid < 0)
252
            return ELIMIT;
272
            return ELIMIT;
253
        /* Set arg3 for server */
273
        /* Set arg3 for server */
254
        IPC_SET_ARG3(call->data, (unative_t) &TASK->phones[newphid]);
274
        IPC_SET_ARG3(call->data, (unative_t) &TASK->phones[newphid]);
255
        call->flags |= IPC_CALL_CONN_ME_TO;
275
        call->flags |= IPC_CALL_CONN_ME_TO;
256
        call->priv = newphid;
276
        call->priv = newphid;
257
        break;
277
        break;
258
    case IPC_M_AS_AREA_SEND:
278
    case IPC_M_AS_AREA_SEND:
259
        size = as_area_get_size(IPC_GET_ARG1(call->data));
279
        size = as_area_get_size(IPC_GET_ARG1(call->data));
260
        if (!size)
280
        if (!size)
261
            return EPERM;
281
            return EPERM;
262
        IPC_SET_ARG2(call->data, size);
282
        IPC_SET_ARG2(call->data, size);
263
        break;
283
        break;
264
    case IPC_M_DATA_SEND:
284
    case IPC_M_DATA_SEND:
265
        src = IPC_GET_ARG2(call->data);
285
        src = IPC_GET_ARG2(call->data);
266
        size = IPC_GET_ARG3(call->data);
286
        size = IPC_GET_ARG3(call->data);
267
       
287
       
268
        if ((size <= 0) || (size > DATA_SEND_LIMIT))
288
        if ((size <= 0) || (size > DATA_SEND_LIMIT))
269
            return ELIMIT;
289
            return ELIMIT;
270
       
290
       
271
        call->buffer = (uint8_t *) malloc(size, 0);
291
        call->buffer = (uint8_t *) malloc(size, 0);
272
        rc = copy_from_uspace(call->buffer, (void *) src, size);
292
        rc = copy_from_uspace(call->buffer, (void *) src, size);
273
        if (rc != 0) {
293
        if (rc != 0) {
274
            free(call->buffer);
294
            free(call->buffer);
275
            return rc;
295
            return rc;
276
        }
296
        }
277
        break;
297
        break;
278
    default:
298
    default:
279
        break;
299
        break;
280
    }
300
    }
281
    return 0;
301
    return 0;
282
}
302
}
283
 
303
 
284
/*******************************************************************************
304
/*******************************************************************************
285
 * Functions called to process received call/answer before passing it to uspace.
305
 * Functions called to process received call/answer before passing it to uspace.
286
 *******************************************************************************/
306
 *******************************************************************************/
287
 
307
 
288
/** Do basic kernel processing of received call answer.
308
/** Do basic kernel processing of received call answer.
289
 *
309
 *
290
 * @param call      Call structure with the answer.
310
 * @param call      Call structure with the answer.
291
 */
311
 */
292
static void process_answer(call_t *call)
312
static void process_answer(call_t *call)
293
{
313
{
294
    if (IPC_GET_RETVAL(call->data) == EHANGUP &&
314
    if (IPC_GET_RETVAL(call->data) == EHANGUP &&
295
        (call->flags & IPC_CALL_FORWARDED))
315
        (call->flags & IPC_CALL_FORWARDED))
296
        IPC_SET_RETVAL(call->data, EFORWARD);
316
        IPC_SET_RETVAL(call->data, EFORWARD);
297
 
317
 
298
    if (call->flags & IPC_CALL_CONN_ME_TO) {
318
    if (call->flags & IPC_CALL_CONN_ME_TO) {
299
        if (IPC_GET_RETVAL(call->data))
319
        if (IPC_GET_RETVAL(call->data))
300
            phone_dealloc(call->priv);
320
            phone_dealloc(call->priv);
301
        else
321
        else
302
            IPC_SET_ARG3(call->data, call->priv);
322
            IPC_SET_ARG3(call->data, call->priv);
303
    }
323
    }
304
}
324
}
305
 
325
 
306
/** Do basic kernel processing of received call request.
326
/** Do basic kernel processing of received call request.
307
 *
327
 *
308
 * @param box       Destination answerbox structure.
328
 * @param box       Destination answerbox structure.
309
 * @param call      Call structure with the request.
329
 * @param call      Call structure with the request.
310
 *
330
 *
311
 * @return      Return 0 if the call should be passed to userspace.
331
 * @return      Return 0 if the call should be passed to userspace.
312
 *          Return -1 if the call should be ignored.
332
 *          Return -1 if the call should be ignored.
313
 */
333
 */
314
static int process_request(answerbox_t *box, call_t *call)
334
static int process_request(answerbox_t *box, call_t *call)
315
{
335
{
316
    int phoneid;
336
    int phoneid;
317
 
337
 
318
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
338
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
319
        phoneid = phone_alloc();
339
        phoneid = phone_alloc();
320
        if (phoneid < 0) { /* Failed to allocate phone */
340
        if (phoneid < 0) { /* Failed to allocate phone */
321
            IPC_SET_RETVAL(call->data, ELIMIT);
341
            IPC_SET_RETVAL(call->data, ELIMIT);
322
            ipc_answer(box, call);
342
            ipc_answer(box, call);
323
            return -1;
343
            return -1;
324
        }
344
        }
325
        IPC_SET_ARG3(call->data, phoneid);
345
        IPC_SET_ARG3(call->data, phoneid);
326
    }
346
    }
327
    return 0;
347
    return 0;
328
}
348
}
329
 
349
 
330
/** Make a fast call over IPC, wait for reply and return to user.
350
/** Make a fast call over IPC, wait for reply and return to user.
331
 *
351
 *
332
 * This function can handle only one argument of payload, but is faster than
352
 * This function can handle only one argument of payload, but is faster than
333
 * the generic function (i.e. sys_ipc_call_sync()).
353
 * the generic function (i.e. sys_ipc_call_sync()).
334
 *
354
 *
335
 * @param phoneid   Phone handle for the call.
355
 * @param phoneid   Phone handle for the call.
336
 * @param method    Method of the call.
356
 * @param method    Method of the call.
337
 * @param arg1      Service-defined payload argument.
357
 * @param arg1      Service-defined payload argument.
338
 * @param data      Address of userspace structure where the reply call will
358
 * @param data      Address of userspace structure where the reply call will
339
 *          be stored.
359
 *          be stored.
340
 *
360
 *
341
 * @return      Returns 0 on success.
361
 * @return      Returns 0 on success.
342
 *          Return ENOENT if there is no such phone handle.
362
 *          Return ENOENT if there is no such phone handle.
343
 */
363
 */
344
unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
364
unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
345
    unative_t arg1, ipc_data_t *data)
365
    unative_t arg1, ipc_data_t *data)
346
{
366
{
347
    call_t call;
367
    call_t call;
348
    phone_t *phone;
368
    phone_t *phone;
349
    int res;
369
    int res;
350
 
370
 
351
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
371
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
352
 
372
 
353
    ipc_call_static_init(&call);
373
    ipc_call_static_init(&call);
354
    IPC_SET_METHOD(call.data, method);
374
    IPC_SET_METHOD(call.data, method);
355
    IPC_SET_ARG1(call.data, arg1);
375
    IPC_SET_ARG1(call.data, arg1);
356
 
376
 
357
    if (!(res = request_preprocess(&call))) {
377
    if (!(res = request_preprocess(&call))) {
358
        ipc_call_sync(phone, &call);
378
        ipc_call_sync(phone, &call);
359
        process_answer(&call);
379
        process_answer(&call);
360
    } else {
380
    } else {
361
        IPC_SET_RETVAL(call.data, res);
381
        IPC_SET_RETVAL(call.data, res);
362
    }
382
    }
363
    STRUCT_TO_USPACE(&data->args, &call.data.args);
383
    STRUCT_TO_USPACE(&data->args, &call.data.args);
364
 
384
 
365
    return 0;
385
    return 0;
366
}
386
}
367
 
387
 
368
/** Make a synchronous IPC call allowing to transmit the entire payload.
388
/** Make a synchronous IPC call allowing to transmit the entire payload.
369
 *
389
 *
370
 * @param phoneid   Phone handle for the call.
390
 * @param phoneid   Phone handle for the call.
371
 * @param question  Userspace address of call data with the request.
391
 * @param question  Userspace address of call data with the request.
372
 * @param reply     Userspace address of call data where to store the
392
 * @param reply     Userspace address of call data where to store the
373
 *          answer.
393
 *          answer.
374
 *
394
 *
375
 * @return      Zero on success or an error code.
395
 * @return      Zero on success or an error code.
376
 */
396
 */
377
unative_t sys_ipc_call_sync(unative_t phoneid, ipc_data_t *question,
397
unative_t sys_ipc_call_sync(unative_t phoneid, ipc_data_t *question,
378
    ipc_data_t *reply)
398
    ipc_data_t *reply)
379
{
399
{
380
    call_t call;
400
    call_t call;
381
    phone_t *phone;
401
    phone_t *phone;
382
    int res;
402
    int res;
383
    int rc;
403
    int rc;
384
 
404
 
385
    ipc_call_static_init(&call);
405
    ipc_call_static_init(&call);
386
    rc = copy_from_uspace(&call.data.args, &question->args,
406
    rc = copy_from_uspace(&call.data.args, &question->args,
387
        sizeof(call.data.args));
407
        sizeof(call.data.args));
388
    if (rc != 0)
408
    if (rc != 0)
389
        return (unative_t) rc;
409
        return (unative_t) rc;
390
 
410
 
391
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
411
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
392
 
412
 
393
    if (!(res = request_preprocess(&call))) {
413
    if (!(res = request_preprocess(&call))) {
394
        ipc_call_sync(phone, &call);
414
        ipc_call_sync(phone, &call);
395
        process_answer(&call);
415
        process_answer(&call);
396
    } else
416
    } else
397
        IPC_SET_RETVAL(call.data, res);
417
        IPC_SET_RETVAL(call.data, res);
398
 
418
 
399
    rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
419
    rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
400
    if (rc != 0)
420
    if (rc != 0)
401
        return rc;
421
        return rc;
402
 
422
 
403
    return 0;
423
    return 0;
404
}
424
}
405
 
425
 
406
/** Check that the task did not exceed the allowed limit of asynchronous calls.
426
/** Check that the task did not exceed the allowed limit of asynchronous calls.
407
 *
427
 *
408
 * @return      Return 0 if limit not reached or -1 if limit exceeded.
428
 * @return      Return 0 if limit not reached or -1 if limit exceeded.
409
 */
429
 */
410
static int check_call_limit(void)
430
static int check_call_limit(void)
411
{
431
{
412
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
432
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
413
        atomic_dec(&TASK->active_calls);
433
        atomic_dec(&TASK->active_calls);
414
        return -1;
434
        return -1;
415
    }
435
    }
416
    return 0;
436
    return 0;
417
}
437
}
418
 
438
 
419
/** Make a fast asynchronous call over IPC.
439
/** Make a fast asynchronous call over IPC.
420
 *
440
 *
421
 * This function can only handle two arguments of payload, but is faster than
441
 * This function can only handle two arguments of payload, but is faster than
422
 * the generic function sys_ipc_call_async().
442
 * the generic function sys_ipc_call_async().
423
 *
443
 *
424
 * @param phoneid   Phone handle for the call.
444
 * @param phoneid   Phone handle for the call.
425
 * @param method    Method of the call.
445
 * @param method    Method of the call.
426
 * @param arg1      Service-defined payload argument.
446
 * @param arg1      Service-defined payload argument.
427
 * @param arg2      Service-defined payload argument.
447
 * @param arg2      Service-defined payload argument.
428
 *
448
 *
429
 * @return      Return call hash on success.
449
 * @return      Return call hash on success.
430
 *          Return IPC_CALLRET_FATAL in case of a fatal error and
450
 *          Return IPC_CALLRET_FATAL in case of a fatal error and
431
 *          IPC_CALLRET_TEMPORARY if there are too many pending
451
 *          IPC_CALLRET_TEMPORARY if there are too many pending
432
 *          asynchronous requests; answers should be handled first.
452
 *          asynchronous requests; answers should be handled first.
433
 */
453
 */
434
unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
454
unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
435
    unative_t arg1, unative_t arg2)
455
    unative_t arg1, unative_t arg2)
436
{
456
{
437
    call_t *call;
457
    call_t *call;
438
    phone_t *phone;
458
    phone_t *phone;
439
    int res;
459
    int res;
440
 
460
 
441
    if (check_call_limit())
461
    if (check_call_limit())
442
        return IPC_CALLRET_TEMPORARY;
462
        return IPC_CALLRET_TEMPORARY;
443
 
463
 
444
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
464
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
445
 
465
 
446
    call = ipc_call_alloc(0);
466
    call = ipc_call_alloc(0);
447
    IPC_SET_METHOD(call->data, method);
467
    IPC_SET_METHOD(call->data, method);
448
    IPC_SET_ARG1(call->data, arg1);
468
    IPC_SET_ARG1(call->data, arg1);
449
    IPC_SET_ARG2(call->data, arg2);
469
    IPC_SET_ARG2(call->data, arg2);
450
    IPC_SET_ARG3(call->data, 0);
470
    IPC_SET_ARG3(call->data, 0);
451
 
471
 
452
    if (!(res = request_preprocess(call)))
472
    if (!(res = request_preprocess(call)))
453
        ipc_call(phone, call);
473
        ipc_call(phone, call);
454
    else
474
    else
455
        ipc_backsend_err(phone, call, res);
475
        ipc_backsend_err(phone, call, res);
456
 
476
 
457
    return (unative_t) call;
477
    return (unative_t) call;
458
}
478
}
459
 
479
 
460
/** Make an asynchronous IPC call allowing to transmit the entire payload.
480
/** Make an asynchronous IPC call allowing to transmit the entire payload.
461
 *
481
 *
462
 * @param phoneid   Phone handle for the call.
482
 * @param phoneid   Phone handle for the call.
463
 * @param data      Userspace address of call data with the request.
483
 * @param data      Userspace address of call data with the request.
464
 *
484
 *
465
 * @return      See sys_ipc_call_async_fast().
485
 * @return      See sys_ipc_call_async_fast().
466
 */
486
 */
467
unative_t sys_ipc_call_async(unative_t phoneid, ipc_data_t *data)
487
unative_t sys_ipc_call_async(unative_t phoneid, ipc_data_t *data)
468
{
488
{
469
    call_t *call;
489
    call_t *call;
470
    phone_t *phone;
490
    phone_t *phone;
471
    int res;
491
    int res;
472
    int rc;
492
    int rc;
473
 
493
 
474
    if (check_call_limit())
494
    if (check_call_limit())
475
        return IPC_CALLRET_TEMPORARY;
495
        return IPC_CALLRET_TEMPORARY;
476
 
496
 
477
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
497
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
478
 
498
 
479
    call = ipc_call_alloc(0);
499
    call = ipc_call_alloc(0);
480
    rc = copy_from_uspace(&call->data.args, &data->args,
500
    rc = copy_from_uspace(&call->data.args, &data->args,
481
        sizeof(call->data.args));
501
        sizeof(call->data.args));
482
    if (rc != 0) {
502
    if (rc != 0) {
483
        ipc_call_free(call);
503
        ipc_call_free(call);
484
        return (unative_t) rc;
504
        return (unative_t) rc;
485
    }
505
    }
486
    if (!(res = request_preprocess(call)))
506
    if (!(res = request_preprocess(call)))
487
        ipc_call(phone, call);
507
        ipc_call(phone, call);
488
    else
508
    else
489
        ipc_backsend_err(phone, call, res);
509
        ipc_backsend_err(phone, call, res);
490
 
510
 
491
    return (unative_t) call;
511
    return (unative_t) call;
492
}
512
}
493
 
513
 
494
/** Forward a received call to another destination.
514
/** Forward a received call to another destination.
495
 *
515
 *
496
 * @param callid    Hash of the call to forward.
516
 * @param callid    Hash of the call to forward.
497
 * @param phoneid   Phone handle to use for forwarding.
517
 * @param phoneid   Phone handle to use for forwarding.
498
 * @param method    New method to use for the forwarded call.
518
 * @param method    New method to use for the forwarded call.
499
 * @param arg1      New value of the first argument for the forwarded call.
519
 * @param arg1      New value of the first argument for the forwarded call.
500
 *
520
 *
501
 * @return      Return 0 on succes, otherwise return an error code.
521
 * @return      Return 0 on succes, otherwise return an error code.
502
 *
522
 *
503
 * In case the original method is a system method, ARG1 and ARG2 are overwritten
523
 * In case the original method is a system method, ARG1 and ARG2 are overwritten
504
 * in the forwarded message with the new method and the new arg1, respectively.
524
 * in the forwarded message with the new method and the new arg1, respectively.
505
 * Otherwise the METHOD and ARG1 are rewritten with the new method and arg1,
525
 * Otherwise the METHOD and ARG1 are rewritten with the new method and arg1,
506
 * respectively.
526
 * respectively. Also note there is a set of immutable methods, for which the
-
 
527
 * new method and argument is not set and these values are ignored.
507
 *
528
 *
508
 * Warning: If implementing non-fast version, make sure that
529
 * Warning: If implementing non-fast version, make sure that
509
 *          ARG3 is not rewritten for certain system IPC
530
 *          ARG3 is not rewritten for certain system IPC
510
 */
531
 */
511
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
532
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
512
    unative_t method, unative_t arg1)
533
    unative_t method, unative_t arg1)
513
{
534
{
514
    call_t *call;
535
    call_t *call;
515
    phone_t *phone;
536
    phone_t *phone;
516
 
537
 
517
    call = get_call(callid);
538
    call = get_call(callid);
518
    if (!call)
539
    if (!call)
519
        return ENOENT;
540
        return ENOENT;
520
 
541
 
521
    call->flags |= IPC_CALL_FORWARDED;
542
    call->flags |= IPC_CALL_FORWARDED;
522
 
543
 
523
    GET_CHECK_PHONE(phone, phoneid, {
544
    GET_CHECK_PHONE(phone, phoneid, {
524
        IPC_SET_RETVAL(call->data, EFORWARD);
545
        IPC_SET_RETVAL(call->data, EFORWARD);
525
        ipc_answer(&TASK->answerbox, call);
546
        ipc_answer(&TASK->answerbox, call);
526
        return ENOENT;
547
        return ENOENT;
527
    });    
548
    });    
528
 
549
 
529
    if (!is_forwardable(IPC_GET_METHOD(call->data))) {
550
    if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
530
        IPC_SET_RETVAL(call->data, EFORWARD);
551
        IPC_SET_RETVAL(call->data, EFORWARD);
531
        ipc_answer(&TASK->answerbox, call);
552
        ipc_answer(&TASK->answerbox, call);
532
        return EPERM;
553
        return EPERM;
533
    }
554
    }
534
 
555
 
-
 
556
    /*
535
    /* Userspace is not allowed to change method of system methods
557
     * Userspace is not allowed to change method of system methods on
536
     * on forward, allow changing ARG1 and ARG2 by means of method and arg1
558
     * forward, allow changing ARG1 and ARG2 by means of method and arg1.
-
 
559
     * If the method is immutable, don't change anything.
537
     */
560
     */
-
 
561
    if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
538
    if (is_system_method(IPC_GET_METHOD(call->data))) {
562
        if (method_is_system(IPC_GET_METHOD(call->data))) {
539
        if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
563
            if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
540
            phone_dealloc(IPC_GET_ARG3(call->data));
564
                phone_dealloc(IPC_GET_ARG3(call->data));
541
 
565
 
542
        IPC_SET_ARG1(call->data, method);
566
            IPC_SET_ARG1(call->data, method);
543
        IPC_SET_ARG2(call->data, arg1);
567
            IPC_SET_ARG2(call->data, arg1);
544
    } else {
568
        } else {
545
        IPC_SET_METHOD(call->data, method);
569
            IPC_SET_METHOD(call->data, method);
546
        IPC_SET_ARG1(call->data, arg1);
570
            IPC_SET_ARG1(call->data, arg1);
-
 
571
        }
547
    }
572
    }
548
 
573
 
549
    return ipc_forward(call, phone, &TASK->answerbox);
574
    return ipc_forward(call, phone, &TASK->answerbox);
550
}
575
}
551
 
576
 
552
/** Answer an IPC call - fast version.
577
/** Answer an IPC call - fast version.
553
 *
578
 *
554
 * This function can handle only two return arguments of payload, but is faster
579
 * This function can handle only two return arguments of payload, but is faster
555
 * than the generic sys_ipc_answer().
580
 * than the generic sys_ipc_answer().
556
 *
581
 *
557
 * @param callid    Hash of the call to be answered.
582
 * @param callid    Hash of the call to be answered.
558
 * @param retval    Return value of the answer.
583
 * @param retval    Return value of the answer.
559
 * @param arg1      Service-defined return value.
584
 * @param arg1      Service-defined return value.
560
 * @param arg2      Service-defined return value.
585
 * @param arg2      Service-defined return value.
561
 *
586
 *
562
 * @return      Return 0 on success, otherwise return an error code.   
587
 * @return      Return 0 on success, otherwise return an error code.   
563
 */
588
 */
564
unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
589
unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
565
    unative_t arg1, unative_t arg2)
590
    unative_t arg1, unative_t arg2)
566
{
591
{
567
    call_t *call;
592
    call_t *call;
568
    ipc_data_t saved_data;
593
    ipc_data_t saved_data;
569
    int saveddata = 0;
594
    int saveddata = 0;
570
    int rc;
595
    int rc;
571
 
596
 
572
    /* Do not answer notification callids */
597
    /* Do not answer notification callids */
573
    if (callid & IPC_CALLID_NOTIFICATION)
598
    if (callid & IPC_CALLID_NOTIFICATION)
574
        return 0;
599
        return 0;
575
 
600
 
576
    call = get_call(callid);
601
    call = get_call(callid);
577
    if (!call)
602
    if (!call)
578
        return ENOENT;
603
        return ENOENT;
579
 
604
 
580
    if (answer_need_old(call)) {
605
    if (answer_need_old(call)) {
581
        memcpy(&saved_data, &call->data, sizeof(call->data));
606
        memcpy(&saved_data, &call->data, sizeof(call->data));
582
        saveddata = 1;
607
        saveddata = 1;
583
    }
608
    }
584
 
609
 
585
    IPC_SET_RETVAL(call->data, retval);
610
    IPC_SET_RETVAL(call->data, retval);
586
    IPC_SET_ARG1(call->data, arg1);
611
    IPC_SET_ARG1(call->data, arg1);
587
    IPC_SET_ARG2(call->data, arg2);
612
    IPC_SET_ARG2(call->data, arg2);
588
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
613
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
589
 
614
 
590
    ipc_answer(&TASK->answerbox, call);
615
    ipc_answer(&TASK->answerbox, call);
591
    return rc;
616
    return rc;
592
}
617
}
593
 
618
 
594
/** Answer an IPC call.
619
/** Answer an IPC call.
595
 *
620
 *
596
 * @param callid    Hash of the call to be answered.
621
 * @param callid    Hash of the call to be answered.
597
 * @param data      Userspace address of call data with the answer.
622
 * @param data      Userspace address of call data with the answer.
598
 *
623
 *
599
 * @return      Return 0 on success, otherwise return an error code.
624
 * @return      Return 0 on success, otherwise return an error code.
600
 */
625
 */
601
unative_t sys_ipc_answer(unative_t callid, ipc_data_t *data)
626
unative_t sys_ipc_answer(unative_t callid, ipc_data_t *data)
602
{
627
{
603
    call_t *call;
628
    call_t *call;
604
    ipc_data_t saved_data;
629
    ipc_data_t saved_data;
605
    int saveddata = 0;
630
    int saveddata = 0;
606
    int rc;
631
    int rc;
607
 
632
 
608
    /* Do not answer notification callids */
633
    /* Do not answer notification callids */
609
    if (callid & IPC_CALLID_NOTIFICATION)
634
    if (callid & IPC_CALLID_NOTIFICATION)
610
        return 0;
635
        return 0;
611
 
636
 
612
    call = get_call(callid);
637
    call = get_call(callid);
613
    if (!call)
638
    if (!call)
614
        return ENOENT;
639
        return ENOENT;
615
 
640
 
616
    if (answer_need_old(call)) {
641
    if (answer_need_old(call)) {
617
        memcpy(&saved_data, &call->data, sizeof(call->data));
642
        memcpy(&saved_data, &call->data, sizeof(call->data));
618
        saveddata = 1;
643
        saveddata = 1;
619
    }
644
    }
620
    rc = copy_from_uspace(&call->data.args, &data->args,
645
    rc = copy_from_uspace(&call->data.args, &data->args,
621
        sizeof(call->data.args));
646
        sizeof(call->data.args));
622
    if (rc != 0)
647
    if (rc != 0)
623
        return rc;
648
        return rc;
624
 
649
 
625
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
650
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
626
   
651
   
627
    ipc_answer(&TASK->answerbox, call);
652
    ipc_answer(&TASK->answerbox, call);
628
 
653
 
629
    return rc;
654
    return rc;
630
}
655
}
631
 
656
 
632
/** Hang up a phone.
657
/** Hang up a phone.
633
 *
658
 *
634
 * @param       Phone handle of the phone to be hung up.
659
 * @param       Phone handle of the phone to be hung up.
635
 *
660
 *
636
 * @return      Return 0 on success or an error code.
661
 * @return      Return 0 on success or an error code.
637
 */
662
 */
638
unative_t sys_ipc_hangup(int phoneid)
663
unative_t sys_ipc_hangup(int phoneid)
639
{
664
{
640
    phone_t *phone;
665
    phone_t *phone;
641
 
666
 
642
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
667
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
643
 
668
 
644
    if (ipc_phone_hangup(phone))
669
    if (ipc_phone_hangup(phone))
645
        return -1;
670
        return -1;
646
 
671
 
647
    return 0;
672
    return 0;
648
}
673
}
649
 
674
 
650
/** Wait for an incoming IPC call or an answer.
675
/** Wait for an incoming IPC call or an answer.
651
 *
676
 *
652
 * @param calldata  Pointer to buffer where the call/answer data is stored.
677
 * @param calldata  Pointer to buffer where the call/answer data is stored.
653
 * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
678
 * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
654
 * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
679
 * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
655
 *          for explanation.
680
 *          for explanation.
656
 *
681
 *
657
 * @return      Hash of the call.
682
 * @return      Hash of the call.
658
 *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
683
 *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
659
 *          call is a notification. IPC_CALLID_ANSWERED denotes an
684
 *          call is a notification. IPC_CALLID_ANSWERED denotes an
660
 *          answer.
685
 *          answer.
661
 */
686
 */
662
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
687
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
663
{
688
{
664
    call_t *call;
689
    call_t *call;
665
 
690
 
666
restart:   
691
restart:   
667
    call = ipc_wait_for_call(&TASK->answerbox, usec,
692
    call = ipc_wait_for_call(&TASK->answerbox, usec,
668
        flags | SYNCH_FLAGS_INTERRUPTIBLE);
693
        flags | SYNCH_FLAGS_INTERRUPTIBLE);
669
    if (!call)
694
    if (!call)
670
        return 0;
695
        return 0;
671
 
696
 
672
    if (call->flags & IPC_CALL_NOTIF) {
697
    if (call->flags & IPC_CALL_NOTIF) {
673
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
698
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
674
 
699
 
675
        /* Set in_phone_hash to the interrupt counter */
700
        /* Set in_phone_hash to the interrupt counter */
676
        call->data.phone = (void *) call->priv;
701
        call->data.phone = (void *) call->priv;
677
       
702
       
678
        STRUCT_TO_USPACE(calldata, &call->data);
703
        STRUCT_TO_USPACE(calldata, &call->data);
679
 
704
 
680
        ipc_call_free(call);
705
        ipc_call_free(call);
681
       
706
       
682
        return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
707
        return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
683
    }
708
    }
684
 
709
 
685
    if (call->flags & IPC_CALL_ANSWERED) {
710
    if (call->flags & IPC_CALL_ANSWERED) {
686
        process_answer(call);
711
        process_answer(call);
687
 
712
 
688
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
713
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
689
 
714
 
690
        atomic_dec(&TASK->active_calls);
715
        atomic_dec(&TASK->active_calls);
691
 
716
 
692
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
717
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
693
            ipc_call_free(call);
718
            ipc_call_free(call);
694
            goto restart;
719
            goto restart;
695
        }
720
        }
696
 
721
 
697
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
722
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
698
        ipc_call_free(call);
723
        ipc_call_free(call);
699
 
724
 
700
        return ((unative_t) call) | IPC_CALLID_ANSWERED;
725
        return ((unative_t) call) | IPC_CALLID_ANSWERED;
701
    }
726
    }
702
 
727
 
703
    if (process_request(&TASK->answerbox, call))
728
    if (process_request(&TASK->answerbox, call))
704
        goto restart;
729
        goto restart;
705
 
730
 
706
    /* Include phone address('id') of the caller in the request,
731
    /* Include phone address('id') of the caller in the request,
707
     * copy whole call->data, not only call->data.args */
732
     * copy whole call->data, not only call->data.args */
708
    if (STRUCT_TO_USPACE(calldata, &call->data)) {
733
    if (STRUCT_TO_USPACE(calldata, &call->data)) {
709
        return 0;
734
        return 0;
710
    }
735
    }
711
    return (unative_t)call;
736
    return (unative_t)call;
712
}
737
}
713
 
738
 
714
/** Connect an IRQ handler to a task.
739
/** Connect an IRQ handler to a task.
715
 *
740
 *
716
 * @param inr       IRQ number.
741
 * @param inr       IRQ number.
717
 * @param devno     Device number.
742
 * @param devno     Device number.
718
 * @param method    Method to be associated with the notification.
743
 * @param method    Method to be associated with the notification.
719
 * @param ucode     Uspace pointer to the top-half pseudocode.
744
 * @param ucode     Uspace pointer to the top-half pseudocode.
720
 *
745
 *
721
 * @return      EPERM or a return code returned by ipc_irq_register().
746
 * @return      EPERM or a return code returned by ipc_irq_register().
722
 */
747
 */
723
unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
748
unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
724
    irq_code_t *ucode)
749
    irq_code_t *ucode)
725
{
750
{
726
    if (!(cap_get(TASK) & CAP_IRQ_REG))
751
    if (!(cap_get(TASK) & CAP_IRQ_REG))
727
        return EPERM;
752
        return EPERM;
728
 
753
 
729
    return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
754
    return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
730
}
755
}
731
 
756
 
732
/** Disconnect an IRQ handler from a task.
757
/** Disconnect an IRQ handler from a task.
733
 *
758
 *
734
 * @param inr       IRQ number.
759
 * @param inr       IRQ number.
735
 * @param devno     Device number.
760
 * @param devno     Device number.
736
 *
761
 *
737
 * @return      Zero on success or EPERM on error..
762
 * @return      Zero on success or EPERM on error..
738
 */
763
 */
739
unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
764
unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
740
{
765
{
741
    if (!(cap_get(TASK) & CAP_IRQ_REG))
766
    if (!(cap_get(TASK) & CAP_IRQ_REG))
742
        return EPERM;
767
        return EPERM;
743
 
768
 
744
    ipc_irq_unregister(&TASK->answerbox, inr, devno);
769
    ipc_irq_unregister(&TASK->answerbox, inr, devno);
745
 
770
 
746
    return 0;
771
    return 0;
747
}
772
}
748
 
773
 
749
/** @}
774
/** @}
750
 */
775
 */
751
 
776