[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>
|
---|
[669f3d32] | 45 | #include <synch/futex.h>
|
---|
[f761f1eb] | 46 | #include <synch/spinlock.h>
|
---|
[5573942] | 47 | #include <synch/waitq.h>
|
---|
[f761f1eb] | 48 | #include <arch.h>
|
---|
[05882233] | 49 | #include <barrier.h>
|
---|
[7f6e755] | 50 | #include <adt/btree.h>
|
---|
[5c9a08b] | 51 | #include <adt/list.h>
|
---|
[ef1eab7] | 52 | #include <adt/odict.h>
|
---|
[3f74275] | 53 | #include <cap/cap.h>
|
---|
[6d9c49a] | 54 | #include <ipc/ipc.h>
|
---|
[c98e6ee] | 55 | #include <ipc/ipcrsc.h>
|
---|
[5d0500c] | 56 | #include <ipc/event.h>
|
---|
[bab75df6] | 57 | #include <stdio.h>
|
---|
[7509ddc] | 58 | #include <errno.h>
|
---|
[b2e121a] | 59 | #include <halt.h>
|
---|
[19f857a] | 60 | #include <str.h>
|
---|
[e3c762cd] | 61 | #include <syscall/copy.h>
|
---|
[95ad426] | 62 | #include <macros.h>
|
---|
[8e5e78f] | 63 |
|
---|
[ef1eab7] | 64 | /** Spinlock protecting the @c tasks ordered dictionary. */
|
---|
[da1bafb] | 65 | IRQ_SPINLOCK_INITIALIZE(tasks_lock);
|
---|
[88169d9] | 66 |
|
---|
[ef1eab7] | 67 | /** Ordered dictionary of active tasks.
|
---|
[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 | {
|
---|
[82d515e9] | 200 | task_t *task = (task_t *) slab_alloc(task_cache, 0);
|
---|
[6a32cc5f] | 201 | if (task == NULL) {
|
---|
| 202 | return NULL;
|
---|
| 203 | }
|
---|
[a35b458] | 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 |
|
---|
[9a1b20c] | 228 | #ifdef CONFIG_UDEBUG
|
---|
| 229 | /* Init debugging stuff */
|
---|
[da1bafb] | 230 | udebug_task_init(&task->udebug);
|
---|
[a35b458] | 231 |
|
---|
[9a1b20c] | 232 | /* Init kbox stuff */
|
---|
[c33f39f] | 233 | task->kb.box.active = true;
|
---|
[da1bafb] | 234 | task->kb.finished = false;
|
---|
[9a1b20c] | 235 | #endif
|
---|
[a35b458] | 236 |
|
---|
[cdc4334] | 237 | if ((ipc_box_0) &&
|
---|
| 238 | (container_check(ipc_box_0->task->container, task->container))) {
|
---|
[eadaeae8] | 239 | cap_phone_handle_t phone_handle;
|
---|
[334c103] | 240 | errno_t rc = phone_alloc(task, true, &phone_handle, NULL);
|
---|
[09d01f2] | 241 | if (rc != EOK) {
|
---|
[6a32cc5f] | 242 | task->as = NULL;
|
---|
| 243 | task_destroy_arch(task);
|
---|
| 244 | slab_free(task_cache, task);
|
---|
| 245 | return NULL;
|
---|
| 246 | }
|
---|
[a35b458] | 247 |
|
---|
[48bcf49] | 248 | kobject_t *phone_obj = kobject_get(task, phone_handle,
|
---|
| 249 | KOBJECT_TYPE_PHONE);
|
---|
[cdc4334] | 250 | (void) ipc_phone_connect(phone_obj->phone, ipc_box_0);
|
---|
[05ffb41] | 251 | }
|
---|
[a35b458] | 252 |
|
---|
[669f3d32] | 253 | futex_task_init(task);
|
---|
[a35b458] | 254 |
|
---|
[6193351] | 255 | /*
|
---|
| 256 | * Get a reference to the address space.
|
---|
| 257 | */
|
---|
[da1bafb] | 258 | as_hold(task->as);
|
---|
[a35b458] | 259 |
|
---|
[da1bafb] | 260 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 261 |
|
---|
[da1bafb] | 262 | task->taskid = ++task_counter;
|
---|
[ef1eab7] | 263 | odlink_initialize(&task->ltasks);
|
---|
| 264 | odict_insert(&task->ltasks, &tasks, NULL);
|
---|
[a35b458] | 265 |
|
---|
[da1bafb] | 266 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 267 |
|
---|
[da1bafb] | 268 | return task;
|
---|
[f761f1eb] | 269 | }
|
---|
| 270 |
|
---|
[7509ddc] | 271 | /** Destroy task.
|
---|
| 272 | *
|
---|
[da1bafb] | 273 | * @param task Task to be destroyed.
|
---|
[5ba201d] | 274 | *
|
---|
[7509ddc] | 275 | */
|
---|
[da1bafb] | 276 | void task_destroy(task_t *task)
|
---|
[7509ddc] | 277 | {
|
---|
[ea7890e7] | 278 | /*
|
---|
| 279 | * Remove the task from the task B+tree.
|
---|
| 280 | */
|
---|
[da1bafb] | 281 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[ef1eab7] | 282 | odict_remove(&task->ltasks);
|
---|
[da1bafb] | 283 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 284 |
|
---|
[ea7890e7] | 285 | /*
|
---|
| 286 | * Perform architecture specific task destruction.
|
---|
| 287 | */
|
---|
[da1bafb] | 288 | task_destroy_arch(task);
|
---|
[a35b458] | 289 |
|
---|
[ea7890e7] | 290 | /*
|
---|
| 291 | * Free up dynamically allocated state.
|
---|
| 292 | */
|
---|
[669f3d32] | 293 | futex_task_deinit(task);
|
---|
[a35b458] | 294 |
|
---|
[ea7890e7] | 295 | /*
|
---|
| 296 | * Drop our reference to the address space.
|
---|
| 297 | */
|
---|
[da1bafb] | 298 | as_release(task->as);
|
---|
[a35b458] | 299 |
|
---|
[82d515e9] | 300 | slab_free(task_cache, task);
|
---|
[7509ddc] | 301 | }
|
---|
| 302 |
|
---|
[278b4a30] | 303 | /** Hold a reference to a task.
|
---|
| 304 | *
|
---|
| 305 | * Holding a reference to a task prevents destruction of that task.
|
---|
| 306 | *
|
---|
[da1bafb] | 307 | * @param task Task to be held.
|
---|
| 308 | *
|
---|
[278b4a30] | 309 | */
|
---|
[da1bafb] | 310 | void task_hold(task_t *task)
|
---|
[278b4a30] | 311 | {
|
---|
[da1bafb] | 312 | atomic_inc(&task->refcount);
|
---|
[278b4a30] | 313 | }
|
---|
| 314 |
|
---|
| 315 | /** Release a reference to a task.
|
---|
| 316 | *
|
---|
| 317 | * The last one to release a reference to a task destroys the task.
|
---|
| 318 | *
|
---|
[da1bafb] | 319 | * @param task Task to be released.
|
---|
| 320 | *
|
---|
[278b4a30] | 321 | */
|
---|
[da1bafb] | 322 | void task_release(task_t *task)
|
---|
[278b4a30] | 323 | {
|
---|
[da1bafb] | 324 | if ((atomic_predec(&task->refcount)) == 0)
|
---|
| 325 | task_destroy(task);
|
---|
[278b4a30] | 326 | }
|
---|
| 327 |
|
---|
[dd8d5a7] | 328 | #ifdef __32_BITS__
|
---|
| 329 |
|
---|
| 330 | /** Syscall for reading task ID from userspace (32 bits)
|
---|
[ec55358] | 331 | *
|
---|
[dd8d5a7] | 332 | * @param uspace_taskid Pointer to user-space buffer
|
---|
| 333 | * where to store current task ID.
|
---|
[5ba201d] | 334 | *
|
---|
| 335 | * @return Zero on success or an error code from @ref errno.h.
|
---|
[ec55358] | 336 | *
|
---|
| 337 | */
|
---|
[b7fd2a0] | 338 | sys_errno_t sys_task_get_id(sysarg64_t *uspace_taskid)
|
---|
[ec55358] | 339 | {
|
---|
| 340 | /*
|
---|
[814c4f5] | 341 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 342 | * the lifespan of the task.
|
---|
[ec55358] | 343 | */
|
---|
[b7fd2a0] | 344 | return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
|
---|
[6f4495f5] | 345 | sizeof(TASK->taskid));
|
---|
[ec55358] | 346 | }
|
---|
| 347 |
|
---|
[dd8d5a7] | 348 | #endif /* __32_BITS__ */
|
---|
| 349 |
|
---|
| 350 | #ifdef __64_BITS__
|
---|
| 351 |
|
---|
| 352 | /** Syscall for reading task ID from userspace (64 bits)
|
---|
| 353 | *
|
---|
| 354 | * @return Current task ID.
|
---|
| 355 | *
|
---|
| 356 | */
|
---|
| 357 | sysarg_t sys_task_get_id(void)
|
---|
| 358 | {
|
---|
| 359 | /*
|
---|
| 360 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
| 361 | * the lifespan of the task.
|
---|
| 362 | */
|
---|
| 363 | return TASK->taskid;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | #endif /* __64_BITS__ */
|
---|
| 367 |
|
---|
[bc18d63] | 368 | /** Syscall for setting the task name.
|
---|
| 369 | *
|
---|
| 370 | * The name simplifies identifying the task in the task list.
|
---|
| 371 | *
|
---|
[5ba201d] | 372 | * @param name The new name for the task. (typically the same
|
---|
| 373 | * as the command used to execute it).
|
---|
[bc18d63] | 374 | *
|
---|
| 375 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[5ba201d] | 376 | *
|
---|
[bc18d63] | 377 | */
|
---|
[b7fd2a0] | 378 | sys_errno_t sys_task_set_name(const char *uspace_name, size_t name_len)
|
---|
[bc18d63] | 379 | {
|
---|
| 380 | char namebuf[TASK_NAME_BUFLEN];
|
---|
[a35b458] | 381 |
|
---|
[bc18d63] | 382 | /* Cap length of name and copy it from userspace. */
|
---|
| 383 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
| 384 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
[a35b458] | 385 |
|
---|
[b7fd2a0] | 386 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
[a53ed3a] | 387 | if (rc != EOK)
|
---|
[b7fd2a0] | 388 | return (sys_errno_t) rc;
|
---|
[a35b458] | 389 |
|
---|
[f4b1535] | 390 | namebuf[name_len] = '\0';
|
---|
[a35b458] | 391 |
|
---|
[577f042a] | 392 | /*
|
---|
| 393 | * As the task name is referenced also from the
|
---|
| 394 | * threads, lock the threads' lock for the course
|
---|
| 395 | * of the update.
|
---|
| 396 | */
|
---|
[a35b458] | 397 |
|
---|
[577f042a] | 398 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 399 | irq_spinlock_lock(&TASK->lock, false);
|
---|
| 400 | irq_spinlock_lock(&threads_lock, false);
|
---|
[a35b458] | 401 |
|
---|
[577f042a] | 402 | /* Set task name */
|
---|
[f4b1535] | 403 | str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
|
---|
[a35b458] | 404 |
|
---|
[577f042a] | 405 | irq_spinlock_unlock(&threads_lock, false);
|
---|
| 406 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
| 407 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 408 |
|
---|
[bc18d63] | 409 | return EOK;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[1e9f8ab] | 412 | /** Syscall to forcefully terminate a task
|
---|
| 413 | *
|
---|
| 414 | * @param uspace_taskid Pointer to task ID in user space.
|
---|
| 415 | *
|
---|
| 416 | * @return 0 on success or an error code from @ref errno.h.
|
---|
| 417 | *
|
---|
| 418 | */
|
---|
[b7fd2a0] | 419 | sys_errno_t sys_task_kill(task_id_t *uspace_taskid)
|
---|
[1e9f8ab] | 420 | {
|
---|
| 421 | task_id_t taskid;
|
---|
[b7fd2a0] | 422 | errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
|
---|
[a53ed3a] | 423 | if (rc != EOK)
|
---|
[b7fd2a0] | 424 | return (sys_errno_t) rc;
|
---|
[a35b458] | 425 |
|
---|
[b7fd2a0] | 426 | return (sys_errno_t) task_kill(taskid);
|
---|
[1e9f8ab] | 427 | }
|
---|
| 428 |
|
---|
[9a8d91b] | 429 | /** Find task structure corresponding to task ID.
|
---|
| 430 | *
|
---|
[814c4f5] | 431 | * The tasks_lock must be already held by the caller of this function and
|
---|
| 432 | * interrupts must be disabled.
|
---|
[9a8d91b] | 433 | *
|
---|
[5ba201d] | 434 | * @param id Task ID.
|
---|
| 435 | *
|
---|
[e1b6742] | 436 | * @return Task structure address or NULL if there is no such task ID.
|
---|
[9a8d91b] | 437 | *
|
---|
| 438 | */
|
---|
[5ba201d] | 439 | task_t *task_find_by_id(task_id_t id)
|
---|
| 440 | {
|
---|
[63e27ef] | 441 | assert(interrupts_disabled());
|
---|
| 442 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
[1d432f9] | 443 |
|
---|
[ef1eab7] | 444 | odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
|
---|
| 445 | if (odlink != NULL)
|
---|
| 446 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
[a35b458] | 447 |
|
---|
[b76a2217] | 448 | return NULL;
|
---|
[9a8d91b] | 449 | }
|
---|
| 450 |
|
---|
[aab5e46] | 451 | /** Get count of tasks.
|
---|
| 452 | *
|
---|
| 453 | * @return Number of tasks in the system
|
---|
| 454 | */
|
---|
| 455 | size_t task_count(void)
|
---|
| 456 | {
|
---|
| 457 | assert(interrupts_disabled());
|
---|
| 458 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 459 |
|
---|
| 460 | return odict_count(&tasks);
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /** Get first task (task with lowest ID).
|
---|
| 464 | *
|
---|
| 465 | * @return Pointer to first task or @c NULL if there are none.
|
---|
| 466 | */
|
---|
| 467 | task_t *task_first(void)
|
---|
| 468 | {
|
---|
| 469 | odlink_t *odlink;
|
---|
| 470 |
|
---|
| 471 | assert(interrupts_disabled());
|
---|
| 472 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 473 |
|
---|
| 474 | odlink = odict_first(&tasks);
|
---|
| 475 | if (odlink == NULL)
|
---|
| 476 | return NULL;
|
---|
| 477 |
|
---|
| 478 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | /** Get next task (with higher task ID).
|
---|
| 482 | *
|
---|
| 483 | * @param cur Current task
|
---|
| 484 | * @return Pointer to next task or @c NULL if there are no more tasks.
|
---|
| 485 | */
|
---|
| 486 | task_t *task_next(task_t *cur)
|
---|
| 487 | {
|
---|
| 488 | odlink_t *odlink;
|
---|
| 489 |
|
---|
| 490 | assert(interrupts_disabled());
|
---|
| 491 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
| 492 |
|
---|
| 493 | odlink = odict_next(&cur->ltasks, &tasks);
|
---|
| 494 | if (odlink == NULL)
|
---|
| 495 | return NULL;
|
---|
| 496 |
|
---|
| 497 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
| 498 | }
|
---|
| 499 |
|
---|
[0313ff0] | 500 | /** Get accounting data of given task.
|
---|
| 501 | *
|
---|
[1d432f9] | 502 | * Note that task lock of 'task' must be already held and interrupts must be
|
---|
[814c4f5] | 503 | * already disabled.
|
---|
[0313ff0] | 504 | *
|
---|
[da1bafb] | 505 | * @param task Pointer to the task.
|
---|
[88dea9d] | 506 | * @param ucycles Out pointer to sum of all user cycles.
|
---|
| 507 | * @param kcycles Out pointer to sum of all kernel cycles.
|
---|
[0313ff0] | 508 | *
|
---|
| 509 | */
|
---|
[da1bafb] | 510 | void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
|
---|
[0313ff0] | 511 | {
|
---|
[63e27ef] | 512 | assert(interrupts_disabled());
|
---|
| 513 | assert(irq_spinlock_locked(&task->lock));
|
---|
[1d432f9] | 514 |
|
---|
[a2a00e8] | 515 | /* Accumulated values of task */
|
---|
[da1bafb] | 516 | uint64_t uret = task->ucycles;
|
---|
| 517 | uint64_t kret = task->kcycles;
|
---|
[a35b458] | 518 |
|
---|
[0313ff0] | 519 | /* Current values of threads */
|
---|
[feeac0d] | 520 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[da1bafb] | 521 | irq_spinlock_lock(&thread->lock, false);
|
---|
[a35b458] | 522 |
|
---|
[62b6d17] | 523 | /* Process only counted threads */
|
---|
[da1bafb] | 524 | if (!thread->uncounted) {
|
---|
| 525 | if (thread == THREAD) {
|
---|
[6f4495f5] | 526 | /* Update accounting of current thread */
|
---|
[a2a00e8] | 527 | thread_update_accounting(false);
|
---|
[da1bafb] | 528 | }
|
---|
[a35b458] | 529 |
|
---|
[da1bafb] | 530 | uret += thread->ucycles;
|
---|
| 531 | kret += thread->kcycles;
|
---|
[62b6d17] | 532 | }
|
---|
[a35b458] | 533 |
|
---|
[da1bafb] | 534 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[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 | {
|
---|
[df58e44] | 543 | irq_spinlock_lock(&task->lock, false);
|
---|
| 544 | irq_spinlock_lock(&threads_lock, false);
|
---|
[a35b458] | 545 |
|
---|
[121966e] | 546 | /*
|
---|
| 547 | * Interrupt all threads.
|
---|
| 548 | */
|
---|
[a35b458] | 549 |
|
---|
[feeac0d] | 550 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
[121966e] | 551 | bool sleeping = false;
|
---|
[a35b458] | 552 |
|
---|
[da1bafb] | 553 | irq_spinlock_lock(&thread->lock, false);
|
---|
[a35b458] | 554 |
|
---|
[da1bafb] | 555 | thread->interrupted = true;
|
---|
| 556 | if (thread->state == Sleeping)
|
---|
[121966e] | 557 | sleeping = true;
|
---|
[a35b458] | 558 |
|
---|
[da1bafb] | 559 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[a35b458] | 560 |
|
---|
[121966e] | 561 | if (sleeping)
|
---|
[da1bafb] | 562 | waitq_interrupt_sleep(thread);
|
---|
[121966e] | 563 | }
|
---|
[a35b458] | 564 |
|
---|
[df58e44] | 565 | irq_spinlock_unlock(&threads_lock, false);
|
---|
[da1bafb] | 566 | irq_spinlock_unlock(&task->lock, false);
|
---|
[121966e] | 567 | }
|
---|
| 568 |
|
---|
[7509ddc] | 569 | /** Kill task.
|
---|
[ea7890e7] | 570 | *
|
---|
| 571 | * This function is idempotent.
|
---|
| 572 | * It signals all the task's threads to bail it out.
|
---|
[7509ddc] | 573 | *
|
---|
[5ba201d] | 574 | * @param id ID of the task to be killed.
|
---|
| 575 | *
|
---|
| 576 | * @return Zero on success or an error code from errno.h.
|
---|
[7509ddc] | 577 | *
|
---|
| 578 | */
|
---|
[b7fd2a0] | 579 | errno_t task_kill(task_id_t id)
|
---|
[7509ddc] | 580 | {
|
---|
[9b6aae6] | 581 | if (id == 1)
|
---|
| 582 | return EPERM;
|
---|
[a35b458] | 583 |
|
---|
[da1bafb] | 584 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 585 |
|
---|
[da1bafb] | 586 | task_t *task = task_find_by_id(id);
|
---|
| 587 | if (!task) {
|
---|
| 588 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[7509ddc] | 589 | return ENOENT;
|
---|
| 590 | }
|
---|
[a35b458] | 591 |
|
---|
[da1bafb] | 592 | task_kill_internal(task);
|
---|
| 593 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 594 |
|
---|
[da1bafb] | 595 | return EOK;
|
---|
[7509ddc] | 596 | }
|
---|
| 597 |
|
---|
[5bcf1f9] | 598 | /** Kill the currently running task.
|
---|
| 599 | *
|
---|
| 600 | * @param notify Send out fault notifications.
|
---|
| 601 | *
|
---|
| 602 | * @return Zero on success or an error code from errno.h.
|
---|
| 603 | *
|
---|
| 604 | */
|
---|
| 605 | void task_kill_self(bool notify)
|
---|
| 606 | {
|
---|
| 607 | /*
|
---|
| 608 | * User space can subscribe for FAULT events to take action
|
---|
| 609 | * whenever a task faults (to take a dump, run a debugger, etc.).
|
---|
| 610 | * The notification is always available, but unless udebug is enabled,
|
---|
| 611 | * that's all you get.
|
---|
[ae7d03c] | 612 | */
|
---|
[5bcf1f9] | 613 | if (notify) {
|
---|
[f9061b4] | 614 | /* Notify the subscriber that a fault occurred. */
|
---|
| 615 | if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
|
---|
| 616 | UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
|
---|
[5bcf1f9] | 617 | #ifdef CONFIG_UDEBUG
|
---|
| 618 | /* Wait for a debugging session. */
|
---|
| 619 | udebug_thread_fault();
|
---|
| 620 | #endif
|
---|
| 621 | }
|
---|
| 622 | }
|
---|
[a35b458] | 623 |
|
---|
[5bcf1f9] | 624 | irq_spinlock_lock(&tasks_lock, true);
|
---|
| 625 | task_kill_internal(TASK);
|
---|
| 626 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[a35b458] | 627 |
|
---|
[5bcf1f9] | 628 | thread_exit();
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | /** Process syscall to terminate the current task.
|
---|
| 632 | *
|
---|
| 633 | * @param notify Send out fault notifications.
|
---|
| 634 | *
|
---|
| 635 | */
|
---|
[b7fd2a0] | 636 | sys_errno_t sys_task_exit(sysarg_t notify)
|
---|
[5bcf1f9] | 637 | {
|
---|
| 638 | task_kill_self(notify);
|
---|
[a35b458] | 639 |
|
---|
[5bcf1f9] | 640 | /* Unreachable */
|
---|
| 641 | return EOK;
|
---|
| 642 | }
|
---|
| 643 |
|
---|
[ef1eab7] | 644 | static void task_print(task_t *task, bool additional)
|
---|
[b76a2217] | 645 | {
|
---|
[da1bafb] | 646 | irq_spinlock_lock(&task->lock, false);
|
---|
[a35b458] | 647 |
|
---|
[a2a00e8] | 648 | uint64_t ucycles;
|
---|
| 649 | uint64_t kcycles;
|
---|
[1ba37fa] | 650 | char usuffix, ksuffix;
|
---|
[da1bafb] | 651 | task_get_accounting(task, &ucycles, &kcycles);
|
---|
[e535eeb] | 652 | order_suffix(ucycles, &ucycles, &usuffix);
|
---|
| 653 | order_suffix(kcycles, &kcycles, &ksuffix);
|
---|
[a35b458] | 654 |
|
---|
[da1bafb] | 655 | #ifdef __32_BITS__
|
---|
[ef1eab7] | 656 | if (additional)
|
---|
[077842c] | 657 | printf("%-8" PRIu64 " %9zu", task->taskid,
|
---|
[036e97c] | 658 | atomic_load(&task->refcount));
|
---|
[c0f13d2] | 659 | else
|
---|
| 660 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
|
---|
| 661 | " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
|
---|
[473d5d2] | 662 | task->name, task->container, task, task->as,
|
---|
[c0f13d2] | 663 | ucycles, usuffix, kcycles, ksuffix);
|
---|
[52755f1] | 664 | #endif
|
---|
[a35b458] | 665 |
|
---|
[52755f1] | 666 | #ifdef __64_BITS__
|
---|
[ef1eab7] | 667 | if (additional)
|
---|
[7e752b2] | 668 | printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
|
---|
[077842c] | 669 | "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
|
---|
[036e97c] | 670 | ksuffix, atomic_load(&task->refcount));
|
---|
[c0f13d2] | 671 | else
|
---|
| 672 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
|
---|
[473d5d2] | 673 | task->taskid, task->name, task->container, task, task->as);
|
---|
[52755f1] | 674 | #endif
|
---|
[a35b458] | 675 |
|
---|
[da1bafb] | 676 | irq_spinlock_unlock(&task->lock, false);
|
---|
[b76a2217] | 677 | }
|
---|
| 678 |
|
---|
[c0f13d2] | 679 | /** Print task list
|
---|
| 680 | *
|
---|
| 681 | * @param additional Print additional information.
|
---|
| 682 | *
|
---|
| 683 | */
|
---|
| 684 | void task_print_list(bool additional)
|
---|
[37c57f2] | 685 | {
|
---|
[f74bbaf] | 686 | /* Messing with task structures, avoid deadlock */
|
---|
[da1bafb] | 687 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[a35b458] | 688 |
|
---|
[5ba201d] | 689 | #ifdef __32_BITS__
|
---|
[c0f13d2] | 690 | if (additional)
|
---|
[48dcc69] | 691 | printf("[id ] [threads] [calls] [callee\n");
|
---|
[c0f13d2] | 692 | else
|
---|
[473d5d2] | 693 | printf("[id ] [name ] [ctn] [address ] [as ]"
|
---|
[c0f13d2] | 694 | " [ucycles ] [kcycles ]\n");
|
---|
[52755f1] | 695 | #endif
|
---|
[a35b458] | 696 |
|
---|
[52755f1] | 697 | #ifdef __64_BITS__
|
---|
[c0f13d2] | 698 | if (additional)
|
---|
[be06914] | 699 | printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
|
---|
[c0f13d2] | 700 | " [callee\n");
|
---|
| 701 | else
|
---|
[473d5d2] | 702 | printf("[id ] [name ] [ctn] [address ]"
|
---|
[c0f13d2] | 703 | " [as ]\n");
|
---|
[52755f1] | 704 | #endif
|
---|
[a35b458] | 705 |
|
---|
[ef1eab7] | 706 | task_t *task;
|
---|
| 707 |
|
---|
[aab5e46] | 708 | task = task_first();
|
---|
| 709 | while (task != NULL) {
|
---|
[ef1eab7] | 710 | task_print(task, additional);
|
---|
[aab5e46] | 711 | task = task_next(task);
|
---|
[ef1eab7] | 712 | }
|
---|
[a35b458] | 713 |
|
---|
[da1bafb] | 714 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[37c57f2] | 715 | }
|
---|
[7509ddc] | 716 |
|
---|
[ef1eab7] | 717 | /** Get key function for the @c tasks ordered dictionary.
|
---|
| 718 | *
|
---|
| 719 | * @param odlink Link
|
---|
| 720 | * @return Pointer to task ID cast as 'void *'
|
---|
| 721 | */
|
---|
| 722 | static void *tasks_getkey(odlink_t *odlink)
|
---|
| 723 | {
|
---|
| 724 | task_t *task = odict_get_instance(odlink, task_t, ltasks);
|
---|
| 725 | return (void *) &task->taskid;
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | /** Key comparison function for the @c tasks ordered dictionary.
|
---|
| 729 | *
|
---|
| 730 | * @param a Pointer to thread A ID
|
---|
| 731 | * @param b Pointer to thread B ID
|
---|
| 732 | * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
|
---|
| 733 | */
|
---|
| 734 | static int tasks_cmp(void *a, void *b)
|
---|
| 735 | {
|
---|
| 736 | task_id_t ida = *(task_id_t *)a;
|
---|
| 737 | task_id_t idb = *(task_id_t *)b;
|
---|
| 738 |
|
---|
| 739 | if (ida < idb)
|
---|
| 740 | return -1;
|
---|
| 741 | else if (ida == idb)
|
---|
| 742 | return 0;
|
---|
| 743 | else
|
---|
| 744 | return +1;
|
---|
| 745 | }
|
---|
| 746 |
|
---|
[cc73a8a1] | 747 | /** @}
|
---|
[b45c443] | 748 | */
|
---|