From nobody Mon Feb 21 08:07:42 2022 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 479A819C906B; Mon, 21 Feb 2022 08:07:43 +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 4K2FKC1WHcz3qH3; Mon, 21 Feb 2022 08:07:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1645430863; 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=PIysPFB/uRhsdj1Ewy5uGCSU53x2Cb9pNMNbIPwfUYc=; b=WIYW/fru7PfKG1YMY3yG+RsYRu1kvdQ8bcIWT6L0OFgiXPR6DL2FIyZoJqg5KnuowlL0wH 8dfyDsBiZzb/xteKGKlQSk1BSKfI0FNB40SmIbil7rOCL44ENX2gYQsgZ+kbSGPBsrQMd+ frnYTowd1te3e1ivssQ8KLXwiW+9o5JPMGmoe8rYtEmhhk1/GBFlwbv8BhzCUd3TeME2Bz hj5pdncADstlc0hVbiQdUsLjF2FNbBCd6lUHz+bZiEP7wHNXg3zGpkiPiW5agNwDlX105p uEM87TbePzHSl/2IbUb2ry9uljWOMqSPTKwsrcHGyG+0WpFILIrJOxgqaxUAew== 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 14F2549EA; Mon, 21 Feb 2022 08:07:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 21L87gJZ036943; Mon, 21 Feb 2022 08:07:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 21L87g4A036942; Mon, 21 Feb 2022 08:07:42 GMT (envelope-from git) Date: Mon, 21 Feb 2022 08:07:42 GMT Message-Id: <202202210807.21L87g4A036942@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "David E. O'Brien" Subject: git: a68e606c402e - stable/12 - random: x86 driver: Prefer RDSEED over RDRAND when available 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: obrien X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: a68e606c402ea5ce2c8257d0566e6f397594aa54 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1645430863; 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=PIysPFB/uRhsdj1Ewy5uGCSU53x2Cb9pNMNbIPwfUYc=; b=BoJmLPE/JLrChcr7lD9Tqr0iDufTKqIvSB1w9WV8DD4WTBh/bGZHyxANwSmJD2YrCzCoYg zRD8P/HRtIjUhhd9XfORul3v3ymQsrZWyWHzHHmZzt2vLZsi+ZoTiAalncO2NjYacHgrsz YyW40j8+0CTZEGtZSVWEe2tTXDrQDwoXlYU73McohHWWdnnbgRcQCg/C4iOMRBhjvhHcnn 0CUcolg91yF+CYM+cQYt1PvMTWRCQRbwbXr4sf8gFH8OqLAMhni4xLYJbT57y8gHqoxCis lLYGb0x3LxtopEUIdCjLG/FPqEkxZBWKwApQ6W3cKIbofNcJR8a6MZ/9J4XThg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1645430863; a=rsa-sha256; cv=none; b=ftC7l+n7pPvykeZxRZaTPKXLQO4lxpd3HSsYJDV8n86fbnoPUXERKukA5Hm2GoqtcwuzY+ z1LaEJJRbwXqsNhJjMgF05zhFxZkSOLPAzergb+Wtih99U0O+u51j4L43cxyecqUGqCBTA FmGsRdl+fLju2oH8Tb63yHEki0pUYzGYx3rR0Q5/EW641JvtiDLSi+EJt4UxcY81qKDfxu 56icJ75RbiEeEx2E9BjiKUxCQMQiuuRmXEikSOVaeXLdoAN8H5shlL1RtdOz34urxy7g5f nhWi8cdowoBkJZfHBCKYrcQNiqvuAP+8UXp4VTRItA3XecoE5mKVIQ8WE5atTw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by obrien: URL: https://cgit.FreeBSD.org/src/commit/?id=a68e606c402ea5ce2c8257d0566e6f397594aa54 commit a68e606c402ea5ce2c8257d0566e6f397594aa54 Author: Conrad Meyer AuthorDate: 2019-05-08 00:45:16 +0000 Commit: David E. O'Brien CommitDate: 2022-02-21 05:56:42 +0000 random: x86 driver: Prefer RDSEED over RDRAND when available Per https://software.intel.com/en-us/blogs/2012/11/17/the-difference-between-rdrand-and-rdseed , RDRAND is a PRNG seeded from the same source as RDSEED. The source is more suitable as PRNG seed material, so prefer it when the RDSEED intrinsic is available (indicated in CPU feature bits). (cherry picked from commit 2cb54a800c3a42cc256adcda0be98bd0af39096c) --- sys/dev/random/ivy.c | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/sys/dev/random/ivy.c b/sys/dev/random/ivy.c index 7431f56f0f69..5ff288b394ac 100644 --- a/sys/dev/random/ivy.c +++ b/sys/dev/random/ivy.c @@ -44,11 +44,13 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #define RETRY_COUNT 10 +static bool has_rdrand, has_rdseed; static u_int random_ivy_read(void *, u_int); static struct random_source random_ivy = { @@ -57,10 +59,9 @@ static struct random_source random_ivy = { .rs_read = random_ivy_read }; -static inline int -ivy_rng_store(u_long *buf) +static int +x86_rdrand_store(u_long *buf) { -#ifdef __GNUCLIKE_ASM u_long rndval; int retry; @@ -75,9 +76,38 @@ ivy_rng_store(u_long *buf) : "+r" (retry), "=r" (rndval) : : "cc"); *buf = rndval; return (retry); -#else /* __GNUCLIKE_ASM */ - return (0); -#endif +} + +static int +x86_rdseed_store(u_long *buf) +{ + u_long rndval; + int retry; + + retry = RETRY_COUNT; + __asm __volatile( + "1:\n\t" + "rdseed %1\n\t" /* read randomness into rndval */ + "jc 2f\n\t" /* CF is set on success, exit retry loop */ + "dec %0\n\t" /* otherwise, retry-- */ + "jne 1b\n\t" /* and loop if retries are not exhausted */ + "2:" + : "+r" (retry), "=r" (rndval) : : "cc"); + *buf = rndval; + return (retry); +} + +DEFINE_IFUNC(static, int, x86_rng_store, (u_long *buf), static) +{ + has_rdrand = (cpu_feature2 & CPUID2_RDRAND); + has_rdseed = (cpu_stdext_feature & CPUID_STDEXT_RDSEED); + + if (has_rdseed) + return (x86_rdseed_store); + else if (has_rdrand) + return (x86_rdrand_store); + else + return (NULL); } /* It is required that buf length is a multiple of sizeof(u_long). */ @@ -90,7 +120,7 @@ random_ivy_read(void *buf, u_int c) KASSERT(c % sizeof(*b) == 0, ("partial read %d", c)); b = buf; for (count = c; count > 0; count -= sizeof(*b)) { - if (ivy_rng_store(&rndval) == 0) + if (x86_rng_store(&rndval) == 0) break; *b++ = rndval; } @@ -104,14 +134,14 @@ rdrand_modevent(module_t mod, int type, void *unused) switch (type) { case MOD_LOAD: - if (cpu_feature2 & CPUID2_RDRAND) { + if (has_rdrand || has_rdseed) { random_source_register(&random_ivy); printf("random: fast provider: \"%s\"\n", random_ivy.rs_ident); } break; case MOD_UNLOAD: - if (cpu_feature2 & CPUID2_RDRAND) + if (has_rdrand || has_rdseed) random_source_deregister(&random_ivy); break;