Skip to content

Commit dec84ed

Browse files
committed
[M2351] Enforce locked entry into TFM for platform-specific NSC calls
On M2351, some spaces like SYS/CLK are hard-wired to secure and cannot change. To access these spaces from non-secure world, we must provide platform-specific NSC functions. With TFM introduced, we must synchronize NSC calls into TFM to keep TFM in sync instead of straight NSC calls. To achieve this goal, we go with the following approach: 1. Like PSA APIs, enforce locked entry through tfm_ns_lock_dispatch(). 2. Run platform-specific secure functions in default secure partition, in which SYS/CLK spaces have been configured to be accessible.
1 parent ad80ed1 commit dec84ed

File tree

6 files changed

+647
-51
lines changed

6 files changed

+647
-51
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/device/stddriver_secure.c

Lines changed: 238 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "partition_M2351.h"
2121
#include "stddriver_secure.h"
2222
#include "mbed_error.h"
23+
#if defined(DOMAIN_NS) && (DOMAIN_NS == 1L) && (TFM_LVL > 0)
24+
#include "tfm_ns_lock.h"
25+
#endif
2326

2427
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
2528

@@ -133,78 +136,70 @@ static const nu_modidx_ns_t modidx_ns_tab[] = {
133136
*/
134137
static bool check_mod_ns(int modclass, uint32_t modidx);
135138

136-
__NONSECURE_ENTRY
137-
void SYS_ResetModule_S(uint32_t u32ModuleIndex)
139+
static void SYS_ResetModule_Impl(uint32_t u32ModuleIndex, bool nonsecure_caller)
138140
{
139141
/* Guard access to secure module from non-secure domain */
140-
if (cmse_nonsecure_caller() &&
142+
if (nonsecure_caller &&
141143
(! check_mod_ns(NU_MODCLASS_SYS, u32ModuleIndex))) {
142144
error("Non-secure domain tries to control secure or undefined module.");
143145
}
144146

145147
SYS_ResetModule(u32ModuleIndex);
146148
}
147149

148-
__NONSECURE_ENTRY
149-
void CLK_SetModuleClock_S(uint32_t u32ModuleIndex, uint32_t u32ClkSrc, uint32_t u32ClkDiv)
150+
static void CLK_SetModuleClock_Impl(uint32_t u32ModuleIndex, uint32_t u32ClkSrc, uint32_t u32ClkDiv, bool nonsecure_caller)
150151
{
151152
/* Guard access to secure module from non-secure domain */
152-
if (cmse_nonsecure_caller() &&
153+
if (nonsecure_caller &&
153154
(! check_mod_ns(NU_MODCLASS_CLK, u32ModuleIndex))) {
154155
error("Non-secure domain tries to control secure or undefined module.");
155156
}
156157

157158
CLK_SetModuleClock(u32ModuleIndex, u32ClkSrc, u32ClkDiv);
158159
}
159160

160-
__NONSECURE_ENTRY
161-
void CLK_EnableModuleClock_S(uint32_t u32ModuleIndex)
161+
static void CLK_EnableModuleClock_Impl(uint32_t u32ModuleIndex, bool nonsecure_caller)
162162
{
163163
/* Guard access to secure module from non-secure domain */
164-
if (cmse_nonsecure_caller() &&
164+
if (nonsecure_caller &&
165165
(! check_mod_ns(NU_MODCLASS_CLK, u32ModuleIndex))) {
166166
error("Non-secure domain tries to control secure or undefined module.");
167167
}
168168

169169
CLK_EnableModuleClock(u32ModuleIndex);
170170
}
171171

172-
__NONSECURE_ENTRY
173-
void CLK_DisableModuleClock_S(uint32_t u32ModuleIndex)
172+
static void CLK_DisableModuleClock_Impl(uint32_t u32ModuleIndex, bool nonsecure_caller)
174173
{
175174
/* Guard access to secure module from non-secure domain */
176-
if (cmse_nonsecure_caller() &&
175+
if (nonsecure_caller &&
177176
(! check_mod_ns(NU_MODCLASS_CLK, u32ModuleIndex))) {
178177
error("Non-secure domain tries to control secure or undefined module.");
179178
}
180179

181180
CLK_DisableModuleClock(u32ModuleIndex);
182181
}
183182

184-
__NONSECURE_ENTRY
185-
void SYS_LockReg_S(void)
183+
static void SYS_LockReg_Impl(void)
186184
{
187185
/* Allow non-secure domain to lock/unlock locked registers without check.
188186
* Guard access to locked registers is done through other related secure functions. */
189187
SYS_LockReg();
190188
}
191189

192-
__NONSECURE_ENTRY
193-
void SYS_UnlockReg_S(void)
190+
static void SYS_UnlockReg_Impl(void)
194191
{
195192
/* Allow non-secure domain to lock/unlock locked registers without check.
196193
* Guard access to locked registers is done through other related secure functions. */
197194
SYS_UnlockReg();
198195
}
199196

200-
__NONSECURE_ENTRY
201-
void CLK_Idle_S(void)
197+
static void CLK_Idle_Impl(void)
202198
{
203199
CLK_Idle();
204200
}
205201

206-
__NONSECURE_ENTRY
207-
void CLK_PowerDown_S(void)
202+
static void CLK_PowerDown_Impl(void)
208203
{
209204
CLK_PowerDown();
210205
}
@@ -251,4 +246,227 @@ static bool check_mod_ns(int modclass, uint32_t modidx)
251246
return false;
252247
}
253248

249+
#if (TFM_LVL > 0)
250+
251+
__NONSECURE_ENTRY
252+
int32_t SYS_ResetModule_Veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
253+
{
254+
uint32_t u32ModuleIndex = (uint32_t) arg0;
255+
SYS_ResetModule_Impl(u32ModuleIndex, cmse_nonsecure_caller());
256+
return 0;
257+
}
258+
259+
__NONSECURE_ENTRY
260+
int32_t CLK_SetModuleClock_Veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
261+
{
262+
uint32_t u32ModuleIndex = (uint32_t) arg0;
263+
uint32_t u32ClkSrc = (uint32_t) arg1;
264+
uint32_t u32ClkDiv = (uint32_t) arg2;
265+
CLK_SetModuleClock_Impl(u32ModuleIndex, u32ClkSrc, u32ClkDiv, cmse_nonsecure_caller());
266+
return 0;
267+
}
268+
269+
__NONSECURE_ENTRY
270+
int32_t CLK_EnableModuleClock_Veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
271+
{
272+
uint32_t u32ModuleIndex = (uint32_t) arg0;
273+
CLK_EnableModuleClock_Impl(u32ModuleIndex, cmse_nonsecure_caller());
274+
return 0;
275+
}
276+
277+
__NONSECURE_ENTRY
278+
int32_t CLK_DisableModuleClock_Veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
279+
{
280+
uint32_t u32ModuleIndex = (uint32_t) arg0;
281+
CLK_DisableModuleClock_Impl(u32ModuleIndex, cmse_nonsecure_caller());
282+
return 0;
283+
}
284+
285+
__NONSECURE_ENTRY
286+
int32_t SYS_LockReg_Veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
287+
{
288+
SYS_LockReg_Impl();
289+
return 0;
290+
}
291+
292+
__NONSECURE_ENTRY
293+
int32_t SYS_UnlockReg_Veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
294+
{
295+
SYS_UnlockReg_Impl();
296+
return 0;
297+
}
298+
299+
__NONSECURE_ENTRY
300+
int32_t CLK_Idle_Veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
301+
{
302+
CLK_Idle_Impl();
303+
return 0;
304+
}
305+
306+
__NONSECURE_ENTRY
307+
int32_t CLK_PowerDown_Veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
308+
{
309+
CLK_PowerDown_Impl();
310+
return 0;
311+
}
312+
313+
__NONSECURE_ENTRY
314+
int32_t nu_idle_veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
315+
{
316+
SYS_UnlockReg_Impl();
317+
CLK_Idle_Impl();
318+
SYS_LockReg_Impl();
319+
return 0;
320+
}
321+
322+
__NONSECURE_ENTRY
323+
int32_t nu_powerdown_veneer(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
324+
{
325+
SYS_UnlockReg_Impl();
326+
CLK_PowerDown_Impl();
327+
SYS_LockReg_Impl();
328+
return 0;
329+
}
330+
331+
#endif
332+
#endif
333+
334+
#if defined(DOMAIN_NS) && (DOMAIN_NS == 1) && (TFM_LVL > 0)
335+
336+
void SYS_ResetModule_S(uint32_t u32ModuleIndex)
337+
{
338+
tfm_ns_lock_dispatch(SYS_ResetModule_Veneer, u32ModuleIndex, 0, 0, 0);
339+
}
340+
341+
void CLK_SetModuleClock_S(uint32_t u32ModuleIndex, uint32_t u32ClkSrc, uint32_t u32ClkDiv)
342+
{
343+
tfm_ns_lock_dispatch(CLK_SetModuleClock_Veneer, u32ModuleIndex, u32ClkSrc, u32ClkDiv, 0);
344+
}
345+
346+
void CLK_EnableModuleClock_S(uint32_t u32ModuleIndex)
347+
{
348+
tfm_ns_lock_dispatch(CLK_EnableModuleClock_Veneer, u32ModuleIndex, 0, 0, 0);
349+
}
350+
351+
void CLK_DisableModuleClock_S(uint32_t u32ModuleIndex)
352+
{
353+
tfm_ns_lock_dispatch(CLK_DisableModuleClock_Veneer, u32ModuleIndex, 0, 0, 0);
354+
}
355+
356+
void SYS_LockReg_S(void)
357+
{
358+
tfm_ns_lock_dispatch(SYS_LockReg_Veneer, 0, 0, 0, 0);
359+
}
360+
361+
void SYS_UnlockReg_S(void)
362+
{
363+
tfm_ns_lock_dispatch(SYS_UnlockReg_Veneer, 0, 0, 0, 0);
364+
}
365+
366+
void CLK_Idle_S(void)
367+
{
368+
tfm_ns_lock_dispatch(CLK_Idle_Veneer, 0, 0, 0, 0);
369+
}
370+
371+
void CLK_PowerDown_S(void)
372+
{
373+
tfm_ns_lock_dispatch(CLK_PowerDown_Veneer, 0, 0, 0, 0);
374+
}
375+
376+
void nu_idle_s(void)
377+
{
378+
tfm_ns_lock_dispatch(nu_idle_veneer, 0, 0, 0, 0);
379+
}
380+
381+
void nu_powerdown_s(void)
382+
{
383+
tfm_ns_lock_dispatch(nu_powerdown_veneer, 0, 0, 0, 0);
384+
}
385+
386+
#elif defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
387+
388+
#if (TFM_LVL == 0)
389+
__NONSECURE_ENTRY
390+
#endif
391+
void SYS_ResetModule_S(uint32_t u32ModuleIndex)
392+
{
393+
SYS_ResetModule_Impl(u32ModuleIndex, cmse_nonsecure_caller());
394+
}
395+
396+
#if (TFM_LVL == 0)
397+
__NONSECURE_ENTRY
398+
#endif
399+
void CLK_SetModuleClock_S(uint32_t u32ModuleIndex, uint32_t u32ClkSrc, uint32_t u32ClkDiv)
400+
{
401+
CLK_SetModuleClock_Impl(u32ModuleIndex, u32ClkSrc, u32ClkDiv, cmse_nonsecure_caller());
402+
}
403+
404+
#if (TFM_LVL == 0)
405+
__NONSECURE_ENTRY
406+
#endif
407+
void CLK_EnableModuleClock_S(uint32_t u32ModuleIndex)
408+
{
409+
CLK_EnableModuleClock_Impl(u32ModuleIndex, cmse_nonsecure_caller());
410+
}
411+
412+
#if (TFM_LVL == 0)
413+
__NONSECURE_ENTRY
414+
#endif
415+
void CLK_DisableModuleClock_S(uint32_t u32ModuleIndex)
416+
{
417+
CLK_DisableModuleClock_Impl(u32ModuleIndex, cmse_nonsecure_caller());
418+
}
419+
420+
#if (TFM_LVL == 0)
421+
__NONSECURE_ENTRY
422+
#endif
423+
void SYS_LockReg_S(void)
424+
{
425+
SYS_LockReg_Impl();
426+
}
427+
428+
#if (TFM_LVL == 0)
429+
__NONSECURE_ENTRY
430+
#endif
431+
void SYS_UnlockReg_S(void)
432+
{
433+
SYS_UnlockReg_Impl();
434+
}
435+
436+
#if (TFM_LVL == 0)
437+
__NONSECURE_ENTRY
438+
#endif
439+
void CLK_Idle_S(void)
440+
{
441+
CLK_Idle_Impl();
442+
}
443+
444+
#if (TFM_LVL == 0)
445+
__NONSECURE_ENTRY
446+
#endif
447+
void CLK_PowerDown_S(void)
448+
{
449+
CLK_PowerDown_Impl();
450+
}
451+
452+
#if (TFM_LVL == 0)
453+
__NONSECURE_ENTRY
454+
#endif
455+
void nu_idle_s(void)
456+
{
457+
SYS_UnlockReg_Impl();
458+
CLK_Idle_Impl();
459+
SYS_LockReg_Impl();
460+
}
461+
462+
#if (TFM_LVL == 0)
463+
__NONSECURE_ENTRY
464+
#endif
465+
void nu_powerdown_s(void)
466+
{
467+
SYS_UnlockReg_Impl();
468+
CLK_PowerDown_Impl();
469+
SYS_LockReg_Impl();
470+
}
471+
254472
#endif

0 commit comments

Comments
 (0)