[f761f1eb] | 1 | /*
|
---|
[278b4a30] | 2 | * Copyright (c) 2010 Jakub Jermar
|
---|
[ef1eab7] | 3 | * Copyright (c) 2018 Jiri Svoboda
|
---|
[f761f1eb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[174156fd] | 30 | /** @addtogroup kernel_generic_proc
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[9179d0a] | 34 | /**
|
---|
[b45c443] | 35 | * @file
|
---|
[5ba201d] | 36 | * @brief Task management.
|
---|
[9179d0a] | 37 | */
|
---|
| 38 |
|
---|
[63e27ef] | 39 | #include <assert.h>
|
---|
[f761f1eb] | 40 | #include <proc/thread.h>
|
---|
| 41 | #include <proc/task.h>
|
---|
[20d50a1] | 42 | #include <mm/as.h>
|
---|
[085d973] | 43 | #include <mm/slab.h>
|
---|
[31d8e10] | 44 | #include <atomic.h>
|
---|
[f761f1eb] | 45 | #include <synch/spinlock.h>
|
---|
[5573942] | 46 | #include <synch/waitq.h>
|
---|
[f761f1eb] | 47 | #include <arch.h>
|
---|
[05882233] | 48 | #include <barrier.h>
|
---|
[5c9a08b] | 49 | #include <adt/list.h>
|
---|
[ef1eab7] | 50 | #include <adt/odict.h>
|
---|
[3f74275] | 51 | #include <cap/cap.h>
|
---|
[6d9c49a] | 52 | #include <ipc/ipc.h>
|
---|
[c98e6ee] | 53 | #include <ipc/ipcrsc.h>
|
---|
[5d0500c] | 54 | #include <ipc/event.h>
|
---|
[bab75df6] | 55 | #include <stdio.h>
|
---|
[7509ddc] | 56 | #include <errno.h>
|
---|
[b2e121a] | 57 | #include <halt.h>
|
---|
[19f857a] | 58 | #include <str.h>
|
---|
[e3c762cd] | 59 | #include <syscall/copy.h>
|
---|
[95ad426] | 60 | #include <macros.h>
|
---|
[8e5e78f] | 61 |
|
---|
[ef1eab7] | 62 | /** Spinlock protecting the @c tasks ordered dictionary. */
|
---|
[da1bafb] | 63 | IRQ_SPINLOCK_INITIALIZE(tasks_lock);
|
---|
[88169d9] | 64 |
|
---|
[88cc71c0] | 65 | /** Ordered dictionary of active tasks by task ID.
|
---|
| 66 | *
|
---|
| 67 | * Members are task_t structures.
|
---|
[88169d9] | 68 | *
|
---|
[ef1eab7] | 69 | * The task is guaranteed to exist after it was found in the @c tasks
|
---|
| 70 | * dictionary as long as:
|
---|
[5ba201d] | 71 | *
|
---|
[88169d9] | 72 | * @li the tasks_lock is held,
|
---|
[6f4495f5] | 73 | * @li the task's lock is held when task's lock is acquired before releasing
|
---|
| 74 | * tasks_lock or
|
---|
[7bb6b06] | 75 | * @li the task's refcount is greater than 0
|
---|
[88169d9] | 76 | *
|
---|
| 77 | */
|
---|
[ef1eab7] | 78 | odict_t tasks;
|
---|
[88169d9] | 79 |
|
---|
[286e03d] | 80 | static task_id_t task_counter = 0;
|
---|
[70527f1] | 81 |
|
---|
[82d515e9] | 82 | static slab_cache_t *task_cache;
|
---|
[103de761] | 83 |
|
---|
[121966e] | 84 | /* Forward declarations. */
|
---|
| 85 | static void task_kill_internal(task_t *);
|
---|
[b7fd2a0] | 86 | static errno_t tsk_constructor(void *, unsigned int);
|
---|
[ef1eab7] | 87 | static size_t tsk_destructor(void *);
|
---|
| 88 |
|
---|
| 89 | static void *tasks_getkey(odlink_t *);
|
---|
| 90 | static int tasks_cmp(void *, void *);
|
---|
[121966e] | 91 |
|
---|
[da1bafb] | 92 | /** Initialize kernel tasks support.
|
---|
| 93 | *
|
---|
| 94 | */
|
---|
[f761f1eb] | 95 | void task_init(void)
|
---|
| 96 | {
|
---|
[43114c5] | 97 | TASK = NULL;
|
---|
[ef1eab7] | 98 | odict_initialize(&tasks, tasks_getkey, tasks_cmp);
|
---|
[82d515e9] | 99 | task_cache = slab_cache_create("task_t", sizeof(task_t), 0,
|
---|
[49115ac] | 100 | tsk_constructor, tsk_destructor, 0);
|
---|
[b76a2217] | 101 | }
|
---|
| 102 |
|
---|
[da1bafb] | 103 | /** Kill all tasks except the current task.
|
---|
| 104 | *
|
---|
| 105 | */
|
---|
[f74bbaf] | 106 | void task_done(void)
|
---|
| 107 | {
|
---|
[da1bafb] | 108 | size_t tasks_left;
|
---|
[ef1eab7] | 109 | task_t *task;
|
---|
[2f2beb4] | 110 |
|
---|
[cdc4334] | 111 | if (ipc_box_0) {
|
---|
| 112 | task_t *task_0 = ipc_box_0->task;
|
---|
| 113 | ipc_box_0 = NULL;
|
---|
[2f2beb4] | 114 | /*
|
---|
| 115 | * The first task is held by kinit(), we need to release it or
|
---|
| 116 | * it will never finish cleanup.
|
---|
| 117 | */
|
---|
| 118 | task_release(task_0);
|
---|
| 119 | }
|
---|
[a35b458] | 120 |
|
---|
[da1bafb] | 121 | /* Repeat until there are any tasks except TASK */
|
---|
| 122 | do {
|
---|
[121966e] | 123 | #ifdef CONFIG_DEBUG
|
---|
| 124 | printf("Killing tasks... ");
|
---|
| 125 | #endif
|
---|
[da1bafb] | 126 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[121966e] | 127 | tasks_left = 0;
|
---|
[ef1eab7] | 128 |
|
---|
[aab5e46] | 129 | task = task_first();
|
---|
| 130 | while (task != NULL) {
|
---|
[ef1eab7] | 131 | if (task != TASK) {
|
---|
| 132 | tasks_left++;
|
---|
| 133 | #ifdef CONFIG_DEBUG
|
---|
| 134 | printf("[%" PRIu64 "] ", task->taskid);
|
---|
| 135 | #endif
|
---|
| 136 | task_kill_internal(task);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[aab5e46] | 139 | task = task_next(task);
|
---|
[ef1eab7] | 140 | }
|
---|
| 141 |
|
---|
[da1bafb] | 142 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 143 |
|
---|
[121966e] | 144 | thread_sleep(1);
|
---|
[a35b458] | 145 |
|
---|
[f74bbaf] | 146 | #ifdef CONFIG_DEBUG
|
---|
[121966e] | 147 | printf("\n");
|
---|
| 148 | #endif
|
---|
[da1bafb] | 149 | } while (tasks_left > 0);
|
---|
[f74bbaf] | 150 | }
|
---|
[70527f1] | 151 |
|
---|
[b7fd2a0] | 152 | errno_t tsk_constructor(void *obj, unsigned int kmflags)
|
---|
[59ee56f] | 153 | {
|
---|
[da1bafb] | 154 | task_t *task = (task_t *) obj;
|
---|
[c46bfbc] | 155 |
|
---|
[b7fd2a0] | 156 | errno_t rc = caps_task_alloc(task);
|
---|
[c46bfbc] | 157 | if (rc != EOK)
|
---|
| 158 | return rc;
|
---|
[a35b458] | 159 |
|
---|
[e3306d04] | 160 | atomic_store(&task->lifecount, 0);
|
---|
[a35b458] | 161 |
|
---|
[da1bafb] | 162 | irq_spinlock_initialize(&task->lock, "task_t_lock");
|
---|
[a35b458] | 163 |
|
---|
[55b77d9] | 164 | list_initialize(&task->threads);
|
---|
[a35b458] | 165 |
|
---|
[da1bafb] | 166 | ipc_answerbox_init(&task->answerbox, task);
|
---|
[a35b458] | 167 |
|
---|
[86939b1] | 168 | spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
|
---|
| 169 | list_initialize(&task->active_calls);
|
---|
[a35b458] | 170 |
|
---|
[59ee56f] | 171 | #ifdef CONFIG_UDEBUG
|
---|
| 172 | /* Init kbox stuff */
|
---|
[da1bafb] | 173 | task->kb.thread = NULL;
|
---|
| 174 | ipc_answerbox_init(&task->kb.box, task);
|
---|
| 175 | mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
|
---|
[59ee56f] | 176 | #endif
|
---|
[a35b458] | 177 |
|
---|
[7f11dc6] | 178 | return EOK;
|
---|
[59ee56f] | 179 | }
|
---|
| 180 |
|
---|
[49115ac] | 181 | size_t tsk_destructor(void *obj)
|
---|
| 182 | {
|
---|
| 183 | task_t *task = (task_t *) obj;
|
---|
[a35b458] | 184 |
|
---|
[3f74275] | 185 | caps_task_free(task);
|
---|
[49115ac] | 186 | return 0;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[814c4f5] | 189 | /** Create new task with no threads.
|
---|
[70527f1] | 190 | *
|
---|
[5ba201d] | 191 | * @param as Task's address space.
|
---|
| 192 | * @param name Symbolic name (a copy is made).
|
---|
[70527f1] | 193 | *
|
---|
[5ba201d] | 194 | * @return New task's structure.
|
---|
[70527f1] | 195 | *
|
---|
| 196 | */
|
---|
[a000878c] | 197 | task_t *task_create(as_t *as, const char *name)
|
---|
[f761f1eb] | 198 | {
|
---|
[abf6c01] | 199 | task_t *task = (task_t *) slab_alloc(task_cache, FRAME_ATOMIC);
|
---|
| 200 | if (!task)
|
---|
[6a32cc5f] | 201 | return NULL;
|
---|
[a35b458] | 202 |
|
---|
[07d4271] | 203 | refcount_init(&task->refcount);
|
---|
| 204 |
|
---|
[da1bafb] | 205 | task_create_arch(task);
|
---|
[a35b458] | 206 |
|
---|
[da1bafb] | 207 | task->as = as;
|
---|
| 208 | str_cpy(task->name, TASK_NAME_BUFLEN, name);
|
---|
[a35b458] | 209 |
|
---|
[473d5d2] | 210 | task->container = CONTAINER;
|
---|
[719a208] | 211 | task->perms = 0;
|
---|
[da1bafb] | 212 | task->ucycles = 0;
|
---|
| 213 | task->kcycles = 0;
|
---|
[7ad17de] | 214 |
|
---|
[3f74275] | 215 | caps_task_init(task);
|
---|
[e9d15d9] | 216 |
|
---|
[da1bafb] | 217 | task->ipc_info.call_sent = 0;
|
---|
[be06914] | 218 | task->ipc_info.call_received = 0;
|
---|
[da1bafb] | 219 | task->ipc_info.answer_sent = 0;
|
---|
[be06914] | 220 | task->ipc_info.answer_received = 0;
|
---|
| 221 | task->ipc_info.irq_notif_received = 0;
|
---|
[da1bafb] | 222 | task->ipc_info.forwarded = 0;
|
---|
[5d0500c] | 223 |
|
---|
| 224 | event_task_init(task);
|
---|
[a35b458] | 225 |
|
---|
[c33f39f] | 226 | task->answerbox.active = true;
|
---|
| 227 |
|
---|
[40eab9f] | 228 | task->debug_sections = NULL;
|
---|
| 229 |
|
---|
[9a1b20c] | 230 | #ifdef CONFIG_UDEBUG
|
---|
| 231 | /* Init debugging stuff */
|
---|
[da1bafb] | 232 | udebug_task_init(&task->udebug);
|
---|
[a35b458] | 233 |
|
---|
[9a1b20c] | 234 | /* Init kbox stuff */
|
---|
[c33f39f] | 235 | task->kb.box.active = true;
|
---|
[da1bafb] | 236 | task->kb.finished = false;
|
---|
[9a1b20c] | 237 | #endif
|
---|
[a35b458] | 238 |
|
---|
[cdc4334] | 239 | if ((ipc_box_0) &&
|
---|
| 240 | (container_check(ipc_box_0->task->container, task->container))) {
|
---|
[eadaeae8] | 241 | cap_phone_handle_t phone_handle;
|
---|
[334c103] | 242 | errno_t rc = phone_alloc(task, true, &phone_handle, NULL);
|
---|
[09d01f2] | 243 | if (rc != EOK) {
|
---|
[6a32cc5f] | 244 | task->as = NULL;
|
---|
| 245 | task_destroy_arch(task);
|
---|
| 246 | slab_free(task_cache, task);
|
---|
| 247 | return NULL;
|
---|
| 248 | }
|
---|
[a35b458] | 249 |
|
---|
[48bcf49] | 250 | kobject_t *phone_obj = kobject_get(task, phone_handle,
|
---|
| 251 | KOBJECT_TYPE_PHONE);
|
---|
[cdc4334] | 252 | (void) ipc_phone_connect(phone_obj->phone, ipc_box_0);
|
---|
[05ffb41] | 253 | }
|
---|
[a35b458] | 254 |
|
---|
[da1bafb] | 255 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 256 |
|
---|
[da1bafb] | 257 | task->taskid = ++task_counter;
|
---|
[ef1eab7] | 258 | odlink_initialize(&task->ltasks);
|
---|
| 259 | odict_insert(&task->ltasks, &tasks, NULL);
|
---|
[a35b458] | 260 |
|
---|
[da1bafb] | 261 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 262 |
|
---|
[da1bafb] | 263 | return task;
|
---|
[f761f1eb] | 264 | }
|
---|
| 265 |
|
---|
[7509ddc] | 266 | /** Destroy task.
|
---|
| 267 | *
|
---|
[da1bafb] | 268 | * @param task Task to be destroyed.
|
---|
[5ba201d] | 269 | *
|
---|
[7509ddc] | 270 | */
|
---|
[07d4271] | 271 | static void task_destroy(task_t *task)
|
---|
[7509ddc] | 272 | {
|
---|
[ea7890e7] | 273 | /*
|
---|
[ca4c5596] | 274 | * Remove the task from the task odict.
|
---|
[ea7890e7] | 275 | */
|
---|
[da1bafb] | 276 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[ef1eab7] | 277 | odict_remove(&task->ltasks);
|
---|
[da1bafb] | 278 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 279 |
|
---|
[ea7890e7] | 280 | /*
|
---|
| 281 | * Perform architecture specific task destruction.
|
---|
| 282 | */
|
---|
[da1bafb] | 283 | task_destroy_arch(task);
|
---|
[a35b458] | 284 |
|
---|
[ea7890e7] | 285 | /*
|
---|
| 286 | * Drop our reference to the address space.
|
---|
| 287 | */
|
---|
[da1bafb] | 288 | as_release(task->as);
|
---|
[a35b458] | 289 |
|
---|
[82d515e9] | 290 | slab_free(task_cache, task);
|
---|
[7509ddc] | 291 | }
|
---|
| 292 |
|
---|
[278b4a30] | 293 | /** Hold a reference to a task.
|
---|
| 294 | *
|
---|
| 295 | * Holding a reference to a task prevents destruction of that task.
|
---|
| 296 | *
|
---|
[da1bafb] | 297 | * @param task Task to be held.
|
---|
| 298 | *
|
---|
[278b4a30] | 299 | */
|
---|
[da1bafb] | 300 | void task_hold(task_t *task)
|
---|
[278b4a30] | 301 | {
|
---|
[07d4271] | 302 | refcount_up(&task->refcount);
|
---|
[278b4a30] | 303 | }
|
---|
| 304 |
|
---|
| 305 | /** Release a reference to a task.
|
---|
| 306 | *
|
---|
| 307 | * The last one to release a reference to a task destroys the task.
|
---|
| 308 | *
|
---|
[da1bafb] | 309 | * @param task Task to be released.
|
---|
| 310 | *
|
---|
[278b4a30] | 311 | */
|
---|
[da1bafb] | 312 | void task_release(task_t *task)
|
---|
[278b4a30] | 313 | {
|
---|
[07d4271] | 314 | if (refcount_down(&task->refcount))
|
---|
[da1bafb] | 315 | task_destroy(task);
|
---|
[278b4a30] | 316 | }
|
---|
| 317 |
|
---|
[dd8d5a7] | 318 | #ifdef __32_BITS__
|
---|
| 319 |
|
---|
| 320 | /** Syscall for reading task ID from userspace (32 bits)
|
---|
[ec55358] | 321 | *
|
---|
[dd8d5a7] | 322 | * @param uspace_taskid Pointer to user-space buffer
|
---|
| 323 | * where to store current task ID.
|
---|
[5ba201d] | 324 | *
|
---|
| 325 | * @return Zero on success or an error code from @ref errno.h.
|
---|
[ec55358] | 326 | *
|
---|
| 327 | */
|
---|
[5a5269d] | 328 | sys_errno_t sys_task_get_id(uspace_ptr_sysarg64_t uspace_taskid)
|
---|
[ec55358] | 329 | {
|
---|
| 330 | /*
|
---|
[814c4f5] | 331 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 332 | * the lifespan of the task.
|
---|
[ec55358] | 333 | */
|
---|
[b7fd2a0] | 334 | return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
|
---|
[6f4495f5] | 335 | sizeof(TASK->taskid));
|
---|
[ec55358] | 336 | }
|
---|
| 337 |
|
---|
[dd8d5a7] | 338 | #endif /* __32_BITS__ */
|
---|
| 339 |
|
---|
| 340 | #ifdef __64_BITS__
|
---|
| 341 |
|
---|
| 342 | /** Syscall for reading task ID from userspace (64 bits)
|
---|
| 343 | *
|
---|
| 344 | * @return Current task ID.
|
---|
| 345 | *
|
---|
| 346 | */
|
---|
| 347 | sysarg_t sys_task_get_id(void)
|
---|
| 348 | {
|
---|
| 349 | /*
|
---|
| 350 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 351 | * the lifespan of the task.
|
---|
| 352 | */
|
---|
| 353 | return TASK->taskid;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | #endif /* __64_BITS__ */
|
---|
| 357 |
|
---|
[bc18d63] | 358 | /** Syscall for setting the task name.
|
---|
| 359 | *
|
---|
| 360 | * The name simplifies identifying the task in the task list.
|
---|
| 361 | *
|
---|
[5ba201d] | 362 | * @param name The new name for the task. (typically the same
|
---|
| 363 | * as the command used to execute it).
|
---|
[bc18d63] | 364 | *
|
---|
| 365 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[5ba201d] | 366 | *
|
---|
[bc18d63] | 367 | */
|
---|
[5a5269d] | 368 | sys_errno_t sys_task_set_name(const uspace_ptr_char uspace_name, size_t name_len)
|
---|
[bc18d63] | 369 | {
|
---|
| 370 | char namebuf[TASK_NAME_BUFLEN];
|
---|
[a35b458] | 371 |
|
---|
[bc18d63] | 372 | /* Cap length of name and copy it from userspace. */
|
---|
| 373 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
| 374 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
[a35b458] | 375 |
|
---|
[b7fd2a0] | 376 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
[a53ed3a] | 377 | if (rc != EOK)
|
---|
[b7fd2a0] | 378 | return (sys_errno_t) rc;
|
---|
[a35b458] | 379 |
|
---|
[f4b1535] | 380 | namebuf[name_len] = '\0';
|
---|
[a35b458] | 381 |
|
---|
[577f042a] | 382 | /*
|
---|
| 383 | * As the task name is referenced also from the
|
---|
| 384 | * threads, lock the threads' lock for the course
|
---|
| 385 | * of the update.
|
---|
| 386 | */
|
---|
[a35b458] | 387 |
|
---|
[577f042a] | 388 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 389 | irq_spinlock_lock(&TASK->lock, false);
|
---|
[a35b458] | 390 |
|
---|
[577f042a] | 391 | /* Set task name */
|
---|
[f4b1535] | 392 | str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
|
---|
[a35b458] | 393 |
|
---|
[577f042a] | 394 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
| 395 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 396 |
|
---|
[bc18d63] | 397 | return EOK;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
[1e9f8ab] | 400 | /** Syscall to forcefully terminate a task
|
---|
| 401 | *
|
---|
| 402 | * @param uspace_taskid Pointer to task ID in user space.
|
---|
| 403 | *
|
---|
| 404 | * @return 0 on success or an error code from @ref errno.h.
|
---|
| 405 | *
|
---|
| 406 | */
|
---|
[5a5269d] | 407 | sys_errno_t sys_task_kill(uspace_ptr_task_id_t uspace_taskid)
|
---|
[1e9f8ab] | 408 | {
|
---|
| 409 | task_id_t taskid;
|
---|
[b7fd2a0] | 410 | errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
|
---|
[a53ed3a] | 411 | if (rc != EOK)
|
---|
[b7fd2a0] | 412 | return (sys_errno_t) rc;
|
---|
[a35b458] | 413 |
|
---|
[b7fd2a0] | 414 | return (sys_errno_t) task_kill(taskid);
|
---|
[1e9f8ab] | 415 | }
|
---|
| 416 |
|
---|
[9a8d91b] | 417 | /** Find task structure corresponding to task ID.
|
---|
| 418 | *
|
---|
[5ba201d] | 419 | * @param id Task ID.
|
---|
| 420 | *
|
---|
[07d4271] | 421 | * @return Task reference or NULL if there is no such task ID.
|
---|
[9a8d91b] | 422 | *
|
---|
| 423 | */
|
---|
[5ba201d] | 424 | task_t *task_find_by_id(task_id_t id)
|
---|
| 425 | {
|
---|
[07d4271] | 426 | task_t *task = NULL;
|
---|
| 427 |
|
---|
| 428 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[1d432f9] | 429 |
|
---|
[ef1eab7] | 430 | odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
|
---|
[07d4271] | 431 | if (odlink != NULL) {
|
---|
| 432 | task = odict_get_instance(odlink, task_t, ltasks);
|
---|
[a35b458] | 433 |
|
---|
[07d4271] | 434 | /*
|
---|
| 435 | * The directory of tasks can't hold a reference, since that would
|
---|
| 436 | * prevent task from ever being destroyed. That means we have to
|
---|
| 437 | * check for the case where the task is already being destroyed, but
|
---|
| 438 | * not yet removed from the directory.
|
---|
| 439 | */
|
---|
| 440 | if (!refcount_try_up(&task->refcount))
|
---|
| 441 | task = NULL;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
| 445 |
|
---|
| 446 | return task;
|
---|
[9a8d91b] | 447 | }
|
---|
| 448 |
|
---|
[aab5e46] | 449 | /** Get count of tasks.
|
---|
| 450 | *
|
---|
| 451 | * @return Number of tasks in the system
|
---|
| 452 | */
|
---|
| 453 | size_t task_count(void)
|
---|
| 454 | {
|
---|
| 455 | assert(interrupts_disabled());
|
---|
| 456 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 457 |
|
---|
| 458 | return odict_count(&tasks);
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | /** Get first task (task with lowest ID).
|
---|
| 462 | *
|
---|
| 463 | * @return Pointer to first task or @c NULL if there are none.
|
---|
| 464 | */
|
---|
| 465 | task_t *task_first(void)
|
---|
| 466 | {
|
---|
| 467 | odlink_t *odlink;
|
---|
| 468 |
|
---|
| 469 | assert(interrupts_disabled());
|
---|
| 470 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 471 |
|
---|
| 472 | odlink = odict_first(&tasks);
|
---|
| 473 | if (odlink == NULL)
|
---|
| 474 | return NULL;
|
---|
| 475 |
|
---|
| 476 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | /** Get next task (with higher task ID).
|
---|
| 480 | *
|
---|
| 481 | * @param cur Current task
|
---|
| 482 | * @return Pointer to next task or @c NULL if there are no more tasks.
|
---|
| 483 | */
|
---|
| 484 | task_t *task_next(task_t *cur)
|
---|
| 485 | {
|
---|
| 486 | odlink_t *odlink;
|
---|
| 487 |
|
---|
| 488 | assert(interrupts_disabled());
|
---|
| 489 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 490 |
|
---|
| 491 | odlink = odict_next(&cur->ltasks, &tasks);
|
---|
| 492 | if (odlink == NULL)
|
---|
| 493 | return NULL;
|
---|
| 494 |
|
---|
| 495 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[0313ff0] | 498 | /** Get accounting data of given task.
|
---|
| 499 | *
|
---|
[1d432f9] | 500 | * Note that task lock of 'task' must be already held and interrupts must be
|
---|
[814c4f5] | 501 | * already disabled.
|
---|
[0313ff0] | 502 | *
|
---|
[da1bafb] | 503 | * @param task Pointer to the task.
|
---|
[88dea9d] | 504 | * @param ucycles Out pointer to sum of all user cycles.
|
---|
| 505 | * @param kcycles Out pointer to sum of all kernel cycles.
|
---|
[0313ff0] | 506 | *
|
---|
| 507 | */
|
---|
[da1bafb] | 508 | void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
|
---|
[0313ff0] | 509 | {
|
---|
[63e27ef] | 510 | assert(interrupts_disabled());
|
---|
| 511 | assert(irq_spinlock_locked(&task->lock));
|
---|
[1d432f9] | 512 |
|
---|
[a2a00e8] | 513 | /* Accumulated values of task */
|
---|
[da1bafb] | 514 | uint64_t uret = task->ucycles;
|
---|
| 515 | uint64_t kret = task->kcycles;
|
---|
[a35b458] | 516 |
|
---|
[0313ff0] | 517 | /* Current values of threads */
|
---|
[feeac0d] | 518 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[62b6d17] | 519 | /* Process only counted threads */
|
---|
[da1bafb] | 520 | if (!thread->uncounted) {
|
---|
| 521 | if (thread == THREAD) {
|
---|
[6f4495f5] | 522 | /* Update accounting of current thread */
|
---|
[a2a00e8] | 523 | thread_update_accounting(false);
|
---|
[da1bafb] | 524 | }
|
---|
[a35b458] | 525 |
|
---|
[11909ce3] | 526 | uret += atomic_time_read(&thread->ucycles);
|
---|
| 527 | kret += atomic_time_read(&thread->kcycles);
|
---|
[62b6d17] | 528 | }
|
---|
[0313ff0] | 529 | }
|
---|
[a35b458] | 530 |
|
---|
[a2a00e8] | 531 | *ucycles = uret;
|
---|
| 532 | *kcycles = kret;
|
---|
[0313ff0] | 533 | }
|
---|
| 534 |
|
---|
[da1bafb] | 535 | static void task_kill_internal(task_t *task)
|
---|
[121966e] | 536 | {
|
---|
[07d4271] | 537 | irq_spinlock_lock(&task->lock, true);
|
---|
[a35b458] | 538 |
|
---|
[121966e] | 539 | /*
|
---|
| 540 | * Interrupt all threads.
|
---|
| 541 | */
|
---|
[a35b458] | 542 |
|
---|
[feeac0d] | 543 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[111b9b9] | 544 | thread_interrupt(thread);
|
---|
[121966e] | 545 | }
|
---|
[a35b458] | 546 |
|
---|
[07d4271] | 547 | irq_spinlock_unlock(&task->lock, true);
|
---|
[121966e] | 548 | }
|
---|
| 549 |
|
---|
[7509ddc] | 550 | /** Kill task.
|
---|
[ea7890e7] | 551 | *
|
---|
| 552 | * This function is idempotent.
|
---|
| 553 | * It signals all the task's threads to bail it out.
|
---|
[7509ddc] | 554 | *
|
---|
[5ba201d] | 555 | * @param id ID of the task to be killed.
|
---|
| 556 | *
|
---|
| 557 | * @return Zero on success or an error code from errno.h.
|
---|
[7509ddc] | 558 | *
|
---|
| 559 | */
|
---|
[b7fd2a0] | 560 | errno_t task_kill(task_id_t id)
|
---|
[7509ddc] | 561 | {
|
---|
[9b6aae6] | 562 | if (id == 1)
|
---|
| 563 | return EPERM;
|
---|
[a35b458] | 564 |
|
---|
[da1bafb] | 565 | task_t *task = task_find_by_id(id);
|
---|
[07d4271] | 566 | if (!task)
|
---|
[7509ddc] | 567 | return ENOENT;
|
---|
[a35b458] | 568 |
|
---|
[da1bafb] | 569 | task_kill_internal(task);
|
---|
[07d4271] | 570 | task_release(task);
|
---|
[da1bafb] | 571 | return EOK;
|
---|
[7509ddc] | 572 | }
|
---|
| 573 |
|
---|
[5bcf1f9] | 574 | /** Kill the currently running task.
|
---|
| 575 | *
|
---|
| 576 | * @param notify Send out fault notifications.
|
---|
| 577 | *
|
---|
| 578 | * @return Zero on success or an error code from errno.h.
|
---|
| 579 | *
|
---|
| 580 | */
|
---|
| 581 | void task_kill_self(bool notify)
|
---|
| 582 | {
|
---|
| 583 | /*
|
---|
| 584 | * User space can subscribe for FAULT events to take action
|
---|
| 585 | * whenever a task faults (to take a dump, run a debugger, etc.).
|
---|
| 586 | * The notification is always available, but unless udebug is enabled,
|
---|
| 587 | * that's all you get.
|
---|
[ae7d03c] | 588 | */
|
---|
[5bcf1f9] | 589 | if (notify) {
|
---|
[f9061b4] | 590 | /* Notify the subscriber that a fault occurred. */
|
---|
| 591 | if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
|
---|
| 592 | UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
|
---|
[5bcf1f9] | 593 | #ifdef CONFIG_UDEBUG
|
---|
| 594 | /* Wait for a debugging session. */
|
---|
| 595 | udebug_thread_fault();
|
---|
| 596 | #endif
|
---|
| 597 | }
|
---|
| 598 | }
|
---|
[a35b458] | 599 |
|
---|
[5bcf1f9] | 600 | task_kill_internal(TASK);
|
---|
| 601 | thread_exit();
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | /** Process syscall to terminate the current task.
|
---|
| 605 | *
|
---|
| 606 | * @param notify Send out fault notifications.
|
---|
| 607 | *
|
---|
| 608 | */
|
---|
[b7fd2a0] | 609 | sys_errno_t sys_task_exit(sysarg_t notify)
|
---|
[5bcf1f9] | 610 | {
|
---|
| 611 | task_kill_self(notify);
|
---|
[88e43bc] | 612 | unreachable();
|
---|
[5bcf1f9] | 613 | }
|
---|
| 614 |
|
---|
[ef1eab7] | 615 | static void task_print(task_t *task, bool additional)
|
---|
[b76a2217] | 616 | {
|
---|
[da1bafb] | 617 | irq_spinlock_lock(&task->lock, false);
|
---|
[a35b458] | 618 |
|
---|
[a2a00e8] | 619 | uint64_t ucycles;
|
---|
| 620 | uint64_t kcycles;
|
---|
[1ba37fa] | 621 | char usuffix, ksuffix;
|
---|
[da1bafb] | 622 | task_get_accounting(task, &ucycles, &kcycles);
|
---|
[e535eeb] | 623 | order_suffix(ucycles, &ucycles, &usuffix);
|
---|
| 624 | order_suffix(kcycles, &kcycles, &ksuffix);
|
---|
[a35b458] | 625 |
|
---|
[da1bafb] | 626 | #ifdef __32_BITS__
|
---|
[ef1eab7] | 627 | if (additional)
|
---|
[077842c] | 628 | printf("%-8" PRIu64 " %9zu", task->taskid,
|
---|
[07d4271] | 629 | atomic_load(&task->lifecount));
|
---|
[c0f13d2] | 630 | else
|
---|
| 631 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
|
---|
| 632 | " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
|
---|
[473d5d2] | 633 | task->name, task->container, task, task->as,
|
---|
[c0f13d2] | 634 | ucycles, usuffix, kcycles, ksuffix);
|
---|
[52755f1] | 635 | #endif
|
---|
[a35b458] | 636 |
|
---|
[52755f1] | 637 | #ifdef __64_BITS__
|
---|
[ef1eab7] | 638 | if (additional)
|
---|
[7e752b2] | 639 | printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
|
---|
[077842c] | 640 | "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
|
---|
[07d4271] | 641 | ksuffix, atomic_load(&task->lifecount));
|
---|
[c0f13d2] | 642 | else
|
---|
| 643 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
|
---|
[473d5d2] | 644 | task->taskid, task->name, task->container, task, task->as);
|
---|
[52755f1] | 645 | #endif
|
---|
[a35b458] | 646 |
|
---|
[da1bafb] | 647 | irq_spinlock_unlock(&task->lock, false);
|
---|
[b76a2217] | 648 | }
|
---|
| 649 |
|
---|
[c0f13d2] | 650 | /** Print task list
|
---|
| 651 | *
|
---|
| 652 | * @param additional Print additional information.
|
---|
| 653 | *
|
---|
| 654 | */
|
---|
| 655 | void task_print_list(bool additional)
|
---|
[37c57f2] | 656 | {
|
---|
[f74bbaf] | 657 | /* Messing with task structures, avoid deadlock */
|
---|
[da1bafb] | 658 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 659 |
|
---|
[5ba201d] | 660 | #ifdef __32_BITS__
|
---|
[c0f13d2] | 661 | if (additional)
|
---|
[48dcc69] | 662 | printf("[id ] [threads] [calls] [callee\n");
|
---|
[c0f13d2] | 663 | else
|
---|
[473d5d2] | 664 | printf("[id ] [name ] [ctn] [address ] [as ]"
|
---|
[c0f13d2] | 665 | " [ucycles ] [kcycles ]\n");
|
---|
[52755f1] | 666 | #endif
|
---|
[a35b458] | 667 |
|
---|
[52755f1] | 668 | #ifdef __64_BITS__
|
---|
[c0f13d2] | 669 | if (additional)
|
---|
[be06914] | 670 | printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
|
---|
[c0f13d2] | 671 | " [callee\n");
|
---|
| 672 | else
|
---|
[473d5d2] | 673 | printf("[id ] [name ] [ctn] [address ]"
|
---|
[c0f13d2] | 674 | " [as ]\n");
|
---|
[52755f1] | 675 | #endif
|
---|
[a35b458] | 676 |
|
---|
[ef1eab7] | 677 | task_t *task;
|
---|
| 678 |
|
---|
[aab5e46] | 679 | task = task_first();
|
---|
| 680 | while (task != NULL) {
|
---|
[ef1eab7] | 681 | task_print(task, additional);
|
---|
[aab5e46] | 682 | task = task_next(task);
|
---|
[ef1eab7] | 683 | }
|
---|
[a35b458] | 684 |
|
---|
[da1bafb] | 685 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[37c57f2] | 686 | }
|
---|
[7509ddc] | 687 |
|
---|
[ef1eab7] | 688 | /** Get key function for the @c tasks ordered dictionary.
|
---|
| 689 | *
|
---|
| 690 | * @param odlink Link
|
---|
| 691 | * @return Pointer to task ID cast as 'void *'
|
---|
| 692 | */
|
---|
| 693 | static void *tasks_getkey(odlink_t *odlink)
|
---|
| 694 | {
|
---|
| 695 | task_t *task = odict_get_instance(odlink, task_t, ltasks);
|
---|
| 696 | return (void *) &task->taskid;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | /** Key comparison function for the @c tasks ordered dictionary.
|
---|
| 700 | *
|
---|
| 701 | * @param a Pointer to thread A ID
|
---|
| 702 | * @param b Pointer to thread B ID
|
---|
| 703 | * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
|
---|
| 704 | */
|
---|
| 705 | static int tasks_cmp(void *a, void *b)
|
---|
| 706 | {
|
---|
| 707 | task_id_t ida = *(task_id_t *)a;
|
---|
| 708 | task_id_t idb = *(task_id_t *)b;
|
---|
| 709 |
|
---|
| 710 | if (ida < idb)
|
---|
| 711 | return -1;
|
---|
| 712 | else if (ida == idb)
|
---|
| 713 | return 0;
|
---|
| 714 | else
|
---|
| 715 | return +1;
|
---|
| 716 | }
|
---|
| 717 |
|
---|
[cc73a8a1] | 718 | /** @}
|
---|
[b45c443] | 719 | */
|
---|