source: mainline/generic/src/ipc/sysipc.c@ ad64a2d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ad64a2d was 7c7aae16, checked in by Ondrej Palkovsky <ondrap@…>, 20 years ago

Reduced unnecessary IPC system calls.
Allow everything to be sync & async, everything is handled using messages.

  • Property mode set to 100644
File size: 11.4 KB
RevLine 
[2d5a54f3]1/*
2 * Copyright (C) 2006 Ondrej Palkovsky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <arch.h>
30#include <proc/task.h>
31
32#include <errno.h>
33#include <mm/page.h>
34#include <memstr.h>
35#include <debug.h>
36#include <ipc/ipc.h>
37#include <ipc/sysipc.h>
[4e49572]38#include <ipc/ipcrsc.h>
[37c57f2]39
40
[2d5a54f3]41#include <print.h>
[37c57f2]42#include <arch.h>
43#include <proc/thread.h>
[2d5a54f3]44
[ba81cab]45#define GET_CHECK_PHONE(phone,phoneid,err) { \
46 if (phoneid > IPC_MAX_PHONES) { err; } \
47 phone = &TASK->phones[phoneid]; \
48}
49
[7c7aae16]50#define STRUCT_TO_USPACE(dst,src) copy_to_uspace(dst,src,sizeof(*(src)))
[ba81cab]51
[2ba7810]52/** Return true if the method is a system method */
53static inline int is_system_method(__native method)
54{
55 if (method <= IPC_M_LAST_SYSTEM)
56 return 1;
57 return 0;
58}
59
60/** Return true if the message with this method is forwardable
61 *
62 * - some system messages may be forwarded, for some of them
63 * it is useless
64 */
65static inline int is_forwardable(__native method)
66{
[fbcfd458]67 if (method == IPC_M_PHONE_HUNGUP)
68 return 0; /* This message is meant only for the receiver */
[2ba7810]69 return 1;
70}
71
[2d5a54f3]72/****************************************************/
73/* Functions that preprocess answer before sending
74 * it to the recepient
75 */
76
77/** Return true if the caller (ipc_answer) should save
[9f22213]78 * the old call contents for answer_preprocess
[2d5a54f3]79 */
[9f22213]80static inline int answer_need_old(call_t *call)
[2d5a54f3]81{
[fbcfd458]82 if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
[2d5a54f3]83 return 1;
[fbcfd458]84 if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_ME_TO)
[37c57f2]85 return 1;
[2d5a54f3]86 return 0;
87}
88
89/** Interpret process answer as control information */
[2ba7810]90static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata)
[2d5a54f3]91{
92 int phoneid;
93
[9f22213]94 if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
95 /* Atomic operation */
96 answer->data.phone->callee = NULL;
97 }
98
99 if (!olddata)
100 return;
101
[fbcfd458]102 if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
[2ba7810]103 phoneid = IPC_GET_ARG3(*olddata);
[2d5a54f3]104 if (IPC_GET_RETVAL(answer->data)) {
105 /* The connection was not accepted */
106 phone_dealloc(phoneid);
[2ba7810]107 } else {
[7c7aae16]108 /* The connection was accepted */
[2ba7810]109 phone_connect(phoneid,&answer->sender->answerbox);
[7c7aae16]110 /* Set 'phone identification' as arg3 of response */
111 IPC_SET_ARG3(answer->data, (__native)&TASK->phones[phoneid]);
[2d5a54f3]112 }
[fbcfd458]113 } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
[37c57f2]114 /* If the users accepted call, connect */
115 if (!IPC_GET_RETVAL(answer->data)) {
116 ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata),
117 &TASK->answerbox);
118 }
[2d5a54f3]119 }
120}
121
[7c7aae16]122/** Called before the request is sent
123 *
124 * @return 0 - no error, -1 - report error to user
125 */
126static int request_preprocess(call_t *call)
127{
128 int newphid;
129
130 switch (IPC_GET_METHOD(call->data)) {
131 case IPC_M_CONNECT_ME_TO:
132 newphid = phone_alloc();
133 if (newphid < 0)
134 return ELIMIT;
135 /* Set arg3 for server */
136 IPC_SET_ARG3(call->data, (__native)&TASK->phones[newphid]);
137 call->flags |= IPC_CALL_CONN_ME_TO;
138 call->private = newphid;
139 break;
140 default:
141 break;
142 }
143 return 0;
144}
145
[2d5a54f3]146/****************************************************/
147/* Functions called to process received call/answer
148 * before passing to uspace
149 */
150
151/** Do basic kernel processing of received call answer */
[7c7aae16]152static void process_answer(call_t *call)
[2d5a54f3]153{
[9f22213]154 if (IPC_GET_RETVAL(call->data) == EHANGUP && \
155 call->flags & IPC_CALL_FORWARDED)
156 IPC_SET_RETVAL(call->data, EFORWARD);
[7c7aae16]157
158 if (call->flags & IPC_CALL_CONN_ME_TO) {
159 if (IPC_GET_RETVAL(call->data))
160 phone_dealloc(call->private);
161 else
162 IPC_SET_ARG3(call->data, call->private);
163 }
[2d5a54f3]164}
165
166/** Do basic kernel processing of received call request
167 *
168 * @return 0 - the call should be passed to userspace, 1 - ignore call
169 */
170static int process_request(answerbox_t *box,call_t *call)
171{
172 int phoneid;
173
[fbcfd458]174 if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
[2ba7810]175 phoneid = phone_alloc();
[2d5a54f3]176 if (phoneid < 0) { /* Failed to allocate phone */
177 IPC_SET_RETVAL(call->data, ELIMIT);
178 ipc_answer(box,call);
179 return -1;
180 }
181 IPC_SET_ARG3(call->data, phoneid);
182 }
183 return 0;
184}
185
186/** Send a call over IPC, wait for reply, return to user
187 *
188 * @return Call identification, returns -1 on fatal error,
189 -2 on 'Too many async request, handle answers first
190 */
191__native sys_ipc_call_sync_fast(__native phoneid, __native method,
[fbcfd458]192 __native arg1, ipc_data_t *data)
[2d5a54f3]193{
194 call_t call;
195 phone_t *phone;
[7c7aae16]196 int res;
[2d5a54f3]197
[ba81cab]198 GET_CHECK_PHONE(phone, phoneid, return ENOENT);
[4e49572]199
[ba81cab]200 ipc_call_static_init(&call);
[2d5a54f3]201 IPC_SET_METHOD(call.data, method);
202 IPC_SET_ARG1(call.data, arg1);
203
[7c7aae16]204 if (!(res=request_preprocess(&call))) {
205 ipc_call_sync(phone, &call);
206 process_answer(&call);
207 } else
208 IPC_SET_RETVAL(call.data, res);
[fbcfd458]209 STRUCT_TO_USPACE(&data->args, &call.data.args);
[2d5a54f3]210
211 return 0;
212}
213
214/** Synchronous IPC call allowing to send whole message */
[fbcfd458]215__native sys_ipc_call_sync(__native phoneid, ipc_data_t *question,
216 ipc_data_t *reply)
[2d5a54f3]217{
218 call_t call;
219 phone_t *phone;
[7c7aae16]220 int res;
[2d5a54f3]221
[ba81cab]222 ipc_call_static_init(&call);
[fbcfd458]223 copy_from_uspace(&call.data.args, &question->args, sizeof(call.data.args));
[2ba7810]224
[ba81cab]225 GET_CHECK_PHONE(phone, phoneid, return ENOENT);
[4e49572]226
[7c7aae16]227 if (!(res=request_preprocess(&call))) {
228 ipc_call_sync(phone, &call);
229 process_answer(&call);
230 } else
231 IPC_SET_RETVAL(call.data, res);
[2d5a54f3]232
[fbcfd458]233 STRUCT_TO_USPACE(&reply->args, &call.data.args);
[2d5a54f3]234
235 return 0;
236}
237
238/** Check that the task did not exceed allowed limit
239 *
240 * @return 0 - Limit OK, -1 - limit exceeded
241 */
242static int check_call_limit(void)
243{
244 if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
245 atomic_dec(&TASK->active_calls);
246 return -1;
247 }
248 return 0;
249}
250
251/** Send an asynchronous call over ipc
252 *
253 * @return Call identification, returns -1 on fatal error,
254 -2 on 'Too many async request, handle answers first
255 */
256__native sys_ipc_call_async_fast(__native phoneid, __native method,
257 __native arg1, __native arg2)
258{
259 call_t *call;
260 phone_t *phone;
[7c7aae16]261 int res;
[2ba7810]262
[2d5a54f3]263 if (check_call_limit())
264 return IPC_CALLRET_TEMPORARY;
265
[7c7aae16]266 GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
[4e49572]267
[2d5a54f3]268 call = ipc_call_alloc();
269 IPC_SET_METHOD(call->data, method);
270 IPC_SET_ARG1(call->data, arg1);
271 IPC_SET_ARG2(call->data, arg2);
272
[7c7aae16]273 if (!(res=request_preprocess(call)))
274 ipc_call(phone, call);
275 else
276 ipc_backsend_err(phone, call, res);
[2d5a54f3]277
278 return (__native) call;
279}
280
281/** Synchronous IPC call allowing to send whole message
282 *
283 * @return The same as sys_ipc_call_async
284 */
[fbcfd458]285__native sys_ipc_call_async(__native phoneid, ipc_data_t *data)
[2d5a54f3]286{
287 call_t *call;
288 phone_t *phone;
[7c7aae16]289 int res;
[2d5a54f3]290
291 if (check_call_limit())
292 return IPC_CALLRET_TEMPORARY;
293
[7c7aae16]294 GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
[4e49572]295
[2d5a54f3]296 call = ipc_call_alloc();
[fbcfd458]297 copy_from_uspace(&call->data.args, &data->args, sizeof(call->data.args));
[7c7aae16]298 if (!(res=request_preprocess(call)))
299 ipc_call(phone, call);
300 else
301 ipc_backsend_err(phone, call, res);
[2d5a54f3]302
303 return (__native) call;
304}
305
[2ba7810]306/** Forward received call to another destination
307 *
308 * The arg1 and arg2 are changed in the forwarded message
[37c57f2]309 *
310 * Warning: If implementing non-fast version, make sure that
311 * arg3 is not rewritten for certain system IPC
[2ba7810]312 */
313__native sys_ipc_forward_fast(__native callid, __native phoneid,
314 __native method, __native arg1)
315{
316 call_t *call;
317 phone_t *phone;
318
319 call = get_call(callid);
320 if (!call)
321 return ENOENT;
322
[9f22213]323 call->flags |= IPC_CALL_FORWARDED;
324
[ba81cab]325 GET_CHECK_PHONE(phone, phoneid, {
[2ba7810]326 IPC_SET_RETVAL(call->data, EFORWARD);
327 ipc_answer(&TASK->answerbox, call);
328 return ENOENT;
[ba81cab]329 });
[2ba7810]330
331 if (!is_forwardable(IPC_GET_METHOD(call->data))) {
332 IPC_SET_RETVAL(call->data, EFORWARD);
333 ipc_answer(&TASK->answerbox, call);
334 return EPERM;
335 }
336
337 /* Userspace is not allowed to change method of system methods
338 * on forward, allow changing ARG1 and ARG2 by means of method and arg1
339 */
340 if (is_system_method(IPC_GET_METHOD(call->data))) {
[7c7aae16]341 if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
342 phone_dealloc(IPC_GET_ARG3(call->data));
343
[2ba7810]344 IPC_SET_ARG1(call->data, method);
345 IPC_SET_ARG2(call->data, arg1);
346 } else {
347 IPC_SET_METHOD(call->data, method);
348 IPC_SET_ARG1(call->data, arg1);
349 }
350
[9f22213]351 return ipc_forward(call, phone, &TASK->answerbox);
[2ba7810]352}
353
[2d5a54f3]354/** Send IPC answer */
355__native sys_ipc_answer_fast(__native callid, __native retval,
356 __native arg1, __native arg2)
357{
358 call_t *call;
[2ba7810]359 ipc_data_t saved_data;
[9f22213]360 int saveddata = 0;
[2d5a54f3]361
[2ba7810]362 call = get_call(callid);
363 if (!call)
364 return ENOENT;
[2d5a54f3]365
[9f22213]366 if (answer_need_old(call)) {
[2ba7810]367 memcpy(&saved_data, &call->data, sizeof(call->data));
[9f22213]368 saveddata = 1;
[2d5a54f3]369 }
370
371 IPC_SET_RETVAL(call->data, retval);
372 IPC_SET_ARG1(call->data, arg1);
373 IPC_SET_ARG2(call->data, arg2);
[9f22213]374 answer_preprocess(call, saveddata ? &saved_data : NULL);
[2d5a54f3]375
376 ipc_answer(&TASK->answerbox, call);
377 return 0;
378}
379
380/** Send IPC answer */
[fbcfd458]381__native sys_ipc_answer(__native callid, ipc_data_t *data)
[2d5a54f3]382{
383 call_t *call;
[2ba7810]384 ipc_data_t saved_data;
[9f22213]385 int saveddata = 0;
[2d5a54f3]386
[2ba7810]387 call = get_call(callid);
388 if (!call)
389 return ENOENT;
[2d5a54f3]390
[9f22213]391 if (answer_need_old(call)) {
[2ba7810]392 memcpy(&saved_data, &call->data, sizeof(call->data));
[9f22213]393 saveddata = 1;
[2d5a54f3]394 }
[fbcfd458]395 copy_from_uspace(&call->data.args, &data->args,
396 sizeof(call->data.args));
[2d5a54f3]397
[9f22213]398 answer_preprocess(call, saveddata ? &saved_data : NULL);
[2d5a54f3]399
400 ipc_answer(&TASK->answerbox, call);
401
402 return 0;
403}
404
[fbcfd458]405/** Hang up the phone
[2d5a54f3]406 *
[fbcfd458]407 */
408__native sys_ipc_hangup(int phoneid)
409{
410 phone_t *phone;
411
412 GET_CHECK_PHONE(phone, phoneid, return ENOENT);
413
414 if (ipc_phone_hangup(phone))
415 return -1;
416
417 return 0;
418}
419
420/** Wait for incoming ipc call or answer
421 *
422 * @param calldata Pointer to buffer where the call/answer data is stored
423 * @param taskid On 'CONNECT_ME_TO' call it is filled with 'taskid' of
424 * the caller.
[2d5a54f3]425 * @param flags
426 * @return Callid, if callid & 1, then the call is answer
427 */
[7c7aae16]428__native sys_ipc_wait_for_call(ipc_data_t *calldata, __native flags)
[2d5a54f3]429{
430 call_t *call;
431
432restart:
433 call = ipc_wait_for_call(&TASK->answerbox, flags);
[9f22213]434 if (!call)
435 return 0;
[2d5a54f3]436
437 if (call->flags & IPC_CALL_ANSWERED) {
[7c7aae16]438 process_answer(call);
[2d5a54f3]439
440 ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
[fbcfd458]441
442 atomic_dec(&TASK->active_calls);
443
444 if (call->flags & IPC_CALL_DISCARD_ANSWER) {
445 ipc_call_free(call);
446 goto restart;
447 }
448
449 STRUCT_TO_USPACE(&calldata->args, &call->data.args);
[2d5a54f3]450 ipc_call_free(call);
451
452 return ((__native)call) | IPC_CALLID_ANSWERED;
453 }
[fbcfd458]454
[2d5a54f3]455 if (process_request(&TASK->answerbox, call))
456 goto restart;
[fbcfd458]457
[7c7aae16]458 /* Include phone address('id') of the caller in the request,
459 * copy whole call->data, not only call->data.args */
[fbcfd458]460 STRUCT_TO_USPACE(calldata, &call->data);
[2d5a54f3]461 return (__native)call;
462}
Note: See TracBrowser for help on using the repository browser.