Zephyr API Documentation  2.7.0-rc2
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#if defined(CONFIG_MPU_ALLOW_FLASH_WRITE)
131/* Note that the access permissions allow for un-privileged writes, contrary
132 * to ARMv7-M where un-privileged code has Read-Only permissions.
133 */
134#define REGION_FLASH_ATTR(base, size) \
135 {\
136 .rbar = P_RW_U_RW_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
137 /* Cache-ability */ \
138 .mair_idx = MPU_MAIR_INDEX_FLASH, \
139 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
140 }
141#else /* CONFIG_MPU_ALLOW_FLASH_WRITE */
142#define REGION_FLASH_ATTR(base, size) \
143 {\
144 .rbar = RO_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
145 /* Cache-ability */ \
146 .mair_idx = MPU_MAIR_INDEX_FLASH, \
147 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
148 }
149#endif /* CONFIG_MPU_ALLOW_FLASH_WRITE */
150
151
152struct arm_mpu_region_attr {
153 /* Attributes belonging to RBAR */
155 /* MAIR index for attribute indirection */
157 /* Region Limit Address value to be written to the RLAR register. */
159};
160
162
163/* Typedef for the k_mem_partition attribute */
164typedef struct {
168
169/* Kernel macros for memory attribution
170 * (access permissions and cache-ability).
171 *
172 * The macros are to be stored in k_mem_partition_attr_t
173 * objects. The format of a k_mem_partition_attr_t object
174 * is as follows: field <rbar> contains a direct mapping
175 * of the <XN> and <AP> bit-fields of the RBAR register;
176 * field <mair_idx> contains a direct mapping of AttrIdx
177 * bit-field, stored in RLAR register.
178 */
179
180/* Read-Write access permission attributes */
181#define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
182 {(P_RW_U_RW_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
183#define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
184 {(P_RW_U_NA_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
185#define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
186 {(P_RO_U_RO_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
187#define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
188 {(P_RO_U_NA_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
189
190/* Execution-allowed attributes */
191#define K_MEM_PARTITION_P_RWX_U_RWX ((k_mem_partition_attr_t) \
192 {(P_RW_U_RW_Msk), MPU_MAIR_INDEX_SRAM})
193#define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
194 {(P_RO_U_RO_Msk), MPU_MAIR_INDEX_SRAM})
195
196/*
197 * @brief Evaluate Write-ability
198 *
199 * Evaluate whether the access permissions include write-ability.
200 *
201 * @param attr The k_mem_partition_attr_t object holding the
202 * MPU attributes to be checked against write-ability.
203 */
204#define K_MEM_PARTITION_IS_WRITABLE(attr) \
205 ({ \
206 int __is_writable__; \
207 switch (attr.rbar & MPU_RBAR_AP_Msk) { \
208 case P_RW_U_RW_Msk: \
209 case P_RW_U_NA_Msk: \
210 __is_writable__ = 1; \
211 break; \
212 default: \
213 __is_writable__ = 0; \
214 } \
215 __is_writable__; \
216 })
217
218/*
219 * @brief Evaluate Execution allowance
220 *
221 * Evaluate whether the access permissions include execution.
222 *
223 * @param attr The k_mem_partition_attr_t object holding the
224 * MPU attributes to be checked against execution
225 * allowance.
226 */
227#define K_MEM_PARTITION_IS_EXECUTABLE(attr) \
228 (!((attr.rbar) & (NOT_EXEC)))
229
230/* Attributes for no-cache enabling (share-ability is selected by default) */
231
232/* Read-Write access permission attributes */
233#define K_MEM_PARTITION_P_RW_U_RW_NOCACHE ((k_mem_partition_attr_t) \
234 {(P_RW_U_RW_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
235 MPU_MAIR_INDEX_SRAM_NOCACHE})
236#define K_MEM_PARTITION_P_RW_U_NA_NOCACHE ((k_mem_partition_attr_t) \
237 {(P_RW_U_NA_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
238 MPU_MAIR_INDEX_SRAM_NOCACHE})
239#define K_MEM_PARTITION_P_RO_U_RO_NOCACHE ((k_mem_partition_attr_t) \
240 {(P_RO_U_RO_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
241 MPU_MAIR_INDEX_SRAM_NOCACHE})
242#define K_MEM_PARTITION_P_RO_U_NA_NOCACHE ((k_mem_partition_attr_t) \
243 {(P_RO_U_NA_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
244 MPU_MAIR_INDEX_SRAM_NOCACHE})
245
246/* Execution-allowed attributes */
247#define K_MEM_PARTITION_P_RWX_U_RWX_NOCACHE ((k_mem_partition_attr_t) \
248 {(P_RW_U_RW_Msk | OUTER_SHAREABLE_Msk), MPU_MAIR_INDEX_SRAM_NOCACHE})
249#define K_MEM_PARTITION_P_RX_U_RX_NOCACHE ((k_mem_partition_attr_t) \
250 {(P_RO_U_RO_Msk | OUTER_SHAREABLE_Msk), MPU_MAIR_INDEX_SRAM_NOCACHE})
251
252#endif /* _ASMLANGUAGE */
253
254#define _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size) \
255 BUILD_ASSERT((size > 0) && ((uint32_t)start % \
256 CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE == 0U) && \
257 ((size) % CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE == 0), \
258 " the start and size of the partition must align " \
259 "with the minimum MPU region size.")
uint32_t k_mem_partition_attr_t
Definition: arch.h:267
CMSIS interface file.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
Definition: arm_mpu_v7m.h:141
uint8_t rbar
Definition: arm_mpu_v8m.h:154
uint32_t r_limit
Definition: arm_mpu_v8m.h:158
uint8_t mair_idx
Definition: arm_mpu_v8m.h:156
uint16_t rbar
Definition: arm_mpu_v8m.h:165
uint16_t mair_idx
Definition: arm_mpu_v8m.h:166