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
RevLine 
[9a1b20c]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>
[c0699467]38#include <abi/ipc/methods.h>
[9a1b20c]39#include <ipc/ipcrsc.h>
40#include <arch.h>
41#include <errno.h>
[0108984a]42#include <debug.h>
[9a1b20c]43#include <udebug/udebug_ipc.h>
[e028660]44#include <ipc/kbox.h>
[1902113]45#include <print.h>
[9a1b20c]46
47void ipc_kbox_cleanup(void)
48{
[da1bafb]49 /*
[31696b4f]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);
[da1bafb]56
57 bool have_kb_thread = (TASK->kb.thread != NULL);
58
[31696b4f]59 /*
60 * From now on nobody will try to connect phones or attach
61 * kbox threads
62 */
[da1bafb]63
[9a1b20c]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 */
[31696b4f]70 ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
[da1bafb]71
72 /*
[94f8e3c1]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 */
[f173404]77 mutex_lock(&TASK->udebug.lock);
[94f8e3c1]78 udebug_task_cleanup(TASK);
[f173404]79 mutex_unlock(&TASK->udebug.lock);
[da1bafb]80
[9a1b20c]81 if (have_kb_thread) {
[ae5aa90]82 LOG("Join kb.thread.");
[31696b4f]83 thread_join(TASK->kb.thread);
84 thread_detach(TASK->kb.thread);
[ae5aa90]85 LOG("...join done.");
[31696b4f]86 TASK->kb.thread = NULL;
[9a1b20c]87 }
[da1bafb]88
[31696b4f]89 /* Answer all messages in 'calls' and 'dispatched_calls' queues. */
[5d3ed34]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);
[9a1b20c]92}
93
[05e69c5]94/** Handle hangup message in kbox.
95 *
[da1bafb]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 */
[05e69c5]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) {
[31696b4f]105 /* Terminate debugging session (if any). */
[ae5aa90]106 LOG("Terminate debugging session.");
[5c088975]107 mutex_lock(&TASK->udebug.lock);
[05e69c5]108 udebug_task_cleanup(TASK);
[5c088975]109 mutex_unlock(&TASK->udebug.lock);
[05e69c5]110 } else {
[ae5aa90]111 LOG("Was not debugger.");
[05e69c5]112 }
[da1bafb]113
[ae5aa90]114 LOG("Continue with hangup message.");
[05e69c5]115 IPC_SET_RETVAL(call->data, 0);
[31696b4f]116 ipc_answer(&TASK->kb.box, call);
[da1bafb]117
[c6f0e3a2]118 mutex_lock(&TASK->kb.cleanup_lock);
[da1bafb]119
120 irq_spinlock_lock(&TASK->lock, true);
121 irq_spinlock_lock(&TASK->kb.box.lock, false);
[fd72312]122 if (list_empty(&TASK->kb.box.connected_phones)) {
[05e69c5]123 /*
124 * Last phone has been disconnected. Detach this thread so it
125 * gets freed and signal to the caller.
126 */
[da1bafb]127
[05e69c5]128 /* Only detach kbox thread unless already terminating. */
[14ecd6c]129 if (TASK->kb.finished == false) {
[05e69c5]130 /* Detach kbox thread so it gets freed from memory. */
[31696b4f]131 thread_detach(TASK->kb.thread);
132 TASK->kb.thread = NULL;
[05e69c5]133 }
[da1bafb]134
[ae5aa90]135 LOG("Phone list is empty.");
[05e69c5]136 *last = true;
[da1bafb]137 } else
[05e69c5]138 *last = false;
[da1bafb]139
[4acaa7c0]140 irq_spinlock_unlock(&TASK->kb.box.lock, false);
141 irq_spinlock_unlock(&TASK->lock, true);
[da1bafb]142
[c6f0e3a2]143 mutex_unlock(&TASK->kb.cleanup_lock);
[05e69c5]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 *
[da1bafb]151 * @param arg Ignored.
152 *
[05e69c5]153 */
[9a1b20c]154static void kbox_thread_proc(void *arg)
155{
[da1bafb]156 (void) arg;
[ae5aa90]157 LOG("Starting.");
[da1bafb]158 bool done = false;
159
[9a1b20c]160 while (!done) {
[da1bafb]161 call_t *call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
[9a1b20c]162 SYNCH_FLAGS_NONE);
[da1bafb]163
[05e69c5]164 if (call == NULL)
[da1bafb]165 continue; /* Try again. */
166
[228e490]167 switch (IPC_GET_IMETHOD(call->data)) {
[da1bafb]168
[79ae36dd]169 case IPC_M_DEBUG:
[05e69c5]170 /* Handle debug call. */
171 udebug_call_receive(call);
172 break;
[da1bafb]173
[05e69c5]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;
[da1bafb]182
[05e69c5]183 default:
184 /* Ignore */
185 break;
[9a1b20c]186 }
187 }
[da1bafb]188
[ae5aa90]189 LOG("Exiting.");
[9a1b20c]190}
191
[da1bafb]192/** Connect phone to a task kernel-box specified by id.
[9a1b20c]193 *
[31696b4f]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.
[9a1b20c]198 *
[da1bafb]199 * @return Phone id on success, or negative error code.
200 *
[9a1b20c]201 */
202int ipc_connect_kbox(task_id_t taskid)
203{
[da1bafb]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);
[9a1b20c]209 return ENOENT;
210 }
[da1bafb]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);
[9a1b20c]221 return ENOENT;
222 }
[da1bafb]223
224 if (task->kb.finished != false) {
225 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]226 return EINVAL;
227 }
[da1bafb]228
229 int newphid = phone_alloc(TASK);
[9a1b20c]230 if (newphid < 0) {
[da1bafb]231 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]232 return ELIMIT;
233 }
[da1bafb]234
[9a1b20c]235 /* Connect the newly allocated phone to the kbox */
[da1bafb]236 ipc_phone_connect(&TASK->phones[newphid], &task->kb.box);
237
238 if (task->kb.thread != NULL) {
239 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]240 return newphid;
241 }
[da1bafb]242
[9a1b20c]243 /* Create a kbox thread */
[6eef3c4]244 thread_t *kb_thread = thread_create(kbox_thread_proc, NULL, task,
245 THREAD_FLAG_NONE, "kbox");
[9a1b20c]246 if (!kb_thread) {
[da1bafb]247 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]248 return ENOMEM;
249 }
[da1bafb]250
251 task->kb.thread = kb_thread;
[9a1b20c]252 thread_ready(kb_thread);
[da1bafb]253
254 mutex_unlock(&task->kb.cleanup_lock);
255
[9a1b20c]256 return newphid;
257}
258
259/** @}
260 */
Note: See TracBrowser for help on using the repository browser.