source: mainline/kernel/generic/src/ipc/ipc.c@ 6d351e6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6d351e6 was 6d351e6, checked in by Jakub Jermar <jakub@…>, 9 years ago

Revert changeset jakub@…

We will use the possibility to specify a different answerbox for replies
for sending page in and page out requests from the user_backend page
fault handler.

  • Property mode set to 100644
File size: 22.7 KB
RevLine 
[6d9c49a]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[6d9c49a]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
[cc73a8a1]29/** @addtogroup genericipc
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[6d9c49a]35/* Lock ordering
36 *
[8b243f2]37 * First the answerbox, then the phone.
[6d9c49a]38 */
39
[2ba7810]40#include <synch/spinlock.h>
[ff48a15]41#include <synch/mutex.h>
[2ba7810]42#include <synch/waitq.h>
[6d9c49a]43#include <ipc/ipc.h>
[c0699467]44#include <abi/ipc/methods.h>
[e028660]45#include <ipc/kbox.h>
[13a638d]46#include <ipc/event.h>
[b1e6269]47#include <ipc/sysipc_ops.h>
[5d3ed34]48#include <ipc/sysipc_priv.h>
[6d9c49a]49#include <errno.h>
50#include <mm/slab.h>
51#include <arch.h>
52#include <proc/task.h>
53#include <memstr.h>
54#include <debug.h>
55#include <print.h>
[9a1b20c]56#include <console/console.h>
[6d9c49a]57#include <proc/thread.h>
[5626277]58#include <arch/interrupt.h>
[162f919]59#include <ipc/irq.h>
[6d9c49a]60
[8b243f2]61/** Open channel that is assigned automatically to new tasks */
[e74cb73]62answerbox_t *ipc_phone_0 = NULL;
[6d9c49a]63
64static slab_cache_t *ipc_call_slab;
[c70ce74]65static slab_cache_t *ipc_answerbox_slab;
[6d9c49a]66
[8b243f2]67/** Initialize a call structure.
68 *
[da1bafb]69 * @param call Call structure to be initialized.
70 *
[8b243f2]71 */
[ba81cab]72static void _ipc_call_init(call_t *call)
73{
[e32e092]74 memsetb(call, sizeof(*call), 0);
[2405bb5]75 spinlock_initialize(&call->forget_lock, "forget_lock");
[20282ef3]76 call->active = false;
[2405bb5]77 call->forget = false;
[03a8a8e]78 call->sender = NULL;
[6d351e6]79 call->callerbox = &TASK->answerbox;
[7918fce]80 call->buffer = NULL;
[ba81cab]81}
82
[cd671c3]83void ipc_call_hold(call_t *call)
84{
85 atomic_inc(&call->refcnt);
86}
87
88void ipc_call_release(call_t *call)
89{
90 if (atomic_predec(&call->refcnt) == 0) {
91 if (call->buffer)
92 free(call->buffer);
93 slab_free(ipc_call_slab, call);
94 }
95}
96
[8b243f2]97/** Allocate and initialize a call structure.
[da1bafb]98 *
[8b243f2]99 * The call is initialized, so that the reply will be directed to
100 * TASK->answerbox.
101 *
[da1bafb]102 * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC).
103 *
104 * @return If flags permit it, return NULL, or initialized kernel
[cd671c3]105 * call structure with one reference.
[5626277]106 *
[6d9c49a]107 */
[da1bafb]108call_t *ipc_call_alloc(unsigned int flags)
[6d9c49a]109{
[da1bafb]110 call_t *call = slab_alloc(ipc_call_slab, flags);
[cd671c3]111 if (call) {
[69eac4aa]112 _ipc_call_init(call);
[cd671c3]113 ipc_call_hold(call);
114 }
[da1bafb]115
[6d9c49a]116 return call;
117}
118
[bd72c3e9]119/** Deallocate a call structure.
[8b243f2]120 *
[da1bafb]121 * @param call Call structure to be freed.
122 *
[8b243f2]123 */
[6d9c49a]124void ipc_call_free(call_t *call)
125{
[cd671c3]126 ipc_call_release(call);
[6d9c49a]127}
128
[8b243f2]129/** Initialize an answerbox structure.
130 *
[da1bafb]131 * @param box Answerbox structure to be initialized.
132 * @param task Task to which the answerbox belongs.
133 *
[6d9c49a]134 */
[12ab886]135void ipc_answerbox_init(answerbox_t *box, task_t *task)
[6d9c49a]136{
[da1bafb]137 irq_spinlock_initialize(&box->lock, "ipc.box.lock");
138 irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
[2ba7810]139 waitq_initialize(&box->wq);
[6d9c49a]140 list_initialize(&box->connected_phones);
141 list_initialize(&box->calls);
142 list_initialize(&box->dispatched_calls);
143 list_initialize(&box->answers);
[5626277]144 list_initialize(&box->irq_notifs);
[55b77d9]145 list_initialize(&box->irq_list);
[12ab886]146 box->task = task;
[6d9c49a]147}
148
[8b243f2]149/** Connect a phone to an answerbox.
150 *
[da1bafb]151 * @param phone Initialized phone structure.
152 * @param box Initialized answerbox structure.
[c33f39f]153 * @return True if the phone was connected, false otherwise.
[8b243f2]154 */
[c33f39f]155bool ipc_phone_connect(phone_t *phone, answerbox_t *box)
[6d9c49a]156{
[c33f39f]157 bool active;
158
[ff48a15]159 mutex_lock(&phone->lock);
[da1bafb]160 irq_spinlock_lock(&box->lock, true);
[c33f39f]161
162 active = box->active;
163 if (active) {
164 phone->state = IPC_PHONE_CONNECTED;
165 phone->callee = box;
166 list_append(&phone->link, &box->connected_phones);
167 }
168
[da1bafb]169 irq_spinlock_unlock(&box->lock, true);
[ff48a15]170 mutex_unlock(&phone->lock);
[c33f39f]171
172 return active;
[2ba7810]173}
174
[8b243f2]175/** Initialize a phone structure.
176 *
[da1bafb]177 * @param phone Phone structure to be initialized.
[03a8a8e]178 * @param caller Owning task.
[da1bafb]179 *
[2ba7810]180 */
[03a8a8e]181void ipc_phone_init(phone_t *phone, task_t *caller)
[2ba7810]182{
[08a19ba]183 mutex_initialize(&phone->lock, MUTEX_PASSIVE);
[03a8a8e]184 phone->caller = caller;
[2ba7810]185 phone->callee = NULL;
[eb3d379]186 phone->state = IPC_PHONE_FREE;
[9f22213]187 atomic_set(&phone->active_calls, 0);
[6d9c49a]188}
189
[8b243f2]190/** Answer a message which was not dispatched and is not listed in any queue.
191 *
[da1bafb]192 * @param call Call structure to be answered.
193 * @param selflocked If true, then TASK->answebox is locked.
194 *
[ba81cab]195 */
[f9841e69]196void _ipc_answer_free_call(call_t *call, bool selflocked)
[ba81cab]197{
[95319bd]198 /* Count sent answer */
[da1bafb]199 irq_spinlock_lock(&TASK->lock, true);
[a307beb]200 TASK->ipc_info.answer_sent++;
[da1bafb]201 irq_spinlock_unlock(&TASK->lock, true);
[2405bb5]202
203 spinlock_lock(&call->forget_lock);
204 if (call->forget) {
205 /* This is a forgotten call and call->sender is not valid. */
206 spinlock_unlock(&call->forget_lock);
[7975433]207 ipc_call_free(call);
[2405bb5]208 return;
209 } else {
210 /*
[20282ef3]211 * If the call is still active, i.e. it was answered
212 * in a non-standard way, remove the call from the
213 * sender's active call list.
[2405bb5]214 */
[20282ef3]215 if (call->active) {
216 spinlock_lock(&call->sender->active_calls_lock);
217 list_remove(&call->ta_link);
218 spinlock_unlock(&call->sender->active_calls_lock);
219 }
[2405bb5]220 }
221 spinlock_unlock(&call->forget_lock);
222
[6d351e6]223 answerbox_t *callerbox = call->callerbox;
[2405bb5]224 bool do_lock = ((!selflocked) || (callerbox != &TASK->answerbox));
[da1bafb]225
[ba81cab]226 call->flags |= IPC_CALL_ANSWERED;
[da1bafb]227
[ab34cc9]228 call->data.task_id = TASK->taskid;
[da1bafb]229
[c713aa56]230 if (do_lock)
[da1bafb]231 irq_spinlock_lock(&callerbox->lock, true);
232
[cfaa35a]233 list_append(&call->ab_link, &callerbox->answers);
[da1bafb]234
[c713aa56]235 if (do_lock)
[da1bafb]236 irq_spinlock_unlock(&callerbox->lock, true);
237
[5c8ba05]238 waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
[ba81cab]239}
240
[8b243f2]241/** Answer a message which is in a callee queue.
[ba81cab]242 *
[da1bafb]243 * @param box Answerbox that is answering the message.
244 * @param call Modified request that is being sent back.
245 *
[ba81cab]246 */
247void ipc_answer(answerbox_t *box, call_t *call)
248{
249 /* Remove from active box */
[da1bafb]250 irq_spinlock_lock(&box->lock, true);
[cfaa35a]251 list_remove(&call->ab_link);
[da1bafb]252 irq_spinlock_unlock(&box->lock, true);
253
[ba81cab]254 /* Send back answer */
[c713aa56]255 _ipc_answer_free_call(call, false);
[ba81cab]256}
257
[6f9c8f6]258static void _ipc_call_actions_internal(phone_t *phone, call_t *call)
[7c7aae16]259{
[03a8a8e]260 task_t *caller = phone->caller;
261
[7c7aae16]262 atomic_inc(&phone->active_calls);
[c97b086]263 call->caller_phone = phone;
[03a8a8e]264 call->sender = caller;
[86939b1]265
[c97b086]266 call->active = true;
[03a8a8e]267 spinlock_lock(&caller->active_calls_lock);
268 list_append(&call->ta_link, &caller->active_calls);
269 spinlock_unlock(&caller->active_calls_lock);
[86939b1]270
[c97b086]271 call->data.phone = phone;
[6f9c8f6]272 call->data.task_id = caller->taskid;
273}
[c97b086]274
[6f9c8f6]275/** Simulate sending back a message.
276 *
277 * Most errors are better handled by forming a normal backward
278 * message and sending it as a normal answer.
279 *
280 * @param phone Phone structure the call should appear to come from.
281 * @param call Call structure to be answered.
282 * @param err Return value to be used for the answer.
283 *
284 */
285void ipc_backsend_err(phone_t *phone, call_t *call, sysarg_t err)
286{
287 _ipc_call_actions_internal(phone, call);
[eb3d379]288 IPC_SET_RETVAL(call->data, err);
[c713aa56]289 _ipc_answer_free_call(call, false);
[7c7aae16]290}
291
[8b243f2]292/** Unsafe unchecking version of ipc_call.
293 *
[da1bafb]294 * @param phone Phone structure the call comes from.
295 * @param box Destination answerbox structure.
296 * @param call Call structure with request.
297 *
[8b243f2]298 */
[fbcfd458]299static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
300{
[03a8a8e]301 task_t *caller = phone->caller;
302
[95319bd]303 /* Count sent ipc call */
[03a8a8e]304 irq_spinlock_lock(&caller->lock, true);
305 caller->ipc_info.call_sent++;
306 irq_spinlock_unlock(&caller->lock, true);
[da1bafb]307
[6f9c8f6]308 if (!(call->flags & IPC_CALL_FORWARDED))
309 _ipc_call_actions_internal(phone, call);
[da1bafb]310
311 irq_spinlock_lock(&box->lock, true);
[cfaa35a]312 list_append(&call->ab_link, &box->calls);
[da1bafb]313 irq_spinlock_unlock(&box->lock, true);
314
[5c8ba05]315 waitq_wakeup(&box->wq, WAKEUP_FIRST);
[fbcfd458]316}
317
[8b243f2]318/** Send an asynchronous request using a phone to an answerbox.
319 *
[da1bafb]320 * @param phone Phone structure the call comes from and which is
321 * connected to the destination answerbox.
322 * @param call Call structure with request.
323 *
324 * @return Return 0 on success, ENOENT on error.
[6d9c49a]325 *
326 */
[9f22213]327int ipc_call(phone_t *phone, call_t *call)
[6d9c49a]328{
[ff48a15]329 mutex_lock(&phone->lock);
[eb3d379]330 if (phone->state != IPC_PHONE_CONNECTED) {
[ff48a15]331 mutex_unlock(&phone->lock);
[f9841e69]332 if (!(call->flags & IPC_CALL_FORWARDED)) {
[eb3d379]333 if (phone->state == IPC_PHONE_HUNGUP)
[7c7aae16]334 ipc_backsend_err(phone, call, EHANGUP);
[9f22213]335 else
[7c7aae16]336 ipc_backsend_err(phone, call, ENOENT);
[9f22213]337 }
[da1bafb]338
[9f22213]339 return ENOENT;
[ba81cab]340 }
[da1bafb]341
342 answerbox_t *box = phone->callee;
[fbcfd458]343 _ipc_call(phone, box, call);
344
[ff48a15]345 mutex_unlock(&phone->lock);
[9f22213]346 return 0;
[fbcfd458]347}
348
[8b243f2]349/** Disconnect phone from answerbox.
[fbcfd458]350 *
[8b243f2]351 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
[eb3d379]352 * lazily later.
[fbcfd458]353 *
[da1bafb]354 * @param phone Phone structure to be hung up.
355 *
356 * @return 0 if the phone is disconnected.
357 * @return -1 if the phone was already disconnected.
358 *
[fbcfd458]359 */
[d8f7362]360int ipc_phone_hangup(phone_t *phone)
[fbcfd458]361{
[ff48a15]362 mutex_lock(&phone->lock);
[7918fce]363 if (phone->state == IPC_PHONE_FREE ||
364 phone->state == IPC_PHONE_HUNGUP ||
[8b243f2]365 phone->state == IPC_PHONE_CONNECTING) {
[ff48a15]366 mutex_unlock(&phone->lock);
[eb3d379]367 return -1;
[fbcfd458]368 }
[da1bafb]369
370 answerbox_t *box = phone->callee;
[eb3d379]371 if (phone->state != IPC_PHONE_SLAMMED) {
372 /* Remove myself from answerbox */
[da1bafb]373 irq_spinlock_lock(&box->lock, true);
[c4e4507]374 list_remove(&phone->link);
[da1bafb]375 irq_spinlock_unlock(&box->lock, true);
376
377 call_t *call = ipc_call_alloc(0);
[228e490]378 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[796692c]379 call->request_method = IPC_M_PHONE_HUNGUP;
[287e83f]380 call->flags |= IPC_CALL_DISCARD_ANSWER;
381 _ipc_call(phone, box, call);
[eb3d379]382 }
[da1bafb]383
[eb3d379]384 phone->state = IPC_PHONE_HUNGUP;
[ff48a15]385 mutex_unlock(&phone->lock);
[da1bafb]386
[fbcfd458]387 return 0;
[2ba7810]388}
389
[8b243f2]390/** Forwards call from one answerbox to another one.
[2ba7810]391 *
[da1bafb]392 * @param call Call structure to be redirected.
393 * @param newphone Phone structure to target answerbox.
394 * @param oldbox Old answerbox structure.
395 * @param mode Flags that specify mode of the forward operation.
396 *
397 * @return 0 if forwarding succeeded or an error code if
398 * there was an error.
[8b243f2]399 *
400 * The return value serves only as an information for the forwarder,
401 * the original caller is notified automatically with EFORWARD.
[da1bafb]402 *
[2ba7810]403 */
[da1bafb]404int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
405 unsigned int mode)
[2ba7810]406{
[95319bd]407 /* Count forwarded calls */
[da1bafb]408 irq_spinlock_lock(&TASK->lock, true);
[a307beb]409 TASK->ipc_info.forwarded++;
[da1bafb]410 irq_spinlock_pass(&TASK->lock, &oldbox->lock);
[cfaa35a]411 list_remove(&call->ab_link);
[da1bafb]412 irq_spinlock_unlock(&oldbox->lock, true);
413
[645d9ed2]414 if (mode & IPC_FF_ROUTE_FROM_ME) {
[9201f47]415 call->data.phone = newphone;
[e2ab36f1]416 call->data.task_id = TASK->taskid;
[645d9ed2]417 }
[da1bafb]418
[9f22213]419 return ipc_call(newphone, call);
[6d9c49a]420}
421
422
[8b243f2]423/** Wait for a phone call.
[6d9c49a]424 *
[da1bafb]425 * @param box Answerbox expecting the call.
426 * @param usec Timeout in microseconds. See documentation for
427 * waitq_sleep_timeout() for decription of its special
428 * meaning.
429 * @param flags Select mode of sleep operation. See documentation for
430 * waitq_sleep_timeout() for description of its special
431 * meaning.
432 *
433 * @return Recived call structure or NULL.
434 *
[8b243f2]435 * To distinguish between a call and an answer, have a look at call->flags.
[da1bafb]436 *
[6d9c49a]437 */
[da1bafb]438call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags)
[6d9c49a]439{
440 call_t *request;
[aa028db]441 uint64_t irq_cnt = 0;
442 uint64_t answer_cnt = 0;
443 uint64_t call_cnt = 0;
[bd5a663]444 int rc;
[da1bafb]445
[bd5a663]446restart:
[116d1ef4]447 rc = waitq_sleep_timeout(&box->wq, usec, flags);
[bd5a663]448 if (SYNCH_FAILED(rc))
449 return NULL;
[fbcfd458]450
[da1bafb]451 irq_spinlock_lock(&box->lock, true);
[5626277]452 if (!list_empty(&box->irq_notifs)) {
[be06914]453 /* Count received IRQ notification */
[da1bafb]454 irq_cnt++;
455
456 irq_spinlock_lock(&box->irq_lock, false);
457
[55b77d9]458 request = list_get_instance(list_first(&box->irq_notifs),
[cfaa35a]459 call_t, ab_link);
460 list_remove(&request->ab_link);
[da1bafb]461
462 irq_spinlock_unlock(&box->irq_lock, false);
[5626277]463 } else if (!list_empty(&box->answers)) {
[be06914]464 /* Count received answer */
[aa028db]465 answer_cnt++;
[da1bafb]466
[fbcfd458]467 /* Handle asynchronous answers */
[55b77d9]468 request = list_get_instance(list_first(&box->answers),
[cfaa35a]469 call_t, ab_link);
470 list_remove(&request->ab_link);
[5a77550]471 atomic_dec(&request->caller_phone->active_calls);
[fbcfd458]472 } else if (!list_empty(&box->calls)) {
[be06914]473 /* Count received call */
[aa028db]474 call_cnt++;
[da1bafb]475
[fbcfd458]476 /* Handle requests */
[55b77d9]477 request = list_get_instance(list_first(&box->calls),
[cfaa35a]478 call_t, ab_link);
479 list_remove(&request->ab_link);
[da1bafb]480
[fbcfd458]481 /* Append request to dispatch queue */
[cfaa35a]482 list_append(&request->ab_link, &box->dispatched_calls);
[fbcfd458]483 } else {
[874621f]484 /* This can happen regularly after ipc_cleanup */
[da1bafb]485 irq_spinlock_unlock(&box->lock, true);
[fbcfd458]486 goto restart;
[6d9c49a]487 }
[aa028db]488
[da1bafb]489 irq_spinlock_pass(&box->lock, &TASK->lock);
490
[be06914]491 TASK->ipc_info.irq_notif_received += irq_cnt;
492 TASK->ipc_info.answer_received += answer_cnt;
493 TASK->ipc_info.call_received += call_cnt;
[da1bafb]494
495 irq_spinlock_unlock(&TASK->lock, true);
496
[6d9c49a]497 return request;
498}
499
[8b243f2]500/** Answer all calls from list with EHANGUP answer.
501 *
[5d3ed34]502 * @param box Answerbox with the list.
[da1bafb]503 * @param lst Head of the list to be cleaned up.
[8b243f2]504 */
[5d3ed34]505void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
[ca687ad]506{
[5d3ed34]507 irq_spinlock_lock(&box->lock, true);
[ca687ad]508 while (!list_empty(lst)) {
[cfaa35a]509 call_t *call = list_get_instance(list_first(lst), call_t,
510 ab_link);
[da1bafb]511
[cfaa35a]512 list_remove(&call->ab_link);
[5d3ed34]513
514 irq_spinlock_unlock(&box->lock, true);
515
[466e95f7]516 if (lst == &box->calls)
517 SYSIPC_OP(request_process, call, box);
[716185d]518
[5d3ed34]519 ipc_data_t old = call->data;
[ca687ad]520 IPC_SET_RETVAL(call->data, EHANGUP);
[5d3ed34]521 answer_preprocess(call, &old);
[c713aa56]522 _ipc_answer_free_call(call, true);
[5d3ed34]523
524 irq_spinlock_lock(&box->lock, true);
[ca687ad]525 }
[5d3ed34]526 irq_spinlock_unlock(&box->lock, true);
[ca687ad]527}
528
[9a1b20c]529/** Disconnects all phones connected to an answerbox.
[4e49572]530 *
[da1bafb]531 * @param box Answerbox to disconnect phones from.
532 * @param notify_box If true, the answerbox will get a hangup message for
533 * each disconnected phone.
534 *
[4e49572]535 */
[9a1b20c]536void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
[4e49572]537{
[ca687ad]538 phone_t *phone;
[31d8e10]539 DEADLOCK_PROBE_INIT(p_phonelck);
[da1bafb]540
541 call_t *call = notify_box ? ipc_call_alloc(0) : NULL;
542
[ca687ad]543 /* Disconnect all phones connected to our answerbox */
544restart_phones:
[da1bafb]545 irq_spinlock_lock(&box->lock, true);
[9a1b20c]546 while (!list_empty(&box->connected_phones)) {
[55b77d9]547 phone = list_get_instance(list_first(&box->connected_phones),
[31d8e10]548 phone_t, link);
[ff48a15]549 if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
[da1bafb]550 irq_spinlock_unlock(&box->lock, true);
[31d8e10]551 DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
[ca687ad]552 goto restart_phones;
553 }
554
555 /* Disconnect phone */
[eb3d379]556 ASSERT(phone->state == IPC_PHONE_CONNECTED);
[da1bafb]557
[c4e4507]558 list_remove(&phone->link);
[9a1b20c]559 phone->state = IPC_PHONE_SLAMMED;
[da1bafb]560
[9a1b20c]561 if (notify_box) {
562 mutex_unlock(&phone->lock);
[da1bafb]563 irq_spinlock_unlock(&box->lock, true);
[f1d5ef8]564
565 // FIXME: phone can become deallocated at any time now
566
[9a1b20c]567 /*
568 * Send one message to the answerbox for each
569 * phone. Used to make sure the kbox thread
570 * wakes up after the last phone has been
571 * disconnected.
572 */
[228e490]573 IPC_SET_IMETHOD(call->data, IPC_M_PHONE_HUNGUP);
[796692c]574 call->request_method = IPC_M_PHONE_HUNGUP;
[9a1b20c]575 call->flags |= IPC_CALL_DISCARD_ANSWER;
576 _ipc_call(phone, box, call);
[da1bafb]577
[9a1b20c]578 /* Allocate another call in advance */
579 call = ipc_call_alloc(0);
[da1bafb]580
[9a1b20c]581 /* Must start again */
582 goto restart_phones;
583 }
[da1bafb]584
[ff48a15]585 mutex_unlock(&phone->lock);
[ca687ad]586 }
[da1bafb]587
588 irq_spinlock_unlock(&box->lock, true);
589
[9a1b20c]590 /* Free unused call */
[119c335]591 if (call)
592 ipc_call_free(call);
[9a1b20c]593}
594
[2405bb5]595static void ipc_forget_all_active_calls(void)
596{
597 call_t *call;
598
599restart:
600 spinlock_lock(&TASK->active_calls_lock);
601 if (list_empty(&TASK->active_calls)) {
602 /*
603 * We are done, there are no more active calls.
604 * Nota bene: there may still be answers waiting for pick up.
605 */
606 spinlock_unlock(&TASK->active_calls_lock);
607 return;
608 }
609
610 call = list_get_instance(list_first(&TASK->active_calls), call_t,
611 ta_link);
612
613 if (!spinlock_trylock(&call->forget_lock)) {
614 /*
[20282ef3]615 * Avoid deadlock and let async_answer() or
616 * _ipc_answer_free_call() win the race to dequeue the first
617 * call on the list.
[2405bb5]618 */
619 spinlock_unlock(&TASK->active_calls_lock);
620 goto restart;
621 }
622
623 /*
624 * Forget the call and donate it to the task which holds up the answer.
625 */
626
627 call->forget = true;
628 call->sender = NULL;
629 list_remove(&call->ta_link);
630
[cd671c3]631 /*
632 * The call may be freed by _ipc_answer_free_call() before we are done
633 * with it; to avoid working with a destroyed call_t structure, we
634 * must hold a reference to it.
635 */
636 ipc_call_hold(call);
637
[2405bb5]638 spinlock_unlock(&call->forget_lock);
639 spinlock_unlock(&TASK->active_calls_lock);
[b1e6269]640
[5a77550]641 atomic_dec(&call->caller_phone->active_calls);
642
[466e95f7]643 SYSIPC_OP(request_forget, call);
[b1e6269]644
[cd671c3]645 ipc_call_release(call);
646
[2405bb5]647 goto restart;
648}
649
650/** Wait for all answers to asynchronous calls to arrive. */
651static void ipc_wait_for_all_answered_calls(void)
652{
653 call_t *call;
654 size_t i;
655
656restart:
657 /*
[d891cba]658 * Go through all phones, until they are all free.
659 * Locking is needed as there may be connection handshakes in progress.
[2405bb5]660 */
661 for (i = 0; i < IPC_MAX_PHONES; i++) {
[d891cba]662 phone_t *phone = &TASK->phones[i];
663
664 mutex_lock(&phone->lock);
665 if ((phone->state == IPC_PHONE_HUNGUP) &&
666 (atomic_get(&phone->active_calls) == 0)) {
667 phone->state = IPC_PHONE_FREE;
668 phone->callee = NULL;
[2405bb5]669 }
670
671 /*
[f9bd2e3]672 * We might have had some IPC_PHONE_CONNECTING phones at the
673 * beginning of ipc_cleanup(). Depending on whether these were
674 * forgotten or answered, they will eventually enter the
675 * IPC_PHONE_FREE or IPC_PHONE_CONNECTED states, respectively.
676 * In the latter case, the other side may slam the open phones
677 * at any time, in which case we will get an IPC_PHONE_SLAMMED
678 * phone.
[2405bb5]679 */
[d891cba]680 if ((phone->state == IPC_PHONE_CONNECTED) ||
681 (phone->state == IPC_PHONE_SLAMMED)) {
682 mutex_unlock(&phone->lock);
683 ipc_phone_hangup(phone);
[8f6858d0]684 /*
[f9bd2e3]685 * Now there may be one extra active call, which needs
686 * to be forgotten.
[8f6858d0]687 */
688 ipc_forget_all_active_calls();
689 goto restart;
690 }
[2405bb5]691
692 /*
693 * If the hangup succeeded, it has sent a HANGUP message, the
694 * IPC is now in HUNGUP state, we wait for the reply to come
695 */
[d891cba]696 if (phone->state != IPC_PHONE_FREE) {
697 mutex_unlock(&phone->lock);
[2405bb5]698 break;
[d891cba]699 }
700
701 mutex_unlock(&phone->lock);
[2405bb5]702 }
703
704 /* Got into cleanup */
705 if (i == IPC_MAX_PHONES)
706 return;
707
708 call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
709 SYNCH_FLAGS_NONE);
[525e91b]710 ASSERT(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
[675fcbd]711
[466e95f7]712 SYSIPC_OP(answer_process, call);
[675fcbd]713
[2405bb5]714 ipc_call_free(call);
715 goto restart;
716}
717
[da1bafb]718/** Clean up all IPC communication of the current task.
[9a1b20c]719 *
720 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
721 * have to change it as well if you want to cleanup other tasks than TASK.
[da1bafb]722 *
[9a1b20c]723 */
724void ipc_cleanup(void)
725{
[c33f39f]726 /*
727 * Mark the answerbox as inactive.
728 *
729 * The main purpose for doing this is to prevent any pending callback
730 * connections from getting established beyond this point.
731 */
732 irq_spinlock_lock(&TASK->answerbox.lock, true);
733 TASK->answerbox.active = false;
734 irq_spinlock_unlock(&TASK->answerbox.lock, true);
735
[9a1b20c]736 /* Disconnect all our phones ('ipc_phone_hangup') */
[2405bb5]737 for (size_t i = 0; i < IPC_MAX_PHONES; i++)
[9a1b20c]738 ipc_phone_hangup(&TASK->phones[i]);
[da1bafb]739
[05641a9e]740 /* Unsubscribe from any event notifications. */
741 event_cleanup_answerbox(&TASK->answerbox);
[da1bafb]742
[9a1b20c]743 /* Disconnect all connected irqs */
744 ipc_irq_cleanup(&TASK->answerbox);
[da1bafb]745
[9a1b20c]746 /* Disconnect all phones connected to our regular answerbox */
747 ipc_answerbox_slam_phones(&TASK->answerbox, false);
[da1bafb]748
[9a1b20c]749#ifdef CONFIG_UDEBUG
750 /* Clean up kbox thread and communications */
751 ipc_kbox_cleanup();
752#endif
[da1bafb]753
[9f22213]754 /* Answer all messages in 'calls' and 'dispatched_calls' queues */
[716185d]755 ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
[5d3ed34]756 ipc_cleanup_call_list(&TASK->answerbox,
757 &TASK->answerbox.dispatched_calls);
[2405bb5]758
759 ipc_forget_all_active_calls();
760 ipc_wait_for_all_answered_calls();
[4e49572]761}
[5626277]762
[da1bafb]763/** Initilize IPC subsystem
764 *
765 */
[5626277]766void ipc_init(void)
767{
[f97f1e51]768 ipc_call_slab = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
[8b243f2]769 NULL, 0);
[f97f1e51]770 ipc_answerbox_slab = slab_cache_create("answerbox_t",
[c70ce74]771 sizeof(answerbox_t), 0, NULL, NULL, 0);
[5626277]772}
773
[9c9903a9]774
775static void ipc_print_call_list(list_t *list)
776{
[feeac0d]777 list_foreach(*list, ab_link, call_t, call) {
[9c9903a9]778#ifdef __32_BITS__
779 printf("%10p ", call);
780#endif
781
782#ifdef __64_BITS__
783 printf("%18p ", call);
784#endif
785
786 spinlock_lock(&call->forget_lock);
787
788 printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
789 " %-6" PRIun " %-6" PRIun " %-7x",
790 IPC_GET_IMETHOD(call->data), IPC_GET_ARG1(call->data),
791 IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
792 IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
793 call->flags);
794
795 if (call->forget) {
796 printf(" ? (call forgotten)\n");
797 } else {
798 printf(" %" PRIu64 " (%s)\n",
799 call->sender->taskid, call->sender->name);
800 }
801
802 spinlock_unlock(&call->forget_lock);
803 }
804}
805
[8b243f2]806/** List answerbox contents.
807 *
[da1bafb]808 * @param taskid Task ID.
809 *
[8b243f2]810 */
[c4e4507]811void ipc_print_task(task_id_t taskid)
812{
[da1bafb]813 irq_spinlock_lock(&tasks_lock, true);
814 task_t *task = task_find_by_id(taskid);
815
[170332d]816 if (!task) {
[da1bafb]817 irq_spinlock_unlock(&tasks_lock, true);
[c4e4507]818 return;
[170332d]819 }
[da1bafb]820
821 /* Hand-over-hand locking */
822 irq_spinlock_exchange(&tasks_lock, &task->lock);
823
[5378f99]824 printf("[phone id] [calls] [state\n");
[da1bafb]825
826 size_t i;
[8b243f2]827 for (i = 0; i < IPC_MAX_PHONES; i++) {
[ff48a15]828 if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
[5378f99]829 printf("%-10zu (mutex busy)\n", i);
[ff48a15]830 continue;
831 }
[da1bafb]832
[c4e4507]833 if (task->phones[i].state != IPC_PHONE_FREE) {
[5378f99]834 printf("%-10zu %7" PRIun " ", i,
835 atomic_get(&task->phones[i].active_calls));
[da1bafb]836
[c4e4507]837 switch (task->phones[i].state) {
838 case IPC_PHONE_CONNECTING:
[5378f99]839 printf("connecting");
[c4e4507]840 break;
841 case IPC_PHONE_CONNECTED:
[5378f99]842 printf("connected to %" PRIu64 " (%s)",
843 task->phones[i].callee->task->taskid,
844 task->phones[i].callee->task->name);
[c4e4507]845 break;
846 case IPC_PHONE_SLAMMED:
[5378f99]847 printf("slammed by %p",
[da1bafb]848 task->phones[i].callee);
[c4e4507]849 break;
850 case IPC_PHONE_HUNGUP:
[5378f99]851 printf("hung up by %p",
[da1bafb]852 task->phones[i].callee);
[c4e4507]853 break;
854 default:
855 break;
856 }
[da1bafb]857
[5378f99]858 printf("\n");
[c4e4507]859 }
[da1bafb]860
[ff48a15]861 mutex_unlock(&task->phones[i].lock);
[c4e4507]862 }
[da1bafb]863
864 irq_spinlock_lock(&task->answerbox.lock, false);
865
[5378f99]866#ifdef __32_BITS__
867 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
868 " [flags] [sender\n");
869#endif
870
871#ifdef __64_BITS__
872 printf("[call id ] [method] [arg1] [arg2] [arg3] [arg4]"
873 " [arg5] [flags] [sender\n");
874#endif
875
876 printf(" --- incomming calls ---\n");
[9c9903a9]877 ipc_print_call_list(&task->answerbox.calls);
[5378f99]878 printf(" --- dispatched calls ---\n");
[9c9903a9]879 ipc_print_call_list(&task->answerbox.dispatched_calls);
[6aef742]880 printf(" --- incoming answers ---\n");
[9c9903a9]881 ipc_print_call_list(&task->answerbox.answers);
[da1bafb]882
883 irq_spinlock_unlock(&task->answerbox.lock, false);
884 irq_spinlock_unlock(&task->lock, true);
[c4e4507]885}
[b45c443]886
[cc73a8a1]887/** @}
[b45c443]888 */
Note: See TracBrowser for help on using the repository browser.