Subversion Repositories HelenOS-historic

Rev

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

Rev 1587 Rev 1588
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
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
/**
29
/**
30
 * @file    task.c
30
 * @file    task.c
31
 * @brief   Task management.
31
 * @brief   Task management.
32
 */
32
 */
33
 
33
 
34
#include <main/uinit.h>
34
#include <main/uinit.h>
35
#include <proc/thread.h>
35
#include <proc/thread.h>
36
#include <proc/task.h>
36
#include <proc/task.h>
37
#include <proc/uarg.h>
37
#include <proc/uarg.h>
38
#include <mm/as.h>
38
#include <mm/as.h>
39
#include <mm/slab.h>
39
#include <mm/slab.h>
40
#include <synch/spinlock.h>
40
#include <synch/spinlock.h>
41
#include <arch.h>
41
#include <arch.h>
42
#include <panic.h>
42
#include <panic.h>
43
#include <adt/btree.h>
43
#include <adt/btree.h>
44
#include <adt/list.h>
44
#include <adt/list.h>
45
#include <ipc/ipc.h>
45
#include <ipc/ipc.h>
46
#include <security/cap.h>
46
#include <security/cap.h>
47
#include <memstr.h>
47
#include <memstr.h>
48
#include <print.h>
48
#include <print.h>
49
#include <elf.h>
49
#include <elf.h>
50
#include <errno.h>
50
#include <errno.h>
51
#include <syscall/copy.h>
51
#include <syscall/copy.h>
52
 
52
 
53
#ifndef LOADED_PROG_STACK_PAGES_NO
53
#ifndef LOADED_PROG_STACK_PAGES_NO
54
#define LOADED_PROG_STACK_PAGES_NO 1
54
#define LOADED_PROG_STACK_PAGES_NO 1
55
#endif
55
#endif
56
 
56
 
57
SPINLOCK_INITIALIZE(tasks_lock);
57
SPINLOCK_INITIALIZE(tasks_lock);
58
btree_t tasks_btree;
58
btree_t tasks_btree;
59
static task_id_t task_counter = 0;
59
static task_id_t task_counter = 0;
60
 
60
 
61
static void ktaskclnp(void *arg);
61
static void ktaskclnp(void *arg);
62
static void ktaskkill(void *arg);
62
static void ktaskkill(void *arg);
63
 
63
 
64
/** Initialize tasks
64
/** Initialize tasks
65
 *
65
 *
66
 * Initialize kernel tasks support.
66
 * Initialize kernel tasks support.
67
 *
67
 *
68
 */
68
 */
69
void task_init(void)
69
void task_init(void)
70
{
70
{
71
    TASK = NULL;
71
    TASK = NULL;
72
    btree_create(&tasks_btree);
72
    btree_create(&tasks_btree);
73
}
73
}
74
 
74
 
75
 
75
 
76
/** Create new task
76
/** Create new task
77
 *
77
 *
78
 * Create new task with no threads.
78
 * Create new task with no threads.
79
 *
79
 *
80
 * @param as Task's address space.
80
 * @param as Task's address space.
81
 * @param name Symbolic name.
81
 * @param name Symbolic name.
82
 *
82
 *
83
 * @return New task's structure
83
 * @return New task's structure
84
 *
84
 *
85
 */
85
 */
86
task_t *task_create(as_t *as, char *name)
86
task_t *task_create(as_t *as, char *name)
87
{
87
{
88
    ipl_t ipl;
88
    ipl_t ipl;
89
    task_t *ta;
89
    task_t *ta;
90
    int i;
90
    int i;
91
   
91
   
92
    ta = (task_t *) malloc(sizeof(task_t), 0);
92
    ta = (task_t *) malloc(sizeof(task_t), 0);
93
 
93
 
94
    task_create_arch(ta);
94
    task_create_arch(ta);
95
 
95
 
96
    spinlock_initialize(&ta->lock, "task_ta_lock");
96
    spinlock_initialize(&ta->lock, "task_ta_lock");
97
    list_initialize(&ta->th_head);
97
    list_initialize(&ta->th_head);
98
    ta->as = as;
98
    ta->as = as;
99
    ta->name = name;
99
    ta->name = name;
100
    ta->main_thread = NULL;
100
    ta->main_thread = NULL;
101
    ta->refcount = 0;
101
    ta->refcount = 0;
102
 
102
 
103
    ta->capabilities = 0;
103
    ta->capabilities = 0;
104
    ta->accept_new_threads = true;
104
    ta->accept_new_threads = true;
105
   
105
   
106
    ipc_answerbox_init(&ta->answerbox);
106
    ipc_answerbox_init(&ta->answerbox);
107
    for (i=0; i < IPC_MAX_PHONES;i++)
107
    for (i=0; i < IPC_MAX_PHONES;i++)
108
        ipc_phone_init(&ta->phones[i]);
108
        ipc_phone_init(&ta->phones[i]);
109
    if (ipc_phone_0)
109
    if (ipc_phone_0)
110
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
110
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
111
    atomic_set(&ta->active_calls, 0);
111
    atomic_set(&ta->active_calls, 0);
112
 
112
 
113
    mutex_initialize(&ta->futexes_lock);
113
    mutex_initialize(&ta->futexes_lock);
114
    btree_create(&ta->futexes);
114
    btree_create(&ta->futexes);
115
   
115
   
116
    ipl = interrupts_disable();
116
    ipl = interrupts_disable();
117
 
117
 
118
    /*
118
    /*
119
     * Increment address space reference count.
119
     * Increment address space reference count.
120
     * TODO: Reconsider the locking scheme.
120
     * TODO: Reconsider the locking scheme.
121
     */
121
     */
122
    mutex_lock(&as->lock);
122
    mutex_lock(&as->lock);
123
    as->refcount++;
123
    as->refcount++;
124
    mutex_unlock(&as->lock);
124
    mutex_unlock(&as->lock);
125
 
125
 
126
    spinlock_lock(&tasks_lock);
126
    spinlock_lock(&tasks_lock);
127
 
127
 
128
    ta->taskid = ++task_counter;
128
    ta->taskid = ++task_counter;
129
    btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
129
    btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
130
 
130
 
131
    spinlock_unlock(&tasks_lock);
131
    spinlock_unlock(&tasks_lock);
132
    interrupts_restore(ipl);
132
    interrupts_restore(ipl);
133
 
133
 
134
    return ta;
134
    return ta;
135
}
135
}
136
 
136
 
137
/** Destroy task.
137
/** Destroy task.
138
 *
138
 *
139
 * @param t Task to be destroyed.
139
 * @param t Task to be destroyed.
140
 */
140
 */
141
void task_destroy(task_t *t)
141
void task_destroy(task_t *t)
142
{
142
{
143
    spinlock_lock(&tasks_lock);
-
 
144
    btree_remove(&tasks_btree, t->taskid, NULL);
-
 
145
    spinlock_unlock(&tasks_lock);
-
 
146
 
-
 
147
    task_destroy_arch(t);
143
    task_destroy_arch(t);
148
    btree_destroy(&t->futexes);
144
    btree_destroy(&t->futexes);
149
 
145
 
150
    mutex_lock_active(&t->as->lock);
146
    mutex_lock_active(&t->as->lock);
151
    if (--t->as->refcount == 0) {
147
    if (--t->as->refcount == 0) {
152
        mutex_unlock(&t->as->lock);
148
        mutex_unlock(&t->as->lock);
153
        as_destroy(t->as);
149
        as_destroy(t->as);
154
        /*
150
        /*
155
         * t->as is destroyed.
151
         * t->as is destroyed.
156
         */
152
         */
157
    } else {
153
    } else {
158
        mutex_unlock(&t->as->lock);
154
        mutex_unlock(&t->as->lock);
159
    }
155
    }
160
   
156
   
161
    free(t);
157
    free(t);
162
    TASK = NULL;
158
    TASK = NULL;
163
}
159
}
164
 
160
 
165
/** Create new task with 1 thread and run it
161
/** Create new task with 1 thread and run it
166
 *
162
 *
167
 * @param program_addr Address of program executable image.
163
 * @param program_addr Address of program executable image.
168
 * @param name Program name.
164
 * @param name Program name.
169
 *
165
 *
170
 * @return Task of the running program or NULL on error.
166
 * @return Task of the running program or NULL on error.
171
 */
167
 */
172
task_t * task_run_program(void *program_addr, char *name)
168
task_t * task_run_program(void *program_addr, char *name)
173
{
169
{
174
    as_t *as;
170
    as_t *as;
175
    as_area_t *a;
171
    as_area_t *a;
176
    int rc;
172
    int rc;
177
    thread_t *t1, *t2;
173
    thread_t *t1, *t2;
178
    task_t *task;
174
    task_t *task;
179
    uspace_arg_t *kernel_uarg;
175
    uspace_arg_t *kernel_uarg;
180
 
176
 
181
    as = as_create(0);
177
    as = as_create(0);
182
    ASSERT(as);
178
    ASSERT(as);
183
 
179
 
184
    rc = elf_load((elf_header_t *) program_addr, as);
180
    rc = elf_load((elf_header_t *) program_addr, as);
185
    if (rc != EE_OK) {
181
    if (rc != EE_OK) {
186
        as_destroy(as);
182
        as_destroy(as);
187
        return NULL;
183
        return NULL;
188
    }
184
    }
189
   
185
   
190
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
186
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
191
    kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
187
    kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
192
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
188
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
193
    kernel_uarg->uspace_thread_function = NULL;
189
    kernel_uarg->uspace_thread_function = NULL;
194
    kernel_uarg->uspace_thread_arg = NULL;
190
    kernel_uarg->uspace_thread_arg = NULL;
195
    kernel_uarg->uspace_uarg = NULL;
191
    kernel_uarg->uspace_uarg = NULL;
196
   
192
   
197
    task = task_create(as, name);
193
    task = task_create(as, name);
198
    ASSERT(task);
194
    ASSERT(task);
199
 
195
 
200
    /*
196
    /*
201
     * Create the data as_area.
197
     * Create the data as_area.
202
     */
198
     */
203
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
199
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
204
        LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
200
        LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
205
        USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
201
        USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
206
 
202
 
207
    /*
203
    /*
208
     * Create the main thread.
204
     * Create the main thread.
209
     */
205
     */
210
    t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
206
    t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
211
    ASSERT(t1);
207
    ASSERT(t1);
212
   
208
   
213
    /*
209
    /*
214
     * Create killer thread for the new task.
210
     * Create killer thread for the new task.
215
     */
211
     */
216
    t2 = thread_create(ktaskkill, t1, task, 0, "ktaskkill");
212
    t2 = thread_create(ktaskkill, t1, task, 0, "ktaskkill");
217
    ASSERT(t2);
213
    ASSERT(t2);
218
    thread_ready(t2);
214
    thread_ready(t2);
219
 
215
 
220
    thread_ready(t1);
216
    thread_ready(t1);
221
 
217
 
222
    return task;
218
    return task;
223
}
219
}
224
 
220
 
225
/** Syscall for reading task ID from userspace.
221
/** Syscall for reading task ID from userspace.
226
 *
222
 *
227
 * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
223
 * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
228
 *
224
 *
229
 * @return 0 on success or an error code from @ref errno.h.
225
 * @return 0 on success or an error code from @ref errno.h.
230
 */
226
 */
231
__native sys_task_get_id(task_id_t *uspace_task_id)
227
__native sys_task_get_id(task_id_t *uspace_task_id)
232
{
228
{
233
    /*
229
    /*
234
     * No need to acquire lock on TASK because taskid
230
     * No need to acquire lock on TASK because taskid
235
     * remains constant for the lifespan of the task.
231
     * remains constant for the lifespan of the task.
236
     */
232
     */
237
    return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
233
    return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
238
}
234
}
239
 
235
 
240
/** Find task structure corresponding to task ID.
236
/** Find task structure corresponding to task ID.
241
 *
237
 *
242
 * The tasks_lock must be already held by the caller of this function
238
 * The tasks_lock must be already held by the caller of this function
243
 * and interrupts must be disabled.
239
 * and interrupts must be disabled.
244
 *
240
 *
245
 * @param id Task ID.
241
 * @param id Task ID.
246
 *
242
 *
247
 * @return Task structure address or NULL if there is no such task ID.
243
 * @return Task structure address or NULL if there is no such task ID.
248
 */
244
 */
249
task_t *task_find_by_id(task_id_t id)
245
task_t *task_find_by_id(task_id_t id)
250
{
246
{
251
    btree_node_t *leaf;
247
    btree_node_t *leaf;
252
   
248
   
253
    return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
249
    return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
254
}
250
}
255
 
251
 
256
/** Kill task.
252
/** Kill task.
257
 *
253
 *
258
 * @param id ID of the task to be killed.
254
 * @param id ID of the task to be killed.
259
 *
255
 *
260
 * @return 0 on success or an error code from errno.h
256
 * @return 0 on success or an error code from errno.h
261
 */
257
 */
262
int task_kill(task_id_t id)
258
int task_kill(task_id_t id)
263
{
259
{
264
    ipl_t ipl;
260
    ipl_t ipl;
265
    task_t *ta;
261
    task_t *ta;
266
    thread_t *t;
262
    thread_t *t;
267
    link_t *cur;
263
    link_t *cur;
268
   
264
   
269
    ipl = interrupts_disable();
265
    ipl = interrupts_disable();
270
    spinlock_lock(&tasks_lock);
266
    spinlock_lock(&tasks_lock);
271
 
267
 
272
    if (!(ta = task_find_by_id(id))) {
268
    if (!(ta = task_find_by_id(id))) {
273
        spinlock_unlock(&tasks_lock);
269
        spinlock_unlock(&tasks_lock);
274
        interrupts_restore(ipl);
270
        interrupts_restore(ipl);
275
        return ENOENT;
271
        return ENOENT;
276
    }
272
    }
277
   
273
 
278
    spinlock_lock(&ta->lock);
274
    spinlock_lock(&ta->lock);
279
    ta->refcount++;
275
    ta->refcount++;
280
    spinlock_unlock(&ta->lock);
276
    spinlock_unlock(&ta->lock);
281
 
277
 
-
 
278
    btree_remove(&tasks_btree, ta->taskid, NULL);
282
    spinlock_unlock(&tasks_lock);
279
    spinlock_unlock(&tasks_lock);
283
   
280
   
284
    t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
281
    t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
285
   
282
   
286
    spinlock_lock(&ta->lock);
283
    spinlock_lock(&ta->lock);
287
    ta->accept_new_threads = false;
284
    ta->accept_new_threads = false;
288
    ta->refcount--;
285
    ta->refcount--;
289
 
286
 
290
    /*
287
    /*
291
     * Interrupt all threads except this one.
288
     * Interrupt all threads except this one.
292
     */
289
     */
293
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
290
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
294
        thread_t *thr;
291
        thread_t *thr;
295
        bool  sleeping = false;
292
        bool  sleeping = false;
296
       
293
       
297
        thr = list_get_instance(cur, thread_t, th_link);
294
        thr = list_get_instance(cur, thread_t, th_link);
298
        if (thr == t)
295
        if (thr == t)
299
            continue;
296
            continue;
300
           
297
           
301
        spinlock_lock(&thr->lock);
298
        spinlock_lock(&thr->lock);
302
        thr->interrupted = true;
299
        thr->interrupted = true;
303
        if (thr->state == Sleeping)
300
        if (thr->state == Sleeping)
304
            sleeping = true;
301
            sleeping = true;
305
        spinlock_unlock(&thr->lock);
302
        spinlock_unlock(&thr->lock);
306
       
303
       
307
        if (sleeping)
304
        if (sleeping)
308
            waitq_interrupt_sleep(thr);
305
            waitq_interrupt_sleep(thr);
309
    }
306
    }
310
   
307
   
311
    spinlock_unlock(&ta->lock);
308
    spinlock_unlock(&ta->lock);
312
    interrupts_restore(ipl);
309
    interrupts_restore(ipl);
313
   
310
   
314
    if (t)
311
    if (t)
315
        thread_ready(t);
312
        thread_ready(t);
316
 
313
 
317
    return 0;
314
    return 0;
318
}
315
}
319
 
316
 
320
/** Print task list */
317
/** Print task list */
321
void task_print_list(void)
318
void task_print_list(void)
322
{
319
{
323
    link_t *cur;
320
    link_t *cur;
324
    ipl_t ipl;
321
    ipl_t ipl;
325
   
322
   
326
    /* Messing with thread structures, avoid deadlock */
323
    /* Messing with thread structures, avoid deadlock */
327
    ipl = interrupts_disable();
324
    ipl = interrupts_disable();
328
    spinlock_lock(&tasks_lock);
325
    spinlock_lock(&tasks_lock);
329
 
326
 
330
    for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
327
    for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
331
        btree_node_t *node;
328
        btree_node_t *node;
332
        int i;
329
        int i;
333
       
330
       
334
        node = list_get_instance(cur, btree_node_t, leaf_link);
331
        node = list_get_instance(cur, btree_node_t, leaf_link);
335
        for (i = 0; i < node->keys; i++) {
332
        for (i = 0; i < node->keys; i++) {
336
            task_t *t;
333
            task_t *t;
337
            int j;
334
            int j;
338
 
335
 
339
            t = (task_t *) node->value[i];
336
            t = (task_t *) node->value[i];
340
       
337
       
341
            spinlock_lock(&t->lock);
338
            spinlock_lock(&t->lock);
342
            printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
339
            printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
343
                t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
340
                t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
344
            for (j=0; j < IPC_MAX_PHONES; j++) {
341
            for (j=0; j < IPC_MAX_PHONES; j++) {
345
                if (t->phones[j].callee)
342
                if (t->phones[j].callee)
346
                    printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
343
                    printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
347
            }
344
            }
348
            printf("\n");
345
            printf("\n");
349
            spinlock_unlock(&t->lock);
346
            spinlock_unlock(&t->lock);
350
        }
347
        }
351
    }
348
    }
352
 
349
 
353
    spinlock_unlock(&tasks_lock);
350
    spinlock_unlock(&tasks_lock);
354
    interrupts_restore(ipl);
351
    interrupts_restore(ipl);
355
}
352
}
356
 
353
 
357
/** Kernel thread used to cleanup the task after it is killed. */
354
/** Kernel thread used to cleanup the task after it is killed. */
358
void ktaskclnp(void *arg)
355
void ktaskclnp(void *arg)
359
{
356
{
360
    ipl_t ipl;
357
    ipl_t ipl;
361
    thread_t *t = NULL, *main_thread;
358
    thread_t *t = NULL, *main_thread;
362
    link_t *cur;
359
    link_t *cur;
363
 
360
 
364
    thread_detach(THREAD);
361
    thread_detach(THREAD);
365
 
362
 
366
loop:
363
loop:
367
    ipl = interrupts_disable();
364
    ipl = interrupts_disable();
368
    spinlock_lock(&TASK->lock);
365
    spinlock_lock(&TASK->lock);
369
   
366
   
370
    main_thread = TASK->main_thread;
367
    main_thread = TASK->main_thread;
371
   
368
   
372
    /*
369
    /*
373
     * Find a thread to join.
370
     * Find a thread to join.
374
     */
371
     */
375
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
372
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
376
        t = list_get_instance(cur, thread_t, th_link);
373
        t = list_get_instance(cur, thread_t, th_link);
377
        if (t == THREAD)
374
        if (t == THREAD)
378
            continue;
375
            continue;
379
        else if (t == main_thread)
376
        else if (t == main_thread)
380
            continue;
377
            continue;
381
        else
378
        else
382
            break;
379
            break;
383
    }
380
    }
384
   
381
   
385
    spinlock_unlock(&TASK->lock);
382
    spinlock_unlock(&TASK->lock);
386
    interrupts_restore(ipl);
383
    interrupts_restore(ipl);
387
   
384
   
388
    if (t != THREAD) {
385
    if (t != THREAD) {
389
        ASSERT(t != main_thread);   /* uninit is joined and detached in ktaskkill */
386
        ASSERT(t != main_thread);   /* uninit is joined and detached in ktaskkill */
390
        thread_join(t);
387
        thread_join(t);
391
        thread_detach(t);
388
        thread_detach(t);
392
        goto loop;  /* go for another thread */
389
        goto loop;  /* go for another thread */
393
    }
390
    }
394
   
391
   
395
    /*
392
    /*
396
     * Now there are no other threads in this task
393
     * Now there are no other threads in this task
397
     * and no new threads can be created.
394
     * and no new threads can be created.
398
     */
395
     */
399
   
396
   
400
    ipc_cleanup();
397
    ipc_cleanup();
401
    futex_cleanup();
398
    futex_cleanup();
402
}
399
}
403
 
400
 
404
/** Kernel task used to kill a userspace task when its main thread exits.
401
/** Kernel task used to kill a userspace task when its main thread exits.
405
 *
402
 *
406
 * This thread waits until the main userspace thread (i.e. uninit) exits.
403
 * This thread waits until the main userspace thread (i.e. uninit) exits.
407
 * When this happens, the task is killed.
404
 * When this happens, the task is killed.
408
 *
405
 *
409
 * @param arg Pointer to the thread structure of the task's main thread.
406
 * @param arg Pointer to the thread structure of the task's main thread.
410
 */
407
 */
411
void ktaskkill(void *arg)
408
void ktaskkill(void *arg)
412
{
409
{
413
    thread_t *t = (thread_t *) arg;
410
    thread_t *t = (thread_t *) arg;
414
   
411
   
415
    /*
412
    /*
416
     * Userspace threads cannot detach themselves,
413
     * Userspace threads cannot detach themselves,
417
     * therefore the thread pointer is guaranteed to be valid.
414
     * therefore the thread pointer is guaranteed to be valid.
418
     */
415
     */
419
    thread_join(t); /* sleep uninterruptibly here! */
416
    thread_join(t); /* sleep uninterruptibly here! */
420
    thread_detach(t);
417
    thread_detach(t);
421
    task_kill(TASK->taskid);
418
    task_kill(TASK->taskid);
422
}
419
}
423
 
420