Date: Wed, 29 Nov 2023 13:36:40 +0000 From: Andrew Turner <andrew@FreeBSD.org> To: John Baldwin <jhb@FreeBSD.org> Cc: "src-committers@freebsd.org" <src-committers@FreeBSD.org>, "dev-commits-src-all@freebsd.org" <dev-commits-src-all@FreeBSD.org>, "dev-commits-src-main@freebsd.org" <dev-commits-src-main@FreeBSD.org>, Jessica Clarke <jrtc27@freebsd.org> Subject: Re: git: 9eecef052155 - main - Add an Armv8 rndr random number provider Message-ID: <C8C966A1-AD3C-4ED1-A0D3-47A4CE1002D1@FreeBSD.org> In-Reply-To: <cb4d53d8-27db-4763-a6c2-372f8d6aa7f4@FreeBSD.org> References: <202311151812.3AFICLIs077567@gitrepo.freebsd.org> <E302C81B-CAA0-4122-8754-FE9F055C3FBB@freebsd.org> <2423E245-0F1B-4195-A766-166183774421@freebsd.org> <cb4d53d8-27db-4763-a6c2-372f8d6aa7f4@FreeBSD.org>
next in thread | previous in thread | raw e-mail | index | archive | help
--Apple-Mail=_02C5D982-810F-4E82-812F-EC44B08517A3 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On 22 Nov 2023, at 18:14, John Baldwin <jhb@FreeBSD.org> wrote: >=20 > On 11/15/23 4:29 PM, Jessica Clarke wrote: >> On 15 Nov 2023, at 18:27, Jessica Clarke <jrtc27@FreeBSD.org> wrote: >>>=20 >>> On 15 Nov 2023, at 18:12, Andrew Turner <andrew@FreeBSD.org> wrote: >>>>=20 >>>> The branch main has been updated by andrew: >>>>=20 >>>> URL: = https://cgit.FreeBSD.org/src/commit/?id=3D9eecef052155646fbc5f8f533b952b37= 2572d06a >>>>=20 >>>> commit 9eecef052155646fbc5f8f533b952b372572d06a >>>> Author: Andrew Turner <andrew@FreeBSD.org> >>>> AuthorDate: 2023-11-15 17:42:02 +0000 >>>> Commit: Andrew Turner <andrew@FreeBSD.org> >>>> CommitDate: 2023-11-15 18:05:08 +0000 >>>>=20 >>>> Add an Armv8 rndr random number provider >>>>=20 >>>> Armv8.5 adds an optional random number generator. This is = implemented >>>> as two special registers one to read a random number, the other = to >>>> re-seed the entropy pool before reading a random number. Both = registers >>>> will set the condition flags to tell the caller they can't = produce a >>>> random number in a reasonable amount of time. >>>>=20 >>>> Without a signal to reseed the entropy pool use the latter = register >>>> to provide random numbers to the kernel pool. If at a later time = we >>>> had a way to tell the provider if it needs to reseed or not we = could >>>> use the former. >>>>=20 >>>> On an Amazon AWS Graviton3 VM this never failed, however this may = not >>>> be the case on low end CPUs so retry reading the random number 10 = times >>>> before returning an error. >>>>=20 >>>> Reviewed by: imp, delphij (csprng) >>>> Sponsored by: The FreeBSD Foundation >>>> Sponsored by: Arm Ltd >>>> Differential Revision: https://reviews.freebsd.org/D35411 >>>> --- >>>> sys/arm64/conf/std.dev | 1 + >>>> sys/conf/files.arm64 | 2 + >>>> sys/dev/random/armv8rng.c | 135 = +++++++++++++++++++++++++++++++++++++++ >>>> sys/dev/random/random_harvestq.c | 1 + >>>> sys/modules/Makefile | 2 + >>>> sys/modules/armv8_rng/Makefile | 11 ++++ >>>> sys/sys/random.h | 1 + >>>> 7 files changed, 153 insertions(+) >>>>=20 >>>> diff --git a/sys/arm64/conf/std.dev b/sys/arm64/conf/std.dev >>>> index 74d2407e0aec..0ebf2e775b11 100644 >>>> --- a/sys/arm64/conf/std.dev >>>> +++ b/sys/arm64/conf/std.dev >>>> @@ -53,6 +53,7 @@ device vt_simplefb >>>>=20 >>>> # Pseudo devices. >>>> device crypto # core crypto support >>>> +device armv8_rng # Armv8.5 rndr RNG >>>> device loop # Network loopback >>>> device ether # Ethernet support >>>> device vlan # 802.1Q VLAN support >>>> diff --git a/sys/conf/files.arm64 b/sys/conf/files.arm64 >>>> index 38b9e40463eb..9ccead6a98e1 100644 >>>> --- a/sys/conf/files.arm64 >>>> +++ b/sys/conf/files.arm64 >>>> @@ -379,6 +379,8 @@ dev/psci/psci.c standard >>>> dev/psci/smccc_arm64.S standard >>>> dev/psci/smccc.c standard >>>>=20 >>>> +dev/random/armv8rng.c optional armv8_rng !random_loadable >>>> + >>>> dev/safexcel/safexcel.c optional safexcel fdt >>>>=20 >>>> dev/sdhci/sdhci_xenon.c optional sdhci_xenon sdhci >>>> diff --git a/sys/dev/random/armv8rng.c b/sys/dev/random/armv8rng.c >>>> new file mode 100644 >>>> index 000000000000..3cca42a5bbf3 >>>> --- /dev/null >>>> +++ b/sys/dev/random/armv8rng.c >>>> @@ -0,0 +1,135 @@ >>>> +/*- >>>> + * Copyright (c) 2022 The FreeBSD Foundation >>>> + * >>>> + * This software was developed by Andrew Turner under sponsorship = from >>>> + * the FreeBSD Foundation. >>>> + * >>>> + * 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 <sys/cdefs.h> >>>> + >>>> +#include <sys/param.h> >>>> +#include <sys/systm.h> >>>> +#include <sys/conf.h> >>>> +#include <sys/kernel.h> >>>> +#include <sys/lock.h> >>>> +#include <sys/malloc.h> >>>> +#include <sys/module.h> >>>> +#include <sys/random.h> >>>> + >>>> +#include <machine/armreg.h> >>>> + >>>> +#include <dev/random/randomdev.h> >>>> + >>>> +static u_int random_rndr_read(void *, u_int); >>>> + >>>> +static bool has_rndr; >>>> +static struct random_source random_armv8_rndr =3D { >>>> + .rs_ident =3D "Armv8 rndr RNG", >>>> + .rs_source =3D RANDOM_PURE_ARMV8, >>>> + .rs_read =3D random_rndr_read, >>>> +}; >>>> + >>>> +static inline int >>>> +random_rndr_read_one(u_long *buf) >>>> +{ >>>> + u_long val; >>>> + int loop, ret; >>>> + >>>> + loop =3D 10; >>>> + do { >>>> + __asm __volatile( >>>> + ".arch_extension rng \n" >>>> + "mrs %0, rndrrs \n" /* Read the random number */ >>>> + "cset %w1, ne \n" /* 1 on success, 0 on failure */ >>>> + ".arch_extension norng \n" >>>> + : "=3D&r" (val), "=3D&r"(ret) :: "cc"); >>>=20 >>> Early clobber doesn=E2=80=99t make sense with no inputs. >> This also breaks the aarch64 ubuntu-20.04 GitHub build, as its LLVM >> doesn=E2=80=99t know about the RNG extension. Can you please stop = breaking the >> arm64 kernel build with older LLVM, this is far from the first time >> it=E2=80=99s happened, and some of those never got fixed upstream, we = just had >> to hack around them downstream. >=20 > The aarch64 GitHub build is still broken and has been broken now for a > week. Can this be fixed to work with that version or should we = deprecated > support for LLVM 12 for aarch64? Morello LLVM is at LLVM 13 = currently. I think depicting LLVM 12 is best. There is https://apt.llvm.org/ that = may be usable to get a toolchain to build FreeBSD on older Ubuntu and = Debian installs. I=E2=80=99m happy to keep 13 as the baseline for now, but expect we will = find it=E2=80=99s too old as I work through adding support for newer = Architecture features. Andrew= --Apple-Mail=_02C5D982-810F-4E82-812F-EC44B08517A3 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 <html><head><meta http-equiv=3D"content-type" content=3D"text/html; = charset=3Dutf-8"></head><body style=3D"overflow-wrap: break-word; = -webkit-nbsp-mode: space; line-break: after-white-space;"><br = id=3D"lineBreakAtBeginningOfMessage"><div><br><blockquote = type=3D"cite"><div>On 22 Nov 2023, at 18:14, John Baldwin = <jhb@FreeBSD.org> wrote:</div><br = class=3D"Apple-interchange-newline"><div><meta charset=3D"UTF-8"><span = style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: = 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; = letter-spacing: normal; text-align: start; text-indent: 0px; = text-transform: none; white-space: normal; word-spacing: 0px; = -webkit-text-stroke-width: 0px; text-decoration: none; float: none; = display: inline !important;">On 11/15/23 4:29 PM, Jessica Clarke = wrote:</span><br style=3D"caret-color: rgb(0, 0, 0); font-family: = Helvetica; font-size: 12px; font-style: normal; font-variant-caps: = normal; font-weight: 400; letter-spacing: normal; text-align: start; = text-indent: 0px; text-transform: none; white-space: normal; = word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: = none;"><blockquote type=3D"cite" style=3D"font-family: Helvetica; = font-size: 12px; font-style: normal; font-variant-caps: normal; = font-weight: 400; letter-spacing: normal; orphans: auto; text-align: = start; text-indent: 0px; text-transform: none; white-space: normal; = widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; = text-decoration: none;">On 15 Nov 2023, at 18:27, Jessica Clarke = <jrtc27@FreeBSD.org> wrote:<br><blockquote type=3D"cite"><br>On 15 = Nov 2023, at 18:12, Andrew Turner <andrew@FreeBSD.org> = wrote:<br><blockquote type=3D"cite"><br>The branch main has been updated = by andrew:<br><br>URL: = https://cgit.FreeBSD.org/src/commit/?id=3D9eecef052155646fbc5f8f533b952b37= 2572d06a<br><br>commit = 9eecef052155646fbc5f8f533b952b372572d06a<br>Author: = Andrew Turner = <andrew@FreeBSD.org><br>AuthorDate: 2023-11-15 17:42:02 = +0000<br>Commit: Andrew Turner = <andrew@FreeBSD.org><br>CommitDate: 2023-11-15 18:05:08 = +0000<br><br> Add an Armv8 rndr random number = provider<br><br> Armv8.5 adds an optional random number = generator. This is implemented<br> as two special registers = one to read a random number, the other to<br> re-seed the = entropy pool before reading a random number. Both = registers<br> will set the condition flags to tell the caller = they can't produce a<br> random number in a reasonable amount = of time.<br><br> Without a signal to reseed the entropy pool = use the latter register<br> to provide random numbers to the = kernel pool. If at a later time we<br> had a way to tell the = provider if it needs to reseed or not we could<br> use the = former.<br><br> On an Amazon AWS Graviton3 VM this never = failed, however this may not<br> be the case on low end CPUs = so retry reading the random number 10 times<br> before = returning an error.<br><br> Reviewed by: = imp, delphij (csprng)<br> Sponsored by: = The FreeBSD Foundation<br> Sponsored by: = Arm Ltd<br> Differential Revision: = https://reviews.freebsd.org/D35411<br>---<br>sys/arm64/conf/std.dev = | = 1 +<br>sys/conf/files.arm64 = | = 2 +<br>sys/dev/random/armv8rng.c = | 135 = +++++++++++++++++++++++++++++++++++++++<br>sys/dev/random/random_harvestq.= c | 1 +<br>sys/modules/Makefile = | = 2 +<br>sys/modules/armv8_rng/Makefile | 11 = ++++<br>sys/sys/random.h = &n= bsp; | 1 +<br>7 files changed, 153 = insertions(+)<br><br>diff --git a/sys/arm64/conf/std.dev = b/sys/arm64/conf/std.dev<br>index 74d2407e0aec..0ebf2e775b11 = 100644<br>--- a/sys/arm64/conf/std.dev<br>+++ = b/sys/arm64/conf/std.dev<br>@@ -53,6 +53,7 @@ device = vt_simplefb<br><br># Pseudo devices.<br>device crypto # core crypto = support<br>+device armv8_rng # Armv8.5 rndr RNG<br>device loop # Network = loopback<br>device ether # Ethernet support<br>device vlan # 802.1Q VLAN = support<br>diff --git a/sys/conf/files.arm64 = b/sys/conf/files.arm64<br>index 38b9e40463eb..9ccead6a98e1 100644<br>--- = a/sys/conf/files.arm64<br>+++ b/sys/conf/files.arm64<br>@@ -379,6 +379,8 = @@ dev/psci/psci.c standard<br>dev/psci/smccc_arm64.S = standard<br>dev/psci/smccc.c standard<br><br>+dev/random/armv8rng.c = optional armv8_rng !random_loadable<br>+<br>dev/safexcel/safexcel.c = optional safexcel fdt<br><br>dev/sdhci/sdhci_xenon.c optional = sdhci_xenon sdhci<br>diff --git a/sys/dev/random/armv8rng.c = b/sys/dev/random/armv8rng.c<br>new file mode 100644<br>index = 000000000000..3cca42a5bbf3<br>--- /dev/null<br>+++ = b/sys/dev/random/armv8rng.c<br>@@ -0,0 +1,135 @@<br>+/*-<br>+ * = Copyright (c) 2022 The FreeBSD Foundation<br>+ *<br>+ * This software = was developed by Andrew Turner under sponsorship from<br>+ * the FreeBSD = Foundation.<br>+ *<br>+ * Redistribution and use in source and binary = forms, with or without<br>+ * modification, are permitted provided that = the following conditions<br>+ * are met:<br>+ * 1. Redistributions of = source code must retain the above copyright<br>+ * = notice, this list of conditions and the following = disclaimer.<br>+ * 2. Redistributions in binary form must reproduce the = above copyright<br>+ * notice, this list of conditions = and the following disclaimer in the<br>+ * = documentation and/or other materials provided with the = distribution.<br>+ *<br>+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND = CONTRIBUTORS ``AS IS'' AND<br>+ * ANY EXPRESS OR IMPLIED WARRANTIES, = INCLUDING, BUT NOT LIMITED TO, THE<br>+ * IMPLIED WARRANTIES OF = MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>+ * ARE = DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE<br>+ = * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR = CONSEQUENTIAL<br>+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT = OF SUBSTITUTE GOODS<br>+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; = OR BUSINESS INTERRUPTION)<br>+ * HOWEVER CAUSED AND ON ANY THEORY OF = LIABILITY, WHETHER IN CONTRACT, STRICT<br>+ * LIABILITY, OR TORT = (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY<br>+ * OUT OF THE = USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>+ * SUCH = DAMAGE.<br>+ */<br>+<br>+#include <sys/cdefs.h><br>+<br>+#include = <sys/param.h><br>+#include <sys/systm.h><br>+#include = <sys/conf.h><br>+#include <sys/kernel.h><br>+#include = <sys/lock.h><br>+#include <sys/malloc.h><br>+#include = <sys/module.h><br>+#include <sys/random.h><br>+<br>+#include = <machine/armreg.h><br>+<br>+#include = <dev/random/randomdev.h><br>+<br>+static u_int = random_rndr_read(void *, u_int);<br>+<br>+static bool = has_rndr;<br>+static struct random_source random_armv8_rndr =3D {<br>+ = .rs_ident =3D "Armv8 rndr RNG",<br>+ .rs_source =3D = RANDOM_PURE_ARMV8,<br>+ .rs_read =3D = random_rndr_read,<br>+};<br>+<br>+static inline = int<br>+random_rndr_read_one(u_long *buf)<br>+{<br>+ u_long val;<br>+ = int loop, ret;<br>+<br>+ loop =3D 10;<br>+ do {<br>+ __asm = __volatile(<br>+ ".arch_extension rng = \n"<br>+ "mrs %0, rndrrs \n" /* Read the = random number */<br>+ "cset %w1, ne = \n" /* 1 on success, 0 on failure */<br>+ = ".arch_extension norng \n"<br>+ : = "=3D&r" (val), "=3D&r"(ret) :: "cc");<br></blockquote><br>Early = clobber doesn=E2=80=99t make sense with no inputs.<br></blockquote>This = also breaks the aarch64 ubuntu-20.04 GitHub build, as its = LLVM<br>doesn=E2=80=99t know about the RNG extension. Can you please = stop breaking the<br>arm64 kernel build with older LLVM, this is far = from the first time<br>it=E2=80=99s happened, and some of those never = got fixed upstream, we just had<br>to hack around them = downstream.<br></blockquote><br style=3D"caret-color: rgb(0, 0, 0); = font-family: Helvetica; font-size: 12px; font-style: normal; = font-variant-caps: normal; font-weight: 400; letter-spacing: normal; = text-align: start; text-indent: 0px; text-transform: none; white-space: = normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; = text-decoration: none;"><span style=3D"caret-color: rgb(0, 0, 0); = font-family: Helvetica; font-size: 12px; font-style: normal; = font-variant-caps: normal; font-weight: 400; letter-spacing: normal; = text-align: start; text-indent: 0px; text-transform: none; white-space: = normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; = text-decoration: none; float: none; display: inline !important;">The = aarch64 GitHub build is still broken and has been broken now for = a</span><br style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; = font-size: 12px; font-style: normal; font-variant-caps: normal; = font-weight: 400; letter-spacing: normal; text-align: start; = text-indent: 0px; text-transform: none; white-space: normal; = word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: = none;"><span style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; = font-size: 12px; font-style: normal; font-variant-caps: normal; = font-weight: 400; letter-spacing: normal; text-align: start; = text-indent: 0px; text-transform: none; white-space: normal; = word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: = none; float: none; display: inline !important;">week. Can this be = fixed to work with that version or should we deprecated</span><br = style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: = 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; = letter-spacing: normal; text-align: start; text-indent: 0px; = text-transform: none; white-space: normal; word-spacing: 0px; = -webkit-text-stroke-width: 0px; text-decoration: none;"><span = style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: = 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; = letter-spacing: normal; text-align: start; text-indent: 0px; = text-transform: none; white-space: normal; word-spacing: 0px; = -webkit-text-stroke-width: 0px; text-decoration: none; float: none; = display: inline !important;">support for LLVM 12 for aarch64? = Morello LLVM is at LLVM 13 currently.</span><br = style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: = 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; = letter-spacing: normal; text-align: start; text-indent: 0px; = text-transform: none; white-space: normal; word-spacing: 0px; = -webkit-text-stroke-width: 0px; text-decoration: = none;"></div></blockquote></div><br><div>I think depicting LLVM 12 is = best. There is https://apt.llvm.org/ that may be usable to get a = toolchain to build FreeBSD on older Ubuntu and Debian = installs.</div><div><br></div><div>I=E2=80=99m happy to keep 13 as the = baseline for now, but expect we will find it=E2=80=99s too old as I work = through adding support for newer Architecture = features.</div><div><br></div><div>Andrew</div></body></html>= --Apple-Mail=_02C5D982-810F-4E82-812F-EC44B08517A3--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?C8C966A1-AD3C-4ED1-A0D3-47A4CE1002D1>