Skip to contentSkip to conversation

Reworking forge FPU switching

Xenomai New Gilles Chanteperdrix 1 participant 8 messages Oct 25, 2013

lore
Share & tools
lore
#0 lore raw

Hi,

after testing a few ideas to avoid useless fpu save/restore, I came to the changes in the following patches. I tried to split the changes in many patches to simplify review. The ARM changes are not included, they are not finished yet, and the critics to the changes in the generic parts may entail some changes for ARM.

Regards.

Signature · 1 line
                                                                Gilles.
#1 lore raw

PATCH 1/7 cobalt/fpu: remove xnarch_init_fpu, it is no longer used

---
 kernel/cobalt/arch/arm/thread.c     |   30 ------------------------------
 kernel/cobalt/arch/powerpc/thread.c |    9 ---------
 kernel/cobalt/arch/sh/thread.c      |   14 --------------
 3 files changed, 53 deletions(-)
kernel/cobalt/arch/arm/thread.c +0 -30
index e29c607..a73eded 100644
@@ -33,20 +33,6 @@ asmlinkage void __asm_thread_trampoline(void);
 
 #ifdef CONFIG_XENO_HW_FPU
 
-static inline void do_init_fpu(struct arm_fpustate *fpuenv)
-{
-	fp_init(&fpuenv->fpstate);
-#if defined(CONFIG_VFP)
-	/* vfpstate has already been zeroed by xnarch_init_fpu */
-	fpuenv->vfpstate.hard.fpexc = FPEXC_EN;
-	fpuenv->vfpstate.hard.fpscr = FPSCR_ROUND_NEAREST;
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)			\
-     || defined(CONFIG_VFP_3_2_BACKPORT)) && defined(CONFIG_SMP)
-	fpuenv->vfpstate.hard.cpu = NR_CPUS;
-#endif /* linux >= 3.2.0 */
-#endif
-}
-
 #define task_fpenv(task)						\
 	((struct arm_fpustate *) &task_thread_info(task)->used_cp[0])
 
@@ -341,22 +327,6 @@ void xnarch_enable_fpu(struct xnarchtcb *tcb)
 #endif /* CONFIG_XENO_HW_FPU */
 }
 
-void xnarch_init_fpu(struct xnarchtcb *tcb)
-{
-	/*
-	 * This must run on behalf of the thread we initialize the FPU
-	 * for.
-	 */
-#ifdef CONFIG_XENO_HW_FPU
-	memset(&tcb->fpuenv, 0, sizeof(tcb->fpuenv));
-	do_init_fpu(&tcb->fpuenv);
-#ifdef CONFIG_VFP
-	do_enable_fpu();
-	do_restore_fpu(&tcb->fpuenv);
-#endif /* CONFIG_VFP */
-#endif /* CONFIG_XENO_HW_FPU */
-}
-
 void xnarch_save_fpu(struct xnarchtcb *tcb)
 {
 #ifdef CONFIG_XENO_HW_FPU
kernel/cobalt/arch/powerpc/thread.c +0 -9
index 1ee8c57..d6f2e74 100644
@@ -115,15 +115,6 @@ asmlinkage void __asm_restore_fpu(struct thread_struct *ts);
 })
 #endif /* CONFIG_PPC64 */
 
-void xnarch_init_fpu(struct xnarchtcb *tcb)
-{
-	/*
-	 * This must run on behalf of the thread we initialize the FPU
-	 * for. All FPU regs are guaranteed zero.
-	 */
-	__asm_init_fpu(&tcb->core.ts);
-}
-
 void xnarch_enable_fpu(struct xnarchtcb *tcb)
 {
 	struct task_struct *task = tcb->core.host_task;
kernel/cobalt/arch/sh/thread.c +0 -14
index 5ef75d3..e4d8414 100644
@@ -226,20 +226,6 @@ static inline void do_restore_fpu(struct thread_struct *ts)
 		      :"memory");
 }
 
-static inline void do_init_fpu(struct thread_struct *ts)
-{
-	do_restore_fpu(ts);
-}
-
-void xnarch_init_fpu(struct xnarchtcb *tcb)
-{
-	/*
-	 * This must run on behalf of the thread we initialize the FPU
-	 * for. All FPU regs are guaranteed zero.
-	 */
-	do_init_fpu(&tcb->core.ts);
-}
-
 inline void xnarch_enable_fpu(struct xnarchtcb *tcb)
 {
 	struct task_struct *task = tcb->core.host_task;
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 2/7 cobalt/fpu: pass xnthread structures to arch/*/thread.c

in order to prepare for next changes

---
 include/cobalt/kernel/thread.h                     |    6 ++++-
 .../cobalt/arch/arm/include/asm/xenomai/thread.h   |   26 +++++++++++---------
 kernel/cobalt/arch/arm/mayday.c                    |    2 +-
 kernel/cobalt/arch/arm/thread.c                    |   15 +++++++----
 .../arch/blackfin/include/asm/xenomai/thread.h     |   18 +++++++-------
 kernel/cobalt/arch/blackfin/mayday.c               |    2 +-
 kernel/cobalt/arch/blackfin/thread.c               |    3 ++-
 .../cobalt/arch/nios2/include/asm/xenomai/thread.h |   18 +++++++-------
 kernel/cobalt/arch/nios2/mayday.c                  |    2 +-
 kernel/cobalt/arch/nios2/thread.c                  |    3 ++-
 .../arch/powerpc/include/asm/xenomai/thread.h      |   24 +++++++++---------
 kernel/cobalt/arch/powerpc/mayday.c                |    2 +-
 kernel/cobalt/arch/powerpc/thread.c                |   17 ++++++++-----
 kernel/cobalt/arch/sh/include/asm/xenomai/thread.h |   18 ++++++++------
 kernel/cobalt/arch/sh/mayday.c                     |    2 +-
 kernel/cobalt/arch/sh/thread.c                     |   15 +++++++----
 .../cobalt/arch/x86/include/asm/xenomai/thread.h   |   26 ++++++++++----------
 kernel/cobalt/arch/x86/mayday.c                    |    2 +-
 kernel/cobalt/arch/x86/thread.c                    |   25 ++++++++++++-------
 kernel/cobalt/include/asm-generic/xenomai/thread.h |    5 ++++
 kernel/cobalt/sched.c                              |    6 ++---
 kernel/cobalt/shadow.c                             |    6 ++---
 kernel/cobalt/thread.c                             |   16 ++++++------
 23 files changed, 146 insertions(+), 113 deletions(-)
include/cobalt/kernel/thread.h +5 -1
index 4539c15..aa2fd46 100644
@@ -208,6 +208,11 @@ static inline void xnthread_clear_info(struct xnthread *thread, int bits)
 	thread->info &= ~bits;
 }
 
+static inline struct xnarchtcb *xnthread_archtcb(struct xnthread *thread)
+{
+	return &thread->tcb;
+}
+
 #define xnthread_lock_count(thread)        ((thread)->schedlck)
 #define xnthread_init_schedparam(thread)   ((thread)->init_schedparam)
 #define xnthread_base_priority(thread)     ((thread)->bprio)
@@ -216,7 +221,6 @@ static inline void xnthread_clear_info(struct xnthread *thread, int bits)
 #define xnthread_base_class(thread)        ((thread)->base_class)
 #define xnthread_sched_class(thread)       ((thread)->sched_class)
 #define xnthread_time_slice(thread)        ((thread)->rrperiod)
-#define xnthread_archtcb(thread)           (&((thread)->tcb))
 #define xnthread_timeout(thread)           xntimer_get_timeout(&(thread)->rtimer)
 #define xnthread_handle(thread)            ((thread)->registry.handle)
 #define xnthread_host_task(thread)         (xnthread_archtcb(thread)->core.host_task)
kernel/cobalt/arch/arm/include/asm/xenomai/thread.h +14 -12
index d12a050..fc79fc8 100644
@@ -81,28 +81,30 @@ struct xnarchtcb {
 
 #define xnarch_fault_notify(d) (!xnarch_fault_bp_p(d))
 
-void xnarch_enable_fpu(struct xnarchtcb *current_tcb);
+void xnarch_enable_fpu(struct xnthread *current_thread);
 
-void xnarch_save_fpu(struct xnarchtcb *tcb);
+void xnarch_save_fpu(struct xnthread *thread);
 
-void xnarch_restore_fpu(struct xnarchtcb *tcb);
+void xnarch_restore_fpu(struct xnthread *thread);
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb);
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in);
 
-static inline void xnarch_enter_root(struct xnarchtcb *rootcb) { }
+static inline void xnarch_enter_root(struct xnthread *root) { }
 
 int xnarch_escalate(void);
 
 #ifdef CONFIG_XENO_HW_FPU
 
-static inline void xnarch_init_root_tcb(struct xnarchtcb *tcb)
+static inline void xnarch_init_root_tcb(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	tcb->is_root = 1;
 	tcb->fpup = NULL;
 }
 
-static inline void xnarch_init_shadow_tcb(struct xnarchtcb *tcb)
+static inline void xnarch_init_shadow_tcb(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	tcb->is_root = 0;
 	tcb->fpup = (struct arm_fpustate *)
 		&task_thread_info(tcb->core.host_task)->used_cp[0];
@@ -110,12 +112,12 @@ static inline void xnarch_init_shadow_tcb(struct xnarchtcb *tcb)
 
 int xnarch_fault_fpu_p(struct ipipe_trap_data *d);
 
-void xnarch_leave_root(struct xnarchtcb *rootcb);
+void xnarch_leave_root(struct xnthread *root);
 
 #else /* !CONFIG_XENO_HW_FPU */
 
-static inline void xnarch_init_root_tcb(struct xnarchtcb *tcb) { }
-static inline void xnarch_init_shadow_tcb(struct xnarchtcb *tcb) { }
+static inline void xnarch_init_root_tcb(struct xnthread *thread) { }
+static inline void xnarch_init_shadow_tcb(struct xnthread *thread) { }
 
 /*
  * Userland may raise FPU faults with FPU-enabled kernels, regardless
@@ -127,11 +129,11 @@ static inline int xnarch_fault_fpu_p(struct ipipe_trap_data *d)
 	return 0;
 }
 
-static inline void xnarch_leave_root(struct xnarchtcb *rootcb) { }
+static inline void xnarch_leave_root(struct xnthread *root) { }
 
 #endif /* !CONFIG_XENO_HW_FPU */
 
-static inline int xnarch_handle_fpu_fault(struct xnarchtcb *tcb)
+static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
 {
 	return 0;
 }
kernel/cobalt/arch/arm/mayday.c +1 -1
index 2532fd2..142d69e 100644
@@ -18,10 +18,10 @@
  */
 #include <linux/types.h>
 #include <linux/ipipe.h>
+#include <cobalt/kernel/thread.h>
 #include <asm/cacheflush.h>
 #include <asm/ptrace.h>
 #include <asm/xenomai/syscall.h>
-#include <asm/xenomai/thread.h>
 
 void xnarch_setup_mayday_page(void *page)
 {
kernel/cobalt/arch/arm/thread.c +10 -5
index a73eded..d77e59a 100644
@@ -235,8 +235,9 @@ int xnarch_fault_fpu_p(struct ipipe_trap_data *d)
 	return exc != IPIPE_TRAP_UNDEFINSTR;
 }
 
-void xnarch_leave_root(struct xnarchtcb *rootcb)
+void xnarch_leave_root(struct xnthread *root)
 {
+	struct xnarchtcb *rootcb = xnthread_archtcb(root);
 #ifdef CONFIG_VFP
 	rootcb->fpup = get_fpu_owner();
 #else /* !CONFIG_VFP */
@@ -249,8 +250,9 @@ void xnarch_leave_root(struct xnarchtcb *rootcb)
 
 #endif /* CONFIG_XENO_HW_FPU */
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb)
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in)
 {
+	struct xnarchtcb *out_tcb = &out->tcb, *in_tcb = &in->tcb;
 	struct mm_struct *prev_mm, *next_mm;
 	struct task_struct *next;
 
@@ -282,8 +284,9 @@ void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb)
 #endif
 }
 
-void xnarch_enable_fpu(struct xnarchtcb *tcb)
+void xnarch_enable_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = &thread->tcb;
 #ifdef CONFIG_XENO_HW_FPU
 #ifdef CONFIG_VFP
 	/* If we are restoring the Linux current thread which does not own the
@@ -327,8 +330,9 @@ void xnarch_enable_fpu(struct xnarchtcb *tcb)
 #endif /* CONFIG_XENO_HW_FPU */
 }
 
-void xnarch_save_fpu(struct xnarchtcb *tcb)
+void xnarch_save_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = &thread->tcb;
 #ifdef CONFIG_XENO_HW_FPU
 #ifdef CONFIG_VFP
 	if (tcb->fpup)
@@ -346,8 +350,9 @@ void xnarch_save_fpu(struct xnarchtcb *tcb)
 #endif /* CONFIG_XENO_HW_FPU */
 }
 
-void xnarch_restore_fpu(struct xnarchtcb *tcb)
+void xnarch_restore_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = &thread->tcb;
 #ifdef CONFIG_XENO_HW_FPU
 #ifdef CONFIG_VFP
 	if (likely(!tcb->is_root)) {
kernel/cobalt/arch/blackfin/include/asm/xenomai/thread.h +9 -9
index 302675a..601fc4b 100644
@@ -50,19 +50,19 @@ struct xnarchtcb {
 
 #define xnarch_fault_notify(d) (!xnarch_fault_bp_p(d))
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb);
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in);
 
 int xnarch_escalate(void);
 
-static inline void xnarch_init_root_tcb(struct xnarchtcb *tcb) { }
-static inline void xnarch_init_shadow_tcb(struct xnarchtcb *tcb) { }
-static inline void xnarch_enter_root(struct xnarchtcb *rootcb) { }
-static inline void xnarch_leave_root(struct xnarchtcb *rootcb) { }
-static inline void xnarch_enable_fpu(struct xnarchtcb *current_tcb) { }
-static inline void xnarch_save_fpu(struct xnarchtcb *tcb) { }
-static inline void xnarch_restore_fpu(struct xnarchtcb *tcb) { }
+static inline void xnarch_init_root_tcb(struct xnthread *thread) { }
+static inline void xnarch_init_shadow_tcb(struct xnthread *thread) { }
+static inline void xnarch_enter_root(struct xnthread *root) { }
+static inline void xnarch_leave_root(struct xnthread *root) { }
+static inline void xnarch_enable_fpu(struct xnthread *current_thread) { }
+static inline void xnarch_save_fpu(struct xnthread *thread) { }
+static inline void xnarch_restore_fpu(struct xnthread *thread) { }
 
-static inline int xnarch_handle_fpu_fault(struct xnarchtcb *tcb)
+static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
 {
 	return 0;
 }
kernel/cobalt/arch/blackfin/mayday.c +1 -1
index a43c8d4..e778794 100644
@@ -18,11 +18,11 @@
  */
 #include <linux/types.h>
 #include <linux/ipipe.h>
+#include <cobalt/kernel/thread.h>
 #include <asm/cacheflush.h>
 #include <asm/ptrace.h>
 #include <asm/bug.h>
 #include <asm/xenomai/syscall.h>
-#include <asm/xenomai/thread.h>
 
 void xnarch_setup_mayday_page(void *page)
 {
kernel/cobalt/arch/blackfin/thread.c +2 -1
index e89cbb6..e0c5d54 100644
@@ -56,8 +56,9 @@ void mpu_switch(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb)
 
 #endif /* CONFIG_MPU */
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb)
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in)
 {
+	struct xnarchtcb *out_tcb = &out->tcb, *in_tcb = &in->tcb;
 	mpu_switch(out_tcb, in_tcb);
 	__asm_switch_context(out_tcb->core.tsp, in_tcb->core.tsp);
 }
kernel/cobalt/arch/nios2/include/asm/xenomai/thread.h +9 -9
index 85b18da..775b92a 100644
@@ -49,20 +49,20 @@ struct xnarchtcb {
 
 #define xnarch_fault_notify(d) (xnarch_fault_bp_p(d) == 0)
 
-static inline void xnarch_init_root_tcb(struct xnarchtcb *tcb) { }
-static inline void xnarch_init_shadow_tcb(struct xnarchtcb *tcb) { }
-static inline void xnarch_enter_root(struct xnarchtcb *rootcb) { }
-static inline void xnarch_leave_root(struct xnarchtcb *rootcb) { }
-static inline void xnarch_enable_fpu(struct xnarchtcb *current_tcb) { }
-static inline void xnarch_save_fpu(struct xnarchtcb *tcb) { }
-static inline void xnarch_restore_fpu(struct xnarchtcb *tcb) { }
+static inline void xnarch_init_root_tcb(struct xnthread *thread) { }
+static inline void xnarch_init_shadow_tcb(struct xnthread *thread) { }
+static inline void xnarch_enter_root(struct xnthread *root) { }
+static inline void xnarch_leave_root(struct xnthread *thread) { }
+static inline void xnarch_enable_fpu(struct xnthread *thread) { }
+static inline void xnarch_save_fpu(struct xnthread *thread) { }
+static inline void xnarch_restore_fpu(struct xnthread *thread) { }
 
-static inline int xnarch_handle_fpu_fault(struct xnarchtcb *tcb)
+static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
 {
 	return 0;
 }
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb);
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in);
 
 int xnarch_escalate(void);
 
kernel/cobalt/arch/nios2/mayday.c +1 -1
index 537034f..89f9572 100644
@@ -19,9 +19,9 @@
 
 #include <linux/types.h>
 #include <linux/ipipe.h>
+#include <cobalt/kernel/thread.h>
 #include <asm/cacheflush.h>
 #include <asm/ptrace.h>
-#include <asm/xenomai/thread.h>
 
 void xnarch_setup_mayday_page(void *page)
 {
kernel/cobalt/arch/nios2/thread.c +2 -1
index 90fdf35..2aed7db 100644
@@ -27,8 +27,9 @@ asmlinkage void
 __asm_thread_switch(struct thread_struct *prev,
 		    struct thread_struct *next);
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb)
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in)
 {
+	struct xnarchtcb *out_tcb = &out->tcb, *in_tcb = &in->tcb;
 	struct mm_struct *prev_mm, *next_mm;
 	struct task_struct *next;
 
kernel/cobalt/arch/powerpc/include/asm/xenomai/thread.h +13 -11
index 042946f..07a17cf 100644
@@ -52,42 +52,44 @@ struct xnarchtcb {
 				 (d)->exception == IPIPE_TRAP_DEBUG))
 #define xnarch_fault_notify(d) (xnarch_fault_bp_p(d) == 0)
 
-static inline void xnarch_enter_root(struct xnarchtcb *rootcb) { }
+static inline void xnarch_enter_root(struct xnthread *root) { }
 
 #ifdef CONFIG_XENO_HW_FPU
 
-static inline void xnarch_init_root_tcb(struct xnarchtcb *tcb)
+static inline void xnarch_init_root_tcb(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	tcb->fpup = NULL;
 }
 
-static inline void xnarch_init_shadow_tcb(struct xnarchtcb *tcb)
+static inline void xnarch_init_shadow_tcb(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	tcb->fpup = &tcb->core.host_task->thread;
 }
 
-void xnarch_leave_root(struct xnarchtcb *rootcb);
+void xnarch_leave_root(struct xnthread *root);
 
 #else  /* !CONFIG_XENO_HW_FPU */
 
-static inline void xnarch_init_root_tcb(struct xnarchtcb *tcb) { }
-static inline void xnarch_init_shadow_tcb(struct xnarchtcb *tcb) { }
+static inline void xnarch_init_root_tcb(struct xnthread *thread) { }
+static inline void xnarch_init_shadow_tcb(struct xnthread *thread) { }
 
 #endif  /* !CONFIG_XENO_HW_FPU */
 
-static inline int xnarch_handle_fpu_fault(struct xnarchtcb *tcb)
+static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
 {
 	return 0;
 }
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb);
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in);
 
 int xnarch_escalate(void);
 
-void xnarch_enable_fpu(struct xnarchtcb *current_tcb);
+void xnarch_enable_fpu(struct xnthread *current_thread);
 
-void xnarch_save_fpu(struct xnarchtcb *tcb);
+void xnarch_save_fpu(struct xnthread *thread);
 
-void xnarch_restore_fpu(struct xnarchtcb *tcb);
+void xnarch_restore_fpu(struct xnthread *thread);
 
 #endif /* !_COBALT_POWERPC_ASM_THREAD_H */
kernel/cobalt/arch/powerpc/mayday.c +1 -1
index 5800caa..8a9ab95 100644
@@ -19,9 +19,9 @@
 
 #include <linux/types.h>
 #include <linux/ipipe.h>
+#include <cobalt/kernel/thread.h>
 #include <asm/cacheflush.h>
 #include <asm/ptrace.h>
-#include <asm/xenomai/thread.h>
 #include <asm/xenomai/syscall.h>
 
 void xnarch_setup_mayday_page(void *page)
kernel/cobalt/arch/powerpc/thread.c +11 -6
index d6f2e74..70aad0e 100644
@@ -30,9 +30,9 @@ asmlinkage struct task_struct *
 __asm_thread_switch(struct thread_struct *prev,
 		    struct thread_struct *next);
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb,
-		      struct xnarchtcb *in_tcb)
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in)
 {
+	struct xnarchtcb *out_tcb = &out->tcb, *in_tcb = &in->tcb;
 	struct mm_struct *prev_mm, *next_mm;
 	struct task_struct *next;
 
@@ -115,8 +115,9 @@ asmlinkage void __asm_restore_fpu(struct thread_struct *ts);
 })
 #endif /* CONFIG_PPC64 */
 
-void xnarch_enable_fpu(struct xnarchtcb *tcb)
+void xnarch_enable_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct task_struct *task = tcb->core.host_task;
 
 	if (task && task != tcb->core.user_fpu_owner)
@@ -125,8 +126,10 @@ void xnarch_enable_fpu(struct xnarchtcb *tcb)
 		do_enable_fpu();
 }
 
-void xnarch_save_fpu(struct xnarchtcb *tcb)
+void xnarch_save_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
+
 	if (tcb->fpup) {
 		__asm_save_fpu(tcb->fpup);
 
@@ -136,8 +139,9 @@ void xnarch_save_fpu(struct xnarchtcb *tcb)
 	}
 }
 
-void xnarch_restore_fpu(struct xnarchtcb *tcb)
+void xnarch_restore_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct thread_struct *ts;
 	struct pt_regs *regs;
 
@@ -164,8 +168,9 @@ void xnarch_restore_fpu(struct xnarchtcb *tcb)
 		do_disable_fpu();
 }
 
-void xnarch_leave_root(struct xnarchtcb *rootcb)
+void xnarch_leave_root(struct xnthread *root)
 {
+	struct xnarchtcb *rootcb = xnthread_archtcb(root);
 	rootcb->core.user_fpu_owner = get_fpu_owner(rootcb->core.host_task);
 	/* So that xnarch_save_fpu() will operate on the right FPU area. */
 	rootcb->fpup = rootcb->core.user_fpu_owner ?
kernel/cobalt/arch/sh/include/asm/xenomai/thread.h +10 -8
index ef1358b..6bf7329 100644
@@ -48,31 +48,33 @@ struct xnarchtcb {
 				(d)->exception == IPIPE_TRAP_BP)
 #define xnarch_fault_notify(d) (xnarch_fault_bp_p(d) == 0)
 
-static inline void xnarch_enter_root(struct xnarchtcb *rootcb) { }
+static inline void xnarch_enter_root(struct xnthread *root) { }
 
 #ifdef CONFIG_XENO_HW_FPU
 
-void xnarch_leave_root(struct xnarchtcb *rootcb);
+void xnarch_leave_root(struct xnthread *root);
 
-static inline void xnarch_init_root_tcb(struct xnarchtcb *tcb)
+static inline void xnarch_init_root_tcb(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	tcb->fpup = NULL;
 }
 
-static inline void xnarch_init_shadow_tcb(struct xnarchtcb *tcb)
+static inline void xnarch_init_shadow_tcb(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	tcb->fpup = &tcb->core.host_task->thread;
 }
 
 #else /* !CONFIG_XENO_HW_FPU */
 
-static inline void xnarch_leave_root(struct xnarchtcb *rootcb) { }
-static inline void xnarch_init_root_tcb(struct xnarchtcb *tcb) { }
-static inline void xnarch_init_shadow_tcb(struct xnarchtcb *tcb) { }
+static inline void xnarch_leave_root(struct xnthread *root) { }
+static inline void xnarch_init_root_tcb(struct xnthread *thread) { }
+static inline void xnarch_init_shadow_tcb(struct xnthread *thread) { }
 
 #endif /* !CONFIG_XENO_HW_FPU */
 
-static inline int xnarch_handle_fpu_fault(struct xnarchtcb *tcb)
+static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
 {
 	return 0;
 }
kernel/cobalt/arch/sh/mayday.c +1 -1
index abaea70..b564a68 100644
@@ -20,9 +20,9 @@
 #include <linux/types.h>
 #include <linux/ipipe.h>
 #include <linux/mm.h>
+#include <cobalt/kernel/thread.h>
 #include <asm/cacheflush.h>
 #include <asm/ptrace.h>
-#include <asm/xenomai/thread.h>
 
 void xnarch_setup_mayday_page(void *page)
 {
kernel/cobalt/arch/sh/thread.c +10 -5
index e4d8414..4c8b532 100644
@@ -90,8 +90,9 @@
 	__last;							\
 	})
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb)
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in)
 {
+	struct xnarchtcb *out_tcb = &out->tcb, *in_tcb = &in->tcb;
 	struct mm_struct *prev_mm, *next_mm;
 	struct task_struct *next;
 
@@ -226,8 +227,9 @@ static inline void do_restore_fpu(struct thread_struct *ts)
 		      :"memory");
 }
 
-inline void xnarch_enable_fpu(struct xnarchtcb *tcb)
+inline void xnarch_enable_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct task_struct *task = tcb->core.host_task;
 
 	if (task != tcb->core.user_fpu_owner)
@@ -236,8 +238,9 @@ inline void xnarch_enable_fpu(struct xnarchtcb *tcb)
 		enable_fpu();
 }
 
-void xnarch_save_fpu(struct xnarchtcb *tcb)
+void xnarch_save_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct pt_regs *regs;
 
 	if (tcb->fpup) {
@@ -249,8 +252,9 @@ void xnarch_save_fpu(struct xnarchtcb *tcb)
 	}
 }
 
-void xnarch_restore_fpu(struct xnarchtcb *tcb)
+void xnarch_restore_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct pt_regs *regs;
 
 	if (tcb->fpup) {
@@ -269,8 +273,9 @@ void xnarch_restore_fpu(struct xnarchtcb *tcb)
 		disable_fpu();
 }
 
-void xnarch_leave_root(struct xnarchtcb *rootcb)
+void xnarch_leave_root(struct xnthread *root)
 {
+	struct xnarchtcb *rootcb = xnthread_archtcb(root);
 	rootcb->core.user_fpu_owner = get_fpu_owner(rootcb->core.host_task);
 	rootcb->fpup = rootcb->core.user_fpu_owner ?
 		&rootcb->core.user_fpu_owner->thread : NULL;
kernel/cobalt/arch/x86/include/asm/xenomai/thread.h +13 -13
index 5d4c7ca..ee0f385 100644
@@ -67,33 +67,33 @@ static inline int xnarch_shadow_p(struct xnarchtcb *tcb, struct task_struct *tas
 
 #ifdef CONFIG_XENO_HW_FPU
 
-void xnarch_save_fpu(struct xnarchtcb *tcb);
-void xnarch_restore_fpu(struct xnarchtcb *tcb);
-void xnarch_enable_fpu(struct xnarchtcb *tcb);
-int xnarch_handle_fpu_fault(struct xnarchtcb *tcb);
+void xnarch_save_fpu(struct xnthread *thread);
+void xnarch_restore_fpu(struct xnthread *thread);
+void xnarch_enable_fpu(struct xnthread *thread);
+int xnarch_handle_fpu_fault(struct xnthread *thread);
 
 #else /* !CONFIG_XENO_HW_FPU */
 
-static inline void xnarch_save_fpu(struct xnarchtcb *tcb) { }
-static inline void xnarch_restore_fpu(struct xnarchtcb *tcb) { }
-static inline void xnarch_enable_fpu(struct xnarchtcb *tcb) { }
+static inline void xnarch_save_fpu(struct xnthread *thread) { }
+static inline void xnarch_restore_fpu(struct xnthread *thread) { }
+static inline void xnarch_enable_fpu(struct xnthread *thread) { }
 
-static inline int xnarch_handle_fpu_fault(struct xnarchtcb *tcb)
+static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
 {
 	return 0;
 }
 
 #endif /* !CONFIG_XENO_HW_FPU */
 
-void xnarch_init_root_tcb(struct xnarchtcb *tcb);
+void xnarch_init_root_tcb(struct xnthread *thread);
 
-void xnarch_init_shadow_tcb(struct xnarchtcb *tcb);
+void xnarch_init_shadow_tcb(struct xnthread *thread);
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb);
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in);
 
-static inline void xnarch_enter_root(struct xnarchtcb *tcb) { }
+static inline void xnarch_enter_root(struct xnthread *root) { }
 
-void xnarch_leave_root(struct xnarchtcb *rootcb);
+void xnarch_leave_root(struct xnthread *root);
 
 int xnarch_escalate(void);
 
kernel/cobalt/arch/x86/mayday.c +1 -1
index 44e1a9c..2914369 100644
@@ -19,8 +19,8 @@
 
 #include <linux/types.h>
 #include <linux/ipipe.h>
+#include <cobalt/kernel/thread.h>
 #include <asm/ptrace.h>
-#include <asm/xenomai/thread.h>
 #include <asm/xenomai/syscall.h>
 
 #ifdef CONFIG_X86_32
kernel/cobalt/arch/x86/thread.c +16 -9
index 548c63c..0c24e02 100644
@@ -83,8 +83,9 @@ static inline void do_switch_threads(struct xnarchtcb *out_tcb,
 
 #endif /* CONFIG_X86_64 */
 
-void xnarch_switch_to(struct xnarchtcb *out_tcb, struct xnarchtcb *in_tcb)
+void xnarch_switch_to(struct xnthread *out, struct xnthread *in)
 {
+	struct xnarchtcb *out_tcb = &out->tcb, *in_tcb = &in->tcb;
 	unsigned long __maybe_unused fs, gs;
 	struct mm_struct *prev_mm, *next_mm;
 	struct task_struct *prev, *next;
@@ -218,8 +219,9 @@ static inline void __do_restore_i387(x86_fpustate *fpup)
 #endif /* CONFIG_X86_64 */
 }
 
-int xnarch_handle_fpu_fault(struct xnarchtcb *tcb)
+int xnarch_handle_fpu_fault(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(to);
 	struct task_struct *p = tcb->core.host_task;
 
 	if (tsk_used_math(p))
@@ -244,8 +246,9 @@ int xnarch_handle_fpu_fault(struct xnarchtcb *tcb)
 	return 1;
 }
 
-void xnarch_leave_root(struct xnarchtcb *rootcb)
+void xnarch_leave_root(struct xnthread *root)
 {
+	struct xnarchtcb *rootcb = xnthread_archtcb(root);
 	struct task_struct *p = current;
 
 #ifdef CONFIG_X86_64
@@ -263,8 +266,9 @@ void xnarch_leave_root(struct xnarchtcb *rootcb)
 	}
 }
 
-void xnarch_save_fpu(struct xnarchtcb *tcb)
+void xnarch_save_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct task_struct *p = tcb->core.host_task;
 
 	if (wrap_test_fpu_used(p) == 0)
@@ -278,8 +282,9 @@ void xnarch_save_fpu(struct xnarchtcb *tcb)
 	wrap_clear_fpu_used(p);
 }
 
-void xnarch_restore_fpu(struct xnarchtcb *tcb)
+void xnarch_restore_fpu(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct task_struct *p = tcb->core.host_task;
 
 	if (tcb->root_kfpu == 0 && (tsk_used_math(p) == 0 || tcb->is_root))
@@ -302,15 +307,16 @@ void xnarch_restore_fpu(struct xnarchtcb *tcb)
 		wrap_set_fpu_used(p);
 }
 
-void xnarch_enable_fpu(struct xnarchtcb *tcb)
+void xnarch_enable_fpu(struct xnthread *thread)
 {
-	xnarch_restore_fpu(tcb);
+	xnarch_restore_fpu(thread);
 }
 
 #endif /* CONFIG_XENO_HW_FPU */
 
-void xnarch_init_root_tcb(struct xnarchtcb *tcb)
+void xnarch_init_root_tcb(struct xnthread *thread)
 {
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	tcb->sp = 0;
 	tcb->spp = &tcb->sp;
 	tcb->ipp = &tcb->ip;
@@ -319,9 +325,10 @@ void xnarch_init_root_tcb(struct xnarchtcb *tcb)
 	tcb->root_kfpu = 0;
 }
 
-void xnarch_init_shadow_tcb(struct xnarchtcb *tcb)
+void xnarch_init_shadow_tcb(struct xnthread *thread)
 {
 	struct task_struct *p = tcb->core.host_task;
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 
 	tcb->sp = 0;
 	tcb->spp = &p->thread.sp;
kernel/cobalt/include/asm-generic/xenomai/thread.h +5 -0
index e1c1e8b..303d373 100644
@@ -28,6 +28,9 @@
 
 struct task_struct;
 
+struct xnthread;
+struct xnarchtcb;
+
 struct xntcb {
 	struct task_struct *host_task;
 	struct thread_struct *tsp;
@@ -43,4 +46,6 @@ struct xntcb {
 #endif
 };
 
+static inline struct xnarchtcb *xnthread_archtcb(struct xnthread *thread);
+
 #endif /* !_COBALT_ASM_GENERIC_THREAD_H */
kernel/cobalt/sched.c +2 -4
index 2e11826..682f71b 100644
@@ -25,7 +25,6 @@
 #include <cobalt/kernel/heap.h>
 #include <cobalt/kernel/shadow.h>
 #include <cobalt/kernel/arith.h>
-#include <asm/xenomai/thread.h>
 
 DEFINE_PER_CPU(struct xnsched, nksched);
 EXPORT_PER_CPU_SYMBOL_GPL(nksched);
@@ -646,8 +645,7 @@ static inline void switch_context(struct xnsched *sched,
 	xnlock_clear_irqon(&nklock);
 #endif /* !CONFIG_XENO_HW_UNLOCKED_SWITCH */
 
-	xnarch_switch_to(xnthread_archtcb(prev),
-			 xnthread_archtcb(next));
+	xnarch_switch_to(prev, next);
 }
 
 /**
@@ -746,7 +744,7 @@ static inline void leave_root(struct xnthread *root)
 #ifdef CONFIG_XENO_HW_UNLOCKED_SWITCH
 	rootcb->core.tip = task_thread_info(p);
 #endif
-	xnarch_leave_root(rootcb);
+	xnarch_leave_root(root);
 }
 
 void __xnsched_run_handler(void) /* hw interrupts off. */
kernel/cobalt/shadow.c +2 -4
index e5ad6e3..99ccee0 100644
@@ -57,9 +57,9 @@
 #include <cobalt/kernel/stat.h>
 #include <cobalt/kernel/ppd.h>
 #include <cobalt/kernel/vdso.h>
+#include <cobalt/kernel/thread.h>
 #include <asm/xenomai/features.h>
 #include <asm/xenomai/syscall.h>
-#include <asm/xenomai/thread.h>
 #include <asm-generic/xenomai/mayday.h>
 #include "debug.h"
 
@@ -2592,7 +2592,6 @@ int ipipe_kevent_hook(int kevent, void *data)
 static inline int handle_exception(struct ipipe_trap_data *d)
 {
 	struct xnthread *thread;
-	struct xnarchtcb *tcb;
 
 	if (xnsched_root_p())
 		return 0;
@@ -2608,8 +2607,7 @@ static inline int handle_exception(struct ipipe_trap_data *d)
 	if (xnarch_fault_fpu_p(d)) {
 		if (!xnthread_test_state(thread, XNROOT)) {
 			/* FPU exception received in primary mode. */
-			tcb = xnthread_archtcb(thread);
-			if (xnarch_handle_fpu_fault(tcb))
+			if (xnarch_handle_fpu_fault(thread))
 				return 1;
 		}
 		print_symbol("invalid use of FPU in Xenomai context at %s\n",
kernel/cobalt/thread.c +7 -9
index c5825ef..09d61e2 100644
@@ -36,7 +36,6 @@
 #include <cobalt/kernel/shadow.h>
 #include <cobalt/kernel/lock.h>
 #include <cobalt/kernel/thread.h>
-#include <asm/xenomai/thread.h>
 
 /**
  * @ingroup nucleus
@@ -225,7 +224,7 @@ void xnthread_init_shadow_tcb(struct xnthread *thread, struct task_struct *task)
 #ifdef CONFIG_XENO_HW_FPU
 	tcb->core.user_fpu_owner = task;
 #endif /* CONFIG_XENO_HW_FPU */
-	xnarch_init_shadow_tcb(tcb);
+	xnarch_init_shadow_tcb(thread);
 }
 
 void xnthread_init_root_tcb(struct xnthread *thread)
@@ -239,7 +238,7 @@ void xnthread_init_root_tcb(struct xnthread *thread)
 #ifdef CONFIG_XENO_HW_UNLOCKED_SWITCH
 	tcb->core.tip = &tcb->core.ti;
 #endif
-	xnarch_init_root_tcb(tcb);
+	xnarch_init_root_tcb(thread);
 }
 
 void xnthread_deregister(struct xnthread *thread)
@@ -380,7 +379,7 @@ static inline void release_fpu(struct xnthread *thread)
 	 * area of the migrated thread.
 	 */
 	if (xnthread_test_state(thread, XNFPU)) {
-		xnarch_save_fpu(xnthread_archtcb(thread));
+		xnarch_save_fpu(thread);
 		thread->sched->fpuholder = NULL;
 	}
 }
@@ -397,16 +396,15 @@ void xnthread_switch_fpu(struct xnsched *sched)
 		    xnarch_fpu_ptr(xnthread_archtcb(sched->fpuholder)) !=
 		    xnarch_fpu_ptr(xnthread_archtcb(curr))) {
 			if (sched->fpuholder)
-				xnarch_save_fpu(xnthread_archtcb
-						(sched->fpuholder));
+				xnarch_save_fpu(sched->fpuholder);
 
-			xnarch_restore_fpu(xnthread_archtcb(curr));
+			xnarch_restore_fpu(curr);
 		} else
-			xnarch_enable_fpu(xnthread_archtcb(curr));
+			xnarch_enable_fpu(curr);
 
 		sched->fpuholder = curr;
 	} else
-		xnarch_enable_fpu(xnthread_archtcb(curr));
+		xnarch_enable_fpu(curr);
 }
 
 #else /* !CONFIG_XENO_HW_FPU */
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 3/7 cobalt/fpu: replace the is_root member with XNROOT tests

In reply to Gilles Chanteperdrix
---
 kernel/cobalt/arch/arm/include/asm/xenomai/thread.h |    4 ----
 kernel/cobalt/arch/arm/thread.c                     |    4 ++--
 kernel/cobalt/arch/x86/include/asm/xenomai/thread.h |    1 -
 kernel/cobalt/arch/x86/thread.c                     |    6 +++---
 4 files changed, 5 insertions(+), 10 deletions(-)
kernel/cobalt/arch/arm/include/asm/xenomai/thread.h +0 -4
index fc79fc8..346e329 100644
@@ -51,9 +51,7 @@ struct arm_fpustate {
 struct xnarchtcb {
 	struct xntcb core;
 #ifdef CONFIG_XENO_HW_FPU
-	struct arm_fpustate fpuenv;
 	struct arm_fpustate *fpup;
-	unsigned int is_root;
 #define xnarch_fpu_ptr(tcb)     ((tcb)->fpup)
 #else
 #define xnarch_fpu_ptr(tcb)     NULL
@@ -98,14 +96,12 @@ int xnarch_escalate(void);
 static inline void xnarch_init_root_tcb(struct xnthread *thread)
 {
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
-	tcb->is_root = 1;
 	tcb->fpup = NULL;
 }
 
 static inline void xnarch_init_shadow_tcb(struct xnthread *thread)
 {
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
-	tcb->is_root = 0;
 	tcb->fpup = (struct arm_fpustate *)
 		&task_thread_info(tcb->core.host_task)->used_cp[0];
 }
kernel/cobalt/arch/arm/thread.c +2 -2
index d77e59a..29a58fe 100644
@@ -294,7 +294,7 @@ void xnarch_enable_fpu(struct xnthread *thread)
 	   newly switched thread uses the FPU, to allow the kernel handler to
 	   pick the correct FPU context.
 	*/
-	if (likely(!tcb->is_root)) {
+	if (likely(!xnthread_test_state(thread, XNROOT))) {
 		do_enable_fpu();
 		/* No exception should be pending, since it should have caused
 		   a trap earlier.
@@ -355,7 +355,7 @@ void xnarch_restore_fpu(struct xnthread *thread)
 	struct xnarchtcb *tcb = &thread->tcb;
 #ifdef CONFIG_XENO_HW_FPU
 #ifdef CONFIG_VFP
-	if (likely(!tcb->is_root)) {
+	if (likely(!xnthread_test_state(thread, XNROOT))) {
 		do_enable_fpu();
 		do_restore_fpu(tcb->fpup);
 	} else {
kernel/cobalt/arch/x86/include/asm/xenomai/thread.h +0 -1
index ee0f385..570d9b5 100644
@@ -35,7 +35,6 @@ struct xnarchtcb {
 	unsigned long ip;
 	unsigned long *ipp;
 	x86_fpustate *fpup;
-	unsigned int is_root: 1;
 	unsigned int root_kfpu: 1;
 	unsigned int root_used_math: 1;
 	struct {
kernel/cobalt/arch/x86/thread.c +3 -3
index 0c24e02..4ff699a 100644
@@ -287,7 +287,8 @@ void xnarch_restore_fpu(struct xnthread *thread)
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct task_struct *p = tcb->core.host_task;
 
-	if (tcb->root_kfpu == 0 && (tsk_used_math(p) == 0 || tcb->is_root))
+	if (tcb->root_kfpu == 0 && 
+		(tsk_used_math(p) == 0 || xnthread_test_state(thread, XNROOT)))
 		/* Restore lazy mode */
 		return;
 
@@ -320,15 +321,14 @@ void xnarch_init_root_tcb(struct xnthread *thread)
 	tcb->sp = 0;
 	tcb->spp = &tcb->sp;
 	tcb->ipp = &tcb->ip;
-	tcb->is_root = 1;
 	tcb->fpup = NULL;
 	tcb->root_kfpu = 0;
 }
 
 void xnarch_init_shadow_tcb(struct xnthread *thread)
 {
-	struct task_struct *p = tcb->core.host_task;
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
+	struct task_struct *p = tcb->core.host_task;
 
 	tcb->sp = 0;
 	tcb->spp = &p->thread.sp;
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH 4/7 cobalt/fpu: dynamic XNFPU

In reply to Gilles Chanteperdrix

Instead of systematically creating user-space tasks with XNFPU enabled, let the architectures decide when they want to enable it. In case they arm it upon FPU fault, set sched->fpuholder if the fault is handled in primary mode.

---
 kernel/cobalt/arch/arm/include/asm/xenomai/thread.h |    7 +------
 kernel/cobalt/arch/arm/thread.c                     |   10 ++++++++++
 kernel/cobalt/arch/sh/include/asm/xenomai/thread.h  |    6 +-----
 kernel/cobalt/arch/sh/thread.c                      |   10 ++++++++++
 kernel/cobalt/arch/x86/thread.c                     |    3 +++
 kernel/cobalt/posix/thread.c                        |    2 +-
 kernel/cobalt/shadow.c                              |   18 +++++++++---------
 7 files changed, 35 insertions(+), 21 deletions(-)
kernel/cobalt/arch/arm/include/asm/xenomai/thread.h +1 -6
index 346e329..8b54b97 100644
@@ -99,12 +99,7 @@ static inline void xnarch_init_root_tcb(struct xnthread *thread)
 	tcb->fpup = NULL;
 }
 
-static inline void xnarch_init_shadow_tcb(struct xnthread *thread)
-{
-	struct xnarchtcb *tcb = xnthread_archtcb(thread);
-	tcb->fpup = (struct arm_fpustate *)
-		&task_thread_info(tcb->core.host_task)->used_cp[0];
-}
+void xnarch_init_shadow_tcb(struct xnthread *thread);
 
 int xnarch_fault_fpu_p(struct ipipe_trap_data *d);
 
kernel/cobalt/arch/arm/thread.c +10 -0
index 29a58fe..5f0d792 100644
@@ -402,3 +402,13 @@ int xnarch_escalate(void)
 
 	return 0;
 }
+
+void xnarch_init_shadow_tcb(struct xnthread *thread)
+{
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
+	tcb->fpup = (struct arm_fpustate *)
+		&task_thread_info(tcb->core.host_task)->used_cp[0];
+
+	if (xnthread_test_state(thread, XNUSER))
+		xnthread_set_state(thread, XNFPU);
+}
kernel/cobalt/arch/sh/include/asm/xenomai/thread.h +1 -5
index 6bf7329..2c7fe88 100644
@@ -60,11 +60,7 @@ static inline void xnarch_init_root_tcb(struct xnthread *thread)
 	tcb->fpup = NULL;
 }
 
-static inline void xnarch_init_shadow_tcb(struct xnthread *thread)
-{
-	struct xnarchtcb *tcb = xnthread_archtcb(thread);
-	tcb->fpup = &tcb->core.host_task->thread;
-}
+void xnarch_init_shadow_tcb(struct xnthread *thread);
 
 #else /* !CONFIG_XENO_HW_FPU */
 
kernel/cobalt/arch/sh/thread.c +10 -0
index 4c8b532..4b2361e 100644
@@ -292,3 +292,13 @@ int xnarch_escalate(void)
 
 	return 0;
 }
+
+void xnarch_init_shadow_tcb(struct xnthread *thread)
+{
+	struct xnarchtcb *tcb = xnthread_archtcb(thread);
+	tcb->fpup = &tcb->core.host_task->thread;
+
+	if (xnthread_test_state(thread, XNUSER))
+		xnthread_set_state(thread, XNFPU);
+}
+
kernel/cobalt/arch/x86/thread.c +3 -0
index 4ff699a..4e80495 100644
@@ -339,6 +339,9 @@ void xnarch_init_shadow_tcb(struct xnthread *thread)
 #endif
 	tcb->fpup = x86_fpustate_ptr(&p->thread);
 	tcb->root_kfpu = 0;
+
+	if (xnthread_test_state(thread, XNUSER))
+		xnthread_set_state(thread, XNFPU);
 }
 
 int xnarch_escalate(void)
kernel/cobalt/posix/thread.c +1 -1
index 7ba91fc..df5949a 100644
@@ -437,7 +437,7 @@ static inline int pthread_create(struct cobalt_thread **thread_p,
 	}
 
 	iattr.name = thread->attr.name;
-	iattr.flags = XNUSER|XNFPU;
+	iattr.flags = XNUSER;
 	iattr.personality = &cobalt_personality;
 	iattr.affinity = CPU_MASK_ALL;
 
kernel/cobalt/shadow.c +9 -9
index 99ccee0..c858777 100644
@@ -2591,12 +2591,14 @@ int ipipe_kevent_hook(int kevent, void *data)
 
 static inline int handle_exception(struct ipipe_trap_data *d)
 {
+	struct xnsched *sched;
 	struct xnthread *thread;
 
-	if (xnsched_root_p())
-		return 0;
+	sched = xnsched_current();
+	thread = sched->curr;
 
-	thread = xnsched_current_thread();
+	if (xnthread_test_state(thread, XNROOT))
+		return 0;
 
 	trace_mark(xn_nucleus, thread_fault,
 		   "thread %p thread_name %s ip %p type 0x%x",
@@ -2605,17 +2607,15 @@ static inline int handle_exception(struct ipipe_trap_data *d)
 		   xnarch_fault_trap(d));
 
 	if (xnarch_fault_fpu_p(d)) {
-		if (!xnthread_test_state(thread, XNROOT)) {
-			/* FPU exception received in primary mode. */
-			if (xnarch_handle_fpu_fault(thread))
-				return 1;
+		/* FPU exception received in primary mode. */
+		if (xnarch_handle_fpu_fault(thread)) {
+			sched->fpuholder = thread;
+			return 1;
 		}
 		print_symbol("invalid use of FPU in Xenomai context at %s\n",
 			     xnarch_fault_pc(d));
 	}
 
-	if (xnthread_test_state(thread, XNROOT))
-		return 0;
 	/*
 	 * If we experienced a trap on behalf of a shadow thread
 	 * running in primary mode, move it to the Linux domain,
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 5/7 cobalt/fpu: change xnarch_handle_fpu_fault interface

In reply to Gilles Chanteperdrix

Pass the current FPU holder to allow enabling XNFPU dynamically. Also pass the faults information for architectures which need to fiddle with the PC register to restart the faulting instruction.

---
 kernel/cobalt/arch/arm/include/asm/xenomai/thread.h      |    4 +++-
 kernel/cobalt/arch/blackfin/include/asm/xenomai/thread.h |    4 +++-
 kernel/cobalt/arch/nios2/include/asm/xenomai/thread.h    |    4 +++-
 kernel/cobalt/arch/powerpc/include/asm/xenomai/thread.h  |    4 +++-
 kernel/cobalt/arch/sh/include/asm/xenomai/thread.h       |    4 +++-
 kernel/cobalt/arch/x86/include/asm/xenomai/thread.h      |    7 +++++--
 kernel/cobalt/arch/x86/thread.c                          |    3 ++-
 kernel/cobalt/shadow.c                                   |    2 +-
 8 files changed, 23 insertions(+), 9 deletions(-)
kernel/cobalt/arch/arm/include/asm/xenomai/thread.h +3 -1
index 8b54b97..8c42775 100644
@@ -124,7 +124,9 @@ static inline void xnarch_leave_root(struct xnthread *root) { }
 
 #endif /* !CONFIG_XENO_HW_FPU */
 
-static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
+static inline int 
+xnarch_handle_fpu_fault(struct xnthread *from, 
+			struct xnthread *to, struct ipipe_trap_data *d)
 {
 	return 0;
 }
kernel/cobalt/arch/blackfin/include/asm/xenomai/thread.h +3 -1
index 601fc4b..6c630e7 100644
@@ -62,7 +62,9 @@ static inline void xnarch_enable_fpu(struct xnthread *current_thread) { }
 static inline void xnarch_save_fpu(struct xnthread *thread) { }
 static inline void xnarch_restore_fpu(struct xnthread *thread) { }
 
-static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
+static inline int 
+xnarch_handle_fpu_fault(struct xnthread *from, 
+			struct xnthread *to, struct ipipe_trap_data *d)
 {
 	return 0;
 }
kernel/cobalt/arch/nios2/include/asm/xenomai/thread.h +3 -1
index 775b92a..05de3c2 100644
@@ -57,7 +57,9 @@ static inline void xnarch_enable_fpu(struct xnthread *thread) { }
 static inline void xnarch_save_fpu(struct xnthread *thread) { }
 static inline void xnarch_restore_fpu(struct xnthread *thread) { }
 
-static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
+static inline int 
+xnarch_handle_fpu_fault(struct xnthread *from, 
+			struct xnthread *to, struct ipipe_trap_data *d)
 {
 	return 0;
 }
kernel/cobalt/arch/powerpc/include/asm/xenomai/thread.h +3 -1
index 07a17cf..7504fba 100644
@@ -77,7 +77,9 @@ static inline void xnarch_init_shadow_tcb(struct xnthread *thread) { }
 
 #endif  /* !CONFIG_XENO_HW_FPU */
 
-static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
+static inline int 
+xnarch_handle_fpu_fault(struct xnthread *from, 
+			struct xnthread *to, struct ipipe_trap_data *d)
 {
 	return 0;
 }
kernel/cobalt/arch/sh/include/asm/xenomai/thread.h +3 -1
index 2c7fe88..9a78124 100644
@@ -70,7 +70,9 @@ static inline void xnarch_init_shadow_tcb(struct xnthread *thread) { }
 
 #endif /* !CONFIG_XENO_HW_FPU */
 
-static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
+static inline int 
+xnarch_handle_fpu_fault(struct xnthread *from, 
+			struct xnthread *to, struct ipipe_trap_data *d)
 {
 	return 0;
 }
kernel/cobalt/arch/x86/include/asm/xenomai/thread.h +5 -2
index 570d9b5..938b25d 100644
@@ -69,7 +69,8 @@ static inline int xnarch_shadow_p(struct xnarchtcb *tcb, struct task_struct *tas
 void xnarch_save_fpu(struct xnthread *thread);
 void xnarch_restore_fpu(struct xnthread *thread);
 void xnarch_enable_fpu(struct xnthread *thread);
-int xnarch_handle_fpu_fault(struct xnthread *thread);
+int xnarch_handle_fpu_fault(struct xnthread *from, 
+			struct xnthread *to, struct ipipe_trap_data *d);
 
 #else /* !CONFIG_XENO_HW_FPU */
 
@@ -77,7 +78,9 @@ static inline void xnarch_save_fpu(struct xnthread *thread) { }
 static inline void xnarch_restore_fpu(struct xnthread *thread) { }
 static inline void xnarch_enable_fpu(struct xnthread *thread) { }
 
-static inline int xnarch_handle_fpu_fault(struct xnthread *thread)
+static inline int
+xnarch_handle_fpu_fault(struct xnthread *from, 
+			struct xnthread *to, struct ipipe_trap_data *d)
 {
 	return 0;
 }
kernel/cobalt/arch/x86/thread.c +2 -1
index 4e80495..cbf3627 100644
@@ -219,7 +219,8 @@ static inline void __do_restore_i387(x86_fpustate *fpup)
 #endif /* CONFIG_X86_64 */
 }
 
-int xnarch_handle_fpu_fault(struct xnthread *thread)
+int xnarch_handle_fpu_fault(struct xnthread *from, 
+			struct xnthread *to, struct ipipe_trap_data *d)
 {
 	struct xnarchtcb *tcb = xnthread_archtcb(to);
 	struct task_struct *p = tcb->core.host_task;
kernel/cobalt/shadow.c +1 -1
index c858777..74e73d5 100644
@@ -2608,7 +2608,7 @@ static inline int handle_exception(struct ipipe_trap_data *d)
 
 	if (xnarch_fault_fpu_p(d)) {
 		/* FPU exception received in primary mode. */
-		if (xnarch_handle_fpu_fault(thread)) {
+		if (xnarch_handle_fpu_fault(sched->fpuholder, thread, d)) {
 			sched->fpuholder = thread;
 			return 1;
 		}
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH 6/7 cobalt/fpu: change fpu handling interface

In reply to Gilles Chanteperdrix

For different reasons on x86 and ARM, implementing FPU switching in terms of xnarch_enable_fpu/xnarch_save_fpu/xnarch_restore_fpu no longer really makes sense. So, keep xnarch_save_fpu for migration, but replace xnarch_enable_fpu and xnarch_restore_fpu with xnarch_switch_fpu(from, to).

A default implementation for xnarch_switch_fpu is provided which switches fpu in terms of xnarch_save_fpu/xnarch_restore_fpu/xnarch_enable_fpu on architectures where it still makes sense.

---
 .../cobalt/arch/arm/include/asm/xenomai/thread.h   |    4 +---
 kernel/cobalt/arch/arm/thread.c                    |   16 ++++++++++++++++
 .../arch/blackfin/include/asm/xenomai/thread.h     |    7 +++++--
 .../cobalt/arch/nios2/include/asm/xenomai/thread.h |    7 +++++--
 .../arch/powerpc/include/asm/xenomai/thread.h      |    4 +---
 kernel/cobalt/arch/powerpc/thread.c                |   20 ++++++++++++++++++--
 kernel/cobalt/arch/sh/thread.c                     |   20 ++++++++++++++++++--
 .../cobalt/arch/x86/include/asm/xenomai/thread.h   |   10 ++++++----
 kernel/cobalt/arch/x86/thread.c                    |    9 +++------
 kernel/cobalt/thread.c                             |   14 +-------------
 10 files changed, 74 insertions(+), 37 deletions(-)
kernel/cobalt/arch/arm/include/asm/xenomai/thread.h +1 -3
index 8c42775..e780212 100644
@@ -79,11 +79,9 @@ struct xnarchtcb {
 
 #define xnarch_fault_notify(d) (!xnarch_fault_bp_p(d))
 
-void xnarch_enable_fpu(struct xnthread *current_thread);
-
 void xnarch_save_fpu(struct xnthread *thread);
 
-void xnarch_restore_fpu(struct xnthread *thread);
+int xnarch_switch_fpu(struct xnthread *from, struct xnthread *thread);
 
 void xnarch_switch_to(struct xnthread *out, struct xnthread *in);
 
kernel/cobalt/arch/arm/thread.c +16 -0
index 5f0d792..5775d2c 100644
@@ -393,6 +393,22 @@ void xnarch_restore_fpu(struct xnthread *thread)
 #endif /* CONFIG_XENO_HW_FPU */
 }
 
+int xnarch_switch_fpu(struct xnthread *from, struct xnthread *to)
+{
+	if (from == to || 
+		xnarch_fpu_ptr(xnthread_archtcb(from)) == 
+		xnarch_fpu_ptr(xnthread_archtcb(to))) {
+		xnarch_enable_fpu(to);
+		return 1;
+	}
+	
+	if (from)
+		xnarch_save_fpu(from);
+	
+	xnarch_restore_fpu(to);
+	return 1;
+}
+
 int xnarch_escalate(void)
 {
 	if (ipipe_root_p) {
kernel/cobalt/arch/blackfin/include/asm/xenomai/thread.h +5 -2
index 6c630e7..b4c5637 100644
@@ -58,9 +58,12 @@ static inline void xnarch_init_root_tcb(struct xnthread *thread) { }
 static inline void xnarch_init_shadow_tcb(struct xnthread *thread) { }
 static inline void xnarch_enter_root(struct xnthread *root) { }
 static inline void xnarch_leave_root(struct xnthread *root) { }
-static inline void xnarch_enable_fpu(struct xnthread *current_thread) { }
 static inline void xnarch_save_fpu(struct xnthread *thread) { }
-static inline void xnarch_restore_fpu(struct xnthread *thread) { }
+static inline int
+xnarch_switch_fpu(struct xnthread *from, struct xnthread *thread)
+{ 
+	return 0;
+}
 
 static inline int 
 xnarch_handle_fpu_fault(struct xnthread *from, 
kernel/cobalt/arch/nios2/include/asm/xenomai/thread.h +5 -2
index 05de3c2..0544ff7 100644
@@ -53,9 +53,12 @@ static inline void xnarch_init_root_tcb(struct xnthread *thread) { }
 static inline void xnarch_init_shadow_tcb(struct xnthread *thread) { }
 static inline void xnarch_enter_root(struct xnthread *root) { }
 static inline void xnarch_leave_root(struct xnthread *thread) { }
-static inline void xnarch_enable_fpu(struct xnthread *thread) { }
 static inline void xnarch_save_fpu(struct xnthread *thread) { }
-static inline void xnarch_restore_fpu(struct xnthread *thread) { }
+static inline int
+xnarch_switch_fpu(struct xnthread *from, struct xnthread *to) 
+{ 
+	return 0;
+}
 
 static inline int 
 xnarch_handle_fpu_fault(struct xnthread *from, 
kernel/cobalt/arch/powerpc/include/asm/xenomai/thread.h +1 -3
index 7504fba..1ee1a79 100644
@@ -88,10 +88,8 @@ void xnarch_switch_to(struct xnthread *out, struct xnthread *in);
 
 int xnarch_escalate(void);
 
-void xnarch_enable_fpu(struct xnthread *current_thread);
-
 void xnarch_save_fpu(struct xnthread *thread);
 
-void xnarch_restore_fpu(struct xnthread *thread);
+int xnarch_switch_fpu(struct xnthread *from, struct xnthread *thread);
 
 #endif /* !_COBALT_POWERPC_ASM_THREAD_H */
kernel/cobalt/arch/powerpc/thread.c +18 -2
index 70aad0e..fd5fd7a 100644
@@ -115,7 +115,7 @@ asmlinkage void __asm_restore_fpu(struct thread_struct *ts);
 })
 #endif /* CONFIG_PPC64 */
 
-void xnarch_enable_fpu(struct xnthread *thread)
+static void xnarch_enable_fpu(struct xnthread *thread)
 {
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct task_struct *task = tcb->core.host_task;
@@ -139,7 +139,7 @@ void xnarch_save_fpu(struct xnthread *thread)
 	}
 }
 
-void xnarch_restore_fpu(struct xnthread *thread)
+static void xnarch_restore_fpu(struct xnthread *thread)
 {
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct thread_struct *ts;
@@ -168,6 +168,22 @@ void xnarch_restore_fpu(struct xnthread *thread)
 		do_disable_fpu();
 }
 
+int xnarch_switch_fpu(struct xnthread *from, struct xnthread *to)
+{
+	if (from == to || 
+		xnarch_fpu_ptr(xnthread_archtcb(from)) == 
+		xnarch_fpu_ptr(xnthread_archtcb(to))) {
+		xnarch_enable_fpu(to);
+		return 1;
+	}
+	
+	if (from)
+		xnarch_save_fpu(from);
+	
+	xnarch_restore_fpu(to);
+	return 1;
+}
+
 void xnarch_leave_root(struct xnthread *root)
 {
 	struct xnarchtcb *rootcb = xnthread_archtcb(root);
kernel/cobalt/arch/sh/thread.c +18 -2
index 4b2361e..10a78a0 100644
@@ -227,7 +227,7 @@ static inline void do_restore_fpu(struct thread_struct *ts)
 		      :"memory");
 }
 
-inline void xnarch_enable_fpu(struct xnthread *thread)
+static inline void xnarch_enable_fpu(struct xnthread *thread)
 {
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct task_struct *task = tcb->core.host_task;
@@ -252,7 +252,7 @@ void xnarch_save_fpu(struct xnthread *thread)
 	}
 }
 
-void xnarch_restore_fpu(struct xnthread *thread)
+static void xnarch_restore_fpu(struct xnthread *thread)
 {
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct pt_regs *regs;
@@ -273,6 +273,22 @@ void xnarch_restore_fpu(struct xnthread *thread)
 		disable_fpu();
 }
 
+int xnarch_switch_fpu(struct xnthread *from, struct xnthread *to)
+{
+	if (from == to || 
+		xnarch_fpu_ptr(xnthread_archtcb(from)) == 
+		xnarch_fpu_ptr(xnthread_archtcb(to))) {
+		xnarch_enable_fpu(to);
+		return 1;
+	}
+	
+	if (from)
+		xnarch_save_fpu(from);
+	
+	xnarch_restore_fpu(to);
+	return 1;
+}
+
 void xnarch_leave_root(struct xnthread *root)
 {
 	struct xnarchtcb *rootcb = xnthread_archtcb(root);
kernel/cobalt/arch/x86/include/asm/xenomai/thread.h +6 -4
index 938b25d..e95774a 100644
@@ -67,16 +67,18 @@ static inline int xnarch_shadow_p(struct xnarchtcb *tcb, struct task_struct *tas
 #ifdef CONFIG_XENO_HW_FPU
 
 void xnarch_save_fpu(struct xnthread *thread);
-void xnarch_restore_fpu(struct xnthread *thread);
-void xnarch_enable_fpu(struct xnthread *thread);
+int xnarch_switch_fpu(struct xnthread *from, struct xnthread *to);
 int xnarch_handle_fpu_fault(struct xnthread *from, 
 			struct xnthread *to, struct ipipe_trap_data *d);
 
 #else /* !CONFIG_XENO_HW_FPU */
 
 static inline void xnarch_save_fpu(struct xnthread *thread) { }
-static inline void xnarch_restore_fpu(struct xnthread *thread) { }
-static inline void xnarch_enable_fpu(struct xnthread *thread) { }
+static inline int 
+xnarch_switch_fpu(struct xnthread *from, struct xnthread *thread) 
+{ 
+	return 0;
+}
 
 static inline int
 xnarch_handle_fpu_fault(struct xnthread *from, 
kernel/cobalt/arch/x86/thread.c +3 -6
index cbf3627..2cb5ff1 100644
@@ -283,7 +283,7 @@ void xnarch_save_fpu(struct xnthread *thread)
 	wrap_clear_fpu_used(p);
 }
 
-void xnarch_restore_fpu(struct xnthread *thread)
+int xnarch_switch_fpu(struct xnthread *from, struct xnthread *thread)
 {
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct task_struct *p = tcb->core.host_task;
@@ -291,7 +291,7 @@ void xnarch_restore_fpu(struct xnthread *thread)
 	if (tcb->root_kfpu == 0 && 
 		(tsk_used_math(p) == 0 || xnthread_test_state(thread, XNROOT)))
 		/* Restore lazy mode */
-		return;
+		return xnthread_test_state(thread, XNROOT);
 
 	/*
 	 * Restore the FPU hardware with valid fp registers from a
@@ -307,11 +307,8 @@ void xnarch_restore_fpu(struct xnthread *thread)
 			clear_stopped_child_used_math(p);
 	} else
 		wrap_set_fpu_used(p);
-}
 
-void xnarch_enable_fpu(struct xnthread *thread)
-{
-	xnarch_restore_fpu(thread);
+	return 1;
 }
 
 #endif /* CONFIG_XENO_HW_FPU */
kernel/cobalt/thread.c +1 -13
index 09d61e2..0c59839 100644
@@ -391,20 +391,8 @@ void xnthread_switch_fpu(struct xnsched *sched)
 	if (!xnthread_test_state(curr, XNFPU))
 		return;
 
-	if (sched->fpuholder != curr) {
-		if (sched->fpuholder == NULL ||
-		    xnarch_fpu_ptr(xnthread_archtcb(sched->fpuholder)) !=
-		    xnarch_fpu_ptr(xnthread_archtcb(curr))) {
-			if (sched->fpuholder)
-				xnarch_save_fpu(sched->fpuholder);
-
-			xnarch_restore_fpu(curr);
-		} else
-			xnarch_enable_fpu(curr);
-
+	if (xnarch_switch_fpu(sched->fpuholder, curr))
 		sched->fpuholder = curr;
-	} else
-		xnarch_enable_fpu(curr);
 }
 
 #else /* !CONFIG_XENO_HW_FPU */
Signature · 1 line
1.7.10.4
#7 lore raw

PATCH 7/7 cobalt/x86: lazy FPU switching

In reply to Gilles Chanteperdrix

Only enable XNFPU on first FPU use in kernel-space. Skip context restore with fxsr when context allows it.

---
 kernel/cobalt/arch/x86/thread.c |   33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
kernel/cobalt/arch/x86/thread.c +24 -9
index 2cb5ff1..aa35eb6 100644
@@ -225,7 +225,7 @@ int xnarch_handle_fpu_fault(struct xnthread *from,
 	struct xnarchtcb *tcb = xnthread_archtcb(to);
 	struct task_struct *p = tcb->core.host_task;
 
-	if (tsk_used_math(p))
+	if (wrap_test_fpu_used(p))
 		return 0;
 
 	/*
@@ -243,6 +243,7 @@ int xnarch_handle_fpu_fault(struct xnthread *from,
 
 	set_stopped_child_used_math(p);
 	wrap_set_fpu_used(p);
+	xnthread_set_state(to, XNFPU);
 
 	return 1;
 }
@@ -273,7 +274,7 @@ void xnarch_save_fpu(struct xnthread *thread)
 	struct task_struct *p = tcb->core.host_task;
 
 	if (wrap_test_fpu_used(p) == 0)
-		/* Common case: already saved by __switch_to */
+		/* Saved by last __switch_to */
 		return;
 	
 	/* Exceptional case: a migrating thread */
@@ -287,11 +288,28 @@ int xnarch_switch_fpu(struct xnthread *from, struct xnthread *thread)
 {
 	struct xnarchtcb *tcb = xnthread_archtcb(thread);
 	struct task_struct *p = tcb->core.host_task;
+	const unsigned root_kfpu = tcb->root_kfpu;
 
-	if (tcb->root_kfpu == 0 && 
-		(tsk_used_math(p) == 0 || xnthread_test_state(thread, XNROOT)))
+	if (root_kfpu == 0) {
 		/* Restore lazy mode */
-		return xnthread_test_state(thread, XNROOT);
+		if (tsk_used_math(p) == 0)
+			return 0;
+		if (xnthread_test_state(thread, XNROOT))
+			return 1;
+
+		/*
+		 * If we never switched fpu, and the cpu has fxsr, the
+		 * last context switch saved fpu but did not
+		 * reinitialize it, so we can simply reenable fpu
+		 */
+		if (cpu_has_fxsr &&
+			(from == thread || 
+				(from && from->tcb.fpup == thread->tcb.fpup))) {
+			clts();
+			wrap_set_fpu_used(p);
+			return thread != from;
+		}
+	}
 
 	/*
 	 * Restore the FPU hardware with valid fp registers from a
@@ -300,7 +318,7 @@ int xnarch_switch_fpu(struct xnthread *from, struct xnthread *thread)
 	clts();
 
 	__do_restore_i387(x86_fpustate_ptr(&p->thread));
-	if (tcb->root_kfpu) {
+	if (root_kfpu) {
 		x86_fpustate_ptr(&p->thread) = tcb->fpup;
 		wrap_clear_fpu_used(p);
 		if (tcb->root_used_math == 0)
@@ -337,9 +355,6 @@ void xnarch_init_shadow_tcb(struct xnthread *thread)
 #endif
 	tcb->fpup = x86_fpustate_ptr(&p->thread);
 	tcb->root_kfpu = 0;
-
-	if (xnthread_test_state(thread, XNUSER))
-		xnthread_set_state(thread, XNFPU);
 }
 
 int xnarch_escalate(void)
Signature · 1 line
1.7.10.4