Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src-draft/trunk]: src/sys/arch/x86/x86 Add kthread_fpu_enter/exit support to...



details:   https://anonhg.NetBSD.org/src-all/rev/ea7314ad86ed
branches:  trunk
changeset: 936595:ea7314ad86ed
user:      Taylor R Campbell <riastradh%NetBSD.org@localhost>
date:      Fri Jul 31 03:14:59 2020 +0000

description:
Add kthread_fpu_enter/exit support to x86.

diffstat:

 sys/arch/x86/x86/fpu.c |  67 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 60 insertions(+), 7 deletions(-)

diffs (144 lines):

diff -r 88114cdf465e -r ea7314ad86ed sys/arch/x86/x86/fpu.c
--- a/sys/arch/x86/x86/fpu.c    Fri Jul 31 03:11:19 2020 +0000
+++ b/sys/arch/x86/x86/fpu.c    Fri Jul 31 03:14:59 2020 +0000
@@ -107,6 +107,7 @@
 #include <sys/file.h>
 #include <sys/proc.h>
 #include <sys/kernel.h>
+#include <sys/kthread.h>
 #include <sys/sysctl.h>
 #include <sys/xcall.h>
 
@@ -131,13 +132,35 @@
 
 uint32_t x86_fpu_mxcsr_mask __read_mostly = 0;
 
+/*
+ * True if this a thread that is allowed to use the FPU -- either a
+ * user thread, or a system thread with LW_SYSTEM_FPU enabled.
+ */
+static inline bool
+lwp_can_haz_fpu(struct lwp *l)
+{
+
+       return (l->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) != LW_SYSTEM;
+}
+
+/*
+ * True if this is a system thread with its own private FPU state.
+ */
+static inline bool
+lwp_system_fpu_p(struct lwp *l)
+{
+
+       return (l->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) ==
+           (LW_SYSTEM|LW_SYSTEM_FPU);
+}
+
 static inline union savefpu *
 fpu_lwp_area(struct lwp *l)
 {
        struct pcb *pcb = lwp_getpcb(l);
        union savefpu *area = &pcb->pcb_savefpu;
 
-       KASSERT((l->l_flag & LW_SYSTEM) == 0);
+       KASSERT(lwp_can_haz_fpu(l));
        if (l == curlwp) {
                fpu_save();
        }
@@ -155,7 +178,7 @@
 
        s = splvm();
        if (l->l_md.md_flags & MDL_FPU_IN_CPU) {
-               KASSERT((l->l_flag & LW_SYSTEM) == 0);
+               KASSERT(lwp_can_haz_fpu(l));
                fpu_area_save(area, x86_xsave_features);
                l->l_md.md_flags &= ~MDL_FPU_IN_CPU;
        }
@@ -307,7 +330,7 @@
            cpu_index(ci), ci->ci_ilevel);
 
        if (oldlwp->l_md.md_flags & MDL_FPU_IN_CPU) {
-               KASSERT(!(oldlwp->l_flag & LW_SYSTEM));
+               KASSERT(lwp_can_haz_fpu(oldlwp));
                pcb = lwp_getpcb(oldlwp);
                fpu_area_save(&pcb->pcb_savefpu, x86_xsave_features);
                oldlwp->l_md.md_flags &= ~MDL_FPU_IN_CPU;
@@ -322,11 +345,11 @@
        union savefpu *fpu_save;
 
        /* Kernel threads have no FPU. */
-       if (__predict_false(l2->l_flag & LW_SYSTEM)) {
+       if (__predict_false(!lwp_can_haz_fpu(l2))) {
                return;
        }
        /* For init(8). */
-       if (__predict_false(l1->l_flag & LW_SYSTEM)) {
+       if (__predict_false(!lwp_can_haz_fpu(l1))) {
                memset(&pcb2->pcb_savefpu, 0, x86_fpu_save_size);
                return;
        }
@@ -350,6 +373,8 @@
 
 /* -------------------------------------------------------------------------- */
 
+static const union savefpu zero_fpu __aligned(64);
+
 /*
  * fpu_kern_enter()
  *
@@ -369,6 +394,11 @@
        struct cpu_info *ci;
        int s;
 
+       if (lwp_system_fpu_p(l) && !cpu_intr_p()) {
+               KASSERT(!cpu_softintr_p());
+               return;
+       }
+
        s = splvm();
 
        ci = curcpu();
@@ -401,10 +431,16 @@
 void
 fpu_kern_leave(void)
 {
-       static const union savefpu zero_fpu __aligned(64);
-       struct cpu_info *ci = curcpu();
+       struct cpu_info *ci;
        int s;
 
+       if (lwp_system_fpu_p(curlwp) && !cpu_intr_p()) {
+               KASSERT(!cpu_softintr_p());
+               return;
+       }
+
+       ci = curcpu();
+
        KASSERT(ci->ci_ilevel == IPL_VM);
        KASSERT(ci->ci_kfpu_spl != -1);
 
@@ -426,6 +462,23 @@
        splx(s);
 }
 
+void
+kthread_fpu_enter_md(void)
+{
+
+       /* Enable the FPU by clearing CR0_TS.  */
+       clts();
+}
+
+void
+kthread_fpu_exit_md(void)
+{
+
+       /* Zero the FPU state and disable the FPU by setting CR0_TS.  */
+       fpu_area_restore(&zero_fpu, x86_xsave_features);
+       stts();
+}
+
 /* -------------------------------------------------------------------------- */
 
 /*



Home | Main Index | Thread Index | Old Index