55 * SPDX-License-Identifier: Apache-2.0
66 */
77
8+ #ifndef _ASMLANGUAGE
9+
10+ #include <arch/arm/cortex_m/cmsis.h>
11+
12+ /* Convenience macros to represent the ARMv7-M-specific
13+ * configuration for memory access permission and
14+ * cache-ability attribution.
15+ */
16+
817/* Privileged No Access, Unprivileged No Access */
918#define NO_ACCESS 0x0
1019#define NO_ACCESS_Msk ((NO_ACCESS << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
@@ -129,24 +138,54 @@ struct arm_mpu_region_attr {
129138
130139typedef struct arm_mpu_region_attr arm_mpu_region_attr_t ;
131140
132- #ifndef _ASMLANGUAGE
141+ /* Typedef for the k_mem_partition attribute */
142+ typedef struct {
143+ u32_t rasr_attr ;
144+ } k_mem_partition_attr_t ;
145+
146+ /* Kernel macros for memory attribution
147+ * (access permissions and cache-ability).
148+ *
149+ * The macros are to be stored in k_mem_partition_attr_t
150+ * objects. The format of k_mem_partition_attr_t is an
151+ * "1-1" mapping of the ARMv7-M MPU RASR attribute register
152+ * fields (excluding the <size> and <enable> bit-fields).
153+ */
154+
133155/* Read-Write access permission attributes */
134- #define K_MEM_PARTITION_P_NA_U_NA (NO_ACCESS_Msk | NOT_EXEC)
135- #define K_MEM_PARTITION_P_RW_U_RW (P_RW_U_RW_Msk | NOT_EXEC)
136- #define K_MEM_PARTITION_P_RW_U_RO (P_RW_U_RO_Msk | NOT_EXEC)
137- #define K_MEM_PARTITION_P_RW_U_NA (P_RW_U_NA_Msk | NOT_EXEC)
138- #define K_MEM_PARTITION_P_RO_U_RO (P_RO_U_RO_Msk | NOT_EXEC)
139- #define K_MEM_PARTITION_P_RO_U_NA (P_RO_U_NA_Msk | NOT_EXEC)
156+ #define K_MEM_PARTITION_P_NA_U_NA ((k_mem_partition_attr_t) \
157+ {(NO_ACCESS_Msk | NOT_EXEC)})
158+ #define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
159+ {(P_RW_U_RW_Msk | NOT_EXEC)})
160+ #define K_MEM_PARTITION_P_RW_U_RO ((k_mem_partition_attr_t) \
161+ {(P_RW_U_RO_Msk | NOT_EXEC)})
162+ #define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
163+ {(P_RW_U_NA_Msk | NOT_EXEC)})
164+ #define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
165+ {(P_RO_U_RO_Msk | NOT_EXEC)})
166+ #define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
167+ {(P_RO_U_NA_Msk | NOT_EXEC)})
140168
141169/* Execution-allowed attributes */
142- #define K_MEM_PARTITION_P_RWX_U_RWX (P_RW_U_RW_Msk)
143- #define K_MEM_PARTITION_P_RWX_U_RX (P_RW_U_RO_Msk)
144- #define K_MEM_PARTITION_P_RX_U_RX (P_RO_U_RO_Msk)
170+ #define K_MEM_PARTITION_P_RWX_U_RWX ((k_mem_partition_attr_t) \
171+ {(P_RW_U_RW_Msk)})
172+ #define K_MEM_PARTITION_P_RWX_U_RX ((k_mem_partition_attr_t) \
173+ {(P_RW_U_RO_Msk)})
174+ #define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
175+ {(P_RO_U_RO_Msk)})
145176
177+ /*
178+ * @brief Evaluate Write-ability
179+ *
180+ * Evaluate whether the access permissions include write-ability.
181+ *
182+ * @param attr The k_mem_partition_attr_t object holding the
183+ * MPU attributes to be checked against write-ability.
184+ */
146185#define K_MEM_PARTITION_IS_WRITABLE (attr ) \
147186 ({ \
148187 int __is_writable__; \
149- switch (attr & MPU_RASR_AP_Msk) { \
188+ switch (attr.rasr_attr & MPU_RASR_AP_Msk) { \
150189 case P_RW_U_RW_Msk: \
151190 case P_RW_U_RO_Msk: \
152191 case P_RW_U_NA_Msk: \
@@ -158,6 +197,55 @@ typedef struct arm_mpu_region_attr arm_mpu_region_attr_t;
158197 __is_writable__; \
159198 })
160199
200+ /*
201+ * @brief Evaluate Execution allowance
202+ *
203+ * Evaluate whether the access permissions include execution.
204+ *
205+ * @param attr The k_mem_partition_attr_t object holding the
206+ * MPU attributes to be checked against execution
207+ * allowance.
208+ */
161209#define K_MEM_PARTITION_IS_EXECUTABLE (attr ) \
162- (!((attr) & (NOT_EXEC)))
210+ (!((attr.rasr_attr) & (NOT_EXEC)))
211+
212+ /* Attributes for no-cache enabling (share-ability is selected by default) */
213+
214+ #define K_MEM_PARTITION_P_NA_U_NA_NOCACHE ((k_mem_partition_attr_t) \
215+ {(K_MEM_PARTITION_P_NA_U_NA \
216+ | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
217+ #define K_MEM_PARTITION_P_RW_U_RW_NOCACHE ((k_mem_partition_attr_t) \
218+ {(K_MEM_PARTITION_P_RW_U_RW \
219+ | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
220+ #define K_MEM_PARTITION_P_RW_U_RO_NOCACHE ((k_mem_partition_attr_t) \
221+ {(K_MEM_PARTITION_P_RW_U_RO \
222+ | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
223+ #define K_MEM_PARTITION_P_RW_U_NA_NOCACHE ((k_mem_partition_attr_t) \
224+ {(K_MEM_PARTITION_P_RW_U_NA \
225+ | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
226+ #define K_MEM_PARTITION_P_RO_U_RO_NOCACHE ((k_mem_partition_attr_t) \
227+ {(K_MEM_PARTITION_P_RO_U_RO \
228+ | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
229+ #define K_MEM_PARTITION_P_RO_U_NA_NOCACHE ((k_mem_partition_attr_t) \
230+ {(K_MEM_PARTITION_P_RO_U_NA \
231+ | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
232+
233+ #define K_MEM_PARTITION_P_RWX_U_RWX_NOCACHE ((k_mem_partition_attr_t) \
234+ {(K_MEM_PARTITION_P_RWX_U_RWX \
235+ | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
236+ #define K_MEM_PARTITION_P_RWX_U_RX_NOCACHE ((k_mem_partition_attr_t) \
237+ {(K_MEM_PARTITION_P_RWX_U_RX \
238+ | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
239+ #define K_MEM_PARTITION_P_RX_U_RX_NOCACHE ((k_mem_partition_attr_t) \
240+ {(K_MEM_PARTITION_P_RX_U_RX \
241+ | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
242+
163243#endif /* _ASMLANGUAGE */
244+
245+ #define _ARCH_MEM_PARTITION_ALIGN_CHECK (start , size ) \
246+ BUILD_ASSERT_MSG(!(((size) & ((size) - 1))) && \
247+ (size) >= CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE && \
248+ !((u32_t)(start) & ((size) - 1)), \
249+ "the size of the partition must be power of 2" \
250+ " and greater than or equal to the minimum MPU region size." \
251+ "start address of the partition must align with size.")
0 commit comments