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