Subversion Repositories HelenOS

Rev

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

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