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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fc89e32 was 1066041, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

preemption_disable: Turned functions into macros. Moved THREAD, AS, TASK, CPU into thread.h, as.h, task.h, cpu.h to fix the include hell that ensued.

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