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