Subversion Repositories HelenOS-historic

Rev

Rev 1587 | Rev 1589 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1248 jermar 29
/**
30
 * @file    task.c
31
 * @brief   Task management.
32
 */
33
 
973 palkovsky 34
#include <main/uinit.h>
1 jermar 35
#include <proc/thread.h>
36
#include <proc/task.h>
1078 jermar 37
#include <proc/uarg.h>
703 jermar 38
#include <mm/as.h>
814 palkovsky 39
#include <mm/slab.h>
1 jermar 40
#include <synch/spinlock.h>
41
#include <arch.h>
42
#include <panic.h>
1159 jermar 43
#include <adt/btree.h>
788 jermar 44
#include <adt/list.h>
955 palkovsky 45
#include <ipc/ipc.h>
1174 jermar 46
#include <security/cap.h>
955 palkovsky 47
#include <memstr.h>
1060 palkovsky 48
#include <print.h>
973 palkovsky 49
#include <elf.h>
1579 jermar 50
#include <errno.h>
1288 jermar 51
#include <syscall/copy.h>
973 palkovsky 52
 
1170 vana 53
#ifndef LOADED_PROG_STACK_PAGES_NO
54
#define LOADED_PROG_STACK_PAGES_NO 1
55
#endif
1168 vana 56
 
623 jermar 57
SPINLOCK_INITIALIZE(tasks_lock);
1159 jermar 58
btree_t tasks_btree;
1005 palkovsky 59
static task_id_t task_counter = 0;
1 jermar 60
 
1585 jermar 61
static void ktaskclnp(void *arg);
62
static void ktaskkill(void *arg);
1579 jermar 63
 
107 decky 64
/** Initialize tasks
65
 *
66
 * Initialize kernel tasks support.
67
 *
68
 */
1 jermar 69
void task_init(void)
70
{
15 jermar 71
    TASK = NULL;
1159 jermar 72
    btree_create(&tasks_btree);
1 jermar 73
}
74
 
107 decky 75
 
76
/** Create new task
77
 *
78
 * Create new task with no threads.
79
 *
703 jermar 80
 * @param as Task's address space.
1062 jermar 81
 * @param name Symbolic name.
107 decky 82
 *
973 palkovsky 83
 * @return New task's structure
107 decky 84
 *
85
 */
1062 jermar 86
task_t *task_create(as_t *as, char *name)
1 jermar 87
{
413 jermar 88
    ipl_t ipl;
1 jermar 89
    task_t *ta;
1040 palkovsky 90
    int i;
1 jermar 91
 
822 palkovsky 92
    ta = (task_t *) malloc(sizeof(task_t), 0);
93
 
1185 jermar 94
    task_create_arch(ta);
95
 
822 palkovsky 96
    spinlock_initialize(&ta->lock, "task_ta_lock");
97
    list_initialize(&ta->th_head);
98
    ta->as = as;
1062 jermar 99
    ta->name = name;
1585 jermar 100
    ta->main_thread = NULL;
1579 jermar 101
    ta->refcount = 0;
102
 
1174 jermar 103
    ta->capabilities = 0;
1579 jermar 104
    ta->accept_new_threads = true;
1040 palkovsky 105
 
955 palkovsky 106
    ipc_answerbox_init(&ta->answerbox);
1040 palkovsky 107
    for (i=0; i < IPC_MAX_PHONES;i++)
108
        ipc_phone_init(&ta->phones[i]);
965 palkovsky 109
    if (ipc_phone_0)
1040 palkovsky 110
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
998 palkovsky 111
    atomic_set(&ta->active_calls, 0);
1460 jermar 112
 
113
    mutex_initialize(&ta->futexes_lock);
114
    btree_create(&ta->futexes);
822 palkovsky 115
 
116
    ipl = interrupts_disable();
1468 jermar 117
 
118
    /*
119
     * Increment address space reference count.
120
     * TODO: Reconsider the locking scheme.
121
     */
122
    mutex_lock(&as->lock);
123
    as->refcount++;
124
    mutex_unlock(&as->lock);
125
 
822 palkovsky 126
    spinlock_lock(&tasks_lock);
1005 palkovsky 127
 
128
    ta->taskid = ++task_counter;
1177 jermar 129
    btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
1005 palkovsky 130
 
822 palkovsky 131
    spinlock_unlock(&tasks_lock);
132
    interrupts_restore(ipl);
133
 
1 jermar 134
    return ta;
135
}
136
 
1579 jermar 137
/** Destroy task.
138
 *
139
 * @param t Task to be destroyed.
140
 */
141
void task_destroy(task_t *t)
142
{
1587 jermar 143
    task_destroy_arch(t);
144
    btree_destroy(&t->futexes);
145
 
146
    mutex_lock_active(&t->as->lock);
147
    if (--t->as->refcount == 0) {
148
        mutex_unlock(&t->as->lock);
149
        as_destroy(t->as);
150
        /*
151
         * t->as is destroyed.
152
         */
153
    } else {
154
        mutex_unlock(&t->as->lock);
155
    }
156
 
157
    free(t);
158
    TASK = NULL;
1579 jermar 159
}
160
 
973 palkovsky 161
/** Create new task with 1 thread and run it
162
 *
1248 jermar 163
 * @param program_addr Address of program executable image.
1062 jermar 164
 * @param name Program name.
165
 *
1229 jermar 166
 * @return Task of the running program or NULL on error.
973 palkovsky 167
 */
1062 jermar 168
task_t * task_run_program(void *program_addr, char *name)
973 palkovsky 169
{
170
    as_t *as;
171
    as_area_t *a;
172
    int rc;
1585 jermar 173
    thread_t *t1, *t2;
973 palkovsky 174
    task_t *task;
1078 jermar 175
    uspace_arg_t *kernel_uarg;
973 palkovsky 176
 
177
    as = as_create(0);
1115 jermar 178
    ASSERT(as);
973 palkovsky 179
 
1025 palkovsky 180
    rc = elf_load((elf_header_t *) program_addr, as);
973 palkovsky 181
    if (rc != EE_OK) {
1468 jermar 182
        as_destroy(as);
973 palkovsky 183
        return NULL;
184
    }
185
 
1078 jermar 186
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
187
    kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
188
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
189
    kernel_uarg->uspace_thread_function = NULL;
190
    kernel_uarg->uspace_thread_arg = NULL;
191
    kernel_uarg->uspace_uarg = NULL;
1066 jermar 192
 
1062 jermar 193
    task = task_create(as, name);
1115 jermar 194
    ASSERT(task);
195
 
973 palkovsky 196
    /*
197
     * Create the data as_area.
198
     */
1424 jermar 199
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
200
        LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
1409 jermar 201
        USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
1115 jermar 202
 
1585 jermar 203
    /*
204
     * Create the main thread.
205
     */
206
    t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
207
    ASSERT(t1);
973 palkovsky 208
 
1585 jermar 209
    /*
210
     * Create killer thread for the new task.
211
     */
212
    t2 = thread_create(ktaskkill, t1, task, 0, "ktaskkill");
213
    ASSERT(t2);
214
    thread_ready(t2);
215
 
216
    thread_ready(t1);
217
 
973 palkovsky 218
    return task;
219
}
1060 palkovsky 220
 
1176 jermar 221
/** Syscall for reading task ID from userspace.
222
 *
1248 jermar 223
 * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
1176 jermar 224
 *
1288 jermar 225
 * @return 0 on success or an error code from @ref errno.h.
1176 jermar 226
 */
1227 jermar 227
__native sys_task_get_id(task_id_t *uspace_task_id)
1176 jermar 228
{
229
    /*
230
     * No need to acquire lock on TASK because taskid
231
     * remains constant for the lifespan of the task.
232
     */
1288 jermar 233
    return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
1176 jermar 234
}
235
 
1178 jermar 236
/** Find task structure corresponding to task ID.
237
 *
238
 * The tasks_lock must be already held by the caller of this function
239
 * and interrupts must be disabled.
240
 *
241
 * @param id Task ID.
242
 *
243
 * @return Task structure address or NULL if there is no such task ID.
244
 */
245
task_t *task_find_by_id(task_id_t id)
246
{
247
    btree_node_t *leaf;
248
 
249
    return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
250
}
251
 
1579 jermar 252
/** Kill task.
253
 *
254
 * @param id ID of the task to be killed.
255
 *
256
 * @return 0 on success or an error code from errno.h
257
 */
258
int task_kill(task_id_t id)
259
{
260
    ipl_t ipl;
261
    task_t *ta;
262
    thread_t *t;
263
    link_t *cur;
264
 
265
    ipl = interrupts_disable();
266
    spinlock_lock(&tasks_lock);
267
 
268
    if (!(ta = task_find_by_id(id))) {
269
        spinlock_unlock(&tasks_lock);
270
        interrupts_restore(ipl);
271
        return ENOENT;
272
    }
1588 jermar 273
 
1579 jermar 274
    spinlock_lock(&ta->lock);
275
    ta->refcount++;
276
    spinlock_unlock(&ta->lock);
1587 jermar 277
 
1588 jermar 278
    btree_remove(&tasks_btree, ta->taskid, NULL);
1587 jermar 279
    spinlock_unlock(&tasks_lock);
1579 jermar 280
 
1580 jermar 281
    t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
1579 jermar 282
 
283
    spinlock_lock(&ta->lock);
1580 jermar 284
    ta->accept_new_threads = false;
1579 jermar 285
    ta->refcount--;
1585 jermar 286
 
287
    /*
288
     * Interrupt all threads except this one.
289
     */
1579 jermar 290
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
291
        thread_t *thr;
292
        bool  sleeping = false;
293
 
294
        thr = list_get_instance(cur, thread_t, th_link);
295
        if (thr == t)
296
            continue;
297
 
298
        spinlock_lock(&thr->lock);
299
        thr->interrupted = true;
300
        if (thr->state == Sleeping)
301
            sleeping = true;
302
        spinlock_unlock(&thr->lock);
303
 
304
        if (sleeping)
305
            waitq_interrupt_sleep(thr);
306
    }
307
 
1580 jermar 308
    spinlock_unlock(&ta->lock);
309
    interrupts_restore(ipl);
1579 jermar 310
 
1580 jermar 311
    if (t)
312
        thread_ready(t);
313
 
1579 jermar 314
    return 0;
315
}
316
 
1060 palkovsky 317
/** Print task list */
318
void task_print_list(void)
319
{
320
    link_t *cur;
321
    ipl_t ipl;
322
 
323
    /* Messing with thread structures, avoid deadlock */
324
    ipl = interrupts_disable();
325
    spinlock_lock(&tasks_lock);
326
 
1159 jermar 327
    for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
328
        btree_node_t *node;
329
        int i;
330
 
331
        node = list_get_instance(cur, btree_node_t, leaf_link);
332
        for (i = 0; i < node->keys; i++) {
333
            task_t *t;
334
            int j;
335
 
336
            t = (task_t *) node->value[i];
337
 
338
            spinlock_lock(&t->lock);
1573 palkovsky 339
            printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
340
                t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
1159 jermar 341
            for (j=0; j < IPC_MAX_PHONES; j++) {
342
                if (t->phones[j].callee)
1196 cejka 343
                    printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
1159 jermar 344
            }
345
            printf("\n");
346
            spinlock_unlock(&t->lock);
1060 palkovsky 347
        }
348
    }
349
 
350
    spinlock_unlock(&tasks_lock);
351
    interrupts_restore(ipl);
352
}
1579 jermar 353
 
1585 jermar 354
/** Kernel thread used to cleanup the task after it is killed. */
1580 jermar 355
void ktaskclnp(void *arg)
1579 jermar 356
{
1580 jermar 357
    ipl_t ipl;
1585 jermar 358
    thread_t *t = NULL, *main_thread;
1580 jermar 359
    link_t *cur;
360
 
361
    thread_detach(THREAD);
362
 
363
loop:
364
    ipl = interrupts_disable();
365
    spinlock_lock(&TASK->lock);
366
 
1585 jermar 367
    main_thread = TASK->main_thread;
368
 
1579 jermar 369
    /*
1580 jermar 370
     * Find a thread to join.
371
     */
372
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
373
        t = list_get_instance(cur, thread_t, th_link);
374
        if (t == THREAD)
375
            continue;
1585 jermar 376
        else if (t == main_thread)
377
            continue;
1580 jermar 378
        else
379
            break;
380
    }
381
 
382
    spinlock_unlock(&TASK->lock);
383
    interrupts_restore(ipl);
384
 
385
    if (t != THREAD) {
1585 jermar 386
        ASSERT(t != main_thread);   /* uninit is joined and detached in ktaskkill */
1580 jermar 387
        thread_join(t);
388
        thread_detach(t);
1585 jermar 389
        goto loop;  /* go for another thread */
1580 jermar 390
    }
391
 
392
    /*
393
     * Now there are no other threads in this task
394
     * and no new threads can be created.
395
     */
396
 
1583 jermar 397
    ipc_cleanup();
398
    futex_cleanup();
1579 jermar 399
}
1585 jermar 400
 
401
/** Kernel task used to kill a userspace task when its main thread exits.
402
 *
403
 * This thread waits until the main userspace thread (i.e. uninit) exits.
404
 * When this happens, the task is killed.
405
 *
406
 * @param arg Pointer to the thread structure of the task's main thread.
407
 */
408
void ktaskkill(void *arg)
409
{
410
    thread_t *t = (thread_t *) arg;
411
 
412
    /*
413
     * Userspace threads cannot detach themselves,
414
     * therefore the thread pointer is guaranteed to be valid.
415
     */
416
    thread_join(t); /* sleep uninterruptibly here! */
417
    thread_detach(t);
418
    task_kill(TASK->taskid);
419
}