Rev 3665 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 3438 | svoboda | 1 | /* |
| 2 | * Copyright (c) 2008 Jiri Svoboda |
||
| 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 | |||
| 29 | /** @addtogroup genericipc |
||
| 30 | * @{ |
||
| 31 | */ |
||
| 32 | /** @file |
||
| 33 | */ |
||
| 34 | |||
| 35 | #include <synch/synch.h> |
||
| 36 | #include <synch/spinlock.h> |
||
| 37 | #include <synch/mutex.h> |
||
| 38 | #include <ipc/ipc.h> |
||
| 39 | #include <ipc/ipcrsc.h> |
||
| 40 | #include <arch.h> |
||
| 4073 | rimsky | 41 | #include <arch/asm.h> |
| 3438 | svoboda | 42 | #include <errno.h> |
| 3441 | svoboda | 43 | #include <debug.h> |
| 4073 | rimsky | 44 | #include <print.h> |
| 3438 | svoboda | 45 | #include <udebug/udebug_ipc.h> |
| 3445 | jermar | 46 | #include <ipc/kbox.h> |
| 3438 | svoboda | 47 | |
| 48 | void ipc_kbox_cleanup(void) |
||
| 49 | { |
||
| 3593 | rimsky | 50 | ipl_t ipl; |
| 3438 | svoboda | 51 | bool have_kb_thread; |
| 52 | |||
| 3665 | rimsky | 53 | /* |
| 54 | * Only hold kb.cleanup_lock while setting kb.finished - |
||
| 55 | * this is enough. |
||
| 56 | */ |
||
| 57 | mutex_lock(&TASK->kb.cleanup_lock); |
||
| 58 | TASK->kb.finished = true; |
||
| 59 | mutex_unlock(&TASK->kb.cleanup_lock); |
||
| 3438 | svoboda | 60 | |
| 3665 | rimsky | 61 | have_kb_thread = (TASK->kb.thread != NULL); |
| 3438 | svoboda | 62 | |
| 3665 | rimsky | 63 | /* |
| 64 | * From now on nobody will try to connect phones or attach |
||
| 65 | * kbox threads |
||
| 66 | */ |
||
| 3438 | svoboda | 67 | |
| 68 | /* |
||
| 69 | * Disconnect all phones connected to our kbox. Passing true for |
||
| 70 | * notify_box causes a HANGUP message to be inserted for each |
||
| 71 | * disconnected phone. This ensures the kbox thread is going to |
||
| 72 | * wake up and terminate. |
||
| 73 | */ |
||
| 3665 | rimsky | 74 | ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread); |
| 3593 | rimsky | 75 | |
| 76 | /* |
||
| 77 | * If the task was being debugged, clean up debugging session. |
||
| 78 | * This is necessarry as slamming the phones won't force |
||
| 79 | * kbox thread to clean it up since sender != debugger. |
||
| 80 | */ |
||
| 81 | ipl = interrupts_disable(); |
||
| 82 | spinlock_lock(&TASK->lock); |
||
| 83 | udebug_task_cleanup(TASK); |
||
| 84 | spinlock_unlock(&TASK->lock); |
||
| 85 | interrupts_restore(ipl); |
||
| 3438 | svoboda | 86 | |
| 87 | if (have_kb_thread) { |
||
| 3665 | rimsky | 88 | LOG("join kb.thread..\n"); |
| 89 | thread_join(TASK->kb.thread); |
||
| 90 | thread_detach(TASK->kb.thread); |
||
| 3441 | svoboda | 91 | LOG("join done\n"); |
| 3665 | rimsky | 92 | TASK->kb.thread = NULL; |
| 3438 | svoboda | 93 | } |
| 94 | |||
| 3665 | rimsky | 95 | /* Answer all messages in 'calls' and 'dispatched_calls' queues. */ |
| 96 | spinlock_lock(&TASK->kb.box.lock); |
||
| 97 | ipc_cleanup_call_list(&TASK->kb.box.dispatched_calls); |
||
| 98 | ipc_cleanup_call_list(&TASK->kb.box.calls); |
||
| 99 | spinlock_unlock(&TASK->kb.box.lock); |
||
| 3438 | svoboda | 100 | } |
| 101 | |||
| 3593 | rimsky | 102 | /** Handle hangup message in kbox. |
| 103 | * |
||
| 104 | * @param call The IPC_M_PHONE_HUNGUP call structure. |
||
| 105 | * @param last Output, the function stores @c true here if |
||
| 106 | * this was the last phone, @c false otherwise. |
||
| 107 | **/ |
||
| 108 | static void kbox_proc_phone_hungup(call_t *call, bool *last) |
||
| 109 | { |
||
| 110 | ipl_t ipl; |
||
| 3438 | svoboda | 111 | |
| 3593 | rimsky | 112 | LOG("kbox_proc_phone_hungup()\n"); |
| 113 | |||
| 114 | /* Was it our debugger, who hung up? */ |
||
| 115 | if (call->sender == TASK->udebug.debugger) { |
||
| 3665 | rimsky | 116 | /* Terminate debugging session (if any). */ |
| 3593 | rimsky | 117 | LOG("kbox: terminate debug session\n"); |
| 118 | ipl = interrupts_disable(); |
||
| 119 | spinlock_lock(&TASK->lock); |
||
| 120 | udebug_task_cleanup(TASK); |
||
| 121 | spinlock_unlock(&TASK->lock); |
||
| 122 | interrupts_restore(ipl); |
||
| 123 | } else { |
||
| 124 | LOG("kbox: was not debugger\n"); |
||
| 125 | } |
||
| 126 | |||
| 127 | LOG("kbox: continue with hangup message\n"); |
||
| 128 | IPC_SET_RETVAL(call->data, 0); |
||
| 3665 | rimsky | 129 | ipc_answer(&TASK->kb.box, call); |
| 3593 | rimsky | 130 | |
| 131 | ipl = interrupts_disable(); |
||
| 132 | spinlock_lock(&TASK->lock); |
||
| 133 | spinlock_lock(&TASK->answerbox.lock); |
||
| 134 | if (list_empty(&TASK->answerbox.connected_phones)) { |
||
| 135 | /* |
||
| 136 | * Last phone has been disconnected. Detach this thread so it |
||
| 137 | * gets freed and signal to the caller. |
||
| 138 | */ |
||
| 139 | |||
| 140 | /* Only detach kbox thread unless already terminating. */ |
||
| 3665 | rimsky | 141 | mutex_lock(&TASK->kb.cleanup_lock); |
| 142 | if (&TASK->kb.finished == false) { |
||
| 3593 | rimsky | 143 | /* Detach kbox thread so it gets freed from memory. */ |
| 3665 | rimsky | 144 | thread_detach(TASK->kb.thread); |
| 145 | TASK->kb.thread = NULL; |
||
| 3593 | rimsky | 146 | } |
| 3665 | rimsky | 147 | mutex_unlock(&TASK->kb.cleanup_lock); |
| 3593 | rimsky | 148 | |
| 149 | LOG("phone list is empty\n"); |
||
| 150 | *last = true; |
||
| 151 | } else { |
||
| 152 | *last = false; |
||
| 153 | } |
||
| 154 | |||
| 155 | spinlock_unlock(&TASK->answerbox.lock); |
||
| 156 | spinlock_unlock(&TASK->lock); |
||
| 157 | interrupts_restore(ipl); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** Implementing function for the kbox thread. |
||
| 161 | * |
||
| 162 | * This function listens for debug requests. It terminates |
||
| 163 | * when all phones are disconnected from the kbox. |
||
| 164 | * |
||
| 165 | * @param arg Ignored. |
||
| 166 | */ |
||
| 3438 | svoboda | 167 | static void kbox_thread_proc(void *arg) |
| 168 | { |
||
| 169 | call_t *call; |
||
| 170 | bool done; |
||
| 171 | |||
| 172 | (void)arg; |
||
| 3441 | svoboda | 173 | LOG("kbox_thread_proc()\n"); |
| 3438 | svoboda | 174 | done = false; |
| 175 | |||
| 176 | while (!done) { |
||
| 3665 | rimsky | 177 | call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT, |
| 3438 | svoboda | 178 | SYNCH_FLAGS_NONE); |
| 179 | |||
| 3593 | rimsky | 180 | if (call == NULL) |
| 181 | continue; /* Try again. */ |
||
| 3438 | svoboda | 182 | |
| 3593 | rimsky | 183 | switch (IPC_GET_METHOD(call->data)) { |
| 3438 | svoboda | 184 | |
| 3593 | rimsky | 185 | case IPC_M_DEBUG_ALL: |
| 186 | /* Handle debug call. */ |
||
| 187 | udebug_call_receive(call); |
||
| 188 | break; |
||
| 3438 | svoboda | 189 | |
| 3593 | rimsky | 190 | case IPC_M_PHONE_HUNGUP: |
| 191 | /* |
||
| 192 | * Process the hangup call. If this was the last |
||
| 193 | * phone, done will be set to true and the |
||
| 194 | * while loop will terminate. |
||
| 195 | */ |
||
| 196 | kbox_proc_phone_hungup(call, &done); |
||
| 197 | break; |
||
| 3438 | svoboda | 198 | |
| 3593 | rimsky | 199 | default: |
| 200 | /* Ignore */ |
||
| 201 | break; |
||
| 3438 | svoboda | 202 | } |
| 203 | } |
||
| 204 | |||
| 3441 | svoboda | 205 | LOG("kbox: finished\n"); |
| 3438 | svoboda | 206 | } |
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Connect phone to a task kernel-box specified by id. |
||
| 211 | * |
||
| 3665 | rimsky | 212 | * Note that this is not completely atomic. For optimisation reasons, the task |
| 213 | * might start cleaning up kbox after the phone has been connected and before |
||
| 214 | * a kbox thread has been created. This must be taken into account in the |
||
| 215 | * cleanup code. |
||
| 3438 | svoboda | 216 | * |
| 217 | * @return Phone id on success, or negative error code. |
||
| 218 | */ |
||
| 219 | int ipc_connect_kbox(task_id_t taskid) |
||
| 220 | { |
||
| 221 | int newphid; |
||
| 222 | task_t *ta; |
||
| 223 | thread_t *kb_thread; |
||
| 224 | ipl_t ipl; |
||
| 225 | |||
| 226 | ipl = interrupts_disable(); |
||
| 227 | spinlock_lock(&tasks_lock); |
||
| 228 | |||
| 229 | ta = task_find_by_id(taskid); |
||
| 230 | if (ta == NULL) { |
||
| 231 | spinlock_unlock(&tasks_lock); |
||
| 232 | interrupts_restore(ipl); |
||
| 233 | return ENOENT; |
||
| 234 | } |
||
| 235 | |||
| 236 | atomic_inc(&ta->refcount); |
||
| 237 | |||
| 238 | spinlock_unlock(&tasks_lock); |
||
| 239 | interrupts_restore(ipl); |
||
| 240 | |||
| 3665 | rimsky | 241 | mutex_lock(&ta->kb.cleanup_lock); |
| 3438 | svoboda | 242 | |
| 243 | if (atomic_predec(&ta->refcount) == 0) { |
||
| 3665 | rimsky | 244 | mutex_unlock(&ta->kb.cleanup_lock); |
| 3438 | svoboda | 245 | task_destroy(ta); |
| 246 | return ENOENT; |
||
| 247 | } |
||
| 248 | |||
| 3665 | rimsky | 249 | if (ta->kb.finished != false) { |
| 250 | mutex_unlock(&ta->kb.cleanup_lock); |
||
| 3438 | svoboda | 251 | return EINVAL; |
| 252 | } |
||
| 253 | |||
| 254 | newphid = phone_alloc(); |
||
| 255 | if (newphid < 0) { |
||
| 3665 | rimsky | 256 | mutex_unlock(&ta->kb.cleanup_lock); |
| 3438 | svoboda | 257 | return ELIMIT; |
| 258 | } |
||
| 259 | |||
| 260 | /* Connect the newly allocated phone to the kbox */ |
||
| 3665 | rimsky | 261 | ipc_phone_connect(&TASK->phones[newphid], &ta->kb.box); |
| 3438 | svoboda | 262 | |
| 3665 | rimsky | 263 | if (ta->kb.thread != NULL) { |
| 264 | mutex_unlock(&ta->kb.cleanup_lock); |
||
| 3438 | svoboda | 265 | return newphid; |
| 266 | } |
||
| 267 | |||
| 268 | /* Create a kbox thread */ |
||
| 3665 | rimsky | 269 | kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, |
| 270 | "kbox", false); |
||
| 3438 | svoboda | 271 | if (!kb_thread) { |
| 3665 | rimsky | 272 | mutex_unlock(&ta->kb.cleanup_lock); |
| 3438 | svoboda | 273 | return ENOMEM; |
| 274 | } |
||
| 275 | |||
| 3665 | rimsky | 276 | ta->kb.thread = kb_thread; |
| 3438 | svoboda | 277 | thread_ready(kb_thread); |
| 278 | |||
| 3665 | rimsky | 279 | mutex_unlock(&ta->kb.cleanup_lock); |
| 3438 | svoboda | 280 | |
| 281 | return newphid; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** @} |
||
| 285 | */ |