feat(rme): add tests for FEAT_MPAM on Realms

Signed-off-by: Javier Almansa Sobrino <javier.almansasobrino@arm.com>
Change-Id: I6e138cbf121793bdaaa3a44824c0dbff74daced1
diff --git a/realm/include/realm_helpers.h b/realm/include/realm_helpers.h
index 7ed8f9b..c5af1be 100644
--- a/realm/include/realm_helpers.h
+++ b/realm/include/realm_helpers.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2024, Arm Limited. All rights reserved.
+ * Copyright (c) 2024-2025, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -12,6 +12,20 @@
 
 /* Generate 64-bit random number */
 unsigned long long realm_rand64(void);
+
+/* Reset the undefined aborts counter */
+void realm_reset_undef_abort_count(void);
+
+/* Return the undefined aborts counter value */
+unsigned int realm_get_undef_abort_count(void);
+
+/*
+ * Sync exception handler.
+ * If the exception is an undefined abort, it increases the value
+ * of the abort counter and returns 'true'. Otherwise, it returns 'false'
+ */
+bool realm_sync_exception_handler(void);
+
 /*
  * Function to enter Aux Plane from Primary Plane
  * arg1 == plane index
diff --git a/realm/include/realm_tests.h b/realm/include/realm_tests.h
index 2c1d3a1..0f8a984 100644
--- a/realm/include/realm_tests.h
+++ b/realm/include/realm_tests.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023, Arm Limited. All rights reserved.
+ * Copyright (c) 2023-2025, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -30,6 +30,7 @@
 bool test_realm_sctlr2_ease(void);
 bool test_realm_attestation(void);
 bool test_realm_attestation_fault(void);
+bool test_realm_mpam_undef_abort(void);
 
 #endif /* REALM_TESTS_H */
 
diff --git a/realm/realm.mk b/realm/realm.mk
index 7d21ed5..6281779 100644
--- a/realm/realm.mk
+++ b/realm/realm.mk
@@ -41,6 +41,7 @@
 	realm_rsi.c							\
 	realm_shared_data.c						\
 	realm_simd.c							\
+	realm_mpam.c							\
 	)
 
 REALM_SOURCES += lib/${ARCH}/cache_helpers.S				\
diff --git a/realm/realm_helpers.c b/realm/realm_helpers.c
index bf47efc..f11e2f3 100644
--- a/realm/realm_helpers.c
+++ b/realm/realm_helpers.c
@@ -1,15 +1,19 @@
 /*
- * Copyright (c) 2024, Arm Limited. All rights reserved.
+ * Copyright (c) 2024-2025, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
+#include <debug.h>
 #include <stdlib.h>
+
 #include <realm_helpers.h>
 #include <realm_psi.h>
 #include <realm_rsi.h>
 #include <smccc.h>
 
+static unsigned int volatile realm_got_undef_abort;
+
 /* Generate 64-bit random number */
 unsigned long long realm_rand64(void)
 {
@@ -39,3 +43,27 @@
 
 	return host_cal.gprs[0];
 }
+
+bool realm_sync_exception_handler(void)
+{
+	uint64_t esr_el1 = read_esr_el1();
+
+	if (EC_BITS(esr_el1) == EC_UNKNOWN) {
+		realm_printf("received undefined abort. "
+			     "ESR_EL1: 0x%llx ELR_EL1: 0x%llx\n",
+			     esr_el1, read_elr_el1());
+		realm_got_undef_abort++;
+	}
+
+	return true;
+}
+
+void realm_reset_undef_abort_count(void)
+{
+	realm_got_undef_abort = 0U;
+}
+
+unsigned int realm_get_undef_abort_count(void)
+{
+	return realm_got_undef_abort;
+}
diff --git a/realm/realm_mpam.c b/realm/realm_mpam.c
new file mode 100644
index 0000000..913e451
--- /dev/null
+++ b/realm/realm_mpam.c
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2025, Arm Limited. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <arch.h>
+#include <arch_helpers.h>
+#include <sync.h>
+#include <realm_helpers.h>
+
+/* Check if Realm gets undefined abort when it access MPAM registers */
+bool test_realm_mpam_undef_abort(void)
+{
+	realm_reset_undef_abort_count();
+
+	/* Install exception handler to catch undefined abort */
+	register_custom_sync_exception_handler(realm_sync_exception_handler);
+	write_mpam0_el1(0UL);
+	unregister_custom_sync_exception_handler();
+
+	return (realm_get_undef_abort_count() != 0UL);
+}
diff --git a/realm/realm_payload_main.c b/realm/realm_payload_main.c
index aeadb9e..1e24f21 100644
--- a/realm/realm_payload_main.c
+++ b/realm/realm_payload_main.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022-2024, Arm Limited. All rights reserved.
+ * Copyright (c) 2022-2025, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -390,6 +390,13 @@
 		case REALM_PLANE_N_REG_RW_CMD:
 			test_succeed = test_realm_enter_plane_n_reg_rw();
 			break;
+		case REALM_MPAM_ACCESS:
+			test_succeed = test_realm_mpam_undef_abort();
+			break;
+		case REALM_MPAM_PRESENT:
+			/* FEAT_MPAM must be hidden to the Realm */
+			test_succeed = !is_feat_mpam_supported();
+			break;
 		case REALM_MULTIPLE_REC_PSCI_DENIED_CMD:
 			test_succeed = test_realm_multiple_rec_psci_denied_cmd();
 			break;
diff --git a/realm/realm_simd.c b/realm/realm_simd.c
index 2eb3eab..0ff1710 100644
--- a/realm/realm_simd.c
+++ b/realm/realm_simd.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023, Arm Limited. All rights reserved.
+ * Copyright (c) 2023-2025, Arm Limited. All rights reserved.
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
@@ -9,9 +9,11 @@
 #include <assert.h>
 #include <debug.h>
 #include <stdlib.h>
+
 #include <sync.h>
 #include <lib/extensions/fpu.h>
 #include <lib/extensions/sve.h>
+#include <realm_helpers.h>
 
 #include <host_realm_simd.h>
 #include <host_shared_data.h>
@@ -34,8 +36,6 @@
 static fpu_cs_regs_t rl_fpu_cs_regs_write;
 static fpu_cs_regs_t rl_fpu_cs_regs_read;
 
-static int volatile realm_got_undef_abort;
-
 /* Returns the maximum supported VL. This test is called only by sve Realm */
 bool test_realm_sve_rdvl(void)
 {
@@ -188,35 +188,17 @@
 	return rc;
 }
 
-static bool realm_sync_exception_handler(void)
-{
-	uint64_t esr_el1 = read_esr_el1();
-
-	if (EC_BITS(esr_el1) == EC_UNKNOWN) {
-		realm_printf("received undefined abort. "
-			     "esr_el1: 0x%llx elr_el1: 0x%llx\n",
-			     esr_el1, read_elr_el1());
-		realm_got_undef_abort++;
-	}
-
-	return true;
-}
-
 /* Check if Realm gets undefined abort when it accesses SVE functionality */
 bool test_realm_sve_undef_abort(void)
 {
-	realm_got_undef_abort = 0UL;
+	realm_reset_undef_abort_count();
 
-	/* install exception handler to catch undef abort */
+	/* Install exception handler to catch undefined abort */
 	register_custom_sync_exception_handler(&realm_sync_exception_handler);
 	(void)sve_rdvl_1();
 	unregister_custom_sync_exception_handler();
 
-	if (realm_got_undef_abort == 0UL) {
-		return false;
-	}
-
-	return true;
+	return (realm_get_undef_abort_count() != 0U);
 }
 
 /* Reads and returns the ID_AA64PFR1_EL1 and ID_AA64SMFR0_EL1 registers */
@@ -240,16 +222,12 @@
 /* Check if Realm gets undefined abort when it access SME functionality */
 bool test_realm_sme_undef_abort(void)
 {
-	realm_got_undef_abort = 0UL;
+	realm_reset_undef_abort_count();
 
-	/* install exception handler to catch undef abort */
+	/* Install exception handler to catch undefined abort */
 	register_custom_sync_exception_handler(&realm_sync_exception_handler);
 	(void)read_svcr();
 	unregister_custom_sync_exception_handler();
 
-	if (realm_got_undef_abort == 0UL) {
-		return false;
-	}
-
-	return true;
+	return (realm_get_undef_abort_count() != 0U);
 }