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 | /* Lock ordering
|
---|
36 | *
|
---|
37 | * First the answerbox, then the phone.
|
---|
38 | */
|
---|
39 |
|
---|
40 | #include <assert.h>
|
---|
41 | #include <synch/spinlock.h>
|
---|
42 | #include <synch/mutex.h>
|
---|
43 | #include <synch/waitq.h>
|
---|
44 | #include <ipc/ipc.h>
|
---|
45 | #include <ipc/ipcrsc.h>
|
---|
46 | #include <abi/ipc/methods.h>
|
---|
47 | #include <ipc/kbox.h>
|
---|
48 | #include <ipc/event.h>
|
---|
49 | #include <ipc/sysipc_ops.h>
|
---|
50 | #include <ipc/sysipc_priv.h>
|
---|
51 | #include <errno.h>
|
---|
52 | #include <mm/slab.h>
|
---|
53 | #include <arch.h>
|
---|
54 | #include <proc/task.h>
|
---|
55 | #include <mem.h>
|
---|
56 | #include <print.h>
|
---|
57 | #include <console/console.h>
|
---|
58 | #include <proc/thread.h>
|
---|
59 | #include <arch/interrupt.h>
|
---|
60 | #include <ipc/irq.h>
|
---|
61 | #include <cap/cap.h>
|
---|
62 |
|
---|
63 | static void ipc_forget_call(call_t *);
|
---|
64 |
|
---|
65 | /** Answerbox that new tasks are automatically connected to */
|
---|
66 | answerbox_t *ipc_box_0 = NULL;
|
---|
67 |
|
---|
68 | static slab_cache_t *call_cache;
|
---|
69 | static slab_cache_t *answerbox_cache;
|
---|
70 |
|
---|
71 | slab_cache_t *phone_cache = NULL;
|
---|
72 |
|
---|
73 | /** Initialize a call structure.
|
---|
74 | *
|
---|
75 | * @param call Call structure to be initialized.
|
---|
76 | *
|
---|
77 | */
|
---|
78 | static void _ipc_call_init(call_t *call)
|
---|
79 | {
|
---|
80 | memsetb(call, sizeof(*call), 0);
|
---|
81 | spinlock_initialize(&call->forget_lock, "forget_lock");
|
---|
82 | call->active = false;
|
---|
83 | call->forget = false;
|
---|
84 | call->sender = NULL;
|
---|
85 | call->callerbox = NULL;
|
---|
86 | call->buffer = NULL;
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void call_destroy(void *arg)
|
---|
90 | {
|
---|
91 | call_t *call = (call_t *) arg;
|
---|
92 |
|
---|
93 | if (call->buffer)
|
---|
94 | free(call->buffer);
|
---|
95 | if (call->caller_phone)
|
---|
96 | kobject_put(call->caller_phone->kobject);
|
---|
97 | slab_free(call_cache, call);
|
---|
98 | }
|
---|
99 |
|
---|
100 | static kobject_ops_t call_kobject_ops = {
|
---|
101 | .destroy = call_destroy
|
---|
102 | };
|
---|
103 |
|
---|
104 | /** Allocate and initialize a call structure.
|
---|
105 | *
|
---|
106 | * The call is initialized, so that the reply will be directed to
|
---|
107 | * TASK->answerbox.
|
---|
108 | *
|
---|
109 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
|
---|
110 | *
|
---|
111 | * @return If flags permit it, return NULL, or initialized kernel
|
---|
112 | * call structure with one reference.
|
---|
113 | *
|
---|
114 | */
|
---|
115 | call_t *ipc_call_alloc(unsigned int flags)
|
---|
116 | {
|
---|
117 | call_t *call = slab_alloc(call_cache, flags);
|
---|
118 | if (!call)
|
---|
119 | return NULL;
|
---|
120 | kobject_t *kobj = (kobject_t *) malloc(sizeof(kobject_t), flags);
|
---|
121 | if (!kobj) {
|
---|
122 | slab_free(call_cache, call);
|
---|
123 | return NULL;
|
---|
124 | }
|
---|
125 |
|
---|
126 | _ipc_call_init(call);
|
---|
127 | kobject_initialize(kobj, KOBJECT_TYPE_CALL, call, &call_kobject_ops);
|
---|
128 | call->kobject = kobj;
|
---|
129 |
|
---|
130 | return call;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Initialize an answerbox structure.
|
---|
134 | *
|
---|
135 | * @param box Answerbox structure to be initialized.
|
---|
136 | * @param task Task to which the answerbox belongs.
|
---|
137 | *
|
---|
138 | */
|
---|
139 | void ipc_answerbox_init(answerbox_t *box, task_t *task)
|
---|
140 | {
|
---|
141 | irq_spinlock_initialize(&box->lock, "ipc.box.lock");
|
---|
142 | irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
|
---|
143 | waitq_initialize(&box->wq);
|
---|
144 | list_initialize(&box->connected_phones);
|
---|
145 | list_initialize(&box->calls);
|
---|
146 | list_initialize(&box->dispatched_calls);
|
---|
147 | list_initialize(&box->answers);
|
---|
148 | list_initialize(&box->irq_notifs);
|
---|
149 | atomic_set(&box->active_calls, 0);
|
---|
150 | box->task = task;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /** Connect a phone to an answerbox.
|
---|
154 | *
|
---|
155 | * This function must be passed a reference to phone->kobject.
|
---|
156 | *
|
---|
157 | * @param phone Initialized phone structure.
|
---|
158 | * @param box Initialized answerbox structure.
|
---|
159 | * @return True if the phone was connected, false otherwise.
|
---|
160 | */
|
---|
161 | bool ipc_phone_connect(phone_t *phone, answerbox_t *box)
|
---|
162 | {
|
---|
163 | bool connected;
|
---|
164 |
|
---|
165 | mutex_lock(&phone->lock);
|
---|
166 | irq_spinlock_lock(&box->lock, true);
|
---|
167 |
|
---|
168 | connected = box->active && (phone->state == IPC_PHONE_CONNECTING);
|
---|
169 | if (connected) {
|
---|
170 | phone->state = IPC_PHONE_CONNECTED;
|
---|
171 | phone->callee = box;
|
---|
172 | /* Pass phone->kobject reference to box->connected_phones */
|
---|
173 | list_append(&phone->link, &box->connected_phones);
|
---|
174 | }
|
---|
175 |
|
---|
176 | irq_spinlock_unlock(&box->lock, true);
|
---|
177 | mutex_unlock(&phone->lock);
|
---|
178 |
|
---|
179 | if (!connected) {
|
---|
180 | /* We still have phone->kobject's reference; drop it */
|
---|
181 | kobject_put(phone->kobject);
|
---|
182 | }
|
---|
183 |
|
---|
184 | return connected;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Initialize a phone structure.
|
---|
188 | *
|
---|
189 | * @param phone Phone structure to be initialized.
|
---|
190 | * @param caller Owning task.
|
---|
191 | *
|
---|
192 | */
|
---|
193 | void ipc_phone_init(phone_t *phone, task_t *caller)
|
---|
194 | {
|
---|
195 | mutex_initialize(&phone->lock, MUTEX_PASSIVE);
|
---|
196 | phone->caller = caller;
|
---|
197 | phone->callee = NULL;
|
---|
198 | phone->state = IPC_PHONE_FREE;
|
---|
199 | atomic_set(&phone->active_calls, 0);
|
---|
200 | phone->kobject = NULL;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /** Helper function to facilitate synchronous calls.
|
---|
204 | *
|
---|
205 | * @param phone Destination kernel phone structure.
|
---|
206 | * @param request Call structure with request.
|
---|
207 | *
|
---|
208 | * @return EOK on success or an error code.
|
---|
209 | *
|
---|
210 | */
|
---|
211 | errno_t ipc_call_sync(phone_t *phone, call_t *request)
|
---|
212 | {
|
---|
213 | answerbox_t *mybox = slab_alloc(answerbox_cache, 0);
|
---|
214 | ipc_answerbox_init(mybox, TASK);
|
---|
215 |
|
---|
216 | /* We will receive data in a special box. */
|
---|
217 | request->callerbox = mybox;
|
---|
218 |
|
---|
219 | errno_t rc = ipc_call(phone, request);
|
---|
220 | if (rc != EOK) {
|
---|
221 | slab_free(answerbox_cache, mybox);
|
---|
222 | return rc;
|
---|
223 | }
|
---|
224 |
|
---|
225 | call_t *answer = ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
|
---|
226 | SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
227 | if (!answer) {
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * The sleep was interrupted.
|
---|
231 | *
|
---|
232 | * There are two possibilities now:
|
---|
233 | * 1) the call gets answered before we manage to forget it
|
---|
234 | * 2) we manage to forget the call before it gets answered
|
---|
235 | */
|
---|
236 |
|
---|
237 | spinlock_lock(&request->forget_lock);
|
---|
238 | spinlock_lock(&TASK->active_calls_lock);
|
---|
239 |
|
---|
240 | assert(!request->forget);
|
---|
241 |
|
---|
242 | bool answered = !request->active;
|
---|
243 | if (!answered) {
|
---|
244 | /*
|
---|
245 | * The call is not yet answered and we won the race to
|
---|
246 | * forget it.
|
---|
247 | */
|
---|
248 | ipc_forget_call(request); /* releases locks */
|
---|
249 | rc = EINTR;
|
---|
250 |
|
---|
251 | } else {
|
---|
252 | spinlock_unlock(&TASK->active_calls_lock);
|
---|
253 | spinlock_unlock(&request->forget_lock);
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (answered) {
|
---|
257 | /*
|
---|
258 | * The other side won the race to answer the call.
|
---|
259 | * It is safe to wait for the answer uninterruptibly
|
---|
260 | * now.
|
---|
261 | */
|
---|
262 | answer = ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
|
---|
263 | SYNCH_FLAGS_NONE);
|
---|
264 | }
|
---|
265 | }
|
---|
266 | assert(!answer || request == answer);
|
---|
267 |
|
---|
268 | slab_free(answerbox_cache, mybox);
|
---|
269 | return rc;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /** Answer a message which was not dispatched and is not listed in any queue.
|
---|
273 | *
|
---|
274 | * @param call Call structure to be answered.
|
---|
275 | * @param selflocked If true, then TASK->answebox is locked.
|
---|
276 | *
|
---|
277 | */
|
---|
278 | void _ipc_answer_free_call(call_t *call, bool selflocked)
|
---|
279 | {
|
---|
280 | /* Count sent answer */
|
---|
281 | irq_spinlock_lock(&TASK->lock, true);
|
---|
282 | TASK->ipc_info.answer_sent++;
|
---|
283 | irq_spinlock_unlock(&TASK->lock, true);
|
---|
284 |
|
---|
285 | spinlock_lock(&call->forget_lock);
|
---|
286 | if (call->forget) {
|
---|
287 | /* This is a forgotten call and call->sender is not valid. */
|
---|
288 | spinlock_unlock(&call->forget_lock);
|
---|
289 | kobject_put(call->kobject);
|
---|
290 | return;
|
---|
291 | } else {
|
---|
292 | /*
|
---|
293 | * If the call is still active, i.e. it was answered
|
---|
294 | * in a non-standard way, remove the call from the
|
---|
295 | * sender's active call list.
|
---|
296 | */
|
---|
297 | if (call->active) {
|
---|
298 | spinlock_lock(&call->sender->active_calls_lock);
|
---|
299 | list_remove(&call->ta_link);
|
---|
300 | spinlock_unlock(&call->sender->active_calls_lock);
|
---|
301 | }
|
---|
302 | }
|
---|
303 | spinlock_unlock(&call->forget_lock);
|
---|
304 |
|
---|
305 | answerbox_t *callerbox = call->callerbox ? call->callerbox :
|
---|
306 | &call->sender->answerbox;
|
---|
307 | bool do_lock = ((!selflocked) || (callerbox != &TASK->answerbox));
|
---|
308 |
|
---|
309 | call->flags |= IPC_CALL_ANSWERED;
|
---|
310 |
|
---|
311 | call->data.task_id = TASK->taskid;
|
---|
312 |
|
---|
313 | if (do_lock)
|
---|
314 | irq_spinlock_lock(&callerbox->lock, true);
|
---|
315 |
|
---|
316 | list_append(&call->ab_link, &callerbox->answers);
|
---|
317 |
|
---|
318 | if (do_lock)
|
---|
319 | irq_spinlock_unlock(&callerbox->lock, true);
|
---|
320 |
|
---|
321 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
|
---|
322 | }
|
---|
323 |
|
---|
324 | /** Answer a message which is in a callee queue.
|
---|
325 | *
|
---|
326 | * @param box Answerbox that is answering the message.
|
---|
327 | * @param call Modified request that is being sent back.
|
---|
328 | *
|
---|
329 | */
|
---|
330 | void ipc_answer(answerbox_t *box, call_t *call)
|
---|
331 | {
|
---|
332 | /* Remove from active box */
|
---|
333 | irq_spinlock_lock(&box->lock, true);
|
---|
334 | list_remove(&call->ab_link);
|
---|
335 | irq_spinlock_unlock(&box->lock, true);
|
---|
336 |
|
---|
337 | /* Send back answer */
|
---|
338 | _ipc_answer_free_call(call, false);
|
---|
339 | }
|
---|
340 |
|
---|
341 | static void _ipc_call_actions_internal(phone_t *phone, call_t *call,
|
---|
342 | bool preforget)
|
---|
343 | {
|
---|
344 | task_t *caller = phone->caller;
|
---|
345 |
|
---|
346 | call->caller_phone = phone;
|
---|
347 | kobject_add_ref(phone->kobject);
|
---|
348 |
|
---|
349 | if (preforget) {
|
---|
350 | call->forget = true;
|
---|
351 | } else {
|
---|
352 | atomic_inc(&phone->active_calls);
|
---|
353 | atomic_inc(&caller->answerbox.active_calls);
|
---|
354 | kobject_add_ref(phone->kobject);
|
---|
355 | call->sender = caller;
|
---|
356 | call->active = true;
|
---|
357 | spinlock_lock(&caller->active_calls_lock);
|
---|
358 | list_append(&call->ta_link, &caller->active_calls);
|
---|
359 | spinlock_unlock(&caller->active_calls_lock);
|
---|
360 | }
|
---|
361 |
|
---|
362 | call->data.phone = phone;
|
---|
363 | call->data.task_id = caller->taskid;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /** Simulate sending back a message.
|
---|
367 | *
|
---|
368 | * Most errors are better handled by forming a normal backward
|
---|
369 | * message and sending it as a normal answer.
|
---|
370 | *
|
---|
371 | * @param phone Phone structure the call should appear to come from.
|
---|
372 | * @param call Call structure to be answered.
|
---|
373 | * @param err Return value to be used for the answer.
|
---|
374 | *
|
---|
375 | */
|
---|
376 | void ipc_backsend_err(phone_t *phone, call_t *call, errno_t err)
|
---|
377 | {
|
---|
378 | _ipc_call_actions_internal(phone, call, false);
|
---|
379 | IPC_SET_RETVAL(call->data, err);
|
---|
380 | _ipc_answer_free_call(call, false);
|
---|
381 | }
|
---|
382 |
|
---|
383 | /** Unsafe unchecking version of ipc_call.
|
---|
384 | *
|
---|
385 | * @param phone Phone structure the call comes from.
|
---|
386 | * @param box Destination answerbox structure.
|
---|
387 | * @param call Call structure with request.
|
---|
388 | * @param preforget If true, the call will be delivered already forgotten.
|
---|
389 | *
|
---|
390 | */
|
---|
391 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call,
|
---|
392 | bool preforget)
|
---|
393 | {
|
---|
394 | task_t *caller = phone->caller;
|
---|
395 |
|
---|
396 | /* Count sent ipc call */
|
---|
397 | irq_spinlock_lock(&caller->lock, true);
|
---|
398 | caller->ipc_info.call_sent++;
|
---|
399 | irq_spinlock_unlock(&caller->lock, true);
|
---|
400 |
|
---|
401 | if (!(call->flags & IPC_CALL_FORWARDED))
|
---|
402 | _ipc_call_actions_internal(phone, call, preforget);
|
---|
403 |
|
---|
404 | irq_spinlock_lock(&box->lock, true);
|
---|
405 | list_append(&call->ab_link, &box->calls);
|
---|
406 | irq_spinlock_unlock(&box->lock, true);
|
---|
407 |
|
---|
408 | waitq_wakeup(&box->wq, WAKEUP_FIRST);
|
---|
409 | }
|
---|
410 |
|
---|
411 | /** Send an asynchronous request using a phone to an answerbox.
|
---|
412 | *
|
---|
413 | * @param phone Phone structure the call comes from and which is
|
---|
414 | * connected to the destination answerbox.
|
---|
415 | * @param call Call structure with request.
|
---|
416 | *
|
---|
417 | * @return Return 0 on success, ENOENT on error.
|
---|
418 | *
|
---|
419 | */
|
---|
420 | errno_t ipc_call(phone_t *phone, call_t *call)
|
---|
421 | {
|
---|
422 | mutex_lock(&phone->lock);
|
---|
423 | if (phone->state != IPC_PHONE_CONNECTED) {
|
---|
424 | mutex_unlock(&phone->lock);
|
---|
425 | if (!(call->flags & IPC_CALL_FORWARDED)) {
|
---|
426 | if (phone->state == IPC_PHONE_HUNGUP)
|
---|
427 | ipc_backsend_err(phone, call, EHANGUP);
|
---|
428 | else
|
---|
429 | ipc_backsend_err(phone, call, ENOENT);
|
---|
430 | }
|
---|
431 |
|
---|
432 | return ENOENT;
|
---|
433 | }
|
---|
434 |
|
---|
435 | answerbox_t *box = phone->callee;
|
---|
436 | _ipc_call(phone, box, call, false);
|
---|
437 |
|
---|
438 | mutex_unlock(&phone->lock);
|
---|
439 | return 0;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /** Disconnect phone from answerbox.
|
---|
443 | *
|
---|
444 | * This call leaves the phone in the hung-up state. The phone is destroyed when
|
---|
445 | * its last active call is answered and there are no references to it.
|
---|
446 | *
|
---|
447 | * @param phone Phone structure to be hung up.
|
---|
448 | *
|
---|
449 | * @return EOK if the phone is disconnected.
|
---|
450 | * @return EINVAL if the phone was already disconnected.
|
---|
451 | *
|
---|
452 | */
|
---|
453 | errno_t ipc_phone_hangup(phone_t *phone)
|
---|
454 | {
|
---|
455 | mutex_lock(&phone->lock);
|
---|
456 | if (phone->state == IPC_PHONE_FREE ||
|
---|
457 | phone->state == IPC_PHONE_HUNGUP ||
|
---|
458 | phone->state == IPC_PHONE_CONNECTING) {
|
---|
459 | mutex_unlock(&phone->lock);
|
---|
460 | return EINVAL;
|
---|
461 | }
|
---|
462 |
|
---|
463 | answerbox_t *box = phone->callee;
|
---|
464 | if (phone->state != IPC_PHONE_SLAMMED) {
|
---|
465 | /* Remove myself from answerbox */
|
---|
466 | irq_spinlock_lock(&box->lock, true);
|
---|
467 | list_remove(&phone->link);
|
---|
468 | irq_spinlock_unlock(&box->lock, true);
|
---|
469 |
|
---|
470 | /* Drop the answerbox reference */
|
---|
471 | kobject_put(phone->kobject);
|
---|
472 |
|
---|
473 | call_t *call = ipc_call_alloc(0);
|
---|
474 | IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
|
---|
475 | call->request_method = IPC_M_PHONE_HUNGUP;
|
---|
476 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
---|
477 | _ipc_call(phone, box, call, false);
|
---|
478 | }
|
---|
479 |
|
---|
480 | phone->state = IPC_PHONE_HUNGUP;
|
---|
481 | mutex_unlock(&phone->lock);
|
---|
482 |
|
---|
483 | return EOK;
|
---|
484 | }
|
---|
485 |
|
---|
486 | /** Forwards call from one answerbox to another one.
|
---|
487 | *
|
---|
488 | * @param call Call structure to be redirected.
|
---|
489 | * @param newphone Phone structure to target answerbox.
|
---|
490 | * @param oldbox Old answerbox structure.
|
---|
491 | * @param mode Flags that specify mode of the forward operation.
|
---|
492 | *
|
---|
493 | * @return 0 if forwarding succeeded or an error code if
|
---|
494 | * there was an error.
|
---|
495 | *
|
---|
496 | * The return value serves only as an information for the forwarder,
|
---|
497 | * the original caller is notified automatically with EFORWARD.
|
---|
498 | *
|
---|
499 | */
|
---|
500 | errno_t ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
|
---|
501 | unsigned int mode)
|
---|
502 | {
|
---|
503 | /* Count forwarded calls */
|
---|
504 | irq_spinlock_lock(&TASK->lock, true);
|
---|
505 | TASK->ipc_info.forwarded++;
|
---|
506 | irq_spinlock_pass(&TASK->lock, &oldbox->lock);
|
---|
507 | list_remove(&call->ab_link);
|
---|
508 | irq_spinlock_unlock(&oldbox->lock, true);
|
---|
509 |
|
---|
510 | if (mode & IPC_FF_ROUTE_FROM_ME) {
|
---|
511 | call->data.phone = newphone;
|
---|
512 | call->data.task_id = TASK->taskid;
|
---|
513 | }
|
---|
514 |
|
---|
515 | return ipc_call(newphone, call);
|
---|
516 | }
|
---|
517 |
|
---|
518 |
|
---|
519 | /** Wait for a phone call.
|
---|
520 | *
|
---|
521 | * @param box Answerbox expecting the call.
|
---|
522 | * @param usec Timeout in microseconds. See documentation for
|
---|
523 | * waitq_sleep_timeout() for decription of its special
|
---|
524 | * meaning.
|
---|
525 | * @param flags Select mode of sleep operation. See documentation for
|
---|
526 | * waitq_sleep_timeout() for description of its special
|
---|
527 | * meaning.
|
---|
528 | *
|
---|
529 | * @return Recived call structure or NULL.
|
---|
530 | *
|
---|
531 | * To distinguish between a call and an answer, have a look at call->flags.
|
---|
532 | *
|
---|
533 | */
|
---|
534 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
|
---|
535 | {
|
---|
536 | call_t *request;
|
---|
537 | uint64_t irq_cnt = 0;
|
---|
538 | uint64_t answer_cnt = 0;
|
---|
539 | uint64_t call_cnt = 0;
|
---|
540 | errno_t rc;
|
---|
541 |
|
---|
542 | restart:
|
---|
543 | rc = waitq_sleep_timeout(&box->wq, usec, flags, NULL);
|
---|
544 | if (rc != EOK)
|
---|
545 | return NULL;
|
---|
546 |
|
---|
547 | irq_spinlock_lock(&box->lock, true);
|
---|
548 | if (!list_empty(&box->irq_notifs)) {
|
---|
549 | /* Count received IRQ notification */
|
---|
550 | irq_cnt++;
|
---|
551 |
|
---|
552 | irq_spinlock_lock(&box->irq_lock, false);
|
---|
553 |
|
---|
554 | request = list_get_instance(list_first(&box->irq_notifs),
|
---|
555 | call_t, ab_link);
|
---|
556 | list_remove(&request->ab_link);
|
---|
557 |
|
---|
558 | irq_spinlock_unlock(&box->irq_lock, false);
|
---|
559 | } else if (!list_empty(&box->answers)) {
|
---|
560 | /* Count received answer */
|
---|
561 | answer_cnt++;
|
---|
562 |
|
---|
563 | /* Handle asynchronous answers */
|
---|
564 | request = list_get_instance(list_first(&box->answers),
|
---|
565 | call_t, ab_link);
|
---|
566 | list_remove(&request->ab_link);
|
---|
567 | atomic_dec(&request->caller_phone->active_calls);
|
---|
568 | atomic_dec(&box->active_calls);
|
---|
569 | kobject_put(request->caller_phone->kobject);
|
---|
570 | } else if (!list_empty(&box->calls)) {
|
---|
571 | /* Count received call */
|
---|
572 | call_cnt++;
|
---|
573 |
|
---|
574 | /* Handle requests */
|
---|
575 | request = list_get_instance(list_first(&box->calls),
|
---|
576 | call_t, ab_link);
|
---|
577 | list_remove(&request->ab_link);
|
---|
578 |
|
---|
579 | /* Append request to dispatch queue */
|
---|
580 | list_append(&request->ab_link, &box->dispatched_calls);
|
---|
581 | } else {
|
---|
582 | /* This can happen regularly after ipc_cleanup */
|
---|
583 | irq_spinlock_unlock(&box->lock, true);
|
---|
584 | goto restart;
|
---|
585 | }
|
---|
586 |
|
---|
587 | irq_spinlock_pass(&box->lock, &TASK->lock);
|
---|
588 |
|
---|
589 | TASK->ipc_info.irq_notif_received += irq_cnt;
|
---|
590 | TASK->ipc_info.answer_received += answer_cnt;
|
---|
591 | TASK->ipc_info.call_received += call_cnt;
|
---|
592 |
|
---|
593 | irq_spinlock_unlock(&TASK->lock, true);
|
---|
594 |
|
---|
595 | return request;
|
---|
596 | }
|
---|
597 |
|
---|
598 | /** Answer all calls from list with EHANGUP answer.
|
---|
599 | *
|
---|
600 | * @param box Answerbox with the list.
|
---|
601 | * @param lst Head of the list to be cleaned up.
|
---|
602 | */
|
---|
603 | void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
|
---|
604 | {
|
---|
605 | irq_spinlock_lock(&box->lock, true);
|
---|
606 | while (!list_empty(lst)) {
|
---|
607 | call_t *call = list_get_instance(list_first(lst), call_t,
|
---|
608 | ab_link);
|
---|
609 |
|
---|
610 | list_remove(&call->ab_link);
|
---|
611 |
|
---|
612 | irq_spinlock_unlock(&box->lock, true);
|
---|
613 |
|
---|
614 | if (lst == &box->calls)
|
---|
615 | SYSIPC_OP(request_process, call, box);
|
---|
616 |
|
---|
617 | ipc_data_t old = call->data;
|
---|
618 | IPC_SET_RETVAL(call->data, EHANGUP);
|
---|
619 | answer_preprocess(call, &old);
|
---|
620 | _ipc_answer_free_call(call, true);
|
---|
621 |
|
---|
622 | irq_spinlock_lock(&box->lock, true);
|
---|
623 | }
|
---|
624 | irq_spinlock_unlock(&box->lock, true);
|
---|
625 | }
|
---|
626 |
|
---|
627 | /** Disconnects all phones connected to an answerbox.
|
---|
628 | *
|
---|
629 | * @param box Answerbox to disconnect phones from.
|
---|
630 | * @param notify_box If true, the answerbox will get a hangup message for
|
---|
631 | * each disconnected phone.
|
---|
632 | *
|
---|
633 | */
|
---|
634 | void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
|
---|
635 | {
|
---|
636 | phone_t *phone;
|
---|
637 | DEADLOCK_PROBE_INIT(p_phonelck);
|
---|
638 |
|
---|
639 | /* Disconnect all phones connected to our answerbox */
|
---|
640 | restart_phones:
|
---|
641 | irq_spinlock_lock(&box->lock, true);
|
---|
642 | while (!list_empty(&box->connected_phones)) {
|
---|
643 | phone = list_get_instance(list_first(&box->connected_phones),
|
---|
644 | phone_t, link);
|
---|
645 | if (mutex_trylock(&phone->lock) != EOK) {
|
---|
646 | irq_spinlock_unlock(&box->lock, true);
|
---|
647 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
|
---|
648 | goto restart_phones;
|
---|
649 | }
|
---|
650 |
|
---|
651 | /* Disconnect phone */
|
---|
652 | assert(phone->state == IPC_PHONE_CONNECTED);
|
---|
653 |
|
---|
654 | list_remove(&phone->link);
|
---|
655 | phone->state = IPC_PHONE_SLAMMED;
|
---|
656 |
|
---|
657 | if (notify_box) {
|
---|
658 | task_hold(phone->caller);
|
---|
659 | mutex_unlock(&phone->lock);
|
---|
660 | irq_spinlock_unlock(&box->lock, true);
|
---|
661 |
|
---|
662 | /*
|
---|
663 | * Send one call to the answerbox for each phone.
|
---|
664 | * Used to make sure the kbox thread wakes up after
|
---|
665 | * the last phone has been disconnected. The call is
|
---|
666 | * forgotten upon sending, so the "caller" may cease
|
---|
667 | * to exist as soon as we release it.
|
---|
668 | */
|
---|
669 | call_t *call = ipc_call_alloc(0);
|
---|
670 | IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
|
---|
671 | call->request_method = IPC_M_PHONE_HUNGUP;
|
---|
672 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
---|
673 | _ipc_call(phone, box, call, true);
|
---|
674 |
|
---|
675 | task_release(phone->caller);
|
---|
676 |
|
---|
677 | kobject_put(phone->kobject);
|
---|
678 |
|
---|
679 | /* Must start again */
|
---|
680 | goto restart_phones;
|
---|
681 | }
|
---|
682 |
|
---|
683 | mutex_unlock(&phone->lock);
|
---|
684 | kobject_put(phone->kobject);
|
---|
685 | }
|
---|
686 |
|
---|
687 | irq_spinlock_unlock(&box->lock, true);
|
---|
688 | }
|
---|
689 |
|
---|
690 | static void ipc_forget_call(call_t *call)
|
---|
691 | {
|
---|
692 | assert(spinlock_locked(&TASK->active_calls_lock));
|
---|
693 | assert(spinlock_locked(&call->forget_lock));
|
---|
694 |
|
---|
695 | /*
|
---|
696 | * Forget the call and donate it to the task which holds up the answer.
|
---|
697 | */
|
---|
698 |
|
---|
699 | call->forget = true;
|
---|
700 | call->sender = NULL;
|
---|
701 | list_remove(&call->ta_link);
|
---|
702 |
|
---|
703 | /*
|
---|
704 | * The call may be freed by _ipc_answer_free_call() before we are done
|
---|
705 | * with it; to avoid working with a destroyed call_t structure, we
|
---|
706 | * must hold a reference to it.
|
---|
707 | */
|
---|
708 | kobject_add_ref(call->kobject);
|
---|
709 |
|
---|
710 | spinlock_unlock(&call->forget_lock);
|
---|
711 | spinlock_unlock(&TASK->active_calls_lock);
|
---|
712 |
|
---|
713 | atomic_dec(&call->caller_phone->active_calls);
|
---|
714 | atomic_dec(&TASK->answerbox.active_calls);
|
---|
715 | kobject_put(call->caller_phone->kobject);
|
---|
716 |
|
---|
717 | SYSIPC_OP(request_forget, call);
|
---|
718 |
|
---|
719 | kobject_put(call->kobject);
|
---|
720 | }
|
---|
721 |
|
---|
722 | static void ipc_forget_all_active_calls(void)
|
---|
723 | {
|
---|
724 | call_t *call;
|
---|
725 |
|
---|
726 | restart:
|
---|
727 | spinlock_lock(&TASK->active_calls_lock);
|
---|
728 | if (list_empty(&TASK->active_calls)) {
|
---|
729 | /*
|
---|
730 | * We are done, there are no more active calls.
|
---|
731 | * Nota bene: there may still be answers waiting for pick up.
|
---|
732 | */
|
---|
733 | spinlock_unlock(&TASK->active_calls_lock);
|
---|
734 | return;
|
---|
735 | }
|
---|
736 |
|
---|
737 | call = list_get_instance(list_first(&TASK->active_calls), call_t,
|
---|
738 | ta_link);
|
---|
739 |
|
---|
740 | if (!spinlock_trylock(&call->forget_lock)) {
|
---|
741 | /*
|
---|
742 | * Avoid deadlock and let async_answer() or
|
---|
743 | * _ipc_answer_free_call() win the race to dequeue the first
|
---|
744 | * call on the list.
|
---|
745 | */
|
---|
746 | spinlock_unlock(&TASK->active_calls_lock);
|
---|
747 | goto restart;
|
---|
748 | }
|
---|
749 |
|
---|
750 | ipc_forget_call(call);
|
---|
751 |
|
---|
752 | goto restart;
|
---|
753 | }
|
---|
754 |
|
---|
755 | static bool phone_cap_cleanup_cb(cap_t *cap, void *arg)
|
---|
756 | {
|
---|
757 | ipc_phone_hangup(cap->kobject->phone);
|
---|
758 | kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
|
---|
759 | KOBJECT_TYPE_PHONE);
|
---|
760 | kobject_put(kobj);
|
---|
761 | cap_free(cap->task, cap->handle);
|
---|
762 | return true;
|
---|
763 | }
|
---|
764 |
|
---|
765 | /** Wait for all answers to asynchronous calls to arrive. */
|
---|
766 | static void ipc_wait_for_all_answered_calls(void)
|
---|
767 | {
|
---|
768 | while (atomic_get(&TASK->answerbox.active_calls) != 0) {
|
---|
769 | call_t *call = ipc_wait_for_call(&TASK->answerbox,
|
---|
770 | SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
|
---|
771 | assert(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
|
---|
772 |
|
---|
773 | SYSIPC_OP(answer_process, call);
|
---|
774 |
|
---|
775 | kobject_put(call->kobject);
|
---|
776 |
|
---|
777 | /*
|
---|
778 | * Now there may be some new phones and new hangup calls to
|
---|
779 | * immediately forget.
|
---|
780 | */
|
---|
781 | caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_PHONE,
|
---|
782 | phone_cap_cleanup_cb, NULL);
|
---|
783 | ipc_forget_all_active_calls();
|
---|
784 | }
|
---|
785 | }
|
---|
786 |
|
---|
787 | static bool irq_cap_cleanup_cb(cap_t *cap, void *arg)
|
---|
788 | {
|
---|
789 | ipc_irq_unsubscribe(&TASK->answerbox, cap->handle);
|
---|
790 | return true;
|
---|
791 | }
|
---|
792 |
|
---|
793 | static bool call_cap_cleanup_cb(cap_t *cap, void *arg)
|
---|
794 | {
|
---|
795 | /*
|
---|
796 | * Here we just free the capability and release the kobject.
|
---|
797 | * The kernel answers the remaining calls elsewhere in ipc_cleanup().
|
---|
798 | */
|
---|
799 | kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
|
---|
800 | KOBJECT_TYPE_CALL);
|
---|
801 | kobject_put(kobj);
|
---|
802 | cap_free(cap->task, cap->handle);
|
---|
803 | return true;
|
---|
804 | }
|
---|
805 |
|
---|
806 | /** Clean up all IPC communication of the current task.
|
---|
807 | *
|
---|
808 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
|
---|
809 | * have to change it as well if you want to cleanup other tasks than TASK.
|
---|
810 | *
|
---|
811 | */
|
---|
812 | void ipc_cleanup(void)
|
---|
813 | {
|
---|
814 | /*
|
---|
815 | * Mark the answerbox as inactive.
|
---|
816 | *
|
---|
817 | * The main purpose for doing this is to prevent any pending callback
|
---|
818 | * connections from getting established beyond this point.
|
---|
819 | */
|
---|
820 | irq_spinlock_lock(&TASK->answerbox.lock, true);
|
---|
821 | TASK->answerbox.active = false;
|
---|
822 | irq_spinlock_unlock(&TASK->answerbox.lock, true);
|
---|
823 |
|
---|
824 | /* Hangup all phones and destroy all phone capabilities */
|
---|
825 | caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_PHONE,
|
---|
826 | phone_cap_cleanup_cb, NULL);
|
---|
827 |
|
---|
828 | /* Unsubscribe from any event notifications */
|
---|
829 | event_cleanup_answerbox(&TASK->answerbox);
|
---|
830 |
|
---|
831 | /* Disconnect all connected IRQs */
|
---|
832 | caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_IRQ, irq_cap_cleanup_cb,
|
---|
833 | NULL);
|
---|
834 |
|
---|
835 | /* Disconnect all phones connected to our regular answerbox */
|
---|
836 | ipc_answerbox_slam_phones(&TASK->answerbox, false);
|
---|
837 |
|
---|
838 | #ifdef CONFIG_UDEBUG
|
---|
839 | /* Clean up kbox thread and communications */
|
---|
840 | ipc_kbox_cleanup();
|
---|
841 | #endif
|
---|
842 |
|
---|
843 | /* Destroy all call capabilities */
|
---|
844 | caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_CALL, call_cap_cleanup_cb,
|
---|
845 | NULL);
|
---|
846 |
|
---|
847 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */
|
---|
848 | ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
|
---|
849 | ipc_cleanup_call_list(&TASK->answerbox,
|
---|
850 | &TASK->answerbox.dispatched_calls);
|
---|
851 |
|
---|
852 | ipc_forget_all_active_calls();
|
---|
853 | ipc_wait_for_all_answered_calls();
|
---|
854 |
|
---|
855 | assert(atomic_get(&TASK->answerbox.active_calls) == 0);
|
---|
856 | }
|
---|
857 |
|
---|
858 | /** Initilize IPC subsystem
|
---|
859 | *
|
---|
860 | */
|
---|
861 | void ipc_init(void)
|
---|
862 | {
|
---|
863 | call_cache = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
|
---|
864 | NULL, 0);
|
---|
865 | phone_cache = slab_cache_create("phone_t", sizeof(phone_t), 0, NULL,
|
---|
866 | NULL, 0);
|
---|
867 | answerbox_cache = slab_cache_create("answerbox_t", sizeof(answerbox_t),
|
---|
868 | 0, NULL, NULL, 0);
|
---|
869 | }
|
---|
870 |
|
---|
871 |
|
---|
872 | static void ipc_print_call_list(list_t *list)
|
---|
873 | {
|
---|
874 | list_foreach(*list, ab_link, call_t, call) {
|
---|
875 | #ifdef __32_BITS__
|
---|
876 | printf("%10p ", call);
|
---|
877 | #endif
|
---|
878 |
|
---|
879 | #ifdef __64_BITS__
|
---|
880 | printf("%18p ", call);
|
---|
881 | #endif
|
---|
882 |
|
---|
883 | spinlock_lock(&call->forget_lock);
|
---|
884 |
|
---|
885 | printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
|
---|
886 | " %-6" PRIun " %-6" PRIun " %-7x",
|
---|
887 | IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
888 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
889 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
890 | call->flags);
|
---|
891 |
|
---|
892 | if (call->forget) {
|
---|
893 | printf(" ? (call forgotten)\n");
|
---|
894 | } else {
|
---|
895 | printf(" %" PRIu64 " (%s)\n",
|
---|
896 | call->sender->taskid, call->sender->name);
|
---|
897 | }
|
---|
898 |
|
---|
899 | spinlock_unlock(&call->forget_lock);
|
---|
900 | }
|
---|
901 | }
|
---|
902 |
|
---|
903 | static bool print_task_phone_cb(cap_t *cap, void *arg)
|
---|
904 | {
|
---|
905 | phone_t *phone = cap->kobject->phone;
|
---|
906 |
|
---|
907 | mutex_lock(&phone->lock);
|
---|
908 | if (phone->state != IPC_PHONE_FREE) {
|
---|
909 | printf("%-11d %7" PRIun " ", cap->handle,
|
---|
910 | atomic_get(&phone->active_calls));
|
---|
911 |
|
---|
912 | switch (phone->state) {
|
---|
913 | case IPC_PHONE_CONNECTING:
|
---|
914 | printf("connecting");
|
---|
915 | break;
|
---|
916 | case IPC_PHONE_CONNECTED:
|
---|
917 | printf("connected to %" PRIu64 " (%s)",
|
---|
918 | phone->callee->task->taskid,
|
---|
919 | phone->callee->task->name);
|
---|
920 | break;
|
---|
921 | case IPC_PHONE_SLAMMED:
|
---|
922 | printf("slammed by %p", phone->callee);
|
---|
923 | break;
|
---|
924 | case IPC_PHONE_HUNGUP:
|
---|
925 | printf("hung up to %p", phone->callee);
|
---|
926 | break;
|
---|
927 | default:
|
---|
928 | break;
|
---|
929 | }
|
---|
930 |
|
---|
931 | printf("\n");
|
---|
932 | }
|
---|
933 | mutex_unlock(&phone->lock);
|
---|
934 |
|
---|
935 | return true;
|
---|
936 | }
|
---|
937 |
|
---|
938 | /** List answerbox contents.
|
---|
939 | *
|
---|
940 | * @param taskid Task ID.
|
---|
941 | *
|
---|
942 | */
|
---|
943 | void ipc_print_task(task_id_t taskid)
|
---|
944 | {
|
---|
945 | irq_spinlock_lock(&tasks_lock, true);
|
---|
946 | task_t *task = task_find_by_id(taskid);
|
---|
947 | if (!task) {
|
---|
948 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
949 | return;
|
---|
950 | }
|
---|
951 | task_hold(task);
|
---|
952 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
953 |
|
---|
954 | printf("[phone cap] [calls] [state\n");
|
---|
955 |
|
---|
956 | caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
|
---|
957 | print_task_phone_cb, NULL);
|
---|
958 |
|
---|
959 | irq_spinlock_lock(&task->lock, true);
|
---|
960 | irq_spinlock_lock(&task->answerbox.lock, false);
|
---|
961 |
|
---|
962 | #ifdef __32_BITS__
|
---|
963 | printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
|
---|
964 | " [flags] [sender\n");
|
---|
965 | #endif
|
---|
966 |
|
---|
967 | #ifdef __64_BITS__
|
---|
968 | printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
|
---|
969 | " [arg5] [flags] [sender\n");
|
---|
970 | #endif
|
---|
971 |
|
---|
972 | printf(" --- incomming calls ---\n");
|
---|
973 | ipc_print_call_list(&task->answerbox.calls);
|
---|
974 | printf(" --- dispatched calls ---\n");
|
---|
975 | ipc_print_call_list(&task->answerbox.dispatched_calls);
|
---|
976 | printf(" --- incoming answers ---\n");
|
---|
977 | ipc_print_call_list(&task->answerbox.answers);
|
---|
978 |
|
---|
979 | irq_spinlock_unlock(&task->answerbox.lock, false);
|
---|
980 | irq_spinlock_unlock(&task->lock, true);
|
---|
981 |
|
---|
982 | task_release(task);
|
---|
983 | }
|
---|
984 |
|
---|
985 | /** @}
|
---|
986 | */
|
---|