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