[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>
|
---|
[1077d91] | 41 | #include <security/cap.h>
|
---|
[6d9c49a] | 42 | #include <memstr.h>
|
---|
[37c57f2] | 43 | #include <print.h>
|
---|
[5be1923] | 44 | #include <elf.h>
|
---|
| 45 |
|
---|
[8e5e78f] | 46 |
|
---|
[a84af84] | 47 | #ifndef LOADED_PROG_STACK_PAGES_NO
|
---|
| 48 | #define LOADED_PROG_STACK_PAGES_NO 1
|
---|
| 49 | #endif
|
---|
[8e5e78f] | 50 |
|
---|
[dc747e3] | 51 | SPINLOCK_INITIALIZE(tasks_lock);
|
---|
[7f6e755] | 52 | btree_t tasks_btree;
|
---|
[286e03d] | 53 | static task_id_t task_counter = 0;
|
---|
[70527f1] | 54 |
|
---|
| 55 | /** Initialize tasks
|
---|
| 56 | *
|
---|
| 57 | * Initialize kernel tasks support.
|
---|
| 58 | *
|
---|
| 59 | */
|
---|
[f761f1eb] | 60 | void task_init(void)
|
---|
| 61 | {
|
---|
[43114c5] | 62 | TASK = NULL;
|
---|
[7f6e755] | 63 | btree_create(&tasks_btree);
|
---|
[f761f1eb] | 64 | }
|
---|
| 65 |
|
---|
[70527f1] | 66 |
|
---|
| 67 | /** Create new task
|
---|
| 68 | *
|
---|
| 69 | * Create new task with no threads.
|
---|
| 70 | *
|
---|
[20d50a1] | 71 | * @param as Task's address space.
|
---|
[ff14c520] | 72 | * @param name Symbolic name.
|
---|
[70527f1] | 73 | *
|
---|
[5be1923] | 74 | * @return New task's structure
|
---|
[70527f1] | 75 | *
|
---|
| 76 | */
|
---|
[ff14c520] | 77 | task_t *task_create(as_t *as, char *name)
|
---|
[f761f1eb] | 78 | {
|
---|
[22f7769] | 79 | ipl_t ipl;
|
---|
[f761f1eb] | 80 | task_t *ta;
|
---|
[2ba7810] | 81 | int i;
|
---|
[f761f1eb] | 82 |
|
---|
[bb68433] | 83 | ta = (task_t *) malloc(sizeof(task_t), 0);
|
---|
| 84 |
|
---|
[963074b3] | 85 | task_create_arch(ta);
|
---|
| 86 |
|
---|
[bb68433] | 87 | spinlock_initialize(&ta->lock, "task_ta_lock");
|
---|
| 88 | list_initialize(&ta->th_head);
|
---|
| 89 | ta->as = as;
|
---|
[ff14c520] | 90 | ta->name = name;
|
---|
[6d9c49a] | 91 |
|
---|
[1077d91] | 92 | ta->capabilities = 0;
|
---|
[2ba7810] | 93 |
|
---|
[6d9c49a] | 94 | ipc_answerbox_init(&ta->answerbox);
|
---|
[2ba7810] | 95 | for (i=0; i < IPC_MAX_PHONES;i++)
|
---|
| 96 | ipc_phone_init(&ta->phones[i]);
|
---|
[e74cb73] | 97 | if (ipc_phone_0)
|
---|
[2ba7810] | 98 | ipc_phone_connect(&ta->phones[0], ipc_phone_0);
|
---|
[5f62ef9] | 99 | atomic_set(&ta->active_calls, 0);
|
---|
[bb68433] | 100 |
|
---|
| 101 | ipl = interrupts_disable();
|
---|
| 102 | spinlock_lock(&tasks_lock);
|
---|
[286e03d] | 103 |
|
---|
| 104 | ta->taskid = ++task_counter;
|
---|
[b7f364e] | 105 | btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
|
---|
[286e03d] | 106 |
|
---|
[bb68433] | 107 | spinlock_unlock(&tasks_lock);
|
---|
| 108 | interrupts_restore(ipl);
|
---|
| 109 |
|
---|
[f761f1eb] | 110 | return ta;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[5be1923] | 113 | /** Create new task with 1 thread and run it
|
---|
[ff14c520] | 114 | *
|
---|
| 115 | * @param programe_addr Address of program executable image.
|
---|
| 116 | * @param name Program name.
|
---|
[5be1923] | 117 | *
|
---|
| 118 | * @return Task of the running program or NULL on error
|
---|
| 119 | */
|
---|
[ff14c520] | 120 | task_t * task_run_program(void *program_addr, char *name)
|
---|
[5be1923] | 121 | {
|
---|
| 122 | as_t *as;
|
---|
| 123 | as_area_t *a;
|
---|
| 124 | int rc;
|
---|
| 125 | thread_t *t;
|
---|
| 126 | task_t *task;
|
---|
[0f250f9] | 127 | uspace_arg_t *kernel_uarg;
|
---|
[5be1923] | 128 |
|
---|
| 129 | as = as_create(0);
|
---|
[a0bb10ef] | 130 | ASSERT(as);
|
---|
[5be1923] | 131 |
|
---|
[649799a] | 132 | rc = elf_load((elf_header_t *) program_addr, as);
|
---|
[5be1923] | 133 | if (rc != EE_OK) {
|
---|
| 134 | as_free(as);
|
---|
| 135 | return NULL;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[0f250f9] | 138 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
|
---|
| 139 | kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
|
---|
| 140 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
|
---|
| 141 | kernel_uarg->uspace_thread_function = NULL;
|
---|
| 142 | kernel_uarg->uspace_thread_arg = NULL;
|
---|
| 143 | kernel_uarg->uspace_uarg = NULL;
|
---|
[9f52563] | 144 |
|
---|
[ff14c520] | 145 | task = task_create(as, name);
|
---|
[a0bb10ef] | 146 | ASSERT(task);
|
---|
| 147 |
|
---|
[5be1923] | 148 | /*
|
---|
| 149 | * Create the data as_area.
|
---|
| 150 | */
|
---|
[8e5e78f] | 151 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE, LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE, USTACK_ADDRESS);
|
---|
[5be1923] | 152 |
|
---|
[a0bb10ef] | 153 | t = thread_create(uinit, kernel_uarg, task, 0, "uinit");
|
---|
| 154 | ASSERT(t);
|
---|
| 155 | thread_ready(t);
|
---|
| 156 |
|
---|
[5be1923] | 157 | return task;
|
---|
| 158 | }
|
---|
[37c57f2] | 159 |
|
---|
[ec55358] | 160 | /** Syscall for reading task ID from userspace.
|
---|
| 161 | *
|
---|
| 162 | * @param uaddr Userspace address of 8-byte buffer where to store current task ID.
|
---|
| 163 | *
|
---|
| 164 | * @return Always returns 0.
|
---|
| 165 | */
|
---|
| 166 | __native sys_get_task_id(task_id_t *uspace_task_id)
|
---|
| 167 | {
|
---|
| 168 | /*
|
---|
| 169 | * No need to acquire lock on TASK because taskid
|
---|
| 170 | * remains constant for the lifespan of the task.
|
---|
| 171 | */
|
---|
| 172 | copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
|
---|
| 173 |
|
---|
| 174 | return 0;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[9a8d91b] | 177 | /** Find task structure corresponding to task ID.
|
---|
| 178 | *
|
---|
| 179 | * The tasks_lock must be already held by the caller of this function
|
---|
| 180 | * and interrupts must be disabled.
|
---|
| 181 | *
|
---|
| 182 | * @param id Task ID.
|
---|
| 183 | *
|
---|
| 184 | * @return Task structure address or NULL if there is no such task ID.
|
---|
| 185 | */
|
---|
| 186 | task_t *task_find_by_id(task_id_t id)
|
---|
| 187 | {
|
---|
| 188 | btree_node_t *leaf;
|
---|
| 189 |
|
---|
| 190 | return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[37c57f2] | 193 | /** Print task list */
|
---|
| 194 | void task_print_list(void)
|
---|
| 195 | {
|
---|
| 196 | link_t *cur;
|
---|
| 197 | ipl_t ipl;
|
---|
| 198 |
|
---|
| 199 | /* Messing with thread structures, avoid deadlock */
|
---|
| 200 | ipl = interrupts_disable();
|
---|
| 201 | spinlock_lock(&tasks_lock);
|
---|
| 202 |
|
---|
[7f6e755] | 203 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
|
---|
| 204 | btree_node_t *node;
|
---|
| 205 | int i;
|
---|
| 206 |
|
---|
| 207 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 208 | for (i = 0; i < node->keys; i++) {
|
---|
| 209 | task_t *t;
|
---|
| 210 | int j;
|
---|
| 211 |
|
---|
| 212 | t = (task_t *) node->value[i];
|
---|
| 213 |
|
---|
| 214 | spinlock_lock(&t->lock);
|
---|
[280a27e] | 215 | printf("%s: address=%#zX, taskid=%#llX, as=%#zX, ActiveCalls: %zd",
|
---|
[7f6e755] | 216 | t->name, t, t->taskid, t->as, atomic_get(&t->active_calls));
|
---|
| 217 | for (j=0; j < IPC_MAX_PHONES; j++) {
|
---|
| 218 | if (t->phones[j].callee)
|
---|
[280a27e] | 219 | printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
|
---|
[7f6e755] | 220 | }
|
---|
| 221 | printf("\n");
|
---|
| 222 | spinlock_unlock(&t->lock);
|
---|
[37c57f2] | 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | spinlock_unlock(&tasks_lock);
|
---|
| 227 | interrupts_restore(ipl);
|
---|
| 228 |
|
---|
| 229 | }
|
---|