Subversion Repositories HelenOS

Rev

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

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