From: deraadt Date: Thu, 18 Dec 2014 16:23:25 +0000 (+0000) Subject: If support is present, use rdrand() at resume time. Make the rdrand() X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=f187f1f0cc68068c9db5109012eae278756fa54c;p=openbsd If support is present, use rdrand() at resume time. Make the rdrand() function easier to call. ok reyk djm --- diff --git a/sys/arch/amd64/amd64/cpu.c b/sys/arch/amd64/amd64/cpu.c index 338b91a45aa..b76e7fc6545 100644 --- a/sys/arch/amd64/amd64/cpu.c +++ b/sys/arch/amd64/amd64/cpu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.c,v 1.75 2014/12/18 05:33:48 mlarkin Exp $ */ +/* $OpenBSD: cpu.c,v 1.76 2014/12/18 16:23:26 deraadt Exp $ */ /* $NetBSD: cpu.c,v 1.1 2003/04/26 18:39:26 fvdl Exp $ */ /*- @@ -110,6 +110,7 @@ int cpu_match(struct device *, void *, void *); void cpu_attach(struct device *, struct device *, void *); +int cpu_activate(struct device *, int); void patinit(struct cpu_info *ci); struct cpu_softc { @@ -192,7 +193,7 @@ struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL, #endif /* MULTIPROCESSOR */ struct cfattach cpu_ca = { - sizeof(struct cpu_softc), cpu_match, cpu_attach + sizeof(struct cpu_softc), cpu_match, cpu_attach, NULL, cpu_activate }; struct cfdriver cpu_cd = { @@ -938,6 +939,7 @@ void rdrand(void *v) { struct timeout *tmo = v; + extern int has_rdrand; union { uint64_t u64; uint32_t u32[2]; @@ -945,6 +947,8 @@ rdrand(void *v) uint64_t valid; int i; + if (has_rdrand == 0) + return; for (i = 0; i < 2; i++) { __asm volatile( "xor %1, %1\n\t" @@ -958,5 +962,21 @@ rdrand(void *v) } } - timeout_add_msec(tmo, 10); + if (tmo) + timeout_add_msec(tmo, 10); +} + +int +cpu_activate(struct device *self, int act) +{ + struct cpu_softc *sc = (struct cpu_softc *)self; + + switch (act) { + case DVACT_RESUME: + if (sc->sc_info->ci_cpuid == 0) + rdrand(NULL); + break; + } + + return (0); } diff --git a/sys/arch/i386/i386/cpu.c b/sys/arch/i386/i386/cpu.c index 9c99e8c0df3..3c613ca6cb7 100644 --- a/sys/arch/i386/i386/cpu.c +++ b/sys/arch/i386/i386/cpu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.c,v 1.58 2014/12/14 05:04:49 guenther Exp $ */ +/* $OpenBSD: cpu.c,v 1.59 2014/12/18 16:23:25 deraadt Exp $ */ /* $NetBSD: cpu.c,v 1.1.2.7 2000/06/26 02:04:05 sommerfeld Exp $ */ /*- @@ -107,6 +107,7 @@ int cpu_match(struct device *, void *, void *); void cpu_attach(struct device *, struct device *, void *); +int cpu_activate(struct device *, int); void patinit(struct cpu_info *ci); void cpu_idle_mwait_cycle(void); void cpu_init_mwait(struct device *); @@ -155,7 +156,7 @@ cpu_init_first() #endif struct cfattach cpu_ca = { - sizeof(struct cpu_info), cpu_match, cpu_attach + sizeof(struct cpu_info), cpu_match, cpu_attach, NULL, cpu_activate }; struct cfdriver cpu_cd = { @@ -483,9 +484,12 @@ void rdrand(void *v) { struct timeout *tmo = v; + extern int has_rdrand; uint32_t r, valid; int i; + if (has_rdrand == 0) + return; for (i = 0; i < 4; i++) { __asm volatile( "xor %1, %1\n\t" @@ -496,7 +500,23 @@ rdrand(void *v) add_true_randomness(r); } - timeout_add_msec(tmo, 10); + if (tmo) + timeout_add_msec(tmo, 10); +} + +int +cpu_activate(struct device *self, int act) +{ + struct cpu_info *sc = (struct cpu_info *)self; + + switch (act) { + case DVACT_RESUME: + if (sc->ci_cpuid == 0) + rdrand(NULL); + break; + } + + return (0); } #ifdef MULTIPROCESSOR