Subversion Repositories HelenOS

Rev

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

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