#include <setjmp.h>
#include <stdio.h>
#include <pico/runtime.h>
#include <pico/stdio.h>
#include <pico/unique_id.h>
#include <arch/arm/cortex-m/stk_arch_arm-tz.h>
#include "example.h"
#if (__ARM_FEATURE_CMSE & 1) == 0
#error "Need ARMv8-M security extensions"
#elif (__ARM_FEATURE_CMSE & 2) == 0
#error "Compile with --mcmse"
#endif
#ifdef _PICO_H
#else
#endif
#define TASK_NS_COUNT (4U)
#define TASK_S_COUNT (1U)
#define TASK_COUNT (TASK_NS_COUNT + TASK_S_COUNT)
#define KERNEL_TYPE KERNEL_STATIC
{
{
};
};
{
}
{
}
{
uint32_t result = 0;
if (cmse_check_address_range(key, size, CMSE_NONSECURE | CMSE_MPU_READWRITE) != nullptr)
{
if (size == 4)
{
key[0] = 1;
key[1] = 2;
key[2] = 3;
key[3] = 4;
result = 4;
}
}
return result;
}
{
{
}
else
{
}
}
{
extern char __nsc_start[];
extern char __nsc_end[];
extern char __ns_ram_start[];
extern char __ns_flash_start[];
extern char __ns_flash_end[];
extern char __ns_scratch_start[];
extern char __ns_scratch_end[];
SAU->CTRL = 0U;
SAU->RNR = 0U;
SAU->RBAR = (uintptr_t)__nsc_start;
SAU->RLAR = (((uintptr_t)__nsc_end - 1U) & ~0x1FU) | 2U | 1U;
SAU->RNR = 1U;
SAU->RBAR = (uintptr_t)__ns_flash_start;
SAU->RLAR = (((uintptr_t)__ns_flash_end - 1U) & ~0x1FU) | 0U | 1U;
SAU->RNR = 2U;
SAU->RBAR = (uintptr_t)__ns_ram_start;
SAU->RLAR = (((uintptr_t)__ns_scratch_start - 1U) & ~0x1FU) | 0U | 1U;
SAU->RNR = 3U;
SAU->RBAR = (uintptr_t)__ns_scratch_start;
SAU->RLAR = (((uintptr_t)__ns_scratch_end - 1U) & ~0x1FU) | 0U | 1U;
for (uint32_t i = 4U; i < 8U; ++i)
{
SAU->RNR = i;
SAU->RBAR = 0U;
SAU->RLAR = 0U;
}
SAU->CTRL = 1U;
__DSB();
__ISB();
}
{
SCB->SHCSR |= SCB_SHCSR_SECUREFAULTENA_Msk |
SCB_SHCSR_BUSFAULTENA_Msk |
SCB_SHCSR_USGFAULTENA_Msk |
SCB_SHCSR_MEMFAULTENA_Msk;
}
{
extern char __ns_flash_start[];
__TZ_set_PSP_NS(0);
__set_PSP(0);
uint32_t *ns_vector_table = reinterpret_cast<uint32_t *>(__ns_flash_start);
uint32_t initial_msp_ns = ns_vector_table[0];
__TZ_set_MSP_NS(initial_msp_ns);
SCB_NS->VTOR = reinterpret_cast<uintptr_t>(__ns_flash_start);
__DSB();
__ISB();
uint32_t reset_handler_address = ns_vector_table[1];
NSFuncT ResetHandler_ns = reinterpret_cast<NSFuncT>(reset_handler_address & ~1UL);
{
ResetHandler_ns();
}
else
{
__TZ_set_PSP_NS(0);
}
}
template <stk::EAccessMode _AccessMode>
{
private:
{
while (true)
{
HwCommand cmd;
{
{
break;
default:
break;
}
}
}
}
};
{
#ifdef DEBUG
{
#ifdef DEBUG
{
printf("\r\n================ MEMMANAGE FAULT DETECTED ================\r\n");
printf("(Sandbox Violation: a task's Run() reached outside the MPU\r\n");
printf(" regions granted to it - see region dump below for what\r\n");
printf(" WAS active for this task at fault time)\r\n");
}
else
{
printf("\r\n================== HARD FAULT DETECTED ===================\r\n");
}
printf("ITask *: 0x%08X:\r\n", (unsigned int)tid);
printf(
"EXC_RETURN: 0x%08X\r\n\r\n", (
unsigned int)ctx->
EXC_RETURN);
printf("--- Stacked CPU Registers ---\r\n");
printf(
"R0: 0x%08X R1: 0x%08X R2: 0x%08X\r\n", (
unsigned int)ctx->
frame.
R0, (
unsigned int)ctx->
frame.
R1, (
unsigned int)ctx->
frame.
R2);
printf(
"R3: 0x%08X R12: 0x%08X LR: 0x%08X\r\n", (
unsigned int)ctx->
frame.
R3, (
unsigned int)ctx->
frame.
R12, (
unsigned int)ctx->
frame.
LR);
printf(
"PC: 0x%08X xPSR: 0x%08X\r\n\r\n", (
unsigned int)ctx->
frame.
PC, (
unsigned int)ctx->
frame.
xPSR);
printf("--- System Control Registers ---\r\n");
printf(
"CFSR: 0x%08X HFSR: 0x%08X AFSR: 0x%08X\r\n", (
unsigned int)ctx->
CFSR, (
unsigned int)ctx->
HFSR, (
unsigned int)ctx->
AFSR);
printf(
"MMFAR: 0x%08X (%s)\r\n", (
unsigned int)ctx->
MMFAR, ctx->
mmfar_valid ?
"VALID" :
"INVALID");
printf(
"BFAR: 0x%08X (%s)\r\n\r\n", (
unsigned int)ctx->
BFAR, ctx->
bfar_valid ?
"VALID" :
"INVALID");
printf(
"CONTROL: 0x%08X (nPRIV=%u)\r\n", (
unsigned int)ctx->
CONTROL, (
unsigned int)(ctx->
CONTROL & 1U));
#if STK_MPU
#if STK_ARCH_ARMV8_M && STK_TZ_SECURE
printf("\r\n");
PrintMpuConfig("Non-Secure", ctx->mpu_ns);
#endif
#endif
printf("=====================================================\r\n");
#endif
return false;
}
#endif
};
{
tz::sec::SetKernel(0, &kernel);
}
{
#ifdef DEBUG
stdio_init_all();
#endif
while (true)
{
#if (KERNEL_TYPE == KERNEL_STATIC)
#endif
}
}
#define __stk_tz_ns_call
ARM TrustZone: attribute for calling Non-Secure functions from Secure state.
#define __stk_tz_nsc_entry
ARM TrustZone: attribute for Non-Secure callable gateway functions.
#define STK_ARCH_ARMV8_M
ARMv8-M platform (Cortex-M23, Cortex-M33, Cortex-M55, Cortex-M85).
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_TZ_SECURE
ARM TrustZone: Defines Secure (1) build.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
static void __stk_debug_break()
Collection of synchronization primitives (stk::sync namespace).
Collection of time-related primitives (stk::time namespace).
Namespace of STK package.
uintptr_t Word
Native processor word type.
EHwException
Hardware exception id (see IPlatform::IEventOverrider::OnException).
@ HW_EXCEPT_MEMACCESS
MemManage on ARM / Page Fault or PMP violation on RISC-V.
Word TId
Task (thread) id.
@ KERNEL_TICKLESS
Tickless mode. To use this mode STK_TICKLESS_IDLE must be defined to 1 in stk_config....
@ KERNEL_SYNC
Synchronization support (see Event).
bool IsPrivilegedContext()
Check if caller context is Privileged.
Word BFAR
BusFault Address Register (BFAR).
Word AFSR
Auxiliary Fault Status Register (AFSR).
Word HFSR
HardFault Status Register (HFSR).
hw::ExceptionFrame frame
Saved hardware exception stack frame.
Word MMFAR
Memory Management Fault Address Register (MMFAR).
Word CFSR
Configurable Fault Status Register (CFSR: MemManage, BusFault, UsageFault).
Word CONTROL
Core CONTROL register state snapshot.
bool mmfar_valid
Flag indicating validity of MMFAR address value (derived from CFSR.MMARVALID).
Word EXC_RETURN
Exception return magic value (EXC_RETURN).
bool bfar_valid
Flag indicating validity of BFAR address value (derived from CFSR.BFARVALID).
Concrete implementation of IKernel.
EAddResult AddTask(ITask *user_task) override
Register task for a soft real-time (SRT) scheduling.
IPlatform * GetPlatform() override
Get platform driver instance owned by this kernel.
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT) override
Initialize kernel.
virtual void SetEventOverrider(IEventOverrider *overrider, bool non_secure=false)=0
Set platform event overrider.
Interface for a platform event overrider.
Partial implementation of the user task.
Thread-safe, type-safe FIFO communication pipe with internal storage.
void Run() override
Entry point of the user task.
bool OnException(stk::EHwException exc_id, stk::TId tid, const struct stk::FaultContext *const ctx) override
static void InitAll(bool init_state)
static void SwitchOnExclusive(Id led)
void NSC_GetBoardUID(pico_unique_board_id_t *id_out)
uint32_t NSC_GetKey(uint8_t key[], uint32_t size)
void NSC_bsp_Led_SwitchOnExclusive(bsp::Led::Id led)
void pico_get_unique_board_id(pico_unique_board_id_t *id_out)
static jmp_buf s_NsExitEnv
static void CreateKernel()
static void InvokeNonSecureState()
static sync::PipeT< HwCommand, 4 > s_HwCmdQueue
static void Configure_SAU(void)
static void ConfigureSecureState()
static STK_MPU_SHARED_DATA_SECTION stk::sync::PipeT< HwCommand, 4 > s_HwCmdQueue