| 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 kernel_generic_ipc
|
|---|
| 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 <proc/thread.h>
|
|---|
| 46 |
|
|---|
| 47 | void ipc_kbox_cleanup(void)
|
|---|
| 48 | {
|
|---|
| 49 | /*
|
|---|
| 50 | * Not really needed, just to be consistent with the meaning of
|
|---|
| 51 | * answerbox_t.active.
|
|---|
| 52 | */
|
|---|
| 53 | irq_spinlock_lock(&TASK->kb.box.lock, true);
|
|---|
| 54 | TASK->kb.box.active = false;
|
|---|
| 55 | irq_spinlock_unlock(&TASK->kb.box.lock, true);
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | * Only hold kb.cleanup_lock while setting kb.finished -
|
|---|
| 59 | * this is enough.
|
|---|
| 60 | */
|
|---|
| 61 | mutex_lock(&TASK->kb.cleanup_lock);
|
|---|
| 62 | TASK->kb.finished = true;
|
|---|
| 63 | mutex_unlock(&TASK->kb.cleanup_lock);
|
|---|
| 64 |
|
|---|
| 65 | bool have_kb_thread = (TASK->kb.thread != NULL);
|
|---|
| 66 |
|
|---|
| 67 | /*
|
|---|
| 68 | * From now on nobody will try to connect phones or attach
|
|---|
| 69 | * kbox threads
|
|---|
| 70 | */
|
|---|
| 71 |
|
|---|
| 72 | /*
|
|---|
| 73 | * Disconnect all phones connected to our kbox. Passing true for
|
|---|
| 74 | * notify_box causes a HANGUP message to be inserted for each
|
|---|
| 75 | * disconnected phone. This ensures the kbox thread is going to
|
|---|
| 76 | * wake up and terminate.
|
|---|
| 77 | */
|
|---|
| 78 | ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
|
|---|
| 79 |
|
|---|
| 80 | /*
|
|---|
| 81 | * If the task was being debugged, clean up debugging session.
|
|---|
| 82 | * This is necessarry as slamming the phones won't force
|
|---|
| 83 | * kbox thread to clean it up since sender != debugger.
|
|---|
| 84 | */
|
|---|
| 85 | mutex_lock(&TASK->udebug.lock);
|
|---|
| 86 | udebug_task_cleanup(TASK);
|
|---|
| 87 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 88 |
|
|---|
| 89 | if (have_kb_thread) {
|
|---|
| 90 | LOG("Join kb.thread.");
|
|---|
| 91 | thread_join(TASK->kb.thread);
|
|---|
| 92 | thread_detach(TASK->kb.thread);
|
|---|
| 93 | LOG("...join done.");
|
|---|
| 94 | TASK->kb.thread = NULL;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /* Answer all messages in 'calls' and 'dispatched_calls' queues. */
|
|---|
| 98 | ipc_cleanup_call_list(&TASK->kb.box, &TASK->kb.box.calls);
|
|---|
| 99 | ipc_cleanup_call_list(&TASK->kb.box, &TASK->kb.box.dispatched_calls);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /** Handle hangup message in kbox.
|
|---|
| 103 | *
|
|---|
| 104 | * @param call The IPC_M_PHONE_HUNGUP call structure.
|
|---|
| 105 | * @param last Output, the function stores @c true here if
|
|---|
| 106 | * this was the last phone, @c false otherwise.
|
|---|
| 107 | *
|
|---|
| 108 | */
|
|---|
| 109 | static void kbox_proc_phone_hungup(call_t *call, bool *last)
|
|---|
| 110 | {
|
|---|
| 111 | /* Was it our debugger, who hung up? */
|
|---|
| 112 | if (call->sender == TASK->udebug.debugger) {
|
|---|
| 113 | /* Terminate debugging session (if any). */
|
|---|
| 114 | LOG("Terminate debugging session.");
|
|---|
| 115 | mutex_lock(&TASK->udebug.lock);
|
|---|
| 116 | udebug_task_cleanup(TASK);
|
|---|
| 117 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 118 | } else {
|
|---|
| 119 | LOG("Was not debugger.");
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | LOG("Continue with hangup message.");
|
|---|
| 123 | IPC_SET_RETVAL(call->data, 0);
|
|---|
| 124 | ipc_answer(&TASK->kb.box, call);
|
|---|
| 125 |
|
|---|
| 126 | mutex_lock(&TASK->kb.cleanup_lock);
|
|---|
| 127 |
|
|---|
| 128 | irq_spinlock_lock(&TASK->lock, true);
|
|---|
| 129 | irq_spinlock_lock(&TASK->kb.box.lock, false);
|
|---|
| 130 | if (list_empty(&TASK->kb.box.connected_phones)) {
|
|---|
| 131 | /*
|
|---|
| 132 | * Last phone has been disconnected. Detach this thread so it
|
|---|
| 133 | * gets freed and signal to the caller.
|
|---|
| 134 | */
|
|---|
| 135 |
|
|---|
| 136 | /* Only detach kbox thread unless already terminating. */
|
|---|
| 137 | if (TASK->kb.finished == false) {
|
|---|
| 138 | /* Detach kbox thread so it gets freed from memory. */
|
|---|
| 139 | thread_detach(TASK->kb.thread);
|
|---|
| 140 | TASK->kb.thread = NULL;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | LOG("Phone list is empty.");
|
|---|
| 144 | *last = true;
|
|---|
| 145 | } else
|
|---|
| 146 | *last = false;
|
|---|
| 147 |
|
|---|
| 148 | irq_spinlock_unlock(&TASK->kb.box.lock, false);
|
|---|
| 149 | irq_spinlock_unlock(&TASK->lock, true);
|
|---|
| 150 |
|
|---|
| 151 | mutex_unlock(&TASK->kb.cleanup_lock);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /** Implementing function for the kbox thread.
|
|---|
| 155 | *
|
|---|
| 156 | * This function listens for debug requests. It terminates
|
|---|
| 157 | * when all phones are disconnected from the kbox.
|
|---|
| 158 | *
|
|---|
| 159 | * @param arg Ignored.
|
|---|
| 160 | *
|
|---|
| 161 | */
|
|---|
| 162 | static void kbox_thread_proc(void *arg)
|
|---|
| 163 | {
|
|---|
| 164 | (void) arg;
|
|---|
| 165 | LOG("Starting.");
|
|---|
| 166 | bool done = false;
|
|---|
| 167 |
|
|---|
| 168 | while (!done) {
|
|---|
| 169 | call_t *call = NULL;
|
|---|
| 170 | (void) ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
|
|---|
| 171 | SYNCH_FLAGS_NONE, &call);
|
|---|
| 172 |
|
|---|
| 173 | if (call == NULL)
|
|---|
| 174 | continue; /* Try again. */
|
|---|
| 175 |
|
|---|
| 176 | switch (IPC_GET_IMETHOD(call->data)) {
|
|---|
| 177 |
|
|---|
| 178 | case IPC_M_DEBUG:
|
|---|
| 179 | /* Handle debug call. */
|
|---|
| 180 | udebug_call_receive(call);
|
|---|
| 181 | break;
|
|---|
| 182 |
|
|---|
| 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;
|
|---|
| 191 |
|
|---|
| 192 | default:
|
|---|
| 193 | /* Ignore */
|
|---|
| 194 | break;
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | LOG("Exiting.");
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /** Connect phone to a task kernel-box specified by id.
|
|---|
| 202 | *
|
|---|
| 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.
|
|---|
| 207 | *
|
|---|
| 208 | * @param[out] out_phone Phone capability handle on success.
|
|---|
| 209 | * @return Error code.
|
|---|
| 210 | *
|
|---|
| 211 | */
|
|---|
| 212 | errno_t ipc_connect_kbox(task_id_t taskid, cap_phone_handle_t *out_phone)
|
|---|
| 213 | {
|
|---|
| 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);
|
|---|
| 219 | return ENOENT;
|
|---|
| 220 | }
|
|---|
| 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);
|
|---|
| 231 | return ENOENT;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | if (task->kb.finished) {
|
|---|
| 235 | mutex_unlock(&task->kb.cleanup_lock);
|
|---|
| 236 | return EINVAL;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /* Create a kbox thread if necessary. */
|
|---|
| 240 | if (task->kb.thread == NULL) {
|
|---|
| 241 | thread_t *kb_thread = thread_create(kbox_thread_proc, NULL, task,
|
|---|
| 242 | THREAD_FLAG_NONE, "kbox");
|
|---|
| 243 |
|
|---|
| 244 | if (!kb_thread) {
|
|---|
| 245 | mutex_unlock(&task->kb.cleanup_lock);
|
|---|
| 246 | return ENOMEM;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | task->kb.thread = kb_thread;
|
|---|
| 250 | thread_ready(kb_thread);
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /* Allocate a new phone. */
|
|---|
| 254 | cap_phone_handle_t phone_handle;
|
|---|
| 255 | errno_t rc = phone_alloc(TASK, true, &phone_handle, NULL);
|
|---|
| 256 | if (rc != EOK) {
|
|---|
| 257 | mutex_unlock(&task->kb.cleanup_lock);
|
|---|
| 258 | return rc;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | kobject_t *phone_obj = kobject_get(TASK, phone_handle,
|
|---|
| 262 | KOBJECT_TYPE_PHONE);
|
|---|
| 263 | /* Connect the newly allocated phone to the kbox */
|
|---|
| 264 | /* Hand over phone_obj's reference to ipc_phone_connect() */
|
|---|
| 265 | (void) ipc_phone_connect(phone_obj->phone, &task->kb.box);
|
|---|
| 266 |
|
|---|
| 267 | mutex_unlock(&task->kb.cleanup_lock);
|
|---|
| 268 | *out_phone = phone_handle;
|
|---|
| 269 | return EOK;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /** @}
|
|---|
| 273 | */
|
|---|