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

Last change on this file since 455241b was 455241b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 5 months ago

Integrate kobject_t into target structures

This just means fewer allocations and indirections.

  • Property mode set to 100644
File size: 6.8 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
[174156fd]29/** @addtogroup kernel_generic_ipc
[9a1b20c]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>
[1066041]45#include <proc/thread.h>
[9a1b20c]46
47void ipc_kbox_cleanup(void)
48{
[c33f39f]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
[da1bafb]57 /*
[31696b4f]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);
[a35b458]64
[da1bafb]65 bool have_kb_thread = (TASK->kb.thread != NULL);
[a35b458]66
[31696b4f]67 /*
68 * From now on nobody will try to connect phones or attach
69 * kbox threads
70 */
[a35b458]71
[9a1b20c]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 */
[31696b4f]78 ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
[a35b458]79
[da1bafb]80 /*
[94f8e3c1]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 */
[f173404]85 mutex_lock(&TASK->udebug.lock);
[94f8e3c1]86 udebug_task_cleanup(TASK);
[f173404]87 mutex_unlock(&TASK->udebug.lock);
[a35b458]88
[9a1b20c]89 if (have_kb_thread) {
[ae5aa90]90 LOG("Join kb.thread.");
[31696b4f]91 thread_join(TASK->kb.thread);
[ae5aa90]92 LOG("...join done.");
[31696b4f]93 TASK->kb.thread = NULL;
[9a1b20c]94 }
[a35b458]95
[31696b4f]96 /* Answer all messages in 'calls' and 'dispatched_calls' queues. */
[5d3ed34]97 ipc_cleanup_call_list(&TASK->kb.box, &TASK->kb.box.calls);
[716185d]98 ipc_cleanup_call_list(&TASK->kb.box, &TASK->kb.box.dispatched_calls);
[9a1b20c]99}
100
[05e69c5]101/** Handle hangup message in kbox.
102 *
[da1bafb]103 * @param call The IPC_M_PHONE_HUNGUP call structure.
104 * @param last Output, the function stores @c true here if
105 * this was the last phone, @c false otherwise.
106 *
107 */
[05e69c5]108static void kbox_proc_phone_hungup(call_t *call, bool *last)
109{
110 /* Was it our debugger, who hung up? */
111 if (call->sender == TASK->udebug.debugger) {
[31696b4f]112 /* Terminate debugging session (if any). */
[ae5aa90]113 LOG("Terminate debugging session.");
[5c088975]114 mutex_lock(&TASK->udebug.lock);
[05e69c5]115 udebug_task_cleanup(TASK);
[5c088975]116 mutex_unlock(&TASK->udebug.lock);
[05e69c5]117 } else {
[ae5aa90]118 LOG("Was not debugger.");
[05e69c5]119 }
[a35b458]120
[ae5aa90]121 LOG("Continue with hangup message.");
[fafb8e5]122 ipc_set_retval(&call->data, 0);
[31696b4f]123 ipc_answer(&TASK->kb.box, call);
[a35b458]124
[c6f0e3a2]125 mutex_lock(&TASK->kb.cleanup_lock);
[a35b458]126
[da1bafb]127 irq_spinlock_lock(&TASK->lock, true);
128 irq_spinlock_lock(&TASK->kb.box.lock, false);
[fd72312]129 if (list_empty(&TASK->kb.box.connected_phones)) {
[05e69c5]130 /*
131 * Last phone has been disconnected. Detach this thread so it
132 * gets freed and signal to the caller.
133 */
[a35b458]134
[05e69c5]135 /* Only detach kbox thread unless already terminating. */
[14ecd6c]136 if (TASK->kb.finished == false) {
[1871118]137 /* Release kbox thread so it gets freed from memory. */
138 thread_put(TASK->kb.thread);
[31696b4f]139 TASK->kb.thread = NULL;
[05e69c5]140 }
[a35b458]141
[ae5aa90]142 LOG("Phone list is empty.");
[05e69c5]143 *last = true;
[da1bafb]144 } else
[05e69c5]145 *last = false;
[a35b458]146
[4acaa7c0]147 irq_spinlock_unlock(&TASK->kb.box.lock, false);
148 irq_spinlock_unlock(&TASK->lock, true);
[a35b458]149
[c6f0e3a2]150 mutex_unlock(&TASK->kb.cleanup_lock);
[05e69c5]151}
152
153/** Implementing function for the kbox thread.
154 *
155 * This function listens for debug requests. It terminates
156 * when all phones are disconnected from the kbox.
157 *
[da1bafb]158 * @param arg Ignored.
159 *
[05e69c5]160 */
[9a1b20c]161static void kbox_thread_proc(void *arg)
162{
[da1bafb]163 (void) arg;
[ae5aa90]164 LOG("Starting.");
[da1bafb]165 bool done = false;
[a35b458]166
[9a1b20c]167 while (!done) {
[acf6b55]168 call_t *call = NULL;
169 (void) ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
170 SYNCH_FLAGS_NONE, &call);
[a35b458]171
[05e69c5]172 if (call == NULL)
[da1bafb]173 continue; /* Try again. */
[a35b458]174
[fafb8e5]175 switch (ipc_get_imethod(&call->data)) {
[a35b458]176
[79ae36dd]177 case IPC_M_DEBUG:
[05e69c5]178 /* Handle debug call. */
179 udebug_call_receive(call);
180 break;
[a35b458]181
[05e69c5]182 case IPC_M_PHONE_HUNGUP:
183 /*
184 * Process the hangup call. If this was the last
185 * phone, done will be set to true and the
186 * while loop will terminate.
187 */
188 kbox_proc_phone_hungup(call, &done);
189 break;
[a35b458]190
[05e69c5]191 default:
192 /* Ignore */
193 break;
[9a1b20c]194 }
195 }
[a35b458]196
[ae5aa90]197 LOG("Exiting.");
[9a1b20c]198}
199
[da1bafb]200/** Connect phone to a task kernel-box specified by id.
[9a1b20c]201 *
[569a51a]202 * @param[out] out_phone Phone capability handle on success.
203 * @return Error code.
[da1bafb]204 *
[9a1b20c]205 */
[eadaeae8]206errno_t ipc_connect_kbox(task_id_t taskid, cap_phone_handle_t *out_phone)
[9a1b20c]207{
[da1bafb]208 task_t *task = task_find_by_id(taskid);
[07d4271]209 if (!task)
[9a1b20c]210 return ENOENT;
[a35b458]211
[da1bafb]212 mutex_lock(&task->kb.cleanup_lock);
[a35b458]213
[0016674]214 if (task->kb.finished) {
[da1bafb]215 mutex_unlock(&task->kb.cleanup_lock);
[07d4271]216 task_release(task);
[9a1b20c]217 return EINVAL;
218 }
[a35b458]219
[0016674]220 /* Create a kbox thread if necessary. */
221 if (task->kb.thread == NULL) {
222 thread_t *kb_thread = thread_create(kbox_thread_proc, NULL, task,
223 THREAD_FLAG_NONE, "kbox");
[a35b458]224
[0016674]225 if (!kb_thread) {
226 mutex_unlock(&task->kb.cleanup_lock);
[07d4271]227 task_release(task);
[0016674]228 return ENOMEM;
229 }
[a35b458]230
[0f4f1b2]231 task->kb.thread = kb_thread;
232 thread_start(kb_thread);
[0016674]233 }
[a35b458]234
[0016674]235 /* Allocate a new phone. */
[eadaeae8]236 cap_phone_handle_t phone_handle;
[334c103]237 errno_t rc = phone_alloc(TASK, true, &phone_handle, NULL);
[09d01f2]238 if (rc != EOK) {
[da1bafb]239 mutex_unlock(&task->kb.cleanup_lock);
[07d4271]240 task_release(task);
[09d01f2]241 return rc;
[9a1b20c]242 }
[a35b458]243
[455241b]244 phone_t *phone = phone_from_kobject(
245 kobject_get(TASK, phone_handle, 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() */
[455241b]248 (void) ipc_phone_connect(phone, &task->kb.box);
[a35b458]249
[da1bafb]250 mutex_unlock(&task->kb.cleanup_lock);
[07d4271]251 task_release(task);
[569a51a]252 *out_phone = phone_handle;
253 return EOK;
[9a1b20c]254}
255
256/** @}
257 */
Note: See TracBrowser for help on using the repository browser.