Source-Changes-HG archive

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

[src/trunk]: src/sys/arch Reintroduce cpu_rng_early_sample(), but this time w...



details:   https://anonhg.NetBSD.org/src/rev/bed1289e6617
branches:  trunk
changeset: 932510:bed1289e6617
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sun May 10 06:30:57 2020 +0000

description:
Reintroduce cpu_rng_early_sample(), but this time with embedded detection
for RDRAND/RDSEED, because TSC is not very strong.

diffstat:

 sys/arch/amd64/amd64/machdep.c |  12 +++++-------
 sys/arch/x86/include/cpu_rng.h |   3 ++-
 sys/arch/x86/x86/cpu_rng.c     |  36 +++++++++++++++++++++++++++++++++++-
 3 files changed, 42 insertions(+), 9 deletions(-)

diffs (117 lines):

diff -r d907a071bb7a -r bed1289e6617 sys/arch/amd64/amd64/machdep.c
--- a/sys/arch/amd64/amd64/machdep.c    Sun May 10 06:24:16 2020 +0000
+++ b/sys/arch/amd64/amd64/machdep.c    Sun May 10 06:30:57 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: machdep.c,v 1.354 2020/05/08 00:52:29 riastradh Exp $  */
+/*     $NetBSD: machdep.c,v 1.355 2020/05/10 06:30:57 maxv Exp $       */
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.354 2020/05/08 00:52:29 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.355 2020/05/10 06:30:57 maxv Exp $");
 
 #include "opt_modular.h"
 #include "opt_user_ldt.h"
@@ -1585,8 +1585,6 @@
 {
        /*
         * XXX Too early to use cprng(9), or even entropy_extract.
-        * Also too early to use rdrand/rdseed, since we haven't probed
-        * cpu features yet.  This is a hack -- fix me!
         */
        struct entpool pool;
        size_t randhole;
@@ -1595,7 +1593,7 @@
        vaddr_t va;
 
        memset(&pool, 0, sizeof pool);
-       sample = rdtsc();
+       cpu_rng_early_sample(&sample);
        entpool_enter(&pool, &sample, sizeof sample);
 
        memset(&slotspace, 0, sizeof(slotspace));
@@ -1651,7 +1649,7 @@
        slotspace.area[SLAREA_KERN].active = true;
 
        /* Main. */
-       sample = rdtsc();
+       cpu_rng_early_sample(&sample);
        entpool_enter(&pool, &sample, sizeof sample);
        entpool_extract(&pool, &randhole, sizeof randhole);
        entpool_extract(&pool, &randva, sizeof randva);
@@ -1662,7 +1660,7 @@
 
 #ifndef XENPV
        /* PTE. */
-       sample = rdtsc();
+       cpu_rng_early_sample(&sample);
        entpool_enter(&pool, &sample, sizeof sample);
        entpool_extract(&pool, &randhole, sizeof randhole);
        entpool_extract(&pool, &randva, sizeof randva);
diff -r d907a071bb7a -r bed1289e6617 sys/arch/x86/include/cpu_rng.h
--- a/sys/arch/x86/include/cpu_rng.h    Sun May 10 06:24:16 2020 +0000
+++ b/sys/arch/x86/include/cpu_rng.h    Sun May 10 06:30:57 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_rng.h,v 1.3 2020/04/30 03:29:19 riastradh Exp $ */
+/* $NetBSD: cpu_rng.h,v 1.4 2020/05/10 06:30:57 maxv Exp $ */
 
 #ifndef _X86_CPU_RNG_H_
 #define _X86_CPU_RNG_H_
@@ -33,5 +33,6 @@
  */
 
 void cpu_rng_init(void);
+void cpu_rng_early_sample(uint64_t *);
 
 #endif /* _X86_CPU_RNG_H_ */
diff -r d907a071bb7a -r bed1289e6617 sys/arch/x86/x86/cpu_rng.c
--- a/sys/arch/x86/x86/cpu_rng.c        Sun May 10 06:24:16 2020 +0000
+++ b/sys/arch/x86/x86/cpu_rng.c        Sun May 10 06:30:57 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_rng.c,v 1.13 2020/04/30 03:40:53 riastradh Exp $ */
+/* $NetBSD: cpu_rng.c,v 1.14 2020/05/10 06:30:57 maxv Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -283,3 +283,37 @@
        rnd_attach_source(&cpu_rng_source, cpu_rng_name[cpu_rng_mode],
            RND_TYPE_RNG, RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
 }
+
+/* -------------------------------------------------------------------------- */
+
+void
+cpu_rng_early_sample(uint64_t *sample)
+{
+       static bool has_rdseed = false;
+       static bool has_rdrand = false;
+       static bool inited = false;
+       u_int descs[4];
+       size_t n;
+
+       if (!inited) {
+               if (cpuid_level >= 7) {
+                       x86_cpuid(0x07, descs);
+                       has_rdseed = (descs[1] & CPUID_SEF_RDSEED) != 0;
+               }
+               if (cpuid_level >= 1) {
+                       x86_cpuid(0x01, descs);
+                       has_rdrand = (descs[2] & CPUID2_RDRAND) != 0;
+               }
+               inited = true;
+       }
+
+       n = 0;
+       if (has_rdseed && has_rdrand)
+               n = cpu_rng_rdseed_rdrand(sample);
+       else if (has_rdseed)
+               n = cpu_rng_rdseed(sample);
+       else if (has_rdrand)
+               n = cpu_rng_rdrand(sample);
+       if (n == 0)
+               *sample = rdtsc();
+}



Home | Main Index | Thread Index | Old Index