Skip to contentSkip to conversation

syslets: x86, add move_user_context() method

PATCH In review Ingo Molnar 2 participants 3 messages Feb 15, 2007 – Feb 16, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 9/14 syslets: x86, add move_user_context() method

From: Ingo Molnar <[email protected]>

add the move_user_context() method to move the user-space context of one kernel thread to another kernel thread. User-space might notice the changed TID, but execution, stack and register contents (general purpose and FPU) are still the same.

An architecture must implement this interface before it can turn CONFIG_ASYNC_SUPPORT on.

---
 arch/i386/kernel/process.c |   21 +++++++++++++++++++++
 include/asm-i386/system.h  |    7 +++++++
 2 files changed, 28 insertions(+)
arch/i386/kernel/process.c +21 -0
Index: linux/arch/i386/kernel/process.c
===================================================================
@@ -820,6 +820,27 @@ unsigned long get_wchan(struct task_stru
 }
 
 /*
+ * Move user-space context from one kernel thread to another.
+ * This includes registers and FPU state. Callers must make
+ * sure that neither task is running user context at the moment:
+ */
+void
+move_user_context(struct task_struct *new_task, struct task_struct *old_task)
+{
+	struct pt_regs *old_regs = task_pt_regs(old_task);
+	struct pt_regs *new_regs = task_pt_regs(new_task);
+	union i387_union *tmp;
+
+	*new_regs = *old_regs;
+	/*
+	 * Flip around the FPU state too:
+	 */
+	tmp = new_task->thread.i387;
+	new_task->thread.i387 = old_task->thread.i387;
+	old_task->thread.i387 = tmp;
+}
+
+/*
  * sys_alloc_thread_area: get a yet unused TLS descriptor index.
  */
 static int get_free_idx(void)
include/asm-i386/system.h +7 -0
Index: linux/include/asm-i386/system.h
===================================================================
@@ -33,6 +33,13 @@ extern struct task_struct * FASTCALL(__s
 		      "2" (prev), "d" (next));				\
 } while (0)
 
+/*
+ * Move user-space context from one kernel thread to another.
+ * This includes registers and FPU state for now:
+ */
+extern void
+move_user_context(struct task_struct *new_task, struct task_struct *old_task);
+
 #define _set_base(addr,base) do { unsigned long __pr; \
 __asm__ __volatile__ ("movw %%dx,%1\n\t" \
 	"rorl $16,%%edx\n\t" \
#1 lore raw
Quoted 10 lines · Ingo Molnar

/*
+ * Move user-space context from one kernel thread to another.
+ * This includes registers and FPU state. Callers must make
+ * sure that neither task is running user context at the moment:
+ */

arch/i386/kernel/process.c +5 -0
+void
+move_user_context(struct task_struct *new_task, struct task_struct *old_task)
+{
+	struct pt_regs *old_regs = task_pt_regs(old_task);
+	struct pt_regs *new_regs = task_pt_regs(new_task);

arch/i386/kernel/process.c unsigned long get_wchan(struct task_stru · line 841

Quoted 10 lines
arch/i386/kernel/process.c +10 -0
+	union i387_union *tmp;
+
+	*new_regs = *old_regs;
+	/*
+	 * Flip around the FPU state too:
+	 */
+	tmp = new_task->thread.i387;
+	new_task->thread.i387 = old_task->thread.i387;
+	old_task->thread.i387 = tmp;
+}

Let's say that old_task ("prev" at the incoming schedule) has TS_USEDFPU set. Its context gets moved to the new_task (the one returning to userspace) *before* the __unlazy_fpu() done in __switch_to(). The __unlazy_fpu() at the following schedule will save the state to the old new_task context, and that fine as far as the going-to-sleep task goes. The next fault happening in new_task (return to userspace one) will reload a non up2date context (the one we got from old_task, but never hit by the __unlazy_fpu() flush). Right?

- Davide

#2 lore raw
Quoted 23 lines · Davide Libenzi
Quoted 10 lines · Ingo Molnar

/*
+ * Move user-space context from one kernel thread to another.
+ * This includes registers and FPU state. Callers must make
+ * sure that neither task is running user context at the moment:
+ */

arch/i386/kernel/process.c +5 -0
+void
+move_user_context(struct task_struct *new_task, struct task_struct *old_task)
+{
+	struct pt_regs *old_regs = task_pt_regs(old_task);
+	struct pt_regs *new_regs = task_pt_regs(new_task);

arch/i386/kernel/process.c unsigned long get_wchan(struct task_stru · line 841

Quoted 10 lines
arch/i386/kernel/process.c +10 -0
+	union i387_union *tmp;
+
+	*new_regs = *old_regs;
+	/*
+	 * Flip around the FPU state too:
+	 */
+	tmp = new_task->thread.i387;
+	new_task->thread.i387 = old_task->thread.i387;
+	old_task->thread.i387 = tmp;
+}
Quoted 8 lines

Let's say that old_task ("prev" at the incoming schedule) has TS_USEDFPU set. Its context gets moved to the new_task (the one returning to userspace) *before* the __unlazy_fpu() done in __switch_to(). The __unlazy_fpu() at the following schedule will save the state to the old new_task context, and that fine as far as the going-to-sleep task goes. The next fault happening in new_task (return to userspace one) will reload a non up2date context (the one we got from old_task, but never hit by the __unlazy_fpu() flush). Right?

Yeah. Given TS_USEDFPU set, before move_user_context():

CPU => FPUc
NTSK => FPUn
OTSK => FPUo

After move_user_context():

CPU => FPUc
NTSK => FPUo
OTSK => FPUn

After the incoming __unlazy_fpu() in __switch_to():

CPU => FPUc
NTSK => FPUo
OTSK => FPUc

After the first fault in NTSK:

CPU => FPUo
NTSK => FPUo
OTSK => FPUc

So NTSK loads a non up2date FPUo, instead of the FPUc that was the "dirty" context to migrate (since TS_USEDFPU was set).
I think you need an early __unlazy_fpu() in that case.

- Davide