Subversion Repositories HelenOS

Rev

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

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