source: mainline/kernel/generic/src/ipc/kbox.c@ 86a3f89b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86a3f89b was f173404, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Fix locking in ipc_kbox_cleanup(). This was a leftover from the time when there was not udebug.lock mutex.

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