feat(rng_trap): add tests for FEAT_RNG_TRAP

Added 2 tests that expect a trap to be triggered when a read is
performed on:
1. RNDR register
2. RNDRRS register
The result will be a panic signal and the whole system will halt,
as there is no handler set for such trap.

Signed-off-by: Juan Pablo Conde <juanpablo.conde@arm.com>
Change-Id: Ia979e60a106b394cc09dfdf94115354fb72142d1
diff --git a/include/common/test_helpers.h b/include/common/test_helpers.h
index c51c785..b1349dd 100644
--- a/include/common/test_helpers.h
+++ b/include/common/test_helpers.h
@@ -334,6 +334,15 @@
 		}								\
 	} while (false)
 
+#define SKIP_TEST_IF_RNG_TRAP_NOT_SUPPORTED()					\
+	do {									\
+		if (!is_feat_rng_trap_present()) {				\
+			tftf_testcase_printf("ARMv8.5-RNG_TRAP not"		\
+ 					"supported\n");				\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+	} while (false)
+
 /* Helper macro to verify if system suspend API is supported */
 #define is_psci_sys_susp_supported()	\
 		(tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND)		\
diff --git a/include/lib/aarch64/arch.h b/include/lib/aarch64/arch.h
index c5c94fb..0c36785 100644
--- a/include/lib/aarch64/arch.h
+++ b/include/lib/aarch64/arch.h
@@ -292,6 +292,12 @@
 #define ID_AA64PFR1_EL1_MTE_SHIFT	U(8)
 #define ID_AA64PFR1_EL1_MTE_MASK	ULL(0xf)
 
+#define ID_AA64PFR1_EL1_RNDR_TRAP_SHIFT	U(28)
+#define ID_AA64PFR1_EL1_RNDR_TRAP_MASK	ULL(0xf)
+
+#define ID_AA64PFR1_EL1_RNG_TRAP_SUPPORTED	ULL(0x1)
+#define ID_AA64PFR1_EL1_RNG_TRAP_NOT_SUPPORTED	ULL(0x0)
+
 #define MTE_UNIMPLEMENTED	ULL(0)
 #define MTE_IMPLEMENTED_EL0	ULL(1)	/* MTE is only implemented at EL0 */
 #define MTE_IMPLEMENTED_ELX	ULL(2)	/* MTE is implemented at all ELs */
diff --git a/include/lib/aarch64/arch_features.h b/include/lib/aarch64/arch_features.h
index 46d12c9..1d90138 100644
--- a/include/lib/aarch64/arch_features.h
+++ b/include/lib/aarch64/arch_features.h
@@ -166,4 +166,11 @@
 		ID_AA64ISAR2_WFXT_MASK) == ID_AA64ISAR2_WFXT_SUPPORTED);
 }
 
+static inline bool is_feat_rng_trap_present(void)
+{
+	return (((read_id_aa64pfr1_el1() >> ID_AA64PFR1_EL1_RNDR_TRAP_SHIFT) &
+			ID_AA64PFR1_EL1_RNDR_TRAP_MASK)
+			== ID_AA64PFR1_EL1_RNG_TRAP_SUPPORTED);
+}
+
 #endif /* ARCH_FEATURES_H */
diff --git a/include/lib/aarch64/arch_helpers.h b/include/lib/aarch64/arch_helpers.h
index b366cdd..e10ddab 100644
--- a/include/lib/aarch64/arch_helpers.h
+++ b/include/lib/aarch64/arch_helpers.h
@@ -417,6 +417,10 @@
 DEFINE_SYSREG_RW_FUNCS(pmevtyper0_el0)
 DEFINE_SYSREG_READ_FUNC(pmevcntr0_el0)
 
+/* Armv8.5 FEAT_RNG Registers */
+DEFINE_SYSREG_READ_FUNC(rndr)
+DEFINE_SYSREG_READ_FUNC(rndrrs)
+
 /* GICv3 System Registers */
 
 DEFINE_RENAME_SYSREG_RW_FUNCS(icc_sre_el1, ICC_SRE_EL1)
diff --git a/tftf/tests/extensions/rng_trap/test_rndr_trap.c b/tftf/tests/extensions/rng_trap/test_rndr_trap.c
new file mode 100644
index 0000000..423c78f
--- /dev/null
+++ b/tftf/tests/extensions/rng_trap/test_rndr_trap.c
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <arch_helpers.h>
+#include <arch_features.h>
+#include <test_helpers.h>
+#include <tftf.h>
+#include <tftf_lib.h>
+
+/*
+ * This very simple test just ensures that a RNDR read access causes a trap
+ * to EL3.
+ */
+test_result_t test_rndr_trap_enabled(void)
+{
+#if defined __aarch64__
+	/* Make sure FEAT_RNG_TRAP is supported. */
+	SKIP_TEST_IF_RNG_TRAP_NOT_SUPPORTED();
+
+	/* Attempt to read RNDR. */
+	read_rndr();
+
+	/*
+	 * If we make it this far, the test fails, as there was no trap
+	 * to EL3 triggered.
+	 */
+	return TEST_RESULT_FAIL;
+#else
+	/* Skip test if AArch32 */
+	SKIP_TEST_IF_AARCH32();
+#endif
+}
diff --git a/tftf/tests/extensions/rng_trap/test_rndrrs_trap.c b/tftf/tests/extensions/rng_trap/test_rndrrs_trap.c
new file mode 100644
index 0000000..e9beb53
--- /dev/null
+++ b/tftf/tests/extensions/rng_trap/test_rndrrs_trap.c
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <arch_helpers.h>
+#include <arch_features.h>
+#include <test_helpers.h>
+#include <tftf.h>
+#include <tftf_lib.h>
+
+/*
+ * This very simple test just ensures that a RNDRRS read access causes a trap
+ * to EL3.
+ */
+test_result_t test_rndrrs_trap_enabled(void)
+{
+#if defined __aarch64__
+	/* Make sure FEAT_RNG_TRAP is supported. */
+	SKIP_TEST_IF_RNG_TRAP_NOT_SUPPORTED();
+
+	/* Attempt to read RNDR. */
+	read_rndrrs();
+
+	/*
+	 * If we make it this far, the test fails, as there was no trap
+	 * to EL3 triggered.
+	 */
+	return TEST_RESULT_FAIL;
+#else
+	/* Skip test if AArch32 */
+	SKIP_TEST_IF_AARCH32();
+#endif
+}
diff --git a/tftf/tests/tests-rndr_trap.mk b/tftf/tests/tests-rndr_trap.mk
new file mode 100644
index 0000000..147b704
--- /dev/null
+++ b/tftf/tests/tests-rndr_trap.mk
@@ -0,0 +1,9 @@
+#
+# Copyright (c) 2022, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+TESTS_SOURCES	+=	$(addprefix tftf/tests/,			\
+	extensions/rng_trap/test_rndr_trap.c				\
+)
diff --git a/tftf/tests/tests-rndr_trap.xml b/tftf/tests/tests-rndr_trap.xml
new file mode 100644
index 0000000..fa32cbf
--- /dev/null
+++ b/tftf/tests/tests-rndr_trap.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  Copyright (c) 2022, Arm Limited. All rights reserved.
+
+  SPDX-License-Identifier: BSD-3-Clause
+-->
+
+<testsuites>
+
+  <testsuite name="RNDR_TRAP" description="Tests that a read access to RNDR generates a trap to EL3.">
+    <testcase name="Test EL3 trap upon a RNDR read access" function="test_rndr_trap_enabled" />
+  </testsuite>
+
+</testsuites>
diff --git a/tftf/tests/tests-rndrrs_trap.mk b/tftf/tests/tests-rndrrs_trap.mk
new file mode 100644
index 0000000..2b2bd08
--- /dev/null
+++ b/tftf/tests/tests-rndrrs_trap.mk
@@ -0,0 +1,9 @@
+#
+# Copyright (c) 2022, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+TESTS_SOURCES	+=	$(addprefix tftf/tests/,			\
+	extensions/rng_trap/test_rndrrs_trap.c				\
+)
diff --git a/tftf/tests/tests-rndrrs_trap.xml b/tftf/tests/tests-rndrrs_trap.xml
new file mode 100644
index 0000000..bf33e31
--- /dev/null
+++ b/tftf/tests/tests-rndrrs_trap.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  Copyright (c) 2022, Arm Limited. All rights reserved.
+
+  SPDX-License-Identifier: BSD-3-Clause
+-->
+
+<testsuites>
+
+  <testsuite name="RNDRRS_TRAP" description="Tests that a read access to RNDRRS generates a trap to EL3.">
+    <testcase name="Test EL3 trap upon a RNDRRS read access" function="test_rndrrs_trap_enabled" />
+  </testsuite>
+
+</testsuites>