source: mainline/generic/src/ipc/sysipc.c@ 2bb8648

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

Add SYS_CAP_GRANT and SYS_CAP_REVOKE syscalls.
Move SYS_PREEMPT_CONTROL to ddi.c.
Add some comments and fix some small issues.

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