From nobody Tue Jun 25 11:45:52 2024
X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4W7jhJ5gNSz5PPVr;
Tue, 25 Jun 2024 11:45:52 +0000 (UTC)
(envelope-from git@FreeBSD.org)
Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256
client-signature RSA-PSS (4096 bits) client-digest SHA256)
(Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK))
by mx1.freebsd.org (Postfix) with ESMTPS id 4W7jhJ4dCQz4Y5X;
Tue, 25 Jun 2024 11:45:52 +0000 (UTC)
(envelope-from git@FreeBSD.org)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim;
t=1719315952;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding;
bh=DBya6iuZvHHu0dFxMZrszBGqzG+mFjP425MqrDa0umg=;
b=ulXpVf4Pi4uO+3kVGpM2BGX6PSe82qwAAHpob29rvPjtYg+jqZJMZGMtbm5KH9sJevPiI+
pZW2t3D618zOAWtbrFlBSOzKGOOYnYZAdEjibKhB/zAYsWUotl5rFwtMytGSWZRNTAa/ob
vbR+yLMj78PCNvcxEqGup0x7OgqBmGxK0YmqqYbvH3dvtdunjvL5wiLvkZS42t1mwk66IT
u3P04v+pJMe/sGCzfhORhvx6Cb1UT9tgomvqYAJqdzP/35S8VC1S8t1g1/GutVVCxKfrxM
Q8fwIwySvpv/WegcELe/HRs/VxMbdMq7khr7IBqJ5zeDDR1fZT8w4mJs1ozBRw==
ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1719315952; a=rsa-sha256; cv=none;
b=K0HCKrhotdLYKt5W9qq+XVeaXf1aZqbBLcmJI83aLDs03RN2xjXn+ptvy+g9OnAkr5hEVo
qD2pMV6LceQv0uz3MAuQOKG6cIrYZ+lC9oFxdUTPYfpkUyc9fnd6+kcyHVTq9BCSHcewAn
GELafw6hoayH57cYMmIyDRV7NpIoGiKB9BSFvr0mJe6f99uo6mOaHJ5K4a0tu11z8b+cjl
a716eAvBeTvJn504zqPXwbx01H7CF8X1ZEK9El1fUCpaCUVIo1aTqNyW+tDlS1AKpoCcft
dTB4zR/IEjEWFTsQ+8RqKFlMXe0DE6kOQEppX+3nMrM2v6dJM3SDprs/qZxshQ==
ARC-Authentication-Results: i=1;
mx1.freebsd.org;
none
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org;
s=dkim; t=1719315952;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding;
bh=DBya6iuZvHHu0dFxMZrszBGqzG+mFjP425MqrDa0umg=;
b=YwTM/+Is8J75xb2GF/ezdCOQ9VIqkphCtZ77NkIojPMBC0dp5INKJWWkGjrU9G7G53mOxP
7vVYLjLx9halyB7mQLaG7/zZfEfbUptiZevv+ciDcBHyLryPdTXTJxeUvpwgX8weVGU24J
LZyLhnC4Tl3hrQrjQbJ+ErVa22LpacRue609Hx6aEhfQ2muW22ukaOhJN3wzeFfkIC7wQi
xPZCfuhrvVVJEI6+DgYZ9ghE27RuKFLsXKbvzNe3XpCATmBGn1qDXPROTLAacfLQM/JwSo
mZaSMuz5IFyc3uHvhU2LlOirjffaEMNaGnv3iCo8HQzlkrC1+x24BN8zXvnYDA==
Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
(Client did not present a certificate)
by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4W7jhJ46Qvz1149;
Tue, 25 Jun 2024 11:45:52 +0000 (UTC)
(envelope-from git@FreeBSD.org)
Received: from gitrepo.freebsd.org ([127.0.1.44])
by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 45PBjqVr031191;
Tue, 25 Jun 2024 11:45:52 GMT
(envelope-from git@gitrepo.freebsd.org)
Received: (from git@localhost)
by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 45PBjqXT031188;
Tue, 25 Jun 2024 11:45:52 GMT
(envelope-from git)
Date: Tue, 25 Jun 2024 11:45:52 GMT
Message-Id: <202406251145.45PBjqXT031188@gitrepo.freebsd.org>
To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org,
dev-commits-src-main@FreeBSD.org
From: Ruslan Bukin
Subject: git: 44d4ee7f3dad - main - riscv: add FPE code.
List-Id: Commit messages for all branches of the src repository
List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all
List-Help:
List-Post:
List-Subscribe:
List-Unsubscribe:
X-BeenThere: dev-commits-src-all@freebsd.org
Sender: owner-dev-commits-src-all@FreeBSD.org
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Git-Committer: br
X-Git-Repository: src
X-Git-Refname: refs/heads/main
X-Git-Reftype: branch
X-Git-Commit: 44d4ee7f3dadb9efa7a8ea599f7276074dd41a91
Auto-Submitted: auto-generated
The branch main has been updated by br:
URL: https://cgit.FreeBSD.org/src/commit/?id=44d4ee7f3dadb9efa7a8ea599f7276074dd41a91
commit 44d4ee7f3dadb9efa7a8ea599f7276074dd41a91
Author: Ruslan Bukin
AuthorDate: 2024-06-25 11:35:35 +0000
Commit: Ruslan Bukin
CommitDate: 2024-06-25 11:35:35 +0000
riscv: add FPE code.
Add floating point extension (FPE) code needed for bhyve and world switch.
Reviewed by: mhorne
Sponsored by: UKRI
Differential Revision: https://reviews.freebsd.org/D45697
---
sys/riscv/include/fpe.h | 12 ++-
sys/riscv/riscv/fpe.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 201 insertions(+), 1 deletion(-)
diff --git a/sys/riscv/include/fpe.h b/sys/riscv/include/fpe.h
index ae1414dde717..2cf0d3382776 100644
--- a/sys/riscv/include/fpe.h
+++ b/sys/riscv/include/fpe.h
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2016 Ruslan Bukin
+ * Copyright (c) 2016-2024 Ruslan Bukin
* All rights reserved.
*
* This software was developed by SRI International and the University of
@@ -34,4 +34,14 @@
void fpe_state_save(struct thread *td);
void fpe_state_clear(void);
+struct fpreg *fpu_save_area_alloc(void);
+void fpu_save_area_free(struct fpreg *fsa);
+void fpu_save_area_reset(struct fpreg *fsa);
+
+void fpe_enable(void);
+void fpe_disable(void);
+
+void fpe_store(struct fpreg *state);
+void fpe_restore(struct fpreg *state);
+
#endif /* !_MACHINE_FPE_H_ */
diff --git a/sys/riscv/riscv/fpe.c b/sys/riscv/riscv/fpe.c
new file mode 100644
index 000000000000..b6c66e5e4f09
--- /dev/null
+++ b/sys/riscv/riscv/fpe.c
@@ -0,0 +1,190 @@
+/*-
+ * Copyright (c) 2024 Ruslan Bukin
+ *
+ * This software was developed by the University of Cambridge Computer
+ * Laboratory (Department of Computer Science and Technology) under Innovate
+ * UK project 105694, "Digital Security by Design (DSbD) Technology Platform
+ * Prototype".
+ *
+ * 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 AUTHOR 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 AUTHOR 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.
+ */
+
+#include
+#include
+#include
+
+#include
+
+#include
+#include
+
+static uma_zone_t fpu_save_area_zone;
+static struct fpreg *fpu_initialstate;
+
+void
+fpe_enable(void)
+{
+ uint64_t reg;
+
+ reg = SSTATUS_FS_INITIAL;
+
+ csr_set(sstatus, reg);
+}
+
+void
+fpe_disable(void)
+{
+ uint64_t mask;
+
+ mask = SSTATUS_FS_MASK;
+
+ csr_clear(sstatus, mask);
+}
+
+void
+fpe_store(struct fpreg *regs)
+{
+ uint64_t fcsr, (*fp_x)[32][2];
+
+ fp_x = ®s->fp_x;
+
+ __asm __volatile(
+ "frcsr %0 \n"
+ "fsd f0, (16 * 0)(%1)\n"
+ "fsd f1, (16 * 1)(%1)\n"
+ "fsd f2, (16 * 2)(%1)\n"
+ "fsd f3, (16 * 3)(%1)\n"
+ "fsd f4, (16 * 4)(%1)\n"
+ "fsd f5, (16 * 5)(%1)\n"
+ "fsd f6, (16 * 6)(%1)\n"
+ "fsd f7, (16 * 7)(%1)\n"
+ "fsd f8, (16 * 8)(%1)\n"
+ "fsd f9, (16 * 9)(%1)\n"
+ "fsd f10, (16 * 10)(%1)\n"
+ "fsd f11, (16 * 11)(%1)\n"
+ "fsd f12, (16 * 12)(%1)\n"
+ "fsd f13, (16 * 13)(%1)\n"
+ "fsd f14, (16 * 14)(%1)\n"
+ "fsd f15, (16 * 15)(%1)\n"
+ "fsd f16, (16 * 16)(%1)\n"
+ "fsd f17, (16 * 17)(%1)\n"
+ "fsd f18, (16 * 18)(%1)\n"
+ "fsd f19, (16 * 19)(%1)\n"
+ "fsd f20, (16 * 20)(%1)\n"
+ "fsd f21, (16 * 21)(%1)\n"
+ "fsd f22, (16 * 22)(%1)\n"
+ "fsd f23, (16 * 23)(%1)\n"
+ "fsd f24, (16 * 24)(%1)\n"
+ "fsd f25, (16 * 25)(%1)\n"
+ "fsd f26, (16 * 26)(%1)\n"
+ "fsd f27, (16 * 27)(%1)\n"
+ "fsd f28, (16 * 28)(%1)\n"
+ "fsd f29, (16 * 29)(%1)\n"
+ "fsd f30, (16 * 30)(%1)\n"
+ "fsd f31, (16 * 31)(%1)\n"
+ : "=&r"(fcsr), "=r"(fp_x), "=m"(*fp_x));
+
+ regs->fp_fcsr = fcsr;
+}
+
+void
+fpe_restore(struct fpreg *regs)
+{
+ uint64_t fcsr, (*fp_x)[32][2];
+
+ fp_x = ®s->fp_x;
+ fcsr = regs->fp_fcsr;
+
+ __asm __volatile(
+ "fscsr %0 \n"
+ "fld f0, (16 * 0)(%1)\n"
+ "fld f1, (16 * 1)(%1)\n"
+ "fld f2, (16 * 2)(%1)\n"
+ "fld f3, (16 * 3)(%1)\n"
+ "fld f4, (16 * 4)(%1)\n"
+ "fld f5, (16 * 5)(%1)\n"
+ "fld f6, (16 * 6)(%1)\n"
+ "fld f7, (16 * 7)(%1)\n"
+ "fld f8, (16 * 8)(%1)\n"
+ "fld f9, (16 * 9)(%1)\n"
+ "fld f10, (16 * 10)(%1)\n"
+ "fld f11, (16 * 11)(%1)\n"
+ "fld f12, (16 * 12)(%1)\n"
+ "fld f13, (16 * 13)(%1)\n"
+ "fld f14, (16 * 14)(%1)\n"
+ "fld f15, (16 * 15)(%1)\n"
+ "fld f16, (16 * 16)(%1)\n"
+ "fld f17, (16 * 17)(%1)\n"
+ "fld f18, (16 * 18)(%1)\n"
+ "fld f19, (16 * 19)(%1)\n"
+ "fld f20, (16 * 20)(%1)\n"
+ "fld f21, (16 * 21)(%1)\n"
+ "fld f22, (16 * 22)(%1)\n"
+ "fld f23, (16 * 23)(%1)\n"
+ "fld f24, (16 * 24)(%1)\n"
+ "fld f25, (16 * 25)(%1)\n"
+ "fld f26, (16 * 26)(%1)\n"
+ "fld f27, (16 * 27)(%1)\n"
+ "fld f28, (16 * 28)(%1)\n"
+ "fld f29, (16 * 29)(%1)\n"
+ "fld f30, (16 * 30)(%1)\n"
+ "fld f31, (16 * 31)(%1)\n"
+ :: "r"(fcsr), "r"(fp_x), "m"(*fp_x));
+}
+
+struct fpreg *
+fpu_save_area_alloc(void)
+{
+
+ return (uma_zalloc(fpu_save_area_zone, M_WAITOK));
+}
+
+void
+fpu_save_area_free(struct fpreg *fsa)
+{
+
+ uma_zfree(fpu_save_area_zone, fsa);
+}
+
+void
+fpu_save_area_reset(struct fpreg *fsa)
+{
+
+ memcpy(fsa, fpu_initialstate, sizeof(*fsa));
+}
+
+static void
+fpe_init(const void *dummy __unused)
+{
+
+ fpu_save_area_zone = uma_zcreate("FPE save area", sizeof(struct fpreg),
+ NULL, NULL, NULL, NULL, _Alignof(struct fpreg) - 1, 0);
+ fpu_initialstate = uma_zalloc(fpu_save_area_zone, M_WAITOK | M_ZERO);
+
+ fpe_enable();
+ fpe_store(fpu_initialstate);
+ fpe_disable();
+
+ bzero(fpu_initialstate->fp_x, sizeof(fpu_initialstate->fp_x));
+}
+
+SYSINIT(fpe, SI_SUB_CPU, SI_ORDER_ANY, fpe_init, NULL);