Subversion Repositories HelenOS-historic

Rev

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

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