source: mainline/kernel/generic/src/ipc/kbox.c@ 566da7f8

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

Call request_process() callback for incoming calls during IPC cleanup.

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