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