Subversion Repositories HelenOS

Rev

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

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