1From ca7696bca357cfd71a34582c65a7c7c08828b6dc Mon Sep 17 00:00:00 2001
2From: Bence Balogh <bence.balogh@arm.com>
3Date: Mon, 18 Dec 2023 14:00:14 +0100
4Subject: [PATCH 2/2] platform: corstone1000: Cover S_DATA with MPU
5
6The S_DATA has to be covered with MPU regions to override the
7other MPU regions with smaller RNR values.
8
9Change-Id: I45fec65f51241939314941e25d287e6fdc82777c
10Signed-off-by: Bence Balogh <bence.balogh@arm.com>
11Upstream-Status: Submitted [https://review.trustedfirmware.org/c/TF-M/trusted-firmware-m/+/25583]
12
13---
14 .../target/arm/corstone1000/CMakeLists.txt    |  8 +++++++
15 .../arm/corstone1000/tfm_hal_isolation.c      | 22 +++++++++++++++++++
16 2 files changed, 30 insertions(+)
17
18diff --git a/platform/ext/target/arm/corstone1000/CMakeLists.txt b/platform/ext/target/arm/corstone1000/CMakeLists.txt
19index 8817f514c..541504368 100644
20--- a/platform/ext/target/arm/corstone1000/CMakeLists.txt
21+++ b/platform/ext/target/arm/corstone1000/CMakeLists.txt
22@@ -40,6 +40,14 @@ target_compile_definitions(platform_region_defs
23     INTERFACE
24         TFM_LINKER_APP_ROT_LINKER_DATA_ALIGNMENT=0x2000
25         TFM_LINKER_SP_META_PTR_ALIGNMENT=0x100
26+
27+        # The RAM MPU Region block sizes are calculated manually. The RAM has to be covered
28+        # with the MPU regions. These regions also have to be the power of 2 and
29+        # the start addresses have to be aligned to these sizes. The sizes can be calculated
30+        # from the S_DATA_START and S_DATA_SIZE defines.
31+        RAM_MPU_REGION_BLOCK_1_SIZE=0x4000
32+        RAM_MPU_REGION_BLOCK_2_SIZE=0x20000
33+
34 )
35 #========================= Platform common defs ===============================#
36
37diff --git a/platform/ext/target/arm/corstone1000/tfm_hal_isolation.c b/platform/ext/target/arm/corstone1000/tfm_hal_isolation.c
38index 98e795dde..39b19c535 100644
39--- a/platform/ext/target/arm/corstone1000/tfm_hal_isolation.c
40+++ b/platform/ext/target/arm/corstone1000/tfm_hal_isolation.c
41@@ -15,6 +15,7 @@
42 #include "mpu_config.h"
43 #include "mmio_defs.h"
44 #include "flash_layout.h"
45+#include "region_defs.h"
46
47 #define PROT_BOUNDARY_VAL \
48     ((1U << HANDLE_ATTR_PRIV_POS) & HANDLE_ATTR_PRIV_MASK)
49@@ -132,6 +133,27 @@ enum tfm_hal_status_t tfm_hal_set_up_static_boundaries(
50
51 #endif
52
53+    /* Set the RAM attributes. It is needed because the first region overlaps the whole
54+     * SRAM and it has to be overridden.
55+     * The RAM_MPU_REGION_BLOCK_1_SIZE and RAM_MPU_REGION_BLOCK_2_SIZE are calculated manually
56+     * and added to the platform_region_defs compile definitions.
57+     */
58+    base = S_DATA_START;
59+    limit = S_DATA_START + RAM_MPU_REGION_BLOCK_1_SIZE;
60+    ret = configure_mpu(rnr++, base, limit,
61+                            XN_EXEC_NOT_OK, AP_RW_PRIV_UNPRIV);
62+    if (ret != TFM_HAL_SUCCESS) {
63+        return ret;
64+    }
65+
66+    base = S_DATA_START + RAM_MPU_REGION_BLOCK_1_SIZE;
67+    limit = S_DATA_START + RAM_MPU_REGION_BLOCK_1_SIZE + RAM_MPU_REGION_BLOCK_2_SIZE;
68+    ret = configure_mpu(rnr++, base, limit,
69+                            XN_EXEC_NOT_OK, AP_RW_PRIV_UNPRIV);
70+    if (ret != TFM_HAL_SUCCESS) {
71+        return ret;
72+    }
73+
74     arm_mpu_enable();
75
76 #endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */
77