[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 |
|
---|
[b91bb65] | 61 | static void ktaskclnp(void *arg);
|
---|
| 62 | static void ktaskkill(void *arg);
|
---|
[7509ddc] | 63 |
|
---|
[70527f1] | 64 | /** Initialize tasks
|
---|
| 65 | *
|
---|
| 66 | * Initialize kernel tasks support.
|
---|
| 67 | *
|
---|
| 68 | */
|
---|
[f761f1eb] | 69 | void task_init(void)
|
---|
| 70 | {
|
---|
[43114c5] | 71 | TASK = NULL;
|
---|
[7f6e755] | 72 | btree_create(&tasks_btree);
|
---|
[f761f1eb] | 73 | }
|
---|
| 74 |
|
---|
[70527f1] | 75 |
|
---|
| 76 | /** Create new task
|
---|
| 77 | *
|
---|
| 78 | * Create new task with no threads.
|
---|
| 79 | *
|
---|
[20d50a1] | 80 | * @param as Task's address space.
|
---|
[ff14c520] | 81 | * @param name Symbolic name.
|
---|
[70527f1] | 82 | *
|
---|
[5be1923] | 83 | * @return New task's structure
|
---|
[70527f1] | 84 | *
|
---|
| 85 | */
|
---|
[ff14c520] | 86 | task_t *task_create(as_t *as, char *name)
|
---|
[f761f1eb] | 87 | {
|
---|
[22f7769] | 88 | ipl_t ipl;
|
---|
[f761f1eb] | 89 | task_t *ta;
|
---|
[2ba7810] | 90 | int i;
|
---|
[f761f1eb] | 91 |
|
---|
[bb68433] | 92 | ta = (task_t *) malloc(sizeof(task_t), 0);
|
---|
| 93 |
|
---|
[963074b3] | 94 | task_create_arch(ta);
|
---|
| 95 |
|
---|
[bb68433] | 96 | spinlock_initialize(&ta->lock, "task_ta_lock");
|
---|
| 97 | list_initialize(&ta->th_head);
|
---|
| 98 | ta->as = as;
|
---|
[ff14c520] | 99 | ta->name = name;
|
---|
[b91bb65] | 100 | ta->main_thread = NULL;
|
---|
[7509ddc] | 101 | ta->refcount = 0;
|
---|
| 102 |
|
---|
[1077d91] | 103 | ta->capabilities = 0;
|
---|
[7509ddc] | 104 | ta->accept_new_threads = true;
|
---|
[2ba7810] | 105 |
|
---|
[6d9c49a] | 106 | ipc_answerbox_init(&ta->answerbox);
|
---|
[2ba7810] | 107 | for (i=0; i < IPC_MAX_PHONES;i++)
|
---|
| 108 | ipc_phone_init(&ta->phones[i]);
|
---|
[e74cb73] | 109 | if (ipc_phone_0)
|
---|
[2ba7810] | 110 | ipc_phone_connect(&ta->phones[0], ipc_phone_0);
|
---|
[5f62ef9] | 111 | atomic_set(&ta->active_calls, 0);
|
---|
[4fded58] | 112 |
|
---|
| 113 | mutex_initialize(&ta->futexes_lock);
|
---|
| 114 | btree_create(&ta->futexes);
|
---|
[bb68433] | 115 |
|
---|
| 116 | ipl = interrupts_disable();
|
---|
[482826d] | 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 |
|
---|
[bb68433] | 126 | spinlock_lock(&tasks_lock);
|
---|
[286e03d] | 127 |
|
---|
| 128 | ta->taskid = ++task_counter;
|
---|
[b7f364e] | 129 | btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
|
---|
[286e03d] | 130 |
|
---|
[bb68433] | 131 | spinlock_unlock(&tasks_lock);
|
---|
| 132 | interrupts_restore(ipl);
|
---|
| 133 |
|
---|
[f761f1eb] | 134 | return ta;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[7509ddc] | 137 | /** Destroy task.
|
---|
| 138 | *
|
---|
| 139 | * @param t Task to be destroyed.
|
---|
| 140 | */
|
---|
| 141 | void task_destroy(task_t *t)
|
---|
| 142 | {
|
---|
[31e8ddd] | 143 | spinlock_lock(&tasks_lock);
|
---|
| 144 | btree_remove(&tasks_btree, t->taskid, NULL);
|
---|
| 145 | spinlock_unlock(&tasks_lock);
|
---|
| 146 |
|
---|
| 147 | task_destroy_arch(t);
|
---|
| 148 | btree_destroy(&t->futexes);
|
---|
| 149 |
|
---|
| 150 | mutex_lock_active(&t->as->lock);
|
---|
| 151 | if (--t->as->refcount == 0) {
|
---|
| 152 | mutex_unlock(&t->as->lock);
|
---|
| 153 | as_destroy(t->as);
|
---|
| 154 | /*
|
---|
| 155 | * t->as is destroyed.
|
---|
| 156 | */
|
---|
| 157 | } else {
|
---|
| 158 | mutex_unlock(&t->as->lock);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | free(t);
|
---|
| 162 | TASK = NULL;
|
---|
[7509ddc] | 163 | }
|
---|
| 164 |
|
---|
[5be1923] | 165 | /** Create new task with 1 thread and run it
|
---|
[ff14c520] | 166 | *
|
---|
[9179d0a] | 167 | * @param program_addr Address of program executable image.
|
---|
[ff14c520] | 168 | * @param name Program name.
|
---|
[5be1923] | 169 | *
|
---|
[7f0837c] | 170 | * @return Task of the running program or NULL on error.
|
---|
[5be1923] | 171 | */
|
---|
[ff14c520] | 172 | task_t * task_run_program(void *program_addr, char *name)
|
---|
[5be1923] | 173 | {
|
---|
| 174 | as_t *as;
|
---|
| 175 | as_area_t *a;
|
---|
| 176 | int rc;
|
---|
[b91bb65] | 177 | thread_t *t1, *t2;
|
---|
[5be1923] | 178 | task_t *task;
|
---|
[0f250f9] | 179 | uspace_arg_t *kernel_uarg;
|
---|
[5be1923] | 180 |
|
---|
| 181 | as = as_create(0);
|
---|
[a0bb10ef] | 182 | ASSERT(as);
|
---|
[5be1923] | 183 |
|
---|
[649799a] | 184 | rc = elf_load((elf_header_t *) program_addr, as);
|
---|
[5be1923] | 185 | if (rc != EE_OK) {
|
---|
[482826d] | 186 | as_destroy(as);
|
---|
[5be1923] | 187 | return NULL;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[0f250f9] | 190 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
|
---|
| 191 | kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
|
---|
| 192 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
|
---|
| 193 | kernel_uarg->uspace_thread_function = NULL;
|
---|
| 194 | kernel_uarg->uspace_thread_arg = NULL;
|
---|
| 195 | kernel_uarg->uspace_uarg = NULL;
|
---|
[9f52563] | 196 |
|
---|
[ff14c520] | 197 | task = task_create(as, name);
|
---|
[a0bb10ef] | 198 | ASSERT(task);
|
---|
| 199 |
|
---|
[5be1923] | 200 | /*
|
---|
| 201 | * Create the data as_area.
|
---|
| 202 | */
|
---|
[0ee077ee] | 203 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
|
---|
| 204 | LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
|
---|
[8182031] | 205 | USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
|
---|
[5be1923] | 206 |
|
---|
[b91bb65] | 207 | /*
|
---|
| 208 | * Create the main thread.
|
---|
| 209 | */
|
---|
| 210 | t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
|
---|
| 211 | ASSERT(t1);
|
---|
[a0bb10ef] | 212 |
|
---|
[b91bb65] | 213 | /*
|
---|
| 214 | * Create killer thread for the new task.
|
---|
| 215 | */
|
---|
| 216 | t2 = thread_create(ktaskkill, t1, task, 0, "ktaskkill");
|
---|
| 217 | ASSERT(t2);
|
---|
| 218 | thread_ready(t2);
|
---|
| 219 |
|
---|
| 220 | thread_ready(t1);
|
---|
| 221 |
|
---|
[5be1923] | 222 | return task;
|
---|
| 223 | }
|
---|
[37c57f2] | 224 |
|
---|
[ec55358] | 225 | /** Syscall for reading task ID from userspace.
|
---|
| 226 | *
|
---|
[9179d0a] | 227 | * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
|
---|
[ec55358] | 228 | *
|
---|
[e3c762cd] | 229 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[ec55358] | 230 | */
|
---|
[24f3874] | 231 | __native sys_task_get_id(task_id_t *uspace_task_id)
|
---|
[ec55358] | 232 | {
|
---|
| 233 | /*
|
---|
| 234 | * No need to acquire lock on TASK because taskid
|
---|
| 235 | * remains constant for the lifespan of the task.
|
---|
| 236 | */
|
---|
[e3c762cd] | 237 | return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
|
---|
[ec55358] | 238 | }
|
---|
| 239 |
|
---|
[9a8d91b] | 240 | /** Find task structure corresponding to task ID.
|
---|
| 241 | *
|
---|
| 242 | * The tasks_lock must be already held by the caller of this function
|
---|
| 243 | * and interrupts must be disabled.
|
---|
| 244 | *
|
---|
| 245 | * @param id Task ID.
|
---|
| 246 | *
|
---|
| 247 | * @return Task structure address or NULL if there is no such task ID.
|
---|
| 248 | */
|
---|
| 249 | task_t *task_find_by_id(task_id_t id)
|
---|
| 250 | {
|
---|
| 251 | btree_node_t *leaf;
|
---|
| 252 |
|
---|
| 253 | return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[7509ddc] | 256 | /** Kill task.
|
---|
| 257 | *
|
---|
| 258 | * @param id ID of the task to be killed.
|
---|
| 259 | *
|
---|
| 260 | * @return 0 on success or an error code from errno.h
|
---|
| 261 | */
|
---|
| 262 | int task_kill(task_id_t id)
|
---|
| 263 | {
|
---|
| 264 | ipl_t ipl;
|
---|
| 265 | task_t *ta;
|
---|
| 266 | thread_t *t;
|
---|
| 267 | link_t *cur;
|
---|
| 268 |
|
---|
| 269 | ipl = interrupts_disable();
|
---|
| 270 | spinlock_lock(&tasks_lock);
|
---|
| 271 |
|
---|
| 272 | if (!(ta = task_find_by_id(id))) {
|
---|
| 273 | spinlock_unlock(&tasks_lock);
|
---|
| 274 | interrupts_restore(ipl);
|
---|
| 275 | return ENOENT;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | spinlock_lock(&ta->lock);
|
---|
| 279 | ta->refcount++;
|
---|
| 280 | spinlock_unlock(&ta->lock);
|
---|
[31e8ddd] | 281 |
|
---|
| 282 | spinlock_unlock(&tasks_lock);
|
---|
[7509ddc] | 283 |
|
---|
[34dcd3f] | 284 | t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
|
---|
[7509ddc] | 285 |
|
---|
| 286 | spinlock_lock(&ta->lock);
|
---|
[34dcd3f] | 287 | ta->accept_new_threads = false;
|
---|
[7509ddc] | 288 | ta->refcount--;
|
---|
[b91bb65] | 289 |
|
---|
| 290 | /*
|
---|
| 291 | * Interrupt all threads except this one.
|
---|
| 292 | */
|
---|
[7509ddc] | 293 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
|
---|
| 294 | thread_t *thr;
|
---|
| 295 | bool sleeping = false;
|
---|
| 296 |
|
---|
| 297 | thr = list_get_instance(cur, thread_t, th_link);
|
---|
| 298 | if (thr == t)
|
---|
| 299 | continue;
|
---|
| 300 |
|
---|
| 301 | spinlock_lock(&thr->lock);
|
---|
| 302 | thr->interrupted = true;
|
---|
| 303 | if (thr->state == Sleeping)
|
---|
| 304 | sleeping = true;
|
---|
| 305 | spinlock_unlock(&thr->lock);
|
---|
| 306 |
|
---|
| 307 | if (sleeping)
|
---|
| 308 | waitq_interrupt_sleep(thr);
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[34dcd3f] | 311 | spinlock_unlock(&ta->lock);
|
---|
| 312 | interrupts_restore(ipl);
|
---|
[7509ddc] | 313 |
|
---|
[34dcd3f] | 314 | if (t)
|
---|
| 315 | thread_ready(t);
|
---|
| 316 |
|
---|
[7509ddc] | 317 | return 0;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[37c57f2] | 320 | /** Print task list */
|
---|
| 321 | void task_print_list(void)
|
---|
| 322 | {
|
---|
| 323 | link_t *cur;
|
---|
| 324 | ipl_t ipl;
|
---|
| 325 |
|
---|
| 326 | /* Messing with thread structures, avoid deadlock */
|
---|
| 327 | ipl = interrupts_disable();
|
---|
| 328 | spinlock_lock(&tasks_lock);
|
---|
| 329 |
|
---|
[7f6e755] | 330 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
|
---|
| 331 | btree_node_t *node;
|
---|
| 332 | int i;
|
---|
| 333 |
|
---|
| 334 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 335 | for (i = 0; i < node->keys; i++) {
|
---|
| 336 | task_t *t;
|
---|
| 337 | int j;
|
---|
| 338 |
|
---|
| 339 | t = (task_t *) node->value[i];
|
---|
| 340 |
|
---|
| 341 | spinlock_lock(&t->lock);
|
---|
[c4e4507] | 342 | printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
|
---|
| 343 | t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
|
---|
[7f6e755] | 344 | for (j=0; j < IPC_MAX_PHONES; j++) {
|
---|
| 345 | if (t->phones[j].callee)
|
---|
[280a27e] | 346 | printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
|
---|
[7f6e755] | 347 | }
|
---|
| 348 | printf("\n");
|
---|
| 349 | spinlock_unlock(&t->lock);
|
---|
[37c57f2] | 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | spinlock_unlock(&tasks_lock);
|
---|
| 354 | interrupts_restore(ipl);
|
---|
| 355 | }
|
---|
[7509ddc] | 356 |
|
---|
[b91bb65] | 357 | /** Kernel thread used to cleanup the task after it is killed. */
|
---|
[34dcd3f] | 358 | void ktaskclnp(void *arg)
|
---|
[7509ddc] | 359 | {
|
---|
[34dcd3f] | 360 | ipl_t ipl;
|
---|
[b91bb65] | 361 | thread_t *t = NULL, *main_thread;
|
---|
[34dcd3f] | 362 | link_t *cur;
|
---|
| 363 |
|
---|
| 364 | thread_detach(THREAD);
|
---|
| 365 |
|
---|
| 366 | loop:
|
---|
| 367 | ipl = interrupts_disable();
|
---|
| 368 | spinlock_lock(&TASK->lock);
|
---|
| 369 |
|
---|
[b91bb65] | 370 | main_thread = TASK->main_thread;
|
---|
| 371 |
|
---|
[34dcd3f] | 372 | /*
|
---|
| 373 | * Find a thread to join.
|
---|
| 374 | */
|
---|
| 375 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
|
---|
| 376 | t = list_get_instance(cur, thread_t, th_link);
|
---|
| 377 | if (t == THREAD)
|
---|
| 378 | continue;
|
---|
[b91bb65] | 379 | else if (t == main_thread)
|
---|
| 380 | continue;
|
---|
[34dcd3f] | 381 | else
|
---|
| 382 | break;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | spinlock_unlock(&TASK->lock);
|
---|
| 386 | interrupts_restore(ipl);
|
---|
| 387 |
|
---|
| 388 | if (t != THREAD) {
|
---|
[b91bb65] | 389 | ASSERT(t != main_thread); /* uninit is joined and detached in ktaskkill */
|
---|
[34dcd3f] | 390 | thread_join(t);
|
---|
| 391 | thread_detach(t);
|
---|
[b91bb65] | 392 | goto loop; /* go for another thread */
|
---|
[34dcd3f] | 393 | }
|
---|
| 394 |
|
---|
| 395 | /*
|
---|
| 396 | * Now there are no other threads in this task
|
---|
| 397 | * and no new threads can be created.
|
---|
| 398 | */
|
---|
| 399 |
|
---|
[e090e1bc] | 400 | ipc_cleanup();
|
---|
| 401 | futex_cleanup();
|
---|
[7509ddc] | 402 | }
|
---|
[b91bb65] | 403 |
|
---|
| 404 | /** Kernel task used to kill a userspace task when its main thread exits.
|
---|
| 405 | *
|
---|
| 406 | * This thread waits until the main userspace thread (i.e. uninit) exits.
|
---|
| 407 | * When this happens, the task is killed.
|
---|
| 408 | *
|
---|
| 409 | * @param arg Pointer to the thread structure of the task's main thread.
|
---|
| 410 | */
|
---|
| 411 | void ktaskkill(void *arg)
|
---|
| 412 | {
|
---|
| 413 | thread_t *t = (thread_t *) arg;
|
---|
| 414 |
|
---|
| 415 | /*
|
---|
| 416 | * Userspace threads cannot detach themselves,
|
---|
| 417 | * therefore the thread pointer is guaranteed to be valid.
|
---|
| 418 | */
|
---|
| 419 | thread_join(t); /* sleep uninterruptibly here! */
|
---|
| 420 | thread_detach(t);
|
---|
| 421 | task_kill(TASK->taskid);
|
---|
| 422 | }
|
---|