| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 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 |
|
|---|
| 29 | /** @addtogroup genericproc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * @brief Task management.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <proc/thread.h>
|
|---|
| 39 | #include <proc/task.h>
|
|---|
| 40 | #include <mm/as.h>
|
|---|
| 41 | #include <mm/slab.h>
|
|---|
| 42 | #include <atomic.h>
|
|---|
| 43 | #include <synch/futex.h>
|
|---|
| 44 | #include <synch/spinlock.h>
|
|---|
| 45 | #include <synch/waitq.h>
|
|---|
| 46 | #include <arch.h>
|
|---|
| 47 | #include <arch/barrier.h>
|
|---|
| 48 | #include <adt/avl.h>
|
|---|
| 49 | #include <adt/btree.h>
|
|---|
| 50 | #include <adt/list.h>
|
|---|
| 51 | #include <ipc/ipc.h>
|
|---|
| 52 | #include <ipc/ipcrsc.h>
|
|---|
| 53 | #include <ipc/event.h>
|
|---|
| 54 | #include <print.h>
|
|---|
| 55 | #include <errno.h>
|
|---|
| 56 | #include <func.h>
|
|---|
| 57 | #include <str.h>
|
|---|
| 58 | #include <memstr.h>
|
|---|
| 59 | #include <syscall/copy.h>
|
|---|
| 60 | #include <macros.h>
|
|---|
| 61 |
|
|---|
| 62 | /** Spinlock protecting the tasks_tree AVL tree. */
|
|---|
| 63 | IRQ_SPINLOCK_INITIALIZE(tasks_lock);
|
|---|
| 64 |
|
|---|
| 65 | /** AVL tree of active tasks.
|
|---|
| 66 | *
|
|---|
| 67 | * The task is guaranteed to exist after it was found in the tasks_tree as
|
|---|
| 68 | * long as:
|
|---|
| 69 | *
|
|---|
| 70 | * @li the tasks_lock is held,
|
|---|
| 71 | * @li the task's lock is held when task's lock is acquired before releasing
|
|---|
| 72 | * tasks_lock or
|
|---|
| 73 | * @li the task's refcount is greater than 0
|
|---|
| 74 | *
|
|---|
| 75 | */
|
|---|
| 76 | avltree_t tasks_tree;
|
|---|
| 77 |
|
|---|
| 78 | static task_id_t task_counter = 0;
|
|---|
| 79 |
|
|---|
| 80 | static slab_cache_t *task_slab;
|
|---|
| 81 |
|
|---|
| 82 | /* Forward declarations. */
|
|---|
| 83 | static void task_kill_internal(task_t *);
|
|---|
| 84 | static int tsk_constructor(void *, unsigned int);
|
|---|
| 85 |
|
|---|
| 86 | /** Initialize kernel tasks support.
|
|---|
| 87 | *
|
|---|
| 88 | */
|
|---|
| 89 | void task_init(void)
|
|---|
| 90 | {
|
|---|
| 91 | TASK = NULL;
|
|---|
| 92 | avltree_create(&tasks_tree);
|
|---|
| 93 | task_slab = slab_cache_create("task_t", sizeof(task_t), 0,
|
|---|
| 94 | tsk_constructor, NULL, 0);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /** Task finish walker.
|
|---|
| 98 | *
|
|---|
| 99 | * The idea behind this walker is to kill and count all tasks different from
|
|---|
| 100 | * TASK.
|
|---|
| 101 | *
|
|---|
| 102 | */
|
|---|
| 103 | static bool task_done_walker(avltree_node_t *node, void *arg)
|
|---|
| 104 | {
|
|---|
| 105 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
|---|
| 106 | size_t *cnt = (size_t *) arg;
|
|---|
| 107 |
|
|---|
| 108 | if (task != TASK) {
|
|---|
| 109 | (*cnt)++;
|
|---|
| 110 |
|
|---|
| 111 | #ifdef CONFIG_DEBUG
|
|---|
| 112 | printf("[%"PRIu64"] ", task->taskid);
|
|---|
| 113 | #endif
|
|---|
| 114 |
|
|---|
| 115 | task_kill_internal(task);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /* Continue the walk */
|
|---|
| 119 | return true;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /** Kill all tasks except the current task.
|
|---|
| 123 | *
|
|---|
| 124 | */
|
|---|
| 125 | void task_done(void)
|
|---|
| 126 | {
|
|---|
| 127 | size_t tasks_left;
|
|---|
| 128 |
|
|---|
| 129 | if (ipc_phone_0) {
|
|---|
| 130 | task_t *task_0 = ipc_phone_0->task;
|
|---|
| 131 | ipc_phone_0 = NULL;
|
|---|
| 132 | /*
|
|---|
| 133 | * The first task is held by kinit(), we need to release it or
|
|---|
| 134 | * it will never finish cleanup.
|
|---|
| 135 | */
|
|---|
| 136 | task_release(task_0);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /* Repeat until there are any tasks except TASK */
|
|---|
| 140 | do {
|
|---|
| 141 | #ifdef CONFIG_DEBUG
|
|---|
| 142 | printf("Killing tasks... ");
|
|---|
| 143 | #endif
|
|---|
| 144 |
|
|---|
| 145 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 146 | tasks_left = 0;
|
|---|
| 147 | avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
|
|---|
| 148 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 149 |
|
|---|
| 150 | thread_sleep(1);
|
|---|
| 151 |
|
|---|
| 152 | #ifdef CONFIG_DEBUG
|
|---|
| 153 | printf("\n");
|
|---|
| 154 | #endif
|
|---|
| 155 | } while (tasks_left > 0);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | int tsk_constructor(void *obj, unsigned int kmflags)
|
|---|
| 159 | {
|
|---|
| 160 | task_t *task = (task_t *) obj;
|
|---|
| 161 |
|
|---|
| 162 | atomic_set(&task->refcount, 0);
|
|---|
| 163 | atomic_set(&task->lifecount, 0);
|
|---|
| 164 |
|
|---|
| 165 | irq_spinlock_initialize(&task->lock, "task_t_lock");
|
|---|
| 166 |
|
|---|
| 167 | list_initialize(&task->threads);
|
|---|
| 168 |
|
|---|
| 169 | ipc_answerbox_init(&task->answerbox, task);
|
|---|
| 170 |
|
|---|
| 171 | size_t i;
|
|---|
| 172 | for (i = 0; i < IPC_MAX_PHONES; i++)
|
|---|
| 173 | ipc_phone_init(&task->phones[i], task);
|
|---|
| 174 |
|
|---|
| 175 | spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
|
|---|
| 176 | list_initialize(&task->active_calls);
|
|---|
| 177 |
|
|---|
| 178 | #ifdef CONFIG_UDEBUG
|
|---|
| 179 | /* Init kbox stuff */
|
|---|
| 180 | task->kb.thread = NULL;
|
|---|
| 181 | ipc_answerbox_init(&task->kb.box, task);
|
|---|
| 182 | mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
|
|---|
| 183 | #endif
|
|---|
| 184 |
|
|---|
| 185 | return 0;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /** Create new task with no threads.
|
|---|
| 189 | *
|
|---|
| 190 | * @param as Task's address space.
|
|---|
| 191 | * @param name Symbolic name (a copy is made).
|
|---|
| 192 | *
|
|---|
| 193 | * @return New task's structure.
|
|---|
| 194 | *
|
|---|
| 195 | */
|
|---|
| 196 | task_t *task_create(as_t *as, const char *name)
|
|---|
| 197 | {
|
|---|
| 198 | task_t *task = (task_t *) slab_alloc(task_slab, 0);
|
|---|
| 199 | task_create_arch(task);
|
|---|
| 200 |
|
|---|
| 201 | task->as = as;
|
|---|
| 202 | str_cpy(task->name, TASK_NAME_BUFLEN, name);
|
|---|
| 203 |
|
|---|
| 204 | task->container = CONTAINER;
|
|---|
| 205 | task->capabilities = 0;
|
|---|
| 206 | task->ucycles = 0;
|
|---|
| 207 | task->kcycles = 0;
|
|---|
| 208 |
|
|---|
| 209 | task->ipc_info.call_sent = 0;
|
|---|
| 210 | task->ipc_info.call_received = 0;
|
|---|
| 211 | task->ipc_info.answer_sent = 0;
|
|---|
| 212 | task->ipc_info.answer_received = 0;
|
|---|
| 213 | task->ipc_info.irq_notif_received = 0;
|
|---|
| 214 | task->ipc_info.forwarded = 0;
|
|---|
| 215 |
|
|---|
| 216 | event_task_init(task);
|
|---|
| 217 |
|
|---|
| 218 | task->answerbox.active = true;
|
|---|
| 219 |
|
|---|
| 220 | #ifdef CONFIG_UDEBUG
|
|---|
| 221 | /* Init debugging stuff */
|
|---|
| 222 | udebug_task_init(&task->udebug);
|
|---|
| 223 |
|
|---|
| 224 | /* Init kbox stuff */
|
|---|
| 225 | task->kb.box.active = true;
|
|---|
| 226 | task->kb.finished = false;
|
|---|
| 227 | #endif
|
|---|
| 228 |
|
|---|
| 229 | if ((ipc_phone_0) &&
|
|---|
| 230 | (container_check(ipc_phone_0->task->container, task->container)))
|
|---|
| 231 | (void) ipc_phone_connect(&task->phones[0], ipc_phone_0);
|
|---|
| 232 |
|
|---|
| 233 | futex_task_init(task);
|
|---|
| 234 |
|
|---|
| 235 | /*
|
|---|
| 236 | * Get a reference to the address space.
|
|---|
| 237 | */
|
|---|
| 238 | as_hold(task->as);
|
|---|
| 239 |
|
|---|
| 240 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 241 |
|
|---|
| 242 | task->taskid = ++task_counter;
|
|---|
| 243 | avltree_node_initialize(&task->tasks_tree_node);
|
|---|
| 244 | task->tasks_tree_node.key = task->taskid;
|
|---|
| 245 | avltree_insert(&tasks_tree, &task->tasks_tree_node);
|
|---|
| 246 |
|
|---|
| 247 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 248 |
|
|---|
| 249 | return task;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | /** Destroy task.
|
|---|
| 253 | *
|
|---|
| 254 | * @param task Task to be destroyed.
|
|---|
| 255 | *
|
|---|
| 256 | */
|
|---|
| 257 | void task_destroy(task_t *task)
|
|---|
| 258 | {
|
|---|
| 259 | /*
|
|---|
| 260 | * Remove the task from the task B+tree.
|
|---|
| 261 | */
|
|---|
| 262 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 263 | avltree_delete(&tasks_tree, &task->tasks_tree_node);
|
|---|
| 264 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 265 |
|
|---|
| 266 | /*
|
|---|
| 267 | * Perform architecture specific task destruction.
|
|---|
| 268 | */
|
|---|
| 269 | task_destroy_arch(task);
|
|---|
| 270 |
|
|---|
| 271 | /*
|
|---|
| 272 | * Free up dynamically allocated state.
|
|---|
| 273 | */
|
|---|
| 274 | futex_task_deinit(task);
|
|---|
| 275 |
|
|---|
| 276 | /*
|
|---|
| 277 | * Drop our reference to the address space.
|
|---|
| 278 | */
|
|---|
| 279 | as_release(task->as);
|
|---|
| 280 |
|
|---|
| 281 | slab_free(task_slab, task);
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /** Hold a reference to a task.
|
|---|
| 285 | *
|
|---|
| 286 | * Holding a reference to a task prevents destruction of that task.
|
|---|
| 287 | *
|
|---|
| 288 | * @param task Task to be held.
|
|---|
| 289 | *
|
|---|
| 290 | */
|
|---|
| 291 | void task_hold(task_t *task)
|
|---|
| 292 | {
|
|---|
| 293 | atomic_inc(&task->refcount);
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /** Release a reference to a task.
|
|---|
| 297 | *
|
|---|
| 298 | * The last one to release a reference to a task destroys the task.
|
|---|
| 299 | *
|
|---|
| 300 | * @param task Task to be released.
|
|---|
| 301 | *
|
|---|
| 302 | */
|
|---|
| 303 | void task_release(task_t *task)
|
|---|
| 304 | {
|
|---|
| 305 | if ((atomic_predec(&task->refcount)) == 0)
|
|---|
| 306 | task_destroy(task);
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | #ifdef __32_BITS__
|
|---|
| 310 |
|
|---|
| 311 | /** Syscall for reading task ID from userspace (32 bits)
|
|---|
| 312 | *
|
|---|
| 313 | * @param uspace_taskid Pointer to user-space buffer
|
|---|
| 314 | * where to store current task ID.
|
|---|
| 315 | *
|
|---|
| 316 | * @return Zero on success or an error code from @ref errno.h.
|
|---|
| 317 | *
|
|---|
| 318 | */
|
|---|
| 319 | sysarg_t sys_task_get_id(sysarg64_t *uspace_taskid)
|
|---|
| 320 | {
|
|---|
| 321 | /*
|
|---|
| 322 | * No need to acquire lock on TASK because taskid remains constant for
|
|---|
| 323 | * the lifespan of the task.
|
|---|
| 324 | */
|
|---|
| 325 | return (sysarg_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
|
|---|
| 326 | sizeof(TASK->taskid));
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | #endif /* __32_BITS__ */
|
|---|
| 330 |
|
|---|
| 331 | #ifdef __64_BITS__
|
|---|
| 332 |
|
|---|
| 333 | /** Syscall for reading task ID from userspace (64 bits)
|
|---|
| 334 | *
|
|---|
| 335 | * @return Current task ID.
|
|---|
| 336 | *
|
|---|
| 337 | */
|
|---|
| 338 | sysarg_t sys_task_get_id(void)
|
|---|
| 339 | {
|
|---|
| 340 | /*
|
|---|
| 341 | * No need to acquire lock on TASK because taskid remains constant for
|
|---|
| 342 | * the lifespan of the task.
|
|---|
| 343 | */
|
|---|
| 344 | return TASK->taskid;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | #endif /* __64_BITS__ */
|
|---|
| 348 |
|
|---|
| 349 | /** Syscall for setting the task name.
|
|---|
| 350 | *
|
|---|
| 351 | * The name simplifies identifying the task in the task list.
|
|---|
| 352 | *
|
|---|
| 353 | * @param name The new name for the task. (typically the same
|
|---|
| 354 | * as the command used to execute it).
|
|---|
| 355 | *
|
|---|
| 356 | * @return 0 on success or an error code from @ref errno.h.
|
|---|
| 357 | *
|
|---|
| 358 | */
|
|---|
| 359 | sysarg_t sys_task_set_name(const char *uspace_name, size_t name_len)
|
|---|
| 360 | {
|
|---|
| 361 | char namebuf[TASK_NAME_BUFLEN];
|
|---|
| 362 |
|
|---|
| 363 | /* Cap length of name and copy it from userspace. */
|
|---|
| 364 | if (name_len > TASK_NAME_BUFLEN - 1)
|
|---|
| 365 | name_len = TASK_NAME_BUFLEN - 1;
|
|---|
| 366 |
|
|---|
| 367 | int rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
|---|
| 368 | if (rc != 0)
|
|---|
| 369 | return (sysarg_t) rc;
|
|---|
| 370 |
|
|---|
| 371 | namebuf[name_len] = '\0';
|
|---|
| 372 |
|
|---|
| 373 | /*
|
|---|
| 374 | * As the task name is referenced also from the
|
|---|
| 375 | * threads, lock the threads' lock for the course
|
|---|
| 376 | * of the update.
|
|---|
| 377 | */
|
|---|
| 378 |
|
|---|
| 379 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 380 | irq_spinlock_lock(&TASK->lock, false);
|
|---|
| 381 | irq_spinlock_lock(&threads_lock, false);
|
|---|
| 382 |
|
|---|
| 383 | /* Set task name */
|
|---|
| 384 | str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
|
|---|
| 385 |
|
|---|
| 386 | irq_spinlock_unlock(&threads_lock, false);
|
|---|
| 387 | irq_spinlock_unlock(&TASK->lock, false);
|
|---|
| 388 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 389 |
|
|---|
| 390 | return EOK;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | /** Syscall to forcefully terminate a task
|
|---|
| 394 | *
|
|---|
| 395 | * @param uspace_taskid Pointer to task ID in user space.
|
|---|
| 396 | *
|
|---|
| 397 | * @return 0 on success or an error code from @ref errno.h.
|
|---|
| 398 | *
|
|---|
| 399 | */
|
|---|
| 400 | sysarg_t sys_task_kill(task_id_t *uspace_taskid)
|
|---|
| 401 | {
|
|---|
| 402 | task_id_t taskid;
|
|---|
| 403 | int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
|
|---|
| 404 | if (rc != 0)
|
|---|
| 405 | return (sysarg_t) rc;
|
|---|
| 406 |
|
|---|
| 407 | return (sysarg_t) task_kill(taskid);
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | /** Find task structure corresponding to task ID.
|
|---|
| 411 | *
|
|---|
| 412 | * The tasks_lock must be already held by the caller of this function and
|
|---|
| 413 | * interrupts must be disabled.
|
|---|
| 414 | *
|
|---|
| 415 | * @param id Task ID.
|
|---|
| 416 | *
|
|---|
| 417 | * @return Task structure address or NULL if there is no such task ID.
|
|---|
| 418 | *
|
|---|
| 419 | */
|
|---|
| 420 | task_t *task_find_by_id(task_id_t id)
|
|---|
| 421 | {
|
|---|
| 422 | ASSERT(interrupts_disabled());
|
|---|
| 423 | ASSERT(irq_spinlock_locked(&tasks_lock));
|
|---|
| 424 |
|
|---|
| 425 | avltree_node_t *node =
|
|---|
| 426 | avltree_search(&tasks_tree, (avltree_key_t) id);
|
|---|
| 427 |
|
|---|
| 428 | if (node)
|
|---|
| 429 | return avltree_get_instance(node, task_t, tasks_tree_node);
|
|---|
| 430 |
|
|---|
| 431 | return NULL;
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | /** Get accounting data of given task.
|
|---|
| 435 | *
|
|---|
| 436 | * Note that task lock of 'task' must be already held and interrupts must be
|
|---|
| 437 | * already disabled.
|
|---|
| 438 | *
|
|---|
| 439 | * @param task Pointer to the task.
|
|---|
| 440 | * @param ucycles Out pointer to sum of all user cycles.
|
|---|
| 441 | * @param kcycles Out pointer to sum of all kernel cycles.
|
|---|
| 442 | *
|
|---|
| 443 | */
|
|---|
| 444 | void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
|
|---|
| 445 | {
|
|---|
| 446 | ASSERT(interrupts_disabled());
|
|---|
| 447 | ASSERT(irq_spinlock_locked(&task->lock));
|
|---|
| 448 |
|
|---|
| 449 | /* Accumulated values of task */
|
|---|
| 450 | uint64_t uret = task->ucycles;
|
|---|
| 451 | uint64_t kret = task->kcycles;
|
|---|
| 452 |
|
|---|
| 453 | /* Current values of threads */
|
|---|
| 454 | list_foreach(task->threads, th_link, thread_t, thread) {
|
|---|
| 455 | irq_spinlock_lock(&thread->lock, false);
|
|---|
| 456 |
|
|---|
| 457 | /* Process only counted threads */
|
|---|
| 458 | if (!thread->uncounted) {
|
|---|
| 459 | if (thread == THREAD) {
|
|---|
| 460 | /* Update accounting of current thread */
|
|---|
| 461 | thread_update_accounting(false);
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | uret += thread->ucycles;
|
|---|
| 465 | kret += thread->kcycles;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | irq_spinlock_unlock(&thread->lock, false);
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | *ucycles = uret;
|
|---|
| 472 | *kcycles = kret;
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | static void task_kill_internal(task_t *task)
|
|---|
| 476 | {
|
|---|
| 477 | irq_spinlock_lock(&task->lock, false);
|
|---|
| 478 | irq_spinlock_lock(&threads_lock, false);
|
|---|
| 479 |
|
|---|
| 480 | /*
|
|---|
| 481 | * Interrupt all threads.
|
|---|
| 482 | */
|
|---|
| 483 |
|
|---|
| 484 | list_foreach(task->threads, th_link, thread_t, thread) {
|
|---|
| 485 | bool sleeping = false;
|
|---|
| 486 |
|
|---|
| 487 | irq_spinlock_lock(&thread->lock, false);
|
|---|
| 488 |
|
|---|
| 489 | thread->interrupted = true;
|
|---|
| 490 | if (thread->state == Sleeping)
|
|---|
| 491 | sleeping = true;
|
|---|
| 492 |
|
|---|
| 493 | irq_spinlock_unlock(&thread->lock, false);
|
|---|
| 494 |
|
|---|
| 495 | if (sleeping)
|
|---|
| 496 | waitq_interrupt_sleep(thread);
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | irq_spinlock_unlock(&threads_lock, false);
|
|---|
| 500 | irq_spinlock_unlock(&task->lock, false);
|
|---|
| 501 | }
|
|---|
| 502 |
|
|---|
| 503 | /** Kill task.
|
|---|
| 504 | *
|
|---|
| 505 | * This function is idempotent.
|
|---|
| 506 | * It signals all the task's threads to bail it out.
|
|---|
| 507 | *
|
|---|
| 508 | * @param id ID of the task to be killed.
|
|---|
| 509 | *
|
|---|
| 510 | * @return Zero on success or an error code from errno.h.
|
|---|
| 511 | *
|
|---|
| 512 | */
|
|---|
| 513 | int task_kill(task_id_t id)
|
|---|
| 514 | {
|
|---|
| 515 | if (id == 1)
|
|---|
| 516 | return EPERM;
|
|---|
| 517 |
|
|---|
| 518 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 519 |
|
|---|
| 520 | task_t *task = task_find_by_id(id);
|
|---|
| 521 | if (!task) {
|
|---|
| 522 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 523 | return ENOENT;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | task_kill_internal(task);
|
|---|
| 527 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 528 |
|
|---|
| 529 | return EOK;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | /** Kill the currently running task.
|
|---|
| 533 | *
|
|---|
| 534 | * @param notify Send out fault notifications.
|
|---|
| 535 | *
|
|---|
| 536 | * @return Zero on success or an error code from errno.h.
|
|---|
| 537 | *
|
|---|
| 538 | */
|
|---|
| 539 | void task_kill_self(bool notify)
|
|---|
| 540 | {
|
|---|
| 541 | /*
|
|---|
| 542 | * User space can subscribe for FAULT events to take action
|
|---|
| 543 | * whenever a task faults (to take a dump, run a debugger, etc.).
|
|---|
| 544 | * The notification is always available, but unless udebug is enabled,
|
|---|
| 545 | * that's all you get.
|
|---|
| 546 | */
|
|---|
| 547 | if (notify) {
|
|---|
| 548 | /* Notify the subscriber that a fault occurred. */
|
|---|
| 549 | if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
|
|---|
| 550 | UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
|
|---|
| 551 | #ifdef CONFIG_UDEBUG
|
|---|
| 552 | /* Wait for a debugging session. */
|
|---|
| 553 | udebug_thread_fault();
|
|---|
| 554 | #endif
|
|---|
| 555 | }
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 559 | task_kill_internal(TASK);
|
|---|
| 560 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 561 |
|
|---|
| 562 | thread_exit();
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | /** Process syscall to terminate the current task.
|
|---|
| 566 | *
|
|---|
| 567 | * @param notify Send out fault notifications.
|
|---|
| 568 | *
|
|---|
| 569 | */
|
|---|
| 570 | sysarg_t sys_task_exit(sysarg_t notify)
|
|---|
| 571 | {
|
|---|
| 572 | task_kill_self(notify);
|
|---|
| 573 |
|
|---|
| 574 | /* Unreachable */
|
|---|
| 575 | return EOK;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | static bool task_print_walker(avltree_node_t *node, void *arg)
|
|---|
| 579 | {
|
|---|
| 580 | bool *additional = (bool *) arg;
|
|---|
| 581 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
|---|
| 582 | irq_spinlock_lock(&task->lock, false);
|
|---|
| 583 |
|
|---|
| 584 | uint64_t ucycles;
|
|---|
| 585 | uint64_t kcycles;
|
|---|
| 586 | char usuffix, ksuffix;
|
|---|
| 587 | task_get_accounting(task, &ucycles, &kcycles);
|
|---|
| 588 | order_suffix(ucycles, &ucycles, &usuffix);
|
|---|
| 589 | order_suffix(kcycles, &kcycles, &ksuffix);
|
|---|
| 590 |
|
|---|
| 591 | #ifdef __32_BITS__
|
|---|
| 592 | if (*additional)
|
|---|
| 593 | printf("%-8" PRIu64 " %9" PRIua, task->taskid,
|
|---|
| 594 | atomic_get(&task->refcount));
|
|---|
| 595 | else
|
|---|
| 596 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
|
|---|
| 597 | " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
|
|---|
| 598 | task->name, task->container, task, task->as,
|
|---|
| 599 | ucycles, usuffix, kcycles, ksuffix);
|
|---|
| 600 | #endif
|
|---|
| 601 |
|
|---|
| 602 | #ifdef __64_BITS__
|
|---|
| 603 | if (*additional)
|
|---|
| 604 | printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
|
|---|
| 605 | "%9" PRIua, task->taskid, ucycles, usuffix, kcycles,
|
|---|
| 606 | ksuffix, atomic_get(&task->refcount));
|
|---|
| 607 | else
|
|---|
| 608 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
|
|---|
| 609 | task->taskid, task->name, task->container, task, task->as);
|
|---|
| 610 | #endif
|
|---|
| 611 |
|
|---|
| 612 | if (*additional) {
|
|---|
| 613 | size_t i;
|
|---|
| 614 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
|---|
| 615 | if (task->phones[i].callee)
|
|---|
| 616 | printf(" %zu:%p", i, task->phones[i].callee);
|
|---|
| 617 | }
|
|---|
| 618 | printf("\n");
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 | irq_spinlock_unlock(&task->lock, false);
|
|---|
| 622 | return true;
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | /** Print task list
|
|---|
| 626 | *
|
|---|
| 627 | * @param additional Print additional information.
|
|---|
| 628 | *
|
|---|
| 629 | */
|
|---|
| 630 | void task_print_list(bool additional)
|
|---|
| 631 | {
|
|---|
| 632 | /* Messing with task structures, avoid deadlock */
|
|---|
| 633 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 634 |
|
|---|
| 635 | #ifdef __32_BITS__
|
|---|
| 636 | if (additional)
|
|---|
| 637 | printf("[id ] [threads] [calls] [callee\n");
|
|---|
| 638 | else
|
|---|
| 639 | printf("[id ] [name ] [ctn] [address ] [as ]"
|
|---|
| 640 | " [ucycles ] [kcycles ]\n");
|
|---|
| 641 | #endif
|
|---|
| 642 |
|
|---|
| 643 | #ifdef __64_BITS__
|
|---|
| 644 | if (additional)
|
|---|
| 645 | printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
|
|---|
| 646 | " [callee\n");
|
|---|
| 647 | else
|
|---|
| 648 | printf("[id ] [name ] [ctn] [address ]"
|
|---|
| 649 | " [as ]\n");
|
|---|
| 650 | #endif
|
|---|
| 651 |
|
|---|
| 652 | avltree_walk(&tasks_tree, task_print_walker, &additional);
|
|---|
| 653 |
|
|---|
| 654 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | /** @}
|
|---|
| 658 | */
|
|---|