Towards finishing exchange_registers()

- Added mutex_trylock()
- Implemented most of exchange_registers()
- thread_control() now needs a lock for operations that can modify thread context.
- thread_start() does not initialise scheduler flags, now done in thread_create.

TODO:
- Fork/clone'ed threads should retain their context in tcb, not syscall stack.
- exchange_registers() calls in userspace need cleaning up.
This commit is contained in:
Bahadir Balban
2008-09-13 18:07:00 +03:00
parent 0b3ab05a98
commit 4fb5277123
23 changed files with 460 additions and 98 deletions

View File

@@ -14,6 +14,7 @@
#include <l4/api/ipc.h>
#include <l4/api/kip.h>
#include <l4/api/errno.h>
#include <l4/api/exregs.h>
#include INC_API(syscall.h)
#include INC_ARCH(exception.h)
@@ -27,28 +28,236 @@ void print_syscall_context(struct ktcb *t)
r->r5, r->r6, r->r7, r->r8, r->sp_usr, r->lr_usr);
}
int sys_exchange_registers(syscall_context_t *regs)
/*
* Bigger, slower but typed, i.e. if task_context_t or syscall_context_t
* fields are reordered in the future, this would not break.
*/
void do_exchange_registers_bigslow(struct tcb *task, struct exregs_data *exregs)
{
struct ktcb *task;
unsigned int pc = regs->r0;
unsigned int sp = regs->r1;
unsigned int pagerid = regs->r2;
l4id_t tid = regs->r3;
unsigned int create_flags = task->flags;
task_context_t *context = &task->context;
syscall_context_t *sysregs = task->syscall_regs;
/* Find tcb from its hash list */
if ((task = find_task(tid)))
goto found;
/* FIXME: Whatif not found??? Recover gracefully. */
BUG();
/*
* NOTE:
* We don't care if register values point at invalid addresses
* since memory protection would prevent any kernel corruption.
* We do however, make sure spsr is not modified, and pc is
* modified only for the userspace.
*/
/*
* If the thread is returning from a syscall,
* we modify the register state pushed to syscall stack.
*/
if ((create_flags == THREAD_COPY_SPACE) ||
(create_flags == THREAD_SAME_SPACE)) {
/* Check register valid bit and copy registers */
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r0))
syscall_regs->r0 = exregs->context.r0;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r1))
syscall_regs->r1 = exregs->context.r1;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r2))
syscall_regs->r2 = exregs->context.r2;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r3))
syscall_regs->r3 = exregs->context.r3;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r4))
syscall_regs->r4 = exregs->context.r4;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r5))
syscall_regs->r5 = exregs->context.r5;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r6))
syscall_regs->r6 = exregs->context.r6;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r7))
syscall_regs->r7 = exregs->context.r7;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r8))
syscall_regs->r8 = exregs->context.r8;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r9))
syscall_regs->r9 = exregs->context.r9;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r10))
syscall_regs->r10 = exregs->context.r10;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r11))
syscall_regs->r11 = exregs->context.r11;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, r12))
syscall_regs->r12 = exregs->context.r12;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, sp_usr))
syscall_regs->sp_usr = exregs->context.sp;
if (exregs.valid_vect & FIELD_TO_BIT(syscall_regs_t, sp_lr))
syscall_regs->sp_lr = exregs->context.lr;
/* Cannot modify program counter of a thread in kernel */
/* If it's a new thread or it's in userspace, modify actual context */
} else if ((create_flags == THREAD_NEW_SPACE) ||
(!create_flags && task_in_user(task))) {
/* Copy registers */
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r0))
context->r0 = exregs->context.r0;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r1))
context->r1 = exregs->context.r1;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r2))
context->r2 = exregs->context.r2;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r3))
context->r3 = exregs->context.r3;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r4))
context->r4 = exregs->context.r4;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r5))
context->r5 = exregs->context.r5;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r6))
context->r6 = exregs->context.r6;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r7))
context->r7 = exregs->context.r7;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r8))
context->r8 = exregs->context.r8;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r9))
context->r9 = exregs->context.r9;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r10))
context->r10 = exregs->context.r10;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r11))
context->r11 = exregs->context.r11;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, r12))
context->r12 = exregs->context.r12;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, sp))
context->sp = exregs->context.sp;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, lr))
context->lr = exregs->context.lr;
if (exregs.valid_vect & FIELD_TO_BIT(task_context_t, pc))
context->pc = exregs->context.pc;
/* Set spsr as user mode if thread is new */
if (create_flags == THREAD_NEW_SPACE)
task->context.spsr = ARM_MODE_USR;
} else
BUG();
}
/*
* This is smaller and faster but would break if task_context_t or
* syscall_regs_t types change, i.e. if their fields are reordered.
*/
void do_exchange_registers(struct tcb *task, struct exregs_data *exregs)
{
unsigned int create_flags = task->flags;
u32 *context_ptr, *exregs_ptr = (u32 *)&exregs.context;
/*
* NOTE:
* We don't care if register values point at invalid addresses
* since memory protection would prevent any kernel corruption.
*/
/*
* If the thread is returning from a syscall,
* we modify the register state pushed to syscall stack.
*/
if ((create_flags == THREAD_COPY_SPACE) ||
(create_flags == THREAD_SAME_SPACE)) {
context_ptr = (u32 *)&task->syscall_regs->r0;
} else if (create_flags == THREAD_NEW_SPACE) {
context_ptr = (u32 *)&task->context.r0;
task->context.spsr = ARM_MODE_USR;
} else
BUG();
/* Traverse the validity bit vector and copy exregs to task context */
for (int i = 0; i < (sizeof(exregs->context) / sizeof(u32)); i++) {
if (exregs.valid_vect & (1 << i)) {
/* NOTE: If structures change, this may break. */
context_ptr[i] = exregs_ptr[i];
}
}
if (create_flags == THREAD_NEW_SPACE)
found:
/* Set its registers */
task->context.pc = pc;
task->context.sp = sp;
task->context.spsr = ARM_MODE_USR;
/* Set its pager */
task->pagerid = pagerid;
}
/*
* exchange_registers()
*
* This call is used by the pagers to set (and in the future read)
* the register context of a thread. The thread's registers can be
* set in 2 thread states:
*
* 1) The thread is executing in userspace:
* i. A newly created thread with a new address space.
* ii. An existing thread that is in userspace.
*
* 2) The thread is executing in the kernel, but suspended when it
* is about to execute "return_from_syscall":
* i. A thread that is just created in an existing address space.
* ii. A thread that is just created copying an existing address space.
*
* These conditions are detected and accordingly the task context is
* modified. A thread executing in the kernel cannot be modified
* since this would compromise the kernel. Also the thread must be
* in suspended condition so that it does not start to execute as we
* modify its context.
*
* TODO: This is an arch-specific call, can move it to ARM
*
*/
int sys_exchange_registers(syscall_context_t *regs)
{
struct ktcb *task;
struct exregs_data *exregs = regs->r0;
unsigned int pagerid = regs->r1;
l4id_t tid = regs->r2;
unsigned int create_flags = task->flags & TASK_CREATE_FLAGS;
int err;
/* Find tcb from its list */
if (!(task = find_task(tid)))
return -ESRCH;
/*
* This lock ensures task is not
* inadvertently resumed by a syscall
*/
if (!mutex_trylock(&task->thread_control_lock))
return -EAGAIN;
/* Now check that the task is suspended */
if (task->state != TASK_INACTIVE) {
mutex_unlock(&task->thread_control_lock);
return -EACTIVE;
}
/*
* Check that it is legitimate to modify
* the task registers state
*/
if (!create_flags) {
/*
* Task is not new. We only allow such tasks
* to be modified in userspace.
*/
if (!task_in_user(task))
return -EPERM;
} else { /* TODO: Simplify it here. */
/* New threads with new address space */
if (create_flags == THREAD_NEW_SPACE)
do_exchange_registers_bigslow(task, exregs);
else if ((create_flags == THREAD_COPY_SPACE) ||
(create_flags == THREAD_SAME_SPACE)) {
/*
* Further check that the task is in
* the kernel but about to exit.
*/
if (task->context.pc != &return_from_syscall ||
!task_in_kernel(task)) {
/* Actually its a bug if not true */
BUG();
return -EPERM;
}
do_exchange_registers_bigslow(task, exregs);
}
}
/* Set its pager if one is supplied */
if (pagerid != THREAD_ID_INVALID)
task->pagerid = pagerid;
return 0;
}

View File

@@ -4,12 +4,12 @@
* Copyright (C) 2007 Bahadir Balban
*/
#include <l4/generic/scheduler.h>
#include INC_API(syscall.h)
#include <l4/api/thread.h>
#include <l4/api/syscall.h>
#include <l4/api/errno.h>
#include <l4/generic/tcb.h>
#include <l4/lib/idpool.h>
#include <l4/lib/mutex.h>
#include <l4/generic/pgalloc.h>
#include INC_ARCH(asm.h)
#include INC_SUBARCH(mm.h)
@@ -24,42 +24,58 @@ int thread_suspend(struct task_ids *ids)
{
struct ktcb *task;
if ((task = find_task(ids->tid))) {
sched_suspend_task(task);
return 0;
}
if (!(task = find_task(ids->tid)))
return -ESRCH;
printk("%s: Error: Could not find any thread with id %d to start.\n",
__FUNCTION__, ids->tid);
return -EINVAL;
/*
* The thread_control_lock is protecting from
* indirect modification of thread context, this
* does not cause any such operation so we don't
* need to acquire that lock here.
*/
sched_suspend_task(task);
return 0;
}
int thread_resume(struct task_ids *ids)
{
struct ktcb *task;
if ((task = find_task(ids->tid))) {
sched_resume_task(task);
return 0;
}
if (!(task = find_task(ids->tid)))
return -ESRCH;
printk("%s: Error: Could not find any thread with id %d to start.\n",
__FUNCTION__, ids->tid);
return -EINVAL;
if (!mutex_trylock(&task->thread_control_lock))
return -EAGAIN;
/* Notify scheduler of task resume */
sched_notify_resume(task);
/* Release lock and return */
mutex_unlock(&task->thread_control_lock);
return 0;
}
int thread_start(struct task_ids *ids)
{
struct ktcb *task;
if ((task = find_task(ids->tid))) {
sched_start_task(task);
return 0;
}
printk("%s: Error: Could not find any thread with id %d to start.\n",
__FUNCTION__, ids->tid);
BUG();
return -EINVAL;
if (!(task = find_task(ids->tid)))
return -ESRCH;
if (!mutex_trylock(&task->thread_control_lock))
return -EAGAIN;
/* Clear creation flags if thread is new */
if (task->flags & THREAD_CREATE_FLAGS)
task->flags &= ~THREAD_CREATE_FLAGS;
/* Notify scheduler of task resume */
sched_notify_resume(task);
/* Release lock and return */
mutex_unlock(&task->thread_control_lock);
return 0;
}
@@ -139,8 +155,8 @@ int thread_setup_new_ids(struct task_ids *ids, unsigned int flags,
* If thread space is new or copied,
* allocate a new space id and tgid
*/
if (flags == THREAD_CREATE_NEWSPC ||
flags == THREAD_CREATE_COPYSPC) {
if (flags == THREAD_NEW_SPACE ||
flags == THREAD_COPY_SPACE) {
/*
* Allocate requested id if
* it's available, else a new one
@@ -156,7 +172,7 @@ int thread_setup_new_ids(struct task_ids *ids, unsigned int flags,
}
/* If thread space is the same, tgid is either new or existing one */
if (flags == THREAD_CREATE_SAMESPC) {
if (flags == THREAD_SAME_SPACE) {
/* Check if same tgid is expected */
if (ids->tgid != orig->tgid) {
if ((ids->tgid = id_get(tgroup_id_pool,
@@ -181,9 +197,9 @@ int thread_setup_new_ids(struct task_ids *ids, unsigned int flags,
int thread_create(struct task_ids *ids, unsigned int flags)
{
struct ktcb *task = 0, *new = (struct ktcb *)zalloc_page();
flags &= THREAD_FLAGS_MASK;
flags &= THREAD_CREATE_MASK;
if (flags == THREAD_CREATE_NEWSPC) {
if (flags == THREAD_NEW_SPACE) {
/* Allocate new pgd and copy all kernel areas */
new->pgd = alloc_pgd();
copy_pgd_kern_all(new->pgd);
@@ -192,7 +208,7 @@ int thread_create(struct task_ids *ids, unsigned int flags)
list_for_each_entry(task, &global_task_list, task_list) {
/* Space ids match, can use existing space */
if (task->spid == ids->spid) {
if (flags == THREAD_CREATE_SAMESPC)
if (flags == THREAD_SAME_SPACE)
new->pgd = task->pgd;
else
new->pgd = copy_page_tables(task->pgd);
@@ -207,8 +223,8 @@ out:
/* Set up new thread's tid, spid, tgid according to flags */
thread_setup_new_ids(ids, flags, new, task);
/* Set task state. */
new->state = TASK_INACTIVE;
/* Initialise task's scheduling state and parameters. */
sched_init_task(new);
/* Initialise ipc waitqueues */
waitqueue_head_init(&new->wqh_send);
@@ -219,10 +235,16 @@ out:
* system call return environment so that it can safely
* return as a copy of its original thread.
*/
if (flags == THREAD_CREATE_COPYSPC ||
flags == THREAD_CREATE_SAMESPC)
if (flags == THREAD_COPY_SPACE ||
flags == THREAD_SAME_SPACE)
arch_setup_new_thread(new, task);
/*
* Set thread's creation flags. They will clear
* when the thread is run for the first time
*/
new->flags = THREAD_CREATE_MASK & flags;
/* Add task to global hlist of tasks */
add_task_global(new);
@@ -253,7 +275,7 @@ int sys_thread_control(syscall_context_t *regs)
case THREAD_RESUME:
ret = thread_resume(ids);
break;
/* TODO: THREAD_DESTROY! */
/* TODO: Add THREAD_DESTROY! */
default:
ret = -EINVAL;
}