[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>
|
---|
[f761f1eb] | 43 | #include <synch/spinlock.h>
|
---|
[5573942] | 44 | #include <synch/waitq.h>
|
---|
[f761f1eb] | 45 | #include <arch.h>
|
---|
[8605b24] | 46 | #include <arch/barrier.h>
|
---|
[b76a2217] | 47 | #include <adt/avl.h>
|
---|
[7f6e755] | 48 | #include <adt/btree.h>
|
---|
[5c9a08b] | 49 | #include <adt/list.h>
|
---|
[6d9c49a] | 50 | #include <ipc/ipc.h>
|
---|
[c98e6ee] | 51 | #include <ipc/ipcrsc.h>
|
---|
[37c57f2] | 52 | #include <print.h>
|
---|
[7509ddc] | 53 | #include <errno.h>
|
---|
[95155b0c] | 54 | #include <func.h>
|
---|
[19f857a] | 55 | #include <str.h>
|
---|
[41df2827] | 56 | #include <memstr.h>
|
---|
[e3c762cd] | 57 | #include <syscall/copy.h>
|
---|
[95ad426] | 58 | #include <macros.h>
|
---|
| 59 | #include <ipc/event.h>
|
---|
[8e5e78f] | 60 |
|
---|
[b76a2217] | 61 | /** Spinlock protecting the tasks_tree AVL tree. */
|
---|
[da1bafb] | 62 | IRQ_SPINLOCK_INITIALIZE(tasks_lock);
|
---|
[88169d9] | 63 |
|
---|
[b76a2217] | 64 | /** AVL tree of active tasks.
|
---|
[88169d9] | 65 | *
|
---|
[b76a2217] | 66 | * The task is guaranteed to exist after it was found in the tasks_tree as
|
---|
[6f4495f5] | 67 | * long as:
|
---|
[5ba201d] | 68 | *
|
---|
[88169d9] | 69 | * @li the tasks_lock is held,
|
---|
[6f4495f5] | 70 | * @li the task's lock is held when task's lock is acquired before releasing
|
---|
| 71 | * tasks_lock or
|
---|
[7bb6b06] | 72 | * @li the task's refcount is greater than 0
|
---|
[88169d9] | 73 | *
|
---|
| 74 | */
|
---|
[b76a2217] | 75 | avltree_t tasks_tree;
|
---|
[88169d9] | 76 |
|
---|
[286e03d] | 77 | static task_id_t task_counter = 0;
|
---|
[70527f1] | 78 |
|
---|
[103de761] | 79 | static slab_cache_t *task_slab;
|
---|
| 80 |
|
---|
[121966e] | 81 | /* Forward declarations. */
|
---|
| 82 | static void task_kill_internal(task_t *);
|
---|
[da1bafb] | 83 | static int tsk_constructor(void *, unsigned int);
|
---|
[121966e] | 84 |
|
---|
[da1bafb] | 85 | /** Initialize kernel tasks support.
|
---|
| 86 | *
|
---|
| 87 | */
|
---|
[f761f1eb] | 88 | void task_init(void)
|
---|
| 89 | {
|
---|
[43114c5] | 90 | TASK = NULL;
|
---|
[b76a2217] | 91 | avltree_create(&tasks_tree);
|
---|
[59ee56f] | 92 | task_slab = slab_cache_create("task_slab", sizeof(task_t), 0,
|
---|
| 93 | tsk_constructor, NULL, 0);
|
---|
[b76a2217] | 94 | }
|
---|
| 95 |
|
---|
[da1bafb] | 96 | /** Task finish walker.
|
---|
| 97 | *
|
---|
[121966e] | 98 | * The idea behind this walker is to kill and count all tasks different from
|
---|
[814c4f5] | 99 | * TASK.
|
---|
[da1bafb] | 100 | *
|
---|
[b76a2217] | 101 | */
|
---|
| 102 | static bool task_done_walker(avltree_node_t *node, void *arg)
|
---|
| 103 | {
|
---|
[da1bafb] | 104 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
| 105 | size_t *cnt = (size_t *) arg;
|
---|
[5ba201d] | 106 |
|
---|
[da1bafb] | 107 | if (task != TASK) {
|
---|
[121966e] | 108 | (*cnt)++;
|
---|
[da1bafb] | 109 |
|
---|
[121966e] | 110 | #ifdef CONFIG_DEBUG
|
---|
[da1bafb] | 111 | printf("[%"PRIu64"] ", task->taskid);
|
---|
[121966e] | 112 | #endif
|
---|
[da1bafb] | 113 |
|
---|
| 114 | task_kill_internal(task);
|
---|
[b76a2217] | 115 | }
|
---|
[5ba201d] | 116 |
|
---|
| 117 | /* Continue the walk */
|
---|
| 118 | return true;
|
---|
[f761f1eb] | 119 | }
|
---|
| 120 |
|
---|
[da1bafb] | 121 | /** Kill all tasks except the current task.
|
---|
| 122 | *
|
---|
| 123 | */
|
---|
[f74bbaf] | 124 | void task_done(void)
|
---|
| 125 | {
|
---|
[da1bafb] | 126 | size_t tasks_left;
|
---|
[5ba201d] | 127 |
|
---|
[da1bafb] | 128 | /* Repeat until there are any tasks except TASK */
|
---|
| 129 | do {
|
---|
[121966e] | 130 | #ifdef CONFIG_DEBUG
|
---|
| 131 | printf("Killing tasks... ");
|
---|
| 132 | #endif
|
---|
[da1bafb] | 133 |
|
---|
| 134 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[121966e] | 135 | tasks_left = 0;
|
---|
| 136 | avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
|
---|
[da1bafb] | 137 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
| 138 |
|
---|
[121966e] | 139 | thread_sleep(1);
|
---|
[da1bafb] | 140 |
|
---|
[f74bbaf] | 141 | #ifdef CONFIG_DEBUG
|
---|
[121966e] | 142 | printf("\n");
|
---|
| 143 | #endif
|
---|
[da1bafb] | 144 | } while (tasks_left > 0);
|
---|
[f74bbaf] | 145 | }
|
---|
[70527f1] | 146 |
|
---|
[da1bafb] | 147 | int tsk_constructor(void *obj, unsigned int kmflags)
|
---|
[59ee56f] | 148 | {
|
---|
[da1bafb] | 149 | task_t *task = (task_t *) obj;
|
---|
| 150 |
|
---|
| 151 | atomic_set(&task->refcount, 0);
|
---|
| 152 | atomic_set(&task->lifecount, 0);
|
---|
| 153 | atomic_set(&task->active_calls, 0);
|
---|
[5ba201d] | 154 |
|
---|
[da1bafb] | 155 | irq_spinlock_initialize(&task->lock, "task_t_lock");
|
---|
| 156 | mutex_initialize(&task->futexes_lock, MUTEX_PASSIVE);
|
---|
[5ba201d] | 157 |
|
---|
[da1bafb] | 158 | list_initialize(&task->th_head);
|
---|
| 159 | list_initialize(&task->sync_box_head);
|
---|
[5ba201d] | 160 |
|
---|
[da1bafb] | 161 | ipc_answerbox_init(&task->answerbox, task);
|
---|
[5ba201d] | 162 |
|
---|
[da1bafb] | 163 | size_t i;
|
---|
[59ee56f] | 164 | for (i = 0; i < IPC_MAX_PHONES; i++)
|
---|
[da1bafb] | 165 | ipc_phone_init(&task->phones[i]);
|
---|
[5ba201d] | 166 |
|
---|
[59ee56f] | 167 | #ifdef CONFIG_UDEBUG
|
---|
| 168 | /* Init kbox stuff */
|
---|
[da1bafb] | 169 | task->kb.thread = NULL;
|
---|
| 170 | ipc_answerbox_init(&task->kb.box, task);
|
---|
| 171 | mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
|
---|
[59ee56f] | 172 | #endif
|
---|
[5ba201d] | 173 |
|
---|
[59ee56f] | 174 | return 0;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[814c4f5] | 177 | /** Create new task with no threads.
|
---|
[70527f1] | 178 | *
|
---|
[5ba201d] | 179 | * @param as Task's address space.
|
---|
| 180 | * @param name Symbolic name (a copy is made).
|
---|
[70527f1] | 181 | *
|
---|
[5ba201d] | 182 | * @return New task's structure.
|
---|
[70527f1] | 183 | *
|
---|
| 184 | */
|
---|
[a000878c] | 185 | task_t *task_create(as_t *as, const char *name)
|
---|
[f761f1eb] | 186 | {
|
---|
[da1bafb] | 187 | task_t *task = (task_t *) slab_alloc(task_slab, 0);
|
---|
| 188 | task_create_arch(task);
|
---|
| 189 |
|
---|
| 190 | task->as = as;
|
---|
| 191 | str_cpy(task->name, TASK_NAME_BUFLEN, name);
|
---|
| 192 |
|
---|
| 193 | task->context = CONTEXT;
|
---|
| 194 | task->capabilities = 0;
|
---|
| 195 | task->ucycles = 0;
|
---|
| 196 | task->kcycles = 0;
|
---|
| 197 |
|
---|
| 198 | task->ipc_info.call_sent = 0;
|
---|
| 199 | task->ipc_info.call_recieved = 0;
|
---|
| 200 | task->ipc_info.answer_sent = 0;
|
---|
| 201 | task->ipc_info.answer_recieved = 0;
|
---|
| 202 | task->ipc_info.irq_notif_recieved = 0;
|
---|
| 203 | task->ipc_info.forwarded = 0;
|
---|
| 204 |
|
---|
[9a1b20c] | 205 | #ifdef CONFIG_UDEBUG
|
---|
| 206 | /* Init debugging stuff */
|
---|
[da1bafb] | 207 | udebug_task_init(&task->udebug);
|
---|
[5ba201d] | 208 |
|
---|
[9a1b20c] | 209 | /* Init kbox stuff */
|
---|
[da1bafb] | 210 | task->kb.finished = false;
|
---|
[9a1b20c] | 211 | #endif
|
---|
[5ba201d] | 212 |
|
---|
[59ee56f] | 213 | if ((ipc_phone_0) &&
|
---|
[da1bafb] | 214 | (context_check(ipc_phone_0->task->context, task->context)))
|
---|
| 215 | ipc_phone_connect(&task->phones[0], ipc_phone_0);
|
---|
[5ba201d] | 216 |
|
---|
[da1bafb] | 217 | btree_create(&task->futexes);
|
---|
[bb68433] | 218 |
|
---|
[6193351] | 219 | /*
|
---|
| 220 | * Get a reference to the address space.
|
---|
| 221 | */
|
---|
[da1bafb] | 222 | as_hold(task->as);
|
---|
| 223 |
|
---|
| 224 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 225 |
|
---|
| 226 | task->taskid = ++task_counter;
|
---|
| 227 | avltree_node_initialize(&task->tasks_tree_node);
|
---|
| 228 | task->tasks_tree_node.key = task->taskid;
|
---|
| 229 | avltree_insert(&tasks_tree, &task->tasks_tree_node);
|
---|
| 230 |
|
---|
| 231 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
| 232 |
|
---|
| 233 | return task;
|
---|
[f761f1eb] | 234 | }
|
---|
| 235 |
|
---|
[7509ddc] | 236 | /** Destroy task.
|
---|
| 237 | *
|
---|
[da1bafb] | 238 | * @param task Task to be destroyed.
|
---|
[5ba201d] | 239 | *
|
---|
[7509ddc] | 240 | */
|
---|
[da1bafb] | 241 | void task_destroy(task_t *task)
|
---|
[7509ddc] | 242 | {
|
---|
[ea7890e7] | 243 | /*
|
---|
| 244 | * Remove the task from the task B+tree.
|
---|
| 245 | */
|
---|
[da1bafb] | 246 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 247 | avltree_delete(&tasks_tree, &task->tasks_tree_node);
|
---|
| 248 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[5ba201d] | 249 |
|
---|
[ea7890e7] | 250 | /*
|
---|
| 251 | * Perform architecture specific task destruction.
|
---|
| 252 | */
|
---|
[da1bafb] | 253 | task_destroy_arch(task);
|
---|
[5ba201d] | 254 |
|
---|
[ea7890e7] | 255 | /*
|
---|
| 256 | * Free up dynamically allocated state.
|
---|
| 257 | */
|
---|
[da1bafb] | 258 | btree_destroy(&task->futexes);
|
---|
[5ba201d] | 259 |
|
---|
[ea7890e7] | 260 | /*
|
---|
| 261 | * Drop our reference to the address space.
|
---|
| 262 | */
|
---|
[da1bafb] | 263 | as_release(task->as);
|
---|
[31e8ddd] | 264 |
|
---|
[da1bafb] | 265 | slab_free(task_slab, task);
|
---|
[7509ddc] | 266 | }
|
---|
| 267 |
|
---|
[278b4a30] | 268 | /** Hold a reference to a task.
|
---|
| 269 | *
|
---|
| 270 | * Holding a reference to a task prevents destruction of that task.
|
---|
| 271 | *
|
---|
[da1bafb] | 272 | * @param task Task to be held.
|
---|
| 273 | *
|
---|
[278b4a30] | 274 | */
|
---|
[da1bafb] | 275 | void task_hold(task_t *task)
|
---|
[278b4a30] | 276 | {
|
---|
[da1bafb] | 277 | atomic_inc(&task->refcount);
|
---|
[278b4a30] | 278 | }
|
---|
| 279 |
|
---|
| 280 | /** Release a reference to a task.
|
---|
| 281 | *
|
---|
| 282 | * The last one to release a reference to a task destroys the task.
|
---|
| 283 | *
|
---|
[da1bafb] | 284 | * @param task Task to be released.
|
---|
| 285 | *
|
---|
[278b4a30] | 286 | */
|
---|
[da1bafb] | 287 | void task_release(task_t *task)
|
---|
[278b4a30] | 288 | {
|
---|
[da1bafb] | 289 | if ((atomic_predec(&task->refcount)) == 0)
|
---|
| 290 | task_destroy(task);
|
---|
[278b4a30] | 291 | }
|
---|
| 292 |
|
---|
[ec55358] | 293 | /** Syscall for reading task ID from userspace.
|
---|
| 294 | *
|
---|
[5ba201d] | 295 | * @param uspace_task_id Userspace address of 8-byte buffer
|
---|
| 296 | * where to store current task ID.
|
---|
| 297 | *
|
---|
| 298 | * @return Zero on success or an error code from @ref errno.h.
|
---|
[ec55358] | 299 | *
|
---|
| 300 | */
|
---|
[7f1c620] | 301 | unative_t sys_task_get_id(task_id_t *uspace_task_id)
|
---|
[ec55358] | 302 | {
|
---|
| 303 | /*
|
---|
[814c4f5] | 304 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 305 | * the lifespan of the task.
|
---|
[ec55358] | 306 | */
|
---|
[6f4495f5] | 307 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
|
---|
| 308 | sizeof(TASK->taskid));
|
---|
[ec55358] | 309 | }
|
---|
| 310 |
|
---|
[bc18d63] | 311 | /** Syscall for setting the task name.
|
---|
| 312 | *
|
---|
| 313 | * The name simplifies identifying the task in the task list.
|
---|
| 314 | *
|
---|
[5ba201d] | 315 | * @param name The new name for the task. (typically the same
|
---|
| 316 | * as the command used to execute it).
|
---|
[bc18d63] | 317 | *
|
---|
| 318 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[5ba201d] | 319 | *
|
---|
[bc18d63] | 320 | */
|
---|
| 321 | unative_t sys_task_set_name(const char *uspace_name, size_t name_len)
|
---|
| 322 | {
|
---|
| 323 | int rc;
|
---|
| 324 | char namebuf[TASK_NAME_BUFLEN];
|
---|
[5ba201d] | 325 |
|
---|
[bc18d63] | 326 | /* Cap length of name and copy it from userspace. */
|
---|
[5ba201d] | 327 |
|
---|
[bc18d63] | 328 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
| 329 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
[5ba201d] | 330 |
|
---|
[bc18d63] | 331 | rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
| 332 | if (rc != 0)
|
---|
| 333 | return (unative_t) rc;
|
---|
[5ba201d] | 334 |
|
---|
[f4b1535] | 335 | namebuf[name_len] = '\0';
|
---|
| 336 | str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
|
---|
[5ba201d] | 337 |
|
---|
[bc18d63] | 338 | return EOK;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[9a8d91b] | 341 | /** Find task structure corresponding to task ID.
|
---|
| 342 | *
|
---|
[814c4f5] | 343 | * The tasks_lock must be already held by the caller of this function and
|
---|
| 344 | * interrupts must be disabled.
|
---|
[9a8d91b] | 345 | *
|
---|
[5ba201d] | 346 | * @param id Task ID.
|
---|
| 347 | *
|
---|
[e1b6742] | 348 | * @return Task structure address or NULL if there is no such task ID.
|
---|
[9a8d91b] | 349 | *
|
---|
| 350 | */
|
---|
[5ba201d] | 351 | task_t *task_find_by_id(task_id_t id)
|
---|
| 352 | {
|
---|
[e1b6742] | 353 | avltree_node_t *node =
|
---|
| 354 | avltree_search(&tasks_tree, (avltree_key_t) id);
|
---|
[5ba201d] | 355 |
|
---|
[b76a2217] | 356 | if (node)
|
---|
[da1bafb] | 357 | return avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
[5ba201d] | 358 |
|
---|
[b76a2217] | 359 | return NULL;
|
---|
[9a8d91b] | 360 | }
|
---|
| 361 |
|
---|
[0313ff0] | 362 | /** Get accounting data of given task.
|
---|
| 363 | *
|
---|
[814c4f5] | 364 | * Note that task lock of 't' must be already held and interrupts must be
|
---|
| 365 | * already disabled.
|
---|
[0313ff0] | 366 | *
|
---|
[da1bafb] | 367 | * @param task Pointer to the task.
|
---|
[88dea9d] | 368 | * @param ucycles Out pointer to sum of all user cycles.
|
---|
| 369 | * @param kcycles Out pointer to sum of all kernel cycles.
|
---|
[0313ff0] | 370 | *
|
---|
| 371 | */
|
---|
[da1bafb] | 372 | void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
|
---|
[0313ff0] | 373 | {
|
---|
[a2a00e8] | 374 | /* Accumulated values of task */
|
---|
[da1bafb] | 375 | uint64_t uret = task->ucycles;
|
---|
| 376 | uint64_t kret = task->kcycles;
|
---|
[0313ff0] | 377 |
|
---|
| 378 | /* Current values of threads */
|
---|
| 379 | link_t *cur;
|
---|
[da1bafb] | 380 | for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
|
---|
| 381 | thread_t *thread = list_get_instance(cur, thread_t, th_link);
|
---|
| 382 |
|
---|
| 383 | irq_spinlock_lock(&thread->lock, false);
|
---|
[0313ff0] | 384 |
|
---|
[62b6d17] | 385 | /* Process only counted threads */
|
---|
[da1bafb] | 386 | if (!thread->uncounted) {
|
---|
| 387 | if (thread == THREAD) {
|
---|
[6f4495f5] | 388 | /* Update accounting of current thread */
|
---|
[a2a00e8] | 389 | thread_update_accounting(false);
|
---|
[da1bafb] | 390 | }
|
---|
| 391 |
|
---|
| 392 | uret += thread->ucycles;
|
---|
| 393 | kret += thread->kcycles;
|
---|
[62b6d17] | 394 | }
|
---|
[da1bafb] | 395 |
|
---|
| 396 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[0313ff0] | 397 | }
|
---|
| 398 |
|
---|
[a2a00e8] | 399 | *ucycles = uret;
|
---|
| 400 | *kcycles = kret;
|
---|
[0313ff0] | 401 | }
|
---|
| 402 |
|
---|
[da1bafb] | 403 | static void task_kill_internal(task_t *task)
|
---|
[121966e] | 404 | {
|
---|
| 405 | link_t *cur;
|
---|
[5ba201d] | 406 |
|
---|
[121966e] | 407 | /*
|
---|
| 408 | * Interrupt all threads.
|
---|
| 409 | */
|
---|
[da1bafb] | 410 | irq_spinlock_lock(&task->lock, false);
|
---|
| 411 | for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
|
---|
| 412 | thread_t *thread = list_get_instance(cur, thread_t, th_link);
|
---|
[121966e] | 413 | bool sleeping = false;
|
---|
| 414 |
|
---|
[da1bafb] | 415 | irq_spinlock_lock(&thread->lock, false);
|
---|
[121966e] | 416 |
|
---|
[da1bafb] | 417 | thread->interrupted = true;
|
---|
| 418 | if (thread->state == Sleeping)
|
---|
[121966e] | 419 | sleeping = true;
|
---|
[da1bafb] | 420 |
|
---|
| 421 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[121966e] | 422 |
|
---|
| 423 | if (sleeping)
|
---|
[da1bafb] | 424 | waitq_interrupt_sleep(thread);
|
---|
[121966e] | 425 | }
|
---|
[da1bafb] | 426 |
|
---|
| 427 | irq_spinlock_unlock(&task->lock, false);
|
---|
[121966e] | 428 | }
|
---|
| 429 |
|
---|
[7509ddc] | 430 | /** Kill task.
|
---|
[ea7890e7] | 431 | *
|
---|
| 432 | * This function is idempotent.
|
---|
| 433 | * It signals all the task's threads to bail it out.
|
---|
[7509ddc] | 434 | *
|
---|
[5ba201d] | 435 | * @param id ID of the task to be killed.
|
---|
| 436 | *
|
---|
| 437 | * @return Zero on success or an error code from errno.h.
|
---|
[7509ddc] | 438 | *
|
---|
| 439 | */
|
---|
| 440 | int task_kill(task_id_t id)
|
---|
| 441 | {
|
---|
[9b6aae6] | 442 | if (id == 1)
|
---|
| 443 | return EPERM;
|
---|
[7509ddc] | 444 |
|
---|
[da1bafb] | 445 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 446 |
|
---|
| 447 | task_t *task = task_find_by_id(id);
|
---|
| 448 | if (!task) {
|
---|
| 449 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[7509ddc] | 450 | return ENOENT;
|
---|
| 451 | }
|
---|
[da1bafb] | 452 |
|
---|
| 453 | task_kill_internal(task);
|
---|
| 454 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
| 455 |
|
---|
| 456 | return EOK;
|
---|
[7509ddc] | 457 | }
|
---|
| 458 |
|
---|
[b76a2217] | 459 | static bool task_print_walker(avltree_node_t *node, void *arg)
|
---|
| 460 | {
|
---|
[da1bafb] | 461 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
| 462 | irq_spinlock_lock(&task->lock, false);
|
---|
[5ba201d] | 463 |
|
---|
[a2a00e8] | 464 | uint64_t ucycles;
|
---|
| 465 | uint64_t kcycles;
|
---|
[1ba37fa] | 466 | char usuffix, ksuffix;
|
---|
[da1bafb] | 467 | task_get_accounting(task, &ucycles, &kcycles);
|
---|
[e535eeb] | 468 | order_suffix(ucycles, &ucycles, &usuffix);
|
---|
| 469 | order_suffix(kcycles, &kcycles, &ksuffix);
|
---|
[88dea9d] | 470 |
|
---|
[da1bafb] | 471 | #ifdef __32_BITS__
|
---|
[07640dfd] | 472 | printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %10p %10p %9" PRIu64 "%c %9"
|
---|
[da1bafb] | 473 | PRIu64 "%c %7ld %6ld", task->taskid, task->name, task->context,
|
---|
| 474 | task, task->as, ucycles, usuffix, kcycles, ksuffix,
|
---|
| 475 | atomic_get(&task->refcount), atomic_get(&task->active_calls));
|
---|
[52755f1] | 476 | #endif
|
---|
[5ba201d] | 477 |
|
---|
[52755f1] | 478 | #ifdef __64_BITS__
|
---|
[07640dfd] | 479 | printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %18p %18p %9" PRIu64 "%c %9"
|
---|
[da1bafb] | 480 | PRIu64 "%c %7ld %6ld", task->taskid, task->name, task->context,
|
---|
| 481 | task, task->as, ucycles, usuffix, kcycles, ksuffix,
|
---|
| 482 | atomic_get(&task->refcount), atomic_get(&task->active_calls));
|
---|
[52755f1] | 483 | #endif
|
---|
[5ba201d] | 484 |
|
---|
[da1bafb] | 485 | size_t i;
|
---|
| 486 | for (i = 0; i < IPC_MAX_PHONES; i++) {
|
---|
| 487 | if (task->phones[i].callee)
|
---|
| 488 | printf(" %" PRIs ":%p", i, task->phones[i].callee);
|
---|
[b76a2217] | 489 | }
|
---|
| 490 | printf("\n");
|
---|
[5ba201d] | 491 |
|
---|
[da1bafb] | 492 | irq_spinlock_unlock(&task->lock, false);
|
---|
[b76a2217] | 493 | return true;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
[37c57f2] | 496 | /** Print task list */
|
---|
| 497 | void task_print_list(void)
|
---|
| 498 | {
|
---|
[f74bbaf] | 499 | /* Messing with task structures, avoid deadlock */
|
---|
[da1bafb] | 500 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[5ba201d] | 501 |
|
---|
| 502 | #ifdef __32_BITS__
|
---|
[a2a00e8] | 503 | printf("taskid name ctx address as "
|
---|
[07640dfd] | 504 | " ucycles kcycles threads calls callee\n");
|
---|
[a2a00e8] | 505 | printf("------ ------------ --- ---------- ----------"
|
---|
[07640dfd] | 506 | " ---------- ---------- ------- ------ ------>\n");
|
---|
[52755f1] | 507 | #endif
|
---|
[5ba201d] | 508 |
|
---|
[52755f1] | 509 | #ifdef __64_BITS__
|
---|
[a2a00e8] | 510 | printf("taskid name ctx address as "
|
---|
[07640dfd] | 511 | " ucycles kcycles threads calls callee\n");
|
---|
[a2a00e8] | 512 | printf("------ ------------ --- ------------------ ------------------"
|
---|
[07640dfd] | 513 | " ---------- ---------- ---------- ------- ------ ------>\n");
|
---|
[52755f1] | 514 | #endif
|
---|
[5ba201d] | 515 |
|
---|
[b76a2217] | 516 | avltree_walk(&tasks_tree, task_print_walker, NULL);
|
---|
[5ba201d] | 517 |
|
---|
[da1bafb] | 518 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[37c57f2] | 519 | }
|
---|
[7509ddc] | 520 |
|
---|
[cc73a8a1] | 521 | /** @}
|
---|
[b45c443] | 522 | */
|
---|