fix(lib/xlat): Assertions for `is_mmu_enabled`.
The runtime checks for some of the xlat APIs for
is_mmu_enabled() are replaced with assertions so as to make it
consistent across the RMM codebase.
Also, added assertions to some APIs to ensure that the
preconditions for calling the API are adhered to.
Modified the unit tests to check the above added assertion.
Signed-off-by: Soby Mathew <soby.mathew@arm.com>
Change-Id: I03aecef79a8dd259a0755808b16f0218d44f4180
diff --git a/lib/xlat/tests/xlat_tests_base.h b/lib/xlat/tests/xlat_tests_base.h
index c16218f..7152020 100644
--- a/lib/xlat/tests/xlat_tests_base.h
+++ b/lib/xlat/tests/xlat_tests_base.h
@@ -24,6 +24,7 @@
void xlat_ctx_cfg_init_tc11(void);
void xlat_ctx_cfg_init_tc12(void);
void xlat_ctx_cfg_init_tc13(void);
+void xlat_ctx_cfg_init_tc14(void);
void xlat_ctx_init_tc1(void);
void xlat_ctx_init_tc2(void);
@@ -60,6 +61,7 @@
void xlat_arch_setup_mmu_cfg_tc3(void);
void xlat_arch_setup_mmu_cfg_tc4(void);
void xlat_arch_setup_mmu_cfg_tc5(void);
+void xlat_arch_setup_mmu_cfg_tc6(void);
void xlat_get_oa_from_tte_tc1(void);
diff --git a/lib/xlat/tests/xlat_tests_base_g1.cpp b/lib/xlat/tests/xlat_tests_base_g1.cpp
index 473e967..8a4f62c 100644
--- a/lib/xlat/tests/xlat_tests_base_g1.cpp
+++ b/lib/xlat/tests/xlat_tests_base_g1.cpp
@@ -1004,6 +1004,42 @@
}
}
+void xlat_ctx_cfg_init_tc14(void)
+{
+ struct xlat_ctx_cfg cfg;
+ uintptr_t start_va, end_va;
+ struct xlat_mmap_region init_mmap;
+ uint64_t max_va_size = XLAT_TEST_MAX_VA_SIZE();
+
+ /***************************************************************
+ * TEST CASE 14:
+ *
+ * Try to initialize the xlat_ctx_cfg structure with the MMU
+ * enabled.
+ *
+ ***************************************************************/
+
+ /* Emulate the MMU enabled */
+ write_sctlr_el2(SCTLR_ELx_WXN_BIT | SCTLR_ELx_M_BIT);
+
+ /* Clean the data structure */
+ memset((void *)&cfg, 0, sizeof(struct xlat_ctx_cfg));
+
+ /* VA space boundaries */
+ start_va = xlat_test_helpers_get_start_va(VA_LOW_REGION, max_va_size);
+ end_va = start_va + max_va_size - 1UL;
+
+ xlat_test_helpers_rand_mmap_array(&init_mmap, 1U, start_va, end_va);
+
+ test_helpers_expect_assert_fail(true);
+
+ /* Initialize the test structure */
+ (void)xlat_ctx_cfg_init(&cfg, VA_LOW_REGION, &init_mmap, 1U,
+ max_va_size);
+
+ test_helpers_fail_if_no_assert_failed();
+}
+
void xlat_ctx_init_tc1(void)
{
struct xlat_ctx ctx;
@@ -1268,51 +1304,49 @@
void xlat_ctx_init_tc5(void)
{
struct xlat_ctx ctx;
- struct xlat_ctx_cfg cfg;
struct xlat_ctx_tbls tbls;
+ struct xlat_ctx_cfg cfg;
uintptr_t start_va, end_va;
- xlat_addr_region_id_t region;
int retval;
- struct xlat_mmap_region init_mmap[XLAT_TESTS_MAX_MMAPS];
+ xlat_addr_region_id_t va_region;
uint64_t max_va_size = XLAT_TEST_MAX_VA_SIZE();
+ struct xlat_mmap_region init_mmap;
/***************************************************************
* TEST CASE 5:
*
- * Try to initialize a context with valid random memory mapping
- * areas and the MMU enabled.
+ * Try to initialize a context with a valid random memory map
+ * and the MMU enabled.
+ *
***************************************************************/
+ va_region = (xlat_addr_region_id_t)test_helpers_get_rand_in_range(0UL,
+ VA_REGIONS - 1U);
- /* Emulate the MMU enabled */
- write_sctlr_el2(SCTLR_ELx_WXN_BIT | SCTLR_ELx_M_BIT);
+ /* Clean the data structures */
+ memset((void *)&ctx, 0, sizeof(struct xlat_ctx));
+ memset((void *)&cfg, 0, sizeof(struct xlat_ctx_cfg));
+ memset((void *)&tbls, 0, sizeof(struct xlat_ctx_tbls));
- for (unsigned int i = 0U; i < (unsigned int)VA_REGIONS; i++) {
- region = (xlat_addr_region_id_t)i;
+ /* VA space boundaries */
+ start_va = xlat_test_helpers_get_start_va(va_region, max_va_size);
+ end_va = start_va + max_va_size - 1UL;
- /* Clean the data structures */
- memset((void *)&ctx, 0, sizeof(struct xlat_ctx));
- memset((void *)&cfg, 0, sizeof(struct xlat_ctx_cfg));
- memset((void *)&tbls, 0, sizeof(struct xlat_ctx_tbls));
+ xlat_test_helpers_rand_mmap_array(&init_mmap, 1U, start_va, end_va);
- /* VA space boundaries */
- start_va = xlat_test_helpers_get_start_va(region, max_va_size);
- end_va = start_va + max_va_size - 1ULL;
+ /* Initialize the test structure */
+ retval = xlat_ctx_cfg_init(&cfg, va_region, &init_mmap, 1U, max_va_size);
- xlat_test_helpers_rand_mmap_array(&init_mmap[0],
- XLAT_TESTS_MAX_MMAPS, start_va, end_va);
+ /* Verify that the context cfg is properly created */
+ CHECK_TRUE(retval == 0);
- /* Initialize the test structure */
- retval = xlat_ctx_cfg_init(&cfg, region, &init_mmap[0],
- XLAT_TESTS_MAX_MMAPS,
- max_va_size);
+ /* Force the MMU enablement */
+ xlat_enable_mmu_el2();
- /* Verify that the context cfg is properly created */
- CHECK_TRUE(retval == 0);
+ test_helpers_expect_assert_fail(true);
- /* Test xlat_ctx_init() */
- retval = xlat_ctx_init(&ctx, &cfg, &tbls,
- xlat_test_helpers_tbls(),
- XLAT_TESTS_MAX_TABLES);
- CHECK_TRUE(retval == -EINVAL);
- }
+ /* Test xlat_ctx_init() with MMU Enabled */
+ (void)xlat_ctx_init(&ctx, &cfg, &tbls, xlat_test_helpers_tbls(),
+ XLAT_TESTS_MAX_TABLES);
+
+ test_helpers_fail_if_no_assert_failed();
}
diff --git a/lib/xlat/tests/xlat_tests_base_g2.cpp b/lib/xlat/tests/xlat_tests_base_g2.cpp
index fd11679..1360403 100644
--- a/lib/xlat/tests/xlat_tests_base_g2.cpp
+++ b/lib/xlat/tests/xlat_tests_base_g2.cpp
@@ -2225,7 +2225,6 @@
* and overwrite it to test different failure conditions on
* xlat_arch_setup_mmu_cfg():
*
- * - Call xlat_arch_setup_mmu_cfg() with the MMU enabled.
* - Call xlat_arch_setup_mmu_cfg() with an uninitialized
* context configuration.
* - Call xlat_arch_setup_mmu_cfg() for a CPU which
@@ -2256,18 +2255,6 @@
XLAT_TESTS_MAX_TABLES);
CHECK_TRUE(retval == 0);
- /* Force the MMU enblement */
- xlat_enable_mmu_el2();
-
- /* Try to initialize MMU for the given context */
- retval = xlat_arch_setup_mmu_cfg(&ctx);
-
- /* Verify that the MMU has failed to be initialized */
- CHECK_TRUE(retval == -EPERM);
-
- /* Restore SCTLR_EL2 to disable the MMU */
- write_sctlr_el2(0ULL);
-
/* Force the context to be uninitialized */
ctx.cfg->initialized = false;
@@ -2378,6 +2365,55 @@
test_helpers_fail_if_no_assert_failed();
}
+void xlat_arch_setup_mmu_cfg_tc6(void)
+{
+ struct xlat_ctx ctx;
+ struct xlat_ctx_cfg cfg;
+ struct xlat_ctx_tbls tbls;
+ uintptr_t start_va, end_va;
+ int retval;
+ struct xlat_mmap_region init_mmap;
+ uint64_t max_va_size = XLAT_TEST_MAX_VA_SIZE();
+
+ /***************************************************************
+ * TEST CASE 6:
+ * Generate a valid translation context for one of the regions
+ * and call xlat_arch_setup_mmu_cfg() with the MMU enabled.
+ *
+ ***************************************************************/
+
+ /* Clean the data structures */
+ memset((void *)&ctx, 0, sizeof(struct xlat_ctx));
+ memset((void *)&cfg, 0, sizeof(struct xlat_ctx_cfg));
+ memset((void *)&tbls, 0, sizeof(struct xlat_ctx_tbls));
+
+ /* VA space boundaries */
+ start_va = xlat_test_helpers_get_start_va(VA_LOW_REGION, max_va_size);
+ end_va = start_va + max_va_size - 1UL;
+
+ /* Generate only a single mmap region for each region */
+ xlat_test_helpers_rand_mmap_array(&init_mmap, 1U, start_va, end_va);
+
+ retval = xlat_ctx_cfg_init(&cfg, VA_LOW_REGION, &init_mmap,
+ 1U, max_va_size);
+ CHECK_TRUE(retval == 0);
+
+ retval = xlat_ctx_init(&ctx, &cfg, &tbls,
+ xlat_test_helpers_tbls(),
+ XLAT_TESTS_MAX_TABLES);
+ CHECK_TRUE(retval == 0);
+
+ /* Force the MMU enablement */
+ xlat_enable_mmu_el2();
+
+ test_helpers_expect_assert_fail(true);
+
+ /* Try to initialize MMU for the given context */
+ retval = xlat_arch_setup_mmu_cfg(&ctx);
+
+ test_helpers_fail_if_no_assert_failed();
+}
+
void xlat_get_oa_from_tte_tc1(void)
{
uint64_t test_tte, val_addr, output_addr;
diff --git a/lib/xlat/tests/xlat_tests_lpa2.cpp b/lib/xlat/tests/xlat_tests_lpa2.cpp
index aa8ab2e..8646bfc 100644
--- a/lib/xlat/tests/xlat_tests_lpa2.cpp
+++ b/lib/xlat/tests/xlat_tests_lpa2.cpp
@@ -102,6 +102,11 @@
xlat_ctx_cfg_init_tc12();
}
+ASSERT_TEST(xlat_tests_LPA2, xlat_ctx_cfg_init_TC14)
+{
+ xlat_ctx_cfg_init_tc14();
+}
+
TEST(xlat_tests_LPA2, xlat_ctx_init_TC1)
{
xlat_ctx_init_tc1();
@@ -122,7 +127,7 @@
xlat_ctx_init_tc4();
}
-TEST(xlat_tests_LPA2, xlat_ctx_init_TC5)
+ASSERT_TEST(xlat_tests_LPA2, xlat_ctx_init_TC5)
{
xlat_ctx_init_tc5();
}
@@ -252,6 +257,11 @@
xlat_arch_setup_mmu_cfg_tc5();
}
+ASSERT_TEST(xlat_tests_LPA2, xlat_arch_setup_mmu_cfg_TC6)
+{
+ xlat_arch_setup_mmu_cfg_tc6();
+}
+
TEST(xlat_tests_LPA2, xlat_get_oa_from_tte_TC1)
{
xlat_get_oa_from_tte_tc1();
diff --git a/lib/xlat/tests/xlat_tests_no_lpa2.cpp b/lib/xlat/tests/xlat_tests_no_lpa2.cpp
index 844df83..500c08b 100644
--- a/lib/xlat/tests/xlat_tests_no_lpa2.cpp
+++ b/lib/xlat/tests/xlat_tests_no_lpa2.cpp
@@ -107,6 +107,11 @@
xlat_ctx_cfg_init_tc13();
}
+ASSERT_TEST(xlat_tests_no_LPA2, xlat_ctx_cfg_init_TC14)
+{
+ xlat_ctx_cfg_init_tc14();
+}
+
TEST(xlat_tests_no_LPA2, xlat_ctx_init_TC1)
{
xlat_ctx_init_tc1();
@@ -127,7 +132,7 @@
xlat_ctx_init_tc4();
}
-TEST(xlat_tests_no_LPA2, xlat_ctx_init_TC5)
+ASSERT_TEST(xlat_tests_no_LPA2, xlat_ctx_init_TC5)
{
xlat_ctx_init_tc5();
}
@@ -257,6 +262,11 @@
xlat_arch_setup_mmu_cfg_tc5();
}
+ASSERT_TEST(xlat_tests_no_LPA2, xlat_arch_setup_mmu_cfg_TC6)
+{
+ xlat_arch_setup_mmu_cfg_tc6();
+}
+
TEST(xlat_tests_no_LPA2, xlat_get_oa_from_tte_TC1)
{
xlat_get_oa_from_tte_tc1();