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 <synch/synch.h>
|
---|
41 | #include <synch/spinlock.h>
|
---|
42 | #include <synch/mutex.h>
|
---|
43 | #include <synch/waitq.h>
|
---|
44 | #include <synch/synch.h>
|
---|
45 | #include <ipc/ipc.h>
|
---|
46 | #include <ipc/kbox.h>
|
---|
47 | #include <ipc/event.h>
|
---|
48 | #include <errno.h>
|
---|
49 | #include <mm/slab.h>
|
---|
50 | #include <arch.h>
|
---|
51 | #include <proc/task.h>
|
---|
52 | #include <memstr.h>
|
---|
53 | #include <debug.h>
|
---|
54 | #include <print.h>
|
---|
55 | #include <console/console.h>
|
---|
56 | #include <proc/thread.h>
|
---|
57 | #include <arch/interrupt.h>
|
---|
58 | #include <ipc/irq.h>
|
---|
59 |
|
---|
60 | /** Open channel that is assigned automatically to new tasks */
|
---|
61 | answerbox_t *ipc_phone_0 = NULL;
|
---|
62 |
|
---|
63 | static slab_cache_t *ipc_call_slab;
|
---|
64 | static slab_cache_t *ipc_answerbox_slab;
|
---|
65 |
|
---|
66 | /** Initialize a call structure.
|
---|
67 | *
|
---|
68 | * @param call Call structure to be initialized.
|
---|
69 | */
|
---|
70 | static void _ipc_call_init(call_t *call)
|
---|
71 | {
|
---|
72 | memsetb(call, sizeof(*call), 0);
|
---|
73 | call->callerbox = &TASK->answerbox;
|
---|
74 | call->sender = TASK;
|
---|
75 | call->buffer = NULL;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Allocate and initialize a call structure.
|
---|
79 | *
|
---|
80 | * The call is initialized, so that the reply will be directed to
|
---|
81 | * TASK->answerbox.
|
---|
82 | *
|
---|
83 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
|
---|
84 | *
|
---|
85 | * @return If flags permit it, return NULL, or initialized kernel
|
---|
86 | * call structure.
|
---|
87 | */
|
---|
88 | call_t *ipc_call_alloc(int flags)
|
---|
89 | {
|
---|
90 | call_t *call;
|
---|
91 |
|
---|
92 | call = slab_alloc(ipc_call_slab, flags);
|
---|
93 | if (call)
|
---|
94 | _ipc_call_init(call);
|
---|
95 |
|
---|
96 | return call;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Deallocate a call structure.
|
---|
100 | *
|
---|
101 | * @param call Call structure to be freed.
|
---|
102 | */
|
---|
103 | void ipc_call_free(call_t *call)
|
---|
104 | {
|
---|
105 | /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
|
---|
106 | if (call->buffer)
|
---|
107 | free(call->buffer);
|
---|
108 | slab_free(ipc_call_slab, call);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Initialize an answerbox structure.
|
---|
112 | *
|
---|
113 | * @param box Answerbox structure to be initialized.
|
---|
114 | * @param task Task to which the answerbox belongs.
|
---|
115 | */
|
---|
116 | void ipc_answerbox_init(answerbox_t *box, task_t *task)
|
---|
117 | {
|
---|
118 | spinlock_initialize(&box->lock, "ipc_box_lock");
|
---|
119 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
|
---|
120 | waitq_initialize(&box->wq);
|
---|
121 | link_initialize(&box->sync_box_link);
|
---|
122 | list_initialize(&box->connected_phones);
|
---|
123 | list_initialize(&box->calls);
|
---|
124 | list_initialize(&box->dispatched_calls);
|
---|
125 | list_initialize(&box->answers);
|
---|
126 | list_initialize(&box->irq_notifs);
|
---|
127 | list_initialize(&box->irq_head);
|
---|
128 | box->task = task;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Connect a phone to an answerbox.
|
---|
132 | *
|
---|
133 | * @param phone Initialized phone structure.
|
---|
134 | * @param box Initialized answerbox structure.
|
---|
135 | */
|
---|
136 | void ipc_phone_connect(phone_t *phone, answerbox_t *box)
|
---|
137 | {
|
---|
138 | mutex_lock(&phone->lock);
|
---|
139 |
|
---|
140 | phone->state = IPC_PHONE_CONNECTED;
|
---|
141 | phone->callee = box;
|
---|
142 |
|
---|
143 | spinlock_lock(&box->lock);
|
---|
144 | list_append(&phone->link, &box->connected_phones);
|
---|
145 | spinlock_unlock(&box->lock);
|
---|
146 |
|
---|
147 | mutex_unlock(&phone->lock);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Initialize a phone structure.
|
---|
151 | *
|
---|
152 | * @param phone Phone structure to be initialized.
|
---|
153 | */
|
---|
154 | void ipc_phone_init(phone_t *phone)
|
---|
155 | {
|
---|
156 | mutex_initialize(&phone->lock, MUTEX_PASSIVE);
|
---|
157 | phone->callee = NULL;
|
---|
158 | phone->state = IPC_PHONE_FREE;
|
---|
159 | atomic_set(&phone->active_calls, 0);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Helper function to facilitate synchronous calls.
|
---|
163 | *
|
---|
164 | * @param phone Destination kernel phone structure.
|
---|
165 | * @param request Call structure with request.
|
---|
166 | *
|
---|
167 | * @return EOK on success or EINTR if the sleep was interrupted.
|
---|
168 | */
|
---|
169 | int ipc_call_sync(phone_t *phone, call_t *request)
|
---|
170 | {
|
---|
171 | answerbox_t *sync_box;
|
---|
172 | ipl_t ipl;
|
---|
173 |
|
---|
174 | sync_box = slab_alloc(ipc_answerbox_slab, 0);
|
---|
175 | ipc_answerbox_init(sync_box, TASK);
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * Put the answerbox on the TASK's list of synchronous answerboxes so
|
---|
179 | * that it can be cleaned up if the call is interrupted.
|
---|
180 | */
|
---|
181 | ipl = interrupts_disable();
|
---|
182 | spinlock_lock(&TASK->lock);
|
---|
183 | list_append(&sync_box->sync_box_link, &TASK->sync_box_head);
|
---|
184 | spinlock_unlock(&TASK->lock);
|
---|
185 | interrupts_restore(ipl);
|
---|
186 |
|
---|
187 | /* We will receive data in a special box. */
|
---|
188 | request->callerbox = sync_box;
|
---|
189 |
|
---|
190 | ipc_call(phone, request);
|
---|
191 | if (!ipc_wait_for_call(sync_box, SYNCH_NO_TIMEOUT,
|
---|
192 | SYNCH_FLAGS_INTERRUPTIBLE)) {
|
---|
193 | /* The answerbox and the call will be freed by ipc_cleanup(). */
|
---|
194 | return EINTR;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * The answer arrived without interruption so we can remove the
|
---|
199 | * answerbox from the TASK's list of synchronous answerboxes.
|
---|
200 | */
|
---|
201 | (void) interrupts_disable();
|
---|
202 | spinlock_lock(&TASK->lock);
|
---|
203 | list_remove(&sync_box->sync_box_link);
|
---|
204 | spinlock_unlock(&TASK->lock);
|
---|
205 | interrupts_restore(ipl);
|
---|
206 |
|
---|
207 | slab_free(ipc_answerbox_slab, sync_box);
|
---|
208 | return EOK;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /** Answer a message which was not dispatched and is not listed in any queue.
|
---|
212 | *
|
---|
213 | * @param call Call structure to be answered.
|
---|
214 | * @param selflocked If true, then TASK->answebox is locked.
|
---|
215 | */
|
---|
216 | static void _ipc_answer_free_call(call_t *call, bool selflocked)
|
---|
217 | {
|
---|
218 | answerbox_t *callerbox = call->callerbox;
|
---|
219 | bool do_lock = ((!selflocked) || callerbox != (&TASK->answerbox));
|
---|
220 |
|
---|
221 | /* Count sent answer */
|
---|
222 | spinlock_lock(&TASK->lock);
|
---|
223 | TASK->ipc_info.answer_sent++;
|
---|
224 | spinlock_unlock(&TASK->lock);
|
---|
225 |
|
---|
226 | call->flags |= IPC_CALL_ANSWERED;
|
---|
227 |
|
---|
228 | if (call->flags & IPC_CALL_FORWARDED) {
|
---|
229 | if (call->caller_phone) {
|
---|
230 | /* Demasquerade the caller phone. */
|
---|
231 | call->data.phone = call->caller_phone;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (do_lock)
|
---|
236 | spinlock_lock(&callerbox->lock);
|
---|
237 | list_append(&call->link, &callerbox->answers);
|
---|
238 | if (do_lock)
|
---|
239 | spinlock_unlock(&callerbox->lock);
|
---|
240 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** Answer a message which is in a callee queue.
|
---|
244 | *
|
---|
245 | * @param box Answerbox that is answering the message.
|
---|
246 | * @param call Modified request that is being sent back.
|
---|
247 | */
|
---|
248 | void ipc_answer(answerbox_t *box, call_t *call)
|
---|
249 | {
|
---|
250 | /* Remove from active box */
|
---|
251 | spinlock_lock(&box->lock);
|
---|
252 | list_remove(&call->link);
|
---|
253 | spinlock_unlock(&box->lock);
|
---|
254 | /* Send back answer */
|
---|
255 | _ipc_answer_free_call(call, false);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /** Simulate sending back a message.
|
---|
259 | *
|
---|
260 | * Most errors are better handled by forming a normal backward
|
---|
261 | * message and sending it as a normal answer.
|
---|
262 | *
|
---|
263 | * @param phone Phone structure the call should appear to come from.
|
---|
264 | * @param call Call structure to be answered.
|
---|
265 | * @param err Return value to be used for the answer.
|
---|
266 | */
|
---|
267 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
|
---|
268 | {
|
---|
269 | call->data.phone = phone;
|
---|
270 | atomic_inc(&phone->active_calls);
|
---|
271 | IPC_SET_RETVAL(call->data, err);
|
---|
272 | _ipc_answer_free_call(call, false);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /** Unsafe unchecking version of ipc_call.
|
---|
276 | *
|
---|
277 | * @param phone Phone structure the call comes from.
|
---|
278 | * @param box Destination answerbox structure.
|
---|
279 | * @param call Call structure with request.
|
---|
280 | */
|
---|
281 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
|
---|
282 | {
|
---|
283 | /* Count sent ipc call */
|
---|
284 | spinlock_lock(&TASK->lock);
|
---|
285 | TASK->ipc_info.call_sent++;
|
---|
286 | spinlock_unlock(&TASK->lock);
|
---|
287 |
|
---|
288 | if (!(call->flags & IPC_CALL_FORWARDED)) {
|
---|
289 | atomic_inc(&phone->active_calls);
|
---|
290 | call->data.phone = phone;
|
---|
291 | }
|
---|
292 |
|
---|
293 | spinlock_lock(&box->lock);
|
---|
294 | list_append(&call->link, &box->calls);
|
---|
295 | spinlock_unlock(&box->lock);
|
---|
296 | waitq_wakeup(&box->wq, WAKEUP_FIRST);
|
---|
297 | }
|
---|
298 |
|
---|
299 | /** Send an asynchronous request using a phone to an answerbox.
|
---|
300 | *
|
---|
301 | * @param phone Phone structure the call comes from and which is
|
---|
302 | * connected to the destination answerbox.
|
---|
303 | * @param call Call structure with request.
|
---|
304 | *
|
---|
305 | * @return Return 0 on success, ENOENT on error.
|
---|
306 | */
|
---|
307 | int ipc_call(phone_t *phone, call_t *call)
|
---|
308 | {
|
---|
309 | answerbox_t *box;
|
---|
310 |
|
---|
311 | mutex_lock(&phone->lock);
|
---|
312 | if (phone->state != IPC_PHONE_CONNECTED) {
|
---|
313 | mutex_unlock(&phone->lock);
|
---|
314 | if (call->flags & IPC_CALL_FORWARDED) {
|
---|
315 | IPC_SET_RETVAL(call->data, EFORWARD);
|
---|
316 | _ipc_answer_free_call(call, false);
|
---|
317 | } else {
|
---|
318 | if (phone->state == IPC_PHONE_HUNGUP)
|
---|
319 | ipc_backsend_err(phone, call, EHANGUP);
|
---|
320 | else
|
---|
321 | ipc_backsend_err(phone, call, ENOENT);
|
---|
322 | }
|
---|
323 | return ENOENT;
|
---|
324 | }
|
---|
325 | box = phone->callee;
|
---|
326 | _ipc_call(phone, box, call);
|
---|
327 |
|
---|
328 | mutex_unlock(&phone->lock);
|
---|
329 | return 0;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /** Disconnect phone from answerbox.
|
---|
333 | *
|
---|
334 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done
|
---|
335 | * lazily later.
|
---|
336 | *
|
---|
337 | * @param phone Phone structure to be hung up.
|
---|
338 | *
|
---|
339 | * @return Return 0 if the phone is disconnected.
|
---|
340 | * Return -1 if the phone was already disconnected.
|
---|
341 | */
|
---|
342 | int ipc_phone_hangup(phone_t *phone)
|
---|
343 | {
|
---|
344 | answerbox_t *box;
|
---|
345 | call_t *call;
|
---|
346 |
|
---|
347 | mutex_lock(&phone->lock);
|
---|
348 | if (phone->state == IPC_PHONE_FREE ||
|
---|
349 | phone->state == IPC_PHONE_HUNGUP ||
|
---|
350 | phone->state == IPC_PHONE_CONNECTING) {
|
---|
351 | mutex_unlock(&phone->lock);
|
---|
352 | return -1;
|
---|
353 | }
|
---|
354 | box = phone->callee;
|
---|
355 | if (phone->state != IPC_PHONE_SLAMMED) {
|
---|
356 | /* Remove myself from answerbox */
|
---|
357 | spinlock_lock(&box->lock);
|
---|
358 | list_remove(&phone->link);
|
---|
359 | spinlock_unlock(&box->lock);
|
---|
360 |
|
---|
361 | call = ipc_call_alloc(0);
|
---|
362 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
|
---|
363 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
---|
364 | _ipc_call(phone, box, call);
|
---|
365 | }
|
---|
366 |
|
---|
367 | phone->state = IPC_PHONE_HUNGUP;
|
---|
368 | mutex_unlock(&phone->lock);
|
---|
369 |
|
---|
370 | return 0;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /** Forwards call from one answerbox to another one.
|
---|
374 | *
|
---|
375 | * @param call Call structure to be redirected.
|
---|
376 | * @param newphone Phone structure to target answerbox.
|
---|
377 | * @param oldbox Old answerbox structure.
|
---|
378 | * @param mode Flags that specify mode of the forward operation.
|
---|
379 | *
|
---|
380 | * @return Return 0 if forwarding succeeded or an error code if
|
---|
381 | * there was error.
|
---|
382 | *
|
---|
383 | * The return value serves only as an information for the forwarder,
|
---|
384 | * the original caller is notified automatically with EFORWARD.
|
---|
385 | */
|
---|
386 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
|
---|
387 | {
|
---|
388 | /* Count forwarded calls */
|
---|
389 | spinlock_lock(&TASK->lock);
|
---|
390 | TASK->ipc_info.forwarded++;
|
---|
391 | spinlock_unlock(&TASK->lock);
|
---|
392 |
|
---|
393 | spinlock_lock(&oldbox->lock);
|
---|
394 | list_remove(&call->link);
|
---|
395 | spinlock_unlock(&oldbox->lock);
|
---|
396 |
|
---|
397 | if (mode & IPC_FF_ROUTE_FROM_ME) {
|
---|
398 | if (!call->caller_phone)
|
---|
399 | call->caller_phone = call->data.phone;
|
---|
400 | call->data.phone = newphone;
|
---|
401 | }
|
---|
402 |
|
---|
403 | return ipc_call(newphone, call);
|
---|
404 | }
|
---|
405 |
|
---|
406 |
|
---|
407 | /** Wait for a phone call.
|
---|
408 | *
|
---|
409 | * @param box Answerbox expecting the call.
|
---|
410 | * @param usec Timeout in microseconds. See documentation for
|
---|
411 | * waitq_sleep_timeout() for decription of its special
|
---|
412 | * meaning.
|
---|
413 | * @param flags Select mode of sleep operation. See documentation for
|
---|
414 | * waitq_sleep_timeout() for description of its special
|
---|
415 | * meaning.
|
---|
416 | * @return Recived call structure or NULL.
|
---|
417 | *
|
---|
418 | * To distinguish between a call and an answer, have a look at call->flags.
|
---|
419 | */
|
---|
420 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
|
---|
421 | {
|
---|
422 | call_t *request;
|
---|
423 | ipl_t ipl;
|
---|
424 | uint64_t irq_cnt = 0;
|
---|
425 | uint64_t answer_cnt = 0;
|
---|
426 | uint64_t call_cnt = 0;
|
---|
427 | int rc;
|
---|
428 |
|
---|
429 | restart:
|
---|
430 | rc = waitq_sleep_timeout(&box->wq, usec, flags);
|
---|
431 | if (SYNCH_FAILED(rc))
|
---|
432 | return NULL;
|
---|
433 |
|
---|
434 | spinlock_lock(&box->lock);
|
---|
435 | if (!list_empty(&box->irq_notifs)) {
|
---|
436 | /* Count recieved IRQ notification */
|
---|
437 | irq_cnt++;
|
---|
438 |
|
---|
439 | ipl = interrupts_disable();
|
---|
440 | spinlock_lock(&box->irq_lock);
|
---|
441 |
|
---|
442 | request = list_get_instance(box->irq_notifs.next, call_t, link);
|
---|
443 | list_remove(&request->link);
|
---|
444 |
|
---|
445 | spinlock_unlock(&box->irq_lock);
|
---|
446 | interrupts_restore(ipl);
|
---|
447 | } else if (!list_empty(&box->answers)) {
|
---|
448 | /* Count recieved answer */
|
---|
449 | answer_cnt++;
|
---|
450 |
|
---|
451 | /* Handle asynchronous answers */
|
---|
452 | request = list_get_instance(box->answers.next, call_t, link);
|
---|
453 | list_remove(&request->link);
|
---|
454 | atomic_dec(&request->data.phone->active_calls);
|
---|
455 | } else if (!list_empty(&box->calls)) {
|
---|
456 | /* Count recieved call */
|
---|
457 | call_cnt++;
|
---|
458 |
|
---|
459 | /* Handle requests */
|
---|
460 | request = list_get_instance(box->calls.next, call_t, link);
|
---|
461 | list_remove(&request->link);
|
---|
462 | /* Append request to dispatch queue */
|
---|
463 | list_append(&request->link, &box->dispatched_calls);
|
---|
464 | } else {
|
---|
465 | /* This can happen regularly after ipc_cleanup */
|
---|
466 | spinlock_unlock(&box->lock);
|
---|
467 | goto restart;
|
---|
468 | }
|
---|
469 | spinlock_unlock(&box->lock);
|
---|
470 |
|
---|
471 | ipl = interrupts_disable();
|
---|
472 | spinlock_lock(&TASK->lock);
|
---|
473 | TASK->ipc_info.irq_notif_recieved += irq_cnt;
|
---|
474 | TASK->ipc_info.answer_recieved += answer_cnt;
|
---|
475 | TASK->ipc_info.call_recieved += call_cnt;
|
---|
476 | spinlock_unlock(&TASK->lock);
|
---|
477 | interrupts_restore(ipl);
|
---|
478 |
|
---|
479 | return request;
|
---|
480 | }
|
---|
481 |
|
---|
482 | /** Answer all calls from list with EHANGUP answer.
|
---|
483 | *
|
---|
484 | * @param lst Head of the list to be cleaned up.
|
---|
485 | */
|
---|
486 | void ipc_cleanup_call_list(link_t *lst)
|
---|
487 | {
|
---|
488 | call_t *call;
|
---|
489 |
|
---|
490 | while (!list_empty(lst)) {
|
---|
491 | call = list_get_instance(lst->next, call_t, link);
|
---|
492 | if (call->buffer)
|
---|
493 | free(call->buffer);
|
---|
494 | list_remove(&call->link);
|
---|
495 |
|
---|
496 | IPC_SET_RETVAL(call->data, EHANGUP);
|
---|
497 | _ipc_answer_free_call(call, true);
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 | /** Disconnects all phones connected to an answerbox.
|
---|
502 | *
|
---|
503 | * @param box Answerbox to disconnect phones from.
|
---|
504 | * @param notify_box If true, the answerbox will get a hangup message for
|
---|
505 | * each disconnected phone.
|
---|
506 | */
|
---|
507 | void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
|
---|
508 | {
|
---|
509 | phone_t *phone;
|
---|
510 | DEADLOCK_PROBE_INIT(p_phonelck);
|
---|
511 | ipl_t ipl;
|
---|
512 | call_t *call;
|
---|
513 |
|
---|
514 | call = notify_box ? ipc_call_alloc(0) : NULL;
|
---|
515 |
|
---|
516 | /* Disconnect all phones connected to our answerbox */
|
---|
517 | restart_phones:
|
---|
518 | ipl = interrupts_disable();
|
---|
519 | spinlock_lock(&box->lock);
|
---|
520 | while (!list_empty(&box->connected_phones)) {
|
---|
521 | phone = list_get_instance(box->connected_phones.next,
|
---|
522 | phone_t, link);
|
---|
523 | if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
|
---|
524 | spinlock_unlock(&box->lock);
|
---|
525 | interrupts_restore(ipl);
|
---|
526 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
|
---|
527 | goto restart_phones;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /* Disconnect phone */
|
---|
531 | ASSERT(phone->state == IPC_PHONE_CONNECTED);
|
---|
532 |
|
---|
533 | list_remove(&phone->link);
|
---|
534 | phone->state = IPC_PHONE_SLAMMED;
|
---|
535 |
|
---|
536 | if (notify_box) {
|
---|
537 | mutex_unlock(&phone->lock);
|
---|
538 | spinlock_unlock(&box->lock);
|
---|
539 | interrupts_restore(ipl);
|
---|
540 |
|
---|
541 | /*
|
---|
542 | * Send one message to the answerbox for each
|
---|
543 | * phone. Used to make sure the kbox thread
|
---|
544 | * wakes up after the last phone has been
|
---|
545 | * disconnected.
|
---|
546 | */
|
---|
547 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
|
---|
548 | call->flags |= IPC_CALL_DISCARD_ANSWER;
|
---|
549 | _ipc_call(phone, box, call);
|
---|
550 |
|
---|
551 | /* Allocate another call in advance */
|
---|
552 | call = ipc_call_alloc(0);
|
---|
553 |
|
---|
554 | /* Must start again */
|
---|
555 | goto restart_phones;
|
---|
556 | }
|
---|
557 |
|
---|
558 | mutex_unlock(&phone->lock);
|
---|
559 | }
|
---|
560 |
|
---|
561 | spinlock_unlock(&box->lock);
|
---|
562 | interrupts_restore(ipl);
|
---|
563 |
|
---|
564 | /* Free unused call */
|
---|
565 | if (call)
|
---|
566 | ipc_call_free(call);
|
---|
567 | }
|
---|
568 |
|
---|
569 | /** Cleans up all IPC communication of the current task.
|
---|
570 | *
|
---|
571 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
|
---|
572 | * have to change it as well if you want to cleanup other tasks than TASK.
|
---|
573 | */
|
---|
574 | void ipc_cleanup(void)
|
---|
575 | {
|
---|
576 | int i;
|
---|
577 | call_t *call;
|
---|
578 | ipl_t ipl;
|
---|
579 |
|
---|
580 | /* Disconnect all our phones ('ipc_phone_hangup') */
|
---|
581 | for (i = 0; i < IPC_MAX_PHONES; i++)
|
---|
582 | ipc_phone_hangup(&TASK->phones[i]);
|
---|
583 |
|
---|
584 | /* Unsubscribe from any event notifications. */
|
---|
585 | event_cleanup_answerbox(&TASK->answerbox);
|
---|
586 |
|
---|
587 | /* Disconnect all connected irqs */
|
---|
588 | ipc_irq_cleanup(&TASK->answerbox);
|
---|
589 |
|
---|
590 | /* Disconnect all phones connected to our regular answerbox */
|
---|
591 | ipc_answerbox_slam_phones(&TASK->answerbox, false);
|
---|
592 |
|
---|
593 | #ifdef CONFIG_UDEBUG
|
---|
594 | /* Clean up kbox thread and communications */
|
---|
595 | ipc_kbox_cleanup();
|
---|
596 | #endif
|
---|
597 |
|
---|
598 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */
|
---|
599 | spinlock_lock(&TASK->answerbox.lock);
|
---|
600 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
|
---|
601 | ipc_cleanup_call_list(&TASK->answerbox.calls);
|
---|
602 | spinlock_unlock(&TASK->answerbox.lock);
|
---|
603 |
|
---|
604 | /* Wait for all answers to interrupted synchronous calls to arrive */
|
---|
605 | ipl = interrupts_disable();
|
---|
606 | while (!list_empty(&TASK->sync_box_head)) {
|
---|
607 | answerbox_t *box = list_get_instance(TASK->sync_box_head.next,
|
---|
608 | answerbox_t, sync_box_link);
|
---|
609 |
|
---|
610 | list_remove(&box->sync_box_link);
|
---|
611 | call = ipc_wait_for_call(box, SYNCH_NO_TIMEOUT,
|
---|
612 | SYNCH_FLAGS_NONE);
|
---|
613 | ipc_call_free(call);
|
---|
614 | slab_free(ipc_answerbox_slab, box);
|
---|
615 | }
|
---|
616 | interrupts_restore(ipl);
|
---|
617 |
|
---|
618 | /* Wait for all answers to asynchronous calls to arrive */
|
---|
619 | while (1) {
|
---|
620 | /* Go through all phones, until all are FREE... */
|
---|
621 | /* Locking not needed, no one else should modify
|
---|
622 | * it, when we are in cleanup */
|
---|
623 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
624 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
|
---|
625 | atomic_get(&TASK->phones[i].active_calls) == 0) {
|
---|
626 | TASK->phones[i].state = IPC_PHONE_FREE;
|
---|
627 | TASK->phones[i].callee = NULL;
|
---|
628 | }
|
---|
629 |
|
---|
630 | /* Just for sure, we might have had some
|
---|
631 | * IPC_PHONE_CONNECTING phones */
|
---|
632 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
|
---|
633 | ipc_phone_hangup(&TASK->phones[i]);
|
---|
634 | /* If the hangup succeeded, it has sent a HANGUP
|
---|
635 | * message, the IPC is now in HUNGUP state, we
|
---|
636 | * wait for the reply to come */
|
---|
637 |
|
---|
638 | if (TASK->phones[i].state != IPC_PHONE_FREE)
|
---|
639 | break;
|
---|
640 | }
|
---|
641 | /* Voila, got into cleanup */
|
---|
642 | if (i == IPC_MAX_PHONES)
|
---|
643 | break;
|
---|
644 |
|
---|
645 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
|
---|
646 | SYNCH_FLAGS_NONE);
|
---|
647 | ASSERT((call->flags & IPC_CALL_ANSWERED) ||
|
---|
648 | (call->flags & IPC_CALL_NOTIF));
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * Record the receipt of this call in the current task's counter
|
---|
652 | * of active calls. IPC_M_PHONE_HUNGUP calls do not contribute
|
---|
653 | * to this counter so do not record answers to them either.
|
---|
654 | */
|
---|
655 | if (!(call->flags & IPC_CALL_DISCARD_ANSWER))
|
---|
656 | atomic_dec(&TASK->active_calls);
|
---|
657 | ipc_call_free(call);
|
---|
658 | }
|
---|
659 | }
|
---|
660 |
|
---|
661 |
|
---|
662 | /** Initilize IPC subsystem */
|
---|
663 | void ipc_init(void)
|
---|
664 | {
|
---|
665 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
|
---|
666 | NULL, 0);
|
---|
667 | ipc_answerbox_slab = slab_cache_create("ipc_answerbox",
|
---|
668 | sizeof(answerbox_t), 0, NULL, NULL, 0);
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | /** List answerbox contents.
|
---|
673 | *
|
---|
674 | * @param taskid Task ID.
|
---|
675 | */
|
---|
676 | void ipc_print_task(task_id_t taskid)
|
---|
677 | {
|
---|
678 | task_t *task;
|
---|
679 | int i;
|
---|
680 | call_t *call;
|
---|
681 | link_t *tmp;
|
---|
682 |
|
---|
683 | spinlock_lock(&tasks_lock);
|
---|
684 | task = task_find_by_id(taskid);
|
---|
685 | if (task)
|
---|
686 | spinlock_lock(&task->lock);
|
---|
687 | spinlock_unlock(&tasks_lock);
|
---|
688 | if (!task)
|
---|
689 | return;
|
---|
690 |
|
---|
691 | /* Print opened phones & details */
|
---|
692 | printf("PHONE:\n");
|
---|
693 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
694 | if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
|
---|
695 | printf("%d: mutex busy\n", i);
|
---|
696 | continue;
|
---|
697 | }
|
---|
698 | if (task->phones[i].state != IPC_PHONE_FREE) {
|
---|
699 | printf("%d: ", i);
|
---|
700 | switch (task->phones[i].state) {
|
---|
701 | case IPC_PHONE_CONNECTING:
|
---|
702 | printf("connecting ");
|
---|
703 | break;
|
---|
704 | case IPC_PHONE_CONNECTED:
|
---|
705 | printf("connected to: %p ",
|
---|
706 | task->phones[i].callee);
|
---|
707 | break;
|
---|
708 | case IPC_PHONE_SLAMMED:
|
---|
709 | printf("slammed by: %p ",
|
---|
710 | task->phones[i].callee);
|
---|
711 | break;
|
---|
712 | case IPC_PHONE_HUNGUP:
|
---|
713 | printf("hung up - was: %p ",
|
---|
714 | task->phones[i].callee);
|
---|
715 | break;
|
---|
716 | default:
|
---|
717 | break;
|
---|
718 | }
|
---|
719 | printf("active: %ld\n",
|
---|
720 | atomic_get(&task->phones[i].active_calls));
|
---|
721 | }
|
---|
722 | mutex_unlock(&task->phones[i].lock);
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | /* Print answerbox - calls */
|
---|
727 | spinlock_lock(&task->answerbox.lock);
|
---|
728 | printf("ABOX - CALLS:\n");
|
---|
729 | for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
|
---|
730 | tmp = tmp->next) {
|
---|
731 | call = list_get_instance(tmp, call_t, link);
|
---|
732 | printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
|
---|
733 | " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
|
---|
734 | " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
|
---|
735 | call->sender->taskid,
|
---|
736 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
737 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
738 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
739 | call->flags);
|
---|
740 | }
|
---|
741 | /* Print answerbox - calls */
|
---|
742 | printf("ABOX - DISPATCHED CALLS:\n");
|
---|
743 | for (tmp = task->answerbox.dispatched_calls.next;
|
---|
744 | tmp != &task->answerbox.dispatched_calls;
|
---|
745 | tmp = tmp->next) {
|
---|
746 | call = list_get_instance(tmp, call_t, link);
|
---|
747 | printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
|
---|
748 | " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
|
---|
749 | " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call,
|
---|
750 | call->sender->taskid,
|
---|
751 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
752 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
753 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
754 | call->flags);
|
---|
755 | }
|
---|
756 | /* Print answerbox - calls */
|
---|
757 | printf("ABOX - ANSWERS:\n");
|
---|
758 | for (tmp = task->answerbox.answers.next;
|
---|
759 | tmp != &task->answerbox.answers;
|
---|
760 | tmp = tmp->next) {
|
---|
761 | call = list_get_instance(tmp, call_t, link);
|
---|
762 | printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
|
---|
763 | " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
|
---|
764 | call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
|
---|
765 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
|
---|
766 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
|
---|
767 | call->flags);
|
---|
768 | }
|
---|
769 |
|
---|
770 | spinlock_unlock(&task->answerbox.lock);
|
---|
771 | spinlock_unlock(&task->lock);
|
---|
772 | }
|
---|
773 |
|
---|
774 | /** @}
|
---|
775 | */
|
---|