Source-Changes-HG archive

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

[src-draft/trunk]: src/sys/arch Draft fpu_kthread_enter/leave.



details:   https://anonhg.NetBSD.org/src-all/rev/992c91efc931
branches:  trunk
changeset: 934515:992c91efc931
user:      Taylor R Campbell <riastradh%NetBSD.org@localhost>
date:      Thu Jun 04 03:34:45 2020 +0000

description:
Draft fpu_kthread_enter/leave.

Only fit for kthreads, not user lwps.  Preemptible, nestable.

diffstat:

 sys/arch/amd64/include/proc.h |   1 +
 sys/arch/i386/include/proc.h  |   1 +
 sys/arch/x86/include/fpu.h    |   3 ++
 sys/arch/x86/x86/fpu.c        |  47 +++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 50 insertions(+), 2 deletions(-)

diffs (106 lines):

diff -r 7a19dc8988ca -r 992c91efc931 sys/arch/amd64/include/proc.h
--- a/sys/arch/amd64/include/proc.h     Sat Jun 13 16:43:32 2020 +0000
+++ b/sys/arch/amd64/include/proc.h     Thu Jun 04 03:34:45 2020 +0000
@@ -54,6 +54,7 @@
 #define        MDL_COMPAT32            0x0008  /* i386, always return via iret */
 #define        MDL_IRET                0x0010  /* force return via iret, not sysret */
 #define        MDL_FPU_IN_CPU          0x0020  /* the FPU state is in the CPU */
+#define        MDL_SYSTEM_FPU          0x0040  /* system thread is allowed FPU use */
 
 struct mdproc {
        int     md_flags;
diff -r 7a19dc8988ca -r 992c91efc931 sys/arch/i386/include/proc.h
--- a/sys/arch/i386/include/proc.h      Sat Jun 13 16:43:32 2020 +0000
+++ b/sys/arch/i386/include/proc.h      Thu Jun 04 03:34:45 2020 +0000
@@ -44,6 +44,7 @@
 struct vm_page;
 
 #define        MDL_FPU_IN_CPU          0x0020  /* the FPU state is in the CPU */
+#define        MDL_SYSTEM_FPU          0x0040  /* system thread is allowed FPU use */
 
 struct mdlwp {
        struct  trapframe *md_regs;     /* registers on current frame */
diff -r 7a19dc8988ca -r 992c91efc931 sys/arch/x86/include/fpu.h
--- a/sys/arch/x86/include/fpu.h        Sat Jun 13 16:43:32 2020 +0000
+++ b/sys/arch/x86/include/fpu.h        Thu Jun 04 03:34:45 2020 +0000
@@ -33,6 +33,9 @@
 void fpu_kern_enter(void);
 void fpu_kern_leave(void);
 
+int fpu_kthread_enter(void);
+void fpu_kthread_leave(int);
+
 void process_write_fpregs_xmm(struct lwp *, const struct fxsave *);
 void process_write_fpregs_s87(struct lwp *, const struct save87 *);
 
diff -r 7a19dc8988ca -r 992c91efc931 sys/arch/x86/x86/fpu.c
--- a/sys/arch/x86/x86/fpu.c    Sat Jun 13 16:43:32 2020 +0000
+++ b/sys/arch/x86/x86/fpu.c    Thu Jun 04 03:34:45 2020 +0000
@@ -137,7 +137,8 @@
        struct pcb *pcb = lwp_getpcb(l);
        union savefpu *area = &pcb->pcb_savefpu;
 
-       KASSERT((l->l_flag & LW_SYSTEM) == 0);
+       KASSERT((l->l_flag & LW_SYSTEM) == 0 ||
+           (l->l_md.md_flags & MDL_SYSTEM_FPU));
        if (l == curlwp) {
                fpu_save();
        }
@@ -154,7 +155,8 @@
 
        kpreempt_disable();
        if (l->l_md.md_flags & MDL_FPU_IN_CPU) {
-               KASSERT((l->l_flag & LW_SYSTEM) == 0);
+               KASSERT((l->l_flag & LW_SYSTEM) == 0 ||
+                   (l->l_md.md_flags & MDL_SYSTEM_FPU));
                fpu_area_save(area, x86_xsave_features);
                l->l_md.md_flags &= ~MDL_FPU_IN_CPU;
        }
@@ -344,6 +346,47 @@
 /* -------------------------------------------------------------------------- */
 
 /*
+ * s = fpu_kthread_enter()
+ *
+ *     Allow the current kthread to use the FPU without disabling
+ *     preemption as fpu_kern_enter/leave do.  Must not be used in a
+ *     user lwp.  When done, call fpu_kthread_leave(s).  May be
+ *     recursively nested.
+ */
+int
+fpu_kthread_enter(void)
+{
+       struct lwp *l = curlwp;
+       int system_fpu = l->l_md.md_flags & MDL_SYSTEM_FPU;
+
+       KASSERTMSG(l->l_flag & LW_SYSTEM,
+           "fpu_kthread_enter is allowed only in kthreads");
+
+       l->l_md.md_flags |= MDL_SYSTEM_FPU;
+
+       return system_fpu;
+}
+
+/*
+ * fpu_kthread_exit(s)
+ *
+ *     Return to the previous state of whether the current kthread can
+ *     use the FPU without disabling preemption.
+ */
+void
+fpu_kthread_leave(int system_fpu)
+{
+       struct lwp *l = curlwp;
+
+       KASSERTMSG(l->l_flag & LW_SYSTEM,
+           "fpu_kthread_leave is allowed only in kthreads");
+       KASSERTMSG(l->l_md.md_flags & MDL_SYSTEM_FPU,
+           "fpu_kthread_leave without fpu_kthread_enter");
+
+       l->l_md.md_flags ^= system_fpu ^ MDL_SYSTEM_FPU;
+}
+
+/*
  * fpu_kern_enter()
  *
  *     Begin using the FPU.  Raises to splhigh, disabling all



Home | Main Index | Thread Index | Old Index