From da4486d93ab21ae2a719244a40ec897d94b62332 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Sun, 20 Nov 2016 00:48:22 +0100 Subject: [PATCH] add rdrand support If available, HermitCore supports rdrand to generate random number. LwIP uses this instruction to support DNSSec. --- hermit/arch/x86/include/asm/processor.h | 21 +++++++++++++++++++++ hermit/arch/x86/kernel/processor.c | 3 ++- hermit/lwip | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/hermit/arch/x86/include/asm/processor.h b/hermit/arch/x86/include/asm/processor.h index 5b21f28c8..b8d672cc7 100644 --- a/hermit/arch/x86/include/asm/processor.h +++ b/hermit/arch/x86/include/asm/processor.h @@ -76,6 +76,7 @@ extern "C" { #define CPU_FEATURE_XSAVE (1 << 26) #define CPU_FEATURE_OSXSAVE (1 << 27) #define CPU_FEATURE_AVX (1 << 28) +#define CPU_FEATURE_RDRAND (1 << 30) #define CPU_FEATURE_HYPERVISOR (1 << 31) // CPUID.80000001H:EDX feature list @@ -389,6 +390,10 @@ inline static uint32_t has_avx(void) { return (cpu_info.feature2 & CPU_FEATURE_AVX); } +inline static uint32_t has_rdrand(void) { + return (cpu_info.feature2 & CPU_FEATURE_RDRAND); +} + inline static uint32_t on_hypervisor(void) { return (cpu_info.feature2 & CPU_FEATURE_HYPERVISOR); } @@ -452,6 +457,22 @@ static inline void clts(void) asm volatile("clts"); } +/** @brief Read a random number + * + * Returns a hardware generated random value. + */ +inline static uint32_t rdrand(void) +{ + uint32_t val; + uint8_t rc; + + do { + asm volatile("rdrand %0 ; setc %1" : "=r" (val), "=qm" (rc)); + } while(rc == 0); // rc == 0: underflow + + return val; +} + /** @brief Read out time stamp counter * * The rdtsc instruction puts a 64 bit time stamp value diff --git a/hermit/arch/x86/kernel/processor.c b/hermit/arch/x86/kernel/processor.c index 832d7b2ff..ac37161fd 100644 --- a/hermit/arch/x86/kernel/processor.c +++ b/hermit/arch/x86/kernel/processor.c @@ -554,7 +554,7 @@ int cpu_detection(void) { a = b = c = d = 0; cpuid(1, &a, &b, &cpu_info.feature2, &cpu_info.feature1); - LOG_INFO("CPU features: %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", + LOG_INFO("CPU features: %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", has_sse() ? "SSE " : "", has_sse2() ? "SSE2 " : "", has_sse3() ? "SSE3 " : "", @@ -562,6 +562,7 @@ int cpu_detection(void) { has_sse4_2() ? "SSE4.2 " : "", has_avx() ? "AVX " : "", has_avx2() ? "AVX2 " : "", + has_rdrand() ? "RDRAND " : "", has_fma() ? "FMA " : "", has_movbe() ? "MOVBE " : "", has_x2apic() ? "X2APIC " : "", diff --git a/hermit/lwip b/hermit/lwip index 594aea714..3dbc27233 160000 --- a/hermit/lwip +++ b/hermit/lwip @@ -1 +1 @@ -Subproject commit 594aea71498c7c11850186f9b7d85e95ee34ac68 +Subproject commit 3dbc272333882dd02029ef9c45bbcb4c59ee7a6d