feat(cm): add test to validate EL2 regs during context switch
Verify that EL2 system registers are preserved when switching
from Normal world to Secure world and vice versa. Do this by
modifying the live EL2 register state and dumping it to memory,
then performing an FF-A Cactus call and checking whether the
state matches the previously saved context.
Change-Id: I0537b4d671c72c0a2fd29ac7e218bf69e1c66001
Signed-off-by: Igor Podgainõi <igor.podgainoi@arm.com>
diff --git a/tftf/framework/framework.mk b/tftf/framework/framework.mk
index 3bff5ed..fc0ccbd 100644
--- a/tftf/framework/framework.mk
+++ b/tftf/framework/framework.mk
@@ -98,7 +98,8 @@
ifeq (${ARCH},aarch64)
# Context Management Library support files
FRAMEWORK_SOURCES += \
- lib/context_mgmt/aarch64/context_el1.c
+ lib/context_mgmt/aarch64/context_el1.c \
+ lib/context_mgmt/aarch64/context_el2.c
endif
TFTF_LINKERFILE := tftf/framework/tftf.ld.S
diff --git a/tftf/tests/context_mgmt_tests/el2/test_spm_el2_context_mgmt.c b/tftf/tests/context_mgmt_tests/el2/test_spm_el2_context_mgmt.c
new file mode 100644
index 0000000..457e650
--- /dev/null
+++ b/tftf/tests/context_mgmt_tests/el2/test_spm_el2_context_mgmt.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2024 Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <cactus_test_cmds.h>
+#include <ffa_endpoints.h>
+#include <lib/context_mgmt/context_el2.h>
+#include <spm_test_helpers.h>
+#include <test_helpers.h>
+#include <tftf.h>
+#include <tftf_lib.h>
+
+static const struct ffa_uuid expected_sp_uuids[] = {
+ {PRIMARY_UUID}, {SECONDARY_UUID}, {TERTIARY_UUID}
+};
+
+/*
+ * This test aims to validate EL2 system registers are restored to previously saved
+ * values upon returning from context switch triggered by an FF-A direct
+ * message request to an SP.
+ */
+test_result_t test_spm_el2_regs_ctx_mgmt(void)
+{
+ struct ffa_value ret;
+ el2_sysregs_t ctx_original = {0}, ctx_expected = {0}, ctx_actual = {0};
+
+ /* Check SPMC has ffa_version and expected FFA endpoints are deployed. */
+ CHECK_SPMC_TESTING_SETUP(1, 0, expected_sp_uuids);
+
+ /* Save the original values of EL2 system registers. */
+ el2_save_registers(&ctx_original);
+
+ /* Corrupt the EL2 system registers and save the expected values. */
+ el2_modify_registers(&ctx_original, CORRUPT_REGS);
+ el2_save_registers(&ctx_expected);
+
+ /* Send a message to SP1 through direct messaging. */
+ ret = cactus_echo_send_cmd(HYP_ID, SP_ID(1), ECHO_VAL1);
+
+ if (cactus_get_response(ret) != CACTUS_SUCCESS ||
+ cactus_echo_get_val(ret) != ECHO_VAL1 ||
+ !is_ffa_direct_response(ret)) {
+ return TEST_RESULT_FAIL;
+ }
+
+ /* Save the modified values of EL2 system registers after the FF-A command. */
+ el2_save_registers(&ctx_actual);
+
+ /* Restore the EL2 system registers to their original state. */
+ el2_modify_registers(&ctx_original, RESTORE_REGS);
+
+ if (memcmp(&ctx_expected, &ctx_actual, sizeof(el2_sysregs_t))) {
+ ERROR("Expected vs actual EL2 register contexts differ.\n\n");
+ el2_dump_register_context("Expected", &ctx_expected);
+ el2_dump_register_context("Actual", &ctx_actual);
+ return TEST_RESULT_FAIL;
+ }
+
+ return TEST_RESULT_SUCCESS;
+}
diff --git a/tftf/tests/tests-cpu-context-mgmt.mk b/tftf/tests/tests-cpu-context-mgmt.mk
index 0919f5f..c94dc37 100644
--- a/tftf/tests/tests-cpu-context-mgmt.mk
+++ b/tftf/tests/tests-cpu-context-mgmt.mk
@@ -4,6 +4,9 @@
# SPDX-License-Identifier: BSD-3-Clause
#
+include tftf/tests/tests-spm.mk
+
TESTS_SOURCES += $(addprefix tftf/tests/, \
context_mgmt_tests/el1/test_tsp_el1_context_mgmt.c \
+ context_mgmt_tests/el2/test_spm_el2_context_mgmt.c \
)
diff --git a/tftf/tests/tests-cpu-context-mgmt.xml b/tftf/tests/tests-cpu-context-mgmt.xml
index eb86f08..abdf5d9 100644
--- a/tftf/tests/tests-cpu-context-mgmt.xml
+++ b/tftf/tests/tests-cpu-context-mgmt.xml
@@ -10,5 +10,7 @@
<testsuite name="CPU Context Management" description="Validate CPU context switch between NWd and SWd">
<testcase name="Test EL1 regs preserved across context switch with TSP"
function="test_tsp_el1_regs_ctx_mgmt" />
+ <testcase name="Test EL2 regs preserved across context switch with SPM"
+ function="test_spm_el2_regs_ctx_mgmt" />
</testsuite>
</testsuites>