Module Name:    src
Committed By:   maxv
Date:           Sun May 10 06:30:57 UTC 2020

Modified Files:
        src/sys/arch/amd64/amd64: machdep.c
        src/sys/arch/x86/include: cpu_rng.h
        src/sys/arch/x86/x86: cpu_rng.c

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


To generate a diff of this commit:
cvs rdiff -u -r1.354 -r1.355 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/x86/include/cpu_rng.h
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/x86/x86/cpu_rng.c

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/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.354 src/sys/arch/amd64/amd64/machdep.c:1.355
--- src/sys/arch/amd64/amd64/machdep.c:1.354	Fri May  8 00:52:29 2020
+++ src/sys/arch/amd64/amd64/machdep.c	Sun May 10 06:30:57 2020
@@ -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 @@ init_slotspace(void)
 {
 	/*
 	 * 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 @@ init_slotspace(void)
 	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 @@ init_slotspace(void)
 	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 @@ init_slotspace(void)
 
 #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);

Index: src/sys/arch/x86/include/cpu_rng.h
diff -u src/sys/arch/x86/include/cpu_rng.h:1.3 src/sys/arch/x86/include/cpu_rng.h:1.4
--- src/sys/arch/x86/include/cpu_rng.h:1.3	Thu Apr 30 03:29:19 2020
+++ src/sys/arch/x86/include/cpu_rng.h	Sun May 10 06:30:57 2020
@@ -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_ */

Index: src/sys/arch/x86/x86/cpu_rng.c
diff -u src/sys/arch/x86/x86/cpu_rng.c:1.13 src/sys/arch/x86/x86/cpu_rng.c:1.14
--- src/sys/arch/x86/x86/cpu_rng.c:1.13	Thu Apr 30 03:40:53 2020
+++ src/sys/arch/x86/x86/cpu_rng.c	Sun May 10 06:30:57 2020
@@ -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 @@ cpu_rng_init(void)
 	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();
+}

Reply via email to