Skip to content

Commit e585eac

Browse files
committed
Fix MPU synchronisation
Synchronisation instructions were not quite right - too strict on entry, and not quite correctly synchronising the instruction stream on exit. References: * https://static.docs.arm.com/dai0321/a/DAI0321A_programming_guide_memory_barriers_for_m_profile.pdf * https://static.docs.arm.com/100699/0100/armv8m_architecture_memory_protection_unit_100699_0100_00_en.pdf
1 parent cdc61c5 commit e585eac

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

hal/mpu/mbed_mpu_v7m.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ MBED_STATIC_ASSERT(
4545
void mbed_mpu_init()
4646
{
4747
// Flush memory writes before configuring the MPU.
48-
__DSB();
48+
__DMB();
4949

5050
const uint32_t regions = (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos;
5151
if (regions < 4) {
@@ -178,40 +178,40 @@ void mbed_mpu_init()
178178
(1 << MPU_CTRL_ENABLE_Pos); // Enable MPU
179179

180180
// Ensure changes take effect
181-
__ISB();
182181
__DSB();
182+
__ISB();
183183
}
184184

185185
void mbed_mpu_free()
186186
{
187187
// Flush memory writes before configuring the MPU.
188-
__DSB();
188+
__DMB();
189189

190190
// Disable the MPU
191191
MPU->CTRL = 0;
192192

193193
// Ensure changes take effect
194-
__ISB();
195194
__DSB();
195+
__ISB();
196196
}
197197

198198
void mbed_mpu_enable_rom_wn(bool enable)
199199
{
200200
// Flush memory writes before configuring the MPU.
201-
__DSB();
201+
__DMB();
202202

203203
MPU->RNR = 0;
204204
MPU->RASR = (MPU->RASR & ~MPU_RASR_ENABLE_Msk) | (enable ? MPU_RASR_ENABLE_Msk : 0);
205205

206206
// Ensure changes take effect
207-
__ISB();
208207
__DSB();
208+
__ISB();
209209
}
210210

211211
void mbed_mpu_enable_ram_xn(bool enable)
212212
{
213213
// Flush memory writes before configuring the MPU.
214-
__DSB();
214+
__DMB();
215215

216216
MPU->RNR = 1;
217217
MPU->RASR = (MPU->RASR & ~MPU_RASR_ENABLE_Msk) | (enable ? MPU_RASR_ENABLE_Msk : 0);
@@ -223,8 +223,8 @@ void mbed_mpu_enable_ram_xn(bool enable)
223223
MPU->RASR = (MPU->RASR & ~MPU_RASR_ENABLE_Msk) | (enable ? MPU_RASR_ENABLE_Msk : 0);
224224

225225
// Ensure changes take effect
226-
__ISB();
227226
__DSB();
227+
__ISB();
228228
}
229229

230230
#endif

hal/mpu/mbed_mpu_v8m.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ MBED_STATIC_ASSERT(MBED_MPU_ROM_END == 0x1fffffff, "Changing MBED_MPU_ROM_END fo
3535
void mbed_mpu_init()
3636
{
3737
// Flush memory writes before configuring the MPU.
38-
__DSB();
38+
__DMB();
3939

4040
const uint32_t regions = (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos;
4141
if (regions < 4) {
@@ -127,40 +127,40 @@ void mbed_mpu_init()
127127
(1 << MPU_CTRL_ENABLE_Pos); // Enable MPU
128128

129129
// Ensure changes take effect
130-
__ISB();
131130
__DSB();
131+
__ISB();
132132
}
133133

134134
void mbed_mpu_free()
135135
{
136136
// Flush memory writes before configuring the MPU.
137-
__DSB();
137+
__DMB();
138138

139139
// Disable the MCU
140140
MPU->CTRL = 0;
141141

142142
// Ensure changes take effect
143-
__ISB();
144143
__DSB();
144+
__ISB();
145145
}
146146

147147
void mbed_mpu_enable_rom_wn(bool enable)
148148
{
149149
// Flush memory writes before configuring the MPU.
150-
__DSB();
150+
__DMB();
151151

152152
MPU->RNR = 0;
153153
MPU->RLAR = (MPU->RLAR & ~MPU_RLAR_EN_Msk) | (enable ? MPU_RLAR_EN_Msk : 0);
154154

155155
// Ensure changes take effect
156-
__ISB();
157156
__DSB();
157+
__ISB();
158158
}
159159

160160
void mbed_mpu_enable_ram_xn(bool enable)
161161
{
162162
// Flush memory writes before configuring the MPU.
163-
__DSB();
163+
__DMB();
164164

165165
MPU->RNR = 1;
166166
MPU->RLAR = (MPU->RLAR & ~MPU_RLAR_EN_Msk) | (enable ? MPU_RLAR_EN_Msk : 0);
@@ -172,8 +172,8 @@ void mbed_mpu_enable_ram_xn(bool enable)
172172
MPU->RLAR = (MPU->RLAR & ~MPU_RLAR_EN_Msk) | (enable ? MPU_RLAR_EN_Msk : 0);
173173

174174
// Ensure changes take effect
175-
__ISB();
176175
__DSB();
176+
__ISB();
177177
}
178178

179179
#endif

0 commit comments

Comments
 (0)