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