Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4054 → Rev 4055

/branches/dd/kernel/generic/src/proc/task.c
35,10 → 35,8
* @brief Task management.
*/
 
#include <main/uinit.h>
#include <proc/thread.h>
#include <proc/task.h>
#include <proc/uarg.h>
#include <mm/as.h>
#include <mm/slab.h>
#include <atomic.h>
45,23 → 43,18
#include <synch/spinlock.h>
#include <synch/waitq.h>
#include <arch.h>
#include <panic.h>
#include <arch/barrier.h>
#include <adt/avl.h>
#include <adt/btree.h>
#include <adt/list.h>
#include <ipc/ipc.h>
#include <security/cap.h>
#include <memstr.h>
#include <ipc/ipcrsc.h>
#include <print.h>
#include <lib/elf.h>
#include <errno.h>
#include <func.h>
#include <string.h>
#include <syscall/copy.h>
 
#ifndef LOADED_PROG_STACK_PAGES_NO
#define LOADED_PROG_STACK_PAGES_NO 1
#endif
 
/** Spinlock protecting the tasks_tree AVL tree. */
SPINLOCK_INITIALIZE(tasks_lock);
 
79,11 → 72,7
 
static task_id_t task_counter = 0;
 
/** Initialize tasks
*
* Initialize kernel tasks support.
*
*/
/** Initialize kernel tasks support. */
void task_init(void)
{
TASK = NULL;
91,7 → 80,8
}
 
/*
* The idea behind this walker is to remember a single task different from TASK.
* The idea behind this walker is to remember a single task different from
* TASK.
*/
static bool task_done_walker(avltree_node_t *node, void *arg)
{
106,9 → 96,7
return true; /* continue the walk */
}
 
/** Kill all tasks except the current task.
*
*/
/** Kill all tasks except the current task. */
void task_done(void)
{
task_t *t;
128,7 → 116,7
interrupts_restore(ipl);
#ifdef CONFIG_DEBUG
printf("Killing task %llu\n", id);
printf("Killing task %" PRIu64 "\n", id);
#endif
task_kill(id);
thread_usleep(10000);
140,15 → 128,13
} while (t != NULL);
}
 
/** Create new task
/** Create new task with no threads.
*
* Create new task with no threads.
* @param as Task's address space.
* @param name Symbolic name (a copy is made).
*
* @param as Task's address space.
* @param name Symbolic name.
* @return New task's structure.
*
* @return New task's structure
*
*/
task_t *task_create(as_t *as, char *name)
{
163,7 → 149,10
spinlock_initialize(&ta->lock, "task_ta_lock");
list_initialize(&ta->th_head);
ta->as = as;
ta->name = name;
 
memcpy(ta->name, name, TASK_NAME_BUFLEN);
ta->name[TASK_NAME_BUFLEN - 1] = '\0';
 
atomic_set(&ta->refcount, 0);
atomic_set(&ta->lifecount, 0);
ta->context = CONTEXT;
170,7 → 159,18
 
ta->capabilities = 0;
ta->cycles = 0;
 
#ifdef CONFIG_UDEBUG
/* Init debugging stuff */
udebug_task_init(&ta->udebug);
 
/* Init kbox stuff */
ipc_answerbox_init(&ta->kb.box, ta);
ta->kb.thread = NULL;
mutex_initialize(&ta->kb.cleanup_lock, MUTEX_PASSIVE);
ta->kb.finished = false;
#endif
 
ipc_answerbox_init(&ta->answerbox, ta);
for (i = 0; i < IPC_MAX_PHONES; i++)
ipc_phone_init(&ta->phones[i]);
179,7 → 179,7
ipc_phone_connect(&ta->phones[0], ipc_phone_0);
atomic_set(&ta->active_calls, 0);
 
mutex_initialize(&ta->futexes_lock);
mutex_initialize(&ta->futexes_lock, MUTEX_PASSIVE);
btree_create(&ta->futexes);
ipl = interrupts_disable();
202,7 → 202,7
 
/** Destroy task.
*
* @param t Task to be destroyed.
* @param t Task to be destroyed.
*/
void task_destroy(task_t *t)
{
233,90 → 233,63
TASK = NULL;
}
 
/** Create new task with 1 thread and run it
/** Syscall for reading task ID from userspace.
*
* @param program_addr Address of program executable image.
* @param name Program name.
* @param uspace_task_id userspace address of 8-byte buffer
* where to store current task ID.
*
* @return Task of the running program or NULL on error.
* @return Zero on success or an error code from @ref errno.h.
*/
task_t *task_run_program(void *program_addr, char *name)
unative_t sys_task_get_id(task_id_t *uspace_task_id)
{
as_t *as;
as_area_t *a;
unsigned int rc;
thread_t *t;
task_t *task;
uspace_arg_t *kernel_uarg;
 
as = as_create(0);
ASSERT(as);
 
rc = elf_load((elf_header_t *) program_addr, as);
if (rc != EE_OK) {
as_destroy(as);
return NULL;
}
kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
kernel_uarg->uspace_entry =
(void *) ((elf_header_t *) program_addr)->e_entry;
kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
kernel_uarg->uspace_thread_function = NULL;
kernel_uarg->uspace_thread_arg = NULL;
kernel_uarg->uspace_uarg = NULL;
task = task_create(as, name);
ASSERT(task);
 
/*
* Create the data as_area.
* No need to acquire lock on TASK because taskid remains constant for
* the lifespan of the task.
*/
a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
AS_AREA_ATTR_NONE, &anon_backend, NULL);
 
/*
* Create the main thread.
*/
t = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE,
"uinit", false);
ASSERT(t);
thread_ready(t);
 
return task;
return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
sizeof(TASK->taskid));
}
 
/** Syscall for reading task ID from userspace.
/** Syscall for setting the task name.
*
* @param uspace_task_id Userspace address of 8-byte buffer where to store
* current task ID.
* The name simplifies identifying the task in the task list.
*
* @param name The new name for the task. (typically the same
* as the command used to execute it).
*
* @return 0 on success or an error code from @ref errno.h.
*/
unative_t sys_task_get_id(task_id_t *uspace_task_id)
unative_t sys_task_set_name(const char *uspace_name, size_t name_len)
{
/*
* No need to acquire lock on TASK because taskid
* remains constant for the lifespan of the task.
*/
return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
sizeof(TASK->taskid));
int rc;
char namebuf[TASK_NAME_BUFLEN];
 
/* Cap length of name and copy it from userspace. */
 
if (name_len > TASK_NAME_BUFLEN - 1)
name_len = TASK_NAME_BUFLEN - 1;
 
rc = copy_from_uspace(namebuf, uspace_name, name_len);
if (rc != 0)
return (unative_t) rc;
 
namebuf[name_len] = '\0';
strncpy(TASK->name, namebuf, TASK_NAME_BUFLEN);
 
return EOK;
}
 
/** Find task structure corresponding to task ID.
*
* The tasks_lock must be already held by the caller of this function
* and interrupts must be disabled.
* The tasks_lock must be already held by the caller of this function and
* interrupts must be disabled.
*
* @param id Task ID.
* @param id Task ID.
*
* @return Task structure address or NULL if there is no such task ID.
* @return Task structure address or NULL if there is no such task
* ID.
*/
task_t *task_find_by_id(task_id_t id)
{
avltree_node_t *node;
task_t *task_find_by_id(task_id_t id) { avltree_node_t *node;
node = avltree_search(&tasks_tree, (avltree_key_t) id);
 
327,11 → 300,13
 
/** Get accounting data of given task.
*
* Note that task lock of 't' must be already held and
* interrupts must be already disabled.
* Note that task lock of 't' must be already held and interrupts must be
* already disabled.
*
* @param t Pointer to thread.
* @param t Pointer to thread.
*
* @return Number of cycles used by the task and all its threads
* so far.
*/
uint64_t task_get_accounting(task_t *t)
{
363,9 → 338,9
* This function is idempotent.
* It signals all the task's threads to bail it out.
*
* @param id ID of the task to be killed.
* @param id ID of the task to be killed.
*
* @return 0 on success or an error code from errno.h
* @return Zero on success or an error code from errno.h.
*/
int task_kill(task_id_t id)
{
386,7 → 361,7
spinlock_unlock(&tasks_lock);
/*
* Interrupt all threads except ktaskclnp.
* Interrupt all threads.
*/
spinlock_lock(&ta->lock);
for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
394,7 → 369,7
bool sleeping = false;
thr = list_get_instance(cur, thread_t, th_link);
spinlock_lock(&thr->lock);
thr->interrupted = true;
if (thr->state == Sleeping)
420,18 → 395,22
uint64_t cycles;
char suffix;
order(task_get_accounting(t), &cycles, &suffix);
if (sizeof(void *) == 4)
printf("%-6llu %-10s %-3ld %#10zx %#10zx %9llu%c %7zd %6zd",
t->taskid, t->name, t->context, t, t->as, cycles, suffix,
t->refcount, atomic_get(&t->active_calls));
else
printf("%-6llu %-10s %-3ld %#18zx %#18zx %9llu%c %7zd %6zd",
t->taskid, t->name, t->context, t, t->as, cycles, suffix,
t->refcount, atomic_get(&t->active_calls));
 
#ifdef __32_BITS__
printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %10p %10p %9" PRIu64
"%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
#endif
 
#ifdef __64_BITS__
printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %18p %18p %9" PRIu64
"%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
#endif
 
for (j = 0; j < IPC_MAX_PHONES; j++) {
if (t->phones[j].callee)
printf(" %zd:%#zx", j, t->phones[j].callee);
printf(" %d:%p", j, t->phones[j].callee);
}
printf("\n");
447,19 → 426,21
/* Messing with task structures, avoid deadlock */
ipl = interrupts_disable();
spinlock_lock(&tasks_lock);
if (sizeof(void *) == 4) {
printf("taskid name ctx address as "
"cycles threads calls callee\n");
printf("------ ---------- --- ---------- ---------- "
"---------- ------- ------ ------>\n");
} else {
printf("taskid name ctx address as "
"cycles threads calls callee\n");
printf("------ ---------- --- ------------------ ------------------ "
"---------- ------- ------ ------>\n");
}
 
#ifdef __32_BITS__
printf("taskid name ctx address as "
"cycles threads calls callee\n");
printf("------ ------------ --- ---------- ---------- "
"---------- ------- ------ ------>\n");
#endif
 
#ifdef __64_BITS__
printf("taskid name ctx address as "
"cycles threads calls callee\n");
printf("------ ------------ --- ------------------ ------------------ "
"---------- ------- ------ ------>\n");
#endif
 
avltree_walk(&tasks_tree, task_print_walker, NULL);
 
spinlock_unlock(&tasks_lock);