[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 |
|
---|
[9179d0a] | 29 | /**
|
---|
| 30 | * @file task.c
|
---|
| 31 | * @brief Task management.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[5be1923] | 34 | #include <main/uinit.h>
|
---|
[f761f1eb] | 35 | #include <proc/thread.h>
|
---|
| 36 | #include <proc/task.h>
|
---|
[0f250f9] | 37 | #include <proc/uarg.h>
|
---|
[20d50a1] | 38 | #include <mm/as.h>
|
---|
[085d973] | 39 | #include <mm/slab.h>
|
---|
[f761f1eb] | 40 | #include <synch/spinlock.h>
|
---|
| 41 | #include <arch.h>
|
---|
| 42 | #include <panic.h>
|
---|
[7f6e755] | 43 | #include <adt/btree.h>
|
---|
[5c9a08b] | 44 | #include <adt/list.h>
|
---|
[6d9c49a] | 45 | #include <ipc/ipc.h>
|
---|
[1077d91] | 46 | #include <security/cap.h>
|
---|
[6d9c49a] | 47 | #include <memstr.h>
|
---|
[37c57f2] | 48 | #include <print.h>
|
---|
[5be1923] | 49 | #include <elf.h>
|
---|
[7509ddc] | 50 | #include <errno.h>
|
---|
[e3c762cd] | 51 | #include <syscall/copy.h>
|
---|
[8e5e78f] | 52 |
|
---|
[a84af84] | 53 | #ifndef LOADED_PROG_STACK_PAGES_NO
|
---|
| 54 | #define LOADED_PROG_STACK_PAGES_NO 1
|
---|
| 55 | #endif
|
---|
[8e5e78f] | 56 |
|
---|
[dc747e3] | 57 | SPINLOCK_INITIALIZE(tasks_lock);
|
---|
[7f6e755] | 58 | btree_t tasks_btree;
|
---|
[286e03d] | 59 | static task_id_t task_counter = 0;
|
---|
[70527f1] | 60 |
|
---|
[34dcd3f] | 61 | static void ktaskclnp(void *);
|
---|
[7509ddc] | 62 |
|
---|
[70527f1] | 63 | /** Initialize tasks
|
---|
| 64 | *
|
---|
| 65 | * Initialize kernel tasks support.
|
---|
| 66 | *
|
---|
| 67 | */
|
---|
[f761f1eb] | 68 | void task_init(void)
|
---|
| 69 | {
|
---|
[43114c5] | 70 | TASK = NULL;
|
---|
[7f6e755] | 71 | btree_create(&tasks_btree);
|
---|
[f761f1eb] | 72 | }
|
---|
| 73 |
|
---|
[70527f1] | 74 |
|
---|
| 75 | /** Create new task
|
---|
| 76 | *
|
---|
| 77 | * Create new task with no threads.
|
---|
| 78 | *
|
---|
[20d50a1] | 79 | * @param as Task's address space.
|
---|
[ff14c520] | 80 | * @param name Symbolic name.
|
---|
[70527f1] | 81 | *
|
---|
[5be1923] | 82 | * @return New task's structure
|
---|
[70527f1] | 83 | *
|
---|
| 84 | */
|
---|
[ff14c520] | 85 | task_t *task_create(as_t *as, char *name)
|
---|
[f761f1eb] | 86 | {
|
---|
[22f7769] | 87 | ipl_t ipl;
|
---|
[f761f1eb] | 88 | task_t *ta;
|
---|
[2ba7810] | 89 | int i;
|
---|
[f761f1eb] | 90 |
|
---|
[bb68433] | 91 | ta = (task_t *) malloc(sizeof(task_t), 0);
|
---|
| 92 |
|
---|
[963074b3] | 93 | task_create_arch(ta);
|
---|
| 94 |
|
---|
[bb68433] | 95 | spinlock_initialize(&ta->lock, "task_ta_lock");
|
---|
| 96 | list_initialize(&ta->th_head);
|
---|
| 97 | ta->as = as;
|
---|
[ff14c520] | 98 | ta->name = name;
|
---|
[6d9c49a] | 99 |
|
---|
[7509ddc] | 100 | ta->refcount = 0;
|
---|
| 101 |
|
---|
[1077d91] | 102 | ta->capabilities = 0;
|
---|
[7509ddc] | 103 | ta->accept_new_threads = true;
|
---|
[2ba7810] | 104 |
|
---|
[6d9c49a] | 105 | ipc_answerbox_init(&ta->answerbox);
|
---|
[2ba7810] | 106 | for (i=0; i < IPC_MAX_PHONES;i++)
|
---|
| 107 | ipc_phone_init(&ta->phones[i]);
|
---|
[e74cb73] | 108 | if (ipc_phone_0)
|
---|
[2ba7810] | 109 | ipc_phone_connect(&ta->phones[0], ipc_phone_0);
|
---|
[5f62ef9] | 110 | atomic_set(&ta->active_calls, 0);
|
---|
[4fded58] | 111 |
|
---|
| 112 | mutex_initialize(&ta->futexes_lock);
|
---|
| 113 | btree_create(&ta->futexes);
|
---|
[bb68433] | 114 |
|
---|
| 115 | ipl = interrupts_disable();
|
---|
[482826d] | 116 |
|
---|
| 117 | /*
|
---|
| 118 | * Increment address space reference count.
|
---|
| 119 | * TODO: Reconsider the locking scheme.
|
---|
| 120 | */
|
---|
| 121 | mutex_lock(&as->lock);
|
---|
| 122 | as->refcount++;
|
---|
| 123 | mutex_unlock(&as->lock);
|
---|
| 124 |
|
---|
[bb68433] | 125 | spinlock_lock(&tasks_lock);
|
---|
[286e03d] | 126 |
|
---|
| 127 | ta->taskid = ++task_counter;
|
---|
[b7f364e] | 128 | btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
|
---|
[286e03d] | 129 |
|
---|
[bb68433] | 130 | spinlock_unlock(&tasks_lock);
|
---|
| 131 | interrupts_restore(ipl);
|
---|
| 132 |
|
---|
[f761f1eb] | 133 | return ta;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[7509ddc] | 136 | /** Destroy task.
|
---|
| 137 | *
|
---|
| 138 | * @param t Task to be destroyed.
|
---|
| 139 | */
|
---|
| 140 | void task_destroy(task_t *t)
|
---|
| 141 | {
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[5be1923] | 144 | /** Create new task with 1 thread and run it
|
---|
[ff14c520] | 145 | *
|
---|
[9179d0a] | 146 | * @param program_addr Address of program executable image.
|
---|
[ff14c520] | 147 | * @param name Program name.
|
---|
[5be1923] | 148 | *
|
---|
[7f0837c] | 149 | * @return Task of the running program or NULL on error.
|
---|
[5be1923] | 150 | */
|
---|
[ff14c520] | 151 | task_t * task_run_program(void *program_addr, char *name)
|
---|
[5be1923] | 152 | {
|
---|
| 153 | as_t *as;
|
---|
| 154 | as_area_t *a;
|
---|
| 155 | int rc;
|
---|
| 156 | thread_t *t;
|
---|
| 157 | task_t *task;
|
---|
[0f250f9] | 158 | uspace_arg_t *kernel_uarg;
|
---|
[5be1923] | 159 |
|
---|
| 160 | as = as_create(0);
|
---|
[a0bb10ef] | 161 | ASSERT(as);
|
---|
[5be1923] | 162 |
|
---|
[649799a] | 163 | rc = elf_load((elf_header_t *) program_addr, as);
|
---|
[5be1923] | 164 | if (rc != EE_OK) {
|
---|
[482826d] | 165 | as_destroy(as);
|
---|
[5be1923] | 166 | return NULL;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[0f250f9] | 169 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
|
---|
| 170 | kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
|
---|
| 171 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
|
---|
| 172 | kernel_uarg->uspace_thread_function = NULL;
|
---|
| 173 | kernel_uarg->uspace_thread_arg = NULL;
|
---|
| 174 | kernel_uarg->uspace_uarg = NULL;
|
---|
[9f52563] | 175 |
|
---|
[ff14c520] | 176 | task = task_create(as, name);
|
---|
[a0bb10ef] | 177 | ASSERT(task);
|
---|
| 178 |
|
---|
[5be1923] | 179 | /*
|
---|
| 180 | * Create the data as_area.
|
---|
| 181 | */
|
---|
[0ee077ee] | 182 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
|
---|
| 183 | LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
|
---|
[8182031] | 184 | USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
|
---|
[5be1923] | 185 |
|
---|
[a0bb10ef] | 186 | t = thread_create(uinit, kernel_uarg, task, 0, "uinit");
|
---|
| 187 | ASSERT(t);
|
---|
| 188 | thread_ready(t);
|
---|
| 189 |
|
---|
[5be1923] | 190 | return task;
|
---|
| 191 | }
|
---|
[37c57f2] | 192 |
|
---|
[ec55358] | 193 | /** Syscall for reading task ID from userspace.
|
---|
| 194 | *
|
---|
[9179d0a] | 195 | * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
|
---|
[ec55358] | 196 | *
|
---|
[e3c762cd] | 197 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[ec55358] | 198 | */
|
---|
[24f3874] | 199 | __native sys_task_get_id(task_id_t *uspace_task_id)
|
---|
[ec55358] | 200 | {
|
---|
| 201 | /*
|
---|
| 202 | * No need to acquire lock on TASK because taskid
|
---|
| 203 | * remains constant for the lifespan of the task.
|
---|
| 204 | */
|
---|
[e3c762cd] | 205 | return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
|
---|
[ec55358] | 206 | }
|
---|
| 207 |
|
---|
[9a8d91b] | 208 | /** Find task structure corresponding to task ID.
|
---|
| 209 | *
|
---|
| 210 | * The tasks_lock must be already held by the caller of this function
|
---|
| 211 | * and interrupts must be disabled.
|
---|
| 212 | *
|
---|
| 213 | * @param id Task ID.
|
---|
| 214 | *
|
---|
| 215 | * @return Task structure address or NULL if there is no such task ID.
|
---|
| 216 | */
|
---|
| 217 | task_t *task_find_by_id(task_id_t id)
|
---|
| 218 | {
|
---|
| 219 | btree_node_t *leaf;
|
---|
| 220 |
|
---|
| 221 | return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[7509ddc] | 224 | /** Kill task.
|
---|
| 225 | *
|
---|
| 226 | * @param id ID of the task to be killed.
|
---|
| 227 | *
|
---|
| 228 | * @return 0 on success or an error code from errno.h
|
---|
| 229 | */
|
---|
| 230 | int task_kill(task_id_t id)
|
---|
| 231 | {
|
---|
| 232 | ipl_t ipl;
|
---|
| 233 | task_t *ta;
|
---|
| 234 | thread_t *t;
|
---|
| 235 | link_t *cur;
|
---|
| 236 |
|
---|
| 237 | ipl = interrupts_disable();
|
---|
| 238 | spinlock_lock(&tasks_lock);
|
---|
| 239 |
|
---|
| 240 | if (!(ta = task_find_by_id(id))) {
|
---|
| 241 | spinlock_unlock(&tasks_lock);
|
---|
| 242 | interrupts_restore(ipl);
|
---|
| 243 | return ENOENT;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | spinlock_lock(&ta->lock);
|
---|
| 247 | ta->refcount++;
|
---|
| 248 | spinlock_unlock(&ta->lock);
|
---|
| 249 |
|
---|
[34dcd3f] | 250 | t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
|
---|
[7509ddc] | 251 |
|
---|
| 252 | spinlock_lock(&ta->lock);
|
---|
[34dcd3f] | 253 | ta->accept_new_threads = false;
|
---|
[7509ddc] | 254 | ta->refcount--;
|
---|
| 255 |
|
---|
| 256 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
|
---|
| 257 | thread_t *thr;
|
---|
| 258 | bool sleeping = false;
|
---|
| 259 |
|
---|
| 260 | thr = list_get_instance(cur, thread_t, th_link);
|
---|
| 261 | if (thr == t)
|
---|
| 262 | continue;
|
---|
| 263 |
|
---|
| 264 | spinlock_lock(&thr->lock);
|
---|
| 265 | thr->interrupted = true;
|
---|
| 266 | if (thr->state == Sleeping)
|
---|
| 267 | sleeping = true;
|
---|
| 268 | spinlock_unlock(&thr->lock);
|
---|
| 269 |
|
---|
| 270 | if (sleeping)
|
---|
| 271 | waitq_interrupt_sleep(thr);
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[34dcd3f] | 274 | spinlock_unlock(&ta->lock);
|
---|
| 275 | interrupts_restore(ipl);
|
---|
[7509ddc] | 276 |
|
---|
[34dcd3f] | 277 | if (t)
|
---|
| 278 | thread_ready(t);
|
---|
| 279 |
|
---|
[7509ddc] | 280 | return 0;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[37c57f2] | 283 | /** Print task list */
|
---|
| 284 | void task_print_list(void)
|
---|
| 285 | {
|
---|
| 286 | link_t *cur;
|
---|
| 287 | ipl_t ipl;
|
---|
| 288 |
|
---|
| 289 | /* Messing with thread structures, avoid deadlock */
|
---|
| 290 | ipl = interrupts_disable();
|
---|
| 291 | spinlock_lock(&tasks_lock);
|
---|
| 292 |
|
---|
[7f6e755] | 293 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
|
---|
| 294 | btree_node_t *node;
|
---|
| 295 | int i;
|
---|
| 296 |
|
---|
| 297 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 298 | for (i = 0; i < node->keys; i++) {
|
---|
| 299 | task_t *t;
|
---|
| 300 | int j;
|
---|
| 301 |
|
---|
| 302 | t = (task_t *) node->value[i];
|
---|
| 303 |
|
---|
| 304 | spinlock_lock(&t->lock);
|
---|
[c4e4507] | 305 | printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
|
---|
| 306 | t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
|
---|
[7f6e755] | 307 | for (j=0; j < IPC_MAX_PHONES; j++) {
|
---|
| 308 | if (t->phones[j].callee)
|
---|
[280a27e] | 309 | printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
|
---|
[7f6e755] | 310 | }
|
---|
| 311 | printf("\n");
|
---|
| 312 | spinlock_unlock(&t->lock);
|
---|
[37c57f2] | 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | spinlock_unlock(&tasks_lock);
|
---|
| 317 | interrupts_restore(ipl);
|
---|
| 318 | }
|
---|
[7509ddc] | 319 |
|
---|
| 320 | /** Kernel thread used to cleanup the task. */
|
---|
[34dcd3f] | 321 | void ktaskclnp(void *arg)
|
---|
[7509ddc] | 322 | {
|
---|
[34dcd3f] | 323 | ipl_t ipl;
|
---|
| 324 | thread_t *t = NULL;
|
---|
| 325 | link_t *cur;
|
---|
| 326 |
|
---|
| 327 | thread_detach(THREAD);
|
---|
| 328 |
|
---|
| 329 | loop:
|
---|
| 330 | ipl = interrupts_disable();
|
---|
| 331 | spinlock_lock(&TASK->lock);
|
---|
| 332 |
|
---|
| 333 | /*
|
---|
| 334 | * Find a thread to join.
|
---|
| 335 | */
|
---|
| 336 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
|
---|
| 337 | t = list_get_instance(cur, thread_t, th_link);
|
---|
| 338 | if (t == THREAD)
|
---|
| 339 | continue;
|
---|
| 340 | else
|
---|
| 341 | break;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | spinlock_unlock(&TASK->lock);
|
---|
| 345 | interrupts_restore(ipl);
|
---|
| 346 |
|
---|
| 347 | if (t != THREAD) {
|
---|
| 348 | thread_join(t);
|
---|
| 349 | thread_detach(t);
|
---|
| 350 | goto loop;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | /*
|
---|
| 354 | * Now there are no other threads in this task
|
---|
| 355 | * and no new threads can be created.
|
---|
| 356 | */
|
---|
| 357 |
|
---|
[e090e1bc] | 358 | ipc_cleanup();
|
---|
| 359 | futex_cleanup();
|
---|
[7509ddc] | 360 | }
|
---|