Subversion Repositories HelenOS

Rev

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

Rev 2048 Rev 2050
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 <func.h>
55
#include <syscall/copy.h>
56
#include <syscall/copy.h>
56
#include <console/klog.h>
57
#include <console/klog.h>
57
 
58
 
58
#ifndef LOADED_PROG_STACK_PAGES_NO
59
#ifndef LOADED_PROG_STACK_PAGES_NO
59
#define LOADED_PROG_STACK_PAGES_NO 1
60
#define LOADED_PROG_STACK_PAGES_NO 1
60
#endif
61
#endif
61
 
62
 
62
/** Spinlock protecting the tasks_btree B+tree. */
63
/** Spinlock protecting the tasks_btree B+tree. */
63
SPINLOCK_INITIALIZE(tasks_lock);
64
SPINLOCK_INITIALIZE(tasks_lock);
64
 
65
 
65
/** B+tree of active tasks.
66
/** B+tree of active tasks.
66
 *
67
 *
67
 * The task is guaranteed to exist after it was found in the tasks_btree as long as:
68
 * The task is guaranteed to exist after it was found in the tasks_btree as long as:
68
 * @li the tasks_lock is held,
69
 * @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
70
 * @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
71
 * @li the task's refcount is greater than 0
71
 *
72
 *
72
 */
73
 */
73
btree_t tasks_btree;
74
btree_t tasks_btree;
74
 
75
 
75
static task_id_t task_counter = 0;
76
static task_id_t task_counter = 0;
76
 
77
 
77
static void ktaskclnp(void *arg);
78
static void ktaskclnp(void *arg);
78
static void ktaskgc(void *arg);
79
static void ktaskgc(void *arg);
79
 
80
 
80
/** Initialize tasks
81
/** Initialize tasks
81
 *
82
 *
82
 * Initialize kernel tasks support.
83
 * Initialize kernel tasks support.
83
 *
84
 *
84
 */
85
 */
85
void task_init(void)
86
void task_init(void)
86
{
87
{
87
    TASK = NULL;
88
    TASK = NULL;
88
    btree_create(&tasks_btree);
89
    btree_create(&tasks_btree);
89
}
90
}
90
 
91
 
91
 
92
 
92
/** Create new task
93
/** Create new task
93
 *
94
 *
94
 * Create new task with no threads.
95
 * Create new task with no threads.
95
 *
96
 *
96
 * @param as Task's address space.
97
 * @param as Task's address space.
97
 * @param name Symbolic name.
98
 * @param name Symbolic name.
98
 *
99
 *
99
 * @return New task's structure
100
 * @return New task's structure
100
 *
101
 *
101
 */
102
 */
102
task_t *task_create(as_t *as, char *name)
103
task_t *task_create(as_t *as, char *name)
103
{
104
{
104
    ipl_t ipl;
105
    ipl_t ipl;
105
    task_t *ta;
106
    task_t *ta;
106
    int i;
107
    int i;
107
   
108
   
108
    ta = (task_t *) malloc(sizeof(task_t), 0);
109
    ta = (task_t *) malloc(sizeof(task_t), 0);
109
 
110
 
110
    task_create_arch(ta);
111
    task_create_arch(ta);
111
 
112
 
112
    spinlock_initialize(&ta->lock, "task_ta_lock");
113
    spinlock_initialize(&ta->lock, "task_ta_lock");
113
    list_initialize(&ta->th_head);
114
    list_initialize(&ta->th_head);
114
    ta->as = as;
115
    ta->as = as;
115
    ta->name = name;
116
    ta->name = name;
116
    ta->main_thread = NULL;
117
    ta->main_thread = NULL;
117
    ta->refcount = 0;
118
    ta->refcount = 0;
118
    ta->context = CONTEXT;
119
    ta->context = CONTEXT;
119
 
120
 
120
    ta->capabilities = 0;
121
    ta->capabilities = 0;
121
    ta->accept_new_threads = true;
122
    ta->accept_new_threads = true;
122
    ta->cycles = 0;
123
    ta->cycles = 0;
123
   
124
   
124
    ipc_answerbox_init(&ta->answerbox);
125
    ipc_answerbox_init(&ta->answerbox);
125
    for (i = 0; i < IPC_MAX_PHONES; i++)
126
    for (i = 0; i < IPC_MAX_PHONES; i++)
126
        ipc_phone_init(&ta->phones[i]);
127
        ipc_phone_init(&ta->phones[i]);
127
    if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, ta->context)))
128
    if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, ta->context)))
128
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
129
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
129
    atomic_set(&ta->active_calls, 0);
130
    atomic_set(&ta->active_calls, 0);
130
 
131
 
131
    mutex_initialize(&ta->futexes_lock);
132
    mutex_initialize(&ta->futexes_lock);
132
    btree_create(&ta->futexes);
133
    btree_create(&ta->futexes);
133
   
134
   
134
    ipl = interrupts_disable();
135
    ipl = interrupts_disable();
135
 
136
 
136
    /*
137
    /*
137
     * Increment address space reference count.
138
     * Increment address space reference count.
138
     * TODO: Reconsider the locking scheme.
139
     * TODO: Reconsider the locking scheme.
139
     */
140
     */
140
    mutex_lock(&as->lock);
141
    mutex_lock(&as->lock);
141
    as->refcount++;
142
    as->refcount++;
142
    mutex_unlock(&as->lock);
143
    mutex_unlock(&as->lock);
143
 
144
 
144
    spinlock_lock(&tasks_lock);
145
    spinlock_lock(&tasks_lock);
145
 
146
 
146
    ta->taskid = ++task_counter;
147
    ta->taskid = ++task_counter;
147
    btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
148
    btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
148
 
149
 
149
    spinlock_unlock(&tasks_lock);
150
    spinlock_unlock(&tasks_lock);
150
    interrupts_restore(ipl);
151
    interrupts_restore(ipl);
151
 
152
 
152
    return ta;
153
    return ta;
153
}
154
}
154
 
155
 
155
/** Destroy task.
156
/** Destroy task.
156
 *
157
 *
157
 * @param t Task to be destroyed.
158
 * @param t Task to be destroyed.
158
 */
159
 */
159
void task_destroy(task_t *t)
160
void task_destroy(task_t *t)
160
{
161
{
161
    task_destroy_arch(t);
162
    task_destroy_arch(t);
162
    btree_destroy(&t->futexes);
163
    btree_destroy(&t->futexes);
163
 
164
 
164
    mutex_lock_active(&t->as->lock);
165
    mutex_lock_active(&t->as->lock);
165
    if (--t->as->refcount == 0) {
166
    if (--t->as->refcount == 0) {
166
        mutex_unlock(&t->as->lock);
167
        mutex_unlock(&t->as->lock);
167
        as_destroy(t->as);
168
        as_destroy(t->as);
168
        /*
169
        /*
169
         * t->as is destroyed.
170
         * t->as is destroyed.
170
         */
171
         */
171
    } else
172
    } else
172
        mutex_unlock(&t->as->lock);
173
        mutex_unlock(&t->as->lock);
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", false);
224
    t1 = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, "uinit", false);
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", true);
230
    t2 = thread_create(ktaskgc, t1, task, 0, "ktaskgc", true);
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
 
269
/** Get accounting data of given task.
270
/** Get accounting data of given task.
270
 *
271
 *
271
 * Note that task lock of 't' must be already held and
272
 * Note that task lock of 't' must be already held and
272
 * interrupts must be already disabled.
273
 * interrupts must be already disabled.
273
 *
274
 *
274
 * @param t Pointer to thread.
275
 * @param t Pointer to thread.
275
 *
276
 *
276
 */
277
 */
277
uint64_t task_get_accounting(task_t *t)
278
uint64_t task_get_accounting(task_t *t)
278
{
279
{
279
    /* Accumulated value of task */
280
    /* Accumulated value of task */
280
    uint64_t ret = t->cycles;
281
    uint64_t ret = t->cycles;
281
   
282
   
282
    /* Current values of threads */
283
    /* Current values of threads */
283
    link_t *cur;
284
    link_t *cur;
284
    for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
285
    for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
285
        thread_t *thr = list_get_instance(cur, thread_t, th_link);
286
        thread_t *thr = list_get_instance(cur, thread_t, th_link);
286
       
287
       
287
        spinlock_lock(&thr->lock);
288
        spinlock_lock(&thr->lock);
288
        /* Process only counted threads */
289
        /* Process only counted threads */
289
        if (!thr->uncounted) {
290
        if (!thr->uncounted) {
290
            if (thr == THREAD) /* Update accounting of current thread */
291
            if (thr == THREAD) /* Update accounting of current thread */
291
                thread_update_accounting();
292
                thread_update_accounting();
292
            ret += thr->cycles;
293
            ret += thr->cycles;
293
        }
294
        }
294
        spinlock_unlock(&thr->lock);
295
        spinlock_unlock(&thr->lock);
295
    }
296
    }
296
   
297
   
297
    return ret;
298
    return ret;
298
}
299
}
299
 
300
 
300
/** Kill task.
301
/** Kill task.
301
 *
302
 *
302
 * @param id ID of the task to be killed.
303
 * @param id ID of the task to be killed.
303
 *
304
 *
304
 * @return 0 on success or an error code from errno.h
305
 * @return 0 on success or an error code from errno.h
305
 */
306
 */
306
int task_kill(task_id_t id)
307
int task_kill(task_id_t id)
307
{
308
{
308
    ipl_t ipl;
309
    ipl_t ipl;
309
    task_t *ta;
310
    task_t *ta;
310
    thread_t *t;
311
    thread_t *t;
311
    link_t *cur;
312
    link_t *cur;
312
 
313
 
313
    if (id == 1)
314
    if (id == 1)
314
        return EPERM;
315
        return EPERM;
315
   
316
   
316
    ipl = interrupts_disable();
317
    ipl = interrupts_disable();
317
    spinlock_lock(&tasks_lock);
318
    spinlock_lock(&tasks_lock);
318
 
319
 
319
    if (!(ta = task_find_by_id(id))) {
320
    if (!(ta = task_find_by_id(id))) {
320
        spinlock_unlock(&tasks_lock);
321
        spinlock_unlock(&tasks_lock);
321
        interrupts_restore(ipl);
322
        interrupts_restore(ipl);
322
        return ENOENT;
323
        return ENOENT;
323
    }
324
    }
324
 
325
 
325
    spinlock_lock(&ta->lock);
326
    spinlock_lock(&ta->lock);
326
    ta->refcount++;
327
    ta->refcount++;
327
    spinlock_unlock(&ta->lock);
328
    spinlock_unlock(&ta->lock);
328
 
329
 
329
    btree_remove(&tasks_btree, ta->taskid, NULL);
330
    btree_remove(&tasks_btree, ta->taskid, NULL);
330
    spinlock_unlock(&tasks_lock);
331
    spinlock_unlock(&tasks_lock);
331
   
332
   
332
    t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp", true);
333
    t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp", true);
333
   
334
   
334
    spinlock_lock(&ta->lock);
335
    spinlock_lock(&ta->lock);
335
    ta->accept_new_threads = false;
336
    ta->accept_new_threads = false;
336
    ta->refcount--;
337
    ta->refcount--;
337
 
338
 
338
    /*
339
    /*
339
     * Interrupt all threads except ktaskclnp.
340
     * Interrupt all threads except ktaskclnp.
340
     */
341
     */
341
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
342
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
342
        thread_t *thr;
343
        thread_t *thr;
343
        bool  sleeping = false;
344
        bool  sleeping = false;
344
       
345
       
345
        thr = list_get_instance(cur, thread_t, th_link);
346
        thr = list_get_instance(cur, thread_t, th_link);
346
        if (thr == t)
347
        if (thr == t)
347
            continue;
348
            continue;
348
           
349
           
349
        spinlock_lock(&thr->lock);
350
        spinlock_lock(&thr->lock);
350
        thr->interrupted = true;
351
        thr->interrupted = true;
351
        if (thr->state == Sleeping)
352
        if (thr->state == Sleeping)
352
            sleeping = true;
353
            sleeping = true;
353
        spinlock_unlock(&thr->lock);
354
        spinlock_unlock(&thr->lock);
354
       
355
       
355
        if (sleeping)
356
        if (sleeping)
356
            waitq_interrupt_sleep(thr);
357
            waitq_interrupt_sleep(thr);
357
    }
358
    }
358
   
359
   
359
    spinlock_unlock(&ta->lock);
360
    spinlock_unlock(&ta->lock);
360
    interrupts_restore(ipl);
361
    interrupts_restore(ipl);
361
   
362
   
362
    if (t)
363
    if (t)
363
        thread_ready(t);
364
        thread_ready(t);
364
 
365
 
365
    return 0;
366
    return 0;
366
}
367
}
367
 
368
 
368
/** Print task list */
369
/** Print task list */
369
void task_print_list(void)
370
void task_print_list(void)
370
{
371
{
371
    link_t *cur;
372
    link_t *cur;
372
    ipl_t ipl;
373
    ipl_t ipl;
373
   
374
   
374
    /* Messing with thread structures, avoid deadlock */
375
    /* Messing with thread structures, avoid deadlock */
375
    ipl = interrupts_disable();
376
    ipl = interrupts_disable();
376
    spinlock_lock(&tasks_lock);
377
    spinlock_lock(&tasks_lock);
377
   
378
   
378
    printf("taskid name       ctx address    as         cycles     threads calls  callee\n");
379
    printf("taskid name       ctx address    as         cycles     threads calls  callee\n");
379
    printf("------ ---------- --- ---------- ---------- ---------- ------- ------ ------>\n");
380
    printf("------ ---------- --- ---------- ---------- ---------- ------- ------ ------>\n");
380
 
381
 
381
    for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
382
    for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
382
        btree_node_t *node;
383
        btree_node_t *node;
383
        int i;
384
        int i;
384
       
385
       
385
        node = list_get_instance(cur, btree_node_t, leaf_link);
386
        node = list_get_instance(cur, btree_node_t, leaf_link);
386
        for (i = 0; i < node->keys; i++) {
387
        for (i = 0; i < node->keys; i++) {
387
            task_t *t;
388
            task_t *t;
388
            int j;
389
            int j;
389
 
390
 
390
            t = (task_t *) node->value[i];
391
            t = (task_t *) node->value[i];
391
       
392
       
392
            spinlock_lock(&t->lock);
393
            spinlock_lock(&t->lock);
393
           
394
           
394
            uint64_t cycles = task_get_accounting(t);
395
            uint64_t cycles;
395
            char suffix;
396
            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) {
397
            order(task_get_accounting(t), &cycles, &suffix);
404
                cycles = cycles / 1000000LL;
-
 
405
                suffix = 'M';
-
 
406
            } else
-
 
407
                suffix = ' ';
-
 
408
           
398
           
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));
399
            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));
410
            for (j = 0; j < IPC_MAX_PHONES; j++) {
400
            for (j = 0; j < IPC_MAX_PHONES; j++) {
411
                if (t->phones[j].callee)
401
                if (t->phones[j].callee)
412
                    printf(" %zd:%#zx", j, t->phones[j].callee);
402
                    printf(" %zd:%#zx", j, t->phones[j].callee);
413
            }
403
            }
414
            printf("\n");
404
            printf("\n");
415
           
405
           
416
            spinlock_unlock(&t->lock);
406
            spinlock_unlock(&t->lock);
417
        }
407
        }
418
    }
408
    }
419
 
409
 
420
    spinlock_unlock(&tasks_lock);
410
    spinlock_unlock(&tasks_lock);
421
    interrupts_restore(ipl);
411
    interrupts_restore(ipl);
422
}
412
}
423
 
413
 
424
/** Kernel thread used to cleanup the task after it is killed. */
414
/** Kernel thread used to cleanup the task after it is killed. */
425
void ktaskclnp(void *arg)
415
void ktaskclnp(void *arg)
426
{
416
{
427
    ipl_t ipl;
417
    ipl_t ipl;
428
    thread_t *t = NULL, *main_thread;
418
    thread_t *t = NULL, *main_thread;
429
    link_t *cur;
419
    link_t *cur;
430
    bool again;
420
    bool again;
431
 
421
 
432
    thread_detach(THREAD);
422
    thread_detach(THREAD);
433
 
423
 
434
loop:
424
loop:
435
    ipl = interrupts_disable();
425
    ipl = interrupts_disable();
436
    spinlock_lock(&TASK->lock);
426
    spinlock_lock(&TASK->lock);
437
   
427
   
438
    main_thread = TASK->main_thread;
428
    main_thread = TASK->main_thread;
439
   
429
   
440
    /*
430
    /*
441
     * Find a thread to join.
431
     * Find a thread to join.
442
     */
432
     */
443
    again = false;
433
    again = false;
444
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
434
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
445
        t = list_get_instance(cur, thread_t, th_link);
435
        t = list_get_instance(cur, thread_t, th_link);
446
 
436
 
447
        spinlock_lock(&t->lock);
437
        spinlock_lock(&t->lock);
448
        if (t == THREAD) {
438
        if (t == THREAD) {
449
            spinlock_unlock(&t->lock);
439
            spinlock_unlock(&t->lock);
450
            continue;
440
            continue;
451
        } else if (t == main_thread) {
441
        } else if (t == main_thread) {
452
            spinlock_unlock(&t->lock);
442
            spinlock_unlock(&t->lock);
453
            continue;
443
            continue;
454
        } else if (t->join_type != None) {
444
        } else if (t->join_type != None) {
455
            spinlock_unlock(&t->lock);
445
            spinlock_unlock(&t->lock);
456
            again = true;
446
            again = true;
457
            continue;
447
            continue;
458
        } else {
448
        } else {
459
            t->join_type = TaskClnp;
449
            t->join_type = TaskClnp;
460
            spinlock_unlock(&t->lock);
450
            spinlock_unlock(&t->lock);
461
            again = false;
451
            again = false;
462
            break;
452
            break;
463
        }
453
        }
464
    }
454
    }
465
   
455
   
466
    spinlock_unlock(&TASK->lock);
456
    spinlock_unlock(&TASK->lock);
467
    interrupts_restore(ipl);
457
    interrupts_restore(ipl);
468
   
458
   
469
    if (again) {
459
    if (again) {
470
        /*
460
        /*
471
         * Other cleanup (e.g. ktaskgc) is in progress.
461
         * Other cleanup (e.g. ktaskgc) is in progress.
472
         */
462
         */
473
        scheduler();
463
        scheduler();
474
        goto loop;
464
        goto loop;
475
    }
465
    }
476
   
466
   
477
    if (t != THREAD) {
467
    if (t != THREAD) {
478
        ASSERT(t != main_thread);   /* uninit is joined and detached in ktaskgc */
468
        ASSERT(t != main_thread);   /* uninit is joined and detached in ktaskgc */
479
        thread_join(t);
469
        thread_join(t);
480
        thread_detach(t);
470
        thread_detach(t);
481
        goto loop;  /* go for another thread */
471
        goto loop;  /* go for another thread */
482
    }
472
    }
483
   
473
   
484
    /*
474
    /*
485
     * Now there are no other threads in this task
475
     * Now there are no other threads in this task
486
     * and no new threads can be created.
476
     * and no new threads can be created.
487
     */
477
     */
488
 
478
 
489
    ipc_cleanup();
479
    ipc_cleanup();
490
    futex_cleanup();
480
    futex_cleanup();
491
    klog_printf("Cleanup of task %lld completed.", TASK->taskid);
481
    klog_printf("Cleanup of task %lld completed.", TASK->taskid);
492
}
482
}
493
 
483
 
494
/** Kernel thread used to kill the userspace task when its main thread exits.
484
/** Kernel thread used to kill the userspace task when its main thread exits.
495
 *
485
 *
496
 * This thread waits until the main userspace thread (i.e. uninit) exits.
486
 * This thread waits until the main userspace thread (i.e. uninit) exits.
497
 * When this happens, the task is killed. In the meantime, exited threads
487
 * When this happens, the task is killed. In the meantime, exited threads
498
 * are garbage collected.
488
 * are garbage collected.
499
 *
489
 *
500
 * @param arg Pointer to the thread structure of the task's main thread.
490
 * @param arg Pointer to the thread structure of the task's main thread.
501
 */
491
 */
502
void ktaskgc(void *arg)
492
void ktaskgc(void *arg)
503
{
493
{
504
    thread_t *t = (thread_t *) arg;
494
    thread_t *t = (thread_t *) arg;
505
loop:  
495
loop:  
506
    /*
496
    /*
507
     * Userspace threads cannot detach themselves,
497
     * Userspace threads cannot detach themselves,
508
     * therefore the thread pointer is guaranteed to be valid.
498
     * therefore the thread pointer is guaranteed to be valid.
509
     */
499
     */
510
    if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) == ESYNCH_TIMEOUT) {  /* sleep uninterruptibly here! */
500
    if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) == ESYNCH_TIMEOUT) {  /* sleep uninterruptibly here! */
511
        ipl_t ipl;
501
        ipl_t ipl;
512
        link_t *cur;
502
        link_t *cur;
513
        thread_t *thr = NULL;
503
        thread_t *thr = NULL;
514
   
504
   
515
        /*
505
        /*
516
         * The join timed out. Try to do some garbage collection of Undead threads.
506
         * The join timed out. Try to do some garbage collection of Undead threads.
517
         */
507
         */
518
more_gc:       
508
more_gc:       
519
        ipl = interrupts_disable();
509
        ipl = interrupts_disable();
520
        spinlock_lock(&TASK->lock);
510
        spinlock_lock(&TASK->lock);
521
       
511
       
522
        for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
512
        for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
523
            thr = list_get_instance(cur, thread_t, th_link);
513
            thr = list_get_instance(cur, thread_t, th_link);
524
            spinlock_lock(&thr->lock);
514
            spinlock_lock(&thr->lock);
525
            if (thr != t && thr->state == Undead && thr->join_type == None) {
515
            if (thr != t && thr->state == Undead && thr->join_type == None) {
526
                thr->join_type = TaskGC;
516
                thr->join_type = TaskGC;
527
                spinlock_unlock(&thr->lock);
517
                spinlock_unlock(&thr->lock);
528
                break;
518
                break;
529
            }
519
            }
530
            spinlock_unlock(&thr->lock);
520
            spinlock_unlock(&thr->lock);
531
            thr = NULL;
521
            thr = NULL;
532
        }
522
        }
533
        spinlock_unlock(&TASK->lock);
523
        spinlock_unlock(&TASK->lock);
534
        interrupts_restore(ipl);
524
        interrupts_restore(ipl);
535
       
525
       
536
        if (thr) {
526
        if (thr) {
537
            thread_join(thr);
527
            thread_join(thr);
538
            thread_detach(thr);
528
            thread_detach(thr);
539
            scheduler();
529
            scheduler();
540
            goto more_gc;
530
            goto more_gc;
541
        }
531
        }
542
           
532
           
543
        goto loop;
533
        goto loop;
544
    }
534
    }
545
    thread_detach(t);
535
    thread_detach(t);
546
    task_kill(TASK->taskid);
536
    task_kill(TASK->taskid);
547
}
537
}
548
 
538
 
549
/** @}
539
/** @}
550
 */
540
 */
551
 
541