source: mainline/kernel/generic/src/ipc/ipc.c@ 796692c

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

Set request method also for manually sent IPC_M_PHONE_HUNGUP messages.

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