2 * Copyright (c) 2010 Jakub Jermar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup genericproc
35 * @brief Task management.
39 #include <proc/thread.h>
40 #include <proc/task.h>
44 #include <synch/futex.h>
45 #include <synch/spinlock.h>
46 #include <synch/waitq.h>
48 #include <arch/barrier.h>
50 #include <adt/btree.h>
54 #include <ipc/ipcrsc.h>
55 #include <ipc/event.h>
60 #include <syscall/copy.h>
63 /** Spinlock protecting the tasks_tree AVL tree. */
64 IRQ_SPINLOCK_INITIALIZE(tasks_lock
);
66 /** AVL tree of active tasks.
68 * The task is guaranteed to exist after it was found in the tasks_tree as
71 * @li the tasks_lock is held,
72 * @li the task's lock is held when task's lock is acquired before releasing
74 * @li the task's refcount is greater than 0
79 static task_id_t task_counter
= 0;
81 static slab_cache_t
*task_slab
;
83 /* Forward declarations. */
84 static void task_kill_internal(task_t
*);
85 static int tsk_constructor(void *, unsigned int);
86 static size_t tsk_destructor(void *obj
);
88 /** Initialize kernel tasks support.
94 avltree_create(&tasks_tree
);
95 task_slab
= slab_cache_create("task_t", sizeof(task_t
), 0,
96 tsk_constructor
, tsk_destructor
, 0);
99 /** Task finish walker.
101 * The idea behind this walker is to kill and count all tasks different from
105 static bool task_done_walker(avltree_node_t
*node
, void *arg
)
107 task_t
*task
= avltree_get_instance(node
, task_t
, tasks_tree_node
);
108 size_t *cnt
= (size_t *) arg
;
114 printf("[%"PRIu64
"] ", task
->taskid
);
117 task_kill_internal(task
);
120 /* Continue the walk */
124 /** Kill all tasks except the current task.
132 task_t
*task_0
= ipc_phone_0
->task
;
135 * The first task is held by kinit(), we need to release it or
136 * it will never finish cleanup.
138 task_release(task_0
);
141 /* Repeat until there are any tasks except TASK */
144 printf("Killing tasks... ");
147 irq_spinlock_lock(&tasks_lock
, true);
149 avltree_walk(&tasks_tree
, task_done_walker
, &tasks_left
);
150 irq_spinlock_unlock(&tasks_lock
, true);
157 } while (tasks_left
> 0);
160 int tsk_constructor(void *obj
, unsigned int kmflags
)
162 task_t
*task
= (task_t
*) obj
;
164 atomic_set(&task
->refcount
, 0);
165 atomic_set(&task
->lifecount
, 0);
167 irq_spinlock_initialize(&task
->lock
, "task_t_lock");
169 list_initialize(&task
->threads
);
171 caps_task_alloc(task
);
173 ipc_answerbox_init(&task
->answerbox
, task
);
175 spinlock_initialize(&task
->active_calls_lock
, "active_calls_lock");
176 list_initialize(&task
->active_calls
);
179 /* Init kbox stuff */
180 task
->kb
.thread
= NULL
;
181 ipc_answerbox_init(&task
->kb
.box
, task
);
182 mutex_initialize(&task
->kb
.cleanup_lock
, MUTEX_PASSIVE
);
188 size_t tsk_destructor(void *obj
)
190 task_t
*task
= (task_t
*) obj
;
192 caps_task_free(task
);
196 /** Create new task with no threads.
198 * @param as Task's address space.
199 * @param name Symbolic name (a copy is made).
201 * @return New task's structure.
204 task_t
*task_create(as_t
*as
, const char *name
)
206 task_t
*task
= (task_t
*) slab_alloc(task_slab
, 0);
207 task_create_arch(task
);
210 str_cpy(task
->name
, TASK_NAME_BUFLEN
, name
);
212 task
->container
= CONTAINER
;
217 caps_task_init(task
);
219 task
->ipc_info
.call_sent
= 0;
220 task
->ipc_info
.call_received
= 0;
221 task
->ipc_info
.answer_sent
= 0;
222 task
->ipc_info
.answer_received
= 0;
223 task
->ipc_info
.irq_notif_received
= 0;
224 task
->ipc_info
.forwarded
= 0;
226 event_task_init(task
);
228 task
->answerbox
.active
= true;
231 /* Init debugging stuff */
232 udebug_task_init(&task
->udebug
);
234 /* Init kbox stuff */
235 task
->kb
.box
.active
= true;
236 task
->kb
.finished
= false;
240 (container_check(ipc_phone_0
->task
->container
, task
->container
))) {
241 cap_handle_t phone_handle
= phone_alloc(task
);
242 kobject_t
*phone_obj
= kobject_get(task
, phone_handle
,
244 (void) ipc_phone_connect(phone_obj
->phone
, ipc_phone_0
);
247 futex_task_init(task
);
250 * Get a reference to the address space.
254 irq_spinlock_lock(&tasks_lock
, true);
256 task
->taskid
= ++task_counter
;
257 avltree_node_initialize(&task
->tasks_tree_node
);
258 task
->tasks_tree_node
.key
= task
->taskid
;
259 avltree_insert(&tasks_tree
, &task
->tasks_tree_node
);
261 irq_spinlock_unlock(&tasks_lock
, true);
268 * @param task Task to be destroyed.
271 void task_destroy(task_t
*task
)
274 * Remove the task from the task B+tree.
276 irq_spinlock_lock(&tasks_lock
, true);
277 avltree_delete(&tasks_tree
, &task
->tasks_tree_node
);
278 irq_spinlock_unlock(&tasks_lock
, true);
281 * Perform architecture specific task destruction.
283 task_destroy_arch(task
);
286 * Free up dynamically allocated state.
288 futex_task_deinit(task
);
291 * Drop our reference to the address space.
293 as_release(task
->as
);
295 slab_free(task_slab
, task
);
298 /** Hold a reference to a task.
300 * Holding a reference to a task prevents destruction of that task.
302 * @param task Task to be held.
305 void task_hold(task_t
*task
)
307 atomic_inc(&task
->refcount
);
310 /** Release a reference to a task.
312 * The last one to release a reference to a task destroys the task.
314 * @param task Task to be released.
317 void task_release(task_t
*task
)
319 if ((atomic_predec(&task
->refcount
)) == 0)
325 /** Syscall for reading task ID from userspace (32 bits)
327 * @param uspace_taskid Pointer to user-space buffer
328 * where to store current task ID.
330 * @return Zero on success or an error code from @ref errno.h.
333 sysarg_t
sys_task_get_id(sysarg64_t
*uspace_taskid
)
336 * No need to acquire lock on TASK because taskid remains constant for
337 * the lifespan of the task.
339 return (sysarg_t
) copy_to_uspace(uspace_taskid
, &TASK
->taskid
,
340 sizeof(TASK
->taskid
));
343 #endif /* __32_BITS__ */
347 /** Syscall for reading task ID from userspace (64 bits)
349 * @return Current task ID.
352 sysarg_t
sys_task_get_id(void)
355 * No need to acquire lock on TASK because taskid remains constant for
356 * the lifespan of the task.
361 #endif /* __64_BITS__ */
363 /** Syscall for setting the task name.
365 * The name simplifies identifying the task in the task list.
367 * @param name The new name for the task. (typically the same
368 * as the command used to execute it).
370 * @return 0 on success or an error code from @ref errno.h.
373 sysarg_t
sys_task_set_name(const char *uspace_name
, size_t name_len
)
375 char namebuf
[TASK_NAME_BUFLEN
];
377 /* Cap length of name and copy it from userspace. */
378 if (name_len
> TASK_NAME_BUFLEN
- 1)
379 name_len
= TASK_NAME_BUFLEN
- 1;
381 int rc
= copy_from_uspace(namebuf
, uspace_name
, name_len
);
383 return (sysarg_t
) rc
;
385 namebuf
[name_len
] = '\0';
388 * As the task name is referenced also from the
389 * threads, lock the threads' lock for the course
393 irq_spinlock_lock(&tasks_lock
, true);
394 irq_spinlock_lock(&TASK
->lock
, false);
395 irq_spinlock_lock(&threads_lock
, false);
398 str_cpy(TASK
->name
, TASK_NAME_BUFLEN
, namebuf
);
400 irq_spinlock_unlock(&threads_lock
, false);
401 irq_spinlock_unlock(&TASK
->lock
, false);
402 irq_spinlock_unlock(&tasks_lock
, true);
407 /** Syscall to forcefully terminate a task
409 * @param uspace_taskid Pointer to task ID in user space.
411 * @return 0 on success or an error code from @ref errno.h.
414 sysarg_t
sys_task_kill(task_id_t
*uspace_taskid
)
417 int rc
= copy_from_uspace(&taskid
, uspace_taskid
, sizeof(taskid
));
419 return (sysarg_t
) rc
;
421 return (sysarg_t
) task_kill(taskid
);
424 /** Find task structure corresponding to task ID.
426 * The tasks_lock must be already held by the caller of this function and
427 * interrupts must be disabled.
431 * @return Task structure address or NULL if there is no such task ID.
434 task_t
*task_find_by_id(task_id_t id
)
436 assert(interrupts_disabled());
437 assert(irq_spinlock_locked(&tasks_lock
));
439 avltree_node_t
*node
=
440 avltree_search(&tasks_tree
, (avltree_key_t
) id
);
443 return avltree_get_instance(node
, task_t
, tasks_tree_node
);
448 /** Get accounting data of given task.
450 * Note that task lock of 'task' must be already held and interrupts must be
453 * @param task Pointer to the task.
454 * @param ucycles Out pointer to sum of all user cycles.
455 * @param kcycles Out pointer to sum of all kernel cycles.
458 void task_get_accounting(task_t
*task
, uint64_t *ucycles
, uint64_t *kcycles
)
460 assert(interrupts_disabled());
461 assert(irq_spinlock_locked(&task
->lock
));
463 /* Accumulated values of task */
464 uint64_t uret
= task
->ucycles
;
465 uint64_t kret
= task
->kcycles
;
467 /* Current values of threads */
468 list_foreach(task
->threads
, th_link
, thread_t
, thread
) {
469 irq_spinlock_lock(&thread
->lock
, false);
471 /* Process only counted threads */
472 if (!thread
->uncounted
) {
473 if (thread
== THREAD
) {
474 /* Update accounting of current thread */
475 thread_update_accounting(false);
478 uret
+= thread
->ucycles
;
479 kret
+= thread
->kcycles
;
482 irq_spinlock_unlock(&thread
->lock
, false);
489 static void task_kill_internal(task_t
*task
)
491 irq_spinlock_lock(&task
->lock
, false);
492 irq_spinlock_lock(&threads_lock
, false);
495 * Interrupt all threads.
498 list_foreach(task
->threads
, th_link
, thread_t
, thread
) {
499 bool sleeping
= false;
501 irq_spinlock_lock(&thread
->lock
, false);
503 thread
->interrupted
= true;
504 if (thread
->state
== Sleeping
)
507 irq_spinlock_unlock(&thread
->lock
, false);
510 waitq_interrupt_sleep(thread
);
513 irq_spinlock_unlock(&threads_lock
, false);
514 irq_spinlock_unlock(&task
->lock
, false);
519 * This function is idempotent.
520 * It signals all the task's threads to bail it out.
522 * @param id ID of the task to be killed.
524 * @return Zero on success or an error code from errno.h.
527 int task_kill(task_id_t id
)
532 irq_spinlock_lock(&tasks_lock
, true);
534 task_t
*task
= task_find_by_id(id
);
536 irq_spinlock_unlock(&tasks_lock
, true);
540 task_kill_internal(task
);
541 irq_spinlock_unlock(&tasks_lock
, true);
546 /** Kill the currently running task.
548 * @param notify Send out fault notifications.
550 * @return Zero on success or an error code from errno.h.
553 void task_kill_self(bool notify
)
556 * User space can subscribe for FAULT events to take action
557 * whenever a task faults (to take a dump, run a debugger, etc.).
558 * The notification is always available, but unless udebug is enabled,
559 * that's all you get.
562 /* Notify the subscriber that a fault occurred. */
563 if (event_notify_3(EVENT_FAULT
, false, LOWER32(TASK
->taskid
),
564 UPPER32(TASK
->taskid
), (sysarg_t
) THREAD
) == EOK
) {
566 /* Wait for a debugging session. */
567 udebug_thread_fault();
572 irq_spinlock_lock(&tasks_lock
, true);
573 task_kill_internal(TASK
);
574 irq_spinlock_unlock(&tasks_lock
, true);
579 /** Process syscall to terminate the current task.
581 * @param notify Send out fault notifications.
584 sysarg_t
sys_task_exit(sysarg_t notify
)
586 task_kill_self(notify
);
592 static bool task_print_walker(avltree_node_t
*node
, void *arg
)
594 bool *additional
= (bool *) arg
;
595 task_t
*task
= avltree_get_instance(node
, task_t
, tasks_tree_node
);
596 irq_spinlock_lock(&task
->lock
, false);
600 char usuffix
, ksuffix
;
601 task_get_accounting(task
, &ucycles
, &kcycles
);
602 order_suffix(ucycles
, &ucycles
, &usuffix
);
603 order_suffix(kcycles
, &kcycles
, &ksuffix
);
607 printf("%-8" PRIu64
" %9" PRIua
, task
->taskid
,
608 atomic_get(&task
->refcount
));
610 printf("%-8" PRIu64
" %-14s %-5" PRIu32
" %10p %10p"
611 " %9" PRIu64
"%c %9" PRIu64
"%c\n", task
->taskid
,
612 task
->name
, task
->container
, task
, task
->as
,
613 ucycles
, usuffix
, kcycles
, ksuffix
);
618 printf("%-8" PRIu64
" %9" PRIu64
"%c %9" PRIu64
"%c "
619 "%9" PRIua
"\n", task
->taskid
, ucycles
, usuffix
, kcycles
,
620 ksuffix
, atomic_get(&task
->refcount
));
622 printf("%-8" PRIu64
" %-14s %-5" PRIu32
" %18p %18p\n",
623 task
->taskid
, task
->name
, task
->container
, task
, task
->as
);
626 irq_spinlock_unlock(&task
->lock
, false);
632 * @param additional Print additional information.
635 void task_print_list(bool additional
)
637 /* Messing with task structures, avoid deadlock */
638 irq_spinlock_lock(&tasks_lock
, true);
642 printf("[id ] [threads] [calls] [callee\n");
644 printf("[id ] [name ] [ctn] [address ] [as ]"
645 " [ucycles ] [kcycles ]\n");
650 printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
653 printf("[id ] [name ] [ctn] [address ]"
657 avltree_walk(&tasks_tree
, task_print_walker
, &additional
);
659 irq_spinlock_unlock(&tasks_lock
, true);