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