source: mainline/generic/src/ipc/sysipc.c@ 5a7d9d1

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

Add support for more then 2GB of address space for AMD64.

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