Rev 3037 | Rev 3425 | 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 | |||
3042 | svoboda | 40 | #include <synch/synch.h> |
1040 | palkovsky | 41 | #include <synch/spinlock.h> |
3042 | svoboda | 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> |
||
2902 | svoboda | 54 | #include <console/klog.h> |
955 | palkovsky | 55 | #include <proc/thread.h> |
1258 | palkovsky | 56 | #include <arch/interrupt.h> |
1281 | palkovsky | 57 | #include <ipc/irq.h> |
955 | palkovsky | 58 | |
2471 | jermar | 59 | /** Open channel that is assigned automatically to new tasks */ |
965 | palkovsky | 60 | answerbox_t *ipc_phone_0 = NULL; |
955 | palkovsky | 61 | |
62 | static slab_cache_t *ipc_call_slab; |
||
63 | |||
2471 | jermar | 64 | /** Initialize a call structure. |
65 | * |
||
66 | * @param call Call structure to be initialized. |
||
67 | */ |
||
1084 | palkovsky | 68 | static void _ipc_call_init(call_t *call) |
69 | { |
||
2471 | jermar | 70 | memsetb((uintptr_t) call, sizeof(*call), 0); |
1084 | palkovsky | 71 | call->callerbox = &TASK->answerbox; |
72 | call->sender = TASK; |
||
2494 | jermar | 73 | call->buffer = NULL; |
1084 | palkovsky | 74 | } |
75 | |||
2471 | jermar | 76 | /** Allocate and initialize a call structure. |
955 | palkovsky | 77 | * |
2471 | jermar | 78 | * The call is initialized, so that the reply will be directed to |
79 | * TASK->answerbox. |
||
1258 | palkovsky | 80 | * |
2471 | jermar | 81 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC). |
82 | * |
||
83 | * @return If flags permit it, return NULL, or initialized kernel |
||
84 | * call structure. |
||
955 | palkovsky | 85 | */ |
2471 | jermar | 86 | call_t *ipc_call_alloc(int flags) |
955 | palkovsky | 87 | { |
88 | call_t *call; |
||
89 | |||
1258 | palkovsky | 90 | call = slab_alloc(ipc_call_slab, flags); |
1084 | palkovsky | 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. |
||
2800 | svoboda | 123 | * @param task Task to which the answerbox belongs. |
955 | palkovsky | 124 | */ |
2800 | svoboda | 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); |
2800 | svoboda | 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 | { |
3042 | svoboda | 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 | |
3042 | svoboda | 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 | { |
||
3042 | svoboda | 164 | mutex_initialize(&phone->lock); |
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 | |
2800 | svoboda | 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 | |||
198 | spinlock_lock(&callerbox->lock); |
||
1573 | palkovsky | 199 | list_append(&call->link, &callerbox->answers); |
1084 | palkovsky | 200 | spinlock_unlock(&callerbox->lock); |
2310 | jermar | 201 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST); |
1084 | palkovsky | 202 | } |
203 | |||
2471 | jermar | 204 | /** Answer a message which is in a callee queue. |
1084 | palkovsky | 205 | * |
2471 | jermar | 206 | * @param box Answerbox that is answering the message. |
207 | * @param call Modified request that is being sent back. |
||
1084 | palkovsky | 208 | */ |
209 | void ipc_answer(answerbox_t *box, call_t *call) |
||
210 | { |
||
211 | /* Remove from active box */ |
||
212 | spinlock_lock(&box->lock); |
||
1573 | palkovsky | 213 | list_remove(&call->link); |
1084 | palkovsky | 214 | spinlock_unlock(&box->lock); |
215 | /* Send back answer */ |
||
216 | _ipc_answer_free_call(call); |
||
217 | } |
||
218 | |||
2471 | jermar | 219 | /** Simulate sending back a message. |
1090 | palkovsky | 220 | * |
221 | * Most errors are better handled by forming a normal backward |
||
222 | * message and sending it as a normal answer. |
||
2471 | jermar | 223 | * |
224 | * @param phone Phone structure the call should appear to come from. |
||
225 | * @param call Call structure to be answered. |
||
226 | * @param err Return value to be used for the answer. |
||
1090 | palkovsky | 227 | */ |
1780 | jermar | 228 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err) |
1090 | palkovsky | 229 | { |
230 | call->data.phone = phone; |
||
231 | atomic_inc(&phone->active_calls); |
||
1568 | palkovsky | 232 | IPC_SET_RETVAL(call->data, err); |
1090 | palkovsky | 233 | _ipc_answer_free_call(call); |
234 | } |
||
235 | |||
2471 | jermar | 236 | /** Unsafe unchecking version of ipc_call. |
237 | * |
||
238 | * @param phone Phone structure the call comes from. |
||
239 | * @param box Destination answerbox structure. |
||
2601 | jermar | 240 | * @param call Call structure with request. |
2471 | jermar | 241 | */ |
1086 | palkovsky | 242 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call) |
243 | { |
||
2471 | jermar | 244 | if (!(call->flags & IPC_CALL_FORWARDED)) { |
1088 | palkovsky | 245 | atomic_inc(&phone->active_calls); |
246 | call->data.phone = phone; |
||
247 | } |
||
1086 | palkovsky | 248 | |
249 | spinlock_lock(&box->lock); |
||
1573 | palkovsky | 250 | list_append(&call->link, &box->calls); |
1086 | palkovsky | 251 | spinlock_unlock(&box->lock); |
2310 | jermar | 252 | waitq_wakeup(&box->wq, WAKEUP_FIRST); |
1086 | palkovsky | 253 | } |
254 | |||
2471 | jermar | 255 | /** Send an asynchronous request using a phone to an answerbox. |
955 | palkovsky | 256 | * |
2471 | jermar | 257 | * @param phone Phone structure the call comes from and which is |
258 | * connected to the destination answerbox. |
||
259 | * @param call Call structure with request. |
||
260 | * |
||
261 | * @return Return 0 on success, ENOENT on error. |
||
955 | palkovsky | 262 | */ |
1088 | palkovsky | 263 | int ipc_call(phone_t *phone, call_t *call) |
955 | palkovsky | 264 | { |
1084 | palkovsky | 265 | answerbox_t *box; |
955 | palkovsky | 266 | |
3042 | svoboda | 267 | mutex_lock(&phone->lock); |
1568 | palkovsky | 268 | if (phone->state != IPC_PHONE_CONNECTED) { |
3042 | svoboda | 269 | mutex_unlock(&phone->lock); |
1088 | palkovsky | 270 | if (call->flags & IPC_CALL_FORWARDED) { |
271 | IPC_SET_RETVAL(call->data, EFORWARD); |
||
1090 | palkovsky | 272 | _ipc_answer_free_call(call); |
1568 | palkovsky | 273 | } else { |
274 | if (phone->state == IPC_PHONE_HUNGUP) |
||
1090 | palkovsky | 275 | ipc_backsend_err(phone, call, EHANGUP); |
1088 | palkovsky | 276 | else |
1090 | palkovsky | 277 | ipc_backsend_err(phone, call, ENOENT); |
1088 | palkovsky | 278 | } |
279 | return ENOENT; |
||
1084 | palkovsky | 280 | } |
1568 | palkovsky | 281 | box = phone->callee; |
1086 | palkovsky | 282 | _ipc_call(phone, box, call); |
283 | |||
3042 | svoboda | 284 | mutex_unlock(&phone->lock); |
1088 | palkovsky | 285 | return 0; |
1086 | palkovsky | 286 | } |
955 | palkovsky | 287 | |
2471 | jermar | 288 | /** Disconnect phone from answerbox. |
1086 | palkovsky | 289 | * |
2471 | jermar | 290 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done |
1568 | palkovsky | 291 | * lazily later. |
1086 | palkovsky | 292 | * |
2471 | jermar | 293 | * @param phone Phone structure to be hung up. |
1568 | palkovsky | 294 | * |
2471 | jermar | 295 | * @return Return 0 if the phone is disconnected. |
296 | * Return -1 if the phone was already disconnected. |
||
1086 | palkovsky | 297 | */ |
1591 | palkovsky | 298 | int ipc_phone_hangup(phone_t *phone) |
1086 | palkovsky | 299 | { |
300 | answerbox_t *box; |
||
301 | call_t *call; |
||
302 | |||
3042 | svoboda | 303 | mutex_lock(&phone->lock); |
2494 | jermar | 304 | if (phone->state == IPC_PHONE_FREE || |
305 | phone->state == IPC_PHONE_HUNGUP || |
||
2471 | jermar | 306 | phone->state == IPC_PHONE_CONNECTING) { |
3042 | svoboda | 307 | mutex_unlock(&phone->lock); |
1568 | palkovsky | 308 | return -1; |
309 | } |
||
1086 | palkovsky | 310 | box = phone->callee; |
1568 | palkovsky | 311 | if (phone->state != IPC_PHONE_SLAMMED) { |
312 | /* Remove myself from answerbox */ |
||
313 | spinlock_lock(&box->lock); |
||
1573 | palkovsky | 314 | list_remove(&phone->link); |
1568 | palkovsky | 315 | spinlock_unlock(&box->lock); |
316 | |||
317 | if (phone->state != IPC_PHONE_SLAMMED) { |
||
318 | call = ipc_call_alloc(0); |
||
319 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
||
320 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
||
321 | _ipc_call(phone, box, call); |
||
1088 | palkovsky | 322 | } |
1086 | palkovsky | 323 | } |
324 | |||
1568 | palkovsky | 325 | phone->state = IPC_PHONE_HUNGUP; |
3042 | svoboda | 326 | mutex_unlock(&phone->lock); |
1086 | palkovsky | 327 | |
328 | return 0; |
||
955 | palkovsky | 329 | } |
330 | |||
2471 | jermar | 331 | /** Forwards call from one answerbox to another one. |
1040 | palkovsky | 332 | * |
2471 | jermar | 333 | * @param call Call structure to be redirected. |
334 | * @param newphone Phone structure to target answerbox. |
||
335 | * @param oldbox Old answerbox structure. |
||
2622 | jermar | 336 | * @param mode Flags that specify mode of the forward operation. |
2471 | jermar | 337 | * |
338 | * @return Return 0 if forwarding succeeded or an error code if |
||
339 | * there was error. |
||
1088 | palkovsky | 340 | * |
2471 | jermar | 341 | * The return value serves only as an information for the forwarder, |
342 | * the original caller is notified automatically with EFORWARD. |
||
1040 | palkovsky | 343 | */ |
2622 | jermar | 344 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode) |
1040 | palkovsky | 345 | { |
346 | spinlock_lock(&oldbox->lock); |
||
1573 | palkovsky | 347 | list_remove(&call->link); |
1040 | palkovsky | 348 | spinlock_unlock(&oldbox->lock); |
349 | |||
2623 | jermar | 350 | if (mode & IPC_FF_ROUTE_FROM_ME) |
351 | call->data.phone = newphone; |
||
352 | |||
1088 | palkovsky | 353 | return ipc_call(newphone, call); |
1040 | palkovsky | 354 | } |
355 | |||
955 | palkovsky | 356 | |
2471 | jermar | 357 | /** Wait for a phone call. |
955 | palkovsky | 358 | * |
2471 | jermar | 359 | * @param box Answerbox expecting the call. |
360 | * @param usec Timeout in microseconds. See documentation for |
||
361 | * waitq_sleep_timeout() for decription of its special |
||
362 | * meaning. |
||
363 | * @param flags Select mode of sleep operation. See documentation for |
||
364 | * waitq_sleep_timeout() for description of its special |
||
365 | * meaning. |
||
366 | * @return Recived call structure or NULL. |
||
367 | * |
||
368 | * To distinguish between a call and an answer, have a look at call->flags. |
||
955 | palkovsky | 369 | */ |
2471 | jermar | 370 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags) |
955 | palkovsky | 371 | { |
372 | call_t *request; |
||
1258 | palkovsky | 373 | ipl_t ipl; |
1364 | jermar | 374 | int rc; |
955 | palkovsky | 375 | |
1364 | jermar | 376 | restart: |
1502 | jermar | 377 | rc = waitq_sleep_timeout(&box->wq, usec, flags); |
1364 | jermar | 378 | if (SYNCH_FAILED(rc)) |
379 | return NULL; |
||
1086 | palkovsky | 380 | |
1040 | palkovsky | 381 | spinlock_lock(&box->lock); |
1258 | palkovsky | 382 | if (!list_empty(&box->irq_notifs)) { |
383 | ipl = interrupts_disable(); |
||
384 | spinlock_lock(&box->irq_lock); |
||
385 | |||
1573 | palkovsky | 386 | request = list_get_instance(box->irq_notifs.next, call_t, link); |
387 | list_remove(&request->link); |
||
1258 | palkovsky | 388 | |
389 | spinlock_unlock(&box->irq_lock); |
||
390 | interrupts_restore(ipl); |
||
391 | } else if (!list_empty(&box->answers)) { |
||
1086 | palkovsky | 392 | /* Handle asynchronous answers */ |
1573 | palkovsky | 393 | request = list_get_instance(box->answers.next, call_t, link); |
394 | list_remove(&request->link); |
||
1086 | palkovsky | 395 | atomic_dec(&request->data.phone->active_calls); |
396 | } else if (!list_empty(&box->calls)) { |
||
397 | /* Handle requests */ |
||
1573 | palkovsky | 398 | request = list_get_instance(box->calls.next, call_t, link); |
399 | list_remove(&request->link); |
||
1086 | palkovsky | 400 | /* Append request to dispatch queue */ |
1573 | palkovsky | 401 | list_append(&request->link, &box->dispatched_calls); |
1086 | palkovsky | 402 | } else { |
1595 | palkovsky | 403 | /* This can happen regularly after ipc_cleanup */ |
1086 | palkovsky | 404 | spinlock_unlock(&box->lock); |
405 | goto restart; |
||
955 | palkovsky | 406 | } |
1040 | palkovsky | 407 | spinlock_unlock(&box->lock); |
955 | palkovsky | 408 | return request; |
409 | } |
||
410 | |||
2471 | jermar | 411 | /** Answer all calls from list with EHANGUP answer. |
412 | * |
||
413 | * @param lst Head of the list to be cleaned up. |
||
414 | */ |
||
1141 | palkovsky | 415 | static void ipc_cleanup_call_list(link_t *lst) |
416 | { |
||
417 | call_t *call; |
||
418 | |||
419 | while (!list_empty(lst)) { |
||
1573 | palkovsky | 420 | call = list_get_instance(lst->next, call_t, link); |
2662 | jermar | 421 | if (call->buffer) |
422 | free(call->buffer); |
||
1573 | palkovsky | 423 | list_remove(&call->link); |
1141 | palkovsky | 424 | |
425 | IPC_SET_RETVAL(call->data, EHANGUP); |
||
426 | _ipc_answer_free_call(call); |
||
427 | } |
||
428 | } |
||
429 | |||
2902 | svoboda | 430 | /** Disconnects all phones connected to an answerbox. |
1072 | palkovsky | 431 | * |
2902 | svoboda | 432 | * @param box Answerbox to disconnect phones from. |
433 | * @param notify_box If true, the answerbox will get a hangup message for |
||
434 | * each disconnected phone. |
||
1072 | palkovsky | 435 | */ |
2902 | svoboda | 436 | static void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box) |
1072 | palkovsky | 437 | { |
1141 | palkovsky | 438 | phone_t *phone; |
2183 | jermar | 439 | DEADLOCK_PROBE_INIT(p_phonelck); |
2902 | svoboda | 440 | ipl_t ipl; |
441 | call_t *call; |
||
1582 | palkovsky | 442 | |
2902 | svoboda | 443 | call = ipc_call_alloc(0); |
1088 | palkovsky | 444 | |
1141 | palkovsky | 445 | /* Disconnect all phones connected to our answerbox */ |
446 | restart_phones: |
||
2902 | svoboda | 447 | ipl = interrupts_disable(); |
448 | spinlock_lock(&box->lock); |
||
449 | while (!list_empty(&box->connected_phones)) { |
||
450 | phone = list_get_instance(box->connected_phones.next, |
||
2183 | jermar | 451 | phone_t, link); |
3042 | svoboda | 452 | if (SYNCH_FAILED(mutex_trylock(&phone->lock))) { |
2902 | svoboda | 453 | spinlock_unlock(&box->lock); |
454 | interrupts_restore(ipl); |
||
2183 | jermar | 455 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD); |
1141 | palkovsky | 456 | goto restart_phones; |
457 | } |
||
458 | |||
459 | /* Disconnect phone */ |
||
1568 | palkovsky | 460 | ASSERT(phone->state == IPC_PHONE_CONNECTED); |
2902 | svoboda | 461 | |
462 | list_remove(&phone->link); |
||
1568 | palkovsky | 463 | phone->state = IPC_PHONE_SLAMMED; |
1088 | palkovsky | 464 | |
2902 | svoboda | 465 | if (notify_box) { |
3042 | svoboda | 466 | mutex_unlock(&phone->lock); |
2902 | svoboda | 467 | spinlock_unlock(&box->lock); |
468 | interrupts_restore(ipl); |
||
469 | |||
470 | /* |
||
471 | * Send one message to the answerbox for each |
||
472 | * phone. Used to make sure the kbox thread |
||
473 | * wakes up after the last phone has been |
||
474 | * disconnected. |
||
475 | */ |
||
476 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
||
477 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
||
478 | _ipc_call(phone, box, call); |
||
479 | |||
480 | /* Allocate another call in advance */ |
||
481 | call = ipc_call_alloc(0); |
||
482 | |||
483 | /* Must start again */ |
||
484 | goto restart_phones; |
||
485 | } |
||
486 | |||
3042 | svoboda | 487 | mutex_unlock(&phone->lock); |
1141 | palkovsky | 488 | } |
489 | |||
2902 | svoboda | 490 | spinlock_unlock(&box->lock); |
491 | interrupts_restore(ipl); |
||
492 | |||
493 | /* Free unused call */ |
||
494 | if (call) ipc_call_free(call); |
||
495 | } |
||
496 | |||
497 | static void ipc_kbox_cleanup() |
||
498 | { |
||
499 | bool have_kb_thread; |
||
500 | |||
501 | /* Only hold kb_cleanup_lock while setting kb_finished - this is enough */ |
||
3034 | svoboda | 502 | mutex_lock(&TASK->kb_cleanup_lock); |
2902 | svoboda | 503 | TASK->kb_finished = true; |
3034 | svoboda | 504 | mutex_unlock(&TASK->kb_cleanup_lock); |
2902 | svoboda | 505 | |
506 | have_kb_thread = (TASK->kb_thread != NULL); |
||
507 | |||
508 | /* From now on nobody will try to connect phones or attach kbox threads */ |
||
509 | |||
510 | /* |
||
511 | * Disconnect all phones connected to our kbox. Passing true for |
||
512 | * notify_box causes a HANGUP message to be inserted for each |
||
513 | * disconnected phone. This ensures the kbox thread is going to |
||
514 | * wake up and terminate. |
||
515 | */ |
||
516 | ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread); |
||
517 | |||
518 | if (have_kb_thread) { |
||
3035 | svoboda | 519 | klog_printf("join kb_thread.."); |
520 | thread_join(TASK->kb_thread); |
||
3037 | svoboda | 521 | thread_detach(TASK->kb_thread); |
3035 | svoboda | 522 | klog_printf("join done"); |
523 | TASK->kb_thread = NULL; |
||
2902 | svoboda | 524 | } |
525 | |||
1088 | palkovsky | 526 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
2902 | svoboda | 527 | spinlock_lock(&TASK->kernel_box.lock); |
528 | ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls); |
||
529 | ipc_cleanup_call_list(&TASK->kernel_box.calls); |
||
530 | spinlock_unlock(&TASK->kernel_box.lock); |
||
531 | } |
||
532 | |||
533 | |||
534 | /** Cleans up all IPC communication of the current task. |
||
535 | * |
||
536 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you |
||
537 | * have to change it as well if you want to cleanup other tasks than TASK. |
||
538 | */ |
||
539 | void ipc_cleanup(void) |
||
540 | { |
||
541 | int i; |
||
542 | call_t *call; |
||
543 | |||
544 | /* Disconnect all our phones ('ipc_phone_hangup') */ |
||
545 | for (i = 0; i < IPC_MAX_PHONES; i++) |
||
546 | ipc_phone_hangup(&TASK->phones[i]); |
||
547 | |||
548 | /* Disconnect all connected irqs */ |
||
549 | ipc_irq_cleanup(&TASK->answerbox); |
||
550 | |||
551 | /* Disconnect all phones connected to our regular answerbox */ |
||
552 | ipc_answerbox_slam_phones(&TASK->answerbox, false); |
||
553 | |||
554 | /* Clean up kbox thread and communications */ |
||
555 | ipc_kbox_cleanup(); |
||
556 | |||
557 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
||
558 | spinlock_lock(&TASK->answerbox.lock); |
||
1582 | palkovsky | 559 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls); |
560 | ipc_cleanup_call_list(&TASK->answerbox.calls); |
||
561 | spinlock_unlock(&TASK->answerbox.lock); |
||
1072 | palkovsky | 562 | |
1088 | palkovsky | 563 | /* Wait for all async answers to arrive */ |
1568 | palkovsky | 564 | while (1) { |
565 | /* Go through all phones, until all are FREE... */ |
||
566 | /* Locking not needed, no one else should modify |
||
567 | * it, when we are in cleanup */ |
||
2471 | jermar | 568 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
569 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP && |
||
1582 | palkovsky | 570 | atomic_get(&TASK->phones[i].active_calls) == 0) |
571 | TASK->phones[i].state = IPC_PHONE_FREE; |
||
572 | |||
1591 | palkovsky | 573 | /* Just for sure, we might have had some |
574 | * IPC_PHONE_CONNECTING phones */ |
||
1582 | palkovsky | 575 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED) |
1591 | palkovsky | 576 | ipc_phone_hangup(&TASK->phones[i]); |
577 | /* If the hangup succeeded, it has sent a HANGUP |
||
578 | * message, the IPC is now in HUNGUP state, we |
||
579 | * wait for the reply to come */ |
||
1582 | palkovsky | 580 | |
581 | if (TASK->phones[i].state != IPC_PHONE_FREE) |
||
1568 | palkovsky | 582 | break; |
583 | } |
||
584 | /* Voila, got into cleanup */ |
||
585 | if (i == IPC_MAX_PHONES) |
||
586 | break; |
||
587 | |||
2471 | jermar | 588 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT, |
589 | SYNCH_FLAGS_NONE); |
||
590 | ASSERT((call->flags & IPC_CALL_ANSWERED) || |
||
591 | (call->flags & IPC_CALL_NOTIF)); |
||
592 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
||
1141 | palkovsky | 593 | |
1582 | palkovsky | 594 | atomic_dec(&TASK->active_calls); |
1141 | palkovsky | 595 | ipc_call_free(call); |
596 | } |
||
1072 | palkovsky | 597 | } |
1258 | palkovsky | 598 | |
599 | |||
1923 | jermar | 600 | /** Initilize IPC subsystem */ |
1258 | palkovsky | 601 | void ipc_init(void) |
602 | { |
||
2471 | jermar | 603 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL, |
604 | NULL, 0); |
||
1258 | palkovsky | 605 | } |
606 | |||
1573 | palkovsky | 607 | |
2471 | jermar | 608 | /** List answerbox contents. |
609 | * |
||
610 | * @param taskid Task ID. |
||
611 | */ |
||
1573 | palkovsky | 612 | void ipc_print_task(task_id_t taskid) |
613 | { |
||
614 | task_t *task; |
||
615 | int i; |
||
616 | call_t *call; |
||
617 | link_t *tmp; |
||
618 | |||
619 | spinlock_lock(&tasks_lock); |
||
620 | task = task_find_by_id(taskid); |
||
621 | if (task) |
||
622 | spinlock_lock(&task->lock); |
||
623 | spinlock_unlock(&tasks_lock); |
||
624 | if (!task) |
||
625 | return; |
||
626 | |||
627 | /* Print opened phones & details */ |
||
628 | printf("PHONE:\n"); |
||
2471 | jermar | 629 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
3042 | svoboda | 630 | if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) { |
631 | printf("%d: mutex busy\n", i); |
||
632 | continue; |
||
633 | } |
||
1573 | palkovsky | 634 | if (task->phones[i].state != IPC_PHONE_FREE) { |
2471 | jermar | 635 | printf("%d: ", i); |
1573 | palkovsky | 636 | switch (task->phones[i].state) { |
637 | case IPC_PHONE_CONNECTING: |
||
638 | printf("connecting "); |
||
639 | break; |
||
640 | case IPC_PHONE_CONNECTED: |
||
1735 | decky | 641 | printf("connected to: %p ", |
1573 | palkovsky | 642 | task->phones[i].callee); |
643 | break; |
||
644 | case IPC_PHONE_SLAMMED: |
||
1735 | decky | 645 | printf("slammed by: %p ", |
1573 | palkovsky | 646 | task->phones[i].callee); |
647 | break; |
||
648 | case IPC_PHONE_HUNGUP: |
||
1735 | decky | 649 | printf("hung up - was: %p ", |
1573 | palkovsky | 650 | task->phones[i].callee); |
651 | break; |
||
652 | default: |
||
653 | break; |
||
654 | } |
||
2472 | jermar | 655 | printf("active: %d\n", |
656 | atomic_get(&task->phones[i].active_calls)); |
||
1573 | palkovsky | 657 | } |
3042 | svoboda | 658 | mutex_unlock(&task->phones[i].lock); |
1573 | palkovsky | 659 | } |
660 | |||
661 | |||
662 | /* Print answerbox - calls */ |
||
663 | spinlock_lock(&task->answerbox.lock); |
||
664 | printf("ABOX - CALLS:\n"); |
||
2472 | jermar | 665 | for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls; |
666 | tmp = tmp->next) { |
||
1573 | palkovsky | 667 | call = list_get_instance(tmp, call_t, link); |
2472 | jermar | 668 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d " |
2637 | cejka | 669 | "A4:%d A5:%d Flags:%x\n", call, call->sender->taskid, |
2472 | jermar | 670 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
671 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
||
2637 | cejka | 672 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
2472 | jermar | 673 | call->flags); |
1573 | palkovsky | 674 | } |
675 | /* Print answerbox - calls */ |
||
676 | printf("ABOX - DISPATCHED CALLS:\n"); |
||
3042 | svoboda | 677 | for (tmp = task->answerbox.dispatched_calls.next; |
1573 | palkovsky | 678 | tmp != &task->answerbox.dispatched_calls; |
679 | tmp = tmp->next) { |
||
680 | call = list_get_instance(tmp, call_t, link); |
||
2472 | jermar | 681 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d " |
2637 | cejka | 682 | "A4:%d A5:%d Flags:%x\n", call, call->sender->taskid, |
2472 | jermar | 683 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
684 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
||
2637 | cejka | 685 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
2472 | jermar | 686 | call->flags); |
1573 | palkovsky | 687 | } |
688 | /* Print answerbox - calls */ |
||
689 | printf("ABOX - ANSWERS:\n"); |
||
2472 | jermar | 690 | for (tmp = task->answerbox.answers.next; tmp != &task->answerbox.answers; |
691 | tmp = tmp->next) { |
||
1573 | palkovsky | 692 | call = list_get_instance(tmp, call_t, link); |
2637 | cejka | 693 | printf("Callid:%p M:%d A1:%d A2:%d A3:%d A4:%d A5:%d Flags:%x\n", |
694 | call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
||
2472 | jermar | 695 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
2637 | cejka | 696 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
2472 | jermar | 697 | call->flags); |
1573 | palkovsky | 698 | } |
699 | |||
700 | spinlock_unlock(&task->answerbox.lock); |
||
701 | spinlock_unlock(&task->lock); |
||
702 | } |
||
1702 | cejka | 703 | |
2801 | svoboda | 704 | #include <ipc/ipcrsc.h> |
705 | #include <console/klog.h> |
||
2815 | svoboda | 706 | #include <udebug/udebug_ipc.h> |
2801 | svoboda | 707 | |
2808 | svoboda | 708 | static void kbox_thread_proc(void *arg) |
709 | { |
||
2809 | svoboda | 710 | call_t *call; |
711 | int method; |
||
2837 | svoboda | 712 | bool done; |
713 | ipl_t ipl; |
||
2809 | svoboda | 714 | |
2808 | svoboda | 715 | (void)arg; |
716 | klog_printf("kbox_thread_proc()"); |
||
2837 | svoboda | 717 | done = false; |
718 | |||
719 | while (!done) { |
||
2913 | svoboda | 720 | //klog_printf("kbox: wait for call"); |
2809 | svoboda | 721 | call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT, |
2808 | svoboda | 722 | SYNCH_FLAGS_NONE); |
2837 | svoboda | 723 | |
2809 | svoboda | 724 | if (call != NULL) { |
725 | method = IPC_GET_METHOD(call->data); |
||
2812 | svoboda | 726 | |
2815 | svoboda | 727 | if (method == IPC_M_DEBUG_ALL) { |
728 | udebug_call_receive(call); |
||
2809 | svoboda | 729 | } |
2837 | svoboda | 730 | |
731 | if (method == IPC_M_PHONE_HUNGUP) { |
||
2902 | svoboda | 732 | klog_printf("kbox: handle hangup message"); |
2870 | svoboda | 733 | |
734 | /* Was it our debugger, who hung up? */ |
||
3014 | svoboda | 735 | if (call->sender == TASK->udebug.debugger) { |
2870 | svoboda | 736 | /* Terminate debugging session (if any) */ |
2902 | svoboda | 737 | klog_printf("kbox: terminate debug session"); |
2870 | svoboda | 738 | ipl = interrupts_disable(); |
739 | spinlock_lock(&TASK->lock); |
||
740 | udebug_task_cleanup(TASK); |
||
741 | spinlock_unlock(&TASK->lock); |
||
742 | interrupts_restore(ipl); |
||
743 | } else { |
||
2902 | svoboda | 744 | klog_printf("kbox: was not debugger"); |
2870 | svoboda | 745 | } |
746 | |||
2902 | svoboda | 747 | klog_printf("kbox: continue with hangup message"); |
2837 | svoboda | 748 | IPC_SET_RETVAL(call->data, 0); |
749 | ipc_answer(&TASK->kernel_box, call); |
||
750 | |||
751 | ipl = interrupts_disable(); |
||
752 | spinlock_lock(&TASK->lock); |
||
753 | spinlock_lock(&TASK->answerbox.lock); |
||
754 | if (list_empty(&TASK->answerbox.connected_phones)) { |
||
755 | /* Last phone has been disconnected */ |
||
756 | TASK->kb_thread = NULL; |
||
757 | done = true; |
||
2902 | svoboda | 758 | klog_printf("phone list is empty"); |
2837 | svoboda | 759 | } |
760 | spinlock_unlock(&TASK->answerbox.lock); |
||
761 | spinlock_unlock(&TASK->lock); |
||
762 | interrupts_restore(ipl); |
||
763 | } |
||
2809 | svoboda | 764 | } |
2808 | svoboda | 765 | } |
2837 | svoboda | 766 | |
767 | klog_printf("kbox: finished"); |
||
2808 | svoboda | 768 | } |
769 | |||
2839 | svoboda | 770 | |
2801 | svoboda | 771 | /** |
2808 | svoboda | 772 | * Connect phone to a task kernel-box specified by id. |
2801 | svoboda | 773 | * |
2902 | svoboda | 774 | * Note that this is not completely atomic. For optimisation reasons, |
775 | * The task might start cleaning up kbox after the phone has been connected |
||
776 | * and before a kbox thread has been created. This must be taken into account |
||
777 | * in the cleanup code. |
||
778 | * |
||
2801 | svoboda | 779 | * @return Phone id on success, or negative error code. |
780 | */ |
||
2808 | svoboda | 781 | int ipc_connect_kbox(task_id_t taskid) |
2801 | svoboda | 782 | { |
783 | int newphid; |
||
784 | task_t *ta; |
||
2839 | svoboda | 785 | thread_t *kb_thread; |
786 | ipl_t ipl; |
||
2801 | svoboda | 787 | |
2839 | svoboda | 788 | ipl = interrupts_disable(); |
2801 | svoboda | 789 | spinlock_lock(&tasks_lock); |
2839 | svoboda | 790 | |
2801 | svoboda | 791 | ta = task_find_by_id(taskid); |
792 | if (ta == NULL) { |
||
793 | spinlock_unlock(&tasks_lock); |
||
2902 | svoboda | 794 | interrupts_restore(ipl); |
2801 | svoboda | 795 | return ENOENT; |
796 | } |
||
2902 | svoboda | 797 | |
3034 | svoboda | 798 | atomic_inc(&ta->refcount); |
3033 | svoboda | 799 | |
3034 | svoboda | 800 | spinlock_unlock(&tasks_lock); |
801 | interrupts_restore(ipl); |
||
802 | |||
803 | mutex_lock(&ta->kb_cleanup_lock); |
||
804 | |||
805 | if (atomic_predec(&ta->refcount) == 0) { |
||
806 | mutex_unlock(&ta->kb_cleanup_lock); |
||
807 | task_destroy(ta); |
||
808 | return ENOENT; |
||
809 | } |
||
810 | |||
3033 | svoboda | 811 | if (ta->kb_finished != false) { |
3034 | svoboda | 812 | mutex_unlock(&ta->kb_cleanup_lock); |
3033 | svoboda | 813 | return EINVAL; |
814 | } |
||
815 | |||
3034 | svoboda | 816 | newphid = phone_alloc(); |
817 | if (newphid < 0) { |
||
818 | mutex_unlock(&ta->kb_cleanup_lock); |
||
819 | return ELIMIT; |
||
820 | } |
||
2801 | svoboda | 821 | |
2902 | svoboda | 822 | /* Connect the newly allocated phone to the kbox */ |
823 | ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box); |
||
2808 | svoboda | 824 | |
3034 | svoboda | 825 | if (ta->kb_thread != NULL) { |
826 | mutex_unlock(&ta->kb_cleanup_lock); |
||
827 | return newphid; |
||
828 | } |
||
2839 | svoboda | 829 | |
2902 | svoboda | 830 | /* Create a kbox thread */ |
3034 | svoboda | 831 | kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false); |
832 | if (!kb_thread) { |
||
833 | mutex_unlock(&ta->kb_cleanup_lock); |
||
2902 | svoboda | 834 | return ENOMEM; |
835 | } |
||
836 | |||
837 | ta->kb_thread = kb_thread; |
||
838 | thread_ready(kb_thread); |
||
839 | |||
3034 | svoboda | 840 | mutex_unlock(&ta->kb_cleanup_lock); |
2902 | svoboda | 841 | |
2801 | svoboda | 842 | return newphid; |
843 | } |
||
844 | |||
1757 | jermar | 845 | /** @} |
1702 | cejka | 846 | */ |