Subversion Repositories HelenOS

Rev

Rev 3665 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3665 Rev 4073
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 <arch/asm.h>
41
#include <errno.h>
42
#include <errno.h>
42
#include <debug.h>
43
#include <debug.h>
-
 
44
#include <print.h>
43
#include <udebug/udebug_ipc.h>
45
#include <udebug/udebug_ipc.h>
44
#include <ipc/kbox.h>
46
#include <ipc/kbox.h>
45
 
47
 
46
void ipc_kbox_cleanup(void)
48
void ipc_kbox_cleanup(void)
47
{
49
{
48
    ipl_t ipl;
50
    ipl_t ipl;
49
    bool have_kb_thread;
51
    bool have_kb_thread;
50
 
52
 
51
    /*
53
    /*
52
     * Only hold kb.cleanup_lock while setting kb.finished -
54
     * Only hold kb.cleanup_lock while setting kb.finished -
53
     * this is enough.
55
     * this is enough.
54
     */
56
     */
55
    mutex_lock(&TASK->kb.cleanup_lock);
57
    mutex_lock(&TASK->kb.cleanup_lock);
56
    TASK->kb.finished = true;
58
    TASK->kb.finished = true;
57
    mutex_unlock(&TASK->kb.cleanup_lock);
59
    mutex_unlock(&TASK->kb.cleanup_lock);
58
 
60
 
59
    have_kb_thread = (TASK->kb.thread != NULL);
61
    have_kb_thread = (TASK->kb.thread != NULL);
60
 
62
 
61
    /*
63
    /*
62
     * From now on nobody will try to connect phones or attach
64
     * From now on nobody will try to connect phones or attach
63
     * kbox threads
65
     * kbox threads
64
     */
66
     */
65
 
67
 
66
    /*
68
    /*
67
     * Disconnect all phones connected to our kbox. Passing true for
69
     * Disconnect all phones connected to our kbox. Passing true for
68
     * notify_box causes a HANGUP message to be inserted for each
70
     * notify_box causes a HANGUP message to be inserted for each
69
     * disconnected phone. This ensures the kbox thread is going to
71
     * disconnected phone. This ensures the kbox thread is going to
70
     * wake up and terminate.
72
     * wake up and terminate.
71
     */
73
     */
72
    ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
74
    ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
73
 
75
 
74
    /*
76
    /*
75
     * If the task was being debugged, clean up debugging session.
77
     * If the task was being debugged, clean up debugging session.
76
     * This is necessarry as slamming the phones won't force
78
     * This is necessarry as slamming the phones won't force
77
     * kbox thread to clean it up since sender != debugger.
79
     * kbox thread to clean it up since sender != debugger.
78
     */
80
     */
79
    ipl = interrupts_disable();
81
    ipl = interrupts_disable();
80
    spinlock_lock(&TASK->lock);
82
    spinlock_lock(&TASK->lock);
81
    udebug_task_cleanup(TASK);
83
    udebug_task_cleanup(TASK);
82
    spinlock_unlock(&TASK->lock);
84
    spinlock_unlock(&TASK->lock);
83
    interrupts_restore(ipl);
85
    interrupts_restore(ipl);
84
   
86
   
85
    if (have_kb_thread) {
87
    if (have_kb_thread) {
86
        LOG("join kb.thread..\n");
88
        LOG("join kb.thread..\n");
87
        thread_join(TASK->kb.thread);
89
        thread_join(TASK->kb.thread);
88
        thread_detach(TASK->kb.thread);
90
        thread_detach(TASK->kb.thread);
89
        LOG("join done\n");
91
        LOG("join done\n");
90
        TASK->kb.thread = NULL;
92
        TASK->kb.thread = NULL;
91
    }
93
    }
92
 
94
 
93
    /* Answer all messages in 'calls' and 'dispatched_calls' queues. */
95
    /* Answer all messages in 'calls' and 'dispatched_calls' queues. */
94
    spinlock_lock(&TASK->kb.box.lock);
96
    spinlock_lock(&TASK->kb.box.lock);
95
    ipc_cleanup_call_list(&TASK->kb.box.dispatched_calls);
97
    ipc_cleanup_call_list(&TASK->kb.box.dispatched_calls);
96
    ipc_cleanup_call_list(&TASK->kb.box.calls);
98
    ipc_cleanup_call_list(&TASK->kb.box.calls);
97
    spinlock_unlock(&TASK->kb.box.lock);
99
    spinlock_unlock(&TASK->kb.box.lock);
98
}
100
}
99
 
101
 
100
/** Handle hangup message in kbox.
102
/** Handle hangup message in kbox.
101
 *
103
 *
102
 * @param call  The IPC_M_PHONE_HUNGUP call structure.
104
 * @param call  The IPC_M_PHONE_HUNGUP call structure.
103
 * @param last  Output, the function stores @c true here if
105
 * @param last  Output, the function stores @c true here if
104
 *      this was the last phone, @c false otherwise.
106
 *      this was the last phone, @c false otherwise.
105
 **/
107
 **/
106
static void kbox_proc_phone_hungup(call_t *call, bool *last)
108
static void kbox_proc_phone_hungup(call_t *call, bool *last)
107
{
109
{
108
    ipl_t ipl;
110
    ipl_t ipl;
109
 
111
 
110
    LOG("kbox_proc_phone_hungup()\n");
112
    LOG("kbox_proc_phone_hungup()\n");
111
 
113
 
112
    /* Was it our debugger, who hung up? */
114
    /* Was it our debugger, who hung up? */
113
    if (call->sender == TASK->udebug.debugger) {
115
    if (call->sender == TASK->udebug.debugger) {
114
        /* Terminate debugging session (if any). */
116
        /* Terminate debugging session (if any). */
115
        LOG("kbox: terminate debug session\n");
117
        LOG("kbox: terminate debug session\n");
116
        ipl = interrupts_disable();
118
        ipl = interrupts_disable();
117
        spinlock_lock(&TASK->lock);
119
        spinlock_lock(&TASK->lock);
118
        udebug_task_cleanup(TASK);
120
        udebug_task_cleanup(TASK);
119
        spinlock_unlock(&TASK->lock);
121
        spinlock_unlock(&TASK->lock);
120
        interrupts_restore(ipl);
122
        interrupts_restore(ipl);
121
    } else {
123
    } else {
122
        LOG("kbox: was not debugger\n");
124
        LOG("kbox: was not debugger\n");
123
    }
125
    }
124
 
126
 
125
    LOG("kbox: continue with hangup message\n");
127
    LOG("kbox: continue with hangup message\n");
126
    IPC_SET_RETVAL(call->data, 0);
128
    IPC_SET_RETVAL(call->data, 0);
127
    ipc_answer(&TASK->kb.box, call);
129
    ipc_answer(&TASK->kb.box, call);
128
 
130
 
129
    ipl = interrupts_disable();
131
    ipl = interrupts_disable();
130
    spinlock_lock(&TASK->lock);
132
    spinlock_lock(&TASK->lock);
131
    spinlock_lock(&TASK->answerbox.lock);
133
    spinlock_lock(&TASK->answerbox.lock);
132
    if (list_empty(&TASK->answerbox.connected_phones)) {
134
    if (list_empty(&TASK->answerbox.connected_phones)) {
133
        /*
135
        /*
134
         * Last phone has been disconnected. Detach this thread so it
136
         * Last phone has been disconnected. Detach this thread so it
135
         * gets freed and signal to the caller.
137
         * gets freed and signal to the caller.
136
         */
138
         */
137
 
139
 
138
        /* Only detach kbox thread unless already terminating. */
140
        /* Only detach kbox thread unless already terminating. */
139
        mutex_lock(&TASK->kb.cleanup_lock);
141
        mutex_lock(&TASK->kb.cleanup_lock);
140
        if (&TASK->kb.finished == false) {
142
        if (&TASK->kb.finished == false) {
141
            /* Detach kbox thread so it gets freed from memory. */
143
            /* Detach kbox thread so it gets freed from memory. */
142
            thread_detach(TASK->kb.thread);
144
            thread_detach(TASK->kb.thread);
143
            TASK->kb.thread = NULL;
145
            TASK->kb.thread = NULL;
144
        }
146
        }
145
        mutex_unlock(&TASK->kb.cleanup_lock);
147
        mutex_unlock(&TASK->kb.cleanup_lock);
146
 
148
 
147
        LOG("phone list is empty\n");
149
        LOG("phone list is empty\n");
148
        *last = true;
150
        *last = true;
149
    } else {
151
    } else {
150
        *last = false;
152
        *last = false;
151
    }
153
    }
152
 
154
 
153
    spinlock_unlock(&TASK->answerbox.lock);
155
    spinlock_unlock(&TASK->answerbox.lock);
154
    spinlock_unlock(&TASK->lock);
156
    spinlock_unlock(&TASK->lock);
155
    interrupts_restore(ipl);
157
    interrupts_restore(ipl);
156
}
158
}
157
 
159
 
158
/** Implementing function for the kbox thread.
160
/** Implementing function for the kbox thread.
159
 *
161
 *
160
 * This function listens for debug requests. It terminates
162
 * This function listens for debug requests. It terminates
161
 * when all phones are disconnected from the kbox.
163
 * when all phones are disconnected from the kbox.
162
 *
164
 *
163
 * @param arg   Ignored.
165
 * @param arg   Ignored.
164
 */
166
 */
165
static void kbox_thread_proc(void *arg)
167
static void kbox_thread_proc(void *arg)
166
{
168
{
167
    call_t *call;
169
    call_t *call;
168
    bool done;
170
    bool done;
169
 
171
 
170
    (void)arg;
172
    (void)arg;
171
    LOG("kbox_thread_proc()\n");
173
    LOG("kbox_thread_proc()\n");
172
    done = false;
174
    done = false;
173
 
175
 
174
    while (!done) {
176
    while (!done) {
175
        call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
177
        call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
176
            SYNCH_FLAGS_NONE);
178
            SYNCH_FLAGS_NONE);
177
 
179
 
178
        if (call == NULL)
180
        if (call == NULL)
179
            continue;   /* Try again. */
181
            continue;   /* Try again. */
180
 
182
 
181
        switch (IPC_GET_METHOD(call->data)) {
183
        switch (IPC_GET_METHOD(call->data)) {
182
 
184
 
183
        case IPC_M_DEBUG_ALL:
185
        case IPC_M_DEBUG_ALL:
184
            /* Handle debug call. */
186
            /* Handle debug call. */
185
            udebug_call_receive(call);
187
            udebug_call_receive(call);
186
            break;
188
            break;
187
 
189
 
188
        case IPC_M_PHONE_HUNGUP:
190
        case IPC_M_PHONE_HUNGUP:
189
            /*
191
            /*
190
             * Process the hangup call. If this was the last
192
             * Process the hangup call. If this was the last
191
             * phone, done will be set to true and the
193
             * phone, done will be set to true and the
192
             * while loop will terminate.
194
             * while loop will terminate.
193
             */
195
             */
194
            kbox_proc_phone_hungup(call, &done);
196
            kbox_proc_phone_hungup(call, &done);
195
            break;
197
            break;
196
 
198
 
197
        default:
199
        default:
198
            /* Ignore */
200
            /* Ignore */
199
            break;
201
            break;
200
        }
202
        }
201
    }
203
    }
202
 
204
 
203
    LOG("kbox: finished\n");
205
    LOG("kbox: finished\n");
204
}
206
}
205
 
207
 
206
 
208
 
207
/**
209
/**
208
 * Connect phone to a task kernel-box specified by id.
210
 * Connect phone to a task kernel-box specified by id.
209
 *
211
 *
210
 * Note that this is not completely atomic. For optimisation reasons, the task
212
 * Note that this is not completely atomic. For optimisation reasons, the task
211
 * might start cleaning up kbox after the phone has been connected and before
213
 * might start cleaning up kbox after the phone has been connected and before
212
 * a kbox thread has been created. This must be taken into account in the
214
 * a kbox thread has been created. This must be taken into account in the
213
 * cleanup code.
215
 * cleanup code.
214
 *
216
 *
215
 * @return      Phone id on success, or negative error code.
217
 * @return      Phone id on success, or negative error code.
216
 */
218
 */
217
int ipc_connect_kbox(task_id_t taskid)
219
int ipc_connect_kbox(task_id_t taskid)
218
{
220
{
219
    int newphid;
221
    int newphid;
220
    task_t *ta;
222
    task_t *ta;
221
    thread_t *kb_thread;
223
    thread_t *kb_thread;
222
    ipl_t ipl;
224
    ipl_t ipl;
223
 
225
 
224
    ipl = interrupts_disable();
226
    ipl = interrupts_disable();
225
    spinlock_lock(&tasks_lock);
227
    spinlock_lock(&tasks_lock);
226
 
228
 
227
    ta = task_find_by_id(taskid);
229
    ta = task_find_by_id(taskid);
228
    if (ta == NULL) {
230
    if (ta == NULL) {
229
        spinlock_unlock(&tasks_lock);
231
        spinlock_unlock(&tasks_lock);
230
        interrupts_restore(ipl);
232
        interrupts_restore(ipl);
231
        return ENOENT;
233
        return ENOENT;
232
    }
234
    }
233
 
235
 
234
    atomic_inc(&ta->refcount);
236
    atomic_inc(&ta->refcount);
235
 
237
 
236
    spinlock_unlock(&tasks_lock);
238
    spinlock_unlock(&tasks_lock);
237
    interrupts_restore(ipl);
239
    interrupts_restore(ipl);
238
 
240
 
239
    mutex_lock(&ta->kb.cleanup_lock);
241
    mutex_lock(&ta->kb.cleanup_lock);
240
 
242
 
241
    if (atomic_predec(&ta->refcount) == 0) {
243
    if (atomic_predec(&ta->refcount) == 0) {
242
        mutex_unlock(&ta->kb.cleanup_lock);
244
        mutex_unlock(&ta->kb.cleanup_lock);
243
        task_destroy(ta);
245
        task_destroy(ta);
244
        return ENOENT;
246
        return ENOENT;
245
    }
247
    }
246
 
248
 
247
    if (ta->kb.finished != false) {
249
    if (ta->kb.finished != false) {
248
        mutex_unlock(&ta->kb.cleanup_lock);
250
        mutex_unlock(&ta->kb.cleanup_lock);
249
        return EINVAL;
251
        return EINVAL;
250
    }
252
    }
251
 
253
 
252
    newphid = phone_alloc();
254
    newphid = phone_alloc();
253
    if (newphid < 0) {
255
    if (newphid < 0) {
254
        mutex_unlock(&ta->kb.cleanup_lock);
256
        mutex_unlock(&ta->kb.cleanup_lock);
255
        return ELIMIT;
257
        return ELIMIT;
256
    }
258
    }
257
 
259
 
258
    /* Connect the newly allocated phone to the kbox */
260
    /* Connect the newly allocated phone to the kbox */
259
    ipc_phone_connect(&TASK->phones[newphid], &ta->kb.box);
261
    ipc_phone_connect(&TASK->phones[newphid], &ta->kb.box);
260
 
262
 
261
    if (ta->kb.thread != NULL) {
263
    if (ta->kb.thread != NULL) {
262
        mutex_unlock(&ta->kb.cleanup_lock);
264
        mutex_unlock(&ta->kb.cleanup_lock);
263
        return newphid;
265
        return newphid;
264
    }
266
    }
265
 
267
 
266
    /* Create a kbox thread */
268
    /* Create a kbox thread */
267
    kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0,
269
    kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0,
268
        "kbox", false);
270
        "kbox", false);
269
    if (!kb_thread) {
271
    if (!kb_thread) {
270
        mutex_unlock(&ta->kb.cleanup_lock);
272
        mutex_unlock(&ta->kb.cleanup_lock);
271
        return ENOMEM;
273
        return ENOMEM;
272
    }
274
    }
273
 
275
 
274
    ta->kb.thread = kb_thread;
276
    ta->kb.thread = kb_thread;
275
    thread_ready(kb_thread);
277
    thread_ready(kb_thread);
276
 
278
 
277
    mutex_unlock(&ta->kb.cleanup_lock);
279
    mutex_unlock(&ta->kb.cleanup_lock);
278
 
280
 
279
    return newphid;
281
    return newphid;
280
}
282
}
281
 
283
 
282
/** @}
284
/** @}
283
 */
285
 */
284
 
286