Subversion Repositories HelenOS

Rev

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

Rev 3494 Rev 3495
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
    ipl_t ipl;
49
    bool have_kb_thread;
49
    bool have_kb_thread;
50
 
50
 
51
    /* 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 */
52
    mutex_lock(&TASK->kb_cleanup_lock);
52
    mutex_lock(&TASK->kb_cleanup_lock);
53
    TASK->kb_finished = true;
53
    TASK->kb_finished = true;
54
    mutex_unlock(&TASK->kb_cleanup_lock);
54
    mutex_unlock(&TASK->kb_cleanup_lock);
55
 
55
 
56
    have_kb_thread = (TASK->kb_thread != NULL);
56
    have_kb_thread = (TASK->kb_thread != NULL);
57
 
57
 
58
    /* 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 */
59
 
59
 
60
    /*
60
    /*
61
     * Disconnect all phones connected to our kbox. Passing true for
61
     * Disconnect all phones connected to our kbox. Passing true for
62
     * notify_box causes a HANGUP message to be inserted for each
62
     * notify_box causes a HANGUP message to be inserted for each
63
     * disconnected phone. This ensures the kbox thread is going to
63
     * disconnected phone. This ensures the kbox thread is going to
64
     * wake up and terminate.
64
     * wake up and terminate.
65
     */
65
     */
66
    ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread);
66
    ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread);
67
 
67
 
68
    /*
68
    /*
69
     * If the task was being debugged, clean up debugging session.
69
     * If the task was being debugged, clean up debugging session.
70
     * This is necessarry as slamming the phones won't force
70
     * This is necessarry as slamming the phones won't force
71
     * kbox thread to clean it up since sender != debugger.
71
     * kbox thread to clean it up since sender != debugger.
72
     */
72
     */
73
    ipl = interrupts_disable();
73
    ipl = interrupts_disable();
74
    spinlock_lock(&TASK->lock);
74
    spinlock_lock(&TASK->lock);
75
    udebug_task_cleanup(TASK);
75
    udebug_task_cleanup(TASK);
76
    spinlock_unlock(&TASK->lock);
76
    spinlock_unlock(&TASK->lock);
77
    interrupts_restore(ipl);
77
    interrupts_restore(ipl);
78
   
78
   
79
    if (have_kb_thread) {
79
    if (have_kb_thread) {
80
        LOG("join kb_thread..\n");
80
        LOG("join kb_thread..\n");
81
        thread_join(TASK->kb_thread);
81
        thread_join(TASK->kb_thread);
82
        thread_detach(TASK->kb_thread);
82
        thread_detach(TASK->kb_thread);
83
        LOG("join done\n");
83
        LOG("join done\n");
84
        TASK->kb_thread = NULL;
84
        TASK->kb_thread = NULL;
85
    }
85
    }
86
 
86
 
87
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
87
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
88
    spinlock_lock(&TASK->kernel_box.lock);
88
    spinlock_lock(&TASK->kernel_box.lock);
89
    ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls);
89
    ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls);
90
    ipc_cleanup_call_list(&TASK->kernel_box.calls);
90
    ipc_cleanup_call_list(&TASK->kernel_box.calls);
91
    spinlock_unlock(&TASK->kernel_box.lock);
91
    spinlock_unlock(&TASK->kernel_box.lock);
92
}
92
}
93
 
93
 
94
 
94
 
95
static void kbox_thread_proc(void *arg)
95
static void kbox_thread_proc(void *arg)
96
{
96
{
97
    call_t *call;
97
    call_t *call;
98
    int method;
98
    int method;
99
    bool done;
99
    bool done;
100
    ipl_t ipl;
100
    ipl_t ipl;
101
 
101
 
102
    (void)arg;
102
    (void)arg;
103
    LOG("kbox_thread_proc()\n");
103
    LOG("kbox_thread_proc()\n");
104
    done = false;
104
    done = false;
105
 
105
 
106
    while (!done) {
106
    while (!done) {
107
        call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT,
107
        call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT,
108
            SYNCH_FLAGS_NONE);
108
            SYNCH_FLAGS_NONE);
109
 
109
 
110
        if (call != NULL) {
110
        if (call != NULL) {
111
            method = IPC_GET_METHOD(call->data);
111
            method = IPC_GET_METHOD(call->data);
112
 
112
 
113
            if (method == IPC_M_DEBUG_ALL) {
113
            if (method == IPC_M_DEBUG_ALL) {
114
                udebug_call_receive(call);
114
                udebug_call_receive(call);
115
            }
115
            }
116
 
116
 
117
            if (method == IPC_M_PHONE_HUNGUP) {
117
            if (method == IPC_M_PHONE_HUNGUP) {
118
                LOG("kbox: handle hangup message\n");
118
                LOG("kbox: handle hangup message\n");
119
 
119
 
120
                /* Was it our debugger, who hung up? */
120
                /* Was it our debugger, who hung up? */
121
                if (call->sender == TASK->udebug.debugger) {
121
                if (call->sender == TASK->udebug.debugger) {
122
                    /* Terminate debugging session (if any) */
122
                    /* Terminate debugging session (if any) */
123
                    LOG("kbox: terminate debug session\n");
123
                    LOG("kbox: terminate debug session\n");
124
                    ipl = interrupts_disable();
124
                    ipl = interrupts_disable();
125
                    spinlock_lock(&TASK->lock);
125
                    spinlock_lock(&TASK->lock);
126
                    udebug_task_cleanup(TASK);
126
                    udebug_task_cleanup(TASK);
127
                    spinlock_unlock(&TASK->lock);
127
                    spinlock_unlock(&TASK->lock);
128
                    interrupts_restore(ipl);
128
                    interrupts_restore(ipl);
129
                } else {
129
                } else {
130
                    LOG("kbox: was not debugger\n");
130
                    LOG("kbox: was not debugger\n");
131
                }
131
                }
132
 
132
 
133
                LOG("kbox: continue with hangup message\n");
133
                LOG("kbox: continue with hangup message\n");
134
                IPC_SET_RETVAL(call->data, 0);
134
                IPC_SET_RETVAL(call->data, 0);
135
                ipc_answer(&TASK->kernel_box, call);
135
                ipc_answer(&TASK->kernel_box, call);
136
 
136
 
137
                ipl = interrupts_disable();
137
                ipl = interrupts_disable();
138
                spinlock_lock(&TASK->lock);
138
                spinlock_lock(&TASK->lock);
139
                spinlock_lock(&TASK->answerbox.lock);
139
                spinlock_lock(&TASK->answerbox.lock);
140
                if (list_empty(&TASK->answerbox.connected_phones)) {
140
                if (list_empty(&TASK->answerbox.connected_phones)) {
141
                    /*
141
                    /*
142
                     * Last phone has been disconnected.
142
                     * Last phone has been disconnected.
-
 
143
                     * Detach this thread so it gets
-
 
144
                     * freed and terminate.
143
                     */
145
                     */
144
 
146
 
-
 
147
                    /* Only need to detach thread unless already terminating. */
-
 
148
                    mutex_lock(&TASK->kb_cleanup_lock);
-
 
149
                    if (&TASK->kb_finished == false) {
145
                    /* Detach thread so it gets freed. */
150
                        /* Detach thread so it gets freed. */
146
                    thread_detach(TASK->kb_thread);
151
                        thread_detach(TASK->kb_thread);
147
                    TASK->kb_thread = NULL;
152
                        TASK->kb_thread = NULL;
-
 
153
                    }
-
 
154
                    mutex_unlock(&TASK->kb_cleanup_lock);
148
                    done = true;
155
                    done = true;
149
                    LOG("phone list is empty\n");
156
                    LOG("phone list is empty\n");
150
                }
157
                }
151
                spinlock_unlock(&TASK->answerbox.lock);
158
                spinlock_unlock(&TASK->answerbox.lock);
152
                spinlock_unlock(&TASK->lock);
159
                spinlock_unlock(&TASK->lock);
153
                interrupts_restore(ipl);
160
                interrupts_restore(ipl);
154
            }
161
            }
155
        }
162
        }
156
    }
163
    }
157
 
164
 
158
    LOG("kbox: finished\n");
165
    LOG("kbox: finished\n");
159
}
166
}
160
 
167
 
161
 
168
 
162
/**
169
/**
163
 * Connect phone to a task kernel-box specified by id.
170
 * Connect phone to a task kernel-box specified by id.
164
 *
171
 *
165
 * Note that this is not completely atomic. For optimisation reasons,
172
 * Note that this is not completely atomic. For optimisation reasons,
166
 * The task might start cleaning up kbox after the phone has been connected
173
 * The task might start cleaning up kbox after the phone has been connected
167
 * and before a kbox thread has been created. This must be taken into account
174
 * and before a kbox thread has been created. This must be taken into account
168
 * in the cleanup code.
175
 * in the cleanup code.
169
 *
176
 *
170
 * @return      Phone id on success, or negative error code.
177
 * @return      Phone id on success, or negative error code.
171
 */
178
 */
172
int ipc_connect_kbox(task_id_t taskid)
179
int ipc_connect_kbox(task_id_t taskid)
173
{
180
{
174
    int newphid;
181
    int newphid;
175
    task_t *ta;
182
    task_t *ta;
176
    thread_t *kb_thread;
183
    thread_t *kb_thread;
177
    ipl_t ipl;
184
    ipl_t ipl;
178
 
185
 
179
    ipl = interrupts_disable();
186
    ipl = interrupts_disable();
180
    spinlock_lock(&tasks_lock);
187
    spinlock_lock(&tasks_lock);
181
 
188
 
182
    ta = task_find_by_id(taskid);
189
    ta = task_find_by_id(taskid);
183
    if (ta == NULL) {
190
    if (ta == NULL) {
184
        spinlock_unlock(&tasks_lock);
191
        spinlock_unlock(&tasks_lock);
185
        interrupts_restore(ipl);
192
        interrupts_restore(ipl);
186
        return ENOENT;
193
        return ENOENT;
187
    }
194
    }
188
 
195
 
189
    atomic_inc(&ta->refcount);
196
    atomic_inc(&ta->refcount);
190
 
197
 
191
    spinlock_unlock(&tasks_lock);
198
    spinlock_unlock(&tasks_lock);
192
    interrupts_restore(ipl);
199
    interrupts_restore(ipl);
193
 
200
 
194
    mutex_lock(&ta->kb_cleanup_lock);
201
    mutex_lock(&ta->kb_cleanup_lock);
195
 
202
 
196
    if (atomic_predec(&ta->refcount) == 0) {
203
    if (atomic_predec(&ta->refcount) == 0) {
197
        mutex_unlock(&ta->kb_cleanup_lock);
204
        mutex_unlock(&ta->kb_cleanup_lock);
198
        task_destroy(ta);
205
        task_destroy(ta);
199
        return ENOENT;
206
        return ENOENT;
200
    }
207
    }
201
 
208
 
202
    if (ta->kb_finished != false) {
209
    if (ta->kb_finished != false) {
203
        mutex_unlock(&ta->kb_cleanup_lock);
210
        mutex_unlock(&ta->kb_cleanup_lock);
204
        return EINVAL;
211
        return EINVAL;
205
    }
212
    }
206
 
213
 
207
    newphid = phone_alloc();
214
    newphid = phone_alloc();
208
    if (newphid < 0) {
215
    if (newphid < 0) {
209
        mutex_unlock(&ta->kb_cleanup_lock);
216
        mutex_unlock(&ta->kb_cleanup_lock);
210
        return ELIMIT;
217
        return ELIMIT;
211
    }
218
    }
212
 
219
 
213
    /* Connect the newly allocated phone to the kbox */
220
    /* Connect the newly allocated phone to the kbox */
214
    ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box);
221
    ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box);
215
 
222
 
216
    if (ta->kb_thread != NULL) {
223
    if (ta->kb_thread != NULL) {
217
        mutex_unlock(&ta->kb_cleanup_lock);
224
        mutex_unlock(&ta->kb_cleanup_lock);
218
        return newphid;
225
        return newphid;
219
    }
226
    }
220
 
227
 
221
    /* Create a kbox thread */
228
    /* Create a kbox thread */
222
    kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false);
229
    kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false);
223
    if (!kb_thread) {
230
    if (!kb_thread) {
224
        mutex_unlock(&ta->kb_cleanup_lock);
231
        mutex_unlock(&ta->kb_cleanup_lock);
225
        return ENOMEM;
232
        return ENOMEM;
226
    }
233
    }
227
 
234
 
228
    ta->kb_thread = kb_thread;
235
    ta->kb_thread = kb_thread;
229
    thread_ready(kb_thread);
236
    thread_ready(kb_thread);
230
 
237
 
231
    mutex_unlock(&ta->kb_cleanup_lock);
238
    mutex_unlock(&ta->kb_cleanup_lock);
232
 
239
 
233
    return newphid;
240
    return newphid;
234
}
241
}
235
 
242
 
236
/** @}
243
/** @}
237
 */
244
 */
238
 
245