Subversion Repositories HelenOS

Rev

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

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