Subversion Repositories HelenOS

Rev

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

Rev 2619 Rev 2620
1
/*
1
/*
2
 * Copyright (c) 2006 Ondrej Palkovsky
2
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup 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 method_is_system(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 method_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
        /* This message is meant only for the original recipient. */
93
        /* This message is meant only for the original recipient. */
94
        return 0;
94
        return 0;
95
    default:
95
    default:
96
        return 1;
96
        return 1;
97
    }
97
    }
98
}
98
}
99
 
99
 
100
/** Decide if the message with this method is immutable on forward.
100
/** Decide if the message with this method is immutable on forward.
101
 *
101
 *
102
 * - some system messages may be forwarded but their content cannot be altered
102
 * - some system messages may be forwarded but their content cannot be altered
103
 *
103
 *
104
 * @param method    Method to be decided.
104
 * @param method    Method to be decided.
105
 *
105
 *
106
 * @return      Return 1 if the method is immutable on forward.
106
 * @return      Return 1 if the method is immutable on forward.
107
 *          Otherwise return 0.
107
 *          Otherwise return 0.
108
 */
108
 */
109
static inline int method_is_immutable(unative_t method)
109
static inline int method_is_immutable(unative_t method)
110
{
110
{
111
    switch (method) {
111
    switch (method) {
112
    case IPC_M_AS_AREA_SEND:
112
    case IPC_M_AS_AREA_SEND:
113
    case IPC_M_AS_AREA_RECV:
113
    case IPC_M_AS_AREA_RECV:
114
    case IPC_M_DATA_SEND:
114
    case IPC_M_DATA_SEND:
115
        return 1;
115
        return 1;
116
        break;
116
        break;
117
    default:
117
    default:
118
        return 0;
118
        return 0;
119
    }
119
    }
120
}
120
}
121
 
121
 
122
 
122
 
123
/***********************************************************************
123
/***********************************************************************
124
 * Functions that preprocess answer before sending it to the recepient.
124
 * Functions that preprocess answer before sending it to the recepient.
125
 ***********************************************************************/
125
 ***********************************************************************/
126
 
126
 
127
/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
127
/** Decide if the caller (e.g. ipc_answer()) should save the old call contents
128
 * for answer_preprocess().
128
 * for answer_preprocess().
129
 *
129
 *
130
 * @param call      Call structure to be decided.
130
 * @param call      Call structure to be decided.
131
 *
131
 *
132
 * @return      Return 1 if the old call contents should be saved.
132
 * @return      Return 1 if the old call contents should be saved.
133
 *          Return 0 otherwise.
133
 *          Return 0 otherwise.
134
 */
134
 */
135
static inline int answer_need_old(call_t *call)
135
static inline int answer_need_old(call_t *call)
136
{
136
{
137
    switch (IPC_GET_METHOD(call->data)) {
137
    switch (IPC_GET_METHOD(call->data)) {
138
    case IPC_M_CONNECT_TO_ME:
138
    case IPC_M_CONNECT_TO_ME:
139
    case IPC_M_CONNECT_ME_TO:
139
    case IPC_M_CONNECT_ME_TO:
140
    case IPC_M_AS_AREA_SEND:
140
    case IPC_M_AS_AREA_SEND:
141
    case IPC_M_AS_AREA_RECV:
141
    case IPC_M_AS_AREA_RECV:
142
    case IPC_M_DATA_SEND:
142
    case IPC_M_DATA_SEND:
143
        return 1;
143
        return 1;
144
    default:
144
    default:
145
        return 0;
145
        return 0;
146
    }
146
    }
147
}
147
}
148
 
148
 
149
/** Interpret process answer as control information.
149
/** Interpret process answer as control information.
150
 *
150
 *
151
 * This function is called directly after sys_ipc_answer().
151
 * This function is called directly after sys_ipc_answer().
152
 *
152
 *
153
 * @param answer    Call structure with the answer.
153
 * @param answer    Call structure with the answer.
154
 * @param olddata   Saved data of the request.
154
 * @param olddata   Saved data of the request.
155
 *
155
 *
156
 * @return      Return 0 on success or an error code.
156
 * @return      Return 0 on success or an error code.
157
 */
157
 */
158
static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
158
static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
159
{
159
{
160
    int phoneid;
160
    int phoneid;
161
 
161
 
162
    if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
162
    if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
163
        /* In case of forward, hangup the forwared phone,
163
        /* In case of forward, hangup the forwared phone,
164
         * not the originator
164
         * not the originator
165
         */
165
         */
166
        spinlock_lock(&answer->data.phone->lock);
166
        spinlock_lock(&answer->data.phone->lock);
167
        spinlock_lock(&TASK->answerbox.lock);
167
        spinlock_lock(&TASK->answerbox.lock);
168
        if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
168
        if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
169
            list_remove(&answer->data.phone->link);
169
            list_remove(&answer->data.phone->link);
170
            answer->data.phone->state = IPC_PHONE_SLAMMED;
170
            answer->data.phone->state = IPC_PHONE_SLAMMED;
171
        }
171
        }
172
        spinlock_unlock(&TASK->answerbox.lock);
172
        spinlock_unlock(&TASK->answerbox.lock);
173
        spinlock_unlock(&answer->data.phone->lock);
173
        spinlock_unlock(&answer->data.phone->lock);
174
    }
174
    }
175
 
175
 
176
    if (!olddata)
176
    if (!olddata)
177
        return 0;
177
        return 0;
178
 
178
 
179
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
179
    if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
180
        phoneid = IPC_GET_ARG3(*olddata);
180
        phoneid = IPC_GET_ARG3(*olddata);
181
        if (IPC_GET_RETVAL(answer->data)) {
181
        if (IPC_GET_RETVAL(answer->data)) {
182
            /* The connection was not accepted */
182
            /* The connection was not accepted */
183
            phone_dealloc(phoneid);
183
            phone_dealloc(phoneid);
184
        } else {
184
        } else {
185
            /* The connection was accepted */
185
            /* The connection was accepted */
186
            phone_connect(phoneid, &answer->sender->answerbox);
186
            phone_connect(phoneid, &answer->sender->answerbox);
187
            /* Set 'phone hash' as arg3 of response */
187
            /* Set 'phone hash' as arg3 of response */
188
            IPC_SET_ARG3(answer->data,
188
            IPC_SET_ARG3(answer->data,
189
                (unative_t) &TASK->phones[phoneid]);
189
                (unative_t) &TASK->phones[phoneid]);
190
        }
190
        }
191
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
191
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
192
        /* If the users accepted call, connect */
192
        /* If the users accepted call, connect */
193
        if (!IPC_GET_RETVAL(answer->data)) {
193
        if (!IPC_GET_RETVAL(answer->data)) {
194
            ipc_phone_connect((phone_t *) IPC_GET_ARG3(*olddata),
194
            ipc_phone_connect((phone_t *) IPC_GET_ARG3(*olddata),
195
                &TASK->answerbox);
195
                &TASK->answerbox);
196
        }
196
        }
197
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) {
197
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) {
198
        if (!IPC_GET_RETVAL(answer->data)) {
198
        if (!IPC_GET_RETVAL(answer->data)) {
199
            /* Accepted, handle as_area receipt */
199
            /* Accepted, handle as_area receipt */
200
            ipl_t ipl;
200
            ipl_t ipl;
201
            int rc;
201
            int rc;
202
            as_t *as;
202
            as_t *as;
203
           
203
           
204
            ipl = interrupts_disable();
204
            ipl = interrupts_disable();
205
            spinlock_lock(&answer->sender->lock);
205
            spinlock_lock(&answer->sender->lock);
206
            as = answer->sender->as;
206
            as = answer->sender->as;
207
            spinlock_unlock(&answer->sender->lock);
207
            spinlock_unlock(&answer->sender->lock);
208
            interrupts_restore(ipl);
208
            interrupts_restore(ipl);
209
           
209
           
210
            rc = as_area_share(as, IPC_GET_ARG1(*olddata),
210
            rc = as_area_share(as, IPC_GET_ARG1(*olddata),
211
                IPC_GET_ARG2(*olddata), AS,
211
                IPC_GET_ARG2(*olddata), AS,
212
                IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
212
                IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
213
            IPC_SET_RETVAL(answer->data, rc);
213
            IPC_SET_RETVAL(answer->data, rc);
214
            return rc;
214
            return rc;
215
        }
215
        }
216
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) {
216
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) {
217
        if (!IPC_GET_RETVAL(answer->data)) {
217
        if (!IPC_GET_RETVAL(answer->data)) {
218
            ipl_t ipl;
218
            ipl_t ipl;
219
            as_t *as;
219
            as_t *as;
220
            int rc;
220
            int rc;
221
           
221
           
222
            ipl = interrupts_disable();
222
            ipl = interrupts_disable();
223
            spinlock_lock(&answer->sender->lock);
223
            spinlock_lock(&answer->sender->lock);
224
            as = answer->sender->as;
224
            as = answer->sender->as;
225
            spinlock_unlock(&answer->sender->lock);
225
            spinlock_unlock(&answer->sender->lock);
226
            interrupts_restore(ipl);
226
            interrupts_restore(ipl);
227
           
227
           
228
            rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
228
            rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
229
                IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
229
                IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
230
                IPC_GET_ARG2(answer->data));
230
                IPC_GET_ARG2(answer->data));
231
            IPC_SET_RETVAL(answer->data, rc);
231
            IPC_SET_RETVAL(answer->data, rc);
232
        }
232
        }
233
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_SEND) {
233
    } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_SEND) {
234
        if (!IPC_GET_RETVAL(answer->data)) {
234
        if (!IPC_GET_RETVAL(answer->data)) {
235
            int rc;
235
            int rc;
236
            uintptr_t dst;
236
            uintptr_t dst;
237
            uintptr_t size;
237
            uintptr_t size;
238
 
238
 
239
            ASSERT(answer->buffer);
239
            ASSERT(answer->buffer);
240
 
240
 
241
            dst = IPC_GET_ARG1(answer->data);
241
            dst = IPC_GET_ARG1(answer->data);
242
            size = IPC_GET_ARG3(answer->data);
242
            size = IPC_GET_ARG3(answer->data);
243
 
243
 
244
            rc = copy_to_uspace((void *) dst, answer->buffer, size);
244
            rc = copy_to_uspace((void *) dst, answer->buffer, size);
245
            if (rc != 0)
245
            if (rc != 0)
246
                IPC_SET_RETVAL(answer->data, rc);
246
                IPC_SET_RETVAL(answer->data, rc);
247
            free(answer->buffer);
247
            free(answer->buffer);
248
            answer->buffer = NULL;
248
            answer->buffer = NULL;
249
        }
249
        }
250
    }
250
    }
251
    return 0;
251
    return 0;
252
}
252
}
253
 
253
 
254
/** Called before the request is sent.
254
/** Called before the request is sent.
255
 *
255
 *
256
 * @param call      Call structure with the request.
256
 * @param call      Call structure with the request.
257
 *
257
 *
258
 * @return      Return 0 on success, ELIMIT or EPERM on error.
258
 * @return      Return 0 on success, ELIMIT or EPERM on error.
259
 */
259
 */
260
static int request_preprocess(call_t *call)
260
static int request_preprocess(call_t *call)
261
{
261
{
262
    int newphid;
262
    int newphid;
263
    size_t size;
263
    size_t size;
264
    uintptr_t src;
264
    uintptr_t src;
265
    int rc;
265
    int rc;
266
 
266
 
267
    switch (IPC_GET_METHOD(call->data)) {
267
    switch (IPC_GET_METHOD(call->data)) {
268
    case IPC_M_CONNECT_ME_TO:
268
    case IPC_M_CONNECT_ME_TO:
269
        newphid = phone_alloc();
269
        newphid = phone_alloc();
270
        if (newphid < 0)
270
        if (newphid < 0)
271
            return ELIMIT;
271
            return ELIMIT;
272
        /* Set arg3 for server */
272
        /* Set arg3 for server */
273
        IPC_SET_ARG3(call->data, (unative_t) &TASK->phones[newphid]);
273
        IPC_SET_ARG3(call->data, (unative_t) &TASK->phones[newphid]);
274
        call->flags |= IPC_CALL_CONN_ME_TO;
274
        call->flags |= IPC_CALL_CONN_ME_TO;
275
        call->priv = newphid;
275
        call->priv = newphid;
276
        break;
276
        break;
277
    case IPC_M_AS_AREA_SEND:
277
    case IPC_M_AS_AREA_SEND:
278
        size = as_area_get_size(IPC_GET_ARG1(call->data));
278
        size = as_area_get_size(IPC_GET_ARG1(call->data));
279
        if (!size)
279
        if (!size)
280
            return EPERM;
280
            return EPERM;
281
        IPC_SET_ARG2(call->data, size);
281
        IPC_SET_ARG2(call->data, size);
282
        break;
282
        break;
283
    case IPC_M_DATA_SEND:
283
    case IPC_M_DATA_SEND:
284
        src = IPC_GET_ARG2(call->data);
284
        src = IPC_GET_ARG2(call->data);
285
        size = IPC_GET_ARG3(call->data);
285
        size = IPC_GET_ARG3(call->data);
286
       
286
       
287
        if ((size <= 0) || (size > DATA_SEND_LIMIT))
287
        if ((size <= 0) || (size > DATA_SEND_LIMIT))
288
            return ELIMIT;
288
            return ELIMIT;
289
       
289
       
290
        call->buffer = (uint8_t *) malloc(size, 0);
290
        call->buffer = (uint8_t *) malloc(size, 0);
291
        rc = copy_from_uspace(call->buffer, (void *) src, size);
291
        rc = copy_from_uspace(call->buffer, (void *) src, size);
292
        if (rc != 0) {
292
        if (rc != 0) {
293
            free(call->buffer);
293
            free(call->buffer);
294
            return rc;
294
            return rc;
295
        }
295
        }
296
        break;
296
        break;
297
    default:
297
    default:
298
        break;
298
        break;
299
    }
299
    }
300
    return 0;
300
    return 0;
301
}
301
}
302
 
302
 
303
/*******************************************************************************
303
/*******************************************************************************
304
 * Functions called to process received call/answer before passing it to uspace.
304
 * Functions called to process received call/answer before passing it to uspace.
305
 *******************************************************************************/
305
 *******************************************************************************/
306
 
306
 
307
/** Do basic kernel processing of received call answer.
307
/** Do basic kernel processing of received call answer.
308
 *
308
 *
309
 * @param call      Call structure with the answer.
309
 * @param call      Call structure with the answer.
310
 */
310
 */
311
static void process_answer(call_t *call)
311
static void process_answer(call_t *call)
312
{
312
{
313
    if (IPC_GET_RETVAL(call->data) == EHANGUP &&
313
    if (IPC_GET_RETVAL(call->data) == EHANGUP &&
314
        (call->flags & IPC_CALL_FORWARDED))
314
        (call->flags & IPC_CALL_FORWARDED))
315
        IPC_SET_RETVAL(call->data, EFORWARD);
315
        IPC_SET_RETVAL(call->data, EFORWARD);
316
 
316
 
317
    if (call->flags & IPC_CALL_CONN_ME_TO) {
317
    if (call->flags & IPC_CALL_CONN_ME_TO) {
318
        if (IPC_GET_RETVAL(call->data))
318
        if (IPC_GET_RETVAL(call->data))
319
            phone_dealloc(call->priv);
319
            phone_dealloc(call->priv);
320
        else
320
        else
321
            IPC_SET_ARG3(call->data, call->priv);
321
            IPC_SET_ARG3(call->data, call->priv);
322
    }
322
    }
323
}
323
}
324
 
324
 
325
/** Do basic kernel processing of received call request.
325
/** Do basic kernel processing of received call request.
326
 *
326
 *
327
 * @param box       Destination answerbox structure.
327
 * @param box       Destination answerbox structure.
328
 * @param call      Call structure with the request.
328
 * @param call      Call structure with the request.
329
 *
329
 *
330
 * @return      Return 0 if the call should be passed to userspace.
330
 * @return      Return 0 if the call should be passed to userspace.
331
 *          Return -1 if the call should be ignored.
331
 *          Return -1 if the call should be ignored.
332
 */
332
 */
333
static int process_request(answerbox_t *box, call_t *call)
333
static int process_request(answerbox_t *box, call_t *call)
334
{
334
{
335
    int phoneid;
335
    int phoneid;
336
 
336
 
337
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
337
    if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
338
        phoneid = phone_alloc();
338
        phoneid = phone_alloc();
339
        if (phoneid < 0) { /* Failed to allocate phone */
339
        if (phoneid < 0) { /* Failed to allocate phone */
340
            IPC_SET_RETVAL(call->data, ELIMIT);
340
            IPC_SET_RETVAL(call->data, ELIMIT);
341
            ipc_answer(box, call);
341
            ipc_answer(box, call);
342
            return -1;
342
            return -1;
343
        }
343
        }
344
        IPC_SET_ARG3(call->data, phoneid);
344
        IPC_SET_ARG3(call->data, phoneid);
345
    }
345
    }
346
    return 0;
346
    return 0;
347
}
347
}
348
 
348
 
349
/** Make a fast call over IPC, wait for reply and return to user.
349
/** Make a fast call over IPC, wait for reply and return to user.
350
 *
350
 *
351
 * This function can handle only three arguments of payload, but is faster than
351
 * This function can handle only three arguments of payload, but is faster than
352
 * the generic function (i.e. sys_ipc_call_sync_slow()).
352
 * the generic function (i.e. sys_ipc_call_sync_slow()).
353
 *
353
 *
354
 * @param phoneid   Phone handle for the call.
354
 * @param phoneid   Phone handle for the call.
355
 * @param method    Method of the call.
355
 * @param method    Method of the call.
356
 * @param arg1      Service-defined payload argument.
356
 * @param arg1      Service-defined payload argument.
357
 * @param arg2      Service-defined payload argument.
357
 * @param arg2      Service-defined payload argument.
358
 * @param arg3      Service-defined payload argument.
358
 * @param arg3      Service-defined payload argument.
359
 * @param data      Address of userspace structure where the reply call will
359
 * @param data      Address of userspace structure where the reply call will
360
 *          be stored.
360
 *          be stored.
361
 *
361
 *
362
 * @return      Returns 0 on success.
362
 * @return      Returns 0 on success.
363
 *          Return ENOENT if there is no such phone handle.
363
 *          Return ENOENT if there is no such phone handle.
364
 */
364
 */
365
unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
365
unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
366
    unative_t arg1, unative_t arg2, unative_t arg3, ipc_data_t *data)
366
    unative_t arg1, unative_t arg2, unative_t arg3, ipc_data_t *data)
367
{
367
{
368
    call_t call;
368
    call_t call;
369
    phone_t *phone;
369
    phone_t *phone;
370
    int res;
370
    int res;
371
    int rc;
371
    int rc;
372
 
372
 
373
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
373
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
374
 
374
 
375
    ipc_call_static_init(&call);
375
    ipc_call_static_init(&call);
376
    IPC_SET_METHOD(call.data, method);
376
    IPC_SET_METHOD(call.data, method);
377
    IPC_SET_ARG1(call.data, arg1);
377
    IPC_SET_ARG1(call.data, arg1);
378
    IPC_SET_ARG2(call.data, arg2);
378
    IPC_SET_ARG2(call.data, arg2);
379
    IPC_SET_ARG3(call.data, arg3);
379
    IPC_SET_ARG3(call.data, arg3);
-
 
380
    /*
-
 
381
     * To achieve deterministic behavior, zero out arguments that are beyond
-
 
382
     * the limits of the fast version.
-
 
383
     */
-
 
384
    IPC_SET_ARG4(call.data, 0);
-
 
385
    IPC_SET_ARG5(call.data, 0);
380
 
386
 
381
    if (!(res = request_preprocess(&call))) {
387
    if (!(res = request_preprocess(&call))) {
382
        ipc_call_sync(phone, &call);
388
        ipc_call_sync(phone, &call);
383
        process_answer(&call);
389
        process_answer(&call);
384
    } else {
390
    } else {
385
        IPC_SET_RETVAL(call.data, res);
391
        IPC_SET_RETVAL(call.data, res);
386
    }
392
    }
387
    rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
393
    rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
388
    if (rc != 0)
394
    if (rc != 0)
389
        return rc;
395
        return rc;
390
 
396
 
391
    return 0;
397
    return 0;
392
}
398
}
393
 
399
 
394
/** Make a synchronous IPC call allowing to transmit the entire payload.
400
/** Make a synchronous IPC call allowing to transmit the entire payload.
395
 *
401
 *
396
 * @param phoneid   Phone handle for the call.
402
 * @param phoneid   Phone handle for the call.
397
 * @param question  Userspace address of call data with the request.
403
 * @param question  Userspace address of call data with the request.
398
 * @param reply     Userspace address of call data where to store the
404
 * @param reply     Userspace address of call data where to store the
399
 *          answer.
405
 *          answer.
400
 *
406
 *
401
 * @return      Zero on success or an error code.
407
 * @return      Zero on success or an error code.
402
 */
408
 */
403
unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
409
unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
404
    ipc_data_t *reply)
410
    ipc_data_t *reply)
405
{
411
{
406
    call_t call;
412
    call_t call;
407
    phone_t *phone;
413
    phone_t *phone;
408
    int res;
414
    int res;
409
    int rc;
415
    int rc;
410
 
416
 
411
    ipc_call_static_init(&call);
417
    ipc_call_static_init(&call);
412
    rc = copy_from_uspace(&call.data.args, &question->args,
418
    rc = copy_from_uspace(&call.data.args, &question->args,
413
        sizeof(call.data.args));
419
        sizeof(call.data.args));
414
    if (rc != 0)
420
    if (rc != 0)
415
        return (unative_t) rc;
421
        return (unative_t) rc;
416
 
422
 
417
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
423
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
418
 
424
 
419
    if (!(res = request_preprocess(&call))) {
425
    if (!(res = request_preprocess(&call))) {
420
        ipc_call_sync(phone, &call);
426
        ipc_call_sync(phone, &call);
421
        process_answer(&call);
427
        process_answer(&call);
422
    } else
428
    } else
423
        IPC_SET_RETVAL(call.data, res);
429
        IPC_SET_RETVAL(call.data, res);
424
 
430
 
425
    rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
431
    rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
426
    if (rc != 0)
432
    if (rc != 0)
427
        return rc;
433
        return rc;
428
 
434
 
429
    return 0;
435
    return 0;
430
}
436
}
431
 
437
 
432
/** Check that the task did not exceed the allowed limit of asynchronous calls.
438
/** Check that the task did not exceed the allowed limit of asynchronous calls.
433
 *
439
 *
434
 * @return      Return 0 if limit not reached or -1 if limit exceeded.
440
 * @return      Return 0 if limit not reached or -1 if limit exceeded.
435
 */
441
 */
436
static int check_call_limit(void)
442
static int check_call_limit(void)
437
{
443
{
438
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
444
    if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
439
        atomic_dec(&TASK->active_calls);
445
        atomic_dec(&TASK->active_calls);
440
        return -1;
446
        return -1;
441
    }
447
    }
442
    return 0;
448
    return 0;
443
}
449
}
444
 
450
 
445
/** Make a fast asynchronous call over IPC.
451
/** Make a fast asynchronous call over IPC.
446
 *
452
 *
447
 * This function can only handle four arguments of payload, but is faster than
453
 * This function can only handle four arguments of payload, but is faster than
448
 * the generic function sys_ipc_call_async_slow().
454
 * the generic function sys_ipc_call_async_slow().
449
 *
455
 *
450
 * @param phoneid   Phone handle for the call.
456
 * @param phoneid   Phone handle for the call.
451
 * @param method    Method of the call.
457
 * @param method    Method of the call.
452
 * @param arg1      Service-defined payload argument.
458
 * @param arg1      Service-defined payload argument.
453
 * @param arg2      Service-defined payload argument.
459
 * @param arg2      Service-defined payload argument.
454
 * @param arg3      Service-defined payload argument.
460
 * @param arg3      Service-defined payload argument.
455
 * @param arg4      Service-defined payload argument.
461
 * @param arg4      Service-defined payload argument.
456
 *
462
 *
457
 * @return      Return call hash on success.
463
 * @return      Return call hash on success.
458
 *          Return IPC_CALLRET_FATAL in case of a fatal error and
464
 *          Return IPC_CALLRET_FATAL in case of a fatal error and
459
 *          IPC_CALLRET_TEMPORARY if there are too many pending
465
 *          IPC_CALLRET_TEMPORARY if there are too many pending
460
 *          asynchronous requests; answers should be handled first.
466
 *          asynchronous requests; answers should be handled first.
461
 */
467
 */
462
unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
468
unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
463
    unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
469
    unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
464
{
470
{
465
    call_t *call;
471
    call_t *call;
466
    phone_t *phone;
472
    phone_t *phone;
467
    int res;
473
    int res;
468
 
474
 
469
    if (check_call_limit())
475
    if (check_call_limit())
470
        return IPC_CALLRET_TEMPORARY;
476
        return IPC_CALLRET_TEMPORARY;
471
 
477
 
472
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
478
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
473
 
479
 
474
    call = ipc_call_alloc(0);
480
    call = ipc_call_alloc(0);
475
    IPC_SET_METHOD(call->data, method);
481
    IPC_SET_METHOD(call->data, method);
476
    IPC_SET_ARG1(call->data, arg1);
482
    IPC_SET_ARG1(call->data, arg1);
477
    IPC_SET_ARG2(call->data, arg2);
483
    IPC_SET_ARG2(call->data, arg2);
478
    IPC_SET_ARG3(call->data, arg3);
484
    IPC_SET_ARG3(call->data, arg3);
479
    IPC_SET_ARG4(call->data, arg4);
485
    IPC_SET_ARG4(call->data, arg4);
-
 
486
    /*
-
 
487
     * To achieve deterministic behavior, zero out arguments that are beyond
-
 
488
     * the limits of the fast version.
-
 
489
     */
-
 
490
    IPC_SET_ARG5(call->data, 0);
480
 
491
 
481
    if (!(res = request_preprocess(call)))
492
    if (!(res = request_preprocess(call)))
482
        ipc_call(phone, call);
493
        ipc_call(phone, call);
483
    else
494
    else
484
        ipc_backsend_err(phone, call, res);
495
        ipc_backsend_err(phone, call, res);
485
 
496
 
486
    return (unative_t) call;
497
    return (unative_t) call;
487
}
498
}
488
 
499
 
489
/** Make an asynchronous IPC call allowing to transmit the entire payload.
500
/** Make an asynchronous IPC call allowing to transmit the entire payload.
490
 *
501
 *
491
 * @param phoneid   Phone handle for the call.
502
 * @param phoneid   Phone handle for the call.
492
 * @param data      Userspace address of call data with the request.
503
 * @param data      Userspace address of call data with the request.
493
 *
504
 *
494
 * @return      See sys_ipc_call_async_fast().
505
 * @return      See sys_ipc_call_async_fast().
495
 */
506
 */
496
unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data)
507
unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data)
497
{
508
{
498
    call_t *call;
509
    call_t *call;
499
    phone_t *phone;
510
    phone_t *phone;
500
    int res;
511
    int res;
501
    int rc;
512
    int rc;
502
 
513
 
503
    if (check_call_limit())
514
    if (check_call_limit())
504
        return IPC_CALLRET_TEMPORARY;
515
        return IPC_CALLRET_TEMPORARY;
505
 
516
 
506
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
517
    GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
507
 
518
 
508
    call = ipc_call_alloc(0);
519
    call = ipc_call_alloc(0);
509
    rc = copy_from_uspace(&call->data.args, &data->args,
520
    rc = copy_from_uspace(&call->data.args, &data->args,
510
        sizeof(call->data.args));
521
        sizeof(call->data.args));
511
    if (rc != 0) {
522
    if (rc != 0) {
512
        ipc_call_free(call);
523
        ipc_call_free(call);
513
        return (unative_t) rc;
524
        return (unative_t) rc;
514
    }
525
    }
515
    if (!(res = request_preprocess(call)))
526
    if (!(res = request_preprocess(call)))
516
        ipc_call(phone, call);
527
        ipc_call(phone, call);
517
    else
528
    else
518
        ipc_backsend_err(phone, call, res);
529
        ipc_backsend_err(phone, call, res);
519
 
530
 
520
    return (unative_t) call;
531
    return (unative_t) call;
521
}
532
}
522
 
533
 
523
/** Forward a received call to another destination.
534
/** Forward a received call to another destination.
524
 *
535
 *
525
 * @param callid    Hash of the call to forward.
536
 * @param callid    Hash of the call to forward.
526
 * @param phoneid   Phone handle to use for forwarding.
537
 * @param phoneid   Phone handle to use for forwarding.
527
 * @param method    New method to use for the forwarded call.
538
 * @param method    New method to use for the forwarded call.
528
 * @param arg1      New value of the first argument for the forwarded call.
539
 * @param arg1      New value of the first argument for the forwarded call.
529
 *
540
 *
530
 * @return      Return 0 on succes, otherwise return an error code.
541
 * @return      Return 0 on succes, otherwise return an error code.
531
 *
542
 *
532
 * In case the original method is a system method, ARG1 and ARG2 are overwritten
543
 * In case the original method is a system method, ARG1 and ARG2 are overwritten
533
 * in the forwarded message with the new method and the new arg1, respectively.
544
 * in the forwarded message with the new method and the new arg1, respectively.
534
 * Otherwise the METHOD and ARG1 are rewritten with the new method and arg1,
545
 * Otherwise the METHOD and ARG1 are rewritten with the new method and arg1,
535
 * respectively. Also note there is a set of immutable methods, for which the
546
 * respectively. Also note there is a set of immutable methods, for which the
536
 * new method and argument is not set and these values are ignored.
547
 * new method and argument is not set and these values are ignored.
537
 *
548
 *
538
 * Warning: If implementing non-fast version, make sure that
549
 * Warning: If implementing non-fast version, make sure that
539
 *          ARG3 is not rewritten for certain system IPC
550
 *          ARG3 is not rewritten for certain system IPC
540
 */
551
 */
541
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
552
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
542
    unative_t method, unative_t arg1)
553
    unative_t method, unative_t arg1)
543
{
554
{
544
    call_t *call;
555
    call_t *call;
545
    phone_t *phone;
556
    phone_t *phone;
546
 
557
 
547
    call = get_call(callid);
558
    call = get_call(callid);
548
    if (!call)
559
    if (!call)
549
        return ENOENT;
560
        return ENOENT;
550
 
561
 
551
    call->flags |= IPC_CALL_FORWARDED;
562
    call->flags |= IPC_CALL_FORWARDED;
552
 
563
 
553
    GET_CHECK_PHONE(phone, phoneid, {
564
    GET_CHECK_PHONE(phone, phoneid, {
554
        IPC_SET_RETVAL(call->data, EFORWARD);
565
        IPC_SET_RETVAL(call->data, EFORWARD);
555
        ipc_answer(&TASK->answerbox, call);
566
        ipc_answer(&TASK->answerbox, call);
556
        return ENOENT;
567
        return ENOENT;
557
    });    
568
    });    
558
 
569
 
559
    if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
570
    if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
560
        IPC_SET_RETVAL(call->data, EFORWARD);
571
        IPC_SET_RETVAL(call->data, EFORWARD);
561
        ipc_answer(&TASK->answerbox, call);
572
        ipc_answer(&TASK->answerbox, call);
562
        return EPERM;
573
        return EPERM;
563
    }
574
    }
564
 
575
 
565
    /*
576
    /*
566
     * Userspace is not allowed to change method of system methods on
577
     * Userspace is not allowed to change method of system methods on
567
     * forward, allow changing ARG1 and ARG2 by means of method and arg1.
578
     * forward, allow changing ARG1 and ARG2 by means of method and arg1.
568
     * If the method is immutable, don't change anything.
579
     * If the method is immutable, don't change anything.
569
     */
580
     */
570
    if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
581
    if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
571
        if (method_is_system(IPC_GET_METHOD(call->data))) {
582
        if (method_is_system(IPC_GET_METHOD(call->data))) {
572
            if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
583
            if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
573
                phone_dealloc(IPC_GET_ARG3(call->data));
584
                phone_dealloc(IPC_GET_ARG3(call->data));
574
 
585
 
575
            IPC_SET_ARG1(call->data, method);
586
            IPC_SET_ARG1(call->data, method);
576
            IPC_SET_ARG2(call->data, arg1);
587
            IPC_SET_ARG2(call->data, arg1);
577
        } else {
588
        } else {
578
            IPC_SET_METHOD(call->data, method);
589
            IPC_SET_METHOD(call->data, method);
579
            IPC_SET_ARG1(call->data, arg1);
590
            IPC_SET_ARG1(call->data, arg1);
580
        }
591
        }
581
    }
592
    }
582
 
593
 
583
    return ipc_forward(call, phone, &TASK->answerbox);
594
    return ipc_forward(call, phone, &TASK->answerbox);
584
}
595
}
585
 
596
 
586
/** Answer an IPC call - fast version.
597
/** Answer an IPC call - fast version.
587
 *
598
 *
588
 * This function can handle only two return arguments of payload, but is faster
599
 * This function can handle only two return arguments of payload, but is faster
589
 * than the generic sys_ipc_answer().
600
 * than the generic sys_ipc_answer().
590
 *
601
 *
591
 * @param callid    Hash of the call to be answered.
602
 * @param callid    Hash of the call to be answered.
592
 * @param retval    Return value of the answer.
603
 * @param retval    Return value of the answer.
593
 * @param arg1      Service-defined return value.
604
 * @param arg1      Service-defined return value.
594
 * @param arg2      Service-defined return value.
605
 * @param arg2      Service-defined return value.
595
 * @param arg3      Service-defined return value.
606
 * @param arg3      Service-defined return value.
596
 * @param arg4      Service-defined return value.
607
 * @param arg4      Service-defined return value.
597
 *
608
 *
598
 * @return      Return 0 on success, otherwise return an error code.   
609
 * @return      Return 0 on success, otherwise return an error code.   
599
 */
610
 */
600
unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
611
unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
601
    unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
612
    unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
602
{
613
{
603
    call_t *call;
614
    call_t *call;
604
    ipc_data_t saved_data;
615
    ipc_data_t saved_data;
605
    int saveddata = 0;
616
    int saveddata = 0;
606
    int rc;
617
    int rc;
607
 
618
 
608
    /* Do not answer notification callids */
619
    /* Do not answer notification callids */
609
    if (callid & IPC_CALLID_NOTIFICATION)
620
    if (callid & IPC_CALLID_NOTIFICATION)
610
        return 0;
621
        return 0;
611
 
622
 
612
    call = get_call(callid);
623
    call = get_call(callid);
613
    if (!call)
624
    if (!call)
614
        return ENOENT;
625
        return ENOENT;
615
 
626
 
616
    if (answer_need_old(call)) {
627
    if (answer_need_old(call)) {
617
        memcpy(&saved_data, &call->data, sizeof(call->data));
628
        memcpy(&saved_data, &call->data, sizeof(call->data));
618
        saveddata = 1;
629
        saveddata = 1;
619
    }
630
    }
620
 
631
 
621
    IPC_SET_RETVAL(call->data, retval);
632
    IPC_SET_RETVAL(call->data, retval);
622
    IPC_SET_ARG1(call->data, arg1);
633
    IPC_SET_ARG1(call->data, arg1);
623
    IPC_SET_ARG2(call->data, arg2);
634
    IPC_SET_ARG2(call->data, arg2);
624
    IPC_SET_ARG3(call->data, arg3);
635
    IPC_SET_ARG3(call->data, arg3);
625
    IPC_SET_ARG4(call->data, arg4);
636
    IPC_SET_ARG4(call->data, arg4);
-
 
637
    /*
-
 
638
     * To achieve deterministic behavior, zero out arguments that are beyond
-
 
639
     * the limits of the fast version.
-
 
640
     */
-
 
641
    IPC_SET_ARG5(call->data, 0);
626
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
642
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
627
 
643
 
628
    ipc_answer(&TASK->answerbox, call);
644
    ipc_answer(&TASK->answerbox, call);
629
    return rc;
645
    return rc;
630
}
646
}
631
 
647
 
632
/** Answer an IPC call.
648
/** Answer an IPC call.
633
 *
649
 *
634
 * @param callid    Hash of the call to be answered.
650
 * @param callid    Hash of the call to be answered.
635
 * @param data      Userspace address of call data with the answer.
651
 * @param data      Userspace address of call data with the answer.
636
 *
652
 *
637
 * @return      Return 0 on success, otherwise return an error code.
653
 * @return      Return 0 on success, otherwise return an error code.
638
 */
654
 */
639
unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data)
655
unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data)
640
{
656
{
641
    call_t *call;
657
    call_t *call;
642
    ipc_data_t saved_data;
658
    ipc_data_t saved_data;
643
    int saveddata = 0;
659
    int saveddata = 0;
644
    int rc;
660
    int rc;
645
 
661
 
646
    /* Do not answer notification callids */
662
    /* Do not answer notification callids */
647
    if (callid & IPC_CALLID_NOTIFICATION)
663
    if (callid & IPC_CALLID_NOTIFICATION)
648
        return 0;
664
        return 0;
649
 
665
 
650
    call = get_call(callid);
666
    call = get_call(callid);
651
    if (!call)
667
    if (!call)
652
        return ENOENT;
668
        return ENOENT;
653
 
669
 
654
    if (answer_need_old(call)) {
670
    if (answer_need_old(call)) {
655
        memcpy(&saved_data, &call->data, sizeof(call->data));
671
        memcpy(&saved_data, &call->data, sizeof(call->data));
656
        saveddata = 1;
672
        saveddata = 1;
657
    }
673
    }
658
    rc = copy_from_uspace(&call->data.args, &data->args,
674
    rc = copy_from_uspace(&call->data.args, &data->args,
659
        sizeof(call->data.args));
675
        sizeof(call->data.args));
660
    if (rc != 0)
676
    if (rc != 0)
661
        return rc;
677
        return rc;
662
 
678
 
663
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
679
    rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
664
   
680
   
665
    ipc_answer(&TASK->answerbox, call);
681
    ipc_answer(&TASK->answerbox, call);
666
 
682
 
667
    return rc;
683
    return rc;
668
}
684
}
669
 
685
 
670
/** Hang up a phone.
686
/** Hang up a phone.
671
 *
687
 *
672
 * @param       Phone handle of the phone to be hung up.
688
 * @param       Phone handle of the phone to be hung up.
673
 *
689
 *
674
 * @return      Return 0 on success or an error code.
690
 * @return      Return 0 on success or an error code.
675
 */
691
 */
676
unative_t sys_ipc_hangup(int phoneid)
692
unative_t sys_ipc_hangup(int phoneid)
677
{
693
{
678
    phone_t *phone;
694
    phone_t *phone;
679
 
695
 
680
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
696
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
681
 
697
 
682
    if (ipc_phone_hangup(phone))
698
    if (ipc_phone_hangup(phone))
683
        return -1;
699
        return -1;
684
 
700
 
685
    return 0;
701
    return 0;
686
}
702
}
687
 
703
 
688
/** Wait for an incoming IPC call or an answer.
704
/** Wait for an incoming IPC call or an answer.
689
 *
705
 *
690
 * @param calldata  Pointer to buffer where the call/answer data is stored.
706
 * @param calldata  Pointer to buffer where the call/answer data is stored.
691
 * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
707
 * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
692
 * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
708
 * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
693
 *          for explanation.
709
 *          for explanation.
694
 *
710
 *
695
 * @return      Hash of the call.
711
 * @return      Hash of the call.
696
 *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
712
 *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
697
 *          call is a notification. IPC_CALLID_ANSWERED denotes an
713
 *          call is a notification. IPC_CALLID_ANSWERED denotes an
698
 *          answer.
714
 *          answer.
699
 */
715
 */
700
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
716
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
701
{
717
{
702
    call_t *call;
718
    call_t *call;
703
 
719
 
704
restart:   
720
restart:   
705
    call = ipc_wait_for_call(&TASK->answerbox, usec,
721
    call = ipc_wait_for_call(&TASK->answerbox, usec,
706
        flags | SYNCH_FLAGS_INTERRUPTIBLE);
722
        flags | SYNCH_FLAGS_INTERRUPTIBLE);
707
    if (!call)
723
    if (!call)
708
        return 0;
724
        return 0;
709
 
725
 
710
    if (call->flags & IPC_CALL_NOTIF) {
726
    if (call->flags & IPC_CALL_NOTIF) {
711
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
727
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
712
 
728
 
713
        /* Set in_phone_hash to the interrupt counter */
729
        /* Set in_phone_hash to the interrupt counter */
714
        call->data.phone = (void *) call->priv;
730
        call->data.phone = (void *) call->priv;
715
       
731
       
716
        STRUCT_TO_USPACE(calldata, &call->data);
732
        STRUCT_TO_USPACE(calldata, &call->data);
717
 
733
 
718
        ipc_call_free(call);
734
        ipc_call_free(call);
719
       
735
       
720
        return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
736
        return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
721
    }
737
    }
722
 
738
 
723
    if (call->flags & IPC_CALL_ANSWERED) {
739
    if (call->flags & IPC_CALL_ANSWERED) {
724
        process_answer(call);
740
        process_answer(call);
725
 
741
 
726
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
742
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
727
 
743
 
728
        atomic_dec(&TASK->active_calls);
744
        atomic_dec(&TASK->active_calls);
729
 
745
 
730
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
746
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
731
            ipc_call_free(call);
747
            ipc_call_free(call);
732
            goto restart;
748
            goto restart;
733
        }
749
        }
734
 
750
 
735
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
751
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
736
        ipc_call_free(call);
752
        ipc_call_free(call);
737
 
753
 
738
        return ((unative_t) call) | IPC_CALLID_ANSWERED;
754
        return ((unative_t) call) | IPC_CALLID_ANSWERED;
739
    }
755
    }
740
 
756
 
741
    if (process_request(&TASK->answerbox, call))
757
    if (process_request(&TASK->answerbox, call))
742
        goto restart;
758
        goto restart;
743
 
759
 
744
    /* Include phone address('id') of the caller in the request,
760
    /* Include phone address('id') of the caller in the request,
745
     * copy whole call->data, not only call->data.args */
761
     * copy whole call->data, not only call->data.args */
746
    if (STRUCT_TO_USPACE(calldata, &call->data)) {
762
    if (STRUCT_TO_USPACE(calldata, &call->data)) {
747
        return 0;
763
        return 0;
748
    }
764
    }
749
    return (unative_t)call;
765
    return (unative_t)call;
750
}
766
}
751
 
767
 
752
/** Connect an IRQ handler to a task.
768
/** Connect an IRQ handler to a task.
753
 *
769
 *
754
 * @param inr       IRQ number.
770
 * @param inr       IRQ number.
755
 * @param devno     Device number.
771
 * @param devno     Device number.
756
 * @param method    Method to be associated with the notification.
772
 * @param method    Method to be associated with the notification.
757
 * @param ucode     Uspace pointer to the top-half pseudocode.
773
 * @param ucode     Uspace pointer to the top-half pseudocode.
758
 *
774
 *
759
 * @return      EPERM or a return code returned by ipc_irq_register().
775
 * @return      EPERM or a return code returned by ipc_irq_register().
760
 */
776
 */
761
unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
777
unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
762
    irq_code_t *ucode)
778
    irq_code_t *ucode)
763
{
779
{
764
    if (!(cap_get(TASK) & CAP_IRQ_REG))
780
    if (!(cap_get(TASK) & CAP_IRQ_REG))
765
        return EPERM;
781
        return EPERM;
766
 
782
 
767
    return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
783
    return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
768
}
784
}
769
 
785
 
770
/** Disconnect an IRQ handler from a task.
786
/** Disconnect an IRQ handler from a task.
771
 *
787
 *
772
 * @param inr       IRQ number.
788
 * @param inr       IRQ number.
773
 * @param devno     Device number.
789
 * @param devno     Device number.
774
 *
790
 *
775
 * @return      Zero on success or EPERM on error..
791
 * @return      Zero on success or EPERM on error..
776
 */
792
 */
777
unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
793
unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
778
{
794
{
779
    if (!(cap_get(TASK) & CAP_IRQ_REG))
795
    if (!(cap_get(TASK) & CAP_IRQ_REG))
780
        return EPERM;
796
        return EPERM;
781
 
797
 
782
    ipc_irq_unregister(&TASK->answerbox, inr, devno);
798
    ipc_irq_unregister(&TASK->answerbox, inr, devno);
783
 
799
 
784
    return 0;
800
    return 0;
785
}
801
}
786
 
802
 
787
/** @}
803
/** @}
788
 */
804
 */
789
 
805