[Midnightbsd-cvs] src [6820] add rdrand option for ivybridge cpus

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Thu Oct 2 23:58:10 EDT 2014


Revision: 6820
          http://svnweb.midnightbsd.org/src/?rev=6820
Author:   laffer1
Date:     2014-10-02 23:58:09 -0400 (Thu, 02 Oct 2014)
Log Message:
-----------
add rdrand option for ivybridge cpus

Modified Paths:
--------------
    trunk/sys/conf/files.amd64
    trunk/sys/conf/files.i386
    trunk/sys/conf/options.amd64
    trunk/sys/conf/options.i386
    trunk/sys/dev/random/nehemiah.c
    trunk/sys/dev/random/probe.c
    trunk/sys/dev/random/randomdev_soft.c
    trunk/sys/modules/random/Makefile

Added Paths:
-----------
    trunk/sys/dev/random/ivy.c

Removed Paths:
-------------
    trunk/sys/dev/random/nehemiah.h

Property Changed:
----------------
    trunk/sys/conf/files
    trunk/sys/conf/files.amd64
    trunk/sys/conf/files.i386
    trunk/sys/conf/options
    trunk/sys/conf/options.amd64
    trunk/sys/conf/options.i386
    trunk/sys/modules/random/Makefile

Index: trunk/sys/conf/files
===================================================================
--- trunk/sys/conf/files	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/conf/files	2014-10-03 03:58:09 UTC (rev 6820)

Property changes on: trunk/sys/conf/files
___________________________________________________________________
Deleted: cvs2svn:cvs-rev
## -1 +0,0 ##
-1.39
\ No newline at end of property
Modified: trunk/sys/conf/files.amd64
===================================================================
--- trunk/sys/conf/files.amd64	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/conf/files.amd64	2014-10-03 03:58:09 UTC (rev 6820)
@@ -216,7 +216,8 @@
 dev/nfe/if_nfe.c		optional	nfe pci
 dev/nve/if_nve.c		optional	nve pci
 dev/nvram/nvram.c		optional	nvram isa
-dev/random/nehemiah.c		optional	random
+dev/random/ivy.c		optional random rdrand_rng
+dev/random/nehemiah.c		optional random padlock_rng
 dev/qlxgb/qla_dbg.c		optional	qlxgb pci
 dev/qlxgb/qla_hw.c		optional	qlxgb pci
 dev/qlxgb/qla_ioctl.c		optional	qlxgb pci


Property changes on: trunk/sys/conf/files.amd64
___________________________________________________________________
Deleted: cvs2svn:cvs-rev
## -1 +0,0 ##
-1.13
\ No newline at end of property
Modified: trunk/sys/conf/files.i386
===================================================================
--- trunk/sys/conf/files.i386	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/conf/files.i386	2014-10-03 03:58:09 UTC (rev 6820)
@@ -226,7 +226,8 @@
 dev/nve/if_nve.c		optional nve pci
 dev/nvram/nvram.c		optional nvram isa
 dev/pcf/pcf_isa.c		optional pcf
-dev/random/nehemiah.c		optional random
+dev/random/ivy.c		optional random rdrand_rng
+dev/random/nehemiah.c		optional random padlock_rng
 dev/sbni/if_sbni.c		optional sbni
 dev/sbni/if_sbni_isa.c		optional sbni isa
 dev/sbni/if_sbni_pci.c		optional sbni pci


Property changes on: trunk/sys/conf/files.i386
___________________________________________________________________
Deleted: cvs2svn:cvs-rev
## -1 +0,0 ##
-1.13
\ No newline at end of property
Index: trunk/sys/conf/options
===================================================================
--- trunk/sys/conf/options	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/conf/options	2014-10-03 03:58:09 UTC (rev 6820)

Property changes on: trunk/sys/conf/options
___________________________________________________________________
Deleted: cvs2svn:cvs-rev
## -1 +0,0 ##
-1.16
\ No newline at end of property
Modified: trunk/sys/conf/options.amd64
===================================================================
--- trunk/sys/conf/options.amd64	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/conf/options.amd64	2014-10-03 03:58:09 UTC (rev 6820)
@@ -68,3 +68,7 @@
 
 # options for the Intel C600 SAS driver (isci)
 ISCI_LOGGING	opt_isci.h
+
+# hw random number generators for random(4)
+PADLOCK_RNG		opt_cpu.h
+RDRAND_RNG		opt_cpu.h


Property changes on: trunk/sys/conf/options.amd64
___________________________________________________________________
Deleted: cvs2svn:cvs-rev
## -1 +0,0 ##
-1.4
\ No newline at end of property
Modified: trunk/sys/conf/options.i386
===================================================================
--- trunk/sys/conf/options.i386	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/conf/options.i386	2014-10-03 03:58:09 UTC (rev 6820)
@@ -123,3 +123,7 @@
 
 # options for the Intel C600 SAS driver (isci)
 ISCI_LOGGING	opt_isci.h
+
+# hw random number generators for random(4)
+PADLOCK_RNG		opt_cpu.h
+RDRAND_RNG		opt_cpu.h


Property changes on: trunk/sys/conf/options.i386
___________________________________________________________________
Deleted: cvs2svn:cvs-rev
## -1 +0,0 ##
-1.4
\ No newline at end of property
Added: trunk/sys/dev/random/ivy.c
===================================================================
--- trunk/sys/dev/random/ivy.c	                        (rev 0)
+++ trunk/sys/dev/random/ivy.c	2014-10-03 03:58:09 UTC (rev 6820)
@@ -0,0 +1,117 @@
+/*-
+ * Copyright (c) 2012 Konstantin Belousov <kib at FreeBSD.org>
+ * All rights reserved.
+ *
+ * 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
+ *    in this position and unchanged.
+ * 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 ``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 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>
+__FBSDID("$FreeBSD: release/9.2.0/sys/dev/random/ivy.c 240950 2012-09-26 09:22:28Z kib $");
+
+#include "opt_cpu.h"
+
+#ifdef RDRAND_RNG
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/selinfo.h>
+#include <sys/systm.h>
+#include <dev/random/randomdev.h>
+
+#define	RETRY_COUNT	10
+
+static void random_ivy_init(void);
+static void random_ivy_deinit(void);
+static int random_ivy_read(void *, int);
+
+struct random_systat random_ivy = {
+	.ident = "Hardware, Intel IvyBridge+ RNG",
+	.init = random_ivy_init,
+	.deinit = random_ivy_deinit,
+	.read = random_ivy_read,
+	.write = (random_write_func_t *)random_null_func,
+	.reseed = (random_reseed_func_t *)random_null_func,
+	.seeded = 1,
+};
+
+static inline int
+ivy_rng_store(long *tmp)
+{
+#ifdef __GNUCLIKE_ASM
+	uint32_t count;
+
+	__asm __volatile(
+#ifdef __amd64__
+	    ".byte\t0x48,0x0f,0xc7,0xf0\n\t" /* rdrand %rax */
+	    "jnc\t1f\n\t"
+	    "movq\t%%rax,%1\n\t"
+	    "movl\t$8,%%eax\n"
+#else /* i386 */
+	    ".byte\t0x0f,0xc7,0xf0\n\t" /* rdrand %eax */
+	    "jnc\t1f\n\t"
+	    "movl\t%%eax,%1\n\t"
+	    "movl\t$4,%%eax\n"
+#endif
+	    "1:\n"	/* %eax is cleared by processor on failure */
+	    : "=a" (count), "=g" (*tmp) : "a" (0) : "cc");
+	return (count);
+#else /* __GNUCLIKE_ASM */
+	return (0);
+#endif
+}
+
+static void
+random_ivy_init(void)
+{
+}
+
+void
+random_ivy_deinit(void)
+{
+}
+
+static int
+random_ivy_read(void *buf, int c)
+{
+	char *b;
+	long tmp;
+	int count, res, retry;
+
+	for (count = c, b = buf; count > 0; count -= res, b += res) {
+		for (retry = 0; retry < RETRY_COUNT; retry++) {
+			res = ivy_rng_store(&tmp);
+			if (res != 0)
+				break;
+		}
+		if (res == 0)
+			break;
+		if (res > count)
+			res = count;
+		memcpy(b, &tmp, res);
+	}
+	return (c - count);
+}
+
+#endif


Property changes on: trunk/sys/dev/random/ivy.c
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Modified: trunk/sys/dev/random/nehemiah.c
===================================================================
--- trunk/sys/dev/random/nehemiah.c	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/dev/random/nehemiah.c	2014-10-03 03:58:09 UTC (rev 6820)
@@ -26,8 +26,12 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: release/9.2.0/sys/dev/random/nehemiah.c 240950 2012-09-26 09:22:28Z kib $");
 
+#include "opt_cpu.h"
+
+#ifdef PADLOCK_RNG
+
 #include <sys/param.h>
 #include <sys/time.h>
 #include <sys/lock.h>
@@ -203,3 +207,5 @@
 	mtx_unlock(&random_nehemiah_mtx);
 	return (c);
 }
+
+#endif

Deleted: trunk/sys/dev/random/nehemiah.h
===================================================================
--- trunk/sys/dev/random/nehemiah.h	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/dev/random/nehemiah.h	2014-10-03 03:58:09 UTC (rev 6820)
@@ -1,29 +0,0 @@
-/*-
- * Copyright (c) 2000-2004 Mark R V Murray
- * All rights reserved.
- *
- * 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
- *    in this position and unchanged.
- * 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 ``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 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.
- *
- * $MidnightBSD$
- */
-
-extern struct random_systat random_nehemiah;

Modified: trunk/sys/dev/random/probe.c
===================================================================
--- trunk/sys/dev/random/probe.c	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/dev/random/probe.c	2014-10-03 03:58:09 UTC (rev 6820)
@@ -26,8 +26,12 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: release/9.2.0/sys/dev/random/probe.c 240950 2012-09-26 09:22:28Z kib $");
 
+#if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
+#include "opt_cpu.h"
+#endif
+
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -35,7 +39,6 @@
 #include <sys/malloc.h>
 #include <sys/random.h>
 #include <sys/selinfo.h>
-#include <sys/stdint.h>
 #include <sys/sysctl.h>
 
 #if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
@@ -47,8 +50,16 @@
 
 #include <dev/random/randomdev.h>
 #include <dev/random/randomdev_soft.h>
-#include <dev/random/nehemiah.h>
 
+#if defined(__amd64__) || defined(__i386__)
+#ifdef PADLOCK_RNG
+extern struct random_systat random_nehemiah;
+#endif
+#ifdef RDRAND_RNG
+extern struct random_systat random_ivy;
+#endif
+#endif
+
 void
 random_ident_hardware(struct random_systat *systat)
 {
@@ -57,14 +68,26 @@
 	*systat = random_yarrow;
 
 	/* Then go looking for hardware */
-#if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
+#if defined(__amd64__) || defined(__i386__)
+#ifdef PADLOCK_RNG
 	if (via_feature_rng & VIA_HAS_RNG) {
 		int enable;
 
-		enable = 0;
+		enable = 1;
 		TUNABLE_INT_FETCH("hw.nehemiah_rng_enable", &enable);
 		if (enable)
 			*systat = random_nehemiah;
 	}
 #endif
+#ifdef RDRAND_RNG
+	if (cpu_feature2 & CPUID2_RDRAND) {
+		int enable;
+
+		enable = 1;
+		TUNABLE_INT_FETCH("hw.ivy_rng_enable", &enable);
+		if (enable)
+			*systat = random_ivy;
+	}
+#endif
+#endif
 }

Modified: trunk/sys/dev/random/randomdev_soft.c
===================================================================
--- trunk/sys/dev/random/randomdev_soft.c	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/dev/random/randomdev_soft.c	2014-10-03 03:58:09 UTC (rev 6820)
@@ -366,6 +366,8 @@
 		selwakeuppri(&random_systat.rsel, PUSER);
 		wakeup(&random_systat);
 	}
+	(void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE,
+	    ARC4_ENTR_HAVE);
 }
 
 static int

Modified: trunk/sys/modules/random/Makefile
===================================================================
--- trunk/sys/modules/random/Makefile	2014-10-03 03:57:31 UTC (rev 6819)
+++ trunk/sys/modules/random/Makefile	2014-10-03 03:58:09 UTC (rev 6820)
@@ -1,4 +1,4 @@
-# $FreeBSD$
+# $FreeBSD: release/9.2.0/sys/modules/random/Makefile 240950 2012-09-26 09:22:28Z kib $
 
 .PATH: ${.CURDIR}/../../dev/random
 .PATH: ${.CURDIR}/../../crypto/rijndael
@@ -8,10 +8,11 @@
 SRCS=	randomdev.c probe.c
 .if ${MACHINE} == "amd64" || ${MACHINE} == "i386"
 SRCS+=	nehemiah.c
+SRCS+=	ivy.c
 .endif
 SRCS+=	randomdev_soft.c yarrow.c hash.c
 SRCS+=	rijndael-alg-fst.c rijndael-api-fst.c sha2.c
-SRCS+=	bus_if.h device_if.h vnode_if.h
+SRCS+=	bus_if.h device_if.h vnode_if.h opt_cpu.h
 
 CFLAGS+= -I${.CURDIR}/../..
 


Property changes on: trunk/sys/modules/random/Makefile
___________________________________________________________________
Deleted: cvs2svn:cvs-rev
## -1 +0,0 ##
-1.1.1.2
\ No newline at end of property


More information about the Midnightbsd-cvs mailing list