[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->refcount, 0);
|
---|
| 161 | atomic_store(&task->lifecount, 0);
|
---|
[a35b458] | 162 |
|
---|
[da1bafb] | 163 | irq_spinlock_initialize(&task->lock, "task_t_lock");
|
---|
[a35b458] | 164 |
|
---|
[55b77d9] | 165 | list_initialize(&task->threads);
|
---|
[a35b458] | 166 |
|
---|
[da1bafb] | 167 | ipc_answerbox_init(&task->answerbox, task);
|
---|
[a35b458] | 168 |
|
---|
[86939b1] | 169 | spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
|
---|
| 170 | list_initialize(&task->active_calls);
|
---|
[a35b458] | 171 |
|
---|
[59ee56f] | 172 | #ifdef CONFIG_UDEBUG
|
---|
| 173 | /* Init kbox stuff */
|
---|
[da1bafb] | 174 | task->kb.thread = NULL;
|
---|
| 175 | ipc_answerbox_init(&task->kb.box, task);
|
---|
| 176 | mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
|
---|
[59ee56f] | 177 | #endif
|
---|
[a35b458] | 178 |
|
---|
[7f11dc6] | 179 | return EOK;
|
---|
[59ee56f] | 180 | }
|
---|
| 181 |
|
---|
[49115ac] | 182 | size_t tsk_destructor(void *obj)
|
---|
| 183 | {
|
---|
| 184 | task_t *task = (task_t *) obj;
|
---|
[a35b458] | 185 |
|
---|
[3f74275] | 186 | caps_task_free(task);
|
---|
[49115ac] | 187 | return 0;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[814c4f5] | 190 | /** Create new task with no threads.
|
---|
[70527f1] | 191 | *
|
---|
[5ba201d] | 192 | * @param as Task's address space.
|
---|
| 193 | * @param name Symbolic name (a copy is made).
|
---|
[70527f1] | 194 | *
|
---|
[5ba201d] | 195 | * @return New task's structure.
|
---|
[70527f1] | 196 | *
|
---|
| 197 | */
|
---|
[a000878c] | 198 | task_t *task_create(as_t *as, const char *name)
|
---|
[f761f1eb] | 199 | {
|
---|
[abf6c01] | 200 | task_t *task = (task_t *) slab_alloc(task_cache, FRAME_ATOMIC);
|
---|
| 201 | if (!task)
|
---|
[6a32cc5f] | 202 | return NULL;
|
---|
[a35b458] | 203 |
|
---|
[da1bafb] | 204 | task_create_arch(task);
|
---|
[a35b458] | 205 |
|
---|
[da1bafb] | 206 | task->as = as;
|
---|
| 207 | str_cpy(task->name, TASK_NAME_BUFLEN, name);
|
---|
[a35b458] | 208 |
|
---|
[473d5d2] | 209 | task->container = CONTAINER;
|
---|
[719a208] | 210 | task->perms = 0;
|
---|
[da1bafb] | 211 | task->ucycles = 0;
|
---|
| 212 | task->kcycles = 0;
|
---|
[7ad17de] | 213 |
|
---|
[3f74275] | 214 | caps_task_init(task);
|
---|
[e9d15d9] | 215 |
|
---|
[da1bafb] | 216 | task->ipc_info.call_sent = 0;
|
---|
[be06914] | 217 | task->ipc_info.call_received = 0;
|
---|
[da1bafb] | 218 | task->ipc_info.answer_sent = 0;
|
---|
[be06914] | 219 | task->ipc_info.answer_received = 0;
|
---|
| 220 | task->ipc_info.irq_notif_received = 0;
|
---|
[da1bafb] | 221 | task->ipc_info.forwarded = 0;
|
---|
[5d0500c] | 222 |
|
---|
| 223 | event_task_init(task);
|
---|
[a35b458] | 224 |
|
---|
[c33f39f] | 225 | task->answerbox.active = true;
|
---|
| 226 |
|
---|
[9a1b20c] | 227 | #ifdef CONFIG_UDEBUG
|
---|
| 228 | /* Init debugging stuff */
|
---|
[da1bafb] | 229 | udebug_task_init(&task->udebug);
|
---|
[a35b458] | 230 |
|
---|
[9a1b20c] | 231 | /* Init kbox stuff */
|
---|
[c33f39f] | 232 | task->kb.box.active = true;
|
---|
[da1bafb] | 233 | task->kb.finished = false;
|
---|
[9a1b20c] | 234 | #endif
|
---|
[a35b458] | 235 |
|
---|
[cdc4334] | 236 | if ((ipc_box_0) &&
|
---|
| 237 | (container_check(ipc_box_0->task->container, task->container))) {
|
---|
[eadaeae8] | 238 | cap_phone_handle_t phone_handle;
|
---|
[334c103] | 239 | errno_t rc = phone_alloc(task, true, &phone_handle, NULL);
|
---|
[09d01f2] | 240 | if (rc != EOK) {
|
---|
[6a32cc5f] | 241 | task->as = NULL;
|
---|
| 242 | task_destroy_arch(task);
|
---|
| 243 | slab_free(task_cache, task);
|
---|
| 244 | return NULL;
|
---|
| 245 | }
|
---|
[a35b458] | 246 |
|
---|
[48bcf49] | 247 | kobject_t *phone_obj = kobject_get(task, phone_handle,
|
---|
| 248 | KOBJECT_TYPE_PHONE);
|
---|
[cdc4334] | 249 | (void) ipc_phone_connect(phone_obj->phone, ipc_box_0);
|
---|
[05ffb41] | 250 | }
|
---|
[a35b458] | 251 |
|
---|
[da1bafb] | 252 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 253 |
|
---|
[da1bafb] | 254 | task->taskid = ++task_counter;
|
---|
[ef1eab7] | 255 | odlink_initialize(&task->ltasks);
|
---|
| 256 | odict_insert(&task->ltasks, &tasks, NULL);
|
---|
[a35b458] | 257 |
|
---|
[da1bafb] | 258 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 259 |
|
---|
[da1bafb] | 260 | return task;
|
---|
[f761f1eb] | 261 | }
|
---|
| 262 |
|
---|
[7509ddc] | 263 | /** Destroy task.
|
---|
| 264 | *
|
---|
[da1bafb] | 265 | * @param task Task to be destroyed.
|
---|
[5ba201d] | 266 | *
|
---|
[7509ddc] | 267 | */
|
---|
[da1bafb] | 268 | void task_destroy(task_t *task)
|
---|
[7509ddc] | 269 | {
|
---|
[ea7890e7] | 270 | /*
|
---|
[ca4c5596] | 271 | * Remove the task from the task odict.
|
---|
[ea7890e7] | 272 | */
|
---|
[da1bafb] | 273 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[ef1eab7] | 274 | odict_remove(&task->ltasks);
|
---|
[da1bafb] | 275 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 276 |
|
---|
[ea7890e7] | 277 | /*
|
---|
| 278 | * Perform architecture specific task destruction.
|
---|
| 279 | */
|
---|
[da1bafb] | 280 | task_destroy_arch(task);
|
---|
[a35b458] | 281 |
|
---|
[ea7890e7] | 282 | /*
|
---|
| 283 | * Drop our reference to the address space.
|
---|
| 284 | */
|
---|
[da1bafb] | 285 | as_release(task->as);
|
---|
[a35b458] | 286 |
|
---|
[82d515e9] | 287 | slab_free(task_cache, task);
|
---|
[7509ddc] | 288 | }
|
---|
| 289 |
|
---|
[278b4a30] | 290 | /** Hold a reference to a task.
|
---|
| 291 | *
|
---|
| 292 | * Holding a reference to a task prevents destruction of that task.
|
---|
| 293 | *
|
---|
[da1bafb] | 294 | * @param task Task to be held.
|
---|
| 295 | *
|
---|
[278b4a30] | 296 | */
|
---|
[da1bafb] | 297 | void task_hold(task_t *task)
|
---|
[278b4a30] | 298 | {
|
---|
[da1bafb] | 299 | atomic_inc(&task->refcount);
|
---|
[278b4a30] | 300 | }
|
---|
| 301 |
|
---|
| 302 | /** Release a reference to a task.
|
---|
| 303 | *
|
---|
| 304 | * The last one to release a reference to a task destroys the task.
|
---|
| 305 | *
|
---|
[da1bafb] | 306 | * @param task Task to be released.
|
---|
| 307 | *
|
---|
[278b4a30] | 308 | */
|
---|
[da1bafb] | 309 | void task_release(task_t *task)
|
---|
[278b4a30] | 310 | {
|
---|
[da1bafb] | 311 | if ((atomic_predec(&task->refcount)) == 0)
|
---|
| 312 | task_destroy(task);
|
---|
[278b4a30] | 313 | }
|
---|
| 314 |
|
---|
[dd8d5a7] | 315 | #ifdef __32_BITS__
|
---|
| 316 |
|
---|
| 317 | /** Syscall for reading task ID from userspace (32 bits)
|
---|
[ec55358] | 318 | *
|
---|
[dd8d5a7] | 319 | * @param uspace_taskid Pointer to user-space buffer
|
---|
| 320 | * where to store current task ID.
|
---|
[5ba201d] | 321 | *
|
---|
| 322 | * @return Zero on success or an error code from @ref errno.h.
|
---|
[ec55358] | 323 | *
|
---|
| 324 | */
|
---|
[5a5269d] | 325 | sys_errno_t sys_task_get_id(uspace_ptr_sysarg64_t uspace_taskid)
|
---|
[ec55358] | 326 | {
|
---|
| 327 | /*
|
---|
[814c4f5] | 328 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 329 | * the lifespan of the task.
|
---|
[ec55358] | 330 | */
|
---|
[b7fd2a0] | 331 | return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
|
---|
[6f4495f5] | 332 | sizeof(TASK->taskid));
|
---|
[ec55358] | 333 | }
|
---|
| 334 |
|
---|
[dd8d5a7] | 335 | #endif /* __32_BITS__ */
|
---|
| 336 |
|
---|
| 337 | #ifdef __64_BITS__
|
---|
| 338 |
|
---|
| 339 | /** Syscall for reading task ID from userspace (64 bits)
|
---|
| 340 | *
|
---|
| 341 | * @return Current task ID.
|
---|
| 342 | *
|
---|
| 343 | */
|
---|
| 344 | sysarg_t sys_task_get_id(void)
|
---|
| 345 | {
|
---|
| 346 | /*
|
---|
| 347 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 348 | * the lifespan of the task.
|
---|
| 349 | */
|
---|
| 350 | return TASK->taskid;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | #endif /* __64_BITS__ */
|
---|
| 354 |
|
---|
[bc18d63] | 355 | /** Syscall for setting the task name.
|
---|
| 356 | *
|
---|
| 357 | * The name simplifies identifying the task in the task list.
|
---|
| 358 | *
|
---|
[5ba201d] | 359 | * @param name The new name for the task. (typically the same
|
---|
| 360 | * as the command used to execute it).
|
---|
[bc18d63] | 361 | *
|
---|
| 362 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[5ba201d] | 363 | *
|
---|
[bc18d63] | 364 | */
|
---|
[5a5269d] | 365 | sys_errno_t sys_task_set_name(const uspace_ptr_char uspace_name, size_t name_len)
|
---|
[bc18d63] | 366 | {
|
---|
| 367 | char namebuf[TASK_NAME_BUFLEN];
|
---|
[a35b458] | 368 |
|
---|
[bc18d63] | 369 | /* Cap length of name and copy it from userspace. */
|
---|
| 370 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
| 371 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
[a35b458] | 372 |
|
---|
[b7fd2a0] | 373 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
[a53ed3a] | 374 | if (rc != EOK)
|
---|
[b7fd2a0] | 375 | return (sys_errno_t) rc;
|
---|
[a35b458] | 376 |
|
---|
[f4b1535] | 377 | namebuf[name_len] = '\0';
|
---|
[a35b458] | 378 |
|
---|
[577f042a] | 379 | /*
|
---|
| 380 | * As the task name is referenced also from the
|
---|
| 381 | * threads, lock the threads' lock for the course
|
---|
| 382 | * of the update.
|
---|
| 383 | */
|
---|
[a35b458] | 384 |
|
---|
[577f042a] | 385 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 386 | irq_spinlock_lock(&TASK->lock, false);
|
---|
[a35b458] | 387 |
|
---|
[577f042a] | 388 | /* Set task name */
|
---|
[f4b1535] | 389 | str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
|
---|
[a35b458] | 390 |
|
---|
[577f042a] | 391 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
| 392 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 393 |
|
---|
[bc18d63] | 394 | return EOK;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[1e9f8ab] | 397 | /** Syscall to forcefully terminate a task
|
---|
| 398 | *
|
---|
| 399 | * @param uspace_taskid Pointer to task ID in user space.
|
---|
| 400 | *
|
---|
| 401 | * @return 0 on success or an error code from @ref errno.h.
|
---|
| 402 | *
|
---|
| 403 | */
|
---|
[5a5269d] | 404 | sys_errno_t sys_task_kill(uspace_ptr_task_id_t uspace_taskid)
|
---|
[1e9f8ab] | 405 | {
|
---|
| 406 | task_id_t taskid;
|
---|
[b7fd2a0] | 407 | errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
|
---|
[a53ed3a] | 408 | if (rc != EOK)
|
---|
[b7fd2a0] | 409 | return (sys_errno_t) rc;
|
---|
[a35b458] | 410 |
|
---|
[b7fd2a0] | 411 | return (sys_errno_t) task_kill(taskid);
|
---|
[1e9f8ab] | 412 | }
|
---|
| 413 |
|
---|
[9a8d91b] | 414 | /** Find task structure corresponding to task ID.
|
---|
| 415 | *
|
---|
[814c4f5] | 416 | * The tasks_lock must be already held by the caller of this function and
|
---|
| 417 | * interrupts must be disabled.
|
---|
[9a8d91b] | 418 | *
|
---|
[5ba201d] | 419 | * @param id Task ID.
|
---|
| 420 | *
|
---|
[e1b6742] | 421 | * @return Task structure address 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 | {
|
---|
[63e27ef] | 426 | assert(interrupts_disabled());
|
---|
| 427 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
[1d432f9] | 428 |
|
---|
[ef1eab7] | 429 | odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
|
---|
| 430 | if (odlink != NULL)
|
---|
| 431 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
[a35b458] | 432 |
|
---|
[b76a2217] | 433 | return NULL;
|
---|
[9a8d91b] | 434 | }
|
---|
| 435 |
|
---|
[aab5e46] | 436 | /** Get count of tasks.
|
---|
| 437 | *
|
---|
| 438 | * @return Number of tasks in the system
|
---|
| 439 | */
|
---|
| 440 | size_t task_count(void)
|
---|
| 441 | {
|
---|
| 442 | assert(interrupts_disabled());
|
---|
| 443 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 444 |
|
---|
| 445 | return odict_count(&tasks);
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | /** Get first task (task with lowest ID).
|
---|
| 449 | *
|
---|
| 450 | * @return Pointer to first task or @c NULL if there are none.
|
---|
| 451 | */
|
---|
| 452 | task_t *task_first(void)
|
---|
| 453 | {
|
---|
| 454 | odlink_t *odlink;
|
---|
| 455 |
|
---|
| 456 | assert(interrupts_disabled());
|
---|
| 457 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 458 |
|
---|
| 459 | odlink = odict_first(&tasks);
|
---|
| 460 | if (odlink == NULL)
|
---|
| 461 | return NULL;
|
---|
| 462 |
|
---|
| 463 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | /** Get next task (with higher task ID).
|
---|
| 467 | *
|
---|
| 468 | * @param cur Current task
|
---|
| 469 | * @return Pointer to next task or @c NULL if there are no more tasks.
|
---|
| 470 | */
|
---|
| 471 | task_t *task_next(task_t *cur)
|
---|
| 472 | {
|
---|
| 473 | odlink_t *odlink;
|
---|
| 474 |
|
---|
| 475 | assert(interrupts_disabled());
|
---|
| 476 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 477 |
|
---|
| 478 | odlink = odict_next(&cur->ltasks, &tasks);
|
---|
| 479 | if (odlink == NULL)
|
---|
| 480 | return NULL;
|
---|
| 481 |
|
---|
| 482 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[0313ff0] | 485 | /** Get accounting data of given task.
|
---|
| 486 | *
|
---|
[1d432f9] | 487 | * Note that task lock of 'task' must be already held and interrupts must be
|
---|
[814c4f5] | 488 | * already disabled.
|
---|
[0313ff0] | 489 | *
|
---|
[da1bafb] | 490 | * @param task Pointer to the task.
|
---|
[88dea9d] | 491 | * @param ucycles Out pointer to sum of all user cycles.
|
---|
| 492 | * @param kcycles Out pointer to sum of all kernel cycles.
|
---|
[0313ff0] | 493 | *
|
---|
| 494 | */
|
---|
[da1bafb] | 495 | void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
|
---|
[0313ff0] | 496 | {
|
---|
[63e27ef] | 497 | assert(interrupts_disabled());
|
---|
| 498 | assert(irq_spinlock_locked(&task->lock));
|
---|
[1d432f9] | 499 |
|
---|
[a2a00e8] | 500 | /* Accumulated values of task */
|
---|
[da1bafb] | 501 | uint64_t uret = task->ucycles;
|
---|
| 502 | uint64_t kret = task->kcycles;
|
---|
[a35b458] | 503 |
|
---|
[0313ff0] | 504 | /* Current values of threads */
|
---|
[feeac0d] | 505 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[da1bafb] | 506 | irq_spinlock_lock(&thread->lock, false);
|
---|
[a35b458] | 507 |
|
---|
[62b6d17] | 508 | /* Process only counted threads */
|
---|
[da1bafb] | 509 | if (!thread->uncounted) {
|
---|
| 510 | if (thread == THREAD) {
|
---|
[6f4495f5] | 511 | /* Update accounting of current thread */
|
---|
[a2a00e8] | 512 | thread_update_accounting(false);
|
---|
[da1bafb] | 513 | }
|
---|
[a35b458] | 514 |
|
---|
[da1bafb] | 515 | uret += thread->ucycles;
|
---|
| 516 | kret += thread->kcycles;
|
---|
[62b6d17] | 517 | }
|
---|
[a35b458] | 518 |
|
---|
[da1bafb] | 519 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[0313ff0] | 520 | }
|
---|
[a35b458] | 521 |
|
---|
[a2a00e8] | 522 | *ucycles = uret;
|
---|
| 523 | *kcycles = kret;
|
---|
[0313ff0] | 524 | }
|
---|
| 525 |
|
---|
[da1bafb] | 526 | static void task_kill_internal(task_t *task)
|
---|
[121966e] | 527 | {
|
---|
[df58e44] | 528 | irq_spinlock_lock(&task->lock, false);
|
---|
[a35b458] | 529 |
|
---|
[121966e] | 530 | /*
|
---|
| 531 | * Interrupt all threads.
|
---|
| 532 | */
|
---|
[a35b458] | 533 |
|
---|
[feeac0d] | 534 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[1871118] | 535 | thread_t *thr = thread_try_ref(thread);
|
---|
| 536 | if (thr)
|
---|
| 537 | thread_interrupt(thr, false);
|
---|
| 538 |
|
---|
| 539 | // If NULL, the thread is already getting destroyed concurrently with this.
|
---|
[121966e] | 540 | }
|
---|
[a35b458] | 541 |
|
---|
[da1bafb] | 542 | irq_spinlock_unlock(&task->lock, false);
|
---|
[121966e] | 543 | }
|
---|
| 544 |
|
---|
[7509ddc] | 545 | /** Kill task.
|
---|
[ea7890e7] | 546 | *
|
---|
| 547 | * This function is idempotent.
|
---|
| 548 | * It signals all the task's threads to bail it out.
|
---|
[7509ddc] | 549 | *
|
---|
[5ba201d] | 550 | * @param id ID of the task to be killed.
|
---|
| 551 | *
|
---|
| 552 | * @return Zero on success or an error code from errno.h.
|
---|
[7509ddc] | 553 | *
|
---|
| 554 | */
|
---|
[b7fd2a0] | 555 | errno_t task_kill(task_id_t id)
|
---|
[7509ddc] | 556 | {
|
---|
[9b6aae6] | 557 | if (id == 1)
|
---|
| 558 | return EPERM;
|
---|
[a35b458] | 559 |
|
---|
[da1bafb] | 560 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 561 |
|
---|
[da1bafb] | 562 | task_t *task = task_find_by_id(id);
|
---|
| 563 | if (!task) {
|
---|
| 564 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[7509ddc] | 565 | return ENOENT;
|
---|
| 566 | }
|
---|
[a35b458] | 567 |
|
---|
[da1bafb] | 568 | task_kill_internal(task);
|
---|
| 569 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 570 |
|
---|
[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 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 601 | task_kill_internal(TASK);
|
---|
| 602 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 603 |
|
---|
[5bcf1f9] | 604 | thread_exit();
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | /** Process syscall to terminate the current task.
|
---|
| 608 | *
|
---|
| 609 | * @param notify Send out fault notifications.
|
---|
| 610 | *
|
---|
| 611 | */
|
---|
[b7fd2a0] | 612 | sys_errno_t sys_task_exit(sysarg_t notify)
|
---|
[5bcf1f9] | 613 | {
|
---|
| 614 | task_kill_self(notify);
|
---|
[88e43bc] | 615 | unreachable();
|
---|
[5bcf1f9] | 616 | }
|
---|
| 617 |
|
---|
[ef1eab7] | 618 | static void task_print(task_t *task, bool additional)
|
---|
[b76a2217] | 619 | {
|
---|
[da1bafb] | 620 | irq_spinlock_lock(&task->lock, false);
|
---|
[a35b458] | 621 |
|
---|
[a2a00e8] | 622 | uint64_t ucycles;
|
---|
| 623 | uint64_t kcycles;
|
---|
[1ba37fa] | 624 | char usuffix, ksuffix;
|
---|
[da1bafb] | 625 | task_get_accounting(task, &ucycles, &kcycles);
|
---|
[e535eeb] | 626 | order_suffix(ucycles, &ucycles, &usuffix);
|
---|
| 627 | order_suffix(kcycles, &kcycles, &ksuffix);
|
---|
[a35b458] | 628 |
|
---|
[da1bafb] | 629 | #ifdef __32_BITS__
|
---|
[ef1eab7] | 630 | if (additional)
|
---|
[077842c] | 631 | printf("%-8" PRIu64 " %9zu", task->taskid,
|
---|
[036e97c] | 632 | atomic_load(&task->refcount));
|
---|
[c0f13d2] | 633 | else
|
---|
| 634 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
|
---|
| 635 | " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
|
---|
[473d5d2] | 636 | task->name, task->container, task, task->as,
|
---|
[c0f13d2] | 637 | ucycles, usuffix, kcycles, ksuffix);
|
---|
[52755f1] | 638 | #endif
|
---|
[a35b458] | 639 |
|
---|
[52755f1] | 640 | #ifdef __64_BITS__
|
---|
[ef1eab7] | 641 | if (additional)
|
---|
[7e752b2] | 642 | printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
|
---|
[077842c] | 643 | "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
|
---|
[036e97c] | 644 | ksuffix, atomic_load(&task->refcount));
|
---|
[c0f13d2] | 645 | else
|
---|
| 646 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
|
---|
[473d5d2] | 647 | task->taskid, task->name, task->container, task, task->as);
|
---|
[52755f1] | 648 | #endif
|
---|
[a35b458] | 649 |
|
---|
[da1bafb] | 650 | irq_spinlock_unlock(&task->lock, false);
|
---|
[b76a2217] | 651 | }
|
---|
| 652 |
|
---|
[c0f13d2] | 653 | /** Print task list
|
---|
| 654 | *
|
---|
| 655 | * @param additional Print additional information.
|
---|
| 656 | *
|
---|
| 657 | */
|
---|
| 658 | void task_print_list(bool additional)
|
---|
[37c57f2] | 659 | {
|
---|
[f74bbaf] | 660 | /* Messing with task structures, avoid deadlock */
|
---|
[da1bafb] | 661 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 662 |
|
---|
[5ba201d] | 663 | #ifdef __32_BITS__
|
---|
[c0f13d2] | 664 | if (additional)
|
---|
[48dcc69] | 665 | printf("[id ] [threads] [calls] [callee\n");
|
---|
[c0f13d2] | 666 | else
|
---|
[473d5d2] | 667 | printf("[id ] [name ] [ctn] [address ] [as ]"
|
---|
[c0f13d2] | 668 | " [ucycles ] [kcycles ]\n");
|
---|
[52755f1] | 669 | #endif
|
---|
[a35b458] | 670 |
|
---|
[52755f1] | 671 | #ifdef __64_BITS__
|
---|
[c0f13d2] | 672 | if (additional)
|
---|
[be06914] | 673 | printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
|
---|
[c0f13d2] | 674 | " [callee\n");
|
---|
| 675 | else
|
---|
[473d5d2] | 676 | printf("[id ] [name ] [ctn] [address ]"
|
---|
[c0f13d2] | 677 | " [as ]\n");
|
---|
[52755f1] | 678 | #endif
|
---|
[a35b458] | 679 |
|
---|
[ef1eab7] | 680 | task_t *task;
|
---|
| 681 |
|
---|
[aab5e46] | 682 | task = task_first();
|
---|
| 683 | while (task != NULL) {
|
---|
[ef1eab7] | 684 | task_print(task, additional);
|
---|
[aab5e46] | 685 | task = task_next(task);
|
---|
[ef1eab7] | 686 | }
|
---|
[a35b458] | 687 |
|
---|
[da1bafb] | 688 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[37c57f2] | 689 | }
|
---|
[7509ddc] | 690 |
|
---|
[ef1eab7] | 691 | /** Get key function for the @c tasks ordered dictionary.
|
---|
| 692 | *
|
---|
| 693 | * @param odlink Link
|
---|
| 694 | * @return Pointer to task ID cast as 'void *'
|
---|
| 695 | */
|
---|
| 696 | static void *tasks_getkey(odlink_t *odlink)
|
---|
| 697 | {
|
---|
| 698 | task_t *task = odict_get_instance(odlink, task_t, ltasks);
|
---|
| 699 | return (void *) &task->taskid;
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | /** Key comparison function for the @c tasks ordered dictionary.
|
---|
| 703 | *
|
---|
| 704 | * @param a Pointer to thread A ID
|
---|
| 705 | * @param b Pointer to thread B ID
|
---|
| 706 | * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
|
---|
| 707 | */
|
---|
| 708 | static int tasks_cmp(void *a, void *b)
|
---|
| 709 | {
|
---|
| 710 | task_id_t ida = *(task_id_t *)a;
|
---|
| 711 | task_id_t idb = *(task_id_t *)b;
|
---|
| 712 |
|
---|
| 713 | if (ida < idb)
|
---|
| 714 | return -1;
|
---|
| 715 | else if (ida == idb)
|
---|
| 716 | return 0;
|
---|
| 717 | else
|
---|
| 718 | return +1;
|
---|
| 719 | }
|
---|
| 720 |
|
---|
[cc73a8a1] | 721 | /** @}
|
---|
[b45c443] | 722 | */
|
---|