Skip to content

Add Nuvoton m2351device #4892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6eed3c5
Generate/Link secure object file
Sep 22, 2017
988b0dd
Non Secure flag is required for pre-processing of linker file
Sep 22, 2017
c4a1697
Allow main thread to call non-secure functions
Sep 12, 2017
87afc6b
Using proper lib functions as suggested in comments
Sep 25, 2017
6ba2bf9
Allow Idle and timer thread to access secure functions
Oct 10, 2017
7b5604e
Update mbed_boot in secure mode to call non-secure reset handler funt…
Oct 10, 2017
07cbb98
Build failed in case of malformed input, code section was not include…
Oct 10, 2017
af83953
Access to serial device is allowed when DEVICE_SERIAL is defined
Oct 11, 2017
e8ef7b2
Add TZ_context function
Sep 12, 2017
c459188
Trust zone module identifier added to thread class
Oct 13, 2017
b7358f8
Systick handler switch to secure/nonsecure
Oct 13, 2017
a7c18b8
[M2351] Add one new target M2351, regard as M0+ with some V8M CPU con…
cyliangtw Jun 30, 2017
dbbc819
[M2351] Sync SDH_CardDetection type to avoid GCC compiler error
cyliangtw Jul 4, 2017
59b78df
[M2351] Support GCC & IAR toolchain
cyliangtw Jul 4, 2017
3246780
[M2351] Support __vector_table instead of __vector_handlers in IAR
cyliangtw Jul 4, 2017
0c8c3c7
[M2351] remove progen, not used any more
cyliangtw Jul 5, 2017
df3fa29
[M2351] Remove mbed_sdk_init_forced
cyliangtw Jul 12, 2017
301b99b
[M2351] Add partition header file for CMSE feature
cyliangtw Aug 10, 2017
8d055f0
[M2351] Modify Nuvoton common files to avoid conflicting with master
cyliangtw Aug 15, 2017
44f183f
[M2351] Revise nu_bitutil.h for M23
cyliangtw Aug 18, 2017
5305656
[M2351] Use Cortex M23 specific header files and interrupts
Aug 11, 2017
d1af03f
[M2351] Added xx_ticker_fire_interrupt function for M2351 device
Aug 17, 2017
dcc7c77
[M2351] Set SAU Region present flag for M2351 device and include secu…
Sep 11, 2017
4baf647
[M2351] Removed device name, till device patch is added to IAR/Keil
Sep 12, 2017
f3b7864
[M2351] ARMC6 compiler related changes
Sep 14, 2017
8e6103d
[M2351] Update GCC linker for NSC Veneer
cyliangtw Sep 15, 2017
660e9a9
[M2351] Linker files support both of secure & non-secure domain
cyliangtw Sep 19, 2017
255ca5c
[M2351] IAR linker file support both of secure & non-secure domain
cyliangtw Sep 19, 2017
862b943
[M2351] Fix GCC linker file 'cannot move location counter backwards' …
cyliangtw Sep 20, 2017
2b5dea6
[M2351] Corrected preprocess define usage in toolchain specific linke…
Sep 27, 2017
460f80c
[M2351] Support secure loader invoke non-secure Mbed OS
cyliangtw Sep 27, 2017
db25e2d
[M2351] Link register base with partition file & correct heap size in…
cyliangtw Sep 29, 2017
ce23676
[M2351] Corrected Vector table address in scatter file
Sep 29, 2017
508037e
[M2351] Add non-secure reset handler address
Oct 10, 2017
1ed15f8
Updating trustzone functions for IAR
Oct 16, 2017
c3822e0
Port dir not required to be secure
Oct 17, 2017
9162f40
Temp change for debugging
Oct 17, 2017
67499b2
Enable export to uvision5 + armc6
theotherjimmy Sep 28, 2017
15e8d94
Correct check for in-template flags
theotherjimmy Oct 10, 2017
bed8a0b
Use relative path to detect config header and remove -std options
theotherjimmy Oct 17, 2017
ea6212a
Remove -MMD and debug print
theotherjimmy Oct 17, 2017
7005a67
Enable uvision export for NS cores
theotherjimmy Oct 17, 2017
83cd048
[M2351] Updating secure/non-secure functions
Oct 18, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions platform/mbed_retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@ extern "C" void PREFIX(_exit)(int return_code) {
}

extern "C" void _ttywrch(int ch) {
#if DEVICE_SERIAL
serial_putc(&stdio_uart, ch);
#endif
}
#endif

Expand Down Expand Up @@ -734,6 +736,7 @@ extern "C" int errno;

// Dynamic memory allocation related syscall.
#if defined(TARGET_NUVOTON)

// Overwrite _sbrk() to support two region model (heap and stack are two distinct regions).
// __wrap__sbrk() is implemented in:
// TARGET_NUMAKER_PFM_NUC472 targets/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/TOOLCHAIN_GCC_ARM/nuc472_retarget.c
Expand Down
27 changes: 26 additions & 1 deletion platform/mbed_sdk_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,39 @@
*/
#if !defined(MBED_CONF_RTOS_PRESENT)

#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
/* call_non_secure_rtos is function for v8 architecture to switch to non-secure
* reset handler. Called by secure application main.
*/
typedef void (*non_secure_call) (void) __attribute__((cmse_nonsecure_call));
void call_non_secure_rtos(void)
{
#ifdef TZ_START_NS
non_secure_call ns_reset_handler;

/* Set non-secure main stack (MSP_NS) */
#if defined(__ICCARM__)
asm volatile("MSR SP_NS, %0" :: "r" (*((uint32_t *)(TZ_START_NS))));
#else
__TZ_set_MSP_NS(*((uint32_t *)(TZ_START_NS)));
#endif
/* Get non-secure reset handler */
ns_reset_handler = (non_secure_call)(*((uint32_t *)((TZ_START_NS) + 4U)));
/* Start non-secure state software application */
ns_reset_handler();
#endif
}
#endif
/* mbed_main is a function that is called before main()
* mbed_sdk_init() is also a function that is called before main(), but unlike
* mbed_main(), it is not meant for user code, but for the SDK itself to perform
* initializations before main() is called.
*/
MBED_WEAK void mbed_main(void)
{

#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
call_non_secure_rtos();
#endif
}

/* This function can be implemented by the target to perform higher level target initialization
Expand Down
215 changes: 215 additions & 0 deletions platform/mbed_tz_context.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

#include "cmsis.h"
#include "mbed_rtx.h"
#include "tz_context.h"


/// Number of process slots (threads may call secure library code)
#ifndef TZ_PROCESS_STACK_SLOTS
#define TZ_PROCESS_STACK_SLOTS 8U
#endif

/// Stack size of the secure library code
#ifndef TZ_PROCESS_STACK_SIZE
#define TZ_PROCESS_STACK_SIZE 512U
#endif

typedef struct {
uint32_t sp_top; // stack space top
uint32_t sp_limit; // stack space limit
uint32_t sp; // current stack pointer
} stack_info_t;

static stack_info_t ProcessStackInfo [TZ_PROCESS_STACK_SLOTS];
static uint64_t ProcessStackMemory[TZ_PROCESS_STACK_SLOTS][TZ_PROCESS_STACK_SIZE/8U];
static uint32_t ProcessStackFreeSlot = 0xFFFFFFFFU;


/// Initialize secure context memory system
/// \return execution status (1: success, 0: error)
__attribute__((cmse_nonsecure_entry))
uint32_t TZ_InitContextSystem_S (void)
{
uint32_t n;

if (__get_IPSR() == 0U) {
return 0U; // Thread Mode
}

for (n = 0U; n < TZ_PROCESS_STACK_SLOTS; n++) {
ProcessStackInfo[n].sp = 0U;
ProcessStackInfo[n].sp_limit = (uint32_t)&ProcessStackMemory[n];
ProcessStackInfo[n].sp_top = (uint32_t)&ProcessStackMemory[n] + TZ_PROCESS_STACK_SIZE;
*((uint32_t *)ProcessStackMemory[n]) = n + 1U;
}
*((uint32_t *)ProcessStackMemory[--n]) = 0xFFFFFFFFU;

ProcessStackFreeSlot = 0U;

// Default process stack pointer and stack limit
#if defined(__ICCARM__)
asm volatile("MSR PSPLIM, %0" :: "r" ((uint32_t)ProcessStackMemory));
asm volatile("MSR PSP, %0" :: "r" ((uint32_t)ProcessStackMemory));
#else
__set_PSPLIM ((uint32_t)ProcessStackMemory);
__set_PSP ((uint32_t)ProcessStackMemory);
#endif

// Privileged Thread Mode using PSP
__set_CONTROL(0x02U);

return 1U; // Success
}


/// Allocate context memory for calling secure software modules in TrustZone
/// \param[in] module identifies software modules called from non-secure mode
/// \return value != 0 id TrustZone memory slot identifier
/// \return value 0 no memory available or internal error
__attribute__((cmse_nonsecure_entry))
TZ_MemoryId_t TZ_AllocModuleContext_S (TZ_ModuleId_t module)
{
uint32_t slot;

(void)module; // Ignore (fixed Stack size)

if (__get_IPSR() == 0U) {
return 0U; // Thread Mode
}

if (ProcessStackFreeSlot == 0xFFFFFFFFU) {
return 0U; // No slot available
}

slot = ProcessStackFreeSlot;
ProcessStackFreeSlot = *((uint32_t *)ProcessStackMemory[slot]);

ProcessStackInfo[slot].sp = ProcessStackInfo[slot].sp_top;

return (slot + 1U);
}

/// Free context memory that was previously allocated with \ref TZ_AllocModuleContext_S
/// \param[in] id TrustZone memory slot identifier
/// \return execution status (1: success, 0: error)
__attribute__((cmse_nonsecure_entry))
uint32_t TZ_FreeModuleContext_S (TZ_MemoryId_t id)
{
uint32_t slot;

if (__get_IPSR() == 0U) {
return 0U; // Thread Mode
}

if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
return 0U; // Invalid ID
}

slot = id - 1U;

if (ProcessStackInfo[slot].sp == 0U) {
return 0U; // Inactive slot
}
ProcessStackInfo[slot].sp = 0U;

*((uint32_t *)ProcessStackMemory[slot]) = ProcessStackFreeSlot;
ProcessStackFreeSlot = slot;

return 1U; // Success
}


/// Load secure context (called on RTOS thread context switch)
/// \param[in] id TrustZone memory slot identifier
/// \return execution status (1: success, 0: error)
__attribute__((cmse_nonsecure_entry))
uint32_t TZ_LoadContext_S (TZ_MemoryId_t id)
{
uint32_t slot;

if ((__get_IPSR() == 0U) || ((__get_CONTROL() & 2U) == 0U)) {
return 0U; // Thread Mode or using Main Stack for threads
}

if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
return 0U; // Invalid ID
}

slot = id - 1U;

if (ProcessStackInfo[slot].sp == 0U) {
return 0U; // Inactive slot
}

// Setup process stack pointer and stack limit
#if defined(__ICCARM__)
asm volatile("MSR PSPLIM, %0" :: "r" ((uint32_t)ProcessStackInfo[slot].sp_limit));
asm volatile("MSR PSP, %0" :: "r" ((uint32_t)ProcessStackInfo[slot].sp));
#else
__set_PSPLIM(ProcessStackInfo[slot].sp_limit);
__set_PSP (ProcessStackInfo[slot].sp);
#endif

return 1U; // Success
}


/// Store secure context (called on RTOS thread context switch)
/// \param[in] id TrustZone memory slot identifier
/// \return execution status (1: success, 0: error)
__attribute__((cmse_nonsecure_entry))
uint32_t TZ_StoreContext_S (TZ_MemoryId_t id)
{
uint32_t slot;
uint32_t sp;

if ((__get_IPSR() == 0U) || ((__get_CONTROL() & 2U) == 0U)) {
return 0U; // Thread Mode or using Main Stack for threads
}

if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
return 0U; // Invalid ID
}

slot = id - 1U;

if (ProcessStackInfo[slot].sp == 0U) {
return 0U; // Inactive slot
}

sp = __get_PSP();
if ((sp < ProcessStackInfo[slot].sp_limit) ||
(sp > ProcessStackInfo[slot].sp_top)) {
return 0U; // SP out of range
}
ProcessStackInfo[slot].sp = sp;

// Default process stack pointer and stack limit
#if defined(__ICCARM__)
asm volatile("MSR PSPLIM, %0" :: "r" ((uint32_t)ProcessStackMemory));
asm volatile("MSR PSP, %0" :: "r" ((uint32_t)ProcessStackMemory));
#else
__set_PSPLIM ((uint32_t)ProcessStackMemory);
__set_PSP ((uint32_t)ProcessStackMemory);
#endif

return 1U; // Success
}
#endif
6 changes: 6 additions & 0 deletions rtos/TARGET_CORTEX/mbed_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ void mbed_start_main(void)
_main_thread_attr.cb_mem = &_main_obj;
_main_thread_attr.priority = osPriorityNormal;
_main_thread_attr.name = "main_thread";

/* Allow main thread to call secure functions */
#if (__DOMAIN_NS == 1U)
_main_thread_attr.tz_module = 1U;
#endif

osThreadId_t result = osThreadNew((osThreadFunc_t)pre_main, NULL, &_main_thread_attr);
if ((void *)result == NULL) {
error("Pre main thread not created");
Expand Down
18 changes: 18 additions & 0 deletions rtos/TARGET_CORTEX/mbed_rtx_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,22 @@
# define OS_PRIVILEGE_MODE 0
#endif

#define OS_SECURE_CALLABLE_THREAD 1

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

/* Number of process slots (threads may call secure library code) */
#ifndef MBED_CONF_APP_TZ_PROCESS_STACK_SLOTS
#define MBED_CONF_APP_TZ_PROCESS_STACK_SLOTS 8U
#endif

/* Stack size of the secure library code */
#ifndef MBED_CONF_APP_TZ_PROCESS_STACK_SIZE
#define MBED_CONF_APP_TZ_PROCESS_STACK_SIZE 512U
#endif

#define TZ_PROCESS_STACK_SLOTS MBED_CONF_APP_TZ_PROCESS_STACK_SLOTS
#define TZ_PROCESS_STACK_SIZE MBED_CONF_APP_TZ_PROCESS_STACK_SIZE
#endif

#endif /* MBED_RTX_CONF_H */
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ Sys_ContextSave
BL TZ_StoreContext_S ; Store secure context
MOV LR,R7 ; Set EXC_RETURN
POP {R1,R2,R3,R7} ; Restore registers
LSLS R7,R7,#25 ; Check domain of interrupted thread
BMI Sys_ContextSave1 ; Branch if secure
MOV R0,LR ; Get EXC_RETURN
LSLS R0,R0,#25 ; Check domain of interrupted thread
BPL Sys_ContextSave1 ; Branch if not secure
MRS R0,PSP ; Get PSP
STR R0,[R1,#TCB_SP_OFS] ; Store SP
B Sys_ContextSave2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ Sys_ContextSave:
BL TZ_StoreContext_S // Store secure context
MOV LR,R7 // Set EXC_RETURN
POP {R1,R2,R3,R7} // Restore registers
LSLS R7,R7,#25 // Check domain of interrupted thread
BMI Sys_ContextSave1 // Branch if secure
MOV R0,LR // Get EXC_RETURN
LSLS R0,R0,#25 // Check domain of interrupted thread
BPL Sys_ContextSave1 // Branch if not secure
MRS R0,PSP // Get PSP
STR R0,[R1,#TCB_SP_OFS] // Store SP
B Sys_ContextSave2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ Sys_ContextSave
BL TZ_StoreContext_S ; Store secure context
MOV LR,R7 ; Set EXC_RETURN
POP {R1,R2,R3,R7} ; Restore registers
LSLS R7,R7,#25 ; Check domain of interrupted thread
BMI Sys_ContextSave1 ; Branch if secure
MOV R0,LR ; Get EXC_RETURN
LSLS R0,R0,#25 ; Check domain of interrupted thread
BPL Sys_ContextSave1 ; Branch if not secure
MRS R0,PSP ; Get PSP
STR R0,[R1,#TCB_SP_OFS] ; Store SP
B Sys_ContextSave2
Expand Down
14 changes: 12 additions & 2 deletions rtos/TARGET_CORTEX/rtx5/rtx_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ static const osThreadAttr_t os_idle_thread_attr = {
&os_idle_thread_stack,
(uint32_t)sizeof(os_idle_thread_stack),
osPriorityIdle,
0U, 0U
#if (__DOMAIN_NS == 1U)
1U,
#else
0U,
#endif
0U
};


Expand Down Expand Up @@ -176,7 +181,12 @@ static const osThreadAttr_t os_timer_thread_attr = {
&os_timer_thread_stack,
(uint32_t)sizeof(os_timer_thread_stack),
(osPriority_t)OS_TIMER_THREAD_PRIO,
0U, 0U
#if (__DOMAIN_NS == 1U)
1U,
#else
0U,
#endif
0U
};

// Timer Message Queue Control Block
Expand Down
7 changes: 4 additions & 3 deletions rtos/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern "C" void thread_terminate_hook(osThreadId_t id)
namespace rtos {

void Thread::constructor(osPriority priority,
uint32_t stack_size, unsigned char *stack_mem, const char *name) {
uint32_t stack_size, unsigned char *stack_mem, const char *name, uint32_t tz_module) {
_tid = 0;
_dynamic_stack = (stack_mem == NULL);
_finished = false;
Expand All @@ -45,11 +45,12 @@ void Thread::constructor(osPriority priority,
_attr.stack_size = stack_size;
_attr.name = name ? name : "application_unnamed_thread";
_attr.stack_mem = (uint32_t*)stack_mem;
_attr.tz_module = tz_module;
}

void Thread::constructor(Callback<void()> task,
osPriority priority, uint32_t stack_size, unsigned char *stack_mem, const char *name) {
constructor(priority, stack_size, stack_mem, name);
osPriority priority, uint32_t stack_size, unsigned char *stack_mem, const char *name, uint32_t tz_module) {
constructor(priority, stack_size, stack_mem, name, tz_module);

switch (start(task)) {
case osErrorResource:
Expand Down
Loading