1From 8d9a62a5fa89001266352a929c5d40b28c0dda85 Mon Sep 17 00:00:00 2001
2From: Matt Weber <matthew.weber@rockwellcollins.com>
3Date: Fri, 12 Jan 2018 19:07:27 -0600
4Subject: [PATCH v2] kvm-unit-tests: test for rdseed/rdrand
5
6The build fails when the host binutils isn't at least 2.23
7(2.22.x introduced RDSEED).
8
9Fixes:
10http://autobuild.buildroot.net/results/c39/c3987a3cbd2960b0ff50f872636bdfd8d1a9c820/
11
12Upstream:
13https://marc.info/?l=kvm&m=151580743523259&w=2
14
15Signed-off-by: Matthew Weber <matthew.weber@rockwellcollins.com>
16---
17 Makefile        |  2 +-
18 configure       | 18 ++++++++++++++++++
19 x86/vmx_tests.c |  6 ++++++
20 3 files changed, 25 insertions(+), 1 deletion(-)
21
22diff --git a/Makefile b/Makefile
23index d9ad42b..799e9b5 100644
24--- a/Makefile
25+++ b/Makefile
26@@ -50,7 +50,7 @@ include $(SRCDIR)/$(TEST_DIR)/Makefile
27 cc-option = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \
28               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
29
30-COMMON_CFLAGS += -g $(autodepend-flags)
31+COMMON_CFLAGS += -g $(autodepend-flags) $(EXTRA_CFLAGS)
32 COMMON_CFLAGS += -Wall -Wwrite-strings -Wclobbered -Wempty-body -Wuninitialized
33 COMMON_CFLAGS += -Wignored-qualifiers -Wunused-but-set-parameter
34 frame-pointer-flag=-f$(if $(KEEP_FRAME_POINTER),no-,)omit-frame-pointer
35diff --git a/configure b/configure
36index dd9d361..21c0219 100755
37--- a/configure
38+++ b/configure
39@@ -171,6 +171,23 @@ mkdir -p lib
40 ln -sf "$asm" lib/asm
41
42
43+cat > rd_test.c <<EOF
44+#include <stdint.h>
45+int main() {
46+   uint16_t seed=0;
47+   unsigned char ok;
48+   asm volatile ("rdseed %0; setc %1"
49+                 : "=r" (seed), "=qm" (ok));
50+   return ok;
51+}
52+EOF
53+if $cross_prefix$cc -o /dev/null rd_test.c &> /dev/null; then
54+  echo "Checking for rdseed/rdrand... Yes."
55+else
56+  echo "Checking for rdseed/rdrand... No."
57+  extra_cflags="-DNO_RDSEEDRAND"
58+fi
59+
60 # create the config
61 cat <<EOF > config.mak
62 SRCDIR=$srcdir
63@@ -181,6 +198,7 @@ ARCH_NAME=$arch_name
64 PROCESSOR=$processor
65 CC=$cross_prefix$cc
66 CXX=$cross_prefix$cxx
67+EXTRA_CFLAGS=$extra_cflags
68 LD=$cross_prefix$ld
69 OBJCOPY=$cross_prefix$objcopy
70 OBJDUMP=$cross_prefix$objdump
71diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c
72index 4a3e94b..2cbe3eb 100644
73--- a/x86/vmx_tests.c
74+++ b/x86/vmx_tests.c
75@@ -770,8 +770,10 @@ asm(
76 	"insn_sldt: sldt %ax;ret\n\t"
77 	"insn_lldt: xor %eax, %eax; lldt %ax;ret\n\t"
78 	"insn_str: str %ax;ret\n\t"
79+#ifndef NO_RDSEEDRAND
80 	"insn_rdrand: rdrand %rax;ret\n\t"
81 	"insn_rdseed: rdseed %rax;ret\n\t"
82+#endif
83 );
84 extern void insn_hlt();
85 extern void insn_invlpg();
86@@ -796,8 +798,10 @@ extern void insn_lldt();
87 extern void insn_str();
88 extern void insn_cpuid();
89 extern void insn_invd();
90+#ifndef NO_RDSEEDRAND
91 extern void insn_rdrand();
92 extern void insn_rdseed();
93+#endif
94
95 u32 cur_insn;
96 u64 cr3;
97@@ -853,8 +857,10 @@ static struct insn_table insn_table[] = {
98 	{"DESC_TABLE (LLDT)", CPU_DESC_TABLE, insn_lldt, INSN_CPU1, 47, 0, 0, 0},
99 	{"DESC_TABLE (STR)", CPU_DESC_TABLE, insn_str, INSN_CPU1, 47, 0, 0, 0},
100 	/* LTR causes a #GP if done with a busy selector, so it is not tested.  */
101+#ifndef NO_RDSEEDRAND
102 	{"RDRAND", CPU_RDRAND, insn_rdrand, INSN_CPU1, VMX_RDRAND, 0, 0, 0},
103 	{"RDSEED", CPU_RDSEED, insn_rdseed, INSN_CPU1, VMX_RDSEED, 0, 0, 0},
104+#endif
105 	// Instructions always trap
106 	{"CPUID", 0, insn_cpuid, INSN_ALWAYS_TRAP, 10, 0, 0, 0},
107 	{"INVD", 0, insn_invd, INSN_ALWAYS_TRAP, 13, 0, 0, 0},
108--
1091.9.1
110
111