Subversion Repositories HelenOS

Rev

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

Rev 3424 Rev 3425
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>
-
 
39
#include <proc/thread.h>
38
#include <proc/thread.h>
40
#include <proc/task.h>
39
#include <proc/task.h>
41
#include <proc/uarg.h>
-
 
42
#include <mm/as.h>
40
#include <mm/as.h>
43
#include <mm/slab.h>
41
#include <mm/slab.h>
44
#include <atomic.h>
42
#include <atomic.h>
45
#include <synch/spinlock.h>
43
#include <synch/spinlock.h>
46
#include <synch/waitq.h>
44
#include <synch/waitq.h>
47
#include <arch.h>
45
#include <arch.h>
48
#include <arch/barrier.h>
46
#include <arch/barrier.h>
49
#include <panic.h>
-
 
50
#include <adt/avl.h>
47
#include <adt/avl.h>
51
#include <adt/btree.h>
48
#include <adt/btree.h>
52
#include <adt/list.h>
49
#include <adt/list.h>
53
#include <ipc/ipc.h>
50
#include <ipc/ipc.h>
54
#include <security/cap.h>
51
#include <ipc/ipcrsc.h>
55
#include <memstr.h>
-
 
56
#include <print.h>
52
#include <print.h>
57
#include <lib/elf.h>
-
 
58
#include <errno.h>
53
#include <errno.h>
59
#include <func.h>
54
#include <func.h>
60
#include <syscall/copy.h>
55
#include <syscall/copy.h>
61
 
56
 
62
#ifndef LOADED_PROG_STACK_PAGES_NO
-
 
63
#define LOADED_PROG_STACK_PAGES_NO 1
-
 
64
#endif
-
 
65
 
-
 
66
/** Spinlock protecting the tasks_tree AVL tree. */
57
/** Spinlock protecting the tasks_tree AVL tree. */
67
SPINLOCK_INITIALIZE(tasks_lock);
58
SPINLOCK_INITIALIZE(tasks_lock);
68
 
59
 
69
/** AVL tree of active tasks.
60
/** AVL tree of active tasks.
70
 *
61
 *
71
 * The task is guaranteed to exist after it was found in the tasks_tree as
62
 * The task is guaranteed to exist after it was found in the tasks_tree as
72
 * long as:
63
 * long as:
73
 * @li the tasks_lock is held,
64
 * @li the tasks_lock is held,
74
 * @li the task's lock is held when task's lock is acquired before releasing
65
 * @li the task's lock is held when task's lock is acquired before releasing
75
 *     tasks_lock or
66
 *     tasks_lock or
76
 * @li the task's refcount is greater than 0
67
 * @li the task's refcount is greater than 0
77
 *
68
 *
78
 */
69
 */
79
avltree_t tasks_tree;
70
avltree_t tasks_tree;
80
 
71
 
81
static task_id_t task_counter = 0;
72
static task_id_t task_counter = 0;
82
 
73
 
83
/** Initialize tasks
-
 
84
 *
-
 
85
 * Initialize kernel tasks support.
74
/** Initialize kernel tasks support. */
86
 *
-
 
87
 */
-
 
88
void task_init(void)
75
void task_init(void)
89
{
76
{
90
    TASK = NULL;
77
    TASK = NULL;
91
    avltree_create(&tasks_tree);
78
    avltree_create(&tasks_tree);
92
}
79
}
93
 
80
 
94
/*
81
/*
95
 * The idea behind this walker is to remember a single task different from TASK.
82
 * The idea behind this walker is to remember a single task different from
-
 
83
 * TASK.
96
 */
84
 */
97
static bool task_done_walker(avltree_node_t *node, void *arg)
85
static bool task_done_walker(avltree_node_t *node, void *arg)
98
{
86
{
99
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
87
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
100
    task_t **tp = (task_t **) arg;
88
    task_t **tp = (task_t **) arg;
101
 
89
 
102
    if (t != TASK) {
90
    if (t != TASK) {
103
        *tp = t;
91
        *tp = t;
104
        return false;   /* stop walking */
92
        return false;   /* stop walking */
105
    }
93
    }
106
 
94
 
107
    return true;    /* continue the walk */
95
    return true;    /* continue the walk */
108
}
96
}
109
 
97
 
110
/** Kill all tasks except the current task.
98
/** Kill all tasks except the current task. */
111
 *
-
 
112
 */
-
 
113
void task_done(void)
99
void task_done(void)
114
{
100
{
115
    task_t *t;
101
    task_t *t;
116
    do { /* Repeat until there are any tasks except TASK */
102
    do { /* Repeat until there are any tasks except TASK */
117
       
103
       
118
        /* Messing with task structures, avoid deadlock */
104
        /* Messing with task structures, avoid deadlock */
119
        ipl_t ipl = interrupts_disable();
105
        ipl_t ipl = interrupts_disable();
120
        spinlock_lock(&tasks_lock);
106
        spinlock_lock(&tasks_lock);
121
       
107
       
122
        t = NULL;
108
        t = NULL;
123
        avltree_walk(&tasks_tree, task_done_walker, &t);
109
        avltree_walk(&tasks_tree, task_done_walker, &t);
124
       
110
       
125
        if (t != NULL) {
111
        if (t != NULL) {
126
            task_id_t id = t->taskid;
112
            task_id_t id = t->taskid;
127
           
113
           
128
            spinlock_unlock(&tasks_lock);
114
            spinlock_unlock(&tasks_lock);
129
            interrupts_restore(ipl);
115
            interrupts_restore(ipl);
130
           
116
           
131
#ifdef CONFIG_DEBUG
117
#ifdef CONFIG_DEBUG
132
            printf("Killing task %" PRIu64 "\n", id);
118
            printf("Killing task %" PRIu64 "\n", id);
133
#endif          
119
#endif          
134
            task_kill(id);
120
            task_kill(id);
135
            thread_usleep(10000);
121
            thread_usleep(10000);
136
        } else {
122
        } else {
137
            spinlock_unlock(&tasks_lock);
123
            spinlock_unlock(&tasks_lock);
138
            interrupts_restore(ipl);
124
            interrupts_restore(ipl);
139
        }
125
        }
140
       
126
       
141
    } while (t != NULL);
127
    } while (t != NULL);
142
}
128
}
143
 
129
 
144
/** Create new task
-
 
145
 *
-
 
146
 * Create new task with no threads.
130
/** Create new task with no threads.
147
 *
131
 *
148
 * @param as Task's address space.
132
 * @param as        Task's address space.
149
 * @param name Symbolic name.
133
 * @param name      Symbolic name.
150
 *
134
 *
151
 * @return New task's structure
135
 * @return      New task's structure.
152
 *
136
 *
153
 */
137
 */
154
task_t *task_create(as_t *as, char *name)
138
task_t *task_create(as_t *as, char *name)
155
{
139
{
156
    ipl_t ipl;
140
    ipl_t ipl;
157
    task_t *ta;
141
    task_t *ta;
158
    int i;
142
    int i;
159
   
143
   
160
    ta = (task_t *) malloc(sizeof(task_t), 0);
144
    ta = (task_t *) malloc(sizeof(task_t), 0);
161
 
145
 
162
    task_create_arch(ta);
146
    task_create_arch(ta);
163
 
147
 
164
    spinlock_initialize(&ta->lock, "task_ta_lock");
148
    spinlock_initialize(&ta->lock, "task_ta_lock");
165
    list_initialize(&ta->th_head);
149
    list_initialize(&ta->th_head);
166
    ta->as = as;
150
    ta->as = as;
167
    ta->name = name;
151
    ta->name = name;
168
    atomic_set(&ta->refcount, 0);
152
    atomic_set(&ta->refcount, 0);
169
    atomic_set(&ta->lifecount, 0);
153
    atomic_set(&ta->lifecount, 0);
170
    ta->context = CONTEXT;
154
    ta->context = CONTEXT;
171
 
155
 
172
    ta->capabilities = 0;
156
    ta->capabilities = 0;
173
    ta->cycles = 0;
157
    ta->cycles = 0;
174
 
158
 
175
    /* Init debugging stuff */
159
    /* Init debugging stuff */
176
    udebug_task_init(&ta->udebug);
160
    udebug_task_init(&ta->udebug);
177
 
161
 
178
    /* Init kbox stuff */
162
    /* Init kbox stuff */
179
    ipc_answerbox_init(&ta->kernel_box, ta);
163
    ipc_answerbox_init(&ta->kernel_box, ta);
180
    ta->kb_thread = NULL;
164
    ta->kb_thread = NULL;
181
    mutex_initialize(&ta->kb_cleanup_lock);
165
    mutex_initialize(&ta->kb_cleanup_lock);
182
    ta->kb_finished = false;
166
    ta->kb_finished = false;
183
 
167
 
184
    ipc_answerbox_init(&ta->answerbox, ta);
168
    ipc_answerbox_init(&ta->answerbox, ta);
185
    for (i = 0; i < IPC_MAX_PHONES; i++)
169
    for (i = 0; i < IPC_MAX_PHONES; i++)
186
        ipc_phone_init(&ta->phones[i]);
170
        ipc_phone_init(&ta->phones[i]);
187
    if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context,
171
    if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context,
188
        ta->context)))
172
        ta->context)))
189
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
173
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
190
    atomic_set(&ta->active_calls, 0);
174
    atomic_set(&ta->active_calls, 0);
191
 
175
 
192
    mutex_initialize(&ta->futexes_lock);
176
    mutex_initialize(&ta->futexes_lock, MUTEX_PASSIVE);
193
    btree_create(&ta->futexes);
177
    btree_create(&ta->futexes);
194
   
178
   
195
    ipl = interrupts_disable();
179
    ipl = interrupts_disable();
196
 
180
 
197
    /*
181
    /*
198
     * Increment address space reference count.
182
     * Increment address space reference count.
199
     */
183
     */
200
    atomic_inc(&as->refcount);
184
    atomic_inc(&as->refcount);
201
 
185
 
202
    spinlock_lock(&tasks_lock);
186
    spinlock_lock(&tasks_lock);
203
    ta->taskid = ++task_counter;
187
    ta->taskid = ++task_counter;
204
    avltree_node_initialize(&ta->tasks_tree_node);
188
    avltree_node_initialize(&ta->tasks_tree_node);
205
    ta->tasks_tree_node.key = ta->taskid;
189
    ta->tasks_tree_node.key = ta->taskid;
206
    avltree_insert(&tasks_tree, &ta->tasks_tree_node);
190
    avltree_insert(&tasks_tree, &ta->tasks_tree_node);
207
    spinlock_unlock(&tasks_lock);
191
    spinlock_unlock(&tasks_lock);
208
    interrupts_restore(ipl);
192
    interrupts_restore(ipl);
209
 
193
 
210
    return ta;
194
    return ta;
211
}
195
}
212
 
196
 
213
/** Destroy task.
197
/** Destroy task.
214
 *
198
 *
215
 * @param t Task to be destroyed.
199
 * @param t     Task to be destroyed.
216
 */
200
 */
217
void task_destroy(task_t *t)
201
void task_destroy(task_t *t)
218
{
202
{
219
    /*
203
    /*
220
     * Remove the task from the task B+tree.
204
     * Remove the task from the task B+tree.
221
     */
205
     */
222
    spinlock_lock(&tasks_lock);
206
    spinlock_lock(&tasks_lock);
223
    avltree_delete(&tasks_tree, &t->tasks_tree_node);
207
    avltree_delete(&tasks_tree, &t->tasks_tree_node);
224
    spinlock_unlock(&tasks_lock);
208
    spinlock_unlock(&tasks_lock);
225
 
209
 
226
    /*
210
    /*
227
     * Perform architecture specific task destruction.
211
     * Perform architecture specific task destruction.
228
     */
212
     */
229
    task_destroy_arch(t);
213
    task_destroy_arch(t);
230
 
214
 
231
    /*
215
    /*
232
     * Free up dynamically allocated state.
216
     * Free up dynamically allocated state.
233
     */
217
     */
234
    btree_destroy(&t->futexes);
218
    btree_destroy(&t->futexes);
235
 
219
 
236
    /*
220
    /*
237
     * Drop our reference to the address space.
221
     * Drop our reference to the address space.
238
     */
222
     */
239
    if (atomic_predec(&t->as->refcount) == 0)
223
    if (atomic_predec(&t->as->refcount) == 0)
240
        as_destroy(t->as);
224
        as_destroy(t->as);
241
   
225
   
242
    free(t);
226
    free(t);
243
    TASK = NULL;
227
    TASK = NULL;
244
}
228
}
245
 
229
 
246
/** Syscall for reading task ID from userspace.
230
/** Syscall for reading task ID from userspace.
247
 *
231
 *
248
 * @param uspace_task_id Userspace address of 8-byte buffer where to store
232
 * @param       uspace_task_id userspace address of 8-byte buffer
249
 * current task ID.
233
 *          where to store current task ID.
250
 *
234
 *
251
 * @return 0 on success or an error code from @ref errno.h.
235
 * @return      Zero on success or an error code from @ref errno.h.
252
 */
236
 */
253
unative_t sys_task_get_id(task_id_t *uspace_task_id)
237
unative_t sys_task_get_id(task_id_t *uspace_task_id)
254
{
238
{
255
    /*
239
    /*
256
     * No need to acquire lock on TASK because taskid
240
     * No need to acquire lock on TASK because taskid remains constant for
257
     * remains constant for the lifespan of the task.
241
     * the lifespan of the task.
258
     */
242
     */
259
    return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
243
    return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
260
        sizeof(TASK->taskid));
244
        sizeof(TASK->taskid));
261
}
245
}
262
 
246
 
263
unative_t sys_task_spawn(void *image, size_t size)
-
 
264
{
-
 
265
    void *kimage = malloc(size, 0);
-
 
266
    if (kimage == NULL)
-
 
267
        return ENOMEM;
-
 
268
   
-
 
269
    int rc = copy_from_uspace(kimage, image, size);
-
 
270
    if (rc != EOK)
-
 
271
        return rc;
-
 
272
 
-
 
273
    /*
-
 
274
     * Not very efficient and it would be better to call it on code only,
-
 
275
     * but this whole function is a temporary hack anyway and one day it
-
 
276
     * will go in favor of the userspace dynamic loader.
-
 
277
     */
-
 
278
    smc_coherence_block(kimage, size);
-
 
279
   
-
 
280
    uspace_arg_t *kernel_uarg;
-
 
281
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
-
 
282
    if (kernel_uarg == NULL) {
-
 
283
        free(kimage);
-
 
284
        return ENOMEM;
-
 
285
    }
-
 
286
   
-
 
287
    kernel_uarg->uspace_entry =
-
 
288
        (void *) ((elf_header_t *) kimage)->e_entry;
-
 
289
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
-
 
290
    kernel_uarg->uspace_thread_function = NULL;
-
 
291
    kernel_uarg->uspace_thread_arg = NULL;
-
 
292
    kernel_uarg->uspace_uarg = NULL;
-
 
293
   
-
 
294
    as_t *as = as_create(0);
-
 
295
    if (as == NULL) {
-
 
296
        free(kernel_uarg);
-
 
297
        free(kimage);
-
 
298
        return ENOMEM;
-
 
299
    }
-
 
300
   
-
 
301
    unsigned int erc = elf_load((elf_header_t *) kimage, as);
-
 
302
    if (erc != EE_OK) {
-
 
303
        as_destroy(as);
-
 
304
        free(kernel_uarg);
-
 
305
        free(kimage);
-
 
306
        return ENOENT;
-
 
307
    }
-
 
308
   
-
 
309
    as_area_t *area = as_area_create(as,
-
 
310
        AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
-
 
311
        LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
-
 
312
        AS_AREA_ATTR_NONE, &anon_backend, NULL);
-
 
313
    if (area == NULL) {
-
 
314
        as_destroy(as);
-
 
315
        free(kernel_uarg);
-
 
316
        free(kimage);
-
 
317
        return ENOMEM;
-
 
318
    }
-
 
319
   
-
 
320
    task_t *task = task_create(as, "app");
-
 
321
    if (task == NULL) {
-
 
322
        as_destroy(as);
-
 
323
        free(kernel_uarg);
-
 
324
        free(kimage);
-
 
325
        return ENOENT;
-
 
326
    }
-
 
327
   
-
 
328
    // FIXME: control the capabilities
-
 
329
    cap_set(task, cap_get(TASK));
-
 
330
   
-
 
331
    thread_t *thread = thread_create(uinit, kernel_uarg, task,
-
 
332
        THREAD_FLAG_USPACE, "user", false);
-
 
333
    if (thread == NULL) {
-
 
334
        task_destroy(task);
-
 
335
        as_destroy(as);
-
 
336
        free(kernel_uarg);
-
 
337
        free(kimage);
-
 
338
        return ENOENT;
-
 
339
    }
-
 
340
   
-
 
341
    thread_ready(thread);
-
 
342
   
-
 
343
    return EOK;
-
 
344
}
-
 
345
 
-
 
346
/** Find task structure corresponding to task ID.
247
/** Find task structure corresponding to task ID.
347
 *
248
 *
348
 * The tasks_lock must be already held by the caller of this function
249
 * The tasks_lock must be already held by the caller of this function and
349
 * and interrupts must be disabled.
250
 * interrupts must be disabled.
350
 *
251
 *
351
 * @param id Task ID.
252
 * @param id        Task ID.
352
 *
253
 *
353
 * @return Task structure address or NULL if there is no such task ID.
254
 * @return      Task structure address or NULL if there is no such task
-
 
255
 *          ID.
354
 */
256
 */
355
task_t *task_find_by_id(task_id_t id)
257
task_t *task_find_by_id(task_id_t id) { avltree_node_t *node;
356
{
-
 
357
    avltree_node_t *node;
-
 
358
   
258
   
359
    node = avltree_search(&tasks_tree, (avltree_key_t) id);
259
    node = avltree_search(&tasks_tree, (avltree_key_t) id);
360
 
260
 
361
    if (node)
261
    if (node)
362
        return avltree_get_instance(node, task_t, tasks_tree_node);
262
        return avltree_get_instance(node, task_t, tasks_tree_node);
363
    return NULL;
263
    return NULL;
364
}
264
}
365
 
265
 
366
/** Get accounting data of given task.
266
/** Get accounting data of given task.
367
 *
267
 *
368
 * Note that task lock of 't' must be already held and
268
 * Note that task lock of 't' must be already held and interrupts must be
369
 * interrupts must be already disabled.
269
 * already disabled.
370
 *
270
 *
371
 * @param t Pointer to thread.
271
 * @param t     Pointer to thread.
372
 *
272
 *
-
 
273
 * @return      Number of cycles used by the task and all its threads
-
 
274
 *          so far.
373
 */
275
 */
374
uint64_t task_get_accounting(task_t *t)
276
uint64_t task_get_accounting(task_t *t)
375
{
277
{
376
    /* Accumulated value of task */
278
    /* Accumulated value of task */
377
    uint64_t ret = t->cycles;
279
    uint64_t ret = t->cycles;
378
   
280
   
379
    /* Current values of threads */
281
    /* Current values of threads */
380
    link_t *cur;
282
    link_t *cur;
381
    for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
283
    for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
382
        thread_t *thr = list_get_instance(cur, thread_t, th_link);
284
        thread_t *thr = list_get_instance(cur, thread_t, th_link);
383
       
285
       
384
        spinlock_lock(&thr->lock);
286
        spinlock_lock(&thr->lock);
385
        /* Process only counted threads */
287
        /* Process only counted threads */
386
        if (!thr->uncounted) {
288
        if (!thr->uncounted) {
387
            if (thr == THREAD) {
289
            if (thr == THREAD) {
388
                /* Update accounting of current thread */
290
                /* Update accounting of current thread */
389
                thread_update_accounting();
291
                thread_update_accounting();
390
            }
292
            }
391
            ret += thr->cycles;
293
            ret += thr->cycles;
392
        }
294
        }
393
        spinlock_unlock(&thr->lock);
295
        spinlock_unlock(&thr->lock);
394
    }
296
    }
395
   
297
   
396
    return ret;
298
    return ret;
397
}
299
}
398
 
300
 
399
/** Kill task.
301
/** Kill task.
400
 *
302
 *
401
 * This function is idempotent.
303
 * This function is idempotent.
402
 * It signals all the task's threads to bail it out.
304
 * It signals all the task's threads to bail it out.
403
 *
305
 *
404
 * @param id ID of the task to be killed.
306
 * @param id        ID of the task to be killed.
405
 *
307
 *
406
 * @return 0 on success or an error code from errno.h
308
 * @return      Zero on success or an error code from errno.h.
407
 */
309
 */
408
int task_kill(task_id_t id)
310
int task_kill(task_id_t id)
409
{
311
{
410
    ipl_t ipl;
312
    ipl_t ipl;
411
    task_t *ta;
313
    task_t *ta;
412
    link_t *cur;
314
    link_t *cur;
413
 
315
 
414
    if (id == 1)
316
    if (id == 1)
415
        return EPERM;
317
        return EPERM;
416
   
318
   
417
    ipl = interrupts_disable();
319
    ipl = interrupts_disable();
418
    spinlock_lock(&tasks_lock);
320
    spinlock_lock(&tasks_lock);
419
    if (!(ta = task_find_by_id(id))) {
321
    if (!(ta = task_find_by_id(id))) {
420
        spinlock_unlock(&tasks_lock);
322
        spinlock_unlock(&tasks_lock);
421
        interrupts_restore(ipl);
323
        interrupts_restore(ipl);
422
        return ENOENT;
324
        return ENOENT;
423
    }
325
    }
424
    spinlock_unlock(&tasks_lock);
326
    spinlock_unlock(&tasks_lock);
425
   
327
   
426
    /*
328
    /*
427
     * Interrupt all threads except ktaskclnp.
329
     * Interrupt all threads.
428
     */
330
     */
429
    spinlock_lock(&ta->lock);
331
    spinlock_lock(&ta->lock);
430
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
332
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
431
        thread_t *thr;
333
        thread_t *thr;
432
        bool sleeping = false;
334
        bool sleeping = false;
433
       
335
       
434
        thr = list_get_instance(cur, thread_t, th_link);
336
        thr = list_get_instance(cur, thread_t, th_link);
435
           
337
           
436
        spinlock_lock(&thr->lock);
338
        spinlock_lock(&thr->lock);
437
        thr->interrupted = true;
339
        thr->interrupted = true;
438
        if (thr->state == Sleeping)
340
        if (thr->state == Sleeping)
439
            sleeping = true;
341
            sleeping = true;
440
        spinlock_unlock(&thr->lock);
342
        spinlock_unlock(&thr->lock);
441
       
343
       
442
        if (sleeping)
344
        if (sleeping)
443
            waitq_interrupt_sleep(thr);
345
            waitq_interrupt_sleep(thr);
444
    }
346
    }
445
    spinlock_unlock(&ta->lock);
347
    spinlock_unlock(&ta->lock);
446
    interrupts_restore(ipl);
348
    interrupts_restore(ipl);
447
   
349
   
448
    return 0;
350
    return 0;
449
}
351
}
450
 
352
 
451
static bool task_print_walker(avltree_node_t *node, void *arg)
353
static bool task_print_walker(avltree_node_t *node, void *arg)
452
{
354
{
453
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
355
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
454
    int j;
356
    int j;
455
       
357
       
456
    spinlock_lock(&t->lock);
358
    spinlock_lock(&t->lock);
457
           
359
           
458
    uint64_t cycles;
360
    uint64_t cycles;
459
    char suffix;
361
    char suffix;
460
    order(task_get_accounting(t), &cycles, &suffix);
362
    order(task_get_accounting(t), &cycles, &suffix);
461
 
363
 
462
#ifdef __32_BITS__  
364
#ifdef __32_BITS__  
463
    printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %10p %10p %9" PRIu64
365
    printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %10p %10p %9" PRIu64
464
        "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
366
        "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
465
        suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
367
        suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
466
#endif
368
#endif
467
 
369
 
468
#ifdef __64_BITS__
370
#ifdef __64_BITS__
469
    printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %18p %18p %9" PRIu64
371
    printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %18p %18p %9" PRIu64
470
        "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
372
        "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
471
        suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
373
        suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
472
#endif
374
#endif
473
 
375
 
474
    for (j = 0; j < IPC_MAX_PHONES; j++) {
376
    for (j = 0; j < IPC_MAX_PHONES; j++) {
475
        if (t->phones[j].callee)
377
        if (t->phones[j].callee)
476
            printf(" %d:%p", j, t->phones[j].callee);
378
            printf(" %d:%p", j, t->phones[j].callee);
477
    }
379
    }
478
    printf("\n");
380
    printf("\n");
479
           
381
           
480
    spinlock_unlock(&t->lock);
382
    spinlock_unlock(&t->lock);
481
    return true;
383
    return true;
482
}
384
}
483
 
385
 
484
/** Print task list */
386
/** Print task list */
485
void task_print_list(void)
387
void task_print_list(void)
486
{
388
{
487
    ipl_t ipl;
389
    ipl_t ipl;
488
   
390
   
489
    /* Messing with task structures, avoid deadlock */
391
    /* Messing with task structures, avoid deadlock */
490
    ipl = interrupts_disable();
392
    ipl = interrupts_disable();
491
    spinlock_lock(&tasks_lock);
393
    spinlock_lock(&tasks_lock);
492
 
394
 
493
#ifdef __32_BITS__  
395
#ifdef __32_BITS__  
494
    printf("taskid name       ctx address    as         "
396
    printf("taskid name       ctx address    as         "
495
        "cycles     threads calls  callee\n");
397
        "cycles     threads calls  callee\n");
496
    printf("------ ---------- --- ---------- ---------- "
398
    printf("------ ---------- --- ---------- ---------- "
497
        "---------- ------- ------ ------>\n");
399
        "---------- ------- ------ ------>\n");
498
#endif
400
#endif
499
 
401
 
500
#ifdef __64_BITS__
402
#ifdef __64_BITS__
501
    printf("taskid name       ctx address            as                 "
403
    printf("taskid name       ctx address            as                 "
502
        "cycles     threads calls  callee\n");
404
        "cycles     threads calls  callee\n");
503
    printf("------ ---------- --- ------------------ ------------------ "
405
    printf("------ ---------- --- ------------------ ------------------ "
504
        "---------- ------- ------ ------>\n");
406
        "---------- ------- ------ ------>\n");
505
#endif
407
#endif
506
 
408
 
507
    avltree_walk(&tasks_tree, task_print_walker, NULL);
409
    avltree_walk(&tasks_tree, task_print_walker, NULL);
508
 
410
 
509
    spinlock_unlock(&tasks_lock);
411
    spinlock_unlock(&tasks_lock);
510
    interrupts_restore(ipl);
412
    interrupts_restore(ipl);
511
}
413
}
512
 
414
 
513
/** @}
415
/** @}
514
 */
416
 */
515
 
417