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