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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 569a51a was 569a51a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Return phone handle in SYS_IPC_CONNECT_KBOX separately from error code.

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