source: mainline/generic/src/ipc/sysipc.c@ 203dcd45

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 203dcd45 was e3c762cd, checked in by Jakub Jermar <jakub@…>, 20 years ago

Complete implementation of copy_from_uspace() and copy_to_uspace()
for amd64 and ia32. Other architectures still compile and run,
but need to implement their own assembly-only memcpy(), memcpy_from_uspace(),
memcpy_to_uspace() and their failover parts. For these architectures
only dummy implementations are provided.

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