Subversion Repositories HelenOS

Rev

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

Rev 3150 Rev 3153
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 <ipc/ipcrsc.h>
54
#include <ipc/ipcrsc.h>
54
#include <security/cap.h>
55
#include <security/cap.h>
55
#include <memstr.h>
56
#include <memstr.h>
56
#include <print.h>
57
#include <print.h>
57
#include <lib/elf.h>
58
#include <lib/elf.h>
58
#include <errno.h>
59
#include <errno.h>
59
#include <func.h>
60
#include <func.h>
60
#include <syscall/copy.h>
61
#include <syscall/copy.h>
61
 
62
 
62
#ifndef LOADED_PROG_STACK_PAGES_NO
63
#ifndef LOADED_PROG_STACK_PAGES_NO
63
#define LOADED_PROG_STACK_PAGES_NO 1
64
#define LOADED_PROG_STACK_PAGES_NO 1
64
#endif
65
#endif
65
 
66
 
66
/** Spinlock protecting the tasks_tree AVL tree. */
67
/** Spinlock protecting the tasks_tree AVL tree. */
67
SPINLOCK_INITIALIZE(tasks_lock);
68
SPINLOCK_INITIALIZE(tasks_lock);
68
 
69
 
69
/** AVL tree of active tasks.
70
/** AVL tree of active tasks.
70
 *
71
 *
71
 * The task is guaranteed to exist after it was found in the tasks_tree as
72
 * The task is guaranteed to exist after it was found in the tasks_tree as
72
 * long as:
73
 * long as:
73
 * @li the tasks_lock is held,
74
 * @li the tasks_lock is held,
74
 * @li the task's lock is held when task's lock is acquired before releasing
75
 * @li the task's lock is held when task's lock is acquired before releasing
75
 *     tasks_lock or
76
 *     tasks_lock or
76
 * @li the task's refcount is greater than 0
77
 * @li the task's refcount is greater than 0
77
 *
78
 *
78
 */
79
 */
79
avltree_t tasks_tree;
80
avltree_t tasks_tree;
80
 
81
 
81
static task_id_t task_counter = 0;
82
static task_id_t task_counter = 0;
82
 
83
 
83
/**
84
/**
84
 * Points to the binary image used as the program loader. All non-initial
85
 * Points to the binary image used as the program loader. All non-initial
85
 * tasks are created from this executable image.
86
 * tasks are created from this executable image.
86
 */
87
 */
87
void *program_loader = NULL;
88
void *program_loader = NULL;
88
 
89
 
89
 
90
 
90
/** Initialize tasks
91
/** Initialize tasks
91
 *
92
 *
92
 * Initialize kernel tasks support.
93
 * Initialize kernel tasks support.
93
 *
94
 *
94
 */
95
 */
95
void task_init(void)
96
void task_init(void)
96
{
97
{
97
    TASK = NULL;
98
    TASK = NULL;
98
    avltree_create(&tasks_tree);
99
    avltree_create(&tasks_tree);
99
}
100
}
100
 
101
 
101
/*
102
/*
102
 * The idea behind this walker is to remember a single task different from TASK.
103
 * The idea behind this walker is to remember a single task different from TASK.
103
 */
104
 */
104
static bool task_done_walker(avltree_node_t *node, void *arg)
105
static bool task_done_walker(avltree_node_t *node, void *arg)
105
{
106
{
106
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
107
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
107
    task_t **tp = (task_t **) arg;
108
    task_t **tp = (task_t **) arg;
108
 
109
 
109
    if (t != TASK) {
110
    if (t != TASK) {
110
        *tp = t;
111
        *tp = t;
111
        return false;   /* stop walking */
112
        return false;   /* stop walking */
112
    }
113
    }
113
 
114
 
114
    return true;    /* continue the walk */
115
    return true;    /* continue the walk */
115
}
116
}
116
 
117
 
117
/** Kill all tasks except the current task.
118
/** Kill all tasks except the current task.
118
 *
119
 *
119
 */
120
 */
120
void task_done(void)
121
void task_done(void)
121
{
122
{
122
    task_t *t;
123
    task_t *t;
123
    do { /* Repeat until there are any tasks except TASK */
124
    do { /* Repeat until there are any tasks except TASK */
124
       
125
       
125
        /* Messing with task structures, avoid deadlock */
126
        /* Messing with task structures, avoid deadlock */
126
        ipl_t ipl = interrupts_disable();
127
        ipl_t ipl = interrupts_disable();
127
        spinlock_lock(&tasks_lock);
128
        spinlock_lock(&tasks_lock);
128
       
129
       
129
        t = NULL;
130
        t = NULL;
130
        avltree_walk(&tasks_tree, task_done_walker, &t);
131
        avltree_walk(&tasks_tree, task_done_walker, &t);
131
       
132
       
132
        if (t != NULL) {
133
        if (t != NULL) {
133
            task_id_t id = t->taskid;
134
            task_id_t id = t->taskid;
134
           
135
           
135
            spinlock_unlock(&tasks_lock);
136
            spinlock_unlock(&tasks_lock);
136
            interrupts_restore(ipl);
137
            interrupts_restore(ipl);
137
           
138
           
138
#ifdef CONFIG_DEBUG
139
#ifdef CONFIG_DEBUG
139
            printf("Killing task %" PRIu64 "\n", id);
140
            printf("Killing task %" PRIu64 "\n", id);
140
#endif          
141
#endif          
141
            task_kill(id);
142
            task_kill(id);
142
            thread_usleep(10000);
143
            thread_usleep(10000);
143
        } else {
144
        } else {
144
            spinlock_unlock(&tasks_lock);
145
            spinlock_unlock(&tasks_lock);
145
            interrupts_restore(ipl);
146
            interrupts_restore(ipl);
146
        }
147
        }
147
       
148
       
148
    } while (t != NULL);
149
    } while (t != NULL);
149
}
150
}
150
 
151
 
151
/** Create new task
152
/** Create new task
152
 *
153
 *
153
 * Create new task with no threads.
154
 * Create new task with no threads.
154
 *
155
 *
155
 * @param as Task's address space.
156
 * @param as Task's address space.
156
 * @param name Symbolic name.
157
 * @param name Symbolic name.
157
 *
158
 *
158
 * @return New task's structure
159
 * @return New task's structure
159
 *
160
 *
160
 */
161
 */
161
task_t *task_create(as_t *as, char *name)
162
task_t *task_create(as_t *as, char *name)
162
{
163
{
163
    ipl_t ipl;
164
    ipl_t ipl;
164
    task_t *ta;
165
    task_t *ta;
165
    int i;
166
    int i;
166
   
167
   
167
    ta = (task_t *) malloc(sizeof(task_t), 0);
168
    ta = (task_t *) malloc(sizeof(task_t), 0);
168
 
169
 
169
    task_create_arch(ta);
170
    task_create_arch(ta);
170
 
171
 
171
    spinlock_initialize(&ta->lock, "task_ta_lock");
172
    spinlock_initialize(&ta->lock, "task_ta_lock");
172
    list_initialize(&ta->th_head);
173
    list_initialize(&ta->th_head);
173
    ta->as = as;
174
    ta->as = as;
174
    ta->name = name;
175
    ta->name = name;
175
    atomic_set(&ta->refcount, 0);
176
    atomic_set(&ta->refcount, 0);
176
    atomic_set(&ta->lifecount, 0);
177
    atomic_set(&ta->lifecount, 0);
177
    ta->context = CONTEXT;
178
    ta->context = CONTEXT;
178
 
179
 
179
    ta->capabilities = 0;
180
    ta->capabilities = 0;
180
    ta->cycles = 0;
181
    ta->cycles = 0;
181
   
182
   
182
    ipc_answerbox_init(&ta->answerbox, ta);
183
    ipc_answerbox_init(&ta->answerbox, ta);
183
    for (i = 0; i < IPC_MAX_PHONES; i++)
184
    for (i = 0; i < IPC_MAX_PHONES; i++)
184
        ipc_phone_init(&ta->phones[i]);
185
        ipc_phone_init(&ta->phones[i]);
185
    if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context,
186
    if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context,
186
        ta->context)))
187
        ta->context)))
187
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
188
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
188
    atomic_set(&ta->active_calls, 0);
189
    atomic_set(&ta->active_calls, 0);
189
 
190
 
190
    mutex_initialize(&ta->futexes_lock);
191
    mutex_initialize(&ta->futexes_lock);
191
    btree_create(&ta->futexes);
192
    btree_create(&ta->futexes);
192
   
193
   
193
    ipl = interrupts_disable();
194
    ipl = interrupts_disable();
194
 
195
 
195
    /*
196
    /*
196
     * Increment address space reference count.
197
     * Increment address space reference count.
197
     */
198
     */
198
    atomic_inc(&as->refcount);
199
    atomic_inc(&as->refcount);
199
 
200
 
200
    spinlock_lock(&tasks_lock);
201
    spinlock_lock(&tasks_lock);
201
    ta->taskid = ++task_counter;
202
    ta->taskid = ++task_counter;
202
    avltree_node_initialize(&ta->tasks_tree_node);
203
    avltree_node_initialize(&ta->tasks_tree_node);
203
    ta->tasks_tree_node.key = ta->taskid;
204
    ta->tasks_tree_node.key = ta->taskid;
204
    avltree_insert(&tasks_tree, &ta->tasks_tree_node);
205
    avltree_insert(&tasks_tree, &ta->tasks_tree_node);
205
    spinlock_unlock(&tasks_lock);
206
    spinlock_unlock(&tasks_lock);
206
    interrupts_restore(ipl);
207
    interrupts_restore(ipl);
207
 
208
 
208
    return ta;
209
    return ta;
209
}
210
}
210
 
211
 
211
/** Destroy task.
212
/** Destroy task.
212
 *
213
 *
213
 * @param t Task to be destroyed.
214
 * @param t Task to be destroyed.
214
 */
215
 */
215
void task_destroy(task_t *t)
216
void task_destroy(task_t *t)
216
{
217
{
217
    /*
218
    /*
218
     * Remove the task from the task B+tree.
219
     * Remove the task from the task B+tree.
219
     */
220
     */
220
    spinlock_lock(&tasks_lock);
221
    spinlock_lock(&tasks_lock);
221
    avltree_delete(&tasks_tree, &t->tasks_tree_node);
222
    avltree_delete(&tasks_tree, &t->tasks_tree_node);
222
    spinlock_unlock(&tasks_lock);
223
    spinlock_unlock(&tasks_lock);
223
 
224
 
224
    /*
225
    /*
225
     * Perform architecture specific task destruction.
226
     * Perform architecture specific task destruction.
226
     */
227
     */
227
    task_destroy_arch(t);
228
    task_destroy_arch(t);
228
 
229
 
229
    /*
230
    /*
230
     * Free up dynamically allocated state.
231
     * Free up dynamically allocated state.
231
     */
232
     */
232
    btree_destroy(&t->futexes);
233
    btree_destroy(&t->futexes);
233
 
234
 
234
    /*
235
    /*
235
     * Drop our reference to the address space.
236
     * Drop our reference to the address space.
236
     */
237
     */
237
    if (atomic_predec(&t->as->refcount) == 0)
238
    if (atomic_predec(&t->as->refcount) == 0)
238
        as_destroy(t->as);
239
        as_destroy(t->as);
239
   
240
   
240
    free(t);
241
    free(t);
241
    TASK = NULL;
242
    TASK = NULL;
242
}
243
}
243
 
244
 
244
/** Create new task with 1 thread and run it
245
/** Create new task with 1 thread and run it
245
 *
246
 *
246
 * @param as Address space containing a binary program image.
247
 * @param as Address space containing a binary program image.
247
 * @param entry_addr Program entry-point address in program address space.
248
 * @param entry_addr Program entry-point address in program address space.
248
 * @param name Program name.
249
 * @param name Program name.
249
 *
250
 *
250
 * @return Task of the running program or NULL on error.
251
 * @return Task of the running program or NULL on error.
251
 */
252
 */
252
task_t *task_create_from_as(as_t *as, uintptr_t entry_addr, char *name,
253
task_t *task_create_from_as(as_t *as, uintptr_t entry_addr, char *name,
253
    thread_t **thr)
254
    thread_t **thr)
254
{
255
{
255
    as_area_t *a;
256
    as_area_t *a;
256
    thread_t *t;
257
    thread_t *t;
257
    task_t *task;
258
    task_t *task;
258
    uspace_arg_t *kernel_uarg;
259
    uspace_arg_t *kernel_uarg;
259
 
260
 
260
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
261
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
261
    kernel_uarg->uspace_entry = (void *) entry_addr;
262
    kernel_uarg->uspace_entry = (void *) entry_addr;
262
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
263
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
263
    kernel_uarg->uspace_thread_function = NULL;
264
    kernel_uarg->uspace_thread_function = NULL;
264
    kernel_uarg->uspace_thread_arg = NULL;
265
    kernel_uarg->uspace_thread_arg = NULL;
265
    kernel_uarg->uspace_uarg = NULL;
266
    kernel_uarg->uspace_uarg = NULL;
266
   
267
   
267
    task = task_create(as, name);
268
    task = task_create(as, name);
268
    ASSERT(task);
269
    ASSERT(task);
269
 
270
 
270
    /*
271
    /*
271
     * Create the data as_area.
272
     * Create the data as_area.
272
     */
273
     */
273
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
274
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
274
        LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
275
        LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
275
        AS_AREA_ATTR_NONE, &anon_backend, NULL);
276
        AS_AREA_ATTR_NONE, &anon_backend, NULL);
276
 
277
 
277
    /*
278
    /*
278
     * Create the main thread.
279
     * Create the main thread.
279
     */
280
     */
280
    t = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE,
281
    t = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE,
281
        "uinit", false);
282
        "uinit", false);
282
    ASSERT(t);
283
    ASSERT(t);
283
 
284
 
284
    *thr = t;
285
    *thr = t;
285
   
286
   
286
    return task;
287
    return task;
287
}
288
}
288
 
289
 
289
/** Parse an executable image in the physical memory.
290
/** Parse an executable image in the physical memory.
290
 *
291
 *
291
 * If the image belongs to a program loader, it is registered as such,
292
 * If the image belongs to a program loader, it is registered as such,
292
 * (and *task is set to NULL). Otherwise a task is created from the
293
 * (and *task is set to NULL). Otherwise a task is created from the
293
 * executable image. The task is returned in *task.
294
 * executable image. The task is returned in *task.
294
 *
295
 *
295
 * @param program_addr Address of program executable image.
296
 * @param program_addr Address of program executable image.
296
 * @param name Program name.
297
 * @param name Program name.
297
 * @param task Where to store the pointer to the newly created task.
298
 * @param task Where to store the pointer to the newly created task.
298
 *
299
 *
299
 * @return EOK on success or negative error code.
300
 * @return EOK on success or negative error code.
300
 */
301
 */
301
int task_parse_initial(void *program_addr, char *name, thread_t **t)
302
int task_parse_initial(void *program_addr, char *name, thread_t **t)
302
{
303
{
303
    as_t *as;
304
    as_t *as;
304
    unsigned int rc;
305
    unsigned int rc;
305
    task_t *task;
306
    task_t *task;
306
 
307
 
307
    as = as_create(0);
308
    as = as_create(0);
308
    ASSERT(as);
309
    ASSERT(as);
309
 
310
 
310
    rc = elf_load((elf_header_t *) program_addr, as, 0);
311
    rc = elf_load((elf_header_t *) program_addr, as, 0);
311
    if (rc != EE_OK) {
312
    if (rc != EE_OK) {
312
        as_destroy(as);
313
        as_destroy(as);
313
        *t = NULL;
314
        *t = NULL;
314
        if (rc != EE_LOADER)
315
        if (rc != EE_LOADER)
315
            return ENOTSUP;
316
            return ENOTSUP;
316
       
317
       
317
        /* Register image as the program loader */
318
        /* Register image as the program loader */
318
        ASSERT(program_loader == NULL);
319
        ASSERT(program_loader == NULL);
319
        program_loader = program_addr;
320
        program_loader = program_addr;
320
        return EOK;
321
        return EOK;
321
    }
322
    }
322
 
323
 
323
    task = task_create_from_as(as, ((elf_header_t *) program_addr)->e_entry,
324
    task = task_create_from_as(as, ((elf_header_t *) program_addr)->e_entry,
324
        name, t);
325
        name, t);
325
 
326
 
326
    return EOK;
327
    return EOK;
327
}
328
}
328
 
329
 
329
/** Create a task from the program loader image.
330
/** Create a task from the program loader image.
330
 *
331
 *
331
 * @param name Program name.
332
 * @param name Program name.
332
 * @param t Buffer for storing pointer to the newly created task.
333
 * @param t Buffer for storing pointer to the newly created task.
333
 *
334
 *
334
 * @return Task of the running program or NULL on error.
335
 * @return Task of the running program or NULL on error.
335
 */
336
 */
336
int task_create_from_loader(char *name, task_t **t)
337
int task_create_from_loader(char *name, task_t **t)
337
{
338
{
338
    as_t *as;
339
    as_t *as;
339
    unsigned int rc;
340
    unsigned int rc;
340
    void *loader;
341
    void *loader;
341
    thread_t *thr;
342
    thread_t *thr;
342
 
343
 
343
    as = as_create(0);
344
    as = as_create(0);
344
    ASSERT(as);
345
    ASSERT(as);
345
 
346
 
346
    loader = program_loader;
347
    loader = program_loader;
347
    if (!loader) return ENOENT;
348
    if (!loader) return ENOENT;
348
 
349
 
349
    rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER);
350
    rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER);
350
    if (rc != EE_OK) {
351
    if (rc != EE_OK) {
351
        as_destroy(as);
352
        as_destroy(as);
352
        return ENOENT;
353
        return ENOENT;
353
    }
354
    }
354
 
355
 
355
    *t = task_create_from_as(
356
    *t = task_create_from_as(
356
        as, ((elf_header_t *) program_loader)->e_entry, name, &thr);
357
        as, ((elf_header_t *) program_loader)->e_entry, name, &thr);
357
 
358
 
358
    return EOK;
359
    return EOK;
359
}
360
}
360
 
361
 
361
/** Make task ready.
362
/** Make task ready.
362
 *
363
 *
363
 * Switch task's thread to the ready state.
364
 * Switch task's thread to the ready state.
364
 *
365
 *
365
 * @param ta Task to make ready.
366
 * @param ta Task to make ready.
366
 */
367
 */
367
void task_ready(task_t *t)
368
void task_ready(task_t *t)
368
{
369
{
369
    thread_t *th;
370
    thread_t *th;
370
 
371
 
371
    th = list_get_instance(t->th_head.next, thread_t, th_link);
372
    th = list_get_instance(t->th_head.next, thread_t, th_link);
372
    thread_ready(th);
373
    thread_ready(th);
373
}
374
}
374
 
375
 
375
/** Syscall for reading task ID from userspace.
376
/** Syscall for reading task ID from userspace.
376
 *
377
 *
377
 * @param uspace_task_id Userspace address of 8-byte buffer where to store
378
 * @param uspace_task_id Userspace address of 8-byte buffer where to store
378
 * current task ID.
379
 * current task ID.
379
 *
380
 *
380
 * @return 0 on success or an error code from @ref errno.h.
381
 * @return 0 on success or an error code from @ref errno.h.
381
 */
382
 */
382
unative_t sys_task_get_id(task_id_t *uspace_task_id)
383
unative_t sys_task_get_id(task_id_t *uspace_task_id)
383
{
384
{
384
    /*
385
    /*
385
     * No need to acquire lock on TASK because taskid
386
     * No need to acquire lock on TASK because taskid
386
     * remains constant for the lifespan of the task.
387
     * remains constant for the lifespan of the task.
387
     */
388
     */
388
    return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
389
    return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
389
        sizeof(TASK->taskid));
390
        sizeof(TASK->taskid));
390
}
391
}
391
 
392
 
392
/** Syscall for creating a new task from userspace.
393
/** Syscall for creating a new task from userspace.
393
 *
394
 *
394
 * Creates a new task from the program loader image, connects a phone
395
 * Creates a new task from the program loader image, connects a phone
395
 * to it and stores the phone id into the provided buffer.
396
 * to it and stores the phone id into the provided buffer.
396
 *
397
 *
397
 * @param uspace_phone_id Userspace address where to store the phone id.
398
 * @param uspace_phone_id Userspace address where to store the phone id.
398
 *
399
 *
399
 * @return 0 on success or an error code from @ref errno.h.
400
 * @return 0 on success or an error code from @ref errno.h.
400
 */
401
 */
401
unative_t sys_task_spawn_loader(int *uspace_phone_id)
402
unative_t sys_task_spawn_loader(int *uspace_phone_id)
402
{
403
{
403
    task_t *t;
404
    task_t *t;
404
    int fake_id;
405
    int fake_id;
405
    int rc;
406
    int rc;
406
    int phone_id;
407
    int phone_id;
407
 
408
 
408
    fake_id = 0;
409
    fake_id = 0;
409
 
410
 
410
    /* Before we even try creating the task, see if we can write the id */
411
    /* Before we even try creating the task, see if we can write the id */
411
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &fake_id,
412
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &fake_id,
412
        sizeof(fake_id));
413
        sizeof(fake_id));
413
    if (rc != 0)
414
    if (rc != 0)
414
        return rc;
415
        return rc;
415
 
416
 
416
    phone_id = phone_alloc();
417
    phone_id = phone_alloc();
417
    if (phone_id < 0)
418
    if (phone_id < 0)
418
        return ELIMIT;
419
        return ELIMIT;
419
 
420
 
420
    rc = task_create_from_loader("loader", &t);
421
    rc = task_create_from_loader("loader", &t);
421
    if (rc != 0)
422
    if (rc != 0)
422
        return rc;
423
        return rc;
423
 
424
 
424
    phone_connect(phone_id, &t->answerbox);
425
    phone_connect(phone_id, &t->answerbox);
425
 
426
 
426
    /* No need to aquire lock before task_ready() */
427
    /* No need to aquire lock before task_ready() */
427
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &phone_id,
428
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &phone_id,
428
        sizeof(phone_id));
429
        sizeof(phone_id));
429
    if (rc != 0) {
430
    if (rc != 0) {
430
        /* Ooops */
431
        /* Ooops */
431
        ipc_phone_hangup(&TASK->phones[phone_id]);
432
        ipc_phone_hangup(&TASK->phones[phone_id]);
432
        task_kill(t->taskid);
433
        task_kill(t->taskid);
433
        return rc;
434
        return rc;
434
    }
435
    }
435
 
436
 
436
    task_ready(t);
437
    task_ready(t);
437
 
438
 
438
    return EOK;
439
    return EOK;
439
}
440
}
440
 
441
 
441
unative_t sys_task_spawn(void *image, size_t size)
442
unative_t sys_task_spawn(void *image, size_t size)
442
{
443
{
443
    void *kimage = malloc(size, 0);
444
    void *kimage = malloc(size, 0);
444
    if (kimage == NULL)
445
    if (kimage == NULL)
445
        return ENOMEM;
446
        return ENOMEM;
446
   
447
   
447
    int rc = copy_from_uspace(kimage, image, size);
448
    int rc = copy_from_uspace(kimage, image, size);
448
    if (rc != EOK)
449
    if (rc != EOK)
449
        return rc;
450
        return rc;
-
 
451
 
-
 
452
    /*
-
 
453
     * Not very efficient and it would be better to call it on code only,
-
 
454
     * but this whole function is a temporary hack anyway and one day it
-
 
455
     * will go in favor of the userspace dynamic loader.
-
 
456
     */
-
 
457
    smc_coherence_block(kimage, size);
450
   
458
   
-
 
459
    uspace_arg_t *kernel_uarg;
451
    uspace_arg_t *kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
460
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
452
    if (kernel_uarg == NULL) {
461
    if (kernel_uarg == NULL) {
453
        free(kimage);
462
        free(kimage);
454
        return ENOMEM;
463
        return ENOMEM;
455
    }
464
    }
456
   
465
   
457
    kernel_uarg->uspace_entry =
466
    kernel_uarg->uspace_entry =
458
        (void *) ((elf_header_t *) kimage)->e_entry;
467
        (void *) ((elf_header_t *) kimage)->e_entry;
459
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
468
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
460
    kernel_uarg->uspace_thread_function = NULL;
469
    kernel_uarg->uspace_thread_function = NULL;
461
    kernel_uarg->uspace_thread_arg = NULL;
470
    kernel_uarg->uspace_thread_arg = NULL;
462
    kernel_uarg->uspace_uarg = NULL;
471
    kernel_uarg->uspace_uarg = NULL;
463
   
472
   
464
    as_t *as = as_create(0);
473
    as_t *as = as_create(0);
465
    if (as == NULL) {
474
    if (as == NULL) {
466
        free(kernel_uarg);
475
        free(kernel_uarg);
467
        free(kimage);
476
        free(kimage);
468
        return ENOMEM;
477
        return ENOMEM;
469
    }
478
    }
470
   
479
   
471
    unsigned int erc = elf_load((elf_header_t *) kimage, as, ELD_F_NONE);
480
    unsigned int erc = elf_load((elf_header_t *) kimage, as, ELD_F_NONE);
472
    if (erc != EE_OK) {
481
    if (erc != EE_OK) {
473
        as_destroy(as);
482
        as_destroy(as);
474
        free(kernel_uarg);
483
        free(kernel_uarg);
475
        free(kimage);
484
        free(kimage);
476
        return ENOENT;
485
        return ENOENT;
477
    }
486
    }
478
   
487
   
479
    as_area_t *area = as_area_create(as,
488
    as_area_t *area = as_area_create(as,
480
        AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
489
        AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
481
        LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
490
        LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
482
        AS_AREA_ATTR_NONE, &anon_backend, NULL);
491
        AS_AREA_ATTR_NONE, &anon_backend, NULL);
483
    if (area == NULL) {
492
    if (area == NULL) {
484
        as_destroy(as);
493
        as_destroy(as);
485
        free(kernel_uarg);
494
        free(kernel_uarg);
486
        free(kimage);
495
        free(kimage);
487
        return ENOMEM;
496
        return ENOMEM;
488
    }
497
    }
489
   
498
   
490
    task_t *task = task_create(as, "app");
499
    task_t *task = task_create(as, "app");
491
    if (task == NULL) {
500
    if (task == NULL) {
492
        as_destroy(as);
501
        as_destroy(as);
493
        free(kernel_uarg);
502
        free(kernel_uarg);
494
        free(kimage);
503
        free(kimage);
495
        return ENOENT;
504
        return ENOENT;
496
    }
505
    }
497
   
506
   
498
    // FIXME: control the capabilities
507
    // FIXME: control the capabilities
499
    cap_set(task, cap_get(TASK));
508
    cap_set(task, cap_get(TASK));
500
   
509
   
501
    thread_t *thread = thread_create(uinit, kernel_uarg, task,
510
    thread_t *thread = thread_create(uinit, kernel_uarg, task,
502
        THREAD_FLAG_USPACE, "user", false);
511
        THREAD_FLAG_USPACE, "user", false);
503
    if (thread == NULL) {
512
    if (thread == NULL) {
504
        task_destroy(task);
513
        task_destroy(task);
505
        as_destroy(as);
514
        as_destroy(as);
506
        free(kernel_uarg);
515
        free(kernel_uarg);
507
        free(kimage);
516
        free(kimage);
508
        return ENOENT;
517
        return ENOENT;
509
    }
518
    }
510
   
519
   
511
    thread_ready(thread);
520
    thread_ready(thread);
512
   
521
   
513
    return EOK;
522
    return EOK;
514
}
523
}
515
 
524
 
516
/** Find task structure corresponding to task ID.
525
/** Find task structure corresponding to task ID.
517
 *
526
 *
518
 * The tasks_lock must be already held by the caller of this function
527
 * The tasks_lock must be already held by the caller of this function
519
 * and interrupts must be disabled.
528
 * and interrupts must be disabled.
520
 *
529
 *
521
 * @param id Task ID.
530
 * @param id Task ID.
522
 *
531
 *
523
 * @return Task structure address or NULL if there is no such task ID.
532
 * @return Task structure address or NULL if there is no such task ID.
524
 */
533
 */
525
task_t *task_find_by_id(task_id_t id)
534
task_t *task_find_by_id(task_id_t id)
526
{
535
{
527
    avltree_node_t *node;
536
    avltree_node_t *node;
528
   
537
   
529
    node = avltree_search(&tasks_tree, (avltree_key_t) id);
538
    node = avltree_search(&tasks_tree, (avltree_key_t) id);
530
 
539
 
531
    if (node)
540
    if (node)
532
        return avltree_get_instance(node, task_t, tasks_tree_node);
541
        return avltree_get_instance(node, task_t, tasks_tree_node);
533
    return NULL;
542
    return NULL;
534
}
543
}
535
 
544
 
536
/** Get accounting data of given task.
545
/** Get accounting data of given task.
537
 *
546
 *
538
 * Note that task lock of 't' must be already held and
547
 * Note that task lock of 't' must be already held and
539
 * interrupts must be already disabled.
548
 * interrupts must be already disabled.
540
 *
549
 *
541
 * @param t Pointer to thread.
550
 * @param t Pointer to thread.
542
 *
551
 *
543
 */
552
 */
544
uint64_t task_get_accounting(task_t *t)
553
uint64_t task_get_accounting(task_t *t)
545
{
554
{
546
    /* Accumulated value of task */
555
    /* Accumulated value of task */
547
    uint64_t ret = t->cycles;
556
    uint64_t ret = t->cycles;
548
   
557
   
549
    /* Current values of threads */
558
    /* Current values of threads */
550
    link_t *cur;
559
    link_t *cur;
551
    for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
560
    for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
552
        thread_t *thr = list_get_instance(cur, thread_t, th_link);
561
        thread_t *thr = list_get_instance(cur, thread_t, th_link);
553
       
562
       
554
        spinlock_lock(&thr->lock);
563
        spinlock_lock(&thr->lock);
555
        /* Process only counted threads */
564
        /* Process only counted threads */
556
        if (!thr->uncounted) {
565
        if (!thr->uncounted) {
557
            if (thr == THREAD) {
566
            if (thr == THREAD) {
558
                /* Update accounting of current thread */
567
                /* Update accounting of current thread */
559
                thread_update_accounting();
568
                thread_update_accounting();
560
            }
569
            }
561
            ret += thr->cycles;
570
            ret += thr->cycles;
562
        }
571
        }
563
        spinlock_unlock(&thr->lock);
572
        spinlock_unlock(&thr->lock);
564
    }
573
    }
565
   
574
   
566
    return ret;
575
    return ret;
567
}
576
}
568
 
577
 
569
/** Kill task.
578
/** Kill task.
570
 *
579
 *
571
 * This function is idempotent.
580
 * This function is idempotent.
572
 * It signals all the task's threads to bail it out.
581
 * It signals all the task's threads to bail it out.
573
 *
582
 *
574
 * @param id ID of the task to be killed.
583
 * @param id ID of the task to be killed.
575
 *
584
 *
576
 * @return 0 on success or an error code from errno.h
585
 * @return 0 on success or an error code from errno.h
577
 */
586
 */
578
int task_kill(task_id_t id)
587
int task_kill(task_id_t id)
579
{
588
{
580
    ipl_t ipl;
589
    ipl_t ipl;
581
    task_t *ta;
590
    task_t *ta;
582
    link_t *cur;
591
    link_t *cur;
583
 
592
 
584
    if (id == 1)
593
    if (id == 1)
585
        return EPERM;
594
        return EPERM;
586
   
595
   
587
    ipl = interrupts_disable();
596
    ipl = interrupts_disable();
588
    spinlock_lock(&tasks_lock);
597
    spinlock_lock(&tasks_lock);
589
    if (!(ta = task_find_by_id(id))) {
598
    if (!(ta = task_find_by_id(id))) {
590
        spinlock_unlock(&tasks_lock);
599
        spinlock_unlock(&tasks_lock);
591
        interrupts_restore(ipl);
600
        interrupts_restore(ipl);
592
        return ENOENT;
601
        return ENOENT;
593
    }
602
    }
594
    spinlock_unlock(&tasks_lock);
603
    spinlock_unlock(&tasks_lock);
595
   
604
   
596
    /*
605
    /*
597
     * Interrupt all threads except ktaskclnp.
606
     * Interrupt all threads except ktaskclnp.
598
     */
607
     */
599
    spinlock_lock(&ta->lock);
608
    spinlock_lock(&ta->lock);
600
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
609
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
601
        thread_t *thr;
610
        thread_t *thr;
602
        bool sleeping = false;
611
        bool sleeping = false;
603
       
612
       
604
        thr = list_get_instance(cur, thread_t, th_link);
613
        thr = list_get_instance(cur, thread_t, th_link);
605
           
614
           
606
        spinlock_lock(&thr->lock);
615
        spinlock_lock(&thr->lock);
607
        thr->interrupted = true;
616
        thr->interrupted = true;
608
        if (thr->state == Sleeping)
617
        if (thr->state == Sleeping)
609
            sleeping = true;
618
            sleeping = true;
610
        spinlock_unlock(&thr->lock);
619
        spinlock_unlock(&thr->lock);
611
       
620
       
612
        if (sleeping)
621
        if (sleeping)
613
            waitq_interrupt_sleep(thr);
622
            waitq_interrupt_sleep(thr);
614
    }
623
    }
615
    spinlock_unlock(&ta->lock);
624
    spinlock_unlock(&ta->lock);
616
    interrupts_restore(ipl);
625
    interrupts_restore(ipl);
617
   
626
   
618
    return 0;
627
    return 0;
619
}
628
}
620
 
629
 
621
static bool task_print_walker(avltree_node_t *node, void *arg)
630
static bool task_print_walker(avltree_node_t *node, void *arg)
622
{
631
{
623
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
632
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
624
    int j;
633
    int j;
625
       
634
       
626
    spinlock_lock(&t->lock);
635
    spinlock_lock(&t->lock);
627
           
636
           
628
    uint64_t cycles;
637
    uint64_t cycles;
629
    char suffix;
638
    char suffix;
630
    order(task_get_accounting(t), &cycles, &suffix);
639
    order(task_get_accounting(t), &cycles, &suffix);
631
 
640
 
632
#ifdef __32_BITS__  
641
#ifdef __32_BITS__  
633
    printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %10p %10p %9" PRIu64 "%c %7ld %6ld",
642
    printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %10p %10p %9" PRIu64
634
        t->taskid, t->name, t->context, t, t->as, cycles, suffix,
643
        "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
635
        atomic_get(&t->refcount), atomic_get(&t->active_calls));
644
        suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
636
#endif
645
#endif
637
 
646
 
638
#ifdef __64_BITS__
647
#ifdef __64_BITS__
639
    printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %18p %18p %9" PRIu64 "%c %7ld %6ld",
648
    printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %18p %18p %9" PRIu64
640
        t->taskid, t->name, t->context, t, t->as, cycles, suffix,
649
        "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
641
        atomic_get(&t->refcount), atomic_get(&t->active_calls));
650
        suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
642
#endif
651
#endif
643
 
652
 
644
    for (j = 0; j < IPC_MAX_PHONES; j++) {
653
    for (j = 0; j < IPC_MAX_PHONES; j++) {
645
        if (t->phones[j].callee)
654
        if (t->phones[j].callee)
646
            printf(" %d:%p", j, t->phones[j].callee);
655
            printf(" %d:%p", j, t->phones[j].callee);
647
    }
656
    }
648
    printf("\n");
657
    printf("\n");
649
           
658
           
650
    spinlock_unlock(&t->lock);
659
    spinlock_unlock(&t->lock);
651
    return true;
660
    return true;
652
}
661
}
653
 
662
 
654
/** Print task list */
663
/** Print task list */
655
void task_print_list(void)
664
void task_print_list(void)
656
{
665
{
657
    ipl_t ipl;
666
    ipl_t ipl;
658
   
667
   
659
    /* Messing with task structures, avoid deadlock */
668
    /* Messing with task structures, avoid deadlock */
660
    ipl = interrupts_disable();
669
    ipl = interrupts_disable();
661
    spinlock_lock(&tasks_lock);
670
    spinlock_lock(&tasks_lock);
662
 
671
 
663
#ifdef __32_BITS__  
672
#ifdef __32_BITS__  
664
    printf("taskid name       ctx address    as         "
673
    printf("taskid name       ctx address    as         "
665
        "cycles     threads calls  callee\n");
674
        "cycles     threads calls  callee\n");
666
    printf("------ ---------- --- ---------- ---------- "
675
    printf("------ ---------- --- ---------- ---------- "
667
        "---------- ------- ------ ------>\n");
676
        "---------- ------- ------ ------>\n");
668
#endif
677
#endif
669
 
678
 
670
#ifdef __64_BITS__
679
#ifdef __64_BITS__
671
    printf("taskid name       ctx address            as                 "
680
    printf("taskid name       ctx address            as                 "
672
        "cycles     threads calls  callee\n");
681
        "cycles     threads calls  callee\n");
673
    printf("------ ---------- --- ------------------ ------------------ "
682
    printf("------ ---------- --- ------------------ ------------------ "
674
        "---------- ------- ------ ------>\n");
683
        "---------- ------- ------ ------>\n");
675
#endif
684
#endif
676
 
685
 
677
    avltree_walk(&tasks_tree, task_print_walker, NULL);
686
    avltree_walk(&tasks_tree, task_print_walker, NULL);
678
 
687
 
679
    spinlock_unlock(&tasks_lock);
688
    spinlock_unlock(&tasks_lock);
680
    interrupts_restore(ipl);
689
    interrupts_restore(ipl);
681
}
690
}
682
 
691
 
683
/** @}
692
/** @}
684
 */
693
 */
685
 
694