SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
example.cpp File Reference

TrustZone + per-task MPU sandboxing, Non-Secure side. More...

#include <new>
#include <pico/runtime.h>
#include <stk.h>
#include <sync/stk_sync.h>
#include <time/stk_time.h>
#include <arch/arm/cortex-m/stk_arch_arm-tz.h>
#include "example.h"
#include "pico/unique_id.h"
Include dependency graph for blinky_mpu-trustzone-ns/example.cpp:

Go to the source code of this file.

Classes

class  PlatformEventHandler
class  MyTask< _AccessMode >
struct  MyTask< _AccessMode >::TaskPrivateData

Functions

void NSC_OnExitNs (void)
uint32_t NSC_GetKey (uint8_t key[], uint32_t size)
void NSC_GetBoardUID (pico_unique_board_id_t *id_out)
void NSC_bsp_Led_SwitchOnExclusive (bsp::Led::Id led)
void runtime_init_clocks (void)
void pico_get_unique_board_id (pico_unique_board_id_t *id_out)
static stk::Word s_LedTaskStackMem[4][TASK_STACK_MEMORY_SIZE] __stk_aligned (32U)
static STK_MPU_SHARED_DATA_SECTION stk::sync::EventFlags g_TaskFlags (FLAGS_ALL[LED_RED])
STK_MPU_SHARED_CODE_SECTION void SetBreakoutTask (stk::ITask *tsk)
static void InitMpuSharedData ()
 PICO_RUNTIME_INIT_FUNC (InitMpuSharedData, "00100")
void RunExample ()

Variables

static constexpr size_t TASK_STACK_SIZE = 256U
static const uint32_t FLAGS_ALL []
const size_t TASK_STACK_MEMORY_SIZE = stk::Align<size_t>(TASK_STACK_SIZE, 32U)
static constexpr size_t TASK_MEMORY_SIZE = stk::Align<size_t>(10U, 32U)
static STK_MPU_SHARED_DATA_SECTION stk::Ticks g_Timeline = 0
static STK_MPU_SHARED_DATA_SECTION stk::ITask * g_BreakoutTask = nullptr

Detailed Description

TrustZone + per-task MPU sandboxing, Non-Secure side.

Definition in file blinky_mpu-trustzone-ns/example.cpp.

Function Documentation

◆ __stk_aligned()

stk::Word s_LedTaskStackMem[4][TASK_STACK_MEMORY_SIZE] __stk_aligned ( 32U )
static

◆ g_TaskFlags()

◆ InitMpuSharedData()

void InitMpuSharedData ( )
static
Examples
blinky_mpu-hard/example.cpp, blinky_mpu-trustzone-ns/example.cpp, blinky_mpu-trustzone-sc/example.cpp, and blinky_mpu_armv8/example.cpp.

Definition at line 132 of file blinky_mpu-trustzone-ns/example.cpp.

133{
134 extern char __stk_mpu_shared_data_start[];
135 extern char __stk_mpu_shared_data_end[];
136 extern char __stk_mpu_shared_data_source__[];
137 extern char __stk_mpu_shared_bss_start[];
138 extern char __stk_mpu_shared_bss_end[];
139
140 // copy initialized variables from FLASH to RAM
141 const size_t data_size = __stk_mpu_shared_data_end - __stk_mpu_shared_data_start;
142 if (data_size > 0)
143 {
144 STK_MEMCPY(__stk_mpu_shared_data_start, __stk_mpu_shared_data_source__, data_size);
145 }
146
147 // zero-fill the BSS region
148 const size_t bss_size = __stk_mpu_shared_bss_end - __stk_mpu_shared_bss_start;
149 if (bss_size > 0)
150 {
151 STK_MEMSET(__stk_mpu_shared_bss_start, 0, bss_size);
152 }
153}
void STK_MEMCPY(void *const dest, const void *const src, size_t size)
Implementation of STK_MEMCPY.
Definition stk_arch.h:788
void STK_MEMSET(void *const dest, uint8_t value, size_t size)
Implementation of STK_MEMSET.
Definition stk_arch.h:823

Referenced by __attribute__(), and PICO_RUNTIME_INIT_FUNC().

Here is the caller graph for this function:

◆ NSC_bsp_Led_SwitchOnExclusive()

void NSC_bsp_Led_SwitchOnExclusive ( bsp::Led::Id led)

Definition at line 115 of file blinky-trustzone-sc/example.cpp.

116{
117 // You can call hardware directly now, because execution is happening in a
118 // Secure world and Non-Secure task is Privileged.
120 {
122 }
123 else
124 {
125 // If Non-Secure task is not Privileged - direct hw calls are not possible, let Secure
126 // Privileged task handle all hw calls for such calling contexts.
127 s_HwCmdQueue.Write({ .id = HwCommand::CMD_LED_ON, led });
128 }
129}
bool IsPrivilegedContext()
Check if caller context is Privileged.
static void SwitchOnExclusive(Id led)
Definition led.h:48
static sync::PipeT< HwCommand, 4 > s_HwCmdQueue

References __stk_tz_nsc_entry, HwCommand::CMD_LED_ON, stk::hw::IsPrivilegedContext(), s_HwCmdQueue, and bsp::Led::SwitchOnExclusive().

Referenced by MyTask< _AccessMode >::Run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ NSC_GetBoardUID()

void NSC_GetBoardUID ( pico_unique_board_id_t * id_out)

Definition at line 87 of file blinky-trustzone-sc/example.cpp.

88{
90}
void pico_get_unique_board_id(pico_unique_board_id_t *id_out)

References __stk_tz_nsc_entry, and pico_get_unique_board_id().

Referenced by pico_get_unique_board_id().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ NSC_GetKey()

uint32_t NSC_GetKey ( uint8_t key[],
uint32_t size )

Definition at line 93 of file blinky-trustzone-sc/example.cpp.

94{
95 uint32_t result = 0;
96
97 // Verify the entire buffer lies in Non-Secure memory and is writable
98 if (cmse_check_address_range(key, size, CMSE_NONSECURE | CMSE_MPU_READWRITE) != nullptr)
99 {
100 if (size == 4)
101 {
102 key[0] = 1;
103 key[1] = 2;
104 key[2] = 3;
105 key[3] = 4;
106
107 result = 4;
108 }
109 }
110
111 return result;
112}

References __stk_tz_nsc_entry.

Referenced by RunExample().

Here is the caller graph for this function:

◆ NSC_OnExitNs()

void NSC_OnExitNs ( void )

Definition at line 79 of file blinky-trustzone-sc/example.cpp.

80{
81 // Jump back to the point right before BLXNS happened,
82 // passing '1' as the return value for setjmp.
83 longjmp(s_NsExitEnv, 1);
84}
static jmp_buf s_NsExitEnv

References __stk_tz_nsc_entry, and s_NsExitEnv.

Referenced by RunExample().

Here is the caller graph for this function:

◆ pico_get_unique_board_id()

void pico_get_unique_board_id ( pico_unique_board_id_t * id_out)

Definition at line 72 of file blinky_mpu-trustzone-ns/example.cpp.

73{
74 // you can get Id via NSC call, if needed for a Non-Secure side of the binary
75 //*id_out = pico_unique_board_id_t{};
76
77 NSC_GetBoardUID(id_out);
78}
void NSC_GetBoardUID(pico_unique_board_id_t *id_out)

References NSC_GetBoardUID().

Here is the call graph for this function:

◆ PICO_RUNTIME_INIT_FUNC()

PICO_RUNTIME_INIT_FUNC ( InitMpuSharedData ,
"00100"  )

◆ RunExample()

void RunExample ( void )

Definition at line 450 of file blinky_mpu-trustzone-ns/example.cpp.

451{
452 using namespace stk;
453
454 uint8_t key[4] = {0};
455 NSC_GetKey(key, 4);
456
457 // make sure memory is enough
459
460 // Non-Secure tasks
461 MyTask *led_task1 = new (s_LedTaskMem[0])
462 MyTask(LED_RED, s_LedTaskStackMem[0], STK_STATIC_ARRAY_SIZE(s_LedTaskStackMem[0]), ACCESS_USER);
463 MyTask *led_task2 = new (s_LedTaskMem[1])
464 MyTask(LED_ORANGE, s_LedTaskStackMem[1], STK_STATIC_ARRAY_SIZE(s_LedTaskStackMem[1]), ACCESS_USER);
465 MyTask *led_task3 = new (s_LedTaskMem[2])
466 MyTask(LED_GREEN, s_LedTaskStackMem[2], STK_STATIC_ARRAY_SIZE(s_LedTaskStackMem[2]), ACCESS_USER); // you can set to ACCESS_PRIVILEGED too, see NSC_bsp_Led_SwitchOnExclusive on secure side, but for smooth performance on RP2350 let background thread handle GPIO
467 MyTask *led_task4 = new (s_LedTaskMem[3])
468 MyTask(LED_BLUE, s_LedTaskStackMem[3], STK_STATIC_ARRAY_SIZE(s_LedTaskStackMem[3]), ACCESS_USER);
469
470 SetBreakoutTask(led_task4);
471
472 static tz::nsec::Kernel kernel;
473
474 // Background Flash region (see PlatformEventHandler::OnConfigureMpu above); no general-RAM
475 // fallback, so each task's isolation comes entirely from its own GetMpuRegions().
476 static PlatformEventHandler event_overrider;
477 kernel.GetPlatform()->SetEventOverrider(&event_overrider);
478
479 kernel.Initialize(0);
480
481 // Register threads (tasks). Each task's MPU regions (stack guard + its own
482 // TaskPrivateData, via GetMpuRegions() above) are latched in here.
483 kernel.AddTask(led_task1);
484 kernel.AddTask(led_task2);
485 kernel.AddTask(led_task3);
486 kernel.AddTask(led_task4);
487
488 // Start scheduler (it will start threads added by AddTask), execution in main() will be blocked on this line.
489 kernel.Start();
490
491 // Return back to Secure binary. Note: kernel.Start() will exit only if Secure side initialized Kernel instance
492 // with KERNEL_DYNAMIC mode and when all tasks exited on both sides.
493 NSC_OnExitNs();
494}
@ LED_ORANGE
Definition led.h:18
@ LED_GREEN
Definition led.h:17
@ LED_BLUE
Definition led.h:20
@ LED_RED
Definition led.h:19
#define STK_STATIC_ARRAY_SIZE(ARRAY)
Get size of the static array.
Definition stk_defs.h:742
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Definition stk_defs.h:560
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:206
@ ACCESS_USER
Unprivileged access mode (access to some hardware is restricted, see CPU manual for details)....
Definition stk_common.h:37
static constexpr size_t TASK_MEMORY_SIZE
void NSC_OnExitNs(void)
uint32_t NSC_GetKey(uint8_t key[], uint32_t size)
STK_MPU_SHARED_CODE_SECTION void SetBreakoutTask(stk::ITask *tsk)

References stk::ACCESS_USER, LED_BLUE, LED_GREEN, LED_ORANGE, LED_RED, NSC_GetKey(), NSC_OnExitNs(), SetBreakoutTask(), STK_STATIC_ARRAY_SIZE, STK_STATIC_ASSERT, and TASK_MEMORY_SIZE.

Here is the call graph for this function:

◆ runtime_init_clocks()

void runtime_init_clocks ( void )

Definition at line 67 of file blinky_mpu-trustzone-ns/example.cpp.

68{
69 // clocks are initialized on Secure side of the binary
70}

◆ SetBreakoutTask()

STK_MPU_SHARED_CODE_SECTION void SetBreakoutTask ( stk::ITask * tsk)
Examples
blinky_mpu-trustzone-ns/example.cpp.

Definition at line 112 of file blinky_mpu-trustzone-ns/example.cpp.

113{
114 g_BreakoutTask = tsk;
115}
static STK_MPU_SHARED_DATA_SECTION stk::ITask * g_BreakoutTask

References g_BreakoutTask, and STK_MPU_SHARED_CODE_SECTION.

Referenced by RunExample().

Here is the caller graph for this function:

Variable Documentation

◆ FLAGS_ALL

const uint32_t FLAGS_ALL[]
static
Initial value:
= {
(1U << LED_RED),
(1U << LED_ORANGE),
(1U << LED_GREEN),
(1U << LED_BLUE)
}

Definition at line 89 of file blinky_mpu-trustzone-ns/example.cpp.

89 {
90 (1U << LED_RED),
91 (1U << LED_ORANGE),
92 (1U << LED_GREEN),
93 (1U << LED_BLUE)
94};

◆ g_BreakoutTask

◆ g_Timeline

STK_MPU_SHARED_DATA_SECTION stk::Ticks g_Timeline = 0
static

Definition at line 108 of file blinky_mpu-trustzone-ns/example.cpp.

◆ TASK_MEMORY_SIZE

size_t TASK_MEMORY_SIZE = stk::Align<size_t>(10U, 32U)
staticconstexpr

Definition at line 101 of file blinky_mpu-trustzone-ns/example.cpp.

◆ TASK_STACK_MEMORY_SIZE

const size_t TASK_STACK_MEMORY_SIZE = stk::Align<size_t>(TASK_STACK_SIZE, 32U)

Definition at line 97 of file blinky_mpu-trustzone-ns/example.cpp.

◆ TASK_STACK_SIZE

size_t TASK_STACK_SIZE = 256U
staticconstexpr

Definition at line 85 of file blinky_mpu-trustzone-ns/example.cpp.