Subversion Repositories HelenOS

Rev

Rev 2035 | Rev 2040 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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