Subversion Repositories HelenOS

Rev

Rev 3492 | Rev 3665 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3492 Rev 3593
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup genericipc
29
/** @addtogroup genericipc
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <synch/synch.h>
35
#include <synch/synch.h>
36
#include <synch/spinlock.h>
36
#include <synch/spinlock.h>
37
#include <synch/mutex.h>
37
#include <synch/mutex.h>
38
#include <ipc/ipc.h>
38
#include <ipc/ipc.h>
39
#include <ipc/ipcrsc.h>
39
#include <ipc/ipcrsc.h>
40
#include <arch.h>
40
#include <arch.h>
41
#include <errno.h>
41
#include <errno.h>
42
#include <debug.h>
42
#include <debug.h>
43
#include <udebug/udebug_ipc.h>
43
#include <udebug/udebug_ipc.h>
44
#include <ipc/kbox.h>
44
#include <ipc/kbox.h>
45
 
45
 
46
void ipc_kbox_cleanup(void)
46
void ipc_kbox_cleanup(void)
47
{
47
{
-
 
48
    ipl_t ipl;
48
    bool have_kb_thread;
49
    bool have_kb_thread;
49
 
50
 
50
    /* Only hold kb_cleanup_lock while setting kb_finished - this is enough */
51
    /* Only hold kb_cleanup_lock while setting kb_finished - this is enough */
51
    mutex_lock(&TASK->kb_cleanup_lock);
52
    mutex_lock(&TASK->kb_cleanup_lock);
52
    TASK->kb_finished = true;
53
    TASK->kb_finished = true;
53
    mutex_unlock(&TASK->kb_cleanup_lock);
54
    mutex_unlock(&TASK->kb_cleanup_lock);
54
 
55
 
55
    have_kb_thread = (TASK->kb_thread != NULL);
56
    have_kb_thread = (TASK->kb_thread != NULL);
56
 
57
 
57
    /* From now on nobody will try to connect phones or attach kbox threads */
58
    /* From now on nobody will try to connect phones or attach kbox threads */
58
 
59
 
59
    /*
60
    /*
60
     * Disconnect all phones connected to our kbox. Passing true for
61
     * Disconnect all phones connected to our kbox. Passing true for
61
     * notify_box causes a HANGUP message to be inserted for each
62
     * notify_box causes a HANGUP message to be inserted for each
62
     * disconnected phone. This ensures the kbox thread is going to
63
     * disconnected phone. This ensures the kbox thread is going to
63
     * wake up and terminate.
64
     * wake up and terminate.
64
     */
65
     */
65
    ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread);
66
    ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread);
-
 
67
 
-
 
68
    /*
-
 
69
     * If the task was being debugged, clean up debugging session.
-
 
70
     * This is necessarry as slamming the phones won't force
-
 
71
     * kbox thread to clean it up since sender != debugger.
-
 
72
     */
-
 
73
    ipl = interrupts_disable();
-
 
74
    spinlock_lock(&TASK->lock);
-
 
75
    udebug_task_cleanup(TASK);
-
 
76
    spinlock_unlock(&TASK->lock);
-
 
77
    interrupts_restore(ipl);
66
   
78
   
67
    if (have_kb_thread) {
79
    if (have_kb_thread) {
68
        LOG("join kb_thread..\n");
80
        LOG("join kb_thread..\n");
69
        thread_join(TASK->kb_thread);
81
        thread_join(TASK->kb_thread);
70
        thread_detach(TASK->kb_thread);
82
        thread_detach(TASK->kb_thread);
71
        LOG("join done\n");
83
        LOG("join done\n");
72
        TASK->kb_thread = NULL;
84
        TASK->kb_thread = NULL;
73
    }
85
    }
74
 
86
 
75
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
87
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
76
    spinlock_lock(&TASK->kernel_box.lock);
88
    spinlock_lock(&TASK->kernel_box.lock);
77
    ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls);
89
    ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls);
78
    ipc_cleanup_call_list(&TASK->kernel_box.calls);
90
    ipc_cleanup_call_list(&TASK->kernel_box.calls);
79
    spinlock_unlock(&TASK->kernel_box.lock);
91
    spinlock_unlock(&TASK->kernel_box.lock);
80
}
92
}
81
 
93
 
-
 
94
/** Handle hangup message in kbox.
-
 
95
 *
-
 
96
 * @param call  The IPC_M_PHONE_HUNGUP call structure.
-
 
97
 * @param last  Output, the function stores @c true here if
-
 
98
 *      this was the last phone, @c false otherwise.
-
 
99
 **/
-
 
100
static void kbox_proc_phone_hungup(call_t *call, bool *last)
-
 
101
{
-
 
102
    ipl_t ipl;
-
 
103
 
-
 
104
    LOG("kbox_proc_phone_hungup()\n");
-
 
105
 
-
 
106
    /* Was it our debugger, who hung up? */
-
 
107
    if (call->sender == TASK->udebug.debugger) {
-
 
108
        /* Terminate debugging session (if any) */
-
 
109
        LOG("kbox: terminate debug session\n");
-
 
110
        ipl = interrupts_disable();
-
 
111
        spinlock_lock(&TASK->lock);
-
 
112
        udebug_task_cleanup(TASK);
-
 
113
        spinlock_unlock(&TASK->lock);
-
 
114
        interrupts_restore(ipl);
-
 
115
    } else {
-
 
116
        LOG("kbox: was not debugger\n");
-
 
117
    }
-
 
118
 
-
 
119
    LOG("kbox: continue with hangup message\n");
-
 
120
    IPC_SET_RETVAL(call->data, 0);
-
 
121
    ipc_answer(&TASK->kernel_box, call);
-
 
122
 
-
 
123
    ipl = interrupts_disable();
-
 
124
    spinlock_lock(&TASK->lock);
-
 
125
    spinlock_lock(&TASK->answerbox.lock);
-
 
126
    if (list_empty(&TASK->answerbox.connected_phones)) {
-
 
127
        /*
-
 
128
         * Last phone has been disconnected. Detach this thread so it
-
 
129
         * gets freed and signal to the caller.
-
 
130
         */
-
 
131
 
-
 
132
        /* Only detach kbox thread unless already terminating. */
-
 
133
        mutex_lock(&TASK->kb_cleanup_lock);
-
 
134
        if (&TASK->kb_finished == false) {
-
 
135
            /* Detach kbox thread so it gets freed from memory. */
-
 
136
            thread_detach(TASK->kb_thread);
-
 
137
            TASK->kb_thread = NULL;
-
 
138
        }
-
 
139
        mutex_unlock(&TASK->kb_cleanup_lock);
-
 
140
 
-
 
141
        LOG("phone list is empty\n");
-
 
142
        *last = true;
-
 
143
    } else {
-
 
144
        *last = false;
-
 
145
    }
-
 
146
 
-
 
147
    spinlock_unlock(&TASK->answerbox.lock);
-
 
148
    spinlock_unlock(&TASK->lock);
-
 
149
    interrupts_restore(ipl);
-
 
150
}
82
 
151
 
-
 
152
/** Implementing function for the kbox thread.
-
 
153
 *
-
 
154
 * This function listens for debug requests. It terminates
-
 
155
 * when all phones are disconnected from the kbox.
-
 
156
 *
-
 
157
 * @param arg   Ignored.
-
 
158
 */
83
static void kbox_thread_proc(void *arg)
159
static void kbox_thread_proc(void *arg)
84
{
160
{
85
    call_t *call;
161
    call_t *call;
86
    int method;
-
 
87
    bool done;
162
    bool done;
88
    ipl_t ipl;
-
 
89
 
163
 
90
    (void)arg;
164
    (void)arg;
91
    LOG("kbox_thread_proc()\n");
165
    LOG("kbox_thread_proc()\n");
92
    done = false;
166
    done = false;
93
 
167
 
94
    while (!done) {
168
    while (!done) {
95
        call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT,
169
        call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT,
96
            SYNCH_FLAGS_NONE);
170
            SYNCH_FLAGS_NONE);
97
 
171
 
98
        if (call != NULL) {
172
        if (call == NULL)
-
 
173
            continue;   /* Try again. */
-
 
174
 
99
            method = IPC_GET_METHOD(call->data);
175
        switch (IPC_GET_METHOD(call->data)) {
100
 
176
 
101
            if (method == IPC_M_DEBUG_ALL) {
177
        case IPC_M_DEBUG_ALL:
-
 
178
            /* Handle debug call. */
102
                udebug_call_receive(call);
179
            udebug_call_receive(call);
103
            }
180
            break;
104
 
181
 
105
            if (method == IPC_M_PHONE_HUNGUP) {
182
        case IPC_M_PHONE_HUNGUP:
106
                LOG("kbox: handle hangup message\n");
-
 
107
 
183
            /*
108
                /* Was it our debugger, who hung up? */
184
             * Process the hangup call. If this was the last
109
                if (call->sender == TASK->udebug.debugger) {
-
 
110
                    /* Terminate debugging session (if any) */
185
             * phone, done will be set to true and the
111
                    LOG("kbox: terminate debug session\n");
-
 
112
                    ipl = interrupts_disable();
186
             * while loop will terminate.
113
                    spinlock_lock(&TASK->lock);
-
 
114
                    udebug_task_cleanup(TASK);
-
 
115
                    spinlock_unlock(&TASK->lock);
-
 
116
                    interrupts_restore(ipl);
-
 
117
                } else {
187
             */
118
                    LOG("kbox: was not debugger\n");
188
            kbox_proc_phone_hungup(call, &done);
119
                }
189
            break;
120
 
-
 
121
                LOG("kbox: continue with hangup message\n");
-
 
122
                IPC_SET_RETVAL(call->data, 0);
-
 
123
                ipc_answer(&TASK->kernel_box, call);
-
 
124
 
190
 
125
                ipl = interrupts_disable();
-
 
126
                spinlock_lock(&TASK->lock);
-
 
127
                spinlock_lock(&TASK->answerbox.lock);
-
 
128
                if (list_empty(&TASK->answerbox.connected_phones)) {
-
 
129
                    /* Last phone has been disconnected */
-
 
130
                    TASK->kb_thread = NULL;
-
 
131
                    done = true;
191
        default:
132
                    LOG("phone list is empty\n");
-
 
133
                }
-
 
134
                spinlock_unlock(&TASK->answerbox.lock);
-
 
135
                spinlock_unlock(&TASK->lock);
-
 
136
                interrupts_restore(ipl);
192
            /* Ignore */
137
            }
193
            break;
138
        }
194
        }
139
    }
195
    }
140
 
196
 
141
    LOG("kbox: finished\n");
197
    LOG("kbox: finished\n");
142
}
198
}
143
 
199
 
144
 
200
 
145
/**
201
/**
146
 * Connect phone to a task kernel-box specified by id.
202
 * Connect phone to a task kernel-box specified by id.
147
 *
203
 *
148
 * Note that this is not completely atomic. For optimisation reasons,
204
 * Note that this is not completely atomic. For optimisation reasons,
149
 * The task might start cleaning up kbox after the phone has been connected
205
 * The task might start cleaning up kbox after the phone has been connected
150
 * and before a kbox thread has been created. This must be taken into account
206
 * and before a kbox thread has been created. This must be taken into account
151
 * in the cleanup code.
207
 * in the cleanup code.
152
 *
208
 *
153
 * @return      Phone id on success, or negative error code.
209
 * @return      Phone id on success, or negative error code.
154
 */
210
 */
155
int ipc_connect_kbox(task_id_t taskid)
211
int ipc_connect_kbox(task_id_t taskid)
156
{
212
{
157
    int newphid;
213
    int newphid;
158
    task_t *ta;
214
    task_t *ta;
159
    thread_t *kb_thread;
215
    thread_t *kb_thread;
160
    ipl_t ipl;
216
    ipl_t ipl;
161
 
217
 
162
    ipl = interrupts_disable();
218
    ipl = interrupts_disable();
163
    spinlock_lock(&tasks_lock);
219
    spinlock_lock(&tasks_lock);
164
 
220
 
165
    ta = task_find_by_id(taskid);
221
    ta = task_find_by_id(taskid);
166
    if (ta == NULL) {
222
    if (ta == NULL) {
167
        spinlock_unlock(&tasks_lock);
223
        spinlock_unlock(&tasks_lock);
168
        interrupts_restore(ipl);
224
        interrupts_restore(ipl);
169
        return ENOENT;
225
        return ENOENT;
170
    }
226
    }
171
 
227
 
172
    atomic_inc(&ta->refcount);
228
    atomic_inc(&ta->refcount);
173
 
229
 
174
    spinlock_unlock(&tasks_lock);
230
    spinlock_unlock(&tasks_lock);
175
    interrupts_restore(ipl);
231
    interrupts_restore(ipl);
176
 
232
 
177
    mutex_lock(&ta->kb_cleanup_lock);
233
    mutex_lock(&ta->kb_cleanup_lock);
178
 
234
 
179
    if (atomic_predec(&ta->refcount) == 0) {
235
    if (atomic_predec(&ta->refcount) == 0) {
180
        mutex_unlock(&ta->kb_cleanup_lock);
236
        mutex_unlock(&ta->kb_cleanup_lock);
181
        task_destroy(ta);
237
        task_destroy(ta);
182
        return ENOENT;
238
        return ENOENT;
183
    }
239
    }
184
 
240
 
185
    if (ta->kb_finished != false) {
241
    if (ta->kb_finished != false) {
186
        mutex_unlock(&ta->kb_cleanup_lock);
242
        mutex_unlock(&ta->kb_cleanup_lock);
187
        return EINVAL;
243
        return EINVAL;
188
    }
244
    }
189
 
245
 
190
    newphid = phone_alloc();
246
    newphid = phone_alloc();
191
    if (newphid < 0) {
247
    if (newphid < 0) {
192
        mutex_unlock(&ta->kb_cleanup_lock);
248
        mutex_unlock(&ta->kb_cleanup_lock);
193
        return ELIMIT;
249
        return ELIMIT;
194
    }
250
    }
195
 
251
 
196
    /* Connect the newly allocated phone to the kbox */
252
    /* Connect the newly allocated phone to the kbox */
197
    ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box);
253
    ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box);
198
 
254
 
199
    if (ta->kb_thread != NULL) {
255
    if (ta->kb_thread != NULL) {
200
        mutex_unlock(&ta->kb_cleanup_lock);
256
        mutex_unlock(&ta->kb_cleanup_lock);
201
        return newphid;
257
        return newphid;
202
    }
258
    }
203
 
259
 
204
    /* Create a kbox thread */
260
    /* Create a kbox thread */
205
    kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false);
261
    kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false);
206
    if (!kb_thread) {
262
    if (!kb_thread) {
207
        mutex_unlock(&ta->kb_cleanup_lock);
263
        mutex_unlock(&ta->kb_cleanup_lock);
208
        return ENOMEM;
264
        return ENOMEM;
209
    }
265
    }
210
 
266
 
211
    ta->kb_thread = kb_thread;
267
    ta->kb_thread = kb_thread;
212
    thread_ready(kb_thread);
268
    thread_ready(kb_thread);
213
 
269
 
214
    mutex_unlock(&ta->kb_cleanup_lock);
270
    mutex_unlock(&ta->kb_cleanup_lock);
215
 
271
 
216
    return newphid;
272
    return newphid;
217
}
273
}
218
 
274
 
219
/** @}
275
/** @}
220
 */
276
 */
221
 
277