Subversion Repositories HelenOS

Rev

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

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