[f761f1eb] | 1 | /*
|
---|
[f35749e] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[278b4a30] | 3 | * Copyright (c) 2010 Jakub Jermar
|
---|
[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 | */
|
---|
[f35749e] | 106 | void task_done(task_t *cur_task)
|
---|
[f74bbaf] | 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;
|
---|
[f35749e] | 114 |
|
---|
[2f2beb4] | 115 | /*
|
---|
| 116 | * The first task is held by kinit(), we need to release it or
|
---|
| 117 | * it will never finish cleanup.
|
---|
| 118 | */
|
---|
| 119 | task_release(task_0);
|
---|
| 120 | }
|
---|
[a35b458] | 121 |
|
---|
[da1bafb] | 122 | /* Repeat until there are any tasks except TASK */
|
---|
| 123 | do {
|
---|
[121966e] | 124 | #ifdef CONFIG_DEBUG
|
---|
| 125 | printf("Killing tasks... ");
|
---|
| 126 | #endif
|
---|
[da1bafb] | 127 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[121966e] | 128 | tasks_left = 0;
|
---|
[ef1eab7] | 129 |
|
---|
[aab5e46] | 130 | task = task_first();
|
---|
| 131 | while (task != NULL) {
|
---|
[f35749e] | 132 | if (task != cur_task) {
|
---|
[ef1eab7] | 133 | tasks_left++;
|
---|
| 134 | #ifdef CONFIG_DEBUG
|
---|
| 135 | printf("[%" PRIu64 "] ", task->taskid);
|
---|
| 136 | #endif
|
---|
| 137 | task_kill_internal(task);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[aab5e46] | 140 | task = task_next(task);
|
---|
[ef1eab7] | 141 | }
|
---|
| 142 |
|
---|
[da1bafb] | 143 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 144 |
|
---|
[121966e] | 145 | thread_sleep(1);
|
---|
[a35b458] | 146 |
|
---|
[f74bbaf] | 147 | #ifdef CONFIG_DEBUG
|
---|
[121966e] | 148 | printf("\n");
|
---|
| 149 | #endif
|
---|
[da1bafb] | 150 | } while (tasks_left > 0);
|
---|
[f74bbaf] | 151 | }
|
---|
[70527f1] | 152 |
|
---|
[b7fd2a0] | 153 | errno_t tsk_constructor(void *obj, unsigned int kmflags)
|
---|
[59ee56f] | 154 | {
|
---|
[da1bafb] | 155 | task_t *task = (task_t *) obj;
|
---|
[c46bfbc] | 156 |
|
---|
[b7fd2a0] | 157 | errno_t rc = caps_task_alloc(task);
|
---|
[c46bfbc] | 158 | if (rc != EOK)
|
---|
| 159 | return rc;
|
---|
[a35b458] | 160 |
|
---|
[e3306d04] | 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 |
|
---|
[fa3ed5b] | 204 | if (caps_task_init(task) != EOK) {
|
---|
| 205 | slab_free(task_cache, task);
|
---|
| 206 | return NULL;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[07d4271] | 209 | refcount_init(&task->refcount);
|
---|
| 210 |
|
---|
[da1bafb] | 211 | task_create_arch(task);
|
---|
[a35b458] | 212 |
|
---|
[da1bafb] | 213 | task->as = as;
|
---|
| 214 | str_cpy(task->name, TASK_NAME_BUFLEN, name);
|
---|
[a35b458] | 215 |
|
---|
[473d5d2] | 216 | task->container = CONTAINER;
|
---|
[719a208] | 217 | task->perms = 0;
|
---|
[da1bafb] | 218 | task->ucycles = 0;
|
---|
| 219 | task->kcycles = 0;
|
---|
[7ad17de] | 220 |
|
---|
[da1bafb] | 221 | task->ipc_info.call_sent = 0;
|
---|
[be06914] | 222 | task->ipc_info.call_received = 0;
|
---|
[da1bafb] | 223 | task->ipc_info.answer_sent = 0;
|
---|
[be06914] | 224 | task->ipc_info.answer_received = 0;
|
---|
| 225 | task->ipc_info.irq_notif_received = 0;
|
---|
[da1bafb] | 226 | task->ipc_info.forwarded = 0;
|
---|
[5d0500c] | 227 |
|
---|
| 228 | event_task_init(task);
|
---|
[a35b458] | 229 |
|
---|
[c33f39f] | 230 | task->answerbox.active = true;
|
---|
| 231 |
|
---|
[40eab9f] | 232 | task->debug_sections = NULL;
|
---|
| 233 |
|
---|
[9a1b20c] | 234 | #ifdef CONFIG_UDEBUG
|
---|
| 235 | /* Init debugging stuff */
|
---|
[da1bafb] | 236 | udebug_task_init(&task->udebug);
|
---|
[a35b458] | 237 |
|
---|
[9a1b20c] | 238 | /* Init kbox stuff */
|
---|
[c33f39f] | 239 | task->kb.box.active = true;
|
---|
[da1bafb] | 240 | task->kb.finished = false;
|
---|
[9a1b20c] | 241 | #endif
|
---|
[a35b458] | 242 |
|
---|
[cdc4334] | 243 | if ((ipc_box_0) &&
|
---|
| 244 | (container_check(ipc_box_0->task->container, task->container))) {
|
---|
[eadaeae8] | 245 | cap_phone_handle_t phone_handle;
|
---|
[334c103] | 246 | errno_t rc = phone_alloc(task, true, &phone_handle, NULL);
|
---|
[09d01f2] | 247 | if (rc != EOK) {
|
---|
[6a32cc5f] | 248 | task->as = NULL;
|
---|
| 249 | task_destroy_arch(task);
|
---|
| 250 | slab_free(task_cache, task);
|
---|
| 251 | return NULL;
|
---|
| 252 | }
|
---|
[a35b458] | 253 |
|
---|
[455241b] | 254 | phone_t *phone = phone_from_kobject(
|
---|
| 255 | kobject_get(task, phone_handle, KOBJECT_TYPE_PHONE));
|
---|
| 256 | (void) ipc_phone_connect(phone, ipc_box_0);
|
---|
[05ffb41] | 257 | }
|
---|
[a35b458] | 258 |
|
---|
[da1bafb] | 259 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 260 |
|
---|
[da1bafb] | 261 | task->taskid = ++task_counter;
|
---|
[ef1eab7] | 262 | odlink_initialize(&task->ltasks);
|
---|
| 263 | odict_insert(&task->ltasks, &tasks, NULL);
|
---|
[a35b458] | 264 |
|
---|
[da1bafb] | 265 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 266 |
|
---|
[da1bafb] | 267 | return task;
|
---|
[f761f1eb] | 268 | }
|
---|
| 269 |
|
---|
[7509ddc] | 270 | /** Destroy task.
|
---|
| 271 | *
|
---|
[da1bafb] | 272 | * @param task Task to be destroyed.
|
---|
[5ba201d] | 273 | *
|
---|
[7509ddc] | 274 | */
|
---|
[07d4271] | 275 | static void task_destroy(task_t *task)
|
---|
[7509ddc] | 276 | {
|
---|
[ea7890e7] | 277 | /*
|
---|
[ca4c5596] | 278 | * Remove the task from the task odict.
|
---|
[ea7890e7] | 279 | */
|
---|
[da1bafb] | 280 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[ef1eab7] | 281 | odict_remove(&task->ltasks);
|
---|
[da1bafb] | 282 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 283 |
|
---|
[ea7890e7] | 284 | /*
|
---|
| 285 | * Perform architecture specific task destruction.
|
---|
| 286 | */
|
---|
[da1bafb] | 287 | task_destroy_arch(task);
|
---|
[a35b458] | 288 |
|
---|
[ea7890e7] | 289 | /*
|
---|
| 290 | * Drop our reference to the address space.
|
---|
| 291 | */
|
---|
[da1bafb] | 292 | as_release(task->as);
|
---|
[a35b458] | 293 |
|
---|
[fa3ed5b] | 294 | caps_task_clear(task);
|
---|
| 295 |
|
---|
[82d515e9] | 296 | slab_free(task_cache, task);
|
---|
[7509ddc] | 297 | }
|
---|
| 298 |
|
---|
[278b4a30] | 299 | /** Hold a reference to a task.
|
---|
| 300 | *
|
---|
| 301 | * Holding a reference to a task prevents destruction of that task.
|
---|
| 302 | *
|
---|
[da1bafb] | 303 | * @param task Task to be held.
|
---|
| 304 | *
|
---|
[278b4a30] | 305 | */
|
---|
[da1bafb] | 306 | void task_hold(task_t *task)
|
---|
[278b4a30] | 307 | {
|
---|
[07d4271] | 308 | refcount_up(&task->refcount);
|
---|
[278b4a30] | 309 | }
|
---|
| 310 |
|
---|
| 311 | /** Release a reference to a task.
|
---|
| 312 | *
|
---|
| 313 | * The last one to release a reference to a task destroys the task.
|
---|
| 314 | *
|
---|
[da1bafb] | 315 | * @param task Task to be released.
|
---|
| 316 | *
|
---|
[278b4a30] | 317 | */
|
---|
[da1bafb] | 318 | void task_release(task_t *task)
|
---|
[278b4a30] | 319 | {
|
---|
[07d4271] | 320 | if (refcount_down(&task->refcount))
|
---|
[da1bafb] | 321 | task_destroy(task);
|
---|
[278b4a30] | 322 | }
|
---|
| 323 |
|
---|
[dd8d5a7] | 324 | #ifdef __32_BITS__
|
---|
| 325 |
|
---|
| 326 | /** Syscall for reading task ID from userspace (32 bits)
|
---|
[ec55358] | 327 | *
|
---|
[dd8d5a7] | 328 | * @param uspace_taskid Pointer to user-space buffer
|
---|
| 329 | * where to store current task ID.
|
---|
[5ba201d] | 330 | *
|
---|
| 331 | * @return Zero on success or an error code from @ref errno.h.
|
---|
[ec55358] | 332 | *
|
---|
| 333 | */
|
---|
[5a5269d] | 334 | sys_errno_t sys_task_get_id(uspace_ptr_sysarg64_t uspace_taskid)
|
---|
[ec55358] | 335 | {
|
---|
| 336 | /*
|
---|
[814c4f5] | 337 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 338 | * the lifespan of the task.
|
---|
[ec55358] | 339 | */
|
---|
[b7fd2a0] | 340 | return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
|
---|
[6f4495f5] | 341 | sizeof(TASK->taskid));
|
---|
[ec55358] | 342 | }
|
---|
| 343 |
|
---|
[dd8d5a7] | 344 | #endif /* __32_BITS__ */
|
---|
| 345 |
|
---|
| 346 | #ifdef __64_BITS__
|
---|
| 347 |
|
---|
| 348 | /** Syscall for reading task ID from userspace (64 bits)
|
---|
| 349 | *
|
---|
| 350 | * @return Current task ID.
|
---|
| 351 | *
|
---|
| 352 | */
|
---|
| 353 | sysarg_t sys_task_get_id(void)
|
---|
| 354 | {
|
---|
| 355 | /*
|
---|
| 356 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 357 | * the lifespan of the task.
|
---|
| 358 | */
|
---|
| 359 | return TASK->taskid;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | #endif /* __64_BITS__ */
|
---|
| 363 |
|
---|
[bc18d63] | 364 | /** Syscall for setting the task name.
|
---|
| 365 | *
|
---|
| 366 | * The name simplifies identifying the task in the task list.
|
---|
| 367 | *
|
---|
[5ba201d] | 368 | * @param name The new name for the task. (typically the same
|
---|
| 369 | * as the command used to execute it).
|
---|
[bc18d63] | 370 | *
|
---|
| 371 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[5ba201d] | 372 | *
|
---|
[bc18d63] | 373 | */
|
---|
[5a5269d] | 374 | sys_errno_t sys_task_set_name(const uspace_ptr_char uspace_name, size_t name_len)
|
---|
[bc18d63] | 375 | {
|
---|
| 376 | char namebuf[TASK_NAME_BUFLEN];
|
---|
[a35b458] | 377 |
|
---|
[bc18d63] | 378 | /* Cap length of name and copy it from userspace. */
|
---|
| 379 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
| 380 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
[a35b458] | 381 |
|
---|
[b7fd2a0] | 382 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
[a53ed3a] | 383 | if (rc != EOK)
|
---|
[b7fd2a0] | 384 | return (sys_errno_t) rc;
|
---|
[a35b458] | 385 |
|
---|
[f4b1535] | 386 | namebuf[name_len] = '\0';
|
---|
[a35b458] | 387 |
|
---|
[577f042a] | 388 | /*
|
---|
| 389 | * As the task name is referenced also from the
|
---|
| 390 | * threads, lock the threads' lock for the course
|
---|
| 391 | * of the update.
|
---|
| 392 | */
|
---|
[a35b458] | 393 |
|
---|
[577f042a] | 394 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 395 | irq_spinlock_lock(&TASK->lock, false);
|
---|
[a35b458] | 396 |
|
---|
[577f042a] | 397 | /* Set task name */
|
---|
[f4b1535] | 398 | str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
|
---|
[a35b458] | 399 |
|
---|
[577f042a] | 400 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
| 401 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 402 |
|
---|
[bc18d63] | 403 | return EOK;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
[1e9f8ab] | 406 | /** Syscall to forcefully terminate a task
|
---|
| 407 | *
|
---|
| 408 | * @param uspace_taskid Pointer to task ID in user space.
|
---|
| 409 | *
|
---|
| 410 | * @return 0 on success or an error code from @ref errno.h.
|
---|
| 411 | *
|
---|
| 412 | */
|
---|
[5a5269d] | 413 | sys_errno_t sys_task_kill(uspace_ptr_task_id_t uspace_taskid)
|
---|
[1e9f8ab] | 414 | {
|
---|
| 415 | task_id_t taskid;
|
---|
[b7fd2a0] | 416 | errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
|
---|
[a53ed3a] | 417 | if (rc != EOK)
|
---|
[b7fd2a0] | 418 | return (sys_errno_t) rc;
|
---|
[a35b458] | 419 |
|
---|
[b7fd2a0] | 420 | return (sys_errno_t) task_kill(taskid);
|
---|
[1e9f8ab] | 421 | }
|
---|
| 422 |
|
---|
[9a8d91b] | 423 | /** Find task structure corresponding to task ID.
|
---|
| 424 | *
|
---|
[5ba201d] | 425 | * @param id Task ID.
|
---|
| 426 | *
|
---|
[07d4271] | 427 | * @return Task reference or NULL if there is no such task ID.
|
---|
[9a8d91b] | 428 | *
|
---|
| 429 | */
|
---|
[5ba201d] | 430 | task_t *task_find_by_id(task_id_t id)
|
---|
| 431 | {
|
---|
[07d4271] | 432 | task_t *task = NULL;
|
---|
| 433 |
|
---|
| 434 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[1d432f9] | 435 |
|
---|
[ef1eab7] | 436 | odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
|
---|
[07d4271] | 437 | if (odlink != NULL) {
|
---|
| 438 | task = odict_get_instance(odlink, task_t, ltasks);
|
---|
[a35b458] | 439 |
|
---|
[07d4271] | 440 | /*
|
---|
| 441 | * The directory of tasks can't hold a reference, since that would
|
---|
| 442 | * prevent task from ever being destroyed. That means we have to
|
---|
| 443 | * check for the case where the task is already being destroyed, but
|
---|
| 444 | * not yet removed from the directory.
|
---|
| 445 | */
|
---|
| 446 | if (!refcount_try_up(&task->refcount))
|
---|
| 447 | task = NULL;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
| 451 |
|
---|
| 452 | return task;
|
---|
[9a8d91b] | 453 | }
|
---|
| 454 |
|
---|
[aab5e46] | 455 | /** Get count of tasks.
|
---|
| 456 | *
|
---|
| 457 | * @return Number of tasks in the system
|
---|
| 458 | */
|
---|
| 459 | size_t task_count(void)
|
---|
| 460 | {
|
---|
| 461 | assert(interrupts_disabled());
|
---|
| 462 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 463 |
|
---|
| 464 | return odict_count(&tasks);
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | /** Get first task (task with lowest ID).
|
---|
| 468 | *
|
---|
| 469 | * @return Pointer to first task or @c NULL if there are none.
|
---|
| 470 | */
|
---|
| 471 | task_t *task_first(void)
|
---|
| 472 | {
|
---|
| 473 | odlink_t *odlink;
|
---|
| 474 |
|
---|
| 475 | assert(interrupts_disabled());
|
---|
| 476 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 477 |
|
---|
| 478 | odlink = odict_first(&tasks);
|
---|
| 479 | if (odlink == NULL)
|
---|
| 480 | return NULL;
|
---|
| 481 |
|
---|
| 482 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | /** Get next task (with higher task ID).
|
---|
| 486 | *
|
---|
| 487 | * @param cur Current task
|
---|
| 488 | * @return Pointer to next task or @c NULL if there are no more tasks.
|
---|
| 489 | */
|
---|
| 490 | task_t *task_next(task_t *cur)
|
---|
| 491 | {
|
---|
| 492 | odlink_t *odlink;
|
---|
| 493 |
|
---|
| 494 | assert(interrupts_disabled());
|
---|
| 495 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 496 |
|
---|
| 497 | odlink = odict_next(&cur->ltasks, &tasks);
|
---|
| 498 | if (odlink == NULL)
|
---|
| 499 | return NULL;
|
---|
| 500 |
|
---|
| 501 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[0313ff0] | 504 | /** Get accounting data of given task.
|
---|
| 505 | *
|
---|
[1d432f9] | 506 | * Note that task lock of 'task' must be already held and interrupts must be
|
---|
[814c4f5] | 507 | * already disabled.
|
---|
[0313ff0] | 508 | *
|
---|
[da1bafb] | 509 | * @param task Pointer to the task.
|
---|
[88dea9d] | 510 | * @param ucycles Out pointer to sum of all user cycles.
|
---|
| 511 | * @param kcycles Out pointer to sum of all kernel cycles.
|
---|
[0313ff0] | 512 | *
|
---|
| 513 | */
|
---|
[da1bafb] | 514 | void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
|
---|
[0313ff0] | 515 | {
|
---|
[63e27ef] | 516 | assert(interrupts_disabled());
|
---|
| 517 | assert(irq_spinlock_locked(&task->lock));
|
---|
[1d432f9] | 518 |
|
---|
[a2a00e8] | 519 | /* Accumulated values of task */
|
---|
[da1bafb] | 520 | uint64_t uret = task->ucycles;
|
---|
| 521 | uint64_t kret = task->kcycles;
|
---|
[a35b458] | 522 |
|
---|
[0313ff0] | 523 | /* Current values of threads */
|
---|
[feeac0d] | 524 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[62b6d17] | 525 | /* Process only counted threads */
|
---|
[da1bafb] | 526 | if (!thread->uncounted) {
|
---|
| 527 | if (thread == THREAD) {
|
---|
[6f4495f5] | 528 | /* Update accounting of current thread */
|
---|
[a2a00e8] | 529 | thread_update_accounting(false);
|
---|
[da1bafb] | 530 | }
|
---|
[a35b458] | 531 |
|
---|
[11909ce3] | 532 | uret += atomic_time_read(&thread->ucycles);
|
---|
| 533 | kret += atomic_time_read(&thread->kcycles);
|
---|
[62b6d17] | 534 | }
|
---|
[0313ff0] | 535 | }
|
---|
[a35b458] | 536 |
|
---|
[a2a00e8] | 537 | *ucycles = uret;
|
---|
| 538 | *kcycles = kret;
|
---|
[0313ff0] | 539 | }
|
---|
| 540 |
|
---|
[da1bafb] | 541 | static void task_kill_internal(task_t *task)
|
---|
[121966e] | 542 | {
|
---|
[07d4271] | 543 | irq_spinlock_lock(&task->lock, true);
|
---|
[a35b458] | 544 |
|
---|
[121966e] | 545 | /*
|
---|
| 546 | * Interrupt all threads.
|
---|
| 547 | */
|
---|
[a35b458] | 548 |
|
---|
[feeac0d] | 549 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[111b9b9] | 550 | thread_interrupt(thread);
|
---|
[121966e] | 551 | }
|
---|
[a35b458] | 552 |
|
---|
[07d4271] | 553 | irq_spinlock_unlock(&task->lock, true);
|
---|
[121966e] | 554 | }
|
---|
| 555 |
|
---|
[7509ddc] | 556 | /** Kill task.
|
---|
[ea7890e7] | 557 | *
|
---|
| 558 | * This function is idempotent.
|
---|
| 559 | * It signals all the task's threads to bail it out.
|
---|
[7509ddc] | 560 | *
|
---|
[5ba201d] | 561 | * @param id ID of the task to be killed.
|
---|
| 562 | *
|
---|
| 563 | * @return Zero on success or an error code from errno.h.
|
---|
[7509ddc] | 564 | *
|
---|
| 565 | */
|
---|
[b7fd2a0] | 566 | errno_t task_kill(task_id_t id)
|
---|
[7509ddc] | 567 | {
|
---|
[9b6aae6] | 568 | if (id == 1)
|
---|
| 569 | return EPERM;
|
---|
[a35b458] | 570 |
|
---|
[da1bafb] | 571 | task_t *task = task_find_by_id(id);
|
---|
[07d4271] | 572 | if (!task)
|
---|
[7509ddc] | 573 | return ENOENT;
|
---|
[a35b458] | 574 |
|
---|
[da1bafb] | 575 | task_kill_internal(task);
|
---|
[07d4271] | 576 | task_release(task);
|
---|
[da1bafb] | 577 | return EOK;
|
---|
[7509ddc] | 578 | }
|
---|
| 579 |
|
---|
[5bcf1f9] | 580 | /** Kill the currently running task.
|
---|
| 581 | *
|
---|
| 582 | * @param notify Send out fault notifications.
|
---|
| 583 | *
|
---|
| 584 | * @return Zero on success or an error code from errno.h.
|
---|
| 585 | *
|
---|
| 586 | */
|
---|
| 587 | void task_kill_self(bool notify)
|
---|
| 588 | {
|
---|
| 589 | /*
|
---|
| 590 | * User space can subscribe for FAULT events to take action
|
---|
| 591 | * whenever a task faults (to take a dump, run a debugger, etc.).
|
---|
| 592 | * The notification is always available, but unless udebug is enabled,
|
---|
| 593 | * that's all you get.
|
---|
[ae7d03c] | 594 | */
|
---|
[5bcf1f9] | 595 | if (notify) {
|
---|
[f9061b4] | 596 | /* Notify the subscriber that a fault occurred. */
|
---|
| 597 | if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
|
---|
| 598 | UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
|
---|
[5bcf1f9] | 599 | #ifdef CONFIG_UDEBUG
|
---|
| 600 | /* Wait for a debugging session. */
|
---|
| 601 | udebug_thread_fault();
|
---|
| 602 | #endif
|
---|
| 603 | }
|
---|
| 604 | }
|
---|
[a35b458] | 605 |
|
---|
[5bcf1f9] | 606 | task_kill_internal(TASK);
|
---|
| 607 | thread_exit();
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | /** Process syscall to terminate the current task.
|
---|
| 611 | *
|
---|
| 612 | * @param notify Send out fault notifications.
|
---|
| 613 | *
|
---|
| 614 | */
|
---|
[b7fd2a0] | 615 | sys_errno_t sys_task_exit(sysarg_t notify)
|
---|
[5bcf1f9] | 616 | {
|
---|
| 617 | task_kill_self(notify);
|
---|
[88e43bc] | 618 | unreachable();
|
---|
[5bcf1f9] | 619 | }
|
---|
| 620 |
|
---|
[ef1eab7] | 621 | static void task_print(task_t *task, bool additional)
|
---|
[b76a2217] | 622 | {
|
---|
[da1bafb] | 623 | irq_spinlock_lock(&task->lock, false);
|
---|
[a35b458] | 624 |
|
---|
[a2a00e8] | 625 | uint64_t ucycles;
|
---|
| 626 | uint64_t kcycles;
|
---|
[1ba37fa] | 627 | char usuffix, ksuffix;
|
---|
[da1bafb] | 628 | task_get_accounting(task, &ucycles, &kcycles);
|
---|
[e535eeb] | 629 | order_suffix(ucycles, &ucycles, &usuffix);
|
---|
| 630 | order_suffix(kcycles, &kcycles, &ksuffix);
|
---|
[a35b458] | 631 |
|
---|
[da1bafb] | 632 | #ifdef __32_BITS__
|
---|
[ef1eab7] | 633 | if (additional)
|
---|
[077842c] | 634 | printf("%-8" PRIu64 " %9zu", task->taskid,
|
---|
[07d4271] | 635 | atomic_load(&task->lifecount));
|
---|
[c0f13d2] | 636 | else
|
---|
| 637 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
|
---|
| 638 | " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
|
---|
[473d5d2] | 639 | task->name, task->container, task, task->as,
|
---|
[c0f13d2] | 640 | ucycles, usuffix, kcycles, ksuffix);
|
---|
[52755f1] | 641 | #endif
|
---|
[a35b458] | 642 |
|
---|
[52755f1] | 643 | #ifdef __64_BITS__
|
---|
[ef1eab7] | 644 | if (additional)
|
---|
[7e752b2] | 645 | printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
|
---|
[077842c] | 646 | "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
|
---|
[07d4271] | 647 | ksuffix, atomic_load(&task->lifecount));
|
---|
[c0f13d2] | 648 | else
|
---|
| 649 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
|
---|
[473d5d2] | 650 | task->taskid, task->name, task->container, task, task->as);
|
---|
[52755f1] | 651 | #endif
|
---|
[a35b458] | 652 |
|
---|
[da1bafb] | 653 | irq_spinlock_unlock(&task->lock, false);
|
---|
[b76a2217] | 654 | }
|
---|
| 655 |
|
---|
[c0f13d2] | 656 | /** Print task list
|
---|
| 657 | *
|
---|
| 658 | * @param additional Print additional information.
|
---|
| 659 | *
|
---|
| 660 | */
|
---|
| 661 | void task_print_list(bool additional)
|
---|
[37c57f2] | 662 | {
|
---|
[f74bbaf] | 663 | /* Messing with task structures, avoid deadlock */
|
---|
[da1bafb] | 664 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 665 |
|
---|
[5ba201d] | 666 | #ifdef __32_BITS__
|
---|
[c0f13d2] | 667 | if (additional)
|
---|
[48dcc69] | 668 | printf("[id ] [threads] [calls] [callee\n");
|
---|
[c0f13d2] | 669 | else
|
---|
[473d5d2] | 670 | printf("[id ] [name ] [ctn] [address ] [as ]"
|
---|
[c0f13d2] | 671 | " [ucycles ] [kcycles ]\n");
|
---|
[52755f1] | 672 | #endif
|
---|
[a35b458] | 673 |
|
---|
[52755f1] | 674 | #ifdef __64_BITS__
|
---|
[c0f13d2] | 675 | if (additional)
|
---|
[be06914] | 676 | printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
|
---|
[c0f13d2] | 677 | " [callee\n");
|
---|
| 678 | else
|
---|
[473d5d2] | 679 | printf("[id ] [name ] [ctn] [address ]"
|
---|
[c0f13d2] | 680 | " [as ]\n");
|
---|
[52755f1] | 681 | #endif
|
---|
[a35b458] | 682 |
|
---|
[ef1eab7] | 683 | task_t *task;
|
---|
| 684 |
|
---|
[aab5e46] | 685 | task = task_first();
|
---|
| 686 | while (task != NULL) {
|
---|
[ef1eab7] | 687 | task_print(task, additional);
|
---|
[aab5e46] | 688 | task = task_next(task);
|
---|
[ef1eab7] | 689 | }
|
---|
[a35b458] | 690 |
|
---|
[da1bafb] | 691 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[37c57f2] | 692 | }
|
---|
[7509ddc] | 693 |
|
---|
[ef1eab7] | 694 | /** Get key function for the @c tasks ordered dictionary.
|
---|
| 695 | *
|
---|
| 696 | * @param odlink Link
|
---|
| 697 | * @return Pointer to task ID cast as 'void *'
|
---|
| 698 | */
|
---|
| 699 | static void *tasks_getkey(odlink_t *odlink)
|
---|
| 700 | {
|
---|
| 701 | task_t *task = odict_get_instance(odlink, task_t, ltasks);
|
---|
| 702 | return (void *) &task->taskid;
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | /** Key comparison function for the @c tasks ordered dictionary.
|
---|
| 706 | *
|
---|
| 707 | * @param a Pointer to thread A ID
|
---|
| 708 | * @param b Pointer to thread B ID
|
---|
| 709 | * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
|
---|
| 710 | */
|
---|
| 711 | static int tasks_cmp(void *a, void *b)
|
---|
| 712 | {
|
---|
| 713 | task_id_t ida = *(task_id_t *)a;
|
---|
| 714 | task_id_t idb = *(task_id_t *)b;
|
---|
| 715 |
|
---|
| 716 | if (ida < idb)
|
---|
| 717 | return -1;
|
---|
| 718 | else if (ida == idb)
|
---|
| 719 | return 0;
|
---|
| 720 | else
|
---|
| 721 | return +1;
|
---|
| 722 | }
|
---|
| 723 |
|
---|
[cc73a8a1] | 724 | /** @}
|
---|
[b45c443] | 725 | */
|
---|