[f761f1eb] | 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 |
|
---|
[5be1923] | 29 | #include <main/uinit.h>
|
---|
[f761f1eb] | 30 | #include <proc/thread.h>
|
---|
| 31 | #include <proc/task.h>
|
---|
[0f250f9] | 32 | #include <proc/uarg.h>
|
---|
[20d50a1] | 33 | #include <mm/as.h>
|
---|
[085d973] | 34 | #include <mm/slab.h>
|
---|
[f761f1eb] | 35 | #include <synch/spinlock.h>
|
---|
| 36 | #include <arch.h>
|
---|
| 37 | #include <panic.h>
|
---|
[7f6e755] | 38 | #include <adt/btree.h>
|
---|
[5c9a08b] | 39 | #include <adt/list.h>
|
---|
[6d9c49a] | 40 | #include <ipc/ipc.h>
|
---|
| 41 | #include <memstr.h>
|
---|
[37c57f2] | 42 | #include <print.h>
|
---|
[5be1923] | 43 | #include <elf.h>
|
---|
| 44 |
|
---|
[8e5e78f] | 45 |
|
---|
| 46 | #define LOADED_PROG_STACK_PAGES_NO 2
|
---|
| 47 |
|
---|
[dc747e3] | 48 | SPINLOCK_INITIALIZE(tasks_lock);
|
---|
[7f6e755] | 49 | btree_t tasks_btree;
|
---|
[286e03d] | 50 | static task_id_t task_counter = 0;
|
---|
[70527f1] | 51 |
|
---|
| 52 | /** Initialize tasks
|
---|
| 53 | *
|
---|
| 54 | * Initialize kernel tasks support.
|
---|
| 55 | *
|
---|
| 56 | */
|
---|
[f761f1eb] | 57 | void task_init(void)
|
---|
| 58 | {
|
---|
[43114c5] | 59 | TASK = NULL;
|
---|
[7f6e755] | 60 | btree_create(&tasks_btree);
|
---|
[f761f1eb] | 61 | }
|
---|
| 62 |
|
---|
[70527f1] | 63 |
|
---|
| 64 | /** Create new task
|
---|
| 65 | *
|
---|
| 66 | * Create new task with no threads.
|
---|
| 67 | *
|
---|
[20d50a1] | 68 | * @param as Task's address space.
|
---|
[ff14c520] | 69 | * @param name Symbolic name.
|
---|
[70527f1] | 70 | *
|
---|
[5be1923] | 71 | * @return New task's structure
|
---|
[70527f1] | 72 | *
|
---|
| 73 | */
|
---|
[ff14c520] | 74 | task_t *task_create(as_t *as, char *name)
|
---|
[f761f1eb] | 75 | {
|
---|
[22f7769] | 76 | ipl_t ipl;
|
---|
[f761f1eb] | 77 | task_t *ta;
|
---|
[2ba7810] | 78 | int i;
|
---|
[f761f1eb] | 79 |
|
---|
[bb68433] | 80 | ta = (task_t *) malloc(sizeof(task_t), 0);
|
---|
| 81 |
|
---|
| 82 | spinlock_initialize(&ta->lock, "task_ta_lock");
|
---|
| 83 | list_initialize(&ta->th_head);
|
---|
| 84 | ta->as = as;
|
---|
[ff14c520] | 85 | ta->name = name;
|
---|
[6d9c49a] | 86 |
|
---|
[2ba7810] | 87 |
|
---|
[6d9c49a] | 88 | ipc_answerbox_init(&ta->answerbox);
|
---|
[2ba7810] | 89 | for (i=0; i < IPC_MAX_PHONES;i++)
|
---|
| 90 | ipc_phone_init(&ta->phones[i]);
|
---|
[e74cb73] | 91 | if (ipc_phone_0)
|
---|
[2ba7810] | 92 | ipc_phone_connect(&ta->phones[0], ipc_phone_0);
|
---|
[5f62ef9] | 93 | atomic_set(&ta->active_calls, 0);
|
---|
[bb68433] | 94 |
|
---|
| 95 | ipl = interrupts_disable();
|
---|
| 96 | spinlock_lock(&tasks_lock);
|
---|
[286e03d] | 97 |
|
---|
| 98 | ta->taskid = ++task_counter;
|
---|
[7f6e755] | 99 | btree_insert(&tasks_btree, (__native) ta, (void *) ta, NULL);
|
---|
[286e03d] | 100 |
|
---|
[bb68433] | 101 | spinlock_unlock(&tasks_lock);
|
---|
| 102 | interrupts_restore(ipl);
|
---|
| 103 |
|
---|
[f761f1eb] | 104 | return ta;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[5be1923] | 107 | /** Create new task with 1 thread and run it
|
---|
[ff14c520] | 108 | *
|
---|
| 109 | * @param programe_addr Address of program executable image.
|
---|
| 110 | * @param name Program name.
|
---|
[5be1923] | 111 | *
|
---|
| 112 | * @return Task of the running program or NULL on error
|
---|
| 113 | */
|
---|
[ff14c520] | 114 | task_t * task_run_program(void *program_addr, char *name)
|
---|
[5be1923] | 115 | {
|
---|
| 116 | as_t *as;
|
---|
| 117 | as_area_t *a;
|
---|
| 118 | int rc;
|
---|
| 119 | thread_t *t;
|
---|
| 120 | task_t *task;
|
---|
[0f250f9] | 121 | uspace_arg_t *kernel_uarg;
|
---|
[5be1923] | 122 |
|
---|
| 123 | as = as_create(0);
|
---|
[a0bb10ef] | 124 | ASSERT(as);
|
---|
[5be1923] | 125 |
|
---|
[649799a] | 126 | rc = elf_load((elf_header_t *) program_addr, as);
|
---|
[5be1923] | 127 | if (rc != EE_OK) {
|
---|
| 128 | as_free(as);
|
---|
| 129 | return NULL;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[0f250f9] | 132 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
|
---|
| 133 | kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
|
---|
| 134 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
|
---|
| 135 | kernel_uarg->uspace_thread_function = NULL;
|
---|
| 136 | kernel_uarg->uspace_thread_arg = NULL;
|
---|
| 137 | kernel_uarg->uspace_uarg = NULL;
|
---|
[9f52563] | 138 |
|
---|
[ff14c520] | 139 | task = task_create(as, name);
|
---|
[a0bb10ef] | 140 | ASSERT(task);
|
---|
| 141 |
|
---|
[5be1923] | 142 | /*
|
---|
| 143 | * Create the data as_area.
|
---|
| 144 | */
|
---|
[8e5e78f] | 145 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE, LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE, USTACK_ADDRESS);
|
---|
[5be1923] | 146 |
|
---|
[a0bb10ef] | 147 | t = thread_create(uinit, kernel_uarg, task, 0, "uinit");
|
---|
| 148 | ASSERT(t);
|
---|
| 149 | thread_ready(t);
|
---|
| 150 |
|
---|
[5be1923] | 151 | return task;
|
---|
| 152 | }
|
---|
[37c57f2] | 153 |
|
---|
| 154 | /** Print task list */
|
---|
| 155 | void task_print_list(void)
|
---|
| 156 | {
|
---|
| 157 | link_t *cur;
|
---|
| 158 | ipl_t ipl;
|
---|
| 159 |
|
---|
| 160 | /* Messing with thread structures, avoid deadlock */
|
---|
| 161 | ipl = interrupts_disable();
|
---|
| 162 | spinlock_lock(&tasks_lock);
|
---|
| 163 |
|
---|
[7f6e755] | 164 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
|
---|
| 165 | btree_node_t *node;
|
---|
| 166 | int i;
|
---|
| 167 |
|
---|
| 168 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 169 | for (i = 0; i < node->keys; i++) {
|
---|
| 170 | task_t *t;
|
---|
| 171 | int j;
|
---|
| 172 |
|
---|
| 173 | t = (task_t *) node->value[i];
|
---|
| 174 |
|
---|
| 175 | spinlock_lock(&t->lock);
|
---|
| 176 | printf("%s: address=%P, taskid=%Q, as=%P, ActiveCalls: %d",
|
---|
| 177 | t->name, t, t->taskid, t->as, atomic_get(&t->active_calls));
|
---|
| 178 | for (j=0; j < IPC_MAX_PHONES; j++) {
|
---|
| 179 | if (t->phones[j].callee)
|
---|
| 180 | printf(" Ph(%d): %P ", j, t->phones[j].callee);
|
---|
| 181 | }
|
---|
| 182 | printf("\n");
|
---|
| 183 | spinlock_unlock(&t->lock);
|
---|
[37c57f2] | 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | spinlock_unlock(&tasks_lock);
|
---|
| 188 | interrupts_restore(ipl);
|
---|
| 189 |
|
---|
| 190 | }
|
---|