source: mainline/kernel/generic/src/ipc/kbox.c@ 5d3ed34

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5d3ed34 was 5d3ed34, checked in by Jakub Jermar <jakub@…>, 13 years ago

Make sure that both dispatched and non-dispatched calls are properly
answered in ipc_cleanup_call_list().

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