feat(rng_trap): modify tests for FEAT_RNG_TRAP
With the introduction of a handler for the trap generated upon an
attempt to read RNDR and RNDRRS registers, the old test will not
work anymore and needs to be changed so it does not longer expect
the system to panic but for EL3 return a valid random number after
handling the trap.
In addition, the test configs are unified as there is no need to
keep them separate since a system panic is not expected anymore.
Signed-off-by: Juan Pablo Conde <juanpablo.conde@arm.com>
Signed-off-by: Manish Pandey <manish.pandey2@arm.com>
Change-Id: I3ea4d09bbae58a906ea0be3b5448f5d3090571e7
diff --git a/tftf/tests/extensions/rng_trap/test_rndr_trap.c b/tftf/tests/extensions/rng_trap/test_rndr_trap.c
deleted file mode 100644
index 423c78f..0000000
--- a/tftf/tests/extensions/rng_trap/test_rndr_trap.c
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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
deleted file mode 100644
index e9beb53..0000000
--- a/tftf/tests/extensions/rng_trap/test_rndrrs_trap.c
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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/extensions/rng_trap/test_rng_trap.c b/tftf/tests/extensions/rng_trap/test_rng_trap.c
new file mode 100644
index 0000000..49ee6ad
--- /dev/null
+++ b/tftf/tests/extensions/rng_trap/test_rng_trap.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <arch_features.h>
+#include <arch_helpers.h>
+#include <test_helpers.h>
+#include <tftf.h>
+#include <tftf_lib.h>
+
+#define MAX_ITERATIONS_EXCLUSIVE 100
+
+/*
+ * This test ensures that a RNDR/RNDRRS instructions causes a trap to EL3 and
+ * generates a random number each time.
+ * Argument "use_rndrrs" decides whether to execute "rndrrs" or "rndr" instruction.
+ *
+ * This test usage load/store exclusive pairs to detect whether the execution
+ * trapped to EL3 or not?
+ * It relies on the fact that when exception level changes the monitor is cleared.
+ * In a load/store exclusive pair with stxr instruction, the status gets updated
+ * to '1' when monitor is cleared.
+ * In this test start with ldxr and execute trap instruction and if the trap to EL3
+ * happened then stxr status will be '1', to avoid chances of monitor being cleared
+ * (highly unlikely in this scenario) by any other reason do this test iteratively.
+ * If stxr succeds even single time we are sure that trap did not happen.
+ */
+static test_result_t test_rng_trap(bool use_rndrrs)
+{
+#if defined __aarch64__
+ u_register_t rng, rng1 = 0;
+ u_register_t exclusive;
+ u_register_t status;
+ unsigned int i;
+
+ /* Make sure FEAT_RNG_TRAP is supported. */
+ SKIP_TEST_IF_RNG_TRAP_NOT_SUPPORTED();
+
+ /*
+ * The test was inserted in a loop that runs a safe number of times
+ * in order to discard any possible trap returns other than RNG_TRAP
+ */
+ for (i = 0; i < MAX_ITERATIONS_EXCLUSIVE; i++) {
+ /* Attempt to acquire address for exclusive access */
+ __asm__ volatile ("ldxr %0, %1\n" : "=r"(rng)
+ : "Q"(exclusive));
+ if (use_rndrrs) {
+ /* Attempt to read RNDRRS. */
+ __asm__ volatile ("mrs %0, rndrrs\n" : "=r" (rng));
+ } else {
+ /* Attempt to read RNDR. */
+ __asm__ volatile ("mrs %0, rndr\n" : "=r" (rng));
+ }
+ /*
+ * After returning from the trap, the monitor variable should
+ * be cleared, so the status value should be 1.
+ */
+ __asm__ volatile ("stxr %w0, %1, %2\n" : "=&r"(status)
+ : "r"(rng), "Q"(exclusive));
+ /* If monitor is not cleared or not a new random number */
+ if ((status == 0) || (rng == rng1)) {
+ return TEST_RESULT_FAIL;
+ }
+ rng1 = rng;
+ }
+
+ return TEST_RESULT_SUCCESS;
+#else
+ /* Skip test if AArch32 */
+ SKIP_TEST_IF_AARCH32();
+#endif
+}
+
+/* Test RNDR read access causes a trap to EL3 and generates a random number each time */
+test_result_t test_rndr_rng_trap(void)
+{
+ return test_rng_trap(false);
+}
+
+/* Test RNDRRS read access causes a trap to EL3 and generates a random number each time */
+test_result_t test_rndrrs_rng_trap(void)
+{
+ return test_rng_trap(true);
+}
diff --git a/tftf/tests/tests-rndr_trap.mk b/tftf/tests/tests-rndr_trap.mk
deleted file mode 100644
index 147b704..0000000
--- a/tftf/tests/tests-rndr_trap.mk
+++ /dev/null
@@ -1,9 +0,0 @@
-#
-# 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
deleted file mode 100644
index fa32cbf..0000000
--- a/tftf/tests/tests-rndr_trap.xml
+++ /dev/null
@@ -1,15 +0,0 @@
-<?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
deleted file mode 100644
index 2b2bd08..0000000
--- a/tftf/tests/tests-rndrrs_trap.mk
+++ /dev/null
@@ -1,9 +0,0 @@
-#
-# 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
deleted file mode 100644
index bf33e31..0000000
--- a/tftf/tests/tests-rndrrs_trap.xml
+++ /dev/null
@@ -1,15 +0,0 @@
-<?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>
diff --git a/tftf/tests/tests-rng_trap.mk b/tftf/tests/tests-rng_trap.mk
new file mode 100644
index 0000000..2457b0c
--- /dev/null
+++ b/tftf/tests/tests-rng_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_rng_trap.c \
+)
diff --git a/tftf/tests/tests-rng_trap.xml b/tftf/tests/tests-rng_trap.xml
new file mode 100644
index 0000000..1f8cb90
--- /dev/null
+++ b/tftf/tests/tests-rng_trap.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+ Copyright (c) 2022, Arm Limited. All rights reserved.
+
+ SPDX-License-Identifier: BSD-3-Clause
+-->
+
+<testsuites>
+
+ <testsuite name="RNG_TRAP" description="Tests that RNDR/RRS instructions trap to EL3 and returns a random number">
+ <testcase name="Test if RNDR instruction traps to el3 and gets a random number" function="test_rndr_rng_trap" />
+ <testcase name="Test if RNDRSS instruction traps to el3 and gets a random number" function="test_rndrrs_rng_trap" />
+ </testsuite>
+
+</testsuites>