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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6f7071b was 6f7071b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Use ordered dictionary for kernel pareas instead of B+ tree.

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