HelenOS sources
This source file includes following definitions.
- task_init
- task_done
- tsk_constructor
- tsk_destructor
- task_create
- task_destroy
- task_hold
- task_release
- sys_task_get_id
- sys_task_get_id
- sys_task_set_name
- sys_task_kill
- task_find_by_id
- task_count
- task_first
- task_next
- task_get_accounting
- task_kill_internal
- task_kill
- task_kill_self
- sys_task_exit
- task_print
- task_print_list
- tasks_getkey
- tasks_cmp
#include <assert.h>
#include <proc/thread.h>
#include <proc/task.h>
#include <mm/as.h>
#include <mm/slab.h>
#include <atomic.h>
#include <synch/spinlock.h>
#include <synch/waitq.h>
#include <arch.h>
#include <barrier.h>
#include <adt/list.h>
#include <adt/odict.h>
#include <cap/cap.h>
#include <ipc/ipc.h>
#include <ipc/ipcrsc.h>
#include <ipc/event.h>
#include <stdio.h>
#include <errno.h>
#include <halt.h>
#include <str.h>
#include <syscall/copy.h>
#include <macros.h>
IRQ_SPINLOCK_INITIALIZE(tasks_lock);
odict_t tasks;
static task_id_t task_counter = 0;
static slab_cache_t *task_cache;
static void task_kill_internal(task_t *);
static errno_t tsk_constructor(void *, unsigned int);
static size_t tsk_destructor(void *);
static void *tasks_getkey(odlink_t *);
static int tasks_cmp(void *, void *);
void task_init(void)
{
TASK = NULL;
odict_initialize(&tasks, tasks_getkey, tasks_cmp);
task_cache = slab_cache_create("task_t", sizeof(task_t), 0,
tsk_constructor, tsk_destructor, 0);
}
void task_done(void)
{
size_t tasks_left;
task_t *task;
if (ipc_box_0) {
task_t *task_0 = ipc_box_0->task;
ipc_box_0 = NULL;
task_release(task_0);
}
do {
#ifdef CONFIG_DEBUG
printf("Killing tasks... ");
#endif
irq_spinlock_lock(&tasks_lock, true);
tasks_left = 0;
task = task_first();
while (task != NULL) {
if (task != TASK) {
tasks_left++;
#ifdef CONFIG_DEBUG
printf("[%" PRIu64 "] ", task->taskid);
#endif
task_kill_internal(task);
}
task = task_next(task);
}
irq_spinlock_unlock(&tasks_lock, true);
thread_sleep(1);
#ifdef CONFIG_DEBUG
printf("\n");
#endif
} while (tasks_left > 0);
}
errno_t tsk_constructor(void *obj, unsigned int kmflags)
{
task_t *task = (task_t *) obj;
errno_t rc = caps_task_alloc(task);
if (rc != EOK)
return rc;
atomic_store(&task->lifecount, 0);
irq_spinlock_initialize(&task->lock, "task_t_lock");
list_initialize(&task->threads);
ipc_answerbox_init(&task->answerbox, task);
spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
list_initialize(&task->active_calls);
#ifdef CONFIG_UDEBUG
task->kb.thread = NULL;
ipc_answerbox_init(&task->kb.box, task);
mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
#endif
return EOK;
}
size_t tsk_destructor(void *obj)
{
task_t *task = (task_t *) obj;
caps_task_free(task);
return 0;
}
task_t *task_create(as_t *as, const char *name)
{
task_t *task = (task_t *) slab_alloc(task_cache, FRAME_ATOMIC);
if (!task)
return NULL;
refcount_init(&task->refcount);
task_create_arch(task);
task->as = as;
str_cpy(task->name, TASK_NAME_BUFLEN, name);
task->container = CONTAINER;
task->perms = 0;
task->ucycles = 0;
task->kcycles = 0;
caps_task_init(task);
task->ipc_info.call_sent = 0;
task->ipc_info.call_received = 0;
task->ipc_info.answer_sent = 0;
task->ipc_info.answer_received = 0;
task->ipc_info.irq_notif_received = 0;
task->ipc_info.forwarded = 0;
event_task_init(task);
task->answerbox.active = true;
task->debug_sections = NULL;
#ifdef CONFIG_UDEBUG
udebug_task_init(&task->udebug);
task->kb.box.active = true;
task->kb.finished = false;
#endif
if ((ipc_box_0) &&
(container_check(ipc_box_0->task->container, task->container))) {
cap_phone_handle_t phone_handle;
errno_t rc = phone_alloc(task, true, &phone_handle, NULL);
if (rc != EOK) {
task->as = NULL;
task_destroy_arch(task);
slab_free(task_cache, task);
return NULL;
}
kobject_t *phone_obj = kobject_get(task, phone_handle,
KOBJECT_TYPE_PHONE);
(void) ipc_phone_connect(phone_obj->phone, ipc_box_0);
}
irq_spinlock_lock(&tasks_lock, true);
task->taskid = ++task_counter;
odlink_initialize(&task->ltasks);
odict_insert(&task->ltasks, &tasks, NULL);
irq_spinlock_unlock(&tasks_lock, true);
return task;
}
static void task_destroy(task_t *task)
{
irq_spinlock_lock(&tasks_lock, true);
odict_remove(&task->ltasks);
irq_spinlock_unlock(&tasks_lock, true);
task_destroy_arch(task);
as_release(task->as);
slab_free(task_cache, task);
}
void task_hold(task_t *task)
{
refcount_up(&task->refcount);
}
void task_release(task_t *task)
{
if (refcount_down(&task->refcount))
task_destroy(task);
}
#ifdef __32_BITS__
sys_errno_t sys_task_get_id(uspace_ptr_sysarg64_t uspace_taskid)
{
return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
sizeof(TASK->taskid));
}
#endif
#ifdef __64_BITS__
sysarg_t sys_task_get_id(void)
{
return TASK->taskid;
}
#endif
sys_errno_t sys_task_set_name(const uspace_ptr_char uspace_name, size_t name_len)
{
char namebuf[TASK_NAME_BUFLEN];
if (name_len > TASK_NAME_BUFLEN - 1)
name_len = TASK_NAME_BUFLEN - 1;
errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
if (rc != EOK)
return (sys_errno_t) rc;
namebuf[name_len] = '\0';
irq_spinlock_lock(&tasks_lock, true);
irq_spinlock_lock(&TASK->lock, false);
str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
irq_spinlock_unlock(&TASK->lock, false);
irq_spinlock_unlock(&tasks_lock, true);
return EOK;
}
sys_errno_t sys_task_kill(uspace_ptr_task_id_t uspace_taskid)
{
task_id_t taskid;
errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
if (rc != EOK)
return (sys_errno_t) rc;
return (sys_errno_t) task_kill(taskid);
}
task_t *task_find_by_id(task_id_t id)
{
task_t *task = NULL;
irq_spinlock_lock(&tasks_lock, true);
odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
if (odlink != NULL) {
task = odict_get_instance(odlink, task_t, ltasks);
if (!refcount_try_up(&task->refcount))
task = NULL;
}
irq_spinlock_unlock(&tasks_lock, true);
return task;
}
size_t task_count(void)
{
assert(interrupts_disabled());
assert(irq_spinlock_locked(&tasks_lock));
return odict_count(&tasks);
}
task_t *task_first(void)
{
odlink_t *odlink;
assert(interrupts_disabled());
assert(irq_spinlock_locked(&tasks_lock));
odlink = odict_first(&tasks);
if (odlink == NULL)
return NULL;
return odict_get_instance(odlink, task_t, ltasks);
}
task_t *task_next(task_t *cur)
{
odlink_t *odlink;
assert(interrupts_disabled());
assert(irq_spinlock_locked(&tasks_lock));
odlink = odict_next(&cur->ltasks, &tasks);
if (odlink == NULL)
return NULL;
return odict_get_instance(odlink, task_t, ltasks);
}
void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
{
assert(interrupts_disabled());
assert(irq_spinlock_locked(&task->lock));
uint64_t uret = task->ucycles;
uint64_t kret = task->kcycles;
list_foreach(task->threads, th_link, thread_t, thread) {
if (!thread->uncounted) {
if (thread == THREAD) {
thread_update_accounting(false);
}
uret += atomic_time_read(&thread->ucycles);
kret += atomic_time_read(&thread->kcycles);
}
}
*ucycles = uret;
*kcycles = kret;
}
static void task_kill_internal(task_t *task)
{
irq_spinlock_lock(&task->lock, true);
list_foreach(task->threads, th_link, thread_t, thread) {
thread_interrupt(thread);
}
irq_spinlock_unlock(&task->lock, true);
}
errno_t task_kill(task_id_t id)
{
if (id == 1)
return EPERM;
task_t *task = task_find_by_id(id);
if (!task)
return ENOENT;
task_kill_internal(task);
task_release(task);
return EOK;
}
void task_kill_self(bool notify)
{
if (notify) {
if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
#ifdef CONFIG_UDEBUG
udebug_thread_fault();
#endif
}
}
task_kill_internal(TASK);
thread_exit();
}
sys_errno_t sys_task_exit(sysarg_t notify)
{
task_kill_self(notify);
unreachable();
}
static void task_print(task_t *task, bool additional)
{
irq_spinlock_lock(&task->lock, false);
uint64_t ucycles;
uint64_t kcycles;
char usuffix, ksuffix;
task_get_accounting(task, &ucycles, &kcycles);
order_suffix(ucycles, &ucycles, &usuffix);
order_suffix(kcycles, &kcycles, &ksuffix);
#ifdef __32_BITS__
if (additional)
printf("%-8" PRIu64 " %9zu", task->taskid,
atomic_load(&task->lifecount));
else
printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
" %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
task->name, task->container, task, task->as,
ucycles, usuffix, kcycles, ksuffix);
#endif
#ifdef __64_BITS__
if (additional)
printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
"%9zu\n", task->taskid, ucycles, usuffix, kcycles,
ksuffix, atomic_load(&task->lifecount));
else
printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
task->taskid, task->name, task->container, task, task->as);
#endif
irq_spinlock_unlock(&task->lock, false);
}
void task_print_list(bool additional)
{
irq_spinlock_lock(&tasks_lock, true);
#ifdef __32_BITS__
if (additional)
printf("[id ] [threads] [calls] [callee\n");
else
printf("[id ] [name ] [ctn] [address ] [as ]"
" [ucycles ] [kcycles ]\n");
#endif
#ifdef __64_BITS__
if (additional)
printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
" [callee\n");
else
printf("[id ] [name ] [ctn] [address ]"
" [as ]\n");
#endif
task_t *task;
task = task_first();
while (task != NULL) {
task_print(task, additional);
task = task_next(task);
}
irq_spinlock_unlock(&tasks_lock, true);
}
static void *tasks_getkey(odlink_t *odlink)
{
task_t *task = odict_get_instance(odlink, task_t, ltasks);
return (void *) &task->taskid;
}
static int tasks_cmp(void *a, void *b)
{
task_id_t ida = *(task_id_t *)a;
task_id_t idb = *(task_id_t *)b;
if (ida < idb)
return -1;
else if (ida == idb)
return 0;
else
return +1;
}
HelenOS homepage, sources at GitHub