Zephyr Project API  3.1.0
A Scalable Open Source RTOS
arm_mpu_v8m.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Linaro Limited.
3 * Copyright (c) 2018 Nordic Semiconductor ASA.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef _ASMLANGUAGE
9
11
12/* Convenience macros to represent the ARMv8-M-specific
13 * configuration for memory access permission and
14 * cache-ability attribution.
15 */
16
17/* Privileged No Access, Unprivileged No Access */
18/*#define NO_ACCESS 0x0 */
19/*#define NO_ACCESS_Msk ((NO_ACCESS << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk) */
20/* Privileged No Access, Unprivileged No Access */
21/*#define P_NA_U_NA 0x0 */
22/*#define P_NA_U_NA_Msk ((P_NA_U_NA << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk) */
23/* Privileged Read Write, Unprivileged No Access */
24#define P_RW_U_NA 0x0
25#define P_RW_U_NA_Msk ((P_RW_U_NA << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
26/* Privileged Read Write, Unprivileged Read Only */
27/*#define P_RW_U_RO 0x2 */
28/*#define P_RW_U_RO_Msk ((P_RW_U_RO << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)*/
29/* Privileged Read Write, Unprivileged Read Write */
30#define P_RW_U_RW 0x1
31#define P_RW_U_RW_Msk ((P_RW_U_RW << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
32/* Privileged Read Write, Unprivileged Read Write */
33#define FULL_ACCESS 0x1
34#define FULL_ACCESS_Msk ((FULL_ACCESS << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
35/* Privileged Read Only, Unprivileged No Access */
36#define P_RO_U_NA 0x2
37#define P_RO_U_NA_Msk ((P_RO_U_NA << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
38/* Privileged Read Only, Unprivileged Read Only */
39#define P_RO_U_RO 0x3
40#define P_RO_U_RO_Msk ((P_RO_U_RO << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
41/* Privileged Read Only, Unprivileged Read Only */
42#define RO 0x3
43#define RO_Msk ((RO << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
44
45/* Attribute flag for not-allowing execution (eXecute Never) */
46#define NOT_EXEC MPU_RBAR_XN_Msk
47
48/* Attribute flags for share-ability */
49#define NON_SHAREABLE 0x0
50#define NON_SHAREABLE_Msk \
51 ((NON_SHAREABLE << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk)
52#define OUTER_SHAREABLE 0x2
53#define OUTER_SHAREABLE_Msk \
54 ((OUTER_SHAREABLE << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk)
55#define INNER_SHAREABLE 0x3
56#define INNER_SHAREABLE_Msk \
57 ((INNER_SHAREABLE << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk)
58
59/* Helper define to calculate the region limit address. */
60#define REGION_LIMIT_ADDR(base, size) \
61 (((base & MPU_RBAR_BASE_Msk) + size - 1) & MPU_RLAR_LIMIT_Msk)
62
63
64/* Attribute flags for cache-ability */
65
66/* Read/Write Allocation Configurations for Cacheable Memory */
67#define R_NON_W_NON 0x0 /* Do not allocate Read/Write */
68#define R_NON_W_ALLOC 0x1 /* Do not allocate Read, Allocate Write */
69#define R_ALLOC_W_NON 0x2 /* Allocate Read, Do not allocate Write */
70#define R_ALLOC_W_ALLOC 0x3 /* Allocate Read/Write */
71
72/* Memory Attributes for Normal Memory */
73#define NORMAL_O_WT_NT 0x80 /* Normal, Outer Write-through non-transient */
74#define NORMAL_O_WB_NT 0xC0 /* Normal, Outer Write-back non-transient */
75#define NORMAL_O_NON_C 0x40 /* Normal, Outer Non-Cacheable */
76
77#define NORMAL_I_WT_NT 0x08 /* Normal, Inner Write-through non-transient */
78#define NORMAL_I_WB_NT 0x0C /* Normal, Inner Write-back non-transient */
79#define NORMAL_I_NON_C 0x04 /* Normal, Inner Non-Cacheable */
80
81#define NORMAL_OUTER_INNER_WRITE_THROUGH_READ_ALLOCATE_NON_TRANS \
82 ((NORMAL_O_WT_NT | (R_ALLOC_W_NON << 4)) \
83 | \
84 (NORMAL_I_WT_NT | R_ALLOC_W_NON)) \
85
86#define NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_TRANS \
87 ((NORMAL_O_WB_NT | (R_ALLOC_W_ALLOC << 4)) \
88 | \
89 (NORMAL_I_WB_NT | R_ALLOC_W_ALLOC))
90
91#define NORMAL_OUTER_INNER_NON_CACHEABLE \
92 ((NORMAL_O_NON_C | (R_NON_W_NON << 4)) \
93 | \
94 (NORMAL_I_NON_C | R_NON_W_NON))
95
96/* Common cache-ability configuration for Flash, SRAM regions */
97#define MPU_CACHE_ATTRIBUTES_FLASH \
98 NORMAL_OUTER_INNER_WRITE_THROUGH_READ_ALLOCATE_NON_TRANS
99#define MPU_CACHE_ATTRIBUTES_SRAM \
100 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_TRANS
101#define MPU_CACHE_ATTRIBUTES_SRAM_NOCACHE \
102 NORMAL_OUTER_INNER_NON_CACHEABLE
103
104/* Global MAIR configurations */
105#define MPU_MAIR_ATTR_FLASH MPU_CACHE_ATTRIBUTES_FLASH
106#define MPU_MAIR_INDEX_FLASH 0
107#define MPU_MAIR_ATTR_SRAM MPU_CACHE_ATTRIBUTES_SRAM
108#define MPU_MAIR_INDEX_SRAM 1
109#define MPU_MAIR_ATTR_SRAM_NOCACHE MPU_CACHE_ATTRIBUTES_SRAM_NOCACHE
110#define MPU_MAIR_INDEX_SRAM_NOCACHE 2
111
112/* Some helper defines for common regions.
113 *
114 * Note that the ARMv8-M MPU architecture requires that the
115 * enabled MPU regions are non-overlapping. Therefore, it is
116 * recommended to use these helper defines only for configuring
117 * fixed MPU regions at build-time (i.e. regions that are not
118 * expected to be re-programmed or re-adjusted at run-time so
119 * that they do not overlap with other MPU regions).
120 */
121#define REGION_RAM_ATTR(base, size) \
122 {\
123 .rbar = NOT_EXEC | \
124 P_RW_U_NA_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
125 /* Cache-ability */ \
126 .mair_idx = MPU_MAIR_INDEX_SRAM, \
127 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
128 }
129
130#define REGION_RAM_NOCACHE_ATTR(base, size) \
131 {\
132 .rbar = NOT_EXEC | \
133 P_RW_U_NA_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
134 /* Cache-ability */ \
135 .mair_idx = MPU_MAIR_INDEX_SRAM_NOCACHE, \
136 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
137 }
138
139#if defined(CONFIG_MPU_ALLOW_FLASH_WRITE)
140/* Note that the access permissions allow for un-privileged writes, contrary
141 * to ARMv7-M where un-privileged code has Read-Only permissions.
142 */
143#define REGION_FLASH_ATTR(base, size) \
144 {\
145 .rbar = P_RW_U_RW_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
146 /* Cache-ability */ \
147 .mair_idx = MPU_MAIR_INDEX_FLASH, \
148 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
149 }
150#else /* CONFIG_MPU_ALLOW_FLASH_WRITE */
151#define REGION_FLASH_ATTR(base, size) \
152 {\
153 .rbar = RO_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
154 /* Cache-ability */ \
155 .mair_idx = MPU_MAIR_INDEX_FLASH, \
156 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
157 }
158#endif /* CONFIG_MPU_ALLOW_FLASH_WRITE */
159
160
161struct arm_mpu_region_attr {
162 /* Attributes belonging to RBAR */
164 /* MAIR index for attribute indirection */
166 /* Region Limit Address value to be written to the RLAR register. */
168};
169
171
172/* Typedef for the k_mem_partition attribute */
173typedef struct {
177
178/* Kernel macros for memory attribution
179 * (access permissions and cache-ability).
180 *
181 * The macros are to be stored in k_mem_partition_attr_t
182 * objects. The format of a k_mem_partition_attr_t object
183 * is as follows: field <rbar> contains a direct mapping
184 * of the <XN> and <AP> bit-fields of the RBAR register;
185 * field <mair_idx> contains a direct mapping of AttrIdx
186 * bit-field, stored in RLAR register.
187 */
188
189/* Read-Write access permission attributes */
190#define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
191 {(P_RW_U_RW_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
192#define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
193 {(P_RW_U_NA_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
194#define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
195 {(P_RO_U_RO_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
196#define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
197 {(P_RO_U_NA_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
198
199/* Execution-allowed attributes */
200#define K_MEM_PARTITION_P_RWX_U_RWX ((k_mem_partition_attr_t) \
201 {(P_RW_U_RW_Msk), MPU_MAIR_INDEX_SRAM})
202#define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
203 {(P_RO_U_RO_Msk), MPU_MAIR_INDEX_SRAM})
204
205/*
206 * @brief Evaluate Write-ability
207 *
208 * Evaluate whether the access permissions include write-ability.
209 *
210 * @param attr The k_mem_partition_attr_t object holding the
211 * MPU attributes to be checked against write-ability.
212 */
213#define K_MEM_PARTITION_IS_WRITABLE(attr) \
214 ({ \
215 int __is_writable__; \
216 switch (attr.rbar & MPU_RBAR_AP_Msk) { \
217 case P_RW_U_RW_Msk: \
218 case P_RW_U_NA_Msk: \
219 __is_writable__ = 1; \
220 break; \
221 default: \
222 __is_writable__ = 0; \
223 } \
224 __is_writable__; \
225 })
226
227/*
228 * @brief Evaluate Execution allowance
229 *
230 * Evaluate whether the access permissions include execution.
231 *
232 * @param attr The k_mem_partition_attr_t object holding the
233 * MPU attributes to be checked against execution
234 * allowance.
235 */
236#define K_MEM_PARTITION_IS_EXECUTABLE(attr) \
237 (!((attr.rbar) & (NOT_EXEC)))
238
239/* Attributes for no-cache enabling (share-ability is selected by default) */
240
241/* Read-Write access permission attributes */
242#define K_MEM_PARTITION_P_RW_U_RW_NOCACHE ((k_mem_partition_attr_t) \
243 {(P_RW_U_RW_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
244 MPU_MAIR_INDEX_SRAM_NOCACHE})
245#define K_MEM_PARTITION_P_RW_U_NA_NOCACHE ((k_mem_partition_attr_t) \
246 {(P_RW_U_NA_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
247 MPU_MAIR_INDEX_SRAM_NOCACHE})
248#define K_MEM_PARTITION_P_RO_U_RO_NOCACHE ((k_mem_partition_attr_t) \
249 {(P_RO_U_RO_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
250 MPU_MAIR_INDEX_SRAM_NOCACHE})
251#define K_MEM_PARTITION_P_RO_U_NA_NOCACHE ((k_mem_partition_attr_t) \
252 {(P_RO_U_NA_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
253 MPU_MAIR_INDEX_SRAM_NOCACHE})
254
255/* Execution-allowed attributes */
256#define K_MEM_PARTITION_P_RWX_U_RWX_NOCACHE ((k_mem_partition_attr_t) \
257 {(P_RW_U_RW_Msk | OUTER_SHAREABLE_Msk), MPU_MAIR_INDEX_SRAM_NOCACHE})
258#define K_MEM_PARTITION_P_RX_U_RX_NOCACHE ((k_mem_partition_attr_t) \
259 {(P_RO_U_RO_Msk | OUTER_SHAREABLE_Msk), MPU_MAIR_INDEX_SRAM_NOCACHE})
260
261#endif /* _ASMLANGUAGE */
262
263#define _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size) \
264 BUILD_ASSERT((size > 0) && ((uint32_t)start % \
265 CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE == 0U) && \
266 ((size) % CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE == 0), \
267 " the start and size of the partition must align " \
268 "with the minimum MPU region size.")
uint32_t k_mem_partition_attr_t
Definition: arch.h:210
CMSIS interface file.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Definition: arm_mpu_v7m.h:141
uint8_t rbar
Definition: arm_mpu_v8m.h:163
uint32_t r_limit
Definition: arm_mpu_v8m.h:167
uint8_t mair_idx
Definition: arm_mpu_v8m.h:165
uint16_t rbar
Definition: arm_mpu_v8m.h:174
uint16_t mair_idx
Definition: arm_mpu_v8m.h:175