Subversion Repositories HelenOS

Rev

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

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