mirror of
https://github.com/drasko/codezero.git
synced 2026-01-12 10:53:16 +01:00
exchange_registers(), modified thread_control calls seem to work
- Fixed do_mmap() so that it returns mapped address, and various bugs. - A child seems to fork with new setup, but with incorrect return value. Need to use and test exregs() for fork + clone. - Shmat searches an unmapped area if input arg is invalid, do_mmap() should do this.
This commit is contained in:
@@ -28,148 +28,55 @@ void print_syscall_context(struct ktcb *t)
|
||||
r->r5, r->r6, r->r7, r->r8, r->sp_usr, r->lr_usr);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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)
|
||||
/* Copy each register to task's context if its valid bit is set */
|
||||
void do_exchange_registers(struct ktcb *task, struct exregs_data *exregs)
|
||||
{
|
||||
unsigned int create_flags = task->flags;
|
||||
task_context_t *context = &task->context;
|
||||
syscall_context_t *sysregs = task->syscall_regs;
|
||||
|
||||
/*
|
||||
* 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.
|
||||
* We do however, make sure spsr is not modified
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 */
|
||||
/* Check register valid bit and 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;
|
||||
|
||||
/* 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)
|
||||
|
||||
/* Set its registers */
|
||||
task->context.pc = pc;
|
||||
task->context.sp = sp;
|
||||
task->context.spsr = ARM_MODE_USR;
|
||||
/* Set thread's pager if one is supplied */
|
||||
if (exregs->flags & EXREGS_SET_PAGER)
|
||||
task->pagerid = exregs->pagerid;
|
||||
|
||||
}
|
||||
|
||||
@@ -178,34 +85,22 @@ void do_exchange_registers(struct tcb *task, struct exregs_data *exregs)
|
||||
*
|
||||
* 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
|
||||
* set only when the thread is in user mode. A newly created thread
|
||||
* that is the copy of another thread (forked or cloned) will also
|
||||
* be given its user mode context so such threads can also be
|
||||
* modified by this call before execution.
|
||||
*
|
||||
* 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 the scheduler does not execute it as we modify
|
||||
* its context.
|
||||
*/
|
||||
int sys_exchange_registers(syscall_context_t *regs)
|
||||
{
|
||||
int err = 0;
|
||||
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;
|
||||
struct exregs_data *exregs = (struct exregs_data *)regs->r0;
|
||||
l4id_t tid = regs->r1;
|
||||
|
||||
/* Find tcb from its list */
|
||||
if (!(task = find_task(tid)))
|
||||
@@ -220,46 +115,26 @@ int sys_exchange_registers(syscall_context_t *regs)
|
||||
|
||||
/* Now check that the task is suspended */
|
||||
if (task->state != TASK_INACTIVE) {
|
||||
mutex_unlock(&task->thread_control_lock);
|
||||
return -EACTIVE;
|
||||
err = -EACTIVE;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check that it is legitimate to modify
|
||||
* the task registers state
|
||||
* The thread must be in user mode for its context
|
||||
* to be modified.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
if (!TASK_IN_USER(task)) {
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Set its pager if one is supplied */
|
||||
if (pagerid != THREAD_ID_INVALID)
|
||||
task->pagerid = pagerid;
|
||||
/* Copy registers */
|
||||
do_exchange_registers(task, exregs);
|
||||
|
||||
return 0;
|
||||
out:
|
||||
/* Unlock and return */
|
||||
mutex_unlock(&task->thread_control_lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
int sys_schedule(syscall_context_t *regs)
|
||||
|
||||
@@ -66,10 +66,6 @@ int thread_start(struct task_ids *ids)
|
||||
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);
|
||||
|
||||
@@ -78,6 +74,50 @@ int thread_start(struct task_ids *ids)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int arch_setup_new_thread(struct ktcb *new, struct ktcb *orig, unsigned int flags)
|
||||
{
|
||||
/* New threads just need their mode set up */
|
||||
if (flags == THREAD_NEW_SPACE) {
|
||||
BUG_ON(orig);
|
||||
new->context.spsr = ARM_MODE_USR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* For duplicated threads pre-syscall context is saved on
|
||||
* the kernel stack. We copy this context of original
|
||||
* into the duplicate thread's current context structure
|
||||
*
|
||||
* We don't lock for context modification because the
|
||||
* thread is not known to the system yet.
|
||||
*/
|
||||
new->context.spsr = orig->syscall_regs->spsr; /* User mode */
|
||||
new->context.r0 = orig->syscall_regs->r0;
|
||||
new->context.r1 = orig->syscall_regs->r1;
|
||||
new->context.r2 = orig->syscall_regs->r2;
|
||||
new->context.r3 = orig->syscall_regs->r3;
|
||||
new->context.r4 = orig->syscall_regs->r4;
|
||||
new->context.r5 = orig->syscall_regs->r5;
|
||||
new->context.r6 = orig->syscall_regs->r6;
|
||||
new->context.r7 = orig->syscall_regs->r7;
|
||||
new->context.r8 = orig->syscall_regs->r8;
|
||||
new->context.r9 = orig->syscall_regs->r9;
|
||||
new->context.r10 = orig->syscall_regs->r10;
|
||||
new->context.r11 = orig->syscall_regs->r11;
|
||||
new->context.r12 = orig->syscall_regs->r12;
|
||||
new->context.sp = orig->syscall_regs->sp_usr;
|
||||
/* Skip lr_svc since it's not going to be used */
|
||||
new->context.pc = orig->syscall_regs->lr_usr;
|
||||
|
||||
/* Copy other relevant fields from original ktcb */
|
||||
new->pagerid = orig->pagerid;
|
||||
|
||||
/* Distribute original thread's ticks into two threads */
|
||||
new->ticks_left = orig->ticks_left / 2;
|
||||
orig->ticks_left /= 2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern unsigned int return_from_syscall;
|
||||
|
||||
@@ -89,7 +129,7 @@ extern unsigned int return_from_syscall;
|
||||
* stack is restored. It also modifies r0 to ensure POSIX child return
|
||||
* semantics.
|
||||
*/
|
||||
int arch_setup_new_thread(struct ktcb *new, struct ktcb *orig)
|
||||
int arch_setup_new_thread_orig(struct ktcb *new, struct ktcb *orig)
|
||||
{
|
||||
/*
|
||||
* Pre-syscall context is saved on the kernel stack upon
|
||||
@@ -230,20 +270,7 @@ out:
|
||||
waitqueue_head_init(&new->wqh_send);
|
||||
waitqueue_head_init(&new->wqh_recv);
|
||||
|
||||
/*
|
||||
* When space is copied this restores the new thread's
|
||||
* system call return environment so that it can safely
|
||||
* return as a copy of its original thread.
|
||||
*/
|
||||
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;
|
||||
arch_setup_new_thread(new, task, flags);
|
||||
|
||||
/* Add task to global hlist of tasks */
|
||||
add_task_global(new);
|
||||
|
||||
@@ -155,7 +155,7 @@ static inline void sched_rq_remove_task(struct ktcb *task)
|
||||
task->rq = 0;
|
||||
}
|
||||
|
||||
static inline void sched_init_task(struct ktcb *task)
|
||||
void sched_init_task(struct ktcb *task)
|
||||
{
|
||||
INIT_LIST_HEAD(&task->rq_list);
|
||||
task->ticks_left = TASK_TIMESLICE_DEFAULT;
|
||||
@@ -226,7 +226,7 @@ void sched_notify_resume(struct ktcb *task)
|
||||
/* NOTE: Might as well just set need_resched instead of full yield.
|
||||
* This would work on irq context as well. */
|
||||
/* Same as resume, but also yields. */
|
||||
int sched_resume_task(struct ktcb *task)
|
||||
void sched_resume_task(struct ktcb *task)
|
||||
{
|
||||
sched_notify_resume(task);
|
||||
sched_yield();
|
||||
|
||||
Reference in New Issue
Block a user