source: mainline/kernel/generic/src/ipc/sysipc.c@ cb4afd8b

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

Improve comments for the IPC subsystem.
Fix formatting and indentation.

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