Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
arm_mpu_v7m.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
10#if defined(CONFIG_CPU_CORTEX_M)
12#endif
13
14/* Convenience macros to represent the ARMv7-M-specific
15 * configuration for memory access permission and
16 * cache-ability attribution.
17 */
18
19/* Privileged No Access, Unprivileged No Access */
20#define NO_ACCESS 0x0
21#define NO_ACCESS_Msk ((NO_ACCESS << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
22/* Privileged No Access, Unprivileged No Access */
23#define P_NA_U_NA 0x0
24#define P_NA_U_NA_Msk ((P_NA_U_NA << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
25/* Privileged Read Write, Unprivileged No Access */
26#define P_RW_U_NA 0x1
27#define P_RW_U_NA_Msk ((P_RW_U_NA << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
28/* Privileged Read Write, Unprivileged Read Only */
29#define P_RW_U_RO 0x2
30#define P_RW_U_RO_Msk ((P_RW_U_RO << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
31/* Privileged Read Write, Unprivileged Read Write */
32#define P_RW_U_RW 0x3U
33#define P_RW_U_RW_Msk ((P_RW_U_RW << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
34/* Privileged Read Write, Unprivileged Read Write */
35#define FULL_ACCESS 0x3
36#define FULL_ACCESS_Msk ((FULL_ACCESS << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
37/* Privileged Read Only, Unprivileged No Access */
38#define P_RO_U_NA 0x5
39#define P_RO_U_NA_Msk ((P_RO_U_NA << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
40/* Privileged Read Only, Unprivileged Read Only */
41#define P_RO_U_RO 0x6
42#define P_RO_U_RO_Msk ((P_RO_U_RO << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
43/* Privileged Read Only, Unprivileged Read Only */
44#define RO 0x7
45#define RO_Msk ((RO << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)
46
47/* Attribute flag for not-allowing execution (eXecute Never) */
48#define NOT_EXEC MPU_RASR_XN_Msk
49
50/* The following definitions are for internal use in arm_mpu.h. */
51#define STRONGLY_ORDERED_SHAREABLE MPU_RASR_S_Msk
52#define DEVICE_SHAREABLE (MPU_RASR_B_Msk | MPU_RASR_S_Msk)
53#define NORMAL_OUTER_INNER_WRITE_THROUGH_SHAREABLE \
54 (MPU_RASR_C_Msk | MPU_RASR_S_Msk)
55#define NORMAL_OUTER_INNER_WRITE_THROUGH_NON_SHAREABLE MPU_RASR_C_Msk
56#define NORMAL_OUTER_INNER_WRITE_BACK_SHAREABLE \
57 (MPU_RASR_C_Msk | MPU_RASR_B_Msk | MPU_RASR_S_Msk)
58#define NORMAL_OUTER_INNER_WRITE_BACK_NON_SHAREABLE \
59 (MPU_RASR_C_Msk | MPU_RASR_B_Msk)
60#define NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE \
61 ((1 << MPU_RASR_TEX_Pos) | MPU_RASR_S_Msk)
62#define NORMAL_OUTER_INNER_NON_CACHEABLE_NON_SHAREABLE \
63 (1 << MPU_RASR_TEX_Pos)
64#define NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_SHAREABLE \
65 ((1 << MPU_RASR_TEX_Pos) |\
66 MPU_RASR_C_Msk | MPU_RASR_B_Msk | MPU_RASR_S_Msk)
67#define NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE \
68 ((1 << MPU_RASR_TEX_Pos) | MPU_RASR_C_Msk | MPU_RASR_B_Msk)
69#define DEVICE_NON_SHAREABLE (2 << MPU_RASR_TEX_Pos)
70
71/* Bit-masks to disable sub-regions. */
72#define SUB_REGION_0_DISABLED ((0x01 << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk)
73#define SUB_REGION_1_DISABLED ((0x02 << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk)
74#define SUB_REGION_2_DISABLED ((0x04 << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk)
75#define SUB_REGION_3_DISABLED ((0x08 << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk)
76#define SUB_REGION_4_DISABLED ((0x10 << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk)
77#define SUB_REGION_5_DISABLED ((0x20 << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk)
78#define SUB_REGION_6_DISABLED ((0x40 << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk)
79#define SUB_REGION_7_DISABLED ((0x80 << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk)
80
81
82#define REGION_SIZE(size) ((ARM_MPU_REGION_SIZE_ ## size \
83 << MPU_RASR_SIZE_Pos) & MPU_RASR_SIZE_Msk)
84
85#define REGION_32B REGION_SIZE(32B)
86#define REGION_64B REGION_SIZE(64B)
87#define REGION_128B REGION_SIZE(128B)
88#define REGION_256B REGION_SIZE(256B)
89#define REGION_512B REGION_SIZE(512B)
90#define REGION_1K REGION_SIZE(1KB)
91#define REGION_2K REGION_SIZE(2KB)
92#define REGION_4K REGION_SIZE(4KB)
93#define REGION_8K REGION_SIZE(8KB)
94#define REGION_16K REGION_SIZE(16KB)
95#define REGION_32K REGION_SIZE(32KB)
96#define REGION_64K REGION_SIZE(64KB)
97#define REGION_128K REGION_SIZE(128KB)
98#define REGION_256K REGION_SIZE(256KB)
99#define REGION_512K REGION_SIZE(512KB)
100#define REGION_1M REGION_SIZE(1MB)
101#define REGION_2M REGION_SIZE(2MB)
102#define REGION_4M REGION_SIZE(4MB)
103#define REGION_8M REGION_SIZE(8MB)
104#define REGION_16M REGION_SIZE(16MB)
105#define REGION_32M REGION_SIZE(32MB)
106#define REGION_64M REGION_SIZE(64MB)
107#define REGION_128M REGION_SIZE(128MB)
108#define REGION_256M REGION_SIZE(256MB)
109#define REGION_512M REGION_SIZE(512MB)
110#define REGION_1G REGION_SIZE(1GB)
111#define REGION_2G REGION_SIZE(2GB)
112#define REGION_4G REGION_SIZE(4GB)
113
114/* Some helper defines for common regions */
115#define REGION_RAM_ATTR(size) \
116{ \
117 (NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE | \
118 MPU_RASR_XN_Msk | size | P_RW_U_NA_Msk) \
119}
120#define REGION_RAM_NOCACHE_ATTR(size) \
121{ \
122 (NORMAL_OUTER_INNER_NON_CACHEABLE_NON_SHAREABLE | \
123 MPU_RASR_XN_Msk | size | P_RW_U_NA_Msk) \
124}
125#if defined(CONFIG_MPU_ALLOW_FLASH_WRITE)
126#define REGION_FLASH_ATTR(size) \
127{ \
128 (NORMAL_OUTER_INNER_WRITE_THROUGH_NON_SHAREABLE | size | \
129 P_RW_U_RO_Msk) \
130}
131#else
132#define REGION_FLASH_ATTR(size) \
133{ \
134 (NORMAL_OUTER_INNER_WRITE_THROUGH_NON_SHAREABLE | size | RO_Msk) \
135}
136#endif
137#define REGION_PPB_ATTR(size) { (STRONGLY_ORDERED_SHAREABLE | size | \
138 P_RW_U_NA_Msk) }
139#define REGION_IO_ATTR(size) { (DEVICE_NON_SHAREABLE | size | P_RW_U_NA_Msk) }
140
142 /* Attributes belonging to RASR (including the encoded region size) */
144};
145
147
148/* Typedef for the k_mem_partition attribute */
149typedef struct {
152
153/* Read-Write access permission attributes */
154#define _K_MEM_PARTITION_P_NA_U_NA (NO_ACCESS_Msk | NOT_EXEC)
155#define _K_MEM_PARTITION_P_RW_U_RW (P_RW_U_RW_Msk | NOT_EXEC)
156#define _K_MEM_PARTITION_P_RW_U_RO (P_RW_U_RO_Msk | NOT_EXEC)
157#define _K_MEM_PARTITION_P_RW_U_NA (P_RW_U_NA_Msk | NOT_EXEC)
158#define _K_MEM_PARTITION_P_RO_U_RO (P_RO_U_RO_Msk | NOT_EXEC)
159#define _K_MEM_PARTITION_P_RO_U_NA (P_RO_U_NA_Msk | NOT_EXEC)
160
161/* Execution-allowed attributes */
162#define _K_MEM_PARTITION_P_RWX_U_RWX (P_RW_U_RW_Msk)
163#define _K_MEM_PARTITION_P_RWX_U_RX (P_RW_U_RO_Msk)
164#define _K_MEM_PARTITION_P_RX_U_RX (P_RO_U_RO_Msk)
165
166/* Kernel macros for memory attribution
167 * (access permissions and cache-ability).
168 *
169 * The macros are to be stored in k_mem_partition_attr_t
170 * objects. The format of k_mem_partition_attr_t is an
171 * "1-1" mapping of the ARMv7-M MPU RASR attribute register
172 * fields (excluding the <size> and <enable> bit-fields).
173 */
174
175/* Read-Write access permission attributes (default cache-ability) */
176#define K_MEM_PARTITION_P_NA_U_NA ((k_mem_partition_attr_t) \
177 { _K_MEM_PARTITION_P_NA_U_NA | \
178 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE})
179#define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
180 { _K_MEM_PARTITION_P_RW_U_RW | \
181 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE})
182#define K_MEM_PARTITION_P_RW_U_RO ((k_mem_partition_attr_t) \
183 { _K_MEM_PARTITION_P_RW_U_RO | \
184 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE})
185#define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
186 { _K_MEM_PARTITION_P_RW_U_NA | \
187 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE})
188#define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
189 { _K_MEM_PARTITION_P_RO_U_RO | \
190 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE})
191#define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
192 { _K_MEM_PARTITION_P_RO_U_NA | \
193 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE})
194
195/* Execution-allowed attributes (default-cacheability) */
196#define K_MEM_PARTITION_P_RWX_U_RWX ((k_mem_partition_attr_t) \
197 { _K_MEM_PARTITION_P_RWX_U_RWX | \
198 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE})
199#define K_MEM_PARTITION_P_RWX_U_RX ((k_mem_partition_attr_t) \
200 { _K_MEM_PARTITION_P_RWX_U_RX | \
201 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE})
202#define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
203 { _K_MEM_PARTITION_P_RX_U_RX | \
204 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE})
205
206/*
207 * @brief Evaluate Write-ability
208 *
209 * Evaluate whether the access permissions include write-ability.
210 *
211 * @param attr The k_mem_partition_attr_t object holding the
212 * MPU attributes to be checked against write-ability.
213 */
214#define K_MEM_PARTITION_IS_WRITABLE(attr) \
215 ({ \
216 int __is_writable__; \
217 switch (attr.rasr_attr & MPU_RASR_AP_Msk) { \
218 case P_RW_U_RW_Msk: \
219 case P_RW_U_RO_Msk: \
220 case P_RW_U_NA_Msk: \
221 __is_writable__ = 1; \
222 break; \
223 default: \
224 __is_writable__ = 0; \
225 } \
226 __is_writable__; \
227 })
228
229/*
230 * @brief Evaluate Execution allowance
231 *
232 * Evaluate whether the access permissions include execution.
233 *
234 * @param attr The k_mem_partition_attr_t object holding the
235 * MPU attributes to be checked against execution
236 * allowance.
237 */
238#define K_MEM_PARTITION_IS_EXECUTABLE(attr) \
239 (!((attr.rasr_attr) & (NOT_EXEC)))
240
241/* Attributes for no-cache enabling (share-ability is selected by default) */
242
243#define K_MEM_PARTITION_P_NA_U_NA_NOCACHE ((k_mem_partition_attr_t) \
244 {(_K_MEM_PARTITION_P_NA_U_NA \
245 | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
246#define K_MEM_PARTITION_P_RW_U_RW_NOCACHE ((k_mem_partition_attr_t) \
247 {(_K_MEM_PARTITION_P_RW_U_RW \
248 | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
249#define K_MEM_PARTITION_P_RW_U_RO_NOCACHE ((k_mem_partition_attr_t) \
250 {(_K_MEM_PARTITION_P_RW_U_RO \
251 | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
252#define K_MEM_PARTITION_P_RW_U_NA_NOCACHE ((k_mem_partition_attr_t) \
253 {(_K_MEM_PARTITION_P_RW_U_NA \
254 | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
255#define K_MEM_PARTITION_P_RO_U_RO_NOCACHE ((k_mem_partition_attr_t) \
256 {(_K_MEM_PARTITION_P_RO_U_RO \
257 | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
258#define K_MEM_PARTITION_P_RO_U_NA_NOCACHE ((k_mem_partition_attr_t) \
259 {(_K_MEM_PARTITION_P_RO_U_NA \
260 | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
261
262#define K_MEM_PARTITION_P_RWX_U_RWX_NOCACHE ((k_mem_partition_attr_t) \
263 {(_K_MEM_PARTITION_P_RWX_U_RWX \
264 | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
265#define K_MEM_PARTITION_P_RWX_U_RX_NOCACHE ((k_mem_partition_attr_t) \
266 {(_K_MEM_PARTITION_P_RWX_U_RX \
267 | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
268#define K_MEM_PARTITION_P_RX_U_RX_NOCACHE ((k_mem_partition_attr_t) \
269 {(_K_MEM_PARTITION_P_RX_U_RX \
270 | NORMAL_OUTER_INNER_NON_CACHEABLE_SHAREABLE)})
271
272#endif /* _ASMLANGUAGE */
273
274#define _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size) \
275 BUILD_ASSERT(!(((size) & ((size) - 1))) && \
276 (size) >= CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE && \
277 !((uint32_t)(start) & ((size) - 1)), \
278 "the size of the partition must be power of 2" \
279 " and greater than or equal to the minimum MPU region size." \
280 "start address of the partition must align with size.")
uint32_t k_mem_partition_attr_t
Definition: arch.h:267
CMSIS interface file.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
Definition: arm_mpu_v7m.h:141
uint32_t rasr
Definition: arm_mpu_v7m.h:143
uint32_t rasr_attr
Definition: arm_mpu_v7m.h:150