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