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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f571ca49 was 48bcf49, checked in by Jakub Jermar <jakub@…>, 8 years ago

Introduce reference-counted kobjects

Capabilities are thus reduced to task-local names for references to kobjects.

  • Property mode set to 100644
File size: 7.2 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 *
[48bcf49]208 * @return Phone capability handle on success, or negative error code.
[da1bafb]209 *
[9a1b20c]210 */
211int ipc_connect_kbox(task_id_t taskid)
212{
[da1bafb]213 irq_spinlock_lock(&tasks_lock, true);
214
215 task_t *task = task_find_by_id(taskid);
216 if (task == NULL) {
217 irq_spinlock_unlock(&tasks_lock, true);
[9a1b20c]218 return ENOENT;
219 }
[da1bafb]220
221 atomic_inc(&task->refcount);
222
223 irq_spinlock_unlock(&tasks_lock, true);
224
225 mutex_lock(&task->kb.cleanup_lock);
226
227 if (atomic_predec(&task->refcount) == 0) {
228 mutex_unlock(&task->kb.cleanup_lock);
229 task_destroy(task);
[9a1b20c]230 return ENOENT;
231 }
[da1bafb]232
233 if (task->kb.finished != false) {
234 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]235 return EINVAL;
236 }
[da1bafb]237
[48bcf49]238 cap_handle_t phone_handle = phone_alloc(TASK);
239 if (phone_handle < 0) {
[da1bafb]240 mutex_unlock(&task->kb.cleanup_lock);
[48bcf49]241 return phone_handle;
[9a1b20c]242 }
[da1bafb]243
[48bcf49]244 kobject_t *phone_obj = kobject_get(TASK, phone_handle,
245 KOBJECT_TYPE_PHONE);
[9a1b20c]246 /* Connect the newly allocated phone to the kbox */
[48bcf49]247 /* Hand over phone_obj's reference to ipc_phone_connect() */
248 (void) ipc_phone_connect(phone_obj->phone, &task->kb.box);
[da1bafb]249
250 if (task->kb.thread != NULL) {
251 mutex_unlock(&task->kb.cleanup_lock);
[48bcf49]252 return phone_handle;
[9a1b20c]253 }
[da1bafb]254
[9a1b20c]255 /* Create a kbox thread */
[6eef3c4]256 thread_t *kb_thread = thread_create(kbox_thread_proc, NULL, task,
257 THREAD_FLAG_NONE, "kbox");
[9a1b20c]258 if (!kb_thread) {
[da1bafb]259 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]260 return ENOMEM;
261 }
[da1bafb]262
263 task->kb.thread = kb_thread;
[9a1b20c]264 thread_ready(kb_thread);
[da1bafb]265
266 mutex_unlock(&task->kb.cleanup_lock);
267
[48bcf49]268 return phone_handle;
[9a1b20c]269}
270
271/** @}
272 */
Note: See TracBrowser for help on using the repository browser.