Subversion Repositories HelenOS

Rev

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

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