Rev 2622 | Rev 2637 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
955 | palkovsky | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2006 Ondrej Palkovsky |
955 | 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 | |||
1757 | jermar | 29 | /** @addtogroup genericipc |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | /** @file |
||
33 | */ |
||
34 | |||
955 | palkovsky | 35 | /* Lock ordering |
36 | * |
||
2471 | jermar | 37 | * First the answerbox, then the phone. |
955 | palkovsky | 38 | */ |
39 | |||
1040 | palkovsky | 40 | #include <synch/spinlock.h> |
41 | #include <synch/waitq.h> |
||
1364 | jermar | 42 | #include <synch/synch.h> |
955 | palkovsky | 43 | #include <ipc/ipc.h> |
44 | #include <errno.h> |
||
45 | #include <mm/slab.h> |
||
46 | #include <arch.h> |
||
47 | #include <proc/task.h> |
||
48 | #include <memstr.h> |
||
49 | #include <debug.h> |
||
50 | |||
51 | #include <print.h> |
||
52 | #include <proc/thread.h> |
||
1258 | palkovsky | 53 | #include <arch/interrupt.h> |
1281 | palkovsky | 54 | #include <ipc/irq.h> |
955 | palkovsky | 55 | |
2471 | jermar | 56 | /** Open channel that is assigned automatically to new tasks */ |
965 | palkovsky | 57 | answerbox_t *ipc_phone_0 = NULL; |
955 | palkovsky | 58 | |
59 | static slab_cache_t *ipc_call_slab; |
||
60 | |||
2471 | jermar | 61 | /** Initialize a call structure. |
62 | * |
||
63 | * @param call Call structure to be initialized. |
||
64 | */ |
||
1084 | palkovsky | 65 | static void _ipc_call_init(call_t *call) |
66 | { |
||
2471 | jermar | 67 | memsetb((uintptr_t) call, sizeof(*call), 0); |
1084 | palkovsky | 68 | call->callerbox = &TASK->answerbox; |
69 | call->sender = TASK; |
||
2494 | jermar | 70 | call->buffer = NULL; |
1084 | palkovsky | 71 | } |
72 | |||
2471 | jermar | 73 | /** Allocate and initialize a call structure. |
955 | palkovsky | 74 | * |
2471 | jermar | 75 | * The call is initialized, so that the reply will be directed to |
76 | * TASK->answerbox. |
||
1258 | palkovsky | 77 | * |
2471 | jermar | 78 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC). |
79 | * |
||
80 | * @return If flags permit it, return NULL, or initialized kernel |
||
81 | * call structure. |
||
955 | palkovsky | 82 | */ |
2471 | jermar | 83 | call_t *ipc_call_alloc(int flags) |
955 | palkovsky | 84 | { |
85 | call_t *call; |
||
86 | |||
1258 | palkovsky | 87 | call = slab_alloc(ipc_call_slab, flags); |
1084 | palkovsky | 88 | _ipc_call_init(call); |
955 | palkovsky | 89 | |
90 | return call; |
||
91 | } |
||
92 | |||
2471 | jermar | 93 | /** Initialize a statically allocated call structure. |
94 | * |
||
95 | * @param call Statically allocated kernel call structure to be |
||
96 | * initialized. |
||
97 | */ |
||
1084 | palkovsky | 98 | void ipc_call_static_init(call_t *call) |
965 | palkovsky | 99 | { |
1084 | palkovsky | 100 | _ipc_call_init(call); |
101 | call->flags |= IPC_CALL_STATIC_ALLOC; |
||
965 | palkovsky | 102 | } |
103 | |||
2472 | jermar | 104 | /** Deallocate a call structure. |
2471 | jermar | 105 | * |
106 | * @param call Call structure to be freed. |
||
107 | */ |
||
955 | palkovsky | 108 | void ipc_call_free(call_t *call) |
109 | { |
||
2471 | jermar | 110 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
2494 | jermar | 111 | /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */ |
112 | if (call->buffer) |
||
113 | free(call->buffer); |
||
955 | palkovsky | 114 | slab_free(ipc_call_slab, call); |
115 | } |
||
116 | |||
2471 | jermar | 117 | /** Initialize an answerbox structure. |
118 | * |
||
119 | * @param box Answerbox structure to be initialized. |
||
955 | palkovsky | 120 | */ |
121 | void ipc_answerbox_init(answerbox_t *box) |
||
122 | { |
||
1040 | palkovsky | 123 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
1258 | palkovsky | 124 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock"); |
1040 | palkovsky | 125 | waitq_initialize(&box->wq); |
955 | palkovsky | 126 | list_initialize(&box->connected_phones); |
127 | list_initialize(&box->calls); |
||
128 | list_initialize(&box->dispatched_calls); |
||
129 | list_initialize(&box->answers); |
||
1258 | palkovsky | 130 | list_initialize(&box->irq_notifs); |
1933 | jermar | 131 | list_initialize(&box->irq_head); |
1027 | palkovsky | 132 | box->task = TASK; |
955 | palkovsky | 133 | } |
134 | |||
2471 | jermar | 135 | /** Connect a phone to an answerbox. |
136 | * |
||
137 | * @param phone Initialized phone structure. |
||
138 | * @param box Initialized answerbox structure. |
||
139 | */ |
||
1040 | palkovsky | 140 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
955 | palkovsky | 141 | { |
1084 | palkovsky | 142 | spinlock_lock(&phone->lock); |
143 | |||
1568 | palkovsky | 144 | phone->state = IPC_PHONE_CONNECTED; |
955 | palkovsky | 145 | phone->callee = box; |
965 | palkovsky | 146 | |
1040 | palkovsky | 147 | spinlock_lock(&box->lock); |
1573 | palkovsky | 148 | list_append(&phone->link, &box->connected_phones); |
1040 | palkovsky | 149 | spinlock_unlock(&box->lock); |
1084 | palkovsky | 150 | |
151 | spinlock_unlock(&phone->lock); |
||
955 | palkovsky | 152 | } |
153 | |||
2471 | jermar | 154 | /** Initialize a phone structure. |
155 | * |
||
156 | * @param phone Phone structure to be initialized. |
||
1040 | palkovsky | 157 | */ |
158 | void ipc_phone_init(phone_t *phone) |
||
159 | { |
||
160 | spinlock_initialize(&phone->lock, "phone_lock"); |
||
161 | phone->callee = NULL; |
||
1568 | palkovsky | 162 | phone->state = IPC_PHONE_FREE; |
1088 | palkovsky | 163 | atomic_set(&phone->active_calls, 0); |
1040 | palkovsky | 164 | } |
165 | |||
2471 | jermar | 166 | /** Helper function to facilitate synchronous calls. |
167 | * |
||
168 | * @param phone Destination kernel phone structure. |
||
169 | * @param request Call structure with request. |
||
170 | */ |
||
959 | palkovsky | 171 | void ipc_call_sync(phone_t *phone, call_t *request) |
172 | { |
||
173 | answerbox_t sync_box; |
||
955 | palkovsky | 174 | |
959 | palkovsky | 175 | ipc_answerbox_init(&sync_box); |
176 | |||
2471 | jermar | 177 | /* We will receive data in a special box. */ |
959 | palkovsky | 178 | request->callerbox = &sync_box; |
179 | |||
180 | ipc_call(phone, request); |
||
1502 | jermar | 181 | ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE); |
959 | palkovsky | 182 | } |
183 | |||
2471 | jermar | 184 | /** Answer a message which was not dispatched and is not listed in any queue. |
185 | * |
||
186 | * @param call Call structure to be answered. |
||
1084 | palkovsky | 187 | */ |
188 | static void _ipc_answer_free_call(call_t *call) |
||
189 | { |
||
190 | answerbox_t *callerbox = call->callerbox; |
||
191 | |||
192 | call->flags |= IPC_CALL_ANSWERED; |
||
193 | |||
194 | spinlock_lock(&callerbox->lock); |
||
1573 | palkovsky | 195 | list_append(&call->link, &callerbox->answers); |
1084 | palkovsky | 196 | spinlock_unlock(&callerbox->lock); |
2310 | jermar | 197 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST); |
1084 | palkovsky | 198 | } |
199 | |||
2471 | jermar | 200 | /** Answer a message which is in a callee queue. |
1084 | palkovsky | 201 | * |
2471 | jermar | 202 | * @param box Answerbox that is answering the message. |
203 | * @param call Modified request that is being sent back. |
||
1084 | palkovsky | 204 | */ |
205 | void ipc_answer(answerbox_t *box, call_t *call) |
||
206 | { |
||
207 | /* Remove from active box */ |
||
208 | spinlock_lock(&box->lock); |
||
1573 | palkovsky | 209 | list_remove(&call->link); |
1084 | palkovsky | 210 | spinlock_unlock(&box->lock); |
211 | /* Send back answer */ |
||
212 | _ipc_answer_free_call(call); |
||
213 | } |
||
214 | |||
2471 | jermar | 215 | /** Simulate sending back a message. |
1090 | palkovsky | 216 | * |
217 | * Most errors are better handled by forming a normal backward |
||
218 | * message and sending it as a normal answer. |
||
2471 | jermar | 219 | * |
220 | * @param phone Phone structure the call should appear to come from. |
||
221 | * @param call Call structure to be answered. |
||
222 | * @param err Return value to be used for the answer. |
||
1090 | palkovsky | 223 | */ |
1780 | jermar | 224 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err) |
1090 | palkovsky | 225 | { |
226 | call->data.phone = phone; |
||
227 | atomic_inc(&phone->active_calls); |
||
1568 | palkovsky | 228 | IPC_SET_RETVAL(call->data, err); |
1090 | palkovsky | 229 | _ipc_answer_free_call(call); |
230 | } |
||
231 | |||
2471 | jermar | 232 | /** Unsafe unchecking version of ipc_call. |
233 | * |
||
234 | * @param phone Phone structure the call comes from. |
||
235 | * @param box Destination answerbox structure. |
||
2601 | jermar | 236 | * @param call Call structure with request. |
2471 | jermar | 237 | */ |
1086 | palkovsky | 238 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call) |
239 | { |
||
2471 | jermar | 240 | if (!(call->flags & IPC_CALL_FORWARDED)) { |
1088 | palkovsky | 241 | atomic_inc(&phone->active_calls); |
242 | call->data.phone = phone; |
||
243 | } |
||
1086 | palkovsky | 244 | |
245 | spinlock_lock(&box->lock); |
||
1573 | palkovsky | 246 | list_append(&call->link, &box->calls); |
1086 | palkovsky | 247 | spinlock_unlock(&box->lock); |
2310 | jermar | 248 | waitq_wakeup(&box->wq, WAKEUP_FIRST); |
1086 | palkovsky | 249 | } |
250 | |||
2471 | jermar | 251 | /** Send an asynchronous request using a phone to an answerbox. |
955 | palkovsky | 252 | * |
2471 | jermar | 253 | * @param phone Phone structure the call comes from and which is |
254 | * connected to the destination answerbox. |
||
255 | * @param call Call structure with request. |
||
256 | * |
||
257 | * @return Return 0 on success, ENOENT on error. |
||
955 | palkovsky | 258 | */ |
1088 | palkovsky | 259 | int ipc_call(phone_t *phone, call_t *call) |
955 | palkovsky | 260 | { |
1084 | palkovsky | 261 | answerbox_t *box; |
955 | palkovsky | 262 | |
1084 | palkovsky | 263 | spinlock_lock(&phone->lock); |
1568 | palkovsky | 264 | if (phone->state != IPC_PHONE_CONNECTED) { |
1086 | palkovsky | 265 | spinlock_unlock(&phone->lock); |
1088 | palkovsky | 266 | if (call->flags & IPC_CALL_FORWARDED) { |
267 | IPC_SET_RETVAL(call->data, EFORWARD); |
||
1090 | palkovsky | 268 | _ipc_answer_free_call(call); |
1568 | palkovsky | 269 | } else { |
270 | if (phone->state == IPC_PHONE_HUNGUP) |
||
1090 | palkovsky | 271 | ipc_backsend_err(phone, call, EHANGUP); |
1088 | palkovsky | 272 | else |
1090 | palkovsky | 273 | ipc_backsend_err(phone, call, ENOENT); |
1088 | palkovsky | 274 | } |
275 | return ENOENT; |
||
1084 | palkovsky | 276 | } |
1568 | palkovsky | 277 | box = phone->callee; |
1086 | palkovsky | 278 | _ipc_call(phone, box, call); |
279 | |||
280 | spinlock_unlock(&phone->lock); |
||
1088 | palkovsky | 281 | return 0; |
1086 | palkovsky | 282 | } |
955 | palkovsky | 283 | |
2471 | jermar | 284 | /** Disconnect phone from answerbox. |
1086 | palkovsky | 285 | * |
2471 | jermar | 286 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done |
1568 | palkovsky | 287 | * lazily later. |
1086 | palkovsky | 288 | * |
2471 | jermar | 289 | * @param phone Phone structure to be hung up. |
1568 | palkovsky | 290 | * |
2471 | jermar | 291 | * @return Return 0 if the phone is disconnected. |
292 | * Return -1 if the phone was already disconnected. |
||
1086 | palkovsky | 293 | */ |
1591 | palkovsky | 294 | int ipc_phone_hangup(phone_t *phone) |
1086 | palkovsky | 295 | { |
296 | answerbox_t *box; |
||
297 | call_t *call; |
||
298 | |||
299 | spinlock_lock(&phone->lock); |
||
2494 | jermar | 300 | if (phone->state == IPC_PHONE_FREE || |
301 | phone->state == IPC_PHONE_HUNGUP || |
||
2471 | jermar | 302 | phone->state == IPC_PHONE_CONNECTING) { |
1568 | palkovsky | 303 | spinlock_unlock(&phone->lock); |
304 | return -1; |
||
305 | } |
||
1086 | palkovsky | 306 | box = phone->callee; |
1568 | palkovsky | 307 | if (phone->state != IPC_PHONE_SLAMMED) { |
308 | /* Remove myself from answerbox */ |
||
309 | spinlock_lock(&box->lock); |
||
1573 | palkovsky | 310 | list_remove(&phone->link); |
1568 | palkovsky | 311 | spinlock_unlock(&box->lock); |
312 | |||
313 | if (phone->state != IPC_PHONE_SLAMMED) { |
||
314 | call = ipc_call_alloc(0); |
||
315 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
||
316 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
||
317 | _ipc_call(phone, box, call); |
||
1088 | palkovsky | 318 | } |
1086 | palkovsky | 319 | } |
320 | |||
1568 | palkovsky | 321 | phone->state = IPC_PHONE_HUNGUP; |
1084 | palkovsky | 322 | spinlock_unlock(&phone->lock); |
1086 | palkovsky | 323 | |
324 | return 0; |
||
955 | palkovsky | 325 | } |
326 | |||
2471 | jermar | 327 | /** Forwards call from one answerbox to another one. |
1040 | palkovsky | 328 | * |
2471 | jermar | 329 | * @param call Call structure to be redirected. |
330 | * @param newphone Phone structure to target answerbox. |
||
331 | * @param oldbox Old answerbox structure. |
||
2622 | jermar | 332 | * @param mode Flags that specify mode of the forward operation. |
2471 | jermar | 333 | * |
334 | * @return Return 0 if forwarding succeeded or an error code if |
||
335 | * there was error. |
||
1088 | palkovsky | 336 | * |
2471 | jermar | 337 | * The return value serves only as an information for the forwarder, |
338 | * the original caller is notified automatically with EFORWARD. |
||
1040 | palkovsky | 339 | */ |
2622 | jermar | 340 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode) |
1040 | palkovsky | 341 | { |
342 | spinlock_lock(&oldbox->lock); |
||
1573 | palkovsky | 343 | list_remove(&call->link); |
1040 | palkovsky | 344 | spinlock_unlock(&oldbox->lock); |
345 | |||
2623 | jermar | 346 | if (mode & IPC_FF_ROUTE_FROM_ME) |
347 | call->data.phone = newphone; |
||
348 | |||
1088 | palkovsky | 349 | return ipc_call(newphone, call); |
1040 | palkovsky | 350 | } |
351 | |||
955 | palkovsky | 352 | |
2471 | jermar | 353 | /** Wait for a phone call. |
955 | palkovsky | 354 | * |
2471 | jermar | 355 | * @param box Answerbox expecting the call. |
356 | * @param usec Timeout in microseconds. See documentation for |
||
357 | * waitq_sleep_timeout() for decription of its special |
||
358 | * meaning. |
||
359 | * @param flags Select mode of sleep operation. See documentation for |
||
360 | * waitq_sleep_timeout() for description of its special |
||
361 | * meaning. |
||
362 | * @return Recived call structure or NULL. |
||
363 | * |
||
364 | * To distinguish between a call and an answer, have a look at call->flags. |
||
955 | palkovsky | 365 | */ |
2471 | jermar | 366 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags) |
955 | palkovsky | 367 | { |
368 | call_t *request; |
||
1258 | palkovsky | 369 | ipl_t ipl; |
1364 | jermar | 370 | int rc; |
955 | palkovsky | 371 | |
1364 | jermar | 372 | restart: |
1502 | jermar | 373 | rc = waitq_sleep_timeout(&box->wq, usec, flags); |
1364 | jermar | 374 | if (SYNCH_FAILED(rc)) |
375 | return NULL; |
||
1086 | palkovsky | 376 | |
1040 | palkovsky | 377 | spinlock_lock(&box->lock); |
1258 | palkovsky | 378 | if (!list_empty(&box->irq_notifs)) { |
379 | ipl = interrupts_disable(); |
||
380 | spinlock_lock(&box->irq_lock); |
||
381 | |||
1573 | palkovsky | 382 | request = list_get_instance(box->irq_notifs.next, call_t, link); |
383 | list_remove(&request->link); |
||
1258 | palkovsky | 384 | |
385 | spinlock_unlock(&box->irq_lock); |
||
386 | interrupts_restore(ipl); |
||
387 | } else if (!list_empty(&box->answers)) { |
||
1086 | palkovsky | 388 | /* Handle asynchronous answers */ |
1573 | palkovsky | 389 | request = list_get_instance(box->answers.next, call_t, link); |
390 | list_remove(&request->link); |
||
1086 | palkovsky | 391 | atomic_dec(&request->data.phone->active_calls); |
392 | } else if (!list_empty(&box->calls)) { |
||
393 | /* Handle requests */ |
||
1573 | palkovsky | 394 | request = list_get_instance(box->calls.next, call_t, link); |
395 | list_remove(&request->link); |
||
1086 | palkovsky | 396 | /* Append request to dispatch queue */ |
1573 | palkovsky | 397 | list_append(&request->link, &box->dispatched_calls); |
1086 | palkovsky | 398 | } else { |
1595 | palkovsky | 399 | /* This can happen regularly after ipc_cleanup */ |
1086 | palkovsky | 400 | spinlock_unlock(&box->lock); |
401 | goto restart; |
||
955 | palkovsky | 402 | } |
1040 | palkovsky | 403 | spinlock_unlock(&box->lock); |
955 | palkovsky | 404 | return request; |
405 | } |
||
406 | |||
2471 | jermar | 407 | /** Answer all calls from list with EHANGUP answer. |
408 | * |
||
409 | * @param lst Head of the list to be cleaned up. |
||
410 | */ |
||
1141 | palkovsky | 411 | static void ipc_cleanup_call_list(link_t *lst) |
412 | { |
||
413 | call_t *call; |
||
414 | |||
415 | while (!list_empty(lst)) { |
||
1573 | palkovsky | 416 | call = list_get_instance(lst->next, call_t, link); |
417 | list_remove(&call->link); |
||
1141 | palkovsky | 418 | |
419 | IPC_SET_RETVAL(call->data, EHANGUP); |
||
420 | _ipc_answer_free_call(call); |
||
421 | } |
||
422 | } |
||
423 | |||
2471 | jermar | 424 | /** Cleans up all IPC communication of the current task. |
1072 | palkovsky | 425 | * |
1582 | palkovsky | 426 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you |
2471 | jermar | 427 | * have to change it as well if you want to cleanup other tasks than TASK. |
1072 | palkovsky | 428 | */ |
1582 | palkovsky | 429 | void ipc_cleanup(void) |
1072 | palkovsky | 430 | { |
1088 | palkovsky | 431 | int i; |
1141 | palkovsky | 432 | call_t *call; |
433 | phone_t *phone; |
||
2183 | jermar | 434 | DEADLOCK_PROBE_INIT(p_phonelck); |
1582 | palkovsky | 435 | |
1088 | palkovsky | 436 | /* Disconnect all our phones ('ipc_phone_hangup') */ |
2471 | jermar | 437 | for (i = 0; i < IPC_MAX_PHONES; i++) |
1591 | palkovsky | 438 | ipc_phone_hangup(&TASK->phones[i]); |
1088 | palkovsky | 439 | |
1258 | palkovsky | 440 | /* Disconnect all connected irqs */ |
1582 | palkovsky | 441 | ipc_irq_cleanup(&TASK->answerbox); |
1258 | palkovsky | 442 | |
1141 | palkovsky | 443 | /* Disconnect all phones connected to our answerbox */ |
444 | restart_phones: |
||
1582 | palkovsky | 445 | spinlock_lock(&TASK->answerbox.lock); |
446 | while (!list_empty(&TASK->answerbox.connected_phones)) { |
||
447 | phone = list_get_instance(TASK->answerbox.connected_phones.next, |
||
2183 | jermar | 448 | phone_t, link); |
2472 | jermar | 449 | if (!spinlock_trylock(&phone->lock)) { |
1582 | palkovsky | 450 | spinlock_unlock(&TASK->answerbox.lock); |
2183 | jermar | 451 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD); |
1141 | palkovsky | 452 | goto restart_phones; |
453 | } |
||
454 | |||
455 | /* Disconnect phone */ |
||
1568 | palkovsky | 456 | ASSERT(phone->state == IPC_PHONE_CONNECTED); |
457 | phone->state = IPC_PHONE_SLAMMED; |
||
1573 | palkovsky | 458 | list_remove(&phone->link); |
1088 | palkovsky | 459 | |
1141 | palkovsky | 460 | spinlock_unlock(&phone->lock); |
461 | } |
||
462 | |||
1088 | palkovsky | 463 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
1582 | palkovsky | 464 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls); |
465 | ipc_cleanup_call_list(&TASK->answerbox.calls); |
||
466 | spinlock_unlock(&TASK->answerbox.lock); |
||
1072 | palkovsky | 467 | |
1088 | palkovsky | 468 | /* Wait for all async answers to arrive */ |
1568 | palkovsky | 469 | while (1) { |
470 | /* Go through all phones, until all are FREE... */ |
||
471 | /* Locking not needed, no one else should modify |
||
472 | * it, when we are in cleanup */ |
||
2471 | jermar | 473 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
474 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP && |
||
1582 | palkovsky | 475 | atomic_get(&TASK->phones[i].active_calls) == 0) |
476 | TASK->phones[i].state = IPC_PHONE_FREE; |
||
477 | |||
1591 | palkovsky | 478 | /* Just for sure, we might have had some |
479 | * IPC_PHONE_CONNECTING phones */ |
||
1582 | palkovsky | 480 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED) |
1591 | palkovsky | 481 | ipc_phone_hangup(&TASK->phones[i]); |
482 | /* If the hangup succeeded, it has sent a HANGUP |
||
483 | * message, the IPC is now in HUNGUP state, we |
||
484 | * wait for the reply to come */ |
||
1582 | palkovsky | 485 | |
486 | if (TASK->phones[i].state != IPC_PHONE_FREE) |
||
1568 | palkovsky | 487 | break; |
488 | } |
||
489 | /* Voila, got into cleanup */ |
||
490 | if (i == IPC_MAX_PHONES) |
||
491 | break; |
||
492 | |||
2471 | jermar | 493 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT, |
494 | SYNCH_FLAGS_NONE); |
||
495 | ASSERT((call->flags & IPC_CALL_ANSWERED) || |
||
496 | (call->flags & IPC_CALL_NOTIF)); |
||
497 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
||
1141 | palkovsky | 498 | |
1582 | palkovsky | 499 | atomic_dec(&TASK->active_calls); |
1141 | palkovsky | 500 | ipc_call_free(call); |
501 | } |
||
1072 | palkovsky | 502 | } |
1258 | palkovsky | 503 | |
504 | |||
1923 | jermar | 505 | /** Initilize IPC subsystem */ |
1258 | palkovsky | 506 | void ipc_init(void) |
507 | { |
||
2471 | jermar | 508 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL, |
509 | NULL, 0); |
||
1258 | palkovsky | 510 | } |
511 | |||
1573 | palkovsky | 512 | |
2471 | jermar | 513 | /** List answerbox contents. |
514 | * |
||
515 | * @param taskid Task ID. |
||
516 | */ |
||
1573 | palkovsky | 517 | void ipc_print_task(task_id_t taskid) |
518 | { |
||
519 | task_t *task; |
||
520 | int i; |
||
521 | call_t *call; |
||
522 | link_t *tmp; |
||
523 | |||
524 | spinlock_lock(&tasks_lock); |
||
525 | task = task_find_by_id(taskid); |
||
526 | if (task) |
||
527 | spinlock_lock(&task->lock); |
||
528 | spinlock_unlock(&tasks_lock); |
||
529 | if (!task) |
||
530 | return; |
||
531 | |||
532 | /* Print opened phones & details */ |
||
533 | printf("PHONE:\n"); |
||
2471 | jermar | 534 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
1573 | palkovsky | 535 | spinlock_lock(&task->phones[i].lock); |
536 | if (task->phones[i].state != IPC_PHONE_FREE) { |
||
2471 | jermar | 537 | printf("%d: ", i); |
1573 | palkovsky | 538 | switch (task->phones[i].state) { |
539 | case IPC_PHONE_CONNECTING: |
||
540 | printf("connecting "); |
||
541 | break; |
||
542 | case IPC_PHONE_CONNECTED: |
||
1735 | decky | 543 | printf("connected to: %p ", |
1573 | palkovsky | 544 | task->phones[i].callee); |
545 | break; |
||
546 | case IPC_PHONE_SLAMMED: |
||
1735 | decky | 547 | printf("slammed by: %p ", |
1573 | palkovsky | 548 | task->phones[i].callee); |
549 | break; |
||
550 | case IPC_PHONE_HUNGUP: |
||
1735 | decky | 551 | printf("hung up - was: %p ", |
1573 | palkovsky | 552 | task->phones[i].callee); |
553 | break; |
||
554 | default: |
||
555 | break; |
||
556 | } |
||
2472 | jermar | 557 | printf("active: %d\n", |
558 | atomic_get(&task->phones[i].active_calls)); |
||
1573 | palkovsky | 559 | } |
560 | spinlock_unlock(&task->phones[i].lock); |
||
561 | } |
||
562 | |||
563 | |||
564 | /* Print answerbox - calls */ |
||
565 | spinlock_lock(&task->answerbox.lock); |
||
566 | printf("ABOX - CALLS:\n"); |
||
2472 | jermar | 567 | for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls; |
568 | tmp = tmp->next) { |
||
1573 | palkovsky | 569 | call = list_get_instance(tmp, call_t, link); |
2472 | jermar | 570 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d " |
571 | "Flags:%x\n", call, call->sender->taskid, |
||
572 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
||
573 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
||
574 | call->flags); |
||
1573 | palkovsky | 575 | } |
576 | /* Print answerbox - calls */ |
||
577 | printf("ABOX - DISPATCHED CALLS:\n"); |
||
578 | for (tmp=task->answerbox.dispatched_calls.next; |
||
579 | tmp != &task->answerbox.dispatched_calls; |
||
580 | tmp = tmp->next) { |
||
581 | call = list_get_instance(tmp, call_t, link); |
||
2472 | jermar | 582 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d " |
583 | "Flags:%x\n", call, call->sender->taskid, |
||
584 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
||
585 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
||
586 | call->flags); |
||
1573 | palkovsky | 587 | } |
588 | /* Print answerbox - calls */ |
||
589 | printf("ABOX - ANSWERS:\n"); |
||
2472 | jermar | 590 | for (tmp = task->answerbox.answers.next; tmp != &task->answerbox.answers; |
591 | tmp = tmp->next) { |
||
1573 | palkovsky | 592 | call = list_get_instance(tmp, call_t, link); |
2472 | jermar | 593 | printf("Callid:%p M:%d A1:%d A2:%d A3:%d Flags:%x\n", call, |
594 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
||
595 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
||
596 | call->flags); |
||
1573 | palkovsky | 597 | } |
598 | |||
599 | spinlock_unlock(&task->answerbox.lock); |
||
600 | spinlock_unlock(&task->lock); |
||
601 | } |
||
1702 | cejka | 602 | |
1757 | jermar | 603 | /** @} |
1702 | cejka | 604 | */ |