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