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 |
|
---|
47 | void 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 | irq_spinlock_lock(&TASK->kb.box.lock, true);
|
---|
91 | ipc_cleanup_call_list(&TASK->kb.box.dispatched_calls);
|
---|
92 | ipc_cleanup_call_list(&TASK->kb.box.calls);
|
---|
93 | irq_spinlock_unlock(&TASK->kb.box.lock, true);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Handle hangup message in kbox.
|
---|
97 | *
|
---|
98 | * @param call The IPC_M_PHONE_HUNGUP call structure.
|
---|
99 | * @param last Output, the function stores @c true here if
|
---|
100 | * this was the last phone, @c false otherwise.
|
---|
101 | *
|
---|
102 | */
|
---|
103 | static void kbox_proc_phone_hungup(call_t *call, bool *last)
|
---|
104 | {
|
---|
105 | /* Was it our debugger, who hung up? */
|
---|
106 | if (call->sender == TASK->udebug.debugger) {
|
---|
107 | /* Terminate debugging session (if any). */
|
---|
108 | LOG("Terminate debugging session.");
|
---|
109 | mutex_lock(&TASK->udebug.lock);
|
---|
110 | udebug_task_cleanup(TASK);
|
---|
111 | mutex_unlock(&TASK->udebug.lock);
|
---|
112 | } else {
|
---|
113 | LOG("Was not debugger.");
|
---|
114 | }
|
---|
115 |
|
---|
116 | LOG("Continue with hangup message.");
|
---|
117 | IPC_SET_RETVAL(call->data, 0);
|
---|
118 | ipc_answer(&TASK->kb.box, call);
|
---|
119 |
|
---|
120 | mutex_lock(&TASK->kb.cleanup_lock);
|
---|
121 |
|
---|
122 | irq_spinlock_lock(&TASK->lock, true);
|
---|
123 | irq_spinlock_lock(&TASK->kb.box.lock, false);
|
---|
124 | if (list_empty(&TASK->kb.box.connected_phones)) {
|
---|
125 | /*
|
---|
126 | * Last phone has been disconnected. Detach this thread so it
|
---|
127 | * gets freed and signal to the caller.
|
---|
128 | */
|
---|
129 |
|
---|
130 | /* Only detach kbox thread unless already terminating. */
|
---|
131 | if (TASK->kb.finished == false) {
|
---|
132 | /* Detach kbox thread so it gets freed from memory. */
|
---|
133 | thread_detach(TASK->kb.thread);
|
---|
134 | TASK->kb.thread = NULL;
|
---|
135 | }
|
---|
136 |
|
---|
137 | LOG("Phone list is empty.");
|
---|
138 | *last = true;
|
---|
139 | } else
|
---|
140 | *last = false;
|
---|
141 |
|
---|
142 | irq_spinlock_unlock(&TASK->kb.box.lock, false);
|
---|
143 | irq_spinlock_unlock(&TASK->lock, true);
|
---|
144 |
|
---|
145 | mutex_unlock(&TASK->kb.cleanup_lock);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Implementing function for the kbox thread.
|
---|
149 | *
|
---|
150 | * This function listens for debug requests. It terminates
|
---|
151 | * when all phones are disconnected from the kbox.
|
---|
152 | *
|
---|
153 | * @param arg Ignored.
|
---|
154 | *
|
---|
155 | */
|
---|
156 | static void kbox_thread_proc(void *arg)
|
---|
157 | {
|
---|
158 | (void) arg;
|
---|
159 | LOG("Starting.");
|
---|
160 | bool done = false;
|
---|
161 |
|
---|
162 | while (!done) {
|
---|
163 | call_t *call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
|
---|
164 | SYNCH_FLAGS_NONE);
|
---|
165 |
|
---|
166 | if (call == NULL)
|
---|
167 | continue; /* Try again. */
|
---|
168 |
|
---|
169 | switch (IPC_GET_IMETHOD(call->data)) {
|
---|
170 |
|
---|
171 | case IPC_M_DEBUG_ALL:
|
---|
172 | /* Handle debug call. */
|
---|
173 | udebug_call_receive(call);
|
---|
174 | break;
|
---|
175 |
|
---|
176 | case IPC_M_PHONE_HUNGUP:
|
---|
177 | /*
|
---|
178 | * Process the hangup call. If this was the last
|
---|
179 | * phone, done will be set to true and the
|
---|
180 | * while loop will terminate.
|
---|
181 | */
|
---|
182 | kbox_proc_phone_hungup(call, &done);
|
---|
183 | break;
|
---|
184 |
|
---|
185 | default:
|
---|
186 | /* Ignore */
|
---|
187 | break;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | LOG("Exiting.");
|
---|
192 | }
|
---|
193 |
|
---|
194 | /** Connect phone to a task kernel-box specified by id.
|
---|
195 | *
|
---|
196 | * Note that this is not completely atomic. For optimisation reasons, the task
|
---|
197 | * might start cleaning up kbox after the phone has been connected and before
|
---|
198 | * a kbox thread has been created. This must be taken into account in the
|
---|
199 | * cleanup code.
|
---|
200 | *
|
---|
201 | * @return Phone id on success, or negative error code.
|
---|
202 | *
|
---|
203 | */
|
---|
204 | int ipc_connect_kbox(task_id_t taskid)
|
---|
205 | {
|
---|
206 | irq_spinlock_lock(&tasks_lock, true);
|
---|
207 |
|
---|
208 | task_t *task = task_find_by_id(taskid);
|
---|
209 | if (task == NULL) {
|
---|
210 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
211 | return ENOENT;
|
---|
212 | }
|
---|
213 |
|
---|
214 | atomic_inc(&task->refcount);
|
---|
215 |
|
---|
216 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
217 |
|
---|
218 | mutex_lock(&task->kb.cleanup_lock);
|
---|
219 |
|
---|
220 | if (atomic_predec(&task->refcount) == 0) {
|
---|
221 | mutex_unlock(&task->kb.cleanup_lock);
|
---|
222 | task_destroy(task);
|
---|
223 | return ENOENT;
|
---|
224 | }
|
---|
225 |
|
---|
226 | if (task->kb.finished != false) {
|
---|
227 | mutex_unlock(&task->kb.cleanup_lock);
|
---|
228 | return EINVAL;
|
---|
229 | }
|
---|
230 |
|
---|
231 | int newphid = phone_alloc(TASK);
|
---|
232 | if (newphid < 0) {
|
---|
233 | mutex_unlock(&task->kb.cleanup_lock);
|
---|
234 | return ELIMIT;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /* Connect the newly allocated phone to the kbox */
|
---|
238 | ipc_phone_connect(&TASK->phones[newphid], &task->kb.box);
|
---|
239 |
|
---|
240 | if (task->kb.thread != NULL) {
|
---|
241 | mutex_unlock(&task->kb.cleanup_lock);
|
---|
242 | return newphid;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /* Create a kbox thread */
|
---|
246 | thread_t *kb_thread = thread_create(kbox_thread_proc, NULL, task, 0,
|
---|
247 | "kbox", false);
|
---|
248 | if (!kb_thread) {
|
---|
249 | mutex_unlock(&task->kb.cleanup_lock);
|
---|
250 | return ENOMEM;
|
---|
251 | }
|
---|
252 |
|
---|
253 | task->kb.thread = kb_thread;
|
---|
254 | thread_ready(kb_thread);
|
---|
255 |
|
---|
256 | mutex_unlock(&task->kb.cleanup_lock);
|
---|
257 |
|
---|
258 | return newphid;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /** @}
|
---|
262 | */
|
---|