source: mainline/kernel/generic/src/proc/task.c@ fb13b44

Last change on this file since fb13b44 was fb13b44, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Spawned tasks' phone is connected to spawning task

  • Only boot time tasks are connected to hardcoded NS task.
  • First connected phone of other tasks is connected to the task that spawned them (spawn parent).
  • Until further changes, effectively it changes nothing (NS was the ultimate spawn parent).

Conflicts:

kernel/generic/include/proc/program.h
kernel/generic/src/ipc/ipc.c
kernel/generic/src/proc/program.c
kernel/generic/src/proc/task.c

  • Property mode set to 100644
File size: 16.6 KB
RevLine 
[f761f1eb]1/*
[278b4a30]2 * Copyright (c) 2010 Jakub Jermar
[ef1eab7]3 * Copyright (c) 2018 Jiri Svoboda
[f761f1eb]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[174156fd]30/** @addtogroup kernel_generic_proc
[b45c443]31 * @{
32 */
33
[9179d0a]34/**
[b45c443]35 * @file
[5ba201d]36 * @brief Task management.
[9179d0a]37 */
38
[63e27ef]39#include <assert.h>
[f761f1eb]40#include <proc/thread.h>
41#include <proc/task.h>
[20d50a1]42#include <mm/as.h>
[085d973]43#include <mm/slab.h>
[31d8e10]44#include <atomic.h>
[f761f1eb]45#include <synch/spinlock.h>
[5573942]46#include <synch/waitq.h>
[f761f1eb]47#include <arch.h>
[05882233]48#include <barrier.h>
[5c9a08b]49#include <adt/list.h>
[ef1eab7]50#include <adt/odict.h>
[3f74275]51#include <cap/cap.h>
[6d9c49a]52#include <ipc/ipc.h>
[c98e6ee]53#include <ipc/ipcrsc.h>
[5d0500c]54#include <ipc/event.h>
[bab75df6]55#include <stdio.h>
[7509ddc]56#include <errno.h>
[b2e121a]57#include <halt.h>
[19f857a]58#include <str.h>
[e3c762cd]59#include <syscall/copy.h>
[95ad426]60#include <macros.h>
[8e5e78f]61
[ef1eab7]62/** Spinlock protecting the @c tasks ordered dictionary. */
[da1bafb]63IRQ_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]78odict_t tasks;
[88169d9]79
[286e03d]80static task_id_t task_counter = 0;
[70527f1]81
[82d515e9]82static slab_cache_t *task_cache;
[103de761]83
[121966e]84/* Forward declarations. */
85static void task_kill_internal(task_t *);
[b7fd2a0]86static errno_t tsk_constructor(void *, unsigned int);
[ef1eab7]87static size_t tsk_destructor(void *);
88
89static void *tasks_getkey(odlink_t *);
90static int tasks_cmp(void *, void *);
[121966e]91
[da1bafb]92/** Initialize kernel tasks support.
93 *
94 */
[f761f1eb]95void 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]106void 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]152errno_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]182size_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 *
[fb13b44]192 * @param as Task's address space.
193 * @param name Symbolic name (a copy is made).
194 * @param answerbox (Optional) answerbox where box 0 is connected to.
[70527f1]195 *
[5ba201d]196 * @return New task's structure.
[70527f1]197 *
198 */
[fb13b44]199task_t *task_create(as_t *as, const char *name, answerbox_t *answerbox)
[f761f1eb]200{
[abf6c01]201 task_t *task = (task_t *) slab_alloc(task_cache, FRAME_ATOMIC);
202 if (!task)
[6a32cc5f]203 return NULL;
[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
[fb13b44]237 if ((answerbox) &&
238 (container_check(answerbox->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);
[fb13b44]250 (void) ipc_phone_connect(phone_obj->phone, answerbox);
[05ffb41]251 }
[a35b458]252
[da1bafb]253 irq_spinlock_lock(&tasks_lock, true);
[a35b458]254
[da1bafb]255 task->taskid = ++task_counter;
[ef1eab7]256 odlink_initialize(&task->ltasks);
257 odict_insert(&task->ltasks, &tasks, NULL);
[a35b458]258
[da1bafb]259 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]260
[da1bafb]261 return task;
[f761f1eb]262}
263
[7509ddc]264/** Destroy task.
265 *
[da1bafb]266 * @param task Task to be destroyed.
[5ba201d]267 *
[7509ddc]268 */
[da1bafb]269void task_destroy(task_t *task)
[7509ddc]270{
[ea7890e7]271 /*
[ca4c5596]272 * Remove the task from the task odict.
[ea7890e7]273 */
[da1bafb]274 irq_spinlock_lock(&tasks_lock, true);
[ef1eab7]275 odict_remove(&task->ltasks);
[da1bafb]276 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]277
[ea7890e7]278 /*
279 * Perform architecture specific task destruction.
280 */
[da1bafb]281 task_destroy_arch(task);
[a35b458]282
[ea7890e7]283 /*
284 * Drop our reference to the address space.
285 */
[da1bafb]286 as_release(task->as);
[a35b458]287
[82d515e9]288 slab_free(task_cache, task);
[7509ddc]289}
290
[278b4a30]291/** Hold a reference to a task.
292 *
293 * Holding a reference to a task prevents destruction of that task.
294 *
[da1bafb]295 * @param task Task to be held.
296 *
[278b4a30]297 */
[da1bafb]298void task_hold(task_t *task)
[278b4a30]299{
[da1bafb]300 atomic_inc(&task->refcount);
[278b4a30]301}
302
303/** Release a reference to a task.
304 *
305 * The last one to release a reference to a task destroys the task.
306 *
[da1bafb]307 * @param task Task to be released.
308 *
[278b4a30]309 */
[da1bafb]310void task_release(task_t *task)
[278b4a30]311{
[da1bafb]312 if ((atomic_predec(&task->refcount)) == 0)
313 task_destroy(task);
[278b4a30]314}
315
[dd8d5a7]316#ifdef __32_BITS__
317
318/** Syscall for reading task ID from userspace (32 bits)
[ec55358]319 *
[dd8d5a7]320 * @param uspace_taskid Pointer to user-space buffer
321 * where to store current task ID.
[5ba201d]322 *
323 * @return Zero on success or an error code from @ref errno.h.
[ec55358]324 *
325 */
[5a5269d]326sys_errno_t sys_task_get_id(uspace_ptr_sysarg64_t uspace_taskid)
[ec55358]327{
328 /*
[814c4f5]329 * No need to acquire lock on TASK because taskid remains constant for
330 * the lifespan of the task.
[ec55358]331 */
[b7fd2a0]332 return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
[6f4495f5]333 sizeof(TASK->taskid));
[ec55358]334}
335
[dd8d5a7]336#endif /* __32_BITS__ */
337
338#ifdef __64_BITS__
339
340/** Syscall for reading task ID from userspace (64 bits)
341 *
342 * @return Current task ID.
343 *
344 */
345sysarg_t sys_task_get_id(void)
346{
347 /*
348 * No need to acquire lock on TASK because taskid remains constant for
349 * the lifespan of the task.
350 */
351 return TASK->taskid;
352}
353
354#endif /* __64_BITS__ */
355
[bc18d63]356/** Syscall for setting the task name.
357 *
358 * The name simplifies identifying the task in the task list.
359 *
[5ba201d]360 * @param name The new name for the task. (typically the same
361 * as the command used to execute it).
[bc18d63]362 *
363 * @return 0 on success or an error code from @ref errno.h.
[5ba201d]364 *
[bc18d63]365 */
[5a5269d]366sys_errno_t sys_task_set_name(const uspace_ptr_char uspace_name, size_t name_len)
[bc18d63]367{
368 char namebuf[TASK_NAME_BUFLEN];
[a35b458]369
[bc18d63]370 /* Cap length of name and copy it from userspace. */
371 if (name_len > TASK_NAME_BUFLEN - 1)
372 name_len = TASK_NAME_BUFLEN - 1;
[a35b458]373
[b7fd2a0]374 errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
[a53ed3a]375 if (rc != EOK)
[b7fd2a0]376 return (sys_errno_t) rc;
[a35b458]377
[f4b1535]378 namebuf[name_len] = '\0';
[a35b458]379
[577f042a]380 /*
381 * As the task name is referenced also from the
382 * threads, lock the threads' lock for the course
383 * of the update.
384 */
[a35b458]385
[577f042a]386 irq_spinlock_lock(&tasks_lock, true);
387 irq_spinlock_lock(&TASK->lock, false);
388 irq_spinlock_lock(&threads_lock, false);
[a35b458]389
[577f042a]390 /* Set task name */
[f4b1535]391 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
[a35b458]392
[577f042a]393 irq_spinlock_unlock(&threads_lock, false);
394 irq_spinlock_unlock(&TASK->lock, false);
395 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]396
[bc18d63]397 return EOK;
398}
399
[1e9f8ab]400/** Syscall to forcefully terminate a task
401 *
402 * @param uspace_taskid Pointer to task ID in user space.
403 *
404 * @return 0 on success or an error code from @ref errno.h.
405 *
406 */
[5a5269d]407sys_errno_t sys_task_kill(uspace_ptr_task_id_t uspace_taskid)
[1e9f8ab]408{
409 task_id_t taskid;
[b7fd2a0]410 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
[a53ed3a]411 if (rc != EOK)
[b7fd2a0]412 return (sys_errno_t) rc;
[a35b458]413
[b7fd2a0]414 return (sys_errno_t) task_kill(taskid);
[1e9f8ab]415}
416
[9a8d91b]417/** Find task structure corresponding to task ID.
418 *
[814c4f5]419 * The tasks_lock must be already held by the caller of this function and
420 * interrupts must be disabled.
[9a8d91b]421 *
[5ba201d]422 * @param id Task ID.
423 *
[e1b6742]424 * @return Task structure address or NULL if there is no such task ID.
[9a8d91b]425 *
426 */
[5ba201d]427task_t *task_find_by_id(task_id_t id)
428{
[63e27ef]429 assert(interrupts_disabled());
430 assert(irq_spinlock_locked(&tasks_lock));
[1d432f9]431
[ef1eab7]432 odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
433 if (odlink != NULL)
434 return odict_get_instance(odlink, task_t, ltasks);
[a35b458]435
[b76a2217]436 return NULL;
[9a8d91b]437}
438
[aab5e46]439/** Get count of tasks.
440 *
441 * @return Number of tasks in the system
442 */
443size_t task_count(void)
444{
445 assert(interrupts_disabled());
446 assert(irq_spinlock_locked(&tasks_lock));
447
448 return odict_count(&tasks);
449}
450
451/** Get first task (task with lowest ID).
452 *
453 * @return Pointer to first task or @c NULL if there are none.
454 */
455task_t *task_first(void)
456{
457 odlink_t *odlink;
458
459 assert(interrupts_disabled());
460 assert(irq_spinlock_locked(&tasks_lock));
461
462 odlink = odict_first(&tasks);
463 if (odlink == NULL)
464 return NULL;
465
466 return odict_get_instance(odlink, task_t, ltasks);
467}
468
469/** Get next task (with higher task ID).
470 *
471 * @param cur Current task
472 * @return Pointer to next task or @c NULL if there are no more tasks.
473 */
474task_t *task_next(task_t *cur)
475{
476 odlink_t *odlink;
477
478 assert(interrupts_disabled());
479 assert(irq_spinlock_locked(&tasks_lock));
480
481 odlink = odict_next(&cur->ltasks, &tasks);
482 if (odlink == NULL)
483 return NULL;
484
485 return odict_get_instance(odlink, task_t, ltasks);
486}
487
[0313ff0]488/** Get accounting data of given task.
489 *
[1d432f9]490 * Note that task lock of 'task' must be already held and interrupts must be
[814c4f5]491 * already disabled.
[0313ff0]492 *
[da1bafb]493 * @param task Pointer to the task.
[88dea9d]494 * @param ucycles Out pointer to sum of all user cycles.
495 * @param kcycles Out pointer to sum of all kernel cycles.
[0313ff0]496 *
497 */
[da1bafb]498void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
[0313ff0]499{
[63e27ef]500 assert(interrupts_disabled());
501 assert(irq_spinlock_locked(&task->lock));
[1d432f9]502
[a2a00e8]503 /* Accumulated values of task */
[da1bafb]504 uint64_t uret = task->ucycles;
505 uint64_t kret = task->kcycles;
[a35b458]506
[0313ff0]507 /* Current values of threads */
[feeac0d]508 list_foreach(task->threads, th_link, thread_t, thread) {
[da1bafb]509 irq_spinlock_lock(&thread->lock, false);
[a35b458]510
[62b6d17]511 /* Process only counted threads */
[da1bafb]512 if (!thread->uncounted) {
513 if (thread == THREAD) {
[6f4495f5]514 /* Update accounting of current thread */
[a2a00e8]515 thread_update_accounting(false);
[da1bafb]516 }
[a35b458]517
[da1bafb]518 uret += thread->ucycles;
519 kret += thread->kcycles;
[62b6d17]520 }
[a35b458]521
[da1bafb]522 irq_spinlock_unlock(&thread->lock, false);
[0313ff0]523 }
[a35b458]524
[a2a00e8]525 *ucycles = uret;
526 *kcycles = kret;
[0313ff0]527}
528
[da1bafb]529static void task_kill_internal(task_t *task)
[121966e]530{
[df58e44]531 irq_spinlock_lock(&task->lock, false);
532 irq_spinlock_lock(&threads_lock, false);
[a35b458]533
[121966e]534 /*
535 * Interrupt all threads.
536 */
[a35b458]537
[feeac0d]538 list_foreach(task->threads, th_link, thread_t, thread) {
[121966e]539 bool sleeping = false;
[a35b458]540
[da1bafb]541 irq_spinlock_lock(&thread->lock, false);
[a35b458]542
[da1bafb]543 thread->interrupted = true;
544 if (thread->state == Sleeping)
[121966e]545 sleeping = true;
[a35b458]546
[da1bafb]547 irq_spinlock_unlock(&thread->lock, false);
[a35b458]548
[121966e]549 if (sleeping)
[da1bafb]550 waitq_interrupt_sleep(thread);
[121966e]551 }
[a35b458]552
[df58e44]553 irq_spinlock_unlock(&threads_lock, false);
[da1bafb]554 irq_spinlock_unlock(&task->lock, false);
[121966e]555}
556
[7509ddc]557/** Kill task.
[ea7890e7]558 *
559 * This function is idempotent.
560 * It signals all the task's threads to bail it out.
[7509ddc]561 *
[5ba201d]562 * @param id ID of the task to be killed.
563 *
564 * @return Zero on success or an error code from errno.h.
[7509ddc]565 *
566 */
[b7fd2a0]567errno_t task_kill(task_id_t id)
[7509ddc]568{
[9b6aae6]569 if (id == 1)
570 return EPERM;
[a35b458]571
[da1bafb]572 irq_spinlock_lock(&tasks_lock, true);
[a35b458]573
[da1bafb]574 task_t *task = task_find_by_id(id);
575 if (!task) {
576 irq_spinlock_unlock(&tasks_lock, true);
[7509ddc]577 return ENOENT;
578 }
[a35b458]579
[da1bafb]580 task_kill_internal(task);
581 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]582
[da1bafb]583 return EOK;
[7509ddc]584}
585
[5bcf1f9]586/** Kill the currently running task.
587 *
588 * @param notify Send out fault notifications.
589 *
590 * @return Zero on success or an error code from errno.h.
591 *
592 */
593void task_kill_self(bool notify)
594{
595 /*
596 * User space can subscribe for FAULT events to take action
597 * whenever a task faults (to take a dump, run a debugger, etc.).
598 * The notification is always available, but unless udebug is enabled,
599 * that's all you get.
[ae7d03c]600 */
[5bcf1f9]601 if (notify) {
[f9061b4]602 /* Notify the subscriber that a fault occurred. */
603 if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
604 UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
[5bcf1f9]605#ifdef CONFIG_UDEBUG
606 /* Wait for a debugging session. */
607 udebug_thread_fault();
608#endif
609 }
610 }
[a35b458]611
[5bcf1f9]612 irq_spinlock_lock(&tasks_lock, true);
613 task_kill_internal(TASK);
614 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]615
[5bcf1f9]616 thread_exit();
617}
618
619/** Process syscall to terminate the current task.
620 *
621 * @param notify Send out fault notifications.
622 *
623 */
[b7fd2a0]624sys_errno_t sys_task_exit(sysarg_t notify)
[5bcf1f9]625{
626 task_kill_self(notify);
[88e43bc]627 unreachable();
[5bcf1f9]628}
629
[ef1eab7]630static void task_print(task_t *task, bool additional)
[b76a2217]631{
[da1bafb]632 irq_spinlock_lock(&task->lock, false);
[a35b458]633
[a2a00e8]634 uint64_t ucycles;
635 uint64_t kcycles;
[1ba37fa]636 char usuffix, ksuffix;
[da1bafb]637 task_get_accounting(task, &ucycles, &kcycles);
[e535eeb]638 order_suffix(ucycles, &ucycles, &usuffix);
639 order_suffix(kcycles, &kcycles, &ksuffix);
[a35b458]640
[da1bafb]641#ifdef __32_BITS__
[ef1eab7]642 if (additional)
[077842c]643 printf("%-8" PRIu64 " %9zu", task->taskid,
[036e97c]644 atomic_load(&task->refcount));
[c0f13d2]645 else
646 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
647 " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
[473d5d2]648 task->name, task->container, task, task->as,
[c0f13d2]649 ucycles, usuffix, kcycles, ksuffix);
[52755f1]650#endif
[a35b458]651
[52755f1]652#ifdef __64_BITS__
[ef1eab7]653 if (additional)
[7e752b2]654 printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
[077842c]655 "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
[036e97c]656 ksuffix, atomic_load(&task->refcount));
[c0f13d2]657 else
658 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
[473d5d2]659 task->taskid, task->name, task->container, task, task->as);
[52755f1]660#endif
[a35b458]661
[da1bafb]662 irq_spinlock_unlock(&task->lock, false);
[b76a2217]663}
664
[c0f13d2]665/** Print task list
666 *
667 * @param additional Print additional information.
668 *
669 */
670void task_print_list(bool additional)
[37c57f2]671{
[f74bbaf]672 /* Messing with task structures, avoid deadlock */
[da1bafb]673 irq_spinlock_lock(&tasks_lock, true);
[a35b458]674
[5ba201d]675#ifdef __32_BITS__
[c0f13d2]676 if (additional)
[48dcc69]677 printf("[id ] [threads] [calls] [callee\n");
[c0f13d2]678 else
[473d5d2]679 printf("[id ] [name ] [ctn] [address ] [as ]"
[c0f13d2]680 " [ucycles ] [kcycles ]\n");
[52755f1]681#endif
[a35b458]682
[52755f1]683#ifdef __64_BITS__
[c0f13d2]684 if (additional)
[be06914]685 printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
[c0f13d2]686 " [callee\n");
687 else
[473d5d2]688 printf("[id ] [name ] [ctn] [address ]"
[c0f13d2]689 " [as ]\n");
[52755f1]690#endif
[a35b458]691
[ef1eab7]692 task_t *task;
693
[aab5e46]694 task = task_first();
695 while (task != NULL) {
[ef1eab7]696 task_print(task, additional);
[aab5e46]697 task = task_next(task);
[ef1eab7]698 }
[a35b458]699
[da1bafb]700 irq_spinlock_unlock(&tasks_lock, true);
[37c57f2]701}
[7509ddc]702
[ef1eab7]703/** Get key function for the @c tasks ordered dictionary.
704 *
705 * @param odlink Link
706 * @return Pointer to task ID cast as 'void *'
707 */
708static void *tasks_getkey(odlink_t *odlink)
709{
710 task_t *task = odict_get_instance(odlink, task_t, ltasks);
711 return (void *) &task->taskid;
712}
713
714/** Key comparison function for the @c tasks ordered dictionary.
715 *
716 * @param a Pointer to thread A ID
717 * @param b Pointer to thread B ID
718 * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
719 */
720static int tasks_cmp(void *a, void *b)
721{
722 task_id_t ida = *(task_id_t *)a;
723 task_id_t idb = *(task_id_t *)b;
724
725 if (ida < idb)
726 return -1;
727 else if (ida == idb)
728 return 0;
729 else
730 return +1;
731}
732
[cc73a8a1]733/** @}
[b45c443]734 */
Note: See TracBrowser for help on using the repository browser.