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