Module Name: src
Committed By: riastradh
Date: Mon Jun 29 23:22:27 UTC 2020
Modified Files:
src/sys/arch/aarch64/aarch64: cpu.c fpu.c
src/sys/arch/aarch64/include: cpu.h machdep.h
Added Files:
src/sys/arch/aarch64/include: fpu.h
Log Message:
Draft fpu_kern_enter/leave on aarch64.
To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/sys/arch/aarch64/aarch64/cpu.c
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/aarch64/aarch64/fpu.c
cvs rdiff -u -r1.22 -r1.23 src/sys/arch/aarch64/include/cpu.h
cvs rdiff -u -r0 -r1.1 src/sys/arch/aarch64/include/fpu.h
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/aarch64/include/machdep.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/arch/aarch64/aarch64/cpu.c
diff -u src/sys/arch/aarch64/aarch64/cpu.c:1.47 src/sys/arch/aarch64/aarch64/cpu.c:1.48
--- src/sys/arch/aarch64/aarch64/cpu.c:1.47 Sun Jun 14 16:10:18 2020
+++ src/sys/arch/aarch64/aarch64/cpu.c Mon Jun 29 23:22:27 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu.c,v 1.47 2020/06/14 16:10:18 riastradh Exp $ */
+/* $NetBSD: cpu.c,v 1.48 2020/06/29 23:22:27 riastradh Exp $ */
/*
* Copyright (c) 2017 Ryo Shimizu <[email protected]>
@@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: cpu.c,v 1.47 2020/06/14 16:10:18 riastradh Exp $");
+__KERNEL_RCSID(1, "$NetBSD: cpu.c,v 1.48 2020/06/29 23:22:27 riastradh Exp $");
#include "locators.h"
#include "opt_arm_debug.h"
@@ -133,6 +133,8 @@ cpu_attach(device_t dv, cpuid_t id)
ci->ci_dev = dv;
dv->dv_private = ci;
+ ci->ci_kfpu_spl = -1;
+
arm_cpu_do_topology(ci);
cpu_identify(ci->ci_dev, ci);
Index: src/sys/arch/aarch64/aarch64/fpu.c
diff -u src/sys/arch/aarch64/aarch64/fpu.c:1.3 src/sys/arch/aarch64/aarch64/fpu.c:1.4
--- src/sys/arch/aarch64/aarch64/fpu.c:1.3 Wed Nov 7 06:47:38 2018
+++ src/sys/arch/aarch64/aarch64/fpu.c Mon Jun 29 23:22:27 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: fpu.c,v 1.3 2018/11/07 06:47:38 riastradh Exp $ */
+/* $NetBSD: fpu.c,v 1.4 2020/06/29 23:22:27 riastradh Exp $ */
/*-
* Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -31,13 +31,15 @@
#include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: fpu.c,v 1.3 2018/11/07 06:47:38 riastradh Exp $");
+__KERNEL_RCSID(1, "$NetBSD: fpu.c,v 1.4 2020/06/29 23:22:27 riastradh Exp $");
#include <sys/param.h>
#include <sys/types.h>
#include <sys/lwp.h>
#include <sys/evcnt.h>
+#include <aarch64/fpu.h>
+#include <aarch64/locore.h>
#include <aarch64/reg.h>
#include <aarch64/pcb.h>
#include <aarch64/armreg.h>
@@ -172,3 +174,68 @@ fpu_state_release(lwp_t *l)
reg_cpacr_el1_write(CPACR_FPEN_NONE);
__asm __volatile ("isb");
}
+
+void
+fpu_kern_enter(void)
+{
+ struct lwp *l = curlwp;
+ struct cpu_info *ci;
+ int s;
+
+ /*
+ * Block all interrupts. We must block preemption since -- if
+ * this is a user thread -- there is nowhere to save the kernel
+ * fpu state, and if we want this to be usable in interrupts,
+ * we can't let interrupts interfere with the fpu state in use
+ * since there's nowhere for them to save it.
+ */
+ s = splhigh();
+ ci = curcpu();
+ KASSERT(ci->ci_kfpu_spl == -1);
+ ci->ci_kfpu_spl = s;
+
+ /*
+ * If we are in a softint and have a pinned lwp, the fpu state
+ * is that of the pinned lwp, so save it there.
+ */
+ if ((l->l_pflag & LP_INTR) && (l->l_switchto != NULL))
+ l = l->l_switchto;
+ if (fpu_used_p(l))
+ fpu_save(l);
+
+ /*
+ * Enable the fpu, and wait until it is enabled before
+ * executing any further instructions.
+ */
+ reg_cpacr_el1_write(CPACR_FPEN_ALL);
+ arm_isb();
+}
+
+void
+fpu_kern_leave(void)
+{
+ static const struct fpreg zero_fpreg;
+ struct cpu_info *ci = curcpu();
+ int s;
+
+ KASSERT(ci->ci_cpl == IPL_HIGH);
+ KASSERT(ci->ci_kfpu_spl != -1);
+
+ /*
+ * Zero the fpu registers; otherwise we might leak secrets
+ * through Spectre-class attacks to userland, even if there are
+ * no bugs in fpu state management.
+ */
+ load_fpregs(&zero_fpreg);
+
+ /*
+ * Disable the fpu so that the kernel can't accidentally use
+ * it again.
+ */
+ reg_cpacr_el1_write(CPACR_FPEN_NONE);
+ arm_isb();
+
+ s = ci->ci_kfpu_spl;
+ ci->ci_kfpu_spl = -1;
+ splx(s);
+}
Index: src/sys/arch/aarch64/include/cpu.h
diff -u src/sys/arch/aarch64/include/cpu.h:1.22 src/sys/arch/aarch64/include/cpu.h:1.23
--- src/sys/arch/aarch64/include/cpu.h:1.22 Tue Mar 10 01:17:33 2020
+++ src/sys/arch/aarch64/include/cpu.h Mon Jun 29 23:22:27 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu.h,v 1.22 2020/03/10 01:17:33 christos Exp $ */
+/* $NetBSD: cpu.h,v 1.23 2020/06/29 23:22:27 riastradh Exp $ */
/*-
* Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -89,6 +89,8 @@ struct cpu_info {
volatile u_int ci_astpending;
volatile u_int ci_intr_depth;
+ int ci_kfpu_spl;
+
/* event counters */
struct evcnt ci_vfp_use;
struct evcnt ci_vfp_reuse;
Index: src/sys/arch/aarch64/include/machdep.h
diff -u src/sys/arch/aarch64/include/machdep.h:1.11 src/sys/arch/aarch64/include/machdep.h:1.12
--- src/sys/arch/aarch64/include/machdep.h:1.11 Sat May 23 18:08:59 2020
+++ src/sys/arch/aarch64/include/machdep.h Mon Jun 29 23:22:27 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: machdep.h,v 1.11 2020/05/23 18:08:59 ryo Exp $ */
+/* $NetBSD: machdep.h,v 1.12 2020/06/29 23:22:27 riastradh Exp $ */
/*
* Copyright (c) 2017 Ryo Shimizu <[email protected]>
@@ -142,7 +142,7 @@ void aarch64_setregs_ptrauth(struct lwp
/* fpu.c */
void fpu_attach(struct cpu_info *);
struct fpreg;
-void load_fpregs(struct fpreg *);
+void load_fpregs(const struct fpreg *);
void save_fpregs(struct fpreg *);
#ifdef TRAP_SIGDEBUG
Added files:
Index: src/sys/arch/aarch64/include/fpu.h
diff -u /dev/null src/sys/arch/aarch64/include/fpu.h:1.1
--- /dev/null Mon Jun 29 23:22:27 2020
+++ src/sys/arch/aarch64/include/fpu.h Mon Jun 29 23:22:27 2020
@@ -0,0 +1,35 @@
+/* $NetBSD: fpu.h,v 1.1 2020/06/29 23:22:27 riastradh Exp $ */
+
+/*
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _AARCH64_FPU_H_
+#define _AARCH64_FPU_H_
+
+void fpu_kern_enter(void);
+void fpu_kern_leave(void);
+
+#endif /* _AARCH64_FPU_H_ */