[f761f1eb] | 1 | /*
|
---|
[278b4a30] | 2 | * Copyright (c) 2010 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
|
---|
[5ba201d] | 35 | * @brief Task management.
|
---|
[9179d0a] | 36 | */
|
---|
| 37 |
|
---|
[f761f1eb] | 38 | #include <proc/thread.h>
|
---|
| 39 | #include <proc/task.h>
|
---|
[20d50a1] | 40 | #include <mm/as.h>
|
---|
[085d973] | 41 | #include <mm/slab.h>
|
---|
[31d8e10] | 42 | #include <atomic.h>
|
---|
[669f3d32] | 43 | #include <synch/futex.h>
|
---|
[f761f1eb] | 44 | #include <synch/spinlock.h>
|
---|
[5573942] | 45 | #include <synch/waitq.h>
|
---|
[f761f1eb] | 46 | #include <arch.h>
|
---|
[8605b24] | 47 | #include <arch/barrier.h>
|
---|
[b76a2217] | 48 | #include <adt/avl.h>
|
---|
[7f6e755] | 49 | #include <adt/btree.h>
|
---|
[5c9a08b] | 50 | #include <adt/list.h>
|
---|
[6d9c49a] | 51 | #include <ipc/ipc.h>
|
---|
[c98e6ee] | 52 | #include <ipc/ipcrsc.h>
|
---|
[5d0500c] | 53 | #include <ipc/event.h>
|
---|
[37c57f2] | 54 | #include <print.h>
|
---|
[7509ddc] | 55 | #include <errno.h>
|
---|
[95155b0c] | 56 | #include <func.h>
|
---|
[19f857a] | 57 | #include <str.h>
|
---|
[41df2827] | 58 | #include <memstr.h>
|
---|
[e3c762cd] | 59 | #include <syscall/copy.h>
|
---|
[95ad426] | 60 | #include <macros.h>
|
---|
[8e5e78f] | 61 |
|
---|
[b76a2217] | 62 | /** Spinlock protecting the tasks_tree AVL tree. */
|
---|
[da1bafb] | 63 | IRQ_SPINLOCK_INITIALIZE(tasks_lock);
|
---|
[88169d9] | 64 |
|
---|
[b76a2217] | 65 | /** AVL tree of active tasks.
|
---|
[88169d9] | 66 | *
|
---|
[b76a2217] | 67 | * The task is guaranteed to exist after it was found in the tasks_tree as
|
---|
[6f4495f5] | 68 | * long as:
|
---|
[5ba201d] | 69 | *
|
---|
[88169d9] | 70 | * @li the tasks_lock is held,
|
---|
[6f4495f5] | 71 | * @li the task's lock is held when task's lock is acquired before releasing
|
---|
| 72 | * tasks_lock or
|
---|
[7bb6b06] | 73 | * @li the task's refcount is greater than 0
|
---|
[88169d9] | 74 | *
|
---|
| 75 | */
|
---|
[b76a2217] | 76 | avltree_t tasks_tree;
|
---|
[88169d9] | 77 |
|
---|
[286e03d] | 78 | static task_id_t task_counter = 0;
|
---|
[70527f1] | 79 |
|
---|
[103de761] | 80 | static slab_cache_t *task_slab;
|
---|
| 81 |
|
---|
[121966e] | 82 | /* Forward declarations. */
|
---|
| 83 | static void task_kill_internal(task_t *);
|
---|
[da1bafb] | 84 | static int tsk_constructor(void *, unsigned int);
|
---|
[121966e] | 85 |
|
---|
[da1bafb] | 86 | /** Initialize kernel tasks support.
|
---|
| 87 | *
|
---|
| 88 | */
|
---|
[f761f1eb] | 89 | void task_init(void)
|
---|
| 90 | {
|
---|
[43114c5] | 91 | TASK = NULL;
|
---|
[b76a2217] | 92 | avltree_create(&tasks_tree);
|
---|
[f97f1e51] | 93 | task_slab = slab_cache_create("task_t", sizeof(task_t), 0,
|
---|
[59ee56f] | 94 | tsk_constructor, NULL, 0);
|
---|
[b76a2217] | 95 | }
|
---|
| 96 |
|
---|
[da1bafb] | 97 | /** Task finish walker.
|
---|
| 98 | *
|
---|
[121966e] | 99 | * The idea behind this walker is to kill and count all tasks different from
|
---|
[814c4f5] | 100 | * TASK.
|
---|
[da1bafb] | 101 | *
|
---|
[b76a2217] | 102 | */
|
---|
| 103 | static bool task_done_walker(avltree_node_t *node, void *arg)
|
---|
| 104 | {
|
---|
[da1bafb] | 105 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
| 106 | size_t *cnt = (size_t *) arg;
|
---|
[5ba201d] | 107 |
|
---|
[da1bafb] | 108 | if (task != TASK) {
|
---|
[121966e] | 109 | (*cnt)++;
|
---|
[da1bafb] | 110 |
|
---|
[121966e] | 111 | #ifdef CONFIG_DEBUG
|
---|
[da1bafb] | 112 | printf("[%"PRIu64"] ", task->taskid);
|
---|
[121966e] | 113 | #endif
|
---|
[da1bafb] | 114 |
|
---|
| 115 | task_kill_internal(task);
|
---|
[b76a2217] | 116 | }
|
---|
[5ba201d] | 117 |
|
---|
| 118 | /* Continue the walk */
|
---|
| 119 | return true;
|
---|
[f761f1eb] | 120 | }
|
---|
| 121 |
|
---|
[da1bafb] | 122 | /** Kill all tasks except the current task.
|
---|
| 123 | *
|
---|
| 124 | */
|
---|
[f74bbaf] | 125 | void task_done(void)
|
---|
| 126 | {
|
---|
[da1bafb] | 127 | size_t tasks_left;
|
---|
[2f2beb4] | 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 | }
|
---|
[5ba201d] | 138 |
|
---|
[da1bafb] | 139 | /* Repeat until there are any tasks except TASK */
|
---|
| 140 | do {
|
---|
[121966e] | 141 | #ifdef CONFIG_DEBUG
|
---|
| 142 | printf("Killing tasks... ");
|
---|
| 143 | #endif
|
---|
[da1bafb] | 144 |
|
---|
| 145 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[121966e] | 146 | tasks_left = 0;
|
---|
| 147 | avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
|
---|
[da1bafb] | 148 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
| 149 |
|
---|
[121966e] | 150 | thread_sleep(1);
|
---|
[da1bafb] | 151 |
|
---|
[f74bbaf] | 152 | #ifdef CONFIG_DEBUG
|
---|
[121966e] | 153 | printf("\n");
|
---|
| 154 | #endif
|
---|
[da1bafb] | 155 | } while (tasks_left > 0);
|
---|
[f74bbaf] | 156 | }
|
---|
[70527f1] | 157 |
|
---|
[da1bafb] | 158 | int tsk_constructor(void *obj, unsigned int kmflags)
|
---|
[59ee56f] | 159 | {
|
---|
[da1bafb] | 160 | task_t *task = (task_t *) obj;
|
---|
| 161 |
|
---|
| 162 | atomic_set(&task->refcount, 0);
|
---|
| 163 | atomic_set(&task->lifecount, 0);
|
---|
[5ba201d] | 164 |
|
---|
[da1bafb] | 165 | irq_spinlock_initialize(&task->lock, "task_t_lock");
|
---|
[5ba201d] | 166 |
|
---|
[55b77d9] | 167 | list_initialize(&task->threads);
|
---|
[5ba201d] | 168 |
|
---|
[da1bafb] | 169 | ipc_answerbox_init(&task->answerbox, task);
|
---|
[5ba201d] | 170 |
|
---|
[da1bafb] | 171 | size_t i;
|
---|
[59ee56f] | 172 | for (i = 0; i < IPC_MAX_PHONES; i++)
|
---|
[03a8a8e] | 173 | ipc_phone_init(&task->phones[i], task);
|
---|
[86939b1] | 174 |
|
---|
| 175 | spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
|
---|
| 176 | list_initialize(&task->active_calls);
|
---|
[3ac5086] | 177 |
|
---|
[59ee56f] | 178 | #ifdef CONFIG_UDEBUG
|
---|
| 179 | /* Init kbox stuff */
|
---|
[da1bafb] | 180 | task->kb.thread = NULL;
|
---|
| 181 | ipc_answerbox_init(&task->kb.box, task);
|
---|
| 182 | mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
|
---|
[59ee56f] | 183 | #endif
|
---|
[5ba201d] | 184 |
|
---|
[59ee56f] | 185 | return 0;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[814c4f5] | 188 | /** Create new task with no threads.
|
---|
[70527f1] | 189 | *
|
---|
[5ba201d] | 190 | * @param as Task's address space.
|
---|
| 191 | * @param name Symbolic name (a copy is made).
|
---|
[70527f1] | 192 | *
|
---|
[5ba201d] | 193 | * @return New task's structure.
|
---|
[70527f1] | 194 | *
|
---|
| 195 | */
|
---|
[a000878c] | 196 | task_t *task_create(as_t *as, const char *name)
|
---|
[f761f1eb] | 197 | {
|
---|
[da1bafb] | 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 |
|
---|
[473d5d2] | 204 | task->container = CONTAINER;
|
---|
[da1bafb] | 205 | task->capabilities = 0;
|
---|
| 206 | task->ucycles = 0;
|
---|
| 207 | task->kcycles = 0;
|
---|
[7ad17de] | 208 |
|
---|
[da1bafb] | 209 | task->ipc_info.call_sent = 0;
|
---|
[be06914] | 210 | task->ipc_info.call_received = 0;
|
---|
[da1bafb] | 211 | task->ipc_info.answer_sent = 0;
|
---|
[be06914] | 212 | task->ipc_info.answer_received = 0;
|
---|
| 213 | task->ipc_info.irq_notif_received = 0;
|
---|
[da1bafb] | 214 | task->ipc_info.forwarded = 0;
|
---|
[5d0500c] | 215 |
|
---|
| 216 | event_task_init(task);
|
---|
[da1bafb] | 217 |
|
---|
[c33f39f] | 218 | task->answerbox.active = true;
|
---|
| 219 |
|
---|
[9a1b20c] | 220 | #ifdef CONFIG_UDEBUG
|
---|
| 221 | /* Init debugging stuff */
|
---|
[da1bafb] | 222 | udebug_task_init(&task->udebug);
|
---|
[5ba201d] | 223 |
|
---|
[9a1b20c] | 224 | /* Init kbox stuff */
|
---|
[c33f39f] | 225 | task->kb.box.active = true;
|
---|
[da1bafb] | 226 | task->kb.finished = false;
|
---|
[9a1b20c] | 227 | #endif
|
---|
[5ba201d] | 228 |
|
---|
[59ee56f] | 229 | if ((ipc_phone_0) &&
|
---|
[473d5d2] | 230 | (container_check(ipc_phone_0->task->container, task->container)))
|
---|
[c33f39f] | 231 | (void) ipc_phone_connect(&task->phones[0], ipc_phone_0);
|
---|
[5ba201d] | 232 |
|
---|
[669f3d32] | 233 | futex_task_init(task);
|
---|
[bb68433] | 234 |
|
---|
[6193351] | 235 | /*
|
---|
| 236 | * Get a reference to the address space.
|
---|
| 237 | */
|
---|
[da1bafb] | 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;
|
---|
[f761f1eb] | 250 | }
|
---|
| 251 |
|
---|
[7509ddc] | 252 | /** Destroy task.
|
---|
| 253 | *
|
---|
[da1bafb] | 254 | * @param task Task to be destroyed.
|
---|
[5ba201d] | 255 | *
|
---|
[7509ddc] | 256 | */
|
---|
[da1bafb] | 257 | void task_destroy(task_t *task)
|
---|
[7509ddc] | 258 | {
|
---|
[ea7890e7] | 259 | /*
|
---|
| 260 | * Remove the task from the task B+tree.
|
---|
| 261 | */
|
---|
[da1bafb] | 262 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 263 | avltree_delete(&tasks_tree, &task->tasks_tree_node);
|
---|
| 264 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[5ba201d] | 265 |
|
---|
[ea7890e7] | 266 | /*
|
---|
| 267 | * Perform architecture specific task destruction.
|
---|
| 268 | */
|
---|
[da1bafb] | 269 | task_destroy_arch(task);
|
---|
[5ba201d] | 270 |
|
---|
[ea7890e7] | 271 | /*
|
---|
| 272 | * Free up dynamically allocated state.
|
---|
| 273 | */
|
---|
[669f3d32] | 274 | futex_task_deinit(task);
|
---|
[5ba201d] | 275 |
|
---|
[ea7890e7] | 276 | /*
|
---|
| 277 | * Drop our reference to the address space.
|
---|
| 278 | */
|
---|
[da1bafb] | 279 | as_release(task->as);
|
---|
[31e8ddd] | 280 |
|
---|
[da1bafb] | 281 | slab_free(task_slab, task);
|
---|
[7509ddc] | 282 | }
|
---|
| 283 |
|
---|
[278b4a30] | 284 | /** Hold a reference to a task.
|
---|
| 285 | *
|
---|
| 286 | * Holding a reference to a task prevents destruction of that task.
|
---|
| 287 | *
|
---|
[da1bafb] | 288 | * @param task Task to be held.
|
---|
| 289 | *
|
---|
[278b4a30] | 290 | */
|
---|
[da1bafb] | 291 | void task_hold(task_t *task)
|
---|
[278b4a30] | 292 | {
|
---|
[da1bafb] | 293 | atomic_inc(&task->refcount);
|
---|
[278b4a30] | 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 | *
|
---|
[da1bafb] | 300 | * @param task Task to be released.
|
---|
| 301 | *
|
---|
[278b4a30] | 302 | */
|
---|
[da1bafb] | 303 | void task_release(task_t *task)
|
---|
[278b4a30] | 304 | {
|
---|
[da1bafb] | 305 | if ((atomic_predec(&task->refcount)) == 0)
|
---|
| 306 | task_destroy(task);
|
---|
[278b4a30] | 307 | }
|
---|
| 308 |
|
---|
[dd8d5a7] | 309 | #ifdef __32_BITS__
|
---|
| 310 |
|
---|
| 311 | /** Syscall for reading task ID from userspace (32 bits)
|
---|
[ec55358] | 312 | *
|
---|
[dd8d5a7] | 313 | * @param uspace_taskid Pointer to user-space buffer
|
---|
| 314 | * where to store current task ID.
|
---|
[5ba201d] | 315 | *
|
---|
| 316 | * @return Zero on success or an error code from @ref errno.h.
|
---|
[ec55358] | 317 | *
|
---|
| 318 | */
|
---|
[dd8d5a7] | 319 | sysarg_t sys_task_get_id(sysarg64_t *uspace_taskid)
|
---|
[ec55358] | 320 | {
|
---|
| 321 | /*
|
---|
[814c4f5] | 322 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 323 | * the lifespan of the task.
|
---|
[ec55358] | 324 | */
|
---|
[dd8d5a7] | 325 | return (sysarg_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
|
---|
[6f4495f5] | 326 | sizeof(TASK->taskid));
|
---|
[ec55358] | 327 | }
|
---|
| 328 |
|
---|
[dd8d5a7] | 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 |
|
---|
[bc18d63] | 349 | /** Syscall for setting the task name.
|
---|
| 350 | *
|
---|
| 351 | * The name simplifies identifying the task in the task list.
|
---|
| 352 | *
|
---|
[5ba201d] | 353 | * @param name The new name for the task. (typically the same
|
---|
| 354 | * as the command used to execute it).
|
---|
[bc18d63] | 355 | *
|
---|
| 356 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[5ba201d] | 357 | *
|
---|
[bc18d63] | 358 | */
|
---|
[96b02eb9] | 359 | sysarg_t sys_task_set_name(const char *uspace_name, size_t name_len)
|
---|
[bc18d63] | 360 | {
|
---|
| 361 | char namebuf[TASK_NAME_BUFLEN];
|
---|
[5ba201d] | 362 |
|
---|
[bc18d63] | 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;
|
---|
[5ba201d] | 366 |
|
---|
[577f042a] | 367 | int rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
[bc18d63] | 368 | if (rc != 0)
|
---|
[96b02eb9] | 369 | return (sysarg_t) rc;
|
---|
[5ba201d] | 370 |
|
---|
[f4b1535] | 371 | namebuf[name_len] = '\0';
|
---|
[577f042a] | 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 */
|
---|
[f4b1535] | 384 | str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
|
---|
[5ba201d] | 385 |
|
---|
[577f042a] | 386 | irq_spinlock_unlock(&threads_lock, false);
|
---|
| 387 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
| 388 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
| 389 |
|
---|
[bc18d63] | 390 | return EOK;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
[1e9f8ab] | 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;
|
---|
[5bcf1f9] | 403 | int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
|
---|
[1e9f8ab] | 404 | if (rc != 0)
|
---|
| 405 | return (sysarg_t) rc;
|
---|
[5bcf1f9] | 406 |
|
---|
[1e9f8ab] | 407 | return (sysarg_t) task_kill(taskid);
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[9a8d91b] | 410 | /** Find task structure corresponding to task ID.
|
---|
| 411 | *
|
---|
[814c4f5] | 412 | * The tasks_lock must be already held by the caller of this function and
|
---|
| 413 | * interrupts must be disabled.
|
---|
[9a8d91b] | 414 | *
|
---|
[5ba201d] | 415 | * @param id Task ID.
|
---|
| 416 | *
|
---|
[e1b6742] | 417 | * @return Task structure address or NULL if there is no such task ID.
|
---|
[9a8d91b] | 418 | *
|
---|
| 419 | */
|
---|
[5ba201d] | 420 | task_t *task_find_by_id(task_id_t id)
|
---|
| 421 | {
|
---|
[1d432f9] | 422 | ASSERT(interrupts_disabled());
|
---|
| 423 | ASSERT(irq_spinlock_locked(&tasks_lock));
|
---|
| 424 |
|
---|
[e1b6742] | 425 | avltree_node_t *node =
|
---|
| 426 | avltree_search(&tasks_tree, (avltree_key_t) id);
|
---|
[5ba201d] | 427 |
|
---|
[b76a2217] | 428 | if (node)
|
---|
[da1bafb] | 429 | return avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
[5ba201d] | 430 |
|
---|
[b76a2217] | 431 | return NULL;
|
---|
[9a8d91b] | 432 | }
|
---|
| 433 |
|
---|
[0313ff0] | 434 | /** Get accounting data of given task.
|
---|
| 435 | *
|
---|
[1d432f9] | 436 | * Note that task lock of 'task' must be already held and interrupts must be
|
---|
[814c4f5] | 437 | * already disabled.
|
---|
[0313ff0] | 438 | *
|
---|
[da1bafb] | 439 | * @param task Pointer to the task.
|
---|
[88dea9d] | 440 | * @param ucycles Out pointer to sum of all user cycles.
|
---|
| 441 | * @param kcycles Out pointer to sum of all kernel cycles.
|
---|
[0313ff0] | 442 | *
|
---|
| 443 | */
|
---|
[da1bafb] | 444 | void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
|
---|
[0313ff0] | 445 | {
|
---|
[1d432f9] | 446 | ASSERT(interrupts_disabled());
|
---|
| 447 | ASSERT(irq_spinlock_locked(&task->lock));
|
---|
| 448 |
|
---|
[a2a00e8] | 449 | /* Accumulated values of task */
|
---|
[da1bafb] | 450 | uint64_t uret = task->ucycles;
|
---|
| 451 | uint64_t kret = task->kcycles;
|
---|
[0313ff0] | 452 |
|
---|
| 453 | /* Current values of threads */
|
---|
[feeac0d] | 454 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[da1bafb] | 455 | irq_spinlock_lock(&thread->lock, false);
|
---|
[0313ff0] | 456 |
|
---|
[62b6d17] | 457 | /* Process only counted threads */
|
---|
[da1bafb] | 458 | if (!thread->uncounted) {
|
---|
| 459 | if (thread == THREAD) {
|
---|
[6f4495f5] | 460 | /* Update accounting of current thread */
|
---|
[a2a00e8] | 461 | thread_update_accounting(false);
|
---|
[da1bafb] | 462 | }
|
---|
| 463 |
|
---|
| 464 | uret += thread->ucycles;
|
---|
| 465 | kret += thread->kcycles;
|
---|
[62b6d17] | 466 | }
|
---|
[da1bafb] | 467 |
|
---|
| 468 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[0313ff0] | 469 | }
|
---|
| 470 |
|
---|
[a2a00e8] | 471 | *ucycles = uret;
|
---|
| 472 | *kcycles = kret;
|
---|
[0313ff0] | 473 | }
|
---|
| 474 |
|
---|
[da1bafb] | 475 | static void task_kill_internal(task_t *task)
|
---|
[121966e] | 476 | {
|
---|
[df58e44] | 477 | irq_spinlock_lock(&task->lock, false);
|
---|
| 478 | irq_spinlock_lock(&threads_lock, false);
|
---|
[5ba201d] | 479 |
|
---|
[121966e] | 480 | /*
|
---|
| 481 | * Interrupt all threads.
|
---|
| 482 | */
|
---|
[df58e44] | 483 |
|
---|
[feeac0d] | 484 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[121966e] | 485 | bool sleeping = false;
|
---|
| 486 |
|
---|
[da1bafb] | 487 | irq_spinlock_lock(&thread->lock, false);
|
---|
[121966e] | 488 |
|
---|
[da1bafb] | 489 | thread->interrupted = true;
|
---|
| 490 | if (thread->state == Sleeping)
|
---|
[121966e] | 491 | sleeping = true;
|
---|
[da1bafb] | 492 |
|
---|
| 493 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[121966e] | 494 |
|
---|
| 495 | if (sleeping)
|
---|
[da1bafb] | 496 | waitq_interrupt_sleep(thread);
|
---|
[121966e] | 497 | }
|
---|
[da1bafb] | 498 |
|
---|
[df58e44] | 499 | irq_spinlock_unlock(&threads_lock, false);
|
---|
[da1bafb] | 500 | irq_spinlock_unlock(&task->lock, false);
|
---|
[121966e] | 501 | }
|
---|
| 502 |
|
---|
[7509ddc] | 503 | /** Kill task.
|
---|
[ea7890e7] | 504 | *
|
---|
| 505 | * This function is idempotent.
|
---|
| 506 | * It signals all the task's threads to bail it out.
|
---|
[7509ddc] | 507 | *
|
---|
[5ba201d] | 508 | * @param id ID of the task to be killed.
|
---|
| 509 | *
|
---|
| 510 | * @return Zero on success or an error code from errno.h.
|
---|
[7509ddc] | 511 | *
|
---|
| 512 | */
|
---|
| 513 | int task_kill(task_id_t id)
|
---|
| 514 | {
|
---|
[9b6aae6] | 515 | if (id == 1)
|
---|
| 516 | return EPERM;
|
---|
[7509ddc] | 517 |
|
---|
[da1bafb] | 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);
|
---|
[7509ddc] | 523 | return ENOENT;
|
---|
| 524 | }
|
---|
[da1bafb] | 525 |
|
---|
| 526 | task_kill_internal(task);
|
---|
| 527 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
| 528 |
|
---|
| 529 | return EOK;
|
---|
[7509ddc] | 530 | }
|
---|
| 531 |
|
---|
[5bcf1f9] | 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) {
|
---|
[f9061b4] | 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) {
|
---|
[5bcf1f9] | 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 |
|
---|
[b76a2217] | 578 | static bool task_print_walker(avltree_node_t *node, void *arg)
|
---|
| 579 | {
|
---|
[c0f13d2] | 580 | bool *additional = (bool *) arg;
|
---|
[da1bafb] | 581 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
| 582 | irq_spinlock_lock(&task->lock, false);
|
---|
[5ba201d] | 583 |
|
---|
[a2a00e8] | 584 | uint64_t ucycles;
|
---|
| 585 | uint64_t kcycles;
|
---|
[1ba37fa] | 586 | char usuffix, ksuffix;
|
---|
[da1bafb] | 587 | task_get_accounting(task, &ucycles, &kcycles);
|
---|
[e535eeb] | 588 | order_suffix(ucycles, &ucycles, &usuffix);
|
---|
| 589 | order_suffix(kcycles, &kcycles, &ksuffix);
|
---|
[88dea9d] | 590 |
|
---|
[da1bafb] | 591 | #ifdef __32_BITS__
|
---|
[c0f13d2] | 592 | if (*additional)
|
---|
[97d17fe] | 593 | printf("%-8" PRIu64 " %9" PRIua, task->taskid,
|
---|
| 594 | atomic_get(&task->refcount));
|
---|
[c0f13d2] | 595 | else
|
---|
| 596 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
|
---|
| 597 | " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
|
---|
[473d5d2] | 598 | task->name, task->container, task, task->as,
|
---|
[c0f13d2] | 599 | ucycles, usuffix, kcycles, ksuffix);
|
---|
[52755f1] | 600 | #endif
|
---|
[5ba201d] | 601 |
|
---|
[52755f1] | 602 | #ifdef __64_BITS__
|
---|
[c0f13d2] | 603 | if (*additional)
|
---|
[7e752b2] | 604 | printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
|
---|
[97d17fe] | 605 | "%9" PRIua, task->taskid, ucycles, usuffix, kcycles,
|
---|
| 606 | ksuffix, atomic_get(&task->refcount));
|
---|
[c0f13d2] | 607 | else
|
---|
| 608 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
|
---|
[473d5d2] | 609 | task->taskid, task->name, task->container, task, task->as);
|
---|
[52755f1] | 610 | #endif
|
---|
[5ba201d] | 611 |
|
---|
[c0f13d2] | 612 | if (*additional) {
|
---|
| 613 | size_t i;
|
---|
| 614 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
| 615 | if (task->phones[i].callee)
|
---|
[7e752b2] | 616 | printf(" %zu:%p", i, task->phones[i].callee);
|
---|
[c0f13d2] | 617 | }
|
---|
| 618 | printf("\n");
|
---|
[b76a2217] | 619 | }
|
---|
[5ba201d] | 620 |
|
---|
[da1bafb] | 621 | irq_spinlock_unlock(&task->lock, false);
|
---|
[b76a2217] | 622 | return true;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
[c0f13d2] | 625 | /** Print task list
|
---|
| 626 | *
|
---|
| 627 | * @param additional Print additional information.
|
---|
| 628 | *
|
---|
| 629 | */
|
---|
| 630 | void task_print_list(bool additional)
|
---|
[37c57f2] | 631 | {
|
---|
[f74bbaf] | 632 | /* Messing with task structures, avoid deadlock */
|
---|
[da1bafb] | 633 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[5ba201d] | 634 |
|
---|
| 635 | #ifdef __32_BITS__
|
---|
[c0f13d2] | 636 | if (additional)
|
---|
[48dcc69] | 637 | printf("[id ] [threads] [calls] [callee\n");
|
---|
[c0f13d2] | 638 | else
|
---|
[473d5d2] | 639 | printf("[id ] [name ] [ctn] [address ] [as ]"
|
---|
[c0f13d2] | 640 | " [ucycles ] [kcycles ]\n");
|
---|
[52755f1] | 641 | #endif
|
---|
[5ba201d] | 642 |
|
---|
[52755f1] | 643 | #ifdef __64_BITS__
|
---|
[c0f13d2] | 644 | if (additional)
|
---|
[be06914] | 645 | printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
|
---|
[c0f13d2] | 646 | " [callee\n");
|
---|
| 647 | else
|
---|
[473d5d2] | 648 | printf("[id ] [name ] [ctn] [address ]"
|
---|
[c0f13d2] | 649 | " [as ]\n");
|
---|
[52755f1] | 650 | #endif
|
---|
[5ba201d] | 651 |
|
---|
[c0f13d2] | 652 | avltree_walk(&tasks_tree, task_print_walker, &additional);
|
---|
[5ba201d] | 653 |
|
---|
[da1bafb] | 654 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[37c57f2] | 655 | }
|
---|
[7509ddc] | 656 |
|
---|
[cc73a8a1] | 657 | /** @}
|
---|
[b45c443] | 658 | */
|
---|