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